@opencode/ai 0.0.0-dev-291 → 0.0.0-dev-306

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.
Files changed (40) hide show
  1. package/README.md +6 -6
  2. package/dist/effect.d.ts +2 -2
  3. package/dist/effect.js +1 -1
  4. package/dist/index.d.ts +10 -13
  5. package/dist/index.js +10 -15
  6. package/dist/llm.d.ts +2 -2
  7. package/dist/llm.js +3 -12
  8. package/dist/protocols/alibaba-responses.d.ts +2 -2
  9. package/dist/protocols/assemblyai-transcription.js +3 -3
  10. package/dist/protocols/meta-responses.d.ts +4 -4
  11. package/dist/protocols/open-responses.d.ts +18 -18
  12. package/dist/protocols/open-responses.js +4 -2
  13. package/dist/protocols/openai-compatible-responses.d.ts +2 -2
  14. package/dist/protocols/openai-responses.d.ts +12 -12
  15. package/dist/protocols/runway-video.js +2 -2
  16. package/dist/protocols/xai-responses.d.ts +2 -2
  17. package/dist/protocols/xai-video.js +2 -2
  18. package/dist/providers/alibaba.d.ts +2 -2
  19. package/dist/providers/amazon-bedrock-mantle.d.ts +2 -2
  20. package/dist/providers/azure.d.ts +4 -4
  21. package/dist/providers/cloudflare-ai-gateway.d.ts +4 -4
  22. package/dist/providers/digitalocean.d.ts +2 -2
  23. package/dist/providers/google-vertex-responses.d.ts +2 -2
  24. package/dist/providers/meta.d.ts +2 -2
  25. package/dist/providers/minimax.d.ts +2 -2
  26. package/dist/providers/moonshot.d.ts +2 -2
  27. package/dist/providers/openai-compatible-responses.d.ts +2 -2
  28. package/dist/providers/openai.d.ts +2 -2
  29. package/dist/providers/openrouter/responses.d.ts +1 -2
  30. package/dist/providers/openrouter.d.ts +36 -23
  31. package/dist/providers/openrouter.js +29 -10
  32. package/dist/providers/venice.d.ts +2 -2
  33. package/dist/providers/zai-coding-plan.d.ts +2 -2
  34. package/dist/route/media-protocol.d.ts +2 -3
  35. package/dist/route/media-protocol.js +2 -6
  36. package/dist/testing.d.ts +1 -1
  37. package/dist/testing.js +1 -1
  38. package/dist/tool.d.ts +33 -71
  39. package/dist/tool.js +27 -18
  40. package/package.json +3 -3
package/dist/tool.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect, JsonSchema, Schema } from "effect";
2
- import { Tool } from "@opencode/schema/tool";
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<Tool.Content>;
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` when the parameters are exactly one required string field, otherwise `undefined`. */
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 Definition<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> {
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,48 @@ export interface Definition<Parameters extends ToolSchema<any>, Success extends
57
55
  /** @internal */
58
56
  readonly _definition: ToolDefinitionClass;
59
57
  }
60
- export type AnyTool = Definition<any, any>;
61
- export type ExecutableTool<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>> = Definition<Parameters, Success> & {
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
- * Constructs a tool. Two input modes:
68
- *
69
- * 1. **Typed** — pass Effect `parameters` and `success` Schemas; inputs and
70
- * outputs are statically typed and decoded/encoded automatically.
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 declare function make<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>>(config: {
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: ToolExecute<Parameters, Success>;
71
+ readonly execute?: Execute;
103
72
  readonly toModelOutput?: ToolToModelOutput<Parameters, Success>;
104
73
  readonly toStructuredOutput?: (output: Success["Encoded"]) => unknown;
105
- }): ExecutableTool<Parameters, Success>;
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: Success;
110
- readonly freeform?: FreeformOption<Parameters>;
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
- readonly freeform?: Freeform;
129
- readonly execute?: undefined;
130
- readonly toModelOutput?: (input: ToolModelOutputInput<unknown, unknown>) => ReadonlyArray<Tool.Content>;
131
- readonly toStructuredOutput?: (output: unknown) => unknown;
132
- }): AnyTool;
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> = never>(config: ToolOptions<Parameters, Success, (params: Schema.Schema.Type<Parameters>, context?: ToolExecuteContext) => Result>) => ((params: Schema.Schema.Type<Parameters>, context?: ToolExecuteContext) => Result) extends infer T ? T extends (params: Schema.Schema.Type<Parameters>, context?: ToolExecuteContext) => Result ? T extends (...args: never) => never ? Tool<Parameters, Success> : ExecutableTool<Parameters, Success> : never : never;
94
+ /** @internal Builds a tool for both clients from its options and an Effect `execute`; no `execute` infers `never`. */
95
+ export declare function build<Parameters extends ToolSchema<any>, Success extends ToolSchema<any>, Execute>(config: ToolOptions<Parameters, Success, Execute>, execute: ToolExecute<Parameters, ToolSchema<unknown>> | undefined): Execute extends (...args: never) => never ? Tool<Parameters, Success> : ExecutableTool<Parameters, Success>;
133
96
  /**
134
97
  * A record of named tools. The record key becomes the tool name on the wire.
135
98
  */
136
- export type Tools = Record<string, AnyTool>;
99
+ export type Tools = Record<string, Tool>;
137
100
  /**
138
101
  * Convert a tools record into the `ToolDefinition[]` shape that
139
102
  * `LLMRequest.tools` expects.
@@ -154,4 +117,3 @@ export declare const toDefinitions: (tools: Tools) => ReadonlyArray<ToolDefiniti
154
117
  */
155
118
  export declare const emptyInputJsonSchema: (schema: Schema.Top) => JsonSchema.JsonSchema | undefined;
156
119
  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
- export function make(config) {
6
- if ("jsonSchema" in config) {
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
- description: config.description,
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
- description: config.description,
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-291",
3
+ "version": "0.0.0-dev-306",
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-291",
519
+ "@opencode/http-recorder": "0.0.0-dev-306",
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-291",
529
+ "@opencode/schema": "0.0.0-dev-306",
530
530
  "aws4fetch": "1.0.20",
531
531
  "effect": "4.0.6",
532
532
  "google-auth-library": "10.5.0"