@opencode/ai 2.2.7 → 2.3.1

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 (158) hide show
  1. package/README.md +164 -99
  2. package/dist/cache-policy.js +4 -2
  3. package/dist/effect.d.ts +11 -8
  4. package/dist/effect.js +2 -1
  5. package/dist/experimental.d.ts +0 -4
  6. package/dist/experimental.js +0 -3
  7. package/dist/generation.d.ts +10 -10
  8. package/dist/generation.js +8 -14
  9. package/dist/image-client.d.ts +56 -59
  10. package/dist/image-client.js +47 -12
  11. package/dist/image.d.ts +36 -95
  12. package/dist/image.js +11 -14
  13. package/dist/index.d.ts +31 -68
  14. package/dist/index.js +54 -30
  15. package/dist/llm.d.ts +4 -1
  16. package/dist/llm.js +10 -1
  17. package/dist/media-client.d.ts +18 -6
  18. package/dist/media-client.js +42 -37
  19. package/dist/media-model.d.ts +2 -0
  20. package/dist/media-model.js +3 -1
  21. package/dist/media.d.ts +26 -4
  22. package/dist/media.js +52 -11
  23. package/dist/protocols/alibaba-messages.d.ts +24 -2
  24. package/dist/protocols/alibaba-responses.d.ts +26 -0
  25. package/dist/protocols/anthropic-messages.d.ts +70 -6
  26. package/dist/protocols/anthropic-messages.js +74 -9
  27. package/dist/protocols/assemblyai-transcription.js +7 -6
  28. package/dist/protocols/cartesia-speech.d.ts +0 -8
  29. package/dist/protocols/deepgram-speech.d.ts +0 -8
  30. package/dist/protocols/elevenlabs-speech.d.ts +0 -8
  31. package/dist/protocols/google-decisions.d.ts +13 -0
  32. package/dist/protocols/google-decisions.js +133 -0
  33. package/dist/protocols/google-interactions-video.d.ts +38 -0
  34. package/dist/protocols/google-interactions-video.js +185 -0
  35. package/dist/protocols/google-interactions.d.ts +14 -1
  36. package/dist/protocols/google-interactions.js +1 -1
  37. package/dist/protocols/google-speech.d.ts +0 -8
  38. package/dist/protocols/google-transcription.d.ts +0 -8
  39. package/dist/protocols/index.d.ts +1 -0
  40. package/dist/protocols/index.js +1 -0
  41. package/dist/protocols/meta-messages.d.ts +24 -2
  42. package/dist/protocols/meta-responses.d.ts +52 -0
  43. package/dist/protocols/minimax-video.d.ts +45 -0
  44. package/dist/protocols/minimax-video.js +290 -0
  45. package/dist/protocols/open-responses.d.ts +250 -24
  46. package/dist/protocols/open-responses.js +40 -24
  47. package/dist/protocols/openai-chat.js +7 -3
  48. package/dist/protocols/openai-compatible-responses.d.ts +26 -0
  49. package/dist/protocols/openai-freeform.d.ts +1 -1
  50. package/dist/protocols/openai-freeform.js +9 -5
  51. package/dist/protocols/openai-images.d.ts +0 -8
  52. package/dist/protocols/openai-responses.d.ts +153 -0
  53. package/dist/protocols/openai-responses.js +68 -7
  54. package/dist/protocols/openai-speech.d.ts +0 -8
  55. package/dist/protocols/openai-transcription.d.ts +0 -8
  56. package/dist/protocols/openrouter-video.d.ts +36 -0
  57. package/dist/protocols/openrouter-video.js +139 -0
  58. package/dist/protocols/seedance-video.d.ts +45 -0
  59. package/dist/protocols/seedance-video.js +217 -0
  60. package/dist/protocols/system-one.d.ts +45 -1
  61. package/dist/protocols/system-one.js +42 -7
  62. package/dist/protocols/utils/gateway-protocol.js +1 -0
  63. package/dist/protocols/utils/responses-compaction.js +8 -4
  64. package/dist/protocols/xai-responses.d.ts +26 -0
  65. package/dist/protocols/xai-video.d.ts +12 -11
  66. package/dist/protocols/xai-video.js +33 -24
  67. package/dist/protocols/zai-messages.d.ts +24 -2
  68. package/dist/provider-error.js +7 -1
  69. package/dist/providers/alibaba.d.ts +50 -2
  70. package/dist/providers/amazon-bedrock-mantle.d.ts +50 -2
  71. package/dist/providers/anthropic-compatible.d.ts +24 -2
  72. package/dist/providers/anthropic.d.ts +24 -2
  73. package/dist/providers/azure.d.ts +76 -2
  74. package/dist/providers/byteplus/videos.d.ts +2 -0
  75. package/dist/providers/byteplus/videos.js +1 -0
  76. package/dist/providers/byteplus.d.ts +26 -0
  77. package/dist/providers/byteplus.js +21 -0
  78. package/dist/providers/cloudflare-ai-gateway.d.ts +100 -4
  79. package/dist/providers/digitalocean.d.ts +26 -0
  80. package/dist/providers/google/decisions.d.ts +2 -0
  81. package/dist/providers/google/decisions.js +1 -0
  82. package/dist/providers/google/videos.d.ts +2 -0
  83. package/dist/providers/google/videos.js +1 -0
  84. package/dist/providers/google-vertex-messages.d.ts +24 -2
  85. package/dist/providers/google-vertex-messages.js +1 -0
  86. package/dist/providers/google-vertex-responses.d.ts +26 -0
  87. package/dist/providers/google.d.ts +11 -4
  88. package/dist/providers/google.js +16 -3
  89. package/dist/providers/index.d.ts +2 -1
  90. package/dist/providers/index.js +2 -1
  91. package/dist/providers/meta.d.ts +50 -2
  92. package/dist/providers/minimax/videos.d.ts +3 -0
  93. package/dist/providers/minimax/videos.js +1 -0
  94. package/dist/providers/minimax.d.ts +56 -3
  95. package/dist/providers/minimax.js +10 -1
  96. package/dist/providers/moonshot.d.ts +50 -2
  97. package/dist/providers/openai-compatible/decisions.d.ts +2 -0
  98. package/dist/providers/openai-compatible/decisions.js +1 -0
  99. package/dist/providers/openai-compatible-responses.d.ts +26 -0
  100. package/dist/providers/openai-compatible.d.ts +2 -0
  101. package/dist/providers/openai-compatible.js +14 -0
  102. package/dist/providers/openai.d.ts +26 -0
  103. package/dist/providers/openrouter/videos.d.ts +2 -0
  104. package/dist/providers/openrouter/videos.js +1 -0
  105. package/dist/providers/openrouter.d.ts +31 -0
  106. package/dist/providers/openrouter.js +8 -0
  107. package/dist/providers/venice.d.ts +26 -0
  108. package/dist/providers/volcengine/videos.d.ts +2 -0
  109. package/dist/providers/volcengine/videos.js +1 -0
  110. package/dist/providers/volcengine.d.ts +26 -0
  111. package/dist/providers/volcengine.js +21 -0
  112. package/dist/providers/xai/videos.d.ts +2 -0
  113. package/dist/providers/xai/videos.js +1 -0
  114. package/dist/providers/xai.d.ts +4 -3
  115. package/dist/providers/xai.js +5 -1
  116. package/dist/providers/zai-coding-plan.d.ts +50 -2
  117. package/dist/public.d.ts +7 -4
  118. package/dist/public.js +2 -1
  119. package/dist/route/client.d.ts +2 -0
  120. package/dist/route/client.js +5 -1
  121. package/dist/route/executor-service.d.ts +20 -1
  122. package/dist/route/executor-service.js +35 -1
  123. package/dist/route/executor.d.ts +11 -2
  124. package/dist/route/executor.js +25 -5
  125. package/dist/route/media-protocol.d.ts +21 -15
  126. package/dist/route/media-protocol.js +11 -4
  127. package/dist/route/media.d.ts +2 -0
  128. package/dist/route/media.js +40 -22
  129. package/dist/route/protocol.d.ts +3 -0
  130. package/dist/route/transport/http.js +1 -1
  131. package/dist/schema/events.d.ts +5 -0
  132. package/dist/schema/messages.d.ts +25 -2
  133. package/dist/schema/messages.js +27 -7
  134. package/dist/schema/options.d.ts +18 -2
  135. package/dist/schema/options.js +1 -0
  136. package/dist/speech-client.d.ts +6 -6
  137. package/dist/speech-client.js +4 -4
  138. package/dist/speech.d.ts +7 -88
  139. package/dist/speech.js +1 -3
  140. package/dist/tool-updates.d.ts +34 -0
  141. package/dist/tool-updates.js +93 -0
  142. package/dist/tool.d.ts +6 -1
  143. package/dist/tool.js +1 -1
  144. package/dist/transcription-client.d.ts +6 -6
  145. package/dist/transcription-client.js +1 -2
  146. package/dist/transcription.d.ts +7 -88
  147. package/dist/transcription.js +1 -3
  148. package/dist/video-client.d.ts +16 -84
  149. package/dist/video-client.js +30 -13
  150. package/dist/video.d.ts +54 -1126
  151. package/dist/video.js +29 -65
  152. package/package.json +59 -7
  153. package/dist/protocols/google-video.d.ts +0 -26
  154. package/dist/protocols/google-video.js +0 -146
  155. package/dist/protocols/runway-video.d.ts +0 -38
  156. package/dist/protocols/runway-video.js +0 -141
  157. package/dist/providers/runway.d.ts +0 -20
  158. package/dist/providers/runway.js +0 -17
@@ -166,10 +166,16 @@ export const ContentPart = Schema.Union([
166
166
  CompactionPart,
167
167
  EffortPart,
168
168
  ]).pipe(Schema.toTaggedUnion("type"));
169
+ /** Tools changed here: `add` appends or redefines tools by name, `remove` drops them by name. */
170
+ export const ToolChanges = Schema.Struct({
171
+ add: Schema.Array(Schema.suspend(() => ToolEntry)),
172
+ remove: Schema.Array(Schema.String),
173
+ }).annotate({ identifier: "LLM.ToolChanges" });
169
174
  export class SystemMessage extends Schema.Class("LLM.SystemMessage")({
170
175
  id: Schema.optional(Schema.String),
171
176
  role: Schema.Literal("system"),
172
177
  content: Schema.Array(Schema.Union([TextPart, EffortPart]).pipe(Schema.toTaggedUnion("type"))),
178
+ tools: Schema.optional(ToolChanges),
173
179
  }) {
174
180
  }
175
181
  export class UserMessage extends Schema.Class("LLM.UserMessage")({
@@ -201,8 +207,16 @@ function contentWithMedia(input) {
201
207
  }
202
208
  function make(input) {
203
209
  switch (input.role) {
204
- case "system":
205
- return input instanceof SystemMessage ? input : new SystemMessage({ ...input, content: content(input.content) });
210
+ case "system": {
211
+ if (input instanceof SystemMessage)
212
+ return input;
213
+ const { tools, ...rest } = input;
214
+ return new SystemMessage({
215
+ ...rest,
216
+ content: content(input.content),
217
+ ...(tools === undefined ? {} : { tools: toolChanges(tools) }),
218
+ });
219
+ }
206
220
  case "user":
207
221
  return input instanceof UserMessage
208
222
  ? input
@@ -235,17 +249,23 @@ export const Message = withStatics(messageSchema, {
235
249
  */
236
250
  system: (input) => make({ role: "system", content: input }),
237
251
  effort: (input) => make({ role: "system", content: [{ type: "effort", effort: input.effort, previous: input.previous }] }),
252
+ /**
253
+ * Change the available tools at this point. A route sends the change in place when the model's `tools.updates`
254
+ * allows it, and otherwise folds it into the top-level `tools`. Returns message input that keeps the tool objects,
255
+ * so `ai.loop` can run an executable tool added here.
256
+ */
257
+ tools: (input) => ({ role: "system", content: [], tools: input }),
238
258
  tool: (result) => make({ role: "tool", content: ["type" in result ? result : ToolResultPart.make(result)] }),
239
259
  });
260
+ const toolChanges = (input) => ({
261
+ add: (input.add ?? []).map((item) => ("_definition" in item ? item._definition : ToolEntry.make(item))),
262
+ remove: (input.remove ?? []).map((item) => (typeof item === "string" ? item : item.name)),
263
+ });
240
264
  /**
241
265
  * Raw-text wire encoding for a tool with exactly one required string parameter. Models that accept the format
242
266
  * write that parameter's value directly instead of JSON arguments; calls still decode to the normal object input.
243
267
  */
244
- export const Freeform = Schema.Union([
245
- Schema.Struct({ type: Schema.Literal("text") }),
246
- Schema.Struct({ type: Schema.Literal("regex"), pattern: Schema.String }),
247
- Schema.Struct({ type: Schema.Literal("lark"), grammar: Schema.String }),
248
- ]).annotate({ identifier: "LLM.Freeform" });
268
+ export const Freeform = Tool.Freeform;
249
269
  const toolDefinitionFields = {
250
270
  name: Schema.String,
251
271
  description: Schema.String,
@@ -29,6 +29,14 @@ export interface HttpOptions {
29
29
  readonly body?: Record<string, unknown>;
30
30
  /** Compression for JSON request bodies. Only set it for endpoints known to decode it. */
31
31
  readonly encoding?: RequestEncoding;
32
+ /** Client certificate presented to HTTPS endpoints that request one (mutual TLS). */
33
+ readonly tls?: ClientTls;
34
+ }
35
+ /** PEM client certificate and private key, with the passphrase of an encrypted key. */
36
+ export interface ClientTls {
37
+ readonly cert: string;
38
+ readonly key: string;
39
+ readonly passphrase?: string;
32
40
  }
33
41
  /** Per-phase time limits in milliseconds. */
34
42
  export interface TimeoutConfig {
@@ -127,8 +135,16 @@ export interface Capabilities {
127
135
  readonly search?: boolean;
128
136
  /** Input formats accepted by custom (free-form) tools. */
129
137
  readonly freeform?: ReadonlyArray<"text" | "lark" | "regex">;
130
- /** Accepts a changed tool list mid-conversation. */
131
- readonly updates?: boolean;
138
+ /**
139
+ * Tool changes the model takes mid-conversation without changing the leading tool declaration, so the prompt
140
+ * cache holds. `add`: declares a tool not declared before. `remove`: withdraws a declared tool. `replace`:
141
+ * declares a tool under a name declared before (a redefinition, or re-adding a removed tool). Unset or empty:
142
+ * every change folds into the top-level tools. Anthropic Messages sends all three as native blocks. OpenAI
143
+ * Responses needs `add` for any of them: it declares every tool in `additional_tools` items with a short
144
+ * incremental-update hint before each later item, and announces a removal as developer text while the tool
145
+ * stays declared, so list `remove` only for models that follow that announcement.
146
+ */
147
+ readonly updates?: ReadonlyArray<"add" | "remove" | "replace">;
132
148
  };
133
149
  readonly reasoning?: {
134
150
  readonly supported?: boolean;
@@ -61,6 +61,7 @@ export const mergeTransport = (...items) => {
61
61
  query: mergeStringRecords(...http.map((item) => item?.query)),
62
62
  body: mergeJsonRecords(...http.map((item) => item?.body)),
63
63
  encoding: last(http.map((item) => item?.encoding)),
64
+ tls: last(http.map((item) => item?.tls)),
64
65
  }),
65
66
  };
66
67
  };
@@ -1,6 +1,6 @@
1
1
  import { Context } from "effect";
2
2
  import { MediaClient } from "./media-client.js";
3
- import { type SpeechEvent, type SpeechRequest, type SpeechResponse } from "./speech.js";
3
+ import type { SpeechEvent, SpeechRequest, SpeechResponse } from "./speech.js";
4
4
  export type Interface = MediaClient.Interface<SpeechRequest, SpeechEvent, SpeechResponse>;
5
5
  declare const SpeechClientService_base: Context.ServiceClass<SpeechClientService, "@opencode/SpeechClient", Interface>;
6
6
  export declare class SpeechClientService extends SpeechClientService_base {
@@ -18,10 +18,6 @@ export declare const SpeechClient: {
18
18
  readonly startSeconds: number;
19
19
  readonly endSeconds: number;
20
20
  }[];
21
- } | {
22
- readonly type: "generation-queued";
23
- readonly id: string;
24
- readonly position?: number | undefined;
25
21
  } | {
26
22
  readonly type: "generation-progress";
27
23
  readonly id: string;
@@ -68,6 +64,10 @@ export declare const SpeechClient: {
68
64
  readonly startSeconds: number;
69
65
  readonly endSeconds: number;
70
66
  }[];
67
+ } | {
68
+ readonly type: "generation-progress";
69
+ readonly id: string;
70
+ readonly progress?: number | undefined;
71
71
  } | {
72
72
  readonly type: "finish";
73
73
  readonly audio: import("./media.js").Asset;
@@ -97,7 +97,7 @@ export declare const SpeechClient: {
97
97
  readonly [x: string]: unknown;
98
98
  };
99
99
  } | undefined;
100
- } | import("./generation.js").Observation, import("./public.js").AIError, SpeechClientService>;
100
+ }, import("./public.js").AIError, SpeechClientService>;
101
101
  readonly start: (request: SpeechRequest) => import("effect/Effect").Effect<import("./generation.js").Generation<SpeechResponse>, import("./public.js").AIError, SpeechClientService>;
102
102
  readonly resume: (model: import("./speech.js").SpeechModel<{
103
103
  readonly [x: string]: unknown;
@@ -1,6 +1,5 @@
1
1
  import { Context } from "effect";
2
2
  import { MediaClient } from "./media-client.js";
3
- import { SpeechTimestampsEvent, SpeechFinishEvent, } from "./speech.js";
4
3
  export class SpeechClientService extends Context.Service()("@opencode/SpeechClient") {
5
4
  }
6
5
  export const Service = SpeechClientService;
@@ -9,12 +8,13 @@ export const SpeechClient = {
9
8
  ...MediaClient.make(Service, {
10
9
  modality: "speech",
11
10
  responseEvents: (response) => [
12
- ...(response.timestamps === undefined ? [] : [SpeechTimestampsEvent.make({ items: response.timestamps })]),
13
- SpeechFinishEvent.make({
11
+ ...(response.timestamps === undefined ? [] : [{ type: "timestamps", items: response.timestamps }]),
12
+ {
13
+ type: "finish",
14
14
  audio: response.audio,
15
15
  usage: response.usage,
16
16
  providerMetadata: response.providerMetadata,
17
- }),
17
+ },
18
18
  ],
19
19
  }),
20
20
  };
package/dist/speech.d.ts CHANGED
@@ -186,10 +186,6 @@ export declare const SpeechFinishEvent: Schema.Struct<{
186
186
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
187
187
  }>;
188
188
  declare const speechEventTagged: Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
189
- readonly type: Schema.tag<"generation-queued">;
190
- readonly id: Schema.String;
191
- readonly position: Schema.optional<Schema.Number>;
192
- }>, Schema.Struct<{
193
189
  readonly type: Schema.tag<"generation-progress">;
194
190
  readonly id: Schema.String;
195
191
  readonly progress: Schema.optional<Schema.Number>;
@@ -258,10 +254,6 @@ declare const speechEventTagged: Schema.toTaggedUnion<"type", readonly [Schema.S
258
254
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
259
255
  }>]>;
260
256
  export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
261
- readonly type: Schema.tag<"generation-queued">;
262
- readonly id: Schema.String;
263
- readonly position: Schema.optional<Schema.Number>;
264
- }>, Schema.Struct<{
265
257
  readonly type: Schema.tag<"generation-progress">;
266
258
  readonly id: Schema.String;
267
259
  readonly progress: Schema.optional<Schema.Number>;
@@ -330,7 +322,7 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
330
322
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
331
323
  }>]> & {
332
324
  readonly tag: "type";
333
- readonly discriminants: readonly ["generation-queued", "generation-progress", "audio-delta", "timestamps", "finish"];
325
+ readonly discriminants: readonly ["generation-progress", "audio-delta", "timestamps", "finish"];
334
326
  readonly cases: {
335
327
  "audio-delta": Schema.Struct<{
336
328
  readonly type: Schema.tag<"audio-delta">;
@@ -395,11 +387,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
395
387
  readonly id: Schema.String;
396
388
  readonly progress: Schema.optional<Schema.Number>;
397
389
  }>;
398
- "generation-queued": Schema.Struct<{
399
- readonly type: Schema.tag<"generation-queued">;
400
- readonly id: Schema.String;
401
- readonly position: Schema.optional<Schema.Number>;
402
- }>;
403
390
  timestamps: Schema.Struct<{
404
391
  readonly type: Schema.tag<"timestamps">;
405
392
  readonly items: Schema.$Array<Schema.Struct<{
@@ -419,10 +406,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
419
406
  readonly startSeconds: number;
420
407
  readonly endSeconds: number;
421
408
  }[];
422
- } | {
423
- readonly type: "generation-queued";
424
- readonly id: string;
425
- readonly position?: number | undefined;
426
409
  } | {
427
410
  readonly type: "generation-progress";
428
411
  readonly id: string;
@@ -470,12 +453,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
470
453
  }[];
471
454
  }, {
472
455
  readonly type: Keys;
473
- }> | Extract<{
474
- readonly type: "generation-queued";
475
- readonly id: string;
476
- readonly position?: number | undefined;
477
- }, {
478
- readonly type: Keys;
479
456
  }> | Extract<{
480
457
  readonly type: "generation-progress";
481
458
  readonly id: string;
@@ -554,11 +531,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
554
531
  readonly id: string;
555
532
  readonly progress?: number | undefined;
556
533
  };
557
- "generation-queued": (u: unknown) => u is {
558
- readonly type: "generation-queued";
559
- readonly id: string;
560
- readonly position?: number | undefined;
561
- };
562
534
  timestamps: (u: unknown) => u is {
563
535
  readonly type: "timestamps";
564
536
  readonly items: readonly {
@@ -569,7 +541,7 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
569
541
  };
570
542
  };
571
543
  readonly match: {
572
- <Handlers extends { [K in "audio-delta" | "finish" | "generation-progress" | "generation-queued" | "timestamps"]: (value: {
544
+ <Handlers extends { [K in "audio-delta" | "finish" | "generation-progress" | "timestamps"]: (value: {
573
545
  readonly "audio-delta": Schema.Struct<{
574
546
  readonly type: Schema.tag<"audio-delta">;
575
547
  readonly chunk: Schema.Uint8Array;
@@ -633,11 +605,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
633
605
  readonly id: Schema.String;
634
606
  readonly progress: Schema.optional<Schema.Number>;
635
607
  }>;
636
- readonly "generation-queued": Schema.Struct<{
637
- readonly type: Schema.tag<"generation-queued">;
638
- readonly id: Schema.String;
639
- readonly position: Schema.optional<Schema.Number>;
640
- }>;
641
608
  readonly timestamps: Schema.Struct<{
642
609
  readonly type: Schema.tag<"timestamps">;
643
610
  readonly items: Schema.$Array<Schema.Struct<{
@@ -656,10 +623,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
656
623
  readonly startSeconds: number;
657
624
  readonly endSeconds: number;
658
625
  }[];
659
- } | {
660
- readonly type: "generation-queued";
661
- readonly id: string;
662
- readonly position?: number | undefined;
663
626
  } | {
664
627
  readonly type: "generation-progress";
665
628
  readonly id: string;
@@ -693,8 +656,8 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
693
656
  readonly [x: string]: unknown;
694
657
  };
695
658
  } | undefined;
696
- }, cases: Handlers): import("effect/Unify").Unify<ReturnType<Handlers["audio-delta" | "finish" | "generation-progress" | "generation-queued" | "timestamps"]>>;
697
- <Handlers extends { [K in "audio-delta" | "finish" | "generation-progress" | "generation-queued" | "timestamps"]: (value: {
659
+ }, cases: Handlers): import("effect/Unify").Unify<ReturnType<Handlers["audio-delta" | "finish" | "generation-progress" | "timestamps"]>>;
660
+ <Handlers extends { [K in "audio-delta" | "finish" | "generation-progress" | "timestamps"]: (value: {
698
661
  readonly "audio-delta": Schema.Struct<{
699
662
  readonly type: Schema.tag<"audio-delta">;
700
663
  readonly chunk: Schema.Uint8Array;
@@ -758,11 +721,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
758
721
  readonly id: Schema.String;
759
722
  readonly progress: Schema.optional<Schema.Number>;
760
723
  }>;
761
- readonly "generation-queued": Schema.Struct<{
762
- readonly type: Schema.tag<"generation-queued">;
763
- readonly id: Schema.String;
764
- readonly position: Schema.optional<Schema.Number>;
765
- }>;
766
724
  readonly timestamps: Schema.Struct<{
767
725
  readonly type: Schema.tag<"timestamps">;
768
726
  readonly items: Schema.$Array<Schema.Struct<{
@@ -781,10 +739,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
781
739
  readonly startSeconds: number;
782
740
  readonly endSeconds: number;
783
741
  }[];
784
- } | {
785
- readonly type: "generation-queued";
786
- readonly id: string;
787
- readonly position?: number | undefined;
788
742
  } | {
789
743
  readonly type: "generation-progress";
790
744
  readonly id: string;
@@ -818,7 +772,7 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
818
772
  readonly [x: string]: unknown;
819
773
  };
820
774
  } | undefined;
821
- }) => import("effect/Unify").Unify<ReturnType<Handlers["audio-delta" | "finish" | "generation-progress" | "generation-queued" | "timestamps"]>>;
775
+ }) => import("effect/Unify").Unify<ReturnType<Handlers["audio-delta" | "finish" | "generation-progress" | "timestamps"]>>;
822
776
  };
823
777
  readonly matchOrElse: {
824
778
  <Handlers, OrElse>(value: {
@@ -831,10 +785,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
831
785
  readonly startSeconds: number;
832
786
  readonly endSeconds: number;
833
787
  }[];
834
- } | {
835
- readonly type: "generation-queued";
836
- readonly id: string;
837
- readonly position?: number | undefined;
838
788
  } | {
839
789
  readonly type: "generation-progress";
840
790
  readonly id: string;
@@ -908,11 +858,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
908
858
  readonly id: string;
909
859
  readonly progress?: number | undefined;
910
860
  }) => any) | undefined;
911
- readonly "generation-queued"?: ((value: {
912
- readonly type: "generation-queued";
913
- readonly id: string;
914
- readonly position?: number | undefined;
915
- }) => any) | undefined;
916
861
  readonly timestamps?: ((value: {
917
862
  readonly type: "timestamps";
918
863
  readonly items: readonly {
@@ -921,7 +866,7 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
921
866
  readonly endSeconds: number;
922
867
  }[];
923
868
  }) => any) | undefined;
924
- } & (Exclude<keyof Handlers, "audio-delta" | "finish" | "generation-progress" | "generation-queued" | "timestamps"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
869
+ } & (Exclude<keyof Handlers, "audio-delta" | "finish" | "generation-progress" | "timestamps"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
925
870
  readonly type: "audio-delta";
926
871
  readonly chunk: Uint8Array<ArrayBufferLike>;
927
872
  }, {
@@ -935,12 +880,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
935
880
  }[];
936
881
  }, {
937
882
  readonly type: keyof Handlers;
938
- }> | Exclude<{
939
- readonly type: "generation-queued";
940
- readonly id: string;
941
- readonly position?: number | undefined;
942
- }, {
943
- readonly type: keyof Handlers;
944
883
  }> | Exclude<{
945
884
  readonly type: "generation-progress";
946
885
  readonly id: string;
@@ -1019,11 +958,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
1019
958
  readonly id: string;
1020
959
  readonly progress?: number | undefined;
1021
960
  }) => any) | undefined;
1022
- readonly "generation-queued"?: ((value: {
1023
- readonly type: "generation-queued";
1024
- readonly id: string;
1025
- readonly position?: number | undefined;
1026
- }) => any) | undefined;
1027
961
  readonly timestamps?: ((value: {
1028
962
  readonly type: "timestamps";
1029
963
  readonly items: readonly {
@@ -1032,7 +966,7 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
1032
966
  readonly endSeconds: number;
1033
967
  }[];
1034
968
  }) => any) | undefined;
1035
- } & (Exclude<keyof Handlers, "audio-delta" | "finish" | "generation-progress" | "generation-queued" | "timestamps"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
969
+ } & (Exclude<keyof Handlers, "audio-delta" | "finish" | "generation-progress" | "timestamps"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
1036
970
  readonly type: "audio-delta";
1037
971
  readonly chunk: Uint8Array<ArrayBufferLike>;
1038
972
  }, {
@@ -1046,12 +980,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
1046
980
  }[];
1047
981
  }, {
1048
982
  readonly type: keyof Handlers;
1049
- }> | Exclude<{
1050
- readonly type: "generation-queued";
1051
- readonly id: string;
1052
- readonly position?: number | undefined;
1053
- }, {
1054
- readonly type: keyof Handlers;
1055
983
  }> | Exclude<{
1056
984
  readonly type: "generation-progress";
1057
985
  readonly id: string;
@@ -1099,10 +1027,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
1099
1027
  readonly startSeconds: number;
1100
1028
  readonly endSeconds: number;
1101
1029
  }[];
1102
- } | {
1103
- readonly type: "generation-queued";
1104
- readonly id: string;
1105
- readonly position?: number | undefined;
1106
1030
  } | {
1107
1031
  readonly type: "generation-progress";
1108
1032
  readonly id: string;
@@ -1140,11 +1064,6 @@ export declare const SpeechEvent: Schema.Union<readonly [Schema.Struct<{
1140
1064
  };
1141
1065
  } & {
1142
1066
  is: {
1143
- generationQueued: (u: unknown) => u is {
1144
- readonly type: "generation-queued";
1145
- readonly id: string;
1146
- readonly position?: number | undefined;
1147
- };
1148
1067
  generationProgress: (u: unknown) => u is {
1149
1068
  readonly type: "generation-progress";
1150
1069
  readonly id: string;
package/dist/speech.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect, Schema, Stream } from "effect";
2
- import { ProgressEvent, QueuedEvent } from "./generation.js";
2
+ import { ProgressEvent } from "./generation.js";
3
3
  import { Media } from "./media.js";
4
4
  import { MediaModel, composeRoute, tryRequest } from "./media-model.js";
5
5
  import { MediaRoute } from "./route/media.js";
@@ -70,7 +70,6 @@ export const SpeechFinishEvent = Schema.Struct({
70
70
  providerMetadata: Schema.optional(ProviderMetadata),
71
71
  }).annotate({ identifier: "Speech.Event.Finish" });
72
72
  const speechEventTagged = Schema.Union([
73
- QueuedEvent,
74
73
  ProgressEvent,
75
74
  SpeechAudioDeltaEvent,
76
75
  SpeechTimestampsEvent,
@@ -78,7 +77,6 @@ const speechEventTagged = Schema.Union([
78
77
  ]).pipe(Schema.toTaggedUnion("type"));
79
78
  export const SpeechEvent = Object.assign(speechEventTagged, {
80
79
  is: {
81
- generationQueued: speechEventTagged.guards["generation-queued"],
82
80
  generationProgress: speechEventTagged.guards["generation-progress"],
83
81
  audioDelta: speechEventTagged.guards["audio-delta"],
84
82
  timestamps: speechEventTagged.guards.timestamps,
@@ -0,0 +1,34 @@
1
+ import { Message, type LLMRequest, type ToolChanges, type ToolEntry } from "./schema/messages.js";
2
+ import type { Capabilities } from "./schema/options.js";
3
+ export type ToolUpdate = NonNullable<NonNullable<Capabilities["tools"]>["updates"]>[number];
4
+ /** What a protocol can send in place of a `Message.tools(...)` marker; the model's `tools.updates` narrows it. */
5
+ export interface ToolUpdateEncoding {
6
+ readonly kinds: ReadonlyArray<ToolUpdate>;
7
+ /** Whether the marker at this message index can be sent where it is. Defaults to anywhere. */
8
+ readonly placeable?: (request: LLMRequest, index: number) => boolean;
9
+ /** Whether a tool can be added or removed in place. Defaults to every tool. */
10
+ readonly declarable?: (tool: ToolEntry) => boolean;
11
+ }
12
+ export declare const toolsUpdate: (message: Message) => ToolChanges | undefined;
13
+ /** Whether any marker adds a tool, so a request without top-level tools still declares some. */
14
+ export declare const addsTools: (request: LLMRequest) => boolean;
15
+ /** The changes this route sends in place: the model's `tools.updates` that the protocol's encoding covers. */
16
+ export declare const toolUpdateKinds: (request: LLMRequest) => ("add" | "remove" | "replace")[];
17
+ /** Apply one change: removals first, then additions replace a same-named tool in place or append. */
18
+ export declare const foldTools: <Item extends {
19
+ readonly name: string;
20
+ }>(tools: ReadonlyArray<Item>, add: ReadonlyArray<Item>, remove: ReadonlyArray<string>) => ReadonlyArray<Item>;
21
+ /**
22
+ * Fold the declarations up to the last one this route can't send in place into the leading one (the request's
23
+ * `tools`), and keep later markers in place, trimmed to their effective changes. Protocols send kept markers in
24
+ * message order and track what they need along the way. A marker is sent in place only after the conversation starts, when the model's
25
+ * `tools.updates` and the protocol's encoding both cover what it does: `add` a new name, `replace` a name declared
26
+ * before (even if removed since), `remove` a current tool. Removing a name that matches no current tool does nothing.
27
+ */
28
+ export declare const planToolUpdates: (request: LLMRequest) => LLMRequest;
29
+ /** Fold every marker into the top-level tools, as a route without in-place tool changes sends them. */
30
+ export declare const foldToolUpdates: (request: LLMRequest) => LLMRequest;
31
+ /** Flattened names of the tools in `before` that `after` no longer has, including tools a redefined namespace drops. */
32
+ export declare const withdrawnTools: (before: ReadonlyArray<ToolEntry>, after: ReadonlyArray<ToolEntry>) => string[];
33
+ /** Rewrite the tools each marker adds, keeping the request identity when there are no markers. */
34
+ export declare const mapToolUpdates: (request: LLMRequest, f: (tools: ReadonlyArray<ToolEntry>) => ReadonlyArray<ToolEntry>) => LLMRequest;
@@ -0,0 +1,93 @@
1
+ // A request's top-level `tools` is shorthand for a leading `Message.tools({ add: tools })`: tool state is one
2
+ // ordered list of declarations, that leading one and then each marker in message order. Folding the whole list
3
+ // gives the tools a route without in-place changes sends. A protocol that can send some changes in place keeps
4
+ // them where they are, so the leading declaration, and the prompt cache behind it, stays fixed.
5
+ import { Message, SystemMessage } from "./schema/messages.js";
6
+ import { flattenTools } from "./protocols/shared.js";
7
+ export const toolsUpdate = (message) => message.role === "system" ? message.tools : undefined;
8
+ /** Whether any marker adds a tool, so a request without top-level tools still declares some. */
9
+ export const addsTools = (request) => request.messages.some((message) => (toolsUpdate(message)?.add.length ?? 0) > 0);
10
+ /** The changes this route sends in place: the model's `tools.updates` that the protocol's encoding covers. */
11
+ export const toolUpdateKinds = (request) => {
12
+ const encoding = request.model.route.toolUpdates;
13
+ return (request.model.capabilities?.tools?.updates ?? []).filter((kind) => encoding?.kinds.includes(kind));
14
+ };
15
+ /** Apply one change: removals first, then additions replace a same-named tool in place or append. */
16
+ export const foldTools = (tools, add, remove) => {
17
+ const added = new Map(add.map((tool) => [tool.name, tool]));
18
+ const kept = tools.filter((tool) => !remove.includes(tool.name)).map((tool) => added.get(tool.name) ?? tool);
19
+ const names = new Set(kept.map((tool) => tool.name));
20
+ return [...kept, ...[...added.values()].filter((tool) => !names.has(tool.name))];
21
+ };
22
+ /**
23
+ * Fold the declarations up to the last one this route can't send in place into the leading one (the request's
24
+ * `tools`), and keep later markers in place, trimmed to their effective changes. Protocols send kept markers in
25
+ * message order and track what they need along the way. A marker is sent in place only after the conversation starts, when the model's
26
+ * `tools.updates` and the protocol's encoding both cover what it does: `add` a new name, `replace` a name declared
27
+ * before (even if removed since), `remove` a current tool. Removing a name that matches no current tool does nothing.
28
+ */
29
+ export const planToolUpdates = (request) => plan(request, toolUpdateKinds(request));
30
+ /** Fold every marker into the top-level tools, as a route without in-place tool changes sends them. */
31
+ export const foldToolUpdates = (request) => plan(request, []);
32
+ const plan = (request, supported) => {
33
+ const changes = request.messages.flatMap((message, index) => message.role === "system" && message.tools !== undefined ? [{ index, ...message.tools }] : []);
34
+ if (changes.length === 0)
35
+ return request;
36
+ const encoding = request.model.route.toolUpdates;
37
+ const declarable = (tool) => encoding?.declarable?.(tool) ?? true;
38
+ const firstTurn = request.messages.findIndex((message) => message.role !== "system");
39
+ const steps = changes.reduce((state, change) => {
40
+ const added = change.add.map((tool) => tool.name);
41
+ const removed = state.tools.filter((tool) => change.remove.includes(tool.name) && !added.includes(tool.name));
42
+ const tools = foldTools(state.tools, change.add, change.remove);
43
+ const kinds = [
44
+ ...(removed.length > 0 || withdrawnTools(state.tools, tools).length > 0 ? ["remove"] : []),
45
+ ...added.map((name) => (state.declared.has(name) ? "replace" : "add")),
46
+ ];
47
+ const native = firstTurn !== -1 &&
48
+ change.index > firstTurn &&
49
+ kinds.every((kind) => supported.includes(kind)) &&
50
+ [...change.add, ...removed].every(declarable) &&
51
+ (encoding?.placeable?.(request, change.index) ?? true);
52
+ const step = {
53
+ index: change.index,
54
+ native,
55
+ tools,
56
+ changes: { add: change.add, remove: removed.map((tool) => tool.name) },
57
+ };
58
+ return { tools, declared: new Set([...state.declared, ...added]), steps: [...state.steps, step] };
59
+ }, { tools: request.tools, declared: new Set(request.tools.map((tool) => tool.name)), steps: [] }).steps;
60
+ const cut = steps.findLastIndex((step) => !step.native);
61
+ const kept = new Map(steps.slice(cut + 1).map((step) => [step.index, step.changes]));
62
+ return {
63
+ ...request,
64
+ tools: cut === -1 ? request.tools : steps[cut].tools,
65
+ messages: request.messages.flatMap((message, index) => {
66
+ if (message.role !== "system" || message.tools === undefined)
67
+ return [message];
68
+ const tools = kept.get(index);
69
+ if (tools !== undefined && (tools.add.length > 0 || tools.remove.length > 0))
70
+ return [new SystemMessage({ ...message, tools })];
71
+ if (message.content.length === 0)
72
+ return [];
73
+ return [new SystemMessage({ id: message.id, role: "system", content: message.content })];
74
+ }),
75
+ };
76
+ };
77
+ /** Flattened names of the tools in `before` that `after` no longer has, including tools a redefined namespace drops. */
78
+ export const withdrawnTools = (before, after) => {
79
+ const kept = new Set(toolNames(after));
80
+ return toolNames(before).filter((name) => !kept.has(name));
81
+ };
82
+ const toolNames = (tools) => flattenTools(tools).map((tool) => tool.name);
83
+ /** Rewrite the tools each marker adds, keeping the request identity when there are no markers. */
84
+ export const mapToolUpdates = (request, f) => {
85
+ if (!request.messages.some((message) => toolsUpdate(message) !== undefined))
86
+ return request;
87
+ return {
88
+ ...request,
89
+ messages: request.messages.map((message) => message.role === "system" && message.tools !== undefined
90
+ ? new SystemMessage({ ...message, tools: { ...message.tools, add: f(message.tools.add) } })
91
+ : message),
92
+ };
93
+ };
package/dist/tool.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Effect, JsonSchema, Schema } from "effect";
2
2
  import type { Content } from "@opencode/schema/tool";
3
3
  import type { Freeform, ToolCallPart, ToolDefinition as ToolDefinitionClass, ToolOutput as ToolOutputType } from "./schema/index.js";
4
- import { ToolFailure } from "./schema/index.js";
4
+ import { ToolFailure, type ToolEntryInput } from "./schema/index.js";
5
5
  /**
6
6
  * Schema constraint for tool input / success values: no decoding or
7
7
  * encoding services are allowed. Tools should be self-contained — anything
@@ -61,6 +61,11 @@ export interface Tool<Input extends ToolInput = any, Success extends ToolSchema<
61
61
  /** @internal */
62
62
  readonly _definition: ToolDefinitionClass;
63
63
  }
64
+ /** Tool changes for `Message.tools`: `add` takes what a request's `tools` takes, `remove` the same tools or their names. */
65
+ export interface ToolChangesInput {
66
+ readonly add?: ReadonlyArray<Tool | ToolEntryInput>;
67
+ readonly remove?: ReadonlyArray<Tool | ToolEntryInput | string>;
68
+ }
64
69
  /** A tool with an `execute` handler, which `LLM.loop` and `ToolRuntime.dispatch` can run. */
65
70
  export type ExecutableTool<Input extends ToolInput = any, Success extends ToolSchema<any> = any> = Tool<Input, Success> & {
66
71
  readonly execute: ToolExecute<Input, Success>;
package/dist/tool.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect, JsonSchema, Schema } from "effect";
2
- import { ToolDefinition, ToolFailure, ToolOutput } from "./schema/index.js";
2
+ import { ToolDefinition, ToolFailure, ToolOutput, } from "./schema/index.js";
3
3
  import { isRecord } from "./utils/record.js";
4
4
  /**
5
5
  * Constructs a tool whose `execute` returns an Effect that fails with `ToolFailure`.
@@ -1,6 +1,6 @@
1
1
  import { Context } from "effect";
2
2
  import { MediaClient } from "./media-client.js";
3
- import { type TranscriptionEvent, type TranscriptionRequest, type TranscriptionResponse } from "./transcription.js";
3
+ import type { TranscriptionEvent, TranscriptionRequest, TranscriptionResponse } from "./transcription.js";
4
4
  export type Interface = MediaClient.Interface<TranscriptionRequest, TranscriptionEvent, TranscriptionResponse>;
5
5
  declare const TranscriptionClientService_base: Context.ServiceClass<TranscriptionClientService, "@opencode/TranscriptionClient", Interface>;
6
6
  export declare class TranscriptionClientService extends TranscriptionClientService_base {
@@ -19,10 +19,6 @@ export declare const TranscriptionClient: {
19
19
  readonly endSeconds: number;
20
20
  readonly speaker?: string | undefined;
21
21
  };
22
- } | {
23
- readonly type: "generation-queued";
24
- readonly id: string;
25
- readonly position?: number | undefined;
26
22
  } | {
27
23
  readonly type: "generation-progress";
28
24
  readonly id: string;
@@ -85,6 +81,10 @@ export declare const TranscriptionClient: {
85
81
  readonly endSeconds: number;
86
82
  readonly speaker?: string | undefined;
87
83
  };
84
+ } | {
85
+ readonly type: "generation-progress";
86
+ readonly id: string;
87
+ readonly progress?: number | undefined;
88
88
  } | {
89
89
  readonly text: string;
90
90
  readonly segments?: readonly {
@@ -129,7 +129,7 @@ export declare const TranscriptionClient: {
129
129
  };
130
130
  } | undefined;
131
131
  readonly type: "finish";
132
- } | import("./generation.js").Observation, import("./public.js").AIError, TranscriptionClientService>;
132
+ }, import("./public.js").AIError, TranscriptionClientService>;
133
133
  readonly start: (request: TranscriptionRequest) => import("effect/Effect").Effect<import("./generation.js").Generation<TranscriptionResponse>, import("./public.js").AIError, TranscriptionClientService>;
134
134
  readonly resume: (model: import("./transcription.js").TranscriptionModel<{
135
135
  readonly [x: string]: unknown;