@inkeep/agents-run-api 0.19.6 → 0.19.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -2250,8 +2250,14 @@ var AgentSession = class {
|
|
|
2250
2250
|
return `Agent ${event.subAgentId} generating response`;
|
|
2251
2251
|
case "agent_reasoning":
|
|
2252
2252
|
return `Agent ${event.subAgentId} reasoning through request`;
|
|
2253
|
-
case "
|
|
2254
|
-
return `Tool
|
|
2253
|
+
case "tool_call":
|
|
2254
|
+
return `Tool call: ${event.data.toolName || "unknown"}`;
|
|
2255
|
+
case "tool_result": {
|
|
2256
|
+
const status = event.data.error ? "failed" : "completed";
|
|
2257
|
+
return `Tool result: ${event.data.toolName || "unknown"} (${status})`;
|
|
2258
|
+
}
|
|
2259
|
+
case "error":
|
|
2260
|
+
return `Error: ${event.data.message}`;
|
|
2255
2261
|
case "transfer":
|
|
2256
2262
|
return `Agent transfer: ${event.data.fromSubAgent} \u2192 ${event.data.targetSubAgent}`;
|
|
2257
2263
|
case "delegation_sent":
|
|
@@ -2972,15 +2978,29 @@ ${this.statusUpdateState?.config.prompt?.trim() || ""}`;
|
|
|
2972
2978
|
const activities = [];
|
|
2973
2979
|
for (const event of events) {
|
|
2974
2980
|
switch (event.eventType) {
|
|
2975
|
-
case "
|
|
2976
|
-
|
|
2981
|
+
case "tool_call": {
|
|
2982
|
+
activities.push(
|
|
2983
|
+
`\u{1F527} **${event.data.toolName}** (called)
|
|
2984
|
+
\u{1F4E5} Input: ${JSON.stringify(event.data.args)}`
|
|
2985
|
+
);
|
|
2986
|
+
break;
|
|
2987
|
+
}
|
|
2988
|
+
case "tool_result": {
|
|
2989
|
+
const resultStr = event.data.error ? `\u274C Error: ${event.data.error}` : JSON.stringify(event.data.result);
|
|
2977
2990
|
activities.push(
|
|
2978
2991
|
`\u{1F527} **${event.data.toolName}** ${event.data.duration ? `(${event.data.duration}ms)` : ""}
|
|
2979
|
-
\u{1F4E5} Input: ${JSON.stringify(event.data.args)}
|
|
2980
2992
|
\u{1F4E4} Output: ${resultStr}`
|
|
2981
2993
|
);
|
|
2982
2994
|
break;
|
|
2983
2995
|
}
|
|
2996
|
+
case "error": {
|
|
2997
|
+
activities.push(
|
|
2998
|
+
`\u274C **Error**: ${event.data.message}
|
|
2999
|
+
\u{1F50D} Code: ${event.data.code || "unknown"}
|
|
3000
|
+
\u{1F4CA} Severity: ${event.data.severity || "error"}`
|
|
3001
|
+
);
|
|
3002
|
+
break;
|
|
3003
|
+
}
|
|
2984
3004
|
// INTERNAL OPERATIONS - DO NOT EXPOSE TO STATUS UPDATES
|
|
2985
3005
|
case "transfer":
|
|
2986
3006
|
case "delegation_sent":
|
|
@@ -3065,7 +3085,7 @@ ${this.statusUpdateState?.config.prompt?.trim() || ""}`;
|
|
|
3065
3085
|
}
|
|
3066
3086
|
});
|
|
3067
3087
|
const toolCallEvent = this.events.find(
|
|
3068
|
-
(event) => event.eventType === "
|
|
3088
|
+
(event) => event.eventType === "tool_result" && event.data && "toolCallId" in event.data && event.data.toolCallId === artifactData.metadata?.toolCallId
|
|
3069
3089
|
);
|
|
3070
3090
|
const toolContext = toolCallEvent ? {
|
|
3071
3091
|
toolName: toolCallEvent.data.toolName,
|
|
@@ -6663,14 +6683,22 @@ var Agent = class {
|
|
|
6663
6683
|
});
|
|
6664
6684
|
}
|
|
6665
6685
|
const isInternalTool = toolName.includes("save_tool_result") || toolName.includes("thinking_complete") || toolName.startsWith("transfer_to_") || toolName.startsWith("delegate_to_");
|
|
6686
|
+
if (streamRequestId && !isInternalTool) {
|
|
6687
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_call", this.config.id, {
|
|
6688
|
+
toolName,
|
|
6689
|
+
args,
|
|
6690
|
+
toolCallId: context?.toolCallId,
|
|
6691
|
+
toolId
|
|
6692
|
+
});
|
|
6693
|
+
}
|
|
6666
6694
|
try {
|
|
6667
6695
|
const result = await originalExecute(args, context);
|
|
6668
6696
|
const duration = Date.now() - startTime;
|
|
6669
6697
|
if (streamRequestId && !isInternalTool) {
|
|
6670
|
-
agentSessionManager.recordEvent(streamRequestId, "
|
|
6698
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_result", this.config.id, {
|
|
6671
6699
|
toolName,
|
|
6672
|
-
args,
|
|
6673
6700
|
result,
|
|
6701
|
+
toolCallId: context?.toolCallId,
|
|
6674
6702
|
toolId,
|
|
6675
6703
|
duration
|
|
6676
6704
|
});
|
|
@@ -6680,12 +6708,13 @@ var Agent = class {
|
|
|
6680
6708
|
const duration = Date.now() - startTime;
|
|
6681
6709
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
6682
6710
|
if (streamRequestId && !isInternalTool) {
|
|
6683
|
-
agentSessionManager.recordEvent(streamRequestId, "
|
|
6711
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_result", this.config.id, {
|
|
6684
6712
|
toolName,
|
|
6685
|
-
|
|
6686
|
-
|
|
6713
|
+
result: null,
|
|
6714
|
+
toolCallId: context?.toolCallId,
|
|
6687
6715
|
toolId,
|
|
6688
|
-
duration
|
|
6716
|
+
duration,
|
|
6717
|
+
error: errorMessage
|
|
6689
6718
|
});
|
|
6690
6719
|
}
|
|
6691
6720
|
throw error;
|
|
@@ -6954,13 +6983,14 @@ var Agent = class {
|
|
|
6954
6983
|
},
|
|
6955
6984
|
(span) => {
|
|
6956
6985
|
setSpanWithError(span, new Error(`0 effective tools available for ${tool3.name}`));
|
|
6957
|
-
agentSessionManager.recordEvent(streamRequestId, "
|
|
6958
|
-
|
|
6959
|
-
|
|
6960
|
-
|
|
6961
|
-
|
|
6962
|
-
|
|
6963
|
-
serverUrl: tool3.config.type === "mcp" ? tool3.config.mcp.server.url : "unknown"
|
|
6986
|
+
agentSessionManager.recordEvent(streamRequestId, "error", this.config.id, {
|
|
6987
|
+
message: `MCP server has 0 effective tools. Double check the selected tools in your graph and the active tools in the MCP server configuration.`,
|
|
6988
|
+
code: "no_tools_available",
|
|
6989
|
+
severity: "error",
|
|
6990
|
+
context: {
|
|
6991
|
+
toolName: tool3.name,
|
|
6992
|
+
serverUrl: tool3.config.type === "mcp" ? tool3.config.mcp.server.url : "unknown",
|
|
6993
|
+
operation: "mcp_tool_discovery"
|
|
6964
6994
|
}
|
|
6965
6995
|
});
|
|
6966
6996
|
span.end();
|
|
@@ -7016,7 +7046,7 @@ var Agent = class {
|
|
|
7016
7046
|
if (functionToolsData.length === 0) {
|
|
7017
7047
|
return functionTools;
|
|
7018
7048
|
}
|
|
7019
|
-
const { LocalSandboxExecutor } = await import('./LocalSandboxExecutor-
|
|
7049
|
+
const { LocalSandboxExecutor } = await import('./LocalSandboxExecutor-S6BPJMJV.js');
|
|
7020
7050
|
const sandboxExecutor = LocalSandboxExecutor.getInstance();
|
|
7021
7051
|
for (const functionToolDef of functionToolsData) {
|
|
7022
7052
|
const functionId = functionToolDef.functionId;
|
|
@@ -7785,7 +7815,9 @@ var Agent = class {
|
|
|
7785
7815
|
if (steps.length >= 2) {
|
|
7786
7816
|
const previousStep = steps[steps.length - 2];
|
|
7787
7817
|
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
7788
|
-
const hasTransferCall = previousStep.toolCalls.some(
|
|
7818
|
+
const hasTransferCall = previousStep.toolCalls.some(
|
|
7819
|
+
(tc) => tc.toolName.startsWith("transfer_to_")
|
|
7820
|
+
);
|
|
7789
7821
|
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
7790
7822
|
return true;
|
|
7791
7823
|
}
|
|
@@ -10084,174 +10116,191 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
10084
10116
|
);
|
|
10085
10117
|
const body = c.get("requestBody") || {};
|
|
10086
10118
|
const conversationId = body.conversationId || getConversationId();
|
|
10087
|
-
const
|
|
10088
|
-
|
|
10089
|
-
|
|
10090
|
-
|
|
10091
|
-
|
|
10092
|
-
|
|
10093
|
-
|
|
10094
|
-
|
|
10095
|
-
|
|
10096
|
-
|
|
10097
|
-
|
|
10098
|
-
|
|
10099
|
-
|
|
10100
|
-
|
|
10101
|
-
|
|
10102
|
-
|
|
10103
|
-
|
|
10104
|
-
agent = await getAgentWithDefaultSubAgent(dbClient_default)({
|
|
10119
|
+
const activeSpan = trace.getActiveSpan();
|
|
10120
|
+
if (activeSpan) {
|
|
10121
|
+
activeSpan.setAttributes({
|
|
10122
|
+
"conversation.id": conversationId,
|
|
10123
|
+
"tenant.id": tenantId,
|
|
10124
|
+
"agent.id": agentId,
|
|
10125
|
+
"project.id": projectId
|
|
10126
|
+
});
|
|
10127
|
+
}
|
|
10128
|
+
let currentBag = propagation.getBaggage(context.active());
|
|
10129
|
+
if (!currentBag) {
|
|
10130
|
+
currentBag = propagation.createBaggage();
|
|
10131
|
+
}
|
|
10132
|
+
currentBag = currentBag.setEntry("conversation.id", { value: conversationId });
|
|
10133
|
+
const ctxWithBaggage = propagation.setBaggage(context.active(), currentBag);
|
|
10134
|
+
return await context.with(ctxWithBaggage, async () => {
|
|
10135
|
+
const fullAgent = await getFullAgent(dbClient_default)({
|
|
10105
10136
|
scopes: { tenantId, projectId, agentId }
|
|
10106
10137
|
});
|
|
10107
|
-
|
|
10138
|
+
let agent;
|
|
10139
|
+
let defaultSubAgentId;
|
|
10140
|
+
if (fullAgent) {
|
|
10141
|
+
agent = {
|
|
10142
|
+
id: fullAgent.id,
|
|
10143
|
+
name: fullAgent.name,
|
|
10144
|
+
tenantId,
|
|
10145
|
+
projectId,
|
|
10146
|
+
defaultSubAgentId: fullAgent.defaultSubAgentId
|
|
10147
|
+
};
|
|
10148
|
+
const agentKeys = Object.keys(fullAgent.subAgents || {});
|
|
10149
|
+
const firstAgentId = agentKeys.length > 0 ? agentKeys[0] : "";
|
|
10150
|
+
defaultSubAgentId = fullAgent.defaultSubAgentId || firstAgentId;
|
|
10151
|
+
} else {
|
|
10152
|
+
agent = await getAgentWithDefaultSubAgent(dbClient_default)({
|
|
10153
|
+
scopes: { tenantId, projectId, agentId }
|
|
10154
|
+
});
|
|
10155
|
+
if (!agent) {
|
|
10156
|
+
throw createApiError({
|
|
10157
|
+
code: "not_found",
|
|
10158
|
+
message: "Agent not found"
|
|
10159
|
+
});
|
|
10160
|
+
}
|
|
10161
|
+
defaultSubAgentId = agent.defaultSubAgentId || "";
|
|
10162
|
+
}
|
|
10163
|
+
if (!defaultSubAgentId) {
|
|
10108
10164
|
throw createApiError({
|
|
10109
10165
|
code: "not_found",
|
|
10110
|
-
message: "
|
|
10166
|
+
message: "No default agent found in agent"
|
|
10111
10167
|
});
|
|
10112
10168
|
}
|
|
10113
|
-
|
|
10114
|
-
|
|
10115
|
-
|
|
10116
|
-
|
|
10117
|
-
|
|
10118
|
-
message: "No default agent found in agent"
|
|
10169
|
+
await createOrGetConversation(dbClient_default)({
|
|
10170
|
+
tenantId,
|
|
10171
|
+
projectId,
|
|
10172
|
+
id: conversationId,
|
|
10173
|
+
activeSubAgentId: defaultSubAgentId
|
|
10119
10174
|
});
|
|
10120
|
-
|
|
10121
|
-
await createOrGetConversation(dbClient_default)({
|
|
10122
|
-
tenantId,
|
|
10123
|
-
projectId,
|
|
10124
|
-
id: conversationId,
|
|
10125
|
-
activeSubAgentId: defaultSubAgentId
|
|
10126
|
-
});
|
|
10127
|
-
const activeAgent = await getActiveAgentForConversation(dbClient_default)({
|
|
10128
|
-
scopes: { tenantId, projectId },
|
|
10129
|
-
conversationId
|
|
10130
|
-
});
|
|
10131
|
-
if (!activeAgent) {
|
|
10132
|
-
setActiveAgentForConversation(dbClient_default)({
|
|
10175
|
+
const activeAgent = await getActiveAgentForConversation(dbClient_default)({
|
|
10133
10176
|
scopes: { tenantId, projectId },
|
|
10134
|
-
conversationId
|
|
10135
|
-
subAgentId: defaultSubAgentId
|
|
10177
|
+
conversationId
|
|
10136
10178
|
});
|
|
10137
|
-
|
|
10138
|
-
|
|
10139
|
-
|
|
10140
|
-
|
|
10141
|
-
|
|
10142
|
-
|
|
10143
|
-
|
|
10144
|
-
|
|
10145
|
-
|
|
10146
|
-
|
|
10179
|
+
if (!activeAgent) {
|
|
10180
|
+
setActiveAgentForConversation(dbClient_default)({
|
|
10181
|
+
scopes: { tenantId, projectId },
|
|
10182
|
+
conversationId,
|
|
10183
|
+
subAgentId: defaultSubAgentId
|
|
10184
|
+
});
|
|
10185
|
+
}
|
|
10186
|
+
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
10187
|
+
const agentInfo = await getSubAgentById(dbClient_default)({
|
|
10188
|
+
scopes: { tenantId, projectId, agentId },
|
|
10189
|
+
subAgentId
|
|
10147
10190
|
});
|
|
10148
|
-
|
|
10149
|
-
|
|
10150
|
-
|
|
10151
|
-
|
|
10152
|
-
|
|
10153
|
-
|
|
10154
|
-
|
|
10155
|
-
|
|
10156
|
-
|
|
10157
|
-
dbClient: dbClient_default,
|
|
10158
|
-
credentialStores
|
|
10159
|
-
});
|
|
10160
|
-
logger21.info(
|
|
10161
|
-
{
|
|
10191
|
+
if (!agentInfo) {
|
|
10192
|
+
throw createApiError({
|
|
10193
|
+
code: "not_found",
|
|
10194
|
+
message: "Agent not found"
|
|
10195
|
+
});
|
|
10196
|
+
}
|
|
10197
|
+
const validatedContext = c.get("validatedContext") || body.headers || {};
|
|
10198
|
+
const credentialStores = c.get("credentialStores");
|
|
10199
|
+
await handleContextResolution({
|
|
10162
10200
|
tenantId,
|
|
10163
10201
|
projectId,
|
|
10164
10202
|
agentId,
|
|
10165
10203
|
conversationId,
|
|
10166
|
-
|
|
10167
|
-
|
|
10168
|
-
|
|
10169
|
-
hasHeaders: !!body.headers,
|
|
10170
|
-
hasValidatedContext: !!validatedContext,
|
|
10171
|
-
validatedContextKeys: Object.keys(validatedContext)
|
|
10172
|
-
},
|
|
10173
|
-
"parameters"
|
|
10174
|
-
);
|
|
10175
|
-
const requestId2 = `chatcmpl-${Date.now()}`;
|
|
10176
|
-
const timestamp = Math.floor(Date.now() / 1e3);
|
|
10177
|
-
const lastUserMessage = body.messages.filter((msg) => msg.role === "user").slice(-1)[0];
|
|
10178
|
-
const userMessage = lastUserMessage ? getMessageText(lastUserMessage.content) : "";
|
|
10179
|
-
const messageSpan = trace.getActiveSpan();
|
|
10180
|
-
if (messageSpan) {
|
|
10181
|
-
messageSpan.setAttributes({
|
|
10182
|
-
"message.content": userMessage,
|
|
10183
|
-
"message.timestamp": Date.now()
|
|
10184
|
-
});
|
|
10185
|
-
}
|
|
10186
|
-
await createMessage(dbClient_default)({
|
|
10187
|
-
id: nanoid(),
|
|
10188
|
-
tenantId,
|
|
10189
|
-
projectId,
|
|
10190
|
-
conversationId,
|
|
10191
|
-
role: "user",
|
|
10192
|
-
content: {
|
|
10193
|
-
text: userMessage
|
|
10194
|
-
},
|
|
10195
|
-
visibility: "user-facing",
|
|
10196
|
-
messageType: "chat"
|
|
10197
|
-
});
|
|
10198
|
-
if (messageSpan) {
|
|
10199
|
-
messageSpan.addEvent("user.message.stored", {
|
|
10200
|
-
"message.id": conversationId,
|
|
10201
|
-
"database.operation": "insert"
|
|
10204
|
+
headers: validatedContext,
|
|
10205
|
+
dbClient: dbClient_default,
|
|
10206
|
+
credentialStores
|
|
10202
10207
|
});
|
|
10203
|
-
|
|
10204
|
-
|
|
10205
|
-
|
|
10206
|
-
|
|
10207
|
-
|
|
10208
|
-
logger21.info({ subAgentId }, "Starting execution");
|
|
10209
|
-
const emitOperationsHeader = c.req.header("x-emit-operations");
|
|
10210
|
-
const emitOperations = emitOperationsHeader === "true";
|
|
10211
|
-
const executionHandler = new ExecutionHandler();
|
|
10212
|
-
const result = await executionHandler.execute({
|
|
10213
|
-
executionContext,
|
|
10208
|
+
logger21.info(
|
|
10209
|
+
{
|
|
10210
|
+
tenantId,
|
|
10211
|
+
projectId,
|
|
10212
|
+
agentId,
|
|
10214
10213
|
conversationId,
|
|
10215
|
-
|
|
10216
|
-
|
|
10217
|
-
|
|
10218
|
-
|
|
10219
|
-
|
|
10214
|
+
defaultSubAgentId,
|
|
10215
|
+
activeSubAgentId: activeAgent?.activeSubAgentId || "none",
|
|
10216
|
+
hasContextConfig: !!agent.contextConfigId,
|
|
10217
|
+
hasHeaders: !!body.headers,
|
|
10218
|
+
hasValidatedContext: !!validatedContext,
|
|
10219
|
+
validatedContextKeys: Object.keys(validatedContext)
|
|
10220
|
+
},
|
|
10221
|
+
"parameters"
|
|
10222
|
+
);
|
|
10223
|
+
const requestId2 = `chatcmpl-${Date.now()}`;
|
|
10224
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
10225
|
+
const lastUserMessage = body.messages.filter((msg) => msg.role === "user").slice(-1)[0];
|
|
10226
|
+
const userMessage = lastUserMessage ? getMessageText(lastUserMessage.content) : "";
|
|
10227
|
+
const messageSpan = trace.getActiveSpan();
|
|
10228
|
+
if (messageSpan) {
|
|
10229
|
+
messageSpan.setAttributes({
|
|
10230
|
+
"message.content": userMessage,
|
|
10231
|
+
"message.timestamp": Date.now()
|
|
10220
10232
|
});
|
|
10221
|
-
|
|
10222
|
-
|
|
10223
|
-
|
|
10224
|
-
|
|
10225
|
-
|
|
10226
|
-
|
|
10227
|
-
|
|
10228
|
-
|
|
10229
|
-
|
|
10230
|
-
|
|
10231
|
-
|
|
10232
|
-
|
|
10233
|
-
|
|
10234
|
-
|
|
10235
|
-
|
|
10236
|
-
|
|
10237
|
-
|
|
10238
|
-
|
|
10239
|
-
|
|
10240
|
-
|
|
10241
|
-
);
|
|
10233
|
+
}
|
|
10234
|
+
await createMessage(dbClient_default)({
|
|
10235
|
+
id: nanoid(),
|
|
10236
|
+
tenantId,
|
|
10237
|
+
projectId,
|
|
10238
|
+
conversationId,
|
|
10239
|
+
role: "user",
|
|
10240
|
+
content: {
|
|
10241
|
+
text: userMessage
|
|
10242
|
+
},
|
|
10243
|
+
visibility: "user-facing",
|
|
10244
|
+
messageType: "chat"
|
|
10245
|
+
});
|
|
10246
|
+
if (messageSpan) {
|
|
10247
|
+
messageSpan.addEvent("user.message.stored", {
|
|
10248
|
+
"message.id": conversationId,
|
|
10249
|
+
"database.operation": "insert"
|
|
10250
|
+
});
|
|
10251
|
+
}
|
|
10252
|
+
return streamSSE(c, async (stream2) => {
|
|
10242
10253
|
try {
|
|
10243
10254
|
const sseHelper = createSSEStreamHelper(stream2, requestId2, timestamp);
|
|
10244
|
-
await sseHelper.
|
|
10245
|
-
|
|
10246
|
-
|
|
10247
|
-
|
|
10248
|
-
|
|
10255
|
+
await sseHelper.writeRole();
|
|
10256
|
+
logger21.info({ subAgentId }, "Starting execution");
|
|
10257
|
+
const emitOperationsHeader = c.req.header("x-emit-operations");
|
|
10258
|
+
const emitOperations = emitOperationsHeader === "true";
|
|
10259
|
+
const executionHandler = new ExecutionHandler();
|
|
10260
|
+
const result = await executionHandler.execute({
|
|
10261
|
+
executionContext,
|
|
10262
|
+
conversationId,
|
|
10263
|
+
userMessage,
|
|
10264
|
+
initialAgentId: subAgentId,
|
|
10265
|
+
requestId: requestId2,
|
|
10266
|
+
sseHelper,
|
|
10267
|
+
emitOperations
|
|
10268
|
+
});
|
|
10269
|
+
logger21.info(
|
|
10270
|
+
{ result },
|
|
10271
|
+
`Execution completed: ${result.success ? "success" : "failed"} after ${result.iterations} iterations`
|
|
10249
10272
|
);
|
|
10273
|
+
if (!result.success) {
|
|
10274
|
+
await sseHelper.writeOperation(
|
|
10275
|
+
errorOp(
|
|
10276
|
+
"Sorry, I was unable to process your request at this time. Please try again.",
|
|
10277
|
+
"system"
|
|
10278
|
+
)
|
|
10279
|
+
);
|
|
10280
|
+
}
|
|
10250
10281
|
await sseHelper.complete();
|
|
10251
|
-
} catch (
|
|
10252
|
-
logger21.error(
|
|
10282
|
+
} catch (error) {
|
|
10283
|
+
logger21.error(
|
|
10284
|
+
{
|
|
10285
|
+
error: error instanceof Error ? error.message : error,
|
|
10286
|
+
stack: error instanceof Error ? error.stack : void 0
|
|
10287
|
+
},
|
|
10288
|
+
"Error during streaming execution"
|
|
10289
|
+
);
|
|
10290
|
+
try {
|
|
10291
|
+
const sseHelper = createSSEStreamHelper(stream2, requestId2, timestamp);
|
|
10292
|
+
await sseHelper.writeOperation(
|
|
10293
|
+
errorOp(
|
|
10294
|
+
"Sorry, I was unable to process your request at this time. Please try again.",
|
|
10295
|
+
"system"
|
|
10296
|
+
)
|
|
10297
|
+
);
|
|
10298
|
+
await sseHelper.complete();
|
|
10299
|
+
} catch (streamError) {
|
|
10300
|
+
logger21.error({ streamError }, "Failed to write error to stream");
|
|
10301
|
+
}
|
|
10253
10302
|
}
|
|
10254
|
-
}
|
|
10303
|
+
});
|
|
10255
10304
|
});
|
|
10256
10305
|
} catch (error) {
|
|
10257
10306
|
logger21.error(
|
|
@@ -10343,123 +10392,131 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
10343
10392
|
"project.id": projectId
|
|
10344
10393
|
});
|
|
10345
10394
|
}
|
|
10346
|
-
|
|
10347
|
-
|
|
10348
|
-
|
|
10349
|
-
if (!agent) {
|
|
10350
|
-
throw createApiError({
|
|
10351
|
-
code: "not_found",
|
|
10352
|
-
message: "Agent not found"
|
|
10353
|
-
});
|
|
10395
|
+
let currentBag = propagation.getBaggage(context.active());
|
|
10396
|
+
if (!currentBag) {
|
|
10397
|
+
currentBag = propagation.createBaggage();
|
|
10354
10398
|
}
|
|
10355
|
-
|
|
10356
|
-
const
|
|
10357
|
-
|
|
10358
|
-
|
|
10359
|
-
|
|
10360
|
-
message: "Agent does not have a default agent configured"
|
|
10399
|
+
currentBag = currentBag.setEntry("conversation.id", { value: conversationId });
|
|
10400
|
+
const ctxWithBaggage = propagation.setBaggage(context.active(), currentBag);
|
|
10401
|
+
return await context.with(ctxWithBaggage, async () => {
|
|
10402
|
+
const agent = await getAgentWithDefaultSubAgent(dbClient_default)({
|
|
10403
|
+
scopes: { tenantId, projectId, agentId }
|
|
10361
10404
|
});
|
|
10362
|
-
|
|
10363
|
-
|
|
10364
|
-
|
|
10365
|
-
|
|
10366
|
-
|
|
10367
|
-
|
|
10368
|
-
|
|
10405
|
+
if (!agent) {
|
|
10406
|
+
throw createApiError({
|
|
10407
|
+
code: "not_found",
|
|
10408
|
+
message: "Agent not found"
|
|
10409
|
+
});
|
|
10410
|
+
}
|
|
10411
|
+
const defaultSubAgentId = agent.defaultSubAgentId;
|
|
10412
|
+
const agentName = agent.name;
|
|
10413
|
+
if (!defaultSubAgentId) {
|
|
10414
|
+
throw createApiError({
|
|
10415
|
+
code: "bad_request",
|
|
10416
|
+
message: "Agent does not have a default agent configured"
|
|
10417
|
+
});
|
|
10418
|
+
}
|
|
10419
|
+
const activeAgent = await getActiveAgentForConversation(dbClient_default)({
|
|
10369
10420
|
scopes: { tenantId, projectId },
|
|
10370
|
-
conversationId
|
|
10371
|
-
subAgentId: defaultSubAgentId
|
|
10421
|
+
conversationId
|
|
10372
10422
|
});
|
|
10373
|
-
|
|
10374
|
-
|
|
10375
|
-
|
|
10376
|
-
|
|
10377
|
-
|
|
10378
|
-
|
|
10379
|
-
|
|
10380
|
-
|
|
10381
|
-
|
|
10382
|
-
|
|
10423
|
+
if (!activeAgent) {
|
|
10424
|
+
setActiveAgentForConversation(dbClient_default)({
|
|
10425
|
+
scopes: { tenantId, projectId },
|
|
10426
|
+
conversationId,
|
|
10427
|
+
subAgentId: defaultSubAgentId
|
|
10428
|
+
});
|
|
10429
|
+
}
|
|
10430
|
+
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
10431
|
+
const agentInfo = await getSubAgentById(dbClient_default)({
|
|
10432
|
+
scopes: { tenantId, projectId, agentId },
|
|
10433
|
+
subAgentId
|
|
10383
10434
|
});
|
|
10384
|
-
|
|
10385
|
-
|
|
10386
|
-
|
|
10387
|
-
|
|
10388
|
-
|
|
10389
|
-
|
|
10390
|
-
|
|
10391
|
-
|
|
10392
|
-
|
|
10393
|
-
|
|
10394
|
-
|
|
10395
|
-
|
|
10396
|
-
|
|
10397
|
-
|
|
10398
|
-
|
|
10399
|
-
|
|
10400
|
-
if (messageSpan) {
|
|
10401
|
-
messageSpan.setAttributes({
|
|
10402
|
-
"message.timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
10403
|
-
"message.content": userText,
|
|
10404
|
-
"agent.name": agentName
|
|
10435
|
+
if (!agentInfo) {
|
|
10436
|
+
throw createApiError({
|
|
10437
|
+
code: "not_found",
|
|
10438
|
+
message: "Agent not found"
|
|
10439
|
+
});
|
|
10440
|
+
}
|
|
10441
|
+
const validatedContext = c.get("validatedContext") || body.headers || {};
|
|
10442
|
+
const credentialStores = c.get("credentialStores");
|
|
10443
|
+
await handleContextResolution({
|
|
10444
|
+
tenantId,
|
|
10445
|
+
projectId,
|
|
10446
|
+
agentId,
|
|
10447
|
+
conversationId,
|
|
10448
|
+
headers: validatedContext,
|
|
10449
|
+
dbClient: dbClient_default,
|
|
10450
|
+
credentialStores
|
|
10405
10451
|
});
|
|
10406
|
-
|
|
10407
|
-
|
|
10408
|
-
|
|
10409
|
-
|
|
10410
|
-
|
|
10411
|
-
|
|
10412
|
-
|
|
10413
|
-
|
|
10414
|
-
|
|
10415
|
-
|
|
10416
|
-
|
|
10417
|
-
|
|
10418
|
-
|
|
10419
|
-
|
|
10420
|
-
|
|
10452
|
+
const lastUserMessage = body.messages.filter((m) => m.role === "user").slice(-1)[0];
|
|
10453
|
+
const userText = typeof lastUserMessage?.content === "string" ? lastUserMessage.content : lastUserMessage?.parts?.map((p) => p.text).join("") || "";
|
|
10454
|
+
logger22.info({ userText, lastUserMessage }, "userText");
|
|
10455
|
+
const messageSpan = trace.getActiveSpan();
|
|
10456
|
+
if (messageSpan) {
|
|
10457
|
+
messageSpan.setAttributes({
|
|
10458
|
+
"message.timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
10459
|
+
"message.content": userText,
|
|
10460
|
+
"agent.name": agentName
|
|
10461
|
+
});
|
|
10462
|
+
}
|
|
10463
|
+
await createMessage(dbClient_default)({
|
|
10464
|
+
id: nanoid(),
|
|
10465
|
+
tenantId,
|
|
10466
|
+
projectId,
|
|
10467
|
+
conversationId,
|
|
10468
|
+
role: "user",
|
|
10469
|
+
content: { text: userText },
|
|
10470
|
+
visibility: "user-facing",
|
|
10471
|
+
messageType: "chat"
|
|
10421
10472
|
});
|
|
10422
|
-
|
|
10423
|
-
|
|
10424
|
-
|
|
10425
|
-
|
|
10426
|
-
|
|
10427
|
-
|
|
10428
|
-
|
|
10429
|
-
|
|
10430
|
-
const
|
|
10431
|
-
|
|
10432
|
-
|
|
10433
|
-
|
|
10434
|
-
|
|
10435
|
-
|
|
10436
|
-
|
|
10437
|
-
|
|
10438
|
-
|
|
10439
|
-
|
|
10440
|
-
|
|
10441
|
-
|
|
10442
|
-
|
|
10443
|
-
|
|
10444
|
-
|
|
10445
|
-
|
|
10446
|
-
|
|
10447
|
-
|
|
10473
|
+
if (messageSpan) {
|
|
10474
|
+
messageSpan.addEvent("user.message.stored", {
|
|
10475
|
+
"message.id": conversationId,
|
|
10476
|
+
"database.operation": "insert"
|
|
10477
|
+
});
|
|
10478
|
+
}
|
|
10479
|
+
const dataStream = createUIMessageStream({
|
|
10480
|
+
execute: async ({ writer }) => {
|
|
10481
|
+
const streamHelper = createVercelStreamHelper(writer);
|
|
10482
|
+
try {
|
|
10483
|
+
const emitOperationsHeader = c.req.header("x-emit-operations");
|
|
10484
|
+
const emitOperations = emitOperationsHeader === "true";
|
|
10485
|
+
const executionHandler = new ExecutionHandler();
|
|
10486
|
+
const result = await executionHandler.execute({
|
|
10487
|
+
executionContext,
|
|
10488
|
+
conversationId,
|
|
10489
|
+
userMessage: userText,
|
|
10490
|
+
initialAgentId: subAgentId,
|
|
10491
|
+
requestId: `chatds-${Date.now()}`,
|
|
10492
|
+
sseHelper: streamHelper,
|
|
10493
|
+
emitOperations
|
|
10494
|
+
});
|
|
10495
|
+
if (!result.success) {
|
|
10496
|
+
await streamHelper.writeOperation(errorOp("Unable to process request", "system"));
|
|
10497
|
+
}
|
|
10498
|
+
} catch (err) {
|
|
10499
|
+
logger22.error({ err }, "Streaming error");
|
|
10500
|
+
await streamHelper.writeOperation(errorOp("Internal server error", "system"));
|
|
10501
|
+
} finally {
|
|
10502
|
+
if ("cleanup" in streamHelper && typeof streamHelper.cleanup === "function") {
|
|
10503
|
+
streamHelper.cleanup();
|
|
10504
|
+
}
|
|
10448
10505
|
}
|
|
10449
10506
|
}
|
|
10450
|
-
}
|
|
10507
|
+
});
|
|
10508
|
+
c.header("content-type", "text/event-stream");
|
|
10509
|
+
c.header("cache-control", "no-cache");
|
|
10510
|
+
c.header("connection", "keep-alive");
|
|
10511
|
+
c.header("x-vercel-ai-data-stream", "v2");
|
|
10512
|
+
c.header("x-accel-buffering", "no");
|
|
10513
|
+
return stream(
|
|
10514
|
+
c,
|
|
10515
|
+
(stream2) => stream2.pipe(
|
|
10516
|
+
dataStream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())
|
|
10517
|
+
)
|
|
10518
|
+
);
|
|
10451
10519
|
});
|
|
10452
|
-
c.header("content-type", "text/event-stream");
|
|
10453
|
-
c.header("cache-control", "no-cache");
|
|
10454
|
-
c.header("connection", "keep-alive");
|
|
10455
|
-
c.header("x-vercel-ai-data-stream", "v2");
|
|
10456
|
-
c.header("x-accel-buffering", "no");
|
|
10457
|
-
return stream(
|
|
10458
|
-
c,
|
|
10459
|
-
(stream2) => stream2.pipe(
|
|
10460
|
-
dataStream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())
|
|
10461
|
-
)
|
|
10462
|
-
);
|
|
10463
10520
|
} catch (error) {
|
|
10464
10521
|
logger22.error({ error }, "chatDataStream error");
|
|
10465
10522
|
throw createApiError({
|
|
@@ -10787,66 +10844,83 @@ var handleInitializationRequest = async (body, executionContext, validatedContex
|
|
|
10787
10844
|
const { tenantId, projectId, agentId } = executionContext;
|
|
10788
10845
|
logger23.info({ body }, "Received initialization request");
|
|
10789
10846
|
const sessionId = getConversationId();
|
|
10790
|
-
const
|
|
10791
|
-
|
|
10792
|
-
|
|
10793
|
-
|
|
10794
|
-
|
|
10795
|
-
|
|
10796
|
-
|
|
10797
|
-
|
|
10798
|
-
id: body.id || null
|
|
10799
|
-
},
|
|
10800
|
-
{ status: 404 }
|
|
10801
|
-
);
|
|
10847
|
+
const activeSpan = trace.getActiveSpan();
|
|
10848
|
+
if (activeSpan) {
|
|
10849
|
+
activeSpan.setAttributes({
|
|
10850
|
+
"conversation.id": sessionId,
|
|
10851
|
+
"tenant.id": tenantId,
|
|
10852
|
+
"agent.id": agentId,
|
|
10853
|
+
"project.id": projectId
|
|
10854
|
+
});
|
|
10802
10855
|
}
|
|
10803
|
-
|
|
10804
|
-
|
|
10856
|
+
let currentBag = propagation.getBaggage(context.active());
|
|
10857
|
+
if (!currentBag) {
|
|
10858
|
+
currentBag = propagation.createBaggage();
|
|
10859
|
+
}
|
|
10860
|
+
currentBag = currentBag.setEntry("conversation.id", { value: sessionId });
|
|
10861
|
+
const ctxWithBaggage = propagation.setBaggage(context.active(), currentBag);
|
|
10862
|
+
return await context.with(ctxWithBaggage, async () => {
|
|
10863
|
+
const agent = await getAgentWithDefaultSubAgent(dbClient_default)({
|
|
10864
|
+
scopes: { tenantId, projectId, agentId }
|
|
10865
|
+
});
|
|
10866
|
+
if (!agent) {
|
|
10867
|
+
return c.json(
|
|
10868
|
+
{
|
|
10869
|
+
jsonrpc: "2.0",
|
|
10870
|
+
error: { code: -32001, message: "Agent not found" },
|
|
10871
|
+
id: body.id || null
|
|
10872
|
+
},
|
|
10873
|
+
{ status: 404 }
|
|
10874
|
+
);
|
|
10875
|
+
}
|
|
10876
|
+
if (!agent.defaultSubAgentId) {
|
|
10877
|
+
return c.json(
|
|
10878
|
+
{
|
|
10879
|
+
jsonrpc: "2.0",
|
|
10880
|
+
error: { code: -32001, message: "Agent does not have a default agent configured" },
|
|
10881
|
+
id: body.id || null
|
|
10882
|
+
},
|
|
10883
|
+
{ status: 400 }
|
|
10884
|
+
);
|
|
10885
|
+
}
|
|
10886
|
+
const conversation = await createOrGetConversation(dbClient_default)({
|
|
10887
|
+
id: sessionId,
|
|
10888
|
+
tenantId,
|
|
10889
|
+
projectId,
|
|
10890
|
+
activeSubAgentId: agent.defaultSubAgentId,
|
|
10891
|
+
metadata: {
|
|
10892
|
+
sessionData: {
|
|
10893
|
+
agentId,
|
|
10894
|
+
sessionType: "mcp",
|
|
10895
|
+
mcpProtocolVersion: c.req.header("mcp-protocol-version"),
|
|
10896
|
+
initialized: false
|
|
10897
|
+
// Track initialization state
|
|
10898
|
+
}
|
|
10899
|
+
}
|
|
10900
|
+
});
|
|
10901
|
+
logger23.info(
|
|
10902
|
+
{ sessionId, conversationId: conversation.id },
|
|
10903
|
+
"Created MCP session as conversation"
|
|
10904
|
+
);
|
|
10905
|
+
const transport = new StreamableHTTPServerTransport({
|
|
10906
|
+
sessionIdGenerator: () => sessionId
|
|
10907
|
+
});
|
|
10908
|
+
const server = await getServer(validatedContext, executionContext, sessionId, credentialStores);
|
|
10909
|
+
await server.connect(transport);
|
|
10910
|
+
logger23.info({ sessionId }, "Server connected for initialization");
|
|
10911
|
+
res.setHeader("Mcp-Session-Id", sessionId);
|
|
10912
|
+
logger23.info(
|
|
10805
10913
|
{
|
|
10806
|
-
|
|
10807
|
-
|
|
10808
|
-
|
|
10914
|
+
sessionId,
|
|
10915
|
+
bodyMethod: body?.method,
|
|
10916
|
+
bodyId: body?.id
|
|
10809
10917
|
},
|
|
10810
|
-
|
|
10918
|
+
"About to handle initialization request"
|
|
10811
10919
|
);
|
|
10812
|
-
|
|
10813
|
-
|
|
10814
|
-
|
|
10815
|
-
tenantId,
|
|
10816
|
-
projectId,
|
|
10817
|
-
activeSubAgentId: agent.defaultSubAgentId,
|
|
10818
|
-
metadata: {
|
|
10819
|
-
sessionData: {
|
|
10820
|
-
agentId,
|
|
10821
|
-
sessionType: "mcp",
|
|
10822
|
-
mcpProtocolVersion: c.req.header("mcp-protocol-version"),
|
|
10823
|
-
initialized: false
|
|
10824
|
-
// Track initialization state
|
|
10825
|
-
}
|
|
10826
|
-
}
|
|
10827
|
-
});
|
|
10828
|
-
logger23.info(
|
|
10829
|
-
{ sessionId, conversationId: conversation.id },
|
|
10830
|
-
"Created MCP session as conversation"
|
|
10831
|
-
);
|
|
10832
|
-
const transport = new StreamableHTTPServerTransport({
|
|
10833
|
-
sessionIdGenerator: () => sessionId
|
|
10920
|
+
await transport.handleRequest(req, res, body);
|
|
10921
|
+
logger23.info({ sessionId }, "Successfully handled initialization request");
|
|
10922
|
+
return toFetchResponse(res);
|
|
10834
10923
|
});
|
|
10835
|
-
const server = await getServer(validatedContext, executionContext, sessionId, credentialStores);
|
|
10836
|
-
await server.connect(transport);
|
|
10837
|
-
logger23.info({ sessionId }, "Server connected for initialization");
|
|
10838
|
-
res.setHeader("Mcp-Session-Id", sessionId);
|
|
10839
|
-
logger23.info(
|
|
10840
|
-
{
|
|
10841
|
-
sessionId,
|
|
10842
|
-
bodyMethod: body?.method,
|
|
10843
|
-
bodyId: body?.id
|
|
10844
|
-
},
|
|
10845
|
-
"About to handle initialization request"
|
|
10846
|
-
);
|
|
10847
|
-
await transport.handleRequest(req, res, body);
|
|
10848
|
-
logger23.info({ sessionId }, "Successfully handled initialization request");
|
|
10849
|
-
return toFetchResponse(res);
|
|
10850
10924
|
};
|
|
10851
10925
|
var handleExistingSessionRequest = async (body, executionContext, validatedContext, req, res, credentialStores) => {
|
|
10852
10926
|
const { tenantId, projectId, agentId } = executionContext;
|