@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.cjs
CHANGED
|
@@ -691,9 +691,26 @@ var init_LocalSandboxExecutor = __esm({
|
|
|
691
691
|
}
|
|
692
692
|
async installDependencies(sandboxDir) {
|
|
693
693
|
return new Promise((resolve, reject) => {
|
|
694
|
-
const
|
|
694
|
+
const npmEnv = {
|
|
695
|
+
...process.env,
|
|
696
|
+
// Set npm cache directory to sandbox to avoid /home/user issues
|
|
697
|
+
npm_config_cache: path.join(sandboxDir, ".npm-cache"),
|
|
698
|
+
// Set npm logs directory to sandbox
|
|
699
|
+
npm_config_logs_dir: path.join(sandboxDir, ".npm-logs"),
|
|
700
|
+
// Set npm temp directory to sandbox
|
|
701
|
+
npm_config_tmp: path.join(sandboxDir, ".npm-tmp"),
|
|
702
|
+
// Override HOME directory as fallback for npm
|
|
703
|
+
HOME: sandboxDir,
|
|
704
|
+
// Disable npm update notifier to avoid permission issues
|
|
705
|
+
npm_config_update_notifier: "false",
|
|
706
|
+
// Use non-interactive mode
|
|
707
|
+
npm_config_progress: "false",
|
|
708
|
+
npm_config_loglevel: "error"
|
|
709
|
+
};
|
|
710
|
+
const npm = child_process.spawn("npm", ["install", "--no-audit", "--no-fund"], {
|
|
695
711
|
cwd: sandboxDir,
|
|
696
|
-
stdio: "pipe"
|
|
712
|
+
stdio: "pipe",
|
|
713
|
+
env: npmEnv
|
|
697
714
|
});
|
|
698
715
|
let stderr = "";
|
|
699
716
|
npm.stdout?.on("data", () => {
|
|
@@ -3166,8 +3183,14 @@ var AgentSession = class {
|
|
|
3166
3183
|
return `Agent ${event.subAgentId} generating response`;
|
|
3167
3184
|
case "agent_reasoning":
|
|
3168
3185
|
return `Agent ${event.subAgentId} reasoning through request`;
|
|
3169
|
-
case "
|
|
3170
|
-
return `Tool
|
|
3186
|
+
case "tool_call":
|
|
3187
|
+
return `Tool call: ${event.data.toolName || "unknown"}`;
|
|
3188
|
+
case "tool_result": {
|
|
3189
|
+
const status = event.data.error ? "failed" : "completed";
|
|
3190
|
+
return `Tool result: ${event.data.toolName || "unknown"} (${status})`;
|
|
3191
|
+
}
|
|
3192
|
+
case "error":
|
|
3193
|
+
return `Error: ${event.data.message}`;
|
|
3171
3194
|
case "transfer":
|
|
3172
3195
|
return `Agent transfer: ${event.data.fromSubAgent} \u2192 ${event.data.targetSubAgent}`;
|
|
3173
3196
|
case "delegation_sent":
|
|
@@ -3888,15 +3911,29 @@ ${this.statusUpdateState?.config.prompt?.trim() || ""}`;
|
|
|
3888
3911
|
const activities = [];
|
|
3889
3912
|
for (const event of events) {
|
|
3890
3913
|
switch (event.eventType) {
|
|
3891
|
-
case "
|
|
3892
|
-
|
|
3914
|
+
case "tool_call": {
|
|
3915
|
+
activities.push(
|
|
3916
|
+
`\u{1F527} **${event.data.toolName}** (called)
|
|
3917
|
+
\u{1F4E5} Input: ${JSON.stringify(event.data.args)}`
|
|
3918
|
+
);
|
|
3919
|
+
break;
|
|
3920
|
+
}
|
|
3921
|
+
case "tool_result": {
|
|
3922
|
+
const resultStr = event.data.error ? `\u274C Error: ${event.data.error}` : JSON.stringify(event.data.result);
|
|
3893
3923
|
activities.push(
|
|
3894
3924
|
`\u{1F527} **${event.data.toolName}** ${event.data.duration ? `(${event.data.duration}ms)` : ""}
|
|
3895
|
-
\u{1F4E5} Input: ${JSON.stringify(event.data.args)}
|
|
3896
3925
|
\u{1F4E4} Output: ${resultStr}`
|
|
3897
3926
|
);
|
|
3898
3927
|
break;
|
|
3899
3928
|
}
|
|
3929
|
+
case "error": {
|
|
3930
|
+
activities.push(
|
|
3931
|
+
`\u274C **Error**: ${event.data.message}
|
|
3932
|
+
\u{1F50D} Code: ${event.data.code || "unknown"}
|
|
3933
|
+
\u{1F4CA} Severity: ${event.data.severity || "error"}`
|
|
3934
|
+
);
|
|
3935
|
+
break;
|
|
3936
|
+
}
|
|
3900
3937
|
// INTERNAL OPERATIONS - DO NOT EXPOSE TO STATUS UPDATES
|
|
3901
3938
|
case "transfer":
|
|
3902
3939
|
case "delegation_sent":
|
|
@@ -3981,7 +4018,7 @@ ${this.statusUpdateState?.config.prompt?.trim() || ""}`;
|
|
|
3981
4018
|
}
|
|
3982
4019
|
});
|
|
3983
4020
|
const toolCallEvent = this.events.find(
|
|
3984
|
-
(event) => event.eventType === "
|
|
4021
|
+
(event) => event.eventType === "tool_result" && event.data && "toolCallId" in event.data && event.data.toolCallId === artifactData.metadata?.toolCallId
|
|
3985
4022
|
);
|
|
3986
4023
|
const toolContext = toolCallEvent ? {
|
|
3987
4024
|
toolName: toolCallEvent.data.toolName,
|
|
@@ -7604,14 +7641,22 @@ var Agent = class {
|
|
|
7604
7641
|
});
|
|
7605
7642
|
}
|
|
7606
7643
|
const isInternalTool = toolName.includes("save_tool_result") || toolName.includes("thinking_complete") || toolName.startsWith("transfer_to_") || toolName.startsWith("delegate_to_");
|
|
7644
|
+
if (streamRequestId && !isInternalTool) {
|
|
7645
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_call", this.config.id, {
|
|
7646
|
+
toolName,
|
|
7647
|
+
args,
|
|
7648
|
+
toolCallId: context?.toolCallId,
|
|
7649
|
+
toolId
|
|
7650
|
+
});
|
|
7651
|
+
}
|
|
7607
7652
|
try {
|
|
7608
7653
|
const result = await originalExecute(args, context);
|
|
7609
7654
|
const duration = Date.now() - startTime;
|
|
7610
7655
|
if (streamRequestId && !isInternalTool) {
|
|
7611
|
-
agentSessionManager.recordEvent(streamRequestId, "
|
|
7656
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_result", this.config.id, {
|
|
7612
7657
|
toolName,
|
|
7613
|
-
args,
|
|
7614
7658
|
result,
|
|
7659
|
+
toolCallId: context?.toolCallId,
|
|
7615
7660
|
toolId,
|
|
7616
7661
|
duration
|
|
7617
7662
|
});
|
|
@@ -7621,12 +7666,13 @@ var Agent = class {
|
|
|
7621
7666
|
const duration = Date.now() - startTime;
|
|
7622
7667
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
7623
7668
|
if (streamRequestId && !isInternalTool) {
|
|
7624
|
-
agentSessionManager.recordEvent(streamRequestId, "
|
|
7669
|
+
agentSessionManager.recordEvent(streamRequestId, "tool_result", this.config.id, {
|
|
7625
7670
|
toolName,
|
|
7626
|
-
|
|
7627
|
-
|
|
7671
|
+
result: null,
|
|
7672
|
+
toolCallId: context?.toolCallId,
|
|
7628
7673
|
toolId,
|
|
7629
|
-
duration
|
|
7674
|
+
duration,
|
|
7675
|
+
error: errorMessage
|
|
7630
7676
|
});
|
|
7631
7677
|
}
|
|
7632
7678
|
throw error;
|
|
@@ -7895,13 +7941,14 @@ var Agent = class {
|
|
|
7895
7941
|
},
|
|
7896
7942
|
(span) => {
|
|
7897
7943
|
agentsCore.setSpanWithError(span, new Error(`0 effective tools available for ${tool3.name}`));
|
|
7898
|
-
agentSessionManager.recordEvent(streamRequestId, "
|
|
7899
|
-
|
|
7900
|
-
|
|
7901
|
-
|
|
7902
|
-
|
|
7903
|
-
|
|
7904
|
-
serverUrl: tool3.config.type === "mcp" ? tool3.config.mcp.server.url : "unknown"
|
|
7944
|
+
agentSessionManager.recordEvent(streamRequestId, "error", this.config.id, {
|
|
7945
|
+
message: `MCP server has 0 effective tools. Double check the selected tools in your graph and the active tools in the MCP server configuration.`,
|
|
7946
|
+
code: "no_tools_available",
|
|
7947
|
+
severity: "error",
|
|
7948
|
+
context: {
|
|
7949
|
+
toolName: tool3.name,
|
|
7950
|
+
serverUrl: tool3.config.type === "mcp" ? tool3.config.mcp.server.url : "unknown",
|
|
7951
|
+
operation: "mcp_tool_discovery"
|
|
7905
7952
|
}
|
|
7906
7953
|
});
|
|
7907
7954
|
span.end();
|
|
@@ -8726,7 +8773,9 @@ var Agent = class {
|
|
|
8726
8773
|
if (steps.length >= 2) {
|
|
8727
8774
|
const previousStep = steps[steps.length - 2];
|
|
8728
8775
|
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
8729
|
-
const hasTransferCall = previousStep.toolCalls.some(
|
|
8776
|
+
const hasTransferCall = previousStep.toolCalls.some(
|
|
8777
|
+
(tc) => tc.toolName.startsWith("transfer_to_")
|
|
8778
|
+
);
|
|
8730
8779
|
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
8731
8780
|
return true;
|
|
8732
8781
|
}
|
|
@@ -11040,174 +11089,191 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
11040
11089
|
);
|
|
11041
11090
|
const body = c.get("requestBody") || {};
|
|
11042
11091
|
const conversationId = body.conversationId || agentsCore.getConversationId();
|
|
11043
|
-
const
|
|
11044
|
-
|
|
11045
|
-
|
|
11046
|
-
|
|
11047
|
-
|
|
11048
|
-
|
|
11049
|
-
|
|
11050
|
-
|
|
11051
|
-
|
|
11052
|
-
|
|
11053
|
-
|
|
11054
|
-
|
|
11055
|
-
|
|
11056
|
-
|
|
11057
|
-
|
|
11058
|
-
|
|
11059
|
-
|
|
11060
|
-
agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11092
|
+
const activeSpan = api.trace.getActiveSpan();
|
|
11093
|
+
if (activeSpan) {
|
|
11094
|
+
activeSpan.setAttributes({
|
|
11095
|
+
"conversation.id": conversationId,
|
|
11096
|
+
"tenant.id": tenantId,
|
|
11097
|
+
"agent.id": agentId,
|
|
11098
|
+
"project.id": projectId
|
|
11099
|
+
});
|
|
11100
|
+
}
|
|
11101
|
+
let currentBag = api.propagation.getBaggage(api.context.active());
|
|
11102
|
+
if (!currentBag) {
|
|
11103
|
+
currentBag = api.propagation.createBaggage();
|
|
11104
|
+
}
|
|
11105
|
+
currentBag = currentBag.setEntry("conversation.id", { value: conversationId });
|
|
11106
|
+
const ctxWithBaggage = api.propagation.setBaggage(api.context.active(), currentBag);
|
|
11107
|
+
return await api.context.with(ctxWithBaggage, async () => {
|
|
11108
|
+
const fullAgent = await agentsCore.getFullAgent(dbClient_default)({
|
|
11061
11109
|
scopes: { tenantId, projectId, agentId }
|
|
11062
11110
|
});
|
|
11063
|
-
|
|
11111
|
+
let agent;
|
|
11112
|
+
let defaultSubAgentId;
|
|
11113
|
+
if (fullAgent) {
|
|
11114
|
+
agent = {
|
|
11115
|
+
id: fullAgent.id,
|
|
11116
|
+
name: fullAgent.name,
|
|
11117
|
+
tenantId,
|
|
11118
|
+
projectId,
|
|
11119
|
+
defaultSubAgentId: fullAgent.defaultSubAgentId
|
|
11120
|
+
};
|
|
11121
|
+
const agentKeys = Object.keys(fullAgent.subAgents || {});
|
|
11122
|
+
const firstAgentId = agentKeys.length > 0 ? agentKeys[0] : "";
|
|
11123
|
+
defaultSubAgentId = fullAgent.defaultSubAgentId || firstAgentId;
|
|
11124
|
+
} else {
|
|
11125
|
+
agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11126
|
+
scopes: { tenantId, projectId, agentId }
|
|
11127
|
+
});
|
|
11128
|
+
if (!agent) {
|
|
11129
|
+
throw agentsCore.createApiError({
|
|
11130
|
+
code: "not_found",
|
|
11131
|
+
message: "Agent not found"
|
|
11132
|
+
});
|
|
11133
|
+
}
|
|
11134
|
+
defaultSubAgentId = agent.defaultSubAgentId || "";
|
|
11135
|
+
}
|
|
11136
|
+
if (!defaultSubAgentId) {
|
|
11064
11137
|
throw agentsCore.createApiError({
|
|
11065
11138
|
code: "not_found",
|
|
11066
|
-
message: "
|
|
11139
|
+
message: "No default agent found in agent"
|
|
11067
11140
|
});
|
|
11068
11141
|
}
|
|
11069
|
-
|
|
11070
|
-
|
|
11071
|
-
|
|
11072
|
-
|
|
11073
|
-
|
|
11074
|
-
message: "No default agent found in agent"
|
|
11142
|
+
await agentsCore.createOrGetConversation(dbClient_default)({
|
|
11143
|
+
tenantId,
|
|
11144
|
+
projectId,
|
|
11145
|
+
id: conversationId,
|
|
11146
|
+
activeSubAgentId: defaultSubAgentId
|
|
11075
11147
|
});
|
|
11076
|
-
|
|
11077
|
-
await agentsCore.createOrGetConversation(dbClient_default)({
|
|
11078
|
-
tenantId,
|
|
11079
|
-
projectId,
|
|
11080
|
-
id: conversationId,
|
|
11081
|
-
activeSubAgentId: defaultSubAgentId
|
|
11082
|
-
});
|
|
11083
|
-
const activeAgent = await agentsCore.getActiveAgentForConversation(dbClient_default)({
|
|
11084
|
-
scopes: { tenantId, projectId },
|
|
11085
|
-
conversationId
|
|
11086
|
-
});
|
|
11087
|
-
if (!activeAgent) {
|
|
11088
|
-
agentsCore.setActiveAgentForConversation(dbClient_default)({
|
|
11148
|
+
const activeAgent = await agentsCore.getActiveAgentForConversation(dbClient_default)({
|
|
11089
11149
|
scopes: { tenantId, projectId },
|
|
11090
|
-
conversationId
|
|
11091
|
-
subAgentId: defaultSubAgentId
|
|
11150
|
+
conversationId
|
|
11092
11151
|
});
|
|
11093
|
-
|
|
11094
|
-
|
|
11095
|
-
|
|
11096
|
-
|
|
11097
|
-
|
|
11098
|
-
|
|
11099
|
-
|
|
11100
|
-
|
|
11101
|
-
|
|
11102
|
-
|
|
11152
|
+
if (!activeAgent) {
|
|
11153
|
+
agentsCore.setActiveAgentForConversation(dbClient_default)({
|
|
11154
|
+
scopes: { tenantId, projectId },
|
|
11155
|
+
conversationId,
|
|
11156
|
+
subAgentId: defaultSubAgentId
|
|
11157
|
+
});
|
|
11158
|
+
}
|
|
11159
|
+
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
11160
|
+
const agentInfo = await agentsCore.getSubAgentById(dbClient_default)({
|
|
11161
|
+
scopes: { tenantId, projectId, agentId },
|
|
11162
|
+
subAgentId
|
|
11103
11163
|
});
|
|
11104
|
-
|
|
11105
|
-
|
|
11106
|
-
|
|
11107
|
-
|
|
11108
|
-
|
|
11109
|
-
|
|
11110
|
-
|
|
11111
|
-
|
|
11112
|
-
|
|
11113
|
-
dbClient: dbClient_default,
|
|
11114
|
-
credentialStores
|
|
11115
|
-
});
|
|
11116
|
-
logger23.info(
|
|
11117
|
-
{
|
|
11164
|
+
if (!agentInfo) {
|
|
11165
|
+
throw agentsCore.createApiError({
|
|
11166
|
+
code: "not_found",
|
|
11167
|
+
message: "Agent not found"
|
|
11168
|
+
});
|
|
11169
|
+
}
|
|
11170
|
+
const validatedContext = c.get("validatedContext") || body.headers || {};
|
|
11171
|
+
const credentialStores = c.get("credentialStores");
|
|
11172
|
+
await agentsCore.handleContextResolution({
|
|
11118
11173
|
tenantId,
|
|
11119
11174
|
projectId,
|
|
11120
11175
|
agentId,
|
|
11121
11176
|
conversationId,
|
|
11122
|
-
|
|
11123
|
-
|
|
11124
|
-
|
|
11125
|
-
hasHeaders: !!body.headers,
|
|
11126
|
-
hasValidatedContext: !!validatedContext,
|
|
11127
|
-
validatedContextKeys: Object.keys(validatedContext)
|
|
11128
|
-
},
|
|
11129
|
-
"parameters"
|
|
11130
|
-
);
|
|
11131
|
-
const requestId2 = `chatcmpl-${Date.now()}`;
|
|
11132
|
-
const timestamp = Math.floor(Date.now() / 1e3);
|
|
11133
|
-
const lastUserMessage = body.messages.filter((msg) => msg.role === "user").slice(-1)[0];
|
|
11134
|
-
const userMessage = lastUserMessage ? getMessageText(lastUserMessage.content) : "";
|
|
11135
|
-
const messageSpan = api.trace.getActiveSpan();
|
|
11136
|
-
if (messageSpan) {
|
|
11137
|
-
messageSpan.setAttributes({
|
|
11138
|
-
"message.content": userMessage,
|
|
11139
|
-
"message.timestamp": Date.now()
|
|
11177
|
+
headers: validatedContext,
|
|
11178
|
+
dbClient: dbClient_default,
|
|
11179
|
+
credentialStores
|
|
11140
11180
|
});
|
|
11141
|
-
|
|
11142
|
-
|
|
11143
|
-
|
|
11144
|
-
|
|
11145
|
-
|
|
11146
|
-
conversationId,
|
|
11147
|
-
role: "user",
|
|
11148
|
-
content: {
|
|
11149
|
-
text: userMessage
|
|
11150
|
-
},
|
|
11151
|
-
visibility: "user-facing",
|
|
11152
|
-
messageType: "chat"
|
|
11153
|
-
});
|
|
11154
|
-
if (messageSpan) {
|
|
11155
|
-
messageSpan.addEvent("user.message.stored", {
|
|
11156
|
-
"message.id": conversationId,
|
|
11157
|
-
"database.operation": "insert"
|
|
11158
|
-
});
|
|
11159
|
-
}
|
|
11160
|
-
return streaming.streamSSE(c, async (stream2) => {
|
|
11161
|
-
try {
|
|
11162
|
-
const sseHelper = createSSEStreamHelper(stream2, requestId2, timestamp);
|
|
11163
|
-
await sseHelper.writeRole();
|
|
11164
|
-
logger23.info({ subAgentId }, "Starting execution");
|
|
11165
|
-
const emitOperationsHeader = c.req.header("x-emit-operations");
|
|
11166
|
-
const emitOperations = emitOperationsHeader === "true";
|
|
11167
|
-
const executionHandler = new ExecutionHandler();
|
|
11168
|
-
const result = await executionHandler.execute({
|
|
11169
|
-
executionContext,
|
|
11181
|
+
logger23.info(
|
|
11182
|
+
{
|
|
11183
|
+
tenantId,
|
|
11184
|
+
projectId,
|
|
11185
|
+
agentId,
|
|
11170
11186
|
conversationId,
|
|
11171
|
-
|
|
11172
|
-
|
|
11173
|
-
|
|
11174
|
-
|
|
11175
|
-
|
|
11187
|
+
defaultSubAgentId,
|
|
11188
|
+
activeSubAgentId: activeAgent?.activeSubAgentId || "none",
|
|
11189
|
+
hasContextConfig: !!agent.contextConfigId,
|
|
11190
|
+
hasHeaders: !!body.headers,
|
|
11191
|
+
hasValidatedContext: !!validatedContext,
|
|
11192
|
+
validatedContextKeys: Object.keys(validatedContext)
|
|
11193
|
+
},
|
|
11194
|
+
"parameters"
|
|
11195
|
+
);
|
|
11196
|
+
const requestId2 = `chatcmpl-${Date.now()}`;
|
|
11197
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
11198
|
+
const lastUserMessage = body.messages.filter((msg) => msg.role === "user").slice(-1)[0];
|
|
11199
|
+
const userMessage = lastUserMessage ? getMessageText(lastUserMessage.content) : "";
|
|
11200
|
+
const messageSpan = api.trace.getActiveSpan();
|
|
11201
|
+
if (messageSpan) {
|
|
11202
|
+
messageSpan.setAttributes({
|
|
11203
|
+
"message.content": userMessage,
|
|
11204
|
+
"message.timestamp": Date.now()
|
|
11176
11205
|
});
|
|
11177
|
-
|
|
11178
|
-
|
|
11179
|
-
|
|
11180
|
-
|
|
11181
|
-
|
|
11182
|
-
|
|
11183
|
-
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
|
|
11187
|
-
|
|
11188
|
-
|
|
11189
|
-
|
|
11190
|
-
|
|
11191
|
-
|
|
11192
|
-
|
|
11193
|
-
|
|
11194
|
-
|
|
11195
|
-
|
|
11196
|
-
|
|
11197
|
-
);
|
|
11206
|
+
}
|
|
11207
|
+
await agentsCore.createMessage(dbClient_default)({
|
|
11208
|
+
id: nanoid.nanoid(),
|
|
11209
|
+
tenantId,
|
|
11210
|
+
projectId,
|
|
11211
|
+
conversationId,
|
|
11212
|
+
role: "user",
|
|
11213
|
+
content: {
|
|
11214
|
+
text: userMessage
|
|
11215
|
+
},
|
|
11216
|
+
visibility: "user-facing",
|
|
11217
|
+
messageType: "chat"
|
|
11218
|
+
});
|
|
11219
|
+
if (messageSpan) {
|
|
11220
|
+
messageSpan.addEvent("user.message.stored", {
|
|
11221
|
+
"message.id": conversationId,
|
|
11222
|
+
"database.operation": "insert"
|
|
11223
|
+
});
|
|
11224
|
+
}
|
|
11225
|
+
return streaming.streamSSE(c, async (stream2) => {
|
|
11198
11226
|
try {
|
|
11199
11227
|
const sseHelper = createSSEStreamHelper(stream2, requestId2, timestamp);
|
|
11200
|
-
await sseHelper.
|
|
11201
|
-
|
|
11202
|
-
|
|
11203
|
-
|
|
11204
|
-
|
|
11228
|
+
await sseHelper.writeRole();
|
|
11229
|
+
logger23.info({ subAgentId }, "Starting execution");
|
|
11230
|
+
const emitOperationsHeader = c.req.header("x-emit-operations");
|
|
11231
|
+
const emitOperations = emitOperationsHeader === "true";
|
|
11232
|
+
const executionHandler = new ExecutionHandler();
|
|
11233
|
+
const result = await executionHandler.execute({
|
|
11234
|
+
executionContext,
|
|
11235
|
+
conversationId,
|
|
11236
|
+
userMessage,
|
|
11237
|
+
initialAgentId: subAgentId,
|
|
11238
|
+
requestId: requestId2,
|
|
11239
|
+
sseHelper,
|
|
11240
|
+
emitOperations
|
|
11241
|
+
});
|
|
11242
|
+
logger23.info(
|
|
11243
|
+
{ result },
|
|
11244
|
+
`Execution completed: ${result.success ? "success" : "failed"} after ${result.iterations} iterations`
|
|
11205
11245
|
);
|
|
11246
|
+
if (!result.success) {
|
|
11247
|
+
await sseHelper.writeOperation(
|
|
11248
|
+
errorOp(
|
|
11249
|
+
"Sorry, I was unable to process your request at this time. Please try again.",
|
|
11250
|
+
"system"
|
|
11251
|
+
)
|
|
11252
|
+
);
|
|
11253
|
+
}
|
|
11206
11254
|
await sseHelper.complete();
|
|
11207
|
-
} catch (
|
|
11208
|
-
logger23.error(
|
|
11255
|
+
} catch (error) {
|
|
11256
|
+
logger23.error(
|
|
11257
|
+
{
|
|
11258
|
+
error: error instanceof Error ? error.message : error,
|
|
11259
|
+
stack: error instanceof Error ? error.stack : void 0
|
|
11260
|
+
},
|
|
11261
|
+
"Error during streaming execution"
|
|
11262
|
+
);
|
|
11263
|
+
try {
|
|
11264
|
+
const sseHelper = createSSEStreamHelper(stream2, requestId2, timestamp);
|
|
11265
|
+
await sseHelper.writeOperation(
|
|
11266
|
+
errorOp(
|
|
11267
|
+
"Sorry, I was unable to process your request at this time. Please try again.",
|
|
11268
|
+
"system"
|
|
11269
|
+
)
|
|
11270
|
+
);
|
|
11271
|
+
await sseHelper.complete();
|
|
11272
|
+
} catch (streamError) {
|
|
11273
|
+
logger23.error({ streamError }, "Failed to write error to stream");
|
|
11274
|
+
}
|
|
11209
11275
|
}
|
|
11210
|
-
}
|
|
11276
|
+
});
|
|
11211
11277
|
});
|
|
11212
11278
|
} catch (error) {
|
|
11213
11279
|
logger23.error(
|
|
@@ -11303,123 +11369,131 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
11303
11369
|
"project.id": projectId
|
|
11304
11370
|
});
|
|
11305
11371
|
}
|
|
11306
|
-
|
|
11307
|
-
|
|
11308
|
-
|
|
11309
|
-
if (!agent) {
|
|
11310
|
-
throw agentsCore.createApiError({
|
|
11311
|
-
code: "not_found",
|
|
11312
|
-
message: "Agent not found"
|
|
11313
|
-
});
|
|
11372
|
+
let currentBag = api.propagation.getBaggage(api.context.active());
|
|
11373
|
+
if (!currentBag) {
|
|
11374
|
+
currentBag = api.propagation.createBaggage();
|
|
11314
11375
|
}
|
|
11315
|
-
|
|
11316
|
-
const
|
|
11317
|
-
|
|
11318
|
-
|
|
11319
|
-
|
|
11320
|
-
message: "Agent does not have a default agent configured"
|
|
11376
|
+
currentBag = currentBag.setEntry("conversation.id", { value: conversationId });
|
|
11377
|
+
const ctxWithBaggage = api.propagation.setBaggage(api.context.active(), currentBag);
|
|
11378
|
+
return await api.context.with(ctxWithBaggage, async () => {
|
|
11379
|
+
const agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11380
|
+
scopes: { tenantId, projectId, agentId }
|
|
11321
11381
|
});
|
|
11322
|
-
|
|
11323
|
-
|
|
11324
|
-
|
|
11325
|
-
|
|
11326
|
-
|
|
11327
|
-
|
|
11328
|
-
|
|
11382
|
+
if (!agent) {
|
|
11383
|
+
throw agentsCore.createApiError({
|
|
11384
|
+
code: "not_found",
|
|
11385
|
+
message: "Agent not found"
|
|
11386
|
+
});
|
|
11387
|
+
}
|
|
11388
|
+
const defaultSubAgentId = agent.defaultSubAgentId;
|
|
11389
|
+
const agentName = agent.name;
|
|
11390
|
+
if (!defaultSubAgentId) {
|
|
11391
|
+
throw agentsCore.createApiError({
|
|
11392
|
+
code: "bad_request",
|
|
11393
|
+
message: "Agent does not have a default agent configured"
|
|
11394
|
+
});
|
|
11395
|
+
}
|
|
11396
|
+
const activeAgent = await agentsCore.getActiveAgentForConversation(dbClient_default)({
|
|
11329
11397
|
scopes: { tenantId, projectId },
|
|
11330
|
-
conversationId
|
|
11331
|
-
subAgentId: defaultSubAgentId
|
|
11398
|
+
conversationId
|
|
11332
11399
|
});
|
|
11333
|
-
|
|
11334
|
-
|
|
11335
|
-
|
|
11336
|
-
|
|
11337
|
-
|
|
11338
|
-
|
|
11339
|
-
|
|
11340
|
-
|
|
11341
|
-
|
|
11342
|
-
|
|
11400
|
+
if (!activeAgent) {
|
|
11401
|
+
agentsCore.setActiveAgentForConversation(dbClient_default)({
|
|
11402
|
+
scopes: { tenantId, projectId },
|
|
11403
|
+
conversationId,
|
|
11404
|
+
subAgentId: defaultSubAgentId
|
|
11405
|
+
});
|
|
11406
|
+
}
|
|
11407
|
+
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
11408
|
+
const agentInfo = await agentsCore.getSubAgentById(dbClient_default)({
|
|
11409
|
+
scopes: { tenantId, projectId, agentId },
|
|
11410
|
+
subAgentId
|
|
11343
11411
|
});
|
|
11344
|
-
|
|
11345
|
-
|
|
11346
|
-
|
|
11347
|
-
|
|
11348
|
-
|
|
11349
|
-
|
|
11350
|
-
|
|
11351
|
-
|
|
11352
|
-
|
|
11353
|
-
|
|
11354
|
-
|
|
11355
|
-
|
|
11356
|
-
|
|
11357
|
-
|
|
11358
|
-
|
|
11359
|
-
|
|
11360
|
-
if (messageSpan) {
|
|
11361
|
-
messageSpan.setAttributes({
|
|
11362
|
-
"message.timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
11363
|
-
"message.content": userText,
|
|
11364
|
-
"agent.name": agentName
|
|
11412
|
+
if (!agentInfo) {
|
|
11413
|
+
throw agentsCore.createApiError({
|
|
11414
|
+
code: "not_found",
|
|
11415
|
+
message: "Agent not found"
|
|
11416
|
+
});
|
|
11417
|
+
}
|
|
11418
|
+
const validatedContext = c.get("validatedContext") || body.headers || {};
|
|
11419
|
+
const credentialStores = c.get("credentialStores");
|
|
11420
|
+
await agentsCore.handleContextResolution({
|
|
11421
|
+
tenantId,
|
|
11422
|
+
projectId,
|
|
11423
|
+
agentId,
|
|
11424
|
+
conversationId,
|
|
11425
|
+
headers: validatedContext,
|
|
11426
|
+
dbClient: dbClient_default,
|
|
11427
|
+
credentialStores
|
|
11365
11428
|
});
|
|
11366
|
-
|
|
11367
|
-
|
|
11368
|
-
|
|
11369
|
-
|
|
11370
|
-
|
|
11371
|
-
|
|
11372
|
-
|
|
11373
|
-
|
|
11374
|
-
|
|
11375
|
-
|
|
11376
|
-
|
|
11377
|
-
|
|
11378
|
-
|
|
11379
|
-
|
|
11380
|
-
|
|
11429
|
+
const lastUserMessage = body.messages.filter((m) => m.role === "user").slice(-1)[0];
|
|
11430
|
+
const userText = typeof lastUserMessage?.content === "string" ? lastUserMessage.content : lastUserMessage?.parts?.map((p) => p.text).join("") || "";
|
|
11431
|
+
logger24.info({ userText, lastUserMessage }, "userText");
|
|
11432
|
+
const messageSpan = api.trace.getActiveSpan();
|
|
11433
|
+
if (messageSpan) {
|
|
11434
|
+
messageSpan.setAttributes({
|
|
11435
|
+
"message.timestamp": (/* @__PURE__ */ new Date()).toISOString(),
|
|
11436
|
+
"message.content": userText,
|
|
11437
|
+
"agent.name": agentName
|
|
11438
|
+
});
|
|
11439
|
+
}
|
|
11440
|
+
await agentsCore.createMessage(dbClient_default)({
|
|
11441
|
+
id: nanoid.nanoid(),
|
|
11442
|
+
tenantId,
|
|
11443
|
+
projectId,
|
|
11444
|
+
conversationId,
|
|
11445
|
+
role: "user",
|
|
11446
|
+
content: { text: userText },
|
|
11447
|
+
visibility: "user-facing",
|
|
11448
|
+
messageType: "chat"
|
|
11381
11449
|
});
|
|
11382
|
-
|
|
11383
|
-
|
|
11384
|
-
|
|
11385
|
-
|
|
11386
|
-
|
|
11387
|
-
|
|
11388
|
-
|
|
11389
|
-
|
|
11390
|
-
const
|
|
11391
|
-
|
|
11392
|
-
|
|
11393
|
-
|
|
11394
|
-
|
|
11395
|
-
|
|
11396
|
-
|
|
11397
|
-
|
|
11398
|
-
|
|
11399
|
-
|
|
11400
|
-
|
|
11401
|
-
|
|
11402
|
-
|
|
11403
|
-
|
|
11404
|
-
|
|
11405
|
-
|
|
11406
|
-
|
|
11407
|
-
|
|
11450
|
+
if (messageSpan) {
|
|
11451
|
+
messageSpan.addEvent("user.message.stored", {
|
|
11452
|
+
"message.id": conversationId,
|
|
11453
|
+
"database.operation": "insert"
|
|
11454
|
+
});
|
|
11455
|
+
}
|
|
11456
|
+
const dataStream = ai.createUIMessageStream({
|
|
11457
|
+
execute: async ({ writer }) => {
|
|
11458
|
+
const streamHelper = createVercelStreamHelper(writer);
|
|
11459
|
+
try {
|
|
11460
|
+
const emitOperationsHeader = c.req.header("x-emit-operations");
|
|
11461
|
+
const emitOperations = emitOperationsHeader === "true";
|
|
11462
|
+
const executionHandler = new ExecutionHandler();
|
|
11463
|
+
const result = await executionHandler.execute({
|
|
11464
|
+
executionContext,
|
|
11465
|
+
conversationId,
|
|
11466
|
+
userMessage: userText,
|
|
11467
|
+
initialAgentId: subAgentId,
|
|
11468
|
+
requestId: `chatds-${Date.now()}`,
|
|
11469
|
+
sseHelper: streamHelper,
|
|
11470
|
+
emitOperations
|
|
11471
|
+
});
|
|
11472
|
+
if (!result.success) {
|
|
11473
|
+
await streamHelper.writeOperation(errorOp("Unable to process request", "system"));
|
|
11474
|
+
}
|
|
11475
|
+
} catch (err) {
|
|
11476
|
+
logger24.error({ err }, "Streaming error");
|
|
11477
|
+
await streamHelper.writeOperation(errorOp("Internal server error", "system"));
|
|
11478
|
+
} finally {
|
|
11479
|
+
if ("cleanup" in streamHelper && typeof streamHelper.cleanup === "function") {
|
|
11480
|
+
streamHelper.cleanup();
|
|
11481
|
+
}
|
|
11408
11482
|
}
|
|
11409
11483
|
}
|
|
11410
|
-
}
|
|
11484
|
+
});
|
|
11485
|
+
c.header("content-type", "text/event-stream");
|
|
11486
|
+
c.header("cache-control", "no-cache");
|
|
11487
|
+
c.header("connection", "keep-alive");
|
|
11488
|
+
c.header("x-vercel-ai-data-stream", "v2");
|
|
11489
|
+
c.header("x-accel-buffering", "no");
|
|
11490
|
+
return streaming.stream(
|
|
11491
|
+
c,
|
|
11492
|
+
(stream2) => stream2.pipe(
|
|
11493
|
+
dataStream.pipeThrough(new ai.JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())
|
|
11494
|
+
)
|
|
11495
|
+
);
|
|
11411
11496
|
});
|
|
11412
|
-
c.header("content-type", "text/event-stream");
|
|
11413
|
-
c.header("cache-control", "no-cache");
|
|
11414
|
-
c.header("connection", "keep-alive");
|
|
11415
|
-
c.header("x-vercel-ai-data-stream", "v2");
|
|
11416
|
-
c.header("x-accel-buffering", "no");
|
|
11417
|
-
return streaming.stream(
|
|
11418
|
-
c,
|
|
11419
|
-
(stream2) => stream2.pipe(
|
|
11420
|
-
dataStream.pipeThrough(new ai.JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())
|
|
11421
|
-
)
|
|
11422
|
-
);
|
|
11423
11497
|
} catch (error) {
|
|
11424
11498
|
logger24.error({ error }, "chatDataStream error");
|
|
11425
11499
|
throw agentsCore.createApiError({
|
|
@@ -11751,66 +11825,83 @@ var handleInitializationRequest = async (body, executionContext, validatedContex
|
|
|
11751
11825
|
const { tenantId, projectId, agentId } = executionContext;
|
|
11752
11826
|
logger25.info({ body }, "Received initialization request");
|
|
11753
11827
|
const sessionId = agentsCore.getConversationId();
|
|
11754
|
-
const
|
|
11755
|
-
|
|
11756
|
-
|
|
11757
|
-
|
|
11758
|
-
|
|
11759
|
-
|
|
11760
|
-
|
|
11761
|
-
|
|
11762
|
-
|
|
11763
|
-
|
|
11764
|
-
|
|
11765
|
-
);
|
|
11828
|
+
const activeSpan = api.trace.getActiveSpan();
|
|
11829
|
+
if (activeSpan) {
|
|
11830
|
+
activeSpan.setAttributes({
|
|
11831
|
+
"conversation.id": sessionId,
|
|
11832
|
+
"tenant.id": tenantId,
|
|
11833
|
+
"agent.id": agentId,
|
|
11834
|
+
"project.id": projectId
|
|
11835
|
+
});
|
|
11836
|
+
}
|
|
11837
|
+
let currentBag = api.propagation.getBaggage(api.context.active());
|
|
11838
|
+
if (!currentBag) {
|
|
11839
|
+
currentBag = api.propagation.createBaggage();
|
|
11766
11840
|
}
|
|
11767
|
-
|
|
11768
|
-
|
|
11841
|
+
currentBag = currentBag.setEntry("conversation.id", { value: sessionId });
|
|
11842
|
+
const ctxWithBaggage = api.propagation.setBaggage(api.context.active(), currentBag);
|
|
11843
|
+
return await api.context.with(ctxWithBaggage, async () => {
|
|
11844
|
+
const agent = await agentsCore.getAgentWithDefaultSubAgent(dbClient_default)({
|
|
11845
|
+
scopes: { tenantId, projectId, agentId }
|
|
11846
|
+
});
|
|
11847
|
+
if (!agent) {
|
|
11848
|
+
return c.json(
|
|
11849
|
+
{
|
|
11850
|
+
jsonrpc: "2.0",
|
|
11851
|
+
error: { code: -32001, message: "Agent not found" },
|
|
11852
|
+
id: body.id || null
|
|
11853
|
+
},
|
|
11854
|
+
{ status: 404 }
|
|
11855
|
+
);
|
|
11856
|
+
}
|
|
11857
|
+
if (!agent.defaultSubAgentId) {
|
|
11858
|
+
return c.json(
|
|
11859
|
+
{
|
|
11860
|
+
jsonrpc: "2.0",
|
|
11861
|
+
error: { code: -32001, message: "Agent does not have a default agent configured" },
|
|
11862
|
+
id: body.id || null
|
|
11863
|
+
},
|
|
11864
|
+
{ status: 400 }
|
|
11865
|
+
);
|
|
11866
|
+
}
|
|
11867
|
+
const conversation = await agentsCore.createOrGetConversation(dbClient_default)({
|
|
11868
|
+
id: sessionId,
|
|
11869
|
+
tenantId,
|
|
11870
|
+
projectId,
|
|
11871
|
+
activeSubAgentId: agent.defaultSubAgentId,
|
|
11872
|
+
metadata: {
|
|
11873
|
+
sessionData: {
|
|
11874
|
+
agentId,
|
|
11875
|
+
sessionType: "mcp",
|
|
11876
|
+
mcpProtocolVersion: c.req.header("mcp-protocol-version"),
|
|
11877
|
+
initialized: false
|
|
11878
|
+
// Track initialization state
|
|
11879
|
+
}
|
|
11880
|
+
}
|
|
11881
|
+
});
|
|
11882
|
+
logger25.info(
|
|
11883
|
+
{ sessionId, conversationId: conversation.id },
|
|
11884
|
+
"Created MCP session as conversation"
|
|
11885
|
+
);
|
|
11886
|
+
const transport = new streamableHttp_js.StreamableHTTPServerTransport({
|
|
11887
|
+
sessionIdGenerator: () => sessionId
|
|
11888
|
+
});
|
|
11889
|
+
const server = await getServer(validatedContext, executionContext, sessionId, credentialStores);
|
|
11890
|
+
await server.connect(transport);
|
|
11891
|
+
logger25.info({ sessionId }, "Server connected for initialization");
|
|
11892
|
+
res.setHeader("Mcp-Session-Id", sessionId);
|
|
11893
|
+
logger25.info(
|
|
11769
11894
|
{
|
|
11770
|
-
|
|
11771
|
-
|
|
11772
|
-
|
|
11895
|
+
sessionId,
|
|
11896
|
+
bodyMethod: body?.method,
|
|
11897
|
+
bodyId: body?.id
|
|
11773
11898
|
},
|
|
11774
|
-
|
|
11899
|
+
"About to handle initialization request"
|
|
11775
11900
|
);
|
|
11776
|
-
|
|
11777
|
-
|
|
11778
|
-
|
|
11779
|
-
tenantId,
|
|
11780
|
-
projectId,
|
|
11781
|
-
activeSubAgentId: agent.defaultSubAgentId,
|
|
11782
|
-
metadata: {
|
|
11783
|
-
sessionData: {
|
|
11784
|
-
agentId,
|
|
11785
|
-
sessionType: "mcp",
|
|
11786
|
-
mcpProtocolVersion: c.req.header("mcp-protocol-version"),
|
|
11787
|
-
initialized: false
|
|
11788
|
-
// Track initialization state
|
|
11789
|
-
}
|
|
11790
|
-
}
|
|
11791
|
-
});
|
|
11792
|
-
logger25.info(
|
|
11793
|
-
{ sessionId, conversationId: conversation.id },
|
|
11794
|
-
"Created MCP session as conversation"
|
|
11795
|
-
);
|
|
11796
|
-
const transport = new streamableHttp_js.StreamableHTTPServerTransport({
|
|
11797
|
-
sessionIdGenerator: () => sessionId
|
|
11901
|
+
await transport.handleRequest(req, res, body);
|
|
11902
|
+
logger25.info({ sessionId }, "Successfully handled initialization request");
|
|
11903
|
+
return fetchToNode.toFetchResponse(res);
|
|
11798
11904
|
});
|
|
11799
|
-
const server = await getServer(validatedContext, executionContext, sessionId, credentialStores);
|
|
11800
|
-
await server.connect(transport);
|
|
11801
|
-
logger25.info({ sessionId }, "Server connected for initialization");
|
|
11802
|
-
res.setHeader("Mcp-Session-Id", sessionId);
|
|
11803
|
-
logger25.info(
|
|
11804
|
-
{
|
|
11805
|
-
sessionId,
|
|
11806
|
-
bodyMethod: body?.method,
|
|
11807
|
-
bodyId: body?.id
|
|
11808
|
-
},
|
|
11809
|
-
"About to handle initialization request"
|
|
11810
|
-
);
|
|
11811
|
-
await transport.handleRequest(req, res, body);
|
|
11812
|
-
logger25.info({ sessionId }, "Successfully handled initialization request");
|
|
11813
|
-
return fetchToNode.toFetchResponse(res);
|
|
11814
11905
|
};
|
|
11815
11906
|
var handleExistingSessionRequest = async (body, executionContext, validatedContext, req, res, credentialStores) => {
|
|
11816
11907
|
const { tenantId, projectId, agentId } = executionContext;
|