@opencode/ai 0.0.0-dev-431 → 0.0.0-dev-433
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 +8 -8
- package/dist/effect.d.ts +0 -1
- package/dist/effect.js +0 -1
- package/dist/index.d.ts +15 -445
- package/dist/index.js +11 -7
- package/dist/llm.d.ts +26 -248
- package/dist/llm.js +70 -53
- package/dist/schema/errors.d.ts +1 -1
- package/dist/schema/errors.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -104,17 +104,17 @@ history works with or without `freeform`.
|
|
|
104
104
|
|
|
105
105
|
### Structured output
|
|
106
106
|
|
|
107
|
-
`
|
|
108
|
-
unvalidated `unknown` object:
|
|
107
|
+
Pass `schema` to `generate`, `stream` or `loop`. The model must answer through a hidden tool call, decoded into
|
|
108
|
+
`object` (typed from an Effect Schema, or an unvalidated `unknown` for a raw JSON Schema object):
|
|
109
109
|
|
|
110
110
|
```ts
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
prompt: "Pick a city.",
|
|
114
|
-
schema: Schema.Struct({ city: Schema.String }),
|
|
115
|
-
})
|
|
111
|
+
const res = await ai.generate({ model, prompt: "Pick a city.", schema: Schema.Struct({ city: Schema.String }) })
|
|
112
|
+
res.object.city
|
|
116
113
|
```
|
|
117
114
|
|
|
115
|
+
`stream` puts `object` on its `finish` event; `loop` runs tools first and puts it on the final step's `finish`. A
|
|
116
|
+
decode failure is an `AIError` whose `partial` holds the response.
|
|
117
|
+
|
|
118
118
|
### Raw provider events
|
|
119
119
|
|
|
120
120
|
Set `includeRaw: true` on a request to also receive every provider frame as `{ type: "raw", data }`, in order and
|
|
@@ -894,7 +894,7 @@ Everything below is exported from `@opencode/ai/effect` unless noted.
|
|
|
894
894
|
- **`Generation`** — provider-neutral handle for an in-flight media generation (`await`, `refresh`, `cancel`, `events`) used by queued media routes.
|
|
895
895
|
- **`ai.decision.request` / `ai.decision.generate`** — typed predicate, choice, and score questions over text, JSON, and media input; `DecisionClient` is its Effect service and layer.
|
|
896
896
|
- **`AIClient.layer` / `AIClient.layerWith(executor)`** — every modality client plus the request executor in one layer.
|
|
897
|
-
- **`@opencode/ai`** (Promise root) — a default `ai` with the text calls (`request`, `generate`, `stream`, `loop`, `
|
|
897
|
+
- **`@opencode/ai`** (Promise root) — a default `ai` with the text calls (`request`, `generate`, `stream`, `loop`, `compact`, `canCompact`) at the top level plus `image` and `decision` (plus `experimental.video`, `experimental.speech`, and `experimental.transcription`) as Promise / `AsyncIterable` APIs, `run(effect)` for any package Effect, plus `file`, `write`, `bytes`, `base64`, and `materialize` for assets. `AI.make({ layer?, clients? })` builds a client over a custom executor or test layers. The same `ai` from `@opencode/ai/effect` returns `Effect` and `Stream`.
|
|
898
898
|
- **`@opencode/ai/experimental`** — APIs that may change without compatibility guarantees: `Video`, `Speech`, `Transcription`, `Extraction`, and `Realtime`. See [Experimental](#experimental).
|
|
899
899
|
|
|
900
900
|
## Experimental
|
package/dist/effect.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ export declare const ai: {
|
|
|
5
5
|
readonly generate: typeof LLM.generate;
|
|
6
6
|
readonly stream: typeof LLM.stream;
|
|
7
7
|
readonly loop: typeof LLM.loop;
|
|
8
|
-
readonly generateObject: typeof LLM.generateObject;
|
|
9
8
|
readonly compact: typeof LLM.compact;
|
|
10
9
|
readonly canCompact: typeof LLM.canCompact;
|
|
11
10
|
readonly image: {
|
package/dist/effect.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { LLM } from "./llm.js";
|
|
|
7
7
|
import { Media } from "./media.js";
|
|
8
8
|
import { type CheckpointRequest, type CompactionRequest, type EndpointCompactOptions, type StreamOptions, type TriggerCompactOptions } from "./route/client.js";
|
|
9
9
|
import { RequestExecutor } from "./route/executor.js";
|
|
10
|
-
import { LanguageModel, type CompactionCheckpointResponse, type CompactionResponse, type LLMRequest } from "./schema/index.js";
|
|
11
|
-
import type {
|
|
10
|
+
import { LanguageModel, type CompactionCheckpointResponse, type CompactionResponse, type LLMEvent, type LLMRequest, type LLMResponse } from "./schema/index.js";
|
|
11
|
+
import type { LoopInput, LoopObjectEvent, ObjectEvent, ObjectInput, ObjectResponse, OutputSchema, OutputType, RequestInput } from "./llm.js";
|
|
12
12
|
import { type SpeechModel, type SpeechRequest, type SpeechRequestInput } from "./speech.js";
|
|
13
13
|
import { type TranscriptionModel, type TranscriptionRequest, type TranscriptionRequestInput } from "./transcription.js";
|
|
14
14
|
import type { NoExecute, ToolExecuteContext, ToolOptions, ToolSchema } from "./tool.js";
|
|
@@ -57,450 +57,20 @@ export declare const make: <R = never>(options?: Options<R>) => {
|
|
|
57
57
|
/** Write an asset's bytes like `Media.write`, downloading `url` sources through the executor. */
|
|
58
58
|
write: (asset: Media.Asset, path: string, options?: RunOptions) => Promise<void>;
|
|
59
59
|
request: typeof LLM.request;
|
|
60
|
-
generate:
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
readonly type: "text-start";
|
|
69
|
-
readonly id: string;
|
|
70
|
-
readonly providerMetadata?: {
|
|
71
|
-
readonly [x: string]: {
|
|
72
|
-
readonly [x: string]: unknown;
|
|
73
|
-
};
|
|
74
|
-
} | undefined;
|
|
75
|
-
} | {
|
|
76
|
-
readonly type: "text-delta";
|
|
77
|
-
readonly id: string;
|
|
78
|
-
readonly text: string;
|
|
79
|
-
readonly providerMetadata?: {
|
|
80
|
-
readonly [x: string]: {
|
|
81
|
-
readonly [x: string]: unknown;
|
|
82
|
-
};
|
|
83
|
-
} | undefined;
|
|
84
|
-
} | {
|
|
85
|
-
readonly type: "text-end";
|
|
86
|
-
readonly id: string;
|
|
87
|
-
readonly text?: string | undefined;
|
|
88
|
-
readonly providerMetadata?: {
|
|
89
|
-
readonly [x: string]: {
|
|
90
|
-
readonly [x: string]: unknown;
|
|
91
|
-
};
|
|
92
|
-
} | undefined;
|
|
93
|
-
} | {
|
|
94
|
-
readonly type: "reasoning-start";
|
|
95
|
-
readonly id: string;
|
|
96
|
-
readonly providerMetadata?: {
|
|
97
|
-
readonly [x: string]: {
|
|
98
|
-
readonly [x: string]: unknown;
|
|
99
|
-
};
|
|
100
|
-
} | undefined;
|
|
101
|
-
} | {
|
|
102
|
-
readonly type: "reasoning-delta";
|
|
103
|
-
readonly id: string;
|
|
104
|
-
readonly text: string;
|
|
105
|
-
readonly providerMetadata?: {
|
|
106
|
-
readonly [x: string]: {
|
|
107
|
-
readonly [x: string]: unknown;
|
|
108
|
-
};
|
|
109
|
-
} | undefined;
|
|
110
|
-
} | {
|
|
111
|
-
readonly type: "reasoning-end";
|
|
112
|
-
readonly id: string;
|
|
113
|
-
readonly text?: string | undefined;
|
|
114
|
-
readonly providerMetadata?: {
|
|
115
|
-
readonly [x: string]: {
|
|
116
|
-
readonly [x: string]: unknown;
|
|
117
|
-
};
|
|
118
|
-
} | undefined;
|
|
119
|
-
} | {
|
|
120
|
-
readonly type: "tool-input-start";
|
|
121
|
-
readonly id: string;
|
|
122
|
-
readonly name: string;
|
|
123
|
-
readonly namespace?: string | undefined;
|
|
124
|
-
readonly providerExecuted?: boolean | undefined;
|
|
125
|
-
readonly providerMetadata?: {
|
|
126
|
-
readonly [x: string]: {
|
|
127
|
-
readonly [x: string]: unknown;
|
|
128
|
-
};
|
|
129
|
-
} | undefined;
|
|
130
|
-
} | {
|
|
131
|
-
readonly type: "tool-input-delta";
|
|
132
|
-
readonly id: string;
|
|
133
|
-
readonly name: string;
|
|
134
|
-
readonly namespace?: string | undefined;
|
|
135
|
-
readonly text: string;
|
|
136
|
-
readonly input?: unknown;
|
|
137
|
-
} | {
|
|
138
|
-
readonly type: "tool-input-end";
|
|
139
|
-
readonly id: string;
|
|
140
|
-
readonly name: string;
|
|
141
|
-
readonly namespace?: string | undefined;
|
|
142
|
-
readonly providerMetadata?: {
|
|
143
|
-
readonly [x: string]: {
|
|
144
|
-
readonly [x: string]: unknown;
|
|
145
|
-
};
|
|
146
|
-
} | undefined;
|
|
147
|
-
} | {
|
|
148
|
-
readonly type: "tool-input-error";
|
|
149
|
-
readonly id: string;
|
|
150
|
-
readonly name: string;
|
|
151
|
-
readonly namespace?: string | undefined;
|
|
152
|
-
readonly raw: string;
|
|
153
|
-
} | {
|
|
154
|
-
readonly type: "tool-call";
|
|
155
|
-
readonly id: string;
|
|
156
|
-
readonly name: string;
|
|
157
|
-
readonly namespace?: string | undefined;
|
|
158
|
-
readonly input: unknown;
|
|
159
|
-
readonly providerExecuted?: boolean | undefined;
|
|
160
|
-
readonly providerMetadata?: {
|
|
161
|
-
readonly [x: string]: {
|
|
162
|
-
readonly [x: string]: unknown;
|
|
163
|
-
};
|
|
164
|
-
} | undefined;
|
|
165
|
-
} | {
|
|
166
|
-
readonly type: "tool-result";
|
|
167
|
-
readonly id: string;
|
|
168
|
-
readonly name: string;
|
|
169
|
-
readonly namespace?: string | undefined;
|
|
170
|
-
readonly result: {
|
|
171
|
-
readonly type: "json";
|
|
172
|
-
readonly value: unknown;
|
|
173
|
-
} | {
|
|
174
|
-
readonly type: "text";
|
|
175
|
-
readonly value: unknown;
|
|
176
|
-
} | {
|
|
177
|
-
readonly type: "error";
|
|
178
|
-
readonly value: unknown;
|
|
179
|
-
} | {
|
|
180
|
-
readonly type: "content";
|
|
181
|
-
readonly value: readonly ({
|
|
182
|
-
readonly type: "text";
|
|
183
|
-
readonly text: string;
|
|
184
|
-
} | {
|
|
185
|
-
readonly type: "file";
|
|
186
|
-
readonly uri: string;
|
|
187
|
-
readonly mime: string;
|
|
188
|
-
readonly name?: string | undefined;
|
|
189
|
-
})[];
|
|
190
|
-
};
|
|
191
|
-
readonly output?: {
|
|
192
|
-
readonly structured: unknown;
|
|
193
|
-
readonly content: readonly ({
|
|
194
|
-
readonly type: "text";
|
|
195
|
-
readonly text: string;
|
|
196
|
-
} | {
|
|
197
|
-
readonly type: "file";
|
|
198
|
-
readonly uri: string;
|
|
199
|
-
readonly mime: string;
|
|
200
|
-
readonly name?: string | undefined;
|
|
201
|
-
})[];
|
|
202
|
-
} | undefined;
|
|
203
|
-
readonly providerExecuted?: boolean | undefined;
|
|
204
|
-
readonly providerMetadata?: {
|
|
205
|
-
readonly [x: string]: {
|
|
206
|
-
readonly [x: string]: unknown;
|
|
207
|
-
};
|
|
208
|
-
} | undefined;
|
|
209
|
-
} | {
|
|
210
|
-
readonly type: "tool-error";
|
|
211
|
-
readonly id: string;
|
|
212
|
-
readonly name: string;
|
|
213
|
-
readonly namespace?: string | undefined;
|
|
214
|
-
readonly message: string;
|
|
215
|
-
readonly error?: unknown;
|
|
216
|
-
readonly providerMetadata?: {
|
|
217
|
-
readonly [x: string]: {
|
|
218
|
-
readonly [x: string]: unknown;
|
|
219
|
-
};
|
|
220
|
-
} | undefined;
|
|
221
|
-
} | {
|
|
222
|
-
readonly type: "step-finish";
|
|
223
|
-
readonly index: number;
|
|
224
|
-
readonly reason: {
|
|
225
|
-
readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
|
|
226
|
-
readonly raw?: string | undefined;
|
|
227
|
-
readonly category?: string | undefined;
|
|
228
|
-
readonly explanation?: string | undefined;
|
|
229
|
-
};
|
|
230
|
-
readonly usage?: import("./index.js").Usage | undefined;
|
|
231
|
-
readonly providerMetadata?: {
|
|
232
|
-
readonly [x: string]: {
|
|
233
|
-
readonly [x: string]: unknown;
|
|
234
|
-
};
|
|
235
|
-
} | undefined;
|
|
236
|
-
} | {
|
|
237
|
-
readonly type: "finish";
|
|
238
|
-
readonly reason: {
|
|
239
|
-
readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
|
|
240
|
-
readonly raw?: string | undefined;
|
|
241
|
-
readonly category?: string | undefined;
|
|
242
|
-
readonly explanation?: string | undefined;
|
|
243
|
-
};
|
|
244
|
-
readonly usage?: import("./index.js").Usage | undefined;
|
|
245
|
-
readonly providerMetadata?: {
|
|
246
|
-
readonly [x: string]: {
|
|
247
|
-
readonly [x: string]: unknown;
|
|
248
|
-
};
|
|
249
|
-
} | undefined;
|
|
250
|
-
} | {
|
|
251
|
-
readonly type: "media";
|
|
252
|
-
readonly media: Media.Asset;
|
|
253
|
-
readonly providerMetadata?: {
|
|
254
|
-
readonly [x: string]: {
|
|
255
|
-
readonly [x: string]: unknown;
|
|
256
|
-
};
|
|
257
|
-
} | undefined;
|
|
258
|
-
} | {
|
|
259
|
-
readonly type: "provider-error";
|
|
260
|
-
readonly message: string;
|
|
261
|
-
readonly classification?: "context-overflow" | "payload-too-large" | undefined;
|
|
262
|
-
readonly providerMetadata?: {
|
|
263
|
-
readonly [x: string]: {
|
|
264
|
-
readonly [x: string]: unknown;
|
|
265
|
-
};
|
|
266
|
-
} | undefined;
|
|
267
|
-
} | ({
|
|
268
|
-
readonly type: "compaction";
|
|
269
|
-
readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
|
|
270
|
-
readonly id?: string | undefined;
|
|
271
|
-
readonly encrypted?: string | undefined;
|
|
272
|
-
readonly text?: string | null | undefined;
|
|
273
|
-
} & ({
|
|
274
|
-
readonly encrypted: string;
|
|
275
|
-
readonly text?: never;
|
|
276
|
-
} | {
|
|
277
|
-
readonly text: string | null;
|
|
278
|
-
readonly encrypted?: never;
|
|
279
|
-
}))>;
|
|
60
|
+
generate: {
|
|
61
|
+
<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions & RunOptions): Promise<ObjectResponse<OutputType<S>>>;
|
|
62
|
+
<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions & RunOptions): Promise<LLMResponse>;
|
|
63
|
+
};
|
|
64
|
+
stream: {
|
|
65
|
+
<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions & RunOptions): AsyncIterable<ObjectEvent<OutputType<S>>>;
|
|
66
|
+
<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions & RunOptions): AsyncIterable<LLMEvent>;
|
|
67
|
+
};
|
|
280
68
|
/** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
|
|
281
|
-
loop:
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
readonly data: unknown;
|
|
287
|
-
} | {
|
|
288
|
-
readonly type: "text-start";
|
|
289
|
-
readonly id: string;
|
|
290
|
-
readonly providerMetadata?: {
|
|
291
|
-
readonly [x: string]: {
|
|
292
|
-
readonly [x: string]: unknown;
|
|
293
|
-
};
|
|
294
|
-
} | undefined;
|
|
295
|
-
} | {
|
|
296
|
-
readonly type: "text-delta";
|
|
297
|
-
readonly id: string;
|
|
298
|
-
readonly text: string;
|
|
299
|
-
readonly providerMetadata?: {
|
|
300
|
-
readonly [x: string]: {
|
|
301
|
-
readonly [x: string]: unknown;
|
|
302
|
-
};
|
|
303
|
-
} | undefined;
|
|
304
|
-
} | {
|
|
305
|
-
readonly type: "text-end";
|
|
306
|
-
readonly id: string;
|
|
307
|
-
readonly text?: string | undefined;
|
|
308
|
-
readonly providerMetadata?: {
|
|
309
|
-
readonly [x: string]: {
|
|
310
|
-
readonly [x: string]: unknown;
|
|
311
|
-
};
|
|
312
|
-
} | undefined;
|
|
313
|
-
} | {
|
|
314
|
-
readonly type: "reasoning-start";
|
|
315
|
-
readonly id: string;
|
|
316
|
-
readonly providerMetadata?: {
|
|
317
|
-
readonly [x: string]: {
|
|
318
|
-
readonly [x: string]: unknown;
|
|
319
|
-
};
|
|
320
|
-
} | undefined;
|
|
321
|
-
} | {
|
|
322
|
-
readonly type: "reasoning-delta";
|
|
323
|
-
readonly id: string;
|
|
324
|
-
readonly text: string;
|
|
325
|
-
readonly providerMetadata?: {
|
|
326
|
-
readonly [x: string]: {
|
|
327
|
-
readonly [x: string]: unknown;
|
|
328
|
-
};
|
|
329
|
-
} | undefined;
|
|
330
|
-
} | {
|
|
331
|
-
readonly type: "reasoning-end";
|
|
332
|
-
readonly id: string;
|
|
333
|
-
readonly text?: string | undefined;
|
|
334
|
-
readonly providerMetadata?: {
|
|
335
|
-
readonly [x: string]: {
|
|
336
|
-
readonly [x: string]: unknown;
|
|
337
|
-
};
|
|
338
|
-
} | undefined;
|
|
339
|
-
} | {
|
|
340
|
-
readonly type: "tool-input-start";
|
|
341
|
-
readonly id: string;
|
|
342
|
-
readonly name: string;
|
|
343
|
-
readonly namespace?: string | undefined;
|
|
344
|
-
readonly providerExecuted?: boolean | undefined;
|
|
345
|
-
readonly providerMetadata?: {
|
|
346
|
-
readonly [x: string]: {
|
|
347
|
-
readonly [x: string]: unknown;
|
|
348
|
-
};
|
|
349
|
-
} | undefined;
|
|
350
|
-
} | {
|
|
351
|
-
readonly type: "tool-input-delta";
|
|
352
|
-
readonly id: string;
|
|
353
|
-
readonly name: string;
|
|
354
|
-
readonly namespace?: string | undefined;
|
|
355
|
-
readonly text: string;
|
|
356
|
-
readonly input?: unknown;
|
|
357
|
-
} | {
|
|
358
|
-
readonly type: "tool-input-end";
|
|
359
|
-
readonly id: string;
|
|
360
|
-
readonly name: string;
|
|
361
|
-
readonly namespace?: string | undefined;
|
|
362
|
-
readonly providerMetadata?: {
|
|
363
|
-
readonly [x: string]: {
|
|
364
|
-
readonly [x: string]: unknown;
|
|
365
|
-
};
|
|
366
|
-
} | undefined;
|
|
367
|
-
} | {
|
|
368
|
-
readonly type: "tool-input-error";
|
|
369
|
-
readonly id: string;
|
|
370
|
-
readonly name: string;
|
|
371
|
-
readonly namespace?: string | undefined;
|
|
372
|
-
readonly raw: string;
|
|
373
|
-
} | {
|
|
374
|
-
readonly type: "tool-call";
|
|
375
|
-
readonly id: string;
|
|
376
|
-
readonly name: string;
|
|
377
|
-
readonly namespace?: string | undefined;
|
|
378
|
-
readonly input: unknown;
|
|
379
|
-
readonly providerExecuted?: boolean | undefined;
|
|
380
|
-
readonly providerMetadata?: {
|
|
381
|
-
readonly [x: string]: {
|
|
382
|
-
readonly [x: string]: unknown;
|
|
383
|
-
};
|
|
384
|
-
} | undefined;
|
|
385
|
-
} | {
|
|
386
|
-
readonly type: "tool-result";
|
|
387
|
-
readonly id: string;
|
|
388
|
-
readonly name: string;
|
|
389
|
-
readonly namespace?: string | undefined;
|
|
390
|
-
readonly result: {
|
|
391
|
-
readonly type: "json";
|
|
392
|
-
readonly value: unknown;
|
|
393
|
-
} | {
|
|
394
|
-
readonly type: "text";
|
|
395
|
-
readonly value: unknown;
|
|
396
|
-
} | {
|
|
397
|
-
readonly type: "error";
|
|
398
|
-
readonly value: unknown;
|
|
399
|
-
} | {
|
|
400
|
-
readonly type: "content";
|
|
401
|
-
readonly value: readonly ({
|
|
402
|
-
readonly type: "text";
|
|
403
|
-
readonly text: string;
|
|
404
|
-
} | {
|
|
405
|
-
readonly type: "file";
|
|
406
|
-
readonly uri: string;
|
|
407
|
-
readonly mime: string;
|
|
408
|
-
readonly name?: string | undefined;
|
|
409
|
-
})[];
|
|
410
|
-
};
|
|
411
|
-
readonly output?: {
|
|
412
|
-
readonly structured: unknown;
|
|
413
|
-
readonly content: readonly ({
|
|
414
|
-
readonly type: "text";
|
|
415
|
-
readonly text: string;
|
|
416
|
-
} | {
|
|
417
|
-
readonly type: "file";
|
|
418
|
-
readonly uri: string;
|
|
419
|
-
readonly mime: string;
|
|
420
|
-
readonly name?: string | undefined;
|
|
421
|
-
})[];
|
|
422
|
-
} | undefined;
|
|
423
|
-
readonly providerExecuted?: boolean | undefined;
|
|
424
|
-
readonly providerMetadata?: {
|
|
425
|
-
readonly [x: string]: {
|
|
426
|
-
readonly [x: string]: unknown;
|
|
427
|
-
};
|
|
428
|
-
} | undefined;
|
|
429
|
-
} | {
|
|
430
|
-
readonly type: "tool-error";
|
|
431
|
-
readonly id: string;
|
|
432
|
-
readonly name: string;
|
|
433
|
-
readonly namespace?: string | undefined;
|
|
434
|
-
readonly message: string;
|
|
435
|
-
readonly error?: unknown;
|
|
436
|
-
readonly providerMetadata?: {
|
|
437
|
-
readonly [x: string]: {
|
|
438
|
-
readonly [x: string]: unknown;
|
|
439
|
-
};
|
|
440
|
-
} | undefined;
|
|
441
|
-
} | {
|
|
442
|
-
readonly type: "step-finish";
|
|
443
|
-
readonly index: number;
|
|
444
|
-
readonly reason: {
|
|
445
|
-
readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
|
|
446
|
-
readonly raw?: string | undefined;
|
|
447
|
-
readonly category?: string | undefined;
|
|
448
|
-
readonly explanation?: string | undefined;
|
|
449
|
-
};
|
|
450
|
-
readonly usage?: import("./index.js").Usage | undefined;
|
|
451
|
-
readonly providerMetadata?: {
|
|
452
|
-
readonly [x: string]: {
|
|
453
|
-
readonly [x: string]: unknown;
|
|
454
|
-
};
|
|
455
|
-
} | undefined;
|
|
456
|
-
} | {
|
|
457
|
-
readonly type: "finish";
|
|
458
|
-
readonly reason: {
|
|
459
|
-
readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
|
|
460
|
-
readonly raw?: string | undefined;
|
|
461
|
-
readonly category?: string | undefined;
|
|
462
|
-
readonly explanation?: string | undefined;
|
|
463
|
-
};
|
|
464
|
-
readonly usage?: import("./index.js").Usage | undefined;
|
|
465
|
-
readonly providerMetadata?: {
|
|
466
|
-
readonly [x: string]: {
|
|
467
|
-
readonly [x: string]: unknown;
|
|
468
|
-
};
|
|
469
|
-
} | undefined;
|
|
470
|
-
} | {
|
|
471
|
-
readonly type: "media";
|
|
472
|
-
readonly media: Media.Asset;
|
|
473
|
-
readonly providerMetadata?: {
|
|
474
|
-
readonly [x: string]: {
|
|
475
|
-
readonly [x: string]: unknown;
|
|
476
|
-
};
|
|
477
|
-
} | undefined;
|
|
478
|
-
} | {
|
|
479
|
-
readonly type: "provider-error";
|
|
480
|
-
readonly message: string;
|
|
481
|
-
readonly classification?: "context-overflow" | "payload-too-large" | undefined;
|
|
482
|
-
readonly providerMetadata?: {
|
|
483
|
-
readonly [x: string]: {
|
|
484
|
-
readonly [x: string]: unknown;
|
|
485
|
-
};
|
|
486
|
-
} | undefined;
|
|
487
|
-
} | ({
|
|
488
|
-
readonly type: "compaction";
|
|
489
|
-
readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
|
|
490
|
-
readonly id?: string | undefined;
|
|
491
|
-
readonly encrypted?: string | undefined;
|
|
492
|
-
readonly text?: string | null | undefined;
|
|
493
|
-
} & ({
|
|
494
|
-
readonly encrypted: string;
|
|
495
|
-
readonly text?: never;
|
|
496
|
-
} | {
|
|
497
|
-
readonly text: string | null;
|
|
498
|
-
readonly encrypted?: never;
|
|
499
|
-
}))>;
|
|
500
|
-
/** Decode the model's output against `schema` (or `jsonSchema`, unvalidated). */
|
|
501
|
-
generateObject: {
|
|
502
|
-
<const Model extends LanguageModel, S extends ToolSchema<any>>(input: GenerateObjectOptions<S, Model>, options?: RunOptions): Promise<GenerateObjectResponse<Schema.Schema.Type<S>>>;
|
|
503
|
-
<const Model extends LanguageModel>(input: GenerateObjectDynamicOptions<Model>, options?: RunOptions): Promise<GenerateObjectResponse<unknown>>;
|
|
69
|
+
loop: {
|
|
70
|
+
<const Model extends LanguageModel, S extends OutputSchema>(input: LoopInput<Model> & {
|
|
71
|
+
readonly schema: S;
|
|
72
|
+
}, options?: StreamOptions & RunOptions): AsyncIterable<LoopObjectEvent<OutputType<S>>>;
|
|
73
|
+
<const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions & RunOptions): AsyncIterable<LLMEvent>;
|
|
504
74
|
};
|
|
505
75
|
/** Compact a Responses request: a replacement window, or a checkpoint with `mechanism: "trigger"`. */
|
|
506
76
|
compact: {
|
package/dist/index.js
CHANGED
|
@@ -31,8 +31,14 @@ export const make = (options = {}) => {
|
|
|
31
31
|
/** Run any package Effect (for example `ai.compact(...)` from `@opencode/ai/effect`) inside this runtime. */
|
|
32
32
|
const run = (effect, options) => runtime.runPromise(Effect.raceFirst(effect, abortEffect(options?.signal)));
|
|
33
33
|
const iterate = (stream, options) => Stream.toAsyncIterable(Stream.unwrap(runtime.contextEffect.pipe(Effect.map((context) => stream.pipe(Stream.interruptWhen(abortEffect(options?.signal)), Stream.provideContext(context))))));
|
|
34
|
-
function
|
|
35
|
-
return run(
|
|
34
|
+
function generate(input, options) {
|
|
35
|
+
return run(LLM.generate(input, options), options);
|
|
36
|
+
}
|
|
37
|
+
function stream(input, options) {
|
|
38
|
+
return iterate(LLM.stream(input, options), options);
|
|
39
|
+
}
|
|
40
|
+
function loop(input, options) {
|
|
41
|
+
return iterate(LLM.loop(input, options), options);
|
|
36
42
|
}
|
|
37
43
|
// The overloads pair each request type with its mechanism; `LLM.compact` checks the route's capability at runtime.
|
|
38
44
|
function compact(request, options) {
|
|
@@ -78,12 +84,10 @@ export const make = (options = {}) => {
|
|
|
78
84
|
}))), options);
|
|
79
85
|
},
|
|
80
86
|
request: LLM.request,
|
|
81
|
-
generate
|
|
82
|
-
stream
|
|
87
|
+
generate,
|
|
88
|
+
stream,
|
|
83
89
|
/** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
|
|
84
|
-
loop
|
|
85
|
-
/** Decode the model's output against `schema` (or `jsonSchema`, unvalidated). */
|
|
86
|
-
generateObject,
|
|
90
|
+
loop,
|
|
87
91
|
/** Compact a Responses request: a replacement window, or a checkpoint with `mechanism: "trigger"`. */
|
|
88
92
|
compact,
|
|
89
93
|
canCompact: LLM.canCompact,
|
package/dist/llm.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect, JsonSchema, Schema, Stream } from "effect";
|
|
2
2
|
import { Service, type StreamOptions } from "./route/client.js";
|
|
3
|
-
import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, type ContentPart, type LanguageModelProviderOptions, type ToolEntryInput } from "./schema/index.js";
|
|
3
|
+
import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, type Finish, ToolChoice, type ContentPart, type LanguageModelProviderOptions, type ToolEntryInput } from "./schema/index.js";
|
|
4
4
|
import { type ExecutableTool, 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"> & {
|
|
@@ -14,7 +14,28 @@ export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageM
|
|
|
14
14
|
readonly providerOptions?: NoInfer<LanguageModelProviderOptions<SelectedLanguageModel>>;
|
|
15
15
|
readonly http?: HttpOptions.Input;
|
|
16
16
|
};
|
|
17
|
+
/** An Effect Schema decodes and types `object`; a raw JSON Schema leaves it `unknown`. */
|
|
18
|
+
export type OutputSchema = ToolSchema<any> | JsonSchema.JsonSchema;
|
|
19
|
+
export type OutputType<S extends OutputSchema> = S extends Schema.Top ? Schema.Schema.Type<S> : unknown;
|
|
20
|
+
export type ObjectInput<S extends OutputSchema, SelectedLanguageModel extends LanguageModel = LanguageModel> = RequestInput<SelectedLanguageModel> & {
|
|
21
|
+
readonly schema: S;
|
|
22
|
+
};
|
|
23
|
+
export type ObjectResponse<T> = LLMResponse & {
|
|
24
|
+
readonly object: T;
|
|
25
|
+
};
|
|
26
|
+
/** Stream events for a request with `schema`: the `finish` event carries the decoded `object`. */
|
|
27
|
+
export type ObjectEvent<T> = Exclude<LLMEvent, Finish> | (Finish & {
|
|
28
|
+
readonly object: T;
|
|
29
|
+
});
|
|
30
|
+
/** Loop events for a request with `schema`: the final step's `finish` event carries the decoded `object`. */
|
|
31
|
+
export type LoopObjectEvent<T> = Exclude<LLMEvent, Finish> | (Finish & {
|
|
32
|
+
readonly object?: T;
|
|
33
|
+
});
|
|
34
|
+
/** Run one model call. With `schema`, the model must answer through a forced tool call decoded into `object`. */
|
|
35
|
+
export declare function generate<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions): Effect.Effect<ObjectResponse<OutputType<S>>, AIError, Service>;
|
|
17
36
|
export declare function generate<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Effect.Effect<LLMResponse, AIError, Service>;
|
|
37
|
+
/** Stream one model call. With `schema`, the `finish` event carries the decoded `object`. */
|
|
38
|
+
export declare function stream<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions): Stream.Stream<ObjectEvent<OutputType<S>>, AIError, Service>;
|
|
18
39
|
export declare function stream<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
|
|
19
40
|
export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools"> & {
|
|
20
41
|
readonly tools: Record<string, ExecutableTool>;
|
|
@@ -26,255 +47,12 @@ export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageMode
|
|
|
26
47
|
/**
|
|
27
48
|
* Stream model calls, running the model's tool calls between them, until a step has no client-executed tool calls,
|
|
28
49
|
* reports a provider error, or `until` returns `true`. Each step's `step-start` and `step-finish` carry its step index.
|
|
50
|
+
* With `schema`, the model ends by calling the hidden answer tool, decoded into `object` on that step's `finish`.
|
|
29
51
|
*/
|
|
52
|
+
export declare function loop<const Model extends LanguageModel, S extends OutputSchema>(input: LoopInput<Model> & {
|
|
53
|
+
readonly schema: S;
|
|
54
|
+
}, options?: StreamOptions): Stream.Stream<LoopObjectEvent<OutputType<S>>, AIError, Service>;
|
|
30
55
|
export declare function loop<const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
|
|
31
56
|
export declare const request: <const SelectedLanguageModel extends LanguageModel>(input: RequestInput<SelectedLanguageModel>) => LLMRequest<SelectedLanguageModel>;
|
|
32
|
-
type GenerateObjectBase<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools" | "toolChoice">;
|
|
33
|
-
export declare class GenerateObjectResponse<T> {
|
|
34
|
-
readonly object: T;
|
|
35
|
-
readonly response: LLMResponse;
|
|
36
|
-
constructor(object: T, response: LLMResponse);
|
|
37
|
-
get events(): readonly ({
|
|
38
|
-
readonly type: "step-start";
|
|
39
|
-
readonly index: number;
|
|
40
|
-
} | {
|
|
41
|
-
readonly type: "raw";
|
|
42
|
-
readonly data: unknown;
|
|
43
|
-
} | {
|
|
44
|
-
readonly type: "text-start";
|
|
45
|
-
readonly id: string;
|
|
46
|
-
readonly providerMetadata?: {
|
|
47
|
-
readonly [x: string]: {
|
|
48
|
-
readonly [x: string]: unknown;
|
|
49
|
-
};
|
|
50
|
-
} | undefined;
|
|
51
|
-
} | {
|
|
52
|
-
readonly type: "text-delta";
|
|
53
|
-
readonly id: string;
|
|
54
|
-
readonly text: string;
|
|
55
|
-
readonly providerMetadata?: {
|
|
56
|
-
readonly [x: string]: {
|
|
57
|
-
readonly [x: string]: unknown;
|
|
58
|
-
};
|
|
59
|
-
} | undefined;
|
|
60
|
-
} | {
|
|
61
|
-
readonly type: "text-end";
|
|
62
|
-
readonly id: string;
|
|
63
|
-
readonly text?: string | undefined;
|
|
64
|
-
readonly providerMetadata?: {
|
|
65
|
-
readonly [x: string]: {
|
|
66
|
-
readonly [x: string]: unknown;
|
|
67
|
-
};
|
|
68
|
-
} | undefined;
|
|
69
|
-
} | {
|
|
70
|
-
readonly type: "reasoning-start";
|
|
71
|
-
readonly id: string;
|
|
72
|
-
readonly providerMetadata?: {
|
|
73
|
-
readonly [x: string]: {
|
|
74
|
-
readonly [x: string]: unknown;
|
|
75
|
-
};
|
|
76
|
-
} | undefined;
|
|
77
|
-
} | {
|
|
78
|
-
readonly type: "reasoning-delta";
|
|
79
|
-
readonly id: string;
|
|
80
|
-
readonly text: string;
|
|
81
|
-
readonly providerMetadata?: {
|
|
82
|
-
readonly [x: string]: {
|
|
83
|
-
readonly [x: string]: unknown;
|
|
84
|
-
};
|
|
85
|
-
} | undefined;
|
|
86
|
-
} | {
|
|
87
|
-
readonly type: "reasoning-end";
|
|
88
|
-
readonly id: string;
|
|
89
|
-
readonly text?: string | undefined;
|
|
90
|
-
readonly providerMetadata?: {
|
|
91
|
-
readonly [x: string]: {
|
|
92
|
-
readonly [x: string]: unknown;
|
|
93
|
-
};
|
|
94
|
-
} | undefined;
|
|
95
|
-
} | {
|
|
96
|
-
readonly type: "tool-input-start";
|
|
97
|
-
readonly id: string;
|
|
98
|
-
readonly name: string;
|
|
99
|
-
readonly namespace?: string | undefined;
|
|
100
|
-
readonly providerExecuted?: boolean | undefined;
|
|
101
|
-
readonly providerMetadata?: {
|
|
102
|
-
readonly [x: string]: {
|
|
103
|
-
readonly [x: string]: unknown;
|
|
104
|
-
};
|
|
105
|
-
} | undefined;
|
|
106
|
-
} | {
|
|
107
|
-
readonly type: "tool-input-delta";
|
|
108
|
-
readonly id: string;
|
|
109
|
-
readonly name: string;
|
|
110
|
-
readonly namespace?: string | undefined;
|
|
111
|
-
readonly text: string;
|
|
112
|
-
readonly input?: unknown;
|
|
113
|
-
} | {
|
|
114
|
-
readonly type: "tool-input-end";
|
|
115
|
-
readonly id: string;
|
|
116
|
-
readonly name: string;
|
|
117
|
-
readonly namespace?: string | undefined;
|
|
118
|
-
readonly providerMetadata?: {
|
|
119
|
-
readonly [x: string]: {
|
|
120
|
-
readonly [x: string]: unknown;
|
|
121
|
-
};
|
|
122
|
-
} | undefined;
|
|
123
|
-
} | {
|
|
124
|
-
readonly type: "tool-input-error";
|
|
125
|
-
readonly id: string;
|
|
126
|
-
readonly name: string;
|
|
127
|
-
readonly namespace?: string | undefined;
|
|
128
|
-
readonly raw: string;
|
|
129
|
-
} | {
|
|
130
|
-
readonly type: "tool-call";
|
|
131
|
-
readonly id: string;
|
|
132
|
-
readonly name: string;
|
|
133
|
-
readonly namespace?: string | undefined;
|
|
134
|
-
readonly input: unknown;
|
|
135
|
-
readonly providerExecuted?: boolean | undefined;
|
|
136
|
-
readonly providerMetadata?: {
|
|
137
|
-
readonly [x: string]: {
|
|
138
|
-
readonly [x: string]: unknown;
|
|
139
|
-
};
|
|
140
|
-
} | undefined;
|
|
141
|
-
} | {
|
|
142
|
-
readonly type: "tool-result";
|
|
143
|
-
readonly id: string;
|
|
144
|
-
readonly name: string;
|
|
145
|
-
readonly namespace?: string | undefined;
|
|
146
|
-
readonly result: {
|
|
147
|
-
readonly type: "json";
|
|
148
|
-
readonly value: unknown;
|
|
149
|
-
} | {
|
|
150
|
-
readonly type: "text";
|
|
151
|
-
readonly value: unknown;
|
|
152
|
-
} | {
|
|
153
|
-
readonly type: "error";
|
|
154
|
-
readonly value: unknown;
|
|
155
|
-
} | {
|
|
156
|
-
readonly type: "content";
|
|
157
|
-
readonly value: readonly ({
|
|
158
|
-
readonly type: "text";
|
|
159
|
-
readonly text: string;
|
|
160
|
-
} | {
|
|
161
|
-
readonly type: "file";
|
|
162
|
-
readonly uri: string;
|
|
163
|
-
readonly mime: string;
|
|
164
|
-
readonly name?: string | undefined;
|
|
165
|
-
})[];
|
|
166
|
-
};
|
|
167
|
-
readonly output?: {
|
|
168
|
-
readonly structured: unknown;
|
|
169
|
-
readonly content: readonly ({
|
|
170
|
-
readonly type: "text";
|
|
171
|
-
readonly text: string;
|
|
172
|
-
} | {
|
|
173
|
-
readonly type: "file";
|
|
174
|
-
readonly uri: string;
|
|
175
|
-
readonly mime: string;
|
|
176
|
-
readonly name?: string | undefined;
|
|
177
|
-
})[];
|
|
178
|
-
} | undefined;
|
|
179
|
-
readonly providerExecuted?: boolean | undefined;
|
|
180
|
-
readonly providerMetadata?: {
|
|
181
|
-
readonly [x: string]: {
|
|
182
|
-
readonly [x: string]: unknown;
|
|
183
|
-
};
|
|
184
|
-
} | undefined;
|
|
185
|
-
} | {
|
|
186
|
-
readonly type: "tool-error";
|
|
187
|
-
readonly id: string;
|
|
188
|
-
readonly name: string;
|
|
189
|
-
readonly namespace?: string | undefined;
|
|
190
|
-
readonly message: string;
|
|
191
|
-
readonly error?: unknown;
|
|
192
|
-
readonly providerMetadata?: {
|
|
193
|
-
readonly [x: string]: {
|
|
194
|
-
readonly [x: string]: unknown;
|
|
195
|
-
};
|
|
196
|
-
} | undefined;
|
|
197
|
-
} | {
|
|
198
|
-
readonly type: "step-finish";
|
|
199
|
-
readonly index: number;
|
|
200
|
-
readonly reason: {
|
|
201
|
-
readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
|
|
202
|
-
readonly raw?: string | undefined;
|
|
203
|
-
readonly category?: string | undefined;
|
|
204
|
-
readonly explanation?: string | undefined;
|
|
205
|
-
};
|
|
206
|
-
readonly usage?: import("./schema/events.js").Usage | undefined;
|
|
207
|
-
readonly providerMetadata?: {
|
|
208
|
-
readonly [x: string]: {
|
|
209
|
-
readonly [x: string]: unknown;
|
|
210
|
-
};
|
|
211
|
-
} | undefined;
|
|
212
|
-
} | {
|
|
213
|
-
readonly type: "finish";
|
|
214
|
-
readonly reason: {
|
|
215
|
-
readonly normalized: "content-filter" | "error" | "length" | "stop" | "tool-calls" | "unknown";
|
|
216
|
-
readonly raw?: string | undefined;
|
|
217
|
-
readonly category?: string | undefined;
|
|
218
|
-
readonly explanation?: string | undefined;
|
|
219
|
-
};
|
|
220
|
-
readonly usage?: import("./schema/events.js").Usage | undefined;
|
|
221
|
-
readonly providerMetadata?: {
|
|
222
|
-
readonly [x: string]: {
|
|
223
|
-
readonly [x: string]: unknown;
|
|
224
|
-
};
|
|
225
|
-
} | undefined;
|
|
226
|
-
} | {
|
|
227
|
-
readonly type: "media";
|
|
228
|
-
readonly media: import("./media.js").Asset;
|
|
229
|
-
readonly providerMetadata?: {
|
|
230
|
-
readonly [x: string]: {
|
|
231
|
-
readonly [x: string]: unknown;
|
|
232
|
-
};
|
|
233
|
-
} | undefined;
|
|
234
|
-
} | {
|
|
235
|
-
readonly type: "provider-error";
|
|
236
|
-
readonly message: string;
|
|
237
|
-
readonly classification?: "context-overflow" | "payload-too-large" | undefined;
|
|
238
|
-
readonly providerMetadata?: {
|
|
239
|
-
readonly [x: string]: {
|
|
240
|
-
readonly [x: string]: unknown;
|
|
241
|
-
};
|
|
242
|
-
} | undefined;
|
|
243
|
-
} | ({
|
|
244
|
-
readonly type: "compaction";
|
|
245
|
-
readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
|
|
246
|
-
readonly id?: string | undefined;
|
|
247
|
-
readonly encrypted?: string | undefined;
|
|
248
|
-
readonly text?: string | null | undefined;
|
|
249
|
-
} & ({
|
|
250
|
-
readonly encrypted: string;
|
|
251
|
-
readonly text?: never;
|
|
252
|
-
} | {
|
|
253
|
-
readonly text: string | null;
|
|
254
|
-
readonly encrypted?: never;
|
|
255
|
-
})))[];
|
|
256
|
-
get usage(): import("./schema/events.js").Usage | undefined;
|
|
257
|
-
}
|
|
258
|
-
export interface GenerateObjectOptions<S extends ToolSchema<any>, SelectedLanguageModel extends LanguageModel = LanguageModel> extends GenerateObjectBase<SelectedLanguageModel> {
|
|
259
|
-
readonly schema: S;
|
|
260
|
-
}
|
|
261
|
-
export interface GenerateObjectDynamicOptions<SelectedLanguageModel extends LanguageModel = LanguageModel> extends GenerateObjectBase<SelectedLanguageModel> {
|
|
262
|
-
/** Raw JSON Schema object describing the expected output shape. */
|
|
263
|
-
readonly jsonSchema: JsonSchema.JsonSchema;
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Run a model and decode its output against `schema`. Works on every protocol
|
|
267
|
-
* because it forces a synthetic tool call internally — provider-native JSON
|
|
268
|
-
* modes are intentionally avoided so behaviour is uniform.
|
|
269
|
-
*
|
|
270
|
-
* Two input modes:
|
|
271
|
-
*
|
|
272
|
-
* 1. `schema: EffectSchema<T>` — `.object` is decoded and typed as `T`.
|
|
273
|
-
* Decode failures surface as `AIError`.
|
|
274
|
-
* 2. `jsonSchema: JsonSchema.JsonSchema` — `.object` is `unknown`. Use when
|
|
275
|
-
* the schema is only available at runtime (MCP, plugin manifests). Caller validates.
|
|
276
|
-
*/
|
|
277
|
-
export declare function generateObject<const SelectedLanguageModel extends LanguageModel, S extends ToolSchema<any>>(options: GenerateObjectOptions<S, SelectedLanguageModel>): Effect.Effect<GenerateObjectResponse<Schema.Schema.Type<S>>, AIError, Service>;
|
|
278
|
-
export declare function generateObject<const SelectedLanguageModel extends LanguageModel>(options: GenerateObjectDynamicOptions<SelectedLanguageModel>): Effect.Effect<GenerateObjectResponse<unknown>, AIError, Service>;
|
|
279
57
|
export { canCompact, compact } from "./route/client.js";
|
|
280
58
|
export * as LLM from "./llm.js";
|
package/dist/llm.js
CHANGED
|
@@ -5,18 +5,40 @@ import { GenerationOptions, HttpOptions, InvalidProviderOutputError, AIError, LL
|
|
|
5
5
|
import { tool, toDefinitions } from "./tool.js";
|
|
6
6
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
7
7
|
export function generate(input, options) {
|
|
8
|
-
return
|
|
8
|
+
return prepare(input).pipe(Effect.flatMap(({ request, output }) => LLMClient.generate(request, options).pipe(Effect.flatMap((response) => output === undefined
|
|
9
|
+
? Effect.succeed(response)
|
|
10
|
+
: decodeObject(output, response).pipe(Effect.map((object) => Object.assign(response, { object })))))));
|
|
9
11
|
}
|
|
10
12
|
export function stream(input, options) {
|
|
11
|
-
return Stream.unwrap(
|
|
13
|
+
return Stream.unwrap(prepare(input).pipe(Effect.map(({ request, output }) => Stream.suspend(() => {
|
|
14
|
+
let state = LLMResponse.empty();
|
|
15
|
+
return LLMClient.stream(request, options).pipe(Stream.mapEffect((event) => {
|
|
16
|
+
state = LLMResponse.reduce(state, event);
|
|
17
|
+
return output === undefined || event.type !== "finish"
|
|
18
|
+
? Effect.succeed(event)
|
|
19
|
+
: withObject(output, event, state);
|
|
20
|
+
}));
|
|
21
|
+
}))));
|
|
12
22
|
}
|
|
13
23
|
export function loop(input, options) {
|
|
14
24
|
const { tools, until, ...rest } = input;
|
|
15
|
-
|
|
25
|
+
// A step that calls the answer tool is final, even if it also called other tools.
|
|
26
|
+
const clientCalls = (response, output) => output && response.toolCalls.some((call) => call.name === OBJECT_TOOL)
|
|
27
|
+
? []
|
|
28
|
+
: response.toolCalls.filter((call) => call.providerExecuted !== true);
|
|
29
|
+
const step = (request, steps, output) => Stream.suspend(() => {
|
|
16
30
|
let state = LLMResponse.empty();
|
|
17
|
-
return LLMClient.stream(request, options).pipe(Stream.map((event) => event.type === "step-start" || event.type === "step-finish" ? { ...event, index: steps.length } : event), Stream.
|
|
31
|
+
return LLMClient.stream(request, options).pipe(Stream.map((event) => event.type === "step-start" || event.type === "step-finish" ? { ...event, index: steps.length } : event), Stream.mapEffect((event) => {
|
|
32
|
+
state = LLMResponse.reduce(state, event);
|
|
33
|
+
if (output === undefined || event.type !== "finish")
|
|
34
|
+
return Effect.succeed(event);
|
|
35
|
+
const response = LLMResponse.complete(state);
|
|
36
|
+
return response && clientCalls(response, output).length > 0
|
|
37
|
+
? Effect.succeed(event)
|
|
38
|
+
: withObject(output, event, state);
|
|
39
|
+
}), Stream.mapError((error) => withPartial(error, LLMResponse.partial(state))), Stream.concat(Stream.unwrap(Effect.gen(function* () {
|
|
18
40
|
const response = LLMResponse.complete(state);
|
|
19
|
-
const calls = response
|
|
41
|
+
const calls = response ? clientCalls(response, output) : [];
|
|
20
42
|
if (!response || calls.length === 0 || state.events.some(LLMEvent.is.providerError))
|
|
21
43
|
return Stream.empty;
|
|
22
44
|
const done = [...steps, response];
|
|
@@ -38,12 +60,52 @@ export function loop(input, options) {
|
|
|
38
60
|
})),
|
|
39
61
|
],
|
|
40
62
|
});
|
|
41
|
-
return Stream.fromIterable(results.flatMap((result) => result.events)).pipe(Stream.concat(step(next, done)));
|
|
63
|
+
return Stream.fromIterable(results.flatMap((result) => result.events)).pipe(Stream.concat(step(next, done, output)));
|
|
42
64
|
}))));
|
|
43
65
|
});
|
|
44
|
-
return Stream.unwrap(
|
|
66
|
+
return Stream.unwrap(prepare({ ...rest, tools: toDefinitions(tools) }).pipe(Effect.map(({ request, output }) => step(request, [], output))));
|
|
45
67
|
}
|
|
46
|
-
const
|
|
68
|
+
const OBJECT_TOOL = "generate_object";
|
|
69
|
+
// `schema` adds a hidden tool whose input is the answer. With other tools the model only has to call some tool.
|
|
70
|
+
const prepare = (input) => {
|
|
71
|
+
if (input instanceof LLMRequest)
|
|
72
|
+
return Effect.succeed({ request: input, output: undefined });
|
|
73
|
+
const { schema, ...rest } = input;
|
|
74
|
+
return tryRequest(() => {
|
|
75
|
+
const base = request(rest);
|
|
76
|
+
if (schema === undefined)
|
|
77
|
+
return { request: base, output: undefined };
|
|
78
|
+
const description = "Return the structured result by calling this tool.";
|
|
79
|
+
const output = Schema.isSchema(schema)
|
|
80
|
+
? tool({ description, parameters: schema })
|
|
81
|
+
: tool({ description, jsonSchema: schema });
|
|
82
|
+
const toolChoice = base.tools.length === 0 ? ToolChoice.named(OBJECT_TOOL) : new ToolChoice({ type: "required" });
|
|
83
|
+
return {
|
|
84
|
+
output,
|
|
85
|
+
request: LLMRequest.update(base, {
|
|
86
|
+
tools: [...base.tools, ...toDefinitions({ [OBJECT_TOOL]: output })],
|
|
87
|
+
toolChoice,
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
const decodeObject = (output, response) => {
|
|
93
|
+
const call = response.toolCalls.find((event) => event.name === OBJECT_TOOL);
|
|
94
|
+
const invalid = (message, cause) => new AIError({ reason: new InvalidProviderOutputError({ message, cause }), partial: response });
|
|
95
|
+
if (call)
|
|
96
|
+
return output
|
|
97
|
+
._decode(call.input)
|
|
98
|
+
.pipe(Effect.mapError((error) => invalid(`schema decode failed: ${error.message}`, error)));
|
|
99
|
+
return Effect.fail(invalid(response.finishReason.normalized === "length"
|
|
100
|
+
? "output hit the token limit before the forced tool call completed"
|
|
101
|
+
: `model did not call the forced \`${OBJECT_TOOL}\` tool`));
|
|
102
|
+
};
|
|
103
|
+
const withObject = (output, event, state) => {
|
|
104
|
+
const response = LLMResponse.complete(state);
|
|
105
|
+
return response === undefined
|
|
106
|
+
? Effect.succeed(event)
|
|
107
|
+
: decodeObject(output, response).pipe(Effect.map((object) => ({ ...event, object })));
|
|
108
|
+
};
|
|
47
109
|
export const request = (input) => {
|
|
48
110
|
const { system: requestSystem, prompt, messages, tools, toolChoice: requestToolChoice, generation: requestGeneration, providerOptions: requestProviderOptions, http: requestHttp, ...rest } = input;
|
|
49
111
|
return new LLMRequest({
|
|
@@ -57,50 +119,5 @@ export const request = (input) => {
|
|
|
57
119
|
http: HttpOptions.make(requestHttp),
|
|
58
120
|
});
|
|
59
121
|
};
|
|
60
|
-
const GENERATE_OBJECT_TOOL_NAME = "generate_object";
|
|
61
|
-
const GENERATE_OBJECT_TOOL_DESCRIPTION = "Return the structured result by calling this tool.";
|
|
62
|
-
export class GenerateObjectResponse {
|
|
63
|
-
object;
|
|
64
|
-
response;
|
|
65
|
-
constructor(object, response) {
|
|
66
|
-
this.object = object;
|
|
67
|
-
this.response = response;
|
|
68
|
-
}
|
|
69
|
-
get events() {
|
|
70
|
-
return this.response.events;
|
|
71
|
-
}
|
|
72
|
-
get usage() {
|
|
73
|
-
return this.response.usage;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
const runGenerateObject = Effect.fn("LLM.generateObject")(function* (options, tool) {
|
|
77
|
-
const baseRequest = request(options);
|
|
78
|
-
const generateRequest = LLMRequest.update(baseRequest, {
|
|
79
|
-
tools: toDefinitions({ [GENERATE_OBJECT_TOOL_NAME]: tool }),
|
|
80
|
-
toolChoice: ToolChoice.named(GENERATE_OBJECT_TOOL_NAME),
|
|
81
|
-
});
|
|
82
|
-
const response = yield* LLMClient.generate(generateRequest);
|
|
83
|
-
const call = response.toolCalls.find((event) => event.name === GENERATE_OBJECT_TOOL_NAME);
|
|
84
|
-
const invalidOutput = (reason) => new AIError({ reason, partial: response });
|
|
85
|
-
if (!call) {
|
|
86
|
-
const message = response.finishReason.normalized === "length"
|
|
87
|
-
? "generateObject: output hit the token limit before the forced tool call completed"
|
|
88
|
-
: `generateObject: model did not call the forced \`${GENERATE_OBJECT_TOOL_NAME}\` tool`;
|
|
89
|
-
return yield* invalidOutput(new InvalidProviderOutputError({ message }));
|
|
90
|
-
}
|
|
91
|
-
const object = yield* tool._decode(call.input).pipe(Effect.mapError((error) => invalidOutput(new InvalidProviderOutputError({
|
|
92
|
-
message: `generateObject: tool input failed schema decode: ${error.message}`,
|
|
93
|
-
cause: error,
|
|
94
|
-
}))));
|
|
95
|
-
return new GenerateObjectResponse(object, response);
|
|
96
|
-
});
|
|
97
|
-
export function generateObject(options) {
|
|
98
|
-
if ("schema" in options) {
|
|
99
|
-
const { schema, ...rest } = options;
|
|
100
|
-
return runGenerateObject(rest, tool({ description: GENERATE_OBJECT_TOOL_DESCRIPTION, parameters: schema }));
|
|
101
|
-
}
|
|
102
|
-
const { jsonSchema, ...rest } = options;
|
|
103
|
-
return runGenerateObject(rest, tool({ description: GENERATE_OBJECT_TOOL_DESCRIPTION, jsonSchema }));
|
|
104
|
-
}
|
|
105
122
|
export { canCompact, compact } from "./route/client.js";
|
|
106
123
|
export * as LLM from "./llm.js";
|
package/dist/schema/errors.d.ts
CHANGED
|
@@ -166,7 +166,7 @@ declare const AIError_base: Schema.Class<AIError, Schema.TaggedStruct<"AI.Error"
|
|
|
166
166
|
}>, import("effect/Cause").YieldableError>;
|
|
167
167
|
export declare class AIError extends AIError_base {
|
|
168
168
|
readonly cause: AuthenticationError | ContentPolicyError | InvalidProviderOutputError | InvalidRequestError | NoRouteError | ProviderInternalError | QuotaExceededError | RateLimitError | TimeoutError | TransportError | UnknownProviderError | UnsupportedOperationError;
|
|
169
|
-
/** Output that arrived before `generate
|
|
169
|
+
/** Output that arrived before `generate` (including its `schema` decode) or a `loop` step failed. Not in the schema, so encoding omits it. */
|
|
170
170
|
readonly partial?: LLMResponse;
|
|
171
171
|
constructor(props: Pick<AIError, "reason" | "partial">);
|
|
172
172
|
get message(): string;
|
package/dist/schema/errors.js
CHANGED
|
@@ -125,7 +125,7 @@ export class AIError extends Schema.TaggedError()("AI.Error", {
|
|
|
125
125
|
reason: AIErrorReason,
|
|
126
126
|
}) {
|
|
127
127
|
cause = this.reason;
|
|
128
|
-
/** Output that arrived before `generate
|
|
128
|
+
/** Output that arrived before `generate` (including its `schema` decode) or a `loop` step failed. Not in the schema, so encoding omits it. */
|
|
129
129
|
partial;
|
|
130
130
|
constructor(props) {
|
|
131
131
|
super({ reason: props.reason });
|
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-
|
|
3
|
+
"version": "0.0.0-dev-433",
|
|
4
4
|
"name": "@opencode/ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -520,7 +520,7 @@
|
|
|
520
520
|
"devDependencies": {
|
|
521
521
|
"@clack/prompts": "1.0.0-alpha.1",
|
|
522
522
|
"@effect/platform-node": "4.1.0",
|
|
523
|
-
"@opencode/http-recorder": "0.0.0-dev-
|
|
523
|
+
"@opencode/http-recorder": "0.0.0-dev-433",
|
|
524
524
|
"@tsconfig/bun": "1.0.9",
|
|
525
525
|
"@types/bun": "1.4.3",
|
|
526
526
|
"@typescript/native": "npm:typescript@7.1.0-beta",
|
|
@@ -530,7 +530,7 @@
|
|
|
530
530
|
"@aws-sdk/credential-providers": "3.1057.0",
|
|
531
531
|
"@smithy/eventstream-codec": "4.2.14",
|
|
532
532
|
"@smithy/util-utf8": "4.2.2",
|
|
533
|
-
"@opencode/schema": "0.0.0-dev-
|
|
533
|
+
"@opencode/schema": "0.0.0-dev-433",
|
|
534
534
|
"aws4fetch": "1.0.20",
|
|
535
535
|
"effect": "4.1.0",
|
|
536
536
|
"google-auth-library": "10.5.0"
|