@opencode/ai 2.0.20 → 2.0.22

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.
Files changed (59) hide show
  1. package/dist/cache-policy.js +22 -3
  2. package/dist/llm.d.ts +4 -0
  3. package/dist/promise.d.ts +8 -0
  4. package/dist/protocols/alibaba-chat.d.ts +20 -10
  5. package/dist/protocols/alibaba-chat.js +2 -1
  6. package/dist/protocols/anthropic-messages.js +14 -12
  7. package/dist/protocols/bedrock-converse.js +30 -1
  8. package/dist/protocols/open-responses.d.ts +4 -0
  9. package/dist/protocols/openai-chat.d.ts +122 -71
  10. package/dist/protocols/openai-chat.js +40 -37
  11. package/dist/protocols/openai-compatible-chat.d.ts +16 -10
  12. package/dist/protocols/openai-responses.js +4 -3
  13. package/dist/protocols/utils/cache.d.ts +5 -0
  14. package/dist/protocols/utils/cache.js +9 -1
  15. package/dist/protocols/utils/tool-stream.d.ts +12 -0
  16. package/dist/protocols/zai-chat.d.ts +20 -10
  17. package/dist/protocols/zai-chat.js +6 -3
  18. package/dist/provider-error.js +24 -6
  19. package/dist/providers/alibaba.d.ts +16 -10
  20. package/dist/providers/amazon-bedrock-mantle.d.ts +16 -10
  21. package/dist/providers/azure.d.ts +16 -10
  22. package/dist/providers/baseten.d.ts +32 -20
  23. package/dist/providers/cerebras.d.ts +32 -20
  24. package/dist/providers/cloudflare-ai-gateway.d.ts +32 -20
  25. package/dist/providers/cloudflare-ai-gateway.js +43 -35
  26. package/dist/providers/cloudflare-workers-ai.d.ts +32 -20
  27. package/dist/providers/deepinfra.d.ts +32 -20
  28. package/dist/providers/deepseek.d.ts +32 -20
  29. package/dist/providers/digitalocean.d.ts +469 -0
  30. package/dist/providers/digitalocean.js +53 -0
  31. package/dist/providers/fireworks.d.ts +32 -20
  32. package/dist/providers/google-vertex-chat.d.ts +16 -10
  33. package/dist/providers/google-vertex-messages.js +1 -1
  34. package/dist/providers/groq.d.ts +36 -20
  35. package/dist/providers/groq.js +1 -1
  36. package/dist/providers/index.d.ts +1 -0
  37. package/dist/providers/index.js +1 -0
  38. package/dist/providers/meta.d.ts +16 -10
  39. package/dist/providers/minimax.d.ts +16 -10
  40. package/dist/providers/moonshot.d.ts +16 -10
  41. package/dist/providers/openai-compatible.d.ts +16 -10
  42. package/dist/providers/openai.d.ts +16 -10
  43. package/dist/providers/openrouter.d.ts +68 -40
  44. package/dist/providers/openrouter.js +1 -13
  45. package/dist/providers/togetherai.d.ts +32 -20
  46. package/dist/providers/xai.d.ts +16 -10
  47. package/dist/providers/zai-coding-plan.d.ts +16 -10
  48. package/dist/providers/zai.d.ts +16 -10
  49. package/dist/route/client.d.ts +4 -0
  50. package/dist/route/executor.js +13 -6
  51. package/dist/route/transport/http.d.ts +2 -1
  52. package/dist/route/transport/http.js +28 -6
  53. package/dist/schema/events.d.ts +176 -0
  54. package/dist/schema/events.js +4 -0
  55. package/dist/schema/options.d.ts +11 -0
  56. package/dist/schema/options.js +15 -2
  57. package/dist/testing.d.ts +32 -0
  58. package/dist/tool-history.js +17 -3
  59. package/package.json +3 -3
@@ -58,15 +58,18 @@ export declare const route: Route<{
58
58
  })[];
59
59
  } | {
60
60
  readonly [x: string]: unknown;
61
- readonly content: string | null;
61
+ readonly content: string | readonly {
62
+ readonly type: "text";
63
+ readonly text: string;
64
+ readonly cache_control?: {
65
+ readonly type: "ephemeral";
66
+ readonly ttl?: string | undefined;
67
+ } | undefined;
68
+ }[] | null;
62
69
  readonly role: "assistant";
63
70
  readonly reasoning?: string | undefined;
64
71
  readonly reasoning_content?: string | undefined;
65
72
  readonly reasoning_text?: string | undefined;
66
- readonly cache_control?: {
67
- readonly type: "ephemeral";
68
- readonly ttl?: string | undefined;
69
- } | undefined;
70
73
  readonly tool_calls?: readonly {
71
74
  readonly function: {
72
75
  readonly name: string;
@@ -82,13 +85,16 @@ export declare const route: Route<{
82
85
  }[] | undefined;
83
86
  readonly reasoning_details?: unknown;
84
87
  } | {
85
- readonly content: string;
86
88
  readonly role: "tool";
87
89
  readonly tool_call_id: string;
88
- readonly cache_control?: {
89
- readonly type: "ephemeral";
90
- readonly ttl?: string | undefined;
91
- } | undefined;
90
+ readonly content: string | readonly {
91
+ readonly type: "text";
92
+ readonly text: string;
93
+ readonly cache_control?: {
94
+ readonly type: "ephemeral";
95
+ readonly ttl?: string | undefined;
96
+ } | undefined;
97
+ }[];
92
98
  })[];
93
99
  readonly stream: true;
94
100
  readonly max_completion_tokens?: number | undefined;
@@ -174,15 +180,18 @@ export declare const routes: Route<{
174
180
  })[];
175
181
  } | {
176
182
  readonly [x: string]: unknown;
177
- readonly content: string | null;
183
+ readonly content: string | readonly {
184
+ readonly type: "text";
185
+ readonly text: string;
186
+ readonly cache_control?: {
187
+ readonly type: "ephemeral";
188
+ readonly ttl?: string | undefined;
189
+ } | undefined;
190
+ }[] | null;
178
191
  readonly role: "assistant";
179
192
  readonly reasoning?: string | undefined;
180
193
  readonly reasoning_content?: string | undefined;
181
194
  readonly reasoning_text?: string | undefined;
182
- readonly cache_control?: {
183
- readonly type: "ephemeral";
184
- readonly ttl?: string | undefined;
185
- } | undefined;
186
195
  readonly tool_calls?: readonly {
187
196
  readonly function: {
188
197
  readonly name: string;
@@ -198,13 +207,16 @@ export declare const routes: Route<{
198
207
  }[] | undefined;
199
208
  readonly reasoning_details?: unknown;
200
209
  } | {
201
- readonly content: string;
202
210
  readonly role: "tool";
203
211
  readonly tool_call_id: string;
204
- readonly cache_control?: {
205
- readonly type: "ephemeral";
206
- readonly ttl?: string | undefined;
207
- } | undefined;
212
+ readonly content: string | readonly {
213
+ readonly type: "text";
214
+ readonly text: string;
215
+ readonly cache_control?: {
216
+ readonly type: "ephemeral";
217
+ readonly ttl?: string | undefined;
218
+ } | undefined;
219
+ }[];
208
220
  })[];
209
221
  readonly stream: true;
210
222
  readonly max_completion_tokens?: number | undefined;
@@ -66,15 +66,18 @@ export declare const routes: (Route<{
66
66
  })[];
67
67
  } | {
68
68
  readonly [x: string]: unknown;
69
- readonly content: string | null;
69
+ readonly content: string | readonly {
70
+ readonly type: "text";
71
+ readonly text: string;
72
+ readonly cache_control?: {
73
+ readonly type: "ephemeral";
74
+ readonly ttl?: string | undefined;
75
+ } | undefined;
76
+ }[] | null;
70
77
  readonly role: "assistant";
71
78
  readonly reasoning?: string | undefined;
72
79
  readonly reasoning_content?: string | undefined;
73
80
  readonly reasoning_text?: string | undefined;
74
- readonly cache_control?: {
75
- readonly type: "ephemeral";
76
- readonly ttl?: string | undefined;
77
- } | undefined;
78
81
  readonly tool_calls?: readonly {
79
82
  readonly function: {
80
83
  readonly name: string;
@@ -90,13 +93,16 @@ export declare const routes: (Route<{
90
93
  }[] | undefined;
91
94
  readonly reasoning_details?: unknown;
92
95
  } | {
93
- readonly content: string;
94
96
  readonly role: "tool";
95
97
  readonly tool_call_id: string;
96
- readonly cache_control?: {
97
- readonly type: "ephemeral";
98
- readonly ttl?: string | undefined;
99
- } | undefined;
98
+ readonly content: string | readonly {
99
+ readonly type: "text";
100
+ readonly text: string;
101
+ readonly cache_control?: {
102
+ readonly type: "ephemeral";
103
+ readonly ttl?: string | undefined;
104
+ } | undefined;
105
+ }[];
100
106
  })[];
101
107
  readonly stream: true;
102
108
  readonly max_completion_tokens?: number | undefined;
@@ -230,15 +230,18 @@ export declare const routes: (Route<{
230
230
  })[];
231
231
  } | {
232
232
  readonly [x: string]: unknown;
233
- readonly content: string | null;
233
+ readonly content: string | readonly {
234
+ readonly type: "text";
235
+ readonly text: string;
236
+ readonly cache_control?: {
237
+ readonly type: "ephemeral";
238
+ readonly ttl?: string | undefined;
239
+ } | undefined;
240
+ }[] | null;
234
241
  readonly role: "assistant";
235
242
  readonly reasoning?: string | undefined;
236
243
  readonly reasoning_content?: string | undefined;
237
244
  readonly reasoning_text?: string | undefined;
238
- readonly cache_control?: {
239
- readonly type: "ephemeral";
240
- readonly ttl?: string | undefined;
241
- } | undefined;
242
245
  readonly tool_calls?: readonly {
243
246
  readonly function: {
244
247
  readonly name: string;
@@ -254,13 +257,16 @@ export declare const routes: (Route<{
254
257
  }[] | undefined;
255
258
  readonly reasoning_details?: unknown;
256
259
  } | {
257
- readonly content: string;
258
260
  readonly role: "tool";
259
261
  readonly tool_call_id: string;
260
- readonly cache_control?: {
261
- readonly type: "ephemeral";
262
- readonly ttl?: string | undefined;
263
- } | undefined;
262
+ readonly content: string | readonly {
263
+ readonly type: "text";
264
+ readonly text: string;
265
+ readonly cache_control?: {
266
+ readonly type: "ephemeral";
267
+ readonly ttl?: string | undefined;
268
+ } | undefined;
269
+ }[];
264
270
  })[];
265
271
  readonly stream: true;
266
272
  readonly max_completion_tokens?: number | undefined;
@@ -60,15 +60,18 @@ export declare const routes: Route<{
60
60
  })[];
61
61
  } | {
62
62
  readonly [x: string]: unknown;
63
- readonly content: string | null;
63
+ readonly content: string | readonly {
64
+ readonly type: "text";
65
+ readonly text: string;
66
+ readonly cache_control?: {
67
+ readonly type: "ephemeral";
68
+ readonly ttl?: string | undefined;
69
+ } | undefined;
70
+ }[] | null;
64
71
  readonly role: "assistant";
65
72
  readonly reasoning?: string | undefined;
66
73
  readonly reasoning_content?: string | undefined;
67
74
  readonly reasoning_text?: string | undefined;
68
- readonly cache_control?: {
69
- readonly type: "ephemeral";
70
- readonly ttl?: string | undefined;
71
- } | undefined;
72
75
  readonly tool_calls?: readonly {
73
76
  readonly function: {
74
77
  readonly name: string;
@@ -84,13 +87,16 @@ export declare const routes: Route<{
84
87
  }[] | undefined;
85
88
  readonly reasoning_details?: unknown;
86
89
  } | {
87
- readonly content: string;
88
90
  readonly role: "tool";
89
91
  readonly tool_call_id: string;
90
- readonly cache_control?: {
91
- readonly type: "ephemeral";
92
- readonly ttl?: string | undefined;
93
- } | undefined;
92
+ readonly content: string | readonly {
93
+ readonly type: "text";
94
+ readonly text: string;
95
+ readonly cache_control?: {
96
+ readonly type: "ephemeral";
97
+ readonly ttl?: string | undefined;
98
+ } | undefined;
99
+ }[];
94
100
  })[];
95
101
  readonly stream: true;
96
102
  readonly max_completion_tokens?: number | undefined;
@@ -396,6 +396,8 @@ export declare const streamRequest: (request: LLMRequest, options?: StreamOption
396
396
  readonly reason: {
397
397
  readonly normalized: "length" | "stop" | "tool-calls" | "content-filter" | "error" | "unknown";
398
398
  readonly raw?: string | undefined;
399
+ readonly category?: string | undefined;
400
+ readonly explanation?: string | undefined;
399
401
  };
400
402
  readonly index: number;
401
403
  readonly providerMetadata?: {
@@ -409,6 +411,8 @@ export declare const streamRequest: (request: LLMRequest, options?: StreamOption
409
411
  readonly reason: {
410
412
  readonly normalized: "length" | "stop" | "tool-calls" | "content-filter" | "error" | "unknown";
411
413
  readonly raw?: string | undefined;
414
+ readonly category?: string | undefined;
415
+ readonly explanation?: string | undefined;
412
416
  };
413
417
  readonly providerMetadata?: {
414
418
  readonly [x: string]: {
@@ -128,6 +128,12 @@ const nativeTransportFailure = (error) => {
128
128
  return cause;
129
129
  return failure;
130
130
  };
131
+ // HTTP hooks re-wrap response bodies, so a read failure can arrive as an HttpClientError caused by
132
+ // another HttpClientError. The innermost cause carries the native failure.
133
+ const rootCause = (error) => HttpClientError.isHttpClientError(error) && "cause" in error.reason && error.reason.cause !== undefined
134
+ ? rootCause(error.reason.cause)
135
+ : error;
136
+ const CONNECTION_LOST = "Connection lost while reading the response";
131
137
  const httpError = (input) => {
132
138
  const request = HttpClientError.isHttpClientError(input.error) ? input.error.request : input.request;
133
139
  const transportError = (failure) => new AIError({
@@ -141,18 +147,19 @@ const httpError = (input) => {
141
147
  url: request.url,
142
148
  }),
143
149
  });
144
- const source = HttpClientError.isHttpClientError(input.error) && "cause" in input.error.reason
145
- ? (input.error.reason.cause ?? input.error)
146
- : input.error;
150
+ const source = rootCause(input.error);
147
151
  const native = nativeTransportFailure(source);
148
152
  const code = native?.code;
149
- const raw = native?.message ?? (input.error instanceof Error ? input.error.message : undefined);
150
- const detail = raw;
151
- const message = code && detail && !detail.includes(code) ? `${code}: ${detail}` : detail;
153
+ const detail = code && native?.message && !native.message.includes(code) ? `${code}: ${native.message}` : native?.message;
154
+ const message = detail ?? (input.error instanceof Error ? input.error.message : undefined);
152
155
  if (Cause.isTimeoutError(input.error) || Cause.isTimeoutError(source))
153
156
  return transportError({ message: message ?? "HTTP transport timed out", code: code ?? "Timeout" });
154
157
  if (!HttpClientError.isHttpClientError(input.error))
155
158
  return transportError({ message: message ?? "HTTP transport failed", code });
159
+ // Effect reports every response body read failure as a DecodeError, but the raw byte stream decodes
160
+ // nothing: provider output parsing happens later and fails as InvalidProviderOutput.
161
+ if (input.operation === "read" && input.error.reason._tag === "DecodeError")
162
+ return transportError({ message: detail ? `${CONNECTION_LOST}: ${detail}` : CONNECTION_LOST, code });
156
163
  if (input.error.reason._tag === "TransportError") {
157
164
  return transportError({
158
165
  message: message ?? input.error.reason.description ?? "HTTP transport failed",
@@ -2,6 +2,7 @@ import { Effect } from "effect";
2
2
  import { Headers, HttpClientRequest } from "effect/unstable/http";
3
3
  import { Framing } from "../framing.js";
4
4
  import type { HttpMiddleware, Transport, TransportPrepareInput } from "./index.js";
5
+ import { AIError } from "../../schema/index.js";
5
6
  export type JsonRequestInput<Body> = TransportPrepareInput<Body>;
6
7
  export interface JsonRequestParts<Body = unknown> {
7
8
  readonly url: string;
@@ -19,7 +20,7 @@ export declare const jsonRequestParts: <Body>(input: JsonRequestInput<Body>) =>
19
20
  jsonBody: Record<string, unknown> | Body;
20
21
  bodyText: string;
21
22
  headers: Headers.Headers;
22
- }, import("../../schema/errors.js").AIError, never>;
23
+ }, AIError, never>;
23
24
  export interface HttpJsonInput<_Body, Frame> {
24
25
  readonly framing: Framing.Definition<Frame>;
25
26
  }
@@ -1,10 +1,10 @@
1
- import { Effect } from "effect";
1
+ import { Clock, Duration, Effect, Stream } from "effect";
2
2
  import { Headers, HttpClientRequest } from "effect/unstable/http";
3
3
  import { Auth } from "../auth.js";
4
4
  import { render as renderEndpoint } from "../endpoint.js";
5
5
  import { Framing } from "../framing.js";
6
6
  import * as ProviderShared from "../../protocols/shared.js";
7
- import { mergeJsonRecords } from "../../schema/index.js";
7
+ import { AIError, DEFAULT_HTTP_TIMEOUT_MS, mergeJsonRecords, TransportError, } from "../../schema/index.js";
8
8
  import { RequestExecutor } from "../executor.js";
9
9
  const applyQuery = (url, query) => {
10
10
  if (!query)
@@ -53,15 +53,37 @@ export const httpJson = (input) => ({
53
53
  middleware: prepareInput.middleware,
54
54
  };
55
55
  }),
56
- execute: (prepared, _request, runtime) => Effect.gen(function* () {
57
- const response = yield* runtime.http.execute(prepared.request, prepared.middleware);
56
+ execute: (prepared, request, runtime) => Effect.gen(function* () {
57
+ const timeout = (operation, message, http) => new AIError({
58
+ reason: new TransportError({
59
+ message,
60
+ transport: "http",
61
+ operation,
62
+ code: "Timeout",
63
+ url: prepared.request.url,
64
+ http,
65
+ }),
66
+ });
67
+ const started = yield* Clock.currentTimeMillis;
68
+ // Unlike the header and chunk limits, the whole-request budget has no default.
69
+ const total = request.http?.timeout ? Duration.millis(request.http.timeout) : Duration.infinity;
70
+ const response = yield* runtime.http.execute(prepared.request, prepared.middleware).pipe(Effect.timeoutOrElse({
71
+ duration: Duration.min(timeoutDuration(request.http?.headerTimeout), total),
72
+ orElse: () => timeout("request", "Timed out waiting for response headers"),
73
+ }));
74
+ const http = RequestExecutor.responseHttp(response);
75
+ const remaining = Duration.subtract(total, Duration.millis((yield* Clock.currentTimeMillis) - started));
58
76
  return {
59
- frames: prepared.framing.frame(RequestExecutor.responseStream(response)),
60
- http: RequestExecutor.responseHttp(response),
77
+ frames: prepared.framing.frame(RequestExecutor.responseStream(response).pipe(Stream.timeoutOrElse({
78
+ duration: timeoutDuration(request.http?.chunkTimeout),
79
+ orElse: () => Stream.fail(timeout("read", "Timed out waiting for response data", http)),
80
+ }), Stream.interruptWhen(Effect.sleep(remaining).pipe(Effect.andThen(Effect.fail(timeout("read", "Timed out waiting for the response to complete", http))))))),
81
+ http,
61
82
  body: prepared.framing.body,
62
83
  };
63
84
  }),
64
85
  });
86
+ const timeoutDuration = (value) => value === false ? Duration.infinity : Duration.millis(value ?? DEFAULT_HTTP_TIMEOUT_MS);
65
87
  export const sseJson = {
66
88
  id: "http-json/sse",
67
89
  with: () => httpJson({ framing: Framing.sse }),