@opencode/ai 0.0.0-dev-1311 → 0.0.0-dev-1313

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 (58) 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 +2 -1
  7. package/dist/experimental/realtime.js +8 -8
  8. package/dist/image.js +1 -1
  9. package/dist/media-model.d.ts +5 -4
  10. package/dist/media-model.js +5 -4
  11. package/dist/media.js +4 -17
  12. package/dist/protocols/open-responses-continuation.js +2 -0
  13. package/dist/protocols/utils/responses-compaction.js +3 -3
  14. package/dist/providers/alibaba.js +2 -3
  15. package/dist/providers/assemblyai.d.ts +2 -4
  16. package/dist/providers/azure.js +5 -11
  17. package/dist/providers/baseten.js +2 -3
  18. package/dist/providers/byteplus.d.ts +2 -4
  19. package/dist/providers/cartesia.d.ts +2 -4
  20. package/dist/providers/cloudflare-ai-gateway.js +3 -3
  21. package/dist/providers/cloudflare-workers-ai.js +2 -3
  22. package/dist/providers/cohere.js +2 -3
  23. package/dist/providers/deepgram.d.ts +2 -4
  24. package/dist/providers/deepinfra.js +2 -3
  25. package/dist/providers/digitalocean.js +3 -5
  26. package/dist/providers/elevenlabs.d.ts +2 -4
  27. package/dist/providers/fireworks.js +2 -3
  28. package/dist/providers/google-vertex.js +6 -2
  29. package/dist/providers/google.js +7 -4
  30. package/dist/providers/openai-compatible.js +3 -5
  31. package/dist/providers/openai.js +4 -10
  32. package/dist/providers/opencode.d.ts +2 -4
  33. package/dist/providers/opencode.js +2 -3
  34. package/dist/providers/openrouter.js +3 -5
  35. package/dist/providers/typesafe-ai.d.ts +2 -4
  36. package/dist/providers/typesafe-ai.js +2 -3
  37. package/dist/providers/vercel-ai-gateway.js +3 -5
  38. package/dist/providers/volcengine.d.ts +2 -4
  39. package/dist/route/decision.d.ts +4 -7
  40. package/dist/route/decision.js +8 -10
  41. package/dist/route/embedding.d.ts +4 -7
  42. package/dist/route/embedding.js +4 -7
  43. package/dist/route/executor-service.d.ts +20 -3
  44. package/dist/route/executor-service.js +72 -2
  45. package/dist/route/executor.d.ts +5 -5
  46. package/dist/route/executor.js +68 -73
  47. package/dist/route/media-protocol.d.ts +2 -1
  48. package/dist/route/media-protocol.js +2 -10
  49. package/dist/route/media.d.ts +6 -8
  50. package/dist/route/media.js +8 -8
  51. package/dist/route/transport/http.d.ts +1 -2
  52. package/dist/route/transport/http.js +5 -28
  53. package/dist/schema/errors.js +2 -1
  54. package/dist/schema/options.d.ts +1 -1
  55. package/dist/speech.js +1 -1
  56. package/dist/transcription.js +1 -1
  57. package/dist/video.js +1 -1
  58. package/package.json +3 -3
@@ -2,7 +2,7 @@ import { OpenAIChat } from "../protocols/openai-chat.js";
2
2
  import { OpenAIEmbeddings } from "../protocols/openai-embeddings.js";
3
3
  import { Route } from "../route/client.js";
4
4
  import { Endpoint } from "../route/endpoint.js";
5
- import { ProviderID } from "../schema/index.js";
5
+ import { mergeTransport, ProviderID } from "../schema/index.js";
6
6
  import { bearerRouteDefaults, toConfig } from "./settings.js";
7
7
  export const id = ProviderID.make("fireworks");
8
8
  const baseURL = "https://api.fireworks.ai/inference/v1";
@@ -22,8 +22,7 @@ export const configure = (input = {}) => {
22
22
  provider: id,
23
23
  endpoint: options.endpoint,
24
24
  auth: options.auth,
25
- headers: input.headers,
26
- http: input.http,
25
+ ...mergeTransport(input),
27
26
  });
28
27
  return {
29
28
  id,
@@ -9,7 +9,7 @@ import { Route } from "../route/client.js";
9
9
  import { EmbeddingRoute } from "../route/embedding.js";
10
10
  import { Endpoint } from "../route/endpoint.js";
11
11
  import { MediaRoute } from "../route/media.js";
12
- import { ProviderConfigurationError, ProviderID, } from "../schema/index.js";
12
+ import { mergeTransport, ProviderConfigurationError, ProviderID, } from "../schema/index.js";
13
13
  import { GoogleVertexShared } from "./google-vertex-shared.js";
14
14
  export const id = ProviderID.make("google-vertex");
15
15
  const fromRequest = Effect.fn("GoogleVertex.fromRequest")(function* (request) {
@@ -121,7 +121,11 @@ export const configure = (input = {}) => {
121
121
  message: "Google Vertex embedContent does not support Express Mode API keys; use project credentials",
122
122
  });
123
123
  return selected.route
124
- .with({ endpoint: target.endpoint, auth: target.auth, headers: input.headers, http: input.http })
124
+ .with({
125
+ endpoint: target.endpoint,
126
+ auth: target.auth,
127
+ ...mergeTransport(input),
128
+ })
125
129
  .model({ id: modelID, providerOptions });
126
130
  },
127
131
  configure,
@@ -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) })));
@@ -1,9 +1,8 @@
1
1
  import { Layer, Stream } from "effect";
2
- import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
3
- import { AIError } from "../schema/index.js";
2
+ import { HttpClient, HttpClientRequest } from "effect/http";
3
+ import { AIError, type ClientTls, type RequestTimeout } from "../schema/index.js";
4
4
  import { Service, type HttpMiddleware, type Interface } from "./executor-service.js";
5
- import type { ClientTls } from "../schema/options.js";
6
- export { composeMiddleware, CurrentMiddleware, CurrentTls, responseHttp, Service, withMiddleware, } from "./executor-service.js";
5
+ export { composeMiddleware, CurrentMiddleware, CurrentTimeout, CurrentTls, responseBytes, responseHttp, responseStream, responseText, Service, withMiddleware, } from "./executor-service.js";
7
6
  export type { HttpHandler, HttpMiddleware, Interface, RequestExecutorService } from "./executor-service.js";
8
7
  /** Preserve HTTP diagnostics for executor and externally captured failures alike. */
9
8
  export declare const httpFailure: (input: {
@@ -15,7 +14,6 @@ export declare const httpFailure: (input: {
15
14
  readonly responseBody?: string | undefined;
16
15
  readonly cause?: unknown;
17
16
  }) => AIError;
18
- export declare const responseStream: (response: HttpClientResponse.HttpClientResponse) => Stream.Stream<Uint8Array, AIError>;
19
17
  export declare const stream: (executor: Interface, request: HttpClientRequest.HttpClientRequest, middleware?: HttpMiddleware) => Stream.Stream<Uint8Array, AIError>;
20
18
  export declare const layer: Layer.Layer<Service, never, HttpClient.HttpClient>;
21
19
  export declare const fetchLayer: Layer.Layer<import("./executor-service.js").RequestExecutorService, never, never>;
@@ -24,6 +22,8 @@ export declare const fetchLayer: Layer.Layer<import("./executor-service.js").Req
24
22
  * downloads made through the plain executor never carry the certificate. The executor's HTTP client reads `CurrentTls`.
25
23
  */
26
24
  export declare const withTls: (execute: Interface["execute"], tls: ClientTls | undefined) => Interface["execute"];
25
+ /** Sends through `execute` with `timeout` applied to the request and its response body. */
26
+ export declare const withTimeout: (execute: Interface["execute"], timeout: RequestTimeout | undefined) => Interface["execute"];
27
27
  /**
28
28
  * Per-call middleware that sends through `fetch` instead of the executor's HTTP client. Executor middleware and
29
29
  * `middleware` still wrap the call, and status classification still applies.