@absolutejs/ai 0.0.24 → 0.0.25
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/dist/ai/index.js +102 -21
- package/dist/ai/index.js.map +9 -9
- package/dist/ai/providers/anthropic.js +56 -19
- package/dist/ai/providers/anthropic.js.map +4 -4
- package/dist/ai/providers/gemini.js.map +2 -2
- package/dist/ai/providers/ollama.js.map +2 -2
- package/dist/ai/providers/openai.js +2 -7
- package/dist/ai/providers/openai.js.map +3 -3
- package/dist/ai/providers/openaiCompatible.js +2 -7
- package/dist/ai/providers/openaiCompatible.js.map +3 -3
- package/dist/ai/providers/openaiResponses.js +2 -7
- package/dist/ai/providers/openaiResponses.js.map +4 -4
- package/dist/ai/tools/index.js +8 -3
- package/dist/ai/tools/index.js.map +4 -4
- package/dist/types/ai.d.ts +14 -0
- package/dist/types/anthropic.d.ts +9 -0
- package/package.json +172 -172
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"version": 3,
|
|
3
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
|
|
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"
|
|
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\"]>;\n 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) => (typeof a === \"string\" ? a : JSON.stringify(a)))\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(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 = error instanceof Error ? error.name : \"Error\";\n const message = 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: {\n name: \"InvalidInput\",\n message: \"expected `{ code: string }`\",\n },\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(\"const cheapest = items.sort((a, b) => a.price - b.price)[0];\");\n lines.push(\"return cheapest;\");\n } else {\n lines.push(\"return 42;\");\n }\n lines.push(\"```\");\n lines.push(\"\");\n lines.push(\n \"Output: JSON with `{ result, log, toolCalls, cpuMs, heapBytes }`.\",\n );\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\"]>;\n 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) => (typeof a === \"string\" ? a : JSON.stringify(a)))\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 = 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 && typeof input === \"object\" && \"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"
|
|
7
7
|
],
|
|
8
|
-
"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,oBAAoB,CAC/B,UAAoC,CAAC,MAChB;AAAA,EACrB,MAAM,cAAc,QAAQ,eAAe;AAAA,EAC3C,MAAM,UAAU,QAAQ,WAAW;AAAA,EACnC,MAAM,UA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A;AAAA,EAIJ,OAAO;AAAA,IACL;AAAA,IACA,SAAS,OAAO,UAAmB;AAAA,MACjC,MAAM,OACJ,UAAU,QAAQ,OAAO,UAAU,YAAY,UAAU,QACpD,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": "6D9924F82C971C5964756E2164756E21",
|
|
10
10
|
"names": []
|
|
11
11
|
}
|
package/dist/types/ai.d.ts
CHANGED
|
@@ -111,6 +111,7 @@ export type AIToolUseChunk = {
|
|
|
111
111
|
export type AIDoneChunk = {
|
|
112
112
|
type: "done";
|
|
113
113
|
usage?: AIUsage;
|
|
114
|
+
stopReason?: string;
|
|
114
115
|
};
|
|
115
116
|
export type AIThinkingChunk = {
|
|
116
117
|
type: "thinking";
|
|
@@ -405,7 +406,20 @@ export type StreamAIOptions = {
|
|
|
405
406
|
onComplete?: (fullResponse: string, usage?: AIUsage, metadata?: StreamAICompleteMetadata) => void;
|
|
406
407
|
onToolUse?: (name: string, input: unknown, result: string) => void;
|
|
407
408
|
onImage?: (imageData: AIImageData) => void;
|
|
409
|
+
/** Invoked once per completed turn with that turn's normalized usage —
|
|
410
|
+
* surfaces per-turn spend (incl. cache reads) for logging/budgets. */
|
|
411
|
+
onTurn?: (turn: number, usage?: AIUsage) => void;
|
|
412
|
+
maxTokens?: number;
|
|
408
413
|
maxTurns?: number;
|
|
414
|
+
/** Cumulative input+output token ceiling across all turns. When reached, the
|
|
415
|
+
* loop aborts with a `status` event. Unset = no token ceiling. */
|
|
416
|
+
maxTotalTokens?: number;
|
|
417
|
+
/** Wall-clock ceiling in ms across all turns. When reached, the loop aborts
|
|
418
|
+
* with a `status` event. Unset = no time ceiling. */
|
|
419
|
+
maxDurationMs?: number;
|
|
420
|
+
/** Cap on tool-result characters fed back into the message array. Larger
|
|
421
|
+
* results are truncated head+tail with a marker. Unset = no truncation. */
|
|
422
|
+
maxToolResultChars?: number;
|
|
409
423
|
signal?: AbortSignal;
|
|
410
424
|
completeMeta?: StreamAICompleteMetadata;
|
|
411
425
|
};
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
export type AnthropicConfig = {
|
|
2
2
|
apiKey: string;
|
|
3
3
|
baseUrl?: string;
|
|
4
|
+
maxTokens?: number;
|
|
5
|
+
/**
|
|
6
|
+
* Enable Anthropic prompt caching breakpoints (tools + system + rolling
|
|
7
|
+
* message prefix). Defaults to `true`. Caching is a silent no-op below the
|
|
8
|
+
* minimum cacheable prefix, so small requests are unaffected. Set `false` to
|
|
9
|
+
* disable entirely.
|
|
10
|
+
*/
|
|
11
|
+
promptCaching?: boolean;
|
|
4
12
|
};
|
|
5
13
|
export type AnthropicMessage = {
|
|
6
14
|
content: string | Array<Record<string, unknown>>;
|
|
@@ -11,6 +19,7 @@ export type AnthropicSSEState = {
|
|
|
11
19
|
currentToolId: string;
|
|
12
20
|
currentToolName: string;
|
|
13
21
|
isThinkingBlock: boolean;
|
|
22
|
+
stopReason: string;
|
|
14
23
|
thinkingSignature: string;
|
|
15
24
|
toolInputJson: string;
|
|
16
25
|
usage: {
|
package/package.json
CHANGED
|
@@ -1,174 +1,174 @@
|
|
|
1
1
|
{
|
|
2
|
-
|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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
2
|
+
"name": "@absolutejs/ai",
|
|
3
|
+
"version": "0.0.25",
|
|
4
|
+
"homepage": "https://github.com/absolutejs/ai",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/absolutejs/ai/issues"
|
|
7
|
+
},
|
|
8
|
+
"license": "BSL-1.1",
|
|
9
|
+
"description": "AI runtime, providers, streaming, and framework adapters extracted from AbsoluteJS",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/ai/index.js",
|
|
12
|
+
"types": "./dist/src/ai/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/ai/index.js",
|
|
20
|
+
"types": "./dist/src/ai/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./client": {
|
|
23
|
+
"import": "./dist/ai/client/index.js",
|
|
24
|
+
"types": "./dist/src/ai/client/index.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./react": {
|
|
27
|
+
"import": "./dist/react/ai/index.js",
|
|
28
|
+
"types": "./dist/src/react/ai/index.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./vue": {
|
|
31
|
+
"import": "./dist/vue/ai/index.js",
|
|
32
|
+
"types": "./dist/src/vue/ai/index.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./svelte": {
|
|
35
|
+
"import": "./dist/svelte/ai/index.js",
|
|
36
|
+
"types": "./dist/src/svelte/ai/index.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./angular": {
|
|
39
|
+
"import": "./dist/angular/ai/index.js",
|
|
40
|
+
"types": "./dist/src/angular/ai/index.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./anthropic": {
|
|
43
|
+
"import": "./dist/ai/providers/anthropic.js",
|
|
44
|
+
"types": "./dist/src/ai/providers/anthropic.d.ts"
|
|
45
|
+
},
|
|
46
|
+
"./gemini": {
|
|
47
|
+
"import": "./dist/ai/providers/gemini.js",
|
|
48
|
+
"types": "./dist/src/ai/providers/gemini.d.ts"
|
|
49
|
+
},
|
|
50
|
+
"./ollama": {
|
|
51
|
+
"import": "./dist/ai/providers/ollama.js",
|
|
52
|
+
"types": "./dist/src/ai/providers/ollama.d.ts"
|
|
53
|
+
},
|
|
54
|
+
"./openai": {
|
|
55
|
+
"import": "./dist/ai/providers/openai.js",
|
|
56
|
+
"types": "./dist/src/ai/providers/openai.d.ts"
|
|
57
|
+
},
|
|
58
|
+
"./openai-compatible": {
|
|
59
|
+
"import": "./dist/ai/providers/openaiCompatible.js",
|
|
60
|
+
"types": "./dist/src/ai/providers/openaiCompatible.d.ts"
|
|
61
|
+
},
|
|
62
|
+
"./openai-responses": {
|
|
63
|
+
"import": "./dist/ai/providers/openaiResponses.js",
|
|
64
|
+
"types": "./dist/src/ai/providers/openaiResponses.d.ts"
|
|
65
|
+
},
|
|
66
|
+
"./providers": {
|
|
67
|
+
"import": "./dist/ai/providers/openaiCompatible.js",
|
|
68
|
+
"types": "./dist/src/ai/providers/openaiCompatible.d.ts"
|
|
69
|
+
},
|
|
70
|
+
"./tools": {
|
|
71
|
+
"import": "./dist/ai/tools/index.js",
|
|
72
|
+
"types": "./dist/src/ai/tools/index.d.ts"
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"dependencies": {
|
|
76
|
+
"@absolutejs/linked-providers": "0.0.2",
|
|
77
|
+
"@absolutejs/sync": "0.7.0"
|
|
78
|
+
},
|
|
79
|
+
"peerDependencies": {
|
|
80
|
+
"@absolutejs/isolated-jsc": ">= 0.4.0",
|
|
81
|
+
"@angular/core": "^21.0.0",
|
|
82
|
+
"elysia": "^1.4.18",
|
|
83
|
+
"react": "^19.2.0",
|
|
84
|
+
"svelte": "^5.35.2",
|
|
85
|
+
"vue": "^3.5.27"
|
|
86
|
+
},
|
|
87
|
+
"peerDependenciesMeta": {
|
|
88
|
+
"@absolutejs/isolated-jsc": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"@angular/core": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"react": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"svelte": {
|
|
98
|
+
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"vue": {
|
|
101
|
+
"optional": true
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"devDependencies": {
|
|
105
|
+
"@absolutejs/absolute": "^0.19.0-beta.1051",
|
|
106
|
+
"@absolutejs/isolated-jsc": "0.6.0",
|
|
107
|
+
"@angular/core": "^21.0.0",
|
|
108
|
+
"@eslint/js": "^10.0.1",
|
|
109
|
+
"@types/bun": "1.3.9",
|
|
110
|
+
"@types/react": "19.2.0",
|
|
111
|
+
"elysia": "1.4.18",
|
|
112
|
+
"eslint": "^10.0.3",
|
|
113
|
+
"eslint-plugin-absolute": "^0.2.6",
|
|
114
|
+
"eslint-plugin-promise": "^7.2.1",
|
|
115
|
+
"globals": "^17.4.0",
|
|
116
|
+
"prettier": "^3.5.3",
|
|
117
|
+
"react": "19.2.1",
|
|
118
|
+
"svelte": "5.55.0",
|
|
119
|
+
"typescript": "^5.9.3",
|
|
120
|
+
"typescript-eslint": "^8.56.1",
|
|
121
|
+
"vue": "3.5.27"
|
|
122
|
+
},
|
|
123
|
+
"scripts": {
|
|
124
|
+
"build": "bun run scripts/build.ts",
|
|
125
|
+
"config": "absolute config",
|
|
126
|
+
"typecheck": "tsc --noEmit --project tsconfig.json",
|
|
127
|
+
"lint": "eslint . --max-warnings 0",
|
|
128
|
+
"format": "prettier --write .",
|
|
129
|
+
"release": "bun run format && bun run build && bun publish"
|
|
130
|
+
},
|
|
131
|
+
"publishConfig": {
|
|
132
|
+
"access": "public"
|
|
133
|
+
},
|
|
134
|
+
"typesVersions": {
|
|
135
|
+
"*": {
|
|
136
|
+
"client": [
|
|
137
|
+
"dist/src/ai/client/index.d.ts"
|
|
138
|
+
],
|
|
139
|
+
"react": [
|
|
140
|
+
"dist/src/react/ai/index.d.ts"
|
|
141
|
+
],
|
|
142
|
+
"vue": [
|
|
143
|
+
"dist/src/vue/ai/index.d.ts"
|
|
144
|
+
],
|
|
145
|
+
"svelte": [
|
|
146
|
+
"dist/src/svelte/ai/index.d.ts"
|
|
147
|
+
],
|
|
148
|
+
"angular": [
|
|
149
|
+
"dist/src/angular/ai/index.d.ts"
|
|
150
|
+
],
|
|
151
|
+
"anthropic": [
|
|
152
|
+
"dist/src/ai/providers/anthropic.d.ts"
|
|
153
|
+
],
|
|
154
|
+
"gemini": [
|
|
155
|
+
"dist/src/ai/providers/gemini.d.ts"
|
|
156
|
+
],
|
|
157
|
+
"ollama": [
|
|
158
|
+
"dist/src/ai/providers/ollama.d.ts"
|
|
159
|
+
],
|
|
160
|
+
"openai": [
|
|
161
|
+
"dist/src/ai/providers/openai.d.ts"
|
|
162
|
+
],
|
|
163
|
+
"openai-compatible": [
|
|
164
|
+
"dist/src/ai/providers/openaiCompatible.d.ts"
|
|
165
|
+
],
|
|
166
|
+
"openai-responses": [
|
|
167
|
+
"dist/src/ai/providers/openaiResponses.d.ts"
|
|
168
|
+
],
|
|
169
|
+
"providers": [
|
|
170
|
+
"dist/src/ai/providers/openaiCompatible.d.ts"
|
|
171
|
+
]
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
174
|
}
|