@inkeep/agents-run-api 0.39.5 → 0.41.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 +576 -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 +268 -0
- package/dist/agents/Agent.js +1932 -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 +523 -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 +41 -0
- package/dist/handlers/executionHandler.js +457 -0
- package/dist/index.d.ts +8 -29
- package/dist/index.js +5 -11386
- 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 +305 -0
- package/dist/routes/chatDataStream.d.ts +13 -0
- package/dist/routes/chatDataStream.js +365 -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 +500 -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} +2 -3
- 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 +10 -26
- 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 -11411
- 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,576 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { TaskState, createMessage, createTask, generateId, getRequestExecutionContext, updateTask } from "@inkeep/agents-core";
|
|
4
|
+
import { streamSSE } from "hono/streaming";
|
|
5
|
+
|
|
6
|
+
//#region src/a2a/handlers.ts
|
|
7
|
+
const logger = getLogger("a2aHandler");
|
|
8
|
+
async function a2aHandler(c, agent) {
|
|
9
|
+
try {
|
|
10
|
+
const rpcRequest = c.get("requestBody");
|
|
11
|
+
if (rpcRequest.jsonrpc !== "2.0") return c.json({
|
|
12
|
+
jsonrpc: "2.0",
|
|
13
|
+
error: {
|
|
14
|
+
code: -32600,
|
|
15
|
+
message: "Invalid Request - must be JSON-RPC 2.0"
|
|
16
|
+
},
|
|
17
|
+
id: rpcRequest.id
|
|
18
|
+
});
|
|
19
|
+
switch (rpcRequest.method) {
|
|
20
|
+
case "message/send": return await handleMessageSend(c, agent, rpcRequest);
|
|
21
|
+
case "message/stream": return await handleMessageStream(c, agent, rpcRequest);
|
|
22
|
+
case "tasks/get": return await handleTasksGet(c, agent, rpcRequest);
|
|
23
|
+
case "tasks/cancel": return await handleTasksCancel(c, agent, rpcRequest);
|
|
24
|
+
case "tasks/resubscribe": return await handleTasksResubscribe(c, agent, rpcRequest);
|
|
25
|
+
case "agent.invoke": return await handleAgentInvoke(c, agent, rpcRequest);
|
|
26
|
+
case "agent.getCapabilities": return await handleGetCapabilities(c, agent, rpcRequest);
|
|
27
|
+
case "agent.getStatus": return await handleGetStatus(c, agent, rpcRequest);
|
|
28
|
+
default: return c.json({
|
|
29
|
+
jsonrpc: "2.0",
|
|
30
|
+
error: {
|
|
31
|
+
code: -32601,
|
|
32
|
+
message: `Method not found: ${rpcRequest.method}`
|
|
33
|
+
},
|
|
34
|
+
id: rpcRequest.id
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error("A2A Handler Error:", error);
|
|
39
|
+
return c.json({
|
|
40
|
+
jsonrpc: "2.0",
|
|
41
|
+
error: {
|
|
42
|
+
code: -32700,
|
|
43
|
+
message: "Parse error"
|
|
44
|
+
},
|
|
45
|
+
id: null
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function handleMessageSend(c, agent, request) {
|
|
50
|
+
try {
|
|
51
|
+
const params = request.params;
|
|
52
|
+
const { agentId } = getRequestExecutionContext(c);
|
|
53
|
+
const forwardedHeaders = {};
|
|
54
|
+
const xForwardedCookie = c.req.header("x-forwarded-cookie");
|
|
55
|
+
const authorization = c.req.header("authorization");
|
|
56
|
+
const cookie = c.req.header("cookie");
|
|
57
|
+
if (xForwardedCookie) forwardedHeaders["x-forwarded-cookie"] = xForwardedCookie;
|
|
58
|
+
else if (cookie) forwardedHeaders["x-forwarded-cookie"] = cookie;
|
|
59
|
+
if (authorization) forwardedHeaders.authorization = authorization;
|
|
60
|
+
const task = {
|
|
61
|
+
id: generateId(),
|
|
62
|
+
input: { parts: params.message.parts.map((part) => ({
|
|
63
|
+
kind: part.kind,
|
|
64
|
+
text: part.kind === "text" ? part.text : void 0,
|
|
65
|
+
data: part.kind === "data" ? part.data : void 0
|
|
66
|
+
})) },
|
|
67
|
+
context: {
|
|
68
|
+
conversationId: params.message.contextId,
|
|
69
|
+
metadata: {
|
|
70
|
+
blocking: params.configuration?.blocking ?? false,
|
|
71
|
+
custom: { agent_id: agentId || "" },
|
|
72
|
+
...params.message.metadata,
|
|
73
|
+
forwardedHeaders: Object.keys(forwardedHeaders).length > 0 ? forwardedHeaders : void 0
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
let effectiveContextId = params.message?.contextId;
|
|
78
|
+
if (!effectiveContextId || effectiveContextId === "default") effectiveContextId = task.context?.conversationId;
|
|
79
|
+
if (!effectiveContextId || effectiveContextId === "default") {
|
|
80
|
+
if (params.message?.metadata?.conversationId && params.message.metadata.conversationId !== "default") effectiveContextId = params.message.metadata.conversationId;
|
|
81
|
+
}
|
|
82
|
+
if (!effectiveContextId || effectiveContextId === "default") effectiveContextId = "default";
|
|
83
|
+
try {
|
|
84
|
+
if (params.message && Object.keys(params.message).length > 0) JSON.stringify(params.message);
|
|
85
|
+
else {
|
|
86
|
+
JSON.stringify({
|
|
87
|
+
role: "agent",
|
|
88
|
+
parts: [{
|
|
89
|
+
text: "Delegation task",
|
|
90
|
+
kind: "text"
|
|
91
|
+
}],
|
|
92
|
+
contextId: effectiveContextId,
|
|
93
|
+
messageId: task.id,
|
|
94
|
+
kind: "message"
|
|
95
|
+
});
|
|
96
|
+
logger.warn({
|
|
97
|
+
taskId: task.id,
|
|
98
|
+
subAgentId: agent.subAgentId,
|
|
99
|
+
originalMessage: params.message
|
|
100
|
+
}, "Created fallback message content for empty delegation message");
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
logger.error({
|
|
104
|
+
error,
|
|
105
|
+
taskId: task.id
|
|
106
|
+
}, "Failed to serialize message");
|
|
107
|
+
JSON.stringify({
|
|
108
|
+
error: "Failed to serialize message",
|
|
109
|
+
taskId: task.id,
|
|
110
|
+
contextId: effectiveContextId,
|
|
111
|
+
parts: [{
|
|
112
|
+
text: "Error in delegation",
|
|
113
|
+
kind: "text"
|
|
114
|
+
}]
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
logger.info({
|
|
118
|
+
originalContextId: params.message.contextId,
|
|
119
|
+
taskContextId: task.context?.conversationId,
|
|
120
|
+
metadataContextId: params.message.metadata?.conversationId,
|
|
121
|
+
finalContextId: effectiveContextId,
|
|
122
|
+
subAgentId: agent.subAgentId
|
|
123
|
+
}, "A2A contextId resolution for delegation");
|
|
124
|
+
await createTask(dbClient_default)({
|
|
125
|
+
id: task.id,
|
|
126
|
+
tenantId: agent.tenantId,
|
|
127
|
+
projectId: agent.projectId,
|
|
128
|
+
agentId: agentId || "",
|
|
129
|
+
contextId: effectiveContextId,
|
|
130
|
+
status: "working",
|
|
131
|
+
metadata: {
|
|
132
|
+
conversation_id: effectiveContextId,
|
|
133
|
+
message_id: params.message.messageId || "",
|
|
134
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
135
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
136
|
+
sub_agent_id: agent.subAgentId,
|
|
137
|
+
agent_id: agentId || "",
|
|
138
|
+
stream_request_id: params.message.metadata?.stream_request_id
|
|
139
|
+
},
|
|
140
|
+
subAgentId: agent.subAgentId,
|
|
141
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
142
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
143
|
+
});
|
|
144
|
+
logger.info({ metadata: params.message.metadata }, "message metadata");
|
|
145
|
+
if (params.message.metadata?.fromSubAgentId || params.message.metadata?.fromExternalAgentId || params.message.metadata?.fromTeamAgentId) {
|
|
146
|
+
const messageText = params.message.parts.filter((part) => part.kind === "text" && "text" in part && part.text).map((part) => part.text).join(" ");
|
|
147
|
+
try {
|
|
148
|
+
const messageData = {
|
|
149
|
+
id: generateId(),
|
|
150
|
+
tenantId: agent.tenantId,
|
|
151
|
+
projectId: agent.projectId,
|
|
152
|
+
conversationId: effectiveContextId,
|
|
153
|
+
role: "agent",
|
|
154
|
+
content: { text: messageText },
|
|
155
|
+
visibility: params.message.metadata?.fromExternalAgentId ? "external" : "internal",
|
|
156
|
+
messageType: "a2a-request",
|
|
157
|
+
taskId: task.id
|
|
158
|
+
};
|
|
159
|
+
if (params.message.metadata?.fromSubAgentId) {
|
|
160
|
+
messageData.fromSubAgentId = params.message.metadata.fromSubAgentId;
|
|
161
|
+
messageData.toSubAgentId = agent.subAgentId;
|
|
162
|
+
} else if (params.message.metadata?.fromExternalAgentId) {
|
|
163
|
+
messageData.fromExternalAgentId = params.message.metadata.fromExternalAgentId;
|
|
164
|
+
messageData.toSubAgentId = agent.subAgentId;
|
|
165
|
+
} else if (params.message.metadata?.fromTeamAgentId) {
|
|
166
|
+
messageData.fromTeamAgentId = params.message.metadata.fromTeamAgentId;
|
|
167
|
+
messageData.toTeamAgentId = agent.subAgentId;
|
|
168
|
+
}
|
|
169
|
+
await createMessage(dbClient_default)(messageData);
|
|
170
|
+
logger.info({
|
|
171
|
+
fromSubAgentId: params.message.metadata.fromSubAgentId,
|
|
172
|
+
fromExternalAgentId: params.message.metadata.fromExternalAgentId,
|
|
173
|
+
fromTeamAgentId: params.message.metadata.fromTeamAgentId,
|
|
174
|
+
toSubAgentId: agent.subAgentId,
|
|
175
|
+
toTeamAgentId: params.message.metadata.fromTeamAgentId ? agent.subAgentId : void 0,
|
|
176
|
+
conversationId: effectiveContextId,
|
|
177
|
+
messageType: "a2a-request",
|
|
178
|
+
taskId: task.id
|
|
179
|
+
}, "A2A message stored in database");
|
|
180
|
+
} catch (error) {
|
|
181
|
+
logger.error({
|
|
182
|
+
error,
|
|
183
|
+
fromSubAgentId: params.message.metadata.fromSubAgentId,
|
|
184
|
+
fromExternalAgentId: params.message.metadata.fromExternalAgentId,
|
|
185
|
+
fromTeamAgentId: params.message.metadata.fromTeamAgentId,
|
|
186
|
+
toSubAgentId: agent.subAgentId,
|
|
187
|
+
conversationId: effectiveContextId
|
|
188
|
+
}, "Failed to store A2A message in database");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const result = await agent.taskHandler(task);
|
|
192
|
+
await updateTask(dbClient_default)({
|
|
193
|
+
taskId: task.id,
|
|
194
|
+
data: {
|
|
195
|
+
status: result.status.state.toLowerCase(),
|
|
196
|
+
metadata: {
|
|
197
|
+
conversation_id: params.message.contextId || "",
|
|
198
|
+
message_id: params.message.messageId || "",
|
|
199
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
200
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
201
|
+
sub_agent_id: agent.subAgentId,
|
|
202
|
+
agent_id: agentId || ""
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
const transferArtifact = result.artifacts?.find((artifact) => artifact.parts?.some((part) => part.kind === "data" && part.data && typeof part.data === "object" && part.data.type === "transfer"));
|
|
207
|
+
if (transferArtifact) {
|
|
208
|
+
const transferPart = transferArtifact.parts?.find((part) => part.kind === "data" && part.data && typeof part.data === "object" && part.data.type === "transfer");
|
|
209
|
+
if (transferPart && transferPart.kind === "data" && transferPart.data) {
|
|
210
|
+
logger.info({ transferPart }, "transferPart");
|
|
211
|
+
return c.json({
|
|
212
|
+
jsonrpc: "2.0",
|
|
213
|
+
result: {
|
|
214
|
+
kind: "task",
|
|
215
|
+
contextId: params.message.contextId,
|
|
216
|
+
id: task.id,
|
|
217
|
+
status: {
|
|
218
|
+
state: TaskState.Completed,
|
|
219
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
220
|
+
},
|
|
221
|
+
artifacts: [{
|
|
222
|
+
artifactId: generateId(),
|
|
223
|
+
parts: [{
|
|
224
|
+
kind: "data",
|
|
225
|
+
data: {
|
|
226
|
+
type: "transfer",
|
|
227
|
+
targetSubAgentId: transferPart.data.targetSubAgentId,
|
|
228
|
+
fromSubAgentId: transferPart.data.fromSubAgentId
|
|
229
|
+
}
|
|
230
|
+
}, {
|
|
231
|
+
kind: "text",
|
|
232
|
+
text: transferPart.data.reason || "Agent requested transfer"
|
|
233
|
+
}]
|
|
234
|
+
}]
|
|
235
|
+
},
|
|
236
|
+
id: request.id
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (result.status.state === TaskState.Failed) {
|
|
241
|
+
if (result.status.type === "connection_refused") return c.json({
|
|
242
|
+
jsonrpc: "2.0",
|
|
243
|
+
error: {
|
|
244
|
+
code: -32603,
|
|
245
|
+
message: result.status.message || "Agent execution failed",
|
|
246
|
+
data: { type: "connection_refused" }
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const taskStatus = {
|
|
251
|
+
state: result.status.state,
|
|
252
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
253
|
+
};
|
|
254
|
+
if (params.configuration?.blocking === false) {
|
|
255
|
+
const taskResponse = {
|
|
256
|
+
id: task.id,
|
|
257
|
+
contextId: params.message.contextId || generateId(),
|
|
258
|
+
status: taskStatus,
|
|
259
|
+
artifacts: result.artifacts,
|
|
260
|
+
kind: "task"
|
|
261
|
+
};
|
|
262
|
+
return c.json({
|
|
263
|
+
jsonrpc: "2.0",
|
|
264
|
+
result: taskResponse,
|
|
265
|
+
id: request.id
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const messageResponse = {
|
|
269
|
+
messageId: generateId(),
|
|
270
|
+
parts: result.artifacts?.[0]?.parts || [{
|
|
271
|
+
kind: "text",
|
|
272
|
+
text: "Task completed successfully"
|
|
273
|
+
}],
|
|
274
|
+
role: "agent",
|
|
275
|
+
taskId: task.id,
|
|
276
|
+
contextId: params.message.contextId,
|
|
277
|
+
kind: "message"
|
|
278
|
+
};
|
|
279
|
+
return c.json({
|
|
280
|
+
jsonrpc: "2.0",
|
|
281
|
+
result: messageResponse,
|
|
282
|
+
id: request.id
|
|
283
|
+
});
|
|
284
|
+
} catch (error) {
|
|
285
|
+
return c.json({
|
|
286
|
+
jsonrpc: "2.0",
|
|
287
|
+
error: {
|
|
288
|
+
code: -32603,
|
|
289
|
+
message: "Internal error during message send",
|
|
290
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
291
|
+
},
|
|
292
|
+
id: request.id
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
async function handleMessageStream(c, agent, request) {
|
|
297
|
+
try {
|
|
298
|
+
const params = request.params;
|
|
299
|
+
const { agentId } = getRequestExecutionContext(c);
|
|
300
|
+
if (!agent.agentCard.capabilities.streaming) return c.json({
|
|
301
|
+
jsonrpc: "2.0",
|
|
302
|
+
error: {
|
|
303
|
+
code: -32604,
|
|
304
|
+
message: "Agent does not support streaming"
|
|
305
|
+
},
|
|
306
|
+
id: request.id
|
|
307
|
+
});
|
|
308
|
+
const forwardedHeaders = {};
|
|
309
|
+
const xForwardedCookie = c.req.header("x-forwarded-cookie");
|
|
310
|
+
const authorization = c.req.header("authorization");
|
|
311
|
+
const cookie = c.req.header("cookie");
|
|
312
|
+
if (xForwardedCookie) forwardedHeaders["x-forwarded-cookie"] = xForwardedCookie;
|
|
313
|
+
else if (cookie) forwardedHeaders["x-forwarded-cookie"] = cookie;
|
|
314
|
+
if (authorization) forwardedHeaders.authorization = authorization;
|
|
315
|
+
const task = {
|
|
316
|
+
id: generateId(),
|
|
317
|
+
input: { parts: params.message.parts.map((part) => ({
|
|
318
|
+
kind: part.kind,
|
|
319
|
+
text: part.kind === "text" ? part.text : void 0,
|
|
320
|
+
data: part.kind === "data" ? part.data : void 0
|
|
321
|
+
})) },
|
|
322
|
+
context: {
|
|
323
|
+
conversationId: params.message.contextId,
|
|
324
|
+
metadata: {
|
|
325
|
+
blocking: false,
|
|
326
|
+
custom: { agent_id: agentId || "" },
|
|
327
|
+
forwardedHeaders: Object.keys(forwardedHeaders).length > 0 ? forwardedHeaders : void 0
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
return streamSSE(c, async (stream$1) => {
|
|
332
|
+
try {
|
|
333
|
+
const initialTask = {
|
|
334
|
+
id: task.id,
|
|
335
|
+
contextId: params.message.contextId || generateId(),
|
|
336
|
+
status: {
|
|
337
|
+
state: TaskState.Working,
|
|
338
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
339
|
+
},
|
|
340
|
+
artifacts: [],
|
|
341
|
+
kind: "task"
|
|
342
|
+
};
|
|
343
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
344
|
+
jsonrpc: "2.0",
|
|
345
|
+
result: initialTask,
|
|
346
|
+
id: request.id
|
|
347
|
+
}) });
|
|
348
|
+
const result = await agent.taskHandler(task);
|
|
349
|
+
const transferArtifact = result.artifacts?.find((artifact) => artifact.parts?.some((part) => part.kind === "data" && part.data && typeof part.data === "object" && part.data.type === "transfer"));
|
|
350
|
+
if (transferArtifact) {
|
|
351
|
+
const transferPart = transferArtifact.parts?.find((part) => part.kind === "data" && part.data && typeof part.data === "object" && part.data.type === "transfer");
|
|
352
|
+
if (transferPart && transferPart.kind === "data" && transferPart.data) {
|
|
353
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
354
|
+
jsonrpc: "2.0",
|
|
355
|
+
result: {
|
|
356
|
+
type: "transfer",
|
|
357
|
+
target: transferPart.data.targetSubAgentId,
|
|
358
|
+
task_id: task.id,
|
|
359
|
+
reason: transferPart.data.reason || "Agent requested transfer",
|
|
360
|
+
original_message: transferPart.data.original_message,
|
|
361
|
+
context: {
|
|
362
|
+
conversationId: params.message.contextId,
|
|
363
|
+
tenantId: agent.tenantId,
|
|
364
|
+
transfer_context: result.artifacts
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
id: request.id
|
|
368
|
+
}) });
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
const messageResponse = {
|
|
373
|
+
messageId: generateId(),
|
|
374
|
+
parts: result.artifacts?.[0]?.parts || [{
|
|
375
|
+
kind: "text",
|
|
376
|
+
text: "Task completed successfully"
|
|
377
|
+
}],
|
|
378
|
+
role: "agent",
|
|
379
|
+
taskId: task.id,
|
|
380
|
+
contextId: params.message.contextId,
|
|
381
|
+
kind: "message"
|
|
382
|
+
};
|
|
383
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
384
|
+
jsonrpc: "2.0",
|
|
385
|
+
result: messageResponse,
|
|
386
|
+
id: request.id
|
|
387
|
+
}) });
|
|
388
|
+
const completedTask = {
|
|
389
|
+
...initialTask,
|
|
390
|
+
status: {
|
|
391
|
+
state: TaskState.Completed,
|
|
392
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
393
|
+
},
|
|
394
|
+
artifacts: result.artifacts
|
|
395
|
+
};
|
|
396
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
397
|
+
jsonrpc: "2.0",
|
|
398
|
+
result: completedTask,
|
|
399
|
+
id: request.id
|
|
400
|
+
}) });
|
|
401
|
+
} catch (error) {
|
|
402
|
+
console.error("Error in stream execution:", error);
|
|
403
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
404
|
+
jsonrpc: "2.0",
|
|
405
|
+
error: {
|
|
406
|
+
code: -32603,
|
|
407
|
+
message: "Internal error during streaming execution",
|
|
408
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
409
|
+
},
|
|
410
|
+
id: request.id
|
|
411
|
+
}) });
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
} catch (error) {
|
|
415
|
+
return c.json({
|
|
416
|
+
jsonrpc: "2.0",
|
|
417
|
+
error: {
|
|
418
|
+
code: -32603,
|
|
419
|
+
message: "Internal error during message stream setup",
|
|
420
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
421
|
+
},
|
|
422
|
+
id: request.id
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
async function handleTasksGet(c, _agent, request) {
|
|
427
|
+
try {
|
|
428
|
+
const params = request.params;
|
|
429
|
+
const task = {
|
|
430
|
+
id: params.id,
|
|
431
|
+
contextId: generateId(),
|
|
432
|
+
status: {
|
|
433
|
+
state: TaskState.Completed,
|
|
434
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
435
|
+
},
|
|
436
|
+
artifacts: [{
|
|
437
|
+
artifactId: generateId(),
|
|
438
|
+
parts: [{
|
|
439
|
+
kind: "text",
|
|
440
|
+
text: `Task ${params.id} completed successfully`
|
|
441
|
+
}],
|
|
442
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
443
|
+
}],
|
|
444
|
+
kind: "task"
|
|
445
|
+
};
|
|
446
|
+
return c.json({
|
|
447
|
+
jsonrpc: "2.0",
|
|
448
|
+
result: task,
|
|
449
|
+
id: request.id
|
|
450
|
+
});
|
|
451
|
+
} catch (error) {
|
|
452
|
+
return c.json({
|
|
453
|
+
jsonrpc: "2.0",
|
|
454
|
+
error: {
|
|
455
|
+
code: -32603,
|
|
456
|
+
message: "Internal error getting task",
|
|
457
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
458
|
+
},
|
|
459
|
+
id: request.id
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
async function handleTasksCancel(c, _agent, request) {
|
|
464
|
+
try {
|
|
465
|
+
request.params;
|
|
466
|
+
return c.json({
|
|
467
|
+
jsonrpc: "2.0",
|
|
468
|
+
result: { success: true },
|
|
469
|
+
id: request.id
|
|
470
|
+
});
|
|
471
|
+
} catch (error) {
|
|
472
|
+
return c.json({
|
|
473
|
+
jsonrpc: "2.0",
|
|
474
|
+
error: {
|
|
475
|
+
code: -32603,
|
|
476
|
+
message: "Internal error canceling task",
|
|
477
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
478
|
+
},
|
|
479
|
+
id: request.id
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
async function handleAgentInvoke(c, agent, request) {
|
|
484
|
+
try {
|
|
485
|
+
const task = request.params;
|
|
486
|
+
const result = await agent.taskHandler(task);
|
|
487
|
+
return c.json({
|
|
488
|
+
jsonrpc: "2.0",
|
|
489
|
+
result,
|
|
490
|
+
id: request.id
|
|
491
|
+
});
|
|
492
|
+
} catch (error) {
|
|
493
|
+
return c.json({
|
|
494
|
+
jsonrpc: "2.0",
|
|
495
|
+
error: {
|
|
496
|
+
code: -32603,
|
|
497
|
+
message: "Internal error during agent invocation",
|
|
498
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
499
|
+
},
|
|
500
|
+
id: request.id
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
async function handleGetCapabilities(c, agent, request) {
|
|
505
|
+
return c.json({
|
|
506
|
+
jsonrpc: "2.0",
|
|
507
|
+
result: agent.agentCard.capabilities,
|
|
508
|
+
id: request.id
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
async function handleGetStatus(c, agent, request) {
|
|
512
|
+
return c.json({
|
|
513
|
+
jsonrpc: "2.0",
|
|
514
|
+
result: {
|
|
515
|
+
status: "ready",
|
|
516
|
+
subAgentId: agent.subAgentId
|
|
517
|
+
},
|
|
518
|
+
id: request.id
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
async function handleTasksResubscribe(c, agent, request) {
|
|
522
|
+
try {
|
|
523
|
+
const params = request.params;
|
|
524
|
+
if (!agent.agentCard.capabilities.streaming) return c.json({
|
|
525
|
+
jsonrpc: "2.0",
|
|
526
|
+
error: {
|
|
527
|
+
code: -32604,
|
|
528
|
+
message: "Agent does not support streaming for resubscription"
|
|
529
|
+
},
|
|
530
|
+
id: request.id
|
|
531
|
+
});
|
|
532
|
+
return streamSSE(c, async (stream$1) => {
|
|
533
|
+
try {
|
|
534
|
+
const task = {
|
|
535
|
+
id: params.taskId,
|
|
536
|
+
contextId: generateId(),
|
|
537
|
+
status: {
|
|
538
|
+
state: TaskState.Completed,
|
|
539
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
540
|
+
},
|
|
541
|
+
artifacts: [],
|
|
542
|
+
kind: "task"
|
|
543
|
+
};
|
|
544
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
545
|
+
jsonrpc: "2.0",
|
|
546
|
+
result: task,
|
|
547
|
+
id: request.id
|
|
548
|
+
}) });
|
|
549
|
+
} catch (error) {
|
|
550
|
+
console.error("Error in task resubscription:", error);
|
|
551
|
+
await stream$1.writeSSE({ data: JSON.stringify({
|
|
552
|
+
jsonrpc: "2.0",
|
|
553
|
+
error: {
|
|
554
|
+
code: -32603,
|
|
555
|
+
message: "Internal error during task resubscription",
|
|
556
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
557
|
+
},
|
|
558
|
+
id: request.id
|
|
559
|
+
}) });
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
} catch (error) {
|
|
563
|
+
return c.json({
|
|
564
|
+
jsonrpc: "2.0",
|
|
565
|
+
error: {
|
|
566
|
+
code: -32603,
|
|
567
|
+
message: "Internal error during task resubscription setup",
|
|
568
|
+
data: error instanceof Error ? error.message : "Unknown error"
|
|
569
|
+
},
|
|
570
|
+
id: request.id
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
//#endregion
|
|
576
|
+
export { a2aHandler };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { extractTransferData, isTransferTask } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/a2a/transfer.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Executes a transfer by sending the original message to the target agent
|
|
6
|
+
*/
|
|
7
|
+
declare function executeTransfer({
|
|
8
|
+
tenantId,
|
|
9
|
+
threadId,
|
|
10
|
+
projectId,
|
|
11
|
+
targetSubAgentId
|
|
12
|
+
}: {
|
|
13
|
+
tenantId: string;
|
|
14
|
+
threadId: string;
|
|
15
|
+
projectId: string;
|
|
16
|
+
targetSubAgentId: string;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
success: boolean;
|
|
19
|
+
targetSubAgentId: string;
|
|
20
|
+
}>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { executeTransfer, extractTransferData, isTransferTask as isTransferResponse };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { extractTransferData, isTransferTask } from "./types.js";
|
|
4
|
+
import { setActiveAgentForThread } from "@inkeep/agents-core";
|
|
5
|
+
|
|
6
|
+
//#region src/a2a/transfer.ts
|
|
7
|
+
const logger = getLogger("Transfer");
|
|
8
|
+
/**
|
|
9
|
+
* Executes a transfer by sending the original message to the target agent
|
|
10
|
+
*/
|
|
11
|
+
async function executeTransfer({ tenantId, threadId, projectId, targetSubAgentId }) {
|
|
12
|
+
logger.info({
|
|
13
|
+
targetAgent: targetSubAgentId,
|
|
14
|
+
threadId,
|
|
15
|
+
tenantId,
|
|
16
|
+
projectId
|
|
17
|
+
}, "Executing transfer - calling setActiveAgentForThread");
|
|
18
|
+
try {
|
|
19
|
+
await setActiveAgentForThread(dbClient_default)({
|
|
20
|
+
scopes: {
|
|
21
|
+
tenantId,
|
|
22
|
+
projectId
|
|
23
|
+
},
|
|
24
|
+
threadId,
|
|
25
|
+
subAgentId: targetSubAgentId
|
|
26
|
+
});
|
|
27
|
+
logger.info({
|
|
28
|
+
targetAgent: targetSubAgentId,
|
|
29
|
+
threadId
|
|
30
|
+
}, "Successfully updated active_sub_agent_id in database");
|
|
31
|
+
} catch (error) {
|
|
32
|
+
logger.error({
|
|
33
|
+
error,
|
|
34
|
+
targetAgent: targetSubAgentId,
|
|
35
|
+
threadId
|
|
36
|
+
}, "Failed to update active_sub_agent_id");
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
success: true,
|
|
41
|
+
targetSubAgentId
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
export { executeTransfer, extractTransferData, isTransferTask as isTransferResponse };
|