@yolk-sdk/codemode 0.1.0-canary.100
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/LICENSE +21 -0
- package/README.md +174 -0
- package/dist/catalog.d.mts +74 -0
- package/dist/catalog.d.mts.map +1 -0
- package/dist/catalog.mjs +164 -0
- package/dist/catalog.mjs.map +1 -0
- package/dist/classifier-tool.d.mts +78 -0
- package/dist/classifier-tool.d.mts.map +1 -0
- package/dist/classifier-tool.mjs +125 -0
- package/dist/classifier-tool.mjs.map +1 -0
- package/dist/executor.d.mts +108 -0
- package/dist/executor.d.mts.map +1 -0
- package/dist/executor.mjs +1 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +7 -0
- package/dist/node.d.mts +35 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +393 -0
- package/dist/node.mjs.map +1 -0
- package/dist/output.d.mts +42 -0
- package/dist/output.d.mts.map +1 -0
- package/dist/output.mjs +184 -0
- package/dist/output.mjs.map +1 -0
- package/dist/search.d.mts +23 -0
- package/dist/search.d.mts.map +1 -0
- package/dist/search.mjs +58 -0
- package/dist/search.mjs.map +1 -0
- package/dist/store.d.mts +77 -0
- package/dist/store.d.mts.map +1 -0
- package/dist/store.mjs +61 -0
- package/dist/store.mjs.map +1 -0
- package/dist/tool.d.mts +103 -0
- package/dist/tool.d.mts.map +1 -0
- package/dist/tool.mjs +392 -0
- package/dist/tool.mjs.map +1 -0
- package/package.json +63 -0
- package/src/catalog.ts +305 -0
- package/src/classifier-tool.ts +227 -0
- package/src/executor.ts +122 -0
- package/src/index.ts +87 -0
- package/src/node.ts +546 -0
- package/src/output.ts +287 -0
- package/src/search.ts +113 -0
- package/src/store.ts +135 -0
- package/src/tool.ts +766 -0
package/dist/node.mjs
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { codeModeCallLabels } from "./catalog.mjs";
|
|
2
|
+
import * as nodeModule from "node:module";
|
|
3
|
+
import { Option, Predicate } from "effect";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
5
|
+
import { CodemodeSandbox } from "@earendil-works/pi-codemode";
|
|
6
|
+
//#region src/node.ts
|
|
7
|
+
/** Default cap of concurrent script executions per executor. */
|
|
8
|
+
const defaultMaxConcurrentExecutions = 4;
|
|
9
|
+
/** Abort- and deadline-aware counting semaphore for executions. */
|
|
10
|
+
const makeExecutionSlots = (max) => {
|
|
11
|
+
let active = 0;
|
|
12
|
+
const waiting = [];
|
|
13
|
+
const acquire = (signal, timeoutMs) => {
|
|
14
|
+
if (signal.aborted) return Promise.resolve("aborted");
|
|
15
|
+
if (active < max) {
|
|
16
|
+
active++;
|
|
17
|
+
return Promise.resolve("acquired");
|
|
18
|
+
}
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
let timer;
|
|
21
|
+
const settle = (outcome) => {
|
|
22
|
+
const index = waiting.indexOf(settle);
|
|
23
|
+
if (index !== -1) waiting.splice(index, 1);
|
|
24
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
25
|
+
signal.removeEventListener("abort", onAbort);
|
|
26
|
+
resolve(outcome);
|
|
27
|
+
};
|
|
28
|
+
const onAbort = () => settle("aborted");
|
|
29
|
+
waiting.push(settle);
|
|
30
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
31
|
+
if (Number.isFinite(timeoutMs)) timer = setTimeout(() => settle("timeout"), Math.max(0, timeoutMs));
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const release = () => {
|
|
35
|
+
const next = waiting[0];
|
|
36
|
+
if (next === void 0) active--;
|
|
37
|
+
else next("acquired");
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
acquire,
|
|
41
|
+
release
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const wrapperPrefix = "async function __yolkCodeMode() {\n";
|
|
45
|
+
const wrapperSuffix = "\n}";
|
|
46
|
+
/**
|
|
47
|
+
* Evaluated in the VM before the script runs: `(tools, labels) => guardedTools`, with `labels` the
|
|
48
|
+
* `[name, callLabel]` pairs of the tools in order (`codeModeCallLabels`). Each call walks its first
|
|
49
|
+
* argument once, the way `JSON.stringify` reads it (each own enumerable string key and each array
|
|
50
|
+
* index read once, so getters run once; a callable `toJSON` is called once with the key, `''` at
|
|
51
|
+
* the root, and its result is walked at the same path), and builds a fresh copy (null-prototype
|
|
52
|
+
* objects and arrays) that is what the call sends. It rejects with a `TypeError` naming the call
|
|
53
|
+
* label and path when JSON would change the value instead of carrying it: non-finite numbers,
|
|
54
|
+
* invalid `Date`s, `undefined`/function/symbol array items and holes, function/symbol/bigint
|
|
55
|
+
* values, cycles, and objects that are neither plain (prototype `Object.prototype` or `null`) nor
|
|
56
|
+
* arrays and have no `toJSON`. An argument with more than `maxArgumentValues` values or nested more
|
|
57
|
+
* than `maxArgumentDepth` levels is rejected too, never sent unchecked. `undefined`-valued keys are
|
|
58
|
+
* left out (absent, as JSON drops them); the same object twice (not a cycle) is copied twice. An
|
|
59
|
+
* error thrown by a getter or `toJSON` rejects the call with that error. A rejected call never
|
|
60
|
+
* reaches the host. Every member of pi's `tools` is wrapped; unknown members fall through to pi's
|
|
61
|
+
* proxy (close-match suggestions). Not a security boundary (`globalThis.tools` is unguarded):
|
|
62
|
+
* arguments are still decoded by the host.
|
|
63
|
+
*/
|
|
64
|
+
const toolsGuardSource = `(function (tools, labels) {
|
|
65
|
+
// Captured before the script runs, so a script that changes built-ins cannot change the checks.
|
|
66
|
+
const { apply, getPrototypeOf, setPrototypeOf } = Reflect
|
|
67
|
+
const isFinite = Number.isFinite
|
|
68
|
+
const isArray = Array.isArray
|
|
69
|
+
const { keys, create, defineProperty } = Object
|
|
70
|
+
const getTime = Date.prototype.getTime
|
|
71
|
+
const TypeErrorCtor = TypeError
|
|
72
|
+
const SetCtor = Set
|
|
73
|
+
const setAdd = Set.prototype.add
|
|
74
|
+
const setHas = Set.prototype.has
|
|
75
|
+
const setDelete = Set.prototype.delete
|
|
76
|
+
const ObjectProto = Object.prototype
|
|
77
|
+
const { trunc, min } = Math
|
|
78
|
+
const stringify = JSON.stringify
|
|
79
|
+
const plainJson = 'pass plain JSON (objects, arrays, strings, finite numbers, booleans, null)'
|
|
80
|
+
const maxValues = 100000
|
|
81
|
+
const maxDepth = 64
|
|
82
|
+
class Rejected {
|
|
83
|
+
constructor(message) {
|
|
84
|
+
defineProperty(this, 'message', { value: message })
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const keyPath = (path, key) =>
|
|
88
|
+
/^[A-Za-z_$][\\w$]*$/.test(key)
|
|
89
|
+
? (path === '' ? key : path + '.' + key)
|
|
90
|
+
: path + '[' + stringify(key) + ']'
|
|
91
|
+
const typeName = value => {
|
|
92
|
+
const proto = getPrototypeOf(value)
|
|
93
|
+
const name = proto && typeof proto.constructor === 'function' ? proto.constructor.name : ''
|
|
94
|
+
return name ? (/^[AEIOU]/.test(name) ? 'an ' : 'a ') + name : 'a non-plain object'
|
|
95
|
+
}
|
|
96
|
+
// The JSON copy of a value already read from its holder at key (undefined: absent), or throws
|
|
97
|
+
// Rejected with why JSON would not carry it unchanged.
|
|
98
|
+
const snapshot = (read, key, path, slot, walk) => {
|
|
99
|
+
const at = path === '' ? 'is ' : 'at ' + path + ' is '
|
|
100
|
+
if (++walk.values > maxValues) {
|
|
101
|
+
throw new Rejected('has more than ' + maxValues + ' values; split the work across calls')
|
|
102
|
+
}
|
|
103
|
+
let value = read
|
|
104
|
+
const kind = typeof value
|
|
105
|
+
if ((kind === 'object' && value !== null) || kind === 'function' || kind === 'bigint') {
|
|
106
|
+
// The intrinsic getTime reads a Date's internal time (and throws for anything else).
|
|
107
|
+
let time
|
|
108
|
+
try {
|
|
109
|
+
time = apply(getTime, value, [])
|
|
110
|
+
} catch {
|
|
111
|
+
time = undefined
|
|
112
|
+
}
|
|
113
|
+
if (time !== undefined && !isFinite(time)) {
|
|
114
|
+
throw new Rejected(at + 'an invalid Date; pass a valid Date or an ISO string')
|
|
115
|
+
}
|
|
116
|
+
const toJSON = value.toJSON
|
|
117
|
+
if (typeof toJSON === 'function') value = apply(toJSON, value, [key])
|
|
118
|
+
}
|
|
119
|
+
switch (typeof value) {
|
|
120
|
+
case 'string':
|
|
121
|
+
case 'boolean':
|
|
122
|
+
return value
|
|
123
|
+
case 'number':
|
|
124
|
+
if (isFinite(value)) return value
|
|
125
|
+
throw new Rejected(
|
|
126
|
+
at + value + '; pass a finite number' + (slot === 'key' ? ' or omit the key' : '')
|
|
127
|
+
)
|
|
128
|
+
case 'undefined':
|
|
129
|
+
if (slot === 'item') throw new Rejected(at + 'undefined; arrays cannot hold undefined')
|
|
130
|
+
return undefined
|
|
131
|
+
case 'bigint':
|
|
132
|
+
case 'function':
|
|
133
|
+
case 'symbol':
|
|
134
|
+
throw new Rejected(at + 'a ' + typeof value + '; ' + plainJson)
|
|
135
|
+
}
|
|
136
|
+
if (value === null) return null
|
|
137
|
+
if (apply(setHas, walk.ancestors, [value])) {
|
|
138
|
+
throw new Rejected(at + 'a circular reference; ' + plainJson)
|
|
139
|
+
}
|
|
140
|
+
if (walk.depth >= maxDepth) {
|
|
141
|
+
throw new Rejected('at ' + path + ' is nested more than ' + maxDepth + ' levels deep')
|
|
142
|
+
}
|
|
143
|
+
const array = isArray(value)
|
|
144
|
+
if (!array) {
|
|
145
|
+
const proto = getPrototypeOf(value)
|
|
146
|
+
if (proto !== ObjectProto && proto !== null) {
|
|
147
|
+
throw new Rejected(at + typeName(value) + '; ' + plainJson)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
apply(setAdd, walk.ancestors, [value])
|
|
151
|
+
walk.depth++
|
|
152
|
+
let copy
|
|
153
|
+
if (array) {
|
|
154
|
+
// LengthOfArrayLike, read once, as JSON.stringify does.
|
|
155
|
+
// Unary plus is ToNumber: one coercion, and it throws for a bigint as JSON.stringify does.
|
|
156
|
+
const raw = +value.length
|
|
157
|
+
const length = raw > 0 ? min(trunc(raw), 9007199254740991) : 0
|
|
158
|
+
// No prototype: no inherited toJSON or index setter can change the checked copy when pi
|
|
159
|
+
// serializes it (still an array to JSON.stringify).
|
|
160
|
+
copy = []
|
|
161
|
+
setPrototypeOf(copy, null)
|
|
162
|
+
for (let i = 0; i < length; i++) {
|
|
163
|
+
copy[i] = snapshot(value[i], '' + i, path + '[' + i + ']', 'item', walk)
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
copy = create(null)
|
|
167
|
+
// Indexed over the fresh key array: no iterator a script could replace.
|
|
168
|
+
const names = keys(value)
|
|
169
|
+
for (let i = 0; i < names.length; i++) {
|
|
170
|
+
const name = names[i]
|
|
171
|
+
const item = snapshot(value[name], name, keyPath(path, name), 'key', walk)
|
|
172
|
+
if (item !== undefined) copy[name] = item
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
walk.depth--
|
|
176
|
+
apply(setDelete, walk.ancestors, [value])
|
|
177
|
+
return copy
|
|
178
|
+
}
|
|
179
|
+
const wrap = (label, call) => async (...args) => {
|
|
180
|
+
let json
|
|
181
|
+
try {
|
|
182
|
+
json = snapshot(args[0], '', '', 'root', { values: 0, depth: 0, ancestors: new SetCtor() })
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (error instanceof Rejected) throw new TypeErrorCtor(label + ': argument ' + error.message)
|
|
185
|
+
throw error
|
|
186
|
+
}
|
|
187
|
+
return call(json)
|
|
188
|
+
}
|
|
189
|
+
// Each tool's function sits at tools[name] unless an earlier tool holds that key, in which case
|
|
190
|
+
// that earlier function is already wrapped.
|
|
191
|
+
const wrappers = new Map()
|
|
192
|
+
for (const [name, label] of labels) {
|
|
193
|
+
const call = name in tools ? tools[name] : undefined
|
|
194
|
+
if (typeof call === 'function' && !wrappers.has(call)) wrappers.set(call, wrap(label, call))
|
|
195
|
+
}
|
|
196
|
+
const guarded = Object.create(null)
|
|
197
|
+
for (const key of Object.keys(tools)) {
|
|
198
|
+
const call = tools[key]
|
|
199
|
+
if (!wrappers.has(call)) wrappers.set(call, wrap('tools[' + JSON.stringify(key) + ']', call))
|
|
200
|
+
guarded[key] = wrappers.get(call)
|
|
201
|
+
}
|
|
202
|
+
return new Proxy(Object.freeze(guarded), {
|
|
203
|
+
get: (target, key) => (key in target ? target[key] : tools[key])
|
|
204
|
+
})
|
|
205
|
+
})`;
|
|
206
|
+
const guardPrefix = "return (async (tools, console) => {";
|
|
207
|
+
/** JSON as a JavaScript expression (U+2028/U+2029 escaped for older parsers). */
|
|
208
|
+
const jsonSource = (value) => JSON.stringify(value).replace(/[\u2028\u2029]/g, (char) => char === "\u2028" ? "\\u2028" : "\\u2029");
|
|
209
|
+
const guardSuffix = (tools) => {
|
|
210
|
+
const labels = codeModeCallLabels(tools.map((tool) => tool.name));
|
|
211
|
+
return `\n})(${toolsGuardSource}(tools, ${jsonSource(tools.map((tool, index) => [tool.name, tool.callLabel ?? labels[index] ?? `tools[${JSON.stringify(tool.name)}]`]))}), console)`;
|
|
212
|
+
};
|
|
213
|
+
const lineTerminators = /\r\n?|[\n\u2028\u2029]/g;
|
|
214
|
+
/** Lines of the script as QuickJS counts them (at least; never fewer). */
|
|
215
|
+
const scriptLineCount = (code) => (code.match(lineTerminators)?.length ?? 0) + 1;
|
|
216
|
+
const scriptFrame = /codemode\.js:(\d+):\d+/;
|
|
217
|
+
/** Drops stack frames of the wrapper and guard, which sit below the script's last line. */
|
|
218
|
+
const withoutWrapperFrames = (stack, scriptLines) => stack.split("\n").filter((line) => {
|
|
219
|
+
const frame = scriptFrame.exec(line);
|
|
220
|
+
return frame === null || Number(frame[1]) <= scriptLines;
|
|
221
|
+
}).join("\n");
|
|
222
|
+
const ErrorWithCode = Schema.Struct({ code: Schema.String });
|
|
223
|
+
const errorCode = (error) => Option.getOrUndefined(Option.map(Schema.decodeUnknownOption(ErrorWithCode)(error), (e) => e.code));
|
|
224
|
+
const syntaxLocation = (error) => {
|
|
225
|
+
const match = /^[^\n]*:(\d+)\n([^\n]*)\n([^\n]*)\n/.exec(error.stack ?? "");
|
|
226
|
+
if (match === null) return "";
|
|
227
|
+
const line = Number(match[1]) - 1;
|
|
228
|
+
return line >= 1 ? ` (line ${line})\n${match[2] ?? ""}\n${match[3] ?? ""}` : "";
|
|
229
|
+
};
|
|
230
|
+
const typeStripMessage = (error) => {
|
|
231
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
232
|
+
if (errorCode(error) === "ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX") return `${message}. Write plain JavaScript: type annotations are stripped, but enums, namespaces, and parameter properties are not supported.`;
|
|
233
|
+
return `The script could not be parsed: ${message}${error instanceof Error ? syntaxLocation(error) : ""}`;
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Strips TypeScript annotations with Node's `stripTypeScriptTypes` (Node 22.13+; whitespace
|
|
237
|
+
* replacement keeps line and column numbers). The script is wrapped in an async function so
|
|
238
|
+
* top-level `return` and `await` parse. On Node without the API, code passes through unchanged.
|
|
239
|
+
*/
|
|
240
|
+
const stripTypes = (code) => {
|
|
241
|
+
if (!Predicate.isFunction(nodeModule.stripTypeScriptTypes)) return {
|
|
242
|
+
ok: true,
|
|
243
|
+
code
|
|
244
|
+
};
|
|
245
|
+
try {
|
|
246
|
+
const stripped = nodeModule.stripTypeScriptTypes(`${wrapperPrefix}${code}${wrapperSuffix}`, { mode: "strip" });
|
|
247
|
+
if (!stripped.startsWith(wrapperPrefix) || !stripped.endsWith(wrapperSuffix)) return {
|
|
248
|
+
ok: false,
|
|
249
|
+
error: {
|
|
250
|
+
kind: "script",
|
|
251
|
+
message: "The script could not be parsed."
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
ok: true,
|
|
256
|
+
code: stripped.slice(34, stripped.length - 2)
|
|
257
|
+
};
|
|
258
|
+
} catch (error) {
|
|
259
|
+
return {
|
|
260
|
+
ok: false,
|
|
261
|
+
error: {
|
|
262
|
+
kind: "script",
|
|
263
|
+
message: typeStripMessage(error)
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
const piTool = (tool) => {
|
|
269
|
+
const converted = {
|
|
270
|
+
name: tool.name,
|
|
271
|
+
execute: (args, context) => tool.execute(args, context)
|
|
272
|
+
};
|
|
273
|
+
if (tool.description !== void 0) converted.description = tool.description;
|
|
274
|
+
if (tool.inputSchema !== void 0) converted.inputSchema = tool.inputSchema;
|
|
275
|
+
if (tool.outputSchema !== void 0) converted.outputSchema = tool.outputSchema;
|
|
276
|
+
return converted;
|
|
277
|
+
};
|
|
278
|
+
const piGlobal = (global) => {
|
|
279
|
+
const converted = piTool(global);
|
|
280
|
+
if (global.spread !== void 0) converted.spread = global.spread;
|
|
281
|
+
if (global.signature !== void 0) converted.signature = global.signature;
|
|
282
|
+
return converted;
|
|
283
|
+
};
|
|
284
|
+
const failure = (error) => ({
|
|
285
|
+
ok: false,
|
|
286
|
+
error,
|
|
287
|
+
output: []
|
|
288
|
+
});
|
|
289
|
+
const StoreWrites = Schema.Struct({
|
|
290
|
+
set: Schema.Record(Schema.String, Schema.Json),
|
|
291
|
+
delete: Schema.Array(Schema.String)
|
|
292
|
+
});
|
|
293
|
+
const decodeStoreWrites = Schema.decodeUnknownOption(StoreWrites);
|
|
294
|
+
const fromPiResult = (result, scriptLines) => {
|
|
295
|
+
if (result.ok) {
|
|
296
|
+
const storeWrites = decodeStoreWrites(result.storeWrites);
|
|
297
|
+
if (Option.isNone(storeWrites)) return failure({
|
|
298
|
+
kind: "sandbox",
|
|
299
|
+
message: "The script reported store writes that are not JSON."
|
|
300
|
+
});
|
|
301
|
+
const converted = {
|
|
302
|
+
ok: true,
|
|
303
|
+
output: result.output,
|
|
304
|
+
storeWrites: storeWrites.value
|
|
305
|
+
};
|
|
306
|
+
if (result.value !== void 0) converted.value = result.value;
|
|
307
|
+
return converted;
|
|
308
|
+
}
|
|
309
|
+
const { kind, name, message, stack } = result.error;
|
|
310
|
+
const error = {
|
|
311
|
+
kind,
|
|
312
|
+
message: name !== void 0 && name.length > 0 && !message.startsWith(name) ? `${name}: ${message}` : message
|
|
313
|
+
};
|
|
314
|
+
if (stack !== void 0) error.stack = withoutWrapperFrames(stack, scriptLines);
|
|
315
|
+
return {
|
|
316
|
+
ok: false,
|
|
317
|
+
error,
|
|
318
|
+
output: result.output
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
const describeError = (error) => error instanceof Error ? error.message : String(error);
|
|
322
|
+
/**
|
|
323
|
+
* A `CodeModeExecutor` on `@earendil-works/pi-codemode`: every execution gets a fresh QuickJS VM
|
|
324
|
+
* (WebAssembly) in a fresh worker thread, closed when the execution ends. Applies the timeout,
|
|
325
|
+
* heap cap, abort signal, and store, strips TypeScript annotations first, rejects tool arguments
|
|
326
|
+
* that JSON would silently change inside the script (see `CodeModeExecutorTool`), and caps concurrent
|
|
327
|
+
* executions per executor; create one executor per process (module scope) so the cap is
|
|
328
|
+
* per process.
|
|
329
|
+
*
|
|
330
|
+
* Node only. The worker inherits a copy of `process.env` (scripts cannot read it: the VM has no
|
|
331
|
+
* `process`). Under Next.js, list `@yolk-sdk/codemode`, `@earendil-works/pi-codemode`, and
|
|
332
|
+
* `quickjs-wasi` in `serverExternalPackages` so the worker file and wasm stay on disk.
|
|
333
|
+
*/
|
|
334
|
+
const makePiCodeModeExecutor = (options = {}) => {
|
|
335
|
+
const slots = makeExecutionSlots(Math.max(1, Math.floor(options.maxConcurrentExecutions ?? 4)));
|
|
336
|
+
const run = async (code, execution, queuedAt) => {
|
|
337
|
+
const remaining = () => execution.timeoutMs - (Date.now() - queuedAt);
|
|
338
|
+
const stripped = stripTypes(code);
|
|
339
|
+
if (!stripped.ok) return failure(stripped.error);
|
|
340
|
+
const timeoutMs = remaining();
|
|
341
|
+
if (timeoutMs <= 0) return failure({
|
|
342
|
+
kind: "timeout",
|
|
343
|
+
message: `Execution timed out after ${execution.timeoutMs} ms`
|
|
344
|
+
});
|
|
345
|
+
const sandboxOptions = {
|
|
346
|
+
tools: execution.tools.map(piTool),
|
|
347
|
+
globals: execution.globals.map(piGlobal),
|
|
348
|
+
timeoutMs,
|
|
349
|
+
memoryLimitBytes: execution.memoryLimitBytes
|
|
350
|
+
};
|
|
351
|
+
if (options.wasm !== void 0) sandboxOptions.wasm = options.wasm;
|
|
352
|
+
if (options.workerUrl !== void 0) sandboxOptions.workerUrl = options.workerUrl;
|
|
353
|
+
let sandbox;
|
|
354
|
+
try {
|
|
355
|
+
sandbox = new CodemodeSandbox(sandboxOptions);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
return failure({
|
|
358
|
+
kind: "sandbox",
|
|
359
|
+
message: `Could not create the sandbox: ${describeError(error)}`
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
try {
|
|
363
|
+
return fromPiResult(await sandbox.execute(`${guardPrefix}${stripped.code}${guardSuffix(execution.tools)}`, {
|
|
364
|
+
signal: execution.signal,
|
|
365
|
+
store: execution.store,
|
|
366
|
+
timeoutMs
|
|
367
|
+
}), scriptLineCount(stripped.code));
|
|
368
|
+
} finally {
|
|
369
|
+
await sandbox.close();
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
return { execute: async (code, execution) => {
|
|
373
|
+
const queuedAt = Date.now();
|
|
374
|
+
const slot = await slots.acquire(execution.signal, execution.timeoutMs);
|
|
375
|
+
if (slot === "aborted") return failure({
|
|
376
|
+
kind: "aborted",
|
|
377
|
+
message: "Execution aborted"
|
|
378
|
+
});
|
|
379
|
+
if (slot === "timeout") return failure({
|
|
380
|
+
kind: "timeout",
|
|
381
|
+
message: `Execution timed out after ${execution.timeoutMs} ms while waiting for a free execution slot`
|
|
382
|
+
});
|
|
383
|
+
try {
|
|
384
|
+
return await run(code, execution, queuedAt);
|
|
385
|
+
} finally {
|
|
386
|
+
slots.release();
|
|
387
|
+
}
|
|
388
|
+
} };
|
|
389
|
+
};
|
|
390
|
+
//#endregion
|
|
391
|
+
export { defaultMaxConcurrentExecutions, makePiCodeModeExecutor };
|
|
392
|
+
|
|
393
|
+
//# sourceMappingURL=node.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.mjs","names":[],"sources":["../src/node.ts"],"sourcesContent":["import * as nodeModule from 'node:module'\nimport { Option, Predicate } from 'effect'\nimport * as Schema from 'effect/Schema'\nimport {\n CodemodeSandbox,\n type CodemodeResult,\n type CodemodeSandboxOptions,\n type CodemodeTool,\n type CodemodeWasmModule\n} from '@earendil-works/pi-codemode'\nimport { codeModeCallLabels } from './catalog.ts'\nimport type {\n CodeModeError,\n CodeModeExecuteOptions,\n CodeModeExecutionResult,\n CodeModeExecutor,\n CodeModeExecutorGlobal,\n CodeModeExecutorTool,\n CodeModeStoreWrites\n} from './executor.ts'\n\n/** Default cap of concurrent script executions per executor. */\nexport const defaultMaxConcurrentExecutions = 4\n\nexport type PiCodeModeExecutorOptions = {\n /**\n * Compiled `quickjs-wasi/quickjs.wasm` (see pi's `loadQuickJSWasm`). Default: the file of the\n * installed `quickjs-wasi` package, compiled once per process.\n */\n readonly wasm?: CodemodeWasmModule | Promise<CodemodeWasmModule>\n /**\n * Worker entry that imports `@earendil-works/pi-codemode/worker`. Default: pi's own worker file,\n * which exists on disk when the package is not bundled (Next `serverExternalPackages`).\n */\n readonly workerUrl?: string | URL\n /** Concurrent executions of this executor; more wait for a free slot. Default 4. */\n readonly maxConcurrentExecutions?: number\n}\n\ntype SlotOutcome = 'acquired' | 'aborted' | 'timeout'\n\ntype Waiter = (outcome: SlotOutcome) => void\n\n/** Abort- and deadline-aware counting semaphore for executions. */\nconst makeExecutionSlots = (max: number) => {\n let active = 0\n const waiting: Array<Waiter> = []\n\n const acquire = (signal: AbortSignal, timeoutMs: number): Promise<SlotOutcome> => {\n if (signal.aborted) return Promise.resolve('aborted')\n\n if (active < max) {\n active++\n\n return Promise.resolve('acquired')\n }\n\n return new Promise(resolve => {\n let timer: ReturnType<typeof setTimeout> | undefined\n\n const settle: Waiter = outcome => {\n const index = waiting.indexOf(settle)\n\n if (index !== -1) waiting.splice(index, 1)\n\n if (timer !== undefined) clearTimeout(timer)\n signal.removeEventListener('abort', onAbort)\n resolve(outcome)\n }\n\n const onAbort = () => settle('aborted')\n\n waiting.push(settle)\n signal.addEventListener('abort', onAbort, { once: true })\n\n if (Number.isFinite(timeoutMs)) {\n timer = setTimeout(() => settle('timeout'), Math.max(0, timeoutMs))\n }\n })\n }\n\n const release = () => {\n const next = waiting[0]\n\n if (next === undefined) {\n active--\n } else {\n // The slot passes straight to the next waiter.\n next('acquired')\n }\n }\n\n return { acquire, release }\n}\n\nconst wrapperPrefix = 'async function __yolkCodeMode() {\\n'\n\nconst wrapperSuffix = '\\n}'\n\n/** Most values one argument may hold; a larger argument is rejected. */\nconst maxArgumentValues = 100_000\n\n/** Deepest object/array nesting one argument may have; deeper arguments are rejected. */\nconst maxArgumentDepth = 64\n\n/**\n * Evaluated in the VM before the script runs: `(tools, labels) => guardedTools`, with `labels` the\n * `[name, callLabel]` pairs of the tools in order (`codeModeCallLabels`). Each call walks its first\n * argument once, the way `JSON.stringify` reads it (each own enumerable string key and each array\n * index read once, so getters run once; a callable `toJSON` is called once with the key, `''` at\n * the root, and its result is walked at the same path), and builds a fresh copy (null-prototype\n * objects and arrays) that is what the call sends. It rejects with a `TypeError` naming the call\n * label and path when JSON would change the value instead of carrying it: non-finite numbers,\n * invalid `Date`s, `undefined`/function/symbol array items and holes, function/symbol/bigint\n * values, cycles, and objects that are neither plain (prototype `Object.prototype` or `null`) nor\n * arrays and have no `toJSON`. An argument with more than `maxArgumentValues` values or nested more\n * than `maxArgumentDepth` levels is rejected too, never sent unchecked. `undefined`-valued keys are\n * left out (absent, as JSON drops them); the same object twice (not a cycle) is copied twice. An\n * error thrown by a getter or `toJSON` rejects the call with that error. A rejected call never\n * reaches the host. Every member of pi's `tools` is wrapped; unknown members fall through to pi's\n * proxy (close-match suggestions). Not a security boundary (`globalThis.tools` is unguarded):\n * arguments are still decoded by the host.\n */\nconst toolsGuardSource = `(function (tools, labels) {\n // Captured before the script runs, so a script that changes built-ins cannot change the checks.\n const { apply, getPrototypeOf, setPrototypeOf } = Reflect\n const isFinite = Number.isFinite\n const isArray = Array.isArray\n const { keys, create, defineProperty } = Object\n const getTime = Date.prototype.getTime\n const TypeErrorCtor = TypeError\n const SetCtor = Set\n const setAdd = Set.prototype.add\n const setHas = Set.prototype.has\n const setDelete = Set.prototype.delete\n const ObjectProto = Object.prototype\n const { trunc, min } = Math\n const stringify = JSON.stringify\n const plainJson = 'pass plain JSON (objects, arrays, strings, finite numbers, booleans, null)'\n const maxValues = ${maxArgumentValues}\n const maxDepth = ${maxArgumentDepth}\n class Rejected {\n constructor(message) {\n defineProperty(this, 'message', { value: message })\n }\n }\n const keyPath = (path, key) =>\n /^[A-Za-z_$][\\\\w$]*$/.test(key)\n ? (path === '' ? key : path + '.' + key)\n : path + '[' + stringify(key) + ']'\n const typeName = value => {\n const proto = getPrototypeOf(value)\n const name = proto && typeof proto.constructor === 'function' ? proto.constructor.name : ''\n return name ? (/^[AEIOU]/.test(name) ? 'an ' : 'a ') + name : 'a non-plain object'\n }\n // The JSON copy of a value already read from its holder at key (undefined: absent), or throws\n // Rejected with why JSON would not carry it unchanged.\n const snapshot = (read, key, path, slot, walk) => {\n const at = path === '' ? 'is ' : 'at ' + path + ' is '\n if (++walk.values > maxValues) {\n throw new Rejected('has more than ' + maxValues + ' values; split the work across calls')\n }\n let value = read\n const kind = typeof value\n if ((kind === 'object' && value !== null) || kind === 'function' || kind === 'bigint') {\n // The intrinsic getTime reads a Date's internal time (and throws for anything else).\n let time\n try {\n time = apply(getTime, value, [])\n } catch {\n time = undefined\n }\n if (time !== undefined && !isFinite(time)) {\n throw new Rejected(at + 'an invalid Date; pass a valid Date or an ISO string')\n }\n const toJSON = value.toJSON\n if (typeof toJSON === 'function') value = apply(toJSON, value, [key])\n }\n switch (typeof value) {\n case 'string':\n case 'boolean':\n return value\n case 'number':\n if (isFinite(value)) return value\n throw new Rejected(\n at + value + '; pass a finite number' + (slot === 'key' ? ' or omit the key' : '')\n )\n case 'undefined':\n if (slot === 'item') throw new Rejected(at + 'undefined; arrays cannot hold undefined')\n return undefined\n case 'bigint':\n case 'function':\n case 'symbol':\n throw new Rejected(at + 'a ' + typeof value + '; ' + plainJson)\n }\n if (value === null) return null\n if (apply(setHas, walk.ancestors, [value])) {\n throw new Rejected(at + 'a circular reference; ' + plainJson)\n }\n if (walk.depth >= maxDepth) {\n throw new Rejected('at ' + path + ' is nested more than ' + maxDepth + ' levels deep')\n }\n const array = isArray(value)\n if (!array) {\n const proto = getPrototypeOf(value)\n if (proto !== ObjectProto && proto !== null) {\n throw new Rejected(at + typeName(value) + '; ' + plainJson)\n }\n }\n apply(setAdd, walk.ancestors, [value])\n walk.depth++\n let copy\n if (array) {\n // LengthOfArrayLike, read once, as JSON.stringify does.\n // Unary plus is ToNumber: one coercion, and it throws for a bigint as JSON.stringify does.\n const raw = +value.length\n const length = raw > 0 ? min(trunc(raw), 9007199254740991) : 0\n // No prototype: no inherited toJSON or index setter can change the checked copy when pi\n // serializes it (still an array to JSON.stringify).\n copy = []\n setPrototypeOf(copy, null)\n for (let i = 0; i < length; i++) {\n copy[i] = snapshot(value[i], '' + i, path + '[' + i + ']', 'item', walk)\n }\n } else {\n copy = create(null)\n // Indexed over the fresh key array: no iterator a script could replace.\n const names = keys(value)\n for (let i = 0; i < names.length; i++) {\n const name = names[i]\n const item = snapshot(value[name], name, keyPath(path, name), 'key', walk)\n if (item !== undefined) copy[name] = item\n }\n }\n walk.depth--\n apply(setDelete, walk.ancestors, [value])\n return copy\n }\n const wrap = (label, call) => async (...args) => {\n let json\n try {\n json = snapshot(args[0], '', '', 'root', { values: 0, depth: 0, ancestors: new SetCtor() })\n } catch (error) {\n if (error instanceof Rejected) throw new TypeErrorCtor(label + ': argument ' + error.message)\n throw error\n }\n return call(json)\n }\n // Each tool's function sits at tools[name] unless an earlier tool holds that key, in which case\n // that earlier function is already wrapped.\n const wrappers = new Map()\n for (const [name, label] of labels) {\n const call = name in tools ? tools[name] : undefined\n if (typeof call === 'function' && !wrappers.has(call)) wrappers.set(call, wrap(label, call))\n }\n const guarded = Object.create(null)\n for (const key of Object.keys(tools)) {\n const call = tools[key]\n if (!wrappers.has(call)) wrappers.set(call, wrap('tools[' + JSON.stringify(key) + ']', call))\n guarded[key] = wrappers.get(call)\n }\n return new Proxy(Object.freeze(guarded), {\n get: (target, key) => (key in target ? target[key] : tools[key])\n })\n})`\n\n// pi runs `(async (tools, console) => {<code>\\n})`. The script runs in an inner function of the\n// same shape whose `tools` is guarded; the prefix shares line 1 with the script (only line-1\n// columns shift) and the guard follows the script, so its line numbers are unchanged.\nconst guardPrefix = 'return (async (tools, console) => {'\n\n/** JSON as a JavaScript expression (U+2028/U+2029 escaped for older parsers). */\nconst jsonSource = (value: unknown) =>\n JSON.stringify(value).replace(/[\\u2028\\u2029]/g, char =>\n char === '\\u2028' ? '\\\\u2028' : '\\\\u2029'\n )\n\nconst guardSuffix = (tools: ReadonlyArray<CodeModeExecutorTool>) => {\n const names = tools.map(tool => tool.name)\n const labels = codeModeCallLabels(names)\n\n // `makeCodeModeTool` supplies each label; other callers get pi's binding rule.\n const pairs = tools.map((tool, index) => [\n tool.name,\n tool.callLabel ?? labels[index] ?? `tools[${JSON.stringify(tool.name)}]`\n ])\n\n return `\\n})(${toolsGuardSource}(tools, ${jsonSource(pairs)}), console)`\n}\n\nconst lineTerminators = /\\r\\n?|[\\n\\u2028\\u2029]/g\n\n/** Lines of the script as QuickJS counts them (at least; never fewer). */\nconst scriptLineCount = (code: string) => (code.match(lineTerminators)?.length ?? 0) + 1\n\nconst scriptFrame = /codemode\\.js:(\\d+):\\d+/\n\n/** Drops stack frames of the wrapper and guard, which sit below the script's last line. */\nconst withoutWrapperFrames = (stack: string, scriptLines: number) =>\n stack\n .split('\\n')\n .filter(line => {\n const frame = scriptFrame.exec(line)\n\n return frame === null || Number(frame[1]) <= scriptLines\n })\n .join('\\n')\n\nconst ErrorWithCode = Schema.Struct({ code: Schema.String })\n\nconst errorCode = (error: unknown) =>\n Option.getOrUndefined(Option.map(Schema.decodeUnknownOption(ErrorWithCode)(error), e => e.code))\n\nconst syntaxLocation = (error: Error) => {\n const match = /^[^\\n]*:(\\d+)\\n([^\\n]*)\\n([^\\n]*)\\n/.exec(error.stack ?? '')\n\n if (match === null) return ''\n\n const line = Number(match[1]) - 1\n\n return line >= 1 ? ` (line ${line})\\n${match[2] ?? ''}\\n${match[3] ?? ''}` : ''\n}\n\nconst typeStripMessage = (error: unknown) => {\n const message = error instanceof Error ? error.message : String(error)\n\n if (errorCode(error) === 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX') {\n return `${message}. Write plain JavaScript: type annotations are stripped, but enums, namespaces, and parameter properties are not supported.`\n }\n\n const location = error instanceof Error ? syntaxLocation(error) : ''\n\n return `The script could not be parsed: ${message}${location}`\n}\n\ntype Stripped =\n | { readonly ok: true; readonly code: string }\n | { readonly ok: false; readonly error: CodeModeError }\n\n/**\n * Strips TypeScript annotations with Node's `stripTypeScriptTypes` (Node 22.13+; whitespace\n * replacement keeps line and column numbers). The script is wrapped in an async function so\n * top-level `return` and `await` parse. On Node without the API, code passes through unchanged.\n */\nconst stripTypes = (code: string): Stripped => {\n if (!Predicate.isFunction(nodeModule.stripTypeScriptTypes)) return { ok: true, code }\n\n try {\n const stripped = nodeModule.stripTypeScriptTypes(`${wrapperPrefix}${code}${wrapperSuffix}`, {\n mode: 'strip'\n })\n\n if (!stripped.startsWith(wrapperPrefix) || !stripped.endsWith(wrapperSuffix)) {\n return { ok: false, error: { kind: 'script', message: 'The script could not be parsed.' } }\n }\n\n return {\n ok: true,\n code: stripped.slice(wrapperPrefix.length, stripped.length - wrapperSuffix.length)\n }\n } catch (error) {\n return { ok: false, error: { kind: 'script', message: typeStripMessage(error) } }\n }\n}\n\nconst piTool = (tool: CodeModeExecutorTool): CodemodeTool => {\n const converted: CodemodeTool = {\n name: tool.name,\n execute: (args, context) => tool.execute(args, context)\n }\n\n if (tool.description !== undefined) converted.description = tool.description\n\n if (tool.inputSchema !== undefined) converted.inputSchema = tool.inputSchema\n\n if (tool.outputSchema !== undefined) converted.outputSchema = tool.outputSchema\n\n return converted\n}\n\nconst piGlobal = (global: CodeModeExecutorGlobal): CodemodeTool => {\n const converted = piTool(global)\n\n if (global.spread !== undefined) converted.spread = global.spread\n\n if (global.signature !== undefined) converted.signature = global.signature\n\n return converted\n}\n\nconst failure = (error: CodeModeError): CodeModeExecutionResult => ({\n ok: false,\n error,\n output: []\n})\n\nconst StoreWrites = Schema.Struct({\n set: Schema.Record(Schema.String, Schema.Json),\n delete: Schema.Array(Schema.String)\n})\n\nconst decodeStoreWrites = Schema.decodeUnknownOption(StoreWrites)\n\ntype MutableCodeModeError = {\n kind: CodeModeError['kind']\n message: string\n stack?: string\n}\n\ntype MutableExecutionResult = {\n ok: boolean\n value?: unknown\n error?: CodeModeError\n output: CodeModeExecutionResult['output']\n storeWrites?: CodeModeStoreWrites\n}\n\nconst fromPiResult = (result: CodemodeResult, scriptLines: number): CodeModeExecutionResult => {\n if (result.ok) {\n const storeWrites = decodeStoreWrites(result.storeWrites)\n\n if (Option.isNone(storeWrites)) {\n return failure({\n kind: 'sandbox',\n message: 'The script reported store writes that are not JSON.'\n })\n }\n\n const converted: MutableExecutionResult = {\n ok: true,\n output: result.output,\n storeWrites: storeWrites.value\n }\n\n if (result.value !== undefined) converted.value = result.value\n\n return converted\n }\n\n const { kind, name, message, stack } = result.error\n const named = name !== undefined && name.length > 0 && !message.startsWith(name)\n\n const error: MutableCodeModeError = {\n kind,\n message: named ? `${name}: ${message}` : message\n }\n\n if (stack !== undefined) error.stack = withoutWrapperFrames(stack, scriptLines)\n\n return { ok: false, error, output: result.output }\n}\n\nconst describeError = (error: unknown) => (error instanceof Error ? error.message : String(error))\n\n/**\n * A `CodeModeExecutor` on `@earendil-works/pi-codemode`: every execution gets a fresh QuickJS VM\n * (WebAssembly) in a fresh worker thread, closed when the execution ends. Applies the timeout,\n * heap cap, abort signal, and store, strips TypeScript annotations first, rejects tool arguments\n * that JSON would silently change inside the script (see `CodeModeExecutorTool`), and caps concurrent\n * executions per executor; create one executor per process (module scope) so the cap is\n * per process.\n *\n * Node only. The worker inherits a copy of `process.env` (scripts cannot read it: the VM has no\n * `process`). Under Next.js, list `@yolk-sdk/codemode`, `@earendil-works/pi-codemode`, and\n * `quickjs-wasi` in `serverExternalPackages` so the worker file and wasm stay on disk.\n */\nexport const makePiCodeModeExecutor = (\n options: PiCodeModeExecutorOptions = {}\n): CodeModeExecutor => {\n const slots = makeExecutionSlots(\n Math.max(1, Math.floor(options.maxConcurrentExecutions ?? defaultMaxConcurrentExecutions))\n )\n\n const run = async (code: string, execution: CodeModeExecuteOptions, queuedAt: number) => {\n const remaining = () => execution.timeoutMs - (Date.now() - queuedAt)\n const stripped = stripTypes(code)\n\n if (!stripped.ok) return failure(stripped.error)\n\n const timeoutMs = remaining()\n\n if (timeoutMs <= 0) {\n return failure({\n kind: 'timeout',\n message: `Execution timed out after ${execution.timeoutMs} ms`\n })\n }\n\n const sandboxOptions: CodemodeSandboxOptions = {\n tools: execution.tools.map(piTool),\n globals: execution.globals.map(piGlobal),\n timeoutMs,\n memoryLimitBytes: execution.memoryLimitBytes\n }\n\n if (options.wasm !== undefined) sandboxOptions.wasm = options.wasm\n\n if (options.workerUrl !== undefined) sandboxOptions.workerUrl = options.workerUrl\n\n let sandbox: CodemodeSandbox\n\n try {\n sandbox = new CodemodeSandbox(sandboxOptions)\n } catch (error) {\n return failure({\n kind: 'sandbox',\n message: `Could not create the sandbox: ${describeError(error)}`\n })\n }\n\n try {\n return fromPiResult(\n await sandbox.execute(`${guardPrefix}${stripped.code}${guardSuffix(execution.tools)}`, {\n signal: execution.signal,\n store: execution.store,\n timeoutMs\n }),\n scriptLineCount(stripped.code)\n )\n } finally {\n await sandbox.close()\n }\n }\n\n return {\n execute: async (code, execution) => {\n const queuedAt = Date.now()\n const slot = await slots.acquire(execution.signal, execution.timeoutMs)\n\n if (slot === 'aborted') return failure({ kind: 'aborted', message: 'Execution aborted' })\n\n if (slot === 'timeout') {\n return failure({\n kind: 'timeout',\n message: `Execution timed out after ${execution.timeoutMs} ms while waiting for a free execution slot`\n })\n }\n\n try {\n return await run(code, execution, queuedAt)\n } finally {\n slots.release()\n }\n }\n }\n}\n"],"mappings":";;;;;;;AAsBA,MAAa,iCAAiC;;AAsB9C,MAAM,sBAAsB,QAAgB;CAC1C,IAAI,SAAS;CACb,MAAM,UAAyB,CAAC;CAEhC,MAAM,WAAW,QAAqB,cAA4C;EAChF,IAAI,OAAO,SAAS,OAAO,QAAQ,QAAQ,SAAS;EAEpD,IAAI,SAAS,KAAK;GAChB;GAEA,OAAO,QAAQ,QAAQ,UAAU;EACnC;EAEA,OAAO,IAAI,SAAQ,YAAW;GAC5B,IAAI;GAEJ,MAAM,UAAiB,YAAW;IAChC,MAAM,QAAQ,QAAQ,QAAQ,MAAM;IAEpC,IAAI,UAAU,IAAI,QAAQ,OAAO,OAAO,CAAC;IAEzC,IAAI,UAAU,KAAA,GAAW,aAAa,KAAK;IAC3C,OAAO,oBAAoB,SAAS,OAAO;IAC3C,QAAQ,OAAO;GACjB;GAEA,MAAM,gBAAgB,OAAO,SAAS;GAEtC,QAAQ,KAAK,MAAM;GACnB,OAAO,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK,CAAC;GAExD,IAAI,OAAO,SAAS,SAAS,GAC3B,QAAQ,iBAAiB,OAAO,SAAS,GAAG,KAAK,IAAI,GAAG,SAAS,CAAC;EAEtE,CAAC;CACH;CAEA,MAAM,gBAAgB;EACpB,MAAM,OAAO,QAAQ;EAErB,IAAI,SAAS,KAAA,GACX;OAGA,KAAK,UAAU;CAEnB;CAEA,OAAO;EAAE;EAAS;CAAQ;AAC5B;AAEA,MAAM,gBAAgB;AAEtB,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;AA0BtB,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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
1
|
+
import { CodeModeExecutionResult, CodeModeOutputItem } from "./executor.mjs";
|
|
2
|
+
import { Content, NestedToolCallStatus } from "@yolk-sdk/agent/protocol";
|
|
3
|
+
|
|
4
|
+
//#region src/output.d.ts
|
|
5
|
+
/** Text or image piece of the model-visible result, in order. */
|
|
6
|
+
type CodeModeResultSegment = CodeModeOutputItem;
|
|
7
|
+
/**
|
|
8
|
+
* Keeps at most about `maxChars` characters of text: the head and the tail of the segments, with an
|
|
9
|
+
* omission marker in between. Images inside the omitted middle are dropped and counted.
|
|
10
|
+
*/
|
|
11
|
+
declare const boundCodeModeSegments: (segments: ReadonlyArray<CodeModeResultSegment>, maxChars: number) => ReadonlyArray<CodeModeResultSegment>;
|
|
12
|
+
/** Agent `Content`: a plain string without images, ordered text and image parts otherwise. */
|
|
13
|
+
declare const codeModeSegmentsContent: (segments: ReadonlyArray<CodeModeResultSegment>) => Content;
|
|
14
|
+
type CodeModeCallSummary = {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly status: NestedToolCallStatus;
|
|
17
|
+
};
|
|
18
|
+
/** `name: 2 ok, 1 error; other: 1 cancelled`, by first appearance. */
|
|
19
|
+
declare const summarizeCodeModeCalls: (calls: ReadonlyArray<CodeModeCallSummary>) => string;
|
|
20
|
+
/** Default cap of images kept in one result. */
|
|
21
|
+
declare const defaultCodeModeMaxImages = 8;
|
|
22
|
+
/** Default cap of base64 image data kept in one result, in characters (4 MiB). */
|
|
23
|
+
declare const defaultCodeModeMaxImageBytes: number;
|
|
24
|
+
/**
|
|
25
|
+
* Model-visible segments of one execution: a `Script completed` / `Script failed` header with the
|
|
26
|
+
* wall time, the output in order, the JSON return value (omitted when undefined), and for failures
|
|
27
|
+
* the error and the tool calls already made. Images are kept in order while they fit `maxImages`
|
|
28
|
+
* (default 8) and `maxImageBytes` of base64 in total (default 4 MiB); an image that would exceed
|
|
29
|
+
* either is dropped and counted in an omission note after the output. The result is then bounded
|
|
30
|
+
* by `maxChars` with a head-and-tail cut, which also drops images in the omitted middle.
|
|
31
|
+
*/
|
|
32
|
+
declare const codeModeResultSegments: (input: {
|
|
33
|
+
readonly result: CodeModeExecutionResult;
|
|
34
|
+
readonly wallTimeMs: number;
|
|
35
|
+
readonly calls: ReadonlyArray<CodeModeCallSummary>;
|
|
36
|
+
readonly maxChars: number;
|
|
37
|
+
readonly maxImages?: number;
|
|
38
|
+
readonly maxImageBytes?: number;
|
|
39
|
+
}) => ReadonlyArray<CodeModeResultSegment>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { CodeModeCallSummary, CodeModeResultSegment, boundCodeModeSegments, codeModeResultSegments, codeModeSegmentsContent, defaultCodeModeMaxImageBytes, defaultCodeModeMaxImages, summarizeCodeModeCalls };
|
|
42
|
+
//# sourceMappingURL=output.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output.d.mts","names":[],"sources":["../src/output.ts"],"mappings":";;;;;KAUY,qBAAA,GAAwB,kBAAkB;AAAtD;;;;AAAA,cA8Ba,qBAAA,GACX,QAAA,EAAU,aAAA,CAAc,qBAAA,GACxB,QAAA,aACC,aAAA,CAAc,qBAAA;AAHjB;AAAA,cA6Fa,uBAAA,GACX,QAAA,EAAU,aAAA,CAAc,qBAAA,MACvB,OAAA;AAAA,KAiBS,mBAAA;EAAA,SACD,IAAA;EAAA,SACA,MAAA,EAAQ,oBAAoB;AAAA;;cAM1B,sBAAA,GAA0B,KAAA,EAAO,aAAa,CAAC,mBAAA;;cAwC/C,wBAAA;;cAGA,4BAAA;;;;;AAhKyB;AA0FtC;;;cAmFa,sBAAA,GAA0B,KAAA;EAAA,SAC5B,MAAA,EAAQ,uBAAA;EAAA,SACR,UAAA;EAAA,SACA,KAAA,EAAO,aAAA,CAAc,mBAAA;EAAA,SACrB,QAAA;EAAA,SACA,SAAA;EAAA,SACA,aAAA;AAAA,MACP,aAAA,CAAc,qBAAA"}
|