@nathapp/nax-ai 0.1.11 → 0.1.12
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.
|
@@ -21,16 +21,14 @@ export declare function classifyHttpError(status: number | undefined): ProtocolE
|
|
|
21
21
|
* verdict of its own keeps it — a 429 mentioning tokens is a rate limit, and
|
|
22
22
|
* the caller should wait rather than compact.
|
|
23
23
|
*
|
|
24
|
-
* A non-failing status is refined to `transport` (nax#1869)
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* the
|
|
32
|
-
* `context-overflow`: an overflow always arrives on a failing status, so
|
|
33
|
-
* declining to guess one here still costs nothing.
|
|
24
|
+
* A non-failing status is refined to `transport` (nax#1869), unless the
|
|
25
|
+
* message carries policy or overflow content, in which case that refinement
|
|
26
|
+
* takes over — see `classifyBrokenStream`. That refinement, not the status
|
|
27
|
+
* gate, is what decides `context-overflow`: OpenRouter (and other
|
|
28
|
+
* OpenAI-compatible aggregators) answer HTTP 200 and relay an upstream
|
|
29
|
+
* provider's 4xx as a stream `error` event, so a genuine overflow can arrive
|
|
30
|
+
* on a status that is not itself a failure (nax#44). The status is a signal
|
|
31
|
+
* about the envelope, not a gate on what the message is allowed to mean.
|
|
34
32
|
*/
|
|
35
33
|
export declare function classifyProviderError(status: number | undefined, message: string | undefined): ProtocolErrorKind;
|
|
36
34
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/protocols/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEnE,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,iBAAiB,CAQ/E;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/protocols/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEnE,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,iBAAiB,CAQ/E;AA8GD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,iBAAiB,CAUhH;AA+BD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CASzG;AAyBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAa5D"}
|
package/dist/protocols/errors.js
CHANGED
|
@@ -35,16 +35,72 @@ export function classifyHttpError(status) {
|
|
|
35
35
|
* send a consumer into an endless shorten-and-retry loop.
|
|
36
36
|
*/
|
|
37
37
|
const CONTEXT_OVERFLOW_MARKERS = [
|
|
38
|
+
// Generic and OpenAI-compatible
|
|
38
39
|
"context_length_exceeded",
|
|
39
40
|
"context length exceeded",
|
|
41
|
+
"token limit exceeded",
|
|
40
42
|
"maximum context length",
|
|
41
43
|
"exceed context limit",
|
|
44
|
+
"exceeds the context window",
|
|
45
|
+
"model_context_window_exceeded",
|
|
46
|
+
// Anthropic (token overflow, and the 413 request byte-size form)
|
|
42
47
|
"prompt is too long",
|
|
48
|
+
"request_too_large",
|
|
49
|
+
// Amazon Bedrock
|
|
43
50
|
"input is too long",
|
|
51
|
+
// Google Gemini
|
|
44
52
|
"exceeds the maximum number of tokens",
|
|
53
|
+
// Groq
|
|
45
54
|
"reduce the length of the messages",
|
|
55
|
+
// xAI Grok
|
|
56
|
+
"maximum prompt length is",
|
|
57
|
+
// OpenRouter / Poolside
|
|
58
|
+
"maximum allowed input length",
|
|
59
|
+
// Together AI
|
|
60
|
+
"is longer than the model",
|
|
61
|
+
// llama.cpp server
|
|
62
|
+
"exceeds the available context size",
|
|
63
|
+
// LM Studio
|
|
64
|
+
"greater than the context length",
|
|
65
|
+
// GitHub Copilot
|
|
66
|
+
"prompt token count of",
|
|
67
|
+
// MiniMax
|
|
68
|
+
"context window exceeds limit",
|
|
69
|
+
// Kimi For Coding
|
|
70
|
+
"exceeded model token limit",
|
|
71
|
+
// DS4 server
|
|
72
|
+
"the configured context size is",
|
|
73
|
+
// DashScope / Qwen Token Plan
|
|
74
|
+
"range of input length should be",
|
|
75
|
+
// Ollama
|
|
76
|
+
"exceeded max context length",
|
|
77
|
+
// Deliberately broad, and the reason NON_OVERFLOW_MARKERS exists: Bedrock
|
|
78
|
+
// words a throttle as "ThrottlingException: Too many tokens, please wait".
|
|
46
79
|
"too many tokens",
|
|
47
80
|
];
|
|
81
|
+
/**
|
|
82
|
+
* Substrings that rule a message OUT of `context-overflow`, lowercased,
|
|
83
|
+
* whatever the overflow table says.
|
|
84
|
+
*
|
|
85
|
+
* The overflow table is matched by substring, so a throttling message can
|
|
86
|
+
* satisfy it by accident: Bedrock's "ThrottlingException: Too many tokens,
|
|
87
|
+
* please wait before trying again" contains the `too many tokens` marker
|
|
88
|
+
* verbatim. Classifying that as an overflow would send a consumer off to
|
|
89
|
+
* compact and retry a conversation that was never too large, while the real
|
|
90
|
+
* fault -- a throttle it should have waited out -- goes unreported.
|
|
91
|
+
*
|
|
92
|
+
* Checked before the overflow table, so an exclusion always wins. Status
|
|
93
|
+
* precedence already protects the 429 case; this protects every other status,
|
|
94
|
+
* including the aggregator-relayed 200 of nax#44 and a plain 400.
|
|
95
|
+
*/
|
|
96
|
+
const NON_OVERFLOW_MARKERS = [
|
|
97
|
+
"throttlingexception",
|
|
98
|
+
"throttling error",
|
|
99
|
+
"service unavailable",
|
|
100
|
+
"rate limit",
|
|
101
|
+
"rate_limit",
|
|
102
|
+
"too many requests",
|
|
103
|
+
];
|
|
48
104
|
/**
|
|
49
105
|
* Substrings that identify a mid-stream error carrying policy content,
|
|
50
106
|
* lowercased. Read only for a stream that broke under a non-failing status —
|
|
@@ -82,16 +138,14 @@ function isNonFailingStatus(status) {
|
|
|
82
138
|
* verdict of its own keeps it — a 429 mentioning tokens is a rate limit, and
|
|
83
139
|
* the caller should wait rather than compact.
|
|
84
140
|
*
|
|
85
|
-
* A non-failing status is refined to `transport` (nax#1869)
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* the
|
|
93
|
-
* `context-overflow`: an overflow always arrives on a failing status, so
|
|
94
|
-
* declining to guess one here still costs nothing.
|
|
141
|
+
* A non-failing status is refined to `transport` (nax#1869), unless the
|
|
142
|
+
* message carries policy or overflow content, in which case that refinement
|
|
143
|
+
* takes over — see `classifyBrokenStream`. That refinement, not the status
|
|
144
|
+
* gate, is what decides `context-overflow`: OpenRouter (and other
|
|
145
|
+
* OpenAI-compatible aggregators) answer HTTP 200 and relay an upstream
|
|
146
|
+
* provider's 4xx as a stream `error` event, so a genuine overflow can arrive
|
|
147
|
+
* on a status that is not itself a failure (nax#44). The status is a signal
|
|
148
|
+
* about the envelope, not a gate on what the message is allowed to mean.
|
|
95
149
|
*/
|
|
96
150
|
export function classifyProviderError(status, message) {
|
|
97
151
|
const kind = classifyHttpError(status);
|
|
@@ -100,19 +154,35 @@ export function classifyProviderError(status, message) {
|
|
|
100
154
|
}
|
|
101
155
|
if (kind !== "bad-request" || message === undefined)
|
|
102
156
|
return kind;
|
|
157
|
+
return matchesContextOverflow(message) ? "context-overflow" : kind;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Whether a message names a context-window overflow, case-insensitively.
|
|
161
|
+
*
|
|
162
|
+
* An exclusion wins over a marker: see `NON_OVERFLOW_MARKERS` for why the
|
|
163
|
+
* overflow table alone cannot separate a throttle from an overflow.
|
|
164
|
+
*/
|
|
165
|
+
function matchesContextOverflow(message) {
|
|
103
166
|
const haystack = message.toLowerCase();
|
|
104
|
-
|
|
167
|
+
if (NON_OVERFLOW_MARKERS.some((marker) => haystack.includes(marker)))
|
|
168
|
+
return false;
|
|
169
|
+
return CONTEXT_OVERFLOW_MARKERS.some((marker) => haystack.includes(marker));
|
|
105
170
|
}
|
|
106
171
|
/**
|
|
107
172
|
* The kind for an error event whose status reported no failure: `transport`,
|
|
108
|
-
* unless the message carries policy content nax-ai must not retry itself
|
|
173
|
+
* unless the message carries policy content nax-ai must not retry itself
|
|
174
|
+
* (checked first, so an established rate-limit/overload phrasing keeps its
|
|
175
|
+
* kind), or names a context overflow (nax#44) — the aggregator-relay shape
|
|
176
|
+
* described on `classifyProviderError`.
|
|
109
177
|
*/
|
|
110
178
|
function classifyBrokenStream(message) {
|
|
111
179
|
if (message === undefined)
|
|
112
180
|
return "transport";
|
|
113
181
|
const haystack = message.toLowerCase();
|
|
114
182
|
const policy = STREAM_POLICY_MARKERS.find(([marker]) => haystack.includes(marker));
|
|
115
|
-
|
|
183
|
+
if (policy)
|
|
184
|
+
return policy[1];
|
|
185
|
+
return matchesContextOverflow(message) ? "context-overflow" : "transport";
|
|
116
186
|
}
|
|
117
187
|
/**
|
|
118
188
|
* Reads `retry-after` as a delay in seconds.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/protocols/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,MAAM,UAAU,iBAAiB,CAAC,MAA0B;IAC1D,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,MAAM,CAAC;IACpD,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,YAAY,CAAC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