@inkeep/agents-run-api 0.0.0-dev-20251222193909 → 0.0.0-dev-20251222214618

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 (173) hide show
  1. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/system-prompt.js +5 -0
  2. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/thinking-preparation.js +5 -0
  3. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/tool.js +5 -0
  4. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-component.js +5 -0
  5. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-components.js +5 -0
  6. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/system-prompt.js +5 -0
  7. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact-retrieval-guidance.js +5 -0
  8. package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact.js +5 -0
  9. package/dist/a2a/client.d.ts +184 -0
  10. package/dist/a2a/client.js +510 -0
  11. package/dist/a2a/handlers.d.ts +7 -0
  12. package/dist/a2a/handlers.js +559 -0
  13. package/dist/a2a/transfer.d.ts +22 -0
  14. package/dist/a2a/transfer.js +46 -0
  15. package/dist/a2a/types.d.ts +79 -0
  16. package/dist/a2a/types.js +22 -0
  17. package/dist/agents/Agent.d.ts +220 -0
  18. package/dist/agents/Agent.js +1938 -0
  19. package/dist/agents/ModelFactory.d.ts +63 -0
  20. package/dist/agents/ModelFactory.js +194 -0
  21. package/dist/agents/SystemPromptBuilder.d.ts +21 -0
  22. package/dist/agents/SystemPromptBuilder.js +48 -0
  23. package/dist/agents/ToolSessionManager.d.ts +63 -0
  24. package/dist/agents/ToolSessionManager.js +146 -0
  25. package/dist/agents/generateTaskHandler.d.ts +49 -0
  26. package/dist/agents/generateTaskHandler.js +512 -0
  27. package/dist/agents/relationTools.d.ts +57 -0
  28. package/dist/agents/relationTools.js +262 -0
  29. package/dist/agents/types.d.ts +28 -0
  30. package/dist/agents/types.js +1 -0
  31. package/dist/agents/versions/v1/Phase1Config.d.ts +26 -0
  32. package/dist/agents/versions/v1/Phase1Config.js +413 -0
  33. package/dist/agents/versions/v1/Phase2Config.d.ts +31 -0
  34. package/dist/agents/versions/v1/Phase2Config.js +330 -0
  35. package/dist/constants/execution-limits/defaults.d.ts +47 -0
  36. package/dist/constants/execution-limits/defaults.js +48 -0
  37. package/dist/constants/execution-limits/index.d.ts +6 -0
  38. package/dist/constants/execution-limits/index.js +16 -0
  39. package/dist/create-app.d.ts +9 -0
  40. package/dist/create-app.js +195 -0
  41. package/dist/data/agent.d.ts +7 -0
  42. package/dist/data/agent.js +72 -0
  43. package/dist/data/agents.d.ts +34 -0
  44. package/dist/data/agents.js +139 -0
  45. package/dist/data/conversations.d.ts +88 -0
  46. package/dist/{conversations2.js → data/conversations.js} +42 -7
  47. package/dist/data/db/dbClient.d.ts +6 -0
  48. package/dist/data/db/dbClient.js +17 -0
  49. package/dist/env.d.ts +57 -0
  50. package/dist/env.js +1 -2
  51. package/dist/handlers/executionHandler.d.ts +39 -0
  52. package/dist/handlers/executionHandler.js +456 -0
  53. package/dist/index.d.ts +8 -29
  54. package/dist/index.js +5 -11487
  55. package/dist/instrumentation.d.ts +1 -2
  56. package/dist/instrumentation.js +66 -3
  57. package/dist/{logger2.js → logger.d.ts} +1 -2
  58. package/dist/logger.js +1 -1
  59. package/dist/middleware/api-key-auth.d.ts +26 -0
  60. package/dist/middleware/api-key-auth.js +240 -0
  61. package/dist/middleware/index.d.ts +2 -0
  62. package/dist/middleware/index.js +3 -0
  63. package/dist/openapi.d.ts +4 -0
  64. package/dist/openapi.js +54 -0
  65. package/dist/routes/agents.d.ts +12 -0
  66. package/dist/routes/agents.js +147 -0
  67. package/dist/routes/chat.d.ts +13 -0
  68. package/dist/routes/chat.js +293 -0
  69. package/dist/routes/chatDataStream.d.ts +13 -0
  70. package/dist/routes/chatDataStream.js +352 -0
  71. package/dist/routes/mcp.d.ts +13 -0
  72. package/dist/routes/mcp.js +495 -0
  73. package/dist/services/AgentSession.d.ts +356 -0
  74. package/dist/services/AgentSession.js +1208 -0
  75. package/dist/services/ArtifactParser.d.ts +105 -0
  76. package/dist/services/ArtifactParser.js +338 -0
  77. package/dist/services/ArtifactService.d.ts +123 -0
  78. package/dist/services/ArtifactService.js +611 -0
  79. package/dist/services/IncrementalStreamParser.d.ts +98 -0
  80. package/dist/services/IncrementalStreamParser.js +327 -0
  81. package/dist/services/MidGenerationCompressor.d.ts +91 -0
  82. package/dist/services/MidGenerationCompressor.js +375 -0
  83. package/dist/services/PendingToolApprovalManager.d.ts +62 -0
  84. package/dist/services/PendingToolApprovalManager.js +133 -0
  85. package/dist/services/ResponseFormatter.d.ts +39 -0
  86. package/dist/services/ResponseFormatter.js +152 -0
  87. package/dist/tools/NativeSandboxExecutor.d.ts +38 -0
  88. package/dist/tools/NativeSandboxExecutor.js +432 -0
  89. package/dist/tools/SandboxExecutorFactory.d.ts +36 -0
  90. package/dist/tools/SandboxExecutorFactory.js +80 -0
  91. package/dist/tools/VercelSandboxExecutor.d.ts +71 -0
  92. package/dist/tools/VercelSandboxExecutor.js +340 -0
  93. package/dist/tools/distill-conversation-tool.d.ts +41 -0
  94. package/dist/tools/distill-conversation-tool.js +156 -0
  95. package/dist/tools/sandbox-utils.d.ts +18 -0
  96. package/dist/tools/sandbox-utils.js +53 -0
  97. package/dist/types/chat.d.ts +27 -0
  98. package/dist/types/chat.js +1 -0
  99. package/dist/types/execution-context.d.ts +46 -0
  100. package/dist/types/execution-context.js +27 -0
  101. package/dist/types/xml.d.ts +5 -0
  102. package/dist/utils/SchemaProcessor.d.ts +52 -0
  103. package/dist/utils/SchemaProcessor.js +182 -0
  104. package/dist/utils/agent-operations.d.ts +62 -0
  105. package/dist/utils/agent-operations.js +53 -0
  106. package/dist/utils/artifact-component-schema.d.ts +42 -0
  107. package/dist/utils/artifact-component-schema.js +186 -0
  108. package/dist/utils/cleanup.d.ts +21 -0
  109. package/dist/utils/cleanup.js +59 -0
  110. package/dist/utils/data-component-schema.d.ts +2 -0
  111. package/dist/utils/data-component-schema.js +3 -0
  112. package/dist/utils/default-status-schemas.d.ts +20 -0
  113. package/dist/utils/default-status-schemas.js +24 -0
  114. package/dist/utils/json-postprocessor.d.ts +13 -0
  115. package/dist/{json-postprocessor.cjs → utils/json-postprocessor.js} +1 -2
  116. package/dist/utils/model-context-utils.d.ts +31 -0
  117. package/dist/utils/model-context-utils.js +149 -0
  118. package/dist/utils/model-resolver.d.ts +6 -0
  119. package/dist/utils/model-resolver.js +34 -0
  120. package/dist/utils/schema-validation.d.ts +44 -0
  121. package/dist/utils/schema-validation.js +97 -0
  122. package/dist/utils/stream-helpers.d.ts +197 -0
  123. package/dist/utils/stream-helpers.js +518 -0
  124. package/dist/utils/stream-registry.d.ts +22 -0
  125. package/dist/utils/stream-registry.js +34 -0
  126. package/dist/utils/token-estimator.d.ts +69 -0
  127. package/dist/utils/token-estimator.js +53 -0
  128. package/dist/utils/tracer.d.ts +7 -0
  129. package/dist/utils/tracer.js +7 -0
  130. package/package.json +4 -20
  131. package/dist/SandboxExecutorFactory.cjs +0 -895
  132. package/dist/SandboxExecutorFactory.js +0 -893
  133. package/dist/SandboxExecutorFactory.js.map +0 -1
  134. package/dist/chunk-VBDAOXYI.cjs +0 -927
  135. package/dist/chunk-VBDAOXYI.js +0 -832
  136. package/dist/chunk-VBDAOXYI.js.map +0 -1
  137. package/dist/chunk.cjs +0 -34
  138. package/dist/conversations.cjs +0 -7
  139. package/dist/conversations.js +0 -7
  140. package/dist/conversations2.cjs +0 -209
  141. package/dist/conversations2.js.map +0 -1
  142. package/dist/dbClient.cjs +0 -9676
  143. package/dist/dbClient.js +0 -9670
  144. package/dist/dbClient.js.map +0 -1
  145. package/dist/dbClient2.cjs +0 -5
  146. package/dist/dbClient2.js +0 -5
  147. package/dist/env.cjs +0 -59
  148. package/dist/env.js.map +0 -1
  149. package/dist/execution-limits.cjs +0 -260
  150. package/dist/execution-limits.js +0 -63
  151. package/dist/execution-limits.js.map +0 -1
  152. package/dist/index.cjs +0 -11512
  153. package/dist/index.d.cts +0 -36
  154. package/dist/index.d.cts.map +0 -1
  155. package/dist/index.d.ts.map +0 -1
  156. package/dist/index.js.map +0 -1
  157. package/dist/instrumentation.cjs +0 -12
  158. package/dist/instrumentation.d.cts +0 -18
  159. package/dist/instrumentation.d.cts.map +0 -1
  160. package/dist/instrumentation.d.ts.map +0 -1
  161. package/dist/instrumentation2.cjs +0 -116
  162. package/dist/instrumentation2.js +0 -69
  163. package/dist/instrumentation2.js.map +0 -1
  164. package/dist/json-postprocessor.js +0 -20
  165. package/dist/json-postprocessor.js.map +0 -1
  166. package/dist/logger.cjs +0 -5
  167. package/dist/logger2.cjs +0 -1
  168. package/dist/nodefs.cjs +0 -29
  169. package/dist/nodefs.js +0 -27
  170. package/dist/nodefs.js.map +0 -1
  171. package/dist/opfs-ahp.cjs +0 -367
  172. package/dist/opfs-ahp.js +0 -368
  173. package/dist/opfs-ahp.js.map +0 -1
@@ -0,0 +1,152 @@
1
+ import { getLogger } from "../logger.js";
2
+ import { setSpanWithError, tracer } from "../utils/tracer.js";
3
+ import { ArtifactParser } from "./ArtifactParser.js";
4
+ import { agentSessionManager } from "./AgentSession.js";
5
+
6
+ //#region src/services/ResponseFormatter.ts
7
+ const logger = getLogger("ResponseFormatter");
8
+ /**
9
+ * Response formatter that uses the unified ArtifactParser to convert artifact markers
10
+ * into data parts for consistent artifact handling across all agent responses
11
+ */
12
+ var ResponseFormatter = class {
13
+ artifactParser;
14
+ subAgentId;
15
+ constructor(tenantId, artifactParserOptions) {
16
+ this.subAgentId = artifactParserOptions?.subAgentId;
17
+ if (artifactParserOptions?.streamRequestId) {
18
+ const sessionParser = agentSessionManager.getArtifactParser(artifactParserOptions.streamRequestId);
19
+ if (sessionParser) {
20
+ this.artifactParser = sessionParser;
21
+ return;
22
+ }
23
+ }
24
+ let sharedArtifactService = null;
25
+ if (artifactParserOptions?.streamRequestId && typeof agentSessionManager.getArtifactService === "function") try {
26
+ sharedArtifactService = agentSessionManager.getArtifactService(artifactParserOptions.streamRequestId);
27
+ } catch (_error) {}
28
+ this.artifactParser = new ArtifactParser(tenantId, {
29
+ ...artifactParserOptions,
30
+ artifactService: sharedArtifactService
31
+ });
32
+ }
33
+ /**
34
+ * Process structured object response and replace artifact markers with actual artifacts
35
+ */
36
+ async formatObjectResponse(responseObject, contextId) {
37
+ return tracer.startActiveSpan("response.format_object_response", async (span) => {
38
+ try {
39
+ const artifactMap = await this.artifactParser.getContextArtifacts(contextId);
40
+ span.setAttributes({
41
+ "response.type": "object",
42
+ "response.availableArtifacts": artifactMap.size
43
+ });
44
+ const parts = await this.artifactParser.parseObject(responseObject, artifactMap, this.subAgentId);
45
+ const uniqueArtifacts = this.countUniqueArtifacts(parts);
46
+ span.setAttributes({
47
+ "response.dataPartsCount": parts.length,
48
+ "response.artifactsCount": uniqueArtifacts,
49
+ "response.totalPartsCount": parts.length
50
+ });
51
+ this.logArtifacts(span, parts);
52
+ span.addEvent("response.parts.final_output", { final_parts_array: JSON.stringify(parts, null, 2) });
53
+ return { parts };
54
+ } catch (error) {
55
+ setSpanWithError(span, error instanceof Error ? error : new Error(String(error)));
56
+ logger.error({
57
+ error,
58
+ responseObject
59
+ }, "Error formatting object response");
60
+ return { parts: [{
61
+ kind: "data",
62
+ data: responseObject
63
+ }] };
64
+ } finally {
65
+ span.end();
66
+ }
67
+ });
68
+ }
69
+ /**
70
+ * Process agent response and convert artifact markers to data parts
71
+ */
72
+ async formatResponse(responseText, contextId) {
73
+ return tracer.startActiveSpan("response.format_response", async (span) => {
74
+ try {
75
+ const hasMarkers = this.artifactParser.hasArtifactMarkers(responseText);
76
+ span.setAttributes({
77
+ "response.hasArtifactMarkers": hasMarkers,
78
+ "response.contextId": contextId,
79
+ "response.textLength": responseText.length
80
+ });
81
+ if (!this.artifactParser.hasArtifactMarkers(responseText)) {
82
+ span.setAttributes({ "response.result": "no_markers_found" });
83
+ return { parts: [{
84
+ kind: "text",
85
+ text: responseText
86
+ }] };
87
+ }
88
+ const artifactMap = await this.artifactParser.getContextArtifacts(contextId);
89
+ span.setAttributes({
90
+ "response.type": "text",
91
+ "response.availableArtifacts": artifactMap.size
92
+ });
93
+ const parts = await this.artifactParser.parseText(responseText, artifactMap, this.subAgentId);
94
+ if (parts.length === 1 && parts[0].kind === "text") return { text: parts[0].text };
95
+ const textParts = parts.filter((p) => p.kind === "text").length;
96
+ const dataParts = parts.filter((p) => p.kind === "data").length;
97
+ const uniqueArtifacts = this.countUniqueArtifacts(parts);
98
+ span.setAttributes({
99
+ "response.textPartsCount": textParts,
100
+ "response.dataPartsCount": dataParts,
101
+ "response.artifactsCount": uniqueArtifacts,
102
+ "response.totalPartsCount": parts.length
103
+ });
104
+ this.logArtifacts(span, parts);
105
+ span.addEvent("response.parts.final_output", { final_parts_array: JSON.stringify(parts, null, 2) });
106
+ return { parts };
107
+ } catch (error) {
108
+ setSpanWithError(span, error instanceof Error ? error : new Error(String(error)));
109
+ logger.error({
110
+ error,
111
+ responseText
112
+ }, "Error formatting response");
113
+ return { text: responseText };
114
+ } finally {
115
+ span.end();
116
+ }
117
+ });
118
+ }
119
+ /**
120
+ * Count unique artifacts in parts array
121
+ */
122
+ countUniqueArtifacts(parts) {
123
+ return new Set(parts.filter((p) => p.kind === "data").map((p) => {
124
+ const data = p.data;
125
+ if (data?.artifactId && data?.taskId) return `${data.artifactId}:${data.taskId}`;
126
+ return null;
127
+ }).filter((key) => key !== null)).size;
128
+ }
129
+ /**
130
+ * Log artifacts found in parts to span
131
+ */
132
+ logArtifacts(span, parts) {
133
+ const artifacts = parts.filter((p) => p.kind === "data").map((p) => {
134
+ const data = p.data;
135
+ return {
136
+ artifactId: data?.artifactId,
137
+ name: data?.name,
138
+ taskId: data?.taskId
139
+ };
140
+ }).filter((art) => art.artifactId && art.taskId);
141
+ if (artifacts.length > 0) {
142
+ const uniqueArtifactsList = Array.from(new Map(artifacts.map((art) => [`${art.artifactId}:${art.taskId}`, art])).values());
143
+ span.addEvent("artifacts.found", {
144
+ "artifacts.count": uniqueArtifactsList.length,
145
+ "artifacts.list": JSON.stringify(uniqueArtifactsList)
146
+ });
147
+ }
148
+ }
149
+ };
150
+
151
+ //#endregion
152
+ export { ResponseFormatter };
@@ -0,0 +1,38 @@
1
+ import { SandboxConfig } from "../types/execution-context.js";
2
+
3
+ //#region src/tools/NativeSandboxExecutor.d.ts
4
+
5
+ interface FunctionToolConfig {
6
+ name?: string;
7
+ description: string;
8
+ inputSchema: Record<string, unknown>;
9
+ executeCode: string;
10
+ dependencies: Record<string, string>;
11
+ sandboxConfig?: SandboxConfig;
12
+ }
13
+ declare class NativeSandboxExecutor {
14
+ private tempDir;
15
+ private sandboxPool;
16
+ private static instance;
17
+ private executionSemaphores;
18
+ constructor();
19
+ static getInstance(): NativeSandboxExecutor;
20
+ private getSemaphore;
21
+ getExecutionStats(): Record<string, {
22
+ availablePermits: number;
23
+ queueLength: number;
24
+ }>;
25
+ private ensureTempDir;
26
+ private generateDependencyHash;
27
+ private getCachedSandbox;
28
+ private addToPool;
29
+ private cleanupSandbox;
30
+ private startPoolCleanup;
31
+ private detectModuleType;
32
+ executeFunctionTool(toolId: string, args: any, config: FunctionToolConfig): Promise<any>;
33
+ private executeInSandbox_Internal;
34
+ private installDependencies;
35
+ private executeInSandbox;
36
+ }
37
+ //#endregion
38
+ export { FunctionToolConfig, NativeSandboxExecutor };
@@ -0,0 +1,432 @@
1
+ import { getLogger } from "../logger.js";
2
+ import { FUNCTION_TOOL_EXECUTION_TIMEOUT_MS_DEFAULT, FUNCTION_TOOL_SANDBOX_CLEANUP_INTERVAL_MS, FUNCTION_TOOL_SANDBOX_MAX_OUTPUT_SIZE_BYTES, FUNCTION_TOOL_SANDBOX_MAX_USE_COUNT, FUNCTION_TOOL_SANDBOX_POOL_TTL_MS, FUNCTION_TOOL_SANDBOX_QUEUE_WAIT_TIMEOUT_MS } from "../constants/execution-limits/index.js";
3
+ import { createExecutionWrapper, parseExecutionResult } from "./sandbox-utils.js";
4
+ import { spawn } from "node:child_process";
5
+ import { createHash } from "node:crypto";
6
+ import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
7
+ import { tmpdir } from "node:os";
8
+ import { join } from "node:path";
9
+
10
+ //#region src/tools/NativeSandboxExecutor.ts
11
+ /**
12
+ * NativeSandboxExecutor - Function Tool Execution Engine
13
+ * ========================================================
14
+ *
15
+ * Executes user-defined function tools in isolated sandboxes using native Node.js processes.
16
+ * The main challenge here is that we can't just eval() user code - that's a security nightmare.
17
+ * Instead, we spin up separate Node.js processes with their own dependency trees.
18
+ *
19
+ * The tricky part is making this fast. Installing deps every time would be brutal
20
+ * (2-5s per execution), so we cache sandboxes based on their dependency fingerprint.
21
+ *
22
+ * How it works:
23
+ *
24
+ * 1. User calls a function tool
25
+ * 2. We hash the dependencies (e.g., "axios@1.6.0,lodash@4.17.21")
26
+ * 3. Check if we already have a sandbox with those deps installed
27
+ * 4. If yes: reuse it. If no: create new one, install deps, cache it
28
+ * 5. Write the user's function code to a temp file
29
+ * 6. Execute it in the sandboxed process with resource limits
30
+ * 7. Return the result
31
+ *
32
+ * Sandbox lifecycle:
33
+ * - Created when first needed for a dependency set
34
+ * - Reused up to 50 times or 5 minutes, whichever comes first
35
+ * - Automatically cleaned up when expired
36
+ * - Failed sandboxes are immediately destroyed
37
+ *
38
+ * Security stuff:
39
+ * - Each execution runs in its own process (not just a function call)
40
+ * - Output limited to 1MB to prevent memory bombs
41
+ * - Timeouts with graceful SIGTERM, then SIGKILL if needed
42
+ * - Runs as non-root when possible
43
+ * - Uses OS temp directory so it gets cleaned up automatically
44
+ *
45
+ * Performance:
46
+ * - Cold start: ~100-500ms (vs 2-5s without caching)
47
+ * - Hot path: ~50-100ms (just execution, no install)
48
+ * - Memory bounded by pool size limits
49
+ *
50
+ * Deployment notes:
51
+ * - Uses /tmp on Linux/macOS, %TEMP% on Windows
52
+ * - Works in Docker, Kubernetes, serverless (Vercel, Lambda)
53
+ * - No files left in project directory (no git pollution)
54
+ *
55
+ * The singleton pattern here is important - we need one shared pool
56
+ * across all tool executions, otherwise caching doesn't work.
57
+ */
58
+ const logger = getLogger("native-sandbox-executor");
59
+ /**
60
+ * Semaphore for limiting concurrent executions based on vCPU allocation
61
+ */
62
+ var ExecutionSemaphore = class {
63
+ permits;
64
+ waitQueue = [];
65
+ maxWaitTime;
66
+ constructor(permits, maxWaitTimeMs = FUNCTION_TOOL_SANDBOX_QUEUE_WAIT_TIMEOUT_MS) {
67
+ this.permits = Math.max(1, permits);
68
+ this.maxWaitTime = maxWaitTimeMs;
69
+ }
70
+ async acquire(fn) {
71
+ await new Promise((resolve, reject) => {
72
+ if (this.permits > 0) {
73
+ this.permits--;
74
+ resolve();
75
+ return;
76
+ }
77
+ const timeoutId = setTimeout(() => {
78
+ const index = this.waitQueue.findIndex((item) => item.resolve === resolve);
79
+ if (index !== -1) {
80
+ this.waitQueue.splice(index, 1);
81
+ reject(/* @__PURE__ */ new Error(`Function execution queue timeout after ${this.maxWaitTime}ms. Too many concurrent executions.`));
82
+ }
83
+ }, this.maxWaitTime);
84
+ this.waitQueue.push({
85
+ resolve: () => {
86
+ clearTimeout(timeoutId);
87
+ this.permits--;
88
+ resolve();
89
+ },
90
+ reject
91
+ });
92
+ });
93
+ try {
94
+ return await fn();
95
+ } finally {
96
+ this.permits++;
97
+ const next = this.waitQueue.shift();
98
+ if (next) next.resolve();
99
+ }
100
+ }
101
+ getAvailablePermits() {
102
+ return this.permits;
103
+ }
104
+ getQueueLength() {
105
+ return this.waitQueue.length;
106
+ }
107
+ };
108
+ var NativeSandboxExecutor = class NativeSandboxExecutor {
109
+ tempDir;
110
+ sandboxPool = {};
111
+ static instance = null;
112
+ executionSemaphores = /* @__PURE__ */ new Map();
113
+ constructor() {
114
+ this.tempDir = join(tmpdir(), "inkeep-sandboxes");
115
+ this.ensureTempDir();
116
+ this.startPoolCleanup();
117
+ }
118
+ static getInstance() {
119
+ if (!NativeSandboxExecutor.instance) NativeSandboxExecutor.instance = new NativeSandboxExecutor();
120
+ return NativeSandboxExecutor.instance;
121
+ }
122
+ getSemaphore(vcpus) {
123
+ const effectiveVcpus = Math.max(1, vcpus || 1);
124
+ if (!this.executionSemaphores.has(effectiveVcpus)) {
125
+ logger.debug({ vcpus: effectiveVcpus }, "Creating new execution semaphore");
126
+ this.executionSemaphores.set(effectiveVcpus, new ExecutionSemaphore(effectiveVcpus));
127
+ }
128
+ const semaphore = this.executionSemaphores.get(effectiveVcpus);
129
+ if (!semaphore) throw new Error(`Failed to create semaphore for ${effectiveVcpus} vCPUs`);
130
+ return semaphore;
131
+ }
132
+ getExecutionStats() {
133
+ const stats = {};
134
+ for (const [vcpus, semaphore] of this.executionSemaphores.entries()) stats[`vcpu_${vcpus}`] = {
135
+ availablePermits: semaphore.getAvailablePermits(),
136
+ queueLength: semaphore.getQueueLength()
137
+ };
138
+ return stats;
139
+ }
140
+ ensureTempDir() {
141
+ try {
142
+ mkdirSync(this.tempDir, { recursive: true });
143
+ } catch {}
144
+ }
145
+ generateDependencyHash(dependencies) {
146
+ const sortedDeps = Object.keys(dependencies).sort().map((key) => `${key}@${dependencies[key]}`).join(",");
147
+ return createHash("sha256").update(sortedDeps).digest("hex").substring(0, 16);
148
+ }
149
+ getCachedSandbox(dependencyHash) {
150
+ const poolKey = dependencyHash;
151
+ const sandbox = this.sandboxPool[poolKey];
152
+ if (sandbox && existsSync(sandbox.sandboxDir)) {
153
+ const now = Date.now();
154
+ if (now - sandbox.lastUsed < FUNCTION_TOOL_SANDBOX_POOL_TTL_MS && sandbox.useCount < FUNCTION_TOOL_SANDBOX_MAX_USE_COUNT) {
155
+ sandbox.lastUsed = now;
156
+ sandbox.useCount++;
157
+ logger.debug({
158
+ poolKey,
159
+ useCount: sandbox.useCount,
160
+ sandboxDir: sandbox.sandboxDir,
161
+ lastUsed: new Date(sandbox.lastUsed)
162
+ }, "Reusing cached sandbox");
163
+ return sandbox.sandboxDir;
164
+ }
165
+ this.cleanupSandbox(sandbox.sandboxDir);
166
+ delete this.sandboxPool[poolKey];
167
+ }
168
+ return null;
169
+ }
170
+ addToPool(dependencyHash, sandboxDir, dependencies) {
171
+ const poolKey = dependencyHash;
172
+ if (this.sandboxPool[poolKey]) this.cleanupSandbox(this.sandboxPool[poolKey].sandboxDir);
173
+ this.sandboxPool[poolKey] = {
174
+ sandboxDir,
175
+ lastUsed: Date.now(),
176
+ useCount: 1,
177
+ dependencies
178
+ };
179
+ logger.debug({
180
+ poolKey,
181
+ sandboxDir
182
+ }, "Added sandbox to pool");
183
+ }
184
+ cleanupSandbox(sandboxDir) {
185
+ try {
186
+ rmSync(sandboxDir, {
187
+ recursive: true,
188
+ force: true
189
+ });
190
+ logger.debug({ sandboxDir }, "Cleaned up sandbox");
191
+ } catch (error) {
192
+ logger.warn({
193
+ sandboxDir,
194
+ error
195
+ }, "Failed to clean up sandbox");
196
+ }
197
+ }
198
+ startPoolCleanup() {
199
+ setInterval(() => {
200
+ const now = Date.now();
201
+ const keysToDelete = [];
202
+ for (const [key, sandbox] of Object.entries(this.sandboxPool)) if (now - sandbox.lastUsed > FUNCTION_TOOL_SANDBOX_POOL_TTL_MS || sandbox.useCount >= FUNCTION_TOOL_SANDBOX_MAX_USE_COUNT) {
203
+ this.cleanupSandbox(sandbox.sandboxDir);
204
+ keysToDelete.push(key);
205
+ }
206
+ keysToDelete.forEach((key) => {
207
+ delete this.sandboxPool[key];
208
+ });
209
+ if (keysToDelete.length > 0) logger.debug({ cleanedCount: keysToDelete.length }, "Cleaned up expired sandboxes");
210
+ }, FUNCTION_TOOL_SANDBOX_CLEANUP_INTERVAL_MS);
211
+ }
212
+ detectModuleType(executeCode, configuredRuntime) {
213
+ const esmPatterns = [
214
+ /import\s+.*\s+from\s+['"]/g,
215
+ /import\s*\(/g,
216
+ /export\s+(default|const|let|var|function|class)/g,
217
+ /export\s*\{/g
218
+ ];
219
+ const cjsPatterns = [
220
+ /require\s*\(/g,
221
+ /module\.exports/g,
222
+ /exports\./g
223
+ ];
224
+ const hasEsmSyntax = esmPatterns.some((pattern) => pattern.test(executeCode));
225
+ const hasCjsSyntax = cjsPatterns.some((pattern) => pattern.test(executeCode));
226
+ if (configuredRuntime === "typescript") return hasCjsSyntax ? "cjs" : "esm";
227
+ if (hasEsmSyntax && hasCjsSyntax) {
228
+ logger.warn({ executeCode: `${executeCode.substring(0, 100)}...` }, "Both ESM and CommonJS syntax detected, defaulting to ESM");
229
+ return "esm";
230
+ }
231
+ if (hasEsmSyntax) return "esm";
232
+ if (hasCjsSyntax) return "cjs";
233
+ return "cjs";
234
+ }
235
+ async executeFunctionTool(toolId, args, config) {
236
+ const vcpus = config.sandboxConfig?.vcpus || 1;
237
+ const semaphore = this.getSemaphore(vcpus);
238
+ logger.debug({
239
+ toolId,
240
+ vcpus,
241
+ availablePermits: semaphore.getAvailablePermits(),
242
+ queueLength: semaphore.getQueueLength(),
243
+ sandboxConfig: config.sandboxConfig,
244
+ poolSize: Object.keys(this.sandboxPool).length
245
+ }, "Acquiring execution slot for function tool");
246
+ return semaphore.acquire(async () => {
247
+ return this.executeInSandbox_Internal(toolId, args, config);
248
+ });
249
+ }
250
+ async executeInSandbox_Internal(toolId, args, config) {
251
+ const dependencies = config.dependencies || {};
252
+ const dependencyHash = this.generateDependencyHash(dependencies);
253
+ logger.debug({
254
+ toolId,
255
+ dependencies,
256
+ dependencyHash,
257
+ sandboxConfig: config.sandboxConfig,
258
+ poolSize: Object.keys(this.sandboxPool).length
259
+ }, "Executing function tool");
260
+ let sandboxDir = this.getCachedSandbox(dependencyHash);
261
+ let isNewSandbox = false;
262
+ if (!sandboxDir) {
263
+ sandboxDir = join(this.tempDir, `sandbox-${dependencyHash}-${Date.now()}`);
264
+ mkdirSync(sandboxDir, { recursive: true });
265
+ isNewSandbox = true;
266
+ logger.debug({
267
+ toolId,
268
+ dependencyHash,
269
+ sandboxDir,
270
+ dependencies
271
+ }, "Creating new sandbox");
272
+ const moduleType = this.detectModuleType(config.executeCode, config.sandboxConfig?.runtime);
273
+ const packageJson = {
274
+ name: `function-tool-${toolId}`,
275
+ version: "1.0.0",
276
+ ...moduleType === "esm" && { type: "module" },
277
+ dependencies,
278
+ scripts: { start: moduleType === "esm" ? "node index.mjs" : "node index.js" }
279
+ };
280
+ writeFileSync(join(sandboxDir, "package.json"), JSON.stringify(packageJson, null, 2), "utf8");
281
+ if (Object.keys(dependencies).length > 0) await this.installDependencies(sandboxDir);
282
+ this.addToPool(dependencyHash, sandboxDir, dependencies);
283
+ }
284
+ try {
285
+ const moduleType = this.detectModuleType(config.executeCode, config.sandboxConfig?.runtime);
286
+ const executionCode = createExecutionWrapper(config.executeCode, args);
287
+ writeFileSync(join(sandboxDir, `index.${moduleType === "esm" ? "mjs" : "js"}`), executionCode, "utf8");
288
+ return await this.executeInSandbox(sandboxDir, config.sandboxConfig?.timeout || FUNCTION_TOOL_EXECUTION_TIMEOUT_MS_DEFAULT, moduleType, config.sandboxConfig);
289
+ } catch (error) {
290
+ if (isNewSandbox) {
291
+ this.cleanupSandbox(sandboxDir);
292
+ const poolKey = dependencyHash;
293
+ delete this.sandboxPool[poolKey];
294
+ }
295
+ throw error;
296
+ }
297
+ }
298
+ async installDependencies(sandboxDir) {
299
+ return new Promise((resolve, reject) => {
300
+ const npm = spawn("npm", [
301
+ "install",
302
+ "--no-audit",
303
+ "--no-fund"
304
+ ], {
305
+ cwd: sandboxDir,
306
+ stdio: "pipe",
307
+ env: {
308
+ ...process.env,
309
+ npm_config_cache: join(sandboxDir, ".npm-cache"),
310
+ npm_config_logs_dir: join(sandboxDir, ".npm-logs"),
311
+ npm_config_tmp: join(sandboxDir, ".npm-tmp"),
312
+ HOME: sandboxDir,
313
+ npm_config_update_notifier: "false",
314
+ npm_config_progress: "false",
315
+ npm_config_loglevel: "error"
316
+ }
317
+ });
318
+ let stderr = "";
319
+ npm.stdout?.on("data", () => {});
320
+ npm.stderr?.on("data", (data) => {
321
+ stderr += data.toString();
322
+ });
323
+ npm.on("close", (code) => {
324
+ if (code === 0) {
325
+ logger.debug({ sandboxDir }, "Dependencies installed successfully");
326
+ resolve();
327
+ } else {
328
+ logger.error({
329
+ sandboxDir,
330
+ code,
331
+ stderr
332
+ }, "Failed to install dependencies");
333
+ reject(/* @__PURE__ */ new Error(`npm install failed with code ${code}: ${stderr}`));
334
+ }
335
+ });
336
+ npm.on("error", (err) => {
337
+ logger.error({
338
+ sandboxDir,
339
+ error: err
340
+ }, "Failed to spawn npm install");
341
+ reject(err);
342
+ });
343
+ });
344
+ }
345
+ async executeInSandbox(sandboxDir, timeout, moduleType, _sandboxConfig) {
346
+ return new Promise((resolve, reject) => {
347
+ const fileExtension = moduleType === "esm" ? "mjs" : "js";
348
+ const spawnOptions = {
349
+ cwd: sandboxDir,
350
+ stdio: "pipe",
351
+ uid: process.getuid ? process.getuid() : void 0,
352
+ gid: process.getgid ? process.getgid() : void 0
353
+ };
354
+ const node = spawn("node", [`index.${fileExtension}`], spawnOptions);
355
+ let stdout = "";
356
+ let stderr = "";
357
+ let outputSize = 0;
358
+ node.stdout?.on("data", (data) => {
359
+ const dataStr = data.toString();
360
+ outputSize += dataStr.length;
361
+ if (outputSize > FUNCTION_TOOL_SANDBOX_MAX_OUTPUT_SIZE_BYTES) {
362
+ node.kill("SIGTERM");
363
+ reject(/* @__PURE__ */ new Error(`Output size exceeded limit of ${FUNCTION_TOOL_SANDBOX_MAX_OUTPUT_SIZE_BYTES} bytes`));
364
+ return;
365
+ }
366
+ stdout += dataStr;
367
+ });
368
+ node.stderr?.on("data", (data) => {
369
+ const dataStr = data.toString();
370
+ outputSize += dataStr.length;
371
+ if (outputSize > FUNCTION_TOOL_SANDBOX_MAX_OUTPUT_SIZE_BYTES) {
372
+ node.kill("SIGTERM");
373
+ reject(/* @__PURE__ */ new Error(`Output size exceeded limit of ${FUNCTION_TOOL_SANDBOX_MAX_OUTPUT_SIZE_BYTES} bytes`));
374
+ return;
375
+ }
376
+ stderr += dataStr;
377
+ });
378
+ const timeoutId = setTimeout(() => {
379
+ logger.warn({
380
+ sandboxDir,
381
+ timeout
382
+ }, "Function execution timed out, killing process");
383
+ node.kill("SIGTERM");
384
+ const forceKillTimeout = Math.min(Math.max(timeout / 10, 2e3), 5e3);
385
+ setTimeout(() => {
386
+ try {
387
+ node.kill("SIGKILL");
388
+ } catch {}
389
+ }, forceKillTimeout);
390
+ reject(/* @__PURE__ */ new Error(`Function execution timed out after ${timeout}ms`));
391
+ }, timeout);
392
+ node.on("close", (code, signal) => {
393
+ clearTimeout(timeoutId);
394
+ if (code === 0) try {
395
+ const result = parseExecutionResult(stdout, "function", logger);
396
+ if (typeof result === "object" && result !== null && "success" in result) {
397
+ const parsed = result;
398
+ if (parsed.success) resolve(parsed.result);
399
+ else reject(new Error(parsed.error || "Function execution failed"));
400
+ } else resolve(result);
401
+ } catch (parseError) {
402
+ logger.error({
403
+ stdout,
404
+ stderr,
405
+ parseError
406
+ }, "Failed to parse function result");
407
+ reject(/* @__PURE__ */ new Error(`Invalid function result: ${stdout}`));
408
+ }
409
+ else {
410
+ const errorMsg = signal ? `Function execution killed by signal ${signal}: ${stderr}` : `Function execution failed with code ${code}: ${stderr}`;
411
+ logger.error({
412
+ code,
413
+ signal,
414
+ stderr
415
+ }, "Function execution failed");
416
+ reject(new Error(errorMsg));
417
+ }
418
+ });
419
+ node.on("error", (error) => {
420
+ clearTimeout(timeoutId);
421
+ logger.error({
422
+ sandboxDir,
423
+ error
424
+ }, "Failed to spawn node process");
425
+ reject(error);
426
+ });
427
+ });
428
+ }
429
+ };
430
+
431
+ //#endregion
432
+ export { NativeSandboxExecutor };
@@ -0,0 +1,36 @@
1
+ import { FunctionToolConfig } from "./NativeSandboxExecutor.js";
2
+
3
+ //#region src/tools/SandboxExecutorFactory.d.ts
4
+
5
+ /**
6
+ * Factory for creating and managing sandbox executors
7
+ * Routes execution to the appropriate sandbox provider (native or Vercel)
8
+ */
9
+ declare class SandboxExecutorFactory {
10
+ private static instance;
11
+ private nativeExecutor;
12
+ private vercelExecutors;
13
+ private constructor();
14
+ /**
15
+ * Get singleton instance of SandboxExecutorFactory
16
+ */
17
+ static getInstance(): SandboxExecutorFactory;
18
+ /**
19
+ * Execute a function tool using the appropriate sandbox provider
20
+ */
21
+ executeFunctionTool(functionId: string, args: Record<string, unknown>, config: FunctionToolConfig): Promise<unknown>;
22
+ /**
23
+ * Execute in native sandbox
24
+ */
25
+ private executeInNativeSandbox;
26
+ /**
27
+ * Execute in Vercel sandbox
28
+ */
29
+ private executeInVercelSandbox;
30
+ /**
31
+ * Clean up all sandbox executors
32
+ */
33
+ cleanup(): Promise<void>;
34
+ }
35
+ //#endregion
36
+ export { SandboxExecutorFactory };