@mondaydotcomorg/atp-server 0.17.14

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 (307) hide show
  1. package/README.md +489 -0
  2. package/dist/aggregator/index.d.ts +59 -0
  3. package/dist/aggregator/index.d.ts.map +1 -0
  4. package/dist/aggregator/index.js +171 -0
  5. package/dist/aggregator/index.js.map +1 -0
  6. package/dist/callback/index.d.ts +98 -0
  7. package/dist/callback/index.d.ts.map +1 -0
  8. package/dist/callback/index.js +136 -0
  9. package/dist/callback/index.js.map +1 -0
  10. package/dist/client-sessions.d.ts +82 -0
  11. package/dist/client-sessions.d.ts.map +1 -0
  12. package/dist/client-sessions.js +174 -0
  13. package/dist/client-sessions.js.map +1 -0
  14. package/dist/controllers/definitions.controller.d.ts +4 -0
  15. package/dist/controllers/definitions.controller.d.ts.map +1 -0
  16. package/dist/controllers/definitions.controller.js +11 -0
  17. package/dist/controllers/definitions.controller.js.map +1 -0
  18. package/dist/controllers/execute.controller.d.ts +18 -0
  19. package/dist/controllers/execute.controller.d.ts.map +1 -0
  20. package/dist/controllers/execute.controller.js +122 -0
  21. package/dist/controllers/execute.controller.js.map +1 -0
  22. package/dist/controllers/info.controller.d.ts +3 -0
  23. package/dist/controllers/info.controller.d.ts.map +1 -0
  24. package/dist/controllers/info.controller.js +13 -0
  25. package/dist/controllers/info.controller.js.map +1 -0
  26. package/dist/controllers/resume.controller.d.ts +11 -0
  27. package/dist/controllers/resume.controller.d.ts.map +1 -0
  28. package/dist/controllers/resume.controller.js +61 -0
  29. package/dist/controllers/resume.controller.js.map +1 -0
  30. package/dist/controllers/search.controller.d.ts +4 -0
  31. package/dist/controllers/search.controller.d.ts.map +1 -0
  32. package/dist/controllers/search.controller.js +7 -0
  33. package/dist/controllers/search.controller.js.map +1 -0
  34. package/dist/controllers/stream.controller.d.ts +19 -0
  35. package/dist/controllers/stream.controller.d.ts.map +1 -0
  36. package/dist/controllers/stream.controller.js +141 -0
  37. package/dist/controllers/stream.controller.js.map +1 -0
  38. package/dist/core/config.d.ts +161 -0
  39. package/dist/core/config.d.ts.map +1 -0
  40. package/dist/core/config.js +7 -0
  41. package/dist/core/config.js.map +1 -0
  42. package/dist/core/http.d.ts +4 -0
  43. package/dist/core/http.d.ts.map +1 -0
  44. package/dist/core/http.js +17 -0
  45. package/dist/core/http.js.map +1 -0
  46. package/dist/create-server.d.ts +120 -0
  47. package/dist/create-server.d.ts.map +1 -0
  48. package/dist/create-server.js +423 -0
  49. package/dist/create-server.js.map +1 -0
  50. package/dist/execution-state/index.d.ts +95 -0
  51. package/dist/execution-state/index.d.ts.map +1 -0
  52. package/dist/execution-state/index.js +128 -0
  53. package/dist/execution-state/index.js.map +1 -0
  54. package/dist/executor/ast-provenance-bridge.d.ts +12 -0
  55. package/dist/executor/ast-provenance-bridge.d.ts.map +1 -0
  56. package/dist/executor/ast-provenance-bridge.js +66 -0
  57. package/dist/executor/ast-provenance-bridge.js.map +1 -0
  58. package/dist/executor/ast-tracking-runtime.d.ts +7 -0
  59. package/dist/executor/ast-tracking-runtime.d.ts.map +1 -0
  60. package/dist/executor/ast-tracking-runtime.js +559 -0
  61. package/dist/executor/ast-tracking-runtime.js.map +1 -0
  62. package/dist/executor/bootstrap-generated.d.ts +32 -0
  63. package/dist/executor/bootstrap-generated.d.ts.map +1 -0
  64. package/dist/executor/bootstrap-generated.js +90 -0
  65. package/dist/executor/bootstrap-generated.js.map +1 -0
  66. package/dist/executor/compiler-config.d.ts +32 -0
  67. package/dist/executor/compiler-config.d.ts.map +1 -0
  68. package/dist/executor/compiler-config.js +99 -0
  69. package/dist/executor/compiler-config.js.map +1 -0
  70. package/dist/executor/constants.d.ts +4 -0
  71. package/dist/executor/constants.d.ts.map +1 -0
  72. package/dist/executor/constants.js +4 -0
  73. package/dist/executor/constants.js.map +1 -0
  74. package/dist/executor/error-handler.d.ts +9 -0
  75. package/dist/executor/error-handler.d.ts.map +1 -0
  76. package/dist/executor/error-handler.js +95 -0
  77. package/dist/executor/error-handler.js.map +1 -0
  78. package/dist/executor/execution-error-handler.d.ts +7 -0
  79. package/dist/executor/execution-error-handler.d.ts.map +1 -0
  80. package/dist/executor/execution-error-handler.js +136 -0
  81. package/dist/executor/execution-error-handler.js.map +1 -0
  82. package/dist/executor/executor.d.ts +20 -0
  83. package/dist/executor/executor.d.ts.map +1 -0
  84. package/dist/executor/executor.js +452 -0
  85. package/dist/executor/executor.js.map +1 -0
  86. package/dist/executor/index.d.ts +4 -0
  87. package/dist/executor/index.d.ts.map +1 -0
  88. package/dist/executor/index.js +3 -0
  89. package/dist/executor/index.js.map +1 -0
  90. package/dist/executor/resume-handler.d.ts +9 -0
  91. package/dist/executor/resume-handler.d.ts.map +1 -0
  92. package/dist/executor/resume-handler.js +22 -0
  93. package/dist/executor/resume-handler.js.map +1 -0
  94. package/dist/executor/sandbox-builder.d.ts +29 -0
  95. package/dist/executor/sandbox-builder.d.ts.map +1 -0
  96. package/dist/executor/sandbox-builder.js +538 -0
  97. package/dist/executor/sandbox-builder.js.map +1 -0
  98. package/dist/executor/sandbox-injector.d.ts +7 -0
  99. package/dist/executor/sandbox-injector.d.ts.map +1 -0
  100. package/dist/executor/sandbox-injector.js +293 -0
  101. package/dist/executor/sandbox-injector.js.map +1 -0
  102. package/dist/executor/types.d.ts +21 -0
  103. package/dist/executor/types.d.ts.map +1 -0
  104. package/dist/executor/types.js +2 -0
  105. package/dist/executor/types.js.map +1 -0
  106. package/dist/explorer/index.d.ts +69 -0
  107. package/dist/explorer/index.d.ts.map +1 -0
  108. package/dist/explorer/index.js +228 -0
  109. package/dist/explorer/index.js.map +1 -0
  110. package/dist/handlers/definitions.handler.d.ts +3 -0
  111. package/dist/handlers/definitions.handler.d.ts.map +1 -0
  112. package/dist/handlers/definitions.handler.js +11 -0
  113. package/dist/handlers/definitions.handler.js.map +1 -0
  114. package/dist/handlers/execute.handler.d.ts +7 -0
  115. package/dist/handlers/execute.handler.d.ts.map +1 -0
  116. package/dist/handlers/execute.handler.js +225 -0
  117. package/dist/handlers/execute.handler.js.map +1 -0
  118. package/dist/handlers/explorer.handler.d.ts +4 -0
  119. package/dist/handlers/explorer.handler.d.ts.map +1 -0
  120. package/dist/handlers/explorer.handler.js +10 -0
  121. package/dist/handlers/explorer.handler.js.map +1 -0
  122. package/dist/handlers/init.handler.d.ts +5 -0
  123. package/dist/handlers/init.handler.d.ts.map +1 -0
  124. package/dist/handlers/init.handler.js +41 -0
  125. package/dist/handlers/init.handler.js.map +1 -0
  126. package/dist/handlers/resume.handler.d.ts +6 -0
  127. package/dist/handlers/resume.handler.d.ts.map +1 -0
  128. package/dist/handlers/resume.handler.js +256 -0
  129. package/dist/handlers/resume.handler.js.map +1 -0
  130. package/dist/handlers/search.handler.d.ts +5 -0
  131. package/dist/handlers/search.handler.d.ts.map +1 -0
  132. package/dist/handlers/search.handler.js +11 -0
  133. package/dist/handlers/search.handler.js.map +1 -0
  134. package/dist/http/request-handler.d.ts +15 -0
  135. package/dist/http/request-handler.d.ts.map +1 -0
  136. package/dist/http/request-handler.js +94 -0
  137. package/dist/http/request-handler.js.map +1 -0
  138. package/dist/http/router.d.ts +4 -0
  139. package/dist/http/router.d.ts.map +1 -0
  140. package/dist/http/router.js +32 -0
  141. package/dist/http/router.js.map +1 -0
  142. package/dist/index.d.ts +10 -0
  143. package/dist/index.d.ts.map +1 -0
  144. package/dist/index.js +8 -0
  145. package/dist/index.js.map +1 -0
  146. package/dist/instrumentation/index.d.ts +5 -0
  147. package/dist/instrumentation/index.d.ts.map +1 -0
  148. package/dist/instrumentation/index.js +5 -0
  149. package/dist/instrumentation/index.js.map +1 -0
  150. package/dist/instrumentation/serializer.d.ts +61 -0
  151. package/dist/instrumentation/serializer.d.ts.map +1 -0
  152. package/dist/instrumentation/serializer.js +334 -0
  153. package/dist/instrumentation/serializer.js.map +1 -0
  154. package/dist/instrumentation/state-manager.d.ts +61 -0
  155. package/dist/instrumentation/state-manager.d.ts.map +1 -0
  156. package/dist/instrumentation/state-manager.js +205 -0
  157. package/dist/instrumentation/state-manager.js.map +1 -0
  158. package/dist/instrumentation/transformer.d.ts +9 -0
  159. package/dist/instrumentation/transformer.d.ts.map +1 -0
  160. package/dist/instrumentation/transformer.js +70 -0
  161. package/dist/instrumentation/transformer.js.map +1 -0
  162. package/dist/instrumentation/types.d.ts +59 -0
  163. package/dist/instrumentation/types.d.ts.map +1 -0
  164. package/dist/instrumentation/types.js +5 -0
  165. package/dist/instrumentation/types.js.map +1 -0
  166. package/dist/middleware/audit.d.ts +18 -0
  167. package/dist/middleware/audit.d.ts.map +1 -0
  168. package/dist/middleware/audit.js +76 -0
  169. package/dist/middleware/audit.js.map +1 -0
  170. package/dist/openapi/index.d.ts +133 -0
  171. package/dist/openapi/index.d.ts.map +1 -0
  172. package/dist/openapi/index.js +235 -0
  173. package/dist/openapi/index.js.map +1 -0
  174. package/dist/openapi-loader.d.ts +87 -0
  175. package/dist/openapi-loader.d.ts.map +1 -0
  176. package/dist/openapi-loader.js +491 -0
  177. package/dist/openapi-loader.js.map +1 -0
  178. package/dist/routes/index.d.ts +21 -0
  179. package/dist/routes/index.d.ts.map +1 -0
  180. package/dist/routes/index.js +47 -0
  181. package/dist/routes/index.js.map +1 -0
  182. package/dist/search/index.d.ts +48 -0
  183. package/dist/search/index.d.ts.map +1 -0
  184. package/dist/search/index.js +156 -0
  185. package/dist/search/index.js.map +1 -0
  186. package/dist/security/index.d.ts +2 -0
  187. package/dist/security/index.d.ts.map +1 -0
  188. package/dist/security/index.js +2 -0
  189. package/dist/security/index.js.map +1 -0
  190. package/dist/shutdown.d.ts +19 -0
  191. package/dist/shutdown.d.ts.map +1 -0
  192. package/dist/shutdown.js +87 -0
  193. package/dist/shutdown.js.map +1 -0
  194. package/dist/utils/banner.d.ts +12 -0
  195. package/dist/utils/banner.d.ts.map +1 -0
  196. package/dist/utils/banner.js +18 -0
  197. package/dist/utils/banner.js.map +1 -0
  198. package/dist/utils/context.d.ts +16 -0
  199. package/dist/utils/context.d.ts.map +1 -0
  200. package/dist/utils/context.js +44 -0
  201. package/dist/utils/context.js.map +1 -0
  202. package/dist/utils/error.d.ts +8 -0
  203. package/dist/utils/error.d.ts.map +1 -0
  204. package/dist/utils/error.js +17 -0
  205. package/dist/utils/error.js.map +1 -0
  206. package/dist/utils/hint-based-instrumentation.d.ts +14 -0
  207. package/dist/utils/hint-based-instrumentation.d.ts.map +1 -0
  208. package/dist/utils/hint-based-instrumentation.js +84 -0
  209. package/dist/utils/hint-based-instrumentation.js.map +1 -0
  210. package/dist/utils/index.d.ts +8 -0
  211. package/dist/utils/index.d.ts.map +1 -0
  212. package/dist/utils/index.js +8 -0
  213. package/dist/utils/index.js.map +1 -0
  214. package/dist/utils/info.d.ts +20 -0
  215. package/dist/utils/info.d.ts.map +1 -0
  216. package/dist/utils/info.js +15 -0
  217. package/dist/utils/info.js.map +1 -0
  218. package/dist/utils/provenance-reattachment.d.ts +32 -0
  219. package/dist/utils/provenance-reattachment.d.ts.map +1 -0
  220. package/dist/utils/provenance-reattachment.js +115 -0
  221. package/dist/utils/provenance-reattachment.js.map +1 -0
  222. package/dist/utils/request.d.ts +21 -0
  223. package/dist/utils/request.d.ts.map +1 -0
  224. package/dist/utils/request.js +44 -0
  225. package/dist/utils/request.js.map +1 -0
  226. package/dist/utils/response.d.ts +30 -0
  227. package/dist/utils/response.d.ts.map +1 -0
  228. package/dist/utils/response.js +53 -0
  229. package/dist/utils/response.js.map +1 -0
  230. package/dist/utils/runtime-types.d.ts +6 -0
  231. package/dist/utils/runtime-types.d.ts.map +1 -0
  232. package/dist/utils/runtime-types.js +14 -0
  233. package/dist/utils/runtime-types.js.map +1 -0
  234. package/dist/utils/schema.d.ts +9 -0
  235. package/dist/utils/schema.d.ts.map +1 -0
  236. package/dist/utils/schema.js +13 -0
  237. package/dist/utils/schema.js.map +1 -0
  238. package/dist/utils/token-emitter.d.ts +21 -0
  239. package/dist/utils/token-emitter.d.ts.map +1 -0
  240. package/dist/utils/token-emitter.js +129 -0
  241. package/dist/utils/token-emitter.js.map +1 -0
  242. package/dist/validator/index.d.ts +36 -0
  243. package/dist/validator/index.d.ts.map +1 -0
  244. package/dist/validator/index.js +224 -0
  245. package/dist/validator/index.js.map +1 -0
  246. package/package.json +68 -0
  247. package/src/aggregator/index.ts +207 -0
  248. package/src/callback/index.ts +191 -0
  249. package/src/client-sessions.ts +234 -0
  250. package/src/controllers/definitions.controller.ts +19 -0
  251. package/src/controllers/execute.controller.ts +166 -0
  252. package/src/controllers/info.controller.ts +14 -0
  253. package/src/controllers/resume.controller.ts +92 -0
  254. package/src/controllers/search.controller.ts +16 -0
  255. package/src/controllers/stream.controller.ts +190 -0
  256. package/src/core/config.ts +180 -0
  257. package/src/core/http.ts +21 -0
  258. package/src/create-server.ts +536 -0
  259. package/src/execution-state/index.ts +204 -0
  260. package/src/executor/ast-provenance-bridge.ts +80 -0
  261. package/src/executor/ast-tracking-runtime.ts +558 -0
  262. package/src/executor/bootstrap-generated.ts +90 -0
  263. package/src/executor/compiler-config.ts +146 -0
  264. package/src/executor/constants.ts +5 -0
  265. package/src/executor/error-handler.ts +118 -0
  266. package/src/executor/execution-error-handler.ts +178 -0
  267. package/src/executor/executor.ts +631 -0
  268. package/src/executor/index.ts +3 -0
  269. package/src/executor/resume-handler.ts +39 -0
  270. package/src/executor/sandbox-builder.ts +684 -0
  271. package/src/executor/sandbox-injector.ts +345 -0
  272. package/src/executor/types.ts +22 -0
  273. package/src/explorer/index.ts +297 -0
  274. package/src/handlers/definitions.handler.ts +13 -0
  275. package/src/handlers/execute.handler.ts +286 -0
  276. package/src/handlers/explorer.handler.ts +18 -0
  277. package/src/handlers/init.handler.ts +53 -0
  278. package/src/handlers/resume.handler.ts +316 -0
  279. package/src/handlers/search.handler.ts +32 -0
  280. package/src/http/request-handler.ts +117 -0
  281. package/src/http/router.ts +29 -0
  282. package/src/index.ts +60 -0
  283. package/src/instrumentation/index.ts +4 -0
  284. package/src/instrumentation/serializer.ts +421 -0
  285. package/src/instrumentation/state-manager.ts +237 -0
  286. package/src/instrumentation/transformer.ts +84 -0
  287. package/src/instrumentation/types.ts +76 -0
  288. package/src/middleware/audit.ts +101 -0
  289. package/src/openapi/index.ts +378 -0
  290. package/src/openapi-loader.ts +744 -0
  291. package/src/routes/index.ts +93 -0
  292. package/src/search/index.ts +216 -0
  293. package/src/security/index.ts +1 -0
  294. package/src/shutdown.ts +108 -0
  295. package/src/utils/banner.ts +25 -0
  296. package/src/utils/context.ts +58 -0
  297. package/src/utils/error.ts +25 -0
  298. package/src/utils/hint-based-instrumentation.ts +99 -0
  299. package/src/utils/index.ts +15 -0
  300. package/src/utils/info.ts +31 -0
  301. package/src/utils/provenance-reattachment.ts +144 -0
  302. package/src/utils/request.ts +53 -0
  303. package/src/utils/response.ts +69 -0
  304. package/src/utils/runtime-types.ts +14 -0
  305. package/src/utils/schema.ts +18 -0
  306. package/src/utils/token-emitter.ts +182 -0
  307. package/src/validator/index.ts +253 -0
@@ -0,0 +1,190 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { ExecutionConfig } from '@mondaydotcomorg/atp-protocol';
3
+ import {
4
+ ExecutionErrorCode,
5
+ validateExecutionConfig,
6
+ sanitizeInput,
7
+ MAX_CODE_SIZE,
8
+ } from '@mondaydotcomorg/atp-protocol';
9
+ import type { CodeValidator } from '../validator/index.js';
10
+ import type { SandboxExecutor } from '../executor/index.js';
11
+ import type { ExecutionStateManager } from '../execution-state/index.js';
12
+ import { nanoid } from 'nanoid';
13
+ import type { log } from '@mondaydotcomorg/atp-runtime';
14
+
15
+ interface StreamContext {
16
+ validator: CodeValidator;
17
+ executor: SandboxExecutor;
18
+ stateManager?: ExecutionStateManager;
19
+ auditConfig?: { enabled: boolean };
20
+ defaultTimeout: number;
21
+ defaultMemoryLimit: number;
22
+ defaultLLMCallLimit: number;
23
+ }
24
+
25
+ export async function handleExecuteStream(
26
+ req: IncomingMessage,
27
+ res: ServerResponse,
28
+ context: StreamContext,
29
+ body: string,
30
+ clientId: string | undefined,
31
+ logger: ReturnType<typeof log.child>
32
+ ): Promise<void> {
33
+ let request: { code: string; config?: Partial<ExecutionConfig> };
34
+
35
+ try {
36
+ request = JSON.parse(body);
37
+ } catch (error) {
38
+ logger.warn('Invalid request body', { error });
39
+ res.writeHead(400, { 'Content-Type': 'application/json' });
40
+ res.end(
41
+ JSON.stringify({
42
+ error: 'Invalid request body',
43
+ message: error instanceof Error ? error.message : 'Failed to parse request',
44
+ })
45
+ );
46
+ return;
47
+ }
48
+
49
+ request.code = sanitizeInput(request.code, MAX_CODE_SIZE);
50
+
51
+ logger.info('Streaming code execution request', {
52
+ codeLength: request.code.length,
53
+ });
54
+
55
+ res.writeHead(200, {
56
+ 'Content-Type': 'text/event-stream',
57
+ 'Cache-Control': 'no-cache',
58
+ Connection: 'keep-alive',
59
+ });
60
+
61
+ const sendEvent = (event: string, data: unknown) => {
62
+ res.write(`event: ${event}\n`);
63
+ res.write(`data: ${JSON.stringify(data)}\n\n`);
64
+ };
65
+
66
+ try {
67
+ if (request.config) {
68
+ try {
69
+ validateExecutionConfig(request.config);
70
+ } catch (error) {
71
+ logger.warn('Invalid execution config', { error });
72
+ sendEvent('error', {
73
+ message: error instanceof Error ? error.message : 'Invalid configuration',
74
+ });
75
+ res.end();
76
+ return;
77
+ }
78
+ }
79
+
80
+ const executionConfig: ExecutionConfig = {
81
+ timeout: request.config?.timeout ?? context.defaultTimeout,
82
+ maxMemory: request.config?.maxMemory ?? context.defaultMemoryLimit,
83
+ maxLLMCalls: request.config?.maxLLMCalls ?? context.defaultLLMCallLimit,
84
+ allowedAPIs: request.config?.allowedAPIs ?? [],
85
+ allowLLMCalls: request.config?.allowLLMCalls ?? true,
86
+ progressCallback: (message: string, fraction: number) => {
87
+ sendEvent('progress', { message, fraction });
88
+ },
89
+ clientServices: request.config?.clientServices,
90
+ provenanceMode: request.config?.provenanceMode,
91
+ securityPolicies: request.config?.securityPolicies,
92
+ provenanceHints: request.config?.provenanceHints,
93
+ };
94
+
95
+ logger.info('Validating code for streaming execution', {
96
+ codeLength: request.code.length,
97
+ timeout: executionConfig.timeout,
98
+ clientServices: executionConfig.clientServices,
99
+ });
100
+
101
+ const validationResult = await context.validator.validate(request.code, executionConfig);
102
+
103
+ if (!validationResult.valid) {
104
+ logger.warn('Code validation failed', {
105
+ errors: validationResult.errors?.length,
106
+ securityIssues: validationResult.securityIssues?.length,
107
+ });
108
+
109
+ const hasSecurityIssues =
110
+ validationResult.securityIssues && validationResult.securityIssues.length > 0;
111
+
112
+ sendEvent('result', {
113
+ executionId: nanoid(),
114
+ status: hasSecurityIssues ? 'security_violation' : 'validation_failed',
115
+ error: {
116
+ message: 'Code validation failed',
117
+ code: hasSecurityIssues
118
+ ? ExecutionErrorCode.SECURITY_VIOLATION
119
+ : ExecutionErrorCode.VALIDATION_FAILED,
120
+ context: {
121
+ errors: validationResult.errors,
122
+ securityIssues: validationResult.securityIssues,
123
+ },
124
+ retryable: false,
125
+ suggestion: hasSecurityIssues
126
+ ? 'Remove forbidden operations and use only allowed APIs'
127
+ : 'Fix syntax errors and validation issues in your code',
128
+ },
129
+ stats: {
130
+ duration: 0,
131
+ memoryUsed: 0,
132
+ llmCallsCount: 0,
133
+ approvalCallsCount: 0,
134
+ },
135
+ });
136
+ res.end();
137
+ return;
138
+ }
139
+
140
+ sendEvent('start', { message: 'Execution started' });
141
+
142
+ logger.info('Executing code in sandbox (streaming)');
143
+ const result = await context.executor.execute(request.code, executionConfig, clientId);
144
+
145
+ logger.info('Code execution completed (streaming)', {
146
+ executionId: result.executionId,
147
+ status: result.status,
148
+ duration: result.stats.duration,
149
+ });
150
+
151
+ if (
152
+ result.status === 'paused' &&
153
+ result.needsCallback &&
154
+ result.callbackHistory &&
155
+ context.stateManager
156
+ ) {
157
+ if (!clientId) {
158
+ sendEvent('error', { message: 'Client ID required for paused executions' });
159
+ res.end();
160
+ return;
161
+ }
162
+
163
+ await context.stateManager.pause({
164
+ executionId: result.executionId,
165
+ code: request.code,
166
+ config: executionConfig,
167
+ clientId,
168
+ callbackRequest: result.needsCallback,
169
+ pausedAt: Date.now(),
170
+ callbackHistory: result.callbackHistory,
171
+ currentCallbackIndex: result.callbackHistory.length - 1,
172
+ context: {},
173
+ });
174
+
175
+ logger.info('Execution state saved (streaming)', {
176
+ executionId: result.executionId,
177
+ callbackType: result.needsCallback.type,
178
+ historyLength: result.callbackHistory.length,
179
+ });
180
+ }
181
+
182
+ sendEvent('result', result);
183
+ res.end();
184
+ } catch (error) {
185
+ const err = error as Error;
186
+ logger.error('Streaming execution error', { error: err.message });
187
+ sendEvent('error', { message: err.message, stack: err.stack });
188
+ res.end();
189
+ }
190
+ }
@@ -0,0 +1,180 @@
1
+ import type {
2
+ CacheProvider,
3
+ AuthProvider,
4
+ AuditSink,
5
+ ProvenanceMode,
6
+ SecurityPolicy,
7
+ ScopeFilteringConfig,
8
+ } from '@mondaydotcomorg/atp-protocol';
9
+
10
+ export const MB = 1024 * 1024;
11
+ export const GB = 1024 * 1024 * 1024;
12
+ export const SECOND = 1000;
13
+ export const MINUTE = 60 * 1000;
14
+ export const HOUR = 60 * 60 * 1000;
15
+ export const DAY = 24 * 60 * 60 * 1000;
16
+
17
+ /**
18
+ * Execution configuration
19
+ */
20
+ export interface ExecutionConfig {
21
+ /** Timeout in milliseconds */
22
+ timeout: number;
23
+ /** Memory limit in bytes */
24
+ memory: number;
25
+ /** Maximum LLM calls allowed */
26
+ llmCalls: number;
27
+ /** Provenance tracking mode (none|proxy|ast) */
28
+ provenanceMode?: ProvenanceMode;
29
+ /** Security policies for provenance-based protection */
30
+ securityPolicies?: SecurityPolicy[];
31
+ }
32
+
33
+ /**
34
+ * Client initialization configuration
35
+ */
36
+ export interface ClientInitConfig {
37
+ /** Token time-to-live in milliseconds */
38
+ tokenTTL: number;
39
+ /** Token rotation interval in milliseconds */
40
+ tokenRotation: number;
41
+ }
42
+
43
+ /**
44
+ * Execution state configuration
45
+ */
46
+ export interface ExecutionStateConfig {
47
+ /** TTL for paused execution state in seconds */
48
+ ttl: number;
49
+ /** Maximum allowed pause duration in seconds (default: 1 hour) */
50
+ maxPauseDuration: number;
51
+ /** Key prefix for execution state (defaults to 'atp:execution:') */
52
+ keyPrefix?: string;
53
+ }
54
+
55
+ /**
56
+ * Discovery configuration
57
+ */
58
+ export interface DiscoveryConfig {
59
+ /** Enable embeddings for semantic search */
60
+ embeddings: boolean;
61
+ /** Scope filtering configuration for search results */
62
+ scopeFiltering?: ScopeFilteringConfig;
63
+ }
64
+
65
+ /**
66
+ * OpenTelemetry configuration
67
+ */
68
+ export interface OpenTelemetryConfig {
69
+ /** Enable OpenTelemetry tracing and metrics */
70
+ enabled: boolean;
71
+ /** Service name for traces and metrics (defaults to 'agent-tool-protocol') */
72
+ serviceName?: string;
73
+ /** Service version for resource attributes */
74
+ serviceVersion?: string;
75
+ /** OTLP endpoint for traces (defaults to http://localhost:4318/v1/traces) */
76
+ traceEndpoint?: string;
77
+ /** OTLP endpoint for metrics (defaults to http://localhost:4318/v1/metrics) */
78
+ metricsEndpoint?: string;
79
+ /** Headers for OTLP exporter (e.g., for authentication) */
80
+ headers?: Record<string, string>;
81
+ /** Metrics export interval in milliseconds (defaults to 60000) */
82
+ metricsInterval?: number;
83
+ /** Additional resource attributes */
84
+ resourceAttributes?: Record<string, string>;
85
+ }
86
+
87
+ /**
88
+ * Logger interface
89
+ */
90
+ export interface Logger {
91
+ debug(message: string, meta?: unknown): void;
92
+ info(message: string, meta?: unknown): void;
93
+ warn(message: string, meta?: unknown): void;
94
+ error(message: string, meta?: unknown): void;
95
+ child?(meta: Record<string, unknown>): Logger;
96
+ }
97
+
98
+ /**
99
+ * Audit configuration
100
+ */
101
+ export interface AuditConfig {
102
+ /** Enable audit logging */
103
+ enabled: boolean;
104
+ /** Audit sink(s) for logging events */
105
+ sinks?: AuditSink | AuditSink[];
106
+ }
107
+
108
+ /**
109
+ * Provider configuration
110
+ */
111
+ export interface ProvidersConfig {
112
+ /** Cache provider for storing execution state and data (defaults to MemoryCache) */
113
+ cache?: CacheProvider;
114
+ /** Token/credential store for managing client tokens (defaults to EnvAuthProvider) */
115
+ auth?: AuthProvider;
116
+ }
117
+
118
+ /**
119
+ * Server configuration (user input - all fields optional)
120
+ */
121
+ export interface ServerConfig {
122
+ execution?: Partial<ExecutionConfig>;
123
+ clientInit?: Partial<ClientInitConfig>;
124
+ executionState?: Partial<ExecutionStateConfig>;
125
+ discovery?: Partial<DiscoveryConfig>;
126
+ /** Audit logging configuration */
127
+ audit?: Partial<AuditConfig>;
128
+ /** OpenTelemetry tracing and metrics configuration */
129
+ otel?: Partial<OpenTelemetryConfig>;
130
+ /** External providers (cache, auth) */
131
+ providers?: ProvidersConfig;
132
+ logger?: 'debug' | 'info' | 'warn' | 'error' | Logger;
133
+ }
134
+
135
+ /**
136
+ * Resolved server configuration with defaults applied
137
+ */
138
+ export interface ResolvedServerConfig {
139
+ execution: ExecutionConfig;
140
+ clientInit: ClientInitConfig;
141
+ executionState: ExecutionStateConfig;
142
+ discovery: DiscoveryConfig;
143
+ audit: AuditConfig;
144
+ otel: OpenTelemetryConfig;
145
+ logger: 'debug' | 'info' | 'warn' | 'error' | Logger;
146
+ }
147
+
148
+ /**
149
+ * Request context passed through middleware
150
+ */
151
+ export interface RequestContext {
152
+ method: string;
153
+ path: string;
154
+ query: Record<string, string>;
155
+ headers: Record<string, string>;
156
+ body: unknown;
157
+ clientId?: string;
158
+ clientToken?: string;
159
+ userId?: string;
160
+ user?: unknown;
161
+ executionId?: string;
162
+ code?: string;
163
+ validation?: unknown;
164
+ result?: unknown;
165
+ error?: Error;
166
+ cache?: CacheProvider;
167
+ auth?: AuthProvider;
168
+ audit?: AuditSink;
169
+ logger: Logger;
170
+ status: number;
171
+ responseBody: unknown;
172
+ throw(status: number, message: string): never;
173
+ assert(condition: boolean, message: string): asserts condition;
174
+ set(header: string, value: string): void;
175
+ }
176
+
177
+ /**
178
+ * Middleware function signature
179
+ */
180
+ export type Middleware = (ctx: RequestContext, next: () => Promise<void>) => Promise<void>;
@@ -0,0 +1,21 @@
1
+ import { IncomingMessage } from 'node:http';
2
+ import { readJsonBody } from '../utils/request.js';
3
+
4
+ export function parseBody(req: IncomingMessage): Promise<unknown> {
5
+ return readJsonBody(req);
6
+ }
7
+
8
+ export function parseQuery(url: string): Record<string, string> {
9
+ const queryIndex = url.indexOf('?');
10
+ if (queryIndex === -1) return {};
11
+
12
+ const queryString = url.substring(queryIndex + 1);
13
+ const params = new URLSearchParams(queryString);
14
+ const result: Record<string, string> = {};
15
+
16
+ for (const [key, value] of params) {
17
+ result[key] = value;
18
+ }
19
+
20
+ return result;
21
+ }