@opencode/codemode 2.2.5 → 2.2.7
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 +164 -7
- package/dist/codemode.d.ts +60 -4
- package/dist/codemode.js +63 -8
- package/dist/extension.d.ts +27 -9
- package/dist/extension.js +7 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/interpreter/callback.d.ts +12 -1
- package/dist/interpreter/callback.js +13 -1
- package/dist/interpreter/execute.d.ts +1 -1
- package/dist/interpreter/execute.js +16 -4
- package/dist/interpreter/extensions.d.ts +3 -3
- package/dist/interpreter/extensions.js +17 -11
- package/dist/interpreter/globals.js +10 -0
- package/dist/interpreter/interpreter.d.ts +11 -1
- package/dist/interpreter/interpreter.js +88 -4
- package/dist/interpreter/modules.d.ts +4 -0
- package/dist/interpreter/modules.js +33 -0
- package/dist/interpreter/promises.js +2 -2
- package/dist/interpreter/runner.d.ts +1 -11
- package/dist/interpreter/runner.js +1 -13
- package/dist/stdlib/iterator.js +1 -2
- package/dist/suspend-error.d.ts +24 -0
- package/dist/suspend-error.js +25 -0
- package/dist/tool-runtime.d.ts +31 -7
- package/dist/tool-runtime.js +116 -26
- package/dist/tool.d.ts +45 -2
- package/dist/tool.js +30 -7
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +106 -0
- package/dist/vm/host.js +231 -0
- package/dist/vm/index.d.ts +6 -0
- package/dist/vm/index.js +6 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +68 -0
- package/dist/vm/runtime/driver.js +254 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +80 -0
- package/dist/vm/runtime/execute.js +442 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +337 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +53 -0
- package/dist/vm/runtime/interpreter.js +96 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +190 -0
- package/dist/vm/scheduler.js +74 -0
- package/dist/vm/snapshot/codec.d.ts +117 -0
- package/dist/vm/snapshot/codec.js +848 -0
- package/dist/vm/snapshot/snapshot.d.ts +53 -0
- package/dist/vm/snapshot/snapshot.js +74 -0
- package/dist/vm/snapshot.d.ts +32 -0
- package/dist/vm/snapshot.js +309 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +226 -0
- package/dist/vm/vm.js +2931 -0
- package/package.json +8 -5
package/dist/tool-runtime.js
CHANGED
|
@@ -4,11 +4,13 @@ import { decodeInput as decodeToolInput, decodeOutput as decodeToolOutput, ident
|
|
|
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);
|
|
7
|
-
/** Runs `before`, then `run`, then `after` with how it ended, including when interrupted. */
|
|
8
|
-
export const hooked = (call, before, after, run) => {
|
|
7
|
+
/** Runs `before`, then `run`, then `after` with how it ended, including when interrupted (unless `detaching`). */
|
|
8
|
+
export const hooked = (call, before, after, run, detaching) => {
|
|
9
9
|
const observed = after === undefined
|
|
10
10
|
? run
|
|
11
11
|
: Effect.onExit(run, (exit) => {
|
|
12
|
+
if (detaching?.())
|
|
13
|
+
return Effect.void;
|
|
12
14
|
if (Exit.isSuccess(exit))
|
|
13
15
|
return after(call, { status: "success", value: exit.value });
|
|
14
16
|
if (Cause.hasInterruptsOnly(exit.cause))
|
|
@@ -237,51 +239,139 @@ export class ToolRuntimeError extends Error {
|
|
|
237
239
|
/** Per-execution call state over tools prepared once for the runtime. */
|
|
238
240
|
export const make = (prepared, maxToolCalls, hooks = {}) => {
|
|
239
241
|
const calls = [];
|
|
242
|
+
const invocations = new Map();
|
|
240
243
|
const root = prepared.root;
|
|
244
|
+
const runId = crypto.randomUUID();
|
|
245
|
+
let nextCallId = 0;
|
|
241
246
|
const recordCall = (call) => {
|
|
242
247
|
if (maxToolCalls !== undefined && calls.length >= maxToolCalls) {
|
|
243
248
|
throw new ToolRuntimeError("ToolCallLimitExceeded", `Execution exceeded its tool-call limit of ${maxToolCalls}.`);
|
|
244
249
|
}
|
|
245
250
|
calls.push(call);
|
|
246
251
|
};
|
|
247
|
-
const
|
|
252
|
+
const decode = (name, tool, args) => Effect.gen(function* () {
|
|
248
253
|
const normalized = args.length === 0 ? [{}] : args;
|
|
249
254
|
if (normalized.length !== 1)
|
|
250
255
|
throw new ToolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`);
|
|
251
|
-
|
|
256
|
+
return yield* Effect.try({
|
|
252
257
|
try: () => decodeToolInput(tool, normalized[0]),
|
|
253
258
|
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."]),
|
|
254
259
|
});
|
|
255
|
-
yield* Effect.sync(() => recordCall({ name }));
|
|
256
|
-
return yield* hooked({ name, input }, hooks["tool.before"], hooks["tool.after"], Effect.gen(function* () {
|
|
257
|
-
const raw = yield* Effect.suspend(() => tool.execute(input)).pipe(Effect.catchCause((cause) => {
|
|
258
|
-
if (Cause.hasInterruptsOnly(cause))
|
|
259
|
-
return Effect.interrupt;
|
|
260
|
-
return Effect.fail(toolError(Cause.prettyErrors(cause)
|
|
261
|
-
.map((error) => (error.cause ? Formatter.format(error) : error.message || error.name))
|
|
262
|
-
.join("\n")));
|
|
263
|
-
}));
|
|
264
|
-
// The same round trip a tool result would take through text: exact JSON.stringify semantics.
|
|
265
|
-
return yield* Effect.try({
|
|
266
|
-
try: () => {
|
|
267
|
-
const text = JSON.stringify(decodeToolOutput(tool, raw));
|
|
268
|
-
return text === undefined ? undefined : JSON.parse(text);
|
|
269
|
-
},
|
|
270
|
-
catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`),
|
|
271
|
-
});
|
|
272
|
-
}));
|
|
273
260
|
});
|
|
261
|
+
// A tool's own failure becomes a ToolError; interruption passes through.
|
|
262
|
+
const failure = (effect) => effect.pipe(Effect.catchCause((cause) => {
|
|
263
|
+
if (Cause.hasInterruptsOnly(cause))
|
|
264
|
+
return Effect.interrupt;
|
|
265
|
+
return Effect.fail(toolError(Cause.prettyErrors(cause)
|
|
266
|
+
.map((error) => (error.cause ? Formatter.format(error) : error.message || error.name))
|
|
267
|
+
.join("\n")));
|
|
268
|
+
}));
|
|
269
|
+
// The same round trip a tool result would take through text: exact JSON.stringify semantics.
|
|
270
|
+
const output = (name, tool, raw) => Effect.try({
|
|
271
|
+
try: () => {
|
|
272
|
+
const text = JSON.stringify(decodeToolOutput(tool, raw));
|
|
273
|
+
return text === undefined ? undefined : JSON.parse(text);
|
|
274
|
+
},
|
|
275
|
+
catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`),
|
|
276
|
+
});
|
|
277
|
+
const executeTool = (name, tool, args, options) => Effect.gen(function* () {
|
|
278
|
+
const key = options?.key ?? `${runId}:${nextCallId++}`;
|
|
279
|
+
const input = yield* decode(name, tool, args);
|
|
280
|
+
const admit = options?.admit;
|
|
281
|
+
yield* Effect.sync(() => (admit !== undefined ? admit(name) : recordCall({ name })));
|
|
282
|
+
return yield* hooked({ name, input, key }, hooks["tool.before"], hooks["tool.after"], Effect.gen(function* () {
|
|
283
|
+
const raw = yield* failure(Effect.suspend(() => tool.execute(input, { key })));
|
|
284
|
+
return yield* output(name, tool, raw);
|
|
285
|
+
}), options?.detaching);
|
|
286
|
+
});
|
|
287
|
+
const durableTool = (path) => {
|
|
288
|
+
const segments = canonicalSegments(path);
|
|
289
|
+
const name = segments.join(".");
|
|
290
|
+
const tool = lookup(root, segments)?.tool;
|
|
291
|
+
if (tool?.mode?._tag !== "CodeModeDurable") {
|
|
292
|
+
throw new TypeError(`Tool '${name}' is not a durable tool.`);
|
|
293
|
+
}
|
|
294
|
+
return { name, tool, durable: tool.mode };
|
|
295
|
+
};
|
|
274
296
|
return {
|
|
275
297
|
calls,
|
|
276
298
|
hooks,
|
|
299
|
+
invocations,
|
|
300
|
+
record: (name) => recordCall({ name }),
|
|
277
301
|
keys: (path) => namespaceKeys(root, path),
|
|
278
|
-
search: (args) => Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args)),
|
|
279
|
-
execute: (path, args) => Effect.suspend(() => {
|
|
302
|
+
search: (args, options) => Effect.suspend(() => executeTool("search", makeSearchTool(prepared.searchIndex), args, options)),
|
|
303
|
+
execute: (path, args, options) => Effect.suspend(() => {
|
|
280
304
|
const segments = canonicalSegments(path);
|
|
281
305
|
// Models often write `tools.search(...)` for the bare `search(...)`; honor it unless a tool owns that path.
|
|
282
306
|
if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined)
|
|
283
|
-
return executeTool("search", makeSearchTool(prepared.searchIndex), args);
|
|
284
|
-
return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args);
|
|
307
|
+
return executeTool("search", makeSearchTool(prepared.searchIndex), args, options);
|
|
308
|
+
return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args, options);
|
|
309
|
+
}),
|
|
310
|
+
mode: (path) => {
|
|
311
|
+
const segments = canonicalSegments(path);
|
|
312
|
+
if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined)
|
|
313
|
+
return undefined;
|
|
314
|
+
return lookup(root, segments)?.tool?.mode;
|
|
315
|
+
},
|
|
316
|
+
start: (path, args, key, admit) => Effect.suspend(() => {
|
|
317
|
+
const found = durableTool(path);
|
|
318
|
+
return Effect.gen(function* () {
|
|
319
|
+
const input = yield* decode(found.name, found.tool, args);
|
|
320
|
+
yield* Effect.sync(() => (admit !== undefined ? admit(found.name) : recordCall({ name: found.name })));
|
|
321
|
+
const invocation = { name: found.name, input, key };
|
|
322
|
+
invocations.set(key, invocation);
|
|
323
|
+
const before = hooks["tool.before"];
|
|
324
|
+
const after = hooks["tool.after"];
|
|
325
|
+
const run = failure(Effect.suspend(() => found.durable.start(input, { key }))).pipe(Effect.flatMap((raw) => Effect.try({
|
|
326
|
+
try: () => {
|
|
327
|
+
const text = JSON.stringify(raw);
|
|
328
|
+
return text === undefined ? null : JSON.parse(text);
|
|
329
|
+
},
|
|
330
|
+
catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid start data from tool '${found.name}': ${cause}`),
|
|
331
|
+
})));
|
|
332
|
+
const observed = after === undefined
|
|
333
|
+
? run
|
|
334
|
+
: Effect.onError(run, (cause) => {
|
|
335
|
+
invocations.delete(key);
|
|
336
|
+
if (Cause.hasInterruptsOnly(cause))
|
|
337
|
+
return after(invocation, { status: "interrupted" });
|
|
338
|
+
return after(invocation, { status: "failure", error: Cause.squash(cause) });
|
|
339
|
+
});
|
|
340
|
+
return yield* before === undefined ? observed : Effect.andThen(before(invocation), observed);
|
|
341
|
+
});
|
|
342
|
+
}),
|
|
343
|
+
wait: (path, args, key, data, detaching) => Effect.suspend(() => {
|
|
344
|
+
const found = durableTool(path);
|
|
345
|
+
const wait = found.durable.wait;
|
|
346
|
+
const after = hooks["tool.after"];
|
|
347
|
+
const run = Effect.gen(function* () {
|
|
348
|
+
const raw = yield* failure(Effect.suspend(() => wait(data, { key })));
|
|
349
|
+
return yield* output(found.name, found.tool, raw);
|
|
350
|
+
});
|
|
351
|
+
if (after === undefined)
|
|
352
|
+
return run;
|
|
353
|
+
const known = invocations.get(key);
|
|
354
|
+
const callEffect = known !== undefined
|
|
355
|
+
? Effect.succeed(known)
|
|
356
|
+
: Effect.map(decode(found.name, found.tool, args), (input) => ({
|
|
357
|
+
name: found.name,
|
|
358
|
+
input,
|
|
359
|
+
key,
|
|
360
|
+
}));
|
|
361
|
+
return Effect.flatMap(callEffect, (call) => Effect.onExit(run, (exit) => {
|
|
362
|
+
if (detaching())
|
|
363
|
+
return Effect.void;
|
|
364
|
+
invocations.delete(key);
|
|
365
|
+
if (Exit.isSuccess(exit))
|
|
366
|
+
return after(call, { status: "success", value: exit.value });
|
|
367
|
+
if (Cause.hasInterruptsOnly(exit.cause))
|
|
368
|
+
return after(call, { status: "interrupted" });
|
|
369
|
+
return after(call, { status: "failure", error: Cause.squash(exit.cause) });
|
|
370
|
+
}));
|
|
371
|
+
}),
|
|
372
|
+
cancel: (path, data) => Effect.suspend(() => {
|
|
373
|
+
const cancel = durableTool(path).durable.cancel;
|
|
374
|
+
return failure(cancel(data));
|
|
285
375
|
}),
|
|
286
376
|
};
|
|
287
377
|
};
|
package/dist/tool.d.ts
CHANGED
|
@@ -38,13 +38,49 @@ export type JsonSchema = {
|
|
|
38
38
|
};
|
|
39
39
|
/** Either a validating Effect Schema or a render-only JSON Schema document. */
|
|
40
40
|
export type SchemaType = Schema.Decoder<unknown> | JsonSchema;
|
|
41
|
+
/** Context passed to a tool call: its stable idempotency key (`<runId>:<callId>`). */
|
|
42
|
+
export type ToolContext = {
|
|
43
|
+
readonly key: string;
|
|
44
|
+
};
|
|
45
|
+
export type ExecuteFn<Input = unknown, Output = unknown, R = never> = (input: Input, context: ToolContext) => Effect.Effect<Output, unknown, R>;
|
|
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
|
+
* (`engine: "vm"`), the VM interrupts it without firing `tool.after`, keeps the call queued in the snapshot, and
|
|
49
|
+
* re-runs it on resume with the same `key` without double-counting against `maxToolCalls`.
|
|
50
|
+
*/
|
|
51
|
+
export type Idempotent<Input = unknown, Output = unknown, R = never> = {
|
|
52
|
+
(input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
|
|
53
|
+
readonly _tag: "CodeModeIdempotent";
|
|
54
|
+
readonly execute: ExecuteFn<Input, Output, R>;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Options for `Tool.durable`: `start` begins the work under `key` and returns JSON `data` saved in the snapshot;
|
|
58
|
+
* `wait` connects to the running work and waits for its output; `cancel` stops the work when the run is interrupted
|
|
59
|
+
* for real or ends, never when it is suspended. `wait` is not tied to the program's JS `await`: the VM calls `wait`
|
|
60
|
+
* right after `start` returns, and again for every `started` call right at resume, whatever the program does with the
|
|
61
|
+
* promise.
|
|
62
|
+
*/
|
|
63
|
+
export type DurableOptions<Data extends Schema.Json, Input = unknown, Output = unknown, RStart = never, RWait = never, RCancel = never> = {
|
|
64
|
+
readonly start: (input: Input, context: ToolContext) => Effect.Effect<Data, unknown, RStart>;
|
|
65
|
+
readonly wait: (data: NoInfer<Data>, context: ToolContext) => Effect.Effect<Output, unknown, RWait>;
|
|
66
|
+
readonly cancel: (data: NoInfer<Data>) => Effect.Effect<void, unknown, RCancel>;
|
|
67
|
+
};
|
|
68
|
+
/** A tool execution wrapper for work that keeps running across a suspend and reconnects through its JSON data. */
|
|
69
|
+
export type Durable<Input = unknown, Output = unknown, R = never> = {
|
|
70
|
+
(input: Input, context: ToolContext): Effect.Effect<Output, unknown, R>;
|
|
71
|
+
readonly _tag: "CodeModeDurable";
|
|
72
|
+
readonly start: (input: Input, context: ToolContext) => Effect.Effect<Schema.Json, unknown, R>;
|
|
73
|
+
readonly wait: (data: never, context: ToolContext) => Effect.Effect<Output, unknown, R>;
|
|
74
|
+
readonly cancel: (data: never) => Effect.Effect<void, unknown, R>;
|
|
75
|
+
};
|
|
41
76
|
/** Executable tool exposed through CodeMode's `tools` object. */
|
|
42
77
|
export type Tool<R = never> = {
|
|
43
78
|
readonly _tag: "CodeModeTool";
|
|
44
79
|
readonly description: string;
|
|
45
80
|
readonly input: SchemaType;
|
|
46
81
|
readonly output: SchemaType | undefined;
|
|
47
|
-
readonly execute: (input: unknown) => Effect.Effect<unknown, unknown, R>;
|
|
82
|
+
readonly execute: (input: unknown, context?: ToolContext) => Effect.Effect<unknown, unknown, R>;
|
|
83
|
+
readonly mode?: Idempotent<unknown, unknown, R> | Durable<unknown, unknown, R> | undefined;
|
|
48
84
|
};
|
|
49
85
|
type InputType<S> = S extends Schema.Decoder<unknown> ? S["Type"] : unknown;
|
|
50
86
|
type ResultType<S> = S extends undefined ? void : S extends Schema.Decoder<unknown> ? S["Encoded"] : unknown;
|
|
@@ -53,9 +89,16 @@ export type Options<I extends SchemaType, O extends SchemaType | undefined, R =
|
|
|
53
89
|
readonly description: string;
|
|
54
90
|
readonly input: I;
|
|
55
91
|
readonly output?: O;
|
|
56
|
-
readonly execute:
|
|
92
|
+
readonly execute: ExecuteFn<InputType<I>, ResultType<O>, R> | Idempotent<InputType<I>, ResultType<O>, R> | Durable<InputType<I>, ResultType<O>, R>;
|
|
57
93
|
};
|
|
58
94
|
export declare const isTool: <R = never>(value: Tool<R> | Namespace<R> | Tools<R> | undefined) => value is Tool<R>;
|
|
95
|
+
/** 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>) => Idempotent<Input, Output, R>;
|
|
97
|
+
/**
|
|
98
|
+
* Wraps a tool's execution as `start`, `wait`, and `cancel` so work started before a suspend keeps running and
|
|
99
|
+
* reconnects through its JSON data on resume.
|
|
100
|
+
*/
|
|
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>) => Durable<Input, Output, RStart | RWait | RCancel>;
|
|
59
102
|
/**
|
|
60
103
|
* Declares one schema-described tool available to a CodeMode program through `tools.*`.
|
|
61
104
|
*
|
package/dist/tool.js
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
export const isTool = (value) => value !== undefined && Object.hasOwn(value, "_tag") && value._tag === "CodeModeTool";
|
|
3
|
+
/** Marks a tool's `execute` function as safe to interrupt on suspend and re-run on resume with the same key. */
|
|
4
|
+
export const idempotent = (execute) => Object.assign((input, context) => execute(input, context), {
|
|
5
|
+
_tag: "CodeModeIdempotent",
|
|
6
|
+
execute,
|
|
7
|
+
});
|
|
8
|
+
/**
|
|
9
|
+
* Wraps a tool's execution as `start`, `wait`, and `cancel` so work started before a suspend keeps running and
|
|
10
|
+
* reconnects through its JSON data on resume.
|
|
11
|
+
*/
|
|
12
|
+
export const durable = (options) => {
|
|
13
|
+
const wait = options.wait;
|
|
14
|
+
return Object.assign((input, context) => Effect.flatMap(options.start(input, context), (data) => wait(data, context)), {
|
|
15
|
+
_tag: "CodeModeDurable",
|
|
16
|
+
start: options.start,
|
|
17
|
+
wait: options.wait,
|
|
18
|
+
cancel: options.cancel,
|
|
19
|
+
});
|
|
20
|
+
};
|
|
3
21
|
/**
|
|
4
22
|
* Declares one schema-described tool available to a CodeMode program through `tools.*`.
|
|
5
23
|
*
|
|
@@ -7,10 +25,15 @@ export const isTool = (value) => value !== undefined && Object.hasOwn(value, "_t
|
|
|
7
25
|
* Without `output`, results are exposed as `void`. Hosts remain responsible for authorization
|
|
8
26
|
* and durable side effects.
|
|
9
27
|
*/
|
|
10
|
-
export const make = (options) =>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
28
|
+
export const make = (options) => {
|
|
29
|
+
const spec = options.execute;
|
|
30
|
+
const mode = "_tag" in spec ? spec : undefined;
|
|
31
|
+
return {
|
|
32
|
+
_tag: "CodeModeTool",
|
|
33
|
+
description: options.description,
|
|
34
|
+
input: options.input,
|
|
35
|
+
output: options.output,
|
|
36
|
+
...(mode === undefined ? {} : { mode }),
|
|
37
|
+
execute: (input, context = { key: crypto.randomUUID() }) => spec(input, context),
|
|
38
|
+
};
|
|
39
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Diagnostic } from "../codemode.js";
|
|
2
|
+
import { type AstNode } from "./runtime/model.js";
|
|
3
|
+
import { type Callable, PromiseObj, type Value } from "./runtime/objects.js";
|
|
4
|
+
import { type Completion } from "./runtime/signals.js";
|
|
5
|
+
import { type Job, type Reaction, Resolving } from "./scheduler.js";
|
|
6
|
+
import { type Frame, type FrameMode, type Vm } from "./vm.js";
|
|
7
|
+
export type SettlingTarget = {
|
|
8
|
+
resolve(value: Value): void;
|
|
9
|
+
reject(reason: unknown): void;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Target for a `Promise.prototype.finally` cleanup frame running in `Mode.SETTLE`.
|
|
13
|
+
* When the cleanup completes with `result`, chains `finallyCleaned` and `finallyOutcome` reactions.
|
|
14
|
+
*/
|
|
15
|
+
export declare class FinallyTarget implements SettlingTarget {
|
|
16
|
+
readonly vm: Vm;
|
|
17
|
+
readonly made: Resolving;
|
|
18
|
+
readonly site: AstNode | undefined;
|
|
19
|
+
readonly outcome: Completion;
|
|
20
|
+
constructor(vm: Vm, made: Resolving, site: AstNode | undefined, outcome: Completion);
|
|
21
|
+
resolve(result: Value): void;
|
|
22
|
+
reject(reason: unknown): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Target for `$vmAsyncFromSyncClose`: regardless of whether `iterator.return()` succeeds or throws,
|
|
26
|
+
* the original rejection reason wins.
|
|
27
|
+
*/
|
|
28
|
+
export declare class AsyncFromSyncCloseTarget implements SettlingTarget {
|
|
29
|
+
readonly made: Resolving;
|
|
30
|
+
readonly reason: unknown;
|
|
31
|
+
constructor(made: Resolving, reason: unknown);
|
|
32
|
+
resolve(_value: Value): void;
|
|
33
|
+
reject(_error: unknown): void;
|
|
34
|
+
}
|
|
35
|
+
type Parked = {
|
|
36
|
+
readonly promise: PromiseObj;
|
|
37
|
+
readonly node: AstNode | undefined;
|
|
38
|
+
readonly result: Value;
|
|
39
|
+
};
|
|
40
|
+
export declare class VmScheduler {
|
|
41
|
+
readonly vm: Vm;
|
|
42
|
+
intrinsicThen: Callable | undefined;
|
|
43
|
+
readonly parked: Map<Frame, Parked>;
|
|
44
|
+
readonly queue: Array<Job>;
|
|
45
|
+
head: number;
|
|
46
|
+
pending: number;
|
|
47
|
+
nextID: number;
|
|
48
|
+
readonly unhandled: Map<PromiseObj, {
|
|
49
|
+
readonly kind: "ExecutionFailure" | "InvalidDataValue" | "InvalidToolInput" | "InvalidToolOutput" | "ParseError" | "TimeoutExceeded" | "ToolCallLimitExceeded" | "ToolFailure" | "Truncated" | "UnknownTool" | "UnsupportedSyntax";
|
|
50
|
+
readonly message: string;
|
|
51
|
+
readonly location?: {
|
|
52
|
+
readonly line: number;
|
|
53
|
+
readonly column: number;
|
|
54
|
+
} | undefined;
|
|
55
|
+
readonly suggestions?: readonly string[] | undefined;
|
|
56
|
+
}>;
|
|
57
|
+
constructor(vm: Vm);
|
|
58
|
+
get hasJobs(): boolean;
|
|
59
|
+
newPromise(): PromiseObj;
|
|
60
|
+
resolving(promise: PromiseObj, site?: AstNode): Resolving;
|
|
61
|
+
then(promise: PromiseObj, reaction: Reaction): void;
|
|
62
|
+
detach(promise: PromiseObj, reaction: Reaction): void;
|
|
63
|
+
promiseResolve(value: Value): PromiseObj;
|
|
64
|
+
reject(promise: PromiseObj, reason: unknown): void;
|
|
65
|
+
resolveWith(promise: PromiseObj, resolution: Value, site?: AstNode): void;
|
|
66
|
+
settle(promise: PromiseObj, completion: Completion): void;
|
|
67
|
+
enqueue(job: Job): void;
|
|
68
|
+
dequeue(): Job | undefined;
|
|
69
|
+
compact(): void;
|
|
70
|
+
park(frames: Array<Frame>, promise: PromiseObj, node: AstNode | undefined, result: Value): void;
|
|
71
|
+
stuckAwait(): AstNode | undefined;
|
|
72
|
+
diagnostics(): Array<Diagnostic>;
|
|
73
|
+
beginJob(job: Job): void;
|
|
74
|
+
enterSettlingCall(callerFrame: Frame | undefined, callable: Value, thisVal: Value, args: Array<Value>, mode: FrameMode, target: SettlingTarget, answer: Value, site: AstNode | undefined): number;
|
|
75
|
+
handlePromisePush(callerFrame: Frame, kindName: string, slots: Array<unknown>, site: AstNode | undefined): number | undefined;
|
|
76
|
+
}
|
|
77
|
+
export declare const registerAsyncIntrinsics: (map: Map<string, unknown>, vm: Vm) => void;
|
|
78
|
+
export {};
|