@coda-console/codex 1.12.0 → 1.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent/errors.mjs +52 -5
- package/package.json +1 -1
package/agent/errors.mjs
CHANGED
|
@@ -4,22 +4,70 @@ export function explainCodexError(message) {
|
|
|
4
4
|
if (!message || typeof message !== 'object' || !('id' in message) || 'method' in message) return message;
|
|
5
5
|
const error = message.error;
|
|
6
6
|
if (!error || typeof error.message !== 'string') return message;
|
|
7
|
-
const
|
|
7
|
+
const resetAt = error.data?._meta?.coda?.failure?.resetAt;
|
|
8
|
+
const readable = text => resetAt ? text.replace(resetText, (_, prefix, time) => `${prefix}<t:${time}>`) : text;
|
|
9
|
+
const parts = [readable(error.message)];
|
|
8
10
|
for (const value of [error.data?.message, error.data?.details, error.data]) {
|
|
9
|
-
const detail = typeof value === 'string' ? value.trim() : '';
|
|
11
|
+
const detail = typeof value === 'string' ? readable(value.trim()) : '';
|
|
10
12
|
if (detail && !parts.some(part => part.includes(detail))) parts.push(detail);
|
|
11
13
|
}
|
|
12
|
-
|
|
14
|
+
const explained = parts.join('\n\n');
|
|
15
|
+
return explained === error.message ? message : { ...message, error: { ...error, message: explained } };
|
|
13
16
|
}
|
|
14
17
|
|
|
18
|
+
// Only absolute reset times from the failed request are evidence. The account-wide rateLimits/read can contain several
|
|
19
|
+
// model pools, without identifying which one blocked this turn; do not schedule a retry from an unrelated window.
|
|
20
|
+
const resetText = /\b(try again at |resets (?:at )?)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?Z)\b/gi;
|
|
21
|
+
function quotaReset(error, data) {
|
|
22
|
+
for (const text of [data.additionalDetails, data.message, data.details, error.message]) {
|
|
23
|
+
if (typeof text !== 'string') continue;
|
|
24
|
+
let body; try { body = JSON.parse(text); } catch {}
|
|
25
|
+
// An HTTP failure quotes the response body inside its own sentence: `unexpected status 429 Too Many Requests: {…}, url: …`.
|
|
26
|
+
const seconds = body ? body.error?.resets_at : Number(text.match(/"resets_at"\s*:\s*(\d{1,12})\b/)?.[1]);
|
|
27
|
+
if (Number.isSafeInteger(seconds) && seconds > 0 && seconds <= 253402300799)
|
|
28
|
+
return new Date(seconds * 1000).toISOString();
|
|
29
|
+
const time = [...text.matchAll(resetText)][0]?.[2];
|
|
30
|
+
if (time && Number.isFinite(Date.parse(time)) && new Date(time).toISOString().slice(0, 19) === time.slice(0, 19))
|
|
31
|
+
return new Date(time).toISOString();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// A 429 that Codex reports as a plain HTTP failure carries the provider's response body in its message. Only the provider's
|
|
36
|
+
// own quota codes and wording establish exhaustion; any other 429 is a rate limit, whose "try again in 1.5s" is the wait.
|
|
37
|
+
const providerQuota = /\b(?:insufficient_quota|quota_exceeded|usage_limit_reached)\b|exceeded your current quota|hit your usage limit/i;
|
|
38
|
+
function suggestedWait(text) {
|
|
39
|
+
const spoken = text.match(/try again in ((?:\d+(?:\.\d+)?(?:ms|h|m|s))+)/i)?.[1];
|
|
40
|
+
if (!spoken) return;
|
|
41
|
+
const unit = { h: 3_600_000, m: 60_000, s: 1000, ms: 1 };
|
|
42
|
+
return Math.round([...spoken.matchAll(/(\d+(?:\.\d+)?)(ms|h|m|s)/g)].reduce((sum, [, n, u]) => sum + n * unit[u], 0));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// The ChatGPT login gone (docs/agent-protocol.md「需要重新登录」): Codex's own words when refreshing the access token fails for good
|
|
46
|
+
// (its refresh token expired, was already used or revoked, or the account was logged out or switched). Its `unauthorized` kind and a
|
|
47
|
+
// bare 401 or 403 also cover a wrong API key or a missing permission, which logging in again does not mend, so they stay unmarked.
|
|
48
|
+
const loginLost = /^Your access token could not be refreshed\b[^\n]*\bsign in again\b/;
|
|
49
|
+
|
|
15
50
|
// Native service facts, not a retry policy: Coda decides when/how often to try again. Explicit permanent kinds win over
|
|
16
51
|
// generic messages (in particular, quota failures also arrive as JSON-RPC Internal error).
|
|
17
52
|
export function codexFailureData(error, input) {
|
|
18
53
|
const data = error.data && typeof error.data === 'object' && !Array.isArray(error.data) ? error.data : {};
|
|
19
54
|
const info = data.codexErrorInfo;
|
|
55
|
+
const reported = failure => ({ ...data, _meta: { ...data._meta, coda: { ...data._meta?.coda, failure } } });
|
|
56
|
+
const quota = () => { const resetAt = quotaReset(error, data); return reported({ kind: 'quota_exhausted', ...(resetAt ? { resetAt } : {}) }); };
|
|
57
|
+
if (info === 'usageLimitExceeded') return quota();
|
|
20
58
|
const message = typeof data.message === 'string' ? data.message : typeof data.details === 'string' ? data.details : typeof error.data === 'string' ? error.data : error.message;
|
|
21
59
|
let provider; try { provider = JSON.parse(message); } catch {}
|
|
22
60
|
const status = provider?.status ?? (info && typeof info === 'object' ? Object.values(info)[0]?.httpStatusCode : null);
|
|
61
|
+
if (loginLost.test(message?.trim()))
|
|
62
|
+
return { ...reported({ kind: 'auth_required', login: 'ChatGPT 账号' }), ...(data.message || data.details ? {} : { details: message }) };
|
|
63
|
+
const accepted = data._meta?.coda?.failure?.input === 'accepted' ? 'accepted' : input;
|
|
64
|
+
if (status === 429 && info && typeof info === 'object' && 'httpConnectionFailed' in info) {
|
|
65
|
+
const text = [data.additionalDetails, message].filter(value => typeof value === 'string').join('\n');
|
|
66
|
+
if (providerQuota.test(text)) return quota();
|
|
67
|
+
const retryAfterMs = suggestedWait(text);
|
|
68
|
+
return { ...reported({ kind: 'service_unavailable', input: accepted, ...(retryAfterMs == null ? {} : { retryAfterMs }) }),
|
|
69
|
+
...(data.message || data.details ? {} : { details: error.message }) };
|
|
70
|
+
}
|
|
23
71
|
const permanent = ['usageLimitExceeded', 'unauthorized', 'contextWindowExceeded', 'sessionBudgetExceeded',
|
|
24
72
|
'cyberPolicy', 'misalignmentPolicyViolation', 'sandboxError', 'threadRollbackFailed'].includes(info) || [401, 403].includes(status);
|
|
25
73
|
const unavailableModel = provider?.type === 'error' && status === 400 && provider.error?.type === 'invalid_request_error' &&
|
|
@@ -32,8 +80,7 @@ export function codexFailureData(error, input) {
|
|
|
32
80
|
(info === 'other' && message === 'Selected model is at capacity. Please try a different model.') ||
|
|
33
81
|
(info == null && error.code === -32603 && message?.trim() === 'Internal error'));
|
|
34
82
|
if (!temporary) return data;
|
|
35
|
-
return { ...
|
|
36
|
-
_meta: { ...data._meta, coda: { ...data._meta?.coda, failure: { kind: 'service_unavailable', input: data._meta?.coda?.failure?.input === 'accepted' ? 'accepted' : input } } } };
|
|
83
|
+
return { ...reported({ kind: 'service_unavailable', input: accepted }), ...(data.message || data.details ? {} : { details: error.message }) };
|
|
37
84
|
}
|
|
38
85
|
|
|
39
86
|
// codex-acp 1.12 only throws quota/auth failures for plain ACP clients; other failed native turns look successful.
|