@opencode/codemode 2.2.7 → 2.3.0
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 +11 -13
- package/dist/{vm/async.d.ts → async.d.ts} +1 -1
- package/dist/codemode.d.ts +8 -22
- package/dist/codemode.js +17 -38
- package/dist/data.d.ts +4 -19
- package/dist/data.js +1 -82
- package/dist/extension.d.ts +8 -2
- package/dist/extension.js +9 -0
- package/dist/{vm/host.d.ts → host.d.ts} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
- package/dist/{vm/prelude → prelude}/helpers.js +1 -1
- package/dist/{vm/runtime → runtime}/driver.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/driver.js +1 -1
- package/dist/{vm/runtime → runtime}/errors.js +2 -2
- package/dist/{vm/runtime → runtime}/execute.d.ts +17 -8
- package/dist/{vm/runtime → runtime}/execute.js +11 -9
- package/dist/{vm/runtime → runtime}/extensions.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/extensions.js +39 -4
- package/dist/{vm/runtime → runtime}/globals.js +1 -1
- package/dist/{vm/runtime → runtime}/interpreter.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.js +1 -1
- package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/operations.js +1 -1
- package/dist/scan.js +2 -2
- package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +1 -1
- package/dist/{vm/snapshot → snapshot}/codec.d.ts +6 -5
- package/dist/{vm/snapshot → snapshot}/codec.js +12 -5
- package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +8 -5
- package/dist/{vm/snapshot → snapshot}/snapshot.js +14 -3
- package/dist/{vm/snapshot.js → snapshot.js} +1 -1
- package/dist/stdlib/array.d.ts +14 -9
- package/dist/stdlib/array.js +89 -276
- package/dist/stdlib/bytes.d.ts +11 -7
- package/dist/stdlib/bytes.js +112 -79
- package/dist/stdlib/collections.d.ts +7 -7
- package/dist/stdlib/collections.js +30 -339
- package/dist/stdlib/console.d.ts +2 -2
- package/dist/stdlib/console.js +3 -3
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +21 -30
- package/dist/stdlib/headers.d.ts +2 -2
- package/dist/stdlib/headers.js +15 -33
- package/dist/stdlib/iterator.d.ts +2 -2
- package/dist/stdlib/iterator.js +10 -302
- package/dist/stdlib/json.d.ts +2 -2
- package/dist/stdlib/json.js +15 -31
- package/dist/stdlib/math.d.ts +2 -7
- package/dist/stdlib/math.js +6 -30
- package/dist/stdlib/number.d.ts +3 -3
- package/dist/stdlib/number.js +8 -8
- package/dist/stdlib/object.d.ts +4 -4
- package/dist/stdlib/object.js +12 -30
- package/dist/stdlib/regexp.d.ts +6 -5
- package/dist/stdlib/regexp.js +18 -13
- package/dist/stdlib/string.d.ts +2 -2
- package/dist/stdlib/string.js +53 -53
- package/dist/stdlib/url.d.ts +21 -9
- package/dist/stdlib/url.js +55 -55
- package/dist/stdlib/value.d.ts +3 -4
- package/dist/stdlib/value.js +5 -6
- package/dist/stdlib/web.d.ts +4 -4
- package/dist/stdlib/web.js +7 -7
- package/dist/tool-runtime.d.ts +14 -13
- package/dist/tool-runtime.js +245 -53
- package/dist/tool-schema.d.ts +5 -0
- package/dist/tool-schema.js +36 -16
- package/dist/tool.d.ts +24 -9
- package/dist/tool.js +15 -22
- package/dist/{vm/vm.d.ts → vm.d.ts} +3 -3
- package/dist/{vm/vm.js → vm.js} +1 -1
- package/package.json +3 -5
- package/dist/interpreter/callback.d.ts +0 -35
- package/dist/interpreter/callback.js +0 -98
- package/dist/interpreter/errors.js +0 -173
- package/dist/interpreter/execute.d.ts +0 -14
- package/dist/interpreter/execute.js +0 -207
- package/dist/interpreter/extensions.d.ts +0 -9
- package/dist/interpreter/extensions.js +0 -248
- package/dist/interpreter/generators.d.ts +0 -3
- package/dist/interpreter/generators.js +0 -168
- package/dist/interpreter/globals.js +0 -144
- package/dist/interpreter/interpreter.d.ts +0 -48
- package/dist/interpreter/interpreter.js +0 -2253
- package/dist/interpreter/intrinsics.d.ts +0 -15
- package/dist/interpreter/intrinsics.js +0 -105
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/native.d.ts +0 -17
- package/dist/interpreter/native.js +0 -35
- package/dist/interpreter/objects.d.ts +0 -301
- package/dist/interpreter/objects.js +0 -592
- package/dist/interpreter/promises.d.ts +0 -18
- package/dist/interpreter/promises.js +0 -292
- package/dist/interpreter/runner.d.ts +0 -178
- package/dist/interpreter/runner.js +0 -480
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/suspend-error.d.ts +0 -24
- package/dist/suspend-error.js +0 -25
- package/dist/vm/data.d.ts +0 -7
- package/dist/vm/data.js +0 -13
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
- package/dist/vm/runtime/errors.d.ts +0 -23
- package/dist/vm/runtime/globals.d.ts +0 -6
- package/dist/vm/runtime/limits.d.ts +0 -10
- package/dist/vm/runtime/limits.js +0 -19
- package/dist/vm/runtime/modules.d.ts +0 -4
- package/dist/vm/runtime/modules.js +0 -33
- package/dist/vm/runtime/references.d.ts +0 -8
- package/dist/vm/runtime/references.js +0 -51
- package/dist/vm/stdlib/array.d.ts +0 -16
- package/dist/vm/stdlib/array.js +0 -311
- package/dist/vm/stdlib/bytes.d.ts +0 -11
- package/dist/vm/stdlib/bytes.js +0 -222
- package/dist/vm/stdlib/collections.d.ts +0 -7
- package/dist/vm/stdlib/collections.js +0 -188
- package/dist/vm/stdlib/console.d.ts +0 -6
- package/dist/vm/stdlib/console.js +0 -93
- package/dist/vm/stdlib/date.d.ts +0 -2
- package/dist/vm/stdlib/date.js +0 -123
- package/dist/vm/stdlib/headers.d.ts +0 -2
- package/dist/vm/stdlib/headers.js +0 -92
- package/dist/vm/stdlib/iterator.d.ts +0 -2
- package/dist/vm/stdlib/iterator.js +0 -30
- package/dist/vm/stdlib/json.d.ts +0 -3
- package/dist/vm/stdlib/json.js +0 -42
- package/dist/vm/stdlib/math.d.ts +0 -3
- package/dist/vm/stdlib/math.js +0 -73
- package/dist/vm/stdlib/number.d.ts +0 -3
- package/dist/vm/stdlib/number.js +0 -72
- package/dist/vm/stdlib/object.d.ts +0 -6
- package/dist/vm/stdlib/object.js +0 -201
- package/dist/vm/stdlib/regexp.d.ts +0 -8
- package/dist/vm/stdlib/regexp.js +0 -119
- package/dist/vm/stdlib/string.d.ts +0 -2
- package/dist/vm/stdlib/string.js +0 -272
- package/dist/vm/stdlib/url.d.ts +0 -25
- package/dist/vm/stdlib/url.js +0 -255
- package/dist/vm/stdlib/value.d.ts +0 -6
- package/dist/vm/stdlib/value.js +0 -32
- package/dist/vm/stdlib/web.d.ts +0 -5
- package/dist/vm/stdlib/web.js +0 -97
- /package/dist/{vm/async.js → async.js} +0 -0
- /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
- /package/dist/{vm/code.js → code.js} +0 -0
- /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/compile.js +0 -0
- /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/scope.js +0 -0
- /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
- /package/dist/{vm/generators.js → generators.js} +0 -0
- /package/dist/{vm/host.js → host.js} +0 -0
- /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
- /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
- /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
- /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
- /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/callback.js +0 -0
- /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.js +0 -0
- /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/interpreter.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
- /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
- /package/dist/{interpreter → runtime}/limits.js +0 -0
- /package/dist/{vm/runtime → runtime}/model.js +0 -0
- /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
- /package/dist/{interpreter → runtime}/modules.js +0 -0
- /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/native.js +0 -0
- /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/promises.js +0 -0
- /package/dist/{interpreter → runtime}/references.d.ts +0 -0
- /package/dist/{interpreter → runtime}/references.js +0 -0
- /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/signals.js +0 -0
- /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/table.js +0 -0
- /package/dist/{vm/scheduler.js → scheduler.js} +0 -0
- /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
package/dist/tool-runtime.d.ts
CHANGED
|
@@ -2,14 +2,11 @@ import { Effect } from "effect";
|
|
|
2
2
|
import type { Json } from "./data.js";
|
|
3
3
|
import type { DiagnosticKind } from "./codemode.js";
|
|
4
4
|
import { type Namespace } from "./namespace.js";
|
|
5
|
-
import { type Durable, type Idempotent, type Tool
|
|
5
|
+
import { type Durable, type Idempotent, type Tool } from "./tool.js";
|
|
6
6
|
import type { Tools } from "./tools.js";
|
|
7
7
|
export declare const compareText: (left: string, right: string) => -1 | 0 | 1;
|
|
8
8
|
export type Services<T> = ServicesOf<T, []>;
|
|
9
|
-
type ServicesOf<T, Depth extends ReadonlyArray<unknown>> = Depth["length"] extends 8 ? never : T extends
|
|
10
|
-
readonly _tag: "CodeModeTool";
|
|
11
|
-
readonly execute: (input: unknown, context?: ToolContext) => Effect.Effect<unknown, unknown, infer R>;
|
|
12
|
-
} ? R : T extends object ? string extends keyof T ? ServicesOf<T[string], [...Depth, unknown]> : ServicesOf<T[keyof T], [...Depth, unknown]> : never;
|
|
9
|
+
type ServicesOf<T, Depth extends ReadonlyArray<unknown>> = Depth["length"] extends 8 ? never : T extends Tool<infer R> ? R : T extends object ? string extends keyof T ? ServicesOf<T[string], [...Depth, unknown]> : ServicesOf<T[keyof T], [...Depth, unknown]> : never;
|
|
13
10
|
export type ToolCall = {
|
|
14
11
|
readonly name: string;
|
|
15
12
|
};
|
|
@@ -71,10 +68,14 @@ export type Prepared<R = never> = {
|
|
|
71
68
|
};
|
|
72
69
|
export type SearchEntry = {
|
|
73
70
|
readonly description: ToolDescription;
|
|
74
|
-
/**
|
|
75
|
-
readonly
|
|
76
|
-
|
|
71
|
+
/** Position in the full search index, for scoring through the word index. */
|
|
72
|
+
readonly position: number;
|
|
73
|
+
/** The tool's searchable text by field, read when the word index is built. */
|
|
74
|
+
readonly text: () => Readonly<Record<SearchField, ReadonlyArray<string>>>;
|
|
75
|
+
/** Words in the tool's own name (the last path segment). */
|
|
76
|
+
readonly nameLength: number;
|
|
77
77
|
};
|
|
78
|
+
type SearchField = "namespace" | "name" | "description" | "parameters" | "namespaceDescription";
|
|
78
79
|
/** Exact callable signature of the built-in `search` function, for host-owned instructions. */
|
|
79
80
|
export declare const searchSignature: string;
|
|
80
81
|
export declare const prepare: <R>(tools: Tools<R>) => Prepared<R>;
|
|
@@ -84,8 +85,8 @@ export declare class ToolRuntimeError extends Error {
|
|
|
84
85
|
constructor(kind: Extract<DiagnosticKind, "UnknownTool" | "InvalidToolInput" | "InvalidToolOutput" | "ToolCallLimitExceeded">, message: string, suggestions?: ReadonlyArray<string>);
|
|
85
86
|
}
|
|
86
87
|
export type ExecuteCallOptions = {
|
|
87
|
-
readonly key
|
|
88
|
-
readonly admit
|
|
88
|
+
readonly key: string;
|
|
89
|
+
readonly admit: (name: string) => void;
|
|
89
90
|
readonly detaching?: (() => boolean) | undefined;
|
|
90
91
|
};
|
|
91
92
|
/** The tool bridge of one execution. Arguments arrive and results leave as JSON; program values never enter. */
|
|
@@ -94,13 +95,13 @@ export type ToolRuntime<R = never> = {
|
|
|
94
95
|
readonly hooks: Hooks<R>;
|
|
95
96
|
readonly invocations: Map<string, ToolInvocation>;
|
|
96
97
|
readonly record: (name: string) => void;
|
|
97
|
-
readonly execute: (path: ReadonlyArray<string>, args: Array<Json | undefined>, options
|
|
98
|
-
readonly search: (args: Array<Json | undefined>, options
|
|
98
|
+
readonly execute: (path: ReadonlyArray<string>, args: Array<Json | undefined>, options: ExecuteCallOptions) => Effect.Effect<Json | undefined, unknown, R>;
|
|
99
|
+
readonly search: (args: Array<Json | undefined>, options: ExecuteCallOptions) => Effect.Effect<Json | undefined, unknown, R>;
|
|
99
100
|
readonly keys: (path: ReadonlyArray<string>) => ReadonlyArray<string>;
|
|
100
101
|
/** How the tool at `path` executes on suspend (`undefined` when unknown or plain). */
|
|
101
102
|
readonly mode: (path: ReadonlyArray<string>) => Idempotent<unknown, unknown, R> | Durable<unknown, unknown, R> | undefined;
|
|
102
103
|
/** Admits a call to a durable tool, runs `tool.before`, and starts its work under `key`: its JSON data. */
|
|
103
|
-
readonly start: (path: ReadonlyArray<string>, args: Array<Json | undefined>, key: string, admit
|
|
104
|
+
readonly start: (path: ReadonlyArray<string>, args: Array<Json | undefined>, key: string, admit: (name: string) => void) => Effect.Effect<Json, unknown, R>;
|
|
104
105
|
/**
|
|
105
106
|
* Waits for a durable tool's running work: its output, as `execute` gives it. `detaching` says whether an
|
|
106
107
|
* interruption detaches to suspend the run, which `tool.after` doesn't see, or ends the call, which it sees as
|
package/dist/tool-runtime.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cause, Effect, Exit, Formatter, Schema } from "effect";
|
|
2
2
|
import { toolError } from "./tool-error.js";
|
|
3
|
-
import { decodeInput as decodeToolInput, decodeOutput as decodeToolOutput, identifierSegment, inputProperties, inputTypeScript, isEmptyInput, outputTypeScript, } from "./tool-schema.js";
|
|
3
|
+
import { decodeInput as decodeToolInput, decodeOutput as decodeToolOutput, identifierSegment, inputProperties, inputTypeScript, isEmptyInput, outputTypeScript, stringParameter, } from "./tool-schema.js";
|
|
4
4
|
import { isNamespace } from "./namespace.js";
|
|
5
5
|
import { isTool } from "./tool.js";
|
|
6
6
|
export const compareText = (left, right) => (left < right ? -1 : left > right ? 1 : 0);
|
|
@@ -92,40 +92,210 @@ const describeTool = (visible) => {
|
|
|
92
92
|
// Joining the final fragments avoids retaining the rendering's intermediate string ropes in JSC.
|
|
93
93
|
return (signature ??= [
|
|
94
94
|
toolExpression(visible.path),
|
|
95
|
-
|
|
95
|
+
stringParameter(visible.tool)?.signature ??
|
|
96
|
+
(isEmptyInput(visible.tool) ? "()" : `(${inputTypeScript(visible.tool, true)})`),
|
|
96
97
|
`: Promise<${outputTypeScript(visible.tool, true)}>`,
|
|
97
98
|
].join(""));
|
|
98
99
|
},
|
|
99
100
|
};
|
|
100
101
|
};
|
|
101
|
-
|
|
102
|
+
// Standard field-weighted BM25 (k1 1.2, b 0.75) plus a preference for short names. Compared against other rankers on
|
|
103
|
+
// model-written and real search queries over an ~11k-tool catalog (anomalyco/evals, dumber/evals/tool-search).
|
|
104
|
+
const fieldWeights = {
|
|
105
|
+
name: 3,
|
|
106
|
+
namespace: 2,
|
|
107
|
+
description: 1,
|
|
108
|
+
namespaceDescription: 0.4,
|
|
109
|
+
parameters: 0.3,
|
|
110
|
+
};
|
|
111
|
+
const searchFields = Object.keys(fieldWeights);
|
|
112
|
+
const k1 = 1.2;
|
|
113
|
+
const lengthNormalization = 0.75;
|
|
114
|
+
/** score × (1 + brevityBoost / words in the tool name): "users" prefers `list_users` over `list_user_groups`. */
|
|
115
|
+
const brevityBoost = 0.5;
|
|
116
|
+
/** A query word of 4+ letters also matches longer words it starts with, at this weight: `term` → `terminal`. */
|
|
117
|
+
const prefixWeight = 0.3;
|
|
118
|
+
/** Words split at case changes and non-alphanumerics: `listUserGroups` → list, user, groups. */
|
|
119
|
+
const splitWords = (text) => text
|
|
102
120
|
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
103
121
|
.toLowerCase()
|
|
104
122
|
.split(/[^a-z0-9]+/)
|
|
105
|
-
.filter((
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
.filter((word) => word.length > 0);
|
|
124
|
+
/** Plural endings only, applied the same everywhere: `settings` → `setting`, `statuses` → `status`, `vms` → `vm`. */
|
|
125
|
+
const stem = (word) => {
|
|
126
|
+
if (word.length > 4 && word.endsWith("ies"))
|
|
127
|
+
return word.slice(0, -3) + "y";
|
|
128
|
+
if (word.length > 4 && /(?:ss|sh|ch|x|z|us)es$/.test(word))
|
|
129
|
+
return word.slice(0, -2);
|
|
130
|
+
if (word.length > 2 && word.endsWith("s") && !/(?:ss|us|is)$/.test(word))
|
|
131
|
+
return word.slice(0, -1);
|
|
132
|
+
return word;
|
|
133
|
+
};
|
|
134
|
+
/** Searchable words of a text: its split words, plus each camelCase word whole (`CronJob` → cron, job, cronjob). */
|
|
135
|
+
const searchWords = (text) => splitWords(text)
|
|
136
|
+
.concat((text.match(/[A-Za-z0-9]*[a-z0-9][A-Z][A-Za-z0-9]*/g) ?? []).map((word) => word.toLowerCase()))
|
|
137
|
+
.map(stem);
|
|
138
|
+
const fieldWords = (text, cache) => {
|
|
139
|
+
const cached = cache.get(text);
|
|
140
|
+
if (cached)
|
|
141
|
+
return cached;
|
|
142
|
+
const words = searchWords(text);
|
|
143
|
+
cache.set(text, words);
|
|
144
|
+
return words;
|
|
145
|
+
};
|
|
146
|
+
// Built once per search index on the first query.
|
|
147
|
+
const wordIndexes = new WeakMap();
|
|
148
|
+
// Locations with the same tools (global MCP servers) share one word index, keyed by a hash of the indexed text. An
|
|
149
|
+
// index lives while some search index uses it; once none does it can be collected and its key is removed. A hash
|
|
150
|
+
// collision (about 1 in 37 trillion with 1,000 distinct catalogs alive) would only misrank results: they still come
|
|
151
|
+
// from the caller's own tools, and the tool count in the key keeps positions in range.
|
|
152
|
+
const shared = typeof WeakRef === "undefined" || typeof FinalizationRegistry === "undefined"
|
|
153
|
+
? undefined
|
|
154
|
+
: {
|
|
155
|
+
indexes: new Map(),
|
|
156
|
+
cleanup: new FinalizationRegistry(({ key, ref }) => {
|
|
157
|
+
// A newer index may have been stored under the same key since this one was dropped.
|
|
158
|
+
if (shared?.indexes.get(key) === ref)
|
|
159
|
+
shared.indexes.delete(key);
|
|
160
|
+
}),
|
|
161
|
+
};
|
|
162
|
+
const wordIndexFor = (index) => {
|
|
163
|
+
const cached = wordIndexes.get(index);
|
|
164
|
+
if (cached)
|
|
165
|
+
return cached;
|
|
166
|
+
const texts = index.map((entry) => entry.text());
|
|
167
|
+
const key = shared ? textKey(texts) : undefined;
|
|
168
|
+
const reused = key === undefined ? undefined : shared?.indexes.get(key)?.deref();
|
|
169
|
+
if (reused) {
|
|
170
|
+
wordIndexes.set(index, reused);
|
|
171
|
+
return reused;
|
|
172
|
+
}
|
|
173
|
+
const built = buildWordIndex(texts);
|
|
174
|
+
wordIndexes.set(index, built);
|
|
175
|
+
if (shared && key !== undefined) {
|
|
176
|
+
const ref = new WeakRef(built);
|
|
177
|
+
shared.indexes.set(key, ref);
|
|
178
|
+
shared.cleanup.register(built, { key, ref });
|
|
179
|
+
}
|
|
180
|
+
return built;
|
|
181
|
+
};
|
|
182
|
+
/** Tool count plus two independent 32-bit FNV-1a hashes of every indexed field, in index order. */
|
|
183
|
+
const textKey = (texts) => {
|
|
184
|
+
let first = 0x811c9dc5;
|
|
185
|
+
let second = 0x9e3779b9;
|
|
186
|
+
const add = (text) => {
|
|
187
|
+
for (let i = 0; i < text.length; i++) {
|
|
188
|
+
const code = text.charCodeAt(i);
|
|
189
|
+
first = Math.imul(first ^ code, 0x01000193);
|
|
190
|
+
second = Math.imul(second ^ code, 0x5bd1e995);
|
|
191
|
+
second ^= second >>> 15;
|
|
192
|
+
}
|
|
193
|
+
// Separators keep field boundaries distinct: ["ab", "c"] and ["a", "bc"] hash differently.
|
|
194
|
+
first = Math.imul(first ^ 0x1f, 0x01000193);
|
|
195
|
+
second = Math.imul(second ^ 0x1f, 0x5bd1e995);
|
|
196
|
+
};
|
|
197
|
+
for (const text of texts)
|
|
198
|
+
for (const field of searchFields) {
|
|
199
|
+
for (const part of text[field])
|
|
200
|
+
add(part);
|
|
201
|
+
add("\u0000");
|
|
202
|
+
}
|
|
203
|
+
return `${texts.length}:${(first >>> 0).toString(36)}:${(second >>> 0).toString(36)}`;
|
|
204
|
+
};
|
|
205
|
+
const buildWordIndex = (texts) => {
|
|
206
|
+
// Field texts are split once here and dropped afterwards; descriptions repeat across tools, so each text is split
|
|
207
|
+
// once and shared while building.
|
|
208
|
+
const cache = new Map();
|
|
209
|
+
const words = (texts) => texts.flatMap((text) => fieldWords(text, cache));
|
|
210
|
+
const fields = texts.map((text) => {
|
|
211
|
+
return {
|
|
212
|
+
namespace: words(text.namespace),
|
|
213
|
+
name: words(text.name),
|
|
214
|
+
description: words(text.description),
|
|
215
|
+
parameters: words(text.parameters),
|
|
216
|
+
namespaceDescription: words(text.namespaceDescription),
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
const averageLength = Object.fromEntries(searchFields.map((field) => [
|
|
220
|
+
field,
|
|
221
|
+
fields.reduce((sum, entry) => sum + entry[field].length, 0) / texts.length || 1,
|
|
222
|
+
]));
|
|
223
|
+
const building = new Map();
|
|
224
|
+
const weights = new Map();
|
|
225
|
+
fields.forEach((entry, position) => {
|
|
226
|
+
weights.clear();
|
|
227
|
+
for (const field of searchFields) {
|
|
228
|
+
const weight = fieldWeights[field] /
|
|
229
|
+
(1 - lengthNormalization + (lengthNormalization * entry[field].length) / averageLength[field]);
|
|
230
|
+
for (const word of entry[field])
|
|
231
|
+
weights.set(word, (weights.get(word) ?? 0) + weight);
|
|
232
|
+
}
|
|
233
|
+
for (const [word, weight] of weights) {
|
|
234
|
+
let posting = building.get(word);
|
|
235
|
+
if (!posting)
|
|
236
|
+
building.set(word, (posting = { positions: [], weights: [] }));
|
|
237
|
+
posting.positions.push(position);
|
|
238
|
+
posting.weights.push(weight);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
const postings = new Map([...building].map(([word, posting]) => [
|
|
242
|
+
word,
|
|
243
|
+
{ positions: Int32Array.from(posting.positions), weights: Float32Array.from(posting.weights) },
|
|
244
|
+
]));
|
|
245
|
+
return { postings, documents: texts.length, words: [...postings.keys()].sort() };
|
|
246
|
+
};
|
|
247
|
+
/** Indexed words longer than a 4+ letter query word that start with it. */
|
|
248
|
+
const prefixMatches = (words, term) => {
|
|
249
|
+
if (term.length < 4)
|
|
250
|
+
return [];
|
|
251
|
+
let low = 0;
|
|
252
|
+
let high = words.words.length;
|
|
253
|
+
while (low < high) {
|
|
254
|
+
const middle = (low + high) >> 1;
|
|
255
|
+
if (words.words[middle] < term)
|
|
256
|
+
low = middle + 1;
|
|
257
|
+
else
|
|
258
|
+
high = middle;
|
|
259
|
+
}
|
|
260
|
+
const matches = [];
|
|
261
|
+
for (let i = low; i < words.words.length && words.words[i].startsWith(term); i++)
|
|
262
|
+
if (words.words[i] !== term)
|
|
263
|
+
matches.push([words.words[i], prefixWeight]);
|
|
264
|
+
return matches;
|
|
113
265
|
};
|
|
114
|
-
const rank = (entries, query) => {
|
|
115
|
-
const terms =
|
|
116
|
-
|
|
117
|
-
.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
266
|
+
const rank = (entries, query, index) => {
|
|
267
|
+
const terms = searchWords(query);
|
|
268
|
+
if (terms.length === 0)
|
|
269
|
+
return [...entries].sort((left, right) => compareText(left.description.path, right.description.path));
|
|
270
|
+
const words = wordIndexFor(index);
|
|
271
|
+
const allowed = entries === index ? undefined : new Set(entries.map((entry) => entry.position));
|
|
272
|
+
const scores = new Map();
|
|
273
|
+
for (const term of terms) {
|
|
274
|
+
// Each tool counts a query word once, through its best-scoring match.
|
|
275
|
+
const best = new Map();
|
|
276
|
+
for (const [word, factor] of [[term, 1], ...prefixMatches(words, term)]) {
|
|
277
|
+
const posting = words.postings.get(word);
|
|
278
|
+
if (!posting)
|
|
279
|
+
continue;
|
|
280
|
+
const idf = Math.log(1 + (words.documents - posting.positions.length + 0.5) / (posting.positions.length + 0.5));
|
|
281
|
+
for (let i = 0; i < posting.positions.length; i++) {
|
|
282
|
+
const position = posting.positions[i];
|
|
283
|
+
if (allowed && !allowed.has(position))
|
|
284
|
+
continue;
|
|
285
|
+
const weight = posting.weights[i];
|
|
286
|
+
const score = (factor * idf * weight * (k1 + 1)) / (weight + k1);
|
|
287
|
+
if (score > (best.get(position) ?? 0))
|
|
288
|
+
best.set(position, score);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
for (const [position, score] of best)
|
|
292
|
+
scores.set(position, (scores.get(position) ?? 0) + score);
|
|
293
|
+
}
|
|
294
|
+
return [...scores]
|
|
295
|
+
.map(([position, score]) => ({
|
|
296
|
+
entry: index[position],
|
|
297
|
+
score: score * (1 + brevityBoost / Math.max(1, index[position].nameLength)),
|
|
298
|
+
}))
|
|
129
299
|
.sort((left, right) => right.score - left.score || compareText(left.entry.description.path, right.entry.description.path))
|
|
130
300
|
.map(({ entry }) => entry);
|
|
131
301
|
};
|
|
@@ -153,7 +323,7 @@ const makeSearchTool = (searchIndex) => ({
|
|
|
153
323
|
const exact = pathQuery === ""
|
|
154
324
|
? undefined
|
|
155
325
|
: scoped.find((entry) => entry.description.path === pathQuery || toolExpression(entry.description.path) === trimmed);
|
|
156
|
-
const ranked = exact !== undefined ? [exact] : rank(scoped,
|
|
326
|
+
const ranked = exact !== undefined ? [exact] : rank(scoped, pathQuery, searchIndex);
|
|
157
327
|
const items = ranked.slice(offset, offset + (request.limit ?? defaultSearchLimit)).map(({ description }) => ({
|
|
158
328
|
...description,
|
|
159
329
|
path: toolExpression(description.path),
|
|
@@ -171,18 +341,23 @@ export const searchSignature = (() => {
|
|
|
171
341
|
const tool = makeSearchTool([]);
|
|
172
342
|
return `search(${inputTypeScript(tool, true)}): ${outputTypeScript(tool, true)}`;
|
|
173
343
|
})();
|
|
174
|
-
const toSearchEntry = (visible) =>
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
visible
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
344
|
+
const toSearchEntry = (visible, position) => {
|
|
345
|
+
const segments = visible.path.split(".");
|
|
346
|
+
const name = segments.at(-1) ?? "";
|
|
347
|
+
return {
|
|
348
|
+
description: describeTool(visible),
|
|
349
|
+
position,
|
|
350
|
+
text: () => ({
|
|
351
|
+
namespace: segments.slice(0, -1),
|
|
352
|
+
name: [name],
|
|
353
|
+
description: [visible.tool.description],
|
|
354
|
+
// Parameter names only: their descriptions are half of all words and didn't improve ranking.
|
|
355
|
+
parameters: inputProperties(visible.tool).map((property) => property.name),
|
|
356
|
+
namespaceDescription: visible.namespaces.map((namespace) => namespace.description ?? ""),
|
|
357
|
+
}),
|
|
358
|
+
nameLength: splitWords(name).length,
|
|
359
|
+
};
|
|
360
|
+
};
|
|
186
361
|
export const prepare = (tools) => {
|
|
187
362
|
const root = toolTrie(tools);
|
|
188
363
|
let searchIndex;
|
|
@@ -216,13 +391,26 @@ const resolve = (root, path, index) => {
|
|
|
216
391
|
if (node === undefined) {
|
|
217
392
|
const name = segments.join(".");
|
|
218
393
|
const ns = segments.length > 1 && root.children.has(segments[0]) ? segments[0] : undefined;
|
|
219
|
-
const closest = rank(ns ? index.filter((entry) => entry.description.path.startsWith(`${ns}.`)) : index, ns ? segments.slice(1).join(" ") : name)[0];
|
|
394
|
+
const closest = rank(ns ? index.filter((entry) => entry.description.path.startsWith(`${ns}.`)) : index, ns ? segments.slice(1).join(" ") : name, index)[0];
|
|
220
395
|
throw new ToolRuntimeError("UnknownTool", closest
|
|
221
396
|
? `Unknown tool '${name}'. Did you mean ${toolExpression(closest.description.path)}?`
|
|
222
397
|
: `Unknown tool '${name}'.`, ["Use search to find available tools."]);
|
|
223
398
|
}
|
|
224
399
|
if (node.tool === undefined) {
|
|
225
|
-
|
|
400
|
+
const name = segments.join(".");
|
|
401
|
+
const under = index.filter((entry) => entry.description.path.startsWith(`${name}.`));
|
|
402
|
+
const ranked = rank(under, segments.at(-1) ?? "", index);
|
|
403
|
+
const shown = [...ranked, ...under.filter((entry) => !ranked.includes(entry))]
|
|
404
|
+
.slice(0, 3)
|
|
405
|
+
.map((entry) => toolExpression(entry.description.path));
|
|
406
|
+
const more = under.length - shown.length;
|
|
407
|
+
throw new ToolRuntimeError("UnknownTool", shown.length === 0
|
|
408
|
+
? `Tool '${name}' is not callable.`
|
|
409
|
+
: `Tool '${name}' is not callable, it is a namespace. Did you mean ${new Intl.ListFormat("en", { type: "disjunction" }).format(shown)}?`, [
|
|
410
|
+
more > 0
|
|
411
|
+
? `${more} more under '${name}'. Use search({ namespace: ${JSON.stringify(name)} }) to list them.`
|
|
412
|
+
: "Use search to find available tools.",
|
|
413
|
+
]);
|
|
226
414
|
}
|
|
227
415
|
return node.tool;
|
|
228
416
|
};
|
|
@@ -241,8 +429,6 @@ export const make = (prepared, maxToolCalls, hooks = {}) => {
|
|
|
241
429
|
const calls = [];
|
|
242
430
|
const invocations = new Map();
|
|
243
431
|
const root = prepared.root;
|
|
244
|
-
const runId = crypto.randomUUID();
|
|
245
|
-
let nextCallId = 0;
|
|
246
432
|
const recordCall = (call) => {
|
|
247
433
|
if (maxToolCalls !== undefined && calls.length >= maxToolCalls) {
|
|
248
434
|
throw new ToolRuntimeError("ToolCallLimitExceeded", `Execution exceeded its tool-call limit of ${maxToolCalls}.`);
|
|
@@ -253,8 +439,10 @@ export const make = (prepared, maxToolCalls, hooks = {}) => {
|
|
|
253
439
|
const normalized = args.length === 0 ? [{}] : args;
|
|
254
440
|
if (normalized.length !== 1)
|
|
255
441
|
throw new ToolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`);
|
|
442
|
+
const input = normalized[0];
|
|
443
|
+
const field = typeof input === "string" ? stringParameter(tool)?.name : undefined;
|
|
256
444
|
return yield* Effect.try({
|
|
257
|
-
try: () => decodeToolInput(tool,
|
|
445
|
+
try: () => decodeToolInput(tool, field === undefined ? input : { [field]: input }),
|
|
258
446
|
catch: (cause) => new ToolRuntimeError("InvalidToolInput", `Invalid input for tool '${name}': ${String(cause)}`, name === "search" ? [] : ["The signature may have changed. Use search to get the current signature."]),
|
|
259
447
|
});
|
|
260
448
|
});
|
|
@@ -275,23 +463,26 @@ export const make = (prepared, maxToolCalls, hooks = {}) => {
|
|
|
275
463
|
catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`),
|
|
276
464
|
});
|
|
277
465
|
const executeTool = (name, tool, args, options) => Effect.gen(function* () {
|
|
278
|
-
|
|
466
|
+
if ("_tag" in tool.execute && tool.execute._tag === "CodeModeDurable") {
|
|
467
|
+
throw new TypeError(`Tool '${name}' is a durable tool; start it with ToolRuntime.start.`);
|
|
468
|
+
}
|
|
469
|
+
const run = "_tag" in tool.execute ? tool.execute.execute : tool.execute;
|
|
470
|
+
const key = options.key;
|
|
279
471
|
const input = yield* decode(name, tool, args);
|
|
280
|
-
|
|
281
|
-
yield* Effect.sync(() => (admit !== undefined ? admit(name) : recordCall({ name })));
|
|
472
|
+
yield* Effect.sync(() => options.admit(name));
|
|
282
473
|
return yield* hooked({ name, input, key }, hooks["tool.before"], hooks["tool.after"], Effect.gen(function* () {
|
|
283
|
-
const raw = yield* failure(Effect.suspend(() =>
|
|
474
|
+
const raw = yield* failure(Effect.suspend(() => run(input, { key })));
|
|
284
475
|
return yield* output(name, tool, raw);
|
|
285
|
-
}), options
|
|
476
|
+
}), options.detaching);
|
|
286
477
|
});
|
|
287
478
|
const durableTool = (path) => {
|
|
288
479
|
const segments = canonicalSegments(path);
|
|
289
480
|
const name = segments.join(".");
|
|
290
481
|
const tool = lookup(root, segments)?.tool;
|
|
291
|
-
if (tool
|
|
482
|
+
if (tool === undefined || !("_tag" in tool.execute) || tool.execute._tag !== "CodeModeDurable") {
|
|
292
483
|
throw new TypeError(`Tool '${name}' is not a durable tool.`);
|
|
293
484
|
}
|
|
294
|
-
return { name, tool, durable: tool.
|
|
485
|
+
return { name, tool, durable: tool.execute };
|
|
295
486
|
};
|
|
296
487
|
return {
|
|
297
488
|
calls,
|
|
@@ -311,13 +502,14 @@ export const make = (prepared, maxToolCalls, hooks = {}) => {
|
|
|
311
502
|
const segments = canonicalSegments(path);
|
|
312
503
|
if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined)
|
|
313
504
|
return undefined;
|
|
314
|
-
|
|
505
|
+
const execute = lookup(root, segments)?.tool?.execute;
|
|
506
|
+
return execute !== undefined && "_tag" in execute ? execute : undefined;
|
|
315
507
|
},
|
|
316
508
|
start: (path, args, key, admit) => Effect.suspend(() => {
|
|
317
509
|
const found = durableTool(path);
|
|
318
510
|
return Effect.gen(function* () {
|
|
319
511
|
const input = yield* decode(found.name, found.tool, args);
|
|
320
|
-
yield* Effect.sync(() =>
|
|
512
|
+
yield* Effect.sync(() => admit(found.name));
|
|
321
513
|
const invocation = { name: found.name, input, key };
|
|
322
514
|
invocations.set(key, invocation);
|
|
323
515
|
const before = hooks["tool.before"];
|
package/dist/tool-schema.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ export type InputProperty = {
|
|
|
9
9
|
readonly required: boolean;
|
|
10
10
|
};
|
|
11
11
|
export declare const inputProperties: <R>(tool: Tool<R>) => Array<InputProperty>;
|
|
12
|
+
/** A freeform tool whose input is one required string field takes that string directly: `tools.patch(patchText: string)`. */
|
|
13
|
+
export declare const stringParameter: <R>(tool: Tool<R>) => {
|
|
14
|
+
name: string;
|
|
15
|
+
signature: string;
|
|
16
|
+
} | undefined;
|
|
12
17
|
export declare const inputTypeScript: <R>(tool: Tool<R>, pretty?: boolean) => string;
|
|
13
18
|
export declare const isEmptyInput: <R>(tool: Tool<R>) => boolean;
|
|
14
19
|
export declare const outputTypeScript: <R>(tool: Tool<R>, pretty?: boolean) => string;
|
package/dist/tool-schema.js
CHANGED
|
@@ -237,22 +237,9 @@ export const jsonSchemaToTypeScript = (schema, pretty = false) => {
|
|
|
237
237
|
};
|
|
238
238
|
export const inputProperties = (tool) => {
|
|
239
239
|
try {
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
schema: tool.input,
|
|
244
|
-
definitions: { ...(tool.input.definitions ?? {}), ...(tool.input.$defs ?? {}) },
|
|
245
|
-
};
|
|
246
|
-
let schema = document.schema;
|
|
247
|
-
if (schema.$ref !== undefined) {
|
|
248
|
-
const name = definitionName(schema.$ref);
|
|
249
|
-
const resolved = name === undefined ? undefined : document.definitions[name];
|
|
250
|
-
if (resolved === undefined)
|
|
251
|
-
return [];
|
|
252
|
-
schema = resolved;
|
|
253
|
-
}
|
|
254
|
-
const required = new Set(schema.required ?? []);
|
|
255
|
-
return Object.entries(schema.properties ?? {}).map(([name, value]) => ({
|
|
240
|
+
const schema = inputDocument(tool)?.schema;
|
|
241
|
+
const required = new Set(schema?.required ?? []);
|
|
242
|
+
return Object.entries(schema?.properties ?? {}).map(([name, value]) => ({
|
|
256
243
|
name,
|
|
257
244
|
description: typeof value.description === "string" ? value.description : undefined,
|
|
258
245
|
required: required.has(name),
|
|
@@ -262,6 +249,39 @@ export const inputProperties = (tool) => {
|
|
|
262
249
|
return [];
|
|
263
250
|
}
|
|
264
251
|
};
|
|
252
|
+
/** A freeform tool whose input is one required string field takes that string directly: `tools.patch(patchText: string)`. */
|
|
253
|
+
export const stringParameter = (tool) => {
|
|
254
|
+
if (!tool.freeform)
|
|
255
|
+
return undefined;
|
|
256
|
+
try {
|
|
257
|
+
const document = inputDocument(tool);
|
|
258
|
+
const fields = Object.entries(document?.schema.properties ?? {});
|
|
259
|
+
if (document === undefined || fields.length !== 1)
|
|
260
|
+
return undefined;
|
|
261
|
+
const [name, field] = fields[0];
|
|
262
|
+
if (field.type !== "string" || !document.schema.required?.includes(name))
|
|
263
|
+
return undefined;
|
|
264
|
+
const label = identifierSegment.test(name) ? name : "input";
|
|
265
|
+
const type = renderSchema(field, { definitions: document.definitions, pretty: false });
|
|
266
|
+
return { name, signature: `(${jsdoc(field, "").replace(/\n$/, " ")}${label}: ${type})` };
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
const inputDocument = (tool) => {
|
|
273
|
+
const document = isEffectSchema(tool.input)
|
|
274
|
+
? Schema.toJsonSchemaDocument(tool.input, { onExcessProperty: "error" })
|
|
275
|
+
: {
|
|
276
|
+
schema: tool.input,
|
|
277
|
+
definitions: { ...(tool.input.definitions ?? {}), ...(tool.input.$defs ?? {}) },
|
|
278
|
+
};
|
|
279
|
+
if (document.schema.$ref === undefined)
|
|
280
|
+
return document;
|
|
281
|
+
const name = definitionName(document.schema.$ref);
|
|
282
|
+
const schema = name === undefined ? undefined : document.definitions[name];
|
|
283
|
+
return schema && { schema, definitions: document.definitions };
|
|
284
|
+
};
|
|
265
285
|
export const inputTypeScript = (tool, pretty = false) => isEffectSchema(tool.input) ? toTypeScript(tool.input, false, pretty) : jsonSchemaToTypeScript(tool.input, pretty);
|
|
266
286
|
// Effect inputs are empty only when their encoded root is Effect's empty-struct marker, which records and checked
|
|
267
287
|
// structs never are. Raw JSON Schema inputs keep the `{}` rendering check, which treats `{ type: "object" }` as empty.
|
package/dist/tool.d.ts
CHANGED
|
@@ -44,12 +44,11 @@ export type ToolContext = {
|
|
|
44
44
|
};
|
|
45
45
|
export type ExecuteFn<Input = unknown, Output = unknown, R = never> = (input: Input, context: ToolContext) => Effect.Effect<Output, unknown, R>;
|
|
46
46
|
/**
|
|
47
|
-
* A tool execution wrapper for idempotent work that is safe to run again. If a suspend lands while the call is running
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* A tool execution wrapper for idempotent work that is safe to run again. If a suspend lands while the call is running,
|
|
48
|
+
* the VM interrupts it without firing `tool.after`, keeps the call queued in the snapshot, and re-runs it on resume
|
|
49
|
+
* with the same `key` without double-counting against `maxToolCalls`.
|
|
50
50
|
*/
|
|
51
51
|
export type Idempotent<Input = unknown, Output = unknown, R = never> = {
|
|
52
|
-
(input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
|
|
53
52
|
readonly _tag: "CodeModeIdempotent";
|
|
54
53
|
readonly execute: ExecuteFn<Input, Output, R>;
|
|
55
54
|
};
|
|
@@ -67,6 +66,17 @@ export type DurableOptions<Data extends Schema.Json, Input = unknown, Output = u
|
|
|
67
66
|
};
|
|
68
67
|
/** A tool execution wrapper for work that keeps running across a suspend and reconnects through its JSON data. */
|
|
69
68
|
export type Durable<Input = unknown, Output = unknown, R = never> = {
|
|
69
|
+
readonly _tag: "CodeModeDurable";
|
|
70
|
+
readonly start: (input: Input, context: ToolContext) => Effect.Effect<Schema.Json, unknown, R>;
|
|
71
|
+
readonly wait: (data: never, context: ToolContext) => Effect.Effect<Output, unknown, R>;
|
|
72
|
+
readonly cancel: (data: never) => Effect.Effect<void, unknown, R>;
|
|
73
|
+
};
|
|
74
|
+
type IdempotentOption<Input = unknown, Output = unknown, R = never> = {
|
|
75
|
+
(input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
|
|
76
|
+
readonly _tag: "CodeModeIdempotent";
|
|
77
|
+
readonly execute: ExecuteFn<Input, Output, R>;
|
|
78
|
+
};
|
|
79
|
+
type DurableOption<Input = unknown, Output = unknown, R = never> = {
|
|
70
80
|
(input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
|
|
71
81
|
readonly _tag: "CodeModeDurable";
|
|
72
82
|
readonly start: (input: Input, context: ToolContext) => Effect.Effect<Schema.Json, unknown, R>;
|
|
@@ -78,9 +88,9 @@ export type Tool<R = never> = {
|
|
|
78
88
|
readonly _tag: "CodeModeTool";
|
|
79
89
|
readonly description: string;
|
|
80
90
|
readonly input: SchemaType;
|
|
91
|
+
readonly freeform?: boolean;
|
|
81
92
|
readonly output: SchemaType | undefined;
|
|
82
|
-
readonly execute:
|
|
83
|
-
readonly mode?: Idempotent<unknown, unknown, R> | Durable<unknown, unknown, R> | undefined;
|
|
93
|
+
readonly execute: ExecuteFn<unknown, unknown, R> | Idempotent<unknown, unknown, R> | Durable<unknown, unknown, R>;
|
|
84
94
|
};
|
|
85
95
|
type InputType<S> = S extends Schema.Decoder<unknown> ? S["Type"] : unknown;
|
|
86
96
|
type ResultType<S> = S extends undefined ? void : S extends Schema.Decoder<unknown> ? S["Encoded"] : unknown;
|
|
@@ -88,17 +98,22 @@ type ResultType<S> = S extends undefined ? void : S extends Schema.Decoder<unkno
|
|
|
88
98
|
export type Options<I extends SchemaType, O extends SchemaType | undefined, R = never> = {
|
|
89
99
|
readonly description: string;
|
|
90
100
|
readonly input: I;
|
|
101
|
+
/**
|
|
102
|
+
* The tool's input is one required string field that programs pass directly, as in `tools.patch("...")`.
|
|
103
|
+
* The object form still works, and the executor receives the object either way.
|
|
104
|
+
*/
|
|
105
|
+
readonly freeform?: boolean;
|
|
91
106
|
readonly output?: O;
|
|
92
|
-
readonly execute: ExecuteFn<InputType<I>, ResultType<O>, R> |
|
|
107
|
+
readonly execute: ExecuteFn<InputType<I>, ResultType<O>, R> | IdempotentOption<InputType<I>, ResultType<O>, R> | DurableOption<InputType<I>, ResultType<O>, R>;
|
|
93
108
|
};
|
|
94
109
|
export declare const isTool: <R = never>(value: Tool<R> | Namespace<R> | Tools<R> | undefined) => value is Tool<R>;
|
|
95
110
|
/** Marks a tool's `execute` function as safe to interrupt on suspend and re-run on resume with the same key. */
|
|
96
|
-
export declare const idempotent: <Input, Output, R = never>(execute: ExecuteFn<Input, Output, R>) =>
|
|
111
|
+
export declare const idempotent: <Input, Output, R = never>(execute: ExecuteFn<Input, Output, R>) => IdempotentOption<Input, Output, R>;
|
|
97
112
|
/**
|
|
98
113
|
* Wraps a tool's execution as `start`, `wait`, and `cancel` so work started before a suspend keeps running and
|
|
99
114
|
* reconnects through its JSON data on resume.
|
|
100
115
|
*/
|
|
101
|
-
export declare const durable: <Data extends Schema.Json, Input = unknown, Output = unknown, RStart = never, RWait = never, RCancel = never>(options: DurableOptions<Data, Input, Output, RStart, RWait, RCancel>) =>
|
|
116
|
+
export declare const durable: <Data extends Schema.Json, Input = unknown, Output = unknown, RStart = never, RWait = never, RCancel = never>(options: DurableOptions<Data, Input, Output, RStart, RWait, RCancel>) => DurableOption<Input, Output, RStart | RWait | RCancel>;
|
|
102
117
|
/**
|
|
103
118
|
* Declares one schema-described tool available to a CodeMode program through `tools.*`.
|
|
104
119
|
*
|