@inkeep/agents-sdk 0.24.2 → 0.25.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/index.js CHANGED
@@ -514,25 +514,17 @@ var Agent = class {
514
514
  this.tenantId = tenantId;
515
515
  this.projectId = projectId;
516
516
  this.baseURL = apiUrl;
517
- for (const agent2 of this.subAgents) {
518
- if (this.isInternalAgent(agent2)) {
519
- const internalAgent = agent2;
520
- if (internalAgent.setContext) {
521
- internalAgent.setContext(tenantId, projectId, apiUrl);
522
- }
523
- const tools = internalAgent.getTools();
524
- for (const [_, toolInstance] of Object.entries(tools)) {
525
- if (toolInstance && typeof toolInstance === "object") {
526
- if ("setContext" in toolInstance && typeof toolInstance.setContext === "function") {
527
- toolInstance.setContext(tenantId, projectId, apiUrl);
528
- }
517
+ for (const subAgent2 of this.subAgents) {
518
+ if (subAgent2.setContext) {
519
+ subAgent2.setContext(tenantId, projectId, apiUrl);
520
+ }
521
+ const tools = subAgent2.getTools();
522
+ for (const [_, toolInstance] of Object.entries(tools)) {
523
+ if (toolInstance && typeof toolInstance === "object") {
524
+ if ("setContext" in toolInstance && typeof toolInstance.setContext === "function") {
525
+ toolInstance.setContext(tenantId, projectId, apiUrl);
529
526
  }
530
527
  }
531
- } else {
532
- const externalAgent2 = agent2;
533
- if (externalAgent2.setContext) {
534
- externalAgent2.setContext(tenantId, apiUrl);
535
- }
536
528
  }
537
529
  }
538
530
  if (this.contextConfig?.setContext) {
@@ -552,92 +544,102 @@ var Agent = class {
552
544
  * Convert the Agent to FullAgentDefinition format for the new agent endpoint
553
545
  */
554
546
  async toFullAgentDefinition() {
555
- const agentsObject = {};
547
+ const subAgentsObject = {};
548
+ const externalAgentsObject = {};
556
549
  const functionToolsObject = {};
557
550
  const functionsObject = {};
558
- for (const agent2 of this.subAgents) {
559
- if (this.isInternalAgent(agent2)) {
560
- const internalAgent = agent2;
561
- const transfers = internalAgent.getTransfers();
562
- const delegates = internalAgent.getDelegates();
563
- const tools = [];
564
- const selectedToolsMapping = {};
565
- const headersMapping = {};
566
- const agentTools = internalAgent.getTools();
567
- for (const [_toolName, toolInstance] of Object.entries(agentTools)) {
568
- const toolId = toolInstance.getId();
569
- if (toolInstance.selectedTools) {
570
- selectedToolsMapping[toolId] = toolInstance.selectedTools;
571
- }
572
- if (toolInstance.headers) {
573
- headersMapping[toolId] = toolInstance.headers;
574
- }
575
- tools.push(toolId);
576
- if (toolInstance.constructor.name === "FunctionTool" && toolInstance instanceof FunctionTool) {
577
- if (!functionsObject[toolId]) {
578
- const functionData = toolInstance.serializeFunction();
579
- functionsObject[toolId] = functionData;
580
- }
581
- if (!functionToolsObject[toolId]) {
582
- const toolData = toolInstance.serializeTool();
583
- functionToolsObject[toolId] = {
584
- id: toolData.id,
585
- name: toolData.name,
586
- description: toolData.description,
587
- functionId: toolData.functionId,
588
- agentId: this.agentId
589
- // Include agentId for agent-scoped function tools
590
- };
591
- }
592
- }
551
+ for (const subAgent2 of this.subAgents) {
552
+ const transfers = subAgent2.getTransfers();
553
+ const delegates = subAgent2.getDelegates();
554
+ const tools = [];
555
+ const selectedToolsMapping = {};
556
+ const headersMapping = {};
557
+ const subAgentTools = subAgent2.getTools();
558
+ for (const [_toolName, toolInstance] of Object.entries(subAgentTools)) {
559
+ const toolId = toolInstance.getId();
560
+ if (toolInstance.selectedTools) {
561
+ selectedToolsMapping[toolId] = toolInstance.selectedTools;
593
562
  }
594
- const dataComponents = [];
595
- const subAgentDataComponents = internalAgent.getDataComponents();
596
- if (subAgentDataComponents) {
597
- for (const dataComponent2 of subAgentDataComponents) {
598
- const dataComponentId = dataComponent2.id || dataComponent2.name.toLowerCase().replace(/\s+/g, "-");
599
- dataComponents.push(dataComponentId);
600
- }
563
+ if (toolInstance.headers) {
564
+ headersMapping[toolId] = toolInstance.headers;
601
565
  }
602
- const artifactComponents = [];
603
- const subAgentArtifactComponents = internalAgent.getArtifactComponents();
604
- if (subAgentArtifactComponents) {
605
- for (const artifactComponent2 of subAgentArtifactComponents) {
606
- const artifactComponentId = artifactComponent2.id || artifactComponent2.name.toLowerCase().replace(/\s+/g, "-");
607
- artifactComponents.push(artifactComponentId);
566
+ tools.push(toolId);
567
+ if (toolInstance.constructor.name === "FunctionTool" && toolInstance instanceof FunctionTool) {
568
+ if (!functionsObject[toolId]) {
569
+ const functionData = toolInstance.serializeFunction();
570
+ functionsObject[toolId] = functionData;
571
+ }
572
+ if (!functionToolsObject[toolId]) {
573
+ const toolData = toolInstance.serializeTool();
574
+ functionToolsObject[toolId] = {
575
+ id: toolData.id,
576
+ name: toolData.name,
577
+ description: toolData.description,
578
+ functionId: toolData.functionId,
579
+ agentId: this.agentId
580
+ // Include agentId for agent-scoped function tools
581
+ };
608
582
  }
609
583
  }
610
- const canUse = tools.map((toolId) => ({
611
- toolId,
612
- toolSelection: selectedToolsMapping[toolId] || null,
613
- headers: headersMapping[toolId] || null
614
- }));
615
- agentsObject[internalAgent.getId()] = {
616
- id: internalAgent.getId(),
617
- name: internalAgent.getName(),
618
- description: internalAgent.config.description || `Agent ${internalAgent.getName()}`,
619
- prompt: internalAgent.getInstructions(),
620
- models: internalAgent.config.models,
621
- canTransferTo: transfers.map((h) => h.getId()),
622
- canDelegateTo: delegates.map((d) => d.getId()),
623
- canUse,
624
- // Always include for internal agents (required by API)
625
- dataComponents: dataComponents.length > 0 ? dataComponents : void 0,
626
- artifactComponents: artifactComponents.length > 0 ? artifactComponents : void 0,
627
- type: "internal"
628
- };
629
- } else {
630
- const externalAgent2 = agent2;
631
- agentsObject[externalAgent2.getId()] = {
584
+ }
585
+ const subAgentExternalAgents = subAgent2.getExternalAgentDelegates();
586
+ for (const externalAgentDelegate of subAgentExternalAgents) {
587
+ const externalAgent2 = externalAgentDelegate.externalAgent;
588
+ externalAgentsObject[externalAgent2.getId()] = {
632
589
  id: externalAgent2.getId(),
633
590
  name: externalAgent2.getName(),
634
591
  description: externalAgent2.getDescription(),
635
592
  baseUrl: externalAgent2.getBaseUrl(),
636
593
  credentialReferenceId: externalAgent2.getCredentialReferenceId(),
637
- headers: externalAgent2.getHeaders(),
638
594
  type: "external"
639
595
  };
640
596
  }
597
+ const dataComponents = [];
598
+ const subAgentDataComponents = subAgent2.getDataComponents();
599
+ if (subAgentDataComponents) {
600
+ for (const dataComponent2 of subAgentDataComponents) {
601
+ const dataComponentId = dataComponent2.id || dataComponent2.name.toLowerCase().replace(/\s+/g, "-");
602
+ dataComponents.push(dataComponentId);
603
+ }
604
+ }
605
+ const artifactComponents = [];
606
+ const subAgentArtifactComponents = subAgent2.getArtifactComponents();
607
+ if (subAgentArtifactComponents) {
608
+ for (const artifactComponent2 of subAgentArtifactComponents) {
609
+ const artifactComponentId = artifactComponent2.id || artifactComponent2.name.toLowerCase().replace(/\s+/g, "-");
610
+ artifactComponents.push(artifactComponentId);
611
+ }
612
+ }
613
+ const canUse = tools.map((toolId) => ({
614
+ toolId,
615
+ toolSelection: selectedToolsMapping[toolId] || null,
616
+ headers: headersMapping[toolId] || null
617
+ }));
618
+ subAgentsObject[subAgent2.getId()] = {
619
+ id: subAgent2.getId(),
620
+ name: subAgent2.getName(),
621
+ description: subAgent2.config.description || `Agent ${subAgent2.getName()}`,
622
+ prompt: subAgent2.getInstructions(),
623
+ models: subAgent2.config.models,
624
+ canTransferTo: transfers.map((h) => h.getId()),
625
+ canDelegateTo: delegates.map((d) => {
626
+ if (typeof d === "object" && "externalAgent" in d) {
627
+ return {
628
+ externalAgentId: d.externalAgent.getId(),
629
+ ...d.headers && { headers: d.headers }
630
+ };
631
+ } else if (typeof d === "object" && "type" in d && d.type === "external") {
632
+ return {
633
+ externalAgentId: d.getId()
634
+ };
635
+ }
636
+ return d.getId();
637
+ }),
638
+ canUse,
639
+ dataComponents: dataComponents.length > 0 ? dataComponents : void 0,
640
+ artifactComponents: artifactComponents.length > 0 ? artifactComponents : void 0,
641
+ type: "internal"
642
+ };
641
643
  }
642
644
  const processedStatusUpdates = this.statusUpdateSettings ? {
643
645
  ...this.statusUpdateSettings,
@@ -669,7 +671,8 @@ var Agent = class {
669
671
  name: this.agentName,
670
672
  description: this.agentDescription,
671
673
  defaultSubAgentId: this.defaultSubAgent?.getId() || "",
672
- subAgents: agentsObject,
674
+ subAgents: subAgentsObject,
675
+ externalAgents: externalAgentsObject,
673
676
  contextConfig: this.contextConfig?.toObject(),
674
677
  ...Object.keys(functionToolsObject).length > 0 && { functionTools: functionToolsObject },
675
678
  ...Object.keys(functionsObject).length > 0 && { functions: functionsObject },
@@ -686,12 +689,8 @@ var Agent = class {
686
689
  async initializeAllTools() {
687
690
  logger5.info({ agentId: this.agentId }, "Initializing all tools in agent");
688
691
  const toolInitPromises = [];
689
- for (const agent2 of this.subAgents) {
690
- if (!agent2.getTools) {
691
- continue;
692
- }
693
- const internalAgent = agent2;
694
- const agentTools = internalAgent.getTools();
692
+ for (const subAgent2 of this.subAgents) {
693
+ const agentTools = subAgent2.getTools();
695
694
  for (const [toolName, toolInstance] of Object.entries(agentTools)) {
696
695
  if (toolInstance && typeof toolInstance === "object") {
697
696
  if (typeof toolInstance.init === "function") {
@@ -710,7 +709,7 @@ var Agent = class {
710
709
  }
711
710
  logger5.debug(
712
711
  {
713
- subAgentId: agent2.getId(),
712
+ subAgentId: subAgent2.getId(),
714
713
  toolName,
715
714
  toolType: toolInstance.constructor.name,
716
715
  skipDbRegistration
@@ -720,7 +719,7 @@ var Agent = class {
720
719
  } catch (error) {
721
720
  logger5.error(
722
721
  {
723
- subAgentId: agent2.getId(),
722
+ subAgentId: subAgent2.getId(),
724
723
  toolName,
725
724
  error: error instanceof Error ? error.message : "Unknown error"
726
725
  },
@@ -793,80 +792,6 @@ var Agent = class {
793
792
  throw error;
794
793
  }
795
794
  }
796
- /**
797
- * Legacy initialization method - kept for backward compatibility
798
- * Initialize the agent and all agents in the backend using individual endpoints
799
- */
800
- async initLegacy() {
801
- if (this.initialized) {
802
- logger5.info({ agentId: this.agentId }, "Agent already initialized");
803
- return;
804
- }
805
- logger5.info(
806
- {
807
- agentId: this.agentId,
808
- agentCount: this.subAgents.length
809
- },
810
- "Initializing agent"
811
- );
812
- try {
813
- if (this.contextConfig) {
814
- await this.contextConfig.init();
815
- logger5.info(
816
- {
817
- agentId: this.agentId,
818
- contextConfigId: this.contextConfig.getId()
819
- },
820
- "Context configuration initialized for agent"
821
- );
822
- }
823
- const initPromises = this.subAgents.map(async (agent2) => {
824
- try {
825
- agent2.config.agentId = this.agentId;
826
- await agent2.init();
827
- logger5.debug(
828
- {
829
- subAgentId: agent2.getId(),
830
- agentId: this.agentId
831
- },
832
- "Agent initialized in agent"
833
- );
834
- } catch (error) {
835
- logger5.error(
836
- {
837
- subAgentId: agent2.getId(),
838
- agentId: this.agentId,
839
- error: error instanceof Error ? error.message : "Unknown error"
840
- },
841
- "Failed to initialize agent in agent"
842
- );
843
- throw error;
844
- }
845
- });
846
- await Promise.all(initPromises);
847
- await this.saveToDatabase();
848
- await this.createExternalAgents();
849
- await this.createAgentRelations();
850
- await this.saveRelations();
851
- this.initialized = true;
852
- logger5.info(
853
- {
854
- agentId: this.agentId,
855
- agentCount: this.subAgents.length
856
- },
857
- "Agent initialized successfully"
858
- );
859
- } catch (error) {
860
- logger5.error(
861
- {
862
- agentId: this.agentId,
863
- error: error instanceof Error ? error.message : "Unknown error"
864
- },
865
- "Failed to initialize agent"
866
- );
867
- throw error;
868
- }
869
- }
870
795
  /**
871
796
  * Generate a response using the default agent
872
797
  */
@@ -928,11 +853,6 @@ var Agent = class {
928
853
  if (!agent2) {
929
854
  throw new Error(`Agent '${subAgentId}' not found in agent`);
930
855
  }
931
- if (!this.isInternalAgent(agent2)) {
932
- throw new Error(
933
- `Agent '${subAgentId}' is an external agent and cannot be run directly. External agents are only accessible via delegation.`
934
- );
935
- }
936
856
  logger5.info(
937
857
  {
938
858
  agentId: this.agentId,
@@ -965,16 +885,15 @@ var Agent = class {
965
885
  addSubAgent(agent2) {
966
886
  this.subAgents.push(agent2);
967
887
  this.agentMap.set(agent2.getId(), agent2);
968
- if (this.models && this.isInternalAgent(agent2)) {
888
+ if (this.models) {
969
889
  this.propagateModelSettingsToAgent(agent2);
970
890
  }
971
891
  logger5.info(
972
892
  {
973
893
  agentId: this.agentId,
974
- subAgentId: agent2.getId(),
975
- agentType: this.isInternalAgent(agent2) ? "internal" : "external"
894
+ subAgentId: agent2.getId()
976
895
  },
977
- "Agent added to agent"
896
+ "SubAgent added to agent"
978
897
  );
979
898
  }
980
899
  /**
@@ -1103,29 +1022,30 @@ var Agent = class {
1103
1022
  errors.push("Agent must have a default agent");
1104
1023
  }
1105
1024
  const names = /* @__PURE__ */ new Set();
1106
- for (const agent2 of this.subAgents) {
1107
- const name = agent2.getName();
1025
+ for (const subAgent2 of this.subAgents) {
1026
+ const name = subAgent2.getName();
1108
1027
  if (names.has(name)) {
1109
1028
  errors.push(`Duplicate agent name: ${name}`);
1110
1029
  }
1111
1030
  names.add(name);
1112
1031
  }
1113
- for (const agent2 of this.subAgents) {
1114
- if (!this.isInternalAgent(agent2)) continue;
1115
- const transfers = agent2.getTransfers();
1032
+ for (const subAgent2 of this.subAgents) {
1033
+ const transfers = subAgent2.getTransfers();
1116
1034
  for (const transferAgent of transfers) {
1117
1035
  if (!this.agentMap.has(transferAgent.getName())) {
1118
1036
  errors.push(
1119
- `Agent '${agent2.getName()}' has transfer to '${transferAgent.getName()}' which is not in the agent`
1037
+ `Agent '${subAgent2.getName()}' has transfer to '${transferAgent.getName()}' which is not in the agent`
1120
1038
  );
1121
1039
  }
1122
1040
  }
1123
- const delegates = agent2.getDelegates();
1041
+ const delegates = subAgent2.getDelegates();
1124
1042
  for (const delegateAgent of delegates) {
1125
- if (!this.agentMap.has(delegateAgent.getName())) {
1126
- errors.push(
1127
- `Agent '${agent2.getName()}' has delegation to '${delegateAgent.getName()}' which is not in the agent`
1128
- );
1043
+ if (this.isInternalAgent(delegateAgent)) {
1044
+ if (!this.agentMap.has(delegateAgent.getName())) {
1045
+ errors.push(
1046
+ `Agent '${subAgent2.getName()}' has delegation to '${delegateAgent.getName()}' which is not in the agent`
1047
+ );
1048
+ }
1129
1049
  }
1130
1050
  }
1131
1051
  }
@@ -1208,10 +1128,8 @@ var Agent = class {
1208
1128
  }
1209
1129
  }
1210
1130
  await this.applyStopWhenInheritance();
1211
- for (const agent2 of this.subAgents) {
1212
- if (this.isInternalAgent(agent2)) {
1213
- this.propagateModelSettingsToAgent(agent2);
1214
- }
1131
+ for (const subAgent2 of this.subAgents) {
1132
+ this.propagateModelSettingsToAgent(subAgent2);
1215
1133
  }
1216
1134
  }
1217
1135
  /**
@@ -1229,15 +1147,12 @@ var Agent = class {
1229
1147
  this.stopWhen.transferCountIs = 10;
1230
1148
  }
1231
1149
  if (projectStopWhen?.stepCountIs !== void 0) {
1232
- for (const agent2 of this.subAgents) {
1233
- if (this.isInternalAgent(agent2)) {
1234
- const internalAgent = agent2;
1235
- if (!internalAgent.config.stopWhen) {
1236
- internalAgent.config.stopWhen = {};
1237
- }
1238
- if (internalAgent.config.stopWhen.stepCountIs === void 0) {
1239
- internalAgent.config.stopWhen.stepCountIs = projectStopWhen.stepCountIs;
1240
- }
1150
+ for (const subAgent2 of this.subAgents) {
1151
+ if (!subAgent2.config.stopWhen) {
1152
+ subAgent2.config.stopWhen = {};
1153
+ }
1154
+ if (subAgent2.config.stopWhen.stepCountIs === void 0) {
1155
+ subAgent2.config.stopWhen.stepCountIs = projectStopWhen.stepCountIs;
1241
1156
  }
1242
1157
  }
1243
1158
  }
@@ -1273,18 +1188,10 @@ var Agent = class {
1273
1188
  * Immediately propagate agent-level models to all agents during construction
1274
1189
  */
1275
1190
  propagateImmediateModelSettings() {
1276
- for (const agent2 of this.subAgents) {
1277
- if (this.isInternalAgent(agent2)) {
1278
- this.propagateModelSettingsToAgent(agent2);
1279
- }
1191
+ for (const subAgent2 of this.subAgents) {
1192
+ this.propagateModelSettingsToAgent(subAgent2);
1280
1193
  }
1281
1194
  }
1282
- /**
1283
- * Type guard to check if an agent is an external AgentInterface
1284
- */
1285
- isExternalAgent(agent2) {
1286
- return !this.isInternalAgent(agent2);
1287
- }
1288
1195
  /**
1289
1196
  * Execute agent using the backend system instead of local runner
1290
1197
  */
@@ -1450,29 +1357,25 @@ var Agent = class {
1450
1357
  }
1451
1358
  }
1452
1359
  }
1453
- async createAgentRelations() {
1454
- const allRelationPromises = [];
1455
- for (const agent2 of this.subAgents) {
1456
- if (this.isInternalAgent(agent2)) {
1457
- const transfers = agent2.getTransfers();
1458
- for (const transferAgent of transfers) {
1459
- allRelationPromises.push(
1460
- this.createInternalAgentRelation(agent2, transferAgent, "transfer")
1360
+ async createSubAgentRelations() {
1361
+ const allSubAgentRelationPromises = [];
1362
+ for (const subAgent2 of this.subAgents) {
1363
+ const transfers = subAgent2.getTransfers();
1364
+ for (const transferAgent of transfers) {
1365
+ allSubAgentRelationPromises.push(
1366
+ this.createSubAgentRelation(subAgent2, transferAgent, "transfer")
1367
+ );
1368
+ }
1369
+ const delegates = subAgent2.getSubAgentDelegates();
1370
+ for (const delegate of delegates) {
1371
+ if (this.isInternalAgent(delegate)) {
1372
+ allSubAgentRelationPromises.push(
1373
+ this.createSubAgentRelation(subAgent2, delegate, "delegate")
1461
1374
  );
1462
1375
  }
1463
- const delegates = agent2.getDelegates();
1464
- for (const delegate of delegates) {
1465
- if (delegate.type === "external") {
1466
- allRelationPromises.push(this.createExternalAgentRelation(agent2, delegate, "delegate"));
1467
- } else {
1468
- allRelationPromises.push(
1469
- this.createInternalAgentRelation(agent2, delegate, "delegate")
1470
- );
1471
- }
1472
- }
1473
1376
  }
1474
1377
  }
1475
- const results = await Promise.allSettled(allRelationPromises);
1378
+ const results = await Promise.allSettled(allSubAgentRelationPromises);
1476
1379
  const errors = [];
1477
1380
  let successCount = 0;
1478
1381
  for (const result of results) {
@@ -1492,7 +1395,7 @@ var Agent = class {
1492
1395
  logger5.info(
1493
1396
  {
1494
1397
  agentId: this.agentId,
1495
- totalRelations: allRelationPromises.length,
1398
+ totalRelations: allSubAgentRelationPromises.length,
1496
1399
  successCount,
1497
1400
  errorCount: errors.length
1498
1401
  },
@@ -1502,7 +1405,7 @@ var Agent = class {
1502
1405
  throw new Error(`All ${errors.length} agent relation creations failed`);
1503
1406
  }
1504
1407
  }
1505
- async createInternalAgentRelation(sourceAgent, targetAgent, relationType) {
1408
+ async createSubAgentRelation(sourceAgent, targetAgent, relationType) {
1506
1409
  try {
1507
1410
  const response = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agent-relations`, {
1508
1411
  method: "POST",
@@ -1530,7 +1433,7 @@ var Agent = class {
1530
1433
  );
1531
1434
  return;
1532
1435
  }
1533
- throw new Error(`Failed to create agent relation: ${response.status} - ${errorText}`);
1436
+ throw new Error(`Failed to create subAgent relation: ${response.status} - ${errorText}`);
1534
1437
  }
1535
1438
  logger5.info(
1536
1439
  {
@@ -1539,7 +1442,7 @@ var Agent = class {
1539
1442
  agentId: this.agentId,
1540
1443
  relationType
1541
1444
  },
1542
- `${relationType} relation created successfully`
1445
+ `${relationType} subAgent relation created successfully`
1543
1446
  );
1544
1447
  } catch (error) {
1545
1448
  logger5.error(
@@ -1550,116 +1453,7 @@ var Agent = class {
1550
1453
  relationType,
1551
1454
  error: error instanceof Error ? error.message : "Unknown error"
1552
1455
  },
1553
- `Failed to create ${relationType} relation`
1554
- );
1555
- throw error;
1556
- }
1557
- }
1558
- async createExternalAgentRelation(sourceAgent, externalAgent2, relationType) {
1559
- try {
1560
- const response = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agent-relations`, {
1561
- method: "POST",
1562
- headers: {
1563
- "Content-Type": "application/json"
1564
- },
1565
- body: JSON.stringify({
1566
- agentId: this.agentId,
1567
- sourceSubAgentId: sourceAgent.getId(),
1568
- externalSubAgentId: externalAgent2.getId(),
1569
- relationType
1570
- })
1571
- });
1572
- if (!response.ok) {
1573
- const errorText = await response.text().catch(() => "Unknown error");
1574
- if (response.status === 422 && errorText.includes("already exists")) {
1575
- logger5.info(
1576
- {
1577
- sourceSubAgentId: sourceAgent.getId(),
1578
- externalSubAgentId: externalAgent2.getId(),
1579
- agentId: this.agentId,
1580
- relationType
1581
- },
1582
- `${relationType} relation already exists, skipping creation`
1583
- );
1584
- return;
1585
- }
1586
- throw new Error(
1587
- `Failed to create external agent relation: ${response.status} - ${errorText}`
1588
- );
1589
- }
1590
- logger5.info(
1591
- {
1592
- sourceSubAgentId: sourceAgent.getId(),
1593
- externalSubAgentId: externalAgent2.getId(),
1594
- agentId: this.agentId,
1595
- relationType
1596
- },
1597
- `${relationType} relation created successfully`
1598
- );
1599
- } catch (error) {
1600
- logger5.error(
1601
- {
1602
- sourceSubAgentId: sourceAgent.getId(),
1603
- externalSubAgentId: externalAgent2.getId(),
1604
- agentId: this.agentId,
1605
- relationType,
1606
- error: error instanceof Error ? error.message : "Unknown error"
1607
- },
1608
- `Failed to create ${relationType} relation`
1609
- );
1610
- throw error;
1611
- }
1612
- }
1613
- /**
1614
- * Create external agents in the database
1615
- */
1616
- async createExternalAgents() {
1617
- const externalAgents2 = this.subAgents.filter((agent2) => this.isExternalAgent(agent2));
1618
- logger5.info(
1619
- {
1620
- agentId: this.agentId,
1621
- externalAgentCount: externalAgents2.length
1622
- },
1623
- "Creating external agents in database"
1624
- );
1625
- const initPromises = externalAgents2.map(async (externalAgent2) => {
1626
- try {
1627
- await externalAgent2.init();
1628
- logger5.debug(
1629
- {
1630
- externalSubAgentId: externalAgent2.getId(),
1631
- agentId: this.agentId
1632
- },
1633
- "External agent created in database"
1634
- );
1635
- } catch (error) {
1636
- logger5.error(
1637
- {
1638
- externalSubAgentId: externalAgent2.getId(),
1639
- agentId: this.agentId,
1640
- error: error instanceof Error ? error.message : "Unknown error"
1641
- },
1642
- "Failed to create external agent in database"
1643
- );
1644
- throw error;
1645
- }
1646
- });
1647
- try {
1648
- await Promise.all(initPromises);
1649
- logger5.info(
1650
- {
1651
- agentId: this.agentId,
1652
- externalAgentCount: externalAgents2.length
1653
- },
1654
- "All external agents created successfully"
1655
- );
1656
- } catch (error) {
1657
- logger5.error(
1658
- {
1659
- agentId: this.agentId,
1660
- error: error instanceof Error ? error.message : "Unknown error"
1661
- },
1662
- "Failed to create some external agents"
1456
+ `Failed to create ${relationType} subAgent relation`
1663
1457
  );
1664
1458
  throw error;
1665
1459
  }
@@ -2068,6 +1862,8 @@ var Project = class {
2068
1862
  __publicField(this, "projectTools", []);
2069
1863
  __publicField(this, "projectDataComponents", []);
2070
1864
  __publicField(this, "projectArtifactComponents", []);
1865
+ __publicField(this, "projectExternalAgents", []);
1866
+ __publicField(this, "externalAgentMap", /* @__PURE__ */ new Map());
2071
1867
  this.projectId = config.id;
2072
1868
  this.projectName = config.name;
2073
1869
  this.projectDescription = config.description;
@@ -2094,6 +1890,12 @@ var Project = class {
2094
1890
  if (config.credentialReferences) {
2095
1891
  this.credentialReferences = config.credentialReferences();
2096
1892
  }
1893
+ if (config.externalAgents) {
1894
+ this.projectExternalAgents = config.externalAgents();
1895
+ this.externalAgentMap = new Map(
1896
+ this.projectExternalAgents.map((externalAgent2) => [externalAgent2.getId(), externalAgent2])
1897
+ );
1898
+ }
2097
1899
  logger8.info(
2098
1900
  {
2099
1901
  projectId: this.projectId,
@@ -2269,6 +2071,46 @@ var Project = class {
2269
2071
  getAgents() {
2270
2072
  return this.agents;
2271
2073
  }
2074
+ /**
2075
+ * Get all external agents in the project
2076
+ */
2077
+ getExternalAgents() {
2078
+ return this.projectExternalAgents;
2079
+ }
2080
+ /**
2081
+ * Get an external agent by ID
2082
+ */
2083
+ getExternalAgent(id) {
2084
+ return this.externalAgentMap.get(id);
2085
+ }
2086
+ /**
2087
+ * Add an external agent to the project
2088
+ */
2089
+ addExternalAgent(externalAgent2) {
2090
+ this.projectExternalAgents.push(externalAgent2);
2091
+ this.externalAgentMap.set(externalAgent2.getId(), externalAgent2);
2092
+ }
2093
+ /**
2094
+ * Remove an external agent from the project
2095
+ */
2096
+ removeExternalAgent(id) {
2097
+ const externalAgentToRemove = this.externalAgentMap.get(id);
2098
+ if (externalAgentToRemove) {
2099
+ this.externalAgentMap.delete(id);
2100
+ this.projectExternalAgents = this.projectExternalAgents.filter(
2101
+ (externalAgent2) => externalAgent2.getId() !== id
2102
+ );
2103
+ logger8.info(
2104
+ {
2105
+ projectId: this.projectId,
2106
+ externalAgentId: id
2107
+ },
2108
+ "External agent removed from project"
2109
+ );
2110
+ return true;
2111
+ }
2112
+ return false;
2113
+ }
2272
2114
  /**
2273
2115
  * Get an agent by ID
2274
2116
  */
@@ -2361,8 +2203,10 @@ var Project = class {
2361
2203
  const dataComponentsObject = {};
2362
2204
  const artifactComponentsObject = {};
2363
2205
  const credentialReferencesObject = {};
2206
+ const externalAgentsObject = {};
2364
2207
  const credentialUsageMap = {};
2365
2208
  for (const agent2 of this.agents) {
2209
+ logger8.info({ agentId: agent2.getId() }, "Agent id");
2366
2210
  const agentDefinition = await agent2.toFullAgentDefinition();
2367
2211
  agentsObject[agent2.getId()] = agentDefinition;
2368
2212
  const agentCredentials = agent2.credentials;
@@ -2437,9 +2281,6 @@ var Project = class {
2437
2281
  }
2438
2282
  }
2439
2283
  for (const subAgent2 of agent2.getSubAgents()) {
2440
- if (subAgent2.type === "external") {
2441
- continue;
2442
- }
2443
2284
  const agentTools = subAgent2.getTools();
2444
2285
  for (const [, toolInstance] of Object.entries(agentTools)) {
2445
2286
  const actualTool = toolInstance;
@@ -2569,8 +2410,40 @@ var Project = class {
2569
2410
  }
2570
2411
  }
2571
2412
  }
2413
+ const subAgentExternalAgents = subAgent2.getExternalAgentDelegates();
2414
+ if (subAgentExternalAgents) {
2415
+ for (const externalAgentDelegate of subAgentExternalAgents) {
2416
+ const externalAgent2 = externalAgentDelegate.externalAgent;
2417
+ const credential2 = externalAgent2.getCredentialReference();
2418
+ if (credential2) {
2419
+ if (!credentialReferencesObject[credential2.id]) {
2420
+ credentialReferencesObject[credential2.id] = {
2421
+ id: credential2.id,
2422
+ type: credential2.type,
2423
+ credentialStoreId: credential2.credentialStoreId,
2424
+ retrievalParams: credential2.retrievalParams
2425
+ };
2426
+ credentialUsageMap[credential2.id] = [];
2427
+ }
2428
+ logger8.info({ credentialId: credential2.id }, "Credential id in external agent");
2429
+ credentialUsageMap[credential2.id].push({
2430
+ type: "externalAgent",
2431
+ id: externalAgent2.getId()
2432
+ });
2433
+ }
2434
+ logger8.info({ externalAgentId: externalAgent2.getId() }, "External agent id");
2435
+ externalAgentsObject[externalAgent2.getId()] = {
2436
+ id: externalAgent2.getId(),
2437
+ name: externalAgent2.getName(),
2438
+ description: externalAgent2.getDescription(),
2439
+ baseUrl: externalAgent2.getBaseUrl(),
2440
+ credentialReferenceId: externalAgent2.getCredentialReferenceId()
2441
+ };
2442
+ }
2443
+ }
2572
2444
  }
2573
2445
  }
2446
+ logger8.info({ externalAgentsObject }, "External agents object");
2574
2447
  for (const tool of this.projectTools) {
2575
2448
  const toolId = tool.getId();
2576
2449
  if (!toolsObject[toolId]) {
@@ -2656,6 +2529,7 @@ var Project = class {
2656
2529
  functions: Object.keys(functionsObject).length > 0 ? functionsObject : void 0,
2657
2530
  dataComponents: Object.keys(dataComponentsObject).length > 0 ? dataComponentsObject : void 0,
2658
2531
  artifactComponents: Object.keys(artifactComponentsObject).length > 0 ? artifactComponentsObject : void 0,
2532
+ externalAgents: Object.keys(externalAgentsObject).length > 0 ? externalAgentsObject : void 0,
2659
2533
  credentialReferences: Object.keys(credentialReferencesObject).length > 0 ? credentialReferencesObject : void 0,
2660
2534
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2661
2535
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
@@ -2965,6 +2839,39 @@ var SubAgent = class {
2965
2839
  getTransfers() {
2966
2840
  return typeof this.config.canTransferTo === "function" ? this.config.canTransferTo() : [];
2967
2841
  }
2842
+ getSubAgentDelegates() {
2843
+ return typeof this.config.canDelegateTo === "function" ? this.config.canDelegateTo().filter((delegate) => {
2844
+ if (typeof delegate === "object" && "externalAgent" in delegate) {
2845
+ return false;
2846
+ }
2847
+ if (typeof delegate === "object" && "type" in delegate && delegate.type === "external") {
2848
+ return false;
2849
+ }
2850
+ return true;
2851
+ }) : [];
2852
+ }
2853
+ getExternalAgentDelegates() {
2854
+ if (typeof this.config.canDelegateTo !== "function") {
2855
+ return [];
2856
+ }
2857
+ return this.config.canDelegateTo().filter((delegate) => {
2858
+ if (typeof delegate === "object" && "externalAgent" in delegate) {
2859
+ return true;
2860
+ }
2861
+ if (typeof delegate === "object" && "type" in delegate && delegate.type === "external") {
2862
+ return true;
2863
+ }
2864
+ return false;
2865
+ }).map((delegate) => {
2866
+ if ("externalAgent" in delegate) {
2867
+ return delegate;
2868
+ }
2869
+ return {
2870
+ externalAgent: delegate,
2871
+ headers: void 0
2872
+ };
2873
+ });
2874
+ }
2968
2875
  getDelegates() {
2969
2876
  return typeof this.config.canDelegateTo === "function" ? this.config.canDelegateTo() : [];
2970
2877
  }
@@ -3760,10 +3667,12 @@ var ExternalAgent = class {
3760
3667
  __publicField(this, "type", "external");
3761
3668
  __publicField(this, "initialized", false);
3762
3669
  __publicField(this, "tenantId");
3670
+ __publicField(this, "projectId");
3763
3671
  __publicField(this, "baseURL");
3764
3672
  this.config = { ...config, type: "external" };
3765
3673
  this.tenantId = "default";
3766
- this.baseURL = process.env.INKEEP_API_URL || "http://localhost:3002";
3674
+ this.projectId = "default";
3675
+ this.baseURL = this.config.baseUrl;
3767
3676
  logger12.debug(
3768
3677
  {
3769
3678
  externalAgentName: this.config.name,
@@ -3799,16 +3708,20 @@ var ExternalAgent = class {
3799
3708
  }
3800
3709
  }
3801
3710
  // Set context (tenantId and baseURL) from external source (agent, CLI, etc)
3802
- setContext(tenantId, baseURL) {
3711
+ setContext(tenantId, projectId) {
3803
3712
  this.tenantId = tenantId;
3804
- if (baseURL) {
3805
- this.baseURL = baseURL;
3806
- }
3713
+ this.projectId = projectId;
3807
3714
  }
3808
3715
  // Compute ID from name using a simple slug transformation
3809
3716
  getId() {
3810
3717
  return this.config.id;
3811
3718
  }
3719
+ with(options) {
3720
+ return {
3721
+ externalAgent: this,
3722
+ headers: options.headers
3723
+ };
3724
+ }
3812
3725
  // Private method to upsert external agent (create or update)
3813
3726
  async upsertExternalAgent() {
3814
3727
  const externalAgentData = {
@@ -3816,11 +3729,10 @@ var ExternalAgent = class {
3816
3729
  name: this.config.name,
3817
3730
  description: this.config.description,
3818
3731
  baseUrl: this.config.baseUrl,
3819
- credentialReferenceId: this.config.credentialReference?.id || void 0,
3820
- headers: this.config.headers || void 0
3732
+ credentialReferenceId: this.config.credentialReference?.id || void 0
3821
3733
  };
3822
3734
  const updateResponse = await fetch(
3823
- `${this.baseURL}/tenants/${this.tenantId}/external-agents/${this.getId()}`,
3735
+ `${this.baseURL}/tenants/${this.tenantId}/projects/${this.projectId}/external-agents/${this.getId()}`,
3824
3736
  {
3825
3737
  method: "PUT",
3826
3738
  headers: {
@@ -3846,7 +3758,7 @@ var ExternalAgent = class {
3846
3758
  "External agent not found, creating new external agent"
3847
3759
  );
3848
3760
  const createResponse = await fetch(
3849
- `${this.baseURL}/tenants/${this.tenantId}/external-agents`,
3761
+ `${this.baseURL}/tenants/${this.tenantId}/projects/${this.projectId}/external-agents`,
3850
3762
  {
3851
3763
  method: "POST",
3852
3764
  headers: {
@@ -3904,8 +3816,8 @@ var ExternalAgent = class {
3904
3816
  getCredentialReferenceId() {
3905
3817
  return this.config.credentialReference?.id || void 0;
3906
3818
  }
3907
- getHeaders() {
3908
- return this.config.headers;
3819
+ getCredentialReference() {
3820
+ return this.config.credentialReference || void 0;
3909
3821
  }
3910
3822
  };
3911
3823
  function externalAgent(config) {