@inkeep/agents-sdk 0.24.1 → 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.cjs CHANGED
@@ -541,25 +541,17 @@ var Agent = class {
541
541
  this.tenantId = tenantId;
542
542
  this.projectId = projectId;
543
543
  this.baseURL = apiUrl;
544
- for (const agent2 of this.subAgents) {
545
- if (this.isInternalAgent(agent2)) {
546
- const internalAgent = agent2;
547
- if (internalAgent.setContext) {
548
- internalAgent.setContext(tenantId, projectId, apiUrl);
549
- }
550
- const tools = internalAgent.getTools();
551
- for (const [_, toolInstance] of Object.entries(tools)) {
552
- if (toolInstance && typeof toolInstance === "object") {
553
- if ("setContext" in toolInstance && typeof toolInstance.setContext === "function") {
554
- toolInstance.setContext(tenantId, projectId, apiUrl);
555
- }
544
+ for (const subAgent2 of this.subAgents) {
545
+ if (subAgent2.setContext) {
546
+ subAgent2.setContext(tenantId, projectId, apiUrl);
547
+ }
548
+ const tools = subAgent2.getTools();
549
+ for (const [_, toolInstance] of Object.entries(tools)) {
550
+ if (toolInstance && typeof toolInstance === "object") {
551
+ if ("setContext" in toolInstance && typeof toolInstance.setContext === "function") {
552
+ toolInstance.setContext(tenantId, projectId, apiUrl);
556
553
  }
557
554
  }
558
- } else {
559
- const externalAgent2 = agent2;
560
- if (externalAgent2.setContext) {
561
- externalAgent2.setContext(tenantId, apiUrl);
562
- }
563
555
  }
564
556
  }
565
557
  if (this.contextConfig?.setContext) {
@@ -579,92 +571,102 @@ var Agent = class {
579
571
  * Convert the Agent to FullAgentDefinition format for the new agent endpoint
580
572
  */
581
573
  async toFullAgentDefinition() {
582
- const agentsObject = {};
574
+ const subAgentsObject = {};
575
+ const externalAgentsObject = {};
583
576
  const functionToolsObject = {};
584
577
  const functionsObject = {};
585
- for (const agent2 of this.subAgents) {
586
- if (this.isInternalAgent(agent2)) {
587
- const internalAgent = agent2;
588
- const transfers = internalAgent.getTransfers();
589
- const delegates = internalAgent.getDelegates();
590
- const tools = [];
591
- const selectedToolsMapping = {};
592
- const headersMapping = {};
593
- const agentTools = internalAgent.getTools();
594
- for (const [_toolName, toolInstance] of Object.entries(agentTools)) {
595
- const toolId = toolInstance.getId();
596
- if (toolInstance.selectedTools) {
597
- selectedToolsMapping[toolId] = toolInstance.selectedTools;
598
- }
599
- if (toolInstance.headers) {
600
- headersMapping[toolId] = toolInstance.headers;
601
- }
602
- tools.push(toolId);
603
- if (toolInstance.constructor.name === "FunctionTool" && toolInstance instanceof FunctionTool) {
604
- if (!functionsObject[toolId]) {
605
- const functionData = toolInstance.serializeFunction();
606
- functionsObject[toolId] = functionData;
607
- }
608
- if (!functionToolsObject[toolId]) {
609
- const toolData = toolInstance.serializeTool();
610
- functionToolsObject[toolId] = {
611
- id: toolData.id,
612
- name: toolData.name,
613
- description: toolData.description,
614
- functionId: toolData.functionId,
615
- agentId: this.agentId
616
- // Include agentId for agent-scoped function tools
617
- };
618
- }
619
- }
578
+ for (const subAgent2 of this.subAgents) {
579
+ const transfers = subAgent2.getTransfers();
580
+ const delegates = subAgent2.getDelegates();
581
+ const tools = [];
582
+ const selectedToolsMapping = {};
583
+ const headersMapping = {};
584
+ const subAgentTools = subAgent2.getTools();
585
+ for (const [_toolName, toolInstance] of Object.entries(subAgentTools)) {
586
+ const toolId = toolInstance.getId();
587
+ if (toolInstance.selectedTools) {
588
+ selectedToolsMapping[toolId] = toolInstance.selectedTools;
620
589
  }
621
- const dataComponents = [];
622
- const subAgentDataComponents = internalAgent.getDataComponents();
623
- if (subAgentDataComponents) {
624
- for (const dataComponent2 of subAgentDataComponents) {
625
- const dataComponentId = dataComponent2.id || dataComponent2.name.toLowerCase().replace(/\s+/g, "-");
626
- dataComponents.push(dataComponentId);
627
- }
590
+ if (toolInstance.headers) {
591
+ headersMapping[toolId] = toolInstance.headers;
628
592
  }
629
- const artifactComponents = [];
630
- const subAgentArtifactComponents = internalAgent.getArtifactComponents();
631
- if (subAgentArtifactComponents) {
632
- for (const artifactComponent2 of subAgentArtifactComponents) {
633
- const artifactComponentId = artifactComponent2.id || artifactComponent2.name.toLowerCase().replace(/\s+/g, "-");
634
- artifactComponents.push(artifactComponentId);
593
+ tools.push(toolId);
594
+ if (toolInstance.constructor.name === "FunctionTool" && toolInstance instanceof FunctionTool) {
595
+ if (!functionsObject[toolId]) {
596
+ const functionData = toolInstance.serializeFunction();
597
+ functionsObject[toolId] = functionData;
598
+ }
599
+ if (!functionToolsObject[toolId]) {
600
+ const toolData = toolInstance.serializeTool();
601
+ functionToolsObject[toolId] = {
602
+ id: toolData.id,
603
+ name: toolData.name,
604
+ description: toolData.description,
605
+ functionId: toolData.functionId,
606
+ agentId: this.agentId
607
+ // Include agentId for agent-scoped function tools
608
+ };
635
609
  }
636
610
  }
637
- const canUse = tools.map((toolId) => ({
638
- toolId,
639
- toolSelection: selectedToolsMapping[toolId] || null,
640
- headers: headersMapping[toolId] || null
641
- }));
642
- agentsObject[internalAgent.getId()] = {
643
- id: internalAgent.getId(),
644
- name: internalAgent.getName(),
645
- description: internalAgent.config.description || `Agent ${internalAgent.getName()}`,
646
- prompt: internalAgent.getInstructions(),
647
- models: internalAgent.config.models,
648
- canTransferTo: transfers.map((h) => h.getId()),
649
- canDelegateTo: delegates.map((d) => d.getId()),
650
- canUse,
651
- // Always include for internal agents (required by API)
652
- dataComponents: dataComponents.length > 0 ? dataComponents : void 0,
653
- artifactComponents: artifactComponents.length > 0 ? artifactComponents : void 0,
654
- type: "internal"
655
- };
656
- } else {
657
- const externalAgent2 = agent2;
658
- agentsObject[externalAgent2.getId()] = {
611
+ }
612
+ const subAgentExternalAgents = subAgent2.getExternalAgentDelegates();
613
+ for (const externalAgentDelegate of subAgentExternalAgents) {
614
+ const externalAgent2 = externalAgentDelegate.externalAgent;
615
+ externalAgentsObject[externalAgent2.getId()] = {
659
616
  id: externalAgent2.getId(),
660
617
  name: externalAgent2.getName(),
661
618
  description: externalAgent2.getDescription(),
662
619
  baseUrl: externalAgent2.getBaseUrl(),
663
620
  credentialReferenceId: externalAgent2.getCredentialReferenceId(),
664
- headers: externalAgent2.getHeaders(),
665
621
  type: "external"
666
622
  };
667
623
  }
624
+ const dataComponents = [];
625
+ const subAgentDataComponents = subAgent2.getDataComponents();
626
+ if (subAgentDataComponents) {
627
+ for (const dataComponent2 of subAgentDataComponents) {
628
+ const dataComponentId = dataComponent2.id || dataComponent2.name.toLowerCase().replace(/\s+/g, "-");
629
+ dataComponents.push(dataComponentId);
630
+ }
631
+ }
632
+ const artifactComponents = [];
633
+ const subAgentArtifactComponents = subAgent2.getArtifactComponents();
634
+ if (subAgentArtifactComponents) {
635
+ for (const artifactComponent2 of subAgentArtifactComponents) {
636
+ const artifactComponentId = artifactComponent2.id || artifactComponent2.name.toLowerCase().replace(/\s+/g, "-");
637
+ artifactComponents.push(artifactComponentId);
638
+ }
639
+ }
640
+ const canUse = tools.map((toolId) => ({
641
+ toolId,
642
+ toolSelection: selectedToolsMapping[toolId] || null,
643
+ headers: headersMapping[toolId] || null
644
+ }));
645
+ subAgentsObject[subAgent2.getId()] = {
646
+ id: subAgent2.getId(),
647
+ name: subAgent2.getName(),
648
+ description: subAgent2.config.description || `Agent ${subAgent2.getName()}`,
649
+ prompt: subAgent2.getInstructions(),
650
+ models: subAgent2.config.models,
651
+ canTransferTo: transfers.map((h) => h.getId()),
652
+ canDelegateTo: delegates.map((d) => {
653
+ if (typeof d === "object" && "externalAgent" in d) {
654
+ return {
655
+ externalAgentId: d.externalAgent.getId(),
656
+ ...d.headers && { headers: d.headers }
657
+ };
658
+ } else if (typeof d === "object" && "type" in d && d.type === "external") {
659
+ return {
660
+ externalAgentId: d.getId()
661
+ };
662
+ }
663
+ return d.getId();
664
+ }),
665
+ canUse,
666
+ dataComponents: dataComponents.length > 0 ? dataComponents : void 0,
667
+ artifactComponents: artifactComponents.length > 0 ? artifactComponents : void 0,
668
+ type: "internal"
669
+ };
668
670
  }
669
671
  const processedStatusUpdates = this.statusUpdateSettings ? {
670
672
  ...this.statusUpdateSettings,
@@ -696,7 +698,8 @@ var Agent = class {
696
698
  name: this.agentName,
697
699
  description: this.agentDescription,
698
700
  defaultSubAgentId: this.defaultSubAgent?.getId() || "",
699
- subAgents: agentsObject,
701
+ subAgents: subAgentsObject,
702
+ externalAgents: externalAgentsObject,
700
703
  contextConfig: this.contextConfig?.toObject(),
701
704
  ...Object.keys(functionToolsObject).length > 0 && { functionTools: functionToolsObject },
702
705
  ...Object.keys(functionsObject).length > 0 && { functions: functionsObject },
@@ -713,12 +716,8 @@ var Agent = class {
713
716
  async initializeAllTools() {
714
717
  logger5.info({ agentId: this.agentId }, "Initializing all tools in agent");
715
718
  const toolInitPromises = [];
716
- for (const agent2 of this.subAgents) {
717
- if (!agent2.getTools) {
718
- continue;
719
- }
720
- const internalAgent = agent2;
721
- const agentTools = internalAgent.getTools();
719
+ for (const subAgent2 of this.subAgents) {
720
+ const agentTools = subAgent2.getTools();
722
721
  for (const [toolName, toolInstance] of Object.entries(agentTools)) {
723
722
  if (toolInstance && typeof toolInstance === "object") {
724
723
  if (typeof toolInstance.init === "function") {
@@ -737,7 +736,7 @@ var Agent = class {
737
736
  }
738
737
  logger5.debug(
739
738
  {
740
- subAgentId: agent2.getId(),
739
+ subAgentId: subAgent2.getId(),
741
740
  toolName,
742
741
  toolType: toolInstance.constructor.name,
743
742
  skipDbRegistration
@@ -747,7 +746,7 @@ var Agent = class {
747
746
  } catch (error) {
748
747
  logger5.error(
749
748
  {
750
- subAgentId: agent2.getId(),
749
+ subAgentId: subAgent2.getId(),
751
750
  toolName,
752
751
  error: error instanceof Error ? error.message : "Unknown error"
753
752
  },
@@ -820,80 +819,6 @@ var Agent = class {
820
819
  throw error;
821
820
  }
822
821
  }
823
- /**
824
- * Legacy initialization method - kept for backward compatibility
825
- * Initialize the agent and all agents in the backend using individual endpoints
826
- */
827
- async initLegacy() {
828
- if (this.initialized) {
829
- logger5.info({ agentId: this.agentId }, "Agent already initialized");
830
- return;
831
- }
832
- logger5.info(
833
- {
834
- agentId: this.agentId,
835
- agentCount: this.subAgents.length
836
- },
837
- "Initializing agent"
838
- );
839
- try {
840
- if (this.contextConfig) {
841
- await this.contextConfig.init();
842
- logger5.info(
843
- {
844
- agentId: this.agentId,
845
- contextConfigId: this.contextConfig.getId()
846
- },
847
- "Context configuration initialized for agent"
848
- );
849
- }
850
- const initPromises = this.subAgents.map(async (agent2) => {
851
- try {
852
- agent2.config.agentId = this.agentId;
853
- await agent2.init();
854
- logger5.debug(
855
- {
856
- subAgentId: agent2.getId(),
857
- agentId: this.agentId
858
- },
859
- "Agent initialized in agent"
860
- );
861
- } catch (error) {
862
- logger5.error(
863
- {
864
- subAgentId: agent2.getId(),
865
- agentId: this.agentId,
866
- error: error instanceof Error ? error.message : "Unknown error"
867
- },
868
- "Failed to initialize agent in agent"
869
- );
870
- throw error;
871
- }
872
- });
873
- await Promise.all(initPromises);
874
- await this.saveToDatabase();
875
- await this.createExternalAgents();
876
- await this.createAgentRelations();
877
- await this.saveRelations();
878
- this.initialized = true;
879
- logger5.info(
880
- {
881
- agentId: this.agentId,
882
- agentCount: this.subAgents.length
883
- },
884
- "Agent initialized successfully"
885
- );
886
- } catch (error) {
887
- logger5.error(
888
- {
889
- agentId: this.agentId,
890
- error: error instanceof Error ? error.message : "Unknown error"
891
- },
892
- "Failed to initialize agent"
893
- );
894
- throw error;
895
- }
896
- }
897
822
  /**
898
823
  * Generate a response using the default agent
899
824
  */
@@ -955,11 +880,6 @@ var Agent = class {
955
880
  if (!agent2) {
956
881
  throw new Error(`Agent '${subAgentId}' not found in agent`);
957
882
  }
958
- if (!this.isInternalAgent(agent2)) {
959
- throw new Error(
960
- `Agent '${subAgentId}' is an external agent and cannot be run directly. External agents are only accessible via delegation.`
961
- );
962
- }
963
883
  logger5.info(
964
884
  {
965
885
  agentId: this.agentId,
@@ -992,16 +912,15 @@ var Agent = class {
992
912
  addSubAgent(agent2) {
993
913
  this.subAgents.push(agent2);
994
914
  this.agentMap.set(agent2.getId(), agent2);
995
- if (this.models && this.isInternalAgent(agent2)) {
915
+ if (this.models) {
996
916
  this.propagateModelSettingsToAgent(agent2);
997
917
  }
998
918
  logger5.info(
999
919
  {
1000
920
  agentId: this.agentId,
1001
- subAgentId: agent2.getId(),
1002
- agentType: this.isInternalAgent(agent2) ? "internal" : "external"
921
+ subAgentId: agent2.getId()
1003
922
  },
1004
- "Agent added to agent"
923
+ "SubAgent added to agent"
1005
924
  );
1006
925
  }
1007
926
  /**
@@ -1130,29 +1049,30 @@ var Agent = class {
1130
1049
  errors.push("Agent must have a default agent");
1131
1050
  }
1132
1051
  const names = /* @__PURE__ */ new Set();
1133
- for (const agent2 of this.subAgents) {
1134
- const name = agent2.getName();
1052
+ for (const subAgent2 of this.subAgents) {
1053
+ const name = subAgent2.getName();
1135
1054
  if (names.has(name)) {
1136
1055
  errors.push(`Duplicate agent name: ${name}`);
1137
1056
  }
1138
1057
  names.add(name);
1139
1058
  }
1140
- for (const agent2 of this.subAgents) {
1141
- if (!this.isInternalAgent(agent2)) continue;
1142
- const transfers = agent2.getTransfers();
1059
+ for (const subAgent2 of this.subAgents) {
1060
+ const transfers = subAgent2.getTransfers();
1143
1061
  for (const transferAgent of transfers) {
1144
1062
  if (!this.agentMap.has(transferAgent.getName())) {
1145
1063
  errors.push(
1146
- `Agent '${agent2.getName()}' has transfer to '${transferAgent.getName()}' which is not in the agent`
1064
+ `Agent '${subAgent2.getName()}' has transfer to '${transferAgent.getName()}' which is not in the agent`
1147
1065
  );
1148
1066
  }
1149
1067
  }
1150
- const delegates = agent2.getDelegates();
1068
+ const delegates = subAgent2.getDelegates();
1151
1069
  for (const delegateAgent of delegates) {
1152
- if (!this.agentMap.has(delegateAgent.getName())) {
1153
- errors.push(
1154
- `Agent '${agent2.getName()}' has delegation to '${delegateAgent.getName()}' which is not in the agent`
1155
- );
1070
+ if (this.isInternalAgent(delegateAgent)) {
1071
+ if (!this.agentMap.has(delegateAgent.getName())) {
1072
+ errors.push(
1073
+ `Agent '${subAgent2.getName()}' has delegation to '${delegateAgent.getName()}' which is not in the agent`
1074
+ );
1075
+ }
1156
1076
  }
1157
1077
  }
1158
1078
  }
@@ -1235,10 +1155,8 @@ var Agent = class {
1235
1155
  }
1236
1156
  }
1237
1157
  await this.applyStopWhenInheritance();
1238
- for (const agent2 of this.subAgents) {
1239
- if (this.isInternalAgent(agent2)) {
1240
- this.propagateModelSettingsToAgent(agent2);
1241
- }
1158
+ for (const subAgent2 of this.subAgents) {
1159
+ this.propagateModelSettingsToAgent(subAgent2);
1242
1160
  }
1243
1161
  }
1244
1162
  /**
@@ -1256,15 +1174,12 @@ var Agent = class {
1256
1174
  this.stopWhen.transferCountIs = 10;
1257
1175
  }
1258
1176
  if (projectStopWhen?.stepCountIs !== void 0) {
1259
- for (const agent2 of this.subAgents) {
1260
- if (this.isInternalAgent(agent2)) {
1261
- const internalAgent = agent2;
1262
- if (!internalAgent.config.stopWhen) {
1263
- internalAgent.config.stopWhen = {};
1264
- }
1265
- if (internalAgent.config.stopWhen.stepCountIs === void 0) {
1266
- internalAgent.config.stopWhen.stepCountIs = projectStopWhen.stepCountIs;
1267
- }
1177
+ for (const subAgent2 of this.subAgents) {
1178
+ if (!subAgent2.config.stopWhen) {
1179
+ subAgent2.config.stopWhen = {};
1180
+ }
1181
+ if (subAgent2.config.stopWhen.stepCountIs === void 0) {
1182
+ subAgent2.config.stopWhen.stepCountIs = projectStopWhen.stepCountIs;
1268
1183
  }
1269
1184
  }
1270
1185
  }
@@ -1300,18 +1215,10 @@ var Agent = class {
1300
1215
  * Immediately propagate agent-level models to all agents during construction
1301
1216
  */
1302
1217
  propagateImmediateModelSettings() {
1303
- for (const agent2 of this.subAgents) {
1304
- if (this.isInternalAgent(agent2)) {
1305
- this.propagateModelSettingsToAgent(agent2);
1306
- }
1218
+ for (const subAgent2 of this.subAgents) {
1219
+ this.propagateModelSettingsToAgent(subAgent2);
1307
1220
  }
1308
1221
  }
1309
- /**
1310
- * Type guard to check if an agent is an external AgentInterface
1311
- */
1312
- isExternalAgent(agent2) {
1313
- return !this.isInternalAgent(agent2);
1314
- }
1315
1222
  /**
1316
1223
  * Execute agent using the backend system instead of local runner
1317
1224
  */
@@ -1477,29 +1384,25 @@ var Agent = class {
1477
1384
  }
1478
1385
  }
1479
1386
  }
1480
- async createAgentRelations() {
1481
- const allRelationPromises = [];
1482
- for (const agent2 of this.subAgents) {
1483
- if (this.isInternalAgent(agent2)) {
1484
- const transfers = agent2.getTransfers();
1485
- for (const transferAgent of transfers) {
1486
- allRelationPromises.push(
1487
- this.createInternalAgentRelation(agent2, transferAgent, "transfer")
1387
+ async createSubAgentRelations() {
1388
+ const allSubAgentRelationPromises = [];
1389
+ for (const subAgent2 of this.subAgents) {
1390
+ const transfers = subAgent2.getTransfers();
1391
+ for (const transferAgent of transfers) {
1392
+ allSubAgentRelationPromises.push(
1393
+ this.createSubAgentRelation(subAgent2, transferAgent, "transfer")
1394
+ );
1395
+ }
1396
+ const delegates = subAgent2.getSubAgentDelegates();
1397
+ for (const delegate of delegates) {
1398
+ if (this.isInternalAgent(delegate)) {
1399
+ allSubAgentRelationPromises.push(
1400
+ this.createSubAgentRelation(subAgent2, delegate, "delegate")
1488
1401
  );
1489
1402
  }
1490
- const delegates = agent2.getDelegates();
1491
- for (const delegate of delegates) {
1492
- if (delegate.type === "external") {
1493
- allRelationPromises.push(this.createExternalAgentRelation(agent2, delegate, "delegate"));
1494
- } else {
1495
- allRelationPromises.push(
1496
- this.createInternalAgentRelation(agent2, delegate, "delegate")
1497
- );
1498
- }
1499
- }
1500
1403
  }
1501
1404
  }
1502
- const results = await Promise.allSettled(allRelationPromises);
1405
+ const results = await Promise.allSettled(allSubAgentRelationPromises);
1503
1406
  const errors = [];
1504
1407
  let successCount = 0;
1505
1408
  for (const result of results) {
@@ -1519,7 +1422,7 @@ var Agent = class {
1519
1422
  logger5.info(
1520
1423
  {
1521
1424
  agentId: this.agentId,
1522
- totalRelations: allRelationPromises.length,
1425
+ totalRelations: allSubAgentRelationPromises.length,
1523
1426
  successCount,
1524
1427
  errorCount: errors.length
1525
1428
  },
@@ -1529,7 +1432,7 @@ var Agent = class {
1529
1432
  throw new Error(`All ${errors.length} agent relation creations failed`);
1530
1433
  }
1531
1434
  }
1532
- async createInternalAgentRelation(sourceAgent, targetAgent, relationType) {
1435
+ async createSubAgentRelation(sourceAgent, targetAgent, relationType) {
1533
1436
  try {
1534
1437
  const response = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agent-relations`, {
1535
1438
  method: "POST",
@@ -1557,7 +1460,7 @@ var Agent = class {
1557
1460
  );
1558
1461
  return;
1559
1462
  }
1560
- throw new Error(`Failed to create agent relation: ${response.status} - ${errorText}`);
1463
+ throw new Error(`Failed to create subAgent relation: ${response.status} - ${errorText}`);
1561
1464
  }
1562
1465
  logger5.info(
1563
1466
  {
@@ -1566,7 +1469,7 @@ var Agent = class {
1566
1469
  agentId: this.agentId,
1567
1470
  relationType
1568
1471
  },
1569
- `${relationType} relation created successfully`
1472
+ `${relationType} subAgent relation created successfully`
1570
1473
  );
1571
1474
  } catch (error) {
1572
1475
  logger5.error(
@@ -1577,116 +1480,7 @@ var Agent = class {
1577
1480
  relationType,
1578
1481
  error: error instanceof Error ? error.message : "Unknown error"
1579
1482
  },
1580
- `Failed to create ${relationType} relation`
1581
- );
1582
- throw error;
1583
- }
1584
- }
1585
- async createExternalAgentRelation(sourceAgent, externalAgent2, relationType) {
1586
- try {
1587
- const response = await fetch(`${this.baseURL}/tenants/${this.tenantId}/agent-relations`, {
1588
- method: "POST",
1589
- headers: {
1590
- "Content-Type": "application/json"
1591
- },
1592
- body: JSON.stringify({
1593
- agentId: this.agentId,
1594
- sourceSubAgentId: sourceAgent.getId(),
1595
- externalSubAgentId: externalAgent2.getId(),
1596
- relationType
1597
- })
1598
- });
1599
- if (!response.ok) {
1600
- const errorText = await response.text().catch(() => "Unknown error");
1601
- if (response.status === 422 && errorText.includes("already exists")) {
1602
- logger5.info(
1603
- {
1604
- sourceSubAgentId: sourceAgent.getId(),
1605
- externalSubAgentId: externalAgent2.getId(),
1606
- agentId: this.agentId,
1607
- relationType
1608
- },
1609
- `${relationType} relation already exists, skipping creation`
1610
- );
1611
- return;
1612
- }
1613
- throw new Error(
1614
- `Failed to create external agent relation: ${response.status} - ${errorText}`
1615
- );
1616
- }
1617
- logger5.info(
1618
- {
1619
- sourceSubAgentId: sourceAgent.getId(),
1620
- externalSubAgentId: externalAgent2.getId(),
1621
- agentId: this.agentId,
1622
- relationType
1623
- },
1624
- `${relationType} relation created successfully`
1625
- );
1626
- } catch (error) {
1627
- logger5.error(
1628
- {
1629
- sourceSubAgentId: sourceAgent.getId(),
1630
- externalSubAgentId: externalAgent2.getId(),
1631
- agentId: this.agentId,
1632
- relationType,
1633
- error: error instanceof Error ? error.message : "Unknown error"
1634
- },
1635
- `Failed to create ${relationType} relation`
1636
- );
1637
- throw error;
1638
- }
1639
- }
1640
- /**
1641
- * Create external agents in the database
1642
- */
1643
- async createExternalAgents() {
1644
- const externalAgents2 = this.subAgents.filter((agent2) => this.isExternalAgent(agent2));
1645
- logger5.info(
1646
- {
1647
- agentId: this.agentId,
1648
- externalAgentCount: externalAgents2.length
1649
- },
1650
- "Creating external agents in database"
1651
- );
1652
- const initPromises = externalAgents2.map(async (externalAgent2) => {
1653
- try {
1654
- await externalAgent2.init();
1655
- logger5.debug(
1656
- {
1657
- externalSubAgentId: externalAgent2.getId(),
1658
- agentId: this.agentId
1659
- },
1660
- "External agent created in database"
1661
- );
1662
- } catch (error) {
1663
- logger5.error(
1664
- {
1665
- externalSubAgentId: externalAgent2.getId(),
1666
- agentId: this.agentId,
1667
- error: error instanceof Error ? error.message : "Unknown error"
1668
- },
1669
- "Failed to create external agent in database"
1670
- );
1671
- throw error;
1672
- }
1673
- });
1674
- try {
1675
- await Promise.all(initPromises);
1676
- logger5.info(
1677
- {
1678
- agentId: this.agentId,
1679
- externalAgentCount: externalAgents2.length
1680
- },
1681
- "All external agents created successfully"
1682
- );
1683
- } catch (error) {
1684
- logger5.error(
1685
- {
1686
- agentId: this.agentId,
1687
- error: error instanceof Error ? error.message : "Unknown error"
1688
- },
1689
- "Failed to create some external agents"
1483
+ `Failed to create ${relationType} subAgent relation`
1690
1484
  );
1691
1485
  throw error;
1692
1486
  }
@@ -2095,6 +1889,8 @@ var Project = class {
2095
1889
  __publicField(this, "projectTools", []);
2096
1890
  __publicField(this, "projectDataComponents", []);
2097
1891
  __publicField(this, "projectArtifactComponents", []);
1892
+ __publicField(this, "projectExternalAgents", []);
1893
+ __publicField(this, "externalAgentMap", /* @__PURE__ */ new Map());
2098
1894
  this.projectId = config.id;
2099
1895
  this.projectName = config.name;
2100
1896
  this.projectDescription = config.description;
@@ -2121,6 +1917,12 @@ var Project = class {
2121
1917
  if (config.credentialReferences) {
2122
1918
  this.credentialReferences = config.credentialReferences();
2123
1919
  }
1920
+ if (config.externalAgents) {
1921
+ this.projectExternalAgents = config.externalAgents();
1922
+ this.externalAgentMap = new Map(
1923
+ this.projectExternalAgents.map((externalAgent2) => [externalAgent2.getId(), externalAgent2])
1924
+ );
1925
+ }
2124
1926
  logger8.info(
2125
1927
  {
2126
1928
  projectId: this.projectId,
@@ -2296,6 +2098,46 @@ var Project = class {
2296
2098
  getAgents() {
2297
2099
  return this.agents;
2298
2100
  }
2101
+ /**
2102
+ * Get all external agents in the project
2103
+ */
2104
+ getExternalAgents() {
2105
+ return this.projectExternalAgents;
2106
+ }
2107
+ /**
2108
+ * Get an external agent by ID
2109
+ */
2110
+ getExternalAgent(id) {
2111
+ return this.externalAgentMap.get(id);
2112
+ }
2113
+ /**
2114
+ * Add an external agent to the project
2115
+ */
2116
+ addExternalAgent(externalAgent2) {
2117
+ this.projectExternalAgents.push(externalAgent2);
2118
+ this.externalAgentMap.set(externalAgent2.getId(), externalAgent2);
2119
+ }
2120
+ /**
2121
+ * Remove an external agent from the project
2122
+ */
2123
+ removeExternalAgent(id) {
2124
+ const externalAgentToRemove = this.externalAgentMap.get(id);
2125
+ if (externalAgentToRemove) {
2126
+ this.externalAgentMap.delete(id);
2127
+ this.projectExternalAgents = this.projectExternalAgents.filter(
2128
+ (externalAgent2) => externalAgent2.getId() !== id
2129
+ );
2130
+ logger8.info(
2131
+ {
2132
+ projectId: this.projectId,
2133
+ externalAgentId: id
2134
+ },
2135
+ "External agent removed from project"
2136
+ );
2137
+ return true;
2138
+ }
2139
+ return false;
2140
+ }
2299
2141
  /**
2300
2142
  * Get an agent by ID
2301
2143
  */
@@ -2388,8 +2230,10 @@ var Project = class {
2388
2230
  const dataComponentsObject = {};
2389
2231
  const artifactComponentsObject = {};
2390
2232
  const credentialReferencesObject = {};
2233
+ const externalAgentsObject = {};
2391
2234
  const credentialUsageMap = {};
2392
2235
  for (const agent2 of this.agents) {
2236
+ logger8.info({ agentId: agent2.getId() }, "Agent id");
2393
2237
  const agentDefinition = await agent2.toFullAgentDefinition();
2394
2238
  agentsObject[agent2.getId()] = agentDefinition;
2395
2239
  const agentCredentials = agent2.credentials;
@@ -2464,9 +2308,6 @@ var Project = class {
2464
2308
  }
2465
2309
  }
2466
2310
  for (const subAgent2 of agent2.getSubAgents()) {
2467
- if (subAgent2.type === "external") {
2468
- continue;
2469
- }
2470
2311
  const agentTools = subAgent2.getTools();
2471
2312
  for (const [, toolInstance] of Object.entries(agentTools)) {
2472
2313
  const actualTool = toolInstance;
@@ -2596,8 +2437,40 @@ var Project = class {
2596
2437
  }
2597
2438
  }
2598
2439
  }
2440
+ const subAgentExternalAgents = subAgent2.getExternalAgentDelegates();
2441
+ if (subAgentExternalAgents) {
2442
+ for (const externalAgentDelegate of subAgentExternalAgents) {
2443
+ const externalAgent2 = externalAgentDelegate.externalAgent;
2444
+ const credential2 = externalAgent2.getCredentialReference();
2445
+ if (credential2) {
2446
+ if (!credentialReferencesObject[credential2.id]) {
2447
+ credentialReferencesObject[credential2.id] = {
2448
+ id: credential2.id,
2449
+ type: credential2.type,
2450
+ credentialStoreId: credential2.credentialStoreId,
2451
+ retrievalParams: credential2.retrievalParams
2452
+ };
2453
+ credentialUsageMap[credential2.id] = [];
2454
+ }
2455
+ logger8.info({ credentialId: credential2.id }, "Credential id in external agent");
2456
+ credentialUsageMap[credential2.id].push({
2457
+ type: "externalAgent",
2458
+ id: externalAgent2.getId()
2459
+ });
2460
+ }
2461
+ logger8.info({ externalAgentId: externalAgent2.getId() }, "External agent id");
2462
+ externalAgentsObject[externalAgent2.getId()] = {
2463
+ id: externalAgent2.getId(),
2464
+ name: externalAgent2.getName(),
2465
+ description: externalAgent2.getDescription(),
2466
+ baseUrl: externalAgent2.getBaseUrl(),
2467
+ credentialReferenceId: externalAgent2.getCredentialReferenceId()
2468
+ };
2469
+ }
2470
+ }
2599
2471
  }
2600
2472
  }
2473
+ logger8.info({ externalAgentsObject }, "External agents object");
2601
2474
  for (const tool of this.projectTools) {
2602
2475
  const toolId = tool.getId();
2603
2476
  if (!toolsObject[toolId]) {
@@ -2683,6 +2556,7 @@ var Project = class {
2683
2556
  functions: Object.keys(functionsObject).length > 0 ? functionsObject : void 0,
2684
2557
  dataComponents: Object.keys(dataComponentsObject).length > 0 ? dataComponentsObject : void 0,
2685
2558
  artifactComponents: Object.keys(artifactComponentsObject).length > 0 ? artifactComponentsObject : void 0,
2559
+ externalAgents: Object.keys(externalAgentsObject).length > 0 ? externalAgentsObject : void 0,
2686
2560
  credentialReferences: Object.keys(credentialReferencesObject).length > 0 ? credentialReferencesObject : void 0,
2687
2561
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2688
2562
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
@@ -2992,6 +2866,39 @@ var SubAgent = class {
2992
2866
  getTransfers() {
2993
2867
  return typeof this.config.canTransferTo === "function" ? this.config.canTransferTo() : [];
2994
2868
  }
2869
+ getSubAgentDelegates() {
2870
+ return typeof this.config.canDelegateTo === "function" ? this.config.canDelegateTo().filter((delegate) => {
2871
+ if (typeof delegate === "object" && "externalAgent" in delegate) {
2872
+ return false;
2873
+ }
2874
+ if (typeof delegate === "object" && "type" in delegate && delegate.type === "external") {
2875
+ return false;
2876
+ }
2877
+ return true;
2878
+ }) : [];
2879
+ }
2880
+ getExternalAgentDelegates() {
2881
+ if (typeof this.config.canDelegateTo !== "function") {
2882
+ return [];
2883
+ }
2884
+ return this.config.canDelegateTo().filter((delegate) => {
2885
+ if (typeof delegate === "object" && "externalAgent" in delegate) {
2886
+ return true;
2887
+ }
2888
+ if (typeof delegate === "object" && "type" in delegate && delegate.type === "external") {
2889
+ return true;
2890
+ }
2891
+ return false;
2892
+ }).map((delegate) => {
2893
+ if ("externalAgent" in delegate) {
2894
+ return delegate;
2895
+ }
2896
+ return {
2897
+ externalAgent: delegate,
2898
+ headers: void 0
2899
+ };
2900
+ });
2901
+ }
2995
2902
  getDelegates() {
2996
2903
  return typeof this.config.canDelegateTo === "function" ? this.config.canDelegateTo() : [];
2997
2904
  }
@@ -3787,10 +3694,12 @@ var ExternalAgent = class {
3787
3694
  __publicField(this, "type", "external");
3788
3695
  __publicField(this, "initialized", false);
3789
3696
  __publicField(this, "tenantId");
3697
+ __publicField(this, "projectId");
3790
3698
  __publicField(this, "baseURL");
3791
3699
  this.config = { ...config, type: "external" };
3792
3700
  this.tenantId = "default";
3793
- this.baseURL = process.env.INKEEP_API_URL || "http://localhost:3002";
3701
+ this.projectId = "default";
3702
+ this.baseURL = this.config.baseUrl;
3794
3703
  logger12.debug(
3795
3704
  {
3796
3705
  externalAgentName: this.config.name,
@@ -3826,16 +3735,20 @@ var ExternalAgent = class {
3826
3735
  }
3827
3736
  }
3828
3737
  // Set context (tenantId and baseURL) from external source (agent, CLI, etc)
3829
- setContext(tenantId, baseURL) {
3738
+ setContext(tenantId, projectId) {
3830
3739
  this.tenantId = tenantId;
3831
- if (baseURL) {
3832
- this.baseURL = baseURL;
3833
- }
3740
+ this.projectId = projectId;
3834
3741
  }
3835
3742
  // Compute ID from name using a simple slug transformation
3836
3743
  getId() {
3837
3744
  return this.config.id;
3838
3745
  }
3746
+ with(options) {
3747
+ return {
3748
+ externalAgent: this,
3749
+ headers: options.headers
3750
+ };
3751
+ }
3839
3752
  // Private method to upsert external agent (create or update)
3840
3753
  async upsertExternalAgent() {
3841
3754
  const externalAgentData = {
@@ -3843,11 +3756,10 @@ var ExternalAgent = class {
3843
3756
  name: this.config.name,
3844
3757
  description: this.config.description,
3845
3758
  baseUrl: this.config.baseUrl,
3846
- credentialReferenceId: this.config.credentialReference?.id || void 0,
3847
- headers: this.config.headers || void 0
3759
+ credentialReferenceId: this.config.credentialReference?.id || void 0
3848
3760
  };
3849
3761
  const updateResponse = await fetch(
3850
- `${this.baseURL}/tenants/${this.tenantId}/external-agents/${this.getId()}`,
3762
+ `${this.baseURL}/tenants/${this.tenantId}/projects/${this.projectId}/external-agents/${this.getId()}`,
3851
3763
  {
3852
3764
  method: "PUT",
3853
3765
  headers: {
@@ -3873,7 +3785,7 @@ var ExternalAgent = class {
3873
3785
  "External agent not found, creating new external agent"
3874
3786
  );
3875
3787
  const createResponse = await fetch(
3876
- `${this.baseURL}/tenants/${this.tenantId}/external-agents`,
3788
+ `${this.baseURL}/tenants/${this.tenantId}/projects/${this.projectId}/external-agents`,
3877
3789
  {
3878
3790
  method: "POST",
3879
3791
  headers: {
@@ -3931,8 +3843,8 @@ var ExternalAgent = class {
3931
3843
  getCredentialReferenceId() {
3932
3844
  return this.config.credentialReference?.id || void 0;
3933
3845
  }
3934
- getHeaders() {
3935
- return this.config.headers;
3846
+ getCredentialReference() {
3847
+ return this.config.credentialReference || void 0;
3936
3848
  }
3937
3849
  };
3938
3850
  function externalAgent(config) {