Open History to see the last commit
1const 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;
36const failures = {
37 invalid_encrypted_content: {
38 message:
39 "Codex could not verify this session's encrypted conversation history. Send another message to continue in a fresh session with the visible conversation history.",
40 },
41 ...legacyFailures,
42 subscription_sharing_user_not_eligible: legacyFailures.subscription_sharing_v2_user_not_eligible,
43 subscription_sharing_route_not_supported:
44 legacyFailures.subscription_sharing_v2_route_not_supported,
45 subscription_sharing_invalid_user: legacyFailures.subscription_sharing_v2_invalid_user,
46 subscription_sharing_user_unavailable: legacyFailures.subscription_sharing_v2_user_unavailable,
47 chatpass_v2_scope_not_authorized: {
48 message:
49 "This ChatGPT grant does not authorize the request. Check the connection's sharing permissions.",
50 },
51 chatpass_v2_invalid_authorization_context: {
52 message:
53 "ChatGPT could not authorize this connection. Check the client and sharing permissions.",
54 },
55};
56const usageLimitCodes = new Set<string>([
57 "subscription_sharing_usage_limit_exceeded",
58 "subscription_sharing_workplace_individual_usage_limit_exceeded",
59]);
60
61function unwrapError(value: unknown): unknown {
62 if (typeof value === "string") {
63 try {
64 value = JSON.parse(value);
65 } catch {
66 return value;
67 }
68 }
69 return typeof value === "object" && value !== null && "error" in value ? value.error : value;
70}
71
72function isManagedCode(value: unknown): value is keyof typeof failures {
73 return typeof value === "string" && Object.hasOwn(failures, value);
74}
75
76function sharingCode(value: unknown) {
77 const error = unwrapError(value);
78 const code = typeof error === "object" && error !== null && "code" in error ? error.code : error;
79 return isManagedCode(code) ? code : undefined;
80}
81
82export function classifyCodexManagedError(value: unknown) {
83 const error = unwrapError(value);
84 const messages =
85 typeof error === "string"
86 ? [error]
87 : typeof error === "object" && error !== null
88 ? [
89 "message" in error ? error.message : null,
90 "additionalDetails" in error ? error.additionalDetails : null,
91 ].filter((message): message is string => typeof message === "string")
92 : [];
93 const code =
94 sharingCode(error) ??
95 messages.map(sharingCode).find((code) => code !== undefined) ??
96 // Codex can stream ChatGPT's sharing-limit prose without its code.
97 (messages.some((message) =>
98 message.includes("has reached their Subscription Sharing usage limit"),
99 )
100 ? "subscription_sharing_usage_limit_exceeded"
101 : undefined);
102 if (code === undefined) return undefined;
103 const failure = { ...failures[code], code, usageLimit: usageLimitCodes.has(code) };
104 const text = messages.join(" ");
105 if (code === "subscription_sharing_unsupported_capability") {
106 if (text.includes("tool 'namespace'"))
107 return {
108 ...failure,
109 message:
110 "Codex sent a tool namespace that ChatGPT sharing does not support. Use another provider for this request.",
111 };
112 if (text.includes("additional_tools"))
113 return {
114 ...failure,
115 message:
116 "Codex sent an input item that ChatGPT sharing does not support. Use another provider for this request.",
117 };
118 }
119 return failure;
120}