@opencode/ai 0.0.0-dev-466 → 0.0.0-dev-469
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 +27 -16
- package/dist/cache-policy.d.ts +1 -1
- package/dist/cache-policy.js +2 -2
- package/dist/decision-client.d.ts +3 -3
- package/dist/decision.d.ts +13 -120
- package/dist/decision.js +17 -13
- package/dist/effect.d.ts +8 -6
- package/dist/effect.js +4 -3
- package/dist/effort-updates.d.ts +1 -1
- package/dist/effort-updates.js +2 -2
- package/dist/experimental/cloudflare-run-extraction.d.ts +7 -3
- package/dist/experimental/cloudflare-run-extraction.js +4 -1
- package/dist/experimental/extraction.d.ts +12 -38
- package/dist/experimental/extraction.js +13 -11
- package/dist/experimental.d.ts +8 -8
- package/dist/experimental.js +4 -4
- package/dist/image-client.d.ts +11 -5
- package/dist/image-client.js +1 -1
- package/dist/image.d.ts +21 -62
- package/dist/image.js +16 -13
- package/dist/index.d.ts +20 -14
- package/dist/index.js +6 -3
- package/dist/llm.d.ts +60 -25
- package/dist/llm.js +126 -38
- package/dist/protocols/alibaba-images.d.ts +2 -2
- package/dist/protocols/alibaba-messages.js +3 -3
- package/dist/protocols/anthropic-messages.d.ts +1 -1
- package/dist/protocols/anthropic-messages.js +4 -3
- package/dist/protocols/assemblyai-transcription.d.ts +2 -2
- package/dist/protocols/cartesia-speech.d.ts +2 -2
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +2 -2
- package/dist/protocols/deepgram-speech.d.ts +2 -2
- package/dist/protocols/deepgram-transcription.d.ts +2 -2
- package/dist/protocols/elevenlabs-speech.d.ts +2 -2
- package/dist/protocols/elevenlabs-transcription.d.ts +2 -2
- package/dist/protocols/gemini.js +2 -8
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-speech.d.ts +2 -2
- package/dist/protocols/google-transcription.d.ts +2 -2
- package/dist/protocols/google-video.d.ts +2 -2
- package/dist/protocols/meta-images.d.ts +2 -2
- package/dist/protocols/meta-responses.js +4 -3
- package/dist/protocols/minimax-images.d.ts +2 -2
- package/dist/protocols/open-responses.d.ts +1 -21
- package/dist/protocols/openai-chat.d.ts +1 -21
- package/dist/protocols/openai-chat.js +7 -9
- package/dist/protocols/openai-decisions.js +1 -1
- package/dist/protocols/openai-images.d.ts +2 -2
- package/dist/protocols/openai-responses.js +0 -1
- package/dist/protocols/openai-speech.d.ts +2 -2
- package/dist/protocols/openai-transcription.d.ts +2 -2
- package/dist/protocols/runway-video.d.ts +2 -2
- package/dist/protocols/shared.d.ts +1 -1
- package/dist/protocols/shared.js +2 -2
- package/dist/protocols/system-one.d.ts +4 -4
- package/dist/protocols/utils/responses-checkpoint.js +1 -1
- package/dist/protocols/utils/responses-compaction.d.ts +1 -1
- package/dist/protocols/utils/responses-compaction.js +4 -3
- package/dist/protocols/utils/tool-stream.d.ts +3 -63
- package/dist/protocols/xai-images.d.ts +2 -2
- package/dist/protocols/xai-video.d.ts +2 -2
- package/dist/protocols/zai-images.d.ts +2 -2
- package/dist/protocols/zai-messages.js +3 -3
- package/dist/provider-error.js +4 -4
- package/dist/providers/vercel-ai-gateway.js +4 -3
- package/dist/route/client.d.ts +43 -19
- package/dist/route/client.js +30 -18
- package/dist/route/decision.d.ts +6 -6
- package/dist/schema/errors.d.ts +1 -2
- package/dist/schema/errors.js +1 -2
- package/dist/schema/events.d.ts +61 -769
- package/dist/schema/events.js +3 -30
- package/dist/schema/messages.d.ts +20 -92
- package/dist/schema/messages.js +1 -52
- package/dist/speech-client.d.ts +5 -5
- package/dist/speech.d.ts +17 -19
- package/dist/speech.js +16 -12
- package/dist/testing.d.ts +8 -168
- package/dist/tool-runtime.js +12 -30
- package/dist/transcription-client.d.ts +5 -5
- package/dist/transcription.d.ts +17 -19
- package/dist/transcription.js +16 -12
- package/dist/video-client.d.ts +5 -5
- package/dist/video-client.js +1 -1
- package/dist/video.d.ts +18 -20
- package/dist/video.js +16 -12
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -54,7 +54,9 @@ Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployme
|
|
|
54
54
|
### Tool loop
|
|
55
55
|
|
|
56
56
|
`stream` runs one model call; `loop` runs the model's tool calls and calls the model again until it stops calling
|
|
57
|
-
tools. Events from every step arrive on one stream, with a `tool-result` after each tool runs
|
|
57
|
+
tools. Events from every step arrive on one stream, with a `tool-result` after each tool runs (an error result when
|
|
58
|
+
it fails). Each step ends with its own `step-finish`; one `finish` ends the loop, carrying the final step's reason
|
|
59
|
+
and the usage summed across steps. `until` is checked
|
|
58
60
|
after each step that has tool calls, with every step's `LLMResponse`; returning `true` ends the loop without running
|
|
59
61
|
them. Tools are an array, sent in the order given so the provider can reuse its cached prompt prefix. Each tool has a
|
|
60
62
|
`name`, and two tools with the same name fail the request. `tool` takes a plain `async` execute; a throw or rejection
|
|
@@ -86,6 +88,17 @@ for await (const event of events) {
|
|
|
86
88
|
}
|
|
87
89
|
```
|
|
88
90
|
|
|
91
|
+
The terminal `finish` carries the final assistant `message` and `usage`. To skip the events, `ai.collect` folds a
|
|
92
|
+
stream from `stream` or `loop` into one result: `text` of the final step, `steps` (one `LLMResponse` per call),
|
|
93
|
+
`messages` (each step's assistant message and its tool results, ready to append to the next request), the total
|
|
94
|
+
`usage`, `finishReason`, and `object` with `schema`.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
const result = await ai.collect(ai.loop({ model, prompt: "Summarize package.json.", tools: { read_file } }))
|
|
98
|
+
// Effect: const result = yield* ai.collect(ai.loop({ ... }))
|
|
99
|
+
console.log(result.text, result.usage?.totalTokens)
|
|
100
|
+
```
|
|
101
|
+
|
|
89
102
|
### Freeform tools
|
|
90
103
|
|
|
91
104
|
A tool with exactly one required string parameter can set `freeform`, so the model writes that parameter as raw text
|
|
@@ -118,18 +131,19 @@ const res = await ai.generate({ model, prompt: "Pick a city.", schema: Schema.St
|
|
|
118
131
|
res.object.city
|
|
119
132
|
```
|
|
120
133
|
|
|
121
|
-
`stream` puts `object` on its `finish` event; `loop` runs tools first and puts it on the
|
|
134
|
+
`stream` puts `object` on its `finish` event; `loop` runs tools first and puts it on the loop's `finish`. A
|
|
122
135
|
decode failure is an `AIError` whose `partial` holds the response.
|
|
123
136
|
|
|
124
137
|
### Raw provider events
|
|
125
138
|
|
|
126
139
|
Set `includeRaw: true` on a request to also receive every provider frame as `{ type: "raw", data }`, in order and
|
|
127
140
|
before the events parsed from that frame. `data` is the parsed JSON frame, so frames this package doesn't handle yet
|
|
128
|
-
are visible too. Raw events are off by default, and `generate` ignores them.
|
|
141
|
+
are visible too. Raw events are off by default, and `generate` ignores them. `RawEvent` is the event's type. Act on a
|
|
142
|
+
frame with a stream map:
|
|
129
143
|
|
|
130
144
|
```ts
|
|
131
|
-
import { Stream } from "effect"
|
|
132
|
-
import { ai,
|
|
145
|
+
import { Effect, Stream } from "effect"
|
|
146
|
+
import { ai, AIError, ContentPolicyError } from "@opencode/ai/effect"
|
|
133
147
|
|
|
134
148
|
const flagged = (data: unknown) =>
|
|
135
149
|
typeof data === "object" && data !== null && "type" in data && data.type === "moderation.flagged"
|
|
@@ -137,10 +151,10 @@ const flagged = (data: unknown) =>
|
|
|
137
151
|
const events = ai
|
|
138
152
|
.stream({ model, prompt: "Say hello.", includeRaw: true })
|
|
139
153
|
.pipe(
|
|
140
|
-
Stream.
|
|
154
|
+
Stream.mapEffect((event) =>
|
|
141
155
|
event.type === "raw" && flagged(event.data)
|
|
142
|
-
?
|
|
143
|
-
: event,
|
|
156
|
+
? Effect.fail(new AIError({ reason: new ContentPolicyError({ message: "Flagged by moderation" }) }))
|
|
157
|
+
: Effect.succeed(event),
|
|
144
158
|
),
|
|
145
159
|
)
|
|
146
160
|
```
|
|
@@ -896,9 +910,10 @@ The hosted result is represented as a provider-executed tool call and a tool res
|
|
|
896
910
|
|
|
897
911
|
Everything below is exported from `@opencode/ai/effect` unless noted.
|
|
898
912
|
|
|
899
|
-
- **`ai.request({...})`** — build a provider-neutral `LLMRequest
|
|
913
|
+
- **`ai.request({...})`** — build a provider-neutral `LLMRequest`, a plain object. Accepts ergonomic inputs (`system: string`, `prompt: string`) and normalizes them into `system`, `messages` and `tools` arrays. Edit it with a spread, or pass the edited object back to `ai.request`.
|
|
900
914
|
- **`ai.generate` / `ai.stream`** — run direct input or an `LLMRequest` through `LLMClient` for one-import use.
|
|
901
915
|
- **`ai.loop`** — stream model calls, running `tool(...)` tools between them until the model stops calling tools.
|
|
916
|
+
- **`ai.collect`** — fold a stream from `ai.stream` or `ai.loop` into its final result (`text`, `steps`, `messages`, `usage`).
|
|
902
917
|
- **`Message.user(...)` / `Message.assistant(...)` / `Message.tool(...)`** — message constructors from the canonical schema model.
|
|
903
918
|
- **`LanguageModel.make(...)` / `ToolCallPart.make(...)` / `ToolResultPart.make(...)` / `ToolDefinition.make(...)`** — model and tool-related constructors from the canonical schema model.
|
|
904
919
|
- **`LLMEvent.is.*`** — typed guards (`is.textDelta`, `is.toolCall`, `is.finish`, …) for filtering streams.
|
|
@@ -1273,9 +1288,7 @@ Prefer this operation, where supported, when the application owns compaction pol
|
|
|
1273
1288
|
```ts
|
|
1274
1289
|
const compacted = Effect.gen(function* () {
|
|
1275
1290
|
const result = yield* ai.compact(request)
|
|
1276
|
-
const next =
|
|
1277
|
-
messages: result.replacement,
|
|
1278
|
-
})
|
|
1291
|
+
const next = { ...request, messages: result.replacement }
|
|
1279
1292
|
return yield* ai.generate(next)
|
|
1280
1293
|
})
|
|
1281
1294
|
```
|
|
@@ -1335,7 +1348,7 @@ Enable OpenAI compaction with typed provider options:
|
|
|
1335
1348
|
|
|
1336
1349
|
```ts
|
|
1337
1350
|
import { Effect } from "effect"
|
|
1338
|
-
import { ai, LLMClient,
|
|
1351
|
+
import { ai, LLMClient, Message } from "@opencode/ai/effect"
|
|
1339
1352
|
import { OpenAI } from "@opencode/ai/providers"
|
|
1340
1353
|
|
|
1341
1354
|
const request = ai.request({
|
|
@@ -1347,9 +1360,7 @@ const request = ai.request({
|
|
|
1347
1360
|
})
|
|
1348
1361
|
const continued = Effect.gen(function* () {
|
|
1349
1362
|
const response = yield* LLMClient.generate(request)
|
|
1350
|
-
return
|
|
1351
|
-
messages: [...request.messages, response.message, Message.user("Continue")],
|
|
1352
|
-
})
|
|
1363
|
+
return { ...request, messages: [...request.messages, response.message, Message.user("Continue")] }
|
|
1353
1364
|
})
|
|
1354
1365
|
```
|
|
1355
1366
|
|
package/dist/cache-policy.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LLMRequest } from "./schema/messages.js";
|
|
1
|
+
import { type LLMRequest } from "./schema/messages.js";
|
|
2
2
|
/** The requested cache lifetime, for routes that send it as a request-level option instead of on markers. */
|
|
3
3
|
export declare const requestTTL: (request: LLMRequest) => `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
4
4
|
export declare const applyCachePolicy: (request: LLMRequest) => LLMRequest;
|
package/dist/cache-policy.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// Manual `cache: CacheHint` placements on individual parts are preserved and
|
|
12
12
|
// count against the four-breakpoint budget; auto only fills remaining slots.
|
|
13
13
|
import { CacheHint } from "./schema/options.js";
|
|
14
|
-
import {
|
|
14
|
+
import { Message, ToolDefinition } from "./schema/messages.js";
|
|
15
15
|
import { effortUpdate } from "./effort-updates.js";
|
|
16
16
|
const AUTO = {
|
|
17
17
|
tools: true,
|
|
@@ -163,7 +163,7 @@ export const applyCachePolicy = (request) => {
|
|
|
163
163
|
const messages = policy.messages ? markMessages(request.messages, policy.messages, hint, budget) : request.messages;
|
|
164
164
|
if (tools === request.tools && system === request.system && messages === request.messages)
|
|
165
165
|
return request;
|
|
166
|
-
return
|
|
166
|
+
return { ...request, tools, system, messages };
|
|
167
167
|
};
|
|
168
168
|
/** Whether the cache policy places inline markers, which then carry the requested lifetime. */
|
|
169
169
|
export const placesMarkers = (request) => {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { Context, Effect, Layer } from "effect";
|
|
2
|
-
import type { DecisionOptions, DecisionQuestions,
|
|
2
|
+
import type { DecisionModel, DecisionOptions, DecisionQuestions, DecisionRequest, DecisionResponseFor } from "./decision.js";
|
|
3
3
|
import { RequestExecutor } from "./route/executor.js";
|
|
4
4
|
import { AIError } from "./schema/index.js";
|
|
5
5
|
export type Execute = RequestExecutor.Interface["execute"];
|
|
6
6
|
export interface Interface {
|
|
7
|
-
readonly generate: <Options extends DecisionOptions, const Questions extends DecisionQuestions>(request:
|
|
7
|
+
readonly generate: <Options extends DecisionOptions, const Questions extends DecisionQuestions>(request: DecisionRequest<DecisionModel<Options>, Questions>) => Effect.Effect<DecisionResponseFor<Questions>, AIError>;
|
|
8
8
|
}
|
|
9
9
|
declare const Service_base: Context.ServiceClass<Service, "@opencode/AI/DecisionClient", Interface>;
|
|
10
10
|
export declare class Service extends Service_base {
|
|
11
11
|
}
|
|
12
|
-
export declare const generate: <Options extends DecisionOptions, const Questions extends DecisionQuestions>(request:
|
|
12
|
+
export declare const generate: <Options extends DecisionOptions, const Questions extends DecisionQuestions>(request: DecisionRequest<DecisionModel<Options>, Questions>) => Effect.Effect<DecisionResponseFor<Questions>, AIError, Service>;
|
|
13
13
|
export declare const layer: Layer.Layer<Service, never, RequestExecutor.Service>;
|
|
14
14
|
export declare const fetchLayer: Layer.Layer<RequestExecutor.RequestExecutorService | Service, never, never>;
|
|
15
15
|
export declare const DecisionClient: {
|
package/dist/decision.d.ts
CHANGED
|
@@ -245,7 +245,7 @@ export type DecisionOptions = Record<string, unknown>;
|
|
|
245
245
|
/** What a decision model's route does; `DecisionRoute.make(...).model(...)` builds one from a protocol. */
|
|
246
246
|
export interface DecisionModelRoute<Options extends DecisionOptions = DecisionOptions> {
|
|
247
247
|
readonly id: string;
|
|
248
|
-
generate(request:
|
|
248
|
+
generate(request: DecisionRequest<DecisionModel<Options>>, execute: Execute): Effect.Effect<DecisionResponse, AIError>;
|
|
249
249
|
}
|
|
250
250
|
declare const DecisionModelCompatibility_base: Schema.Class<DecisionModelCompatibility, Schema.Struct<{
|
|
251
251
|
/**
|
|
@@ -290,128 +290,22 @@ export declare namespace DecisionModel {
|
|
|
290
290
|
}
|
|
291
291
|
export declare const DecisionModelSchema: Schema.declare<DecisionModel<DecisionOptions>, DecisionModel<DecisionOptions>>;
|
|
292
292
|
export type DecisionModelOptions<Model> = Model extends DecisionModel<infer Options> ? Options : never;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
readonly
|
|
296
|
-
|
|
297
|
-
readonly text: Schema.String;
|
|
298
|
-
}>, Schema.Struct<{
|
|
299
|
-
readonly type: Schema.Literal<"json">;
|
|
300
|
-
readonly value: Schema.Codec<Schema.Json, Schema.Json, never, never>;
|
|
301
|
-
}>, Schema.Struct<{
|
|
302
|
-
readonly type: Schema.Literal<"media">;
|
|
303
|
-
readonly media: Schema.decodeTo<Schema.declare<Media.Asset, Media.Asset>, Schema.Struct<{
|
|
304
|
-
readonly source: Schema.Union<readonly [Schema.Struct<{
|
|
305
|
-
readonly type: Schema.Literal<"bytes">;
|
|
306
|
-
readonly data: Schema.Uint8Array;
|
|
307
|
-
readonly mediaType: Schema.String;
|
|
308
|
-
}>, Schema.Struct<{
|
|
309
|
-
readonly type: Schema.Literal<"base64">;
|
|
310
|
-
readonly data: Schema.String;
|
|
311
|
-
readonly mediaType: Schema.String;
|
|
312
|
-
}>, Schema.Struct<{
|
|
313
|
-
readonly type: Schema.Literal<"url">;
|
|
314
|
-
readonly url: Schema.String;
|
|
315
|
-
readonly mediaType: Schema.optional<Schema.String>;
|
|
316
|
-
readonly expiresAt: Schema.optional<Schema.Number>;
|
|
317
|
-
}>, Schema.Struct<{
|
|
318
|
-
readonly type: Schema.Literal<"ref">;
|
|
319
|
-
readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
|
|
320
|
-
readonly id: Schema.String;
|
|
321
|
-
readonly mediaType: Schema.optional<Schema.String>;
|
|
322
|
-
}>]>;
|
|
323
|
-
readonly info: Schema.optional<Schema.Struct<{
|
|
324
|
-
readonly width: Schema.optional<Schema.Number>;
|
|
325
|
-
readonly height: Schema.optional<Schema.Number>;
|
|
326
|
-
readonly durationSeconds: Schema.optional<Schema.Number>;
|
|
327
|
-
readonly sampleRate: Schema.optional<Schema.Number>;
|
|
328
|
-
readonly channels: Schema.optional<Schema.Number>;
|
|
329
|
-
readonly encoding: Schema.optional<Schema.String>;
|
|
330
|
-
readonly format: Schema.optional<Schema.String>;
|
|
331
|
-
}>>;
|
|
332
|
-
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
333
|
-
}>, never, never>;
|
|
334
|
-
readonly filename: Schema.optional<Schema.String>;
|
|
335
|
-
}>]>>;
|
|
336
|
-
readonly questions: Schema.refine<readonly ({
|
|
337
|
-
readonly name: string;
|
|
338
|
-
readonly type: "predicate";
|
|
339
|
-
readonly instructions: string;
|
|
340
|
-
} | {
|
|
341
|
-
readonly name: string;
|
|
342
|
-
readonly type: "choice";
|
|
343
|
-
readonly instructions: string;
|
|
344
|
-
readonly choices: readonly {
|
|
345
|
-
readonly value: string | boolean;
|
|
346
|
-
readonly description?: string | undefined;
|
|
347
|
-
}[];
|
|
348
|
-
} | {
|
|
349
|
-
readonly name: string;
|
|
350
|
-
readonly type: "score";
|
|
351
|
-
readonly instructions: string;
|
|
352
|
-
readonly levels: readonly {
|
|
353
|
-
readonly label: string;
|
|
354
|
-
readonly description?: string | undefined;
|
|
355
|
-
}[];
|
|
356
|
-
})[], Schema.refine<readonly ({
|
|
357
|
-
readonly name: string;
|
|
358
|
-
readonly type: "predicate";
|
|
359
|
-
readonly instructions: string;
|
|
360
|
-
} | {
|
|
361
|
-
readonly name: string;
|
|
362
|
-
readonly type: "choice";
|
|
363
|
-
readonly instructions: string;
|
|
364
|
-
readonly choices: readonly {
|
|
365
|
-
readonly value: string | boolean;
|
|
366
|
-
readonly description?: string | undefined;
|
|
367
|
-
}[];
|
|
368
|
-
} | {
|
|
369
|
-
readonly name: string;
|
|
370
|
-
readonly type: "score";
|
|
371
|
-
readonly instructions: string;
|
|
372
|
-
readonly levels: readonly {
|
|
373
|
-
readonly label: string;
|
|
374
|
-
readonly description?: string | undefined;
|
|
375
|
-
}[];
|
|
376
|
-
})[], Schema.$Array<Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
|
|
377
|
-
readonly name: Schema.String;
|
|
378
|
-
readonly type: Schema.Literal<"predicate">;
|
|
379
|
-
readonly instructions: Schema.String;
|
|
380
|
-
}>, Schema.Struct<{
|
|
381
|
-
readonly name: Schema.String;
|
|
382
|
-
readonly type: Schema.Literal<"choice">;
|
|
383
|
-
readonly instructions: Schema.String;
|
|
384
|
-
readonly choices: Schema.$Array<Schema.Struct<{
|
|
385
|
-
readonly value: Schema.Union<readonly [Schema.String, Schema.Boolean]>;
|
|
386
|
-
readonly description: Schema.optional<Schema.String>;
|
|
387
|
-
}>>;
|
|
388
|
-
}>, Schema.Struct<{
|
|
389
|
-
readonly name: Schema.String;
|
|
390
|
-
readonly type: Schema.Literal<"score">;
|
|
391
|
-
readonly instructions: Schema.String;
|
|
392
|
-
readonly levels: Schema.$Array<Schema.Struct<{
|
|
393
|
-
readonly label: Schema.String;
|
|
394
|
-
readonly description: Schema.optional<Schema.String>;
|
|
395
|
-
}>>;
|
|
396
|
-
}>]>>>>;
|
|
397
|
-
readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
398
|
-
readonly http: Schema.optional<typeof HttpOptions>;
|
|
399
|
-
}>, {}>;
|
|
400
|
-
export declare class DecisionRequest extends DecisionRequest_base {
|
|
401
|
-
protected readonly _DecisionRequest: void;
|
|
402
|
-
}
|
|
403
|
-
export type DecisionRequestFor<Options extends DecisionOptions = DecisionOptions, Questions extends DecisionQuestions = DecisionQuestions> = Omit<DecisionRequest, "model" | "questions" | "providerOptions"> & {
|
|
404
|
-
readonly model: DecisionModel<Options>;
|
|
293
|
+
/** A normalized decision request, the plain object `ai.decision.request(...)` returns. */
|
|
294
|
+
export interface DecisionRequest<Model extends DecisionModel = DecisionModel, Questions extends DecisionQuestions = DecisionQuestions> {
|
|
295
|
+
readonly model: Model;
|
|
296
|
+
readonly input: ReadonlyArray<DecisionPart>;
|
|
405
297
|
readonly questions: Questions;
|
|
406
|
-
readonly providerOptions?:
|
|
407
|
-
|
|
408
|
-
|
|
298
|
+
readonly providerOptions?: DecisionModelOptions<Model>;
|
|
299
|
+
readonly http?: HttpOptions;
|
|
300
|
+
}
|
|
301
|
+
/** Input accepted by `ai.decision.request` and `generate`. A `DecisionRequest` is also a valid input. */
|
|
302
|
+
export interface DecisionRequestInput<Model extends DecisionModel = DecisionModel, Questions extends DecisionQuestions = DecisionQuestions> {
|
|
409
303
|
readonly model: Model;
|
|
410
304
|
readonly input: DecisionInput;
|
|
411
305
|
readonly questions: Questions;
|
|
412
306
|
readonly providerOptions?: NoInfer<DecisionModelOptions<Model>>;
|
|
413
307
|
readonly http?: HttpOptions.Input;
|
|
414
|
-
}
|
|
308
|
+
}
|
|
415
309
|
declare const DecisionUsage_base: Schema.Class<DecisionUsage, Schema.Struct<{
|
|
416
310
|
readonly inputTokens: Schema.optional<Schema.Number>;
|
|
417
311
|
readonly outputTokens: Schema.optional<Schema.Number>;
|
|
@@ -460,9 +354,8 @@ export declare class DecisionResponse extends DecisionResponse_base {
|
|
|
460
354
|
export type DecisionResponseFor<Questions extends DecisionQuestions> = Omit<DecisionResponse, "answers"> & {
|
|
461
355
|
readonly answers: AnswersFor<Questions>;
|
|
462
356
|
};
|
|
463
|
-
|
|
464
|
-
export declare
|
|
465
|
-
export declare function request(input: DecisionRequest | DecisionRequestInput): DecisionRequest;
|
|
357
|
+
/** Normalize decision request input into a plain `DecisionRequest`; a plain object `input` becomes one `json` part. */
|
|
358
|
+
export declare const request: <const Model extends DecisionModel, const Questions extends DecisionQuestions>(input: DecisionRequestInput<Model, Questions>) => DecisionRequest<Model, Questions>;
|
|
466
359
|
export declare function generate<const Model extends DecisionModel, const Questions extends DecisionQuestions>(input: DecisionRequestInput<Model, Questions>): Effect.Effect<DecisionResponseFor<Questions>, AIError, Service>;
|
|
467
360
|
export declare const Decision: {
|
|
468
361
|
readonly request: typeof request;
|
package/dist/decision.js
CHANGED
|
@@ -117,14 +117,13 @@ export const DecisionModelSchema = Schema.declare((value) => value instanceof De
|
|
|
117
117
|
// ---------------------------------------------------------------------------
|
|
118
118
|
// Request and response
|
|
119
119
|
// ---------------------------------------------------------------------------
|
|
120
|
-
|
|
120
|
+
const DecisionRequestSchema = Schema.Struct({
|
|
121
121
|
model: DecisionModelSchema,
|
|
122
122
|
input: Schema.Array(DecisionPart),
|
|
123
123
|
questions: DecisionQuestionList,
|
|
124
124
|
providerOptions: Schema.optional(ProviderOptions),
|
|
125
125
|
http: Schema.optional(HttpOptions),
|
|
126
|
-
})
|
|
127
|
-
}
|
|
126
|
+
});
|
|
128
127
|
/** What decision hosts report; their raw usage stays in the response's `providerMetadata`. */
|
|
129
128
|
export class DecisionUsage extends Schema.Class("Decision.Usage")({
|
|
130
129
|
inputTokens: Schema.optional(Schema.Number),
|
|
@@ -140,17 +139,22 @@ export class DecisionResponse extends Schema.Class("Decision.Response")({
|
|
|
140
139
|
providerMetadata: Schema.optional(ProviderMetadata),
|
|
141
140
|
}) {
|
|
142
141
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
// Call API
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
/** Normalize decision request input into a plain `DecisionRequest`; a plain object `input` becomes one `json` part. */
|
|
146
|
+
export const request = (input) => ({
|
|
147
|
+
...input,
|
|
148
|
+
input: Array.isArray(input.input) ? input.input : [{ type: "json", value: input.input }],
|
|
149
|
+
http: HttpOptions.make(input.http),
|
|
150
|
+
});
|
|
151
|
+
const validate = Schema.decodeUnknownSync(DecisionRequestSchema);
|
|
152
152
|
export function generate(input) {
|
|
153
|
-
return tryRequest(() =>
|
|
153
|
+
return tryRequest(() => {
|
|
154
|
+
const normalized = request(input);
|
|
155
|
+
validate(normalized);
|
|
156
|
+
return normalized;
|
|
157
|
+
}).pipe(Effect.flatMap(DecisionClient.generate));
|
|
154
158
|
}
|
|
155
159
|
export const Decision = {
|
|
156
160
|
request,
|
package/dist/effect.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ 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 collect: typeof LLM.collect;
|
|
8
9
|
readonly compact: typeof LLM.compact;
|
|
9
10
|
readonly canCompact: typeof LLM.canCompact;
|
|
10
11
|
readonly image: {
|
|
@@ -48,13 +49,13 @@ export { Auth } from "./route/auth.js";
|
|
|
48
49
|
export { ProviderPackage } from "./provider-package.js";
|
|
49
50
|
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
50
51
|
export type { LLMClientService } from "./route/client.js";
|
|
51
|
-
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice,
|
|
52
|
-
export type { GenerationOptionsFields, GenerationOptionsInput, LanguageModelInput, LanguageModelProviderOptions, ToolDefinitionInput, ToolNamespaceInput, ToolNamespaceEntryInput, ToolEntryInput, UsageInput, } from "./schema/index.js";
|
|
53
|
-
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema,
|
|
54
|
-
export type { ImageModelOptions, ImageOptions,
|
|
52
|
+
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, FinishReasonDetails, StepFinish, Finish, MediaEvent, RawEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
|
|
53
|
+
export type { GenerationOptionsFields, GenerationOptionsInput, LanguageModelInput, LanguageModelProviderOptions, LLMRequest, ToolDefinitionInput, ToolNamespaceInput, ToolNamespaceEntryInput, ToolEntryInput, UsageInput, } from "./schema/index.js";
|
|
54
|
+
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageResponse } from "./image.js";
|
|
55
|
+
export type { ImageModelOptions, ImageOptions, ImageRequest, ImageRequestInput, ImageResolution, ImageRoute, } from "./image.js";
|
|
55
56
|
export { DecisionClient } from "./decision-client.js";
|
|
56
|
-
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion,
|
|
57
|
-
export type { AnswerFor, AnswersFor, DecisionInput, DecisionModelOptions, DecisionOptions, DecisionQuestions,
|
|
57
|
+
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
|
|
58
|
+
export type { AnswerFor, AnswersFor, DecisionInput, DecisionModelOptions, DecisionOptions, DecisionQuestions, DecisionRequest, DecisionRequestInput, DecisionResponseFor, DecisionModelRoute, } from "./decision.js";
|
|
58
59
|
export { DecisionRoute } from "./route/decision.js";
|
|
59
60
|
export { Media } from "./media.js";
|
|
60
61
|
export { Generation } from "./generation.js";
|
|
@@ -64,3 +65,4 @@ export { ToolRuntime } from "./tool-runtime.js";
|
|
|
64
65
|
export type { ToolSettlement } from "./tool-runtime.js";
|
|
65
66
|
export type { ExecutableTool, Tool, ToolExecute, ToolExecuteContext, ToolInput, ToolInputType, ToolModelOutputInput, ToolOptions, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
66
67
|
export type { LLM } from "./llm.js";
|
|
68
|
+
export type { LLMLoopInput, LLMRequestInput } from "./llm.js";
|
package/dist/effect.js
CHANGED
|
@@ -10,6 +10,7 @@ export const ai = {
|
|
|
10
10
|
generate: LLM.generate,
|
|
11
11
|
stream: LLM.stream,
|
|
12
12
|
loop: LLM.loop,
|
|
13
|
+
collect: LLM.collect,
|
|
13
14
|
compact: LLM.compact,
|
|
14
15
|
canCompact: LLM.canCompact,
|
|
15
16
|
image: Image,
|
|
@@ -27,10 +28,10 @@ export { ImageClient } from "./image-client.js";
|
|
|
27
28
|
export { Auth } from "./route/auth.js";
|
|
28
29
|
export { ProviderPackage } from "./provider-package.js";
|
|
29
30
|
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
30
|
-
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice,
|
|
31
|
-
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema,
|
|
31
|
+
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, FinishReasonDetails, StepFinish, Finish, MediaEvent, RawEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
|
|
32
|
+
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageResponse } from "./image.js";
|
|
32
33
|
export { DecisionClient } from "./decision-client.js";
|
|
33
|
-
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion,
|
|
34
|
+
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
|
|
34
35
|
export { DecisionRoute } from "./route/decision.js";
|
|
35
36
|
export { Media } from "./media.js";
|
|
36
37
|
export { Generation } from "./generation.js";
|
package/dist/effort-updates.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LLMRequest, type EffortPart, type Message } from "./schema/messages.js";
|
|
1
|
+
import { type LLMRequest, type EffortPart, type Message } from "./schema/messages.js";
|
|
2
2
|
export declare const effortUpdate: (message: Message) => EffortPart | undefined;
|
|
3
3
|
export declare const stripEffortUpdates: (request: LLMRequest) => LLMRequest<import("./effect.js").LanguageModel<{
|
|
4
4
|
readonly [x: string]: unknown;
|
package/dist/effort-updates.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Changing the top-level reasoning effort invalidates the provider prompt cache. Protocols with a native
|
|
2
2
|
// per-message update keep it frozen and lower `Message.effort(...)` markers instead; other routes strip them.
|
|
3
|
-
import {
|
|
3
|
+
import {} from "./schema/messages.js";
|
|
4
4
|
export const effortUpdate = (message) => {
|
|
5
5
|
if (message.role !== "system" || message.content.length !== 1)
|
|
6
6
|
return undefined;
|
|
@@ -9,7 +9,7 @@ export const effortUpdate = (message) => {
|
|
|
9
9
|
};
|
|
10
10
|
export const stripEffortUpdates = (request) => {
|
|
11
11
|
const messages = request.messages.filter((message) => effortUpdate(message) === undefined);
|
|
12
|
-
return messages.length === request.messages.length ? request :
|
|
12
|
+
return messages.length === request.messages.length ? request : { ...request, messages };
|
|
13
13
|
};
|
|
14
14
|
export const applyEffortUpdates = (request) => request.model.route.supportsEffortUpdates?.(request) ? request : stripEffortUpdates(request);
|
|
15
15
|
// Reverted or forked history can leave the last marker disagreeing with the requested effort.
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { ExtractionModel, ExtractionResponse, type
|
|
1
|
+
import { ExtractionModel, ExtractionResponse, type ExtractionRequest } from "./extraction.js";
|
|
2
2
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
3
|
import { MediaRoute } from "../route/media.js";
|
|
4
4
|
/** Posts Clairic's `{ model, state, fields }` body to Cloudflare's `/ai/run/{model}`. */
|
|
5
|
-
export declare const protocol: MediaProtocol.Inline<
|
|
5
|
+
export declare const protocol: MediaProtocol.Inline<ExtractionRequest<ExtractionModel<{
|
|
6
|
+
readonly [x: string]: unknown;
|
|
7
|
+
}>, Readonly<Record<string, import("./extraction.js").ExtractionField>>>, ExtractionResponse>;
|
|
6
8
|
/** `baseURL` is the Run API base, `.../client/v4/accounts/{account}/ai/run`. */
|
|
7
9
|
export declare const model: (input: MediaRoute.ModelInput) => ExtractionModel<{
|
|
8
10
|
readonly [x: string]: unknown;
|
|
9
11
|
}>;
|
|
10
12
|
export declare const CloudflareRunExtraction: {
|
|
11
|
-
readonly protocol: MediaProtocol.Inline<
|
|
13
|
+
readonly protocol: MediaProtocol.Inline<ExtractionRequest<ExtractionModel<{
|
|
14
|
+
readonly [x: string]: unknown;
|
|
15
|
+
}>, Readonly<Record<string, import("./extraction.js").ExtractionField>>>, ExtractionResponse>;
|
|
12
16
|
readonly model: typeof model;
|
|
13
17
|
};
|
|
@@ -16,7 +16,10 @@ const decodeEnvelope = route.decodeJson(Schema.Struct({
|
|
|
16
16
|
answers: Schema.Record(Schema.String, JsonObject),
|
|
17
17
|
truncated: Schema.optional(Schema.Boolean),
|
|
18
18
|
state_text: Schema.optional(Schema.String),
|
|
19
|
-
usage: Schema.optional(Schema.StructWithRest(Schema.Struct({
|
|
19
|
+
usage: Schema.optional(Schema.StructWithRest(Schema.Struct({
|
|
20
|
+
input_tokens: Schema.optional(Schema.Number),
|
|
21
|
+
output_tokens: Schema.optional(Schema.Number),
|
|
22
|
+
}), [JsonObject])),
|
|
20
23
|
}),
|
|
21
24
|
}));
|
|
22
25
|
/** Posts Clairic's `{ model, state, fields }` body to Cloudflare's `/ai/run/{model}`. */
|
|
@@ -116,48 +116,24 @@ export type ExtractionField = typeof ExtractionFieldSchema.Type & {
|
|
|
116
116
|
};
|
|
117
117
|
export type ExtractionFields = Readonly<Record<string, ExtractionField>>;
|
|
118
118
|
export type ExtractionOptions = MediaModel.Options;
|
|
119
|
-
export type ExtractionRoute = MediaRoute.InlineRoute<
|
|
119
|
+
export type ExtractionRoute = MediaRoute.InlineRoute<ExtractionRequest, ExtractionResponse>;
|
|
120
120
|
export declare class ExtractionModel<Options extends ExtractionOptions = ExtractionOptions> extends MediaModel<ExtractionRoute, Options> {
|
|
121
121
|
protected readonly _ExtractionModel: void;
|
|
122
122
|
}
|
|
123
123
|
export type ExtractionModelOptions<Model> = Model extends ExtractionModel<infer Options> ? Options : never;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
readonly [x: string]: unknown;
|
|
127
|
-
}>, unknown>;
|
|
128
|
-
/** The text to read; text answers carry spans into it. */
|
|
129
|
-
readonly input: Schema.String;
|
|
130
|
-
readonly fields: Schema.refine<{
|
|
131
|
-
readonly [x: string]: {
|
|
132
|
-
readonly [x: string]: unknown;
|
|
133
|
-
readonly type: string;
|
|
134
|
-
readonly instructions?: string | undefined;
|
|
135
|
-
readonly nullThreshold?: number | undefined;
|
|
136
|
-
};
|
|
137
|
-
}, Schema.$Record<Schema.String, Schema.StructWithRest<Schema.Struct<{
|
|
138
|
-
readonly type: Schema.String;
|
|
139
|
-
readonly instructions: Schema.optional<Schema.String>;
|
|
140
|
-
/** Probability of null above which the value is null; the host's default is 0.5. */
|
|
141
|
-
readonly nullThreshold: Schema.optional<Schema.Number>;
|
|
142
|
-
}>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
|
|
143
|
-
/** ISO date that anchors dates written without a year. */
|
|
144
|
-
readonly referenceDate: Schema.optional<Schema.String>;
|
|
145
|
-
readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
146
|
-
readonly http: Schema.optional<typeof HttpOptions>;
|
|
147
|
-
}>, {}>;
|
|
148
|
-
export declare class ExtractionRequest extends ExtractionRequest_base {
|
|
149
|
-
protected readonly _ExtractionRequest: void;
|
|
150
|
-
}
|
|
151
|
-
export type ExtractionRequestFor<Options extends ExtractionOptions = ExtractionOptions, Fields extends ExtractionFields = ExtractionFields> = Omit<ExtractionRequest, "model" | "fields" | "providerOptions"> & {
|
|
152
|
-
readonly model: ExtractionModel<Options>;
|
|
153
|
-
readonly fields: Fields;
|
|
154
|
-
readonly providerOptions?: Options;
|
|
155
|
-
};
|
|
156
|
-
export type ExtractionRequestInput<Model extends ExtractionModel = ExtractionModel, Fields extends ExtractionFields = ExtractionFields> = {
|
|
124
|
+
/** A normalized extraction request, the plain object `Extraction.request(...)` returns. */
|
|
125
|
+
export interface ExtractionRequest<Model extends ExtractionModel = ExtractionModel, Fields extends ExtractionFields = ExtractionFields> {
|
|
157
126
|
readonly model: Model;
|
|
127
|
+
/** The text to read; text answers carry spans into it. */
|
|
158
128
|
readonly input: string;
|
|
159
129
|
readonly fields: Fields;
|
|
130
|
+
/** ISO date that anchors dates written without a year. */
|
|
160
131
|
readonly referenceDate?: string;
|
|
132
|
+
readonly providerOptions?: ExtractionModelOptions<Model>;
|
|
133
|
+
readonly http?: HttpOptions;
|
|
134
|
+
}
|
|
135
|
+
/** Input accepted by `Extraction.request` and `generate`; an `ExtractionRequest` is also a valid input. */
|
|
136
|
+
export type ExtractionRequestInput<Model extends ExtractionModel = ExtractionModel, Fields extends ExtractionFields = ExtractionFields> = Omit<ExtractionRequest<Model, Fields>, "providerOptions" | "http"> & {
|
|
161
137
|
readonly providerOptions?: NoInfer<ExtractionModelOptions<Model>>;
|
|
162
138
|
readonly http?: HttpOptions.Input;
|
|
163
139
|
};
|
|
@@ -171,11 +147,9 @@ export declare class ExtractionResponse extends ExtractionResponse_base {
|
|
|
171
147
|
export type ExtractionResponseFor<Fields extends ExtractionFields> = Omit<ExtractionResponse, "answers"> & {
|
|
172
148
|
readonly answers: ExtractionAnswersFor<Fields>;
|
|
173
149
|
};
|
|
174
|
-
|
|
175
|
-
export declare
|
|
176
|
-
export declare function request(input: ExtractionRequest | ExtractionRequestInput): ExtractionRequest;
|
|
150
|
+
/** Normalize extraction request input into a plain `ExtractionRequest`. */
|
|
151
|
+
export declare const request: <const Model extends ExtractionModel, const Fields extends ExtractionFields>(input: ExtractionRequestInput<Model, Fields>) => ExtractionRequest<Model, Fields>;
|
|
177
152
|
export declare function generate<const Model extends ExtractionModel, const Fields extends ExtractionFields>(input: ExtractionRequestInput<Model, Fields>): Effect.Effect<ExtractionResponseFor<Fields>, AIError, RequestExecutor.Service>;
|
|
178
|
-
export declare function generate(input: ExtractionRequest): Effect.Effect<ExtractionResponse, AIError, RequestExecutor.Service>;
|
|
179
153
|
export declare const Extraction: {
|
|
180
154
|
readonly request: typeof request;
|
|
181
155
|
readonly generate: typeof generate;
|
|
@@ -65,34 +65,36 @@ export class ExtractionModel extends MediaModel {
|
|
|
65
65
|
// ---------------------------------------------------------------------------
|
|
66
66
|
// Request and response
|
|
67
67
|
// ---------------------------------------------------------------------------
|
|
68
|
-
|
|
68
|
+
const ExtractionRequestSchema = Schema.Struct({
|
|
69
69
|
model: Schema.declare((value) => value instanceof ExtractionModel, {
|
|
70
70
|
expected: "Extraction.Model",
|
|
71
71
|
}),
|
|
72
|
-
/** The text to read; text answers carry spans into it. */
|
|
73
72
|
input: Schema.String,
|
|
74
73
|
fields: Schema.Record(Schema.String, ExtractionFieldSchema).pipe(Schema.refine((x) => Object.keys(x).length > 0, { message: "Extraction fields must not be empty" })),
|
|
75
|
-
/** ISO date that anchors dates written without a year. */
|
|
76
74
|
referenceDate: Schema.optional(Schema.String),
|
|
77
75
|
providerOptions: Schema.optional(ProviderOptions),
|
|
78
76
|
http: Schema.optional(HttpOptions),
|
|
79
|
-
})
|
|
80
|
-
}
|
|
77
|
+
});
|
|
81
78
|
export class ExtractionResponse extends Schema.Class("Extraction.Response")({
|
|
82
79
|
answers: Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Unknown)),
|
|
83
80
|
usage: Schema.optional(Usage),
|
|
84
81
|
providerMetadata: Schema.optional(ProviderMetadata),
|
|
85
82
|
}) {
|
|
86
83
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// Call API
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
/** Normalize extraction request input into a plain `ExtractionRequest`. */
|
|
88
|
+
export const request = (input) => ({ ...input, http: HttpOptions.make(input.http) });
|
|
89
|
+
const validate = Schema.decodeUnknownSync(ExtractionRequestSchema);
|
|
92
90
|
export function generate(input) {
|
|
93
91
|
return Effect.gen(function* () {
|
|
94
92
|
const executor = yield* RequestExecutor.Service;
|
|
95
|
-
const built = yield* tryRequest(() =>
|
|
93
|
+
const built = yield* tryRequest(() => {
|
|
94
|
+
const normalized = request(input);
|
|
95
|
+
validate(normalized);
|
|
96
|
+
return normalized;
|
|
97
|
+
});
|
|
96
98
|
return yield* built.model.route.generate(built, executor.execute);
|
|
97
99
|
});
|
|
98
100
|
}
|