@opencode/ai 2.0.21 → 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 (37) hide show
  1. package/dist/cache-policy.js +9 -2
  2. package/dist/llm.d.ts +4 -0
  3. package/dist/promise.d.ts +8 -0
  4. package/dist/protocols/alibaba-chat.d.ts +4 -0
  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 +8 -0
  10. package/dist/protocols/openai-chat.js +9 -5
  11. package/dist/protocols/openai-responses.js +4 -3
  12. package/dist/protocols/utils/cache.d.ts +5 -0
  13. package/dist/protocols/utils/cache.js +9 -1
  14. package/dist/protocols/utils/tool-stream.d.ts +12 -0
  15. package/dist/protocols/zai-chat.d.ts +4 -0
  16. package/dist/protocols/zai-chat.js +6 -3
  17. package/dist/provider-error.js +20 -6
  18. package/dist/providers/digitalocean.d.ts +469 -0
  19. package/dist/providers/digitalocean.js +53 -0
  20. package/dist/providers/google-vertex-messages.js +1 -1
  21. package/dist/providers/groq.d.ts +4 -0
  22. package/dist/providers/groq.js +1 -1
  23. package/dist/providers/index.d.ts +1 -0
  24. package/dist/providers/index.js +1 -0
  25. package/dist/providers/openrouter.d.ts +4 -0
  26. package/dist/providers/openrouter.js +1 -13
  27. package/dist/route/client.d.ts +4 -0
  28. package/dist/route/executor.js +13 -6
  29. package/dist/route/transport/http.d.ts +2 -1
  30. package/dist/route/transport/http.js +28 -6
  31. package/dist/schema/events.d.ts +176 -0
  32. package/dist/schema/events.js +4 -0
  33. package/dist/schema/options.d.ts +11 -0
  34. package/dist/schema/options.js +15 -2
  35. package/dist/testing.d.ts +32 -0
  36. package/dist/tool-history.js +17 -3
  37. package/package.json +3 -3
@@ -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 }),