@absolutejs/ai 0.0.11 → 0.0.12

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.
@@ -116,9 +116,194 @@ var codeExecutionTool = (options = {}) => {
116
116
  }
117
117
  };
118
118
  };
119
+ // src/ai/tools/codeMode.ts
120
+ var buildDescription = (tools, types) => {
121
+ const lines = [
122
+ "Execute JavaScript that calls one or more host tools, returning a",
123
+ "single value. Prefer this over calling individual tools when you'd",
124
+ "otherwise need multiple sequential tool calls \u2014 one Code Mode call",
125
+ "replaces N tool calls plus the intermediate context.",
126
+ "",
127
+ "Input: `{ code: string }`. The code is a function BODY (not a full",
128
+ "function). It can use `await`, `const`/`let`, control flow, etc.",
129
+ "Whatever you `return` becomes the tool output.",
130
+ "",
131
+ "Built-in: `log(...args)` captures messages for debugging. Logs are",
132
+ "returned alongside the result; they don't enter the model context.",
133
+ "",
134
+ "Available host functions (calling these from your code is what runs",
135
+ "the real work; everything else is plain JS):",
136
+ ""
137
+ ];
138
+ for (const [name, tool] of Object.entries(tools)) {
139
+ lines.push(`// ${tool.description}`);
140
+ lines.push(`declare const ${name}: ${tool.tsSignature};`);
141
+ lines.push("");
142
+ }
143
+ if (types !== undefined && types.trim().length > 0) {
144
+ lines.push("// Shared types referenced by the signatures above:");
145
+ lines.push(types.trim());
146
+ lines.push("");
147
+ }
148
+ lines.push("Example:");
149
+ lines.push("```js");
150
+ lines.push("// Get the cheapest matching product and its full record.");
151
+ const firstTool = Object.keys(tools)[0];
152
+ if (firstTool !== undefined) {
153
+ lines.push(`const items = await ${firstTool}('search query');`);
154
+ lines.push("const cheapest = items.sort((a, b) => a.price - b.price)[0];");
155
+ lines.push("return cheapest;");
156
+ } else {
157
+ lines.push("return 42;");
158
+ }
159
+ lines.push("```");
160
+ lines.push("");
161
+ lines.push("Output: JSON with `{ result, log, toolCalls, cpuMs, heapBytes }`.");
162
+ return lines.join(`
163
+ `);
164
+ };
165
+ var codeModeTool = (options) => {
166
+ const memoryLimit = options.memoryLimit ?? 64;
167
+ const timeout = options.timeout ?? 5000;
168
+ const backend = options.backend ?? "auto";
169
+ const poolSize = options.poolSize ?? 8;
170
+ const recycleAfter = options.recycleAfter ?? 50;
171
+ const tools = options.tools;
172
+ const description = options.description ?? buildDescription(tools, options.types);
173
+ let cachedPool = null;
174
+ let cachedPoolPromise = null;
175
+ const loadPool = async () => {
176
+ if (cachedPool !== null)
177
+ return cachedPool;
178
+ if (cachedPoolPromise !== null)
179
+ return cachedPoolPromise;
180
+ cachedPoolPromise = (async () => {
181
+ const jsc = await import("@absolutejs/isolated-jsc");
182
+ const pool = jsc.createIsolatePool({
183
+ isolate: { backend, memoryLimit },
184
+ maxSize: poolSize,
185
+ recycleAfter
186
+ });
187
+ cachedPool = { pool, jsc };
188
+ return cachedPool;
189
+ })();
190
+ return cachedPoolPromise;
191
+ };
192
+ const runCode = async (code) => {
193
+ const log = [];
194
+ const toolCalls = [];
195
+ let cpuMs = 0;
196
+ let heapBytes = 0;
197
+ try {
198
+ const { pool, jsc } = await loadPool();
199
+ return await pool.run("default", async (isolate) => {
200
+ const context = await isolate.createContext();
201
+ try {
202
+ await context.setGlobal("log", new jsc.Reference((...args) => {
203
+ log.push(args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" "));
204
+ }));
205
+ for (const [name, tool] of Object.entries(tools)) {
206
+ await context.setGlobal(name, new jsc.Reference((...args) => {
207
+ const startedAt = performance.now();
208
+ const recordSuccess = () => {
209
+ toolCalls.push({
210
+ args,
211
+ durationMs: performance.now() - startedAt,
212
+ name,
213
+ ok: true
214
+ });
215
+ };
216
+ const recordError = (err) => {
217
+ toolCalls.push({
218
+ args,
219
+ durationMs: performance.now() - startedAt,
220
+ error: err instanceof Error ? err.message : String(err),
221
+ name,
222
+ ok: false
223
+ });
224
+ };
225
+ let outcome;
226
+ try {
227
+ outcome = tool.handler(...args);
228
+ } catch (err) {
229
+ recordError(err);
230
+ throw err;
231
+ }
232
+ if (outcome !== null && typeof outcome === "object" && "then" in outcome && typeof outcome.then === "function") {
233
+ return outcome.then((v) => {
234
+ recordSuccess();
235
+ return v;
236
+ }, (err) => {
237
+ recordError(err);
238
+ throw err;
239
+ });
240
+ }
241
+ recordSuccess();
242
+ return outcome;
243
+ }));
244
+ }
245
+ const wrapped = `(async () => { ${code}
246
+ })()`;
247
+ const script = await isolate.compileScript(wrapped);
248
+ const { result, metrics } = await script.runWithMetrics(context, {
249
+ timeout
250
+ });
251
+ cpuMs = metrics.cpuMs;
252
+ heapBytes = metrics.heapBytes;
253
+ return { cpuMs, heapBytes, log, ok: true, result, toolCalls };
254
+ } finally {
255
+ await context.dispose().catch(() => {});
256
+ }
257
+ });
258
+ } catch (error) {
259
+ const name = error instanceof Error ? error.name : "Error";
260
+ const message = error instanceof Error ? error.message : String(error);
261
+ return {
262
+ cpuMs,
263
+ error: { message, name },
264
+ heapBytes,
265
+ log,
266
+ ok: false,
267
+ toolCalls
268
+ };
269
+ }
270
+ };
271
+ return {
272
+ description,
273
+ handler: async (input) => {
274
+ const code = input !== null && typeof input === "object" && "code" in input ? input.code : undefined;
275
+ if (typeof code !== "string") {
276
+ return JSON.stringify({
277
+ cpuMs: 0,
278
+ error: {
279
+ message: "expected `{ code: string }`",
280
+ name: "InvalidInput"
281
+ },
282
+ heapBytes: 0,
283
+ log: [],
284
+ ok: false,
285
+ toolCalls: []
286
+ });
287
+ }
288
+ const run = await runCode(code);
289
+ return JSON.stringify(run);
290
+ },
291
+ input: {
292
+ properties: {
293
+ code: {
294
+ description: "JavaScript function-body source. Use `await` to call host tools; `return` the final value. Multiple tool calls in one " + "block are encouraged \u2014 that's the whole point of Code Mode.",
295
+ type: "string"
296
+ }
297
+ },
298
+ required: ["code"],
299
+ type: "object"
300
+ }
301
+ };
302
+ };
119
303
  export {
304
+ codeModeTool,
120
305
  codeExecutionTool
121
306
  };
122
307
 
123
- //# debugId=3DCC3269A76159BC64756E2164756E21
308
+ //# debugId=05A8D1541DB1A68E64756E2164756E21
124
309
  //# sourceMappingURL=index.js.map
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/ai/tools/codeExecution.ts"],
3
+ "sources": ["../src/ai/tools/codeExecution.ts", "../src/ai/tools/codeMode.ts"],
4
4
  "sourcesContent": [
5
- "/**\n * `codeExecutionTool` — an `AIToolDefinition` that runs model-generated\n * JavaScript inside an `@absolutejs/isolated-jsc` sandbox.\n *\n * Drop into any `tools: {...}` map:\n *\n * ```ts\n * import { codeExecutionTool } from '@absolutejs/ai/tools';\n *\n * const tools = {\n * run_code: codeExecutionTool({\n * memoryLimit: 64,\n * timeout: 1000,\n * expose: {\n * lookup_user: async (id) => db.users.findById(id as string),\n * round: (n) => Math.round(n as number),\n * },\n * }),\n * };\n * ```\n *\n * The model emits `{ code: '<JS source>' }` as the tool input; the host\n * runs the code in a fresh context inside a pooled isolate and returns\n * a JSON-stringified result containing:\n *\n * - `result` — the script's return value (JSON-clonable).\n * - `log` — array of strings captured via the host-injected `log(...)`.\n * - `error` — error message + name if the script threw or timed out.\n * - `cpuMs`, `heapBytes` — per-call telemetry (Phase 3 docs / monitoring).\n *\n * Defaults to the FFI backend on macOS + Linux (with libJSC installed),\n * Worker fallback elsewhere. Per-isolate pool is created once per\n * `codeExecutionTool()` call; pool key is `'default'` (one isolate for\n * all calls). For per-tenant isolation, create one tool instance per\n * tenant.\n *\n * Constraint: when using the FFI backend, **exposed host fns must be\n * synchronous**. Async host fns (returning a Promise that doesn't settle\n * synchronously — `fetch`, `setTimeout`-resolved Promises, real I/O)\n * require `backend: 'worker'` per isolated-jsc 0.3 documented limit.\n * Set `backend: 'worker'` in the tool options if any of your `expose`d\n * fns are async-settling.\n */\n\nimport type { AIToolDefinition } from \"../../../types/ai\";\n\n/** Options for {@link codeExecutionTool}. */\nexport type CodeExecutionToolOptions = {\n /**\n * Per-isolate heap memory cap (MB). Default 64. Note that the\n * sandbox's cold-start baseline differs by backend (FFI ~300 KB vs\n * Worker ~46 MB), so the practical floor for Worker is ~64 MB.\n */\n memoryLimit?: number;\n /** Wall-clock timeout per `run_code` call (ms). Default 1000. */\n timeout?: number;\n /**\n * isolated-jsc backend. Default `\"auto\"` (FFI when reachable, Worker\n * otherwise). Set to `\"worker\"` if your `expose`d fns are async-settling\n * — the FFI backend only supports sync host fns (see Reference docs).\n */\n backend?: \"auto\" | \"ffi\" | \"worker\";\n /**\n * Host functions the model can call from inside the sandbox. Names\n * become globals; the model invokes them with `await name(...)`.\n * The function's description (`.toString()` first line, if a doc\n * comment) is included in the tool's description so the model knows\n * what's available.\n */\n expose?: Record<string, (...args: unknown[]) => unknown>;\n /**\n * Override the tool's description string. Default is auto-generated\n * from the exposed function list.\n */\n description?: string;\n /**\n * Pool size cap — max concurrent isolates across all parallel tool\n * calls. Default 8.\n */\n poolSize?: number;\n /**\n * Recycle the isolate after N successful runs to bound per-context\n * heap creep. Default 50.\n */\n recycleAfter?: number;\n};\n\ntype Run = {\n ok: boolean;\n result?: unknown;\n log: string[];\n error?: { name: string; message: string };\n cpuMs: number;\n heapBytes: number;\n};\n\nconst defaultDescription = (\n expose: Record<string, (...args: unknown[]) => unknown> | undefined,\n): string => {\n const lines = [\n \"Execute JavaScript code in a sandboxed environment.\",\n \"\",\n \"Input: `{ code: string }` — the JS source to evaluate. The script's last\",\n \"expression is the return value (vm.Script semantics).\",\n \"\",\n \"Output: JSON with `{ result, log, error?, cpuMs, heapBytes }`.\",\n \"\",\n \"Built-in: `log(...args)` captures stdout-like output into the result.log\",\n \"array.\",\n ];\n const exposed = expose ? Object.keys(expose) : [];\n if (exposed.length > 0) {\n lines.push(\"\");\n lines.push(\"Host functions available in the sandbox:\");\n for (const name of exposed) {\n lines.push(` - ${name}(...)`);\n }\n }\n return lines.join(\"\\n\");\n};\n\nexport const codeExecutionTool = (\n options: CodeExecutionToolOptions = {},\n): AIToolDefinition => {\n const memoryLimit = options.memoryLimit ?? 64;\n const timeout = options.timeout ?? 1000;\n const backend = options.backend ?? \"auto\";\n const expose = options.expose ?? {};\n const poolSize = options.poolSize ?? 8;\n const recycleAfter = options.recycleAfter ?? 50;\n const description = options.description ?? defaultDescription(expose);\n\n // Lazy import isolated-jsc so this module loads without it (consumers\n // who never call the tool don't pay the dep). The first call resolves\n // and caches the pool; later calls reuse it.\n type IsolatedJsc = typeof import(\"@absolutejs/isolated-jsc\");\n let cachedPool:\n | { pool: ReturnType<IsolatedJsc[\"createIsolatePool\"]>; jsc: IsolatedJsc }\n | null = null;\n let cachedPoolPromise: Promise<{\n pool: ReturnType<IsolatedJsc[\"createIsolatePool\"]>;\n jsc: IsolatedJsc;\n }> | null = null;\n\n const loadPool = async () => {\n if (cachedPool !== null) return cachedPool;\n if (cachedPoolPromise !== null) return cachedPoolPromise;\n cachedPoolPromise = (async () => {\n const jsc = (await import(\"@absolutejs/isolated-jsc\")) as IsolatedJsc;\n const pool = jsc.createIsolatePool({\n isolate: { backend, memoryLimit },\n maxSize: poolSize,\n recycleAfter,\n });\n cachedPool = { pool, jsc };\n return cachedPool;\n })();\n return cachedPoolPromise;\n };\n\n const runCode = async (code: string): Promise<Run> => {\n const log: string[] = [];\n let cpuMs = 0;\n let heapBytes = 0;\n try {\n const { pool, jsc } = await loadPool();\n return await pool.run(\"default\", async (isolate) => {\n const context = await isolate.createContext();\n try {\n // Built-in log capture. Sync host fn → works on FFI + Worker.\n await context.setGlobal(\n \"log\",\n new jsc.Reference((...args: unknown[]) => {\n log.push(\n args\n .map((a) =>\n typeof a === \"string\" ? a : JSON.stringify(a),\n )\n .join(\" \"),\n );\n }),\n );\n // Exposed host fns. Names become globals; user code calls them\n // directly (FFI sync path) or with `await` (Worker path).\n for (const [name, fn] of Object.entries(expose)) {\n await context.setGlobal(name, new jsc.Reference(fn));\n }\n // Wrap the user code so the last expression is what's returned\n // (matching the conventional script-result semantics).\n const script = await isolate.compileScript(code);\n const { result, metrics } = await script.runWithMetrics(\n context,\n { timeout },\n );\n cpuMs = metrics.cpuMs;\n heapBytes = metrics.heapBytes;\n return { ok: true, result, log, cpuMs, heapBytes };\n } finally {\n await context.dispose().catch(() => {\n /* dead context — fine */\n });\n }\n });\n } catch (error) {\n const name =\n error instanceof Error ? error.name : \"Error\";\n const message =\n error instanceof Error ? error.message : String(error);\n return {\n ok: false,\n log,\n error: { name, message },\n cpuMs,\n heapBytes,\n };\n }\n };\n\n return {\n description,\n input: {\n type: \"object\",\n properties: {\n code: {\n type: \"string\",\n description:\n \"JavaScript source to evaluate. The script's last expression \" +\n \"is the return value. Use `log(...)` for stdout-like output.\",\n },\n },\n required: [\"code\"],\n },\n handler: async (input: unknown) => {\n const code =\n input && typeof input === \"object\" && \"code\" in input\n ? (input as { code: unknown }).code\n : undefined;\n if (typeof code !== \"string\") {\n return JSON.stringify({\n ok: false,\n error: { name: \"InvalidInput\", message: \"expected `{ code: string }`\" },\n log: [],\n cpuMs: 0,\n heapBytes: 0,\n });\n }\n const run = await runCode(code);\n return JSON.stringify(run);\n },\n };\n};\n"
5
+ "/**\n * `codeExecutionTool` — an `AIToolDefinition` that runs model-generated\n * JavaScript inside an `@absolutejs/isolated-jsc` sandbox.\n *\n * Drop into any `tools: {...}` map:\n *\n * ```ts\n * import { codeExecutionTool } from '@absolutejs/ai/tools';\n *\n * const tools = {\n * run_code: codeExecutionTool({\n * memoryLimit: 64,\n * timeout: 1000,\n * expose: {\n * lookup_user: async (id) => db.users.findById(id as string),\n * round: (n) => Math.round(n as number),\n * },\n * }),\n * };\n * ```\n *\n * The model emits `{ code: '<JS source>' }` as the tool input; the host\n * runs the code in a fresh context inside a pooled isolate and returns\n * a JSON-stringified result containing:\n *\n * - `result` — the script's return value (JSON-clonable).\n * - `log` — array of strings captured via the host-injected `log(...)`.\n * - `error` — error message + name if the script threw or timed out.\n * - `cpuMs`, `heapBytes` — per-call telemetry (Phase 3 docs / monitoring).\n *\n * Defaults to the FFI backend on macOS + Linux (with libJSC installed),\n * Worker fallback elsewhere. Per-isolate pool is created once per\n * `codeExecutionTool()` call; pool key is `'default'` (one isolate for\n * all calls). For per-tenant isolation, create one tool instance per\n * tenant.\n *\n * Constraint: when using the FFI backend, **exposed host fns must be\n * synchronous**. Async host fns (returning a Promise that doesn't settle\n * synchronously — `fetch`, `setTimeout`-resolved Promises, real I/O)\n * require `backend: 'worker'` per isolated-jsc 0.3 documented limit.\n * Set `backend: 'worker'` in the tool options if any of your `expose`d\n * fns are async-settling.\n */\n\nimport type { AIToolDefinition } from \"../../../types/ai\";\n\n/** Options for {@link codeExecutionTool}. */\nexport type CodeExecutionToolOptions = {\n /**\n * Per-isolate heap memory cap (MB). Default 64. Note that the\n * sandbox's cold-start baseline differs by backend (FFI ~300 KB vs\n * Worker ~46 MB), so the practical floor for Worker is ~64 MB.\n */\n memoryLimit?: number;\n /** Wall-clock timeout per `run_code` call (ms). Default 1000. */\n timeout?: number;\n /**\n * isolated-jsc backend. Default `\"auto\"` (FFI when reachable, Worker\n * otherwise). Set to `\"worker\"` if your `expose`d fns are async-settling\n * — the FFI backend only supports sync host fns (see Reference docs).\n */\n backend?: \"auto\" | \"ffi\" | \"worker\";\n /**\n * Host functions the model can call from inside the sandbox. Names\n * become globals; the model invokes them with `await name(...)`.\n * The function's description (`.toString()` first line, if a doc\n * comment) is included in the tool's description so the model knows\n * what's available.\n */\n expose?: Record<string, (...args: unknown[]) => unknown>;\n /**\n * Override the tool's description string. Default is auto-generated\n * from the exposed function list.\n */\n description?: string;\n /**\n * Pool size cap — max concurrent isolates across all parallel tool\n * calls. Default 8.\n */\n poolSize?: number;\n /**\n * Recycle the isolate after N successful runs to bound per-context\n * heap creep. Default 50.\n */\n recycleAfter?: number;\n};\n\ntype Run = {\n ok: boolean;\n result?: unknown;\n log: string[];\n error?: { name: string; message: string };\n cpuMs: number;\n heapBytes: number;\n};\n\nconst defaultDescription = (\n expose: Record<string, (...args: unknown[]) => unknown> | undefined,\n): string => {\n const lines = [\n \"Execute JavaScript code in a sandboxed environment.\",\n \"\",\n \"Input: `{ code: string }` — the JS source to evaluate. The script's last\",\n \"expression is the return value (vm.Script semantics).\",\n \"\",\n \"Output: JSON with `{ result, log, error?, cpuMs, heapBytes }`.\",\n \"\",\n \"Built-in: `log(...args)` captures stdout-like output into the result.log\",\n \"array.\",\n ];\n const exposed = expose ? Object.keys(expose) : [];\n if (exposed.length > 0) {\n lines.push(\"\");\n lines.push(\"Host functions available in the sandbox:\");\n for (const name of exposed) {\n lines.push(` - ${name}(...)`);\n }\n }\n return lines.join(\"\\n\");\n};\n\nexport const codeExecutionTool = (\n options: CodeExecutionToolOptions = {},\n): AIToolDefinition => {\n const memoryLimit = options.memoryLimit ?? 64;\n const timeout = options.timeout ?? 1000;\n const backend = options.backend ?? \"auto\";\n const expose = options.expose ?? {};\n const poolSize = options.poolSize ?? 8;\n const recycleAfter = options.recycleAfter ?? 50;\n const description = options.description ?? defaultDescription(expose);\n\n // Lazy import isolated-jsc so this module loads without it (consumers\n // who never call the tool don't pay the dep). The first call resolves\n // and caches the pool; later calls reuse it.\n type IsolatedJsc = typeof import(\"@absolutejs/isolated-jsc\");\n let cachedPool:\n | { pool: ReturnType<IsolatedJsc[\"createIsolatePool\"]>; jsc: IsolatedJsc }\n | null = null;\n let cachedPoolPromise: Promise<{\n pool: ReturnType<IsolatedJsc[\"createIsolatePool\"]>;\n jsc: IsolatedJsc;\n }> | null = null;\n\n const loadPool = async () => {\n if (cachedPool !== null) return cachedPool;\n if (cachedPoolPromise !== null) return cachedPoolPromise;\n cachedPoolPromise = (async () => {\n const jsc = (await import(\"@absolutejs/isolated-jsc\")) as IsolatedJsc;\n const pool = jsc.createIsolatePool({\n isolate: { backend, memoryLimit },\n maxSize: poolSize,\n recycleAfter,\n });\n cachedPool = { pool, jsc };\n return cachedPool;\n })();\n return cachedPoolPromise;\n };\n\n const runCode = async (code: string): Promise<Run> => {\n const log: string[] = [];\n let cpuMs = 0;\n let heapBytes = 0;\n try {\n const { pool, jsc } = await loadPool();\n return await pool.run(\"default\", async (isolate) => {\n const context = await isolate.createContext();\n try {\n // Built-in log capture. Sync host fn → works on FFI + Worker.\n await context.setGlobal(\n \"log\",\n new jsc.Reference((...args: unknown[]) => {\n log.push(\n args\n .map((a) =>\n typeof a === \"string\" ? a : JSON.stringify(a),\n )\n .join(\" \"),\n );\n }),\n );\n // Exposed host fns. Names become globals; user code calls them\n // directly (FFI sync path) or with `await` (Worker path).\n for (const [name, fn] of Object.entries(expose)) {\n await context.setGlobal(name, new jsc.Reference(fn));\n }\n // Wrap the user code so the last expression is what's returned\n // (matching the conventional script-result semantics).\n const script = await isolate.compileScript(code);\n const { result, metrics } = await script.runWithMetrics(\n context,\n { timeout },\n );\n cpuMs = metrics.cpuMs;\n heapBytes = metrics.heapBytes;\n return { ok: true, result, log, cpuMs, heapBytes };\n } finally {\n await context.dispose().catch(() => {\n /* dead context — fine */\n });\n }\n });\n } catch (error) {\n const name =\n error instanceof Error ? error.name : \"Error\";\n const message =\n error instanceof Error ? error.message : String(error);\n return {\n ok: false,\n log,\n error: { name, message },\n cpuMs,\n heapBytes,\n };\n }\n };\n\n return {\n description,\n input: {\n type: \"object\",\n properties: {\n code: {\n type: \"string\",\n description:\n \"JavaScript source to evaluate. The script's last expression \" +\n \"is the return value. Use `log(...)` for stdout-like output.\",\n },\n },\n required: [\"code\"],\n },\n handler: async (input: unknown) => {\n const code =\n input && typeof input === \"object\" && \"code\" in input\n ? (input as { code: unknown }).code\n : undefined;\n if (typeof code !== \"string\") {\n return JSON.stringify({\n ok: false,\n error: { name: \"InvalidInput\", message: \"expected `{ code: string }`\" },\n log: [],\n cpuMs: 0,\n heapBytes: 0,\n });\n }\n const run = await runCode(code);\n return JSON.stringify(run);\n },\n };\n};\n",
6
+ "/**\n * `codeModeTool` — Code Mode for AI agents.\n *\n * Instead of exposing N tools to the model and having it call them one\n * at a time (N round-trips per turn, N tool-call tokens, the model has\n * to track intermediate state in context), Code Mode exposes ONE tool:\n * `run_code`. The model sees the typed TypeScript signatures of all\n * underlying tools and emits a single function that chains them.\n *\n * Pattern was popularized by Cloudflare's Dynamic Workers (April 2026\n * blog post: \"100× faster than containers\"). Anthropic's programmatic\n * tool calling is the same idea — execution pauses on a sub-tool call,\n * the API yields a tool_use, you return a result, execution resumes.\n * Both vendors report ~80% token reduction on multi-tool turns.\n *\n * ```ts\n * import { codeModeTool } from '@absolutejs/ai/tools';\n *\n * const tools = {\n * run_code: codeModeTool({\n * timeout: 5000,\n * tools: {\n * search_products: {\n * description: 'Full-text search the product catalogue.',\n * tsSignature: '(query: string) => Promise<Product[]>',\n * handler: async (q) => db.products.search(q as string),\n * },\n * get_product: {\n * description: 'Fetch one product by id.',\n * tsSignature: '(id: string) => Promise<Product | null>',\n * handler: async (id) => db.products.findById(id as string),\n * },\n * },\n * types: `\n * type Product = { id: string; name: string; price: number };\n * `,\n * }),\n * };\n * ```\n *\n * The model emits a single function:\n *\n * ```js\n * const items = await search_products('hat');\n * const cheapest = items.sort((a, b) => a.price - b.price)[0];\n * const detail = await get_product(cheapest.id);\n * return { name: detail.name, price: detail.price };\n * ```\n *\n * One sandbox eval. Two host-fn calls. One returned value. The model's\n * context only ever sees the final return — intermediate tool results\n * don't enter the conversation window, so multi-step workflows are\n * dramatically cheaper.\n *\n * Each underlying tool's `handler` runs on the HOST side (not in the\n * sandbox). Async host fns work on both FFI (via the 0.4 pump) and\n * Worker backends since isolated-jsc 0.4+. Errors thrown by host\n * handlers propagate into the sandbox as JS Errors the model can\n * catch and recover from.\n */\n\nimport type { AIToolDefinition } from \"../../../types/ai\";\n\n/**\n * One callable surfaced to the sandbox. The `tsSignature` shows up in\n * the model-visible description; `handler` runs on the host when the\n * sandbox calls it.\n */\nexport type CodeModeHostTool = {\n /** One-line human description of what this tool does. */\n description: string;\n /** TypeScript signature shown to the model. Example:\n * `'(query: string, options?: { limit?: number }) => Promise<Item[]>'`.\n * The model writes JS against this signature; we don't enforce it at\n * runtime — type-check is the model's responsibility. */\n tsSignature: string;\n /** Host implementation. Receives positional args as the model passed\n * them. Return value is structure-cloned back into the sandbox. */\n handler: (...args: unknown[]) => unknown;\n};\n\n/** Options for {@link codeModeTool}. */\nexport type CodeModeToolOptions = {\n /** Map of host-tool name → {@link CodeModeHostTool}. */\n tools: Record<string, CodeModeHostTool>;\n /**\n * Optional shared TypeScript declarations stitched into the prompt\n * (type aliases, interfaces, etc.) so signatures can reference them.\n * Use raw TS source; no parsing happens host-side.\n */\n types?: string;\n /**\n * Per-isolate heap memory cap (MB). Default 64. As with the regular\n * code-execution tool, FFI's cold heap is much smaller than Worker's,\n * but per-call retention scales similarly.\n */\n memoryLimit?: number;\n /** Wall-clock timeout per `run_code` call (ms). Default 5000. */\n timeout?: number;\n /**\n * isolated-jsc backend. Defaults to `'auto'`. Since isolated-jsc 0.4\n * both backends support async host fns, so the choice is purely\n * about cold spawn (FFI wins ~6×) vs Web APIs availability (Worker\n * has `URL` / `TextEncoder` / `WebSocket`; FFI does not).\n */\n backend?: \"auto\" | \"ffi\" | \"worker\";\n /**\n * Override the auto-generated description. By default we emit the\n * model-facing prompt: a short instruction header + the host fn\n * signatures + any shared `types`.\n */\n description?: string;\n /** Pool size cap. Default 8. */\n poolSize?: number;\n /** Recycle the isolate after N successful runs. Default 50. */\n recycleAfter?: number;\n};\n\ntype RunResult = {\n ok: boolean;\n result?: unknown;\n log: string[];\n toolCalls: Array<{\n name: string;\n args: unknown[];\n durationMs: number;\n ok: boolean;\n error?: string;\n }>;\n error?: { name: string; message: string };\n cpuMs: number;\n heapBytes: number;\n};\n\nconst buildDescription = (\n tools: Record<string, CodeModeHostTool>,\n types: string | undefined,\n): string => {\n const lines: string[] = [\n \"Execute JavaScript that calls one or more host tools, returning a\",\n \"single value. Prefer this over calling individual tools when you'd\",\n \"otherwise need multiple sequential tool calls — one Code Mode call\",\n \"replaces N tool calls plus the intermediate context.\",\n \"\",\n \"Input: `{ code: string }`. The code is a function BODY (not a full\",\n \"function). It can use `await`, `const`/`let`, control flow, etc.\",\n \"Whatever you `return` becomes the tool output.\",\n \"\",\n \"Built-in: `log(...args)` captures messages for debugging. Logs are\",\n \"returned alongside the result; they don't enter the model context.\",\n \"\",\n \"Available host functions (calling these from your code is what runs\",\n \"the real work; everything else is plain JS):\",\n \"\",\n ];\n for (const [name, tool] of Object.entries(tools)) {\n lines.push(`// ${tool.description}`);\n lines.push(`declare const ${name}: ${tool.tsSignature};`);\n lines.push(\"\");\n }\n if (types !== undefined && types.trim().length > 0) {\n lines.push(\"// Shared types referenced by the signatures above:\");\n lines.push(types.trim());\n lines.push(\"\");\n }\n lines.push(\"Example:\");\n lines.push(\"```js\");\n lines.push(\"// Get the cheapest matching product and its full record.\");\n const firstTool = Object.keys(tools)[0];\n if (firstTool !== undefined) {\n lines.push(`const items = await ${firstTool}('search query');`);\n lines.push(\n \"const cheapest = items.sort((a, b) => a.price - b.price)[0];\",\n );\n lines.push(\"return cheapest;\");\n } else {\n lines.push(\"return 42;\");\n }\n lines.push(\"```\");\n lines.push(\"\");\n lines.push(\"Output: JSON with `{ result, log, toolCalls, cpuMs, heapBytes }`.\");\n return lines.join(\"\\n\");\n};\n\nexport const codeModeTool = (\n options: CodeModeToolOptions,\n): AIToolDefinition => {\n const memoryLimit = options.memoryLimit ?? 64;\n const timeout = options.timeout ?? 5000;\n const backend = options.backend ?? \"auto\";\n const poolSize = options.poolSize ?? 8;\n const recycleAfter = options.recycleAfter ?? 50;\n const tools = options.tools;\n const description =\n options.description ?? buildDescription(tools, options.types);\n\n type IsolatedJsc = typeof import(\"@absolutejs/isolated-jsc\");\n let cachedPool:\n | { pool: ReturnType<IsolatedJsc[\"createIsolatePool\"]>; jsc: IsolatedJsc }\n | null = null;\n let cachedPoolPromise: Promise<{\n pool: ReturnType<IsolatedJsc[\"createIsolatePool\"]>;\n jsc: IsolatedJsc;\n }> | null = null;\n\n const loadPool = async () => {\n if (cachedPool !== null) return cachedPool;\n if (cachedPoolPromise !== null) return cachedPoolPromise;\n cachedPoolPromise = (async () => {\n const jsc = (await import(\"@absolutejs/isolated-jsc\")) as IsolatedJsc;\n const pool = jsc.createIsolatePool({\n isolate: { backend, memoryLimit },\n maxSize: poolSize,\n recycleAfter,\n });\n cachedPool = { pool, jsc };\n return cachedPool;\n })();\n return cachedPoolPromise;\n };\n\n const runCode = async (code: string): Promise<RunResult> => {\n const log: string[] = [];\n const toolCalls: RunResult[\"toolCalls\"] = [];\n let cpuMs = 0;\n let heapBytes = 0;\n try {\n const { pool, jsc } = await loadPool();\n return await pool.run(\"default\", async (isolate) => {\n const context = await isolate.createContext();\n try {\n // Built-in log capture.\n await context.setGlobal(\n \"log\",\n new jsc.Reference((...args: unknown[]) => {\n log.push(\n args\n .map((a) =>\n typeof a === \"string\" ? a : JSON.stringify(a),\n )\n .join(\" \"),\n );\n }),\n );\n\n // Bind each host tool as a global Reference. Wrap so we\n // capture per-call telemetry (durationMs, error). Async\n // host fns work on both FFI (0.4+) and Worker backends.\n for (const [name, tool] of Object.entries(tools)) {\n await context.setGlobal(\n name,\n new jsc.Reference(((...args: unknown[]) => {\n const startedAt = performance.now();\n const recordSuccess = () => {\n toolCalls.push({\n args,\n durationMs: performance.now() - startedAt,\n name,\n ok: true,\n });\n };\n const recordError = (err: unknown) => {\n toolCalls.push({\n args,\n durationMs: performance.now() - startedAt,\n error: err instanceof Error ? err.message : String(err),\n name,\n ok: false,\n });\n };\n let outcome: unknown;\n try {\n outcome = tool.handler(...args);\n } catch (err) {\n recordError(err);\n throw err;\n }\n if (\n outcome !== null &&\n typeof outcome === \"object\" &&\n \"then\" in outcome &&\n typeof (outcome as { then: unknown }).then === \"function\"\n ) {\n return (outcome as Promise<unknown>).then(\n (v) => {\n recordSuccess();\n return v;\n },\n (err) => {\n recordError(err);\n throw err;\n },\n );\n }\n recordSuccess();\n return outcome;\n }) as (...args: unknown[]) => unknown),\n );\n }\n\n // Wrap the model's code as an async function body. Whatever\n // it `return`s becomes the tool output.\n const wrapped = `(async () => { ${code}\\n})()`;\n const script = await isolate.compileScript(wrapped);\n const { result, metrics } = await script.runWithMetrics(context, {\n timeout,\n });\n cpuMs = metrics.cpuMs;\n heapBytes = metrics.heapBytes;\n return { cpuMs, heapBytes, log, ok: true, result, toolCalls };\n } finally {\n await context.dispose().catch(() => {\n /* ok */\n });\n }\n });\n } catch (error) {\n const name = error instanceof Error ? error.name : \"Error\";\n const message =\n error instanceof Error ? error.message : String(error);\n return {\n cpuMs,\n error: { message, name },\n heapBytes,\n log,\n ok: false,\n toolCalls,\n };\n }\n };\n\n return {\n description,\n handler: async (input: unknown) => {\n const code =\n input !== null &&\n typeof input === \"object\" &&\n \"code\" in input\n ? (input as { code: unknown }).code\n : undefined;\n if (typeof code !== \"string\") {\n return JSON.stringify({\n cpuMs: 0,\n error: {\n message: \"expected `{ code: string }`\",\n name: \"InvalidInput\",\n },\n heapBytes: 0,\n log: [],\n ok: false,\n toolCalls: [],\n });\n }\n const run = await runCode(code);\n return JSON.stringify(run);\n },\n input: {\n properties: {\n code: {\n description:\n \"JavaScript function-body source. Use `await` to call host \" +\n \"tools; `return` the final value. Multiple tool calls in one \" +\n \"block are encouraged — that's the whole point of Code Mode.\",\n type: \"string\",\n },\n },\n required: [\"code\"],\n type: \"object\",\n },\n };\n};\n"
6
7
  ],
7
- "mappings": ";;;;AAgGA,IAAM,qBAAqB,CACzB,WACW;AAAA,EACX,MAAM,QAAQ;AAAA,IACZ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,MAAM,UAAU,SAAS,OAAO,KAAK,MAAM,IAAI,CAAC;AAAA,EAChD,IAAI,QAAQ,SAAS,GAAG;AAAA,IACtB,MAAM,KAAK,EAAE;AAAA,IACb,MAAM,KAAK,0CAA0C;AAAA,IACrD,WAAW,QAAQ,SAAS;AAAA,MAC1B,MAAM,KAAK,OAAO,WAAW;AAAA,IAC/B;AAAA,EACF;AAAA,EACA,OAAO,MAAM,KAAK;AAAA,CAAI;AAAA;AAGjB,IAAM,oB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AM,OACJ,UAAU,QACV,OAAO,UAAU,YACjB,UAAU,QACL,MAA4B,OAC7B;AAAA,MACN,IAAI,OAAO,SAAS,UAAU;AAAA,QAC5B,OAAO,KAAK,UAAU;AAAA,UACpB,OAAO;AAAA,UACP,OAAO;AAAA,YACL,SAAS;AAAA,YACT,MAAM;AAAA,UACR;AAAA,UACA,WAAW;AAAA,UACX,KAAK,CAAC;AAAA,UACN,IAAI;AAAA,UACJ,WAAW,CAAC;AAAA,QACd,CAAC;AAAA,MACH;AAAA,MACA,MAAM,MAAM,MAAM,QAAQ,IAAI;AAAA,MAC9B,OAAO,KAAK,UAAU,GAAG;AAAA;AAAA,IAE3B,OAAO;AAAA,MACL,YAAY;AAAA,QACV,MAAM;AAAA,UACJ,aACE,2HAEA;AAAA,UACF,MAAM;AAAA,QACR;AAAA,MACF;AAAA,MACA,UAAU,CAAC,MAAM;AAAA,MACjB,MAAM;AAAA,IACR;AAAA,EACF;AAAA;",
9
+ "debugId": "05A8D1541DB1A68E64756E2164756E21",
9
10
  "names": []
10
11
  }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * `codeModeTool` — Code Mode for AI agents.
3
+ *
4
+ * Instead of exposing N tools to the model and having it call them one
5
+ * at a time (N round-trips per turn, N tool-call tokens, the model has
6
+ * to track intermediate state in context), Code Mode exposes ONE tool:
7
+ * `run_code`. The model sees the typed TypeScript signatures of all
8
+ * underlying tools and emits a single function that chains them.
9
+ *
10
+ * Pattern was popularized by Cloudflare's Dynamic Workers (April 2026
11
+ * blog post: "100× faster than containers"). Anthropic's programmatic
12
+ * tool calling is the same idea — execution pauses on a sub-tool call,
13
+ * the API yields a tool_use, you return a result, execution resumes.
14
+ * Both vendors report ~80% token reduction on multi-tool turns.
15
+ *
16
+ * ```ts
17
+ * import { codeModeTool } from '@absolutejs/ai/tools';
18
+ *
19
+ * const tools = {
20
+ * run_code: codeModeTool({
21
+ * timeout: 5000,
22
+ * tools: {
23
+ * search_products: {
24
+ * description: 'Full-text search the product catalogue.',
25
+ * tsSignature: '(query: string) => Promise<Product[]>',
26
+ * handler: async (q) => db.products.search(q as string),
27
+ * },
28
+ * get_product: {
29
+ * description: 'Fetch one product by id.',
30
+ * tsSignature: '(id: string) => Promise<Product | null>',
31
+ * handler: async (id) => db.products.findById(id as string),
32
+ * },
33
+ * },
34
+ * types: `
35
+ * type Product = { id: string; name: string; price: number };
36
+ * `,
37
+ * }),
38
+ * };
39
+ * ```
40
+ *
41
+ * The model emits a single function:
42
+ *
43
+ * ```js
44
+ * const items = await search_products('hat');
45
+ * const cheapest = items.sort((a, b) => a.price - b.price)[0];
46
+ * const detail = await get_product(cheapest.id);
47
+ * return { name: detail.name, price: detail.price };
48
+ * ```
49
+ *
50
+ * One sandbox eval. Two host-fn calls. One returned value. The model's
51
+ * context only ever sees the final return — intermediate tool results
52
+ * don't enter the conversation window, so multi-step workflows are
53
+ * dramatically cheaper.
54
+ *
55
+ * Each underlying tool's `handler` runs on the HOST side (not in the
56
+ * sandbox). Async host fns work on both FFI (via the 0.4 pump) and
57
+ * Worker backends since isolated-jsc 0.4+. Errors thrown by host
58
+ * handlers propagate into the sandbox as JS Errors the model can
59
+ * catch and recover from.
60
+ */
61
+ import type { AIToolDefinition } from "../../../types/ai";
62
+ /**
63
+ * One callable surfaced to the sandbox. The `tsSignature` shows up in
64
+ * the model-visible description; `handler` runs on the host when the
65
+ * sandbox calls it.
66
+ */
67
+ export type CodeModeHostTool = {
68
+ /** One-line human description of what this tool does. */
69
+ description: string;
70
+ /** TypeScript signature shown to the model. Example:
71
+ * `'(query: string, options?: { limit?: number }) => Promise<Item[]>'`.
72
+ * The model writes JS against this signature; we don't enforce it at
73
+ * runtime — type-check is the model's responsibility. */
74
+ tsSignature: string;
75
+ /** Host implementation. Receives positional args as the model passed
76
+ * them. Return value is structure-cloned back into the sandbox. */
77
+ handler: (...args: unknown[]) => unknown;
78
+ };
79
+ /** Options for {@link codeModeTool}. */
80
+ export type CodeModeToolOptions = {
81
+ /** Map of host-tool name → {@link CodeModeHostTool}. */
82
+ tools: Record<string, CodeModeHostTool>;
83
+ /**
84
+ * Optional shared TypeScript declarations stitched into the prompt
85
+ * (type aliases, interfaces, etc.) so signatures can reference them.
86
+ * Use raw TS source; no parsing happens host-side.
87
+ */
88
+ types?: string;
89
+ /**
90
+ * Per-isolate heap memory cap (MB). Default 64. As with the regular
91
+ * code-execution tool, FFI's cold heap is much smaller than Worker's,
92
+ * but per-call retention scales similarly.
93
+ */
94
+ memoryLimit?: number;
95
+ /** Wall-clock timeout per `run_code` call (ms). Default 5000. */
96
+ timeout?: number;
97
+ /**
98
+ * isolated-jsc backend. Defaults to `'auto'`. Since isolated-jsc 0.4
99
+ * both backends support async host fns, so the choice is purely
100
+ * about cold spawn (FFI wins ~6×) vs Web APIs availability (Worker
101
+ * has `URL` / `TextEncoder` / `WebSocket`; FFI does not).
102
+ */
103
+ backend?: "auto" | "ffi" | "worker";
104
+ /**
105
+ * Override the auto-generated description. By default we emit the
106
+ * model-facing prompt: a short instruction header + the host fn
107
+ * signatures + any shared `types`.
108
+ */
109
+ description?: string;
110
+ /** Pool size cap. Default 8. */
111
+ poolSize?: number;
112
+ /** Recycle the isolate after N successful runs. Default 50. */
113
+ recycleAfter?: number;
114
+ };
115
+ export declare const codeModeTool: (options: CodeModeToolOptions) => AIToolDefinition;
@@ -5,5 +5,9 @@
5
5
  * - {@link codeExecutionTool} — execute model-generated JavaScript in a
6
6
  * sandboxed `@absolutejs/isolated-jsc` isolate. Optional peer; install
7
7
  * `@absolutejs/isolated-jsc` to use it.
8
+ * - {@link codeModeTool} — "Code Mode" wrapper for N host tools: model
9
+ * sees typed TS signatures, emits one function chaining several
10
+ * tool calls, ~80% token reduction vs N separate tool calls.
8
11
  */
9
12
  export { codeExecutionTool, type CodeExecutionToolOptions, } from "./codeExecution";
13
+ export { type CodeModeHostTool, codeModeTool, type CodeModeToolOptions, } from "./codeMode";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@absolutejs/ai",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "AI runtime, providers, streaming, and framework adapters extracted from AbsoluteJS",
5
5
  "license": "BSL-1.1",
6
6
  "type": "module",
@@ -73,7 +73,7 @@
73
73
  "@absolutejs/sync": "0.7.0"
74
74
  },
75
75
  "peerDependencies": {
76
- "@absolutejs/isolated-jsc": ">= 0.3.0",
76
+ "@absolutejs/isolated-jsc": ">= 0.4.0",
77
77
  "@angular/core": "^21.0.0",
78
78
  "elysia": "^1.4.18",
79
79
  "react": "^19.2.0",
@@ -99,7 +99,7 @@
99
99
  },
100
100
  "devDependencies": {
101
101
  "@absolutejs/absolute": "^0.19.0-beta.1051",
102
- "@absolutejs/isolated-jsc": "^0.3.0",
102
+ "@absolutejs/isolated-jsc": "0.6.0",
103
103
  "@angular/core": "^21.0.0",
104
104
  "@eslint/js": "^10.0.1",
105
105
  "@types/bun": "1.3.9",