@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 116 lines 4.44 KB
Raw
1 const legacyFailures = {
2 subscription_sharing_v2_user_not_eligible: {
3 message:
4 "ChatGPT sharing is unavailable for this account or workspace. Use another provider or check its sharing policy.",
5 },
6 subscription_sharing_usage_limit_exceeded: {
7 message:
8 "Your ChatGPT usage limit was reached. Check ChatGPT Usage settings for your available allowance.",
9 },
10 subscription_sharing_workplace_individual_usage_limit_exceeded: {
11 message:
12 "You reached your workspace's limit for using ChatGPT in other apps. Ask your workspace admin to increase it.",
13 },
14 subscription_sharing_usage_unavailable: {
15 message: "ChatGPT usage is temporarily unavailable. Try again shortly.",
16 },
17 subscription_sharing_unsupported_capability: {
18 message:
19 "Codex used a feature that ChatGPT sharing does not support. Use another provider for this request.",
20 },
21 subscription_sharing_v2_client_not_enabled: {
22 message:
23 "This app is not enabled for this ChatGPT connection. Use your existing CLI or another provider.",
24 },
25 subscription_sharing_v2_route_not_supported: {
26 message: "ChatGPT does not support this request route.",
27 },
28 subscription_sharing_v2_invalid_user: {
29 message:
30 "ChatGPT could not validate this connection. Check the selected account and sharing permissions.",
31 },
32 subscription_sharing_v2_user_unavailable: {
33 message: "ChatGPT is temporarily unavailable. Try again shortly.",
34 },
35 } as const;
36 const failures = {
37 ...legacyFailures,
38 subscription_sharing_user_not_eligible: legacyFailures.subscription_sharing_v2_user_not_eligible,
39 subscription_sharing_route_not_supported:
40 legacyFailures.subscription_sharing_v2_route_not_supported,
41 subscription_sharing_invalid_user: legacyFailures.subscription_sharing_v2_invalid_user,
42 subscription_sharing_user_unavailable: legacyFailures.subscription_sharing_v2_user_unavailable,
43 chatpass_v2_scope_not_authorized: {
44 message:
45 "This ChatGPT grant does not authorize the request. Check the connection's sharing permissions.",
46 },
47 chatpass_v2_invalid_authorization_context: {
48 message:
49 "ChatGPT could not authorize this connection. Check the client and sharing permissions.",
50 },
51 };
52 const usageLimitCodes = new Set<string>([
53 "subscription_sharing_usage_limit_exceeded",
54 "subscription_sharing_workplace_individual_usage_limit_exceeded",
55 ]);
56
57 function unwrapError(value: unknown): unknown {
58 if (typeof value === "string") {
59 try {
60 value = JSON.parse(value);
61 } catch {
62 return value;
63 }
64 }
65 return typeof value === "object" && value !== null && "error" in value ? value.error : value;
66 }
67
68 function isManagedCode(value: unknown): value is keyof typeof failures {
69 return typeof value === "string" && Object.hasOwn(failures, value);
70 }
71
72 function sharingCode(value: unknown) {
73 const error = unwrapError(value);
74 const code = typeof error === "object" && error !== null && "code" in error ? error.code : error;
75 return isManagedCode(code) ? code : undefined;
76 }
77
78 export function classifyCodexManagedError(value: unknown) {
79 const error = unwrapError(value);
80 const messages =
81 typeof error === "string"
82 ? [error]
83 : typeof error === "object" && error !== null
84 ? [
85 "message" in error ? error.message : null,
86 "additionalDetails" in error ? error.additionalDetails : null,
87 ].filter((message): message is string => typeof message === "string")
88 : [];
89 const code =
90 sharingCode(error) ??
91 messages.map(sharingCode).find((code) => code !== undefined) ??
92 // Codex can stream ChatGPT's sharing-limit prose without its code.
93 (messages.some((message) =>
94 message.includes("has reached their Subscription Sharing usage limit"),
95 )
96 ? "subscription_sharing_usage_limit_exceeded"
97 : undefined);
98 if (code === undefined) return undefined;
99 const failure = { ...failures[code], code, usageLimit: usageLimitCodes.has(code) };
100 const text = messages.join(" ");
101 if (code === "subscription_sharing_unsupported_capability") {
102 if (text.includes("tool 'namespace'"))
103 return {
104 ...failure,
105 message:
106 "Codex sent a tool namespace that ChatGPT sharing does not support. Use another provider for this request.",
107 };
108 if (text.includes("additional_tools"))
109 return {
110 ...failure,
111 message:
112 "Codex sent an input item that ChatGPT sharing does not support. Use another provider for this request.",
113 };
114 }
115 return failure;
116 }