@codeworksh/aikit 0.2.1 → 0.3.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.
@@ -0,0 +1,7 @@
1
+ import { t as CodeMode } from "../../../codemode-z3ZIw-5q.mjs";
2
+
3
+ //#region src/agent/codemode/drivers/quickjs-wasi-driver.d.ts
4
+ declare function createQuickJSWasiDriver(): CodeMode.Driver;
5
+ //#endregion
6
+ export { createQuickJSWasiDriver };
7
+ //# sourceMappingURL=drivers.d.mts.map
@@ -0,0 +1,122 @@
1
+ import { QuickJS } from "quickjs-wasi";
2
+ //#region src/agent/codemode/drivers/quickjs-wasi-driver.ts
3
+ const DEFAULT_TIMEOUT_MS = 3e4;
4
+ const DEFAULT_MEMORY_LIMIT_MB = 128;
5
+ function withMessageInStack(message, stack) {
6
+ if (!stack) return void 0;
7
+ return stack.includes(message) ? stack : `${message}\n${stack}`;
8
+ }
9
+ function parseErrorLine(stack) {
10
+ if (typeof stack !== "string") return void 0;
11
+ const lineMatch = stack.match(/:(\d+)(?::\d+)?\)?$/m);
12
+ return lineMatch ? Number(lineMatch[1]) : void 0;
13
+ }
14
+ function normalizeExecutionError(error) {
15
+ if (error instanceof Error) return {
16
+ name: error.name,
17
+ message: error.message,
18
+ stack: withMessageInStack(error.message, error.stack),
19
+ line: parseErrorLine(error.stack)
20
+ };
21
+ if (typeof error === "object" && error !== null) {
22
+ const candidate = error;
23
+ const message = typeof candidate.message === "string" ? candidate.message : JSON.stringify(candidate) || "Unknown Execution Error";
24
+ return {
25
+ name: typeof candidate.name === "string" ? candidate.name : void 0,
26
+ message,
27
+ stack: typeof candidate.stack === "string" ? withMessageInStack(message, candidate.stack) : void 0,
28
+ line: typeof candidate.line === "number" ? candidate.line : parseErrorLine(candidate.stack)
29
+ };
30
+ }
31
+ return {
32
+ name: "Error",
33
+ message: String(error)
34
+ };
35
+ }
36
+ function toMegabytes(value) {
37
+ return (value ?? DEFAULT_MEMORY_LIMIT_MB) * 1024 * 1024;
38
+ }
39
+ function createConsole(vm, logs) {
40
+ const consoleObject = vm.newObject();
41
+ for (const [name, prefix] of [
42
+ ["log", ""],
43
+ ["info", "INFO: "],
44
+ ["warn", "WARN: "],
45
+ ["error", "ERROR: "]
46
+ ]) {
47
+ const fn = vm.newFunction(name, (...args) => {
48
+ const message = args.map((arg) => String(vm.dump(arg))).join(" ");
49
+ logs.push(`${prefix}${message}`);
50
+ return vm.undefined;
51
+ });
52
+ consoleObject.setProp(name, fn);
53
+ }
54
+ vm.setProp(vm.global, "console", consoleObject);
55
+ }
56
+ function registerBindings(vm, bindings, signal) {
57
+ let callCounter = 0;
58
+ for (const [name, binding] of Object.entries(bindings)) {
59
+ const fn = vm.newFunction(name, (...args) => {
60
+ const input = args[0] ? vm.dump(args[0]) : void 0;
61
+ const result = Promise.resolve(binding.execute(`${name}:${++callCounter}`, input, signal));
62
+ return vm.hostToHandle(result);
63
+ });
64
+ vm.setProp(vm.global, name, fn);
65
+ }
66
+ }
67
+ function createQuickJSWasiDriver() {
68
+ return { async createContext(config) {
69
+ const timeout = config.timeout ?? DEFAULT_TIMEOUT_MS;
70
+ const deadline = Date.now() + timeout;
71
+ const logs = [];
72
+ const vm = await QuickJS.create({
73
+ memoryLimit: toMegabytes(config.memoryLimit),
74
+ interruptHandler: () => Boolean(config.signal?.aborted) || Date.now() > deadline
75
+ });
76
+ createConsole(vm, logs);
77
+ registerBindings(vm, config.bindings, config.signal);
78
+ return {
79
+ async execute(code) {
80
+ let resultHandle;
81
+ try {
82
+ resultHandle = vm.evalCode(code, "script.js");
83
+ vm.executePendingJobs();
84
+ const settled = await vm.resolvePromise(resultHandle);
85
+ if ("error" in settled) try {
86
+ return {
87
+ success: false,
88
+ error: normalizeExecutionError(vm.dump(settled.error)),
89
+ logs: [...logs]
90
+ };
91
+ } finally {
92
+ settled.error.dispose();
93
+ }
94
+ try {
95
+ return {
96
+ success: true,
97
+ value: vm.dump(settled.value),
98
+ logs: [...logs]
99
+ };
100
+ } finally {
101
+ settled.value.dispose();
102
+ }
103
+ } catch (error) {
104
+ return {
105
+ success: false,
106
+ error: normalizeExecutionError(error),
107
+ logs: [...logs]
108
+ };
109
+ } finally {
110
+ resultHandle?.dispose();
111
+ }
112
+ },
113
+ async dispose() {
114
+ vm.dispose();
115
+ }
116
+ };
117
+ } };
118
+ }
119
+ //#endregion
120
+ export { createQuickJSWasiDriver };
121
+
122
+ //# sourceMappingURL=drivers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drivers.mjs","names":[],"sources":["../../../../../src/agent/codemode/drivers/quickjs-wasi-driver.ts"],"sourcesContent":["import { QuickJS } from \"quickjs-wasi\";\nimport type { CodeMode } from \"../codemode\";\n\nconst DEFAULT_TIMEOUT_MS = 30_000;\nconst DEFAULT_MEMORY_LIMIT_MB = 128;\n\nfunction withMessageInStack(message: string, stack: string | undefined): string | undefined {\n\tif (!stack) return undefined;\n\treturn stack.includes(message) ? stack : `${message}\\n${stack}`;\n}\n\nfunction parseErrorLine(stack: unknown): number | undefined {\n\tif (typeof stack !== \"string\") return undefined;\n\tconst lineMatch = stack.match(/:(\\d+)(?::\\d+)?\\)?$/m);\n\treturn lineMatch ? Number(lineMatch[1]) : undefined;\n}\n\nfunction normalizeExecutionError(error: unknown): CodeMode.NormalizedError {\n\tif (error instanceof Error) {\n\t\treturn {\n\t\t\tname: error.name,\n\t\t\tmessage: error.message,\n\t\t\tstack: withMessageInStack(error.message, error.stack),\n\t\t\tline: parseErrorLine(error.stack),\n\t\t};\n\t}\n\n\tif (typeof error === \"object\" && error !== null) {\n\t\tconst candidate = error as Record<string, unknown>;\n\t\tconst message =\n\t\t\ttypeof candidate.message === \"string\"\n\t\t\t\t? candidate.message\n\t\t\t\t: JSON.stringify(candidate) || \"Unknown Execution Error\";\n\t\treturn {\n\t\t\tname: typeof candidate.name === \"string\" ? candidate.name : undefined,\n\t\t\tmessage,\n\t\t\tstack: typeof candidate.stack === \"string\" ? withMessageInStack(message, candidate.stack) : undefined,\n\t\t\tline: typeof candidate.line === \"number\" ? candidate.line : parseErrorLine(candidate.stack),\n\t\t};\n\t}\n\n\treturn {\n\t\tname: \"Error\",\n\t\tmessage: String(error),\n\t};\n}\n\nfunction toMegabytes(value: number | undefined): number {\n\treturn (value ?? DEFAULT_MEMORY_LIMIT_MB) * 1024 * 1024;\n}\n\nfunction createConsole(vm: QuickJS, logs: string[]) {\n\tconst consoleObject = vm.newObject();\n\tconst methods = [\n\t\t[\"log\", \"\"],\n\t\t[\"info\", \"INFO: \"],\n\t\t[\"warn\", \"WARN: \"],\n\t\t[\"error\", \"ERROR: \"],\n\t] as const;\n\n\tfor (const [name, prefix] of methods) {\n\t\tconst fn = vm.newFunction(name, (...args) => {\n\t\t\tconst message = args.map((arg) => String(vm.dump(arg))).join(\" \");\n\t\t\tlogs.push(`${prefix}${message}`);\n\t\t\treturn vm.undefined;\n\t\t});\n\t\tconsoleObject.setProp(name, fn);\n\t}\n\n\tvm.setProp(vm.global, \"console\", consoleObject);\n}\n\nfunction registerBindings(vm: QuickJS, bindings: Record<string, CodeMode.ToolBinding>, signal?: AbortSignal) {\n\tlet callCounter = 0;\n\n\tfor (const [name, binding] of Object.entries(bindings)) {\n\t\tconst fn = vm.newFunction(name, (...args) => {\n\t\t\tconst input = args[0] ? vm.dump(args[0]) : undefined;\n\t\t\tconst result = Promise.resolve(binding.execute(`${name}:${++callCounter}`, input, signal));\n\t\t\treturn vm.hostToHandle(result);\n\t\t});\n\t\tvm.setProp(vm.global, name, fn);\n\t}\n}\n\nexport function createQuickJSWasiDriver(): CodeMode.Driver {\n\treturn {\n\t\tasync createContext(config: CodeMode.DriverContextConfig): Promise<CodeMode.Context> {\n\t\t\tconst timeout = config.timeout ?? DEFAULT_TIMEOUT_MS;\n\t\t\tconst deadline = Date.now() + timeout;\n\t\t\tconst logs: string[] = [];\n\t\t\t// create VM for execution sandbox\n\t\t\tconst vm = await QuickJS.create({\n\t\t\t\tmemoryLimit: toMegabytes(config.memoryLimit),\n\t\t\t\tinterruptHandler: () => Boolean(config.signal?.aborted) || Date.now() > deadline,\n\t\t\t});\n\n\t\t\t// create console log bindings\n\t\t\tcreateConsole(vm, logs);\n\t\t\t// register tool fn bindings\n\t\t\tregisterBindings(vm, config.bindings, config.signal);\n\n\t\t\treturn {\n\t\t\t\tasync execute<T = unknown>(code: string) {\n\t\t\t\t\tlet resultHandle: ReturnType<typeof vm.evalCode> | undefined;\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tresultHandle = vm.evalCode(code, \"script.js\");\n\t\t\t\t\t\tvm.executePendingJobs();\n\n\t\t\t\t\t\tconst settled = await vm.resolvePromise(resultHandle);\n\t\t\t\t\t\tif (\"error\" in settled) {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\t\t\t\terror: normalizeExecutionError(vm.dump(settled.error)),\n\t\t\t\t\t\t\t\t\tlogs: [...logs],\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\t\tsettled.error.dispose();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\t\tvalue: vm.dump(settled.value) as T,\n\t\t\t\t\t\t\t\tlogs: [...logs],\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tsettled.value.dispose();\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\t\terror: normalizeExecutionError(error),\n\t\t\t\t\t\t\tlogs: [...logs],\n\t\t\t\t\t\t};\n\t\t\t\t\t} finally {\n\t\t\t\t\t\tresultHandle?.dispose();\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tasync disp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