ai-functions 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +35 -0
- package/dist/ai.d.ts +1 -1
- package/dist/ai.d.ts.map +1 -1
- package/dist/ai.js +1 -1
- package/dist/ai.js.map +1 -1
- package/dist/function-registry.d.ts +60 -0
- package/dist/function-registry.d.ts.map +1 -1
- package/dist/function-registry.js +162 -23
- package/dist/function-registry.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/sandbox.d.ts +36 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/sandbox.js +44 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/types.d.ts +20 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +5 -4
- package/src/ai.ts +2 -0
- package/src/function-registry.ts +229 -26
- package/src/index.ts +3 -0
- package/src/sandbox.ts +52 -0
- package/src/types.ts +22 -2
- package/test/fill-template.test.ts +89 -0
- package/test/sandbox-execution.test.ts +155 -0
package/dist/sandbox.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandbox execution boundary for ai-functions.
|
|
3
|
+
*
|
|
4
|
+
* ALL dynamic code execution in ai-functions is delegated to ai-evaluate's
|
|
5
|
+
* V8-isolate sandbox (Cloudflare Dynamic Workers). `new Function`/`eval` are
|
|
6
|
+
* banned in this package — they are broken under Workers and unsandboxed under
|
|
7
|
+
* Node.
|
|
8
|
+
*
|
|
9
|
+
* ## The env boundary
|
|
10
|
+
*
|
|
11
|
+
* "Zero env plumbing" is NOT achievable:
|
|
12
|
+
* - The Workers entry (`ai-evaluate`) requires a `LOADER` binding (and, for the
|
|
13
|
+
* test path, a `TEST` service binding) to be passed in `env`.
|
|
14
|
+
* - That entry imports `cloudflare:workers`, which is Node-incompatible, so it
|
|
15
|
+
* cannot be imported eagerly in a Node/dev process.
|
|
16
|
+
*
|
|
17
|
+
* The clean boundary is therefore an **explicit, optional `env`**:
|
|
18
|
+
* - When a host Workers `env` (carrying `LOADER` + `TEST`) is supplied, run on
|
|
19
|
+
* the real Dynamic Workers loader via `ai-evaluate`.
|
|
20
|
+
* - When absent (Node / dev / tests), import from `ai-evaluate/node`, which
|
|
21
|
+
* falls back to Miniflare and runs with no live Worker.
|
|
22
|
+
*
|
|
23
|
+
* The `ai-evaluate/node` module is only imported when no `env` is present, so a
|
|
24
|
+
* Node process never pulls in `cloudflare:workers`.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Run an evaluation in the appropriate sandbox.
|
|
28
|
+
*
|
|
29
|
+
* @param options - What to evaluate (`script`, or `module` + `tests`, etc.)
|
|
30
|
+
* @param env - Optional host Workers env carrying `LOADER` (+ `TEST` for the
|
|
31
|
+
* test path). When omitted, falls back to the Miniflare-backed Node entry.
|
|
32
|
+
*/
|
|
33
|
+
export async function runInSandbox(options, env) {
|
|
34
|
+
if (env && (env.loader || env.LOADER)) {
|
|
35
|
+
// Host Workers env present — use the Dynamic Workers loader entry.
|
|
36
|
+
const { evaluate } = await import('ai-evaluate');
|
|
37
|
+
return evaluate(options, env);
|
|
38
|
+
}
|
|
39
|
+
// No live Worker — use the Node entry (Miniflare fallback). This module is
|
|
40
|
+
// imported lazily so Node processes never eagerly pull in `cloudflare:workers`.
|
|
41
|
+
const { evaluate } = await import('ai-evaluate/node');
|
|
42
|
+
return evaluate(options, env);
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=sandbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sandbox.js","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAMH;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAAwB,EACxB,GAAgB;IAEhB,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACtC,mEAAmE;QACnE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAA;QAChD,OAAO,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;IAC/B,CAAC;IAED,2EAA2E;IAC3E,gFAAgF;IAChF,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAA;IACrD,OAAO,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;AAC/B,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Core types for AI functions
|
|
3
3
|
*/
|
|
4
|
+
import type { SandboxEnv } from 'ai-evaluate';
|
|
5
|
+
/**
|
|
6
|
+
* Host Workers environment for the ai-evaluate sandbox.
|
|
7
|
+
*
|
|
8
|
+
* Re-exported from ai-evaluate so consumers can type the optional `env`
|
|
9
|
+
* argument threaded through `DefinedFunction.call` / `generateAndRunCode`
|
|
10
|
+
* without importing ai-evaluate directly.
|
|
11
|
+
*/
|
|
12
|
+
export type { SandboxEnv } from 'ai-evaluate';
|
|
4
13
|
/**
|
|
5
14
|
* Symbol used to identify pending human function results
|
|
6
15
|
*/
|
|
@@ -578,8 +587,17 @@ export type FunctionDefinition<TOutput = unknown, TInput = unknown> = CodeFuncti
|
|
|
578
587
|
export interface DefinedFunction<TOutput = unknown, TInput = unknown> {
|
|
579
588
|
/** The original definition */
|
|
580
589
|
definition: FunctionDefinition<TOutput, TInput>;
|
|
581
|
-
/**
|
|
582
|
-
|
|
590
|
+
/**
|
|
591
|
+
* Call the function.
|
|
592
|
+
*
|
|
593
|
+
* @param args - The function arguments.
|
|
594
|
+
* @param env - Optional host Workers env (carrying a `LOADER` worker-loader
|
|
595
|
+
* binding, and `TEST` for the test path) used by `type: 'code'` functions
|
|
596
|
+
* to run inline `code` bodies in ai-evaluate's sandbox. When omitted, the
|
|
597
|
+
* inline-code path falls back to the Miniflare-backed Node runtime. Has no
|
|
598
|
+
* effect on `handler`-based code functions or other function types.
|
|
599
|
+
*/
|
|
600
|
+
call: (args: TInput, env?: SandboxEnv) => Promise<TOutput>;
|
|
583
601
|
/** Get the function as a tool definition for AI */
|
|
584
602
|
asTool: () => AIFunctionDefinition<TOutput, TInput>;
|
|
585
603
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAE7C;;;;;;GAMG;AACH,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAM7C;;GAEG;AACH,eAAO,MAAM,2BAA2B,eAAqC,CAAA;AAE7E;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,KAAK,GAAG,WAAW,GAAG,KAAK,CAAA;AAEnF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,oBAAoB,CAAC,SAAS,GAAG,OAAO;IACvD,4CAA4C;IAC5C,QAAQ,CAAC,CAAC,2BAA2B,CAAC,EAAE,IAAI,CAAA;IAC5C,yEAAyE;IACzE,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAA;IACvB,kDAAkD;IAClD,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAA;IAC9B,qDAAqD;IACrD,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAA;IAC3B,wCAAwC;IACxC,QAAQ,CA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8BAA8B;IAC9B,UAAU,EAAE,kBAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAC/C;;;;;;;;;OASG;IACH,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,UAAU,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAC1D,mDAAmD;IACnD,MAAM,EAAE,MAAM,oBAAoB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;CACpD;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,6BAA6B;IAC7B,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAAA;IAC9C,2BAA2B;IAC3B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,eAAe,GAAG,IAAI,CAAA;IAC5C,iCAAiC;IACjC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;IAC1B,8BAA8B;IAC9B,IAAI,IAAI,MAAM,EAAE,CAAA;IAChB,wBAAwB;IACxB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;IAC7B,0BAA0B;IAC1B,KAAK,IAAI,IAAI,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,mCAAmC;IACnC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,GAAG,OAAO,CAAA;IACjD,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAA;IACjB,uCAAuC;IACvC,UAAU,EAAE,kBAAkB,CAAA;CAC/B"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAaH,+EAA+E;AAC/E,+BAA+B;AAC/B,+EAA+E;AAE/E;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAA;AAgD7E;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAA+B;IAE/B,OAAO,CACL,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,UAAU,IAAI,KAAK;QACnB,KAAK,CAAC,QAAQ,KAAK,IAAI;QACvB,2BAA2B,IAAI,KAAK,CACrC,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-functions",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Core AI primitives for building intelligent applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -45,11 +45,12 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@ai-sdk/amazon-bedrock": "^3.0.0",
|
|
47
47
|
"@ai-sdk/provider": "^3.0.7",
|
|
48
|
-
"@org.ai/types": "2.
|
|
48
|
+
"@org.ai/types": "2.4.0",
|
|
49
49
|
"ai": "^6.0.0",
|
|
50
|
-
"ai-
|
|
50
|
+
"ai-evaluate": "^2.4.0",
|
|
51
|
+
"ai-providers": "^2.4.0",
|
|
51
52
|
"digital-objects": "^1.1.0",
|
|
52
|
-
"language-models": "2.
|
|
53
|
+
"language-models": "2.4.0",
|
|
53
54
|
"yaml": "^2.8.0",
|
|
54
55
|
"zod": "^3.23.0",
|
|
55
56
|
"zod-to-json-schema": "^3.25.1"
|
package/src/ai.ts
CHANGED
package/src/function-registry.ts
CHANGED
|
@@ -23,6 +23,7 @@ import type {
|
|
|
23
23
|
import { PENDING_HUMAN_RESULT_SYMBOL } from './types.js'
|
|
24
24
|
import { schema as convertSchema, type SimpleSchema as SimpleSchemaType } from './schema.js'
|
|
25
25
|
import { getLogger } from './logger.js'
|
|
26
|
+
import { runInSandbox, type SandboxEnv } from './sandbox.js'
|
|
26
27
|
|
|
27
28
|
// ============================================================================
|
|
28
29
|
// JSON Schema Conversion
|
|
@@ -113,7 +114,13 @@ function convertValueToJSONSchema(value: unknown): JSONSchema {
|
|
|
113
114
|
* Fill template with values
|
|
114
115
|
*/
|
|
115
116
|
export function fillTemplate(template: string, args: Record<string, unknown>): string {
|
|
116
|
-
return template.replace(/\{\{(\w+)\}\}/g, (_, key) =>
|
|
117
|
+
return template.replace(/\{\{(\w+)\}\}/g, (_, key) => {
|
|
118
|
+
const v = args[key] ?? ''
|
|
119
|
+
if (typeof v === 'object' && v !== null) {
|
|
120
|
+
return JSON.stringify(v)
|
|
121
|
+
}
|
|
122
|
+
return String(v)
|
|
123
|
+
})
|
|
117
124
|
}
|
|
118
125
|
|
|
119
126
|
// ============================================================================
|
|
@@ -130,16 +137,25 @@ export function fillTemplate(template: string, args: Record<string, unknown>): s
|
|
|
130
137
|
*
|
|
131
138
|
* This is a deliberate change from the previous behavior, where `type: 'code'`
|
|
132
139
|
* LLM-generated source at call time. That code-*authoring* behavior now lives
|
|
133
|
-
* in {@link
|
|
134
|
-
* `Code` can carry the "deterministic handler" contract consumers depend
|
|
135
|
-
* (ADR-0033). See the package README migration note.
|
|
140
|
+
* in {@link generateAndRunCode} (and the `generate('code', …)` primitive), so
|
|
141
|
+
* that `Code` can carry the "deterministic handler" contract consumers depend
|
|
142
|
+
* on (ADR-0033). See the package README migration note.
|
|
143
|
+
*
|
|
144
|
+
* Inline `code` bodies are executed in ai-evaluate's V8-isolate sandbox — never
|
|
145
|
+
* via `new Function`/`eval`. Execution stays deterministic: no model is ever
|
|
146
|
+
* consulted on this path.
|
|
147
|
+
*
|
|
148
|
+
* @param env - Optional host Workers env (carrying `LOADER`) for the sandbox;
|
|
149
|
+
* when omitted the inline-code path falls back to the Miniflare-backed Node
|
|
150
|
+
* runtime. Ignored when a `handler` is supplied (direct call, no sandbox).
|
|
136
151
|
*
|
|
137
152
|
* @throws if neither `handler` nor `code` is provided, or if an inline `code`
|
|
138
153
|
* body is in a non-evaluable language.
|
|
139
154
|
*/
|
|
140
155
|
async function executeCodeFunction<TOutput, TInput>(
|
|
141
156
|
definition: CodeFunctionDefinition<TOutput, TInput>,
|
|
142
|
-
args: TInput
|
|
157
|
+
args: TInput,
|
|
158
|
+
env?: SandboxEnv
|
|
143
159
|
): Promise<TOutput> {
|
|
144
160
|
const { handler, code, language = 'typescript', name } = definition
|
|
145
161
|
|
|
@@ -148,55 +164,78 @@ async function executeCodeFunction<TOutput, TInput>(
|
|
|
148
164
|
}
|
|
149
165
|
|
|
150
166
|
if (typeof code === 'string' && code.length > 0) {
|
|
151
|
-
return await runInlineCode<TOutput, TInput>(code, args, language, name)
|
|
167
|
+
return await runInlineCode<TOutput, TInput>(code, args, language, name, env)
|
|
152
168
|
}
|
|
153
169
|
|
|
154
170
|
throw new Error(
|
|
155
171
|
`Code function '${name}' has no handler or inline code. ` +
|
|
156
172
|
`'code' functions are deterministic and require a handler: (input) => output ` +
|
|
157
173
|
`(or an inline 'code' body). To have a model *author* code instead, use ` +
|
|
158
|
-
`generateCode() or define a 'generative' function.`
|
|
174
|
+
`generateAndRunCode() / generateCode() or define a 'generative' function.`
|
|
159
175
|
)
|
|
160
176
|
}
|
|
161
177
|
|
|
162
178
|
/**
|
|
163
|
-
* Deterministically
|
|
179
|
+
* Deterministically run an inline `code` body for a Code function in the
|
|
180
|
+
* ai-evaluate V8-isolate sandbox.
|
|
164
181
|
*
|
|
165
|
-
* The body is treated as a function whose
|
|
166
|
-
* `args`
|
|
167
|
-
* JS/TS-compatible languages can be evaluated
|
|
182
|
+
* The body is treated as a function whose `return` value is the result; the
|
|
183
|
+
* parsed `args` are exposed as a top-level `args` binding inside the sandbox.
|
|
184
|
+
* Only the JS/TS-compatible languages can be evaluated; other languages are
|
|
168
185
|
* carried as metadata for an external runtime and are rejected here.
|
|
169
186
|
*
|
|
170
|
-
* No model is involved —
|
|
171
|
-
*
|
|
187
|
+
* No model is involved — the same body always produces the same behavior. This
|
|
188
|
+
* replaces the former `new Function(...)` path: `new Function`/`eval` are
|
|
189
|
+
* banned in this package (broken under Workers, unsandboxed under Node).
|
|
190
|
+
*
|
|
191
|
+
* Limitation: `args` are injected by JSON-serializing them into the sandbox
|
|
192
|
+
* script (`JSON.parse(<json>)`), so only JSON-serializable inputs are
|
|
193
|
+
* supported on the inline-`code` path. Pass a `handler` for non-serializable
|
|
194
|
+
* inputs (functions, class instances, etc.).
|
|
195
|
+
*
|
|
196
|
+
* @param env - Optional host Workers env (carrying `LOADER`) for the sandbox;
|
|
197
|
+
* when omitted, runs against the Miniflare-backed Node runtime.
|
|
172
198
|
*/
|
|
173
199
|
async function runInlineCode<TOutput, TInput>(
|
|
174
200
|
code: string,
|
|
175
201
|
args: TInput,
|
|
176
202
|
language: string,
|
|
177
|
-
name: string
|
|
203
|
+
name: string,
|
|
204
|
+
env?: SandboxEnv
|
|
178
205
|
): Promise<TOutput> {
|
|
179
206
|
if (language !== 'typescript' && language !== 'javascript') {
|
|
180
207
|
throw new Error(
|
|
181
208
|
`Code function '${name}' has an inline 'code' body in language '${language}', ` +
|
|
182
|
-
`which cannot be evaluated in
|
|
209
|
+
`which cannot be evaluated in the sandbox. Pass a 'handler' instead, or run it ` +
|
|
183
210
|
`in an external deterministic runtime.`
|
|
184
211
|
)
|
|
185
212
|
}
|
|
186
213
|
|
|
187
214
|
const body = /\breturn\b/.test(code) ? code : `return (${code})`
|
|
188
|
-
|
|
215
|
+
|
|
216
|
+
// Inject args deterministically by serializing them into the sandbox script.
|
|
217
|
+
// (JSON-serializable inputs only — see the doc comment.)
|
|
218
|
+
let argsJson: string
|
|
189
219
|
try {
|
|
190
|
-
|
|
191
|
-
fn = new Function('args', `"use strict";\n${body}`) as (
|
|
192
|
-
input: TInput
|
|
193
|
-
) => TOutput | Promise<TOutput>
|
|
220
|
+
argsJson = JSON.stringify(args ?? null)
|
|
194
221
|
} catch (e) {
|
|
195
222
|
throw new Error(
|
|
196
|
-
`Code function '${name}'
|
|
223
|
+
`Code function '${name}' received non-JSON-serializable args for its inline 'code' ` +
|
|
224
|
+
`body: ${(e as Error).message}. Pass a 'handler' for non-serializable inputs.`
|
|
197
225
|
)
|
|
198
226
|
}
|
|
199
|
-
|
|
227
|
+
|
|
228
|
+
const script = `const args = JSON.parse(${JSON.stringify(argsJson)});\n${body}`
|
|
229
|
+
|
|
230
|
+
const result = await runInSandbox({ script }, env)
|
|
231
|
+
|
|
232
|
+
if (result.success === false) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
`Code function '${name}' failed in the sandbox: ${result.error ?? 'unknown error'}`
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return result.value as TOutput
|
|
200
239
|
}
|
|
201
240
|
|
|
202
241
|
/**
|
|
@@ -225,8 +264,14 @@ export async function generateCode<TInput>(
|
|
|
225
264
|
definition: CodeGenerationDefinition<TInput>,
|
|
226
265
|
args?: TInput
|
|
227
266
|
): Promise<string> {
|
|
228
|
-
const {
|
|
229
|
-
|
|
267
|
+
const {
|
|
268
|
+
name,
|
|
269
|
+
description,
|
|
270
|
+
language = 'typescript',
|
|
271
|
+
instructions,
|
|
272
|
+
returnType,
|
|
273
|
+
model = 'sonnet',
|
|
274
|
+
} = definition
|
|
230
275
|
|
|
231
276
|
const argsDescription = JSON.stringify(args ?? definition.args, null, 2)
|
|
232
277
|
|
|
@@ -255,6 +300,162 @@ Requirements:
|
|
|
255
300
|
return (result.object as { code: string }).code
|
|
256
301
|
}
|
|
257
302
|
|
|
303
|
+
/**
|
|
304
|
+
* Result of {@link generateAndRunCode}: the executed value plus the artifacts
|
|
305
|
+
* that produced it.
|
|
306
|
+
*/
|
|
307
|
+
export interface GeneratedCodeRunResult<TOutput = unknown> {
|
|
308
|
+
/** The value returned by running the authored code against the inputs. */
|
|
309
|
+
value: TOutput
|
|
310
|
+
/** The model-authored module source that was executed. */
|
|
311
|
+
code: string
|
|
312
|
+
/** The model-authored test source, if tests were requested. */
|
|
313
|
+
tests?: string
|
|
314
|
+
/** Test results, if tests ran in the sandbox. */
|
|
315
|
+
testResults?: {
|
|
316
|
+
total: number
|
|
317
|
+
passed: number
|
|
318
|
+
failed: number
|
|
319
|
+
skipped: number
|
|
320
|
+
}
|
|
321
|
+
/** Console logs captured during execution. */
|
|
322
|
+
logs: { level: string; message: string }[]
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The **non-deterministic** generate → run → test → return capability.
|
|
327
|
+
*
|
|
328
|
+
* This is the headline of the `generate('code', …)` primitive: a model
|
|
329
|
+
* **authors** code, that code is **run** in ai-evaluate's V8-isolate sandbox,
|
|
330
|
+
* optionally **tested** there, and the executed **result** is returned (not
|
|
331
|
+
* just the source). This is deliberately separate from `type: 'code'`, which is
|
|
332
|
+
* deterministic and never consults a model — so determinism is never blurred.
|
|
333
|
+
*
|
|
334
|
+
* Unlike {@link generateCode} (which only returns source text), this runs the
|
|
335
|
+
* authored code. The authored module is expected to `export function ${name}`
|
|
336
|
+
* (a NAMED export — the sandbox's module loader does not support `export
|
|
337
|
+
* default`); the sandbox script invokes `${name}(args)` and returns its result.
|
|
338
|
+
*
|
|
339
|
+
* @param definition - The code-authoring spec ({@link CodeGenerationDefinition}).
|
|
340
|
+
* Set `includeTests: false` to skip test authoring (default: tests included).
|
|
341
|
+
* @param args - Concrete inputs the authored code is invoked with.
|
|
342
|
+
* @param env - Optional host Workers env. When it carries `LOADER` **and**
|
|
343
|
+
* `TEST`, tests run on the real Dynamic Workers loader; otherwise execution
|
|
344
|
+
* falls back to the Miniflare-backed Node runtime (whose dev worker has its
|
|
345
|
+
* own embedded test runner and needs no live `TEST` binding).
|
|
346
|
+
* @returns The executed result plus authored artifacts.
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* import { generateAndRunCode } from 'ai-functions'
|
|
351
|
+
*
|
|
352
|
+
* const { value } = await generateAndRunCode(
|
|
353
|
+
* { name: 'calculateTax', args: { amount: '(number)', rate: '(number)' } },
|
|
354
|
+
* { amount: 100, rate: 0.2 }
|
|
355
|
+
* )
|
|
356
|
+
* // value === 20 (the model authored the code, the sandbox ran it)
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
export async function generateAndRunCode<TOutput = unknown, TInput = unknown>(
|
|
360
|
+
definition: CodeGenerationDefinition<TInput>,
|
|
361
|
+
args?: TInput,
|
|
362
|
+
env?: SandboxEnv
|
|
363
|
+
): Promise<GeneratedCodeRunResult<TOutput>> {
|
|
364
|
+
const {
|
|
365
|
+
name,
|
|
366
|
+
description,
|
|
367
|
+
language = 'typescript',
|
|
368
|
+
instructions,
|
|
369
|
+
returnType,
|
|
370
|
+
includeTests = true,
|
|
371
|
+
model = 'sonnet',
|
|
372
|
+
} = definition
|
|
373
|
+
|
|
374
|
+
const argsDescription = JSON.stringify(args ?? definition.args, null, 2)
|
|
375
|
+
|
|
376
|
+
// Step 1 — model AUTHORS the module (+ optional tests). Non-deterministic.
|
|
377
|
+
const codeSpec = `The complete ${language} module. It MUST contain a NAMED export 'export function ${name}(args) { ... }' (NOT a default export) that takes a single arguments object and returns the result. Output ONLY raw code, no markdown fences.`
|
|
378
|
+
const schema: SimpleSchemaType = includeTests
|
|
379
|
+
? {
|
|
380
|
+
code: codeSpec,
|
|
381
|
+
tests: `vitest-style tests using global describe/it/expect. The function '${name}' is already in scope (do not import it). Output ONLY raw code, no markdown fences.`,
|
|
382
|
+
}
|
|
383
|
+
: {
|
|
384
|
+
code: codeSpec,
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const authored = await generateObject({
|
|
388
|
+
model,
|
|
389
|
+
schema,
|
|
390
|
+
system: `You are an expert ${language} developer. Generate clean, production-ready code. The module MUST expose a NAMED export 'export function ${name}(args)' taking one arguments object — do NOT use 'export default'. Output ONLY raw code, no markdown code fences or language tags.`,
|
|
391
|
+
prompt: `Author a ${language} module with the following specification:
|
|
392
|
+
|
|
393
|
+
Name: ${name}
|
|
394
|
+
Description: ${description || 'No description provided'}
|
|
395
|
+
Arguments: ${argsDescription}
|
|
396
|
+
Return Type: ${JSON.stringify(returnType)}
|
|
397
|
+
|
|
398
|
+
${instructions ? `Additional Instructions: ${instructions}` : ''}
|
|
399
|
+
|
|
400
|
+
Requirements:
|
|
401
|
+
- Expose 'export function ${name}(args) { ... }' (a NAMED export, not default), taking one arguments object.
|
|
402
|
+
- Handle edge cases appropriately.
|
|
403
|
+
- Return ONLY raw code without markdown formatting.`,
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
const authoredObj = authored.object as { code: string; tests?: string }
|
|
407
|
+
const code = authoredObj.code
|
|
408
|
+
const tests = includeTests ? authoredObj.tests : undefined
|
|
409
|
+
|
|
410
|
+
// Step 2 — RUN the authored code in the sandbox and capture its return value.
|
|
411
|
+
// The module's default export is invoked with the JSON-injected args; the
|
|
412
|
+
// result is returned by the sandbox script. Tests (if any) run in the same
|
|
413
|
+
// sandbox via the worker template's test runner.
|
|
414
|
+
let argsJson: string
|
|
415
|
+
try {
|
|
416
|
+
argsJson = JSON.stringify(args ?? null)
|
|
417
|
+
} catch (e) {
|
|
418
|
+
throw new Error(
|
|
419
|
+
`generateAndRunCode('${name}'): args are not JSON-serializable: ${(e as Error).message}`
|
|
420
|
+
)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// The named export `${name}` is exposed as a top-level binding by the worker
|
|
424
|
+
// template (`const { ${name} } = exports`). The script calls it with the
|
|
425
|
+
// JSON-injected args and returns the result.
|
|
426
|
+
const result = await runInSandbox(
|
|
427
|
+
{
|
|
428
|
+
module: code,
|
|
429
|
+
script: `const __args__ = JSON.parse(${JSON.stringify(
|
|
430
|
+
argsJson
|
|
431
|
+
)}); if (typeof ${name} !== 'function') { throw new Error("authored module did not export a callable '${name}'"); } return await ${name}(__args__);`,
|
|
432
|
+
...(tests !== undefined && { tests }),
|
|
433
|
+
},
|
|
434
|
+
env
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
if (result.success === false) {
|
|
438
|
+
throw new Error(
|
|
439
|
+
`generateAndRunCode('${name}') failed in the sandbox: ${result.error ?? 'unknown error'}`
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return {
|
|
444
|
+
value: result.value as TOutput,
|
|
445
|
+
code,
|
|
446
|
+
...(tests !== undefined && { tests }),
|
|
447
|
+
...(result.testResults && {
|
|
448
|
+
testResults: {
|
|
449
|
+
total: result.testResults.total,
|
|
450
|
+
passed: result.testResults.passed,
|
|
451
|
+
failed: result.testResults.failed,
|
|
452
|
+
skipped: result.testResults.skipped,
|
|
453
|
+
},
|
|
454
|
+
}),
|
|
455
|
+
logs: result.logs.map((l) => ({ level: l.level, message: l.message })),
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
258
459
|
/**
|
|
259
460
|
* Execute a generative function - uses AI to generate content
|
|
260
461
|
*/
|
|
@@ -523,10 +724,12 @@ Generate the appropriate ${channel} UI/content to collect this response from a h
|
|
|
523
724
|
export function createDefinedFunction<TOutput, TInput>(
|
|
524
725
|
definition: FunctionDefinition<TOutput, TInput>
|
|
525
726
|
): DefinedFunction<TOutput, TInput> {
|
|
526
|
-
const call = async (args: TInput): Promise<TOutput> => {
|
|
727
|
+
const call = async (args: TInput, env?: SandboxEnv): Promise<TOutput> => {
|
|
527
728
|
switch (definition.type) {
|
|
528
729
|
case 'code':
|
|
529
|
-
|
|
730
|
+
// Optional host Workers env threads through to the sandbox for inline
|
|
731
|
+
// `code` bodies; ignored for `handler` (direct call) and other types.
|
|
732
|
+
return executeCodeFunction(definition, args, env) as Promise<TOutput>
|
|
530
733
|
case 'generative':
|
|
531
734
|
return executeGenerativeFunction(definition, args) as Promise<TOutput>
|
|
532
735
|
case 'agentic':
|
package/src/index.ts
CHANGED
|
@@ -57,6 +57,7 @@ export type {
|
|
|
57
57
|
DefinedFunction,
|
|
58
58
|
FunctionRegistry,
|
|
59
59
|
AutoDefineResult,
|
|
60
|
+
SandboxEnv,
|
|
60
61
|
} from './types.js'
|
|
61
62
|
|
|
62
63
|
// Schema exports
|
|
@@ -167,6 +168,8 @@ export {
|
|
|
167
168
|
convertArgsToJSONSchema,
|
|
168
169
|
fillTemplate,
|
|
169
170
|
generateCode,
|
|
171
|
+
generateAndRunCode,
|
|
172
|
+
type GeneratedCodeRunResult,
|
|
170
173
|
} from './ai.js'
|
|
171
174
|
|
|
172
175
|
// Also export 'ai' primitive as 'aiPrompt' for convenience
|
package/src/sandbox.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandbox execution boundary for ai-functions.
|
|
3
|
+
*
|
|
4
|
+
* ALL dynamic code execution in ai-functions is delegated to ai-evaluate's
|
|
5
|
+
* V8-isolate sandbox (Cloudflare Dynamic Workers). `new Function`/`eval` are
|
|
6
|
+
* banned in this package — they are broken under Workers and unsandboxed under
|
|
7
|
+
* Node.
|
|
8
|
+
*
|
|
9
|
+
* ## The env boundary
|
|
10
|
+
*
|
|
11
|
+
* "Zero env plumbing" is NOT achievable:
|
|
12
|
+
* - The Workers entry (`ai-evaluate`) requires a `LOADER` binding (and, for the
|
|
13
|
+
* test path, a `TEST` service binding) to be passed in `env`.
|
|
14
|
+
* - That entry imports `cloudflare:workers`, which is Node-incompatible, so it
|
|
15
|
+
* cannot be imported eagerly in a Node/dev process.
|
|
16
|
+
*
|
|
17
|
+
* The clean boundary is therefore an **explicit, optional `env`**:
|
|
18
|
+
* - When a host Workers `env` (carrying `LOADER` + `TEST`) is supplied, run on
|
|
19
|
+
* the real Dynamic Workers loader via `ai-evaluate`.
|
|
20
|
+
* - When absent (Node / dev / tests), import from `ai-evaluate/node`, which
|
|
21
|
+
* falls back to Miniflare and runs with no live Worker.
|
|
22
|
+
*
|
|
23
|
+
* The `ai-evaluate/node` module is only imported when no `env` is present, so a
|
|
24
|
+
* Node process never pulls in `cloudflare:workers`.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { EvaluateOptions, EvaluateResult, SandboxEnv } from 'ai-evaluate'
|
|
28
|
+
|
|
29
|
+
export type { SandboxEnv } from 'ai-evaluate'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Run an evaluation in the appropriate sandbox.
|
|
33
|
+
*
|
|
34
|
+
* @param options - What to evaluate (`script`, or `module` + `tests`, etc.)
|
|
35
|
+
* @param env - Optional host Workers env carrying `LOADER` (+ `TEST` for the
|
|
36
|
+
* test path). When omitted, falls back to the Miniflare-backed Node entry.
|
|
37
|
+
*/
|
|
38
|
+
export async function runInSandbox(
|
|
39
|
+
options: EvaluateOptions,
|
|
40
|
+
env?: SandboxEnv
|
|
41
|
+
): Promise<EvaluateResult> {
|
|
42
|
+
if (env && (env.loader || env.LOADER)) {
|
|
43
|
+
// Host Workers env present — use the Dynamic Workers loader entry.
|
|
44
|
+
const { evaluate } = await import('ai-evaluate')
|
|
45
|
+
return evaluate(options, env)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// No live Worker — use the Node entry (Miniflare fallback). This module is
|
|
49
|
+
// imported lazily so Node processes never eagerly pull in `cloudflare:workers`.
|
|
50
|
+
const { evaluate } = await import('ai-evaluate/node')
|
|
51
|
+
return evaluate(options, env)
|
|
52
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
* Core types for AI functions
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import type { SandboxEnv } from 'ai-evaluate'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Host Workers environment for the ai-evaluate sandbox.
|
|
9
|
+
*
|
|
10
|
+
* Re-exported from ai-evaluate so consumers can type the optional `env`
|
|
11
|
+
* argument threaded through `DefinedFunction.call` / `generateAndRunCode`
|
|
12
|
+
* without importing ai-evaluate directly.
|
|
13
|
+
*/
|
|
14
|
+
export type { SandboxEnv } from 'ai-evaluate'
|
|
15
|
+
|
|
5
16
|
// ============================================================================
|
|
6
17
|
// Human Function Pending Types
|
|
7
18
|
// ============================================================================
|
|
@@ -658,8 +669,17 @@ export type FunctionDefinition<TOutput = unknown, TInput = unknown> =
|
|
|
658
669
|
export interface DefinedFunction<TOutput = unknown, TInput = unknown> {
|
|
659
670
|
/** The original definition */
|
|
660
671
|
definition: FunctionDefinition<TOutput, TInput>
|
|
661
|
-
/**
|
|
662
|
-
|
|
672
|
+
/**
|
|
673
|
+
* Call the function.
|
|
674
|
+
*
|
|
675
|
+
* @param args - The function arguments.
|
|
676
|
+
* @param env - Optional host Workers env (carrying a `LOADER` worker-loader
|
|
677
|
+
* binding, and `TEST` for the test path) used by `type: 'code'` functions
|
|
678
|
+
* to run inline `code` bodies in ai-evaluate's sandbox. When omitted, the
|
|
679
|
+
* inline-code path falls back to the Miniflare-backed Node runtime. Has no
|
|
680
|
+
* effect on `handler`-based code functions or other function types.
|
|
681
|
+
*/
|
|
682
|
+
call: (args: TInput, env?: SandboxEnv) => Promise<TOutput>
|
|
663
683
|
/** Get the function as a tool definition for AI */
|
|
664
684
|
asTool: () => AIFunctionDefinition<TOutput, TInput>
|
|
665
685
|
}
|