@execbox/quickjs 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.
Files changed (49) hide show
  1. package/README.md +18 -4
  2. package/dist/index.cjs +543 -3
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +15 -4
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.ts +15 -4
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +543 -3
  9. package/dist/index.js.map +1 -1
  10. package/dist/processEntry.cjs +17 -0
  11. package/dist/processEntry.cjs.map +1 -0
  12. package/dist/processEntry.d.cts +5 -0
  13. package/dist/processEntry.d.ts +5 -0
  14. package/dist/processEntry.js +18 -0
  15. package/dist/processEntry.js.map +1 -0
  16. package/dist/protocolEndpoint-A1JBvHG_.cjs +147 -0
  17. package/dist/protocolEndpoint-A1JBvHG_.cjs.map +1 -0
  18. package/dist/protocolEndpoint-CDGgtfFu.js +142 -0
  19. package/dist/protocolEndpoint-CDGgtfFu.js.map +1 -0
  20. package/dist/runner/index.cjs +1 -1
  21. package/dist/runner/index.d.cts +10 -3
  22. package/dist/runner/index.d.cts.map +1 -1
  23. package/dist/runner/index.d.ts +10 -3
  24. package/dist/runner/index.d.ts.map +1 -1
  25. package/dist/runner/index.js +1 -1
  26. package/dist/runner/protocolEndpoint.cjs +3 -141
  27. package/dist/runner/protocolEndpoint.js +3 -141
  28. package/dist/{runner-CVaY4RVQ.js → runner-CteKTaPD.js} +28 -10
  29. package/dist/runner-CteKTaPD.js.map +1 -0
  30. package/dist/{runner-BqkDnP0t.cjs → runner-DRt0kpEk.cjs} +33 -9
  31. package/dist/{runner-CVaY4RVQ.js.map → runner-DRt0kpEk.cjs.map} +1 -1
  32. package/dist/types-BeVqrcj8.d.ts +71 -0
  33. package/dist/types-BeVqrcj8.d.ts.map +1 -0
  34. package/dist/types-CnNmLawC.d.cts +71 -0
  35. package/dist/types-CnNmLawC.d.cts.map +1 -0
  36. package/dist/workerEntry.cjs +19 -0
  37. package/dist/workerEntry.cjs.map +1 -0
  38. package/dist/workerEntry.d.cts +5 -0
  39. package/dist/workerEntry.d.ts +5 -0
  40. package/dist/workerEntry.js +20 -0
  41. package/dist/workerEntry.js.map +1 -0
  42. package/package.json +3 -3
  43. package/dist/runner/protocolEndpoint.cjs.map +0 -1
  44. package/dist/runner/protocolEndpoint.js.map +0 -1
  45. package/dist/runner-BqkDnP0t.cjs.map +0 -1
  46. package/dist/types-BXi8Lqtk.d.ts +0 -18
  47. package/dist/types-BXi8Lqtk.d.ts.map +0 -1
  48. package/dist/types-CVE2n2LY.d.cts +0 -18
  49. package/dist/types-CVE2n2LY.d.cts.map +0 -1
@@ -1,142 +1,4 @@
1
- const require_runner = require('../runner-BqkDnP0t.cjs');
2
- let node_crypto = require("node:crypto");
1
+ require('../runner-DRt0kpEk.cjs');
2
+ const require_protocolEndpoint = require('../protocolEndpoint-A1JBvHG_.cjs');
3
3
 
4
- //#region ../protocol/src/messages.ts
5
- function isRecord(value) {
6
- return typeof value === "object" && value !== null;
7
- }
8
- function isFiniteNumber(value) {
9
- return typeof value === "number" && Number.isFinite(value);
10
- }
11
- function isRuntimeOptions(value) {
12
- if (!isRecord(value)) return false;
13
- return isFiniteNumber(value.maxLogChars) && isFiniteNumber(value.maxLogLines) && isFiniteNumber(value.memoryLimitBytes) && isFiniteNumber(value.timeoutMs);
14
- }
15
- function isProviderManifest(value) {
16
- if (!isRecord(value) || typeof value.name !== "string") return false;
17
- if (!isRecord(value.tools) || typeof value.types !== "string") return false;
18
- return Object.values(value.tools).every((tool) => isRecord(tool) && typeof tool.originalName === "string" && typeof tool.safeName === "string" && (tool.description === void 0 || typeof tool.description === "string"));
19
- }
20
- function isExecuteError(value) {
21
- return isRecord(value) && typeof value.code === "string" && typeof value.message === "string";
22
- }
23
- /**
24
- * Returns whether an unknown value is a dispatcher-to-runner message.
25
- */
26
- function isDispatcherMessage(value) {
27
- if (!isRecord(value) || typeof value.type !== "string") return false;
28
- switch (value.type) {
29
- case "cancel": return typeof value.id === "string";
30
- case "execute": return typeof value.code === "string" && typeof value.id === "string" && isRuntimeOptions(value.options) && Array.isArray(value.providers) && value.providers.every(isProviderManifest);
31
- case "tool_result":
32
- if (typeof value.callId !== "string" || typeof value.ok !== "boolean") return false;
33
- if (value.ok) return "result" in value;
34
- return isExecuteError(value.error);
35
- default: return false;
36
- }
37
- }
38
-
39
- //#endregion
40
- //#region src/runner/protocolEndpoint.ts
41
- /**
42
- * @packageDocumentation
43
- * Public API for the `@execbox/quickjs/runner/protocol-endpoint` entrypoint.
44
- */
45
- /**
46
- * Attaches the shared QuickJS protocol loop to a worker/process messaging port.
47
- */
48
- function attachQuickJsProtocolEndpoint(port) {
49
- const pendingToolCalls = /* @__PURE__ */ new Map();
50
- let activeAbortController;
51
- let activeExecutionId;
52
- async function startExecution(message) {
53
- if (activeExecutionId) {
54
- port.send({
55
- durationMs: 0,
56
- error: {
57
- code: "internal_error",
58
- message: "QuickJS endpoint already has an active execution"
59
- },
60
- id: message.id,
61
- logs: [],
62
- ok: false,
63
- type: "done"
64
- });
65
- return;
66
- }
67
- const abortController = new AbortController();
68
- activeAbortController = abortController;
69
- activeExecutionId = message.id;
70
- try {
71
- const result = await require_runner.runQuickJsSession({
72
- abortController,
73
- code: message.code,
74
- onStarted: () => {
75
- port.send({
76
- id: message.id,
77
- type: "started"
78
- });
79
- },
80
- onToolCall: (call) => new Promise((resolve) => {
81
- const callId = (0, node_crypto.randomUUID)();
82
- pendingToolCalls.set(callId, resolve);
83
- port.send({
84
- ...call,
85
- callId,
86
- type: "tool_call"
87
- });
88
- }),
89
- providers: message.providers
90
- }, message.options);
91
- port.send({
92
- ...result,
93
- id: message.id,
94
- type: "done"
95
- });
96
- } catch (error) {
97
- port.send({
98
- durationMs: 0,
99
- error: {
100
- code: "internal_error",
101
- message: error instanceof Error ? error.message : String(error)
102
- },
103
- id: message.id,
104
- logs: [],
105
- ok: false,
106
- type: "done"
107
- });
108
- } finally {
109
- pendingToolCalls.clear();
110
- activeAbortController = void 0;
111
- activeExecutionId = void 0;
112
- }
113
- }
114
- const maybeDetach = port.onMessage((message) => {
115
- if (!isDispatcherMessage(message)) return;
116
- switch (message.type) {
117
- case "cancel":
118
- if (message.id === activeExecutionId) activeAbortController?.abort();
119
- break;
120
- case "execute":
121
- startExecution(message);
122
- break;
123
- case "tool_result": {
124
- const resolve = pendingToolCalls.get(message.callId);
125
- pendingToolCalls.delete(message.callId);
126
- resolve?.(message);
127
- break;
128
- }
129
- }
130
- });
131
- return () => {
132
- if (typeof maybeDetach === "function") maybeDetach();
133
- pendingToolCalls.clear();
134
- activeAbortController?.abort();
135
- activeAbortController = void 0;
136
- activeExecutionId = void 0;
137
- };
138
- }
139
-
140
- //#endregion
141
- exports.attachQuickJsProtocolEndpoint = attachQuickJsProtocolEndpoint;
142
- //# sourceMappingURL=protocolEndpoint.cjs.map
4
+ exports.attachQuickJsProtocolEndpoint = require_protocolEndpoint.attachQuickJsProtocolEndpoint;
@@ -1,142 +1,4 @@
1
- import { t as runQuickJsSession } from "../runner-CVaY4RVQ.js";
2
- import { randomUUID } from "node:crypto";
1
+ import "../runner-CteKTaPD.js";
2
+ import { t as attachQuickJsProtocolEndpoint } from "../protocolEndpoint-CDGgtfFu.js";
3
3
 
4
- //#region ../protocol/src/messages.ts
5
- function isRecord(value) {
6
- return typeof value === "object" && value !== null;
7
- }
8
- function isFiniteNumber(value) {
9
- return typeof value === "number" && Number.isFinite(value);
10
- }
11
- function isRuntimeOptions(value) {
12
- if (!isRecord(value)) return false;
13
- return isFiniteNumber(value.maxLogChars) && isFiniteNumber(value.maxLogLines) && isFiniteNumber(value.memoryLimitBytes) && isFiniteNumber(value.timeoutMs);
14
- }
15
- function isProviderManifest(value) {
16
- if (!isRecord(value) || typeof value.name !== "string") return false;
17
- if (!isRecord(value.tools) || typeof value.types !== "string") return false;
18
- return Object.values(value.tools).every((tool) => isRecord(tool) && typeof tool.originalName === "string" && typeof tool.safeName === "string" && (tool.description === void 0 || typeof tool.description === "string"));
19
- }
20
- function isExecuteError(value) {
21
- return isRecord(value) && typeof value.code === "string" && typeof value.message === "string";
22
- }
23
- /**
24
- * Returns whether an unknown value is a dispatcher-to-runner message.
25
- */
26
- function isDispatcherMessage(value) {
27
- if (!isRecord(value) || typeof value.type !== "string") return false;
28
- switch (value.type) {
29
- case "cancel": return typeof value.id === "string";
30
- case "execute": return typeof value.code === "string" && typeof value.id === "string" && isRuntimeOptions(value.options) && Array.isArray(value.providers) && value.providers.every(isProviderManifest);
31
- case "tool_result":
32
- if (typeof value.callId !== "string" || typeof value.ok !== "boolean") return false;
33
- if (value.ok) return "result" in value;
34
- return isExecuteError(value.error);
35
- default: return false;
36
- }
37
- }
38
-
39
- //#endregion
40
- //#region src/runner/protocolEndpoint.ts
41
- /**
42
- * @packageDocumentation
43
- * Public API for the `@execbox/quickjs/runner/protocol-endpoint` entrypoint.
44
- */
45
- /**
46
- * Attaches the shared QuickJS protocol loop to a worker/process messaging port.
47
- */
48
- function attachQuickJsProtocolEndpoint(port) {
49
- const pendingToolCalls = /* @__PURE__ */ new Map();
50
- let activeAbortController;
51
- let activeExecutionId;
52
- async function startExecution(message) {
53
- if (activeExecutionId) {
54
- port.send({
55
- durationMs: 0,
56
- error: {
57
- code: "internal_error",
58
- message: "QuickJS endpoint already has an active execution"
59
- },
60
- id: message.id,
61
- logs: [],
62
- ok: false,
63
- type: "done"
64
- });
65
- return;
66
- }
67
- const abortController = new AbortController();
68
- activeAbortController = abortController;
69
- activeExecutionId = message.id;
70
- try {
71
- const result = await runQuickJsSession({
72
- abortController,
73
- code: message.code,
74
- onStarted: () => {
75
- port.send({
76
- id: message.id,
77
- type: "started"
78
- });
79
- },
80
- onToolCall: (call) => new Promise((resolve) => {
81
- const callId = randomUUID();
82
- pendingToolCalls.set(callId, resolve);
83
- port.send({
84
- ...call,
85
- callId,
86
- type: "tool_call"
87
- });
88
- }),
89
- providers: message.providers
90
- }, message.options);
91
- port.send({
92
- ...result,
93
- id: message.id,
94
- type: "done"
95
- });
96
- } catch (error) {
97
- port.send({
98
- durationMs: 0,
99
- error: {
100
- code: "internal_error",
101
- message: error instanceof Error ? error.message : String(error)
102
- },
103
- id: message.id,
104
- logs: [],
105
- ok: false,
106
- type: "done"
107
- });
108
- } finally {
109
- pendingToolCalls.clear();
110
- activeAbortController = void 0;
111
- activeExecutionId = void 0;
112
- }
113
- }
114
- const maybeDetach = port.onMessage((message) => {
115
- if (!isDispatcherMessage(message)) return;
116
- switch (message.type) {
117
- case "cancel":
118
- if (message.id === activeExecutionId) activeAbortController?.abort();
119
- break;
120
- case "execute":
121
- startExecution(message);
122
- break;
123
- case "tool_result": {
124
- const resolve = pendingToolCalls.get(message.callId);
125
- pendingToolCalls.delete(message.callId);
126
- resolve?.(message);
127
- break;
128
- }
129
- }
130
- });
131
- return () => {
132
- if (typeof maybeDetach === "function") maybeDetach();
133
- pendingToolCalls.clear();
134
- activeAbortController?.abort();
135
- activeAbortController = void 0;
136
- activeExecutionId = void 0;
137
- };
138
- }
139
-
140
- //#endregion
141
- export { attachQuickJsProtocolEndpoint };
142
- //# sourceMappingURL=protocolEndpoint.js.map
4
+ export { attachQuickJsProtocolEndpoint };
@@ -5288,6 +5288,30 @@ function isJsonSerializable(value, active = /* @__PURE__ */ new Set(), memo = /*
5288
5288
  return false;
5289
5289
  }
5290
5290
 
5291
+ //#endregion
5292
+ //#region ../core/src/runtimeOptions.ts
5293
+ /**
5294
+ * Default runtime limits shared across the built-in executor implementations.
5295
+ */
5296
+ const DEFAULT_EXECUTOR_RUNTIME_OPTIONS = {
5297
+ maxLogChars: 64e3,
5298
+ maxLogLines: 100,
5299
+ memoryLimitBytes: 64 * 1024 * 1024,
5300
+ timeoutMs: 5e3
5301
+ };
5302
+ /**
5303
+ * Resolves executor runtime limits by applying explicit overrides on top of a
5304
+ * base options object and finally the shared defaults.
5305
+ */
5306
+ function resolveExecutorRuntimeOptions(options = {}, overrides = {}) {
5307
+ return {
5308
+ maxLogChars: overrides.maxLogChars ?? options.maxLogChars ?? DEFAULT_EXECUTOR_RUNTIME_OPTIONS.maxLogChars,
5309
+ maxLogLines: overrides.maxLogLines ?? options.maxLogLines ?? DEFAULT_EXECUTOR_RUNTIME_OPTIONS.maxLogLines,
5310
+ memoryLimitBytes: overrides.memoryLimitBytes ?? options.memoryLimitBytes ?? DEFAULT_EXECUTOR_RUNTIME_OPTIONS.memoryLimitBytes,
5311
+ timeoutMs: overrides.timeoutMs ?? options.timeoutMs ?? DEFAULT_EXECUTOR_RUNTIME_OPTIONS.timeoutMs
5312
+ };
5313
+ }
5314
+
5291
5315
  //#endregion
5292
5316
  //#region src/quickjsBridge.ts
5293
5317
  /**
@@ -5347,10 +5371,6 @@ function toGuestHandle(context, value) {
5347
5371
  * @packageDocumentation
5348
5372
  * Public API for the `@execbox/quickjs/runner` entrypoint.
5349
5373
  */
5350
- const DEFAULT_MEMORY_LIMIT_BYTES = 64 * 1024 * 1024;
5351
- const DEFAULT_TIMEOUT_MS = 5e3;
5352
- const DEFAULT_MAX_LOG_LINES = 100;
5353
- const DEFAULT_MAX_LOG_CHARS = 64e3;
5354
5374
  const loadDefaultModule = memoizePromiseFactory(() => newQuickJSWASMModule(RELEASE_SYNC));
5355
5375
  /**
5356
5376
  * Converts unexpected executor failures into stable public result errors.
@@ -5544,14 +5564,12 @@ function createToolHandle(context, providerName, safeToolName, signal, trustedHo
5544
5564
  * Runs one QuickJS-backed execution session using a transport-neutral tool callback.
5545
5565
  */
5546
5566
  async function runQuickJsSession(request, options = {}) {
5567
+ const runtimeOptions = resolveExecutorRuntimeOptions(options);
5547
5568
  const loadModule = async () => {
5548
5569
  if (options.module) return options.module;
5549
5570
  return options.loadModule ? await options.loadModule() : await loadDefaultModule();
5550
5571
  };
5551
- const maxLogChars = options.maxLogChars ?? DEFAULT_MAX_LOG_CHARS;
5552
- const maxLogLines = options.maxLogLines ?? DEFAULT_MAX_LOG_LINES;
5553
- const memoryLimitBytes = options.memoryLimitBytes ?? DEFAULT_MEMORY_LIMIT_BYTES;
5554
- const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
5572
+ const { maxLogChars, maxLogLines, memoryLimitBytes, timeoutMs } = runtimeOptions;
5555
5573
  const startedAt = Date.now();
5556
5574
  const logs = [];
5557
5575
  const abortController = new AbortController();
@@ -5632,5 +5650,5 @@ async function runQuickJsSession(request, options = {}) {
5632
5650
  }
5633
5651
 
5634
5652
  //#endregion
5635
- export { createExecutionContext as a, normalizeThrownMessage as c, isJsonSerializable as i, ExecuteFailure as n, createTimeoutExecuteResult as o, isExecuteFailure as r, getExecutionTimeoutMessage as s, runQuickJsSession as t };
5636
- //# sourceMappingURL=runner-CVaY4RVQ.js.map
5653
+ export { isJsonSerializable as a, getExecutionTimeoutMessage as c, isExecuteFailure as i, normalizeThrownMessage as l, resolveExecutorRuntimeOptions as n, createExecutionContext as o, ExecuteFailure as r, createTimeoutExecuteResult as s, runQuickJsSession as t };
5654
+ //# sourceMappingURL=runner-CteKTaPD.js.map