@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
|
@@ -7,6 +7,7 @@ import { ProviderID } from "../schema/index.js";
|
|
|
7
7
|
import { SystemOne } from "../protocols/system-one.js";
|
|
8
8
|
import { OpenAIImages } from "../protocols/openai-images.js";
|
|
9
9
|
import { MediaRoute } from "../route/media.js";
|
|
10
|
+
import { HttpTransport } from "../route/transport/index.js";
|
|
10
11
|
import { OpenAIChat } from "../protocols/openai-chat.js";
|
|
11
12
|
import { OpenResponses } from "../protocols/open-responses.js";
|
|
12
13
|
import { cacheControl } from "../protocols/utils/cache.js";
|
|
@@ -14,14 +15,17 @@ import { isRecord, JsonObject, lenient, ProviderShared } from "../protocols/shar
|
|
|
14
15
|
export const id = ProviderID.make("openrouter");
|
|
15
16
|
const baseURL = "https://openrouter.ai/api/v1";
|
|
16
17
|
const ADAPTER = "openrouter";
|
|
17
|
-
const
|
|
18
|
-
usage: lenient(Schema.Union([Schema.Boolean, JsonObject])),
|
|
18
|
+
const SharedOptions = Schema.Struct({
|
|
19
19
|
models: lenient(Schema.Array(Schema.String)),
|
|
20
20
|
provider: lenient(JsonObject),
|
|
21
21
|
plugins: lenient(Schema.Array(JsonObject)),
|
|
22
|
-
web_search_options: lenient(JsonObject),
|
|
23
22
|
debug: lenient(JsonObject),
|
|
24
23
|
user: lenient(Schema.String),
|
|
24
|
+
});
|
|
25
|
+
const Options = Schema.Struct({
|
|
26
|
+
...SharedOptions.fields,
|
|
27
|
+
usage: lenient(Schema.Union([Schema.Boolean, JsonObject])),
|
|
28
|
+
web_search_options: lenient(JsonObject),
|
|
25
29
|
reasoning: lenient(JsonObject),
|
|
26
30
|
});
|
|
27
31
|
const decodeOptions = Schema.decodeUnknownSync(Options);
|
|
@@ -74,12 +78,8 @@ const bodyOptions = (body, input, maxTokens) => {
|
|
|
74
78
|
...options,
|
|
75
79
|
...body,
|
|
76
80
|
...(usage !== undefined ? { usage } : {}),
|
|
77
|
-
...(known
|
|
78
|
-
...(known.provider !== undefined ? { provider: known.provider } : {}),
|
|
79
|
-
...(known.plugins !== undefined ? { plugins: known.plugins } : {}),
|
|
81
|
+
...sharedOptions(known),
|
|
80
82
|
...(known.web_search_options !== undefined ? { web_search_options: known.web_search_options } : {}),
|
|
81
|
-
...(known.debug !== undefined ? { debug: known.debug } : {}),
|
|
82
|
-
...(known.user !== undefined ? { user: known.user } : {}),
|
|
83
83
|
...(known.reasoning !== undefined ? { reasoning: fitReasoning(known.reasoning, maxTokens) } : {}),
|
|
84
84
|
};
|
|
85
85
|
};
|
|
@@ -96,6 +96,25 @@ const imageSizing = (request) => {
|
|
|
96
96
|
resolution: resolution !== undefined && IMAGE_RESOLUTIONS.has(resolution) ? resolution : undefined,
|
|
97
97
|
};
|
|
98
98
|
};
|
|
99
|
+
const sharedOptions = (known) => ({
|
|
100
|
+
...(known.models !== undefined ? { models: known.models } : {}),
|
|
101
|
+
...(known.provider !== undefined ? { provider: known.provider } : {}),
|
|
102
|
+
...(known.plugins !== undefined ? { plugins: known.plugins } : {}),
|
|
103
|
+
...(known.debug !== undefined ? { debug: known.debug } : {}),
|
|
104
|
+
...(known.user !== undefined ? { user: known.user } : {}),
|
|
105
|
+
});
|
|
106
|
+
const OpenRouterResponsesBody = Schema.Struct({
|
|
107
|
+
...OpenResponses.OpenResponsesBody.fields,
|
|
108
|
+
...SharedOptions.fields,
|
|
109
|
+
});
|
|
110
|
+
const responsesProtocol = Protocol.make({
|
|
111
|
+
id: "openrouter-responses",
|
|
112
|
+
body: {
|
|
113
|
+
schema: OpenRouterResponsesBody,
|
|
114
|
+
from: (request) => OpenResponses.fromRequest(request).pipe(Effect.map((body) => ({ ...body, ...sharedOptions(decodeOptions(request.providerOptions ?? {})) }))),
|
|
115
|
+
},
|
|
116
|
+
stream: OpenResponses.protocol.stream,
|
|
117
|
+
});
|
|
99
118
|
export const route = Route.make({
|
|
100
119
|
id: ADAPTER,
|
|
101
120
|
provider: id,
|
|
@@ -108,9 +127,9 @@ const responsesRoute = Route.make({
|
|
|
108
127
|
id: "openrouter-responses",
|
|
109
128
|
provider: id,
|
|
110
129
|
providerMetadataKey: "openrouter",
|
|
111
|
-
protocol:
|
|
130
|
+
protocol: responsesProtocol,
|
|
112
131
|
endpoint: Endpoint.path("/responses", { baseURL }),
|
|
113
|
-
transport:
|
|
132
|
+
transport: HttpTransport.sseJson.with(),
|
|
114
133
|
defaults: { providerOptions: { store: false, include: ["reasoning.encrypted_content"] } },
|
|
115
134
|
});
|
|
116
135
|
export const routes = [route, responsesRoute];
|
|
@@ -46,7 +46,7 @@ export declare const routes: (Route<{
|
|
|
46
46
|
readonly detail?: string | undefined;
|
|
47
47
|
} | {
|
|
48
48
|
readonly type: "input_file";
|
|
49
|
-
readonly filename
|
|
49
|
+
readonly filename?: string | undefined;
|
|
50
50
|
readonly detail?: string | undefined;
|
|
51
51
|
readonly file_data?: string | undefined;
|
|
52
52
|
readonly file_url?: string | undefined;
|
|
@@ -78,7 +78,7 @@ export declare const routes: (Route<{
|
|
|
78
78
|
readonly detail?: string | undefined;
|
|
79
79
|
} | {
|
|
80
80
|
readonly type: "input_file";
|
|
81
|
-
readonly filename
|
|
81
|
+
readonly filename?: string | undefined;
|
|
82
82
|
readonly detail?: string | undefined;
|
|
83
83
|
readonly file_data?: string | undefined;
|
|
84
84
|
readonly file_url?: string | undefined;
|
|
@@ -49,7 +49,7 @@ export declare const routes: (Route<{
|
|
|
49
49
|
readonly detail?: string | undefined;
|
|
50
50
|
} | {
|
|
51
51
|
readonly type: "input_file";
|
|
52
|
-
readonly filename
|
|
52
|
+
readonly filename?: string | undefined;
|
|
53
53
|
readonly detail?: string | undefined;
|
|
54
54
|
readonly file_data?: string | undefined;
|
|
55
55
|
readonly file_url?: string | undefined;
|
|
@@ -81,7 +81,7 @@ export declare const routes: (Route<{
|
|
|
81
81
|
readonly detail?: string | undefined;
|
|
82
82
|
} | {
|
|
83
83
|
readonly type: "input_file";
|
|
84
|
-
readonly filename
|
|
84
|
+
readonly filename?: string | undefined;
|
|
85
85
|
readonly detail?: string | undefined;
|
|
86
86
|
readonly file_data?: string | undefined;
|
|
87
87
|
readonly file_url?: string | undefined;
|
|
@@ -171,9 +171,8 @@ export declare const identity: (input: {
|
|
|
171
171
|
unsupported: (operation: string, message: string) => AIError;
|
|
172
172
|
};
|
|
173
173
|
export type Identity = ReturnType<typeof identity>;
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
export declare const status: <Table extends Record<string, Status>>(table: Table, raw: string, output: Output) => Effect.Effect<Status, AIError>;
|
|
174
|
+
/** Map a provider status string through the protocol's table; unknown values keep the generation running. */
|
|
175
|
+
export declare const status: (table: Readonly<Record<string, Status>>, raw: string) => Status;
|
|
177
176
|
/** Map a provider error code through the protocol's table; missing or unmapped codes are `ProviderInternal`. */
|
|
178
177
|
export declare const failure: (table: Readonly<Record<string, Failure>>, code: string | number | undefined) => Failure;
|
|
179
178
|
/** A `url` asset whose provider-declared retention window starts now. */
|
|
@@ -109,12 +109,8 @@ export const identity = (input) => {
|
|
|
109
109
|
unsupported: (operation, message) => new AIError({ reason: new UnsupportedOperationError({ operation, provider, route: input.id, message }) }),
|
|
110
110
|
};
|
|
111
111
|
};
|
|
112
|
-
/** Map a provider status string through the protocol's table; unknown values
|
|
113
|
-
export const status = (table, raw,
|
|
114
|
-
if (!Object.hasOwn(table, raw))
|
|
115
|
-
return Effect.fail(output.invalid(`Unknown generation status "${raw}"`));
|
|
116
|
-
return Effect.succeed(table[raw]);
|
|
117
|
-
};
|
|
112
|
+
/** Map a provider status string through the protocol's table; unknown values keep the generation running. */
|
|
113
|
+
export const status = (table, raw) => Object.hasOwn(table, raw) ? table[raw] : "running";
|
|
118
114
|
/** Map a provider error code through the protocol's table; missing or unmapped codes are `ProviderInternal`. */
|
|
119
115
|
export const failure = (table, code) => code !== undefined && Object.hasOwn(table, code) ? table[code] : "ProviderInternal";
|
|
120
116
|
/** A `url` asset whose provider-declared retention window starts now. */
|
package/dist/testing.d.ts
CHANGED
|
@@ -1118,7 +1118,7 @@ export declare const textWithUsage: (value: string, id: string, inputTokens: num
|
|
|
1118
1118
|
readonly text: string | null;
|
|
1119
1119
|
readonly encrypted?: never;
|
|
1120
1120
|
})))[];
|
|
1121
|
-
export declare const
|
|
1121
|
+
export declare const toolCall: (id: string, name: string, input: unknown) => ({
|
|
1122
1122
|
readonly type: "step-start";
|
|
1123
1123
|
readonly index: number;
|
|
1124
1124
|
} | {
|
package/dist/testing.js
CHANGED
|
@@ -31,7 +31,7 @@ const textEvents = (value, id) => [
|
|
|
31
31
|
];
|
|
32
32
|
export const text = (value, id) => stop(...textEvents(value, id));
|
|
33
33
|
export const textWithUsage = (value, id, inputTokens) => complete({ reason: { normalized: "stop" }, usage: { inputTokens, nonCachedInputTokens: inputTokens } }, ...textEvents(value, id));
|
|
34
|
-
export const
|
|
34
|
+
export const toolCall = (id, name, input) => toolCalls(LLMEvent.toolCall({ id, name, input }));
|
|
35
35
|
export const failAfter = (error, ...events) => Stream.fromIterable(events).pipe(Stream.concat(Stream.fail(error)));
|
|
36
36
|
export const hangAfter = (...events) => Stream.concat(Stream.fromIterable(events), Stream.never);
|
|
37
37
|
const make = (options) => Effect.sync(() => {
|
package/dist/tool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, JsonSchema, Schema } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import type { Content } from "@opencode/schema/tool";
|
|
3
3
|
import type { Freeform, ToolCallPart, ToolDefinition as ToolDefinitionClass, ToolOutput as ToolOutputType } from "./schema/index.js";
|
|
4
4
|
import { ToolFailure } from "./schema/index.js";
|
|
5
5
|
/**
|
|
@@ -19,14 +19,12 @@ export interface ToolModelOutputInput<Parameters, Output> {
|
|
|
19
19
|
readonly parameters: Parameters;
|
|
20
20
|
readonly output: Output;
|
|
21
21
|
}
|
|
22
|
-
export type ToolToModelOutput<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> = (input: ToolModelOutputInput<Schema.Schema.Type<Parameters>, Success["Encoded"]>) => ReadonlyArray<
|
|
22
|
+
export type ToolToModelOutput<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> = (input: ToolModelOutputInput<Schema.Schema.Type<Parameters>, Success["Encoded"]>) => ReadonlyArray<Content>;
|
|
23
23
|
type FreeformField<T> = {
|
|
24
24
|
[K in keyof T]-?: [Exclude<keyof T, K>] extends [never] ? {} extends Pick<T, K> ? never : T[K] extends string ? K : never : never;
|
|
25
25
|
}[keyof T];
|
|
26
|
-
/** `Freeform`
|
|
27
|
-
export type FreeformOption<Parameters extends ToolSchema<any>> = [
|
|
28
|
-
FreeformField<Schema.Schema.Type<Parameters>>
|
|
29
|
-
] extends [never] ? undefined : Freeform;
|
|
26
|
+
/** `Freeform` for exactly one required string parameter or unknown ones (`jsonSchema`), otherwise `undefined`. */
|
|
27
|
+
export type FreeformOption<Parameters extends ToolSchema<any>> = unknown extends Schema.Schema.Type<Parameters> ? Freeform : [FreeformField<Schema.Schema.Type<Parameters>>] extends [never] ? undefined : Freeform;
|
|
30
28
|
/**
|
|
31
29
|
* A type-safe LLM tool. Each tool bundles its own description, parameter
|
|
32
30
|
* Schema and success Schema. The execute handler is optional: omit it when you
|
|
@@ -39,7 +37,7 @@ export type FreeformOption<Parameters extends ToolSchema<any>> = [
|
|
|
39
37
|
* Internally each tool also carries memoized codecs and a precomputed
|
|
40
38
|
* `ToolDefinition` so callers do not rebuild them per invocation.
|
|
41
39
|
*/
|
|
42
|
-
export interface
|
|
40
|
+
export interface Tool<Parameters extends ToolSchema<any> = any, Success extends ToolSchema<any> = any> {
|
|
43
41
|
readonly description: string;
|
|
44
42
|
readonly parameters: Parameters;
|
|
45
43
|
readonly success: Success;
|
|
@@ -57,83 +55,53 @@ export interface Definition<Parameters extends ToolSchema<any>, Success extends
|
|
|
57
55
|
/** @internal */
|
|
58
56
|
readonly _definition: ToolDefinitionClass;
|
|
59
57
|
}
|
|
60
|
-
|
|
61
|
-
export type ExecutableTool<Parameters extends ToolSchema<any
|
|
58
|
+
/** A tool with an `execute` handler, which `LLM.loop` and `ToolRuntime.dispatch` can run. */
|
|
59
|
+
export type ExecutableTool<Parameters extends ToolSchema<any> = any, Success extends ToolSchema<any> = any> = Tool<Parameters, Success> & {
|
|
62
60
|
readonly execute: ToolExecute<Parameters, Success>;
|
|
63
61
|
};
|
|
64
|
-
export type AnyExecutableTool = ExecutableTool<any, any>;
|
|
65
|
-
export type ExecutableTools = Record<string, AnyExecutableTool>;
|
|
66
62
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* ```ts
|
|
73
|
-
* Tool.make({
|
|
74
|
-
* description: "Get current weather",
|
|
75
|
-
* parameters: Schema.Struct({ city: Schema.String }),
|
|
76
|
-
* success: Schema.Struct({ temperature: Schema.Number }),
|
|
77
|
-
* execute: ({ city }) => Effect.succeed({ temperature: 22 }),
|
|
78
|
-
* })
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* 2. **Dynamic** — pass raw JSON Schema as `jsonSchema`. Use this when the
|
|
82
|
-
* schema comes from an external source (MCP server, plugin manifest,
|
|
83
|
-
* dynamic config) and is not known at compile time. Inputs are typed as
|
|
84
|
-
* `unknown`; the handler is responsible for any validation it needs.
|
|
85
|
-
*
|
|
86
|
-
* ```ts
|
|
87
|
-
* Tool.make({
|
|
88
|
-
* description: "Look something up",
|
|
89
|
-
* jsonSchema: { type: "object", properties: { ... } },
|
|
90
|
-
* execute: (params) => Effect.succeed(...),
|
|
91
|
-
* })
|
|
92
|
-
* ```
|
|
93
|
-
*
|
|
94
|
-
* In both modes the produced tool flows through `toDefinitions(...)`
|
|
95
|
-
* identically.
|
|
63
|
+
* Options for `tool`, where `Execute` is the only part that differs between the Effect and Promise clients. Pass
|
|
64
|
+
* Effect `parameters` (and optionally a `success` schema that validates the result, otherwise it is sent as is with
|
|
65
|
+
* `undefined` as `null`), or raw `jsonSchema` known only at runtime, such as an MCP tool's. Omit `execute` to declare
|
|
66
|
+
* a tool the model can call without running it here.
|
|
96
67
|
*/
|
|
97
|
-
export
|
|
68
|
+
export type ToolOptions<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>, Execute = ToolExecute<Parameters, Success>> = {
|
|
98
69
|
readonly description: string;
|
|
99
|
-
readonly parameters: Parameters;
|
|
100
|
-
readonly success: Success;
|
|
101
70
|
readonly freeform?: FreeformOption<Parameters>;
|
|
102
|
-
readonly execute
|
|
71
|
+
readonly execute?: Execute;
|
|
103
72
|
readonly toModelOutput?: ToolToModelOutput<Parameters, Success>;
|
|
104
73
|
readonly toStructuredOutput?: (output: Success["Encoded"]) => unknown;
|
|
105
|
-
}
|
|
106
|
-
export declare function make<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>>(config: {
|
|
107
|
-
readonly description: string;
|
|
74
|
+
} & ({
|
|
108
75
|
readonly parameters: Parameters;
|
|
109
|
-
readonly success
|
|
110
|
-
|
|
111
|
-
readonly execute?: undefined;
|
|
112
|
-
readonly toModelOutput?: ToolToModelOutput<Parameters, Success>;
|
|
113
|
-
readonly toStructuredOutput?: (output: Success["Encoded"]) => unknown;
|
|
114
|
-
}): Definition<Parameters, Success>;
|
|
115
|
-
export declare function make(config: {
|
|
116
|
-
readonly description: string;
|
|
117
|
-
readonly jsonSchema: JsonSchema.JsonSchema;
|
|
118
|
-
readonly outputSchema?: JsonSchema.JsonSchema;
|
|
119
|
-
readonly freeform?: Freeform;
|
|
120
|
-
readonly execute: (params: unknown, context?: ToolExecuteContext) => Effect.Effect<unknown, ToolFailure>;
|
|
121
|
-
readonly toModelOutput?: (input: ToolModelOutputInput<unknown, unknown>) => ReadonlyArray<Tool.Content>;
|
|
122
|
-
readonly toStructuredOutput?: (output: unknown) => unknown;
|
|
123
|
-
}): AnyExecutableTool;
|
|
124
|
-
export declare function make(config: {
|
|
125
|
-
readonly description: string;
|
|
76
|
+
readonly success?: Success;
|
|
77
|
+
} | {
|
|
126
78
|
readonly jsonSchema: JsonSchema.JsonSchema;
|
|
127
79
|
readonly outputSchema?: JsonSchema.JsonSchema;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* Constructs a tool whose `execute` returns an Effect that fails with `ToolFailure`.
|
|
83
|
+
*
|
|
84
|
+
* ```ts
|
|
85
|
+
* tool({
|
|
86
|
+
* description: "Get current weather",
|
|
87
|
+
* parameters: Schema.Struct({ city: Schema.String }),
|
|
88
|
+
* success: Schema.Struct({ temperature: Schema.Number }),
|
|
89
|
+
* execute: ({ city }) => Effect.succeed({ temperature: 22 }),
|
|
90
|
+
* })
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare const tool: <Parameters extends ToolSchema<any> = typeof Schema.Unknown, Success extends ToolSchema<any> = typeof Schema.Unknown, Result extends Effect.Effect<Schema.Schema.Type<Success>, ToolFailure> | NoExecute = NoExecute>(config: ToolOptions<Parameters, Success, (params: Schema.Schema.Type<Parameters>, context?: ToolExecuteContext) => Result>) => 0 extends 1 & Result ? ExecutableTool<Parameters, Success> : [Result, NoExecute] extends [NoExecute, Result] ? Tool<Parameters, Success> : ExecutableTool<Parameters, Success>;
|
|
94
|
+
declare const noExecute: unique symbol;
|
|
95
|
+
/** @internal The inferred `execute` result when `execute` is omitted; no function can return it. */
|
|
96
|
+
export type NoExecute = {
|
|
97
|
+
readonly [noExecute]: never;
|
|
98
|
+
};
|
|
99
|
+
/** @internal Builds a tool for both clients from its options and an Effect `execute`. */
|
|
100
|
+
export declare function build<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>, Result>(config: ToolOptions<Parameters, Success, (...args: never) => Result>, execute: ((params: Schema.Schema.Type<Parameters>, context?: ToolExecuteContext) => unknown) | undefined): 0 extends 1 & Result ? ExecutableTool<Parameters, Success> : [Result, NoExecute] extends [NoExecute, Result] ? Tool<Parameters, Success> : ExecutableTool<Parameters, Success>;
|
|
133
101
|
/**
|
|
134
102
|
* A record of named tools. The record key becomes the tool name on the wire.
|
|
135
103
|
*/
|
|
136
|
-
export type Tools = Record<string,
|
|
104
|
+
export type Tools = Record<string, Tool>;
|
|
137
105
|
/**
|
|
138
106
|
* Convert a tools record into the `ToolDefinition[]` shape that
|
|
139
107
|
* `LLMRequest.tools` expects.
|
|
@@ -154,4 +122,3 @@ export declare const toDefinitions: (tools: Tools) => ReadonlyArray<ToolDefiniti
|
|
|
154
122
|
*/
|
|
155
123
|
export declare const emptyInputJsonSchema: (schema: Schema.Top) => JsonSchema.JsonSchema | undefined;
|
|
156
124
|
export { ToolFailure };
|
|
157
|
-
export * as Tool from "./tool.js";
|
package/dist/tool.js
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
import { Effect, JsonSchema, Schema } from "effect";
|
|
2
|
-
import { Tool } from "@opencode/schema/tool";
|
|
3
2
|
import { ToolDefinition, ToolFailure, ToolOutput } from "./schema/index.js";
|
|
4
3
|
import { isRecord } from "./utils/record.js";
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Constructs a tool whose `execute` returns an Effect that fails with `ToolFailure`.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* tool({
|
|
9
|
+
* description: "Get current weather",
|
|
10
|
+
* parameters: Schema.Struct({ city: Schema.String }),
|
|
11
|
+
* success: Schema.Struct({ temperature: Schema.Number }),
|
|
12
|
+
* execute: ({ city }) => Effect.succeed({ temperature: 22 }),
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const tool = (config) => build(config, config.execute);
|
|
17
|
+
export function build(config, execute) {
|
|
18
|
+
const shared = {
|
|
19
|
+
description: config.description,
|
|
20
|
+
execute,
|
|
21
|
+
toModelOutput: config.toModelOutput,
|
|
22
|
+
toStructuredOutput: config.toStructuredOutput,
|
|
23
|
+
_project: (parameters, id, output) => project(config.toModelOutput, config.toStructuredOutput, parameters, id, output),
|
|
24
|
+
};
|
|
25
|
+
if ("jsonSchema" in config)
|
|
7
26
|
return {
|
|
8
|
-
|
|
27
|
+
...shared,
|
|
9
28
|
parameters: Schema.Unknown,
|
|
10
29
|
success: Schema.Unknown,
|
|
11
|
-
execute: config.execute,
|
|
12
|
-
toModelOutput: config.toModelOutput,
|
|
13
|
-
toStructuredOutput: config.toStructuredOutput,
|
|
14
30
|
_decode: Effect.succeed,
|
|
15
31
|
_encode: Effect.succeed,
|
|
16
|
-
_project: (parameters, id, output) => project(config.toModelOutput, config.toStructuredOutput, parameters, id, output),
|
|
17
32
|
_legacyResult: config.toModelOutput === undefined && config.toStructuredOutput === undefined,
|
|
18
33
|
_definition: definition({
|
|
19
34
|
name: "",
|
|
@@ -23,23 +38,18 @@ export function make(config) {
|
|
|
23
38
|
freeform: config.freeform,
|
|
24
39
|
}),
|
|
25
40
|
};
|
|
26
|
-
}
|
|
27
41
|
return {
|
|
28
|
-
|
|
42
|
+
...shared,
|
|
29
43
|
parameters: config.parameters,
|
|
30
|
-
success: config.success,
|
|
31
|
-
execute: config.execute,
|
|
32
|
-
toModelOutput: config.toModelOutput,
|
|
33
|
-
toStructuredOutput: config.toStructuredOutput,
|
|
44
|
+
success: config.success ?? Schema.Unknown,
|
|
34
45
|
_decode: Schema.decodeUnknownEffect(config.parameters),
|
|
35
|
-
_encode: Schema.encodeEffect(config.success),
|
|
36
|
-
_project: (parameters, id, output) => project(config.toModelOutput, config.toStructuredOutput, parameters, id, output),
|
|
46
|
+
_encode: config.success ? Schema.encodeEffect(config.success) : (value) => Effect.succeed(value ?? null),
|
|
37
47
|
_legacyResult: false,
|
|
38
48
|
_definition: definition({
|
|
39
49
|
name: "",
|
|
40
50
|
description: config.description,
|
|
41
51
|
inputSchema: emptyInputJsonSchema(config.parameters) ?? toJsonSchema(config.parameters),
|
|
42
|
-
outputSchema: toJsonSchema(config.success),
|
|
52
|
+
outputSchema: config.success && toJsonSchema(config.success),
|
|
43
53
|
freeform: config.freeform,
|
|
44
54
|
}),
|
|
45
55
|
};
|
|
@@ -90,4 +100,3 @@ const toJsonSchema = (schema) => {
|
|
|
90
100
|
};
|
|
91
101
|
const project = (toModelOutput, toStructuredOutput, parameters, id, output) => ToolOutput.make(toStructuredOutput?.(output) ?? output, toModelOutput?.({ id, parameters, output }) ?? (typeof output === "string" ? [{ type: "text", text: output }] : []));
|
|
92
102
|
export { ToolFailure };
|
|
93
|
-
export * as Tool from "./tool.js";
|
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-329",
|
|
4
4
|
"name": "@opencode/ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -516,7 +516,7 @@
|
|
|
516
516
|
"devDependencies": {
|
|
517
517
|
"@clack/prompts": "1.0.0-alpha.1",
|
|
518
518
|
"@effect/platform-node": "4.0.6",
|
|
519
|
-
"@opencode/http-recorder": "0.0.0-dev-
|
|
519
|
+
"@opencode/http-recorder": "0.0.0-dev-329",
|
|
520
520
|
"@tsconfig/bun": "1.0.9",
|
|
521
521
|
"@types/bun": "1.4.3",
|
|
522
522
|
"@typescript/native": "npm:typescript@7.1.0-beta",
|
|
@@ -526,7 +526,7 @@
|
|
|
526
526
|
"@aws-sdk/credential-providers": "3.1057.0",
|
|
527
527
|
"@smithy/eventstream-codec": "4.2.14",
|
|
528
528
|
"@smithy/util-utf8": "4.2.2",
|
|
529
|
-
"@opencode/schema": "0.0.0-dev-
|
|
529
|
+
"@opencode/schema": "0.0.0-dev-329",
|
|
530
530
|
"aws4fetch": "1.0.20",
|
|
531
531
|
"effect": "4.0.6",
|
|
532
532
|
"google-auth-library": "10.5.0"
|