@inkeep/agents-run-api 0.0.0-dev-20260113172432 → 0.0.0-dev-20260115183047
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/a2a/client.d.ts +3 -1
- package/dist/a2a/client.js +23 -9
- package/dist/a2a/handlers.js +11 -5
- package/dist/a2a/transfer.d.ts +6 -1
- package/dist/a2a/transfer.js +10 -6
- package/dist/agents/Agent.d.ts +8 -4
- package/dist/agents/Agent.js +226 -92
- package/dist/agents/ToolSessionManager.js +2 -2
- package/dist/agents/generateTaskHandler.d.ts +4 -9
- package/dist/agents/generateTaskHandler.js +107 -246
- package/dist/agents/relationTools.d.ts +19 -8
- package/dist/agents/relationTools.js +105 -14
- package/dist/agents/types.d.ts +4 -2
- package/dist/agents/versions/v1/Phase1Config.d.ts +4 -3
- package/dist/agents/versions/v1/Phase1Config.js +33 -20
- package/dist/constants/execution-limits/index.js +1 -1
- package/dist/context/ContextFetcher.d.ts +68 -0
- package/dist/context/ContextFetcher.js +276 -0
- package/dist/context/ContextResolver.d.ts +56 -0
- package/dist/context/ContextResolver.js +273 -0
- package/dist/context/context.d.ts +19 -0
- package/dist/context/context.js +108 -0
- package/dist/context/contextCache.d.ts +56 -0
- package/dist/context/contextCache.js +175 -0
- package/dist/context/index.d.ts +6 -0
- package/dist/context/index.js +7 -0
- package/dist/context/validation.d.ts +39 -0
- package/dist/context/validation.js +255 -0
- package/dist/create-app.d.ts +2 -2
- package/dist/create-app.js +7 -2
- package/dist/data/agent.d.ts +2 -2
- package/dist/data/agent.js +17 -22
- package/dist/data/agents.d.ts +2 -2
- package/dist/data/agents.js +34 -42
- package/dist/data/conversations.d.ts +2 -1
- package/dist/data/conversations.js +7 -8
- package/dist/data/db/dbClient.d.ts +2 -2
- package/dist/data/db/dbClient.js +2 -10
- package/dist/env.d.ts +6 -2
- package/dist/env.js +3 -1
- package/dist/handlers/executionHandler.d.ts +3 -2
- package/dist/handlers/executionHandler.js +46 -22
- package/dist/index.d.ts +3 -3
- package/dist/middleware/api-key-auth.d.ts +4 -10
- package/dist/middleware/api-key-auth.js +164 -163
- package/dist/middleware/index.d.ts +3 -2
- package/dist/middleware/index.js +3 -2
- package/dist/middleware/projectConfig.d.ts +19 -0
- package/dist/middleware/projectConfig.js +80 -0
- package/dist/routes/agents.d.ts +2 -1
- package/dist/routes/agents.js +7 -13
- package/dist/routes/chat.d.ts +2 -1
- package/dist/routes/chat.js +29 -52
- package/dist/routes/chatDataStream.d.ts +2 -1
- package/dist/routes/chatDataStream.js +28 -30
- package/dist/routes/mcp.d.ts +2 -1
- package/dist/routes/mcp.js +25 -37
- package/dist/services/AgentSession.d.ts +5 -7
- package/dist/services/AgentSession.js +25 -34
- package/dist/services/ArtifactParser.d.ts +2 -2
- package/dist/services/ArtifactParser.js +2 -2
- package/dist/services/ArtifactService.d.ts +2 -3
- package/dist/services/ArtifactService.js +23 -20
- package/dist/services/BaseCompressor.js +2 -2
- package/dist/services/IncrementalStreamParser.d.ts +2 -2
- package/dist/services/IncrementalStreamParser.js +2 -2
- package/dist/services/ResponseFormatter.d.ts +2 -2
- package/dist/services/ResponseFormatter.js +2 -2
- package/dist/services/evaluationHttpClient.d.ts +21 -0
- package/dist/services/evaluationHttpClient.js +48 -0
- package/dist/services/evaluationRunConfigMatcher.d.ts +6 -0
- package/dist/services/evaluationRunConfigMatcher.js +7 -0
- package/dist/tools/NativeSandboxExecutor.js +1 -1
- package/dist/tools/distill-conversation-history-tool.js +2 -2
- package/dist/tools/distill-conversation-tool.js +2 -2
- package/dist/types/execution-context.d.ts +8 -14
- package/dist/types/execution-context.js +3 -2
- package/dist/utils/model-resolver.d.ts +2 -2
- package/dist/utils/model-resolver.js +3 -13
- package/dist/utils/project.d.ts +207 -0
- package/dist/utils/project.js +315 -0
- package/dist/utils/token-estimator.d.ts +6 -52
- package/dist/utils/token-estimator.js +2 -38
- package/package.json +2 -2
package/dist/agents/Agent.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { env } from "../env.js";
|
|
2
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
2
3
|
import dbClient_default from "../data/db/dbClient.js";
|
|
3
4
|
import { AGENT_EXECUTION_MAX_GENERATION_STEPS, FUNCTION_TOOL_EXECUTION_TIMEOUT_MS_DEFAULT, FUNCTION_TOOL_SANDBOX_VCPUS_DEFAULT, LLM_GENERATION_FIRST_CALL_TIMEOUT_MS_NON_STREAMING, LLM_GENERATION_FIRST_CALL_TIMEOUT_MS_STREAMING, LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS, LLM_GENERATION_SUBSEQUENT_CALL_TIMEOUT_MS } from "../constants/execution-limits/index.js";
|
|
4
5
|
import { toolSessionManager } from "./ToolSessionManager.js";
|
|
@@ -8,6 +9,8 @@ import { getModelAwareCompressionConfig } from "../services/BaseCompressor.js";
|
|
|
8
9
|
import { createDefaultConversationHistoryConfig, getConversationHistoryWithCompression } from "../data/conversations.js";
|
|
9
10
|
import { getStreamHelper } from "../utils/stream-registry.js";
|
|
10
11
|
import { agentSessionManager } from "../services/AgentSession.js";
|
|
12
|
+
import { ContextResolver } from "../context/ContextResolver.js";
|
|
13
|
+
import "../context/index.js";
|
|
11
14
|
import { IncrementalStreamParser } from "../services/IncrementalStreamParser.js";
|
|
12
15
|
import { MidGenerationCompressor } from "../services/MidGenerationCompressor.js";
|
|
13
16
|
import { pendingToolApprovalManager } from "../services/PendingToolApprovalManager.js";
|
|
@@ -19,10 +22,10 @@ import { withJsonPostProcessing } from "../utils/json-postprocessor.js";
|
|
|
19
22
|
import { calculateBreakdownTotal, estimateTokens } from "../utils/token-estimator.js";
|
|
20
23
|
import { createDelegateToAgentTool, createTransferToAgentTool } from "./relationTools.js";
|
|
21
24
|
import { SystemPromptBuilder } from "./SystemPromptBuilder.js";
|
|
22
|
-
import { Phase1Config } from "./versions/v1/Phase1Config.js";
|
|
25
|
+
import { Phase1Config, V1_BREAKDOWN_SCHEMA } from "./versions/v1/Phase1Config.js";
|
|
23
26
|
import { Phase2Config } from "./versions/v1/Phase2Config.js";
|
|
24
27
|
import { z } from "@hono/zod-openapi";
|
|
25
|
-
import {
|
|
28
|
+
import { CredentialStuffer, InternalServices, JsonTransformer, MCPServerType, MCPTransportType, ManagementApiClient, McpClient, ModelFactory, TemplateEngine, createMessage, generateId, getLedgerArtifacts, listTaskIdsByContextId, parseEmbeddedJson } from "@inkeep/agents-core";
|
|
26
29
|
import { SpanStatusCode, trace } from "@opentelemetry/api";
|
|
27
30
|
import { Output, generateText, streamText, tool } from "ai";
|
|
28
31
|
|
|
@@ -39,7 +42,7 @@ function hasToolCallWithPrefix(prefix) {
|
|
|
39
42
|
return false;
|
|
40
43
|
};
|
|
41
44
|
}
|
|
42
|
-
const logger = getLogger("Agent");
|
|
45
|
+
const logger = getLogger$1("Agent");
|
|
43
46
|
function validateModel(modelString, modelType) {
|
|
44
47
|
if (!modelString?.trim()) throw new Error(`${modelType} model is required. Please configure models at the project level.`);
|
|
45
48
|
return modelString.trim();
|
|
@@ -62,8 +65,10 @@ var Agent = class {
|
|
|
62
65
|
mcpClientCache = /* @__PURE__ */ new Map();
|
|
63
66
|
mcpConnectionLocks = /* @__PURE__ */ new Map();
|
|
64
67
|
currentCompressor = null;
|
|
65
|
-
|
|
68
|
+
executionContext;
|
|
69
|
+
constructor(config, executionContext, credentialStoreRegistry) {
|
|
66
70
|
this.artifactComponents = config.artifactComponents || [];
|
|
71
|
+
this.executionContext = executionContext;
|
|
67
72
|
let processedDataComponents = config.dataComponents || [];
|
|
68
73
|
if (processedDataComponents.length > 0) processedDataComponents.push({
|
|
69
74
|
id: "text-content",
|
|
@@ -86,7 +91,7 @@ var Agent = class {
|
|
|
86
91
|
};
|
|
87
92
|
this.credentialStoreRegistry = credentialStoreRegistry;
|
|
88
93
|
if (credentialStoreRegistry) {
|
|
89
|
-
this.contextResolver = new ContextResolver(
|
|
94
|
+
this.contextResolver = new ContextResolver(executionContext, credentialStoreRegistry);
|
|
90
95
|
this.credentialStuffer = new CredentialStuffer(credentialStoreRegistry, this.contextResolver);
|
|
91
96
|
}
|
|
92
97
|
}
|
|
@@ -318,9 +323,7 @@ var Agent = class {
|
|
|
318
323
|
return [toolName, this.wrapToolWithStreaming(toolName, createDelegateToAgentTool({
|
|
319
324
|
delegateConfig: relation,
|
|
320
325
|
callingAgentId: this.config.id,
|
|
321
|
-
|
|
322
|
-
projectId: this.config.projectId,
|
|
323
|
-
agentId: this.config.agentId,
|
|
326
|
+
executionContext: this.executionContext,
|
|
324
327
|
contextId: runtimeContext?.contextId || "default",
|
|
325
328
|
metadata: runtimeContext?.metadata || {
|
|
326
329
|
conversationId: runtimeContext?.contextId || "default",
|
|
@@ -344,7 +347,7 @@ var Agent = class {
|
|
|
344
347
|
for (const toolSet of tools) for (const [toolName, toolDef] of Object.entries(toolSet.tools)) {
|
|
345
348
|
const needsApproval = toolSet.toolPolicies?.[toolName]?.needsApproval || false;
|
|
346
349
|
const enhancedTool = {
|
|
347
|
-
...toolDef,
|
|
350
|
+
...toolDef || {},
|
|
348
351
|
needsApproval
|
|
349
352
|
};
|
|
350
353
|
wrappedTools$1[toolName] = this.wrapToolWithStreaming(toolName, enhancedTool, streamRequestId, "mcp", {
|
|
@@ -535,34 +538,24 @@ var Agent = class {
|
|
|
535
538
|
headers: {
|
|
536
539
|
...tool$1.headers,
|
|
537
540
|
...agentToolRelationHeaders
|
|
538
|
-
}
|
|
541
|
+
},
|
|
542
|
+
toolOverrides: tool$1.config.mcp.toolOverrides
|
|
539
543
|
};
|
|
540
544
|
}
|
|
541
545
|
async getMcpTool(tool$1) {
|
|
542
546
|
const forwardedHeadersHash = this.config.forwardedHeaders ? Object.keys(this.config.forwardedHeaders).sort().join(",") : "no-fwd";
|
|
543
547
|
const cacheKey = `${this.config.tenantId}-${this.config.projectId}-${tool$1.id}-${tool$1.credentialReferenceId || "no-cred"}-${forwardedHeadersHash}`;
|
|
548
|
+
const project = this.executionContext.project;
|
|
544
549
|
const credentialReferenceId = tool$1.credentialReferenceId;
|
|
545
|
-
const toolRelation = (
|
|
546
|
-
tenantId: this.config.tenantId,
|
|
547
|
-
projectId: this.config.projectId,
|
|
548
|
-
agentId: this.config.agentId,
|
|
549
|
-
subAgentId: this.config.id
|
|
550
|
-
} })).data.find((t) => t.toolId === tool$1.id);
|
|
550
|
+
const toolRelation = (project.agents[this.config.agentId]?.subAgents?.[this.config.id])?.canUse?.find((t) => t.toolId === tool$1.id);
|
|
551
551
|
const agentToolRelationHeaders = toolRelation?.headers || void 0;
|
|
552
|
-
const selectedTools = toolRelation?.
|
|
552
|
+
const selectedTools = toolRelation?.toolSelection || void 0;
|
|
553
553
|
const toolPolicies = toolRelation?.toolPolicies || {};
|
|
554
554
|
let serverConfig;
|
|
555
555
|
const isUserScoped = tool$1.credentialScope === "user";
|
|
556
556
|
const userId = this.config.userId;
|
|
557
557
|
if (isUserScoped && userId && this.credentialStuffer) {
|
|
558
|
-
const userCredentialReference =
|
|
559
|
-
scopes: {
|
|
560
|
-
tenantId: this.config.tenantId,
|
|
561
|
-
projectId: this.config.projectId
|
|
562
|
-
},
|
|
563
|
-
toolId: tool$1.id,
|
|
564
|
-
userId
|
|
565
|
-
});
|
|
558
|
+
const userCredentialReference = project.credentialReferences ? Object.values(project.credentialReferences).find((c) => c.toolId === tool$1.id && c.userId === userId) : void 0;
|
|
566
559
|
if (userCredentialReference) {
|
|
567
560
|
const storeReference = {
|
|
568
561
|
credentialStoreId: userCredentialReference.credentialStoreId,
|
|
@@ -587,14 +580,8 @@ var Agent = class {
|
|
|
587
580
|
}, this.convertToMCPToolConfig(tool$1, agentToolRelationHeaders), void 0, selectedTools);
|
|
588
581
|
}
|
|
589
582
|
} else if (credentialReferenceId && this.credentialStuffer) {
|
|
590
|
-
const credentialReference =
|
|
591
|
-
|
|
592
|
-
tenantId: this.config.tenantId,
|
|
593
|
-
projectId: this.config.projectId
|
|
594
|
-
},
|
|
595
|
-
id: credentialReferenceId
|
|
596
|
-
});
|
|
597
|
-
if (!credentialReference) throw new Error(`Credential store not found: ${credentialReferenceId}`);
|
|
583
|
+
const credentialReference = project.credentialReferences?.[credentialReferenceId];
|
|
584
|
+
if (!credentialReference) throw new Error(`Credential reference not found: ${credentialReferenceId}`);
|
|
598
585
|
const storeReference = {
|
|
599
586
|
credentialStoreId: credentialReference.credentialStoreId,
|
|
600
587
|
retrievalParams: credentialReference.retrievalParams || {}
|
|
@@ -663,7 +650,8 @@ var Agent = class {
|
|
|
663
650
|
throw error;
|
|
664
651
|
}
|
|
665
652
|
}
|
|
666
|
-
const
|
|
653
|
+
const originalTools = await client.tools();
|
|
654
|
+
const tools = await this.applyToolOverrides(originalTools, tool$1);
|
|
667
655
|
if (!tools || Object.keys(tools).length === 0) {
|
|
668
656
|
const streamRequestId = this.getStreamRequestId();
|
|
669
657
|
if (streamRequestId) tracer.startActiveSpan("ai.toolCall", { attributes: {
|
|
@@ -728,15 +716,19 @@ var Agent = class {
|
|
|
728
716
|
}
|
|
729
717
|
async getFunctionTools(sessionId, streamRequestId) {
|
|
730
718
|
const functionTools = {};
|
|
719
|
+
const project = this.executionContext.project;
|
|
731
720
|
try {
|
|
732
|
-
const functionToolsData =
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
721
|
+
const functionToolsData = await new ManagementApiClient({
|
|
722
|
+
apiUrl: env.INKEEP_AGENTS_MANAGE_API_URL,
|
|
723
|
+
tenantId: this.config.tenantId,
|
|
724
|
+
projectId: this.config.projectId,
|
|
725
|
+
auth: {
|
|
726
|
+
mode: "internalService",
|
|
727
|
+
internalServiceName: InternalServices.INKEEP_AGENTS_RUN_API
|
|
737
728
|
},
|
|
738
|
-
|
|
739
|
-
|
|
729
|
+
ref: this.executionContext.resolvedRef.name,
|
|
730
|
+
userId: this.config.userId
|
|
731
|
+
}).getFunctionToolsForSubAgent(this.config.agentId, this.config.id) ?? [];
|
|
740
732
|
if (functionToolsData.length === 0) return functionTools;
|
|
741
733
|
const { SandboxExecutorFactory } = await import("../tools/SandboxExecutorFactory.js");
|
|
742
734
|
const sandboxExecutor = SandboxExecutorFactory.getInstance();
|
|
@@ -746,13 +738,7 @@ var Agent = class {
|
|
|
746
738
|
logger.warn({ functionToolId: functionToolDef.id }, "Function tool missing functionId reference");
|
|
747
739
|
continue;
|
|
748
740
|
}
|
|
749
|
-
const functionData =
|
|
750
|
-
functionId,
|
|
751
|
-
scopes: {
|
|
752
|
-
tenantId: this.config.tenantId || "default",
|
|
753
|
-
projectId: this.config.projectId || "default"
|
|
754
|
-
}
|
|
755
|
-
});
|
|
741
|
+
const functionData = project.functions?.[functionId];
|
|
756
742
|
if (!functionData) {
|
|
757
743
|
logger.warn({
|
|
758
744
|
functionId,
|
|
@@ -833,24 +819,30 @@ var Agent = class {
|
|
|
833
819
|
*/
|
|
834
820
|
async getResolvedContext(conversationId, headers$1) {
|
|
835
821
|
try {
|
|
822
|
+
const project = this.executionContext.project;
|
|
836
823
|
if (!this.config.contextConfigId) {
|
|
837
824
|
logger.debug({ agentId: this.config.agentId }, "No context config found for agent");
|
|
838
825
|
return null;
|
|
839
826
|
}
|
|
840
|
-
const contextConfig =
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
827
|
+
const contextConfig = project.agents[this.config.agentId]?.contextConfig;
|
|
828
|
+
if (!contextConfig) {
|
|
829
|
+
logger.warn({ contextConfigId: this.config.contextConfigId }, "Context config not found");
|
|
830
|
+
return null;
|
|
831
|
+
}
|
|
832
|
+
const contextConfigWithScopes = {
|
|
833
|
+
...contextConfig,
|
|
834
|
+
tenantId: this.config.tenantId,
|
|
835
|
+
projectId: this.config.projectId,
|
|
836
|
+
agentId: this.config.agentId,
|
|
837
|
+
createdAt: contextConfig.createdAt || "",
|
|
838
|
+
updatedAt: contextConfig.updatedAt || ""
|
|
839
|
+
};
|
|
848
840
|
if (!contextConfig) {
|
|
849
841
|
logger.warn({ contextConfigId: this.config.contextConfigId }, "Context config not found");
|
|
850
842
|
return null;
|
|
851
843
|
}
|
|
852
844
|
if (!this.contextResolver) throw new Error("Context resolver not found");
|
|
853
|
-
const result = await this.contextResolver.resolve(
|
|
845
|
+
const result = await this.contextResolver.resolve(contextConfigWithScopes, {
|
|
854
846
|
triggerEvent: "invocation",
|
|
855
847
|
conversationId,
|
|
856
848
|
headers: headers$1 || {},
|
|
@@ -882,12 +874,9 @@ var Agent = class {
|
|
|
882
874
|
* Get the agent prompt for this agent's agent
|
|
883
875
|
*/
|
|
884
876
|
async getPrompt() {
|
|
877
|
+
const agentDefinition = this.executionContext.project.agents[this.config.agentId];
|
|
885
878
|
try {
|
|
886
|
-
return
|
|
887
|
-
tenantId: this.config.tenantId,
|
|
888
|
-
projectId: this.config.projectId,
|
|
889
|
-
agentId: this.config.agentId
|
|
890
|
-
} }))?.prompt || void 0;
|
|
879
|
+
return agentDefinition?.prompt || void 0;
|
|
891
880
|
} catch (error) {
|
|
892
881
|
logger.warn({
|
|
893
882
|
agentId: this.config.agentId,
|
|
@@ -900,12 +889,9 @@ var Agent = class {
|
|
|
900
889
|
* Check if any agent in the agent has artifact components configured
|
|
901
890
|
*/
|
|
902
891
|
async hasAgentArtifactComponents() {
|
|
892
|
+
const project = this.executionContext.project;
|
|
903
893
|
try {
|
|
904
|
-
const agentDefinition =
|
|
905
|
-
tenantId: this.config.tenantId,
|
|
906
|
-
projectId: this.config.projectId,
|
|
907
|
-
agentId: this.config.agentId
|
|
908
|
-
} });
|
|
894
|
+
const agentDefinition = project.agents[this.config.agentId];
|
|
909
895
|
if (!agentDefinition) return false;
|
|
910
896
|
return Object.values(agentDefinition.subAgents).some((subAgent) => "artifactComponents" in subAgent && subAgent.artifactComponents && subAgent.artifactComponents.length > 0);
|
|
911
897
|
} catch (error) {
|
|
@@ -1012,7 +998,8 @@ var Agent = class {
|
|
|
1012
998
|
tenantId: this.config.tenantId,
|
|
1013
999
|
projectId: this.config.projectId,
|
|
1014
1000
|
conversationId: runtimeContext?.contextId || "",
|
|
1015
|
-
historyConfig
|
|
1001
|
+
historyConfig,
|
|
1002
|
+
ref: this.executionContext.resolvedRef
|
|
1016
1003
|
});
|
|
1017
1004
|
const componentDataComponents = excludeDataComponents ? [] : this.config.dataComponents || [];
|
|
1018
1005
|
const isThinkingPreparation = this.config.dataComponents && this.config.dataComponents.length > 0 && excludeDataComponents;
|
|
@@ -1117,6 +1104,162 @@ var Agent = class {
|
|
|
1117
1104
|
getStreamRequestId() {
|
|
1118
1105
|
return this.streamRequestId || "";
|
|
1119
1106
|
}
|
|
1107
|
+
async applyToolOverrides(originalTools, mcpTool) {
|
|
1108
|
+
const toolOverrides = mcpTool.config.type === "mcp" ? mcpTool.config.mcp?.toolOverrides : void 0;
|
|
1109
|
+
if (!toolOverrides) {
|
|
1110
|
+
logger.debug({ mcpToolName: mcpTool.name }, "No tool overrides configured, using original tools");
|
|
1111
|
+
return originalTools;
|
|
1112
|
+
}
|
|
1113
|
+
if (!originalTools || typeof originalTools !== "object") {
|
|
1114
|
+
logger.warn({
|
|
1115
|
+
mcpToolName: mcpTool.name,
|
|
1116
|
+
originalToolsType: typeof originalTools
|
|
1117
|
+
}, "Invalid original tools structure, skipping overrides");
|
|
1118
|
+
return originalTools || {};
|
|
1119
|
+
}
|
|
1120
|
+
const processedTools = {};
|
|
1121
|
+
const availableToolNames = Object.keys(originalTools);
|
|
1122
|
+
const overrideNames = Object.keys(toolOverrides);
|
|
1123
|
+
const invalidOverrides = overrideNames.filter((name) => !availableToolNames.includes(name));
|
|
1124
|
+
if (invalidOverrides.length > 0) logger.warn({
|
|
1125
|
+
mcpToolName: mcpTool.name,
|
|
1126
|
+
invalidOverrides,
|
|
1127
|
+
availableTools: availableToolNames
|
|
1128
|
+
}, "Tool override configured for non-existent tools");
|
|
1129
|
+
logger.info({
|
|
1130
|
+
mcpToolName: mcpTool.name,
|
|
1131
|
+
totalTools: availableToolNames.length,
|
|
1132
|
+
toolsWithOverrides: overrideNames.length,
|
|
1133
|
+
availableTools: availableToolNames,
|
|
1134
|
+
overrideTools: overrideNames
|
|
1135
|
+
}, "Starting tool override application");
|
|
1136
|
+
for (const [toolName, toolDef] of Object.entries(originalTools)) {
|
|
1137
|
+
if (!toolDef || typeof toolDef !== "object") {
|
|
1138
|
+
logger.warn({
|
|
1139
|
+
mcpToolName: mcpTool.name,
|
|
1140
|
+
toolName,
|
|
1141
|
+
toolDefType: typeof toolDef
|
|
1142
|
+
}, "Invalid tool definition structure, skipping tool");
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
const override = toolOverrides[toolName];
|
|
1146
|
+
if (override && (override.schema || override.description || override.displayName)) try {
|
|
1147
|
+
logger.debug({
|
|
1148
|
+
mcpToolName: mcpTool.name,
|
|
1149
|
+
toolName,
|
|
1150
|
+
override: {
|
|
1151
|
+
hasSchema: !!override.schema,
|
|
1152
|
+
hasDescription: !!override.description,
|
|
1153
|
+
hasDisplayName: !!override.displayName,
|
|
1154
|
+
hasTransformation: !!override.transformation,
|
|
1155
|
+
transformationType: typeof override.transformation
|
|
1156
|
+
}
|
|
1157
|
+
}, "Processing tool override");
|
|
1158
|
+
let inputSchema;
|
|
1159
|
+
try {
|
|
1160
|
+
inputSchema = override.schema ? jsonSchemaToZod(override.schema) : toolDef.inputSchema;
|
|
1161
|
+
} catch (schemaError) {
|
|
1162
|
+
logger.error({
|
|
1163
|
+
mcpToolName: mcpTool.name,
|
|
1164
|
+
toolName,
|
|
1165
|
+
schemaError: schemaError instanceof Error ? schemaError.message : String(schemaError),
|
|
1166
|
+
overrideSchema: override.schema
|
|
1167
|
+
}, "Failed to convert override schema, using original");
|
|
1168
|
+
inputSchema = toolDef.inputSchema;
|
|
1169
|
+
}
|
|
1170
|
+
const toolId = override.displayName || toolName;
|
|
1171
|
+
const simplifiedTool = tool({
|
|
1172
|
+
description: override.description || toolDef.description || `Tool ${toolId}`,
|
|
1173
|
+
inputSchema,
|
|
1174
|
+
execute: async (simpleArgs) => {
|
|
1175
|
+
let complexArgs = simpleArgs;
|
|
1176
|
+
if (override.transformation) try {
|
|
1177
|
+
const startTime = Date.now();
|
|
1178
|
+
if (typeof override.transformation === "string") complexArgs = await JsonTransformer.transform(simpleArgs, override.transformation, { timeout: 1e4 });
|
|
1179
|
+
else if (typeof override.transformation === "object" && override.transformation !== null) complexArgs = await JsonTransformer.transformWithConfig(simpleArgs, { objectTransformation: override.transformation }, { timeout: 1e4 });
|
|
1180
|
+
else logger.warn({
|
|
1181
|
+
mcpToolName: mcpTool.name,
|
|
1182
|
+
toolName,
|
|
1183
|
+
transformationType: typeof override.transformation
|
|
1184
|
+
}, "Invalid transformation type, skipping transformation");
|
|
1185
|
+
const duration = Date.now() - startTime;
|
|
1186
|
+
logger.debug({
|
|
1187
|
+
mcpToolName: mcpTool.name,
|
|
1188
|
+
toolName,
|
|
1189
|
+
transformationDuration: duration,
|
|
1190
|
+
hasSimpleArgs: !!simpleArgs,
|
|
1191
|
+
hasComplexArgs: !!complexArgs,
|
|
1192
|
+
transformation: typeof override.transformation === "string" ? override.transformation.substring(0, 100) + "..." : "object-transformation"
|
|
1193
|
+
}, "Successfully transformed tool arguments");
|
|
1194
|
+
} catch (transformError) {
|
|
1195
|
+
const errorMessage = transformError instanceof Error ? transformError.message : String(transformError);
|
|
1196
|
+
logger.error({
|
|
1197
|
+
mcpToolName: mcpTool.name,
|
|
1198
|
+
toolName,
|
|
1199
|
+
transformError: errorMessage,
|
|
1200
|
+
transformation: override.transformation,
|
|
1201
|
+
simpleArgs
|
|
1202
|
+
}, "Failed to transform tool arguments, using original arguments");
|
|
1203
|
+
complexArgs = simpleArgs;
|
|
1204
|
+
}
|
|
1205
|
+
if (typeof toolDef.execute !== "function") throw new Error(`Original tool ${toolName} does not have a valid execute function`);
|
|
1206
|
+
try {
|
|
1207
|
+
logger.debug({
|
|
1208
|
+
mcpToolName: mcpTool.name,
|
|
1209
|
+
toolName,
|
|
1210
|
+
hasComplexArgs: !!complexArgs
|
|
1211
|
+
}, "Executing original tool with processed arguments");
|
|
1212
|
+
return await toolDef.execute(complexArgs);
|
|
1213
|
+
} catch (executeError) {
|
|
1214
|
+
const errorMessage = executeError instanceof Error ? executeError.message : String(executeError);
|
|
1215
|
+
logger.error({
|
|
1216
|
+
mcpToolName: mcpTool.name,
|
|
1217
|
+
toolName,
|
|
1218
|
+
executeError: errorMessage,
|
|
1219
|
+
complexArgs
|
|
1220
|
+
}, "Failed to execute original tool");
|
|
1221
|
+
throw new Error(`Tool execution failed for ${toolName}: ${errorMessage}`);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
});
|
|
1225
|
+
const finalToolName = override.displayName || toolName;
|
|
1226
|
+
processedTools[finalToolName] = simplifiedTool;
|
|
1227
|
+
logger.info({
|
|
1228
|
+
mcpToolName: mcpTool.name,
|
|
1229
|
+
originalToolName: toolName,
|
|
1230
|
+
finalToolName,
|
|
1231
|
+
displayName: override.displayName,
|
|
1232
|
+
hasSchemaOverride: !!override.schema,
|
|
1233
|
+
hasDescriptionOverride: !!override.description,
|
|
1234
|
+
hasTransformation: !!override.transformation
|
|
1235
|
+
}, "Successfully applied tool overrides");
|
|
1236
|
+
} catch (error) {
|
|
1237
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1238
|
+
logger.error({
|
|
1239
|
+
mcpToolName: mcpTool.name,
|
|
1240
|
+
toolName,
|
|
1241
|
+
error: errorMessage,
|
|
1242
|
+
override
|
|
1243
|
+
}, "Failed to apply tool overrides, using original tool");
|
|
1244
|
+
processedTools[toolName] = toolDef;
|
|
1245
|
+
}
|
|
1246
|
+
else {
|
|
1247
|
+
processedTools[toolName] = toolDef;
|
|
1248
|
+
logger.debug({
|
|
1249
|
+
mcpToolName: mcpTool.name,
|
|
1250
|
+
toolName
|
|
1251
|
+
}, "No overrides configured for tool, using original");
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
const processedToolNames = Object.keys(processedTools);
|
|
1255
|
+
logger.info({
|
|
1256
|
+
mcpToolName: mcpTool.name,
|
|
1257
|
+
originalToolCount: availableToolNames.length,
|
|
1258
|
+
processedToolCount: processedToolNames.length,
|
|
1259
|
+
processedTools: processedToolNames
|
|
1260
|
+
}, "Completed tool override application");
|
|
1261
|
+
return processedTools;
|
|
1262
|
+
}
|
|
1120
1263
|
/**
|
|
1121
1264
|
* Format tool result for storage in conversation history
|
|
1122
1265
|
*/
|
|
@@ -1291,11 +1434,9 @@ ${typeof cleanResult === "string" ? cleanResult : JSON.stringify(cleanResult, nu
|
|
|
1291
1434
|
}
|
|
1292
1435
|
async agentHasArtifactComponents() {
|
|
1293
1436
|
try {
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
agentId: this.config.agentId
|
|
1298
|
-
} });
|
|
1437
|
+
const subAgents = this.executionContext.project.agents[this.config.agentId]?.subAgents;
|
|
1438
|
+
if (!subAgents) return false;
|
|
1439
|
+
return Object.values(subAgents).some((subAgent) => subAgent.artifactComponents?.length ?? false);
|
|
1299
1440
|
} catch (error) {
|
|
1300
1441
|
logger.error({
|
|
1301
1442
|
error,
|
|
@@ -1316,20 +1457,10 @@ ${typeof cleanResult === "string" ? cleanResult : JSON.stringify(cleanResult, nu
|
|
|
1316
1457
|
const conversationId = runtimeContext?.metadata?.conversationId;
|
|
1317
1458
|
if (conversationId) this.setConversationId(conversationId);
|
|
1318
1459
|
const { conversationHistory, contextBreakdown } = await this.buildConversationHistory(contextId, taskId, userMessage, streamRequestId, initialContextBreakdown);
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
"context.breakdown.tools_tokens": contextBreakdown.toolsSection,
|
|
1324
|
-
"context.breakdown.artifacts_tokens": contextBreakdown.artifactsSection,
|
|
1325
|
-
"context.breakdown.data_components_tokens": contextBreakdown.dataComponents,
|
|
1326
|
-
"context.breakdown.artifact_components_tokens": contextBreakdown.artifactComponents,
|
|
1327
|
-
"context.breakdown.transfer_instructions_tokens": contextBreakdown.transferInstructions,
|
|
1328
|
-
"context.breakdown.delegation_instructions_tokens": contextBreakdown.delegationInstructions,
|
|
1329
|
-
"context.breakdown.thinking_preparation_tokens": contextBreakdown.thinkingPreparation,
|
|
1330
|
-
"context.breakdown.conversation_history_tokens": contextBreakdown.conversationHistory,
|
|
1331
|
-
"context.breakdown.total_tokens": contextBreakdown.total
|
|
1332
|
-
});
|
|
1460
|
+
const breakdownAttributes = {};
|
|
1461
|
+
for (const componentDef of V1_BREAKDOWN_SCHEMA) breakdownAttributes[componentDef.spanAttribute] = contextBreakdown.components[componentDef.key] ?? 0;
|
|
1462
|
+
breakdownAttributes["context.breakdown.total_tokens"] = contextBreakdown.total;
|
|
1463
|
+
span.setAttributes(breakdownAttributes);
|
|
1333
1464
|
const { primaryModelSettings, modelSettings, hasStructuredOutput, shouldStreamPhase1, timeoutMs } = this.configureModelSettings();
|
|
1334
1465
|
let response;
|
|
1335
1466
|
let textResponse;
|
|
@@ -1499,8 +1630,11 @@ ${typeof cleanResult === "string" ? cleanResult : JSON.stringify(cleanResult, nu
|
|
|
1499
1630
|
}
|
|
1500
1631
|
const conversationHistoryTokens = estimateTokens(conversationHistory);
|
|
1501
1632
|
const updatedContextBreakdown = {
|
|
1502
|
-
|
|
1503
|
-
|
|
1633
|
+
components: {
|
|
1634
|
+
...initialContextBreakdown.components,
|
|
1635
|
+
conversationHistory: conversationHistoryTokens
|
|
1636
|
+
},
|
|
1637
|
+
total: initialContextBreakdown.total
|
|
1504
1638
|
};
|
|
1505
1639
|
calculateBreakdownTotal(updatedContextBreakdown);
|
|
1506
1640
|
return {
|
|
@@ -1666,7 +1800,7 @@ ${typeof cleanResult === "string" ? cleanResult : JSON.stringify(cleanResult, nu
|
|
|
1666
1800
|
streamRequestId: this.getStreamRequestId(),
|
|
1667
1801
|
subAgentId: this.config.id
|
|
1668
1802
|
};
|
|
1669
|
-
return new IncrementalStreamParser(streamHelper, this.
|
|
1803
|
+
return new IncrementalStreamParser(streamHelper, this.executionContext, contextId, artifactParserOptions);
|
|
1670
1804
|
}
|
|
1671
1805
|
buildTelemetryConfig(phase) {
|
|
1672
1806
|
return {
|
|
@@ -1861,7 +1995,7 @@ ${output}${structureHintsFormatted}`;
|
|
|
1861
1995
|
let formattedContent = response.formattedContent || null;
|
|
1862
1996
|
if (!formattedContent) {
|
|
1863
1997
|
const session = toolSessionManager.getSession(sessionId);
|
|
1864
|
-
const responseFormatter = new ResponseFormatter(this.
|
|
1998
|
+
const responseFormatter = new ResponseFormatter(this.executionContext, {
|
|
1865
1999
|
sessionId,
|
|
1866
2000
|
taskId: session?.taskId,
|
|
1867
2001
|
projectId: session?.projectId,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { getLogger } from "../logger.js";
|
|
1
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
2
2
|
import { SESSION_CLEANUP_INTERVAL_MS, SESSION_TOOL_RESULT_CACHE_TIMEOUT_MS } from "../constants/execution-limits/index.js";
|
|
3
3
|
import { generateId } from "@inkeep/agents-core";
|
|
4
4
|
|
|
5
5
|
//#region src/agents/ToolSessionManager.ts
|
|
6
|
-
const logger = getLogger("ToolSessionManager");
|
|
6
|
+
const logger = getLogger$1("ToolSessionManager");
|
|
7
7
|
/**
|
|
8
8
|
* Manages tool execution state during agent generation sessions.
|
|
9
9
|
* Allows tools to access previous tool call results within the same execution.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { A2ATask, A2ATaskResult } from "../a2a/types.js";
|
|
2
2
|
import { SandboxConfig } from "../types/execution-context.js";
|
|
3
|
-
import { AgentConversationHistoryConfig, CredentialStoreRegistry, SubAgentApiSelect } from "@inkeep/agents-core";
|
|
3
|
+
import { AgentConversationHistoryConfig, CredentialStoreRegistry, FullExecutionContext, SubAgentApiSelect } from "@inkeep/agents-core";
|
|
4
4
|
|
|
5
5
|
//#region src/agents/generateTaskHandler.d.ts
|
|
6
6
|
|
|
@@ -8,9 +8,7 @@ import { AgentConversationHistoryConfig, CredentialStoreRegistry, SubAgentApiSel
|
|
|
8
8
|
* Serializable configuration for creating task handlers
|
|
9
9
|
*/
|
|
10
10
|
interface TaskHandlerConfig {
|
|
11
|
-
|
|
12
|
-
projectId: string;
|
|
13
|
-
agentId: string;
|
|
11
|
+
executionContext: FullExecutionContext;
|
|
14
12
|
subAgentId: string;
|
|
15
13
|
agentSchema: SubAgentApiSelect;
|
|
16
14
|
name: string;
|
|
@@ -33,17 +31,14 @@ declare const serializeTaskHandlerConfig: (config: TaskHandlerConfig) => string;
|
|
|
33
31
|
*/
|
|
34
32
|
declare const deserializeTaskHandlerConfig: (configJson: string) => TaskHandlerConfig;
|
|
35
33
|
/**
|
|
36
|
-
* Creates a task handler configuration from
|
|
34
|
+
* Creates a task handler configuration from execution context and project data
|
|
37
35
|
*/
|
|
38
36
|
declare const createTaskHandlerConfig: (params: {
|
|
39
|
-
|
|
40
|
-
projectId: string;
|
|
41
|
-
agentId: string;
|
|
37
|
+
executionContext: FullExecutionContext;
|
|
42
38
|
subAgentId: string;
|
|
43
39
|
baseUrl: string;
|
|
44
40
|
apiKey?: string;
|
|
45
41
|
sandboxConfig?: SandboxConfig;
|
|
46
|
-
userId?: string;
|
|
47
42
|
}) => Promise<TaskHandlerConfig>;
|
|
48
43
|
//#endregion
|
|
49
44
|
export { TaskHandlerConfig, createTaskHandler, createTaskHandlerConfig, deserializeTaskHandlerConfig, serializeTaskHandlerConfig };
|