@mondaydotcomorg/atp-server 0.19.10 → 0.19.11
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/dist/core/config.d.ts +20 -1
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js.map +1 -1
- package/dist/core/request-scope.d.ts +33 -0
- package/dist/core/request-scope.d.ts.map +1 -0
- package/dist/core/request-scope.js +93 -0
- package/dist/core/request-scope.js.map +1 -0
- package/dist/create-server.d.ts +1 -0
- package/dist/create-server.d.ts.map +1 -1
- package/dist/create-server.js +8 -0
- package/dist/create-server.js.map +1 -1
- package/dist/executor/sandbox-builder.d.ts.map +1 -1
- package/dist/executor/sandbox-builder.js +27 -9
- package/dist/executor/sandbox-builder.js.map +1 -1
- package/dist/explorer/index.d.ts +14 -1
- package/dist/explorer/index.d.ts.map +1 -1
- package/dist/explorer/index.js +75 -6
- package/dist/explorer/index.js.map +1 -1
- package/dist/handlers/definitions.handler.d.ts.map +1 -1
- package/dist/handlers/definitions.handler.js +4 -2
- package/dist/handlers/definitions.handler.js.map +1 -1
- package/dist/handlers/execute.handler.d.ts.map +1 -1
- package/dist/handlers/execute.handler.js +27 -0
- package/dist/handlers/execute.handler.js.map +1 -1
- package/dist/http/request-handler.d.ts +2 -1
- package/dist/http/request-handler.d.ts.map +1 -1
- package/dist/http/request-handler.js +68 -57
- package/dist/http/request-handler.js.map +1 -1
- package/dist/index.cjs +275 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +275 -71
- package/dist/index.js.map +1 -1
- package/dist/search/index.d.ts +2 -0
- package/dist/search/index.d.ts.map +1 -1
- package/dist/search/index.js +16 -0
- package/dist/search/index.js.map +1 -1
- package/package.json +6 -6
- package/src/core/config.ts +21 -0
- package/src/core/request-scope.ts +131 -0
- package/src/create-server.ts +10 -0
- package/src/executor/sandbox-builder.ts +28 -10
- package/src/explorer/index.ts +98 -8
- package/src/handlers/definitions.handler.ts +5 -2
- package/src/handlers/execute.handler.ts +29 -1
- package/src/http/request-handler.ts +70 -58
- package/src/index.ts +1 -0
- package/src/search/index.ts +18 -0
package/dist/index.cjs
CHANGED
|
@@ -10,6 +10,7 @@ var crypto = require('crypto');
|
|
|
10
10
|
var nanoid = require('nanoid');
|
|
11
11
|
var jwt = require('jsonwebtoken');
|
|
12
12
|
var ivm = require('isolated-vm');
|
|
13
|
+
var async_hooks = require('async_hooks');
|
|
13
14
|
var parser = require('@babel/parser');
|
|
14
15
|
var _traverse = require('@babel/traverse');
|
|
15
16
|
var _generate = require('@babel/generator');
|
|
@@ -6948,6 +6949,62 @@ var BOOTSTRAP_CODE = `
|
|
|
6948
6949
|
}
|
|
6949
6950
|
};
|
|
6950
6951
|
`;
|
|
6952
|
+
var asyncLocalStorage = new async_hooks.AsyncLocalStorage();
|
|
6953
|
+
function runInRequestScope(context, fn) {
|
|
6954
|
+
return asyncLocalStorage.run(context, fn);
|
|
6955
|
+
}
|
|
6956
|
+
__name(runInRequestScope, "runInRequestScope");
|
|
6957
|
+
function getRequestToolRules() {
|
|
6958
|
+
return asyncLocalStorage.getStore()?.toolRules;
|
|
6959
|
+
}
|
|
6960
|
+
__name(getRequestToolRules, "getRequestToolRules");
|
|
6961
|
+
function isGroupAllowed(groupName, rules) {
|
|
6962
|
+
if (!rules) return true;
|
|
6963
|
+
if (rules.allowOnlyApiGroups && rules.allowOnlyApiGroups.length > 0) {
|
|
6964
|
+
return rules.allowOnlyApiGroups.includes(groupName);
|
|
6965
|
+
}
|
|
6966
|
+
if (rules.blockApiGroups && rules.blockApiGroups.length > 0) {
|
|
6967
|
+
return !rules.blockApiGroups.includes(groupName);
|
|
6968
|
+
}
|
|
6969
|
+
return true;
|
|
6970
|
+
}
|
|
6971
|
+
__name(isGroupAllowed, "isGroupAllowed");
|
|
6972
|
+
function isToolAllowed(toolName, groupName, metadata, rules) {
|
|
6973
|
+
if (!rules) return true;
|
|
6974
|
+
const fullName = `${groupName}.${toolName}`;
|
|
6975
|
+
if (rules.allowOnlyTools && rules.allowOnlyTools.length > 0) {
|
|
6976
|
+
return rules.allowOnlyTools.some((t2) => t2 === toolName || t2 === fullName);
|
|
6977
|
+
}
|
|
6978
|
+
if (rules.blockTools && rules.blockTools.length > 0) {
|
|
6979
|
+
if (rules.blockTools.some((t2) => t2 === toolName || t2 === fullName)) {
|
|
6980
|
+
return false;
|
|
6981
|
+
}
|
|
6982
|
+
}
|
|
6983
|
+
if (metadata) {
|
|
6984
|
+
if (rules.blockOperationTypes && metadata.operationType && rules.blockOperationTypes.includes(metadata.operationType)) {
|
|
6985
|
+
return false;
|
|
6986
|
+
}
|
|
6987
|
+
if (rules.blockSensitivityLevels && metadata.sensitivityLevel && rules.blockSensitivityLevels.includes(metadata.sensitivityLevel)) {
|
|
6988
|
+
return false;
|
|
6989
|
+
}
|
|
6990
|
+
}
|
|
6991
|
+
return true;
|
|
6992
|
+
}
|
|
6993
|
+
__name(isToolAllowed, "isToolAllowed");
|
|
6994
|
+
function filterApiGroups(apiGroups, rules) {
|
|
6995
|
+
const effectiveRules = rules ?? getRequestToolRules();
|
|
6996
|
+
if (!effectiveRules) return apiGroups;
|
|
6997
|
+
return apiGroups.filter((group) => isGroupAllowed(group.name, effectiveRules)).map((group) => {
|
|
6998
|
+
if (!group.functions) return group;
|
|
6999
|
+
const filteredFunctions = group.functions.filter((func) => isToolAllowed(func.name, group.name, func.metadata, effectiveRules));
|
|
7000
|
+
if (filteredFunctions.length === 0) return null;
|
|
7001
|
+
return {
|
|
7002
|
+
...group,
|
|
7003
|
+
functions: filteredFunctions
|
|
7004
|
+
};
|
|
7005
|
+
}).filter((group) => group !== null);
|
|
7006
|
+
}
|
|
7007
|
+
__name(filterApiGroups, "filterApiGroups");
|
|
6951
7008
|
var executionHintMaps = /* @__PURE__ */ new Map();
|
|
6952
7009
|
var executionHintValues = /* @__PURE__ */ new Map();
|
|
6953
7010
|
function storeHintMap(executionId, hintMap) {
|
|
@@ -7223,7 +7280,8 @@ var SandboxBuilder = class {
|
|
|
7223
7280
|
}
|
|
7224
7281
|
createAPIFunctions(logger, executionId, config) {
|
|
7225
7282
|
const api = {};
|
|
7226
|
-
|
|
7283
|
+
const allowedGroups = filterApiGroups(this.apiGroups, config.toolRules);
|
|
7284
|
+
for (const group of allowedGroups) {
|
|
7227
7285
|
if (group.functions) {
|
|
7228
7286
|
const groupObj = this.getOrCreateNestedGroup(api, group.name);
|
|
7229
7287
|
for (const func of group.functions) {
|
|
@@ -7375,7 +7433,28 @@ var SandboxBuilder = class {
|
|
|
7375
7433
|
metadata,
|
|
7376
7434
|
requestContext: config.requestContext
|
|
7377
7435
|
};
|
|
7378
|
-
const
|
|
7436
|
+
const toolCallStartTime = Date.now();
|
|
7437
|
+
let result;
|
|
7438
|
+
let toolCallError;
|
|
7439
|
+
try {
|
|
7440
|
+
result = await handler(input, handlerContext);
|
|
7441
|
+
} catch (error) {
|
|
7442
|
+
toolCallError = error instanceof Error ? error : new Error(String(error));
|
|
7443
|
+
throw error;
|
|
7444
|
+
} finally {
|
|
7445
|
+
if (config.onToolCall) {
|
|
7446
|
+
const duration = Date.now() - toolCallStartTime;
|
|
7447
|
+
config.onToolCall({
|
|
7448
|
+
toolName: func.name,
|
|
7449
|
+
apiGroup: group.name,
|
|
7450
|
+
input,
|
|
7451
|
+
output: toolCallError ? void 0 : result,
|
|
7452
|
+
error: toolCallError,
|
|
7453
|
+
duration,
|
|
7454
|
+
success: !toolCallError
|
|
7455
|
+
});
|
|
7456
|
+
}
|
|
7457
|
+
}
|
|
7379
7458
|
try {
|
|
7380
7459
|
atpRuntime.storeAPICallResult({
|
|
7381
7460
|
type: "api",
|
|
@@ -9964,12 +10043,14 @@ var SearchEngine = class {
|
|
|
9964
10043
|
__name(this, "SearchEngine");
|
|
9965
10044
|
}
|
|
9966
10045
|
index = [];
|
|
10046
|
+
apiGroups = [];
|
|
9967
10047
|
/**
|
|
9968
10048
|
* Creates a new SearchEngine instance.
|
|
9969
10049
|
* @param apiGroups - Array of API group configurations to index
|
|
9970
10050
|
*/
|
|
9971
10051
|
constructor(apiGroups) {
|
|
9972
10052
|
if (apiGroups) {
|
|
10053
|
+
this.apiGroups = apiGroups;
|
|
9973
10054
|
this.buildIndex(apiGroups);
|
|
9974
10055
|
}
|
|
9975
10056
|
}
|
|
@@ -10002,6 +10083,7 @@ var SearchEngine = class {
|
|
|
10002
10083
|
}
|
|
10003
10084
|
/**
|
|
10004
10085
|
* Searches for API functions matching the query.
|
|
10086
|
+
* Tool rules are automatically applied from the request scope.
|
|
10005
10087
|
* @param options - Search options including query and filters
|
|
10006
10088
|
* @param userId - Optional user ID for scope filtering
|
|
10007
10089
|
* @param authProvider - Optional auth provider for checking user scopes
|
|
@@ -10009,9 +10091,21 @@ var SearchEngine = class {
|
|
|
10009
10091
|
* @returns Array of search results sorted by relevance
|
|
10010
10092
|
*/
|
|
10011
10093
|
async search(options, userId, authProvider, scopeFilteringConfig) {
|
|
10094
|
+
const allowedGroups = filterApiGroups(this.apiGroups);
|
|
10095
|
+
const allowedTools = /* @__PURE__ */ new Set();
|
|
10096
|
+
for (const group of allowedGroups) {
|
|
10097
|
+
if (group.functions) {
|
|
10098
|
+
for (const func of group.functions) {
|
|
10099
|
+
allowedTools.add(`${group.name}:${func.name}`);
|
|
10100
|
+
}
|
|
10101
|
+
}
|
|
10102
|
+
}
|
|
10012
10103
|
const queryWords = this.extractKeywords(options.query);
|
|
10013
10104
|
const results = [];
|
|
10014
10105
|
for (const item of this.index) {
|
|
10106
|
+
if (!allowedTools.has(`${item.apiGroup}:${item.functionName}`)) {
|
|
10107
|
+
continue;
|
|
10108
|
+
}
|
|
10015
10109
|
if (options.apiGroups && !options.apiGroups.includes(item.apiGroup)) {
|
|
10016
10110
|
continue;
|
|
10017
10111
|
}
|
|
@@ -10241,7 +10335,9 @@ var ExplorerService = class {
|
|
|
10241
10335
|
__name(this, "ExplorerService");
|
|
10242
10336
|
}
|
|
10243
10337
|
root;
|
|
10338
|
+
apiGroups;
|
|
10244
10339
|
constructor(apiGroups) {
|
|
10340
|
+
this.apiGroups = apiGroups;
|
|
10245
10341
|
this.root = {
|
|
10246
10342
|
type: "directory",
|
|
10247
10343
|
name: "/",
|
|
@@ -10250,12 +10346,55 @@ var ExplorerService = class {
|
|
|
10250
10346
|
this.buildTree(apiGroups);
|
|
10251
10347
|
}
|
|
10252
10348
|
/**
|
|
10349
|
+
* Get filtering context based on current request's tool rules.
|
|
10350
|
+
* Returns sets for allowed items and all known items (for filtering).
|
|
10351
|
+
*/
|
|
10352
|
+
getFilterContext() {
|
|
10353
|
+
const allowedGroups = filterApiGroups(this.apiGroups);
|
|
10354
|
+
const context = {
|
|
10355
|
+
allowedTypes: /* @__PURE__ */ new Set(),
|
|
10356
|
+
allowedGroups: /* @__PURE__ */ new Set(),
|
|
10357
|
+
allowedTools: /* @__PURE__ */ new Set(),
|
|
10358
|
+
allGroups: new Set(this.apiGroups.map((g) => g.name))
|
|
10359
|
+
};
|
|
10360
|
+
for (const group of allowedGroups) {
|
|
10361
|
+
if (group.type !== "graphql") {
|
|
10362
|
+
context.allowedTypes.add(group.type);
|
|
10363
|
+
}
|
|
10364
|
+
context.allowedGroups.add(group.name);
|
|
10365
|
+
if (group.functions) {
|
|
10366
|
+
for (const func of group.functions) {
|
|
10367
|
+
context.allowedTools.add(`${group.name}:${func.name}`);
|
|
10368
|
+
}
|
|
10369
|
+
}
|
|
10370
|
+
}
|
|
10371
|
+
return context;
|
|
10372
|
+
}
|
|
10373
|
+
/**
|
|
10374
|
+
* Check if a directory should be visible based on filter context.
|
|
10375
|
+
*/
|
|
10376
|
+
isDirectoryAllowed(name, currentPath, ctx) {
|
|
10377
|
+
const API_TYPES = [
|
|
10378
|
+
"openapi",
|
|
10379
|
+
"mcp",
|
|
10380
|
+
"custom",
|
|
10381
|
+
"graphql"
|
|
10382
|
+
];
|
|
10383
|
+
if (currentPath === "/" && API_TYPES.includes(name)) {
|
|
10384
|
+
return ctx.allowedTypes.has(name) || ctx.allowedGroups.has(name);
|
|
10385
|
+
}
|
|
10386
|
+
if (ctx.allGroups.has(name)) {
|
|
10387
|
+
return ctx.allowedGroups.has(name);
|
|
10388
|
+
}
|
|
10389
|
+
return true;
|
|
10390
|
+
}
|
|
10391
|
+
/**
|
|
10253
10392
|
* Builds the virtual filesystem tree from API groups
|
|
10254
10393
|
*/
|
|
10255
10394
|
buildTree(apiGroups) {
|
|
10256
10395
|
for (const group of apiGroups) {
|
|
10257
10396
|
if (!group.functions || group.functions.length === 0) continue;
|
|
10258
|
-
const groupFolder = group.type === "graphql" ? this.ensureDirectory(this.root, group.name) : this.ensureDirectory(this.ensureDirectory(this.root, group.type), group.name);
|
|
10397
|
+
const groupFolder = group.type === "graphql" ? this.ensureDirectory(this.root, group.name, group.description) : this.ensureDirectory(this.ensureDirectory(this.root, group.type), group.name, group.description);
|
|
10259
10398
|
for (const func of group.functions) {
|
|
10260
10399
|
const segments = this.extractSegments(func, group);
|
|
10261
10400
|
if (segments.length > 1) {
|
|
@@ -10338,7 +10477,7 @@ var ExplorerService = class {
|
|
|
10338
10477
|
/**
|
|
10339
10478
|
* Ensures a directory exists at the given path
|
|
10340
10479
|
*/
|
|
10341
|
-
ensureDirectory(parent, name) {
|
|
10480
|
+
ensureDirectory(parent, name, description) {
|
|
10342
10481
|
if (!parent.children) {
|
|
10343
10482
|
parent.children = /* @__PURE__ */ new Map();
|
|
10344
10483
|
}
|
|
@@ -10347,9 +10486,12 @@ var ExplorerService = class {
|
|
|
10347
10486
|
child = {
|
|
10348
10487
|
type: "directory",
|
|
10349
10488
|
name,
|
|
10489
|
+
description,
|
|
10350
10490
|
children: /* @__PURE__ */ new Map()
|
|
10351
10491
|
};
|
|
10352
10492
|
parent.children.set(name, child);
|
|
10493
|
+
} else if (description && !child.description) {
|
|
10494
|
+
child.description = description;
|
|
10353
10495
|
}
|
|
10354
10496
|
return child;
|
|
10355
10497
|
}
|
|
@@ -10370,9 +10512,12 @@ var ExplorerService = class {
|
|
|
10370
10512
|
});
|
|
10371
10513
|
}
|
|
10372
10514
|
/**
|
|
10373
|
-
* Explores the filesystem at the given path
|
|
10515
|
+
* Explores the filesystem at the given path.
|
|
10516
|
+
* Tool rules are automatically applied from the request scope.
|
|
10517
|
+
* @param path - The path to explore
|
|
10374
10518
|
*/
|
|
10375
10519
|
explore(path) {
|
|
10520
|
+
const ctx = this.getFilterContext();
|
|
10376
10521
|
const normalizedPath = this.normalizePath(path);
|
|
10377
10522
|
const segments = normalizedPath === "/" ? [] : normalizedPath.split("/").filter((s) => s);
|
|
10378
10523
|
let current = this.root;
|
|
@@ -10388,10 +10533,21 @@ var ExplorerService = class {
|
|
|
10388
10533
|
const items = [];
|
|
10389
10534
|
if (current.children) {
|
|
10390
10535
|
for (const [name, node] of current.children) {
|
|
10391
|
-
|
|
10536
|
+
if (node.type === "directory") {
|
|
10537
|
+
if (!this.isDirectoryAllowed(name, currentPath, ctx)) continue;
|
|
10538
|
+
} else if (node.type === "function" && node.functionDef) {
|
|
10539
|
+
if (!ctx.allowedTools.has(`${node.functionDef.group}:${name}`)) continue;
|
|
10540
|
+
}
|
|
10541
|
+
const item = {
|
|
10392
10542
|
name,
|
|
10393
10543
|
type: node.type
|
|
10394
|
-
}
|
|
10544
|
+
};
|
|
10545
|
+
if (node.description) {
|
|
10546
|
+
item.description = node.description;
|
|
10547
|
+
} else if (node.type === "function" && node.functionDef?.func.description) {
|
|
10548
|
+
item.description = node.functionDef.func.description;
|
|
10549
|
+
}
|
|
10550
|
+
items.push(item);
|
|
10395
10551
|
}
|
|
10396
10552
|
}
|
|
10397
10553
|
items.sort((a, b) => {
|
|
@@ -10410,6 +10566,9 @@ var ExplorerService = class {
|
|
|
10410
10566
|
return null;
|
|
10411
10567
|
}
|
|
10412
10568
|
const { func, group } = current.functionDef;
|
|
10569
|
+
if (!ctx.allowedTools.has(`${group}:${func.name}`)) {
|
|
10570
|
+
return null;
|
|
10571
|
+
}
|
|
10413
10572
|
const definition = this.generateFunctionDefinition(func, group);
|
|
10414
10573
|
return {
|
|
10415
10574
|
type: "function",
|
|
@@ -10672,77 +10831,90 @@ async function handleHTTPRequest(req, res, deps, responseHeaders) {
|
|
|
10672
10831
|
});
|
|
10673
10832
|
const headers = /* @__PURE__ */ new Map();
|
|
10674
10833
|
responseHeaders.set(req, headers);
|
|
10675
|
-
|
|
10676
|
-
if (req.method === "POST" || req.method === "PUT") {
|
|
10677
|
-
const reqWithBody = req;
|
|
10678
|
-
if (reqWithBody.body !== void 0) {
|
|
10679
|
-
ctx.body = reqWithBody.body;
|
|
10680
|
-
} else {
|
|
10681
|
-
ctx.body = await parseBody(req);
|
|
10682
|
-
}
|
|
10683
|
-
}
|
|
10684
|
-
await runMiddleware(ctx, deps.middleware, deps.routeHandler);
|
|
10834
|
+
if (deps.toolRulesProvider) {
|
|
10685
10835
|
try {
|
|
10686
|
-
|
|
10687
|
-
|
|
10688
|
-
|
|
10689
|
-
|
|
10690
|
-
headers.set("X-ATP-Token", newToken);
|
|
10691
|
-
headers.set("X-ATP-Token-Expires", expiresAt.toString());
|
|
10692
|
-
} catch (error) {
|
|
10693
|
-
}
|
|
10694
|
-
}
|
|
10695
|
-
const isStringResponse = typeof ctx.responseBody === "string";
|
|
10696
|
-
res.writeHead(ctx.status, {
|
|
10697
|
-
"Content-Type": isStringResponse ? "text/plain; charset=utf-8" : "application/json",
|
|
10698
|
-
...Object.fromEntries(headers)
|
|
10836
|
+
ctx.toolRules = deps.toolRulesProvider(ctx);
|
|
10837
|
+
} catch (error) {
|
|
10838
|
+
atpRuntime.log.warn("Tool rules provider failed", {
|
|
10839
|
+
error
|
|
10699
10840
|
});
|
|
10700
|
-
res.end(isStringResponse ? ctx.responseBody : JSON.stringify(ctx.responseBody));
|
|
10701
|
-
} catch (writeError) {
|
|
10702
10841
|
}
|
|
10703
|
-
}
|
|
10842
|
+
}
|
|
10843
|
+
await runInRequestScope({
|
|
10844
|
+
toolRules: ctx.toolRules
|
|
10845
|
+
}, async () => {
|
|
10704
10846
|
try {
|
|
10705
|
-
if (
|
|
10706
|
-
|
|
10707
|
-
|
|
10708
|
-
|
|
10709
|
-
|
|
10710
|
-
|
|
10711
|
-
atpRuntime.log.debug("Token refresh headers set on error", {
|
|
10712
|
-
clientId: ctx.clientId,
|
|
10713
|
-
path: ctx.path,
|
|
10714
|
-
hasSessionManager: !!deps.sessionManager,
|
|
10715
|
-
headerCount: headers.size
|
|
10716
|
-
});
|
|
10717
|
-
} catch (tokenError) {
|
|
10718
|
-
atpRuntime.log.warn("Token refresh failed on error", {
|
|
10719
|
-
error: tokenError
|
|
10720
|
-
});
|
|
10847
|
+
if (req.method === "POST" || req.method === "PUT") {
|
|
10848
|
+
const reqWithBody = req;
|
|
10849
|
+
if (reqWithBody.body !== void 0) {
|
|
10850
|
+
ctx.body = reqWithBody.body;
|
|
10851
|
+
} else {
|
|
10852
|
+
ctx.body = await parseBody(req);
|
|
10721
10853
|
}
|
|
10722
|
-
}
|
|
10723
|
-
|
|
10724
|
-
|
|
10725
|
-
|
|
10726
|
-
|
|
10854
|
+
}
|
|
10855
|
+
await runMiddleware(ctx, deps.middleware, deps.routeHandler);
|
|
10856
|
+
try {
|
|
10857
|
+
if (ctx.clientId && deps.sessionManager && ctx.path !== "/api/init") {
|
|
10858
|
+
try {
|
|
10859
|
+
const newToken = deps.sessionManager.generateToken(ctx.clientId);
|
|
10860
|
+
const expiresAt = Date.now() + 60 * 60 * 1e3;
|
|
10861
|
+
headers.set("X-ATP-Token", newToken);
|
|
10862
|
+
headers.set("X-ATP-Token-Expires", expiresAt.toString());
|
|
10863
|
+
} catch (error) {
|
|
10864
|
+
}
|
|
10865
|
+
}
|
|
10866
|
+
const isStringResponse = typeof ctx.responseBody === "string";
|
|
10867
|
+
res.writeHead(ctx.status, {
|
|
10868
|
+
"Content-Type": isStringResponse ? "text/plain; charset=utf-8" : "application/json",
|
|
10869
|
+
...Object.fromEntries(headers)
|
|
10727
10870
|
});
|
|
10871
|
+
res.end(isStringResponse ? ctx.responseBody : JSON.stringify(ctx.responseBody));
|
|
10872
|
+
} catch (writeError) {
|
|
10728
10873
|
}
|
|
10729
|
-
|
|
10730
|
-
} catch (handlerError) {
|
|
10874
|
+
} catch (error) {
|
|
10731
10875
|
try {
|
|
10732
|
-
if (
|
|
10733
|
-
|
|
10734
|
-
|
|
10876
|
+
if (ctx.clientId && deps.sessionManager && ctx.path !== "/api/init") {
|
|
10877
|
+
try {
|
|
10878
|
+
const newToken = deps.sessionManager.generateToken(ctx.clientId);
|
|
10879
|
+
const expiresAt = Date.now() + 60 * 60 * 1e3;
|
|
10880
|
+
headers.set("X-ATP-Token", newToken);
|
|
10881
|
+
headers.set("X-ATP-Token-Expires", expiresAt.toString());
|
|
10882
|
+
atpRuntime.log.debug("Token refresh headers set on error", {
|
|
10883
|
+
clientId: ctx.clientId,
|
|
10884
|
+
path: ctx.path,
|
|
10885
|
+
hasSessionManager: !!deps.sessionManager,
|
|
10886
|
+
headerCount: headers.size
|
|
10887
|
+
});
|
|
10888
|
+
} catch (tokenError) {
|
|
10889
|
+
atpRuntime.log.warn("Token refresh failed on error", {
|
|
10890
|
+
error: tokenError
|
|
10891
|
+
});
|
|
10892
|
+
}
|
|
10893
|
+
} else {
|
|
10894
|
+
atpRuntime.log.debug("Token refresh skipped on error", {
|
|
10895
|
+
hasClientId: !!ctx.clientId,
|
|
10896
|
+
hasSessionManager: !!deps.sessionManager,
|
|
10897
|
+
path: ctx.path
|
|
10735
10898
|
});
|
|
10736
|
-
res.end(JSON.stringify({
|
|
10737
|
-
error: "Internal server error"
|
|
10738
|
-
}));
|
|
10739
10899
|
}
|
|
10740
|
-
|
|
10900
|
+
handleError(res, error, nanoid.nanoid(), headers);
|
|
10901
|
+
} catch (handlerError) {
|
|
10902
|
+
try {
|
|
10903
|
+
if (!res.headersSent) {
|
|
10904
|
+
res.writeHead(500, {
|
|
10905
|
+
"Content-Type": "application/json"
|
|
10906
|
+
});
|
|
10907
|
+
res.end(JSON.stringify({
|
|
10908
|
+
error: "Internal server error"
|
|
10909
|
+
}));
|
|
10910
|
+
}
|
|
10911
|
+
} catch {
|
|
10912
|
+
}
|
|
10741
10913
|
}
|
|
10914
|
+
} finally {
|
|
10915
|
+
responseHeaders.delete(req);
|
|
10742
10916
|
}
|
|
10743
|
-
}
|
|
10744
|
-
responseHeaders.delete(req);
|
|
10745
|
-
}
|
|
10917
|
+
});
|
|
10746
10918
|
}
|
|
10747
10919
|
__name(handleHTTPRequest, "handleHTTPRequest");
|
|
10748
10920
|
async function runMiddleware(ctx, middleware, routeHandler) {
|
|
@@ -11057,6 +11229,29 @@ async function handleExecute(ctx, executor, stateManager, config, auditSink, ses
|
|
|
11057
11229
|
} catch (error) {
|
|
11058
11230
|
}
|
|
11059
11231
|
}
|
|
11232
|
+
const onToolCall = auditSink ? (event) => {
|
|
11233
|
+
const auditEvent = {
|
|
11234
|
+
eventId: nanoid.nanoid(),
|
|
11235
|
+
timestamp: Date.now(),
|
|
11236
|
+
clientId: ctx.clientId || "anonymous",
|
|
11237
|
+
eventType: "tool_call",
|
|
11238
|
+
action: "complete",
|
|
11239
|
+
toolName: event.toolName,
|
|
11240
|
+
apiGroup: event.apiGroup,
|
|
11241
|
+
input: event.input,
|
|
11242
|
+
output: event.output,
|
|
11243
|
+
status: event.success ? "success" : "failed",
|
|
11244
|
+
error: event.error ? {
|
|
11245
|
+
message: event.error.message,
|
|
11246
|
+
stack: event.error.stack
|
|
11247
|
+
} : void 0,
|
|
11248
|
+
metadata: {
|
|
11249
|
+
duration: event.duration
|
|
11250
|
+
}
|
|
11251
|
+
};
|
|
11252
|
+
auditSink.write(auditEvent).catch(() => {
|
|
11253
|
+
});
|
|
11254
|
+
} : void 0;
|
|
11060
11255
|
const executionConfig = {
|
|
11061
11256
|
timeout: requestConfig.timeout || config.execution.timeout,
|
|
11062
11257
|
maxMemory: memoryInBytes,
|
|
@@ -11067,7 +11262,8 @@ async function handleExecute(ctx, executor, stateManager, config, auditSink, ses
|
|
|
11067
11262
|
provenanceMode: requestConfig.provenanceMode || config.execution.provenanceMode || atpProtocol.ProvenanceMode.NONE,
|
|
11068
11263
|
securityPolicies: config.execution.securityPolicies || [],
|
|
11069
11264
|
provenanceHints: requestConfig.provenanceHints,
|
|
11070
|
-
requestContext: requestConfig.requestContext
|
|
11265
|
+
requestContext: requestConfig.requestContext,
|
|
11266
|
+
onToolCall
|
|
11071
11267
|
};
|
|
11072
11268
|
let hintMap;
|
|
11073
11269
|
const prelimExecutionId = nanoid.nanoid();
|
|
@@ -11734,11 +11930,12 @@ export { api };
|
|
|
11734
11930
|
|
|
11735
11931
|
// src/handlers/definitions.handler.ts
|
|
11736
11932
|
async function getDefinitions(apiGroups) {
|
|
11737
|
-
const
|
|
11933
|
+
const filteredGroups = filterApiGroups(apiGroups);
|
|
11934
|
+
const aggregator = new APIAggregator(filteredGroups);
|
|
11738
11935
|
const typescript = await aggregator.generateTypeScript();
|
|
11739
11936
|
return {
|
|
11740
11937
|
typescript,
|
|
11741
|
-
apiGroups:
|
|
11938
|
+
apiGroups: filteredGroups.map((g) => g.name),
|
|
11742
11939
|
version: "1.0.0"
|
|
11743
11940
|
};
|
|
11744
11941
|
}
|
|
@@ -21346,6 +21543,7 @@ var AgentToolProtocolServer = class {
|
|
|
21346
21543
|
auditSink;
|
|
21347
21544
|
compiler;
|
|
21348
21545
|
customLogger;
|
|
21546
|
+
toolRulesProvider;
|
|
21349
21547
|
constructor(config = {}) {
|
|
21350
21548
|
const initialPolicies = config.execution?.securityPolicies ?? [];
|
|
21351
21549
|
this.policyRegistry = new atpProvenance.DynamicPolicyRegistry(initialPolicies);
|
|
@@ -21396,6 +21594,10 @@ var AgentToolProtocolServer = class {
|
|
|
21396
21594
|
},
|
|
21397
21595
|
logger: config.logger ?? "info"
|
|
21398
21596
|
};
|
|
21597
|
+
if (config.toolRulesProvider) {
|
|
21598
|
+
this.toolRulesProvider = config.toolRulesProvider;
|
|
21599
|
+
atpRuntime.log.info("Tool rules provider configured");
|
|
21600
|
+
}
|
|
21399
21601
|
if (config.providers) {
|
|
21400
21602
|
if (config.providers.cache) {
|
|
21401
21603
|
this.cacheProvider = config.providers.cache;
|
|
@@ -21665,7 +21867,8 @@ var AgentToolProtocolServer = class {
|
|
|
21665
21867
|
customLogger: this.customLogger,
|
|
21666
21868
|
middleware: this.middleware,
|
|
21667
21869
|
routeHandler: /* @__PURE__ */ __name((ctx) => handleRoute(ctx, this), "routeHandler"),
|
|
21668
|
-
sessionManager: this.sessionManager
|
|
21870
|
+
sessionManager: this.sessionManager,
|
|
21871
|
+
toolRulesProvider: this.toolRulesProvider
|
|
21669
21872
|
}, this.responseHeaders).catch((error) => {
|
|
21670
21873
|
atpRuntime.log.error("Unhandled error in HTTP request handler:", error);
|
|
21671
21874
|
try {
|
|
@@ -21865,7 +22068,8 @@ var AgentToolProtocolServer = class {
|
|
|
21865
22068
|
customLogger: this.customLogger,
|
|
21866
22069
|
middleware: this.middleware,
|
|
21867
22070
|
routeHandler: /* @__PURE__ */ __name((ctx) => handleRoute(ctx, this), "routeHandler"),
|
|
21868
|
-
sessionManager: this.sessionManager
|
|
22071
|
+
sessionManager: this.sessionManager,
|
|
22072
|
+
toolRulesProvider: this.toolRulesProvider
|
|
21869
22073
|
}, this.responseHeaders);
|
|
21870
22074
|
};
|
|
21871
22075
|
}
|