@opencode/ai 0.0.0-dev-476 → 0.0.0-dev-478
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/README.md +17 -0
- package/dist/provider-error.js +2 -74
- package/dist/schema/errors.d.ts +11 -0
- package/dist/schema/errors.js +60 -0
- package/dist/utils/provider-codes.d.ts +3 -0
- package/dist/utils/provider-codes.js +41 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -137,6 +137,23 @@ res.object.city
|
|
|
137
137
|
`stream` puts `object` on its `finish` event; `loop` runs tools first and puts it on the loop's `finish`. A
|
|
138
138
|
decode failure is an `AIError` whose `partial` holds the response.
|
|
139
139
|
|
|
140
|
+
### Errors
|
|
141
|
+
|
|
142
|
+
Every failure is an `AIError`. `reason._tag` names the kind (`RateLimit`, `InvalidRequest`, `Transport`, ...), and
|
|
143
|
+
getters read the common fields without digging into `reason` or the raw body:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
try {
|
|
147
|
+
await ai.generate({ model, prompt: "Hi" })
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (!(error instanceof AIError)) throw error
|
|
150
|
+
error.status // HTTP status, when the failure came from a response
|
|
151
|
+
error.code // provider error code such as "model_not_found", when known
|
|
152
|
+
error.isRetryable // whether sending the same request again may succeed
|
|
153
|
+
error.retryAfterMs // delay the provider asked for, when known
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
140
157
|
### Raw provider events
|
|
141
158
|
|
|
142
159
|
Set `includeRaw: true` on a request to also receive every provider frame as `{ type: "raw", data }`, in order and
|
package/dist/provider-error.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Option, Schema, SchemaGetter } from "effect";
|
|
2
2
|
import { AuthenticationError, ContentPolicyError, InvalidRequestError, AIError, ProviderInternalError, QuotaExceededError, RateLimitError, UnknownProviderError, } from "./schema/index.js";
|
|
3
|
-
import {
|
|
4
|
-
import { isRecord } from "./utils/record.js";
|
|
3
|
+
import { awsErrorType, providerCodes } from "./utils/provider-codes.js";
|
|
5
4
|
const patterns = [
|
|
6
5
|
/prompt is too long/i,
|
|
7
6
|
/input is too long for (?:the )?(?:requested|selected) model/i,
|
|
@@ -65,42 +64,7 @@ export const isContextOverflowFailure = (failure) => failure instanceof AIError
|
|
|
65
64
|
* Whether a failed call may succeed when sent again: rate limits, provider-side failures, transport failures that did
|
|
66
65
|
* not deliver an accepted write, and unrecognized failures. Callers decide which calls are safe to repeat.
|
|
67
66
|
*/
|
|
68
|
-
export const isRetryable = (error) =>
|
|
69
|
-
const override = error.reason.http?.headers["x-should-retry"];
|
|
70
|
-
if (override === "true")
|
|
71
|
-
return true;
|
|
72
|
-
if (override === "false")
|
|
73
|
-
return false;
|
|
74
|
-
switch (error.reason._tag) {
|
|
75
|
-
case "RateLimit":
|
|
76
|
-
case "ProviderInternal":
|
|
77
|
-
return true;
|
|
78
|
-
// A WebSocket acknowledgment marks delivery accepted before model output may exist.
|
|
79
|
-
// Read failures can still recover; the caller chooses retry versus continuation from durable output.
|
|
80
|
-
case "Transport":
|
|
81
|
-
return (error.reason.delivery !== "rejected" &&
|
|
82
|
-
(error.reason.delivery !== "accepted" || error.reason.operation === "read"));
|
|
83
|
-
case "InvalidProviderOutput":
|
|
84
|
-
return error.reason.classification === "incomplete-stream";
|
|
85
|
-
// Unrecognized failures retry: classification records affirmative
|
|
86
|
-
// deterministic evidence, and transient failures are exactly the ones
|
|
87
|
-
// that arrive in shapes no classifier anticipates.
|
|
88
|
-
case "UnknownProvider":
|
|
89
|
-
return true;
|
|
90
|
-
case "Authentication":
|
|
91
|
-
case "QuotaExceeded":
|
|
92
|
-
case "ContentPolicy":
|
|
93
|
-
case "InvalidRequest":
|
|
94
|
-
case "UnsupportedOperation":
|
|
95
|
-
case "Timeout":
|
|
96
|
-
return false;
|
|
97
|
-
default: {
|
|
98
|
-
const exhaustive = error.reason;
|
|
99
|
-
return exhaustive;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
const decodeJson = Schema.decodeUnknownOption(Json);
|
|
67
|
+
export const isRetryable = (error) => error.isRetryable;
|
|
104
68
|
// OpenCode Zen reports account caps as typed 429/402 errors that are not throttles.
|
|
105
69
|
const QUOTA_CODES = new Set([
|
|
106
70
|
"insufficient_quota",
|
|
@@ -250,39 +214,3 @@ export function classifyProviderFailure(input) {
|
|
|
250
214
|
return new InvalidRequestError(details);
|
|
251
215
|
return new UnknownProviderError(details);
|
|
252
216
|
}
|
|
253
|
-
function providerCodes(value) {
|
|
254
|
-
const decoded = typeof value === "string" ? Option.getOrUndefined(decodeJson(value)) : value;
|
|
255
|
-
if (!isRecord(decoded))
|
|
256
|
-
return [];
|
|
257
|
-
const error = isRecord(decoded.error) ? decoded.error : undefined;
|
|
258
|
-
const inner = error && isRecord(error.innererror) ? error.innererror : undefined;
|
|
259
|
-
const metadata = error && isRecord(error.metadata) ? error.metadata : undefined;
|
|
260
|
-
const response = isRecord(decoded.response) ? decoded.response : undefined;
|
|
261
|
-
const responseError = response && isRecord(response.error) ? response.error : undefined;
|
|
262
|
-
const exception = isRecord(decoded.exception) ? decoded.exception : undefined;
|
|
263
|
-
return [
|
|
264
|
-
decoded.code,
|
|
265
|
-
...awsErrorType(decoded.__type),
|
|
266
|
-
...awsErrorType(decoded.code),
|
|
267
|
-
decoded.error_type,
|
|
268
|
-
error?.code,
|
|
269
|
-
error?.type,
|
|
270
|
-
error?.status,
|
|
271
|
-
error?.error_type,
|
|
272
|
-
// Google `google.rpc.ErrorInfo` details carry the specific reason.
|
|
273
|
-
...(Array.isArray(error?.details)
|
|
274
|
-
? error.details.map((detail) => (isRecord(detail) ? detail.reason : undefined))
|
|
275
|
-
: []),
|
|
276
|
-
inner?.code,
|
|
277
|
-
metadata?.error_type,
|
|
278
|
-
responseError?.code,
|
|
279
|
-
response?.error_type,
|
|
280
|
-
exception?.type,
|
|
281
|
-
].filter((value) => typeof value === "string");
|
|
282
|
-
}
|
|
283
|
-
// AWS restJson1 reports the modeled error in `X-Amzn-Errortype`, `__type` or `code`, possibly
|
|
284
|
-
// namespaced (`ns#Name`) or suffixed (`Name:uri`). Keep the shape name only.
|
|
285
|
-
// https://smithy.io/2.0/aws/protocols/aws-restjson1-protocol.html#operation-error-serialization
|
|
286
|
-
function awsErrorType(value) {
|
|
287
|
-
return typeof value === "string" ? [value.replace(/:.*/s, "").replace(/^[^#]*#/, "")] : [];
|
|
288
|
-
}
|
package/dist/schema/errors.d.ts
CHANGED
|
@@ -159,6 +159,17 @@ export declare class AIError extends AIError_base {
|
|
|
159
159
|
readonly partial?: LLMResponse;
|
|
160
160
|
constructor(props: Pick<AIError, "reason" | "partial">);
|
|
161
161
|
get message(): string;
|
|
162
|
+
/** HTTP status of the failed response, when the failure came from one. */
|
|
163
|
+
get status(): number | undefined;
|
|
164
|
+
/** The provider's error code from the response body or `x-amzn-errortype`, or the transport's system error code. */
|
|
165
|
+
get code(): string | undefined;
|
|
166
|
+
/** Delay the provider asked for before retrying, from `retry-after` style headers. */
|
|
167
|
+
get retryAfterMs(): number | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* Whether a failed call may succeed when sent again: rate limits, provider-side failures, transport failures that did
|
|
170
|
+
* not deliver an accepted write, and unrecognized failures. Callers decide which calls are safe to repeat.
|
|
171
|
+
*/
|
|
172
|
+
get isRetryable(): boolean;
|
|
162
173
|
}
|
|
163
174
|
/** Return `error` with `partial` attached, or unchanged when there is no partial output. */
|
|
164
175
|
export declare const withPartial: (error: AIError, partial: LLMResponse | undefined) => AIError;
|
package/dist/schema/errors.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Schema } from "effect";
|
|
|
2
2
|
import { NonNegativeInt } from "@opencode/schema";
|
|
3
3
|
import { Tool } from "@opencode/schema/tool";
|
|
4
4
|
import { ProviderID, RouteID } from "./ids.js";
|
|
5
|
+
import { awsErrorType, providerCodes } from "../utils/provider-codes.js";
|
|
5
6
|
export const ProviderFailureClassification = Schema.Literals(["context-overflow", "payload-too-large"]);
|
|
6
7
|
export class HttpContext extends Schema.Class("AI.HttpContext")({
|
|
7
8
|
url: Schema.String,
|
|
@@ -126,6 +127,65 @@ export class AIError extends Schema.TaggedError()("AI.Error", {
|
|
|
126
127
|
get message() {
|
|
127
128
|
return this.reason.message;
|
|
128
129
|
}
|
|
130
|
+
/** HTTP status of the failed response, when the failure came from one. */
|
|
131
|
+
get status() {
|
|
132
|
+
return this.reason.http?.status;
|
|
133
|
+
}
|
|
134
|
+
/** The provider's error code from the response body or `x-amzn-errortype`, or the transport's system error code. */
|
|
135
|
+
get code() {
|
|
136
|
+
if (this.reason._tag === "Transport" && this.reason.code !== undefined)
|
|
137
|
+
return this.reason.code;
|
|
138
|
+
return [
|
|
139
|
+
...(this.reason.body === undefined ? [] : providerCodes(this.reason.body)),
|
|
140
|
+
...awsErrorType(this.reason.http?.headers["x-amzn-errortype"]),
|
|
141
|
+
][0];
|
|
142
|
+
}
|
|
143
|
+
/** Delay the provider asked for before retrying, from `retry-after` style headers. */
|
|
144
|
+
get retryAfterMs() {
|
|
145
|
+
if (this.reason._tag === "RateLimit")
|
|
146
|
+
return this.reason.retryAfterMs ?? this.reason.rateLimit?.retryAfterMs;
|
|
147
|
+
if (this.reason._tag === "QuotaExceeded" || this.reason._tag === "ProviderInternal")
|
|
148
|
+
return this.reason.retryAfterMs;
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Whether a failed call may succeed when sent again: rate limits, provider-side failures, transport failures that did
|
|
153
|
+
* not deliver an accepted write, and unrecognized failures. Callers decide which calls are safe to repeat.
|
|
154
|
+
*/
|
|
155
|
+
get isRetryable() {
|
|
156
|
+
const override = this.reason.http?.headers["x-should-retry"];
|
|
157
|
+
if (override === "true")
|
|
158
|
+
return true;
|
|
159
|
+
if (override === "false")
|
|
160
|
+
return false;
|
|
161
|
+
switch (this.reason._tag) {
|
|
162
|
+
case "RateLimit":
|
|
163
|
+
case "ProviderInternal":
|
|
164
|
+
return true;
|
|
165
|
+
// A WebSocket acknowledgment marks delivery accepted before model output may exist.
|
|
166
|
+
// Read failures can still recover; the caller chooses retry versus continuation from durable output.
|
|
167
|
+
case "Transport":
|
|
168
|
+
return (this.reason.delivery !== "rejected" && (this.reason.delivery !== "accepted" || this.reason.operation === "read"));
|
|
169
|
+
case "InvalidProviderOutput":
|
|
170
|
+
return this.reason.classification === "incomplete-stream";
|
|
171
|
+
// Unrecognized failures retry: classification records affirmative
|
|
172
|
+
// deterministic evidence, and transient failures are exactly the ones
|
|
173
|
+
// that arrive in shapes no classifier anticipates.
|
|
174
|
+
case "UnknownProvider":
|
|
175
|
+
return true;
|
|
176
|
+
case "Authentication":
|
|
177
|
+
case "QuotaExceeded":
|
|
178
|
+
case "ContentPolicy":
|
|
179
|
+
case "InvalidRequest":
|
|
180
|
+
case "UnsupportedOperation":
|
|
181
|
+
case "Timeout":
|
|
182
|
+
return false;
|
|
183
|
+
default: {
|
|
184
|
+
const exhaustive = this.reason;
|
|
185
|
+
return exhaustive;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
129
189
|
}
|
|
130
190
|
/** Return `error` with `partial` attached, or unchanged when there is no partial output. */
|
|
131
191
|
export const withPartial = (error, partial) => partial === undefined ? error : new AIError({ reason: error.reason, partial });
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { Json } from "./json.js";
|
|
3
|
+
import { isRecord } from "./record.js";
|
|
4
|
+
const decodeJson = Schema.decodeUnknownOption(Json);
|
|
5
|
+
/** Error codes a provider error body carries, in their original case. The first is its primary code (`AIError.code`). */
|
|
6
|
+
export function providerCodes(value) {
|
|
7
|
+
const decoded = typeof value === "string" ? Option.getOrUndefined(decodeJson(value)) : value;
|
|
8
|
+
if (!isRecord(decoded))
|
|
9
|
+
return [];
|
|
10
|
+
const error = isRecord(decoded.error) ? decoded.error : undefined;
|
|
11
|
+
const inner = error && isRecord(error.innererror) ? error.innererror : undefined;
|
|
12
|
+
const metadata = error && isRecord(error.metadata) ? error.metadata : undefined;
|
|
13
|
+
const response = isRecord(decoded.response) ? decoded.response : undefined;
|
|
14
|
+
const responseError = response && isRecord(response.error) ? response.error : undefined;
|
|
15
|
+
const exception = isRecord(decoded.exception) ? decoded.exception : undefined;
|
|
16
|
+
return [
|
|
17
|
+
decoded.code,
|
|
18
|
+
...awsErrorType(decoded.__type),
|
|
19
|
+
...awsErrorType(decoded.code),
|
|
20
|
+
decoded.error_type,
|
|
21
|
+
error?.code,
|
|
22
|
+
error?.type,
|
|
23
|
+
error?.status,
|
|
24
|
+
error?.error_type,
|
|
25
|
+
// Google `google.rpc.ErrorInfo` details carry the specific reason.
|
|
26
|
+
...(Array.isArray(error?.details)
|
|
27
|
+
? error.details.map((detail) => (isRecord(detail) ? detail.reason : undefined))
|
|
28
|
+
: []),
|
|
29
|
+
inner?.code,
|
|
30
|
+
metadata?.error_type,
|
|
31
|
+
responseError?.code,
|
|
32
|
+
response?.error_type,
|
|
33
|
+
exception?.type,
|
|
34
|
+
].filter((value) => typeof value === "string");
|
|
35
|
+
}
|
|
36
|
+
// AWS restJson1 reports the modeled error in `X-Amzn-Errortype`, `__type` or `code`, possibly
|
|
37
|
+
// namespaced (`ns#Name`) or suffixed (`Name:uri`). Keep the shape name only.
|
|
38
|
+
// https://smithy.io/2.0/aws/protocols/aws-restjson1-protocol.html#operation-error-serialization
|
|
39
|
+
export function awsErrorType(value) {
|
|
40
|
+
return typeof value === "string" ? [value.replace(/:.*/s, "").replace(/^[^#]*#/, "")] : [];
|
|
41
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-478",
|
|
4
4
|
"name": "@opencode/ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -520,7 +520,7 @@
|
|
|
520
520
|
"devDependencies": {
|
|
521
521
|
"@clack/prompts": "1.0.0-alpha.1",
|
|
522
522
|
"@effect/platform-node": "4.1.0",
|
|
523
|
-
"@opencode/http-recorder": "0.0.0-dev-
|
|
523
|
+
"@opencode/http-recorder": "0.0.0-dev-478",
|
|
524
524
|
"@tsconfig/bun": "1.0.9",
|
|
525
525
|
"@types/bun": "1.4.3",
|
|
526
526
|
"@typescript/native": "npm:typescript@7.1.0-beta",
|
|
@@ -530,7 +530,7 @@
|
|
|
530
530
|
"@aws-sdk/credential-providers": "3.1057.0",
|
|
531
531
|
"@smithy/eventstream-codec": "4.2.14",
|
|
532
532
|
"@smithy/util-utf8": "4.2.2",
|
|
533
|
-
"@opencode/schema": "0.0.0-dev-
|
|
533
|
+
"@opencode/schema": "0.0.0-dev-478",
|
|
534
534
|
"aws4fetch": "1.0.20",
|
|
535
535
|
"effect": "4.1.0",
|
|
536
536
|
"google-auth-library": "10.5.0"
|