@opencode/ai 0.0.0-dev-465 → 0.0.0-dev-468

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/README.md +27 -16
  2. package/dist/cache-policy.d.ts +1 -1
  3. package/dist/cache-policy.js +2 -2
  4. package/dist/decision-client.d.ts +3 -3
  5. package/dist/decision.d.ts +13 -120
  6. package/dist/decision.js +17 -13
  7. package/dist/effect.d.ts +8 -6
  8. package/dist/effect.js +4 -3
  9. package/dist/effort-updates.d.ts +1 -1
  10. package/dist/effort-updates.js +2 -2
  11. package/dist/experimental/cloudflare-run-extraction.d.ts +7 -3
  12. package/dist/experimental/cloudflare-run-extraction.js +4 -1
  13. package/dist/experimental/extraction.d.ts +12 -38
  14. package/dist/experimental/extraction.js +13 -11
  15. package/dist/experimental.d.ts +8 -8
  16. package/dist/experimental.js +4 -4
  17. package/dist/image-client.d.ts +11 -5
  18. package/dist/image-client.js +1 -1
  19. package/dist/image.d.ts +21 -62
  20. package/dist/image.js +16 -13
  21. package/dist/index.d.ts +20 -14
  22. package/dist/index.js +6 -3
  23. package/dist/llm.d.ts +60 -25
  24. package/dist/llm.js +126 -38
  25. package/dist/protocols/alibaba-images.d.ts +2 -2
  26. package/dist/protocols/alibaba-messages.js +3 -3
  27. package/dist/protocols/anthropic-messages.d.ts +1 -1
  28. package/dist/protocols/anthropic-messages.js +4 -3
  29. package/dist/protocols/assemblyai-transcription.d.ts +2 -2
  30. package/dist/protocols/cartesia-speech.d.ts +2 -2
  31. package/dist/protocols/cloudflare-workers-ai-images.d.ts +2 -2
  32. package/dist/protocols/deepgram-speech.d.ts +2 -2
  33. package/dist/protocols/deepgram-transcription.d.ts +2 -2
  34. package/dist/protocols/elevenlabs-speech.d.ts +2 -2
  35. package/dist/protocols/elevenlabs-transcription.d.ts +2 -2
  36. package/dist/protocols/gemini.js +2 -8
  37. package/dist/protocols/google-images.d.ts +2 -2
  38. package/dist/protocols/google-speech.d.ts +2 -2
  39. package/dist/protocols/google-transcription.d.ts +2 -2
  40. package/dist/protocols/google-video.d.ts +2 -2
  41. package/dist/protocols/meta-images.d.ts +2 -2
  42. package/dist/protocols/meta-responses.js +4 -3
  43. package/dist/protocols/minimax-images.d.ts +2 -2
  44. package/dist/protocols/open-responses.d.ts +1 -21
  45. package/dist/protocols/openai-chat.d.ts +1 -21
  46. package/dist/protocols/openai-chat.js +7 -9
  47. package/dist/protocols/openai-decisions.js +1 -1
  48. package/dist/protocols/openai-images.d.ts +2 -2
  49. package/dist/protocols/openai-responses.js +0 -1
  50. package/dist/protocols/openai-speech.d.ts +2 -2
  51. package/dist/protocols/openai-transcription.d.ts +2 -2
  52. package/dist/protocols/runway-video.d.ts +2 -2
  53. package/dist/protocols/shared.d.ts +1 -1
  54. package/dist/protocols/shared.js +2 -2
  55. package/dist/protocols/system-one.d.ts +4 -4
  56. package/dist/protocols/utils/responses-checkpoint.js +1 -1
  57. package/dist/protocols/utils/responses-compaction.d.ts +1 -1
  58. package/dist/protocols/utils/responses-compaction.js +4 -3
  59. package/dist/protocols/utils/tool-stream.d.ts +3 -63
  60. package/dist/protocols/xai-images.d.ts +2 -2
  61. package/dist/protocols/xai-video.d.ts +2 -2
  62. package/dist/protocols/zai-images.d.ts +2 -2
  63. package/dist/protocols/zai-messages.js +3 -3
  64. package/dist/provider-error.js +4 -4
  65. package/dist/providers/vercel-ai-gateway.js +4 -3
  66. package/dist/route/client.d.ts +43 -19
  67. package/dist/route/client.js +30 -18
  68. package/dist/route/decision.d.ts +6 -6
  69. package/dist/schema/errors.d.ts +1 -2
  70. package/dist/schema/errors.js +1 -2
  71. package/dist/schema/events.d.ts +61 -769
  72. package/dist/schema/events.js +3 -30
  73. package/dist/schema/messages.d.ts +20 -92
  74. package/dist/schema/messages.js +1 -52
  75. package/dist/speech-client.d.ts +5 -5
  76. package/dist/speech.d.ts +17 -19
  77. package/dist/speech.js +16 -12
  78. package/dist/testing.d.ts +8 -168
  79. package/dist/tool-runtime.js +12 -30
  80. package/dist/transcription-client.d.ts +5 -5
  81. package/dist/transcription.d.ts +17 -19
  82. package/dist/transcription.js +16 -12
  83. package/dist/video-client.d.ts +5 -5
  84. package/dist/video-client.js +1 -1
  85. package/dist/video.d.ts +18 -20
  86. package/dist/video.js +16 -12
  87. package/package.json +3 -3
@@ -10,7 +10,7 @@ import { normalizeToolHistory } from "../tool-history.js";
10
10
  import { sanitizeSurrogates } from "../utils/sanitize.js";
11
11
  import * as ProviderShared from "../protocols/shared.js";
12
12
  import { ToolSchemaProjection } from "../protocols/utils/tool-schema.js";
13
- import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, GenerationOptions, HttpOptions, LLMRequest, LLMResponse, LanguageModel, LLMEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, mergeGenerationOptions, mergeHttpOptions, mergeProviderOptions, withPartial, } from "../schema/index.js";
13
+ import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, GenerationOptions, HttpOptions, LLMResponse, LanguageModel, Message, LLMEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, ToolChoice, ToolEntry, mergeGenerationOptions, mergeHttpOptions, mergeProviderOptions, withPartial, } from "../schema/index.js";
14
14
  const makeRouteLanguageModel = (route, mapped) => {
15
15
  const provider = route.provider ?? ("provider" in mapped ? mapped.provider : undefined);
16
16
  if (!provider)
@@ -43,21 +43,22 @@ const mergeHeaders = (...items) => {
43
43
  return undefined;
44
44
  return Object.fromEntries(entries);
45
45
  };
46
- export function canCompact(request, options) {
46
+ export function canCompact(target, options) {
47
+ const model = target instanceof LanguageModel ? target : target.model;
47
48
  if (options?.mechanism === "trigger")
48
- return request.model.route.compact?.trigger !== undefined;
49
+ return model.route.compact?.trigger !== undefined;
49
50
  if (options?.mechanism !== undefined && options.mechanism !== "endpoint")
50
51
  return false;
51
- return request.model.route.compact?.endpoint !== undefined;
52
+ return model.route.compact?.endpoint !== undefined;
52
53
  }
53
- const unsupportedCompaction = (request, mechanism) => {
54
+ const unsupportedCompaction = (model, mechanism) => {
54
55
  if (mechanism !== undefined && mechanism !== "endpoint" && mechanism !== "trigger")
55
56
  return ProviderShared.invalidRequest(`Unknown compaction mechanism: ${mechanism}`);
56
57
  return ProviderShared.unsupportedOperation({
57
58
  operation: mechanism === "trigger" ? "compact.trigger" : "compact",
58
- provider: request.model.provider,
59
- route: request.model.route.id,
60
- message: `${request.model.provider}/${request.model.route.id} does not support ${mechanism === "trigger" ? "trigger" : "explicit"} compaction`,
59
+ provider: model.provider,
60
+ route: model.route.id,
61
+ message: `${model.provider}/${model.route.id} does not support ${mechanism === "trigger" ? "trigger" : "explicit"} compaction`,
61
62
  });
62
63
  };
63
64
  export class LLMClientService extends Context.Service()("@opencode/LLMClient") {
@@ -65,15 +66,16 @@ export class LLMClientService extends Context.Service()("@opencode/LLMClient") {
65
66
  export const Service = LLMClientService;
66
67
  const resolveRequestOptions = (request) => {
67
68
  const messages = normalizeToolHistory(request.messages);
68
- const normalized = messages === request.messages ? request : LLMRequest.update(request, { messages });
69
+ const normalized = messages === request.messages ? request : { ...request, messages };
69
70
  const routeDefaults = normalized.model.route.defaults;
70
71
  const modelDefaults = normalized.model.defaults;
71
72
  const generation = mergeGenerationOptions(routeDefaults.generation, modelDefaults?.generation, normalized.generation);
72
- return LLMRequest.update(normalized, {
73
+ return {
74
+ ...normalized,
73
75
  generation: generation ?? new GenerationOptions({}),
74
76
  providerOptions: mergeProviderOptions(routeDefaults.providerOptions, modelDefaults?.providerOptions, normalized.providerOptions),
75
77
  http: mergeHttpOptions(routeDefaults.http, modelDefaults?.http, normalized.http),
76
- });
78
+ };
77
79
  };
78
80
  const streamError = (route, message, cause) => {
79
81
  const failed = cause.reasons.find(Cause.isFailReason)?.error;
@@ -94,7 +96,7 @@ const requireTerminalEvent = (route) => (events) => Stream.suspend(() => {
94
96
  // Raw events carry provider frames that may trail the terminal event.
95
97
  if (terminal && !LLMEvent.is.raw(event))
96
98
  return Effect.fail(ProviderShared.eventError(route, `Provider emitted ${event.type} after the terminal event`));
97
- if (LLMEvent.is.finish(event) || LLMEvent.is.providerError(event))
99
+ if (LLMEvent.is.finish(event))
98
100
  terminal = true;
99
101
  return Effect.succeed(event);
100
102
  }), Stream.onEnd(Effect.suspend(() => (terminal ? Effect.void : Effect.fail(incompleteStreamError(route))))));
@@ -215,15 +217,24 @@ export function make(input) {
215
217
  }
216
218
  const prepareRequest = (request) => {
217
219
  const original = resolveRequestOptions(request);
218
- const sanitized = LLMRequest.update(original, sanitizeSurrogates({ ...LLMRequest.input(original), model: undefined }));
220
+ const sanitized = restoreClasses({ ...sanitizeSurrogates({ ...original, model: undefined }), model: original.model });
219
221
  // Deduplicate per sibling level; a tool and a namespace may share a name.
220
222
  const dedupe = (tools) => [...new Map(tools.map((tool) => [`${tool.type}:${tool.name}`, tool])).values()].map((tool) => tool.type === "tool" ? tool : { ...tool, tools: dedupe(tool.tools) });
221
- const resolved = applyCachePolicy(applyEffortUpdates(LLMRequest.update(sanitized, { tools: ToolSchemaProjection.tools(dedupe(sanitized.tools), sanitized.model) })));
223
+ const resolved = applyCachePolicy(applyEffortUpdates({ ...sanitized, tools: ToolSchemaProjection.tools(dedupe(sanitized.tools), sanitized.model) }));
222
224
  const headers = resolved.model.route.headers?.({ request: resolved });
223
225
  return headers === undefined
224
226
  ? resolved
225
- : LLMRequest.update(resolved, { http: mergeHttpOptions(new HttpOptions({ headers }), resolved.http) });
227
+ : { ...resolved, http: mergeHttpOptions(new HttpOptions({ headers }), resolved.http) };
226
228
  };
229
+ // Sanitizing copies changed class instances into plain records; rebuild them so protocols see the canonical classes.
230
+ const restoreClasses = (request) => ({
231
+ ...request,
232
+ messages: request.messages.map(Message.make),
233
+ tools: request.tools.map(ToolEntry.make),
234
+ toolChoice: request.toolChoice === undefined ? undefined : ToolChoice.make(request.toolChoice),
235
+ generation: request.generation === undefined ? undefined : GenerationOptions.make(request.generation),
236
+ http: HttpOptions.make(request.http),
237
+ });
227
238
  const compile = Effect.fn("LLM.compile")(function* (request, options) {
228
239
  const references = request.messages.flatMap((message) => message.content.flatMap((part) => {
229
240
  if (part.type === "media") {
@@ -281,14 +292,15 @@ export function generate(request, options) {
281
292
  return yield* (yield* Service).generate(request, options);
282
293
  });
283
294
  }
284
- export function compact(request, options) {
295
+ /** Compact through the client for `options.mechanism`, failing typed when the request's route does not support it. */
296
+ export function compactRequest(request, options) {
285
297
  return Effect.gen(function* () {
286
298
  const client = yield* Service;
287
299
  if (options?.mechanism === "trigger" && canCompact(request, options))
288
300
  return yield* client.compact(request, options);
289
301
  if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
290
302
  return yield* client.compact(request, options);
291
- return yield* unsupportedCompaction(request, options?.mechanism);
303
+ return yield* unsupportedCompaction(request.model, options?.mechanism);
292
304
  });
293
305
  }
294
306
  export const layer = Layer.effect(Service, Effect.gen(function* () {
@@ -300,7 +312,7 @@ export const layer = Layer.effect(Service, Effect.gen(function* () {
300
312
  return request.model.route.compact.trigger(prepareRequest(request), executor, options);
301
313
  if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
302
314
  return request.model.route.compact.endpoint(prepareRequest(request), executor, options);
303
- return unsupportedCompaction(request, options?.mechanism);
315
+ return unsupportedCompaction(request.model, options?.mechanism);
304
316
  });
305
317
  }
306
318
  return Service.of({
@@ -1,6 +1,6 @@
1
1
  import { Effect } from "effect";
2
2
  import type { HttpClientResponse } from "effect/http";
3
- import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type DecisionRequestFor, type DecisionResponse } from "../decision.js";
3
+ import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type DecisionRequest, type DecisionResponse } from "../decision.js";
4
4
  import { HttpOptions, ProviderID, type AIError, type ProviderOptions } from "../schema/index.js";
5
5
  import { Auth } from "./auth.js";
6
6
  import { Endpoint } from "./endpoint.js";
@@ -12,11 +12,11 @@ export interface Protocol {
12
12
  /** The host only takes inline data, so URL media is downloaded before `body.from`. */
13
13
  readonly inlineMedia?: boolean;
14
14
  readonly body: {
15
- readonly from: (request: DecisionRequestFor) => Effect.Effect<Record<string, unknown>, AIError>;
15
+ readonly from: (request: DecisionRequest) => Effect.Effect<Record<string, unknown>, AIError>;
16
16
  };
17
17
  readonly response: {
18
18
  readonly decode: (response: HttpClientResponse.HttpClientResponse, context: {
19
- readonly request: DecisionRequestFor;
19
+ readonly request: DecisionRequest;
20
20
  readonly providerMetadataKey: string;
21
21
  }) => Effect.Effect<DecisionResponse, AIError>;
22
22
  };
@@ -27,7 +27,7 @@ export interface Patch {
27
27
  readonly provider?: string | ProviderID;
28
28
  /** ProviderMetadata namespace for responses; defaults to the provider. */
29
29
  readonly providerMetadataKey?: string;
30
- readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, DecisionRequestFor>;
30
+ readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, DecisionRequest>;
31
31
  readonly auth?: Auth.Definition;
32
32
  readonly headers?: Record<string, string>;
33
33
  readonly http?: HttpOptions.Input;
@@ -37,7 +37,7 @@ export interface Route {
37
37
  readonly provider: ProviderID;
38
38
  readonly providerMetadataKey: string;
39
39
  readonly protocol: Protocol;
40
- readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequestFor>;
40
+ readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
41
41
  readonly auth: Auth.Definition;
42
42
  readonly headers?: Record<string, string>;
43
43
  readonly http?: HttpOptions;
@@ -52,7 +52,7 @@ export interface MakeInput extends Omit<Patch, "endpoint"> {
52
52
  readonly id: string;
53
53
  readonly provider: string | ProviderID;
54
54
  readonly protocol: Protocol;
55
- readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequestFor>;
55
+ readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
56
56
  }
57
57
  export declare const make: (input: MakeInput) => Route;
58
58
  export * as DecisionRoute from "./decision.js";
@@ -165,8 +165,7 @@ export declare const withPartial: (error: AIError, partial: LLMResponse | undefi
165
165
  /**
166
166
  * Failure type for tool execute handlers. Handlers must map their internal
167
167
  * errors to this shape; the runtime catches `ToolFailure`s and surfaces them
168
- * as `tool-error` events plus a `tool-result` of `type: "error"` so the model
169
- * can self-correct.
168
+ * as a `tool-result` of `type: "error"` so the model can self-correct.
170
169
  *
171
170
  * Anything thrown or yielded by a handler that is not a `ToolFailure` is
172
171
  * treated as a defect and fails the stream.
@@ -132,8 +132,7 @@ export const withPartial = (error, partial) => partial === undefined ? error : n
132
132
  /**
133
133
  * Failure type for tool execute handlers. Handlers must map their internal
134
134
  * errors to this shape; the runtime catches `ToolFailure`s and surfaces them
135
- * as `tool-error` events plus a `tool-result` of `type: "error"` so the model
136
- * can self-correct.
135
+ * as a `tool-result` of `type: "error"` so the model can self-correct.
137
136
  *
138
137
  * Anything thrown or yielded by a handler that is not a `ToolFailure` is
139
138
  * treated as a defect and fails the stream.