@execbox/quickjs 0.1.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/LICENSE +21 -0
- package/README.md +65 -0
- package/dist/index.cjs +46 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/dist/runner/index.cjs +3 -0
- package/dist/runner/index.d.cts +104 -0
- package/dist/runner/index.d.cts.map +1 -0
- package/dist/runner/index.d.ts +104 -0
- package/dist/runner/index.d.ts.map +1 -0
- package/dist/runner/index.js +3 -0
- package/dist/runner/protocolEndpoint.cjs +101 -0
- package/dist/runner/protocolEndpoint.cjs.map +1 -0
- package/dist/runner/protocolEndpoint.d.cts +18 -0
- package/dist/runner/protocolEndpoint.d.cts.map +1 -0
- package/dist/runner/protocolEndpoint.d.ts +18 -0
- package/dist/runner/protocolEndpoint.d.ts.map +1 -0
- package/dist/runner/protocolEndpoint.js +101 -0
- package/dist/runner/protocolEndpoint.js.map +1 -0
- package/dist/runner-CYXIVOmJ.cjs +5562 -0
- package/dist/runner-CYXIVOmJ.cjs.map +1 -0
- package/dist/runner-DVOLiGt4.js +5557 -0
- package/dist/runner-DVOLiGt4.js.map +1 -0
- package/dist/types-BB8mb_-T.d.cts +14 -0
- package/dist/types-BB8mb_-T.d.cts.map +1 -0
- package/dist/types-D7uau0GM.d.ts +14 -0
- package/dist/types-D7uau0GM.d.ts.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { t as runQuickJsSession } from "../runner-DVOLiGt4.js";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
//#region src/runner/protocolEndpoint.ts
|
|
5
|
+
/**
|
|
6
|
+
* Attaches the shared QuickJS protocol loop to a worker/process messaging port.
|
|
7
|
+
*/
|
|
8
|
+
function attachQuickJsProtocolEndpoint(port) {
|
|
9
|
+
const pendingToolCalls = /* @__PURE__ */ new Map();
|
|
10
|
+
let activeAbortController;
|
|
11
|
+
let activeExecutionId;
|
|
12
|
+
async function startExecution(message) {
|
|
13
|
+
if (activeExecutionId) {
|
|
14
|
+
port.send({
|
|
15
|
+
durationMs: 0,
|
|
16
|
+
error: {
|
|
17
|
+
code: "internal_error",
|
|
18
|
+
message: "QuickJS endpoint already has an active execution"
|
|
19
|
+
},
|
|
20
|
+
id: message.id,
|
|
21
|
+
logs: [],
|
|
22
|
+
ok: false,
|
|
23
|
+
type: "done"
|
|
24
|
+
});
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const abortController = new AbortController();
|
|
28
|
+
activeAbortController = abortController;
|
|
29
|
+
activeExecutionId = message.id;
|
|
30
|
+
try {
|
|
31
|
+
const result = await runQuickJsSession({
|
|
32
|
+
abortController,
|
|
33
|
+
code: message.code,
|
|
34
|
+
onStarted: () => {
|
|
35
|
+
port.send({
|
|
36
|
+
id: message.id,
|
|
37
|
+
type: "started"
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
onToolCall: (call) => new Promise((resolve) => {
|
|
41
|
+
const callId = randomUUID();
|
|
42
|
+
pendingToolCalls.set(callId, resolve);
|
|
43
|
+
port.send({
|
|
44
|
+
...call,
|
|
45
|
+
callId,
|
|
46
|
+
type: "tool_call"
|
|
47
|
+
});
|
|
48
|
+
}),
|
|
49
|
+
providers: message.providers
|
|
50
|
+
}, message.options);
|
|
51
|
+
port.send({
|
|
52
|
+
...result,
|
|
53
|
+
id: message.id,
|
|
54
|
+
type: "done"
|
|
55
|
+
});
|
|
56
|
+
} catch (error) {
|
|
57
|
+
port.send({
|
|
58
|
+
durationMs: 0,
|
|
59
|
+
error: {
|
|
60
|
+
code: "internal_error",
|
|
61
|
+
message: error instanceof Error ? error.message : String(error)
|
|
62
|
+
},
|
|
63
|
+
id: message.id,
|
|
64
|
+
logs: [],
|
|
65
|
+
ok: false,
|
|
66
|
+
type: "done"
|
|
67
|
+
});
|
|
68
|
+
} finally {
|
|
69
|
+
pendingToolCalls.clear();
|
|
70
|
+
activeAbortController = void 0;
|
|
71
|
+
activeExecutionId = void 0;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const maybeDetach = port.onMessage((message) => {
|
|
75
|
+
switch (message.type) {
|
|
76
|
+
case "cancel":
|
|
77
|
+
if (message.id === activeExecutionId) activeAbortController?.abort();
|
|
78
|
+
break;
|
|
79
|
+
case "execute":
|
|
80
|
+
startExecution(message);
|
|
81
|
+
break;
|
|
82
|
+
case "tool_result": {
|
|
83
|
+
const resolve = pendingToolCalls.get(message.callId);
|
|
84
|
+
pendingToolCalls.delete(message.callId);
|
|
85
|
+
resolve?.(message);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return () => {
|
|
91
|
+
if (typeof maybeDetach === "function") maybeDetach();
|
|
92
|
+
pendingToolCalls.clear();
|
|
93
|
+
activeAbortController?.abort();
|
|
94
|
+
activeAbortController = void 0;
|
|
95
|
+
activeExecutionId = void 0;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { attachQuickJsProtocolEndpoint };
|
|
101
|
+
//# sourceMappingURL=protocolEndpoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocolEndpoint.js","names":["activeAbortController: AbortController | undefined","activeExecutionId: string | undefined"],"sources":["../../src/runner/protocolEndpoint.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\n\nimport type {\n DispatcherMessage,\n ExecuteMessage,\n RunnerMessage,\n ToolCallResult,\n} from \"@execbox/protocol\";\n\nimport { runQuickJsSession } from \"./index.ts\";\n\n/**\n * Minimal worker/process-side port used by the shared QuickJS protocol endpoint.\n */\nexport interface QuickJsProtocolPort {\n onMessage(handler: (message: DispatcherMessage) => void): void | (() => void);\n send(message: RunnerMessage): void;\n}\n\n/**\n * Attaches the shared QuickJS protocol loop to a worker/process messaging port.\n */\nexport function attachQuickJsProtocolEndpoint(\n port: QuickJsProtocolPort,\n): () => void {\n const pendingToolCalls = new Map<string, (result: ToolCallResult) => void>();\n let activeAbortController: AbortController | undefined;\n let activeExecutionId: string | undefined;\n\n async function startExecution(message: ExecuteMessage): Promise<void> {\n if (activeExecutionId) {\n port.send({\n durationMs: 0,\n error: {\n code: \"internal_error\",\n message: \"QuickJS endpoint already has an active execution\",\n },\n id: message.id,\n logs: [],\n ok: false,\n type: \"done\",\n });\n return;\n }\n\n const abortController = new AbortController();\n activeAbortController = abortController;\n activeExecutionId = message.id;\n\n try {\n const result = await runQuickJsSession(\n {\n abortController,\n code: message.code,\n onStarted: () => {\n port.send({\n id: message.id,\n type: \"started\",\n });\n },\n onToolCall: (call) =>\n new Promise<ToolCallResult>((resolve) => {\n const callId = randomUUID();\n pendingToolCalls.set(callId, resolve);\n port.send({\n ...call,\n callId,\n type: \"tool_call\",\n });\n }),\n providers: message.providers,\n },\n message.options,\n );\n\n port.send({\n ...result,\n id: message.id,\n type: \"done\",\n });\n } catch (error) {\n port.send({\n durationMs: 0,\n error: {\n code: \"internal_error\",\n message: error instanceof Error ? error.message : String(error),\n },\n id: message.id,\n logs: [],\n ok: false,\n type: \"done\",\n });\n } finally {\n pendingToolCalls.clear();\n activeAbortController = undefined;\n activeExecutionId = undefined;\n }\n }\n\n const maybeDetach = port.onMessage((message: DispatcherMessage) => {\n switch (message.type) {\n case \"cancel\":\n if (message.id === activeExecutionId) {\n activeAbortController?.abort();\n }\n break;\n case \"execute\":\n void startExecution(message);\n break;\n case \"tool_result\": {\n const resolve = pendingToolCalls.get(message.callId);\n pendingToolCalls.delete(message.callId);\n resolve?.(message);\n break;\n }\n }\n });\n\n return () => {\n if (typeof maybeDetach === \"function\") {\n maybeDetach();\n }\n pendingToolCalls.clear();\n activeAbortController?.abort();\n activ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