@vellumai/assistant 0.10.7-dev.202607091507.bac9bfe → 0.10.7-dev.202607091656.6914014
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/package.json +1 -1
- package/src/__tests__/anthropic-error-formatting.test.ts +193 -2
- package/src/__tests__/anthropic-gateway-shared.test.ts +26 -0
- package/src/__tests__/conversation-error.test.ts +200 -0
- package/src/__tests__/gemini-provider.test.ts +140 -1
- package/src/__tests__/memory-identity-context-parity.test.ts +103 -0
- package/src/__tests__/plugin-import-boundary-guard.test.ts +3 -3
- package/src/__tests__/provider-error-scenarios.test.ts +49 -0
- package/src/daemon/approval-generators.ts +17 -11
- package/src/daemon/conversation-error.ts +232 -103
- package/src/daemon/conversation-store.ts +11 -13
- package/src/plugin-api/index.ts +8 -0
- package/src/plugins/defaults/memory/graph/extraction.ts +130 -48
- package/src/plugins/defaults/memory/identity-context.ts +39 -0
- package/src/plugins/defaults/memory/routes/__tests__/memory-v2-simulate-route.test.ts +8 -2
- package/src/plugins/defaults/memory/v2/__tests__/router.test.ts +9 -7
- package/src/plugins/defaults/memory/v2/__tests__/sweep-job.test.ts +6 -0
- package/src/plugins/defaults/memory/v2/router.ts +5 -5
- package/src/plugins/defaults/memory/v2/sweep-job.ts +5 -5
- package/src/providers/__tests__/preflight-resolved-config.test.ts +189 -0
- package/src/providers/anthropic/client.ts +102 -4
- package/src/providers/anthropic-gateway-shared.ts +5 -0
- package/src/providers/connection-resolution.ts +181 -3
- package/src/providers/gemini/client.ts +58 -2
- package/src/providers/openai/__tests__/api-error-normalization.test.ts +98 -0
- package/src/providers/openai/api-error-normalization.ts +61 -0
- package/src/providers/openai/chat-completions-provider.ts +8 -2
- package/src/providers/openai/responses-provider.ts +3 -1
- package/src/providers/provider-availability.ts +23 -1
- package/src/providers/provider-send-message.ts +19 -3
- package/src/providers/retry.ts +18 -1
- package/src/providers/types.ts +7 -7
- package/src/subagent/manager.ts +5 -6
- package/src/util/errors.ts +26 -0
- package/src/util/provider-error-patterns.ts +21 -0
package/package.json
CHANGED
|
@@ -10,13 +10,20 @@ import type { Message } from "../providers/types.js";
|
|
|
10
10
|
class FakeAPIError extends Error {
|
|
11
11
|
status: number | undefined;
|
|
12
12
|
headers: Map<string, string> = new Map();
|
|
13
|
-
|
|
13
|
+
error: unknown;
|
|
14
|
+
constructor(status: number | undefined, message: string, body?: unknown) {
|
|
14
15
|
super(message);
|
|
15
16
|
this.status = status;
|
|
17
|
+
this.error = body;
|
|
16
18
|
this.name = "APIError";
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
/** Anthropic error body shape: `{ type: "error", error: { type, message } }`. */
|
|
23
|
+
function anthropicBody(type: string, message?: string): unknown {
|
|
24
|
+
return { type: "error", error: { type, message: message ?? "" } };
|
|
25
|
+
}
|
|
26
|
+
|
|
20
27
|
let nextThrown: FakeAPIError | null = null;
|
|
21
28
|
|
|
22
29
|
mock.module("@anthropic-ai/sdk", () => ({
|
|
@@ -48,7 +55,9 @@ mock.module("@anthropic-ai/sdk", () => ({
|
|
|
48
55
|
}));
|
|
49
56
|
|
|
50
57
|
import { AnthropicProvider } from "../providers/anthropic/client.js";
|
|
51
|
-
import {
|
|
58
|
+
import { ContextOverflowError } from "../providers/types.js";
|
|
59
|
+
import { createAbortReason } from "../util/abort-reasons.js";
|
|
60
|
+
import { ProviderError, type ProviderErrorReason } from "../util/errors.js";
|
|
52
61
|
|
|
53
62
|
function userMsg(text: string): Message {
|
|
54
63
|
return { role: "user", content: [{ type: "text", text }] };
|
|
@@ -96,3 +105,185 @@ describe("AnthropicProvider — error message formatting (JARVIS-390)", () => {
|
|
|
96
105
|
}
|
|
97
106
|
});
|
|
98
107
|
});
|
|
108
|
+
|
|
109
|
+
describe("AnthropicProvider — semantic reason stamping", () => {
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
nextThrown = null;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
async function reasonFor(error: FakeAPIError): Promise<ProviderError> {
|
|
115
|
+
nextThrown = error;
|
|
116
|
+
const provider = new AnthropicProvider("sk-ant-test", "claude-sonnet-4-6");
|
|
117
|
+
try {
|
|
118
|
+
await provider.sendMessage([userMsg("hi")]);
|
|
119
|
+
throw new Error("expected sendMessage to throw");
|
|
120
|
+
} catch (err) {
|
|
121
|
+
expect(err).toBeInstanceOf(ProviderError);
|
|
122
|
+
return err as ProviderError;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const cases: Array<{
|
|
127
|
+
name: string;
|
|
128
|
+
error: FakeAPIError;
|
|
129
|
+
reason: ProviderErrorReason;
|
|
130
|
+
}> = [
|
|
131
|
+
{
|
|
132
|
+
name: "authentication_error → invalid_credentials",
|
|
133
|
+
error: new FakeAPIError(
|
|
134
|
+
401,
|
|
135
|
+
"invalid x-api-key",
|
|
136
|
+
anthropicBody("authentication_error"),
|
|
137
|
+
),
|
|
138
|
+
reason: "invalid_credentials",
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
name: "generic 403 permission → invalid_credentials",
|
|
142
|
+
error: new FakeAPIError(
|
|
143
|
+
403,
|
|
144
|
+
"Forbidden",
|
|
145
|
+
anthropicBody("permission_error"),
|
|
146
|
+
),
|
|
147
|
+
reason: "invalid_credentials",
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: "403 model/plan restriction → model_restricted",
|
|
151
|
+
error: new FakeAPIError(
|
|
152
|
+
403,
|
|
153
|
+
"Your plan does not have access to this model",
|
|
154
|
+
anthropicBody("permission_error"),
|
|
155
|
+
),
|
|
156
|
+
reason: "model_restricted",
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
name: "generic 403 'not allowed' without model context → invalid_credentials",
|
|
160
|
+
error: new FakeAPIError(
|
|
161
|
+
403,
|
|
162
|
+
"Request not allowed: access denied",
|
|
163
|
+
anthropicBody("permission_error"),
|
|
164
|
+
),
|
|
165
|
+
reason: "invalid_credentials",
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
name: "not_found_error → model_not_found",
|
|
169
|
+
error: new FakeAPIError(
|
|
170
|
+
404,
|
|
171
|
+
"model: claude-nope not found",
|
|
172
|
+
anthropicBody("not_found_error"),
|
|
173
|
+
),
|
|
174
|
+
reason: "model_not_found",
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "non-model 404 (missing gateway resource) → bad_request (defers to legacy fallback)",
|
|
178
|
+
error: new FakeAPIError(404, "Not Found: /v1/messages", undefined),
|
|
179
|
+
reason: "bad_request",
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "rate_limit_error → rate_limited",
|
|
183
|
+
error: new FakeAPIError(
|
|
184
|
+
429,
|
|
185
|
+
"rate limited",
|
|
186
|
+
anthropicBody("rate_limit_error"),
|
|
187
|
+
),
|
|
188
|
+
reason: "rate_limited",
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: "overloaded_error (529) → overloaded",
|
|
192
|
+
error: new FakeAPIError(
|
|
193
|
+
529,
|
|
194
|
+
"Overloaded",
|
|
195
|
+
anthropicBody("overloaded_error"),
|
|
196
|
+
),
|
|
197
|
+
reason: "overloaded",
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: "billing/credits → insufficient_credits",
|
|
201
|
+
error: new FakeAPIError(
|
|
202
|
+
400,
|
|
203
|
+
"Your credit balance is too low to access the API.",
|
|
204
|
+
anthropicBody("invalid_request_error"),
|
|
205
|
+
),
|
|
206
|
+
reason: "insufficient_credits",
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: "5xx → server_error",
|
|
210
|
+
error: new FakeAPIError(503, "internal", anthropicBody("api_error")),
|
|
211
|
+
reason: "server_error",
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
name: "other 4xx → bad_request",
|
|
215
|
+
error: new FakeAPIError(
|
|
216
|
+
400,
|
|
217
|
+
"messages: malformed request",
|
|
218
|
+
anthropicBody("invalid_request_error"),
|
|
219
|
+
),
|
|
220
|
+
reason: "bad_request",
|
|
221
|
+
},
|
|
222
|
+
];
|
|
223
|
+
|
|
224
|
+
for (const c of cases) {
|
|
225
|
+
test(c.name, async () => {
|
|
226
|
+
const err = await reasonFor(c.error);
|
|
227
|
+
expect(err.reason).toBe(c.reason);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
test("stamps apiErrorType from the inner error body", async () => {
|
|
232
|
+
const err = await reasonFor(
|
|
233
|
+
new FakeAPIError(429, "rate limited", anthropicBody("rate_limit_error")),
|
|
234
|
+
);
|
|
235
|
+
expect(err.apiErrorType).toBe("rate_limit_error");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("surfaces the inner human message, not JSON, when the SDK stringifies the body", async () => {
|
|
239
|
+
const inner = "You do not have access to this model on your current plan.";
|
|
240
|
+
// Real SDK behaviour: no top-level message, so error.message is the raw JSON body.
|
|
241
|
+
const body = anthropicBody("permission_error", inner);
|
|
242
|
+
const err = await reasonFor(
|
|
243
|
+
new FakeAPIError(403, JSON.stringify(body), body),
|
|
244
|
+
);
|
|
245
|
+
expect(err.reason).toBe("model_restricted");
|
|
246
|
+
expect(err.message).toContain(inner);
|
|
247
|
+
expect(err.message).not.toContain("{");
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("context overflow → ContextOverflowError with reason context_overflow", async () => {
|
|
251
|
+
nextThrown = new FakeAPIError(
|
|
252
|
+
400,
|
|
253
|
+
"prompt is too long: 250000 tokens > 200000 maximum",
|
|
254
|
+
anthropicBody(
|
|
255
|
+
"invalid_request_error",
|
|
256
|
+
"prompt is too long: 250000 tokens > 200000 maximum",
|
|
257
|
+
),
|
|
258
|
+
);
|
|
259
|
+
const provider = new AnthropicProvider("sk-ant-test", "claude-sonnet-4-6");
|
|
260
|
+
try {
|
|
261
|
+
await provider.sendMessage([userMsg("hi")]);
|
|
262
|
+
throw new Error("expected sendMessage to throw");
|
|
263
|
+
} catch (err) {
|
|
264
|
+
expect(err).toBeInstanceOf(ContextOverflowError);
|
|
265
|
+
const overflow = err as ContextOverflowError;
|
|
266
|
+
expect(overflow.reason).toBe("context_overflow");
|
|
267
|
+
expect(overflow.actualTokens).toBe(250000);
|
|
268
|
+
expect(overflow.maxTokens).toBe(200000);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("does not stamp a reason on caller-aborted requests", async () => {
|
|
273
|
+
const abortReason = createAbortReason("user_cancel", "test");
|
|
274
|
+
const controller = new AbortController();
|
|
275
|
+
controller.abort(abortReason);
|
|
276
|
+
nextThrown = new FakeAPIError(undefined, "Request was aborted.");
|
|
277
|
+
const provider = new AnthropicProvider("sk-ant-test", "claude-sonnet-4-6");
|
|
278
|
+
try {
|
|
279
|
+
await provider.sendMessage([userMsg("hi")], {
|
|
280
|
+
signal: controller.signal,
|
|
281
|
+
});
|
|
282
|
+
throw new Error("expected sendMessage to throw");
|
|
283
|
+
} catch (err) {
|
|
284
|
+
expect(err).toBeInstanceOf(ProviderError);
|
|
285
|
+
expect((err as ProviderError).reason).toBeUndefined();
|
|
286
|
+
expect((err as ProviderError).abortReason).toBe(abortReason);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
});
|
|
@@ -79,6 +79,32 @@ describe("retagDelegateError", () => {
|
|
|
79
79
|
expect(err.cause).toBe(inner);
|
|
80
80
|
});
|
|
81
81
|
|
|
82
|
+
test("carries reason + structured fields across the re-tag", () => {
|
|
83
|
+
const inner = new ProviderError("model restricted", "anthropic", 403, {
|
|
84
|
+
reason: "model_restricted",
|
|
85
|
+
apiErrorType: "permission_error",
|
|
86
|
+
apiErrorCode: "restricted",
|
|
87
|
+
apiErrorParam: "model",
|
|
88
|
+
rawBody: '{"type":"error"}',
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
let caught: unknown;
|
|
92
|
+
try {
|
|
93
|
+
retagDelegateError(inner, providerName);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
caught = error;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
expect(caught).toBeInstanceOf(ProviderError);
|
|
99
|
+
const err = caught as ProviderError;
|
|
100
|
+
expect(err.provider).toBe(providerName);
|
|
101
|
+
expect(err.reason).toBe("model_restricted");
|
|
102
|
+
expect(err.apiErrorType).toBe("permission_error");
|
|
103
|
+
expect(err.apiErrorCode).toBe("restricted");
|
|
104
|
+
expect(err.apiErrorParam).toBe("model");
|
|
105
|
+
expect(err.rawBody).toBe('{"type":"error"}');
|
|
106
|
+
});
|
|
107
|
+
|
|
82
108
|
test("re-tags a delegate ContextOverflowError, preserving token counts", () => {
|
|
83
109
|
const inner = new ContextOverflowError("context overflow", "anthropic", {
|
|
84
110
|
actualTokens: 250_000,
|
|
@@ -827,6 +827,181 @@ describe("classifyConversationError", () => {
|
|
|
827
827
|
});
|
|
828
828
|
});
|
|
829
829
|
|
|
830
|
+
describe("reason-driven classification (ProviderError.reason)", () => {
|
|
831
|
+
it("classifies reason=model_restricted on the skew-safe PROVIDER_API code with a specific errorCategory", () => {
|
|
832
|
+
const err = new ProviderError(
|
|
833
|
+
"Vercel AI Gateway API error (403): Model claude-opus-4 is restricted on your plan [type=no_providers_available]",
|
|
834
|
+
"vercel-ai-gateway",
|
|
835
|
+
403,
|
|
836
|
+
{ reason: "model_restricted" },
|
|
837
|
+
);
|
|
838
|
+
|
|
839
|
+
const result = classifyConversationError(err, baseCtx);
|
|
840
|
+
|
|
841
|
+
// Rides the existing PROVIDER_API code so version-skewed clients still
|
|
842
|
+
// parse the event; the specific signal is on the free-form errorCategory.
|
|
843
|
+
expect(result.code).toBe("PROVIDER_API");
|
|
844
|
+
expect(result.errorCategory).toBe("provider_model_restricted");
|
|
845
|
+
expect(result.retryable).toBe(false);
|
|
846
|
+
expect(result.userMessage).toContain(
|
|
847
|
+
"Model claude-opus-4 is restricted on your plan",
|
|
848
|
+
);
|
|
849
|
+
expect(result.userMessage).toContain("upgrade your plan");
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
it("falls back to the plan sentence without detail when none is extractable", () => {
|
|
853
|
+
const err = new ProviderError(
|
|
854
|
+
"Vercel AI Gateway API error (403): [type=no_providers_available]",
|
|
855
|
+
"vercel-ai-gateway",
|
|
856
|
+
403,
|
|
857
|
+
{ reason: "model_restricted" },
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
const result = classifyConversationError(err, baseCtx);
|
|
861
|
+
|
|
862
|
+
expect(result.code).toBe("PROVIDER_API");
|
|
863
|
+
expect(result.errorCategory).toBe("provider_model_restricted");
|
|
864
|
+
expect(result.userMessage).toContain(
|
|
865
|
+
"This model isn't available on your current provider plan. Switch",
|
|
866
|
+
);
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
it("routes reason=invalid_credentials to MANAGED_KEY_INVALID under managed-proxy", () => {
|
|
870
|
+
providerRoutingSources["vercel-ai-gateway"] = "managed-proxy";
|
|
871
|
+
const err = new ProviderError(
|
|
872
|
+
"Vercel AI Gateway API error (401): unauthorized",
|
|
873
|
+
"vercel-ai-gateway",
|
|
874
|
+
401,
|
|
875
|
+
{ reason: "invalid_credentials" },
|
|
876
|
+
);
|
|
877
|
+
|
|
878
|
+
const result = classifyConversationError(err, baseCtx);
|
|
879
|
+
|
|
880
|
+
expect(result.code).toBe("MANAGED_KEY_INVALID");
|
|
881
|
+
expect(result.errorCategory).toBe("managed_key_invalid");
|
|
882
|
+
expect(result.retryable).toBe(false);
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
it("routes reason=invalid_credentials to PROVIDER_INVALID_KEY under a user key", () => {
|
|
886
|
+
providerRoutingSources.openai = "user-key";
|
|
887
|
+
const err = new ProviderError(
|
|
888
|
+
"OpenAI API error (401): invalid api key",
|
|
889
|
+
"openai",
|
|
890
|
+
401,
|
|
891
|
+
{ reason: "invalid_credentials" },
|
|
892
|
+
);
|
|
893
|
+
|
|
894
|
+
const result = classifyConversationError(err, baseCtx);
|
|
895
|
+
|
|
896
|
+
expect(result.code).toBe("PROVIDER_INVALID_KEY");
|
|
897
|
+
expect(result.errorCategory).toBe("provider_invalid_key");
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
it("overlays reason=rate_limited both ways (managed vs user)", () => {
|
|
901
|
+
providerRoutingSources.openrouter = "managed-proxy";
|
|
902
|
+
const managed = classifyConversationError(
|
|
903
|
+
new ProviderError("rate limited", "openrouter", 429, {
|
|
904
|
+
reason: "rate_limited",
|
|
905
|
+
}),
|
|
906
|
+
baseCtx,
|
|
907
|
+
);
|
|
908
|
+
expect(managed.code).toBe("MANAGED_USAGE_LIMIT");
|
|
909
|
+
expect(managed.errorCategory).toBe("managed_usage_limit");
|
|
910
|
+
|
|
911
|
+
providerRoutingSources.openai = "user-key";
|
|
912
|
+
const user = classifyConversationError(
|
|
913
|
+
new ProviderError("rate limited", "openai", 429, {
|
|
914
|
+
reason: "rate_limited",
|
|
915
|
+
}),
|
|
916
|
+
baseCtx,
|
|
917
|
+
);
|
|
918
|
+
expect(user.code).toBe("PROVIDER_RATE_LIMIT");
|
|
919
|
+
expect(user.errorCategory).toBe("rate_limit");
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
it("classifies reason=rate_limited managed quota bodies as MANAGED_USAGE_LIMIT even without a managed routing source", () => {
|
|
923
|
+
// Per-connection platform auth path leaves routingSource unset, so the
|
|
924
|
+
// managed quota body pattern must still win over PROVIDER_RATE_LIMIT.
|
|
925
|
+
providerRoutingSources.openai = "user-key";
|
|
926
|
+
const result = classifyConversationError(
|
|
927
|
+
new ProviderError(
|
|
928
|
+
'{"code":"daily_quota_exceeded"}',
|
|
929
|
+
"openai",
|
|
930
|
+
429,
|
|
931
|
+
{ reason: "rate_limited" },
|
|
932
|
+
),
|
|
933
|
+
baseCtx,
|
|
934
|
+
);
|
|
935
|
+
expect(result.code).toBe("MANAGED_USAGE_LIMIT");
|
|
936
|
+
expect(result.errorCategory).toBe("managed_usage_limit");
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
it("defers reason=bad_request to the existing status/regex fallback", () => {
|
|
940
|
+
const err = new ProviderError(
|
|
941
|
+
"context_length_exceeded: your prompt is too long",
|
|
942
|
+
"openai",
|
|
943
|
+
400,
|
|
944
|
+
{ reason: "bad_request" },
|
|
945
|
+
);
|
|
946
|
+
|
|
947
|
+
const result = classifyConversationError(err, baseCtx);
|
|
948
|
+
|
|
949
|
+
// Falls through to the 4xx context-too-large branch, unchanged.
|
|
950
|
+
expect(result.code).toBe("CONTEXT_TOO_LARGE");
|
|
951
|
+
expect(result.errorCategory).toBe("context_too_large");
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
it("honors a stamped reason on a statusless ProviderError (no HTTP status)", () => {
|
|
955
|
+
// SDK streaming failures throw with statusCode undefined but can still
|
|
956
|
+
// carry a reason; it must classify semantically, not fall to the fallback.
|
|
957
|
+
const err = new ProviderError("stream failed", "openai", undefined, {
|
|
958
|
+
reason: "insufficient_credits",
|
|
959
|
+
});
|
|
960
|
+
const result = classifyConversationError(err, baseCtx);
|
|
961
|
+
expect(result.code).toBe("PROVIDER_BILLING");
|
|
962
|
+
expect(result.errorCategory).toBe("provider_billing");
|
|
963
|
+
});
|
|
964
|
+
|
|
965
|
+
it("keeps reason-less ProviderErrors on the legacy status path", () => {
|
|
966
|
+
const err = new ProviderError("Unauthorized", "anthropic", 401);
|
|
967
|
+
const result = classifyConversationError(err, baseCtx);
|
|
968
|
+
expect(result.code).toBe("PROVIDER_INVALID_KEY");
|
|
969
|
+
expect(result.errorCategory).toBe("provider_invalid_key");
|
|
970
|
+
});
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
describe("reason-less fallback stays functional", () => {
|
|
974
|
+
it("classifies a reason-less network Error via the regex battery", () => {
|
|
975
|
+
const result = classifyConversationError(
|
|
976
|
+
new Error("ECONNREFUSED"),
|
|
977
|
+
baseCtx,
|
|
978
|
+
);
|
|
979
|
+
expect(result.code).toBe("PROVIDER_NETWORK");
|
|
980
|
+
expect(result.errorCategory).toBe("provider_network");
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
it("classifies a reason-less ProviderError 500 via the status switch", () => {
|
|
984
|
+
const err = new ProviderError("Internal server error", "openai", 500);
|
|
985
|
+
const result = classifyConversationError(err, baseCtx);
|
|
986
|
+
expect(result.code).toBe("PROVIDER_API");
|
|
987
|
+
expect(result.errorCategory).toBe("provider_server_error");
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
it("yields the same classification for a stamped reason and its reason-less twin", () => {
|
|
991
|
+
const withReason = classifyConversationError(
|
|
992
|
+
new ProviderError("boom", "openai", 500, { reason: "server_error" }),
|
|
993
|
+
baseCtx,
|
|
994
|
+
);
|
|
995
|
+
const withoutReason = classifyConversationError(
|
|
996
|
+
new ProviderError("boom", "openai", 500),
|
|
997
|
+
baseCtx,
|
|
998
|
+
);
|
|
999
|
+
expect(withReason.code).toBe(withoutReason.code);
|
|
1000
|
+
expect(withReason.errorCategory).toBe(withoutReason.errorCategory);
|
|
1001
|
+
expect(withReason.userMessage).toBe(withoutReason.userMessage);
|
|
1002
|
+
});
|
|
1003
|
+
});
|
|
1004
|
+
|
|
830
1005
|
describe("debug detail truncation", () => {
|
|
831
1006
|
it("truncates debugDetails longer than 4000 chars", () => {
|
|
832
1007
|
const longMsg = "x".repeat(5000);
|
|
@@ -1098,6 +1273,31 @@ describe("ConnectionResolutionError classification", () => {
|
|
|
1098
1273
|
expect(result.userMessage).toContain("Restart the assistant");
|
|
1099
1274
|
});
|
|
1100
1275
|
|
|
1276
|
+
it("classifies missing_credential naming the connection and fix", () => {
|
|
1277
|
+
const err = new ConnectionResolutionError(
|
|
1278
|
+
"anthropic-personal",
|
|
1279
|
+
"missing_credential",
|
|
1280
|
+
"no key",
|
|
1281
|
+
{ profileName: "custom-fast" },
|
|
1282
|
+
);
|
|
1283
|
+
const result = classifyConversationError(err, errCtx);
|
|
1284
|
+
expect(result.code).toBe("PROVIDER_NOT_CONFIGURED");
|
|
1285
|
+
expect(result.userMessage).toContain('"anthropic-personal"');
|
|
1286
|
+
expect(result.userMessage).toContain("no stored credential");
|
|
1287
|
+
expect(result.userMessage).toContain('profile "custom-fast"');
|
|
1288
|
+
});
|
|
1289
|
+
|
|
1290
|
+
it("classifies platform_unauthenticated with a log-in fix", () => {
|
|
1291
|
+
const err = new ConnectionResolutionError(
|
|
1292
|
+
"vellum",
|
|
1293
|
+
"platform_unauthenticated",
|
|
1294
|
+
"not logged in",
|
|
1295
|
+
);
|
|
1296
|
+
const result = classifyConversationError(err, errCtx);
|
|
1297
|
+
expect(result.userMessage).toContain("platform login");
|
|
1298
|
+
expect(result.userMessage).toContain("Log in");
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1101
1301
|
it("is a structured VellumError (ConfigError) for logging/monitoring", () => {
|
|
1102
1302
|
const err = new ConnectionResolutionError("x", "not_found", "m");
|
|
1103
1303
|
expect(err).toBeInstanceOf(VellumError);
|
|
@@ -6,8 +6,9 @@ import type {
|
|
|
6
6
|
ProviderEvent,
|
|
7
7
|
ToolDefinition,
|
|
8
8
|
} from "../providers/types.js";
|
|
9
|
+
import { ContextOverflowError } from "../providers/types.js";
|
|
9
10
|
import { createAbortReason } from "../util/abort-reasons.js";
|
|
10
|
-
import { ProviderError } from "../util/errors.js";
|
|
11
|
+
import { ProviderError, type ProviderErrorReason } from "../util/errors.js";
|
|
11
12
|
|
|
12
13
|
// ---------------------------------------------------------------------------
|
|
13
14
|
// Mock @google/genai module — must be before importing the provider
|
|
@@ -1389,6 +1390,144 @@ describe("GeminiProvider", () => {
|
|
|
1389
1390
|
}
|
|
1390
1391
|
});
|
|
1391
1392
|
|
|
1393
|
+
// -----------------------------------------------------------------------
|
|
1394
|
+
// Error reason mapping
|
|
1395
|
+
// -----------------------------------------------------------------------
|
|
1396
|
+
async function reasonForApiError(
|
|
1397
|
+
status: number,
|
|
1398
|
+
message: string,
|
|
1399
|
+
): Promise<ProviderErrorReason | undefined> {
|
|
1400
|
+
shouldThrow = new FakeApiError(status, message);
|
|
1401
|
+
try {
|
|
1402
|
+
await provider.sendMessage([
|
|
1403
|
+
{ role: "user", content: [{ type: "text", text: "Hi" }] },
|
|
1404
|
+
]);
|
|
1405
|
+
throw new Error("expected sendMessage to throw");
|
|
1406
|
+
} catch (error) {
|
|
1407
|
+
expect(error).toBeInstanceOf(ProviderError);
|
|
1408
|
+
return (error as ProviderError).reason;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
test("maps 401 / UNAUTHENTICATED to invalid_credentials", async () => {
|
|
1413
|
+
expect(await reasonForApiError(401, "API key not valid")).toBe(
|
|
1414
|
+
"invalid_credentials",
|
|
1415
|
+
);
|
|
1416
|
+
expect(await reasonForApiError(0, "UNAUTHENTICATED: bad key")).toBe(
|
|
1417
|
+
"invalid_credentials",
|
|
1418
|
+
);
|
|
1419
|
+
});
|
|
1420
|
+
|
|
1421
|
+
test("maps a plan/model-restricted 403 to model_restricted", async () => {
|
|
1422
|
+
expect(
|
|
1423
|
+
await reasonForApiError(
|
|
1424
|
+
403,
|
|
1425
|
+
"PERMISSION_DENIED: Gemini API model gemini-3-pro is not available on your billing plan",
|
|
1426
|
+
),
|
|
1427
|
+
).toBe("model_restricted");
|
|
1428
|
+
});
|
|
1429
|
+
|
|
1430
|
+
test("maps a generic 403 with no model signal to invalid_credentials", async () => {
|
|
1431
|
+
expect(
|
|
1432
|
+
await reasonForApiError(
|
|
1433
|
+
403,
|
|
1434
|
+
"PERMISSION_DENIED: The caller does not have permission",
|
|
1435
|
+
),
|
|
1436
|
+
).toBe("invalid_credentials");
|
|
1437
|
+
});
|
|
1438
|
+
|
|
1439
|
+
test("maps an IAM 403 on a models/* resource to invalid_credentials, not model_restricted", async () => {
|
|
1440
|
+
expect(
|
|
1441
|
+
await reasonForApiError(
|
|
1442
|
+
403,
|
|
1443
|
+
"PERMISSION_DENIED: Permission 'generativelanguage.models.generateContent' denied on resource //generativelanguage.googleapis.com/models/gemini-2.5-pro",
|
|
1444
|
+
),
|
|
1445
|
+
).toBe("invalid_credentials");
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
test("maps a generic project/billing setup 403 to invalid_credentials, not model_restricted", async () => {
|
|
1449
|
+
// Generic setup terms (billing, not enabled, not supported) must not trip
|
|
1450
|
+
// the "switch models / upgrade plan" banner — they need their own detail.
|
|
1451
|
+
for (const message of [
|
|
1452
|
+
"PERMISSION_DENIED: Cloud Billing has not been enabled for this project",
|
|
1453
|
+
"PERMISSION_DENIED: Generative Language API has not been used in project 123 or it is not enabled",
|
|
1454
|
+
"PERMISSION_DENIED: User location is not supported for the API use",
|
|
1455
|
+
]) {
|
|
1456
|
+
expect(await reasonForApiError(403, message)).toBe("invalid_credentials");
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
|
|
1460
|
+
test("maps 404 / NOT_FOUND to model_not_found", async () => {
|
|
1461
|
+
expect(
|
|
1462
|
+
await reasonForApiError(404, "NOT_FOUND: model does not exist"),
|
|
1463
|
+
).toBe("model_not_found");
|
|
1464
|
+
});
|
|
1465
|
+
|
|
1466
|
+
test("maps a plain 429 (no token signal) to rate_limited, not context overflow", async () => {
|
|
1467
|
+
shouldThrow = new FakeApiError(
|
|
1468
|
+
429,
|
|
1469
|
+
"RESOURCE_EXHAUSTED: Quota exceeded for requests per minute",
|
|
1470
|
+
);
|
|
1471
|
+
try {
|
|
1472
|
+
await provider.sendMessage([
|
|
1473
|
+
{ role: "user", content: [{ type: "text", text: "Hi" }] },
|
|
1474
|
+
]);
|
|
1475
|
+
throw new Error("expected sendMessage to throw");
|
|
1476
|
+
} catch (error) {
|
|
1477
|
+
expect(error).toBeInstanceOf(ProviderError);
|
|
1478
|
+
expect(error).not.toBeInstanceOf(ContextOverflowError);
|
|
1479
|
+
expect((error as ProviderError).reason).toBe("rate_limited");
|
|
1480
|
+
}
|
|
1481
|
+
});
|
|
1482
|
+
|
|
1483
|
+
test("maps 5xx to server_error, or overloaded when the body says so", async () => {
|
|
1484
|
+
expect(await reasonForApiError(500, "Internal error")).toBe("server_error");
|
|
1485
|
+
expect(
|
|
1486
|
+
await reasonForApiError(
|
|
1487
|
+
503,
|
|
1488
|
+
"UNAVAILABLE: The model is overloaded. Please try again later.",
|
|
1489
|
+
),
|
|
1490
|
+
).toBe("overloaded");
|
|
1491
|
+
});
|
|
1492
|
+
|
|
1493
|
+
test("maps other 4xx to bad_request", async () => {
|
|
1494
|
+
expect(await reasonForApiError(400, "INVALID_ARGUMENT: bad field")).toBe(
|
|
1495
|
+
"bad_request",
|
|
1496
|
+
);
|
|
1497
|
+
});
|
|
1498
|
+
|
|
1499
|
+
test("a genuine token-limit 429 still yields ContextOverflowError / context_overflow", async () => {
|
|
1500
|
+
shouldThrow = new FakeApiError(
|
|
1501
|
+
429,
|
|
1502
|
+
"RESOURCE_EXHAUSTED: The input token count (2000000) exceeds the maximum number of tokens allowed (1048576).",
|
|
1503
|
+
);
|
|
1504
|
+
try {
|
|
1505
|
+
await provider.sendMessage([
|
|
1506
|
+
{ role: "user", content: [{ type: "text", text: "Hi" }] },
|
|
1507
|
+
]);
|
|
1508
|
+
throw new Error("expected sendMessage to throw");
|
|
1509
|
+
} catch (error) {
|
|
1510
|
+
expect(error).toBeInstanceOf(ContextOverflowError);
|
|
1511
|
+
expect((error as ProviderError).reason).toBe("context_overflow");
|
|
1512
|
+
}
|
|
1513
|
+
});
|
|
1514
|
+
|
|
1515
|
+
test("a token-limit 400 still yields ContextOverflowError / context_overflow", async () => {
|
|
1516
|
+
shouldThrow = new FakeApiError(
|
|
1517
|
+
400,
|
|
1518
|
+
"INVALID_ARGUMENT: The prompt is too long; token count exceeds the model's context length.",
|
|
1519
|
+
);
|
|
1520
|
+
try {
|
|
1521
|
+
await provider.sendMessage([
|
|
1522
|
+
{ role: "user", content: [{ type: "text", text: "Hi" }] },
|
|
1523
|
+
]);
|
|
1524
|
+
throw new Error("expected sendMessage to throw");
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
expect(error).toBeInstanceOf(ContextOverflowError);
|
|
1527
|
+
expect((error as ProviderError).reason).toBe("context_overflow");
|
|
1528
|
+
}
|
|
1529
|
+
});
|
|
1530
|
+
|
|
1392
1531
|
// -----------------------------------------------------------------------
|
|
1393
1532
|
// Model and contents passed correctly
|
|
1394
1533
|
// -----------------------------------------------------------------------
|