@opencode/ai 2.3.2 → 2.4.0

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 (87) hide show
  1. package/dist/decision.d.ts +4 -3
  2. package/dist/decision.js +3 -2
  3. package/dist/embedding.d.ts +4 -3
  4. package/dist/embedding.js +3 -2
  5. package/dist/experimental/cloudflare-run-extraction.js +1 -2
  6. package/dist/experimental/realtime.d.ts +12 -43
  7. package/dist/experimental/realtime.js +14 -30
  8. package/dist/experimental.d.ts +1 -1
  9. package/dist/image.js +1 -1
  10. package/dist/media-model.d.ts +5 -4
  11. package/dist/media-model.js +5 -4
  12. package/dist/media.js +4 -17
  13. package/dist/protocols/anthropic-messages.js +7 -11
  14. package/dist/protocols/bedrock-converse.js +6 -6
  15. package/dist/protocols/cohere-chat.js +3 -3
  16. package/dist/protocols/gemini.js +6 -10
  17. package/dist/protocols/google-interactions.d.ts +299 -6
  18. package/dist/protocols/google-interactions.js +251 -33
  19. package/dist/protocols/meta-messages.js +3 -3
  20. package/dist/protocols/minimax-video.js +7 -1
  21. package/dist/protocols/mistral-chat.d.ts +1 -0
  22. package/dist/protocols/mistral-chat.js +3 -3
  23. package/dist/protocols/open-responses-continuation.js +2 -0
  24. package/dist/protocols/open-responses.d.ts +2 -0
  25. package/dist/protocols/open-responses.js +5 -3
  26. package/dist/protocols/openai-chat.d.ts +1 -0
  27. package/dist/protocols/openai-chat.js +6 -5
  28. package/dist/protocols/openai-responses.js +1 -0
  29. package/dist/protocols/shared.d.ts +12 -1
  30. package/dist/protocols/shared.js +56 -9
  31. package/dist/protocols/utils/gateway-protocol.js +1 -0
  32. package/dist/protocols/utils/gemini-generate-content.d.ts +4 -0
  33. package/dist/protocols/utils/gemini-generate-content.js +5 -1
  34. package/dist/protocols/utils/responses-compaction.js +3 -3
  35. package/dist/protocols/xai-responses.d.ts +2 -1
  36. package/dist/protocols/xai-responses.js +4 -1
  37. package/dist/provider-error.js +3 -1
  38. package/dist/providers/alibaba.js +2 -3
  39. package/dist/providers/assemblyai.d.ts +2 -4
  40. package/dist/providers/azure.js +5 -11
  41. package/dist/providers/baseten.js +2 -3
  42. package/dist/providers/byteplus.d.ts +2 -4
  43. package/dist/providers/cartesia.d.ts +2 -4
  44. package/dist/providers/cloudflare-ai-gateway.js +3 -3
  45. package/dist/providers/cloudflare-workers-ai.js +2 -3
  46. package/dist/providers/cohere.js +2 -3
  47. package/dist/providers/deepgram.d.ts +2 -4
  48. package/dist/providers/deepinfra.js +2 -3
  49. package/dist/providers/digitalocean.js +3 -5
  50. package/dist/providers/elevenlabs.d.ts +2 -4
  51. package/dist/providers/fireworks.js +2 -3
  52. package/dist/providers/google-vertex-interactions.d.ts +12 -3
  53. package/dist/providers/google-vertex-interactions.js +1 -2
  54. package/dist/providers/google-vertex.js +6 -2
  55. package/dist/providers/google.d.ts +20 -3
  56. package/dist/providers/google.js +7 -4
  57. package/dist/providers/openai-compatible.js +3 -5
  58. package/dist/providers/openai.js +4 -10
  59. package/dist/providers/opencode.d.ts +2 -4
  60. package/dist/providers/opencode.js +2 -3
  61. package/dist/providers/openrouter.js +3 -5
  62. package/dist/providers/typesafe-ai.d.ts +2 -4
  63. package/dist/providers/typesafe-ai.js +2 -3
  64. package/dist/providers/vercel-ai-gateway.js +3 -5
  65. package/dist/providers/volcengine.d.ts +2 -4
  66. package/dist/route/decision.d.ts +4 -7
  67. package/dist/route/decision.js +8 -10
  68. package/dist/route/embedding.d.ts +4 -7
  69. package/dist/route/embedding.js +4 -7
  70. package/dist/route/executor-service.d.ts +20 -3
  71. package/dist/route/executor-service.js +72 -2
  72. package/dist/route/executor.d.ts +5 -5
  73. package/dist/route/executor.js +68 -73
  74. package/dist/route/media-protocol.d.ts +2 -1
  75. package/dist/route/media-protocol.js +2 -10
  76. package/dist/route/media.d.ts +6 -8
  77. package/dist/route/media.js +8 -8
  78. package/dist/route/protocol.d.ts +2 -0
  79. package/dist/route/protocol.js +4 -1
  80. package/dist/route/transport/http.d.ts +1 -2
  81. package/dist/route/transport/http.js +5 -28
  82. package/dist/schema/errors.js +2 -1
  83. package/dist/schema/options.d.ts +1 -1
  84. package/dist/speech.js +1 -1
  85. package/dist/transcription.js +1 -1
  86. package/dist/video.js +1 -1
  87. package/package.json +3 -3
@@ -102,7 +102,7 @@ export declare const routes: (import("../route.js").Route<{
102
102
  readonly continuationToken?: string | undefined;
103
103
  }, Gemini.Prepared, import("../route.js").CompactionOperations | undefined> | import("../route.js").Route<{
104
104
  readonly model: string;
105
- readonly input: readonly ({
105
+ readonly input?: readonly ({
106
106
  readonly type: "model_output" | "user_input";
107
107
  readonly content: readonly ({
108
108
  readonly type: "text";
@@ -132,12 +132,17 @@ export declare const routes: (import("../route.js").Route<{
132
132
  readonly name: string;
133
133
  readonly result: unknown;
134
134
  readonly is_error?: boolean | undefined;
135
- })[];
135
+ })[] | undefined;
136
136
  readonly stream: true;
137
137
  readonly store: boolean;
138
138
  readonly previous_interaction_id?: string | undefined;
139
+ readonly continuation_token?: string | undefined;
139
140
  readonly system_instruction?: string | undefined;
140
141
  readonly service_tier?: string | undefined;
142
+ readonly safety_settings?: readonly {
143
+ readonly type: string;
144
+ readonly threshold: string;
145
+ }[] | undefined;
141
146
  readonly tools?: readonly {
142
147
  readonly type: "function";
143
148
  readonly name: string;
@@ -161,7 +166,7 @@ export declare const routes: (import("../route.js").Route<{
161
166
  };
162
167
  } | undefined;
163
168
  };
164
- }, import("../route/transport/http.js").HttpPrepared<string>, import("../route.js").CompactionOperations | undefined>)[];
169
+ }, GoogleInteractions.Prepared, import("../route.js").CompactionOperations | undefined>)[];
165
170
  export type Config = RequestOptions & ProviderAuthOption<"optional"> & {
166
171
  readonly baseURL?: string;
167
172
  readonly providerOptions?: Gemini.ProviderOptionsInput & GoogleInteractions.ProviderOptionsInput;
@@ -180,6 +185,10 @@ export declare const configure: (input?: Config) => {
180
185
  readonly thinkingLevel?: import("../schema/options.js").OpenString<"high" | "low" | "medium" | "minimal"> | undefined;
181
186
  readonly thinkingSummaries?: import("../schema/options.js").OpenString<"auto" | "none"> | undefined;
182
187
  readonly serviceTier?: import("../schema/options.js").OpenString<"deferred" | "flex" | "priority" | "standard"> | undefined;
188
+ readonly safetySettings?: readonly {
189
+ readonly category: import("../schema/options.js").OpenString<"HARM_CATEGORY_CIVIC_INTEGRITY" | "HARM_CATEGORY_DANGEROUS_CONTENT" | "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_UNSPECIFIED">;
190
+ readonly threshold: import("../schema/options.js").OpenString<"BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_NONE" | "BLOCK_ONLY_HIGH" | "HARM_BLOCK_THRESHOLD_UNSPECIFIED" | "OFF">;
191
+ }[] | undefined;
183
192
  }, import("../route.js").CompactionOperations | undefined>;
184
193
  image: (modelID: string | ModelID, providerOptions?: ProviderOptions) => import("../image.js").ImageModel<import("../protocols/google-images.js").GoogleImageOptions>;
185
194
  video: (modelID: string | ModelID, providerOptions?: ProviderOptions) => import("../video.js").VideoModel<import("../protocols/google-interactions-video.js").GoogleVideoOptions>;
@@ -199,6 +208,10 @@ export declare const provider: {
199
208
  readonly thinkingLevel?: import("../schema/options.js").OpenString<"high" | "low" | "medium" | "minimal"> | undefined;
200
209
  readonly thinkingSummaries?: import("../schema/options.js").OpenString<"auto" | "none"> | undefined;
201
210
  readonly serviceTier?: import("../schema/options.js").OpenString<"deferred" | "flex" | "priority" | "standard"> | undefined;
211
+ readonly safetySettings?: readonly {
212
+ readonly category: import("../schema/options.js").OpenString<"HARM_CATEGORY_CIVIC_INTEGRITY" | "HARM_CATEGORY_DANGEROUS_CONTENT" | "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_UNSPECIFIED">;
213
+ readonly threshold: import("../schema/options.js").OpenString<"BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_NONE" | "BLOCK_ONLY_HIGH" | "HARM_BLOCK_THRESHOLD_UNSPECIFIED" | "OFF">;
214
+ }[] | undefined;
202
215
  }, import("../route.js").CompactionOperations | undefined>;
203
216
  image: (modelID: string | ModelID, providerOptions?: ProviderOptions) => import("../image.js").ImageModel<import("../protocols/google-images.js").GoogleImageOptions>;
204
217
  video: (modelID: string | ModelID, providerOptions?: ProviderOptions) => import("../video.js").VideoModel<import("../protocols/google-interactions-video.js").GoogleVideoOptions>;
@@ -223,6 +236,10 @@ export declare const interactions: (modelID: string | ModelID) => import("../sch
223
236
  readonly thinkingLevel?: import("../schema/options.js").OpenString<"high" | "low" | "medium" | "minimal"> | undefined;
224
237
  readonly thinkingSummaries?: import("../schema/options.js").OpenString<"auto" | "none"> | undefined;
225
238
  readonly serviceTier?: import("../schema/options.js").OpenString<"deferred" | "flex" | "priority" | "standard"> | undefined;
239
+ readonly safetySettings?: readonly {
240
+ readonly category: import("../schema/options.js").OpenString<"HARM_CATEGORY_CIVIC_INTEGRITY" | "HARM_CATEGORY_DANGEROUS_CONTENT" | "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_UNSPECIFIED">;
241
+ readonly threshold: import("../schema/options.js").OpenString<"BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_NONE" | "BLOCK_ONLY_HIGH" | "HARM_BLOCK_THRESHOLD_UNSPECIFIED" | "OFF">;
242
+ }[] | undefined;
226
243
  }, import("../route.js").CompactionOperations | undefined>;
227
244
  export declare const speech: (modelID: string | ModelID) => import("../speech.js").SpeechModel<import("../protocols/google-speech.js").GoogleSpeechOptions>;
228
245
  export declare const transcription: (modelID: string | ModelID) => import("../transcription.js").TranscriptionModel<import("../protocols/google-transcription.js").GoogleTranscriptionOptions>;
@@ -1,7 +1,7 @@
1
1
  import { Auth } from "../route/auth.js";
2
2
  import { AuthOptions } from "../route/auth-options.js";
3
3
  import { MediaRoute } from "../route/media.js";
4
- import { ProviderID } from "../schema/index.js";
4
+ import { mergeTransport, ProviderID } from "../schema/index.js";
5
5
  import { Gemini } from "../protocols/gemini.js";
6
6
  import { GoogleEmbedContent, } from "../protocols/google-embed-content.js";
7
7
  import { GoogleDecisions } from "../protocols/google-decisions.js";
@@ -30,8 +30,7 @@ export const configure = (input = {}) => {
30
30
  provider: id,
31
31
  endpoint: { baseURL: input.baseURL },
32
32
  auth: auth(input),
33
- headers: input.headers,
34
- http: input.http,
33
+ ...mergeTransport(input),
35
34
  });
36
35
  return {
37
36
  id,
@@ -44,7 +43,11 @@ export const configure = (input = {}) => {
44
43
  transcription: (modelID) => GoogleTranscription.model({ ...media, id: modelID }),
45
44
  embedding: (modelID, providerOptions) => embeddingRoute.model({ id: modelID, providerOptions }),
46
45
  decision: (modelID, providerOptions) => GoogleDecisions.route
47
- .with({ endpoint: { baseURL: media.baseURL }, auth: media.auth, headers: media.headers, http: media.http })
46
+ .with({
47
+ endpoint: { baseURL: media.baseURL },
48
+ auth: media.auth,
49
+ ...media.transport,
50
+ })
48
51
  .model({ id: modelID, providerOptions }),
49
52
  configure,
50
53
  };
@@ -1,4 +1,4 @@
1
- import { mergeProviderOptions, ProviderID, } from "../schema/index.js";
1
+ import { mergeTransport, mergeProviderOptions, ProviderID, } from "../schema/index.js";
2
2
  import { OpenAICompatibleChat } from "../protocols/openai-compatible-chat.js";
3
3
  import { OpenAIEmbeddings } from "../protocols/openai-embeddings.js";
4
4
  import { OpenAIImages } from "../protocols/openai-images.js";
@@ -23,15 +23,13 @@ export const configure = (input) => {
23
23
  provider,
24
24
  endpoint: { baseURL },
25
25
  auth,
26
- headers: input.headers,
27
- http: input.http,
26
+ ...mergeTransport(input),
28
27
  });
29
28
  const decisionRoute = SystemOne.route.with({
30
29
  provider,
31
30
  endpoint: { baseURL },
32
31
  auth,
33
- headers: input.headers,
34
- http: input.http,
32
+ ...mergeTransport(input),
35
33
  });
36
34
  return {
37
35
  id: ProviderID.make(provider),
@@ -1,6 +1,6 @@
1
1
  import { AuthOptions } from "../route/auth-options.js";
2
2
  import { MediaRoute } from "../route/media.js";
3
- import { ProviderID, ToolDefinition, } from "../schema/index.js";
3
+ import { mergeTransport, ProviderID, ToolDefinition, } from "../schema/index.js";
4
4
  import * as OpenAIChat from "../protocols/openai-chat.js";
5
5
  import * as OpenAIResponses from "../protocols/openai-responses.js";
6
6
  import { withOpenAIOptions } from "./openai-options.js";
@@ -47,11 +47,7 @@ export const configure = (input = {}) => {
47
47
  id,
48
48
  capabilities: { promptCacheKey: true },
49
49
  });
50
- const deployment = MediaRoute.deployment(input, auth(input));
51
- const media = {
52
- ...deployment,
53
- http: { ...deployment.http, query: { ...deployment.http?.query, ...input.queryParams } },
54
- };
50
+ const media = MediaRoute.deployment({ ...mergeTransport(input, { http: { query: input.queryParams } }), baseURL: input.baseURL }, auth(input));
55
51
  // The ChatGPT backend (ChatGPT sign-in) rejects multipart image edits with "Unsupported content type".
56
52
  const imageInput = media.baseURL?.startsWith("https://chatgpt.com/") ? "json-edits" : undefined;
57
53
  const image = (modelID, providerOptions) => OpenAIImages.model({ ...media, id: modelID, providerOptions, imageInput });
@@ -60,15 +56,13 @@ export const configure = (input = {}) => {
60
56
  const decisionRoute = OpenAIDecisions.route.with({
61
57
  endpoint: { baseURL: media.baseURL },
62
58
  auth: media.auth,
63
- headers: media.headers,
64
- http: media.http,
59
+ ...media.transport,
65
60
  });
66
61
  const decision = (modelID, providerOptions) => decisionRoute.model({ id: modelID, providerOptions });
67
62
  const embeddingRoute = OpenAIEmbeddings.route.with({
68
63
  endpoint: { baseURL: media.baseURL },
69
64
  auth: media.auth,
70
- headers: media.headers,
71
- http: media.http,
65
+ ...media.transport,
72
66
  });
73
67
  const embedding = (modelID, providerOptions) => embeddingRoute.model({ id: modelID, providerOptions });
74
68
  return {
@@ -1,11 +1,9 @@
1
- import { type HttpOptions, type ModelID, type ProviderOptions } from "../schema/index.js";
1
+ import { type ModelID, type ProviderOptions, type TransportOptions } from "../schema/index.js";
2
2
  import type { ProviderPackage } from "../provider-package.js";
3
3
  import { type ProviderAuthOption } from "../route/auth-options.js";
4
4
  export declare const id: string & import("effect/Brand").Brand<"AI.ProviderID">;
5
- export type Config = ProviderAuthOption<"optional"> & {
5
+ export type Config = TransportOptions & ProviderAuthOption<"optional"> & {
6
6
  readonly baseURL?: string;
7
- readonly headers?: Record<string, string>;
8
- readonly http?: HttpOptions;
9
7
  };
10
8
  export type Settings = ProviderPackage.Settings & {
11
9
  readonly apiKey?: string | false;
@@ -1,4 +1,4 @@
1
- import { ProviderID } from "../schema/index.js";
1
+ import { mergeTransport, ProviderID, } from "../schema/index.js";
2
2
  import { AuthOptions } from "../route/auth-options.js";
3
3
  import { SystemOne } from "../protocols/system-one.js";
4
4
  import { toConfig } from "./settings.js";
@@ -11,8 +11,7 @@ export const configure = (input = {}) => {
11
11
  endpoint: { baseURL: input.baseURL ?? baseURL },
12
12
  auth: AuthOptions.bearer(input, "OPENCODE_API_KEY"),
13
13
  // Zen forwards TypeSafe's image opt-in header; text-only requests are unchanged with it.
14
- headers: { "TypeSafe-Danger-Zone-Images": "true", ...input.headers },
15
- http: input.http,
14
+ ...mergeTransport({ headers: { "TypeSafe-Danger-Zone-Images": "true" } }, input),
16
15
  });
17
16
  const decision = (modelID, providerOptions) => route.model({ id: modelID, providerOptions });
18
17
  return { id, decision, configure };
@@ -2,7 +2,7 @@ import { Effect, Schema } from "effect";
2
2
  import { Route } from "../route/client.js";
3
3
  import { Endpoint } from "../route/endpoint.js";
4
4
  import { Protocol } from "../route/protocol.js";
5
- import { ProviderID, } from "../schema/index.js";
5
+ import { mergeTransport, ProviderID, } from "../schema/index.js";
6
6
  import { SystemOne } from "../protocols/system-one.js";
7
7
  import { OpenAIEmbeddings } from "../protocols/openai-embeddings.js";
8
8
  import { OpenAIImages } from "../protocols/openai-images.js";
@@ -144,8 +144,7 @@ export const configure = (input = {}) => {
144
144
  providerMetadataKey: "openrouter",
145
145
  endpoint: options.endpoint,
146
146
  auth: options.auth,
147
- headers: input.headers,
148
- http: input.http,
147
+ ...mergeTransport(input),
149
148
  });
150
149
  const media = MediaRoute.deployment({ ...input, baseURL: options.endpoint.baseURL }, options.auth);
151
150
  const image = (modelID, providerOptions) => OpenAIImages.model({
@@ -166,8 +165,7 @@ export const configure = (input = {}) => {
166
165
  provider: id,
167
166
  endpoint: options.endpoint,
168
167
  auth: options.auth,
169
- headers: input.headers,
170
- http: input.http,
168
+ ...mergeTransport(input),
171
169
  });
172
170
  const embedding = (modelID, providerOptions) => embeddingRoute.model({ id: modelID, providerOptions });
173
171
  const chat = (modelID) => chatRoute.model({ id: modelID, capabilities: { promptCacheKey: true } });
@@ -1,11 +1,9 @@
1
- import { type HttpOptions, type ModelID, type ProviderOptions } from "../schema/index.js";
1
+ import { type ModelID, type ProviderOptions, type TransportOptions } from "../schema/index.js";
2
2
  import type { ProviderPackage } from "../provider-package.js";
3
3
  import { type ProviderAuthOption } from "../route/auth-options.js";
4
4
  export declare const id: string & import("effect/Brand").Brand<"AI.ProviderID">;
5
- export type Config = ProviderAuthOption<"optional"> & {
5
+ export type Config = TransportOptions & ProviderAuthOption<"optional"> & {
6
6
  readonly baseURL?: string;
7
- readonly headers?: Record<string, string>;
8
- readonly http?: HttpOptions;
9
7
  };
10
8
  export type Settings = ProviderPackage.Settings & {
11
9
  readonly apiKey?: string | false;
@@ -1,4 +1,4 @@
1
- import { ProviderID } from "../schema/index.js";
1
+ import { mergeTransport, ProviderID, } from "../schema/index.js";
2
2
  import { AuthOptions } from "../route/auth-options.js";
3
3
  import { SystemOne } from "../protocols/system-one.js";
4
4
  import { toConfig } from "./settings.js";
@@ -11,8 +11,7 @@ export const configure = (input = {}) => {
11
11
  endpoint: { baseURL: input.baseURL ?? baseURL },
12
12
  auth: AuthOptions.bearer(input, "TYPESAFE_API_KEY"),
13
13
  // Required for images; text-only requests are unchanged with it.
14
- headers: { "TypeSafe-Danger-Zone-Images": "true", ...input.headers },
15
- http: input.http,
14
+ ...mergeTransport({ headers: { "TypeSafe-Danger-Zone-Images": "true" } }, input),
16
15
  });
17
16
  const decision = (modelID, providerOptions) => route.model({ id: modelID, providerOptions });
18
17
  return { id, decision, configure };
@@ -12,7 +12,7 @@ import { Route } from "../route/client.js";
12
12
  import { Endpoint } from "../route/endpoint.js";
13
13
  import { Framing } from "../route/framing.js";
14
14
  import { MediaRoute } from "../route/media.js";
15
- import { ModelID, ProviderID, ProviderMetadata, ReasoningEffort, } from "../schema/index.js";
15
+ import { mergeTransport, ModelID, ProviderID, ProviderMetadata, ReasoningEffort, } from "../schema/index.js";
16
16
  import { bearerRouteDefaults, toConfig } from "./settings.js";
17
17
  export const id = ProviderID.make("vercel-ai-gateway");
18
18
  const baseURL = "https://ai-gateway.vercel.sh/v1";
@@ -143,16 +143,14 @@ export const configure = (input = {}) => {
143
143
  provider: id,
144
144
  endpoint: { baseURL: configured.endpoint.baseURL },
145
145
  auth: configured.auth,
146
- headers: input.headers,
147
- http: input.http,
146
+ ...mergeTransport(input),
148
147
  });
149
148
  const decision = (modelID, providerOptions) => decisionRoute.model({ id: modelID, providerOptions });
150
149
  const embeddingRoute = OpenAIEmbeddings.route.with({
151
150
  provider: id,
152
151
  endpoint: { baseURL: configured.endpoint.baseURL },
153
152
  auth: configured.auth,
154
- headers: input.headers,
155
- http: input.http,
153
+ ...mergeTransport(input),
156
154
  });
157
155
  const embedding = (modelID, providerOptions) => embeddingRoute.model({ id: modelID, providerOptions });
158
156
  return { id, model, messages, responses, chat, image, decision, embedding, configure };
@@ -1,11 +1,9 @@
1
1
  import type { ProviderPackage } from "../provider-package.js";
2
2
  import { type ProviderAuthOption } from "../route/auth-options.js";
3
- import { type HttpOptions, type ModelID, type ProviderOptions } from "../schema/index.js";
3
+ import { type ModelID, type ProviderOptions, type TransportOptions } from "../schema/index.js";
4
4
  export declare const id: string & import("effect/Brand").Brand<"AI.ProviderID">;
5
- export type Config = ProviderAuthOption<"optional"> & {
5
+ export type Config = TransportOptions & ProviderAuthOption<"optional"> & {
6
6
  readonly baseURL?: string;
7
- readonly headers?: Record<string, string>;
8
- readonly http?: HttpOptions;
9
7
  };
10
8
  export type Settings = ProviderPackage.Settings & {
11
9
  readonly apiKey?: string | false;
@@ -1,7 +1,7 @@
1
1
  import { Effect } from "effect";
2
2
  import type { HttpClientResponse } from "effect/http";
3
3
  import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type DecisionRequest, type DecisionResponse } from "../decision.js";
4
- import { ProviderID, type HttpOptions, type AIError, type ProviderOptions } from "../schema/index.js";
4
+ import { ProviderID, type AIError, type ProviderOptions, type TransportOptions } from "../schema/index.js";
5
5
  import { Auth } from "./auth.js";
6
6
  import { Endpoint } from "./endpoint.js";
7
7
  import { MediaProtocol } from "./media-protocol.js";
@@ -22,25 +22,22 @@ export interface Protocol {
22
22
  };
23
23
  }
24
24
  export declare const protocol: (input: Protocol) => Protocol;
25
- export interface Patch {
25
+ /** Headers, timeouts and `http` overlays merge into the route's; every model of the route starts from them. */
26
+ export interface Patch extends TransportOptions {
26
27
  readonly id?: string;
27
28
  readonly provider?: string | ProviderID;
28
29
  /** ProviderMetadata namespace for responses; defaults to the provider. */
29
30
  readonly providerMetadataKey?: string;
30
31
  readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, DecisionRequest>;
31
32
  readonly auth?: Auth.Definition;
32
- readonly headers?: Record<string, string>;
33
- readonly http?: HttpOptions;
34
33
  }
35
- export interface Route {
34
+ export interface Route extends TransportOptions {
36
35
  readonly id: string;
37
36
  readonly provider: ProviderID;
38
37
  readonly providerMetadataKey: string;
39
38
  readonly protocol: Protocol;
40
39
  readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
41
40
  readonly auth: Auth.Definition;
42
- readonly headers?: Record<string, string>;
43
- readonly http?: HttpOptions;
44
41
  readonly with: (patch: Patch) => Route;
45
42
  readonly model: <Options extends DecisionOptions = DecisionOptions>(input: {
46
43
  readonly id: string;
@@ -1,9 +1,9 @@
1
1
  import { Effect } from "effect";
2
2
  import { DecisionModel, DecisionModelCompatibility, } from "../decision.js";
3
- import { ProviderID } from "../schema/index.js";
3
+ import { mergeTransport, ProviderID, } from "../schema/index.js";
4
4
  import { Auth } from "./auth.js";
5
5
  import { Endpoint } from "./endpoint.js";
6
- import { RequestExecutorService } from "./executor-service.js";
6
+ import { RequestExecutor } from "./executor.js";
7
7
  import { MediaProtocol } from "./media-protocol.js";
8
8
  import { MediaRoute } from "./media.js";
9
9
  export const protocol = (input) => input;
@@ -16,8 +16,7 @@ export const make = (input) => {
16
16
  protocol: input.protocol,
17
17
  endpoint: input.endpoint,
18
18
  auth: input.auth ?? Auth.none,
19
- headers: input.headers,
20
- http: input.http,
19
+ ...mergeTransport(input),
21
20
  };
22
21
  return {
23
22
  ...route,
@@ -27,15 +26,14 @@ export const make = (input) => {
27
26
  // A new provider gets its own metadata key unless the patch names one.
28
27
  providerMetadataKey: patch.providerMetadataKey ?? (patch.provider === undefined ? route.providerMetadataKey : undefined),
29
28
  endpoint: patch.endpoint === undefined ? route.endpoint : Endpoint.merge(route.endpoint, patch.endpoint),
30
- headers: patch.headers === undefined ? route.headers : { ...route.headers, ...patch.headers },
31
- http: patch.http ?? route.http,
29
+ ...mergeTransport(route, patch),
32
30
  }),
33
31
  model: (model) => {
34
32
  const send = transport(route);
35
33
  return DecisionModel.make({
36
34
  id: model.id,
37
35
  provider,
38
- http: route.http,
36
+ transport: mergeTransport(route),
39
37
  providerOptions: model.providerOptions,
40
38
  compatibility: model.compatibility === undefined ? undefined : DecisionModelCompatibility.make(model.compatibility),
41
39
  route: { id: route.id, generate: send },
@@ -60,16 +58,16 @@ const transport = (route) => {
60
58
  }),
61
59
  endpoint: route.endpoint,
62
60
  auth: route.auth,
63
- headers: route.headers,
64
61
  });
65
62
  return Effect.fnUntraced(function* (request, execute) {
66
63
  if (!route.protocol.inlineMedia)
67
64
  return yield* composed.generate(request, execute);
68
- const input = yield* Effect.forEach(request.input, (part) => materialize(part, execute));
65
+ const timed = RequestExecutor.withTimeout(execute, mergeTransport(request.model.transport, request).timeout);
66
+ const input = yield* Effect.forEach(request.input, (part) => materialize(part, timed));
69
67
  return yield* composed.generate({ ...request, input }, execute);
70
68
  });
71
69
  };
72
70
  const materialize = (part, execute) => part.type === "media" && part.media.source.type === "url"
73
- ? part.media.materialize().pipe(Effect.provideService(RequestExecutorService, { execute }), Effect.map((media) => ({ ...part, media })))
71
+ ? part.media.materialize().pipe(Effect.provideService(RequestExecutor.Service, { execute }), Effect.map((media) => ({ ...part, media })))
74
72
  : Effect.succeed(part);
75
73
  export * as DecisionRoute from "./decision.js";
@@ -1,7 +1,7 @@
1
1
  import { Effect } from "effect";
2
2
  import type { HttpClientResponse } from "effect/http";
3
3
  import { EmbeddingModel, EmbeddingResponse, type EmbeddingOptions, type EmbeddingRequest } from "../embedding.js";
4
- import { ProviderID, type HttpOptions, type AIError, type ProviderOptions } from "../schema/index.js";
4
+ import { ProviderID, type AIError, type ProviderOptions, type TransportOptions } from "../schema/index.js";
5
5
  import { Auth } from "./auth.js";
6
6
  import { Endpoint } from "./endpoint.js";
7
7
  import { MediaProtocol } from "./media-protocol.js";
@@ -20,30 +20,27 @@ export interface Protocol {
20
20
  };
21
21
  }
22
22
  export declare const protocol: (input: Protocol) => Protocol;
23
- export interface Patch {
23
+ /** Headers, timeouts and `http` overlays merge into the route's; every model of the route starts from them. */
24
+ export interface Patch extends TransportOptions {
24
25
  readonly id?: string;
25
26
  readonly provider?: string | ProviderID;
26
27
  /** ProviderMetadata namespace for responses; defaults to the provider. */
27
28
  readonly providerMetadataKey?: string;
28
29
  readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, EmbeddingRequest>;
29
30
  readonly auth?: Auth.Definition;
30
- readonly headers?: Record<string, string>;
31
- readonly http?: HttpOptions;
32
31
  /**
33
32
  * Most values one HTTP request may carry. Longer requests are split into requests of this size, sent a few at a
34
33
  * time, and joined back in value order.
35
34
  */
36
35
  readonly maxValues?: number;
37
36
  }
38
- export interface Route {
37
+ export interface Route extends TransportOptions {
39
38
  readonly id: string;
40
39
  readonly provider: ProviderID;
41
40
  readonly providerMetadataKey: string;
42
41
  readonly protocol: Protocol;
43
42
  readonly endpoint: Endpoint.Definition<MediaProtocol.Body, EmbeddingRequest>;
44
43
  readonly auth: Auth.Definition;
45
- readonly headers?: Record<string, string>;
46
- readonly http?: HttpOptions;
47
44
  readonly maxValues?: number;
48
45
  readonly with: (patch: Patch) => Route;
49
46
  readonly model: <Options extends EmbeddingOptions = EmbeddingOptions>(input: {
@@ -1,6 +1,6 @@
1
1
  import { Effect } from "effect";
2
2
  import { EmbeddingModel, EmbeddingResponse, EmbeddingUsage, } from "../embedding.js";
3
- import { ProviderID } from "../schema/index.js";
3
+ import { mergeTransport, ProviderID, } from "../schema/index.js";
4
4
  import { Auth } from "./auth.js";
5
5
  import { Endpoint } from "./endpoint.js";
6
6
  import { MediaProtocol } from "./media-protocol.js";
@@ -15,8 +15,7 @@ export const make = (input) => {
15
15
  protocol: input.protocol,
16
16
  endpoint: input.endpoint,
17
17
  auth: input.auth ?? Auth.none,
18
- headers: input.headers,
19
- http: input.http,
18
+ ...mergeTransport(input),
20
19
  maxValues: input.maxValues,
21
20
  };
22
21
  return {
@@ -27,14 +26,13 @@ export const make = (input) => {
27
26
  // A new provider gets its own metadata key unless the patch names one.
28
27
  providerMetadataKey: patch.providerMetadataKey ?? (patch.provider === undefined ? route.providerMetadataKey : undefined),
29
28
  endpoint: patch.endpoint === undefined ? route.endpoint : Endpoint.merge(route.endpoint, patch.endpoint),
30
- headers: patch.headers === undefined ? route.headers : { ...route.headers, ...patch.headers },
31
- http: patch.http ?? route.http,
29
+ ...mergeTransport(route, patch),
32
30
  maxValues: patch.maxValues ?? route.maxValues,
33
31
  }),
34
32
  model: (model) => EmbeddingModel.make({
35
33
  id: model.id,
36
34
  provider,
37
- http: route.http,
35
+ transport: mergeTransport(route),
38
36
  providerOptions: model.providerOptions,
39
37
  route: { id: route.id, generate: split(route.maxValues, transport(route).generate) },
40
38
  }),
@@ -83,6 +81,5 @@ const transport = (route) => MediaRoute.inline({
83
81
  }),
84
82
  endpoint: route.endpoint,
85
83
  auth: route.auth,
86
- headers: route.headers,
87
84
  });
88
85
  export * as EmbeddingRoute from "./embedding.js";
@@ -1,7 +1,7 @@
1
1
  import { Context, Effect, Stream } from "effect";
2
- import type { Headers, HttpClientRequest, HttpClientResponse } from "effect/http";
3
- import { HttpContext, type AIError } from "../schema/errors.js";
4
- import type { ClientTls } from "../schema/options.js";
2
+ import { type Headers, type HttpClientRequest, type HttpClientResponse } from "effect/http";
3
+ import { AIError, HttpContext } from "../schema/errors.js";
4
+ import type { ClientTls, RequestTimeout } from "../schema/options.js";
5
5
  export interface Interface {
6
6
  readonly execute: (request: HttpClientRequest.HttpClientRequest, middleware?: HttpMiddleware) => Effect.Effect<HttpClientResponse.HttpClientResponse, AIError>;
7
7
  }
@@ -16,6 +16,12 @@ export declare const Service: typeof RequestExecutorService;
16
16
  * client presents it, since only the runtime's fetch can.
17
17
  */
18
18
  export declare const CurrentTls: Context.Reference<ClientTls | undefined>;
19
+ /**
20
+ * Per-phase HTTP timeouts for requests executed while it is set. Transports set it from `request.timeout`;
21
+ * `RequestExecutor` applies defaults (`DEFAULT_HTTP_TIMEOUT_MS` for headers, `DEFAULT_CHUNK_TIMEOUT_MS` between
22
+ * response chunks) whenever a phase is left unset.
23
+ */
24
+ export declare const CurrentTimeout: Context.Reference<RequestTimeout | undefined>;
19
25
  export type Service = RequestExecutorService;
20
26
  /**
21
27
  * Middleware for every provider request made while it is set: model calls, media submits, polls, results and cancels,
@@ -34,4 +40,15 @@ export declare const headerDetails: (headers: Headers.Headers) => {
34
40
  [k: string]: string;
35
41
  };
36
42
  export declare const responseHttp: (response: HttpClientResponse.HttpClientResponse) => HttpContext;
43
+ export type HttpOperation = "request" | "read";
44
+ export declare const rootCause: (error: unknown) => unknown;
45
+ export declare const httpError: (input: {
46
+ readonly error: unknown;
47
+ readonly request: HttpClientRequest.HttpClientRequest;
48
+ readonly operation: HttpOperation;
49
+ readonly http?: HttpContext;
50
+ }) => AIError;
51
+ export declare const responseStream: (response: HttpClientResponse.HttpClientResponse) => Stream.Stream<Uint8Array, AIError>;
52
+ export declare const responseBytes: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<Uint8Array, AIError>;
53
+ export declare const responseText: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<string, AIError>;
37
54
  export {};
@@ -1,5 +1,6 @@
1
- import { Context, Effect, Stream } from "effect";
2
- import { HttpContext } from "../schema/errors.js";
1
+ import { Cause, Context, Effect, Option, Schema, Stream } from "effect";
2
+ import { HttpClientError } from "effect/http";
3
+ import { AIError, HttpContext, TransportError } from "../schema/errors.js";
3
4
  export class RequestExecutorService extends Context.Service()("@opencode/AI/RequestExecutor") {
4
5
  }
5
6
  export const Service = RequestExecutorService;
@@ -10,6 +11,12 @@ export const Service = RequestExecutorService;
10
11
  export const CurrentTls = Context.Reference("@opencode/AI/RequestExecutor/CurrentTls", {
11
12
  defaultValue: () => undefined,
12
13
  });
14
+ /**
15
+ * Per-phase HTTP timeouts for requests executed while it is set. Transports set it from `request.timeout`;
16
+ * `RequestExecutor` applies defaults (`DEFAULT_HTTP_TIMEOUT_MS` for headers, `DEFAULT_CHUNK_TIMEOUT_MS` between
17
+ * response chunks) whenever a phase is left unset.
18
+ */
19
+ export const CurrentTimeout = Context.Reference("@opencode/AI/RequestExecutor/CurrentTimeout", { defaultValue: () => undefined });
13
20
  /**
14
21
  * Middleware for every provider request made while it is set: model calls, media submits, polls, results and cancels,
15
22
  * whatever the modality. Set it with {@link withMiddleware}. Downloads of a `url` asset don't use it; they use the
@@ -43,3 +50,66 @@ export const responseHttp = (response) => new HttpContext({
43
50
  status: response.status,
44
51
  headers: headerDetails(response.headers),
45
52
  });
53
+ const NativeTransportFailure = Schema.Struct({
54
+ message: Schema.String,
55
+ code: Schema.optionalKey(Schema.String),
56
+ cause: Schema.optionalKey(Schema.Unknown),
57
+ });
58
+ const decodeNativeTransportFailure = Schema.decodeUnknownOption(NativeTransportFailure);
59
+ const nativeTransportFailure = (error) => {
60
+ const failure = Option.getOrUndefined(decodeNativeTransportFailure(error));
61
+ if (!failure)
62
+ return undefined;
63
+ if (failure.code !== undefined)
64
+ return failure;
65
+ const cause = Option.getOrUndefined(decodeNativeTransportFailure(failure.cause));
66
+ if (cause?.code !== undefined)
67
+ return cause;
68
+ return failure;
69
+ };
70
+ // HTTP hooks re-wrap response bodies, so a read failure can arrive as an HttpClientError caused by
71
+ // another HttpClientError. The innermost cause carries the native failure.
72
+ export const rootCause = (error) => HttpClientError.isHttpClientError(error) && "cause" in error.reason && error.reason.cause !== undefined
73
+ ? rootCause(error.reason.cause)
74
+ : error;
75
+ const CONNECTION_LOST = "Connection lost while reading the response";
76
+ export const httpError = (input) => {
77
+ const request = HttpClientError.isHttpClientError(input.error) ? input.error.request : input.request;
78
+ const transportError = (failure) => new AIError({
79
+ reason: new TransportError({
80
+ message: failure.message,
81
+ cause: source,
82
+ http: input.http,
83
+ transport: "http",
84
+ operation: input.operation,
85
+ code: failure.code,
86
+ url: request.url,
87
+ }),
88
+ });
89
+ const source = rootCause(input.error);
90
+ const native = nativeTransportFailure(source);
91
+ const code = native?.code;
92
+ const detail = code && native?.message && !native.message.includes(code) ? `${code}: ${native.message}` : native?.message;
93
+ const message = detail ?? (input.error instanceof Error ? input.error.message : undefined);
94
+ if (Cause.isTimeoutError(input.error) || Cause.isTimeoutError(source))
95
+ return transportError({ message: message ?? "HTTP transport timed out", code: code ?? "Timeout" });
96
+ if (!HttpClientError.isHttpClientError(input.error))
97
+ return transportError({ message: message ?? "HTTP transport failed", code });
98
+ // Effect reports every response body read failure as a DecodeError, but the raw byte stream decodes
99
+ // nothing: provider output parsing happens later and fails as InvalidProviderOutput.
100
+ if (input.operation === "read" && input.error.reason._tag === "DecodeError")
101
+ return transportError({ message: detail ? `${CONNECTION_LOST}: ${detail}` : CONNECTION_LOST, code });
102
+ if (input.error.reason._tag === "TransportError") {
103
+ return transportError({
104
+ message: message ?? input.error.reason.description ?? "HTTP transport failed",
105
+ code: code ?? input.error.reason._tag,
106
+ });
107
+ }
108
+ return transportError({
109
+ message: message ?? `HTTP transport failed: ${input.error.reason._tag}`,
110
+ code: code ?? input.error.reason._tag,
111
+ });
112
+ };
113
+ export const responseStream = (response) => response.stream.pipe(Stream.mapError((error) => httpError({ error, request: response.request, operation: "read", http: responseHttp(response) })));
114
+ export const responseBytes = (response) => response.arrayBuffer.pipe(Effect.map((buffer) => new Uint8Array(buffer)), Effect.mapError((error) => httpError({ error, request: response.request, operation: "read", http: responseHttp(response) })));
115
+ export const responseText = (response) => response.text.pipe(Effect.mapError((error) => httpError({ error, request: response.request, operation: "read", http: responseHttp(response) })));