@opencode/ai 0.0.0-dev-51 → 0.0.0-dev-56

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.
package/README.md CHANGED
@@ -50,6 +50,40 @@ Run `LLM.stream(...)` instead of `generate` when you want incremental `LLMEvent`
50
50
  `LLM.request(...)`. The event stream is provider-neutral — same shape across OpenAI Chat, OpenAI Responses,
51
51
  Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployment.
52
52
 
53
+ ### Tool loop
54
+
55
+ `stream` runs one model call; `loop` runs the model's tool calls and calls the model again until it stops calling
56
+ tools. Events from every step arrive on one stream, with a `tool-result` after each tool runs. `until` is checked
57
+ after each step that has tool calls, with every step's `LLMResponse`; returning `true` ends the loop without running
58
+ them:
59
+
60
+ ```ts
61
+ import { readFile } from "node:fs/promises"
62
+ import { Effect, Schema } from "effect"
63
+ import { Tool, ToolFailure } from "@opencode/ai/effect"
64
+
65
+ const read_file = Tool.make({
66
+ description: "Read a UTF-8 file.",
67
+ parameters: Schema.Struct({ path: Schema.String }),
68
+ success: Schema.String,
69
+ execute: ({ path }) =>
70
+ Effect.tryPromise({
71
+ try: () => readFile(path, "utf8"),
72
+ catch: (error) => new ToolFailure({ message: `Failed to read ${path}`, error }),
73
+ }),
74
+ })
75
+
76
+ const events = ai.llm.loop({
77
+ model,
78
+ prompt: "Summarize package.json.",
79
+ tools: { read_file },
80
+ until: ({ steps }) => steps.length >= 8,
81
+ })
82
+ for await (const event of events) {
83
+ if (event.type === "text-delta") process.stdout.write(event.text)
84
+ }
85
+ ```
86
+
53
87
  ### Raw provider events
54
88
 
55
89
  Set `includeRaw: true` on a request to also receive every provider frame as `{ type: "raw", data }`, in order and
@@ -1112,6 +1146,7 @@ Everything below is exported from `@opencode/ai/effect` unless noted.
1112
1146
 
1113
1147
  - **`LLM.request({...})`** — build a provider-neutral `LLMRequest`. Accepts ergonomic inputs (`system: string`, `prompt: string`) that normalize into the canonical Schema classes.
1114
1148
  - **`LLM.generate` / `LLM.stream`** — run direct input or an `LLMRequest` through `LLMClient` for one-import use.
1149
+ - **`LLM.loop`** — stream model calls, running `Tool.make(...)` tools between them until the model stops calling tools.
1115
1150
  - **`Message.user(...)` / `Message.assistant(...)` / `Message.tool(...)`** — message constructors from the canonical schema model.
1116
1151
  - **`LanguageModel.make(...)` / `ToolCallPart.make(...)` / `ToolResultPart.make(...)` / `ToolDefinition.make(...)`** — model and tool-related constructors from the canonical schema model.
1117
1152
  - **`LLMEvent.is.*`** — typed guards (`is.textDelta`, `is.toolCall`, `is.finish`, …) for filtering streams.
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ import { LLM } from "./effect.js";
7
7
  import { Media } from "./media.js";
8
8
  import { RequestExecutor } from "./route/executor.js";
9
9
  import { LanguageModel, type LLMRequest } from "./schema/index.js";
10
- import type { RequestInput } from "./llm.js";
10
+ import type { LoopInput, RequestInput } from "./llm.js";
11
11
  import { type SpeechModel, type SpeechRequest, type SpeechRequestInput } from "./speech.js";
12
12
  import { type TranscriptionModel, type TranscriptionRequest, type TranscriptionRequestInput } from "./transcription.js";
13
13
  import { type VideoModel, type VideoRequest, type VideoRequestInput } from "./video.js";
@@ -272,6 +272,226 @@ export declare const make: (options?: Options) => {
272
272
  readonly text: string | null;
273
273
  readonly encrypted?: never;
274
274
  }))>;
275
+ /** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
276
+ loop: <const Model extends LanguageModel>(input: LoopInput<Model>, options?: RunOptions) => AsyncIterable<{
277
+ readonly type: "step-start";
278
+ readonly index: number;
279
+ } | {
280
+ readonly type: "raw";
281
+ readonly data: unknown;
282
+ } | {
283
+ readonly type: "text-start";
284
+ readonly id: string;
285
+ readonly providerMetadata?: {
286
+ readonly [x: string]: {
287
+ readonly [x: string]: unknown;
288
+ };
289
+ } | undefined;
290
+ } | {
291
+ readonly type: "text-delta";
292
+ readonly id: string;
293
+ readonly text: string;
294
+ readonly providerMetadata?: {
295
+ readonly [x: string]: {
296
+ readonly [x: string]: unknown;
297
+ };
298
+ } | undefined;
299
+ } | {
300
+ readonly type: "text-end";
301
+ readonly id: string;
302
+ readonly text?: string | undefined;
303
+ readonly providerMetadata?: {
304
+ readonly [x: string]: {
305
+ readonly [x: string]: unknown;
306
+ };
307
+ } | undefined;
308
+ } | {
309
+ readonly type: "reasoning-start";
310
+ readonly id: string;
311
+ readonly providerMetadata?: {
312
+ readonly [x: string]: {
313
+ readonly [x: string]: unknown;
314
+ };
315
+ } | undefined;
316
+ } | {
317
+ readonly type: "reasoning-delta";
318
+ readonly id: string;
319
+ readonly text: string;
320
+ readonly providerMetadata?: {
321
+ readonly [x: string]: {
322
+ readonly [x: string]: unknown;
323
+ };
324
+ } | undefined;
325
+ } | {
326
+ readonly type: "reasoning-end";
327
+ readonly id: string;
328
+ readonly text?: string | undefined;
329
+ readonly providerMetadata?: {
330
+ readonly [x: string]: {
331
+ readonly [x: string]: unknown;
332
+ };
333
+ } | undefined;
334
+ } | {
335
+ readonly type: "tool-input-start";
336
+ readonly id: string;
337
+ readonly name: string;
338
+ readonly namespace?: string | undefined;
339
+ readonly providerExecuted?: boolean | undefined;
340
+ readonly providerMetadata?: {
341
+ readonly [x: string]: {
342
+ readonly [x: string]: unknown;
343
+ };
344
+ } | undefined;
345
+ } | {
346
+ readonly type: "tool-input-delta";
347
+ readonly id: string;
348
+ readonly name: string;
349
+ readonly namespace?: string | undefined;
350
+ readonly text: string;
351
+ readonly input?: unknown;
352
+ } | {
353
+ readonly type: "tool-input-end";
354
+ readonly id: string;
355
+ readonly name: string;
356
+ readonly namespace?: string | undefined;
357
+ readonly providerMetadata?: {
358
+ readonly [x: string]: {
359
+ readonly [x: string]: unknown;
360
+ };
361
+ } | undefined;
362
+ } | {
363
+ readonly type: "tool-input-error";
364
+ readonly id: string;
365
+ readonly name: string;
366
+ readonly namespace?: string | undefined;
367
+ readonly raw: string;
368
+ } | {
369
+ readonly type: "tool-call";
370
+ readonly id: string;
371
+ readonly name: string;
372
+ readonly namespace?: string | undefined;
373
+ readonly input: unknown;
374
+ readonly providerExecuted?: boolean | undefined;
375
+ readonly providerMetadata?: {
376
+ readonly [x: string]: {
377
+ readonly [x: string]: unknown;
378
+ };
379
+ } | undefined;
380
+ } | {
381
+ readonly type: "tool-result";
382
+ readonly id: string;
383
+ readonly name: string;
384
+ readonly namespace?: string | undefined;
385
+ readonly result: {
386
+ readonly type: "json";
387
+ readonly value: unknown;
388
+ } | {
389
+ readonly type: "text";
390
+ readonly value: unknown;
391
+ } | {
392
+ readonly type: "error";
393
+ readonly value: unknown;
394
+ } | {
395
+ readonly type: "content";
396
+ readonly value: readonly ({
397
+ readonly type: "text";
398
+ readonly text: string;
399
+ } | {
400
+ readonly type: "file";
401
+ readonly uri: string;
402
+ readonly mime: string;
403
+ readonly name?: string | undefined;
404
+ })[];
405
+ };
406
+ readonly output?: {
407
+ readonly structured: unknown;
408
+ readonly content: readonly ({
409
+ readonly type: "text";
410
+ readonly text: string;
411
+ } | {
412
+ readonly type: "file";
413
+ readonly uri: string;
414
+ readonly mime: string;
415
+ readonly name?: string | undefined;
416
+ })[];
417
+ } | undefined;
418
+ readonly providerExecuted?: boolean | undefined;
419
+ readonly providerMetadata?: {
420
+ readonly [x: string]: {
421
+ readonly [x: string]: unknown;
422
+ };
423
+ } | undefined;
424
+ } | {
425
+ readonly type: "tool-error";
426
+ readonly id: string;
427
+ readonly name: string;
428
+ readonly namespace?: string | undefined;
429
+ readonly message: string;
430
+ readonly error?: unknown;
431
+ readonly providerMetadata?: {
432
+ readonly [x: string]: {
433
+ readonly [x: string]: unknown;
434
+ };
435
+ } | undefined;
436
+ } | {
437
+ readonly type: "step-finish";
438
+ readonly index: number;
439
+ readonly reason: {
440
+ readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
441
+ readonly raw?: string | undefined;
442
+ readonly category?: string | undefined;
443
+ readonly explanation?: string | undefined;
444
+ };
445
+ readonly usage?: import("./effect.js").Usage | undefined;
446
+ readonly providerMetadata?: {
447
+ readonly [x: string]: {
448
+ readonly [x: string]: unknown;
449
+ };
450
+ } | undefined;
451
+ } | {
452
+ readonly type: "finish";
453
+ readonly reason: {
454
+ readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
455
+ readonly raw?: string | undefined;
456
+ readonly category?: string | undefined;
457
+ readonly explanation?: string | undefined;
458
+ };
459
+ readonly usage?: import("./effect.js").Usage | undefined;
460
+ readonly providerMetadata?: {
461
+ readonly [x: string]: {
462
+ readonly [x: string]: unknown;
463
+ };
464
+ } | undefined;
465
+ } | {
466
+ readonly type: "media";
467
+ readonly media: Media.Asset;
468
+ readonly providerMetadata?: {
469
+ readonly [x: string]: {
470
+ readonly [x: string]: unknown;
471
+ };
472
+ } | undefined;
473
+ } | {
474
+ readonly type: "provider-error";
475
+ readonly message: string;
476
+ readonly classification?: "context-overflow" | "payload-too-large" | undefined;
477
+ readonly providerMetadata?: {
478
+ readonly [x: string]: {
479
+ readonly [x: string]: unknown;
480
+ };
481
+ } | undefined;
482
+ } | ({
483
+ readonly type: "compaction";
484
+ readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
485
+ readonly id?: string | undefined;
486
+ readonly encrypted?: string | undefined;
487
+ readonly text?: string | null | undefined;
488
+ } & ({
489
+ readonly encrypted: string;
490
+ readonly text?: never;
491
+ } | {
492
+ readonly text: string | null;
493
+ readonly encrypted?: never;
494
+ }))>;
275
495
  };
276
496
  image: {
277
497
  request: typeof import("./image.js").request;
@@ -770,6 +990,226 @@ export declare const ai: {
770
990
  readonly text: string | null;
771
991
  readonly encrypted?: never;
772
992
  }))>;
993
+ /** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
994
+ loop: <const Model extends LanguageModel>(input: LoopInput<Model>, options?: RunOptions) => AsyncIterable<{
995
+ readonly type: "step-start";
996
+ readonly index: number;
997
+ } | {
998
+ readonly type: "raw";
999
+ readonly data: unknown;
1000
+ } | {
1001
+ readonly type: "text-start";
1002
+ readonly id: string;
1003
+ readonly providerMetadata?: {
1004
+ readonly [x: string]: {
1005
+ readonly [x: string]: unknown;
1006
+ };
1007
+ } | undefined;
1008
+ } | {
1009
+ readonly type: "text-delta";
1010
+ readonly id: string;
1011
+ readonly text: string;
1012
+ readonly providerMetadata?: {
1013
+ readonly [x: string]: {
1014
+ readonly [x: string]: unknown;
1015
+ };
1016
+ } | undefined;
1017
+ } | {
1018
+ readonly type: "text-end";
1019
+ readonly id: string;
1020
+ readonly text?: string | undefined;
1021
+ readonly providerMetadata?: {
1022
+ readonly [x: string]: {
1023
+ readonly [x: string]: unknown;
1024
+ };
1025
+ } | undefined;
1026
+ } | {
1027
+ readonly type: "reasoning-start";
1028
+ readonly id: string;
1029
+ readonly providerMetadata?: {
1030
+ readonly [x: string]: {
1031
+ readonly [x: string]: unknown;
1032
+ };
1033
+ } | undefined;
1034
+ } | {
1035
+ readonly type: "reasoning-delta";
1036
+ readonly id: string;
1037
+ readonly text: string;
1038
+ readonly providerMetadata?: {
1039
+ readonly [x: string]: {
1040
+ readonly [x: string]: unknown;
1041
+ };
1042
+ } | undefined;
1043
+ } | {
1044
+ readonly type: "reasoning-end";
1045
+ readonly id: string;
1046
+ readonly text?: string | undefined;
1047
+ readonly providerMetadata?: {
1048
+ readonly [x: string]: {
1049
+ readonly [x: string]: unknown;
1050
+ };
1051
+ } | undefined;
1052
+ } | {
1053
+ readonly type: "tool-input-start";
1054
+ readonly id: string;
1055
+ readonly name: string;
1056
+ readonly namespace?: string | undefined;
1057
+ readonly providerExecuted?: boolean | undefined;
1058
+ readonly providerMetadata?: {
1059
+ readonly [x: string]: {
1060
+ readonly [x: string]: unknown;
1061
+ };
1062
+ } | undefined;
1063
+ } | {
1064
+ readonly type: "tool-input-delta";
1065
+ readonly id: string;
1066
+ readonly name: string;
1067
+ readonly namespace?: string | undefined;
1068
+ readonly text: string;
1069
+ readonly input?: unknown;
1070
+ } | {
1071
+ readonly type: "tool-input-end";
1072
+ readonly id: string;
1073
+ readonly name: string;
1074
+ readonly namespace?: string | undefined;
1075
+ readonly providerMetadata?: {
1076
+ readonly [x: string]: {
1077
+ readonly [x: string]: unknown;
1078
+ };
1079
+ } | undefined;
1080
+ } | {
1081
+ readonly type: "tool-input-error";
1082
+ readonly id: string;
1083
+ readonly name: string;
1084
+ readonly namespace?: string | undefined;
1085
+ readonly raw: string;
1086
+ } | {
1087
+ readonly type: "tool-call";
1088
+ readonly id: string;
1089
+ readonly name: string;
1090
+ readonly namespace?: string | undefined;
1091
+ readonly input: unknown;
1092
+ readonly providerExecuted?: boolean | undefined;
1093
+ readonly providerMetadata?: {
1094
+ readonly [x: string]: {
1095
+ readonly [x: string]: unknown;
1096
+ };
1097
+ } | undefined;
1098
+ } | {
1099
+ readonly type: "tool-result";
1100
+ readonly id: string;
1101
+ readonly name: string;
1102
+ readonly namespace?: string | undefined;
1103
+ readonly result: {
1104
+ readonly type: "json";
1105
+ readonly value: unknown;
1106
+ } | {
1107
+ readonly type: "text";
1108
+ readonly value: unknown;
1109
+ } | {
1110
+ readonly type: "error";
1111
+ readonly value: unknown;
1112
+ } | {
1113
+ readonly type: "content";
1114
+ readonly value: readonly ({
1115
+ readonly type: "text";
1116
+ readonly text: string;
1117
+ } | {
1118
+ readonly type: "file";
1119
+ readonly uri: string;
1120
+ readonly mime: string;
1121
+ readonly name?: string | undefined;
1122
+ })[];
1123
+ };
1124
+ readonly output?: {
1125
+ readonly structured: unknown;
1126
+ readonly content: readonly ({
1127
+ readonly type: "text";
1128
+ readonly text: string;
1129
+ } | {
1130
+ readonly type: "file";
1131
+ readonly uri: string;
1132
+ readonly mime: string;
1133
+ readonly name?: string | undefined;
1134
+ })[];
1135
+ } | undefined;
1136
+ readonly providerExecuted?: boolean | undefined;
1137
+ readonly providerMetadata?: {
1138
+ readonly [x: string]: {
1139
+ readonly [x: string]: unknown;
1140
+ };
1141
+ } | undefined;
1142
+ } | {
1143
+ readonly type: "tool-error";
1144
+ readonly id: string;
1145
+ readonly name: string;
1146
+ readonly namespace?: string | undefined;
1147
+ readonly message: string;
1148
+ readonly error?: unknown;
1149
+ readonly providerMetadata?: {
1150
+ readonly [x: string]: {
1151
+ readonly [x: string]: unknown;
1152
+ };
1153
+ } | undefined;
1154
+ } | {
1155
+ readonly type: "step-finish";
1156
+ readonly index: number;
1157
+ readonly reason: {
1158
+ readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
1159
+ readonly raw?: string | undefined;
1160
+ readonly category?: string | undefined;
1161
+ readonly explanation?: string | undefined;
1162
+ };
1163
+ readonly usage?: import("./effect.js").Usage | undefined;
1164
+ readonly providerMetadata?: {
1165
+ readonly [x: string]: {
1166
+ readonly [x: string]: unknown;
1167
+ };
1168
+ } | undefined;
1169
+ } | {
1170
+ readonly type: "finish";
1171
+ readonly reason: {
1172
+ readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
1173
+ readonly raw?: string | undefined;
1174
+ readonly category?: string | undefined;
1175
+ readonly explanation?: string | undefined;
1176
+ };
1177
+ readonly usage?: import("./effect.js").Usage | undefined;
1178
+ readonly providerMetadata?: {
1179
+ readonly [x: string]: {
1180
+ readonly [x: string]: unknown;
1181
+ };
1182
+ } | undefined;
1183
+ } | {
1184
+ readonly type: "media";
1185
+ readonly media: Media.Asset;
1186
+ readonly providerMetadata?: {
1187
+ readonly [x: string]: {
1188
+ readonly [x: string]: unknown;
1189
+ };
1190
+ } | undefined;
1191
+ } | {
1192
+ readonly type: "provider-error";
1193
+ readonly message: string;
1194
+ readonly classification?: "context-overflow" | "payload-too-large" | undefined;
1195
+ readonly providerMetadata?: {
1196
+ readonly [x: string]: {
1197
+ readonly [x: string]: unknown;
1198
+ };
1199
+ } | undefined;
1200
+ } | ({
1201
+ readonly type: "compaction";
1202
+ readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
1203
+ readonly id?: string | undefined;
1204
+ readonly encrypted?: string | undefined;
1205
+ readonly text?: string | null | undefined;
1206
+ } & ({
1207
+ readonly encrypted: string;
1208
+ readonly text?: never;
1209
+ } | {
1210
+ readonly text: string | null;
1211
+ readonly encrypted?: never;
1212
+ }))>;
773
1213
  };
774
1214
  image: {
775
1215
  request: typeof import("./image.js").request;
package/dist/index.js CHANGED
@@ -66,6 +66,8 @@ export const make = (options = {}) => {
66
66
  request: LLM.request,
67
67
  generate: (input, options) => run(LLM.generate(input), options),
68
68
  stream: (input, options) => iterate(LLM.stream(input), options),
69
+ /** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
70
+ loop: (input, options) => iterate(LLM.loop(input), options),
69
71
  },
70
72
  image: {
71
73
  request: Image.request,
package/dist/llm.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Effect, JsonSchema, Schema, Stream } from "effect";
2
2
  import { Service, type StreamOptions } from "./route/client.js";
3
3
  import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, type ContentPart, type LanguageModelProviderOptions, type ToolEntryInput } from "./schema/index.js";
4
- import { type ToolSchema } from "./tool.js";
4
+ import { type ExecutableTools, type ToolSchema } from "./tool.js";
5
5
  /** Input accepted by `LLM.request`, normalized into the canonical `LLMRequest` class. */
6
6
  export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<ConstructorParameters<typeof LLMRequest>[0], "model" | "system" | "messages" | "tools" | "toolChoice" | "generation" | "http" | "providerOptions"> & {
7
7
  readonly model: SelectedLanguageModel;
@@ -16,6 +16,18 @@ export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageM
16
16
  };
17
17
  export declare function generate<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Effect.Effect<LLMResponse, AIError, Service>;
18
18
  export declare function stream<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
19
+ export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools"> & {
20
+ readonly tools: ExecutableTools;
21
+ /** Called after each step that has tool calls; returning `true` ends the loop without running them. */
22
+ readonly until?: (state: {
23
+ readonly steps: ReadonlyArray<LLMResponse>;
24
+ }) => boolean;
25
+ };
26
+ /**
27
+ * Stream model calls, running the model's tool calls between them, until a step has no client-executed tool calls or
28
+ * `until` returns `true`. Each step's `step-start` and `step-finish` carry its step index.
29
+ */
30
+ export declare function loop<const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
19
31
  export declare const request: <const SelectedLanguageModel extends LanguageModel>(input: RequestInput<SelectedLanguageModel>) => LLMRequest<SelectedLanguageModel>;
20
32
  type GenerateObjectBase<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools" | "toolChoice">;
21
33
  export declare class GenerateObjectResponse<T> {
package/dist/llm.js CHANGED
@@ -3,12 +3,46 @@ import { tryRequest } from "./media-model.js";
3
3
  import { LLMClient, Service } from "./route/client.js";
4
4
  import { GenerationOptions, HttpOptions, InvalidProviderOutputError, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolEntry, } from "./schema/index.js";
5
5
  import { make as makeTool, toDefinitions } from "./tool.js";
6
+ import { ToolRuntime } from "./tool-runtime.js";
6
7
  export function generate(input, options) {
7
8
  return requestEffect(input).pipe(Effect.flatMap((request) => LLMClient.generate(request, options)));
8
9
  }
9
10
  export function stream(input, options) {
10
11
  return Stream.unwrap(requestEffect(input).pipe(Effect.map((request) => LLMClient.stream(request, options))));
11
12
  }
13
+ export function loop(input, options) {
14
+ const { tools, until, ...rest } = input;
15
+ const step = (request, steps) => Stream.suspend(() => {
16
+ const events = [];
17
+ return LLMClient.stream(request, options).pipe(Stream.map((event) => event.type === "step-start" || event.type === "step-finish" ? { ...event, index: steps.length } : event), Stream.tap((event) => Effect.sync(() => events.push(event))), Stream.concat(Stream.unwrap(Effect.gen(function* () {
18
+ const response = LLMResponse.fromEvents(events);
19
+ const calls = response?.toolCalls.filter((call) => call.providerExecuted !== true) ?? [];
20
+ if (!response || calls.length === 0)
21
+ return Stream.empty;
22
+ const done = [...steps, response];
23
+ if (until?.({ steps: done }))
24
+ return Stream.empty;
25
+ const results = yield* Effect.forEach(calls, (call) => ToolRuntime.dispatch(tools, call), {
26
+ concurrency: "unbounded",
27
+ });
28
+ const next = LLMRequest.update(request, {
29
+ messages: [
30
+ ...request.messages,
31
+ response.message,
32
+ ...calls.map((call, i) => Message.tool({
33
+ id: call.id,
34
+ name: call.name,
35
+ namespace: call.namespace,
36
+ result: results[i].result,
37
+ providerMetadata: call.providerMetadata,
38
+ })),
39
+ ],
40
+ });
41
+ return Stream.fromIterable(results.flatMap((result) => result.events)).pipe(Stream.concat(step(next, done)));
42
+ }))));
43
+ });
44
+ return Stream.unwrap(requestEffect({ ...rest, tools: toDefinitions(tools) }).pipe(Effect.map((request) => step(request, []))));
45
+ }
12
46
  const requestEffect = (input) => input instanceof LLMRequest ? Effect.succeed(input) : tryRequest(() => request(input));
13
47
  export const request = (input) => {
14
48
  const { system: requestSystem, prompt, messages, tools, toolChoice: requestToolChoice, generation: requestGeneration, providerOptions: requestProviderOptions, http: requestHttp, ...rest } = input;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
- "version": "0.0.0-dev-51",
3
+ "version": "0.0.0-dev-56",
4
4
  "name": "@opencode/ai",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -532,7 +532,7 @@
532
532
  "devDependencies": {
533
533
  "@clack/prompts": "1.0.0-alpha.1",
534
534
  "@effect/platform-node": "4.0.1",
535
- "@opencode/http-recorder": "0.0.0-dev-51",
535
+ "@opencode/http-recorder": "0.0.0-dev-56",
536
536
  "@tsconfig/bun": "1.0.9",
537
537
  "@types/bun": "1.4.3",
538
538
  "@typescript/native": "npm:typescript@7.1.0-beta",
@@ -542,7 +542,7 @@
542
542
  "@aws-sdk/credential-providers": "3.1057.0",
543
543
  "@smithy/eventstream-codec": "4.2.14",
544
544
  "@smithy/util-utf8": "4.2.2",
545
- "@opencode/schema": "0.0.0-dev-51",
545
+ "@opencode/schema": "0.0.0-dev-56",
546
546
  "aws4fetch": "1.0.20",
547
547
  "effect": "4.0.1",
548
548
  "google-auth-library": "10.5.0"