@opencode/ai 0.0.0-dev-291 → 0.0.0-dev-329
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 +6 -6
- package/dist/decision.d.ts +0 -1
- package/dist/effect.d.ts +2 -2
- package/dist/effect.js +1 -1
- package/dist/index.d.ts +17 -20
- package/dist/index.js +20 -22
- package/dist/llm.d.ts +2 -2
- package/dist/llm.js +3 -12
- package/dist/protocols/alibaba-responses.d.ts +2 -2
- package/dist/protocols/assemblyai-transcription.js +3 -3
- package/dist/protocols/meta-responses.d.ts +4 -4
- package/dist/protocols/open-responses.d.ts +18 -18
- package/dist/protocols/open-responses.js +4 -2
- package/dist/protocols/openai-compatible-responses.d.ts +2 -2
- package/dist/protocols/openai-responses.d.ts +12 -12
- package/dist/protocols/runway-video.js +2 -2
- package/dist/protocols/xai-responses.d.ts +2 -2
- package/dist/protocols/xai-video.js +2 -2
- package/dist/providers/alibaba.d.ts +2 -2
- package/dist/providers/amazon-bedrock-mantle.d.ts +2 -2
- package/dist/providers/azure.d.ts +4 -4
- package/dist/providers/cloudflare-ai-gateway.d.ts +4 -4
- package/dist/providers/digitalocean.d.ts +2 -2
- package/dist/providers/google-vertex-responses.d.ts +2 -2
- package/dist/providers/meta.d.ts +2 -2
- package/dist/providers/minimax.d.ts +2 -2
- package/dist/providers/moonshot.d.ts +2 -2
- package/dist/providers/openai-compatible-responses.d.ts +2 -2
- package/dist/providers/openai.d.ts +2 -2
- package/dist/providers/openrouter/responses.d.ts +1 -2
- package/dist/providers/openrouter.d.ts +36 -23
- package/dist/providers/openrouter.js +29 -10
- package/dist/providers/venice.d.ts +2 -2
- package/dist/providers/zai-coding-plan.d.ts +2 -2
- package/dist/route/media-protocol.d.ts +2 -3
- package/dist/route/media-protocol.js +2 -6
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +1 -1
- package/dist/tool.d.ts +38 -71
- package/dist/tool.js +27 -18
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -55,16 +55,16 @@ Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployme
|
|
|
55
55
|
`stream` runs one model call; `loop` runs the model's tool calls and calls the model again until it stops calling
|
|
56
56
|
tools. Events from every step arrive on one stream, with a `tool-result` after each tool runs. `until` is checked
|
|
57
57
|
after each step that has tool calls, with every step's `LLMResponse`; returning `true` ends the loop without running
|
|
58
|
-
them. `
|
|
58
|
+
them. `tool` takes a plain `async` execute; a throw or rejection goes back to the model as an error result, and
|
|
59
59
|
`signal` aborts when the loop is interrupted.
|
|
60
|
-
`
|
|
60
|
+
`tool` from `@opencode/ai/effect` takes the same options (including an optional `success` schema) with an Effect execute:
|
|
61
61
|
|
|
62
62
|
```ts
|
|
63
63
|
import { readFile } from "node:fs/promises"
|
|
64
64
|
import { Schema } from "effect"
|
|
65
|
-
import {
|
|
65
|
+
import { tool } from "@opencode/ai"
|
|
66
66
|
|
|
67
|
-
const read_file =
|
|
67
|
+
const read_file = tool({
|
|
68
68
|
description: "Read a UTF-8 file.",
|
|
69
69
|
parameters: Schema.Struct({ path: Schema.String }),
|
|
70
70
|
execute: ({ path }, { signal }) => readFile(path, { encoding: "utf8", signal }),
|
|
@@ -87,7 +87,7 @@ A tool with exactly one required string parameter can set `freeform`, so the mod
|
|
|
87
87
|
instead of JSON arguments: unconstrained `text`, or text matching a `regex` or `lark` grammar.
|
|
88
88
|
|
|
89
89
|
```ts
|
|
90
|
-
const run_code =
|
|
90
|
+
const run_code = tool({
|
|
91
91
|
description: "Run JavaScript",
|
|
92
92
|
parameters: Schema.Struct({ code: Schema.String }),
|
|
93
93
|
freeform: { type: "lark", grammar: JS_GRAMMAR },
|
|
@@ -1158,7 +1158,7 @@ Everything below is exported from `@opencode/ai/effect` unless noted.
|
|
|
1158
1158
|
|
|
1159
1159
|
- **`LLM.request({...})`** — build a provider-neutral `LLMRequest`. Accepts ergonomic inputs (`system: string`, `prompt: string`) that normalize into the canonical Schema classes.
|
|
1160
1160
|
- **`LLM.generate` / `LLM.stream`** — run direct input or an `LLMRequest` through `LLMClient` for one-import use.
|
|
1161
|
-
- **`LLM.loop`** — stream model calls, running `
|
|
1161
|
+
- **`LLM.loop`** — stream model calls, running `tool(...)` tools between them until the model stops calling tools.
|
|
1162
1162
|
- **`Message.user(...)` / `Message.assistant(...)` / `Message.tool(...)`** — message constructors from the canonical schema model.
|
|
1163
1163
|
- **`LanguageModel.make(...)` / `ToolCallPart.make(...)` / `ToolResultPart.make(...)` / `ToolDefinition.make(...)`** — model and tool-related constructors from the canonical schema model.
|
|
1164
1164
|
- **`LLMEvent.is.*`** — typed guards (`is.textDelta`, `is.toolCall`, `is.finish`, …) for filtering streams.
|
package/dist/decision.d.ts
CHANGED
|
@@ -464,7 +464,6 @@ export declare function request<const Model extends DecisionModel, const Questio
|
|
|
464
464
|
export declare function request(input: DecisionRequest): DecisionRequest;
|
|
465
465
|
export declare function request(input: DecisionRequest | DecisionRequestInput): DecisionRequest;
|
|
466
466
|
export declare function generate<const Model extends DecisionModel, const Questions extends DecisionQuestions>(input: DecisionRequestInput<Model, Questions>): Effect.Effect<DecisionResponseFor<Questions>, AIError, Service>;
|
|
467
|
-
export declare function generate(input: DecisionRequest): Effect.Effect<DecisionResponse, AIError, Service>;
|
|
468
467
|
export declare const Decision: {
|
|
469
468
|
readonly request: typeof request;
|
|
470
469
|
readonly generate: typeof generate;
|
package/dist/effect.d.ts
CHANGED
|
@@ -30,9 +30,9 @@ export { DecisionRoute } from "./route/decision.js";
|
|
|
30
30
|
export { Media } from "./media.js";
|
|
31
31
|
export { Generation } from "./generation.js";
|
|
32
32
|
export type { AwaitOptions as GenerationAwaitOptions, Event as GenerationEvent, Poll, Route as GenerationRoute, Snapshot as GenerationSnapshot, Status as GenerationStatus, } from "./generation.js";
|
|
33
|
-
export {
|
|
33
|
+
export { ToolFailure, emptyInputJsonSchema, toDefinitions, tool } from "./tool.js";
|
|
34
34
|
export { ToolRuntime } from "./tool-runtime.js";
|
|
35
35
|
export type { DispatchResult as ToolDispatchResult, ToolSettlement } from "./tool-runtime.js";
|
|
36
|
-
export type {
|
|
36
|
+
export type { ExecutableTool, Tool, ToolExecute, ToolExecuteContext, ToolModelOutputInput, ToolOptions, Tools, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
37
37
|
export * as LLM from "./llm.js";
|
|
38
38
|
export type { Definition as ProviderPackageDefinition, Settings as ProviderPackageSettings, } from "./provider-package.js";
|
package/dist/effect.js
CHANGED
|
@@ -22,6 +22,6 @@ export { Decision } from "./decision.js";
|
|
|
22
22
|
export { DecisionRoute } from "./route/decision.js";
|
|
23
23
|
export { Media } from "./media.js";
|
|
24
24
|
export { Generation } from "./generation.js";
|
|
25
|
-
export {
|
|
25
|
+
export { ToolFailure, emptyInputJsonSchema, toDefinitions, tool } from "./tool.js";
|
|
26
26
|
export { ToolRuntime } from "./tool-runtime.js";
|
|
27
27
|
export * as LLM from "./llm.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -5,13 +5,13 @@ import type { AwaitOptions, Event, Snapshot } from "./generation.js";
|
|
|
5
5
|
import { type ImageModel, type ImageRequest, type ImageRequestInput } from "./image.js";
|
|
6
6
|
import { LLM } from "./effect.js";
|
|
7
7
|
import { Media } from "./media.js";
|
|
8
|
-
import { type CheckpointRequest, type CompactionRequest, type EndpointCompactOptions, type TriggerCompactOptions } from "./route/client.js";
|
|
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
10
|
import { LanguageModel, type CompactionCheckpointResponse, type CompactionResponse, type LLMRequest } from "./schema/index.js";
|
|
11
11
|
import type { GenerateObjectDynamicOptions, GenerateObjectOptions, GenerateObjectResponse, LoopInput, 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
|
-
import {
|
|
14
|
+
import type { NoExecute, ToolExecuteContext, ToolOptions, ToolSchema } from "./tool.js";
|
|
15
15
|
import { type VideoModel, type VideoRequest, type VideoRequestInput } from "./video.js";
|
|
16
16
|
/**
|
|
17
17
|
* Promise-first entrypoint for scripts and non-Effect callers. One `ManagedRuntime` hosts the LLM, image, video, speech,
|
|
@@ -33,6 +33,8 @@ export type Services = AIClient.Services;
|
|
|
33
33
|
* new handle rather than updating this one.
|
|
34
34
|
*/
|
|
35
35
|
export type GenerationHandle<Response> = Snapshot & {
|
|
36
|
+
readonly snapshot: Snapshot;
|
|
37
|
+
readonly terminal: boolean;
|
|
36
38
|
/** Serializable JSON; pass it back to `resume` from another process. */
|
|
37
39
|
readonly token: unknown;
|
|
38
40
|
readonly await: (options?: AwaitOptions & RunOptions) => Promise<Response>;
|
|
@@ -56,8 +58,8 @@ export declare const make: <R = never>(options?: Options<R>) => {
|
|
|
56
58
|
write: (asset: Media.Asset, path: string, options?: RunOptions) => Promise<void>;
|
|
57
59
|
llm: {
|
|
58
60
|
request: typeof LLM.request;
|
|
59
|
-
generate: <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: RunOptions) => Promise<import("./effect.js").LLMResponse>;
|
|
60
|
-
stream: <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: RunOptions) => AsyncIterable<{
|
|
61
|
+
generate: <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions & RunOptions) => Promise<import("./effect.js").LLMResponse>;
|
|
62
|
+
stream: <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions & RunOptions) => AsyncIterable<{
|
|
61
63
|
readonly type: "step-start";
|
|
62
64
|
readonly index: number;
|
|
63
65
|
} | {
|
|
@@ -277,7 +279,7 @@ export declare const make: <R = never>(options?: Options<R>) => {
|
|
|
277
279
|
readonly encrypted?: never;
|
|
278
280
|
}))>;
|
|
279
281
|
/** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
|
|
280
|
-
loop: <const Model extends LanguageModel>(input: LoopInput<Model>, options?: RunOptions) => AsyncIterable<{
|
|
282
|
+
loop: <const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions & RunOptions) => AsyncIterable<{
|
|
281
283
|
readonly type: "step-start";
|
|
282
284
|
readonly index: number;
|
|
283
285
|
} | {
|
|
@@ -506,6 +508,7 @@ export declare const make: <R = never>(options?: Options<R>) => {
|
|
|
506
508
|
(request: CheckpointRequest, options: TriggerCompactOptions & RunOptions): Promise<CompactionCheckpointResponse>;
|
|
507
509
|
(request: CompactionRequest, options?: EndpointCompactOptions & RunOptions): Promise<CompactionResponse>;
|
|
508
510
|
};
|
|
511
|
+
canCompact: typeof import("./route/client.js").canCompact;
|
|
509
512
|
};
|
|
510
513
|
image: {
|
|
511
514
|
request: typeof import("./image.js").request;
|
|
@@ -734,18 +737,10 @@ export declare const make: <R = never>(options?: Options<R>) => {
|
|
|
734
737
|
dispose: () => Promise<void>;
|
|
735
738
|
};
|
|
736
739
|
export type Client<R = never> = ReturnType<typeof make<R>>;
|
|
737
|
-
/**
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
export declare const tool: <Parameters extends ToolSchema<any>>(config: {
|
|
742
|
-
readonly description: string;
|
|
743
|
-
readonly parameters: Parameters;
|
|
744
|
-
readonly freeform?: FreeformOption<Parameters>;
|
|
745
|
-
readonly execute: (params: Schema.Schema.Type<Parameters>, context: Partial<ToolExecuteContext> & {
|
|
746
|
-
readonly signal: AbortSignal;
|
|
747
|
-
}) => unknown;
|
|
748
|
-
}) => Tool.ExecutableTool<Parameters, Schema.Unknown>;
|
|
740
|
+
/** `tool` from `@opencode/ai/effect`, with an `execute` that returns a value or a Promise and throws to fail. */
|
|
741
|
+
export declare const tool: <Parameters extends ToolSchema<any> = typeof Schema.Unknown, Success extends ToolSchema<any> = typeof Schema.Unknown, Result extends Schema.Schema.Type<Success> | PromiseLike<Schema.Schema.Type<Success>> | NoExecute = NoExecute>(config: ToolOptions<Parameters, Success, (params: Schema.Schema.Type<Parameters>, context: Partial<ToolExecuteContext> & {
|
|
742
|
+
signal: AbortSignal;
|
|
743
|
+
}) => Result>) => 0 extends 1 & Result ? import("./tool.js").ExecutableTool<Parameters, Success> : [Result, NoExecute] extends [NoExecute, Result] ? import("./tool.js").Tool<Parameters, Success> : import("./tool.js").ExecutableTool<Parameters, Success>;
|
|
749
744
|
/** Default client over `RequestExecutor.fetchLayer` for scripts; the runtime builds its layer on first use. */
|
|
750
745
|
export declare const ai: {
|
|
751
746
|
run: <A, E>(effect: Effect.Effect<A, E, import("./image-client.js").ImageClientService | import("./effect.js").LLMClientService | RequestExecutor.RequestExecutorService | import("./decision-client.js").Service | import("./speech-client.js").SpeechClientService | import("./transcription-client.js").TranscriptionClientService | import("./video-client.js").VideoClientService>, options?: RunOptions) => Promise<A>;
|
|
@@ -762,10 +757,10 @@ export declare const ai: {
|
|
|
762
757
|
request: typeof LLM.request;
|
|
763
758
|
generate: <const Model extends LanguageModel>(input: LLMRequest<LanguageModel<{
|
|
764
759
|
readonly [x: string]: unknown;
|
|
765
|
-
}, import("./route/client.js").CompactionOperations | undefined>> | RequestInput<Model>, options?: RunOptions) => Promise<import("./effect.js").LLMResponse>;
|
|
760
|
+
}, import("./route/client.js").CompactionOperations | undefined>> | RequestInput<Model>, options?: StreamOptions & RunOptions) => Promise<import("./effect.js").LLMResponse>;
|
|
766
761
|
stream: <const Model extends LanguageModel>(input: LLMRequest<LanguageModel<{
|
|
767
762
|
readonly [x: string]: unknown;
|
|
768
|
-
}, import("./route/client.js").CompactionOperations | undefined>> | RequestInput<Model>, options?: RunOptions) => AsyncIterable<{
|
|
763
|
+
}, import("./route/client.js").CompactionOperations | undefined>> | RequestInput<Model>, options?: StreamOptions & RunOptions) => AsyncIterable<{
|
|
769
764
|
readonly type: "step-start";
|
|
770
765
|
readonly index: number;
|
|
771
766
|
} | {
|
|
@@ -985,7 +980,7 @@ export declare const ai: {
|
|
|
985
980
|
readonly encrypted?: never;
|
|
986
981
|
}))>;
|
|
987
982
|
/** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
|
|
988
|
-
loop: <const Model extends LanguageModel>(input: LoopInput<Model>, options?: RunOptions) => AsyncIterable<{
|
|
983
|
+
loop: <const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions & RunOptions) => AsyncIterable<{
|
|
989
984
|
readonly type: "step-start";
|
|
990
985
|
readonly index: number;
|
|
991
986
|
} | {
|
|
@@ -1214,6 +1209,7 @@ export declare const ai: {
|
|
|
1214
1209
|
(request: CheckpointRequest, options: TriggerCompactOptions & RunOptions): Promise<CompactionCheckpointResponse>;
|
|
1215
1210
|
(request: CompactionRequest, options?: EndpointCompactOptions & RunOptions): Promise<CompactionResponse>;
|
|
1216
1211
|
};
|
|
1212
|
+
canCompact: typeof import("./route/client.js").canCompact;
|
|
1217
1213
|
};
|
|
1218
1214
|
image: {
|
|
1219
1215
|
request: typeof import("./image.js").request;
|
|
@@ -1442,3 +1438,4 @@ export declare const ai: {
|
|
|
1442
1438
|
dispose: () => Promise<void>;
|
|
1443
1439
|
};
|
|
1444
1440
|
export * as AI from "./index.js";
|
|
1441
|
+
export type { ExecutableTool, Tool, ToolOptions, Tools } from "./tool.js";
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import { RequestExecutor } from "./route/executor.js";
|
|
|
9
9
|
import { AIError, InvalidRequestError, LanguageModel, } from "./schema/index.js";
|
|
10
10
|
import { Speech } from "./speech.js";
|
|
11
11
|
import { Transcription, } from "./transcription.js";
|
|
12
|
-
import {
|
|
12
|
+
import { ToolFailure, build } from "./tool.js";
|
|
13
13
|
import { fileMediaType } from "./utils/media-type.js";
|
|
14
14
|
import { Video } from "./video.js";
|
|
15
15
|
// Fails with `signal.reason` so aborted calls reject and aborted streams throw like `fetch`: an `AbortError` by default.
|
|
@@ -42,9 +42,11 @@ export const make = (options = {}) => {
|
|
|
42
42
|
}
|
|
43
43
|
const handle = (generation) => ({
|
|
44
44
|
...generation.snapshot,
|
|
45
|
+
snapshot: generation.snapshot,
|
|
46
|
+
terminal: generation.terminal,
|
|
45
47
|
token: generation.token,
|
|
46
|
-
await: (options) => run(generation.await(
|
|
47
|
-
events: (options) => iterate(generation.events(
|
|
48
|
+
await: (options) => run(generation.await(options), options),
|
|
49
|
+
events: (options) => iterate(generation.events(options), options),
|
|
48
50
|
result: (options) => run(generation.result(), options),
|
|
49
51
|
refresh: (options) => run(generation.refresh(), options).then(handle),
|
|
50
52
|
cancel: (options) => run(generation.cancel(), options),
|
|
@@ -77,19 +79,20 @@ export const make = (options = {}) => {
|
|
|
77
79
|
},
|
|
78
80
|
llm: {
|
|
79
81
|
request: LLM.request,
|
|
80
|
-
generate: (input, options) => run(LLM.generate(input), options),
|
|
81
|
-
stream: (input, options) => iterate(LLM.stream(input), options),
|
|
82
|
+
generate: (input, options) => run(LLM.generate(input, options), options),
|
|
83
|
+
stream: (input, options) => iterate(LLM.stream(input, options), options),
|
|
82
84
|
/** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
|
|
83
|
-
loop: (input, options) => iterate(LLM.loop(input), options),
|
|
85
|
+
loop: (input, options) => iterate(LLM.loop(input, options), options),
|
|
84
86
|
/** Decode the model's output against `schema` (or `jsonSchema`, unvalidated) like `LLM.generateObject`. */
|
|
85
87
|
generateObject,
|
|
86
88
|
/** Compact a Responses request like `LLMClient.compact`: a replacement window, or a checkpoint with `mechanism: "trigger"`. */
|
|
87
89
|
compact,
|
|
90
|
+
canCompact: LLMClient.canCompact,
|
|
88
91
|
},
|
|
89
92
|
image: {
|
|
90
93
|
request: Image.request,
|
|
91
|
-
generate: (input, options) => run(Image.generate(input,
|
|
92
|
-
stream: (input, options) => iterate(Image.stream(input,
|
|
94
|
+
generate: (input, options) => run(Image.generate(input, options), options),
|
|
95
|
+
stream: (input, options) => iterate(Image.stream(input, options), options),
|
|
93
96
|
start: (input, options) => run(Image.start(input), options).then(handle),
|
|
94
97
|
resume: (model, token, options) => run(Image.resume(model, token), options).then(handle),
|
|
95
98
|
},
|
|
@@ -120,20 +123,15 @@ export const make = (options = {}) => {
|
|
|
120
123
|
};
|
|
121
124
|
};
|
|
122
125
|
const fileError = (message, cause) => new AIError({ reason: new InvalidRequestError({ message, cause }) });
|
|
123
|
-
/**
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
execute: (params, context) => Effect.tryPromise({
|
|
133
|
-
try: async (signal) => (await config.execute(params, { ...context, signal })) ?? null,
|
|
134
|
-
catch: (error) => new ToolFailure({ message: error instanceof Error ? error.message : String(error), error }),
|
|
135
|
-
}),
|
|
136
|
-
});
|
|
126
|
+
/** `tool` from `@opencode/ai/effect`, with an `execute` that returns a value or a Promise and throws to fail. */
|
|
127
|
+
export const tool = (config) => {
|
|
128
|
+
const execute = config.execute;
|
|
129
|
+
return build(config, execute &&
|
|
130
|
+
((params, context) => Effect.tryPromise({
|
|
131
|
+
try: async (signal) => execute(params, { ...context, signal }),
|
|
132
|
+
catch: (error) => new ToolFailure({ message: error instanceof Error ? error.message : String(error), error }),
|
|
133
|
+
})));
|
|
134
|
+
};
|
|
137
135
|
/** Default client over `RequestExecutor.fetchLayer` for scripts; the runtime builds its layer on first use. */
|
|
138
136
|
export const ai = make();
|
|
139
137
|
export * as AI from "./index.js";
|
package/dist/llm.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, JsonSchema, Schema, Stream } from "effect";
|
|
2
2
|
import { Service, type StreamOptions } from "./route/client.js";
|
|
3
3
|
import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, type ContentPart, type LanguageModelProviderOptions, type ToolEntryInput } from "./schema/index.js";
|
|
4
|
-
import { type
|
|
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"> & {
|
|
7
7
|
readonly model: SelectedLanguageModel;
|
|
@@ -17,7 +17,7 @@ export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageM
|
|
|
17
17
|
export declare function generate<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Effect.Effect<LLMResponse, AIError, Service>;
|
|
18
18
|
export declare function stream<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
|
|
19
19
|
export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestInput<SelectedLanguageModel>, "tools"> & {
|
|
20
|
-
readonly tools:
|
|
20
|
+
readonly tools: Record<string, ExecutableTool>;
|
|
21
21
|
/** Called after each step that has tool calls; returning `true` ends the loop without running them. */
|
|
22
22
|
readonly until?: (state: {
|
|
23
23
|
readonly steps: ReadonlyArray<LLMResponse>;
|
package/dist/llm.js
CHANGED
|
@@ -2,7 +2,7 @@ import { Effect, JsonSchema, Schema, Stream } from "effect";
|
|
|
2
2
|
import { tryRequest } from "./media-model.js";
|
|
3
3
|
import { LLMClient, Service } from "./route/client.js";
|
|
4
4
|
import { GenerationOptions, HttpOptions, InvalidProviderOutputError, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, ToolChoice, ToolEntry, } from "./schema/index.js";
|
|
5
|
-
import {
|
|
5
|
+
import { tool, toDefinitions } from "./tool.js";
|
|
6
6
|
import { ToolRuntime } from "./tool-runtime.js";
|
|
7
7
|
export function generate(input, options) {
|
|
8
8
|
return requestEffect(input).pipe(Effect.flatMap((request) => LLMClient.generate(request, options)));
|
|
@@ -98,17 +98,8 @@ const runGenerateObject = Effect.fn("LLM.generateObject")(function* (options, to
|
|
|
98
98
|
export function generateObject(options) {
|
|
99
99
|
if ("schema" in options) {
|
|
100
100
|
const { schema, ...rest } = options;
|
|
101
|
-
return runGenerateObject(rest,
|
|
102
|
-
description: GENERATE_OBJECT_TOOL_DESCRIPTION,
|
|
103
|
-
parameters: schema,
|
|
104
|
-
success: Schema.Unknown,
|
|
105
|
-
execute: () => Effect.void,
|
|
106
|
-
}));
|
|
101
|
+
return runGenerateObject(rest, tool({ description: GENERATE_OBJECT_TOOL_DESCRIPTION, parameters: schema }));
|
|
107
102
|
}
|
|
108
103
|
const { jsonSchema, ...rest } = options;
|
|
109
|
-
return runGenerateObject(rest,
|
|
110
|
-
description: GENERATE_OBJECT_TOOL_DESCRIPTION,
|
|
111
|
-
jsonSchema,
|
|
112
|
-
execute: () => Effect.void,
|
|
113
|
-
}));
|
|
104
|
+
return runGenerateObject(rest, tool({ description: GENERATE_OBJECT_TOOL_DESCRIPTION, jsonSchema }));
|
|
114
105
|
}
|
|
@@ -40,7 +40,7 @@ export declare const protocol: Protocol<{
|
|
|
40
40
|
readonly detail?: string | undefined;
|
|
41
41
|
} | {
|
|
42
42
|
readonly type: "input_file";
|
|
43
|
-
readonly filename
|
|
43
|
+
readonly filename?: string | undefined;
|
|
44
44
|
readonly detail?: string | undefined;
|
|
45
45
|
readonly file_data?: string | undefined;
|
|
46
46
|
readonly file_url?: string | undefined;
|
|
@@ -72,7 +72,7 @@ export declare const protocol: Protocol<{
|
|
|
72
72
|
readonly detail?: string | undefined;
|
|
73
73
|
} | {
|
|
74
74
|
readonly type: "input_file";
|
|
75
|
-
readonly filename
|
|
75
|
+
readonly filename?: string | undefined;
|
|
76
76
|
readonly detail?: string | undefined;
|
|
77
77
|
readonly file_data?: string | undefined;
|
|
78
78
|
readonly file_url?: string | undefined;
|
|
@@ -77,19 +77,19 @@ const fromRequest = Effect.fn("AssemblyAITranscription.fromRequest")(function* (
|
|
|
77
77
|
const decodeTranscript = route.decodeJson(Transcript);
|
|
78
78
|
const decodeStart = Effect.fn("AssemblyAITranscription.decodeStart")(function* (response) {
|
|
79
79
|
const output = yield* decodeTranscript(response);
|
|
80
|
-
const status =
|
|
80
|
+
const status = MediaProtocol.status(STATUS, output.value.status);
|
|
81
81
|
return { token: { transcriptID: output.value.id }, snapshot: { id: output.value.id, status } };
|
|
82
82
|
});
|
|
83
83
|
const decodeStatus = Effect.fn("AssemblyAITranscription.decodeStatus")(function* (response, context) {
|
|
84
84
|
const output = yield* decodeTranscript(response);
|
|
85
|
-
const status =
|
|
85
|
+
const status = MediaProtocol.status(STATUS, output.value.status);
|
|
86
86
|
return { id: context.token.transcriptID, status };
|
|
87
87
|
});
|
|
88
88
|
const seconds = (milliseconds) => milliseconds / 1000;
|
|
89
89
|
const decodeResult = Effect.fn("AssemblyAITranscription.decodeResult")(function* (response, context) {
|
|
90
90
|
const output = yield* decodeTranscript(response);
|
|
91
91
|
const transcript = output.value;
|
|
92
|
-
const status =
|
|
92
|
+
const status = MediaProtocol.status(STATUS, transcript.status);
|
|
93
93
|
const error = transcript.error ?? undefined;
|
|
94
94
|
if (status === "failed")
|
|
95
95
|
return yield* output.ended("failed", `${route.name} transcription failed${error === undefined ? "" : `: ${error}`}`);
|
|
@@ -54,7 +54,7 @@ export declare const protocol: Protocol<{
|
|
|
54
54
|
readonly detail?: string | undefined;
|
|
55
55
|
} | {
|
|
56
56
|
readonly type: "input_file";
|
|
57
|
-
readonly filename
|
|
57
|
+
readonly filename?: string | undefined;
|
|
58
58
|
readonly detail?: string | undefined;
|
|
59
59
|
readonly file_data?: string | undefined;
|
|
60
60
|
readonly file_url?: string | undefined;
|
|
@@ -86,7 +86,7 @@ export declare const protocol: Protocol<{
|
|
|
86
86
|
readonly detail?: string | undefined;
|
|
87
87
|
} | {
|
|
88
88
|
readonly type: "input_file";
|
|
89
|
-
readonly filename
|
|
89
|
+
readonly filename?: string | undefined;
|
|
90
90
|
readonly detail?: string | undefined;
|
|
91
91
|
readonly file_data?: string | undefined;
|
|
92
92
|
readonly file_url?: string | undefined;
|
|
@@ -310,7 +310,7 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
310
310
|
readonly detail?: string | undefined;
|
|
311
311
|
} | {
|
|
312
312
|
readonly type: "input_file";
|
|
313
|
-
readonly filename
|
|
313
|
+
readonly filename?: string | undefined;
|
|
314
314
|
readonly detail?: string | undefined;
|
|
315
315
|
readonly file_data?: string | undefined;
|
|
316
316
|
readonly file_url?: string | undefined;
|
|
@@ -342,7 +342,7 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
342
342
|
readonly detail?: string | undefined;
|
|
343
343
|
} | {
|
|
344
344
|
readonly type: "input_file";
|
|
345
|
-
readonly filename
|
|
345
|
+
readonly filename?: string | undefined;
|
|
346
346
|
readonly detail?: string | undefined;
|
|
347
347
|
readonly file_data?: string | undefined;
|
|
348
348
|
readonly file_url?: string | undefined;
|
|
@@ -19,7 +19,7 @@ export declare const OpenResponsesInputImage: Schema.Struct<{
|
|
|
19
19
|
}>;
|
|
20
20
|
export declare const OpenResponsesInputFile: Schema.Struct<{
|
|
21
21
|
readonly type: Schema.tag<"input_file">;
|
|
22
|
-
readonly filename: Schema.String
|
|
22
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
23
23
|
readonly detail: Schema.optional<Schema.String>;
|
|
24
24
|
readonly file_data: Schema.optional<Schema.String>;
|
|
25
25
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -30,7 +30,7 @@ declare const MediaInput: Schema.Union<readonly [Schema.Struct<{
|
|
|
30
30
|
readonly detail: Schema.optional<Schema.String>;
|
|
31
31
|
}>, Schema.Struct<{
|
|
32
32
|
readonly type: Schema.tag<"input_file">;
|
|
33
|
-
readonly filename: Schema.String
|
|
33
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
34
34
|
readonly detail: Schema.optional<Schema.String>;
|
|
35
35
|
readonly file_data: Schema.optional<Schema.String>;
|
|
36
36
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -134,7 +134,7 @@ export declare const InputItem: Schema.Union<readonly [Schema.Struct<{
|
|
|
134
134
|
readonly detail: Schema.optional<Schema.String>;
|
|
135
135
|
}>, Schema.Struct<{
|
|
136
136
|
readonly type: Schema.tag<"input_file">;
|
|
137
|
-
readonly filename: Schema.String
|
|
137
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
138
138
|
readonly detail: Schema.optional<Schema.String>;
|
|
139
139
|
readonly file_data: Schema.optional<Schema.String>;
|
|
140
140
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -181,7 +181,7 @@ export declare const InputItem: Schema.Union<readonly [Schema.Struct<{
|
|
|
181
181
|
readonly detail: Schema.optional<Schema.String>;
|
|
182
182
|
}>, Schema.Struct<{
|
|
183
183
|
readonly type: Schema.tag<"input_file">;
|
|
184
|
-
readonly filename: Schema.String
|
|
184
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
185
185
|
readonly detail: Schema.optional<Schema.String>;
|
|
186
186
|
readonly file_data: Schema.optional<Schema.String>;
|
|
187
187
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -291,7 +291,7 @@ export declare const coreFields: {
|
|
|
291
291
|
readonly detail: Schema.optional<Schema.String>;
|
|
292
292
|
}>, Schema.Struct<{
|
|
293
293
|
readonly type: Schema.tag<"input_file">;
|
|
294
|
-
readonly filename: Schema.String
|
|
294
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
295
295
|
readonly detail: Schema.optional<Schema.String>;
|
|
296
296
|
readonly file_data: Schema.optional<Schema.String>;
|
|
297
297
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -338,7 +338,7 @@ export declare const coreFields: {
|
|
|
338
338
|
readonly detail: Schema.optional<Schema.String>;
|
|
339
339
|
}>, Schema.Struct<{
|
|
340
340
|
readonly type: Schema.tag<"input_file">;
|
|
341
|
-
readonly filename: Schema.String
|
|
341
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
342
342
|
readonly detail: Schema.optional<Schema.String>;
|
|
343
343
|
readonly file_data: Schema.optional<Schema.String>;
|
|
344
344
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -472,7 +472,7 @@ export declare const OpenResponsesBody: Schema.Struct<{
|
|
|
472
472
|
readonly detail: Schema.optional<Schema.String>;
|
|
473
473
|
}>, Schema.Struct<{
|
|
474
474
|
readonly type: Schema.tag<"input_file">;
|
|
475
|
-
readonly filename: Schema.String
|
|
475
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
476
476
|
readonly detail: Schema.optional<Schema.String>;
|
|
477
477
|
readonly file_data: Schema.optional<Schema.String>;
|
|
478
478
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -519,7 +519,7 @@ export declare const OpenResponsesBody: Schema.Struct<{
|
|
|
519
519
|
readonly detail: Schema.optional<Schema.String>;
|
|
520
520
|
}>, Schema.Struct<{
|
|
521
521
|
readonly type: Schema.tag<"input_file">;
|
|
522
|
-
readonly filename: Schema.String
|
|
522
|
+
readonly filename: Schema.optional<Schema.String>;
|
|
523
523
|
readonly detail: Schema.optional<Schema.String>;
|
|
524
524
|
readonly file_data: Schema.optional<Schema.String>;
|
|
525
525
|
readonly file_url: Schema.optional<Schema.String>;
|
|
@@ -1023,7 +1023,7 @@ export declare const lowerConversation: (request: LLMRequest<import("../schema/o
|
|
|
1023
1023
|
readonly detail?: string | undefined;
|
|
1024
1024
|
} | {
|
|
1025
1025
|
readonly type: "input_file";
|
|
1026
|
-
readonly filename
|
|
1026
|
+
readonly filename?: string | undefined;
|
|
1027
1027
|
readonly detail?: string | undefined;
|
|
1028
1028
|
readonly file_data?: string | undefined;
|
|
1029
1029
|
readonly file_url?: string | undefined;
|
|
@@ -1055,7 +1055,7 @@ export declare const lowerConversation: (request: LLMRequest<import("../schema/o
|
|
|
1055
1055
|
readonly detail?: string | undefined;
|
|
1056
1056
|
} | {
|
|
1057
1057
|
readonly type: "input_file";
|
|
1058
|
-
readonly filename
|
|
1058
|
+
readonly filename?: string | undefined;
|
|
1059
1059
|
readonly detail?: string | undefined;
|
|
1060
1060
|
readonly file_data?: string | undefined;
|
|
1061
1061
|
readonly file_url?: string | undefined;
|
|
@@ -1200,7 +1200,7 @@ export declare const fromRequestWithAdapter: (request: LLMRequest<import("../sch
|
|
|
1200
1200
|
readonly detail?: string | undefined;
|
|
1201
1201
|
} | {
|
|
1202
1202
|
readonly type: "input_file";
|
|
1203
|
-
readonly filename
|
|
1203
|
+
readonly filename?: string | undefined;
|
|
1204
1204
|
readonly detail?: string | undefined;
|
|
1205
1205
|
readonly file_data?: string | undefined;
|
|
1206
1206
|
readonly file_url?: string | undefined;
|
|
@@ -1232,7 +1232,7 @@ export declare const fromRequestWithAdapter: (request: LLMRequest<import("../sch
|
|
|
1232
1232
|
readonly detail?: string | undefined;
|
|
1233
1233
|
} | {
|
|
1234
1234
|
readonly type: "input_file";
|
|
1235
|
-
readonly filename
|
|
1235
|
+
readonly filename?: string | undefined;
|
|
1236
1236
|
readonly detail?: string | undefined;
|
|
1237
1237
|
readonly file_data?: string | undefined;
|
|
1238
1238
|
readonly file_url?: string | undefined;
|
|
@@ -1398,7 +1398,7 @@ export declare const fromRequest: (request: LLMRequest) => Effect.Effect<{
|
|
|
1398
1398
|
readonly detail?: string | undefined;
|
|
1399
1399
|
} | {
|
|
1400
1400
|
readonly type: "input_file";
|
|
1401
|
-
readonly filename
|
|
1401
|
+
readonly filename?: string | undefined;
|
|
1402
1402
|
readonly detail?: string | undefined;
|
|
1403
1403
|
readonly file_data?: string | undefined;
|
|
1404
1404
|
readonly file_url?: string | undefined;
|
|
@@ -1430,7 +1430,7 @@ export declare const fromRequest: (request: LLMRequest) => Effect.Effect<{
|
|
|
1430
1430
|
readonly detail?: string | undefined;
|
|
1431
1431
|
} | {
|
|
1432
1432
|
readonly type: "input_file";
|
|
1433
|
-
readonly filename
|
|
1433
|
+
readonly filename?: string | undefined;
|
|
1434
1434
|
readonly detail?: string | undefined;
|
|
1435
1435
|
readonly file_data?: string | undefined;
|
|
1436
1436
|
readonly file_url?: string | undefined;
|
|
@@ -1834,7 +1834,7 @@ export declare const protocol: Protocol<{
|
|
|
1834
1834
|
readonly detail?: string | undefined;
|
|
1835
1835
|
} | {
|
|
1836
1836
|
readonly type: "input_file";
|
|
1837
|
-
readonly filename
|
|
1837
|
+
readonly filename?: string | undefined;
|
|
1838
1838
|
readonly detail?: string | undefined;
|
|
1839
1839
|
readonly file_data?: string | undefined;
|
|
1840
1840
|
readonly file_url?: string | undefined;
|
|
@@ -1866,7 +1866,7 @@ export declare const protocol: Protocol<{
|
|
|
1866
1866
|
readonly detail?: string | undefined;
|
|
1867
1867
|
} | {
|
|
1868
1868
|
readonly type: "input_file";
|
|
1869
|
-
readonly filename
|
|
1869
|
+
readonly filename?: string | undefined;
|
|
1870
1870
|
readonly detail?: string | undefined;
|
|
1871
1871
|
readonly file_data?: string | undefined;
|
|
1872
1872
|
readonly file_url?: string | undefined;
|
|
@@ -2090,7 +2090,7 @@ export declare const httpTransport: HttpTransport.HttpJsonTransport<{
|
|
|
2090
2090
|
readonly detail?: string | undefined;
|
|
2091
2091
|
} | {
|
|
2092
2092
|
readonly type: "input_file";
|
|
2093
|
-
readonly filename
|
|
2093
|
+
readonly filename?: string | undefined;
|
|
2094
2094
|
readonly detail?: string | undefined;
|
|
2095
2095
|
readonly file_data?: string | undefined;
|
|
2096
2096
|
readonly file_url?: string | undefined;
|
|
@@ -2122,7 +2122,7 @@ export declare const httpTransport: HttpTransport.HttpJsonTransport<{
|
|
|
2122
2122
|
readonly detail?: string | undefined;
|
|
2123
2123
|
} | {
|
|
2124
2124
|
readonly type: "input_file";
|
|
2125
|
-
readonly filename
|
|
2125
|
+
readonly filename?: string | undefined;
|
|
2126
2126
|
readonly detail?: string | undefined;
|
|
2127
2127
|
readonly file_data?: string | undefined;
|
|
2128
2128
|
readonly file_url?: string | undefined;
|
|
@@ -26,7 +26,7 @@ export const OpenResponsesInputImage = Schema.Struct({
|
|
|
26
26
|
});
|
|
27
27
|
export const OpenResponsesInputFile = Schema.Struct({
|
|
28
28
|
type: Schema.tag("input_file"),
|
|
29
|
-
filename: Schema.String,
|
|
29
|
+
filename: Schema.optional(Schema.String),
|
|
30
30
|
detail: Schema.optional(Schema.String),
|
|
31
31
|
file_data: Schema.optional(Schema.String),
|
|
32
32
|
file_url: Schema.optional(Schema.String),
|
|
@@ -405,11 +405,13 @@ const lowerMedia = Effect.fnUntraced(function* (part, request, adapter) {
|
|
|
405
405
|
if (part.media.kind !== "image") {
|
|
406
406
|
if (part.media.kind === "video")
|
|
407
407
|
return { type: "input_video", video_url: location };
|
|
408
|
+
if (url)
|
|
409
|
+
return { type: "input_file", file_url: url, detail };
|
|
408
410
|
return {
|
|
409
411
|
type: "input_file",
|
|
410
412
|
filename: part.filename ?? (mime === "application/pdf" ? "document.pdf" : "file"),
|
|
411
413
|
detail,
|
|
412
|
-
|
|
414
|
+
file_data: location,
|
|
413
415
|
};
|
|
414
416
|
}
|
|
415
417
|
return {
|
|
@@ -34,7 +34,7 @@ export declare const route: Route<{
|
|
|
34
34
|
readonly detail?: string | undefined;
|
|
35
35
|
} | {
|
|
36
36
|
readonly type: "input_file";
|
|
37
|
-
readonly filename
|
|
37
|
+
readonly filename?: string | undefined;
|
|
38
38
|
readonly detail?: string | undefined;
|
|
39
39
|
readonly file_data?: string | undefined;
|
|
40
40
|
readonly file_url?: string | undefined;
|
|
@@ -66,7 +66,7 @@ export declare const route: Route<{
|
|
|
66
66
|
readonly detail?: string | undefined;
|
|
67
67
|
} | {
|
|
68
68
|
readonly type: "input_file";
|
|
69
|
-
readonly filename
|
|
69
|
+
readonly filename?: string | undefined;
|
|
70
70
|
readonly detail?: string | undefined;
|
|
71
71
|
readonly file_data?: string | undefined;
|
|
72
72
|
readonly file_url?: string | undefined;
|