@inkeep/agents-run-api 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{LocalSandboxExecutor-PQIRECLQ.js → LocalSandboxExecutor-2UQ32ZZH.js} +111 -6
- package/dist/{chunk-DQQRVSJS.js → chunk-QRT6EIUX.js} +6 -6
- package/dist/{conversations-JSORLLWS.js → conversations-OFIM2WLJ.js} +1 -1
- package/dist/index.cjs +705 -556
- package/dist/index.js +590 -543
- package/package.json +4 -3
- package/templates/v1/phase1/system-prompt.xml +1 -1
- package/templates/v1/shared/artifact-retrieval-guidance.xml +1 -1
package/dist/index.cjs
CHANGED
|
@@ -228,7 +228,7 @@ async function getScopedHistory({
|
|
|
228
228
|
let matchesAgent = true;
|
|
229
229
|
let matchesTask = true;
|
|
230
230
|
if (filters.subAgentId) {
|
|
231
|
-
matchesAgent = msg.role === "agent" && msg.visibility === "user-facing" || msg.
|
|
231
|
+
matchesAgent = msg.role === "agent" && msg.visibility === "user-facing" || msg.toSubAgentId === filters.subAgentId || msg.fromSubAgentId === filters.subAgentId;
|
|
232
232
|
}
|
|
233
233
|
if (filters.taskId) {
|
|
234
234
|
matchesTask = msg.taskId === filters.taskId || msg.a2aTaskId === filters.taskId;
|
|
@@ -303,12 +303,12 @@ async function getFormattedConversationHistory({
|
|
|
303
303
|
if (msg.role === "user") {
|
|
304
304
|
roleLabel = "user";
|
|
305
305
|
} else if (msg.role === "agent" && (msg.messageType === "a2a-request" || msg.messageType === "a2a-response")) {
|
|
306
|
-
const
|
|
307
|
-
const
|
|
308
|
-
roleLabel = `${
|
|
306
|
+
const fromSubAgent = msg.fromSubAgentId || msg.fromExternalAgentId || "unknown";
|
|
307
|
+
const toSubAgent = msg.toSubAgentId || msg.toExternalAgentId || "unknown";
|
|
308
|
+
roleLabel = `${fromSubAgent} to ${toSubAgent}`;
|
|
309
309
|
} else if (msg.role === "agent" && msg.messageType === "chat") {
|
|
310
|
-
const
|
|
311
|
-
roleLabel = `${
|
|
310
|
+
const fromSubAgent = msg.fromSubAgentId || "unknown";
|
|
311
|
+
roleLabel = `${fromSubAgent} to User`;
|
|
312
312
|
} else {
|
|
313
313
|
roleLabel = msg.role || "system";
|
|
314
314
|
}
|
|
@@ -389,10 +389,62 @@ var LocalSandboxExecutor_exports = {};
|
|
|
389
389
|
__export(LocalSandboxExecutor_exports, {
|
|
390
390
|
LocalSandboxExecutor: () => LocalSandboxExecutor
|
|
391
391
|
});
|
|
392
|
-
var logger17, _LocalSandboxExecutor, LocalSandboxExecutor;
|
|
392
|
+
var logger17, ExecutionSemaphore, _LocalSandboxExecutor, LocalSandboxExecutor;
|
|
393
393
|
var init_LocalSandboxExecutor = __esm({
|
|
394
394
|
"src/tools/LocalSandboxExecutor.ts"() {
|
|
395
395
|
logger17 = agentsCore.getLogger("local-sandbox-executor");
|
|
396
|
+
ExecutionSemaphore = class {
|
|
397
|
+
constructor(permits, maxWaitTimeMs = 3e4) {
|
|
398
|
+
__publicField(this, "permits");
|
|
399
|
+
__publicField(this, "waitQueue", []);
|
|
400
|
+
__publicField(this, "maxWaitTime");
|
|
401
|
+
this.permits = Math.max(1, permits);
|
|
402
|
+
this.maxWaitTime = maxWaitTimeMs;
|
|
403
|
+
}
|
|
404
|
+
async acquire(fn) {
|
|
405
|
+
await new Promise((resolve, reject) => {
|
|
406
|
+
if (this.permits > 0) {
|
|
407
|
+
this.permits--;
|
|
408
|
+
resolve();
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const timeoutId = setTimeout(() => {
|
|
412
|
+
const index = this.waitQueue.findIndex((item) => item.resolve === resolve);
|
|
413
|
+
if (index !== -1) {
|
|
414
|
+
this.waitQueue.splice(index, 1);
|
|
415
|
+
reject(
|
|
416
|
+
new Error(
|
|
417
|
+
`Function execution queue timeout after ${this.maxWaitTime}ms. Too many concurrent executions.`
|
|
418
|
+
)
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
}, this.maxWaitTime);
|
|
422
|
+
this.waitQueue.push({
|
|
423
|
+
resolve: () => {
|
|
424
|
+
clearTimeout(timeoutId);
|
|
425
|
+
this.permits--;
|
|
426
|
+
resolve();
|
|
427
|
+
},
|
|
428
|
+
reject
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
try {
|
|
432
|
+
return await fn();
|
|
433
|
+
} finally {
|
|
434
|
+
this.permits++;
|
|
435
|
+
const next = this.waitQueue.shift();
|
|
436
|
+
if (next) {
|
|
437
|
+
next.resolve();
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
getAvailablePermits() {
|
|
442
|
+
return this.permits;
|
|
443
|
+
}
|
|
444
|
+
getQueueLength() {
|
|
445
|
+
return this.waitQueue.length;
|
|
446
|
+
}
|
|
447
|
+
};
|
|
396
448
|
_LocalSandboxExecutor = class _LocalSandboxExecutor {
|
|
397
449
|
constructor() {
|
|
398
450
|
__publicField(this, "tempDir");
|
|
@@ -400,6 +452,7 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
400
452
|
__publicField(this, "POOL_TTL", 5 * 60 * 1e3);
|
|
401
453
|
// 5 minutes
|
|
402
454
|
__publicField(this, "MAX_USE_COUNT", 50);
|
|
455
|
+
__publicField(this, "executionSemaphores", /* @__PURE__ */ new Map());
|
|
403
456
|
this.tempDir = path.join(os.tmpdir(), "inkeep-sandboxes");
|
|
404
457
|
this.ensureTempDir();
|
|
405
458
|
this.startPoolCleanup();
|
|
@@ -410,6 +463,34 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
410
463
|
}
|
|
411
464
|
return _LocalSandboxExecutor.instance;
|
|
412
465
|
}
|
|
466
|
+
/**
|
|
467
|
+
* Get or create a semaphore for the specified vCPU limit
|
|
468
|
+
*/
|
|
469
|
+
getSemaphore(vcpus) {
|
|
470
|
+
const effectiveVcpus = Math.max(1, vcpus || 1);
|
|
471
|
+
if (!this.executionSemaphores.has(effectiveVcpus)) {
|
|
472
|
+
logger17.debug({ vcpus: effectiveVcpus }, "Creating new execution semaphore");
|
|
473
|
+
this.executionSemaphores.set(effectiveVcpus, new ExecutionSemaphore(effectiveVcpus));
|
|
474
|
+
}
|
|
475
|
+
const semaphore = this.executionSemaphores.get(effectiveVcpus);
|
|
476
|
+
if (!semaphore) {
|
|
477
|
+
throw new Error(`Failed to create semaphore for ${effectiveVcpus} vCPUs`);
|
|
478
|
+
}
|
|
479
|
+
return semaphore;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Get execution statistics for monitoring and debugging
|
|
483
|
+
*/
|
|
484
|
+
getExecutionStats() {
|
|
485
|
+
const stats = {};
|
|
486
|
+
for (const [vcpus, semaphore] of this.executionSemaphores.entries()) {
|
|
487
|
+
stats[`vcpu_${vcpus}`] = {
|
|
488
|
+
availablePermits: semaphore.getAvailablePermits(),
|
|
489
|
+
queueLength: semaphore.getQueueLength()
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
return stats;
|
|
493
|
+
}
|
|
413
494
|
ensureTempDir() {
|
|
414
495
|
try {
|
|
415
496
|
fs.mkdirSync(this.tempDir, { recursive: true });
|
|
@@ -484,7 +565,7 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
484
565
|
}
|
|
485
566
|
}, 6e4);
|
|
486
567
|
}
|
|
487
|
-
detectModuleType(executeCode) {
|
|
568
|
+
detectModuleType(executeCode, configuredRuntime) {
|
|
488
569
|
const esmPatterns = [
|
|
489
570
|
/import\s+.*\s+from\s+['"]/g,
|
|
490
571
|
// import ... from '...'
|
|
@@ -505,6 +586,9 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
505
586
|
];
|
|
506
587
|
const hasEsmSyntax = esmPatterns.some((pattern) => pattern.test(executeCode));
|
|
507
588
|
const hasCjsSyntax = cjsPatterns.some((pattern) => pattern.test(executeCode));
|
|
589
|
+
if (configuredRuntime === "typescript") {
|
|
590
|
+
return hasCjsSyntax ? "cjs" : "esm";
|
|
591
|
+
}
|
|
508
592
|
if (hasEsmSyntax && hasCjsSyntax) {
|
|
509
593
|
logger17.warn(
|
|
510
594
|
{ executeCode: `${executeCode.substring(0, 100)}...` },
|
|
@@ -521,6 +605,24 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
521
605
|
return "cjs";
|
|
522
606
|
}
|
|
523
607
|
async executeFunctionTool(toolId, args, config) {
|
|
608
|
+
const vcpus = config.sandboxConfig?.vcpus || 1;
|
|
609
|
+
const semaphore = this.getSemaphore(vcpus);
|
|
610
|
+
logger17.debug(
|
|
611
|
+
{
|
|
612
|
+
toolId,
|
|
613
|
+
vcpus,
|
|
614
|
+
availablePermits: semaphore.getAvailablePermits(),
|
|
615
|
+
queueLength: semaphore.getQueueLength(),
|
|
616
|
+
sandboxConfig: config.sandboxConfig,
|
|
617
|
+
poolSize: Object.keys(this.sandboxPool).length
|
|
618
|
+
},
|
|
619
|
+
"Acquiring execution slot for function tool"
|
|
620
|
+
);
|
|
621
|
+
return semaphore.acquire(async () => {
|
|
622
|
+
return this.executeInSandbox_Internal(toolId, args, config);
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
async executeInSandbox_Internal(toolId, args, config) {
|
|
524
626
|
const dependencies = config.dependencies || {};
|
|
525
627
|
const dependencyHash = this.generateDependencyHash(dependencies);
|
|
526
628
|
logger17.debug(
|
|
@@ -528,6 +630,7 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
528
630
|
toolId,
|
|
529
631
|
dependencies,
|
|
530
632
|
dependencyHash,
|
|
633
|
+
sandboxConfig: config.sandboxConfig,
|
|
531
634
|
poolSize: Object.keys(this.sandboxPool).length
|
|
532
635
|
},
|
|
533
636
|
"Executing function tool"
|
|
@@ -547,7 +650,7 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
547
650
|
},
|
|
548
651
|
"Creating new sandbox"
|
|
549
652
|
);
|
|
550
|
-
const moduleType = this.detectModuleType(config.executeCode);
|
|
653
|
+
const moduleType = this.detectModuleType(config.executeCode, config.sandboxConfig?.runtime);
|
|
551
654
|
const packageJson = {
|
|
552
655
|
name: `function-tool-${toolId}`,
|
|
553
656
|
version: "1.0.0",
|
|
@@ -564,14 +667,15 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
564
667
|
this.addToPool(dependencyHash, sandboxDir, dependencies);
|
|
565
668
|
}
|
|
566
669
|
try {
|
|
567
|
-
const moduleType = this.detectModuleType(config.executeCode);
|
|
670
|
+
const moduleType = this.detectModuleType(config.executeCode, config.sandboxConfig?.runtime);
|
|
568
671
|
const executionCode = this.wrapFunctionCode(config.executeCode, args);
|
|
569
672
|
const fileExtension = moduleType === "esm" ? "mjs" : "js";
|
|
570
673
|
fs.writeFileSync(path.join(sandboxDir, `index.${fileExtension}`), executionCode, "utf8");
|
|
571
674
|
const result = await this.executeInSandbox(
|
|
572
675
|
sandboxDir,
|
|
573
676
|
config.sandboxConfig?.timeout || 3e4,
|
|
574
|
-
moduleType
|
|
677
|
+
moduleType,
|
|
678
|
+
config.sandboxConfig
|
|
575
679
|
);
|
|
576
680
|
return result;
|
|
577
681
|
} catch (error) {
|
|
@@ -610,7 +714,7 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
610
714
|
});
|
|
611
715
|
});
|
|
612
716
|
}
|
|
613
|
-
async executeInSandbox(sandboxDir, timeout, moduleType) {
|
|
717
|
+
async executeInSandbox(sandboxDir, timeout, moduleType, _sandboxConfig) {
|
|
614
718
|
return new Promise((resolve, reject) => {
|
|
615
719
|
const fileExtension = moduleType === "esm" ? "mjs" : "js";
|
|
616
720
|
const spawnOptions = {
|
|
@@ -648,12 +752,13 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
648
752
|
const timeoutId = setTimeout(() => {
|
|
649
753
|
logger17.warn({ sandboxDir, timeout }, "Function execution timed out, killing process");
|
|
650
754
|
node.kill("SIGTERM");
|
|
755
|
+
const forceKillTimeout = Math.min(Math.max(timeout / 10, 2e3), 5e3);
|
|
651
756
|
setTimeout(() => {
|
|
652
757
|
try {
|
|
653
758
|
node.kill("SIGKILL");
|
|
654
759
|
} catch {
|
|
655
760
|
}
|
|
656
|
-
},
|
|
761
|
+
}, forceKillTimeout);
|
|
657
762
|
reject(new Error(`Function execution timed out after ${timeout}ms`));
|
|
658
763
|
}, timeout);
|
|
659
764
|
node.on("close", (code, signal) => {
|
|
@@ -819,7 +924,7 @@ function createExecutionContext(params) {
|
|
|
819
924
|
apiKey: params.apiKey,
|
|
820
925
|
tenantId: params.tenantId,
|
|
821
926
|
projectId: params.projectId,
|
|
822
|
-
|
|
927
|
+
agentId: params.agentId,
|
|
823
928
|
baseUrl: params.baseUrl || process.env.API_URL || "http://localhost:3003",
|
|
824
929
|
apiKeyId: params.apiKeyId,
|
|
825
930
|
subAgentId: params.subAgentId
|
|
@@ -836,8 +941,8 @@ var apiKeyAuth = () => factory.createMiddleware(async (c, next) => {
|
|
|
836
941
|
const authHeader = c.req.header("Authorization");
|
|
837
942
|
const tenantId = c.req.header("x-inkeep-tenant-id");
|
|
838
943
|
const projectId = c.req.header("x-inkeep-project-id");
|
|
839
|
-
const
|
|
840
|
-
const subAgentId = c.req.header("x-inkeep-agent-id");
|
|
944
|
+
const agentId = c.req.header("x-inkeep-agent-id");
|
|
945
|
+
const subAgentId = c.req.header("x-inkeep-sub-agent-id");
|
|
841
946
|
const proto = c.req.header("x-forwarded-proto")?.split(",")[0].trim();
|
|
842
947
|
const fwdHost = c.req.header("x-forwarded-host")?.split(",")[0].trim();
|
|
843
948
|
const host = fwdHost ?? c.req.header("host");
|
|
@@ -855,7 +960,7 @@ var apiKeyAuth = () => factory.createMiddleware(async (c, next) => {
|
|
|
855
960
|
apiKey: "development",
|
|
856
961
|
tenantId: tenantId || "test-tenant",
|
|
857
962
|
projectId: projectId || "test-project",
|
|
858
|
-
|
|
963
|
+
agentId: agentId || "test-agent",
|
|
859
964
|
apiKeyId: "test-key",
|
|
860
965
|
baseUrl,
|
|
861
966
|
subAgentId
|
|
@@ -870,7 +975,7 @@ var apiKeyAuth = () => factory.createMiddleware(async (c, next) => {
|
|
|
870
975
|
apiKey: "development",
|
|
871
976
|
tenantId: tenantId || "test-tenant",
|
|
872
977
|
projectId: projectId || "test-project",
|
|
873
|
-
|
|
978
|
+
agentId: agentId || "test-agent",
|
|
874
979
|
apiKeyId: "test-key",
|
|
875
980
|
baseUrl,
|
|
876
981
|
subAgentId
|
|
@@ -892,16 +997,16 @@ var apiKeyAuth = () => factory.createMiddleware(async (c, next) => {
|
|
|
892
997
|
const apiKey = authHeader.substring(7);
|
|
893
998
|
if (env.INKEEP_AGENTS_RUN_API_BYPASS_SECRET) {
|
|
894
999
|
if (apiKey === env.INKEEP_AGENTS_RUN_API_BYPASS_SECRET) {
|
|
895
|
-
if (!tenantId || !projectId || !
|
|
1000
|
+
if (!tenantId || !projectId || !agentId) {
|
|
896
1001
|
throw new httpException.HTTPException(401, {
|
|
897
|
-
message: "Missing or invalid tenant, project, or
|
|
1002
|
+
message: "Missing or invalid tenant, project, or agent ID"
|
|
898
1003
|
});
|
|
899
1004
|
}
|
|
900
1005
|
const executionContext = createExecutionContext({
|
|
901
1006
|
apiKey,
|
|
902
1007
|
tenantId,
|
|
903
1008
|
projectId,
|
|
904
|
-
|
|
1009
|
+
agentId,
|
|
905
1010
|
apiKeyId: "bypass",
|
|
906
1011
|
baseUrl,
|
|
907
1012
|
subAgentId
|
|
@@ -936,7 +1041,7 @@ var apiKeyAuth = () => factory.createMiddleware(async (c, next) => {
|
|
|
936
1041
|
{
|
|
937
1042
|
tenantId: executionContext.tenantId,
|
|
938
1043
|
projectId: executionContext.projectId,
|
|
939
|
-
|
|
1044
|
+
agentId: executionContext.agentId,
|
|
940
1045
|
subAgentId: executionContext.subAgentId
|
|
941
1046
|
},
|
|
942
1047
|
"API key authenticated successfully"
|
|
@@ -963,7 +1068,7 @@ var extractContextFromApiKey = async (apiKey, baseUrl) => {
|
|
|
963
1068
|
apiKey,
|
|
964
1069
|
tenantId: apiKeyRecord.tenantId,
|
|
965
1070
|
projectId: apiKeyRecord.projectId,
|
|
966
|
-
|
|
1071
|
+
agentId: apiKeyRecord.agentId,
|
|
967
1072
|
apiKeyId: apiKeyRecord.id,
|
|
968
1073
|
baseUrl
|
|
969
1074
|
});
|
|
@@ -1066,7 +1171,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1066
1171
|
try {
|
|
1067
1172
|
const params = request.params;
|
|
1068
1173
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
1069
|
-
const {
|
|
1174
|
+
const { agentId } = executionContext;
|
|
1070
1175
|
const task = {
|
|
1071
1176
|
id: nanoid.nanoid(),
|
|
1072
1177
|
input: {
|
|
@@ -1080,7 +1185,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1080
1185
|
conversationId: params.message.contextId,
|
|
1081
1186
|
metadata: {
|
|
1082
1187
|
blocking: params.configuration?.blocking ?? false,
|
|
1083
|
-
custom: {
|
|
1188
|
+
custom: { agent_id: agentId || "" },
|
|
1084
1189
|
// Pass through streaming metadata from the original message
|
|
1085
1190
|
...params.message.metadata
|
|
1086
1191
|
}
|
|
@@ -1141,7 +1246,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1141
1246
|
id: task.id,
|
|
1142
1247
|
tenantId: agent.tenantId,
|
|
1143
1248
|
projectId: agent.projectId,
|
|
1144
|
-
|
|
1249
|
+
agentId: agentId || "",
|
|
1145
1250
|
contextId: effectiveContextId,
|
|
1146
1251
|
status: "working",
|
|
1147
1252
|
metadata: {
|
|
@@ -1149,8 +1254,8 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1149
1254
|
message_id: params.message.messageId || "",
|
|
1150
1255
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1151
1256
|
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1152
|
-
|
|
1153
|
-
|
|
1257
|
+
sub_agent_id: agent.subAgentId,
|
|
1258
|
+
agent_id: agentId || "",
|
|
1154
1259
|
stream_request_id: params.message.metadata?.stream_request_id
|
|
1155
1260
|
},
|
|
1156
1261
|
subAgentId: agent.subAgentId,
|
|
@@ -1158,7 +1263,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1158
1263
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1159
1264
|
});
|
|
1160
1265
|
logger3.info({ metadata: params.message.metadata }, "message metadata");
|
|
1161
|
-
if (params.message.metadata?.
|
|
1266
|
+
if (params.message.metadata?.fromSubAgentId || params.message.metadata?.fromExternalAgentId) {
|
|
1162
1267
|
const messageText = params.message.parts.filter((part) => part.kind === "text" && "text" in part && part.text).map((part) => part.text).join(" ");
|
|
1163
1268
|
try {
|
|
1164
1269
|
const messageData = {
|
|
@@ -1174,19 +1279,19 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1174
1279
|
messageType: "a2a-request",
|
|
1175
1280
|
taskId: task.id
|
|
1176
1281
|
};
|
|
1177
|
-
if (params.message.metadata?.
|
|
1178
|
-
messageData.
|
|
1179
|
-
messageData.
|
|
1282
|
+
if (params.message.metadata?.fromSubAgentId) {
|
|
1283
|
+
messageData.fromSubAgentId = params.message.metadata.fromSubAgentId;
|
|
1284
|
+
messageData.toSubAgentId = agent.subAgentId;
|
|
1180
1285
|
} else if (params.message.metadata?.fromExternalAgentId) {
|
|
1181
1286
|
messageData.fromExternalAgentId = params.message.metadata.fromExternalAgentId;
|
|
1182
|
-
messageData.
|
|
1287
|
+
messageData.toSubAgentId = agent.subAgentId;
|
|
1183
1288
|
}
|
|
1184
1289
|
await agentsCore.createMessage(dbClient_default)(messageData);
|
|
1185
1290
|
logger3.info(
|
|
1186
1291
|
{
|
|
1187
|
-
|
|
1292
|
+
fromSubAgentId: params.message.metadata.fromSubAgentId,
|
|
1188
1293
|
fromExternalAgentId: params.message.metadata.fromExternalAgentId,
|
|
1189
|
-
|
|
1294
|
+
toSubAgentId: agent.subAgentId,
|
|
1190
1295
|
conversationId: effectiveContextId,
|
|
1191
1296
|
messageType: "a2a-request",
|
|
1192
1297
|
taskId: task.id
|
|
@@ -1197,9 +1302,9 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1197
1302
|
logger3.error(
|
|
1198
1303
|
{
|
|
1199
1304
|
error,
|
|
1200
|
-
|
|
1305
|
+
fromSubAgentId: params.message.metadata.fromSubAgentId,
|
|
1201
1306
|
fromExternalAgentId: params.message.metadata.fromExternalAgentId,
|
|
1202
|
-
|
|
1307
|
+
toSubAgentId: agent.subAgentId,
|
|
1203
1308
|
conversationId: effectiveContextId
|
|
1204
1309
|
},
|
|
1205
1310
|
"Failed to store A2A message in database"
|
|
@@ -1216,8 +1321,8 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1216
1321
|
message_id: params.message.messageId || "",
|
|
1217
1322
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1218
1323
|
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1219
|
-
|
|
1220
|
-
|
|
1324
|
+
sub_agent_id: agent.subAgentId,
|
|
1325
|
+
agent_id: agentId || ""
|
|
1221
1326
|
}
|
|
1222
1327
|
}
|
|
1223
1328
|
});
|
|
@@ -1250,7 +1355,8 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1250
1355
|
kind: "data",
|
|
1251
1356
|
data: {
|
|
1252
1357
|
type: "transfer",
|
|
1253
|
-
targetSubAgentId: transferPart.data.
|
|
1358
|
+
targetSubAgentId: transferPart.data.targetSubAgentId,
|
|
1359
|
+
fromSubAgentId: transferPart.data.fromSubAgentId
|
|
1254
1360
|
}
|
|
1255
1361
|
},
|
|
1256
1362
|
{
|
|
@@ -1318,7 +1424,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
1318
1424
|
try {
|
|
1319
1425
|
const params = request.params;
|
|
1320
1426
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
1321
|
-
const {
|
|
1427
|
+
const { agentId } = executionContext;
|
|
1322
1428
|
if (!agent.agentCard.capabilities.streaming) {
|
|
1323
1429
|
return c.json({
|
|
1324
1430
|
jsonrpc: "2.0",
|
|
@@ -1343,7 +1449,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
1343
1449
|
metadata: {
|
|
1344
1450
|
blocking: false,
|
|
1345
1451
|
// Streaming is always non-blocking
|
|
1346
|
-
custom: {
|
|
1452
|
+
custom: { agent_id: agentId || "" }
|
|
1347
1453
|
}
|
|
1348
1454
|
}
|
|
1349
1455
|
};
|
|
@@ -1382,7 +1488,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
1382
1488
|
jsonrpc: "2.0",
|
|
1383
1489
|
result: {
|
|
1384
1490
|
type: "transfer",
|
|
1385
|
-
target: transferPart.data.
|
|
1491
|
+
target: transferPart.data.targetSubAgentId,
|
|
1386
1492
|
task_id: task.id,
|
|
1387
1493
|
reason: transferPart.data.reason || "Agent requested transfer",
|
|
1388
1494
|
original_message: transferPart.data.original_message,
|
|
@@ -1614,129 +1720,6 @@ async function handleTasksResubscribe(c, agent, request) {
|
|
|
1614
1720
|
}
|
|
1615
1721
|
}
|
|
1616
1722
|
init_dbClient();
|
|
1617
|
-
function createAgentCard({
|
|
1618
|
-
dbAgent,
|
|
1619
|
-
baseUrl
|
|
1620
|
-
}) {
|
|
1621
|
-
const description = dbAgent.description || "AI Agent";
|
|
1622
|
-
return {
|
|
1623
|
-
name: dbAgent.name,
|
|
1624
|
-
description,
|
|
1625
|
-
url: baseUrl ? `${baseUrl}/a2a` : "",
|
|
1626
|
-
version: "1.0.0",
|
|
1627
|
-
capabilities: {
|
|
1628
|
-
streaming: true,
|
|
1629
|
-
// Enable streaming for A2A compliance
|
|
1630
|
-
pushNotifications: false,
|
|
1631
|
-
stateTransitionHistory: false
|
|
1632
|
-
},
|
|
1633
|
-
defaultInputModes: ["text", "text/plain"],
|
|
1634
|
-
defaultOutputModes: ["text", "text/plain"],
|
|
1635
|
-
skills: [],
|
|
1636
|
-
// Add provider info if available
|
|
1637
|
-
...baseUrl && {
|
|
1638
|
-
provider: {
|
|
1639
|
-
organization: "Inkeep",
|
|
1640
|
-
url: baseUrl
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
};
|
|
1644
|
-
}
|
|
1645
|
-
function generateDescriptionWithTransfers(baseDescription, internalRelations, externalRelations) {
|
|
1646
|
-
const transfers = [
|
|
1647
|
-
...internalRelations.filter((rel) => rel.relationType === "transfer"),
|
|
1648
|
-
...externalRelations.filter((rel) => rel.relationType === "transfer")
|
|
1649
|
-
];
|
|
1650
|
-
const delegates = [
|
|
1651
|
-
...internalRelations.filter((rel) => rel.relationType === "delegate"),
|
|
1652
|
-
...externalRelations.filter((rel) => rel.relationType === "delegate")
|
|
1653
|
-
];
|
|
1654
|
-
if (transfers.length === 0 && delegates.length === 0) {
|
|
1655
|
-
return baseDescription;
|
|
1656
|
-
}
|
|
1657
|
-
let enhancedDescription = baseDescription;
|
|
1658
|
-
if (transfers.length > 0) {
|
|
1659
|
-
const transferList = transfers.map((rel) => {
|
|
1660
|
-
const name = rel.externalAgent?.name || rel.name;
|
|
1661
|
-
const desc = rel.externalAgent?.description || rel.description || "";
|
|
1662
|
-
return `- ${name}: ${desc}`;
|
|
1663
|
-
}).join("\n");
|
|
1664
|
-
enhancedDescription += `
|
|
1665
|
-
|
|
1666
|
-
Can transfer to:
|
|
1667
|
-
${transferList}`;
|
|
1668
|
-
}
|
|
1669
|
-
if (delegates.length > 0) {
|
|
1670
|
-
const delegateList = delegates.map((rel) => {
|
|
1671
|
-
const name = rel.externalAgent?.name || rel.name;
|
|
1672
|
-
const desc = rel.externalAgent?.description || rel.description || "";
|
|
1673
|
-
return `- ${name}: ${desc}`;
|
|
1674
|
-
}).join("\n");
|
|
1675
|
-
enhancedDescription += `
|
|
1676
|
-
|
|
1677
|
-
Can delegate to:
|
|
1678
|
-
${delegateList}`;
|
|
1679
|
-
}
|
|
1680
|
-
return enhancedDescription;
|
|
1681
|
-
}
|
|
1682
|
-
async function hydrateAgent({
|
|
1683
|
-
dbAgent,
|
|
1684
|
-
graphId,
|
|
1685
|
-
baseUrl,
|
|
1686
|
-
apiKey,
|
|
1687
|
-
credentialStoreRegistry
|
|
1688
|
-
}) {
|
|
1689
|
-
try {
|
|
1690
|
-
const taskHandlerConfig = await createTaskHandlerConfig({
|
|
1691
|
-
tenantId: dbAgent.tenantId,
|
|
1692
|
-
projectId: dbAgent.projectId,
|
|
1693
|
-
graphId,
|
|
1694
|
-
subAgentId: dbAgent.id,
|
|
1695
|
-
baseUrl,
|
|
1696
|
-
apiKey
|
|
1697
|
-
});
|
|
1698
|
-
const taskHandler = createTaskHandler(taskHandlerConfig, credentialStoreRegistry);
|
|
1699
|
-
const agentCard = createAgentCard({
|
|
1700
|
-
dbAgent,
|
|
1701
|
-
baseUrl
|
|
1702
|
-
});
|
|
1703
|
-
return {
|
|
1704
|
-
subAgentId: dbAgent.id,
|
|
1705
|
-
tenantId: dbAgent.tenantId,
|
|
1706
|
-
projectId: dbAgent.projectId,
|
|
1707
|
-
graphId,
|
|
1708
|
-
agentCard,
|
|
1709
|
-
taskHandler
|
|
1710
|
-
};
|
|
1711
|
-
} catch (error) {
|
|
1712
|
-
console.error(`\u274C Failed to hydrate agent ${dbAgent.id}:`, error);
|
|
1713
|
-
throw error;
|
|
1714
|
-
}
|
|
1715
|
-
}
|
|
1716
|
-
async function getRegisteredAgent(executionContext, credentialStoreRegistry) {
|
|
1717
|
-
const { tenantId, projectId, graphId, subAgentId, baseUrl, apiKey } = executionContext;
|
|
1718
|
-
if (!subAgentId) {
|
|
1719
|
-
throw new Error("Agent ID is required");
|
|
1720
|
-
}
|
|
1721
|
-
const dbAgent = await agentsCore.getSubAgentById(dbClient_default)({
|
|
1722
|
-
scopes: { tenantId, projectId, graphId },
|
|
1723
|
-
subAgentId
|
|
1724
|
-
});
|
|
1725
|
-
if (!dbAgent) {
|
|
1726
|
-
return null;
|
|
1727
|
-
}
|
|
1728
|
-
const agentFrameworkBaseUrl = `${baseUrl}/agents`;
|
|
1729
|
-
return hydrateAgent({
|
|
1730
|
-
dbAgent,
|
|
1731
|
-
graphId,
|
|
1732
|
-
baseUrl: agentFrameworkBaseUrl,
|
|
1733
|
-
credentialStoreRegistry,
|
|
1734
|
-
apiKey
|
|
1735
|
-
});
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
// src/agents/generateTaskHandler.ts
|
|
1739
|
-
init_dbClient();
|
|
1740
1723
|
init_logger();
|
|
1741
1724
|
|
|
1742
1725
|
// src/agents/ModelFactory.ts
|
|
@@ -1955,7 +1938,7 @@ var _ToolSessionManager = class _ToolSessionManager {
|
|
|
1955
1938
|
return this.createSessionWithId(sessionId, tenantId, projectId, contextId, taskId);
|
|
1956
1939
|
}
|
|
1957
1940
|
/**
|
|
1958
|
-
* Create a new tool session with a specific ID (for coordination with
|
|
1941
|
+
* Create a new tool session with a specific ID (for coordination with AgentSession)
|
|
1959
1942
|
*/
|
|
1960
1943
|
createSessionWithId(sessionId, tenantId, projectId, contextId, taskId) {
|
|
1961
1944
|
const session = {
|
|
@@ -1981,17 +1964,17 @@ var _ToolSessionManager = class _ToolSessionManager {
|
|
|
1981
1964
|
return sessionId;
|
|
1982
1965
|
}
|
|
1983
1966
|
/**
|
|
1984
|
-
* Ensure
|
|
1985
|
-
* All agents in the same
|
|
1967
|
+
* Ensure an agent-scoped session exists (idempotent)
|
|
1968
|
+
* All agents in the same agent execution share this session
|
|
1986
1969
|
*/
|
|
1987
|
-
|
|
1970
|
+
ensureAgentSession(sessionId, tenantId, projectId, contextId, taskId) {
|
|
1988
1971
|
if (this.sessions.has(sessionId)) {
|
|
1989
|
-
logger5.debug({ sessionId }, "
|
|
1972
|
+
logger5.debug({ sessionId }, "Agent session already exists, reusing");
|
|
1990
1973
|
return sessionId;
|
|
1991
1974
|
}
|
|
1992
1975
|
logger5.debug(
|
|
1993
1976
|
{ sessionId, tenantId, contextId, taskId },
|
|
1994
|
-
"Creating new
|
|
1977
|
+
"Creating new agent-scoped tool session"
|
|
1995
1978
|
);
|
|
1996
1979
|
return this.createSessionWithId(sessionId, tenantId, projectId, contextId, taskId);
|
|
1997
1980
|
}
|
|
@@ -2101,7 +2084,7 @@ __publicField(_ToolSessionManager, "instance");
|
|
|
2101
2084
|
var ToolSessionManager = _ToolSessionManager;
|
|
2102
2085
|
var toolSessionManager = ToolSessionManager.getInstance();
|
|
2103
2086
|
|
|
2104
|
-
// src/services/
|
|
2087
|
+
// src/services/AgentSession.ts
|
|
2105
2088
|
init_conversations();
|
|
2106
2089
|
init_dbClient();
|
|
2107
2090
|
init_logger();
|
|
@@ -2321,7 +2304,7 @@ var _ArtifactService = class _ArtifactService {
|
|
|
2321
2304
|
async getArtifactSummary(artifactId, toolCallId, artifactMap) {
|
|
2322
2305
|
const key = `${artifactId}:${toolCallId}`;
|
|
2323
2306
|
if (this.context.streamRequestId) {
|
|
2324
|
-
const cachedArtifact = await
|
|
2307
|
+
const cachedArtifact = await agentSessionManager.getArtifactCache(
|
|
2325
2308
|
this.context.streamRequestId,
|
|
2326
2309
|
key
|
|
2327
2310
|
);
|
|
@@ -2367,7 +2350,7 @@ var _ArtifactService = class _ArtifactService {
|
|
|
2367
2350
|
async getArtifactFull(artifactId, toolCallId, artifactMap) {
|
|
2368
2351
|
const key = `${artifactId}:${toolCallId}`;
|
|
2369
2352
|
if (this.context.streamRequestId) {
|
|
2370
|
-
const cachedArtifact = await
|
|
2353
|
+
const cachedArtifact = await agentSessionManager.getArtifactCache(
|
|
2371
2354
|
this.context.streamRequestId,
|
|
2372
2355
|
key
|
|
2373
2356
|
);
|
|
@@ -2434,12 +2417,12 @@ var _ArtifactService = class _ArtifactService {
|
|
|
2434
2417
|
};
|
|
2435
2418
|
}
|
|
2436
2419
|
/**
|
|
2437
|
-
* Persist artifact to database
|
|
2420
|
+
* Persist artifact to database vian agent session
|
|
2438
2421
|
*/
|
|
2439
2422
|
async persistArtifact(request, summaryData, fullData, subAgentId) {
|
|
2440
2423
|
const effectiveAgentId = subAgentId || this.context.subAgentId;
|
|
2441
2424
|
if (this.context.streamRequestId && effectiveAgentId && this.context.taskId) {
|
|
2442
|
-
await
|
|
2425
|
+
await agentSessionManager.recordEvent(
|
|
2443
2426
|
this.context.streamRequestId,
|
|
2444
2427
|
"artifact_saved",
|
|
2445
2428
|
effectiveAgentId,
|
|
@@ -2491,7 +2474,7 @@ var _ArtifactService = class _ArtifactService {
|
|
|
2491
2474
|
};
|
|
2492
2475
|
this.createdArtifacts.set(cacheKey, artifactForCache);
|
|
2493
2476
|
if (this.context.streamRequestId) {
|
|
2494
|
-
await
|
|
2477
|
+
await agentSessionManager.setArtifactCache(
|
|
2495
2478
|
this.context.streamRequestId,
|
|
2496
2479
|
cacheKey,
|
|
2497
2480
|
artifactForCache
|
|
@@ -2523,7 +2506,7 @@ var _ArtifactService = class _ArtifactService {
|
|
|
2523
2506
|
}
|
|
2524
2507
|
/**
|
|
2525
2508
|
* Save an already-created artifact directly to the database
|
|
2526
|
-
* Used by
|
|
2509
|
+
* Used by AgentSession to save artifacts after name/description generation
|
|
2527
2510
|
*/
|
|
2528
2511
|
async saveArtifact(artifact) {
|
|
2529
2512
|
let summaryData = artifact.data;
|
|
@@ -3070,14 +3053,14 @@ __publicField(_ArtifactParser, "ARTIFACT_PATTERNS", [
|
|
|
3070
3053
|
__publicField(_ArtifactParser, "INCOMPLETE_CREATE_REGEX", /<artifact:create(?![^>]*(?:\/>|<\/artifact:create>))/);
|
|
3071
3054
|
var ArtifactParser = _ArtifactParser;
|
|
3072
3055
|
|
|
3073
|
-
// src/services/
|
|
3074
|
-
var logger9 = agentsCore.getLogger("
|
|
3075
|
-
var
|
|
3056
|
+
// src/services/AgentSession.ts
|
|
3057
|
+
var logger9 = agentsCore.getLogger("AgentSession");
|
|
3058
|
+
var AgentSession = class {
|
|
3076
3059
|
// Whether to send data operations
|
|
3077
|
-
constructor(sessionId, messageId,
|
|
3060
|
+
constructor(sessionId, messageId, agentId, tenantId, projectId, contextId) {
|
|
3078
3061
|
this.sessionId = sessionId;
|
|
3079
3062
|
this.messageId = messageId;
|
|
3080
|
-
this.
|
|
3063
|
+
this.agentId = agentId;
|
|
3081
3064
|
this.tenantId = tenantId;
|
|
3082
3065
|
this.projectId = projectId;
|
|
3083
3066
|
this.contextId = contextId;
|
|
@@ -3104,7 +3087,7 @@ var GraphSession = class {
|
|
|
3104
3087
|
__publicField(this, "artifactParser");
|
|
3105
3088
|
// Session-scoped ArtifactParser instance
|
|
3106
3089
|
__publicField(this, "isEmitOperations", false);
|
|
3107
|
-
logger9.debug({ sessionId, messageId,
|
|
3090
|
+
logger9.debug({ sessionId, messageId, agentId }, "AgentSession created");
|
|
3108
3091
|
if (tenantId && projectId) {
|
|
3109
3092
|
toolSessionManager.createSessionWithId(
|
|
3110
3093
|
sessionId,
|
|
@@ -3140,7 +3123,7 @@ var GraphSession = class {
|
|
|
3140
3123
|
this.isEmitOperations = true;
|
|
3141
3124
|
logger9.info(
|
|
3142
3125
|
{ sessionId: this.sessionId },
|
|
3143
|
-
"\u{1F50D} DEBUG: Emit operations enabled for
|
|
3126
|
+
"\u{1F50D} DEBUG: Emit operations enabled for AgentSession"
|
|
3144
3127
|
);
|
|
3145
3128
|
}
|
|
3146
3129
|
/**
|
|
@@ -3413,7 +3396,7 @@ var GraphSession = class {
|
|
|
3413
3396
|
return {
|
|
3414
3397
|
sessionId: this.sessionId,
|
|
3415
3398
|
messageId: this.messageId,
|
|
3416
|
-
|
|
3399
|
+
agentId: this.agentId,
|
|
3417
3400
|
totalEvents: this.events.length,
|
|
3418
3401
|
eventCounts,
|
|
3419
3402
|
agentCounts,
|
|
@@ -3464,7 +3447,7 @@ var GraphSession = class {
|
|
|
3464
3447
|
}
|
|
3465
3448
|
}
|
|
3466
3449
|
/**
|
|
3467
|
-
* Generate and send a status update using
|
|
3450
|
+
* Generate and send a status update using agent-level summarizer
|
|
3468
3451
|
*/
|
|
3469
3452
|
async generateAndSendUpdate() {
|
|
3470
3453
|
if (this.isEnded) {
|
|
@@ -3489,8 +3472,8 @@ var GraphSession = class {
|
|
|
3489
3472
|
logger9.warn({ sessionId: this.sessionId }, "No status update state - cannot generate update");
|
|
3490
3473
|
return;
|
|
3491
3474
|
}
|
|
3492
|
-
if (!this.
|
|
3493
|
-
logger9.warn({ sessionId: this.sessionId }, "No
|
|
3475
|
+
if (!this.agentId) {
|
|
3476
|
+
logger9.warn({ sessionId: this.sessionId }, "No agent ID - cannot generate update");
|
|
3494
3477
|
return;
|
|
3495
3478
|
}
|
|
3496
3479
|
const newEventCount = this.events.length - this.statusUpdateState.lastEventCount;
|
|
@@ -3642,10 +3625,10 @@ var GraphSession = class {
|
|
|
3642
3625
|
*/
|
|
3643
3626
|
async generateStructuredStatusUpdate(newEvents, elapsedTime, statusComponents, summarizerModel, previousSummaries = []) {
|
|
3644
3627
|
return tracer.startActiveSpan(
|
|
3645
|
-
"
|
|
3628
|
+
"agent_session.generate_structured_update",
|
|
3646
3629
|
{
|
|
3647
3630
|
attributes: {
|
|
3648
|
-
"
|
|
3631
|
+
"agent_session.id": this.sessionId,
|
|
3649
3632
|
"events.count": newEvents.length,
|
|
3650
3633
|
"elapsed_time.seconds": Math.round(elapsedTime / 1e3),
|
|
3651
3634
|
"llm.model": summarizerModel?.model,
|
|
@@ -3726,7 +3709,7 @@ CRITICAL - HIDE ALL INTERNAL SYSTEM OPERATIONS:
|
|
|
3726
3709
|
- ABSOLUTELY FORBIDDEN WORDS/PHRASES: "transfer", "transferring", "delegation", "delegating", "delegate", "agent", "routing", "route", "artifact", "saving artifact", "stored artifact", "artifact saved", "continuing", "passing to", "handing off", "switching to"
|
|
3727
3710
|
- NEVER reveal internal architecture: No mentions of different agents, components, systems, or modules working together
|
|
3728
3711
|
- NEVER mention artifact operations: Users don't need to know about data being saved, stored, or organized internally
|
|
3729
|
-
- NEVER describe
|
|
3712
|
+
- NEVER describe transfers or transitions: Present everything as one seamless operation
|
|
3730
3713
|
- If you see "transfer", "delegation_sent", "delegation_returned", or "artifact_saved" events - IGNORE THEM or translate to user-facing information only
|
|
3731
3714
|
- Focus ONLY on actual discoveries, findings, and results that matter to the user
|
|
3732
3715
|
|
|
@@ -3948,10 +3931,10 @@ ${this.statusUpdateState?.config.prompt?.trim() || ""}`;
|
|
|
3948
3931
|
*/
|
|
3949
3932
|
async processArtifact(artifactData) {
|
|
3950
3933
|
return tracer.startActiveSpan(
|
|
3951
|
-
"
|
|
3934
|
+
"agent_session.process_artifact",
|
|
3952
3935
|
{
|
|
3953
3936
|
attributes: {
|
|
3954
|
-
"
|
|
3937
|
+
"agent_session.id": this.sessionId,
|
|
3955
3938
|
"artifact.id": artifactData.artifactId,
|
|
3956
3939
|
"artifact.type": artifactData.artifactType || "unknown",
|
|
3957
3940
|
"artifact.agent_id": artifactData.subAgentId || "unknown",
|
|
@@ -4019,7 +4002,7 @@ Make it specific and relevant.`;
|
|
|
4019
4002
|
scopes: {
|
|
4020
4003
|
tenantId: artifactData.tenantId,
|
|
4021
4004
|
projectId: artifactData.projectId,
|
|
4022
|
-
|
|
4005
|
+
agentId: this.agentId || ""
|
|
4023
4006
|
},
|
|
4024
4007
|
subAgentId: artifactData.subAgentId
|
|
4025
4008
|
});
|
|
@@ -4074,7 +4057,7 @@ Make it specific and relevant.`;
|
|
|
4074
4057
|
description: z6.z.string().describe("Brief description of the artifact's relevance to the user's question")
|
|
4075
4058
|
});
|
|
4076
4059
|
const { object } = await tracer.startActiveSpan(
|
|
4077
|
-
"
|
|
4060
|
+
"agent_session.generate_artifact_metadata",
|
|
4078
4061
|
{
|
|
4079
4062
|
attributes: {
|
|
4080
4063
|
"llm.model": this.statusUpdateState?.summarizerModel?.model,
|
|
@@ -4284,20 +4267,20 @@ Make it specific and relevant.`;
|
|
|
4284
4267
|
}
|
|
4285
4268
|
}
|
|
4286
4269
|
};
|
|
4287
|
-
var
|
|
4270
|
+
var AgentSessionManager = class {
|
|
4288
4271
|
constructor() {
|
|
4289
4272
|
__publicField(this, "sessions", /* @__PURE__ */ new Map());
|
|
4290
4273
|
}
|
|
4291
4274
|
/**
|
|
4292
4275
|
* Create a new session for a message
|
|
4293
4276
|
*/
|
|
4294
|
-
createSession(messageId,
|
|
4277
|
+
createSession(messageId, agentId, tenantId, projectId, contextId) {
|
|
4295
4278
|
const sessionId = messageId;
|
|
4296
|
-
const session = new
|
|
4279
|
+
const session = new AgentSession(sessionId, messageId, agentId, tenantId, projectId, contextId);
|
|
4297
4280
|
this.sessions.set(sessionId, session);
|
|
4298
4281
|
logger9.info(
|
|
4299
|
-
{ sessionId, messageId,
|
|
4300
|
-
"
|
|
4282
|
+
{ sessionId, messageId, agentId, tenantId, projectId, contextId },
|
|
4283
|
+
"AgentSession created"
|
|
4301
4284
|
);
|
|
4302
4285
|
return sessionId;
|
|
4303
4286
|
}
|
|
@@ -4364,7 +4347,7 @@ var GraphSessionManager = class {
|
|
|
4364
4347
|
}
|
|
4365
4348
|
const events = session.getEvents();
|
|
4366
4349
|
const summary = session.getSummary();
|
|
4367
|
-
logger9.info({ sessionId, summary }, "
|
|
4350
|
+
logger9.info({ sessionId, summary }, "AgentSession ended");
|
|
4368
4351
|
session.cleanup();
|
|
4369
4352
|
this.sessions.delete(sessionId);
|
|
4370
4353
|
return events;
|
|
@@ -4428,36 +4411,36 @@ var GraphSessionManager = class {
|
|
|
4428
4411
|
}
|
|
4429
4412
|
}
|
|
4430
4413
|
};
|
|
4431
|
-
var
|
|
4414
|
+
var agentSessionManager = new AgentSessionManager();
|
|
4432
4415
|
|
|
4433
4416
|
// src/utils/model-resolver.ts
|
|
4434
4417
|
init_dbClient();
|
|
4435
|
-
async function resolveModelConfig(
|
|
4436
|
-
if (
|
|
4418
|
+
async function resolveModelConfig(agentId, subAgent) {
|
|
4419
|
+
if (subAgent.models?.base?.model) {
|
|
4437
4420
|
return {
|
|
4438
|
-
base:
|
|
4439
|
-
structuredOutput:
|
|
4440
|
-
summarizer:
|
|
4421
|
+
base: subAgent.models.base,
|
|
4422
|
+
structuredOutput: subAgent.models.structuredOutput || subAgent.models.base,
|
|
4423
|
+
summarizer: subAgent.models.summarizer || subAgent.models.base
|
|
4441
4424
|
};
|
|
4442
4425
|
}
|
|
4443
|
-
const
|
|
4444
|
-
scopes: { tenantId:
|
|
4426
|
+
const agent = await agentsCore.getAgentById(dbClient_default)({
|
|
4427
|
+
scopes: { tenantId: subAgent.tenantId, projectId: subAgent.projectId, agentId }
|
|
4445
4428
|
});
|
|
4446
|
-
if (
|
|
4429
|
+
if (agent?.models?.base?.model) {
|
|
4447
4430
|
return {
|
|
4448
|
-
base:
|
|
4449
|
-
structuredOutput:
|
|
4450
|
-
summarizer:
|
|
4431
|
+
base: agent.models.base,
|
|
4432
|
+
structuredOutput: subAgent.models?.structuredOutput || agent.models.structuredOutput || agent.models.base,
|
|
4433
|
+
summarizer: subAgent.models?.summarizer || agent.models.summarizer || agent.models.base
|
|
4451
4434
|
};
|
|
4452
4435
|
}
|
|
4453
4436
|
const project = await agentsCore.getProject(dbClient_default)({
|
|
4454
|
-
scopes: { tenantId:
|
|
4437
|
+
scopes: { tenantId: subAgent.tenantId, projectId: subAgent.projectId }
|
|
4455
4438
|
});
|
|
4456
4439
|
if (project?.models?.base?.model) {
|
|
4457
4440
|
return {
|
|
4458
4441
|
base: project.models.base,
|
|
4459
|
-
structuredOutput:
|
|
4460
|
-
summarizer:
|
|
4442
|
+
structuredOutput: subAgent.models?.structuredOutput || project.models.structuredOutput || project.models.base,
|
|
4443
|
+
summarizer: subAgent.models?.summarizer || project.models.summarizer || project.models.base
|
|
4461
4444
|
};
|
|
4462
4445
|
}
|
|
4463
4446
|
throw new Error(
|
|
@@ -4494,7 +4477,7 @@ var _IncrementalStreamParser = class _IncrementalStreamParser {
|
|
|
4494
4477
|
this.contextId = contextId;
|
|
4495
4478
|
this.subAgentId = artifactParserOptions?.subAgentId;
|
|
4496
4479
|
if (artifactParserOptions?.streamRequestId) {
|
|
4497
|
-
const sessionParser =
|
|
4480
|
+
const sessionParser = agentSessionManager.getArtifactParser(
|
|
4498
4481
|
artifactParserOptions.streamRequestId
|
|
4499
4482
|
);
|
|
4500
4483
|
if (sessionParser) {
|
|
@@ -4503,9 +4486,9 @@ var _IncrementalStreamParser = class _IncrementalStreamParser {
|
|
|
4503
4486
|
}
|
|
4504
4487
|
}
|
|
4505
4488
|
let sharedArtifactService = null;
|
|
4506
|
-
if (artifactParserOptions?.streamRequestId && typeof
|
|
4489
|
+
if (artifactParserOptions?.streamRequestId && typeof agentSessionManager.getArtifactService === "function") {
|
|
4507
4490
|
try {
|
|
4508
|
-
sharedArtifactService =
|
|
4491
|
+
sharedArtifactService = agentSessionManager.getArtifactService(
|
|
4509
4492
|
artifactParserOptions.streamRequestId
|
|
4510
4493
|
);
|
|
4511
4494
|
} catch (error) {
|
|
@@ -4880,7 +4863,7 @@ var ResponseFormatter = class {
|
|
|
4880
4863
|
__publicField(this, "subAgentId");
|
|
4881
4864
|
this.subAgentId = artifactParserOptions?.subAgentId;
|
|
4882
4865
|
if (artifactParserOptions?.streamRequestId) {
|
|
4883
|
-
const sessionParser =
|
|
4866
|
+
const sessionParser = agentSessionManager.getArtifactParser(
|
|
4884
4867
|
artifactParserOptions.streamRequestId
|
|
4885
4868
|
);
|
|
4886
4869
|
if (sessionParser) {
|
|
@@ -4889,9 +4872,9 @@ var ResponseFormatter = class {
|
|
|
4889
4872
|
}
|
|
4890
4873
|
}
|
|
4891
4874
|
let sharedArtifactService = null;
|
|
4892
|
-
if (artifactParserOptions?.streamRequestId && typeof
|
|
4875
|
+
if (artifactParserOptions?.streamRequestId && typeof agentSessionManager.getArtifactService === "function") {
|
|
4893
4876
|
try {
|
|
4894
|
-
sharedArtifactService =
|
|
4877
|
+
sharedArtifactService = agentSessionManager.getArtifactService(
|
|
4895
4878
|
artifactParserOptions.streamRequestId
|
|
4896
4879
|
);
|
|
4897
4880
|
} catch (error) {
|
|
@@ -5033,12 +5016,12 @@ var ResponseFormatter = class {
|
|
|
5033
5016
|
}
|
|
5034
5017
|
}
|
|
5035
5018
|
};
|
|
5036
|
-
function agentInitializingOp(sessionId,
|
|
5019
|
+
function agentInitializingOp(sessionId, agentId) {
|
|
5037
5020
|
return {
|
|
5038
5021
|
type: "agent_initializing",
|
|
5039
5022
|
details: {
|
|
5040
5023
|
sessionId,
|
|
5041
|
-
|
|
5024
|
+
agentId
|
|
5042
5025
|
}
|
|
5043
5026
|
};
|
|
5044
5027
|
}
|
|
@@ -6124,23 +6107,29 @@ var createTransferToAgentTool = ({
|
|
|
6124
6107
|
logger14.info(
|
|
6125
6108
|
{
|
|
6126
6109
|
transferTo: transferConfig.id ?? "unknown",
|
|
6127
|
-
|
|
6110
|
+
fromSubAgent: callingAgentId
|
|
6128
6111
|
},
|
|
6129
6112
|
"invoked transferToAgentTool"
|
|
6130
6113
|
);
|
|
6131
6114
|
if (streamRequestId) {
|
|
6132
|
-
|
|
6115
|
+
agentSessionManager.recordEvent(streamRequestId, "transfer", callingAgentId, {
|
|
6133
6116
|
fromSubAgent: callingAgentId,
|
|
6134
6117
|
targetSubAgent: transferConfig.id ?? "unknown",
|
|
6135
6118
|
reason: `Transfer to ${transferConfig.name || transferConfig.id}`
|
|
6136
6119
|
});
|
|
6137
6120
|
}
|
|
6138
|
-
|
|
6121
|
+
const transferResult = {
|
|
6139
6122
|
type: "transfer",
|
|
6140
|
-
|
|
6141
|
-
|
|
6123
|
+
targetSubAgentId: transferConfig.id ?? "unknown",
|
|
6124
|
+
// Changed from "target" for type safety
|
|
6125
|
+
fromSubAgentId: callingAgentId
|
|
6142
6126
|
// Include the calling agent ID for tracking
|
|
6143
6127
|
};
|
|
6128
|
+
logger14.info({
|
|
6129
|
+
transferResult,
|
|
6130
|
+
transferResultKeys: Object.keys(transferResult)
|
|
6131
|
+
}, "[DEBUG] Transfer tool returning");
|
|
6132
|
+
return transferResult;
|
|
6144
6133
|
}
|
|
6145
6134
|
});
|
|
6146
6135
|
};
|
|
@@ -6149,7 +6138,7 @@ function createDelegateToAgentTool({
|
|
|
6149
6138
|
callingAgentId,
|
|
6150
6139
|
tenantId,
|
|
6151
6140
|
projectId,
|
|
6152
|
-
|
|
6141
|
+
agentId,
|
|
6153
6142
|
contextId,
|
|
6154
6143
|
metadata,
|
|
6155
6144
|
sessionId,
|
|
@@ -6170,7 +6159,7 @@ function createDelegateToAgentTool({
|
|
|
6170
6159
|
});
|
|
6171
6160
|
}
|
|
6172
6161
|
if (metadata.streamRequestId) {
|
|
6173
|
-
|
|
6162
|
+
agentSessionManager.recordEvent(
|
|
6174
6163
|
metadata.streamRequestId,
|
|
6175
6164
|
"delegation_sent",
|
|
6176
6165
|
callingAgentId,
|
|
@@ -6189,7 +6178,7 @@ function createDelegateToAgentTool({
|
|
|
6189
6178
|
scopes: {
|
|
6190
6179
|
tenantId,
|
|
6191
6180
|
projectId,
|
|
6192
|
-
|
|
6181
|
+
agentId
|
|
6193
6182
|
},
|
|
6194
6183
|
subAgentId: delegateConfig.config.id
|
|
6195
6184
|
});
|
|
@@ -6235,8 +6224,8 @@ function createDelegateToAgentTool({
|
|
|
6235
6224
|
Authorization: `Bearer ${metadata.apiKey}`,
|
|
6236
6225
|
"x-inkeep-tenant-id": tenantId,
|
|
6237
6226
|
"x-inkeep-project-id": projectId,
|
|
6238
|
-
"x-inkeep-
|
|
6239
|
-
"x-inkeep-agent-id": delegateConfig.config.id
|
|
6227
|
+
"x-inkeep-agent-id": agentId,
|
|
6228
|
+
"x-inkeep-sub-agent-id": delegateConfig.config.id
|
|
6240
6229
|
};
|
|
6241
6230
|
}
|
|
6242
6231
|
const a2aClient = new A2AClient(delegateConfig.config.baseUrl, {
|
|
@@ -6267,7 +6256,7 @@ function createDelegateToAgentTool({
|
|
|
6267
6256
|
// Flag to prevent streaming in delegated agents
|
|
6268
6257
|
delegationId,
|
|
6269
6258
|
// Include delegation ID for tracking
|
|
6270
|
-
...isInternal ? {
|
|
6259
|
+
...isInternal ? { fromSubAgentId: callingAgentId } : { fromExternalAgentId: callingAgentId }
|
|
6271
6260
|
}
|
|
6272
6261
|
};
|
|
6273
6262
|
logger14.info({ messageToSend }, "messageToSend");
|
|
@@ -6283,7 +6272,7 @@ function createDelegateToAgentTool({
|
|
|
6283
6272
|
visibility: isInternal ? "internal" : "external",
|
|
6284
6273
|
messageType: "a2a-request",
|
|
6285
6274
|
fromSubAgentId: callingAgentId,
|
|
6286
|
-
...isInternal ? {
|
|
6275
|
+
...isInternal ? { toSubAgentId: delegateConfig.config.id } : { toExternalAgentId: delegateConfig.config.id }
|
|
6287
6276
|
});
|
|
6288
6277
|
const response = await a2aClient.sendMessage({
|
|
6289
6278
|
message: messageToSend
|
|
@@ -6311,7 +6300,7 @@ function createDelegateToAgentTool({
|
|
|
6311
6300
|
toolSessionManager.recordToolResult(sessionId, toolResult);
|
|
6312
6301
|
}
|
|
6313
6302
|
if (metadata.streamRequestId) {
|
|
6314
|
-
|
|
6303
|
+
agentSessionManager.recordEvent(
|
|
6315
6304
|
metadata.streamRequestId,
|
|
6316
6305
|
"delegation_returned",
|
|
6317
6306
|
callingAgentId,
|
|
@@ -6390,7 +6379,7 @@ var system_prompt_default = `<system_message>
|
|
|
6390
6379
|
{{CORE_INSTRUCTIONS}}
|
|
6391
6380
|
</core_instructions>
|
|
6392
6381
|
|
|
6393
|
-
{{
|
|
6382
|
+
{{AGENT_CONTEXT_SECTION}}
|
|
6394
6383
|
|
|
6395
6384
|
{{ARTIFACTS_SECTION}}
|
|
6396
6385
|
{{TOOLS_SECTION}}
|
|
@@ -6555,7 +6544,7 @@ When you use delegation tools, the response may include artifacts in the parts a
|
|
|
6555
6544
|
These artifacts become immediately available for you to reference using the artifactId and toolCallId from the response.
|
|
6556
6545
|
Present delegation results naturally without mentioning the delegation process itself.
|
|
6557
6546
|
|
|
6558
|
-
IMPORTANT: All agents can retrieve and use information from existing artifacts when the
|
|
6547
|
+
IMPORTANT: All sub-agents can retrieve and use information from existing artifacts when the agent has artifact components, regardless of whether the individual agent or sub-agents can create new artifacts.`;
|
|
6559
6548
|
|
|
6560
6549
|
// src/agents/versions/v1/Phase1Config.ts
|
|
6561
6550
|
init_logger();
|
|
@@ -6601,8 +6590,8 @@ var Phase1Config = class _Phase1Config {
|
|
|
6601
6590
|
}
|
|
6602
6591
|
let systemPrompt = systemPromptTemplate;
|
|
6603
6592
|
systemPrompt = systemPrompt.replace("{{CORE_INSTRUCTIONS}}", config.corePrompt);
|
|
6604
|
-
const
|
|
6605
|
-
systemPrompt = systemPrompt.replace("{{
|
|
6593
|
+
const agentContextSection = this.generateAgentContextSection(config.agentPrompt);
|
|
6594
|
+
systemPrompt = systemPrompt.replace("{{AGENT_CONTEXT_SECTION}}", agentContextSection);
|
|
6606
6595
|
const toolData = this.isToolDataArray(config.tools) ? config.tools : _Phase1Config.convertMcpToolsToToolData(config.tools);
|
|
6607
6596
|
const hasArtifactComponents = config.artifactComponents && config.artifactComponents.length > 0;
|
|
6608
6597
|
const artifactsSection = this.generateArtifactsSection(
|
|
@@ -6610,7 +6599,7 @@ var Phase1Config = class _Phase1Config {
|
|
|
6610
6599
|
config.artifacts,
|
|
6611
6600
|
hasArtifactComponents,
|
|
6612
6601
|
config.artifactComponents,
|
|
6613
|
-
config.
|
|
6602
|
+
config.hasAgentArtifactComponents
|
|
6614
6603
|
);
|
|
6615
6604
|
systemPrompt = systemPrompt.replace("{{ARTIFACTS_SECTION}}", artifactsSection);
|
|
6616
6605
|
const toolsSection = this.generateToolsSection(templates, toolData);
|
|
@@ -6629,14 +6618,14 @@ var Phase1Config = class _Phase1Config {
|
|
|
6629
6618
|
systemPrompt = systemPrompt.replace("{{DELEGATION_INSTRUCTIONS}}", delegationSection);
|
|
6630
6619
|
return systemPrompt;
|
|
6631
6620
|
}
|
|
6632
|
-
|
|
6633
|
-
if (!
|
|
6621
|
+
generateAgentContextSection(agentPrompt) {
|
|
6622
|
+
if (!agentPrompt) {
|
|
6634
6623
|
return "";
|
|
6635
6624
|
}
|
|
6636
6625
|
return `
|
|
6637
|
-
<
|
|
6638
|
-
${
|
|
6639
|
-
</
|
|
6626
|
+
<agent_context>
|
|
6627
|
+
${agentPrompt}
|
|
6628
|
+
</agent_context>`;
|
|
6640
6629
|
}
|
|
6641
6630
|
generateThinkingPreparationSection(templates, isThinkingPreparation) {
|
|
6642
6631
|
if (!isThinkingPreparation) {
|
|
@@ -6654,7 +6643,7 @@ var Phase1Config = class _Phase1Config {
|
|
|
6654
6643
|
}
|
|
6655
6644
|
return `- You have transfer_to_* tools that seamlessly continue the conversation
|
|
6656
6645
|
- NEVER announce transfers - just call the tool when needed
|
|
6657
|
-
- The conversation continues naturally without any
|
|
6646
|
+
- The conversation continues naturally without any transfer language`;
|
|
6658
6647
|
}
|
|
6659
6648
|
generateDelegationInstructions(hasDelegateRelations) {
|
|
6660
6649
|
if (!hasDelegateRelations) {
|
|
@@ -6888,8 +6877,8 @@ ${typeDescriptions}
|
|
|
6888
6877
|
- Do NOT abbreviate, modify, or guess the type name
|
|
6889
6878
|
- Copy the exact quoted name from the "AVAILABLE ARTIFACT TYPES" list above`;
|
|
6890
6879
|
}
|
|
6891
|
-
generateArtifactsSection(templates, artifacts, hasArtifactComponents = false, artifactComponents,
|
|
6892
|
-
const shouldShowReferencingRules =
|
|
6880
|
+
generateArtifactsSection(templates, artifacts, hasArtifactComponents = false, artifactComponents, hasAgentArtifactComponents) {
|
|
6881
|
+
const shouldShowReferencingRules = hasAgentArtifactComponents || artifacts.length > 0;
|
|
6893
6882
|
const rules = this.getArtifactReferencingRules(
|
|
6894
6883
|
hasArtifactComponents,
|
|
6895
6884
|
templates,
|
|
@@ -7369,7 +7358,7 @@ ${artifact_retrieval_guidance_default}
|
|
|
7369
7358
|
dataComponents,
|
|
7370
7359
|
artifactComponents,
|
|
7371
7360
|
hasArtifactComponents,
|
|
7372
|
-
|
|
7361
|
+
hasAgentArtifactComponents,
|
|
7373
7362
|
artifacts = []
|
|
7374
7363
|
} = config;
|
|
7375
7364
|
let allDataComponents = [...dataComponents];
|
|
@@ -7385,7 +7374,7 @@ ${artifact_retrieval_guidance_default}
|
|
|
7385
7374
|
}
|
|
7386
7375
|
const dataComponentsSection = this.generateDataComponentsSection(allDataComponents);
|
|
7387
7376
|
const artifactsSection = this.generateArtifactsSection(artifacts);
|
|
7388
|
-
const shouldShowReferencingRules =
|
|
7377
|
+
const shouldShowReferencingRules = hasAgentArtifactComponents || artifacts.length > 0;
|
|
7389
7378
|
const artifactGuidance = this.getStructuredArtifactGuidance(
|
|
7390
7379
|
hasArtifactComponents,
|
|
7391
7380
|
artifactComponents,
|
|
@@ -7590,7 +7579,7 @@ var Agent = class {
|
|
|
7590
7579
|
return this.isDelegatedAgent ? void 0 : this.streamHelper;
|
|
7591
7580
|
}
|
|
7592
7581
|
/**
|
|
7593
|
-
* Wraps a tool with streaming lifecycle tracking (start, complete, error) and
|
|
7582
|
+
* Wraps a tool with streaming lifecycle tracking (start, complete, error) and AgentSession recording
|
|
7594
7583
|
*/
|
|
7595
7584
|
wrapToolWithStreaming(toolName, toolDefinition, streamRequestId, toolType) {
|
|
7596
7585
|
if (!toolDefinition || typeof toolDefinition !== "object" || !("execute" in toolDefinition)) {
|
|
@@ -7609,7 +7598,7 @@ var Agent = class {
|
|
|
7609
7598
|
"tool.purpose": toolDefinition.description || "No description provided",
|
|
7610
7599
|
"ai.toolType": toolType || "unknown",
|
|
7611
7600
|
"ai.agentName": this.config.name || "unknown",
|
|
7612
|
-
"
|
|
7601
|
+
"agent.id": this.config.agentId || "unknown"
|
|
7613
7602
|
});
|
|
7614
7603
|
}
|
|
7615
7604
|
const isInternalTool = toolName.includes("save_tool_result") || toolName.includes("thinking_complete") || toolName.startsWith("transfer_to_") || toolName.startsWith("delegate_to_");
|
|
@@ -7617,7 +7606,7 @@ var Agent = class {
|
|
|
7617
7606
|
const result = await originalExecute(args, context);
|
|
7618
7607
|
const duration = Date.now() - startTime;
|
|
7619
7608
|
if (streamRequestId && !isInternalTool) {
|
|
7620
|
-
|
|
7609
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_execution", this.config.id, {
|
|
7621
7610
|
toolName,
|
|
7622
7611
|
args,
|
|
7623
7612
|
result,
|
|
@@ -7630,7 +7619,7 @@ var Agent = class {
|
|
|
7630
7619
|
const duration = Date.now() - startTime;
|
|
7631
7620
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
7632
7621
|
if (streamRequestId && !isInternalTool) {
|
|
7633
|
-
|
|
7622
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_execution", this.config.id, {
|
|
7634
7623
|
toolName,
|
|
7635
7624
|
args,
|
|
7636
7625
|
result: { error: errorMessage },
|
|
@@ -7675,7 +7664,7 @@ var Agent = class {
|
|
|
7675
7664
|
callingAgentId: this.config.id,
|
|
7676
7665
|
tenantId: this.config.tenantId,
|
|
7677
7666
|
projectId: this.config.projectId,
|
|
7678
|
-
|
|
7667
|
+
agentId: this.config.agentId,
|
|
7679
7668
|
contextId: runtimeContext?.contextId || "default",
|
|
7680
7669
|
// fallback for compatibility
|
|
7681
7670
|
metadata: runtimeContext?.metadata || {
|
|
@@ -7784,7 +7773,7 @@ var Agent = class {
|
|
|
7784
7773
|
scopes: {
|
|
7785
7774
|
tenantId: this.config.tenantId,
|
|
7786
7775
|
projectId: this.config.projectId,
|
|
7787
|
-
|
|
7776
|
+
agentId: this.config.agentId,
|
|
7788
7777
|
subAgentId: this.config.id
|
|
7789
7778
|
}
|
|
7790
7779
|
});
|
|
@@ -7890,26 +7879,26 @@ var Agent = class {
|
|
|
7890
7879
|
"ai.toolCall.args": JSON.stringify({ operation: "mcp_tool_discovery" }),
|
|
7891
7880
|
"ai.toolCall.result": JSON.stringify({
|
|
7892
7881
|
status: "no_tools_available",
|
|
7893
|
-
message: `MCP server has 0 effective tools. Double check the selected tools in your
|
|
7882
|
+
message: `MCP server has 0 effective tools. Double check the selected tools in your agent and the active tools in the MCP server configuration.`,
|
|
7894
7883
|
serverUrl: tool3.config.type === "mcp" ? tool3.config.mcp.server.url : "unknown",
|
|
7895
7884
|
originalToolName: tool3.name
|
|
7896
7885
|
}),
|
|
7897
7886
|
"ai.toolType": "mcp",
|
|
7898
7887
|
"ai.agentName": this.config.name || "unknown",
|
|
7899
7888
|
"conversation.id": this.conversationId || "unknown",
|
|
7900
|
-
"
|
|
7889
|
+
"agent.id": this.config.agentId || "unknown",
|
|
7901
7890
|
"tenant.id": this.config.tenantId || "unknown",
|
|
7902
7891
|
"project.id": this.config.projectId || "unknown"
|
|
7903
7892
|
}
|
|
7904
7893
|
},
|
|
7905
7894
|
(span) => {
|
|
7906
7895
|
agentsCore.setSpanWithError(span, new Error(`0 effective tools available for ${tool3.name}`));
|
|
7907
|
-
|
|
7896
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_execution", this.config.id, {
|
|
7908
7897
|
toolName: tool3.name,
|
|
7909
7898
|
args: { operation: "mcp_tool_discovery" },
|
|
7910
7899
|
result: {
|
|
7911
7900
|
status: "no_tools_available",
|
|
7912
|
-
message: `MCP server has 0 effective tools. Double check the selected tools in your
|
|
7901
|
+
message: `MCP server has 0 effective tools. Double check the selected tools in your agent and the active tools in the MCP server configuration.`,
|
|
7913
7902
|
serverUrl: tool3.config.type === "mcp" ? tool3.config.mcp.server.url : "unknown"
|
|
7914
7903
|
}
|
|
7915
7904
|
});
|
|
@@ -7958,7 +7947,7 @@ var Agent = class {
|
|
|
7958
7947
|
scopes: {
|
|
7959
7948
|
tenantId: this.config.tenantId,
|
|
7960
7949
|
projectId: this.config.projectId,
|
|
7961
|
-
|
|
7950
|
+
agentId: this.config.agentId
|
|
7962
7951
|
},
|
|
7963
7952
|
subAgentId: this.config.id
|
|
7964
7953
|
});
|
|
@@ -8001,11 +7990,21 @@ var Agent = class {
|
|
|
8001
7990
|
"Function Tool Called"
|
|
8002
7991
|
);
|
|
8003
7992
|
try {
|
|
7993
|
+
const project = await agentsCore.getProject(dbClient_default)({
|
|
7994
|
+
scopes: { tenantId: this.config.tenantId, projectId: this.config.projectId }
|
|
7995
|
+
});
|
|
7996
|
+
const defaultSandboxConfig = {
|
|
7997
|
+
provider: "local",
|
|
7998
|
+
runtime: "node22",
|
|
7999
|
+
timeout: 3e4,
|
|
8000
|
+
vcpus: 1
|
|
8001
|
+
};
|
|
8004
8002
|
const result = await sandboxExecutor.executeFunctionTool(functionToolDef.id, args, {
|
|
8005
8003
|
description: functionToolDef.description || functionToolDef.name,
|
|
8006
8004
|
inputSchema: functionData.inputSchema || {},
|
|
8007
8005
|
executeCode: functionData.executeCode,
|
|
8008
|
-
dependencies: functionData.dependencies || {}
|
|
8006
|
+
dependencies: functionData.dependencies || {},
|
|
8007
|
+
sandboxConfig: project?.sandboxConfig || defaultSandboxConfig
|
|
8009
8008
|
});
|
|
8010
8009
|
toolSessionManager.recordToolResult(sessionId || "", {
|
|
8011
8010
|
toolCallId,
|
|
@@ -8042,14 +8041,14 @@ var Agent = class {
|
|
|
8042
8041
|
async getResolvedContext(conversationId, headers) {
|
|
8043
8042
|
try {
|
|
8044
8043
|
if (!this.config.contextConfigId) {
|
|
8045
|
-
logger18.debug({
|
|
8044
|
+
logger18.debug({ agentId: this.config.agentId }, "No context config found for agent");
|
|
8046
8045
|
return null;
|
|
8047
8046
|
}
|
|
8048
8047
|
const contextConfig = await agentsCore.getContextConfigById(dbClient_default)({
|
|
8049
8048
|
scopes: {
|
|
8050
8049
|
tenantId: this.config.tenantId,
|
|
8051
8050
|
projectId: this.config.projectId,
|
|
8052
|
-
|
|
8051
|
+
agentId: this.config.agentId
|
|
8053
8052
|
},
|
|
8054
8053
|
id: this.config.contextConfigId
|
|
8055
8054
|
});
|
|
@@ -8096,67 +8095,67 @@ var Agent = class {
|
|
|
8096
8095
|
}
|
|
8097
8096
|
}
|
|
8098
8097
|
/**
|
|
8099
|
-
* Get the
|
|
8098
|
+
* Get the agent prompt for this agent's agent
|
|
8100
8099
|
*/
|
|
8101
|
-
async
|
|
8100
|
+
async getAgentPrompt() {
|
|
8102
8101
|
try {
|
|
8103
|
-
const
|
|
8102
|
+
const agentDefinition = await agentsCore.getFullAgentDefinition(dbClient_default)({
|
|
8104
8103
|
scopes: {
|
|
8105
8104
|
tenantId: this.config.tenantId,
|
|
8106
8105
|
projectId: this.config.projectId,
|
|
8107
|
-
|
|
8106
|
+
agentId: this.config.agentId
|
|
8108
8107
|
}
|
|
8109
8108
|
});
|
|
8110
|
-
return
|
|
8109
|
+
return agentDefinition?.prompt || void 0;
|
|
8111
8110
|
} catch (error) {
|
|
8112
8111
|
logger18.warn(
|
|
8113
8112
|
{
|
|
8114
|
-
|
|
8113
|
+
agentId: this.config.agentId,
|
|
8115
8114
|
error: error instanceof Error ? error.message : "Unknown error"
|
|
8116
8115
|
},
|
|
8117
|
-
"Failed to get
|
|
8116
|
+
"Failed to get agent prompt"
|
|
8118
8117
|
);
|
|
8119
8118
|
return void 0;
|
|
8120
8119
|
}
|
|
8121
8120
|
}
|
|
8122
8121
|
/**
|
|
8123
|
-
* Check if any agent in the
|
|
8122
|
+
* Check if any agent in the agent has artifact components configured
|
|
8124
8123
|
*/
|
|
8125
|
-
async
|
|
8124
|
+
async hasAgentArtifactComponents() {
|
|
8126
8125
|
try {
|
|
8127
|
-
const
|
|
8126
|
+
const agentDefinition = await agentsCore.getFullAgentDefinition(dbClient_default)({
|
|
8128
8127
|
scopes: {
|
|
8129
8128
|
tenantId: this.config.tenantId,
|
|
8130
8129
|
projectId: this.config.projectId,
|
|
8131
|
-
|
|
8130
|
+
agentId: this.config.agentId
|
|
8132
8131
|
}
|
|
8133
8132
|
});
|
|
8134
|
-
if (!
|
|
8133
|
+
if (!agentDefinition) {
|
|
8135
8134
|
return false;
|
|
8136
8135
|
}
|
|
8137
|
-
return Object.values(
|
|
8136
|
+
return Object.values(agentDefinition.subAgents).some(
|
|
8138
8137
|
(subAgent) => "artifactComponents" in subAgent && subAgent.artifactComponents && subAgent.artifactComponents.length > 0
|
|
8139
8138
|
);
|
|
8140
8139
|
} catch (error) {
|
|
8141
8140
|
logger18.warn(
|
|
8142
8141
|
{
|
|
8143
|
-
|
|
8142
|
+
agentId: this.config.agentId,
|
|
8144
8143
|
tenantId: this.config.tenantId,
|
|
8145
8144
|
projectId: this.config.projectId,
|
|
8146
8145
|
error: error instanceof Error ? error.message : "Unknown error"
|
|
8147
8146
|
},
|
|
8148
|
-
"Failed to check
|
|
8147
|
+
"Failed to check agent artifact components, assuming none exist"
|
|
8149
8148
|
);
|
|
8150
8149
|
return this.artifactComponents.length > 0;
|
|
8151
8150
|
}
|
|
8152
8151
|
}
|
|
8153
8152
|
/**
|
|
8154
8153
|
* Build adaptive system prompt for Phase 2 structured output generation
|
|
8155
|
-
* based on configured data components and artifact components across the
|
|
8154
|
+
* based on configured data components and artifact components across the agent
|
|
8156
8155
|
*/
|
|
8157
8156
|
async buildPhase2SystemPrompt(runtimeContext) {
|
|
8158
8157
|
const phase2Config = new Phase2Config();
|
|
8159
|
-
const
|
|
8158
|
+
const hasAgentArtifactComponents = await this.hasAgentArtifactComponents();
|
|
8160
8159
|
const conversationId = runtimeContext?.metadata?.conversationId || runtimeContext?.contextId;
|
|
8161
8160
|
const resolvedContext = conversationId ? await this.getResolvedContext(conversationId) : null;
|
|
8162
8161
|
let processedPrompt = this.config.agentPrompt;
|
|
@@ -8196,7 +8195,7 @@ var Agent = class {
|
|
|
8196
8195
|
dataComponents: this.config.dataComponents || [],
|
|
8197
8196
|
artifactComponents: this.artifactComponents,
|
|
8198
8197
|
hasArtifactComponents: this.artifactComponents && this.artifactComponents.length > 0,
|
|
8199
|
-
|
|
8198
|
+
hasAgentArtifactComponents,
|
|
8200
8199
|
artifacts: referenceArtifacts
|
|
8201
8200
|
});
|
|
8202
8201
|
}
|
|
@@ -8260,10 +8259,10 @@ var Agent = class {
|
|
|
8260
8259
|
});
|
|
8261
8260
|
const componentDataComponents = excludeDataComponents ? [] : this.config.dataComponents || [];
|
|
8262
8261
|
const isThinkingPreparation = this.config.dataComponents && this.config.dataComponents.length > 0 && excludeDataComponents;
|
|
8263
|
-
let
|
|
8264
|
-
if (
|
|
8262
|
+
let agentPrompt = await this.getAgentPrompt();
|
|
8263
|
+
if (agentPrompt && resolvedContext) {
|
|
8265
8264
|
try {
|
|
8266
|
-
|
|
8265
|
+
agentPrompt = agentsCore.TemplateEngine.render(agentPrompt, resolvedContext, {
|
|
8267
8266
|
strict: false,
|
|
8268
8267
|
preserveUnresolved: false
|
|
8269
8268
|
});
|
|
@@ -8273,20 +8272,20 @@ var Agent = class {
|
|
|
8273
8272
|
conversationId,
|
|
8274
8273
|
error: error instanceof Error ? error.message : "Unknown error"
|
|
8275
8274
|
},
|
|
8276
|
-
"Failed to process
|
|
8275
|
+
"Failed to process agent prompt with context, using original"
|
|
8277
8276
|
);
|
|
8278
8277
|
}
|
|
8279
8278
|
}
|
|
8280
8279
|
const shouldIncludeArtifactComponents = !excludeDataComponents;
|
|
8281
|
-
const
|
|
8280
|
+
const hasAgentArtifactComponents = await this.hasAgentArtifactComponents();
|
|
8282
8281
|
const config = {
|
|
8283
8282
|
corePrompt: processedPrompt,
|
|
8284
|
-
|
|
8283
|
+
agentPrompt,
|
|
8285
8284
|
tools: toolDefinitions,
|
|
8286
8285
|
dataComponents: componentDataComponents,
|
|
8287
8286
|
artifacts: referenceArtifacts,
|
|
8288
8287
|
artifactComponents: shouldIncludeArtifactComponents ? this.artifactComponents : [],
|
|
8289
|
-
|
|
8288
|
+
hasAgentArtifactComponents,
|
|
8290
8289
|
isThinkingPreparation,
|
|
8291
8290
|
hasTransferRelations: (this.config.transferRelations?.length ?? 0) > 0,
|
|
8292
8291
|
hasDelegateRelations: (this.config.delegateRelations?.length ?? 0) > 0
|
|
@@ -8303,7 +8302,7 @@ var Agent = class {
|
|
|
8303
8302
|
execute: async ({ artifactId, toolCallId }) => {
|
|
8304
8303
|
logger18.info({ artifactId, toolCallId }, "get_artifact_full executed");
|
|
8305
8304
|
const streamRequestId = this.getStreamRequestId();
|
|
8306
|
-
const artifactService =
|
|
8305
|
+
const artifactService = agentSessionManager.getArtifactService(streamRequestId);
|
|
8307
8306
|
if (!artifactService) {
|
|
8308
8307
|
throw new Error(`ArtifactService not found for session ${streamRequestId}`);
|
|
8309
8308
|
}
|
|
@@ -8337,7 +8336,7 @@ var Agent = class {
|
|
|
8337
8336
|
// Provide a default tool set that is always available to the agent.
|
|
8338
8337
|
async getDefaultTools(streamRequestId) {
|
|
8339
8338
|
const defaultTools = {};
|
|
8340
|
-
if (await this.
|
|
8339
|
+
if (await this.agentHasArtifactComponents()) {
|
|
8341
8340
|
defaultTools.get_reference_artifact = this.getArtifactTools();
|
|
8342
8341
|
}
|
|
8343
8342
|
const hasStructuredOutput = this.config.dataComponents && this.config.dataComponents.length > 0;
|
|
@@ -8565,20 +8564,20 @@ var Agent = class {
|
|
|
8565
8564
|
return result;
|
|
8566
8565
|
}
|
|
8567
8566
|
}
|
|
8568
|
-
// Check if any agents in the
|
|
8569
|
-
async
|
|
8567
|
+
// Check if any agents in the agent have artifact components
|
|
8568
|
+
async agentHasArtifactComponents() {
|
|
8570
8569
|
try {
|
|
8571
|
-
return await agentsCore.
|
|
8570
|
+
return await agentsCore.agentHasArtifactComponents(dbClient_default)({
|
|
8572
8571
|
scopes: {
|
|
8573
8572
|
tenantId: this.config.tenantId,
|
|
8574
8573
|
projectId: this.config.projectId,
|
|
8575
|
-
|
|
8574
|
+
agentId: this.config.agentId
|
|
8576
8575
|
}
|
|
8577
8576
|
});
|
|
8578
8577
|
} catch (error) {
|
|
8579
8578
|
logger18.error(
|
|
8580
|
-
{ error,
|
|
8581
|
-
"Failed to check
|
|
8579
|
+
{ error, agentId: this.config.agentId },
|
|
8580
|
+
"Failed to check agent artifact components"
|
|
8582
8581
|
);
|
|
8583
8582
|
return false;
|
|
8584
8583
|
}
|
|
@@ -8710,7 +8709,7 @@ var Agent = class {
|
|
|
8710
8709
|
const last = steps.at(-1);
|
|
8711
8710
|
if (last && "text" in last && last.text) {
|
|
8712
8711
|
try {
|
|
8713
|
-
await
|
|
8712
|
+
await agentSessionManager.recordEvent(
|
|
8714
8713
|
this.getStreamRequestId(),
|
|
8715
8714
|
"agent_reasoning",
|
|
8716
8715
|
this.config.id,
|
|
@@ -8722,8 +8721,14 @@ var Agent = class {
|
|
|
8722
8721
|
logger18.debug({ error }, "Failed to track agent reasoning");
|
|
8723
8722
|
}
|
|
8724
8723
|
}
|
|
8725
|
-
if (
|
|
8726
|
-
|
|
8724
|
+
if (steps.length >= 2) {
|
|
8725
|
+
const previousStep = steps[steps.length - 2];
|
|
8726
|
+
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
8727
|
+
const hasTransferCall = previousStep.toolCalls.some((tc) => tc.toolName.startsWith("transfer_to_"));
|
|
8728
|
+
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
8729
|
+
return true;
|
|
8730
|
+
}
|
|
8731
|
+
}
|
|
8727
8732
|
}
|
|
8728
8733
|
return steps.length >= this.getMaxGenerationSteps();
|
|
8729
8734
|
},
|
|
@@ -8824,7 +8829,7 @@ var Agent = class {
|
|
|
8824
8829
|
const last = steps.at(-1);
|
|
8825
8830
|
if (last && "text" in last && last.text) {
|
|
8826
8831
|
try {
|
|
8827
|
-
await
|
|
8832
|
+
await agentSessionManager.recordEvent(
|
|
8828
8833
|
this.getStreamRequestId(),
|
|
8829
8834
|
"agent_reasoning",
|
|
8830
8835
|
this.config.id,
|
|
@@ -8836,10 +8841,16 @@ var Agent = class {
|
|
|
8836
8841
|
logger18.debug({ error }, "Failed to track agent reasoning");
|
|
8837
8842
|
}
|
|
8838
8843
|
}
|
|
8839
|
-
if (
|
|
8840
|
-
|
|
8841
|
-
|
|
8842
|
-
|
|
8844
|
+
if (steps.length >= 2) {
|
|
8845
|
+
const previousStep = steps[steps.length - 2];
|
|
8846
|
+
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
8847
|
+
const hasStopTool = previousStep.toolCalls.some(
|
|
8848
|
+
(tc) => tc.toolName.startsWith("transfer_to_") || tc.toolName === "thinking_complete"
|
|
8849
|
+
);
|
|
8850
|
+
if (hasStopTool && "toolResults" in previousStep && previousStep.toolResults) {
|
|
8851
|
+
return true;
|
|
8852
|
+
}
|
|
8853
|
+
}
|
|
8843
8854
|
}
|
|
8844
8855
|
return steps.length >= this.getMaxGenerationSteps();
|
|
8845
8856
|
},
|
|
@@ -9105,7 +9116,7 @@ ${output}${structureHintsFormatted}`;
|
|
|
9105
9116
|
};
|
|
9106
9117
|
if (streamRequestId) {
|
|
9107
9118
|
const generationType = response.object ? "object_generation" : "text_generation";
|
|
9108
|
-
|
|
9119
|
+
agentSessionManager.recordEvent(streamRequestId, "agent_generate", this.config.id, {
|
|
9109
9120
|
parts: (formattedContent?.parts || []).map((part) => ({
|
|
9110
9121
|
type: part.kind === "text" ? "text" : part.kind === "data" ? "tool_result" : "text",
|
|
9111
9122
|
content: part.text || JSON.stringify(part.data)
|
|
@@ -9145,11 +9156,11 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9145
9156
|
dataComponents,
|
|
9146
9157
|
artifactComponents
|
|
9147
9158
|
] = await Promise.all([
|
|
9148
|
-
agentsCore.
|
|
9159
|
+
agentsCore.getRelatedAgentsForAgent(dbClient_default)({
|
|
9149
9160
|
scopes: {
|
|
9150
9161
|
tenantId: config.tenantId,
|
|
9151
9162
|
projectId: config.projectId,
|
|
9152
|
-
|
|
9163
|
+
agentId: config.agentId
|
|
9153
9164
|
},
|
|
9154
9165
|
subAgentId: config.subAgentId
|
|
9155
9166
|
}),
|
|
@@ -9157,7 +9168,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9157
9168
|
scopes: {
|
|
9158
9169
|
tenantId: config.tenantId,
|
|
9159
9170
|
projectId: config.projectId,
|
|
9160
|
-
|
|
9171
|
+
agentId: config.agentId,
|
|
9161
9172
|
subAgentId: config.subAgentId
|
|
9162
9173
|
}
|
|
9163
9174
|
}),
|
|
@@ -9165,7 +9176,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9165
9176
|
scopes: {
|
|
9166
9177
|
tenantId: config.tenantId,
|
|
9167
9178
|
projectId: config.projectId,
|
|
9168
|
-
|
|
9179
|
+
agentId: config.agentId,
|
|
9169
9180
|
subAgentId: config.subAgentId
|
|
9170
9181
|
}
|
|
9171
9182
|
}),
|
|
@@ -9173,7 +9184,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9173
9184
|
scopes: {
|
|
9174
9185
|
tenantId: config.tenantId,
|
|
9175
9186
|
projectId: config.projectId,
|
|
9176
|
-
|
|
9187
|
+
agentId: config.agentId,
|
|
9177
9188
|
subAgentId: config.subAgentId
|
|
9178
9189
|
}
|
|
9179
9190
|
})
|
|
@@ -9186,16 +9197,16 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9186
9197
|
scopes: {
|
|
9187
9198
|
tenantId: config.tenantId,
|
|
9188
9199
|
projectId: config.projectId,
|
|
9189
|
-
|
|
9200
|
+
agentId: config.agentId
|
|
9190
9201
|
},
|
|
9191
9202
|
subAgentId: relation.id
|
|
9192
9203
|
});
|
|
9193
9204
|
if (relatedAgent) {
|
|
9194
|
-
const relatedAgentRelations = await agentsCore.
|
|
9205
|
+
const relatedAgentRelations = await agentsCore.getRelatedAgentsForAgent(dbClient_default)({
|
|
9195
9206
|
scopes: {
|
|
9196
9207
|
tenantId: config.tenantId,
|
|
9197
9208
|
projectId: config.projectId,
|
|
9198
|
-
|
|
9209
|
+
agentId: config.agentId
|
|
9199
9210
|
},
|
|
9200
9211
|
subAgentId: relation.id
|
|
9201
9212
|
});
|
|
@@ -9225,7 +9236,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9225
9236
|
id: config.subAgentId,
|
|
9226
9237
|
tenantId: config.tenantId,
|
|
9227
9238
|
projectId: config.projectId,
|
|
9228
|
-
|
|
9239
|
+
agentId: config.agentId,
|
|
9229
9240
|
baseUrl: config.baseUrl,
|
|
9230
9241
|
apiKey: config.apiKey,
|
|
9231
9242
|
name: config.name,
|
|
@@ -9237,7 +9248,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9237
9248
|
id: relation.id,
|
|
9238
9249
|
tenantId: config.tenantId,
|
|
9239
9250
|
projectId: config.projectId,
|
|
9240
|
-
|
|
9251
|
+
agentId: config.agentId,
|
|
9241
9252
|
baseUrl: config.baseUrl,
|
|
9242
9253
|
apiKey: config.apiKey,
|
|
9243
9254
|
name: relation.name,
|
|
@@ -9253,7 +9264,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9253
9264
|
id: relation.id,
|
|
9254
9265
|
tenantId: config.tenantId,
|
|
9255
9266
|
projectId: config.projectId,
|
|
9256
|
-
|
|
9267
|
+
agentId: config.agentId,
|
|
9257
9268
|
name: relation.name,
|
|
9258
9269
|
description: relation.description,
|
|
9259
9270
|
agentPrompt: "",
|
|
@@ -9269,7 +9280,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9269
9280
|
id: relation.id,
|
|
9270
9281
|
tenantId: config.tenantId,
|
|
9271
9282
|
projectId: config.projectId,
|
|
9272
|
-
|
|
9283
|
+
agentId: config.agentId,
|
|
9273
9284
|
baseUrl: config.baseUrl,
|
|
9274
9285
|
apiKey: config.apiKey,
|
|
9275
9286
|
name: relation.name,
|
|
@@ -9304,7 +9315,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9304
9315
|
);
|
|
9305
9316
|
const artifactStreamRequestId = task.context?.metadata?.streamRequestId;
|
|
9306
9317
|
if (artifactStreamRequestId && artifactComponents.length > 0) {
|
|
9307
|
-
|
|
9318
|
+
agentSessionManager.updateArtifactComponents(artifactStreamRequestId, artifactComponents);
|
|
9308
9319
|
}
|
|
9309
9320
|
let contextId = task.context?.conversationId;
|
|
9310
9321
|
if (!contextId || contextId === "default" || contextId === "") {
|
|
@@ -9332,7 +9343,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9332
9343
|
"Delegated agent - streaming disabled"
|
|
9333
9344
|
);
|
|
9334
9345
|
if (streamRequestId && config.tenantId && config.projectId) {
|
|
9335
|
-
toolSessionManager.
|
|
9346
|
+
toolSessionManager.ensureAgentSession(
|
|
9336
9347
|
streamRequestId,
|
|
9337
9348
|
config.tenantId,
|
|
9338
9349
|
config.projectId,
|
|
@@ -9364,17 +9375,42 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9364
9375
|
const toolResult = allToolResults.find(
|
|
9365
9376
|
(result) => result.toolCallId === toolCall.toolCallId
|
|
9366
9377
|
);
|
|
9378
|
+
logger19.info({
|
|
9379
|
+
toolCallName: toolCall.toolName,
|
|
9380
|
+
toolCallId: toolCall.toolCallId,
|
|
9381
|
+
hasToolResult: !!toolResult,
|
|
9382
|
+
toolResultOutput: toolResult?.output,
|
|
9383
|
+
toolResultKeys: toolResult?.output ? Object.keys(toolResult.output) : []
|
|
9384
|
+
}, "[DEBUG] Transfer tool result found");
|
|
9367
9385
|
const isValidTransferResult = (output) => {
|
|
9368
|
-
return typeof output === "object" && output !== null && "type" in output && "
|
|
9386
|
+
return typeof output === "object" && output !== null && "type" in output && "targetSubAgentId" in output && output.type === "transfer" && typeof output.targetSubAgentId === "string";
|
|
9369
9387
|
};
|
|
9370
9388
|
const responseText = response.text || (response.object ? JSON.stringify(response.object) : "");
|
|
9371
9389
|
const transferReason = responseText || allThoughts[allThoughts.length - 1]?.text || "Agent requested transfer. No reason provided.";
|
|
9372
9390
|
if (toolResult?.output && isValidTransferResult(toolResult.output)) {
|
|
9373
9391
|
const transferResult = toolResult.output;
|
|
9392
|
+
logger19.info({
|
|
9393
|
+
validationPassed: true,
|
|
9394
|
+
transferResult,
|
|
9395
|
+
targetSubAgentId: transferResult.targetSubAgentId,
|
|
9396
|
+
fromSubAgentId: transferResult.fromSubAgentId
|
|
9397
|
+
}, "[DEBUG] Transfer validation passed, extracted data");
|
|
9398
|
+
const artifactData = {
|
|
9399
|
+
type: "transfer",
|
|
9400
|
+
targetSubAgentId: transferResult.targetSubAgentId,
|
|
9401
|
+
fromSubAgentId: transferResult.fromSubAgentId,
|
|
9402
|
+
task_id: task.id,
|
|
9403
|
+
reason: transferReason,
|
|
9404
|
+
original_message: userMessage
|
|
9405
|
+
};
|
|
9406
|
+
logger19.info({
|
|
9407
|
+
artifactData,
|
|
9408
|
+
artifactDataKeys: Object.keys(artifactData)
|
|
9409
|
+
}, "[DEBUG] Artifact data being returned");
|
|
9374
9410
|
return {
|
|
9375
9411
|
status: {
|
|
9376
9412
|
state: agentsCore.TaskState.Completed,
|
|
9377
|
-
message: `Transfer requested to ${transferResult.
|
|
9413
|
+
message: `Transfer requested to ${transferResult.targetSubAgentId}`
|
|
9378
9414
|
},
|
|
9379
9415
|
artifacts: [
|
|
9380
9416
|
{
|
|
@@ -9382,18 +9418,19 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9382
9418
|
parts: [
|
|
9383
9419
|
{
|
|
9384
9420
|
kind: "data",
|
|
9385
|
-
data:
|
|
9386
|
-
type: "transfer",
|
|
9387
|
-
target: transferResult.target,
|
|
9388
|
-
task_id: task.id,
|
|
9389
|
-
reason: transferReason,
|
|
9390
|
-
original_message: userMessage
|
|
9391
|
-
}
|
|
9421
|
+
data: artifactData
|
|
9392
9422
|
}
|
|
9393
9423
|
]
|
|
9394
9424
|
}
|
|
9395
9425
|
]
|
|
9396
9426
|
};
|
|
9427
|
+
} else {
|
|
9428
|
+
logger19.warn({
|
|
9429
|
+
hasToolResult: !!toolResult,
|
|
9430
|
+
hasOutput: !!toolResult?.output,
|
|
9431
|
+
validationPassed: false,
|
|
9432
|
+
output: toolResult?.output
|
|
9433
|
+
}, "[DEBUG] Transfer validation FAILED");
|
|
9397
9434
|
}
|
|
9398
9435
|
}
|
|
9399
9436
|
}
|
|
@@ -9425,129 +9462,172 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9425
9462
|
};
|
|
9426
9463
|
};
|
|
9427
9464
|
var createTaskHandlerConfig = async (params) => {
|
|
9428
|
-
const
|
|
9465
|
+
const subAgent = await agentsCore.getSubAgentById(dbClient_default)({
|
|
9429
9466
|
scopes: {
|
|
9430
9467
|
tenantId: params.tenantId,
|
|
9431
9468
|
projectId: params.projectId,
|
|
9432
|
-
|
|
9469
|
+
agentId: params.agentId
|
|
9433
9470
|
},
|
|
9434
9471
|
subAgentId: params.subAgentId
|
|
9435
9472
|
});
|
|
9436
|
-
const
|
|
9473
|
+
const agent = await agentsCore.getAgentById(dbClient_default)({
|
|
9437
9474
|
scopes: {
|
|
9438
9475
|
tenantId: params.tenantId,
|
|
9439
9476
|
projectId: params.projectId,
|
|
9440
|
-
|
|
9477
|
+
agentId: params.agentId
|
|
9441
9478
|
}
|
|
9442
9479
|
});
|
|
9443
|
-
if (!
|
|
9480
|
+
if (!subAgent) {
|
|
9444
9481
|
throw new Error(`Agent not found: ${params.subAgentId}`);
|
|
9445
9482
|
}
|
|
9446
|
-
const effectiveModels = await resolveModelConfig(params.
|
|
9447
|
-
const effectiveConversationHistoryConfig =
|
|
9483
|
+
const effectiveModels = await resolveModelConfig(params.agentId, subAgent);
|
|
9484
|
+
const effectiveConversationHistoryConfig = subAgent.conversationHistoryConfig || { mode: "full" };
|
|
9448
9485
|
return {
|
|
9449
9486
|
tenantId: params.tenantId,
|
|
9450
9487
|
projectId: params.projectId,
|
|
9451
|
-
|
|
9488
|
+
agentId: params.agentId,
|
|
9452
9489
|
subAgentId: params.subAgentId,
|
|
9453
9490
|
agentSchema: {
|
|
9454
|
-
id:
|
|
9455
|
-
name:
|
|
9456
|
-
description:
|
|
9457
|
-
prompt:
|
|
9491
|
+
id: subAgent.id,
|
|
9492
|
+
name: subAgent.name,
|
|
9493
|
+
description: subAgent.description,
|
|
9494
|
+
prompt: subAgent.prompt,
|
|
9458
9495
|
models: effectiveModels,
|
|
9459
9496
|
conversationHistoryConfig: effectiveConversationHistoryConfig || null,
|
|
9460
|
-
stopWhen:
|
|
9461
|
-
createdAt:
|
|
9462
|
-
updatedAt:
|
|
9497
|
+
stopWhen: subAgent.stopWhen || null,
|
|
9498
|
+
createdAt: subAgent.createdAt,
|
|
9499
|
+
updatedAt: subAgent.updatedAt
|
|
9463
9500
|
},
|
|
9464
9501
|
baseUrl: params.baseUrl,
|
|
9465
9502
|
apiKey: params.apiKey,
|
|
9466
|
-
name:
|
|
9467
|
-
description:
|
|
9503
|
+
name: subAgent.name,
|
|
9504
|
+
description: subAgent.description,
|
|
9468
9505
|
conversationHistoryConfig: effectiveConversationHistoryConfig,
|
|
9469
|
-
contextConfigId:
|
|
9506
|
+
contextConfigId: agent?.contextConfigId || void 0
|
|
9470
9507
|
};
|
|
9471
9508
|
};
|
|
9472
9509
|
|
|
9473
|
-
// src/data/
|
|
9510
|
+
// src/data/agents.ts
|
|
9474
9511
|
init_dbClient();
|
|
9475
|
-
|
|
9476
|
-
|
|
9512
|
+
function createAgentCard({
|
|
9513
|
+
dbAgent,
|
|
9514
|
+
baseUrl
|
|
9515
|
+
}) {
|
|
9516
|
+
const description = dbAgent.description || "AI Agent";
|
|
9517
|
+
return {
|
|
9518
|
+
name: dbAgent.name,
|
|
9519
|
+
description,
|
|
9520
|
+
url: baseUrl ? `${baseUrl}/a2a` : "",
|
|
9521
|
+
version: "1.0.0",
|
|
9522
|
+
capabilities: {
|
|
9523
|
+
streaming: true,
|
|
9524
|
+
// Enable streaming for A2A compliance
|
|
9525
|
+
pushNotifications: false,
|
|
9526
|
+
stateTransitionHistory: false
|
|
9527
|
+
},
|
|
9528
|
+
defaultInputModes: ["text", "text/plain"],
|
|
9529
|
+
defaultOutputModes: ["text", "text/plain"],
|
|
9530
|
+
skills: [],
|
|
9531
|
+
// Add provider info if available
|
|
9532
|
+
...baseUrl && {
|
|
9533
|
+
provider: {
|
|
9534
|
+
organization: "Inkeep",
|
|
9535
|
+
url: baseUrl
|
|
9536
|
+
}
|
|
9537
|
+
}
|
|
9538
|
+
};
|
|
9539
|
+
}
|
|
9540
|
+
function generateDescriptionWithTransfers(baseDescription, internalRelations, externalRelations) {
|
|
9541
|
+
const transfers = [
|
|
9542
|
+
...internalRelations.filter((rel) => rel.relationType === "transfer"),
|
|
9543
|
+
...externalRelations.filter((rel) => rel.relationType === "transfer")
|
|
9544
|
+
];
|
|
9545
|
+
const delegates = [
|
|
9546
|
+
...internalRelations.filter((rel) => rel.relationType === "delegate"),
|
|
9547
|
+
...externalRelations.filter((rel) => rel.relationType === "delegate")
|
|
9548
|
+
];
|
|
9549
|
+
if (transfers.length === 0 && delegates.length === 0) {
|
|
9550
|
+
return baseDescription;
|
|
9551
|
+
}
|
|
9552
|
+
let enhancedDescription = baseDescription;
|
|
9553
|
+
if (transfers.length > 0) {
|
|
9554
|
+
const transferList = transfers.map((rel) => {
|
|
9555
|
+
const name = rel.externalAgent?.name || rel.name;
|
|
9556
|
+
const desc = rel.externalAgent?.description || rel.description || "";
|
|
9557
|
+
return `- ${name}: ${desc}`;
|
|
9558
|
+
}).join("\n");
|
|
9559
|
+
enhancedDescription += `
|
|
9560
|
+
|
|
9561
|
+
Can transfer to:
|
|
9562
|
+
${transferList}`;
|
|
9563
|
+
}
|
|
9564
|
+
if (delegates.length > 0) {
|
|
9565
|
+
const delegateList = delegates.map((rel) => {
|
|
9566
|
+
const name = rel.externalAgent?.name || rel.name;
|
|
9567
|
+
const desc = rel.externalAgent?.description || rel.description || "";
|
|
9568
|
+
return `- ${name}: ${desc}`;
|
|
9569
|
+
}).join("\n");
|
|
9570
|
+
enhancedDescription += `
|
|
9571
|
+
|
|
9572
|
+
Can delegate to:
|
|
9573
|
+
${delegateList}`;
|
|
9574
|
+
}
|
|
9575
|
+
return enhancedDescription;
|
|
9576
|
+
}
|
|
9577
|
+
async function hydrateAgent({
|
|
9578
|
+
dbAgent,
|
|
9579
|
+
agentId,
|
|
9477
9580
|
baseUrl,
|
|
9478
|
-
apiKey
|
|
9581
|
+
apiKey,
|
|
9582
|
+
credentialStoreRegistry
|
|
9479
9583
|
}) {
|
|
9480
9584
|
try {
|
|
9481
|
-
if (!dbGraph.defaultSubAgentId) {
|
|
9482
|
-
throw new Error(`Graph ${dbGraph.id} does not have a default agent configured`);
|
|
9483
|
-
}
|
|
9484
|
-
const defaultSubAgent = await agentsCore.getSubAgentById(dbClient_default)({
|
|
9485
|
-
scopes: {
|
|
9486
|
-
tenantId: dbGraph.tenantId,
|
|
9487
|
-
projectId: dbGraph.projectId,
|
|
9488
|
-
graphId: dbGraph.id
|
|
9489
|
-
},
|
|
9490
|
-
subAgentId: dbGraph.defaultSubAgentId
|
|
9491
|
-
});
|
|
9492
|
-
if (!defaultSubAgent) {
|
|
9493
|
-
throw new Error(
|
|
9494
|
-
`Default agent ${dbGraph.defaultSubAgentId} not found for graph ${dbGraph.id}`
|
|
9495
|
-
);
|
|
9496
|
-
}
|
|
9497
9585
|
const taskHandlerConfig = await createTaskHandlerConfig({
|
|
9498
|
-
tenantId:
|
|
9499
|
-
projectId:
|
|
9500
|
-
|
|
9501
|
-
subAgentId:
|
|
9586
|
+
tenantId: dbAgent.tenantId,
|
|
9587
|
+
projectId: dbAgent.projectId,
|
|
9588
|
+
agentId,
|
|
9589
|
+
subAgentId: dbAgent.id,
|
|
9502
9590
|
baseUrl,
|
|
9503
9591
|
apiKey
|
|
9504
9592
|
});
|
|
9505
|
-
const taskHandler = createTaskHandler(taskHandlerConfig);
|
|
9506
|
-
const agentCard = {
|
|
9507
|
-
|
|
9508
|
-
|
|
9509
|
-
|
|
9510
|
-
version: "1.0.0",
|
|
9511
|
-
capabilities: {
|
|
9512
|
-
streaming: true,
|
|
9513
|
-
// Enable streaming for A2A compliance
|
|
9514
|
-
pushNotifications: false,
|
|
9515
|
-
stateTransitionHistory: false
|
|
9516
|
-
},
|
|
9517
|
-
defaultInputModes: ["text", "text/plain"],
|
|
9518
|
-
defaultOutputModes: ["text", "text/plain"],
|
|
9519
|
-
skills: [],
|
|
9520
|
-
// TODO: Could aggregate skills from all agents in the graph
|
|
9521
|
-
// Add provider info if available
|
|
9522
|
-
...baseUrl && {
|
|
9523
|
-
provider: {
|
|
9524
|
-
organization: "Inkeep",
|
|
9525
|
-
url: baseUrl
|
|
9526
|
-
}
|
|
9527
|
-
}
|
|
9528
|
-
};
|
|
9593
|
+
const taskHandler = createTaskHandler(taskHandlerConfig, credentialStoreRegistry);
|
|
9594
|
+
const agentCard = createAgentCard({
|
|
9595
|
+
dbAgent,
|
|
9596
|
+
baseUrl
|
|
9597
|
+
});
|
|
9529
9598
|
return {
|
|
9530
|
-
subAgentId:
|
|
9531
|
-
|
|
9532
|
-
|
|
9533
|
-
|
|
9534
|
-
graphId: dbGraph.id,
|
|
9599
|
+
subAgentId: dbAgent.id,
|
|
9600
|
+
tenantId: dbAgent.tenantId,
|
|
9601
|
+
projectId: dbAgent.projectId,
|
|
9602
|
+
agentId,
|
|
9535
9603
|
agentCard,
|
|
9536
9604
|
taskHandler
|
|
9537
9605
|
};
|
|
9538
9606
|
} catch (error) {
|
|
9539
|
-
console.error(`\u274C Failed to hydrate
|
|
9607
|
+
console.error(`\u274C Failed to hydrate agent ${dbAgent.id}:`, error);
|
|
9540
9608
|
throw error;
|
|
9541
9609
|
}
|
|
9542
9610
|
}
|
|
9543
|
-
async function
|
|
9544
|
-
const { tenantId, projectId,
|
|
9545
|
-
|
|
9546
|
-
|
|
9611
|
+
async function getRegisteredAgent(executionContext, credentialStoreRegistry) {
|
|
9612
|
+
const { tenantId, projectId, agentId, subAgentId, baseUrl, apiKey } = executionContext;
|
|
9613
|
+
if (!subAgentId) {
|
|
9614
|
+
throw new Error("Agent ID is required");
|
|
9615
|
+
}
|
|
9616
|
+
const dbAgent = await agentsCore.getSubAgentById(dbClient_default)({
|
|
9617
|
+
scopes: { tenantId, projectId, agentId },
|
|
9618
|
+
subAgentId
|
|
9619
|
+
});
|
|
9620
|
+
if (!dbAgent) {
|
|
9547
9621
|
return null;
|
|
9548
9622
|
}
|
|
9549
9623
|
const agentFrameworkBaseUrl = `${baseUrl}/agents`;
|
|
9550
|
-
return
|
|
9624
|
+
return hydrateAgent({
|
|
9625
|
+
dbAgent,
|
|
9626
|
+
agentId,
|
|
9627
|
+
baseUrl: agentFrameworkBaseUrl,
|
|
9628
|
+
credentialStoreRegistry,
|
|
9629
|
+
apiKey
|
|
9630
|
+
});
|
|
9551
9631
|
}
|
|
9552
9632
|
|
|
9553
9633
|
// src/routes/agents.ts
|
|
@@ -9601,7 +9681,7 @@ app.openapi(
|
|
|
9601
9681
|
"OpenTelemetry headers: well-known agent.json"
|
|
9602
9682
|
);
|
|
9603
9683
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
9604
|
-
const { tenantId, projectId,
|
|
9684
|
+
const { tenantId, projectId, agentId, subAgentId } = executionContext;
|
|
9605
9685
|
console.dir("executionContext", executionContext);
|
|
9606
9686
|
if (subAgentId) {
|
|
9607
9687
|
logger20.info(
|
|
@@ -9609,7 +9689,7 @@ app.openapi(
|
|
|
9609
9689
|
message: "getRegisteredAgent (agent-level)",
|
|
9610
9690
|
tenantId,
|
|
9611
9691
|
projectId,
|
|
9612
|
-
|
|
9692
|
+
agentId,
|
|
9613
9693
|
subAgentId
|
|
9614
9694
|
},
|
|
9615
9695
|
"agent-level well-known agent.json"
|
|
@@ -9627,21 +9707,21 @@ app.openapi(
|
|
|
9627
9707
|
} else {
|
|
9628
9708
|
logger20.info(
|
|
9629
9709
|
{
|
|
9630
|
-
message: "
|
|
9710
|
+
message: "getRegisteredAgent (agent-level)",
|
|
9631
9711
|
tenantId,
|
|
9632
9712
|
projectId,
|
|
9633
|
-
|
|
9713
|
+
agentId
|
|
9634
9714
|
},
|
|
9635
|
-
"
|
|
9715
|
+
"agent-level well-known agent.json"
|
|
9636
9716
|
);
|
|
9637
|
-
const
|
|
9638
|
-
if (!
|
|
9717
|
+
const agent = await getRegisteredAgent(executionContext);
|
|
9718
|
+
if (!agent) {
|
|
9639
9719
|
throw agentsCore.createApiError({
|
|
9640
9720
|
code: "not_found",
|
|
9641
|
-
message: "
|
|
9721
|
+
message: "Agent not found"
|
|
9642
9722
|
});
|
|
9643
9723
|
}
|
|
9644
|
-
return c.json(
|
|
9724
|
+
return c.json(agent.agentCard);
|
|
9645
9725
|
}
|
|
9646
9726
|
}
|
|
9647
9727
|
);
|
|
@@ -9660,14 +9740,14 @@ app.post("/a2a", async (c) => {
|
|
|
9660
9740
|
"OpenTelemetry headers: a2a"
|
|
9661
9741
|
);
|
|
9662
9742
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
9663
|
-
const { tenantId, projectId,
|
|
9743
|
+
const { tenantId, projectId, agentId, subAgentId } = executionContext;
|
|
9664
9744
|
if (subAgentId) {
|
|
9665
9745
|
logger20.info(
|
|
9666
9746
|
{
|
|
9667
9747
|
message: "a2a (agent-level)",
|
|
9668
9748
|
tenantId,
|
|
9669
9749
|
projectId,
|
|
9670
|
-
|
|
9750
|
+
agentId,
|
|
9671
9751
|
subAgentId
|
|
9672
9752
|
},
|
|
9673
9753
|
"agent-level a2a endpoint"
|
|
@@ -9688,17 +9768,17 @@ app.post("/a2a", async (c) => {
|
|
|
9688
9768
|
} else {
|
|
9689
9769
|
logger20.info(
|
|
9690
9770
|
{
|
|
9691
|
-
message: "a2a (
|
|
9771
|
+
message: "a2a (agent-level)",
|
|
9692
9772
|
tenantId,
|
|
9693
9773
|
projectId,
|
|
9694
|
-
|
|
9774
|
+
agentId
|
|
9695
9775
|
},
|
|
9696
|
-
"
|
|
9776
|
+
"agent-level a2a endpoint"
|
|
9697
9777
|
);
|
|
9698
|
-
const
|
|
9699
|
-
scopes: { tenantId, projectId,
|
|
9778
|
+
const agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
9779
|
+
scopes: { tenantId, projectId, agentId }
|
|
9700
9780
|
});
|
|
9701
|
-
if (!
|
|
9781
|
+
if (!agent) {
|
|
9702
9782
|
return c.json(
|
|
9703
9783
|
{
|
|
9704
9784
|
jsonrpc: "2.0",
|
|
@@ -9708,17 +9788,17 @@ app.post("/a2a", async (c) => {
|
|
|
9708
9788
|
404
|
|
9709
9789
|
);
|
|
9710
9790
|
}
|
|
9711
|
-
if (!
|
|
9791
|
+
if (!agent.defaultSubAgentId) {
|
|
9712
9792
|
return c.json(
|
|
9713
9793
|
{
|
|
9714
9794
|
jsonrpc: "2.0",
|
|
9715
|
-
error: { code: -32004, message: "
|
|
9795
|
+
error: { code: -32004, message: "Agent does not have a default agent configured" },
|
|
9716
9796
|
id: null
|
|
9717
9797
|
},
|
|
9718
9798
|
400
|
|
9719
9799
|
);
|
|
9720
9800
|
}
|
|
9721
|
-
executionContext.subAgentId =
|
|
9801
|
+
executionContext.subAgentId = agent.defaultSubAgentId;
|
|
9722
9802
|
const credentialStores = c.get("credentialStores");
|
|
9723
9803
|
const defaultSubAgent = await getRegisteredAgent(executionContext, credentialStores);
|
|
9724
9804
|
if (!defaultSubAgent) {
|
|
@@ -9742,6 +9822,55 @@ init_dbClient();
|
|
|
9742
9822
|
// src/a2a/transfer.ts
|
|
9743
9823
|
init_dbClient();
|
|
9744
9824
|
init_logger();
|
|
9825
|
+
function isTransferTask(result) {
|
|
9826
|
+
console.log(
|
|
9827
|
+
"[isTransferTask] Checking result:",
|
|
9828
|
+
JSON.stringify(
|
|
9829
|
+
{
|
|
9830
|
+
hasArtifacts: "artifacts" in result,
|
|
9831
|
+
artifactsLength: result.kind === "task" ? result.artifacts?.length : 0,
|
|
9832
|
+
firstArtifactParts: result.kind === "task" ? result.artifacts?.[0]?.parts?.length : 0,
|
|
9833
|
+
allParts: result.kind === "task" ? result.artifacts?.[0]?.parts?.map((p, i) => ({
|
|
9834
|
+
index: i,
|
|
9835
|
+
kind: p.kind,
|
|
9836
|
+
hasData: !!(p.kind === "data" && p.data),
|
|
9837
|
+
dataType: p.kind === "data" ? p.data?.type : void 0,
|
|
9838
|
+
dataKeys: p.kind === "data" ? Object.keys(p.data) : []
|
|
9839
|
+
})) : []
|
|
9840
|
+
},
|
|
9841
|
+
null,
|
|
9842
|
+
2
|
|
9843
|
+
)
|
|
9844
|
+
);
|
|
9845
|
+
if (!("artifacts" in result) || !result.artifacts) {
|
|
9846
|
+
console.log("[isTransferTask] No artifacts found");
|
|
9847
|
+
return false;
|
|
9848
|
+
}
|
|
9849
|
+
const hasTransfer = result.artifacts.some(
|
|
9850
|
+
(artifact) => artifact.parts.some((part) => {
|
|
9851
|
+
if (part.kind !== "data" || !part.data) return false;
|
|
9852
|
+
const isTransfer = typeof part.data === "object" && "type" in part.data && part.data.type === "transfer";
|
|
9853
|
+
if (isTransfer) {
|
|
9854
|
+
console.log("[isTransferTask] Found transfer data:", JSON.stringify(part.data, null, 2));
|
|
9855
|
+
}
|
|
9856
|
+
return isTransfer;
|
|
9857
|
+
})
|
|
9858
|
+
);
|
|
9859
|
+
console.log("[isTransferTask] Result:", hasTransfer);
|
|
9860
|
+
return hasTransfer;
|
|
9861
|
+
}
|
|
9862
|
+
function extractTransferData(task) {
|
|
9863
|
+
for (const artifact of task.artifacts) {
|
|
9864
|
+
for (const part of artifact.parts) {
|
|
9865
|
+
if (part.kind === "data" && part.data?.type === "transfer") {
|
|
9866
|
+
return part.data;
|
|
9867
|
+
}
|
|
9868
|
+
}
|
|
9869
|
+
}
|
|
9870
|
+
return null;
|
|
9871
|
+
}
|
|
9872
|
+
|
|
9873
|
+
// src/a2a/transfer.ts
|
|
9745
9874
|
var logger21 = agentsCore.getLogger("Transfer");
|
|
9746
9875
|
async function executeTransfer({
|
|
9747
9876
|
tenantId,
|
|
@@ -9749,19 +9878,25 @@ async function executeTransfer({
|
|
|
9749
9878
|
projectId,
|
|
9750
9879
|
targetSubAgentId
|
|
9751
9880
|
}) {
|
|
9752
|
-
logger21.info({
|
|
9753
|
-
|
|
9754
|
-
scopes: { tenantId, projectId },
|
|
9881
|
+
logger21.info({
|
|
9882
|
+
targetAgent: targetSubAgentId,
|
|
9755
9883
|
threadId,
|
|
9756
|
-
|
|
9757
|
-
|
|
9884
|
+
tenantId,
|
|
9885
|
+
projectId
|
|
9886
|
+
}, "Executing transfer - calling setActiveAgentForThread");
|
|
9887
|
+
try {
|
|
9888
|
+
await agentsCore.setActiveAgentForThread(dbClient_default)({
|
|
9889
|
+
scopes: { tenantId, projectId },
|
|
9890
|
+
threadId,
|
|
9891
|
+
subAgentId: targetSubAgentId
|
|
9892
|
+
});
|
|
9893
|
+
logger21.info({ targetAgent: targetSubAgentId, threadId }, "Successfully updated active_sub_agent_id in database");
|
|
9894
|
+
} catch (error) {
|
|
9895
|
+
logger21.error({ error, targetAgent: targetSubAgentId, threadId }, "Failed to update active_sub_agent_id");
|
|
9896
|
+
throw error;
|
|
9897
|
+
}
|
|
9758
9898
|
return { success: true, targetSubAgentId };
|
|
9759
9899
|
}
|
|
9760
|
-
function isTransferResponse(result) {
|
|
9761
|
-
return result?.artifacts.some(
|
|
9762
|
-
(artifact) => artifact.parts.some((part) => part.kind === "data" && part.data?.type === "transfer")
|
|
9763
|
-
);
|
|
9764
|
-
}
|
|
9765
9900
|
|
|
9766
9901
|
// src/handlers/executionHandler.ts
|
|
9767
9902
|
init_dbClient();
|
|
@@ -10365,24 +10500,26 @@ var ExecutionHandler = class {
|
|
|
10365
10500
|
sseHelper,
|
|
10366
10501
|
emitOperations
|
|
10367
10502
|
} = params;
|
|
10368
|
-
const { tenantId, projectId,
|
|
10503
|
+
const { tenantId, projectId, agentId, apiKey, baseUrl } = executionContext;
|
|
10369
10504
|
registerStreamHelper(requestId2, sseHelper);
|
|
10370
|
-
|
|
10505
|
+
agentSessionManager.createSession(requestId2, agentId, tenantId, projectId, conversationId);
|
|
10371
10506
|
if (emitOperations) {
|
|
10372
|
-
|
|
10507
|
+
agentSessionManager.enableEmitOperations(requestId2);
|
|
10373
10508
|
}
|
|
10374
10509
|
logger22.info(
|
|
10375
|
-
{ sessionId: requestId2,
|
|
10376
|
-
"Created
|
|
10510
|
+
{ sessionId: requestId2, agentId, conversationId, emitOperations },
|
|
10511
|
+
"Created AgentSession for message execution"
|
|
10377
10512
|
);
|
|
10378
|
-
let
|
|
10513
|
+
let agentConfig = null;
|
|
10379
10514
|
try {
|
|
10380
|
-
|
|
10381
|
-
|
|
10382
|
-
|
|
10515
|
+
agentConfig = await agentsCore.getFullAgent(dbClient_default)({
|
|
10516
|
+
scopes: { tenantId, projectId, agentId }
|
|
10517
|
+
});
|
|
10518
|
+
if (agentConfig?.statusUpdates && agentConfig.statusUpdates.enabled !== false) {
|
|
10519
|
+
agentSessionManager.initializeStatusUpdates(
|
|
10383
10520
|
requestId2,
|
|
10384
|
-
|
|
10385
|
-
|
|
10521
|
+
agentConfig.statusUpdates,
|
|
10522
|
+
agentConfig.models?.summarizer
|
|
10386
10523
|
);
|
|
10387
10524
|
}
|
|
10388
10525
|
} catch (error) {
|
|
@@ -10398,9 +10535,9 @@ var ExecutionHandler = class {
|
|
|
10398
10535
|
let iterations = 0;
|
|
10399
10536
|
let errorCount = 0;
|
|
10400
10537
|
let task = null;
|
|
10401
|
-
let
|
|
10538
|
+
let fromSubAgentId;
|
|
10402
10539
|
try {
|
|
10403
|
-
await sseHelper.writeOperation(agentInitializingOp(requestId2,
|
|
10540
|
+
await sseHelper.writeOperation(agentInitializingOp(requestId2, agentId));
|
|
10404
10541
|
const taskId = `task_${conversationId}-${requestId2}`;
|
|
10405
10542
|
logger22.info(
|
|
10406
10543
|
{ taskId, currentAgentId, conversationId, requestId: requestId2 },
|
|
@@ -10411,7 +10548,7 @@ var ExecutionHandler = class {
|
|
|
10411
10548
|
id: taskId,
|
|
10412
10549
|
tenantId,
|
|
10413
10550
|
projectId,
|
|
10414
|
-
|
|
10551
|
+
agentId,
|
|
10415
10552
|
subAgentId: currentAgentId,
|
|
10416
10553
|
contextId: conversationId,
|
|
10417
10554
|
status: "pending",
|
|
@@ -10419,11 +10556,11 @@ var ExecutionHandler = class {
|
|
|
10419
10556
|
conversation_id: conversationId,
|
|
10420
10557
|
message_id: requestId2,
|
|
10421
10558
|
stream_request_id: requestId2,
|
|
10422
|
-
// This also serves as the
|
|
10559
|
+
// This also serves as the AgentSession ID
|
|
10423
10560
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10424
10561
|
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10425
|
-
|
|
10426
|
-
|
|
10562
|
+
root_sub_agent_id: initialAgentId,
|
|
10563
|
+
sub_agent_id: currentAgentId
|
|
10427
10564
|
}
|
|
10428
10565
|
});
|
|
10429
10566
|
logger22.info(
|
|
@@ -10460,6 +10597,7 @@ var ExecutionHandler = class {
|
|
|
10460
10597
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10461
10598
|
executionType: "create_initial_task",
|
|
10462
10599
|
conversationId,
|
|
10600
|
+
agentId,
|
|
10463
10601
|
requestId: requestId2,
|
|
10464
10602
|
currentAgentId,
|
|
10465
10603
|
taskId: Array.isArray(task) ? task[0]?.id : task?.id,
|
|
@@ -10470,12 +10608,12 @@ var ExecutionHandler = class {
|
|
|
10470
10608
|
);
|
|
10471
10609
|
if (Array.isArray(task)) task = task[0];
|
|
10472
10610
|
let currentMessage = userMessage;
|
|
10473
|
-
const maxTransfers =
|
|
10611
|
+
const maxTransfers = agentConfig?.stopWhen?.transferCountIs ?? 10;
|
|
10474
10612
|
while (iterations < maxTransfers) {
|
|
10475
10613
|
iterations++;
|
|
10476
10614
|
logger22.info(
|
|
10477
|
-
{ iterations, currentAgentId,
|
|
10478
|
-
`Execution loop iteration ${iterations} with agent ${currentAgentId}, transfer from: ${
|
|
10615
|
+
{ iterations, currentAgentId, agentId, conversationId, fromSubAgentId },
|
|
10616
|
+
`Execution loop iteration ${iterations} with agent ${currentAgentId}, transfer from: ${fromSubAgentId || "none"}`
|
|
10479
10617
|
);
|
|
10480
10618
|
const activeAgent = await agentsCore.getActiveAgentForConversation(dbClient_default)({
|
|
10481
10619
|
scopes: { tenantId, projectId },
|
|
@@ -10492,17 +10630,17 @@ var ExecutionHandler = class {
|
|
|
10492
10630
|
Authorization: `Bearer ${apiKey}`,
|
|
10493
10631
|
"x-inkeep-tenant-id": tenantId,
|
|
10494
10632
|
"x-inkeep-project-id": projectId,
|
|
10495
|
-
"x-inkeep-
|
|
10496
|
-
"x-inkeep-agent-id": currentAgentId
|
|
10633
|
+
"x-inkeep-agent-id": agentId,
|
|
10634
|
+
"x-inkeep-sub-agent-id": currentAgentId
|
|
10497
10635
|
}
|
|
10498
10636
|
});
|
|
10499
10637
|
let messageResponse = null;
|
|
10500
10638
|
const messageMetadata = {
|
|
10501
10639
|
stream_request_id: requestId2
|
|
10502
|
-
// This also serves as the
|
|
10640
|
+
// This also serves as the AgentSession ID
|
|
10503
10641
|
};
|
|
10504
|
-
if (
|
|
10505
|
-
messageMetadata.
|
|
10642
|
+
if (fromSubAgentId) {
|
|
10643
|
+
messageMetadata.fromSubAgentId = fromSubAgentId;
|
|
10506
10644
|
}
|
|
10507
10645
|
messageResponse = await a2aClient.sendMessage({
|
|
10508
10646
|
message: {
|
|
@@ -10546,17 +10684,28 @@ var ExecutionHandler = class {
|
|
|
10546
10684
|
}
|
|
10547
10685
|
});
|
|
10548
10686
|
}
|
|
10549
|
-
|
|
10687
|
+
agentSessionManager.endSession(requestId2);
|
|
10550
10688
|
unregisterStreamHelper(requestId2);
|
|
10551
10689
|
return { success: false, error: errorMessage2, iterations };
|
|
10552
10690
|
}
|
|
10553
10691
|
continue;
|
|
10554
10692
|
}
|
|
10555
|
-
if (
|
|
10556
|
-
const
|
|
10557
|
-
|
|
10558
|
-
|
|
10559
|
-
|
|
10693
|
+
if (isTransferTask(messageResponse.result)) {
|
|
10694
|
+
const transferData = extractTransferData(messageResponse.result);
|
|
10695
|
+
if (!transferData) {
|
|
10696
|
+
logger22.error(
|
|
10697
|
+
{ result: messageResponse.result },
|
|
10698
|
+
"Transfer detected but no transfer data found"
|
|
10699
|
+
);
|
|
10700
|
+
continue;
|
|
10701
|
+
}
|
|
10702
|
+
const { targetSubAgentId, fromSubAgentId: transferFromAgent } = transferData;
|
|
10703
|
+
const firstArtifact = messageResponse.result.artifacts[0];
|
|
10704
|
+
const transferReason = firstArtifact?.parts[1]?.kind === "text" ? firstArtifact.parts[1].text : "Transfer initiated";
|
|
10705
|
+
logger22.info(
|
|
10706
|
+
{ targetSubAgentId, transferReason, transferFromAgent },
|
|
10707
|
+
"Transfer response"
|
|
10708
|
+
);
|
|
10560
10709
|
currentMessage = `<transfer_context> ${transferReason} </transfer_context>`;
|
|
10561
10710
|
const { success, targetSubAgentId: newAgentId } = await executeTransfer({
|
|
10562
10711
|
projectId,
|
|
@@ -10565,15 +10714,15 @@ var ExecutionHandler = class {
|
|
|
10565
10714
|
targetSubAgentId
|
|
10566
10715
|
});
|
|
10567
10716
|
if (success) {
|
|
10568
|
-
|
|
10717
|
+
fromSubAgentId = currentAgentId;
|
|
10569
10718
|
currentAgentId = newAgentId;
|
|
10570
10719
|
logger22.info(
|
|
10571
10720
|
{
|
|
10572
|
-
transferFrom:
|
|
10721
|
+
transferFrom: fromSubAgentId,
|
|
10573
10722
|
transferTo: currentAgentId,
|
|
10574
10723
|
reason: transferReason
|
|
10575
10724
|
},
|
|
10576
|
-
"Transfer executed, tracking
|
|
10725
|
+
"Transfer executed, tracking fromSubAgentId for next iteration"
|
|
10577
10726
|
);
|
|
10578
10727
|
}
|
|
10579
10728
|
continue;
|
|
@@ -10595,10 +10744,10 @@ var ExecutionHandler = class {
|
|
|
10595
10744
|
);
|
|
10596
10745
|
}
|
|
10597
10746
|
if (responseParts && responseParts.length > 0) {
|
|
10598
|
-
const
|
|
10599
|
-
if (
|
|
10600
|
-
const sessionSummary =
|
|
10601
|
-
logger22.info(sessionSummary, "
|
|
10747
|
+
const agentSessionData = agentSessionManager.getSession(requestId2);
|
|
10748
|
+
if (agentSessionData) {
|
|
10749
|
+
const sessionSummary = agentSessionData.getSummary();
|
|
10750
|
+
logger22.info(sessionSummary, "AgentSession data after completion");
|
|
10602
10751
|
}
|
|
10603
10752
|
let textContent = "";
|
|
10604
10753
|
for (const part of responseParts) {
|
|
@@ -10657,8 +10806,8 @@ var ExecutionHandler = class {
|
|
|
10657
10806
|
);
|
|
10658
10807
|
await sseHelper.writeOperation(completionOp(currentAgentId, iterations));
|
|
10659
10808
|
await sseHelper.complete();
|
|
10660
|
-
logger22.info({}, "Ending
|
|
10661
|
-
|
|
10809
|
+
logger22.info({}, "Ending AgentSession and cleaning up");
|
|
10810
|
+
agentSessionManager.endSession(requestId2);
|
|
10662
10811
|
logger22.info({}, "Cleaning up streamHelper");
|
|
10663
10812
|
unregisterStreamHelper(requestId2);
|
|
10664
10813
|
let response;
|
|
@@ -10698,7 +10847,7 @@ var ExecutionHandler = class {
|
|
|
10698
10847
|
}
|
|
10699
10848
|
});
|
|
10700
10849
|
}
|
|
10701
|
-
|
|
10850
|
+
agentSessionManager.endSession(requestId2);
|
|
10702
10851
|
unregisterStreamHelper(requestId2);
|
|
10703
10852
|
return { success: false, error: errorMessage2, iterations };
|
|
10704
10853
|
}
|
|
@@ -10719,7 +10868,7 @@ var ExecutionHandler = class {
|
|
|
10719
10868
|
}
|
|
10720
10869
|
});
|
|
10721
10870
|
}
|
|
10722
|
-
|
|
10871
|
+
agentSessionManager.endSession(requestId2);
|
|
10723
10872
|
unregisterStreamHelper(requestId2);
|
|
10724
10873
|
return { success: false, error: errorMessage, iterations };
|
|
10725
10874
|
} catch (error) {
|
|
@@ -10741,7 +10890,7 @@ var ExecutionHandler = class {
|
|
|
10741
10890
|
}
|
|
10742
10891
|
});
|
|
10743
10892
|
}
|
|
10744
|
-
|
|
10893
|
+
agentSessionManager.endSession(requestId2);
|
|
10745
10894
|
unregisterStreamHelper(requestId2);
|
|
10746
10895
|
return { success: false, error: errorMessage, iterations };
|
|
10747
10896
|
}
|
|
@@ -10757,7 +10906,7 @@ var chatCompletionsRoute = zodOpenapi.createRoute({
|
|
|
10757
10906
|
path: "/completions",
|
|
10758
10907
|
tags: ["chat"],
|
|
10759
10908
|
summary: "Create chat completion",
|
|
10760
|
-
description: "Creates a new chat completion with streaming SSE response using the configured agent
|
|
10909
|
+
description: "Creates a new chat completion with streaming SSE response using the configured agent",
|
|
10761
10910
|
security: [{ bearerAuth: [] }],
|
|
10762
10911
|
request: {
|
|
10763
10912
|
body: {
|
|
@@ -10832,7 +10981,7 @@ var chatCompletionsRoute = zodOpenapi.createRoute({
|
|
|
10832
10981
|
}
|
|
10833
10982
|
},
|
|
10834
10983
|
404: {
|
|
10835
|
-
description: "Agent
|
|
10984
|
+
description: "Agent or agent not found",
|
|
10836
10985
|
content: {
|
|
10837
10986
|
"application/json": {
|
|
10838
10987
|
schema: z6.z.object({
|
|
@@ -10879,48 +11028,48 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
10879
11028
|
);
|
|
10880
11029
|
try {
|
|
10881
11030
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
10882
|
-
const { tenantId, projectId,
|
|
11031
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
10883
11032
|
agentsCore.getLogger("chat").debug(
|
|
10884
11033
|
{
|
|
10885
11034
|
tenantId,
|
|
10886
|
-
|
|
11035
|
+
agentId
|
|
10887
11036
|
},
|
|
10888
11037
|
"Extracted chat parameters from API key context"
|
|
10889
11038
|
);
|
|
10890
11039
|
const body = c.get("requestBody") || {};
|
|
10891
11040
|
const conversationId = body.conversationId || agentsCore.getConversationId();
|
|
10892
|
-
const
|
|
10893
|
-
scopes: { tenantId, projectId,
|
|
11041
|
+
const fullAgent = await agentsCore.getFullAgent(dbClient_default)({
|
|
11042
|
+
scopes: { tenantId, projectId, agentId }
|
|
10894
11043
|
});
|
|
10895
|
-
let
|
|
11044
|
+
let agent;
|
|
10896
11045
|
let defaultSubAgentId;
|
|
10897
|
-
if (
|
|
10898
|
-
|
|
10899
|
-
id:
|
|
10900
|
-
name:
|
|
11046
|
+
if (fullAgent) {
|
|
11047
|
+
agent = {
|
|
11048
|
+
id: fullAgent.id,
|
|
11049
|
+
name: fullAgent.name,
|
|
10901
11050
|
tenantId,
|
|
10902
11051
|
projectId,
|
|
10903
|
-
defaultSubAgentId:
|
|
11052
|
+
defaultSubAgentId: fullAgent.defaultSubAgentId
|
|
10904
11053
|
};
|
|
10905
|
-
const agentKeys = Object.keys(
|
|
11054
|
+
const agentKeys = Object.keys(fullAgent.subAgents || {});
|
|
10906
11055
|
const firstAgentId = agentKeys.length > 0 ? agentKeys[0] : "";
|
|
10907
|
-
defaultSubAgentId =
|
|
11056
|
+
defaultSubAgentId = fullAgent.defaultSubAgentId || firstAgentId;
|
|
10908
11057
|
} else {
|
|
10909
|
-
|
|
10910
|
-
scopes: { tenantId, projectId,
|
|
11058
|
+
agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11059
|
+
scopes: { tenantId, projectId, agentId }
|
|
10911
11060
|
});
|
|
10912
|
-
if (!
|
|
11061
|
+
if (!agent) {
|
|
10913
11062
|
throw agentsCore.createApiError({
|
|
10914
11063
|
code: "not_found",
|
|
10915
|
-
message: "Agent
|
|
11064
|
+
message: "Agent not found"
|
|
10916
11065
|
});
|
|
10917
11066
|
}
|
|
10918
|
-
defaultSubAgentId =
|
|
11067
|
+
defaultSubAgentId = agent.defaultSubAgentId || "";
|
|
10919
11068
|
}
|
|
10920
11069
|
if (!defaultSubAgentId) {
|
|
10921
11070
|
throw agentsCore.createApiError({
|
|
10922
11071
|
code: "not_found",
|
|
10923
|
-
message: "No default agent found in
|
|
11072
|
+
message: "No default agent found in agent"
|
|
10924
11073
|
});
|
|
10925
11074
|
}
|
|
10926
11075
|
await agentsCore.createOrGetConversation(dbClient_default)({
|
|
@@ -10942,7 +11091,7 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
10942
11091
|
}
|
|
10943
11092
|
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
10944
11093
|
const agentInfo = await agentsCore.getSubAgentById(dbClient_default)({
|
|
10945
|
-
scopes: { tenantId, projectId,
|
|
11094
|
+
scopes: { tenantId, projectId, agentId },
|
|
10946
11095
|
subAgentId
|
|
10947
11096
|
});
|
|
10948
11097
|
if (!agentInfo) {
|
|
@@ -10956,7 +11105,7 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
10956
11105
|
await agentsCore.handleContextResolution({
|
|
10957
11106
|
tenantId,
|
|
10958
11107
|
projectId,
|
|
10959
|
-
|
|
11108
|
+
agentId,
|
|
10960
11109
|
conversationId,
|
|
10961
11110
|
headers: validatedContext,
|
|
10962
11111
|
dbClient: dbClient_default,
|
|
@@ -10966,11 +11115,11 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
10966
11115
|
{
|
|
10967
11116
|
tenantId,
|
|
10968
11117
|
projectId,
|
|
10969
|
-
|
|
11118
|
+
agentId,
|
|
10970
11119
|
conversationId,
|
|
10971
11120
|
defaultSubAgentId,
|
|
10972
11121
|
activeSubAgentId: activeAgent?.activeSubAgentId || "none",
|
|
10973
|
-
hasContextConfig: !!
|
|
11122
|
+
hasContextConfig: !!agent.contextConfigId,
|
|
10974
11123
|
hasHeaders: !!body.headers,
|
|
10975
11124
|
hasValidatedContext: !!validatedContext,
|
|
10976
11125
|
validatedContextKeys: Object.keys(validatedContext)
|
|
@@ -11139,8 +11288,8 @@ app3.use("/chat", agentsCore.contextValidationMiddleware(dbClient_default));
|
|
|
11139
11288
|
app3.openapi(chatDataStreamRoute, async (c) => {
|
|
11140
11289
|
try {
|
|
11141
11290
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
11142
|
-
const { tenantId, projectId,
|
|
11143
|
-
agentsCore.loggerFactory.getLogger("chatDataStream").debug({ tenantId, projectId,
|
|
11291
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
11292
|
+
agentsCore.loggerFactory.getLogger("chatDataStream").debug({ tenantId, projectId, agentId }, "Extracted chatDataStream parameters");
|
|
11144
11293
|
const body = c.get("requestBody") || {};
|
|
11145
11294
|
const conversationId = body.conversationId || agentsCore.getConversationId();
|
|
11146
11295
|
const activeSpan = api.trace.getActiveSpan();
|
|
@@ -11148,25 +11297,25 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
11148
11297
|
activeSpan.setAttributes({
|
|
11149
11298
|
"conversation.id": conversationId,
|
|
11150
11299
|
"tenant.id": tenantId,
|
|
11151
|
-
"
|
|
11300
|
+
"agent.id": agentId,
|
|
11152
11301
|
"project.id": projectId
|
|
11153
11302
|
});
|
|
11154
11303
|
}
|
|
11155
|
-
const
|
|
11156
|
-
scopes: { tenantId, projectId,
|
|
11304
|
+
const agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11305
|
+
scopes: { tenantId, projectId, agentId }
|
|
11157
11306
|
});
|
|
11158
|
-
if (!
|
|
11307
|
+
if (!agent) {
|
|
11159
11308
|
throw agentsCore.createApiError({
|
|
11160
11309
|
code: "not_found",
|
|
11161
|
-
message: "Agent
|
|
11310
|
+
message: "Agent not found"
|
|
11162
11311
|
});
|
|
11163
11312
|
}
|
|
11164
|
-
const defaultSubAgentId =
|
|
11165
|
-
const
|
|
11313
|
+
const defaultSubAgentId = agent.defaultSubAgentId;
|
|
11314
|
+
const agentName = agent.name;
|
|
11166
11315
|
if (!defaultSubAgentId) {
|
|
11167
11316
|
throw agentsCore.createApiError({
|
|
11168
11317
|
code: "bad_request",
|
|
11169
|
-
message: "
|
|
11318
|
+
message: "Agent does not have a default agent configured"
|
|
11170
11319
|
});
|
|
11171
11320
|
}
|
|
11172
11321
|
const activeAgent = await agentsCore.getActiveAgentForConversation(dbClient_default)({
|
|
@@ -11182,7 +11331,7 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
11182
11331
|
}
|
|
11183
11332
|
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
11184
11333
|
const agentInfo = await agentsCore.getSubAgentById(dbClient_default)({
|
|
11185
|
-
scopes: { tenantId, projectId,
|
|
11334
|
+
scopes: { tenantId, projectId, agentId },
|
|
11186
11335
|
subAgentId
|
|
11187
11336
|
});
|
|
11188
11337
|
if (!agentInfo) {
|
|
@@ -11196,7 +11345,7 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
11196
11345
|
await agentsCore.handleContextResolution({
|
|
11197
11346
|
tenantId,
|
|
11198
11347
|
projectId,
|
|
11199
|
-
|
|
11348
|
+
agentId,
|
|
11200
11349
|
conversationId,
|
|
11201
11350
|
headers: validatedContext,
|
|
11202
11351
|
dbClient: dbClient_default,
|
|
@@ -11210,7 +11359,7 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
11210
11359
|
messageSpan.setAttributes({
|
|
11211
11360
|
"message.timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
11212
11361
|
"message.content": userText,
|
|
11213
|
-
"
|
|
11362
|
+
"agent.name": agentName
|
|
11214
11363
|
});
|
|
11215
11364
|
}
|
|
11216
11365
|
await agentsCore.createMessage(dbClient_default)({
|
|
@@ -11350,7 +11499,7 @@ var spoofTransportInitialization = async (transport, req, sessionId, mcpProtocol
|
|
|
11350
11499
|
logger25.warn({ sessionId, error: spoofError }, "Spoof initialization failed, continuing anyway");
|
|
11351
11500
|
}
|
|
11352
11501
|
};
|
|
11353
|
-
var validateSession = async (req, res, body, tenantId, projectId,
|
|
11502
|
+
var validateSession = async (req, res, body, tenantId, projectId, agentId) => {
|
|
11354
11503
|
const sessionId = req.headers["mcp-session-id"];
|
|
11355
11504
|
logger25.info({ sessionId }, "Received MCP session ID");
|
|
11356
11505
|
if (!sessionId) {
|
|
@@ -11385,12 +11534,12 @@ var validateSession = async (req, res, body, tenantId, projectId, graphId) => {
|
|
|
11385
11534
|
sessionId,
|
|
11386
11535
|
conversationFound: !!conversation,
|
|
11387
11536
|
sessionType: conversation?.metadata?.sessionData?.sessionType,
|
|
11388
|
-
|
|
11389
|
-
|
|
11537
|
+
storedAgentId: conversation?.metadata?.sessionData?.agentId,
|
|
11538
|
+
requestAgentId: agentId
|
|
11390
11539
|
},
|
|
11391
11540
|
"Conversation lookup result"
|
|
11392
11541
|
);
|
|
11393
|
-
if (!conversation || conversation.metadata?.sessionData?.sessionType !== "mcp" || conversation.metadata?.sessionData?.
|
|
11542
|
+
if (!conversation || conversation.metadata?.sessionData?.sessionType !== "mcp" || conversation.metadata?.sessionData?.agentId !== agentId) {
|
|
11394
11543
|
logger25.info(
|
|
11395
11544
|
{ sessionId, conversationId: conversation?.id },
|
|
11396
11545
|
"MCP session not found or invalid"
|
|
@@ -11409,13 +11558,13 @@ var validateSession = async (req, res, body, tenantId, projectId, graphId) => {
|
|
|
11409
11558
|
}
|
|
11410
11559
|
return conversation;
|
|
11411
11560
|
};
|
|
11412
|
-
var setupTracing = (conversationId, tenantId,
|
|
11561
|
+
var setupTracing = (conversationId, tenantId, agentId) => {
|
|
11413
11562
|
const activeSpan = api.trace.getActiveSpan();
|
|
11414
11563
|
if (activeSpan) {
|
|
11415
11564
|
activeSpan.setAttributes({
|
|
11416
11565
|
"conversation.id": conversationId,
|
|
11417
11566
|
"tenant.id": tenantId,
|
|
11418
|
-
"
|
|
11567
|
+
"agent.id": agentId
|
|
11419
11568
|
});
|
|
11420
11569
|
}
|
|
11421
11570
|
};
|
|
@@ -11477,13 +11626,13 @@ var executeAgentQuery = async (executionContext, conversationId, query, defaultS
|
|
|
11477
11626
|
};
|
|
11478
11627
|
};
|
|
11479
11628
|
var getServer = async (headers, executionContext, conversationId, credentialStores) => {
|
|
11480
|
-
const { tenantId, projectId,
|
|
11481
|
-
setupTracing(conversationId, tenantId,
|
|
11482
|
-
const
|
|
11483
|
-
scopes: { tenantId, projectId,
|
|
11629
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
11630
|
+
setupTracing(conversationId, tenantId, agentId);
|
|
11631
|
+
const agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11632
|
+
scopes: { tenantId, projectId, agentId }
|
|
11484
11633
|
});
|
|
11485
|
-
if (!
|
|
11486
|
-
throw new Error("Agent
|
|
11634
|
+
if (!agent) {
|
|
11635
|
+
throw new Error("Agent not found");
|
|
11487
11636
|
}
|
|
11488
11637
|
const server = new mcp_js.McpServer(
|
|
11489
11638
|
{
|
|
@@ -11494,26 +11643,26 @@ var getServer = async (headers, executionContext, conversationId, credentialStor
|
|
|
11494
11643
|
);
|
|
11495
11644
|
server.tool(
|
|
11496
11645
|
"send-query-to-agent",
|
|
11497
|
-
`Send a query to the ${
|
|
11646
|
+
`Send a query to the ${agent.name} agent. The agent has the following description: ${agent.description}`,
|
|
11498
11647
|
{
|
|
11499
11648
|
query: createMCPSchema(v3.z.string().describe("The query to send to the agent"))
|
|
11500
11649
|
},
|
|
11501
11650
|
async ({ query }) => {
|
|
11502
11651
|
try {
|
|
11503
|
-
if (!
|
|
11652
|
+
if (!agent.defaultSubAgentId) {
|
|
11504
11653
|
return {
|
|
11505
11654
|
content: [
|
|
11506
11655
|
{
|
|
11507
11656
|
type: "text",
|
|
11508
|
-
text: `
|
|
11657
|
+
text: `Agent does not have a default agent configured`
|
|
11509
11658
|
}
|
|
11510
11659
|
],
|
|
11511
11660
|
isError: true
|
|
11512
11661
|
};
|
|
11513
11662
|
}
|
|
11514
|
-
const defaultSubAgentId =
|
|
11663
|
+
const defaultSubAgentId = agent.defaultSubAgentId;
|
|
11515
11664
|
const agentInfo = await agentsCore.getSubAgentById(dbClient_default)({
|
|
11516
|
-
scopes: { tenantId, projectId,
|
|
11665
|
+
scopes: { tenantId, projectId, agentId },
|
|
11517
11666
|
subAgentId: defaultSubAgentId
|
|
11518
11667
|
});
|
|
11519
11668
|
if (!agentInfo) {
|
|
@@ -11530,7 +11679,7 @@ var getServer = async (headers, executionContext, conversationId, credentialStor
|
|
|
11530
11679
|
const resolvedContext = await agentsCore.handleContextResolution({
|
|
11531
11680
|
tenantId,
|
|
11532
11681
|
projectId,
|
|
11533
|
-
|
|
11682
|
+
agentId,
|
|
11534
11683
|
conversationId,
|
|
11535
11684
|
headers,
|
|
11536
11685
|
dbClient: dbClient_default,
|
|
@@ -11540,9 +11689,9 @@ var getServer = async (headers, executionContext, conversationId, credentialStor
|
|
|
11540
11689
|
{
|
|
11541
11690
|
tenantId,
|
|
11542
11691
|
projectId,
|
|
11543
|
-
|
|
11692
|
+
agentId,
|
|
11544
11693
|
conversationId,
|
|
11545
|
-
hasContextConfig: !!
|
|
11694
|
+
hasContextConfig: !!agent.contextConfigId,
|
|
11546
11695
|
hasHeaders: !!headers,
|
|
11547
11696
|
hasValidatedContext: !!resolvedContext
|
|
11548
11697
|
},
|
|
@@ -11575,8 +11724,8 @@ app4.use("/", async (c, next) => {
|
|
|
11575
11724
|
var validateRequestParameters = (c) => {
|
|
11576
11725
|
try {
|
|
11577
11726
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
11578
|
-
const { tenantId, projectId,
|
|
11579
|
-
agentsCore.getLogger("mcp").debug({ tenantId, projectId,
|
|
11727
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
11728
|
+
agentsCore.getLogger("mcp").debug({ tenantId, projectId, agentId }, "Extracted MCP entity parameters");
|
|
11580
11729
|
return { valid: true, executionContext };
|
|
11581
11730
|
} catch (error) {
|
|
11582
11731
|
agentsCore.getLogger("chat").warn(
|
|
@@ -11597,27 +11746,27 @@ var validateRequestParameters = (c) => {
|
|
|
11597
11746
|
}
|
|
11598
11747
|
};
|
|
11599
11748
|
var handleInitializationRequest = async (body, executionContext, validatedContext, req, res, c, credentialStores) => {
|
|
11600
|
-
const { tenantId, projectId,
|
|
11749
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
11601
11750
|
logger25.info({ body }, "Received initialization request");
|
|
11602
11751
|
const sessionId = agentsCore.getConversationId();
|
|
11603
|
-
const
|
|
11604
|
-
scopes: { tenantId, projectId,
|
|
11752
|
+
const agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11753
|
+
scopes: { tenantId, projectId, agentId }
|
|
11605
11754
|
});
|
|
11606
|
-
if (!
|
|
11755
|
+
if (!agent) {
|
|
11607
11756
|
return c.json(
|
|
11608
11757
|
{
|
|
11609
11758
|
jsonrpc: "2.0",
|
|
11610
|
-
error: { code: -32001, message: "Agent
|
|
11759
|
+
error: { code: -32001, message: "Agent not found" },
|
|
11611
11760
|
id: body.id || null
|
|
11612
11761
|
},
|
|
11613
11762
|
{ status: 404 }
|
|
11614
11763
|
);
|
|
11615
11764
|
}
|
|
11616
|
-
if (!
|
|
11765
|
+
if (!agent.defaultSubAgentId) {
|
|
11617
11766
|
return c.json(
|
|
11618
11767
|
{
|
|
11619
11768
|
jsonrpc: "2.0",
|
|
11620
|
-
error: { code: -32001, message: "
|
|
11769
|
+
error: { code: -32001, message: "Agent does not have a default agent configured" },
|
|
11621
11770
|
id: body.id || null
|
|
11622
11771
|
},
|
|
11623
11772
|
{ status: 400 }
|
|
@@ -11627,10 +11776,10 @@ var handleInitializationRequest = async (body, executionContext, validatedContex
|
|
|
11627
11776
|
id: sessionId,
|
|
11628
11777
|
tenantId,
|
|
11629
11778
|
projectId,
|
|
11630
|
-
activeSubAgentId:
|
|
11779
|
+
activeSubAgentId: agent.defaultSubAgentId,
|
|
11631
11780
|
metadata: {
|
|
11632
11781
|
sessionData: {
|
|
11633
|
-
|
|
11782
|
+
agentId,
|
|
11634
11783
|
sessionType: "mcp",
|
|
11635
11784
|
mcpProtocolVersion: c.req.header("mcp-protocol-version"),
|
|
11636
11785
|
initialized: false
|
|
@@ -11662,8 +11811,8 @@ var handleInitializationRequest = async (body, executionContext, validatedContex
|
|
|
11662
11811
|
return fetchToNode.toFetchResponse(res);
|
|
11663
11812
|
};
|
|
11664
11813
|
var handleExistingSessionRequest = async (body, executionContext, validatedContext, req, res, credentialStores) => {
|
|
11665
|
-
const { tenantId, projectId,
|
|
11666
|
-
const conversation = await validateSession(req, res, body, tenantId, projectId,
|
|
11814
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
11815
|
+
const conversation = await validateSession(req, res, body, tenantId, projectId, agentId);
|
|
11667
11816
|
if (!conversation) {
|
|
11668
11817
|
return fetchToNode.toFetchResponse(res);
|
|
11669
11818
|
}
|
|
@@ -11739,7 +11888,7 @@ app4.openapi(
|
|
|
11739
11888
|
description: "Unauthorized - API key authentication required"
|
|
11740
11889
|
},
|
|
11741
11890
|
404: {
|
|
11742
|
-
description: "Not Found - Agent
|
|
11891
|
+
description: "Not Found - Agent not found"
|
|
11743
11892
|
},
|
|
11744
11893
|
500: {
|
|
11745
11894
|
description: "Internal Server Error"
|
|
@@ -11957,7 +12106,7 @@ function createExecutionHono(serverConfig, credentialStores) {
|
|
|
11957
12106
|
logger26.debug({}, "Empty execution context");
|
|
11958
12107
|
return next();
|
|
11959
12108
|
}
|
|
11960
|
-
const { tenantId, projectId,
|
|
12109
|
+
const { tenantId, projectId, agentId } = executionContext;
|
|
11961
12110
|
let conversationId;
|
|
11962
12111
|
const requestBody = c.get("requestBody") || {};
|
|
11963
12112
|
if (requestBody) {
|
|
@@ -11968,7 +12117,7 @@ function createExecutionHono(serverConfig, credentialStores) {
|
|
|
11968
12117
|
}
|
|
11969
12118
|
const entries = Object.fromEntries(
|
|
11970
12119
|
Object.entries({
|
|
11971
|
-
"
|
|
12120
|
+
"agent.id": agentId,
|
|
11972
12121
|
"tenant.id": tenantId,
|
|
11973
12122
|
"project.id": projectId,
|
|
11974
12123
|
"conversation.id": conversationId
|