@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.
- package/README.md +489 -0
- package/dist/aggregator/index.d.ts +59 -0
- package/dist/aggregator/index.d.ts.map +1 -0
- package/dist/aggregator/index.js +171 -0
- package/dist/aggregator/index.js.map +1 -0
- package/dist/callback/index.d.ts +98 -0
- package/dist/callback/index.d.ts.map +1 -0
- package/dist/callback/index.js +136 -0
- package/dist/callback/index.js.map +1 -0
- package/dist/client-sessions.d.ts +82 -0
- package/dist/client-sessions.d.ts.map +1 -0
- package/dist/client-sessions.js +174 -0
- package/dist/client-sessions.js.map +1 -0
- package/dist/controllers/definitions.controller.d.ts +4 -0
- package/dist/controllers/definitions.controller.d.ts.map +1 -0
- package/dist/controllers/definitions.controller.js +11 -0
- package/dist/controllers/definitions.controller.js.map +1 -0
- package/dist/controllers/execute.controller.d.ts +18 -0
- package/dist/controllers/execute.controller.d.ts.map +1 -0
- package/dist/controllers/execute.controller.js +122 -0
- package/dist/controllers/execute.controller.js.map +1 -0
- package/dist/controllers/info.controller.d.ts +3 -0
- package/dist/controllers/info.controller.d.ts.map +1 -0
- package/dist/controllers/info.controller.js +13 -0
- package/dist/controllers/info.controller.js.map +1 -0
- package/dist/controllers/resume.controller.d.ts +11 -0
- package/dist/controllers/resume.controller.d.ts.map +1 -0
- package/dist/controllers/resume.controller.js +61 -0
- package/dist/controllers/resume.controller.js.map +1 -0
- package/dist/controllers/search.controller.d.ts +4 -0
- package/dist/controllers/search.controller.d.ts.map +1 -0
- package/dist/controllers/search.controller.js +7 -0
- package/dist/controllers/search.controller.js.map +1 -0
- package/dist/controllers/stream.controller.d.ts +19 -0
- package/dist/controllers/stream.controller.d.ts.map +1 -0
- package/dist/controllers/stream.controller.js +141 -0
- package/dist/controllers/stream.controller.js.map +1 -0
- package/dist/core/config.d.ts +161 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +7 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/http.d.ts +4 -0
- package/dist/core/http.d.ts.map +1 -0
- package/dist/core/http.js +17 -0
- package/dist/core/http.js.map +1 -0
- package/dist/create-server.d.ts +120 -0
- package/dist/create-server.d.ts.map +1 -0
- package/dist/create-server.js +423 -0
- package/dist/create-server.js.map +1 -0
- package/dist/execution-state/index.d.ts +95 -0
- package/dist/execution-state/index.d.ts.map +1 -0
- package/dist/execution-state/index.js +128 -0
- package/dist/execution-state/index.js.map +1 -0
- package/dist/executor/ast-provenance-bridge.d.ts +12 -0
- package/dist/executor/ast-provenance-bridge.d.ts.map +1 -0
- package/dist/executor/ast-provenance-bridge.js +66 -0
- package/dist/executor/ast-provenance-bridge.js.map +1 -0
- package/dist/executor/ast-tracking-runtime.d.ts +7 -0
- package/dist/executor/ast-tracking-runtime.d.ts.map +1 -0
- package/dist/executor/ast-tracking-runtime.js +559 -0
- package/dist/executor/ast-tracking-runtime.js.map +1 -0
- package/dist/executor/bootstrap-generated.d.ts +32 -0
- package/dist/executor/bootstrap-generated.d.ts.map +1 -0
- package/dist/executor/bootstrap-generated.js +90 -0
- package/dist/executor/bootstrap-generated.js.map +1 -0
- package/dist/executor/compiler-config.d.ts +32 -0
- package/dist/executor/compiler-config.d.ts.map +1 -0
- package/dist/executor/compiler-config.js +99 -0
- package/dist/executor/compiler-config.js.map +1 -0
- package/dist/executor/constants.d.ts +4 -0
- package/dist/executor/constants.d.ts.map +1 -0
- package/dist/executor/constants.js +4 -0
- package/dist/executor/constants.js.map +1 -0
- package/dist/executor/error-handler.d.ts +9 -0
- package/dist/executor/error-handler.d.ts.map +1 -0
- package/dist/executor/error-handler.js +95 -0
- package/dist/executor/error-handler.js.map +1 -0
- package/dist/executor/execution-error-handler.d.ts +7 -0
- package/dist/executor/execution-error-handler.d.ts.map +1 -0
- package/dist/executor/execution-error-handler.js +136 -0
- package/dist/executor/execution-error-handler.js.map +1 -0
- package/dist/executor/executor.d.ts +20 -0
- package/dist/executor/executor.d.ts.map +1 -0
- package/dist/executor/executor.js +452 -0
- package/dist/executor/executor.js.map +1 -0
- package/dist/executor/index.d.ts +4 -0
- package/dist/executor/index.d.ts.map +1 -0
- package/dist/executor/index.js +3 -0
- package/dist/executor/index.js.map +1 -0
- package/dist/executor/resume-handler.d.ts +9 -0
- package/dist/executor/resume-handler.d.ts.map +1 -0
- package/dist/executor/resume-handler.js +22 -0
- package/dist/executor/resume-handler.js.map +1 -0
- package/dist/executor/sandbox-builder.d.ts +29 -0
- package/dist/executor/sandbox-builder.d.ts.map +1 -0
- package/dist/executor/sandbox-builder.js +538 -0
- package/dist/executor/sandbox-builder.js.map +1 -0
- package/dist/executor/sandbox-injector.d.ts +7 -0
- package/dist/executor/sandbox-injector.d.ts.map +1 -0
- package/dist/executor/sandbox-injector.js +293 -0
- package/dist/executor/sandbox-injector.js.map +1 -0
- package/dist/executor/types.d.ts +21 -0
- package/dist/executor/types.d.ts.map +1 -0
- package/dist/executor/types.js +2 -0
- package/dist/executor/types.js.map +1 -0
- package/dist/explorer/index.d.ts +69 -0
- package/dist/explorer/index.d.ts.map +1 -0
- package/dist/explorer/index.js +228 -0
- package/dist/explorer/index.js.map +1 -0
- package/dist/handlers/definitions.handler.d.ts +3 -0
- package/dist/handlers/definitions.handler.d.ts.map +1 -0
- package/dist/handlers/definitions.handler.js +11 -0
- package/dist/handlers/definitions.handler.js.map +1 -0
- package/dist/handlers/execute.handler.d.ts +7 -0
- package/dist/handlers/execute.handler.d.ts.map +1 -0
- package/dist/handlers/execute.handler.js +225 -0
- package/dist/handlers/execute.handler.js.map +1 -0
- package/dist/handlers/explorer.handler.d.ts +4 -0
- package/dist/handlers/explorer.handler.d.ts.map +1 -0
- package/dist/handlers/explorer.handler.js +10 -0
- package/dist/handlers/explorer.handler.js.map +1 -0
- package/dist/handlers/init.handler.d.ts +5 -0
- package/dist/handlers/init.handler.d.ts.map +1 -0
- package/dist/handlers/init.handler.js +41 -0
- package/dist/handlers/init.handler.js.map +1 -0
- package/dist/handlers/resume.handler.d.ts +6 -0
- package/dist/handlers/resume.handler.d.ts.map +1 -0
- package/dist/handlers/resume.handler.js +256 -0
- package/dist/handlers/resume.handler.js.map +1 -0
- package/dist/handlers/search.handler.d.ts +5 -0
- package/dist/handlers/search.handler.d.ts.map +1 -0
- package/dist/handlers/search.handler.js +11 -0
- package/dist/handlers/search.handler.js.map +1 -0
- package/dist/http/request-handler.d.ts +15 -0
- package/dist/http/request-handler.d.ts.map +1 -0
- package/dist/http/request-handler.js +94 -0
- package/dist/http/request-handler.js.map +1 -0
- package/dist/http/router.d.ts +4 -0
- package/dist/http/router.d.ts.map +1 -0
- package/dist/http/router.js +32 -0
- package/dist/http/router.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation/index.d.ts +5 -0
- package/dist/instrumentation/index.d.ts.map +1 -0
- package/dist/instrumentation/index.js +5 -0
- package/dist/instrumentation/index.js.map +1 -0
- package/dist/instrumentation/serializer.d.ts +61 -0
- package/dist/instrumentation/serializer.d.ts.map +1 -0
- package/dist/instrumentation/serializer.js +334 -0
- package/dist/instrumentation/serializer.js.map +1 -0
- package/dist/instrumentation/state-manager.d.ts +61 -0
- package/dist/instrumentation/state-manager.d.ts.map +1 -0
- package/dist/instrumentation/state-manager.js +205 -0
- package/dist/instrumentation/state-manager.js.map +1 -0
- package/dist/instrumentation/transformer.d.ts +9 -0
- package/dist/instrumentation/transformer.d.ts.map +1 -0
- package/dist/instrumentation/transformer.js +70 -0
- package/dist/instrumentation/transformer.js.map +1 -0
- package/dist/instrumentation/types.d.ts +59 -0
- package/dist/instrumentation/types.d.ts.map +1 -0
- package/dist/instrumentation/types.js +5 -0
- package/dist/instrumentation/types.js.map +1 -0
- package/dist/middleware/audit.d.ts +18 -0
- package/dist/middleware/audit.d.ts.map +1 -0
- package/dist/middleware/audit.js +76 -0
- package/dist/middleware/audit.js.map +1 -0
- package/dist/openapi/index.d.ts +133 -0
- package/dist/openapi/index.d.ts.map +1 -0
- package/dist/openapi/index.js +235 -0
- package/dist/openapi/index.js.map +1 -0
- package/dist/openapi-loader.d.ts +87 -0
- package/dist/openapi-loader.d.ts.map +1 -0
- package/dist/openapi-loader.js +491 -0
- package/dist/openapi-loader.js.map +1 -0
- package/dist/routes/index.d.ts +21 -0
- package/dist/routes/index.d.ts.map +1 -0
- package/dist/routes/index.js +47 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/search/index.d.ts +48 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +156 -0
- package/dist/search/index.js.map +1 -0
- package/dist/security/index.d.ts +2 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +2 -0
- package/dist/security/index.js.map +1 -0
- package/dist/shutdown.d.ts +19 -0
- package/dist/shutdown.d.ts.map +1 -0
- package/dist/shutdown.js +87 -0
- package/dist/shutdown.js.map +1 -0
- package/dist/utils/banner.d.ts +12 -0
- package/dist/utils/banner.d.ts.map +1 -0
- package/dist/utils/banner.js +18 -0
- package/dist/utils/banner.js.map +1 -0
- package/dist/utils/context.d.ts +16 -0
- package/dist/utils/context.d.ts.map +1 -0
- package/dist/utils/context.js +44 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/error.d.ts +8 -0
- package/dist/utils/error.d.ts.map +1 -0
- package/dist/utils/error.js +17 -0
- package/dist/utils/error.js.map +1 -0
- package/dist/utils/hint-based-instrumentation.d.ts +14 -0
- package/dist/utils/hint-based-instrumentation.d.ts.map +1 -0
- package/dist/utils/hint-based-instrumentation.js +84 -0
- package/dist/utils/hint-based-instrumentation.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/info.d.ts +20 -0
- package/dist/utils/info.d.ts.map +1 -0
- package/dist/utils/info.js +15 -0
- package/dist/utils/info.js.map +1 -0
- package/dist/utils/provenance-reattachment.d.ts +32 -0
- package/dist/utils/provenance-reattachment.d.ts.map +1 -0
- package/dist/utils/provenance-reattachment.js +115 -0
- package/dist/utils/provenance-reattachment.js.map +1 -0
- package/dist/utils/request.d.ts +21 -0
- package/dist/utils/request.d.ts.map +1 -0
- package/dist/utils/request.js +44 -0
- package/dist/utils/request.js.map +1 -0
- package/dist/utils/response.d.ts +30 -0
- package/dist/utils/response.d.ts.map +1 -0
- package/dist/utils/response.js +53 -0
- package/dist/utils/response.js.map +1 -0
- package/dist/utils/runtime-types.d.ts +6 -0
- package/dist/utils/runtime-types.d.ts.map +1 -0
- package/dist/utils/runtime-types.js +14 -0
- package/dist/utils/runtime-types.js.map +1 -0
- package/dist/utils/schema.d.ts +9 -0
- package/dist/utils/schema.d.ts.map +1 -0
- package/dist/utils/schema.js +13 -0
- package/dist/utils/schema.js.map +1 -0
- package/dist/utils/token-emitter.d.ts +21 -0
- package/dist/utils/token-emitter.d.ts.map +1 -0
- package/dist/utils/token-emitter.js +129 -0
- package/dist/utils/token-emitter.js.map +1 -0
- package/dist/validator/index.d.ts +36 -0
- package/dist/validator/index.d.ts.map +1 -0
- package/dist/validator/index.js +224 -0
- package/dist/validator/index.js.map +1 -0
- package/package.json +68 -0
- package/src/aggregator/index.ts +207 -0
- package/src/callback/index.ts +191 -0
- package/src/client-sessions.ts +234 -0
- package/src/controllers/definitions.controller.ts +19 -0
- package/src/controllers/execute.controller.ts +166 -0
- package/src/controllers/info.controller.ts +14 -0
- package/src/controllers/resume.controller.ts +92 -0
- package/src/controllers/search.controller.ts +16 -0
- package/src/controllers/stream.controller.ts +190 -0
- package/src/core/config.ts +180 -0
- package/src/core/http.ts +21 -0
- package/src/create-server.ts +536 -0
- package/src/execution-state/index.ts +204 -0
- package/src/executor/ast-provenance-bridge.ts +80 -0
- package/src/executor/ast-tracking-runtime.ts +558 -0
- package/src/executor/bootstrap-generated.ts +90 -0
- package/src/executor/compiler-config.ts +146 -0
- package/src/executor/constants.ts +5 -0
- package/src/executor/error-handler.ts +118 -0
- package/src/executor/execution-error-handler.ts +178 -0
- package/src/executor/executor.ts +631 -0
- package/src/executor/index.ts +3 -0
- package/src/executor/resume-handler.ts +39 -0
- package/src/executor/sandbox-builder.ts +684 -0
- package/src/executor/sandbox-injector.ts +345 -0
- package/src/executor/types.ts +22 -0
- package/src/explorer/index.ts +297 -0
- package/src/handlers/definitions.handler.ts +13 -0
- package/src/handlers/execute.handler.ts +286 -0
- package/src/handlers/explorer.handler.ts +18 -0
- package/src/handlers/init.handler.ts +53 -0
- package/src/handlers/resume.handler.ts +316 -0
- package/src/handlers/search.handler.ts +32 -0
- package/src/http/request-handler.ts +117 -0
- package/src/http/router.ts +29 -0
- package/src/index.ts +60 -0
- package/src/instrumentation/index.ts +4 -0
- package/src/instrumentation/serializer.ts +421 -0
- package/src/instrumentation/state-manager.ts +237 -0
- package/src/instrumentation/transformer.ts +84 -0
- package/src/instrumentation/types.ts +76 -0
- package/src/middleware/audit.ts +101 -0
- package/src/openapi/index.ts +378 -0
- package/src/openapi-loader.ts +744 -0
- package/src/routes/index.ts +93 -0
- package/src/search/index.ts +216 -0
- package/src/security/index.ts +1 -0
- package/src/shutdown.ts +108 -0
- package/src/utils/banner.ts +25 -0
- package/src/utils/context.ts +58 -0
- package/src/utils/error.ts +25 -0
- package/src/utils/hint-based-instrumentation.ts +99 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/info.ts +31 -0
- package/src/utils/provenance-reattachment.ts +144 -0
- package/src/utils/request.ts +53 -0
- package/src/utils/response.ts +69 -0
- package/src/utils/runtime-types.ts +14 -0
- package/src/utils/schema.ts +18 -0
- package/src/utils/token-emitter.ts +182 -0
- package/src/validator/index.ts +253 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { nanoid } from 'nanoid';
|
|
3
|
+
import { log } from '@mondaydotcomorg/atp-runtime';
|
|
4
|
+
import type { CacheProvider, AuthProvider, AuditSink } from '@mondaydotcomorg/atp-protocol';
|
|
5
|
+
import { parseBody } from '../core/http.js';
|
|
6
|
+
import { handleError, createContext } from '../utils/index.js';
|
|
7
|
+
import type { RequestContext, Middleware } from '../core/config.js';
|
|
8
|
+
import type { ClientSessionManager } from '../client-sessions.js';
|
|
9
|
+
|
|
10
|
+
export interface RequestHandlerDeps {
|
|
11
|
+
cacheProvider?: CacheProvider;
|
|
12
|
+
authProvider?: AuthProvider;
|
|
13
|
+
auditSink?: AuditSink;
|
|
14
|
+
customLogger?: any;
|
|
15
|
+
middleware: Middleware[];
|
|
16
|
+
routeHandler: (ctx: RequestContext) => Promise<void>;
|
|
17
|
+
sessionManager?: ClientSessionManager;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function handleHTTPRequest(
|
|
21
|
+
req: IncomingMessage,
|
|
22
|
+
res: ServerResponse,
|
|
23
|
+
deps: RequestHandlerDeps,
|
|
24
|
+
responseHeaders: Map<IncomingMessage, Map<string, string>>
|
|
25
|
+
): Promise<void> {
|
|
26
|
+
const ctx = createContext({
|
|
27
|
+
req,
|
|
28
|
+
cacheProvider: deps.cacheProvider,
|
|
29
|
+
authProvider: deps.authProvider,
|
|
30
|
+
auditSink: deps.auditSink,
|
|
31
|
+
customLogger: deps.customLogger,
|
|
32
|
+
responseHeaders,
|
|
33
|
+
});
|
|
34
|
+
const headers = new Map<string, string>();
|
|
35
|
+
responseHeaders.set(req, headers);
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
if (req.method === 'POST' || req.method === 'PUT') {
|
|
39
|
+
ctx.body = await parseBody(req);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
await runMiddleware(ctx, deps.middleware, deps.routeHandler);
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
if (ctx.clientId && deps.sessionManager && ctx.path !== '/api/init') {
|
|
46
|
+
try {
|
|
47
|
+
const newToken = deps.sessionManager.generateToken(ctx.clientId);
|
|
48
|
+
const expiresAt = Date.now() + 60 * 60 * 1000;
|
|
49
|
+
|
|
50
|
+
headers.set('X-ATP-Token', newToken);
|
|
51
|
+
headers.set('X-ATP-Token-Expires', expiresAt.toString());
|
|
52
|
+
} catch (error) {}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
res.writeHead(ctx.status, {
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
...Object.fromEntries(headers),
|
|
58
|
+
});
|
|
59
|
+
res.end(JSON.stringify(ctx.responseBody));
|
|
60
|
+
} catch (writeError) {}
|
|
61
|
+
} catch (error) {
|
|
62
|
+
try {
|
|
63
|
+
if (ctx.clientId && deps.sessionManager && ctx.path !== '/api/init') {
|
|
64
|
+
try {
|
|
65
|
+
const newToken = deps.sessionManager.generateToken(ctx.clientId);
|
|
66
|
+
const expiresAt = Date.now() + 60 * 60 * 1000;
|
|
67
|
+
|
|
68
|
+
headers.set('X-ATP-Token', newToken);
|
|
69
|
+
headers.set('X-ATP-Token-Expires', expiresAt.toString());
|
|
70
|
+
|
|
71
|
+
log.debug('Token refresh headers set on error', {
|
|
72
|
+
clientId: ctx.clientId,
|
|
73
|
+
path: ctx.path,
|
|
74
|
+
hasSessionManager: !!deps.sessionManager,
|
|
75
|
+
headerCount: headers.size,
|
|
76
|
+
});
|
|
77
|
+
} catch (tokenError) {
|
|
78
|
+
log.warn('Token refresh failed on error', { error: tokenError });
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
log.debug('Token refresh skipped on error', {
|
|
82
|
+
hasClientId: !!ctx.clientId,
|
|
83
|
+
hasSessionManager: !!deps.sessionManager,
|
|
84
|
+
path: ctx.path,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
handleError(res, error as Error, nanoid(), headers);
|
|
89
|
+
} catch (handlerError) {
|
|
90
|
+
try {
|
|
91
|
+
if (!res.headersSent) {
|
|
92
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
93
|
+
res.end(JSON.stringify({ error: 'Internal server error' }));
|
|
94
|
+
}
|
|
95
|
+
} catch {}
|
|
96
|
+
}
|
|
97
|
+
} finally {
|
|
98
|
+
responseHeaders.delete(req);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function runMiddleware(
|
|
103
|
+
ctx: RequestContext,
|
|
104
|
+
middleware: Middleware[],
|
|
105
|
+
routeHandler: (ctx: RequestContext) => Promise<void>
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
let index = 0;
|
|
108
|
+
const next = async (): Promise<void> => {
|
|
109
|
+
const mw = middleware[index++];
|
|
110
|
+
if (mw) {
|
|
111
|
+
await mw(ctx, next);
|
|
112
|
+
} else {
|
|
113
|
+
await routeHandler(ctx);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
await next();
|
|
117
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { RequestContext } from '../core/config.js';
|
|
2
|
+
import type { AgentToolProtocolServer } from '../create-server.js';
|
|
3
|
+
|
|
4
|
+
export async function handleRoute(
|
|
5
|
+
ctx: RequestContext,
|
|
6
|
+
server: AgentToolProtocolServer
|
|
7
|
+
): Promise<void> {
|
|
8
|
+
if (ctx.path === '/api/info' && ctx.method === 'GET') {
|
|
9
|
+
ctx.responseBody = server.getInfo();
|
|
10
|
+
} else if (ctx.path === '/api/definitions' && ctx.method === 'GET') {
|
|
11
|
+
ctx.responseBody = await server.getDefinitions(ctx);
|
|
12
|
+
} else if (ctx.path === '/api/init' && ctx.method === 'POST') {
|
|
13
|
+
ctx.responseBody = await server.handleInit(ctx);
|
|
14
|
+
} else if (ctx.path === '/api/search' && ctx.method === 'POST') {
|
|
15
|
+
ctx.responseBody = await server.handleSearch(ctx);
|
|
16
|
+
} else if (ctx.path.startsWith('/api/search') && ctx.method === 'GET') {
|
|
17
|
+
ctx.responseBody = await server.handleSearchQuery(ctx);
|
|
18
|
+
} else if (ctx.path === '/api/explore' && ctx.method === 'POST') {
|
|
19
|
+
ctx.responseBody = await server.handleExplore(ctx);
|
|
20
|
+
} else if (ctx.path === '/api/execute' && ctx.method === 'POST') {
|
|
21
|
+
ctx.responseBody = await server.handleExecute(ctx);
|
|
22
|
+
} else if (ctx.path.startsWith('/api/resume/') && ctx.method === 'POST') {
|
|
23
|
+
const executionId = ctx.path.substring('/api/resume/'.length);
|
|
24
|
+
ctx.responseBody = await server.handleResume(ctx, executionId);
|
|
25
|
+
} else {
|
|
26
|
+
ctx.status = 404;
|
|
27
|
+
ctx.responseBody = { error: 'Not found' };
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export { AgentToolProtocolServer, createServer } from './create-server.js';
|
|
2
|
+
export { loadOpenAPI } from './openapi-loader.js';
|
|
3
|
+
export { APIAggregator } from './aggregator/index.js';
|
|
4
|
+
export { SearchEngine } from './search/index.js';
|
|
5
|
+
export { SandboxExecutor } from './executor/index.js';
|
|
6
|
+
export type {
|
|
7
|
+
ServerConfig,
|
|
8
|
+
AuditConfig,
|
|
9
|
+
Logger,
|
|
10
|
+
Middleware,
|
|
11
|
+
RequestContext,
|
|
12
|
+
} from './core/config.js';
|
|
13
|
+
export { MB, GB, SECOND, MINUTE, HOUR, DAY } from './core/config.js';
|
|
14
|
+
|
|
15
|
+
export type {
|
|
16
|
+
ProvenanceMetadata,
|
|
17
|
+
SourceMetadata,
|
|
18
|
+
ToolSource,
|
|
19
|
+
LLMSource,
|
|
20
|
+
UserSource,
|
|
21
|
+
SystemSource,
|
|
22
|
+
ReaderPermissions,
|
|
23
|
+
ProvenanceState,
|
|
24
|
+
PolicyAction,
|
|
25
|
+
PolicyResult,
|
|
26
|
+
SecurityPolicy,
|
|
27
|
+
} from '@mondaydotcomorg/atp-provenance';
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
ProvenanceMode,
|
|
31
|
+
ProvenanceSource,
|
|
32
|
+
ProvenanceSecurityError,
|
|
33
|
+
createProvenanceProxy,
|
|
34
|
+
getProvenance,
|
|
35
|
+
hasProvenance,
|
|
36
|
+
getAllProvenance,
|
|
37
|
+
canRead,
|
|
38
|
+
getProvenanceForPrimitive,
|
|
39
|
+
markPrimitiveTainted,
|
|
40
|
+
isPrimitiveTainted,
|
|
41
|
+
setProvenanceExecutionId,
|
|
42
|
+
clearProvenanceExecutionId,
|
|
43
|
+
cleanupProvenanceForExecution,
|
|
44
|
+
captureProvenanceState,
|
|
45
|
+
restoreProvenanceState,
|
|
46
|
+
SecurityPolicyEngine,
|
|
47
|
+
type Logger as ProvenanceLogger,
|
|
48
|
+
preventDataExfiltration,
|
|
49
|
+
preventDataExfiltrationWithApproval,
|
|
50
|
+
requireUserOrigin,
|
|
51
|
+
requireUserOriginWithApproval,
|
|
52
|
+
blockLLMRecipients,
|
|
53
|
+
blockLLMRecipientsWithApproval,
|
|
54
|
+
auditSensitiveAccess,
|
|
55
|
+
getBuiltInPolicies,
|
|
56
|
+
getBuiltInPoliciesWithApproval,
|
|
57
|
+
createCustomPolicy,
|
|
58
|
+
instrumentCode,
|
|
59
|
+
createTrackingRuntime,
|
|
60
|
+
} from '@mondaydotcomorg/atp-provenance';
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*/
|
|
3
|
+
import type { SerializedValue } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the Serializer
|
|
7
|
+
*/
|
|
8
|
+
export interface SerializerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Additional global built-ins to skip during variable extraction
|
|
11
|
+
* (beyond the standard JavaScript built-ins that are detected automatically)
|
|
12
|
+
*/
|
|
13
|
+
customGlobalBuiltIns?: string[];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether to use caching for keyword/built-in detection
|
|
17
|
+
* Default: true (recommended for performance)
|
|
18
|
+
*/
|
|
19
|
+
enableCaching?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Runtime-detected global built-ins (cached for performance)
|
|
24
|
+
*/
|
|
25
|
+
let cachedGlobals: Set<string> | null = null;
|
|
26
|
+
|
|
27
|
+
export class Serializer {
|
|
28
|
+
private refCounter = 0;
|
|
29
|
+
private refMap = new WeakMap<object, string>();
|
|
30
|
+
private customGlobalBuiltIns: Set<string>;
|
|
31
|
+
private keywordCache = new Map<string, boolean>();
|
|
32
|
+
private globalCache: Set<string> | null = null;
|
|
33
|
+
private enableCaching: boolean;
|
|
34
|
+
|
|
35
|
+
constructor(options: SerializerOptions = {}) {
|
|
36
|
+
this.customGlobalBuiltIns = new Set(options.customGlobalBuiltIns || []);
|
|
37
|
+
this.enableCaching = options.enableCaching !== false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Deep serialize a value with circular reference detection
|
|
42
|
+
*/
|
|
43
|
+
serialize(value: unknown, scope: Record<string, unknown> = {}): SerializedValue {
|
|
44
|
+
const visited = new WeakSet();
|
|
45
|
+
return this.deepSerialize(value, scope, visited);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Deserialize a value back to its original form
|
|
50
|
+
*/
|
|
51
|
+
deserialize(serialized: SerializedValue, refRegistry: Map<string, unknown> = new Map()): unknown {
|
|
52
|
+
return this.deepDeserialize(serialized, refRegistry);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private deepSerialize(
|
|
56
|
+
value: unknown,
|
|
57
|
+
scope: Record<string, unknown>,
|
|
58
|
+
visited: WeakSet<object>
|
|
59
|
+
): SerializedValue {
|
|
60
|
+
if (value === null || value === undefined) {
|
|
61
|
+
return { type: 'primitive', value };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const type = typeof value;
|
|
65
|
+
if (
|
|
66
|
+
type === 'string' ||
|
|
67
|
+
type === 'number' ||
|
|
68
|
+
type === 'boolean' ||
|
|
69
|
+
type === 'bigint' ||
|
|
70
|
+
type === 'symbol'
|
|
71
|
+
) {
|
|
72
|
+
if (type === 'bigint') {
|
|
73
|
+
return { type: 'primitive', value: value.toString() + 'n' };
|
|
74
|
+
}
|
|
75
|
+
if (type === 'symbol') {
|
|
76
|
+
return { type: 'primitive', value: value.toString() };
|
|
77
|
+
}
|
|
78
|
+
return { type: 'primitive', value };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (type === 'function') {
|
|
82
|
+
return this.serializeFunction(value as Function, scope, visited);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (type === 'object' && value !== null) {
|
|
86
|
+
if (visited.has(value as object)) {
|
|
87
|
+
let refId = this.refMap.get(value as object);
|
|
88
|
+
if (!refId) {
|
|
89
|
+
refId = `ref_${this.refCounter++}`;
|
|
90
|
+
this.refMap.set(value as object, refId);
|
|
91
|
+
}
|
|
92
|
+
return { type: 'circular', refId };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
visited.add(value as object);
|
|
96
|
+
|
|
97
|
+
if (value instanceof Date) {
|
|
98
|
+
return {
|
|
99
|
+
type: 'date',
|
|
100
|
+
value: value.toISOString(),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (value instanceof RegExp) {
|
|
105
|
+
return {
|
|
106
|
+
type: 'regexp',
|
|
107
|
+
pattern: value.source,
|
|
108
|
+
flags: value.flags,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
return {
|
|
114
|
+
type: 'array',
|
|
115
|
+
value: value.map((item) => this.deepSerialize(item, scope, visited)),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (value instanceof Map) {
|
|
120
|
+
const entries: Array<[SerializedValue, SerializedValue]> = [];
|
|
121
|
+
for (const [k, v] of value.entries()) {
|
|
122
|
+
entries.push([
|
|
123
|
+
this.deepSerialize(k, scope, visited),
|
|
124
|
+
this.deepSerialize(v, scope, visited),
|
|
125
|
+
]);
|
|
126
|
+
}
|
|
127
|
+
return { type: 'map', entries };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (value instanceof Set) {
|
|
131
|
+
const items: SerializedValue[] = [];
|
|
132
|
+
for (const item of value.values()) {
|
|
133
|
+
items.push(this.deepSerialize(item, scope, visited));
|
|
134
|
+
}
|
|
135
|
+
return { type: 'set', items };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const properties: Record<string, SerializedValue> = {};
|
|
140
|
+
for (const [k, v] of Object.entries(value as object)) {
|
|
141
|
+
properties[k] = this.deepSerialize(v, scope, visited);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
type: 'object',
|
|
146
|
+
className: (value as any).constructor?.name,
|
|
147
|
+
properties,
|
|
148
|
+
};
|
|
149
|
+
} catch (e) {
|
|
150
|
+
return { type: 'nonserializable' };
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return { type: 'nonserializable' };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private serializeFunction(
|
|
158
|
+
fn: Function,
|
|
159
|
+
scope: Record<string, unknown>,
|
|
160
|
+
visited: WeakSet<object>
|
|
161
|
+
): SerializedValue {
|
|
162
|
+
try {
|
|
163
|
+
const source = fn.toString();
|
|
164
|
+
|
|
165
|
+
if (source.includes('[native code]')) {
|
|
166
|
+
return {
|
|
167
|
+
type: 'function',
|
|
168
|
+
source: 'native',
|
|
169
|
+
className: fn.name,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const referencedVars = this.extractReferencedVars(source);
|
|
174
|
+
|
|
175
|
+
const closure: Record<string, SerializedValue> = {};
|
|
176
|
+
for (const varName of referencedVars) {
|
|
177
|
+
if (varName in scope) {
|
|
178
|
+
closure[varName] = this.deepSerialize(scope[varName], scope, visited);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
type: 'function',
|
|
184
|
+
source,
|
|
185
|
+
closure,
|
|
186
|
+
isAsync: source.startsWith('async'),
|
|
187
|
+
isGenerator: fn.constructor.name === 'GeneratorFunction',
|
|
188
|
+
isArrow: source.includes('=>'),
|
|
189
|
+
};
|
|
190
|
+
} catch (e) {
|
|
191
|
+
return { type: 'nonserializable' };
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private extractReferencedVars(source: string): Set<string> {
|
|
196
|
+
const vars = new Set<string>();
|
|
197
|
+
|
|
198
|
+
const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
|
|
199
|
+
let match;
|
|
200
|
+
|
|
201
|
+
while ((match = identifierRegex.exec(source)) !== null) {
|
|
202
|
+
const name = match[1];
|
|
203
|
+
|
|
204
|
+
if (!name) continue;
|
|
205
|
+
|
|
206
|
+
if (this.isReservedKeyword(name)) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (this.isGlobalBuiltIn(name)) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
vars.add(name);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return vars;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Check if a name is a reserved JavaScript keyword
|
|
222
|
+
* Uses caching for performance when enabled
|
|
223
|
+
*/
|
|
224
|
+
private isReservedKeyword(name: string): boolean {
|
|
225
|
+
if (this.enableCaching && this.keywordCache.has(name)) {
|
|
226
|
+
return this.keywordCache.get(name)!;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
let isKeyword = false;
|
|
230
|
+
try {
|
|
231
|
+
new Function(`var ${name};`);
|
|
232
|
+
isKeyword = false;
|
|
233
|
+
} catch {
|
|
234
|
+
isKeyword = true;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (this.enableCaching) {
|
|
238
|
+
this.keywordCache.set(name, isKeyword);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return isKeyword;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Check if a name is a global built-in
|
|
246
|
+
* Dynamically detects built-ins at runtime with caching for performance
|
|
247
|
+
*/
|
|
248
|
+
private isGlobalBuiltIn(name: string): boolean {
|
|
249
|
+
if (this.customGlobalBuiltIns.has(name)) {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (this.enableCaching && !this.globalCache) {
|
|
254
|
+
this.globalCache = this.detectGlobalBuiltIns();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (this.enableCaching && this.globalCache) {
|
|
258
|
+
return this.globalCache.has(name);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return this.isGlobalProperty(name);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Detect all global built-ins at runtime
|
|
266
|
+
* This is called once and cached for performance
|
|
267
|
+
*/
|
|
268
|
+
private detectGlobalBuiltIns(): Set<string> {
|
|
269
|
+
const globals = new Set<string>();
|
|
270
|
+
|
|
271
|
+
const globalObj = typeof globalThis !== 'undefined' ? globalThis : global;
|
|
272
|
+
|
|
273
|
+
try {
|
|
274
|
+
const ownProps = Object.getOwnPropertyNames(globalObj);
|
|
275
|
+
for (const prop of ownProps) {
|
|
276
|
+
globals.add(prop);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
let proto = Object.getPrototypeOf(globalObj);
|
|
280
|
+
while (proto) {
|
|
281
|
+
const protoProps = Object.getOwnPropertyNames(proto);
|
|
282
|
+
for (const prop of protoProps) {
|
|
283
|
+
globals.add(prop);
|
|
284
|
+
}
|
|
285
|
+
proto = Object.getPrototypeOf(proto);
|
|
286
|
+
}
|
|
287
|
+
} catch (e) {
|
|
288
|
+
const fallbackGlobals = [
|
|
289
|
+
'undefined',
|
|
290
|
+
'null',
|
|
291
|
+
'true',
|
|
292
|
+
'false',
|
|
293
|
+
'Math',
|
|
294
|
+
'Date',
|
|
295
|
+
'Array',
|
|
296
|
+
'Object',
|
|
297
|
+
'String',
|
|
298
|
+
'Number',
|
|
299
|
+
'Boolean',
|
|
300
|
+
'Promise',
|
|
301
|
+
'Set',
|
|
302
|
+
'Map',
|
|
303
|
+
'WeakMap',
|
|
304
|
+
'WeakSet',
|
|
305
|
+
'JSON',
|
|
306
|
+
'Error',
|
|
307
|
+
'TypeError',
|
|
308
|
+
'RegExp',
|
|
309
|
+
'parseInt',
|
|
310
|
+
'parseFloat',
|
|
311
|
+
'isNaN',
|
|
312
|
+
'isFinite',
|
|
313
|
+
'console',
|
|
314
|
+
'process',
|
|
315
|
+
'Buffer',
|
|
316
|
+
'global',
|
|
317
|
+
'globalThis',
|
|
318
|
+
];
|
|
319
|
+
for (const g of fallbackGlobals) {
|
|
320
|
+
globals.add(g);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return globals;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Check if a name is a property of the global object
|
|
329
|
+
* Used when caching is disabled
|
|
330
|
+
*/
|
|
331
|
+
private isGlobalProperty(name: string): boolean {
|
|
332
|
+
const globalObj = typeof globalThis !== 'undefined' ? globalThis : global;
|
|
333
|
+
|
|
334
|
+
try {
|
|
335
|
+
return name in globalObj;
|
|
336
|
+
} catch {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private deepDeserialize(serialized: SerializedValue, refRegistry: Map<string, unknown>): unknown {
|
|
342
|
+
switch (serialized.type) {
|
|
343
|
+
case 'primitive':
|
|
344
|
+
return serialized.value;
|
|
345
|
+
|
|
346
|
+
case 'date':
|
|
347
|
+
return new Date(serialized.value as string);
|
|
348
|
+
|
|
349
|
+
case 'regexp':
|
|
350
|
+
return new RegExp(serialized.pattern || '', serialized.flags || '');
|
|
351
|
+
|
|
352
|
+
case 'array':
|
|
353
|
+
return (serialized.value as SerializedValue[]).map((item) =>
|
|
354
|
+
this.deepDeserialize(item, refRegistry)
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
case 'map': {
|
|
358
|
+
const map = new Map();
|
|
359
|
+
if (serialized.entries) {
|
|
360
|
+
for (const [k, v] of serialized.entries) {
|
|
361
|
+
map.set(this.deepDeserialize(k, refRegistry), this.deepDeserialize(v, refRegistry));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return map;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
case 'set': {
|
|
368
|
+
const set = new Set();
|
|
369
|
+
if (serialized.items) {
|
|
370
|
+
for (const item of serialized.items) {
|
|
371
|
+
set.add(this.deepDeserialize(item, refRegistry));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return set;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
case 'object': {
|
|
378
|
+
const obj: Record<string, unknown> = {};
|
|
379
|
+
if (serialized.properties) {
|
|
380
|
+
for (const [k, v] of Object.entries(serialized.properties)) {
|
|
381
|
+
obj[k] = this.deepDeserialize(v, refRegistry);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return obj;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
case 'function':
|
|
388
|
+
return this.deserializeFunction(serialized, refRegistry);
|
|
389
|
+
|
|
390
|
+
case 'circular':
|
|
391
|
+
return refRegistry.get(serialized.refId!) || null;
|
|
392
|
+
|
|
393
|
+
case 'nonserializable':
|
|
394
|
+
default:
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
private deserializeFunction(
|
|
400
|
+
serialized: SerializedValue,
|
|
401
|
+
refRegistry: Map<string, unknown>
|
|
402
|
+
): Function | undefined {
|
|
403
|
+
try {
|
|
404
|
+
if (serialized.source === 'native' || !serialized.source) {
|
|
405
|
+
return undefined;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const closureNames = Object.keys(serialized.closure || {});
|
|
409
|
+
const closureValues = closureNames.map((name) => {
|
|
410
|
+
const closureValue = serialized.closure?.[name];
|
|
411
|
+
return closureValue ? this.deepDeserialize(closureValue, refRegistry) : undefined;
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const fnFactory = new Function(...closureNames, `return ${serialized.source}`);
|
|
415
|
+
|
|
416
|
+
return fnFactory(...closureValues);
|
|
417
|
+
} catch (e) {
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|