@inkeep/agents-run-api 0.39.4 → 0.40.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/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/thinking-preparation.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/tool.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-component.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-components.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact-retrieval-guidance.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact.js +5 -0
- package/dist/a2a/client.d.ts +184 -0
- package/dist/a2a/client.js +510 -0
- package/dist/a2a/handlers.d.ts +7 -0
- package/dist/a2a/handlers.js +560 -0
- package/dist/a2a/transfer.d.ts +22 -0
- package/dist/a2a/transfer.js +46 -0
- package/dist/a2a/types.d.ts +79 -0
- package/dist/a2a/types.js +22 -0
- package/dist/agents/Agent.d.ts +266 -0
- package/dist/agents/Agent.js +1927 -0
- package/dist/agents/ModelFactory.d.ts +63 -0
- package/dist/agents/ModelFactory.js +194 -0
- package/dist/agents/SystemPromptBuilder.d.ts +21 -0
- package/dist/agents/SystemPromptBuilder.js +48 -0
- package/dist/agents/ToolSessionManager.d.ts +63 -0
- package/dist/agents/ToolSessionManager.js +146 -0
- package/dist/agents/generateTaskHandler.d.ts +49 -0
- package/dist/agents/generateTaskHandler.js +521 -0
- package/dist/agents/relationTools.d.ts +57 -0
- package/dist/agents/relationTools.js +262 -0
- package/dist/agents/types.d.ts +28 -0
- package/dist/agents/types.js +1 -0
- package/dist/agents/versions/v1/Phase1Config.d.ts +27 -0
- package/dist/agents/versions/v1/Phase1Config.js +424 -0
- package/dist/agents/versions/v1/Phase2Config.d.ts +31 -0
- package/dist/agents/versions/v1/Phase2Config.js +330 -0
- package/dist/constants/execution-limits/defaults.d.ts +51 -0
- package/dist/constants/execution-limits/defaults.js +52 -0
- package/dist/constants/execution-limits/index.d.ts +6 -0
- package/dist/constants/execution-limits/index.js +21 -0
- package/dist/create-app.d.ts +9 -0
- package/dist/create-app.js +195 -0
- package/dist/data/agent.d.ts +7 -0
- package/dist/data/agent.js +72 -0
- package/dist/data/agents.d.ts +34 -0
- package/dist/data/agents.js +139 -0
- package/dist/data/conversations.d.ts +128 -0
- package/dist/data/conversations.js +522 -0
- package/dist/data/db/dbClient.d.ts +6 -0
- package/dist/data/db/dbClient.js +17 -0
- package/dist/env.d.ts +57 -0
- package/dist/env.js +1 -2
- package/dist/handlers/executionHandler.d.ts +39 -0
- package/dist/handlers/executionHandler.js +456 -0
- package/dist/index.d.ts +8 -29
- package/dist/index.js +5 -11235
- package/dist/instrumentation.d.ts +1 -2
- package/dist/instrumentation.js +66 -3
- package/dist/{logger2.js → logger.d.ts} +1 -2
- package/dist/logger.js +1 -1
- package/dist/middleware/api-key-auth.d.ts +26 -0
- package/dist/middleware/api-key-auth.js +240 -0
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +3 -0
- package/dist/openapi.d.ts +4 -0
- package/dist/openapi.js +54 -0
- package/dist/routes/agents.d.ts +12 -0
- package/dist/routes/agents.js +147 -0
- package/dist/routes/chat.d.ts +13 -0
- package/dist/routes/chat.js +293 -0
- package/dist/routes/chatDataStream.d.ts +13 -0
- package/dist/routes/chatDataStream.js +352 -0
- package/dist/routes/mcp.d.ts +13 -0
- package/dist/routes/mcp.js +495 -0
- package/dist/services/AgentSession.d.ts +356 -0
- package/dist/services/AgentSession.js +1208 -0
- package/dist/services/ArtifactParser.d.ts +105 -0
- package/dist/services/ArtifactParser.js +338 -0
- package/dist/services/ArtifactService.d.ts +123 -0
- package/dist/services/ArtifactService.js +612 -0
- package/dist/services/BaseCompressor.d.ts +183 -0
- package/dist/services/BaseCompressor.js +504 -0
- package/dist/services/ConversationCompressor.d.ts +32 -0
- package/dist/services/ConversationCompressor.js +91 -0
- package/dist/services/IncrementalStreamParser.d.ts +98 -0
- package/dist/services/IncrementalStreamParser.js +327 -0
- package/dist/services/MidGenerationCompressor.d.ts +63 -0
- package/dist/services/MidGenerationCompressor.js +104 -0
- package/dist/services/PendingToolApprovalManager.d.ts +62 -0
- package/dist/services/PendingToolApprovalManager.js +133 -0
- package/dist/services/ResponseFormatter.d.ts +39 -0
- package/dist/services/ResponseFormatter.js +152 -0
- package/dist/tools/NativeSandboxExecutor.d.ts +38 -0
- package/dist/tools/NativeSandboxExecutor.js +432 -0
- package/dist/tools/SandboxExecutorFactory.d.ts +36 -0
- package/dist/tools/SandboxExecutorFactory.js +80 -0
- package/dist/tools/VercelSandboxExecutor.d.ts +71 -0
- package/dist/tools/VercelSandboxExecutor.js +340 -0
- package/dist/tools/distill-conversation-history-tool.d.ts +62 -0
- package/dist/tools/distill-conversation-history-tool.js +206 -0
- package/dist/tools/distill-conversation-tool.d.ts +41 -0
- package/dist/tools/distill-conversation-tool.js +141 -0
- package/dist/tools/sandbox-utils.d.ts +18 -0
- package/dist/tools/sandbox-utils.js +53 -0
- package/dist/types/chat.d.ts +27 -0
- package/dist/types/chat.js +1 -0
- package/dist/types/execution-context.d.ts +46 -0
- package/dist/types/execution-context.js +27 -0
- package/dist/types/xml.d.ts +5 -0
- package/dist/utils/SchemaProcessor.d.ts +52 -0
- package/dist/utils/SchemaProcessor.js +182 -0
- package/dist/utils/agent-operations.d.ts +62 -0
- package/dist/utils/agent-operations.js +53 -0
- package/dist/utils/artifact-component-schema.d.ts +42 -0
- package/dist/utils/artifact-component-schema.js +186 -0
- package/dist/utils/cleanup.d.ts +21 -0
- package/dist/utils/cleanup.js +59 -0
- package/dist/utils/data-component-schema.d.ts +2 -0
- package/dist/utils/data-component-schema.js +3 -0
- package/dist/utils/default-status-schemas.d.ts +20 -0
- package/dist/utils/default-status-schemas.js +24 -0
- package/dist/utils/json-postprocessor.d.ts +13 -0
- package/dist/{json-postprocessor.cjs → utils/json-postprocessor.js} +1 -2
- package/dist/utils/model-context-utils.d.ts +39 -0
- package/dist/utils/model-context-utils.js +181 -0
- package/dist/utils/model-resolver.d.ts +6 -0
- package/dist/utils/model-resolver.js +34 -0
- package/dist/utils/schema-validation.d.ts +44 -0
- package/dist/utils/schema-validation.js +97 -0
- package/dist/utils/stream-helpers.d.ts +197 -0
- package/dist/utils/stream-helpers.js +518 -0
- package/dist/utils/stream-registry.d.ts +22 -0
- package/dist/utils/stream-registry.js +34 -0
- package/dist/utils/token-estimator.d.ts +69 -0
- package/dist/utils/token-estimator.js +53 -0
- package/dist/utils/tracer.d.ts +7 -0
- package/dist/utils/tracer.js +7 -0
- package/package.json +5 -20
- package/dist/SandboxExecutorFactory.cjs +0 -895
- package/dist/SandboxExecutorFactory.js +0 -893
- package/dist/SandboxExecutorFactory.js.map +0 -1
- package/dist/chunk-VBDAOXYI.cjs +0 -927
- package/dist/chunk-VBDAOXYI.js +0 -832
- package/dist/chunk-VBDAOXYI.js.map +0 -1
- package/dist/chunk.cjs +0 -34
- package/dist/conversations.cjs +0 -7
- package/dist/conversations.js +0 -7
- package/dist/conversations2.cjs +0 -209
- package/dist/conversations2.js +0 -180
- package/dist/conversations2.js.map +0 -1
- package/dist/dbClient.cjs +0 -9676
- package/dist/dbClient.js +0 -9670
- package/dist/dbClient.js.map +0 -1
- package/dist/dbClient2.cjs +0 -5
- package/dist/dbClient2.js +0 -5
- package/dist/env.cjs +0 -59
- package/dist/env.js.map +0 -1
- package/dist/execution-limits.cjs +0 -260
- package/dist/execution-limits.js +0 -63
- package/dist/execution-limits.js.map +0 -1
- package/dist/index.cjs +0 -11260
- package/dist/index.d.cts +0 -36
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/instrumentation.cjs +0 -12
- package/dist/instrumentation.d.cts +0 -18
- package/dist/instrumentation.d.cts.map +0 -1
- package/dist/instrumentation.d.ts.map +0 -1
- package/dist/instrumentation2.cjs +0 -116
- package/dist/instrumentation2.js +0 -69
- package/dist/instrumentation2.js.map +0 -1
- package/dist/json-postprocessor.js +0 -20
- package/dist/json-postprocessor.js.map +0 -1
- package/dist/logger.cjs +0 -5
- package/dist/logger2.cjs +0 -1
- package/dist/nodefs.cjs +0 -29
- package/dist/nodefs.js +0 -27
- package/dist/nodefs.js.map +0 -1
- package/dist/opfs-ahp.cjs +0 -367
- package/dist/opfs-ahp.js +0 -368
- package/dist/opfs-ahp.js.map +0 -1
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { toolSessionManager } from "./ToolSessionManager.js";
|
|
4
|
+
import { agentSessionManager } from "../services/AgentSession.js";
|
|
5
|
+
import { resolveModelConfig } from "../utils/model-resolver.js";
|
|
6
|
+
import { Agent } from "./Agent.js";
|
|
7
|
+
import { generateDescriptionWithRelationData } from "../data/agents.js";
|
|
8
|
+
import { TaskState, dbResultToMcpTool, generateId, getAgentById, getAgentWithDefaultSubAgent, getArtifactComponentsForAgent, getDataComponentsForAgent, getExternalAgentsForSubAgent, getRelatedAgentsForAgent, getSubAgentById, getTeamAgentsForSubAgent, getToolsForAgent } from "@inkeep/agents-core";
|
|
9
|
+
|
|
10
|
+
//#region src/agents/generateTaskHandler.ts
|
|
11
|
+
const logger = getLogger("generateTaskHandler");
|
|
12
|
+
const createTaskHandler = (config, credentialStoreRegistry) => {
|
|
13
|
+
return async (task) => {
|
|
14
|
+
let agent;
|
|
15
|
+
try {
|
|
16
|
+
const userMessage = task.input.parts.filter((part) => part.text).map((part) => part.text).join(" ");
|
|
17
|
+
if (!userMessage.trim()) return {
|
|
18
|
+
status: {
|
|
19
|
+
state: TaskState.Failed,
|
|
20
|
+
message: "No text content found in task input"
|
|
21
|
+
},
|
|
22
|
+
artifacts: []
|
|
23
|
+
};
|
|
24
|
+
const [internalRelations, externalRelations, teamRelations, toolsForAgent, dataComponents, artifactComponents] = await Promise.all([
|
|
25
|
+
getRelatedAgentsForAgent(dbClient_default)({
|
|
26
|
+
scopes: {
|
|
27
|
+
tenantId: config.tenantId,
|
|
28
|
+
projectId: config.projectId,
|
|
29
|
+
agentId: config.agentId
|
|
30
|
+
},
|
|
31
|
+
subAgentId: config.subAgentId
|
|
32
|
+
}),
|
|
33
|
+
getExternalAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
34
|
+
tenantId: config.tenantId,
|
|
35
|
+
projectId: config.projectId,
|
|
36
|
+
agentId: config.agentId,
|
|
37
|
+
subAgentId: config.subAgentId
|
|
38
|
+
} }),
|
|
39
|
+
getTeamAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
40
|
+
tenantId: config.tenantId,
|
|
41
|
+
projectId: config.projectId,
|
|
42
|
+
agentId: config.agentId,
|
|
43
|
+
subAgentId: config.subAgentId
|
|
44
|
+
} }),
|
|
45
|
+
getToolsForAgent(dbClient_default)({ scopes: {
|
|
46
|
+
tenantId: config.tenantId,
|
|
47
|
+
projectId: config.projectId,
|
|
48
|
+
agentId: config.agentId,
|
|
49
|
+
subAgentId: config.subAgentId
|
|
50
|
+
} }),
|
|
51
|
+
getDataComponentsForAgent(dbClient_default)({ scopes: {
|
|
52
|
+
tenantId: config.tenantId,
|
|
53
|
+
projectId: config.projectId,
|
|
54
|
+
agentId: config.agentId,
|
|
55
|
+
subAgentId: config.subAgentId
|
|
56
|
+
} }),
|
|
57
|
+
getArtifactComponentsForAgent(dbClient_default)({ scopes: {
|
|
58
|
+
tenantId: config.tenantId,
|
|
59
|
+
projectId: config.projectId,
|
|
60
|
+
agentId: config.agentId,
|
|
61
|
+
subAgentId: config.subAgentId
|
|
62
|
+
} })
|
|
63
|
+
]);
|
|
64
|
+
const enhancedInternalRelations = await Promise.all(internalRelations.data.map(async (relation) => {
|
|
65
|
+
try {
|
|
66
|
+
if (await getSubAgentById(dbClient_default)({
|
|
67
|
+
scopes: {
|
|
68
|
+
tenantId: config.tenantId,
|
|
69
|
+
projectId: config.projectId,
|
|
70
|
+
agentId: config.agentId
|
|
71
|
+
},
|
|
72
|
+
subAgentId: relation.id
|
|
73
|
+
})) {
|
|
74
|
+
const relatedAgentRelations = await getRelatedAgentsForAgent(dbClient_default)({
|
|
75
|
+
scopes: {
|
|
76
|
+
tenantId: config.tenantId,
|
|
77
|
+
projectId: config.projectId,
|
|
78
|
+
agentId: config.agentId
|
|
79
|
+
},
|
|
80
|
+
subAgentId: relation.id
|
|
81
|
+
});
|
|
82
|
+
const relatedAgentExternalAgentRelations = await getExternalAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
83
|
+
tenantId: config.tenantId,
|
|
84
|
+
projectId: config.projectId,
|
|
85
|
+
agentId: config.agentId,
|
|
86
|
+
subAgentId: relation.id
|
|
87
|
+
} });
|
|
88
|
+
const relatedAgentTeamAgentRelations = await getTeamAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
89
|
+
tenantId: config.tenantId,
|
|
90
|
+
projectId: config.projectId,
|
|
91
|
+
agentId: config.agentId,
|
|
92
|
+
subAgentId: relation.id
|
|
93
|
+
} });
|
|
94
|
+
const enhancedDescription = generateDescriptionWithRelationData(relation.description || "", relatedAgentRelations.data, relatedAgentExternalAgentRelations.data, relatedAgentTeamAgentRelations.data);
|
|
95
|
+
return {
|
|
96
|
+
...relation,
|
|
97
|
+
description: enhancedDescription
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
logger.warn({
|
|
102
|
+
subAgentId: relation.id,
|
|
103
|
+
error
|
|
104
|
+
}, "Failed to enhance agent description");
|
|
105
|
+
}
|
|
106
|
+
return relation;
|
|
107
|
+
}));
|
|
108
|
+
const enhancedTeamRelations = await Promise.all(teamRelations.data.map(async (relation) => {
|
|
109
|
+
try {
|
|
110
|
+
const teamAgentWithDefault = await getAgentWithDefaultSubAgent(dbClient_default)({ scopes: {
|
|
111
|
+
tenantId: config.tenantId,
|
|
112
|
+
projectId: config.projectId,
|
|
113
|
+
agentId: relation.targetAgentId
|
|
114
|
+
} });
|
|
115
|
+
if (teamAgentWithDefault?.defaultSubAgent) {
|
|
116
|
+
const defaultSubAgent = teamAgentWithDefault.defaultSubAgent;
|
|
117
|
+
const relatedAgentRelations = await getRelatedAgentsForAgent(dbClient_default)({
|
|
118
|
+
scopes: {
|
|
119
|
+
tenantId: config.tenantId,
|
|
120
|
+
projectId: config.projectId,
|
|
121
|
+
agentId: relation.targetAgentId
|
|
122
|
+
},
|
|
123
|
+
subAgentId: defaultSubAgent.id
|
|
124
|
+
});
|
|
125
|
+
const relatedAgentExternalAgentRelations = await getExternalAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
126
|
+
tenantId: config.tenantId,
|
|
127
|
+
projectId: config.projectId,
|
|
128
|
+
agentId: relation.targetAgentId,
|
|
129
|
+
subAgentId: defaultSubAgent.id
|
|
130
|
+
} });
|
|
131
|
+
const relatedAgentTeamAgentRelations = await getTeamAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
132
|
+
tenantId: config.tenantId,
|
|
133
|
+
projectId: config.projectId,
|
|
134
|
+
agentId: relation.targetAgentId,
|
|
135
|
+
subAgentId: defaultSubAgent.id
|
|
136
|
+
} });
|
|
137
|
+
const enhancedDescription = generateDescriptionWithRelationData(teamAgentWithDefault.description || "", relatedAgentRelations.data, relatedAgentExternalAgentRelations.data, relatedAgentTeamAgentRelations.data);
|
|
138
|
+
return {
|
|
139
|
+
...relation,
|
|
140
|
+
targetAgent: {
|
|
141
|
+
...relation.targetAgent,
|
|
142
|
+
description: enhancedDescription
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
logger.warn({
|
|
148
|
+
targetAgentId: relation.targetAgentId,
|
|
149
|
+
error
|
|
150
|
+
}, "Failed to enhance team agent description");
|
|
151
|
+
}
|
|
152
|
+
return relation;
|
|
153
|
+
}));
|
|
154
|
+
const prompt = "prompt" in config.agentSchema ? config.agentSchema.prompt || void 0 : "";
|
|
155
|
+
const models = "models" in config.agentSchema ? config.agentSchema.models : void 0;
|
|
156
|
+
const stopWhen = "stopWhen" in config.agentSchema ? config.agentSchema.stopWhen : void 0;
|
|
157
|
+
const toolsForAgentResult = await Promise.all(toolsForAgent.data.map(async (item) => {
|
|
158
|
+
const mcpTool = await dbResultToMcpTool(item.tool, dbClient_default, credentialStoreRegistry, item.id, config.userId);
|
|
159
|
+
if (item.selectedTools && item.selectedTools.length > 0) {
|
|
160
|
+
const selectedToolsSet = new Set(item.selectedTools);
|
|
161
|
+
mcpTool.availableTools = mcpTool.availableTools?.filter((tool) => selectedToolsSet.has(tool.name)) || [];
|
|
162
|
+
}
|
|
163
|
+
return mcpTool;
|
|
164
|
+
})) ?? [];
|
|
165
|
+
agent = new Agent({
|
|
166
|
+
id: config.subAgentId,
|
|
167
|
+
tenantId: config.tenantId,
|
|
168
|
+
projectId: config.projectId,
|
|
169
|
+
agentId: config.agentId,
|
|
170
|
+
baseUrl: config.baseUrl,
|
|
171
|
+
apiKey: config.apiKey,
|
|
172
|
+
userId: config.userId,
|
|
173
|
+
name: config.name,
|
|
174
|
+
description: config.description || "",
|
|
175
|
+
prompt,
|
|
176
|
+
models: models || void 0,
|
|
177
|
+
stopWhen: stopWhen || void 0,
|
|
178
|
+
subAgentRelations: enhancedInternalRelations.map((relation) => ({
|
|
179
|
+
id: relation.id,
|
|
180
|
+
tenantId: config.tenantId,
|
|
181
|
+
projectId: config.projectId,
|
|
182
|
+
agentId: config.agentId,
|
|
183
|
+
baseUrl: config.baseUrl,
|
|
184
|
+
apiKey: config.apiKey,
|
|
185
|
+
name: relation.name,
|
|
186
|
+
description: relation.description || void 0,
|
|
187
|
+
prompt: "",
|
|
188
|
+
delegateRelations: [],
|
|
189
|
+
subAgentRelations: [],
|
|
190
|
+
transferRelations: []
|
|
191
|
+
})),
|
|
192
|
+
transferRelations: await Promise.all(enhancedInternalRelations.filter((relation) => relation.relationType === "transfer").map(async (relation) => {
|
|
193
|
+
const targetToolsForAgent = await getToolsForAgent(dbClient_default)({ scopes: {
|
|
194
|
+
tenantId: config.tenantId,
|
|
195
|
+
projectId: config.projectId,
|
|
196
|
+
agentId: config.agentId,
|
|
197
|
+
subAgentId: relation.id
|
|
198
|
+
} });
|
|
199
|
+
let targetTransferRelations = { data: [] };
|
|
200
|
+
let targetDelegateRelations = { data: [] };
|
|
201
|
+
try {
|
|
202
|
+
const [transferRel, delegateRel] = await Promise.all([getRelatedAgentsForAgent(dbClient_default)({
|
|
203
|
+
scopes: {
|
|
204
|
+
tenantId: config.tenantId,
|
|
205
|
+
projectId: config.projectId,
|
|
206
|
+
agentId: config.agentId
|
|
207
|
+
},
|
|
208
|
+
subAgentId: relation.id
|
|
209
|
+
}), getExternalAgentsForSubAgent(dbClient_default)({ scopes: {
|
|
210
|
+
tenantId: config.tenantId,
|
|
211
|
+
projectId: config.projectId,
|
|
212
|
+
agentId: config.agentId,
|
|
213
|
+
subAgentId: relation.id
|
|
214
|
+
} })]);
|
|
215
|
+
targetTransferRelations = transferRel;
|
|
216
|
+
targetDelegateRelations = delegateRel;
|
|
217
|
+
} catch (err) {
|
|
218
|
+
logger.info({
|
|
219
|
+
agentId: relation.id,
|
|
220
|
+
error: err?.message || "Unknown error"
|
|
221
|
+
}, "Could not fetch relations for target agent (likely external/team agent), using basic info only");
|
|
222
|
+
}
|
|
223
|
+
const targetAgentTools = await Promise.all(targetToolsForAgent.data.map(async (item) => {
|
|
224
|
+
const mcpTool = await dbResultToMcpTool(item.tool, dbClient_default, credentialStoreRegistry, item.id, config.userId);
|
|
225
|
+
if (item.selectedTools && item.selectedTools.length > 0) {
|
|
226
|
+
const selectedToolsSet = new Set(item.selectedTools);
|
|
227
|
+
mcpTool.availableTools = mcpTool.availableTools?.filter((tool) => selectedToolsSet.has(tool.name)) || [];
|
|
228
|
+
}
|
|
229
|
+
return mcpTool;
|
|
230
|
+
})) ?? [];
|
|
231
|
+
const targetTransferRelationsConfig = targetTransferRelations.data.filter((rel) => rel.relationType === "transfer").map((rel) => ({
|
|
232
|
+
baseUrl: config.baseUrl,
|
|
233
|
+
apiKey: config.apiKey,
|
|
234
|
+
id: rel.id,
|
|
235
|
+
tenantId: config.tenantId,
|
|
236
|
+
projectId: config.projectId,
|
|
237
|
+
agentId: config.agentId,
|
|
238
|
+
name: rel.name,
|
|
239
|
+
description: rel.description,
|
|
240
|
+
prompt: "",
|
|
241
|
+
delegateRelations: [],
|
|
242
|
+
subAgentRelations: [],
|
|
243
|
+
transferRelations: []
|
|
244
|
+
}));
|
|
245
|
+
const targetDelegateRelationsConfig = targetDelegateRelations.data.map((rel) => ({
|
|
246
|
+
type: "external",
|
|
247
|
+
config: {
|
|
248
|
+
id: rel.externalAgent.id,
|
|
249
|
+
name: rel.externalAgent.name,
|
|
250
|
+
description: rel.externalAgent.description || "",
|
|
251
|
+
baseUrl: rel.externalAgent.baseUrl,
|
|
252
|
+
headers: rel.headers,
|
|
253
|
+
credentialReferenceId: rel.externalAgent.credentialReferenceId,
|
|
254
|
+
relationId: rel.id,
|
|
255
|
+
relationType: "delegate"
|
|
256
|
+
}
|
|
257
|
+
}));
|
|
258
|
+
return {
|
|
259
|
+
baseUrl: config.baseUrl,
|
|
260
|
+
apiKey: config.apiKey,
|
|
261
|
+
id: relation.id,
|
|
262
|
+
tenantId: config.tenantId,
|
|
263
|
+
projectId: config.projectId,
|
|
264
|
+
agentId: config.agentId,
|
|
265
|
+
name: relation.name,
|
|
266
|
+
description: relation.description || void 0,
|
|
267
|
+
prompt: "",
|
|
268
|
+
delegateRelations: targetDelegateRelationsConfig,
|
|
269
|
+
subAgentRelations: [],
|
|
270
|
+
transferRelations: targetTransferRelationsConfig,
|
|
271
|
+
tools: targetAgentTools
|
|
272
|
+
};
|
|
273
|
+
})),
|
|
274
|
+
delegateRelations: [
|
|
275
|
+
...enhancedInternalRelations.filter((relation) => relation.relationType === "delegate").map((relation) => ({
|
|
276
|
+
type: "internal",
|
|
277
|
+
config: {
|
|
278
|
+
id: relation.id,
|
|
279
|
+
relationId: relation.relationId,
|
|
280
|
+
tenantId: config.tenantId,
|
|
281
|
+
projectId: config.projectId,
|
|
282
|
+
agentId: config.agentId,
|
|
283
|
+
baseUrl: config.baseUrl,
|
|
284
|
+
apiKey: config.apiKey,
|
|
285
|
+
name: relation.name,
|
|
286
|
+
description: relation.description || void 0,
|
|
287
|
+
prompt: "",
|
|
288
|
+
delegateRelations: [],
|
|
289
|
+
subAgentRelations: [],
|
|
290
|
+
transferRelations: [],
|
|
291
|
+
tools: []
|
|
292
|
+
}
|
|
293
|
+
})),
|
|
294
|
+
...externalRelations.data.map((relation) => ({
|
|
295
|
+
type: "external",
|
|
296
|
+
config: {
|
|
297
|
+
id: relation.externalAgent.id,
|
|
298
|
+
name: relation.externalAgent.name,
|
|
299
|
+
description: relation.externalAgent.description || "",
|
|
300
|
+
baseUrl: relation.externalAgent.baseUrl,
|
|
301
|
+
headers: relation.headers,
|
|
302
|
+
credentialReferenceId: relation.externalAgent.credentialReferenceId,
|
|
303
|
+
relationId: relation.id,
|
|
304
|
+
relationType: "delegate"
|
|
305
|
+
}
|
|
306
|
+
})),
|
|
307
|
+
...enhancedTeamRelations.map((relation) => ({
|
|
308
|
+
type: "team",
|
|
309
|
+
config: {
|
|
310
|
+
id: relation.targetAgent.id,
|
|
311
|
+
name: relation.targetAgent.name,
|
|
312
|
+
description: relation.targetAgent.description || "",
|
|
313
|
+
baseUrl: config.baseUrl,
|
|
314
|
+
headers: relation.headers,
|
|
315
|
+
relationId: relation.id
|
|
316
|
+
}
|
|
317
|
+
}))
|
|
318
|
+
],
|
|
319
|
+
tools: toolsForAgentResult,
|
|
320
|
+
functionTools: [],
|
|
321
|
+
dataComponents,
|
|
322
|
+
artifactComponents,
|
|
323
|
+
contextConfigId: config.contextConfigId || void 0,
|
|
324
|
+
conversationHistoryConfig: config.conversationHistoryConfig,
|
|
325
|
+
sandboxConfig: config.sandboxConfig
|
|
326
|
+
}, credentialStoreRegistry);
|
|
327
|
+
const artifactStreamRequestId = task.context?.metadata?.streamRequestId;
|
|
328
|
+
if (artifactStreamRequestId && artifactComponents.length > 0) agentSessionManager.updateArtifactComponents(artifactStreamRequestId, artifactComponents);
|
|
329
|
+
let contextId = task.context?.conversationId;
|
|
330
|
+
if (!contextId || contextId === "default" || contextId === "") {
|
|
331
|
+
const taskIdMatch = task.id.match(/^task_([^-]+-[^-]+-\d+)-/);
|
|
332
|
+
if (taskIdMatch) {
|
|
333
|
+
contextId = taskIdMatch[1];
|
|
334
|
+
logger.info({
|
|
335
|
+
taskId: task.id,
|
|
336
|
+
extractedContextId: contextId,
|
|
337
|
+
subAgentId: config.subAgentId
|
|
338
|
+
}, "Extracted contextId from task ID for delegation");
|
|
339
|
+
} else contextId = "default";
|
|
340
|
+
}
|
|
341
|
+
const streamRequestId = task.context?.metadata?.stream_request_id || task.context?.metadata?.streamRequestId;
|
|
342
|
+
const isDelegation = task.context?.metadata?.isDelegation === true;
|
|
343
|
+
const delegationId = task.context?.metadata?.delegationId;
|
|
344
|
+
agent.setDelegationStatus(isDelegation);
|
|
345
|
+
agent.setDelegationId(delegationId);
|
|
346
|
+
if (isDelegation) {
|
|
347
|
+
logger.info({
|
|
348
|
+
subAgentId: config.subAgentId,
|
|
349
|
+
taskId: task.id,
|
|
350
|
+
delegationId
|
|
351
|
+
}, "Delegated agent - streaming disabled");
|
|
352
|
+
if (streamRequestId && config.tenantId && config.projectId) toolSessionManager.ensureAgentSession(streamRequestId, config.tenantId, config.projectId, contextId, task.id);
|
|
353
|
+
}
|
|
354
|
+
const response = await agent.generate(userMessage, {
|
|
355
|
+
contextId,
|
|
356
|
+
metadata: {
|
|
357
|
+
conversationId: contextId,
|
|
358
|
+
taskId: task.id,
|
|
359
|
+
threadId: contextId,
|
|
360
|
+
streamRequestId,
|
|
361
|
+
...config.apiKey ? { apiKey: config.apiKey } : {}
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
agent.cleanupCompression();
|
|
365
|
+
const stepContents = response.steps && Array.isArray(response.steps) ? response.steps.flatMap((step) => {
|
|
366
|
+
return step.content && Array.isArray(step.content) ? step.content : [];
|
|
367
|
+
}) : [];
|
|
368
|
+
const allToolCalls = stepContents.filter((content) => content.type === "tool-call");
|
|
369
|
+
const allToolResults = stepContents.filter((content) => content.type === "tool-result");
|
|
370
|
+
const allThoughts = stepContents.filter((content) => content.type === "text");
|
|
371
|
+
if (allToolCalls.length > 0) {
|
|
372
|
+
for (const toolCall of allToolCalls) if (toolCall.toolName.includes("transfer") || toolCall.toolName.includes("transferToRefundAgent")) {
|
|
373
|
+
const toolResult = allToolResults.find((result) => result.toolCallId === toolCall.toolCallId);
|
|
374
|
+
logger.info({
|
|
375
|
+
toolCallName: toolCall.toolName,
|
|
376
|
+
toolCallId: toolCall.toolCallId,
|
|
377
|
+
hasToolResult: !!toolResult,
|
|
378
|
+
toolResultOutput: toolResult?.output,
|
|
379
|
+
toolResultKeys: toolResult?.output ? Object.keys(toolResult.output) : []
|
|
380
|
+
}, "[DEBUG] Transfer tool result found");
|
|
381
|
+
const isValidTransferResult = (output) => {
|
|
382
|
+
return typeof output === "object" && output !== null && "type" in output && "targetSubAgentId" in output && output.type === "transfer" && typeof output.targetSubAgentId === "string";
|
|
383
|
+
};
|
|
384
|
+
const transferReason = response.text || (response.object ? JSON.stringify(response.object) : "") || allThoughts[allThoughts.length - 1]?.text || "Agent requested transfer. No reason provided.";
|
|
385
|
+
if (toolResult?.output && isValidTransferResult(toolResult.output)) {
|
|
386
|
+
const transferResult = toolResult.output;
|
|
387
|
+
logger.info({
|
|
388
|
+
validationPassed: true,
|
|
389
|
+
transferResult,
|
|
390
|
+
targetSubAgentId: transferResult.targetSubAgentId,
|
|
391
|
+
fromSubAgentId: transferResult.fromSubAgentId
|
|
392
|
+
}, "[DEBUG] Transfer validation passed, extracted data");
|
|
393
|
+
const artifactData = {
|
|
394
|
+
type: "transfer",
|
|
395
|
+
targetSubAgentId: transferResult.targetSubAgentId,
|
|
396
|
+
fromSubAgentId: transferResult.fromSubAgentId,
|
|
397
|
+
task_id: task.id,
|
|
398
|
+
reason: transferReason,
|
|
399
|
+
original_message: userMessage
|
|
400
|
+
};
|
|
401
|
+
logger.info({
|
|
402
|
+
artifactData,
|
|
403
|
+
artifactDataKeys: Object.keys(artifactData)
|
|
404
|
+
}, "[DEBUG] Artifact data being returned");
|
|
405
|
+
return {
|
|
406
|
+
status: {
|
|
407
|
+
state: TaskState.Completed,
|
|
408
|
+
message: `Transfer requested to ${transferResult.targetSubAgentId}`
|
|
409
|
+
},
|
|
410
|
+
artifacts: [{
|
|
411
|
+
artifactId: generateId(),
|
|
412
|
+
parts: [{
|
|
413
|
+
kind: "data",
|
|
414
|
+
data: artifactData
|
|
415
|
+
}],
|
|
416
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
417
|
+
}]
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
logger.warn({
|
|
421
|
+
hasToolResult: !!toolResult,
|
|
422
|
+
hasOutput: !!toolResult?.output,
|
|
423
|
+
validationPassed: false,
|
|
424
|
+
output: toolResult?.output
|
|
425
|
+
}, "[DEBUG] Transfer validation FAILED");
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
const parts = (response.formattedContent?.parts || []).map((part) => ({
|
|
429
|
+
kind: part.kind,
|
|
430
|
+
...part.kind === "text" && { text: part.text },
|
|
431
|
+
...part.kind === "data" && { data: part.data }
|
|
432
|
+
}));
|
|
433
|
+
return {
|
|
434
|
+
status: { state: TaskState.Completed },
|
|
435
|
+
artifacts: [{
|
|
436
|
+
artifactId: generateId(),
|
|
437
|
+
parts,
|
|
438
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
439
|
+
}]
|
|
440
|
+
};
|
|
441
|
+
} catch (error) {
|
|
442
|
+
console.error("Task handler error:", error);
|
|
443
|
+
try {
|
|
444
|
+
if (agent) agent.cleanupCompression();
|
|
445
|
+
} catch (cleanupError) {
|
|
446
|
+
logger.warn({ cleanupError }, "Failed to cleanup agent compression on error");
|
|
447
|
+
}
|
|
448
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
449
|
+
const isConnectionRefused = errorMessage.includes("Connection refused. Please check if the MCP server is running.");
|
|
450
|
+
return {
|
|
451
|
+
status: {
|
|
452
|
+
state: TaskState.Failed,
|
|
453
|
+
message: errorMessage,
|
|
454
|
+
type: isConnectionRefused ? "connection_refused" : "unknown"
|
|
455
|
+
},
|
|
456
|
+
artifacts: []
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
/**
|
|
462
|
+
* Serializes a TaskHandlerConfig to JSON
|
|
463
|
+
*/
|
|
464
|
+
const serializeTaskHandlerConfig = (config) => {
|
|
465
|
+
return JSON.stringify(config, null, 2);
|
|
466
|
+
};
|
|
467
|
+
/**
|
|
468
|
+
* Deserializes a TaskHandlerConfig from JSON
|
|
469
|
+
*/
|
|
470
|
+
const deserializeTaskHandlerConfig = (configJson) => {
|
|
471
|
+
return JSON.parse(configJson);
|
|
472
|
+
};
|
|
473
|
+
/**
|
|
474
|
+
* Creates a task handler configuration from agent data
|
|
475
|
+
*/
|
|
476
|
+
const createTaskHandlerConfig = async (params) => {
|
|
477
|
+
const subAgent = await getSubAgentById(dbClient_default)({
|
|
478
|
+
scopes: {
|
|
479
|
+
tenantId: params.tenantId,
|
|
480
|
+
projectId: params.projectId,
|
|
481
|
+
agentId: params.agentId
|
|
482
|
+
},
|
|
483
|
+
subAgentId: params.subAgentId
|
|
484
|
+
});
|
|
485
|
+
const agent = await getAgentById(dbClient_default)({ scopes: {
|
|
486
|
+
tenantId: params.tenantId,
|
|
487
|
+
projectId: params.projectId,
|
|
488
|
+
agentId: params.agentId
|
|
489
|
+
} });
|
|
490
|
+
if (!subAgent) throw new Error(`Agent not found: ${params.subAgentId}`);
|
|
491
|
+
const effectiveModels = await resolveModelConfig(params.agentId, subAgent);
|
|
492
|
+
const effectiveConversationHistoryConfig = subAgent.conversationHistoryConfig;
|
|
493
|
+
return {
|
|
494
|
+
tenantId: params.tenantId,
|
|
495
|
+
projectId: params.projectId,
|
|
496
|
+
agentId: params.agentId,
|
|
497
|
+
subAgentId: params.subAgentId,
|
|
498
|
+
agentSchema: {
|
|
499
|
+
id: subAgent.id,
|
|
500
|
+
name: subAgent.name,
|
|
501
|
+
description: subAgent.description,
|
|
502
|
+
prompt: subAgent.prompt,
|
|
503
|
+
models: effectiveModels,
|
|
504
|
+
conversationHistoryConfig: effectiveConversationHistoryConfig || null,
|
|
505
|
+
stopWhen: subAgent.stopWhen || null,
|
|
506
|
+
createdAt: subAgent.createdAt,
|
|
507
|
+
updatedAt: subAgent.updatedAt
|
|
508
|
+
},
|
|
509
|
+
baseUrl: params.baseUrl,
|
|
510
|
+
apiKey: params.apiKey,
|
|
511
|
+
name: subAgent.name,
|
|
512
|
+
description: subAgent.description || void 0,
|
|
513
|
+
conversationHistoryConfig: effectiveConversationHistoryConfig,
|
|
514
|
+
contextConfigId: agent?.contextConfigId || void 0,
|
|
515
|
+
sandboxConfig: params.sandboxConfig,
|
|
516
|
+
userId: params.userId
|
|
517
|
+
};
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
//#endregion
|
|
521
|
+
export { createTaskHandler, createTaskHandlerConfig, deserializeTaskHandlerConfig, serializeTaskHandlerConfig };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { AgentConfig, DelegateRelation } from "./Agent.js";
|
|
2
|
+
import * as _inkeep_agents_core0 from "@inkeep/agents-core";
|
|
3
|
+
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
4
|
+
import * as ai0 from "ai";
|
|
5
|
+
|
|
6
|
+
//#region src/agents/relationTools.d.ts
|
|
7
|
+
declare const createTransferToAgentTool: ({
|
|
8
|
+
transferConfig,
|
|
9
|
+
callingAgentId,
|
|
10
|
+
subAgent,
|
|
11
|
+
streamRequestId
|
|
12
|
+
}: {
|
|
13
|
+
transferConfig: AgentConfig;
|
|
14
|
+
callingAgentId: string;
|
|
15
|
+
subAgent: any;
|
|
16
|
+
streamRequestId?: string;
|
|
17
|
+
}) => ai0.Tool<Record<string, never>, {
|
|
18
|
+
type: string;
|
|
19
|
+
targetSubAgentId: string;
|
|
20
|
+
fromSubAgentId: string;
|
|
21
|
+
}>;
|
|
22
|
+
declare function createDelegateToAgentTool({
|
|
23
|
+
delegateConfig,
|
|
24
|
+
callingAgentId,
|
|
25
|
+
tenantId,
|
|
26
|
+
projectId,
|
|
27
|
+
agentId,
|
|
28
|
+
contextId,
|
|
29
|
+
metadata,
|
|
30
|
+
sessionId,
|
|
31
|
+
subAgent,
|
|
32
|
+
credentialStoreRegistry
|
|
33
|
+
}: {
|
|
34
|
+
delegateConfig: DelegateRelation;
|
|
35
|
+
callingAgentId: string;
|
|
36
|
+
tenantId: string;
|
|
37
|
+
projectId: string;
|
|
38
|
+
agentId: string;
|
|
39
|
+
contextId: string;
|
|
40
|
+
metadata: {
|
|
41
|
+
conversationId: string;
|
|
42
|
+
threadId: string;
|
|
43
|
+
streamRequestId?: string;
|
|
44
|
+
streamBaseUrl?: string;
|
|
45
|
+
apiKey?: string;
|
|
46
|
+
};
|
|
47
|
+
sessionId?: string;
|
|
48
|
+
subAgent: any;
|
|
49
|
+
credentialStoreRegistry?: CredentialStoreRegistry;
|
|
50
|
+
}): ai0.Tool<{
|
|
51
|
+
message: string;
|
|
52
|
+
}, {
|
|
53
|
+
toolCallId: any;
|
|
54
|
+
result: _inkeep_agents_core0.Message | _inkeep_agents_core0.Task;
|
|
55
|
+
}>;
|
|
56
|
+
//#endregion
|
|
57
|
+
export { createDelegateToAgentTool, createTransferToAgentTool };
|