@inkeep/agents-run-api 0.23.4 → 0.24.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/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { flushBatchProcessor } from './chunk-TVLDBLRZ.js';
|
|
2
|
-
import { getFormattedConversationHistory, createDefaultConversationHistoryConfig, saveA2AMessageResponse } from './chunk-
|
|
2
|
+
import { getFormattedConversationHistory, createDefaultConversationHistoryConfig, saveA2AMessageResponse } from './chunk-XLUE6U2L.js';
|
|
3
3
|
import { dbClient_default } from './chunk-IBMWBEXH.js';
|
|
4
4
|
import { env } from './chunk-LHCIBW34.js';
|
|
5
5
|
import { getLogger } from './chunk-A2S7GSHL.js';
|
|
6
6
|
import { __publicField } from './chunk-PKBMQBKP.js';
|
|
7
|
-
import { getTracer, HeadersScopeSchema, getRequestExecutionContext, createApiError, getAgentWithDefaultSubAgent, contextValidationMiddleware, getConversationId, getFullAgent, createOrGetConversation, getActiveAgentForConversation, setActiveAgentForConversation, getSubAgentById, handleContextResolution, createMessage, commonGetErrorResponses, loggerFactory, getDataComponent, getProject, createDefaultCredentialStores, CredentialStoreRegistry, listTaskIdsByContextId, getTask, getLedgerArtifacts, upsertLedgerArtifact, createTask, updateTask, setSpanWithError, updateConversation, handleApiError, TaskState, setActiveAgentForThread, getConversation, getAgentById, getRelatedAgentsForAgent, getToolsForAgent, getDataComponentsForAgent, getArtifactComponentsForAgent, dbResultToMcpTool, validateAndGetApiKey, ContextResolver, CredentialStuffer, MCPServerType, getCredentialReference, McpClient, getFunctionToolsForSubAgent, getFunction, getContextConfigById, getFullAgentDefinition, TemplateEngine, agentHasArtifactComponents, MCPTransportType, SPAN_KEYS, getExternalAgent } from '@inkeep/agents-core';
|
|
7
|
+
import { getTracer, HeadersScopeSchema, getRequestExecutionContext, createApiError, getAgentWithDefaultSubAgent, contextValidationMiddleware, getConversationId, getFullAgent, createOrGetConversation, getActiveAgentForConversation, setActiveAgentForConversation, getSubAgentById, handleContextResolution, createMessage, generateId, commonGetErrorResponses, loggerFactory, getDataComponent, getProject, createDefaultCredentialStores, CredentialStoreRegistry, listTaskIdsByContextId, getTask, getLedgerArtifacts, upsertLedgerArtifact, createTask, updateTask, setSpanWithError, updateConversation, handleApiError, TaskState, setActiveAgentForThread, getConversation, getAgentById, getRelatedAgentsForAgent, getToolsForAgent, getDataComponentsForAgent, getArtifactComponentsForAgent, dbResultToMcpTool, validateAndGetApiKey, ContextResolver, CredentialStuffer, MCPServerType, getCredentialReference, McpClient, getFunctionToolsForSubAgent, getFunction, getContextConfigById, getFullAgentDefinition, TemplateEngine, agentHasArtifactComponents, MCPTransportType, SPAN_KEYS, getExternalAgent } from '@inkeep/agents-core';
|
|
8
8
|
import { otel } from '@hono/otel';
|
|
9
9
|
import { OpenAPIHono, createRoute, z as z$1 } from '@hono/zod-openapi';
|
|
10
10
|
import { trace, propagation, context, SpanStatusCode } from '@opentelemetry/api';
|
|
@@ -16,7 +16,6 @@ import { createMiddleware } from 'hono/factory';
|
|
|
16
16
|
import { swaggerUI } from '@hono/swagger-ui';
|
|
17
17
|
import z5, { z } from 'zod';
|
|
18
18
|
import { streamSSE, stream } from 'hono/streaming';
|
|
19
|
-
import { nanoid } from 'nanoid';
|
|
20
19
|
import { createUIMessageStream, JsonToSseTransformStream, streamObject, parsePartialJson, generateObject, tool, streamText, generateText } from 'ai';
|
|
21
20
|
import { createAnthropic, anthropic } from '@ai-sdk/anthropic';
|
|
22
21
|
import { createGateway, gateway } from '@ai-sdk/gateway';
|
|
@@ -278,7 +277,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
278
277
|
const executionContext = getRequestExecutionContext(c);
|
|
279
278
|
const { agentId } = executionContext;
|
|
280
279
|
const task = {
|
|
281
|
-
id:
|
|
280
|
+
id: generateId(),
|
|
282
281
|
input: {
|
|
283
282
|
parts: params.message.parts.map((part) => ({
|
|
284
283
|
kind: part.kind,
|
|
@@ -371,7 +370,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
371
370
|
const messageText = params.message.parts.filter((part) => part.kind === "text" && "text" in part && part.text).map((part) => part.text).join(" ");
|
|
372
371
|
try {
|
|
373
372
|
const messageData = {
|
|
374
|
-
id:
|
|
373
|
+
id: generateId(),
|
|
375
374
|
tenantId: agent.tenantId,
|
|
376
375
|
projectId: agent.projectId,
|
|
377
376
|
conversationId: effectiveContextId,
|
|
@@ -453,7 +452,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
453
452
|
},
|
|
454
453
|
artifacts: [
|
|
455
454
|
{
|
|
456
|
-
artifactId:
|
|
455
|
+
artifactId: generateId(),
|
|
457
456
|
parts: [
|
|
458
457
|
{
|
|
459
458
|
kind: "data",
|
|
@@ -482,7 +481,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
482
481
|
if (params.configuration?.blocking === false) {
|
|
483
482
|
const taskResponse = {
|
|
484
483
|
id: task.id,
|
|
485
|
-
contextId: params.message.contextId ||
|
|
484
|
+
contextId: params.message.contextId || generateId(),
|
|
486
485
|
status: taskStatus,
|
|
487
486
|
artifacts: result.artifacts,
|
|
488
487
|
kind: "task"
|
|
@@ -494,7 +493,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
494
493
|
});
|
|
495
494
|
}
|
|
496
495
|
const messageResponse = {
|
|
497
|
-
messageId:
|
|
496
|
+
messageId: generateId(),
|
|
498
497
|
parts: result.artifacts?.[0]?.parts || [
|
|
499
498
|
{
|
|
500
499
|
kind: "text",
|
|
@@ -539,7 +538,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
539
538
|
});
|
|
540
539
|
}
|
|
541
540
|
const task = {
|
|
542
|
-
id:
|
|
541
|
+
id: generateId(),
|
|
543
542
|
input: {
|
|
544
543
|
parts: params.message.parts.map((part) => ({
|
|
545
544
|
kind: part.kind,
|
|
@@ -560,7 +559,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
560
559
|
try {
|
|
561
560
|
const initialTask = {
|
|
562
561
|
id: task.id,
|
|
563
|
-
contextId: params.message.contextId ||
|
|
562
|
+
contextId: params.message.contextId || generateId(),
|
|
564
563
|
status: {
|
|
565
564
|
state: TaskState.Working,
|
|
566
565
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -608,7 +607,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
608
607
|
}
|
|
609
608
|
}
|
|
610
609
|
const messageResponse = {
|
|
611
|
-
messageId:
|
|
610
|
+
messageId: generateId(),
|
|
612
611
|
parts: result.artifacts?.[0]?.parts || [
|
|
613
612
|
{
|
|
614
613
|
kind: "text",
|
|
@@ -674,14 +673,14 @@ async function handleTasksGet(c, _agent, request) {
|
|
|
674
673
|
const params = request.params;
|
|
675
674
|
const task = {
|
|
676
675
|
id: params.id,
|
|
677
|
-
contextId:
|
|
676
|
+
contextId: generateId(),
|
|
678
677
|
status: {
|
|
679
678
|
state: TaskState.Completed,
|
|
680
679
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
681
680
|
},
|
|
682
681
|
artifacts: [
|
|
683
682
|
{
|
|
684
|
-
artifactId:
|
|
683
|
+
artifactId: generateId(),
|
|
685
684
|
parts: [
|
|
686
685
|
{
|
|
687
686
|
kind: "text",
|
|
@@ -780,7 +779,7 @@ async function handleTasksResubscribe(c, agent, request) {
|
|
|
780
779
|
try {
|
|
781
780
|
const task = {
|
|
782
781
|
id: params.taskId,
|
|
783
|
-
contextId:
|
|
782
|
+
contextId: generateId(),
|
|
784
783
|
status: {
|
|
785
784
|
state: TaskState.Completed,
|
|
786
785
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -1029,7 +1028,7 @@ var _ToolSessionManager = class _ToolSessionManager {
|
|
|
1029
1028
|
* Create a new tool session for an agent execution
|
|
1030
1029
|
*/
|
|
1031
1030
|
createSession(tenantId, projectId, contextId, taskId) {
|
|
1032
|
-
const sessionId =
|
|
1031
|
+
const sessionId = generateId();
|
|
1033
1032
|
return this.createSessionWithId(sessionId, tenantId, projectId, contextId, taskId);
|
|
1034
1033
|
}
|
|
1035
1034
|
/**
|
|
@@ -4105,7 +4104,7 @@ function errorOp(message, subAgentId, severity = "error", code) {
|
|
|
4105
4104
|
};
|
|
4106
4105
|
}
|
|
4107
4106
|
function generateToolId() {
|
|
4108
|
-
return `tool_${
|
|
4107
|
+
return `tool_${generateId(8)}`;
|
|
4109
4108
|
}
|
|
4110
4109
|
var logger11 = getLogger("DataComponentSchema");
|
|
4111
4110
|
function jsonSchemaToZod(jsonSchema) {
|
|
@@ -5202,7 +5201,7 @@ function createDelegateToAgentTool({
|
|
|
5202
5201
|
description: generateDelegateToolDescription(delegateConfig.config),
|
|
5203
5202
|
inputSchema: z5.object({ message: z5.string() }),
|
|
5204
5203
|
execute: async (input, context) => {
|
|
5205
|
-
const delegationId = `del_${
|
|
5204
|
+
const delegationId = `del_${generateId()}`;
|
|
5206
5205
|
const activeSpan = trace.getActiveSpan();
|
|
5207
5206
|
if (activeSpan) {
|
|
5208
5207
|
activeSpan.setAttributes({
|
|
@@ -5299,7 +5298,7 @@ function createDelegateToAgentTool({
|
|
|
5299
5298
|
const messageToSend = {
|
|
5300
5299
|
role: "agent",
|
|
5301
5300
|
parts: [{ text: input.message, kind: "text" }],
|
|
5302
|
-
messageId:
|
|
5301
|
+
messageId: generateId(),
|
|
5303
5302
|
kind: "message",
|
|
5304
5303
|
contextId,
|
|
5305
5304
|
metadata: {
|
|
@@ -5314,7 +5313,7 @@ function createDelegateToAgentTool({
|
|
|
5314
5313
|
};
|
|
5315
5314
|
logger13.info({ messageToSend }, "messageToSend");
|
|
5316
5315
|
await createMessage(dbClient_default)({
|
|
5317
|
-
id:
|
|
5316
|
+
id: generateId(),
|
|
5318
5317
|
tenantId,
|
|
5319
5318
|
projectId,
|
|
5320
5319
|
conversationId: contextId,
|
|
@@ -7343,7 +7342,7 @@ var Agent = class {
|
|
|
7343
7342
|
inputSchema: tool3.inputSchema || tool3.parameters || {},
|
|
7344
7343
|
usageGuidelines: name.startsWith("transfer_to_") || name.startsWith("delegate_to_") ? `Use this tool to ${name.startsWith("transfer_to_") ? "transfer" : "delegate"} to another agent when appropriate.` : "Use this tool when appropriate for the task at hand."
|
|
7345
7344
|
}));
|
|
7346
|
-
const { getConversationScopedArtifacts } = await import('./conversations-
|
|
7345
|
+
const { getConversationScopedArtifacts } = await import('./conversations-H2TSLD3U.js');
|
|
7347
7346
|
const historyConfig = this.config.conversationHistoryConfig ?? createDefaultConversationHistoryConfig();
|
|
7348
7347
|
const referenceArtifacts = await getConversationScopedArtifacts({
|
|
7349
7348
|
tenantId: this.config.tenantId,
|
|
@@ -7677,326 +7676,334 @@ var Agent = class {
|
|
|
7677
7676
|
}
|
|
7678
7677
|
}
|
|
7679
7678
|
async generate(userMessage, runtimeContext) {
|
|
7680
|
-
return tracer.startActiveSpan(
|
|
7681
|
-
|
|
7682
|
-
|
|
7683
|
-
|
|
7684
|
-
|
|
7685
|
-
|
|
7686
|
-
this.streamRequestId = streamRequestId;
|
|
7687
|
-
this.streamHelper = streamRequestId ? getStreamHelper(streamRequestId) : void 0;
|
|
7688
|
-
const conversationId = runtimeContext?.metadata?.conversationId;
|
|
7689
|
-
if (conversationId) {
|
|
7690
|
-
this.setConversationId(conversationId);
|
|
7679
|
+
return tracer.startActiveSpan(
|
|
7680
|
+
"agent.generate",
|
|
7681
|
+
{
|
|
7682
|
+
attributes: {
|
|
7683
|
+
"subagent.id": this.config.id,
|
|
7684
|
+
"subagent.name": this.config.name
|
|
7691
7685
|
}
|
|
7692
|
-
|
|
7693
|
-
|
|
7694
|
-
|
|
7695
|
-
|
|
7696
|
-
|
|
7697
|
-
|
|
7698
|
-
|
|
7699
|
-
|
|
7700
|
-
|
|
7701
|
-
|
|
7702
|
-
|
|
7703
|
-
|
|
7704
|
-
"session.id": sessionId
|
|
7705
|
-
}
|
|
7706
|
-
},
|
|
7707
|
-
async (childSpan) => {
|
|
7708
|
-
try {
|
|
7709
|
-
const result = await Promise.all([
|
|
7710
|
-
this.getMcpTools(sessionId, streamRequestId),
|
|
7711
|
-
this.buildSystemPrompt(runtimeContext, false),
|
|
7712
|
-
// Normal prompt with data components
|
|
7713
|
-
this.buildSystemPrompt(runtimeContext, true),
|
|
7714
|
-
// Thinking prompt without data components
|
|
7715
|
-
this.getFunctionTools(sessionId, streamRequestId),
|
|
7716
|
-
Promise.resolve(this.getRelationTools(runtimeContext, sessionId)),
|
|
7717
|
-
this.getDefaultTools(streamRequestId)
|
|
7718
|
-
]);
|
|
7719
|
-
childSpan.setStatus({ code: SpanStatusCode.OK });
|
|
7720
|
-
return result;
|
|
7721
|
-
} catch (err) {
|
|
7722
|
-
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
7723
|
-
setSpanWithError(childSpan, errorObj);
|
|
7724
|
-
throw err;
|
|
7725
|
-
} finally {
|
|
7726
|
-
childSpan.end();
|
|
7727
|
-
}
|
|
7728
|
-
}
|
|
7729
|
-
);
|
|
7730
|
-
const allTools = {
|
|
7731
|
-
...mcpTools,
|
|
7732
|
-
...functionTools,
|
|
7733
|
-
...relationTools,
|
|
7734
|
-
...defaultTools
|
|
7735
|
-
};
|
|
7736
|
-
const sanitizedTools = this.sanitizeToolsForAISDK(allTools);
|
|
7737
|
-
let conversationHistory = "";
|
|
7738
|
-
const historyConfig = this.config.conversationHistoryConfig ?? createDefaultConversationHistoryConfig();
|
|
7739
|
-
if (historyConfig && historyConfig.mode !== "none") {
|
|
7740
|
-
if (historyConfig.mode === "full") {
|
|
7741
|
-
conversationHistory = await getFormattedConversationHistory({
|
|
7742
|
-
tenantId: this.config.tenantId,
|
|
7743
|
-
projectId: this.config.projectId,
|
|
7744
|
-
conversationId: contextId,
|
|
7745
|
-
currentMessage: userMessage,
|
|
7746
|
-
options: historyConfig,
|
|
7747
|
-
filters: {}
|
|
7748
|
-
});
|
|
7749
|
-
} else if (historyConfig.mode === "scoped") {
|
|
7750
|
-
conversationHistory = await getFormattedConversationHistory({
|
|
7751
|
-
tenantId: this.config.tenantId,
|
|
7752
|
-
projectId: this.config.projectId,
|
|
7753
|
-
conversationId: contextId,
|
|
7754
|
-
currentMessage: userMessage,
|
|
7755
|
-
options: historyConfig,
|
|
7756
|
-
filters: {
|
|
7757
|
-
subAgentId: this.config.id,
|
|
7758
|
-
taskId
|
|
7759
|
-
}
|
|
7760
|
-
});
|
|
7686
|
+
},
|
|
7687
|
+
async (span) => {
|
|
7688
|
+
const contextId = runtimeContext?.contextId || "default";
|
|
7689
|
+
const taskId = runtimeContext?.metadata?.taskId || "unknown";
|
|
7690
|
+
const streamRequestId = runtimeContext?.metadata?.streamRequestId;
|
|
7691
|
+
const sessionId = streamRequestId || "fallback-session";
|
|
7692
|
+
try {
|
|
7693
|
+
this.streamRequestId = streamRequestId;
|
|
7694
|
+
this.streamHelper = streamRequestId ? getStreamHelper(streamRequestId) : void 0;
|
|
7695
|
+
const conversationId = runtimeContext?.metadata?.conversationId;
|
|
7696
|
+
if (conversationId) {
|
|
7697
|
+
this.setConversationId(conversationId);
|
|
7761
7698
|
}
|
|
7762
|
-
|
|
7763
|
-
|
|
7764
|
-
|
|
7765
|
-
|
|
7766
|
-
|
|
7767
|
-
|
|
7768
|
-
|
|
7769
|
-
|
|
7770
|
-
|
|
7771
|
-
const timeoutMs = Math.min(configuredTimeout, MAX_ALLOWED_TIMEOUT_MS);
|
|
7772
|
-
if (modelSettings.maxDuration && modelSettings.maxDuration * 1e3 > MAX_ALLOWED_TIMEOUT_MS) {
|
|
7773
|
-
logger15.warn(
|
|
7699
|
+
const [
|
|
7700
|
+
mcpTools,
|
|
7701
|
+
systemPrompt,
|
|
7702
|
+
thinkingSystemPrompt,
|
|
7703
|
+
functionTools,
|
|
7704
|
+
relationTools,
|
|
7705
|
+
defaultTools
|
|
7706
|
+
] = await tracer.startActiveSpan(
|
|
7707
|
+
"agent.load_tools",
|
|
7774
7708
|
{
|
|
7775
|
-
|
|
7776
|
-
|
|
7777
|
-
|
|
7778
|
-
},
|
|
7779
|
-
"Requested timeout exceeded maximum allowed, capping to 10 minutes"
|
|
7780
|
-
);
|
|
7781
|
-
}
|
|
7782
|
-
const phase1SystemPrompt = hasStructuredOutput ? thinkingSystemPrompt : systemPrompt;
|
|
7783
|
-
const messages = [];
|
|
7784
|
-
messages.push({ role: "system", content: phase1SystemPrompt });
|
|
7785
|
-
if (conversationHistory.trim() !== "") {
|
|
7786
|
-
messages.push({ role: "user", content: conversationHistory });
|
|
7787
|
-
}
|
|
7788
|
-
messages.push({
|
|
7789
|
-
role: "user",
|
|
7790
|
-
content: userMessage
|
|
7791
|
-
});
|
|
7792
|
-
if (shouldStreamPhase1) {
|
|
7793
|
-
const streamConfig = {
|
|
7794
|
-
...modelSettings,
|
|
7795
|
-
toolChoice: "auto"
|
|
7796
|
-
// Allow natural text + tools
|
|
7797
|
-
};
|
|
7798
|
-
const streamResult = streamText({
|
|
7799
|
-
...streamConfig,
|
|
7800
|
-
messages,
|
|
7801
|
-
tools: sanitizedTools,
|
|
7802
|
-
stopWhen: async ({ steps }) => {
|
|
7803
|
-
const last = steps.at(-1);
|
|
7804
|
-
if (last && "text" in last && last.text) {
|
|
7805
|
-
try {
|
|
7806
|
-
await agentSessionManager.recordEvent(
|
|
7807
|
-
this.getStreamRequestId(),
|
|
7808
|
-
"agent_reasoning",
|
|
7809
|
-
this.config.id,
|
|
7810
|
-
{
|
|
7811
|
-
parts: [{ type: "text", content: last.text }]
|
|
7812
|
-
}
|
|
7813
|
-
);
|
|
7814
|
-
} catch (error) {
|
|
7815
|
-
logger15.debug({ error }, "Failed to track agent reasoning");
|
|
7816
|
-
}
|
|
7817
|
-
}
|
|
7818
|
-
if (steps.length >= 2) {
|
|
7819
|
-
const previousStep = steps[steps.length - 2];
|
|
7820
|
-
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
7821
|
-
const hasTransferCall = previousStep.toolCalls.some(
|
|
7822
|
-
(tc) => tc.toolName.startsWith("transfer_to_")
|
|
7823
|
-
);
|
|
7824
|
-
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
7825
|
-
return true;
|
|
7826
|
-
}
|
|
7827
|
-
}
|
|
7709
|
+
attributes: {
|
|
7710
|
+
"subAgent.name": this.config.name,
|
|
7711
|
+
"session.id": sessionId
|
|
7828
7712
|
}
|
|
7829
|
-
return steps.length >= this.getMaxGenerationSteps();
|
|
7830
7713
|
},
|
|
7831
|
-
|
|
7832
|
-
|
|
7833
|
-
|
|
7834
|
-
|
|
7835
|
-
|
|
7836
|
-
|
|
7837
|
-
|
|
7838
|
-
|
|
7839
|
-
|
|
7840
|
-
|
|
7841
|
-
|
|
7842
|
-
|
|
7843
|
-
|
|
7844
|
-
|
|
7845
|
-
|
|
7846
|
-
|
|
7847
|
-
|
|
7848
|
-
|
|
7849
|
-
|
|
7850
|
-
|
|
7851
|
-
|
|
7852
|
-
|
|
7853
|
-
streamHelper,
|
|
7854
|
-
this.config.tenantId,
|
|
7855
|
-
contextId,
|
|
7856
|
-
artifactParserOptions
|
|
7714
|
+
async (childSpan) => {
|
|
7715
|
+
try {
|
|
7716
|
+
const result = await Promise.all([
|
|
7717
|
+
this.getMcpTools(sessionId, streamRequestId),
|
|
7718
|
+
this.buildSystemPrompt(runtimeContext, false),
|
|
7719
|
+
// Normal prompt with data components
|
|
7720
|
+
this.buildSystemPrompt(runtimeContext, true),
|
|
7721
|
+
// Thinking prompt without data components
|
|
7722
|
+
this.getFunctionTools(sessionId, streamRequestId),
|
|
7723
|
+
Promise.resolve(this.getRelationTools(runtimeContext, sessionId)),
|
|
7724
|
+
this.getDefaultTools(streamRequestId)
|
|
7725
|
+
]);
|
|
7726
|
+
childSpan.setStatus({ code: SpanStatusCode.OK });
|
|
7727
|
+
return result;
|
|
7728
|
+
} catch (err) {
|
|
7729
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
7730
|
+
setSpanWithError(childSpan, errorObj);
|
|
7731
|
+
throw err;
|
|
7732
|
+
} finally {
|
|
7733
|
+
childSpan.end();
|
|
7734
|
+
}
|
|
7735
|
+
}
|
|
7857
7736
|
);
|
|
7858
|
-
|
|
7859
|
-
|
|
7860
|
-
|
|
7861
|
-
|
|
7862
|
-
|
|
7863
|
-
|
|
7864
|
-
|
|
7865
|
-
|
|
7866
|
-
|
|
7867
|
-
|
|
7868
|
-
|
|
7869
|
-
|
|
7870
|
-
|
|
7871
|
-
|
|
7872
|
-
|
|
7873
|
-
|
|
7874
|
-
|
|
7875
|
-
|
|
7876
|
-
|
|
7877
|
-
|
|
7878
|
-
|
|
7879
|
-
|
|
7737
|
+
const allTools = {
|
|
7738
|
+
...mcpTools,
|
|
7739
|
+
...functionTools,
|
|
7740
|
+
...relationTools,
|
|
7741
|
+
...defaultTools
|
|
7742
|
+
};
|
|
7743
|
+
const sanitizedTools = this.sanitizeToolsForAISDK(allTools);
|
|
7744
|
+
let conversationHistory = "";
|
|
7745
|
+
const historyConfig = this.config.conversationHistoryConfig ?? createDefaultConversationHistoryConfig();
|
|
7746
|
+
if (historyConfig && historyConfig.mode !== "none") {
|
|
7747
|
+
if (historyConfig.mode === "full") {
|
|
7748
|
+
conversationHistory = await getFormattedConversationHistory({
|
|
7749
|
+
tenantId: this.config.tenantId,
|
|
7750
|
+
projectId: this.config.projectId,
|
|
7751
|
+
conversationId: contextId,
|
|
7752
|
+
currentMessage: userMessage,
|
|
7753
|
+
options: historyConfig,
|
|
7754
|
+
filters: {}
|
|
7755
|
+
});
|
|
7756
|
+
} else if (historyConfig.mode === "scoped") {
|
|
7757
|
+
conversationHistory = await getFormattedConversationHistory({
|
|
7758
|
+
tenantId: this.config.tenantId,
|
|
7759
|
+
projectId: this.config.projectId,
|
|
7760
|
+
conversationId: contextId,
|
|
7761
|
+
currentMessage: userMessage,
|
|
7762
|
+
options: historyConfig,
|
|
7763
|
+
filters: {
|
|
7764
|
+
subAgentId: this.config.id,
|
|
7765
|
+
taskId
|
|
7880
7766
|
}
|
|
7767
|
+
});
|
|
7881
7768
|
}
|
|
7882
7769
|
}
|
|
7883
|
-
|
|
7884
|
-
|
|
7885
|
-
|
|
7886
|
-
|
|
7887
|
-
|
|
7888
|
-
|
|
7889
|
-
|
|
7890
|
-
|
|
7891
|
-
|
|
7892
|
-
|
|
7893
|
-
|
|
7770
|
+
const primaryModelSettings = this.getPrimaryModel();
|
|
7771
|
+
const modelSettings = ModelFactory.prepareGenerationConfig(primaryModelSettings);
|
|
7772
|
+
let response;
|
|
7773
|
+
let textResponse;
|
|
7774
|
+
const hasStructuredOutput = this.config.dataComponents && this.config.dataComponents.length > 0;
|
|
7775
|
+
const shouldStreamPhase1 = this.getStreamingHelper() && !hasStructuredOutput;
|
|
7776
|
+
const MAX_ALLOWED_TIMEOUT_MS = 6e5;
|
|
7777
|
+
const configuredTimeout = modelSettings.maxDuration ? Math.min(modelSettings.maxDuration * 1e3, MAX_ALLOWED_TIMEOUT_MS) : shouldStreamPhase1 ? CONSTANTS.PHASE_1_TIMEOUT_MS : CONSTANTS.NON_STREAMING_PHASE_1_TIMEOUT_MS;
|
|
7778
|
+
const timeoutMs = Math.min(configuredTimeout, MAX_ALLOWED_TIMEOUT_MS);
|
|
7779
|
+
if (modelSettings.maxDuration && modelSettings.maxDuration * 1e3 > MAX_ALLOWED_TIMEOUT_MS) {
|
|
7780
|
+
logger15.warn(
|
|
7781
|
+
{
|
|
7782
|
+
requestedTimeout: modelSettings.maxDuration * 1e3,
|
|
7783
|
+
appliedTimeout: timeoutMs,
|
|
7784
|
+
maxAllowed: MAX_ALLOWED_TIMEOUT_MS
|
|
7785
|
+
},
|
|
7786
|
+
"Requested timeout exceeded maximum allowed, capping to 10 minutes"
|
|
7787
|
+
);
|
|
7894
7788
|
}
|
|
7895
|
-
const
|
|
7896
|
-
|
|
7897
|
-
|
|
7898
|
-
|
|
7899
|
-
|
|
7900
|
-
...part.kind === "text" && { text: part.text },
|
|
7901
|
-
...part.kind === "data" && { data: part.data }
|
|
7902
|
-
}))
|
|
7903
|
-
};
|
|
7789
|
+
const phase1SystemPrompt = hasStructuredOutput ? thinkingSystemPrompt : systemPrompt;
|
|
7790
|
+
const messages = [];
|
|
7791
|
+
messages.push({ role: "system", content: phase1SystemPrompt });
|
|
7792
|
+
if (conversationHistory.trim() !== "") {
|
|
7793
|
+
messages.push({ role: "user", content: conversationHistory });
|
|
7904
7794
|
}
|
|
7905
|
-
|
|
7906
|
-
|
|
7907
|
-
|
|
7908
|
-
|
|
7909
|
-
|
|
7910
|
-
|
|
7911
|
-
// Force tool usage, prevent text generation
|
|
7912
|
-
};
|
|
7913
|
-
} else {
|
|
7914
|
-
genConfig = {
|
|
7795
|
+
messages.push({
|
|
7796
|
+
role: "user",
|
|
7797
|
+
content: userMessage
|
|
7798
|
+
});
|
|
7799
|
+
if (shouldStreamPhase1) {
|
|
7800
|
+
const streamConfig = {
|
|
7915
7801
|
...modelSettings,
|
|
7916
7802
|
toolChoice: "auto"
|
|
7917
|
-
// Allow
|
|
7803
|
+
// Allow natural text + tools
|
|
7918
7804
|
};
|
|
7919
|
-
|
|
7920
|
-
|
|
7921
|
-
|
|
7922
|
-
|
|
7923
|
-
|
|
7924
|
-
|
|
7925
|
-
|
|
7926
|
-
|
|
7927
|
-
|
|
7928
|
-
|
|
7929
|
-
|
|
7930
|
-
|
|
7931
|
-
|
|
7932
|
-
|
|
7933
|
-
|
|
7934
|
-
|
|
7935
|
-
)
|
|
7936
|
-
|
|
7937
|
-
|
|
7805
|
+
const streamResult = streamText({
|
|
7806
|
+
...streamConfig,
|
|
7807
|
+
messages,
|
|
7808
|
+
tools: sanitizedTools,
|
|
7809
|
+
stopWhen: async ({ steps }) => {
|
|
7810
|
+
const last = steps.at(-1);
|
|
7811
|
+
if (last && "text" in last && last.text) {
|
|
7812
|
+
try {
|
|
7813
|
+
await agentSessionManager.recordEvent(
|
|
7814
|
+
this.getStreamRequestId(),
|
|
7815
|
+
"agent_reasoning",
|
|
7816
|
+
this.config.id,
|
|
7817
|
+
{
|
|
7818
|
+
parts: [{ type: "text", content: last.text }]
|
|
7819
|
+
}
|
|
7820
|
+
);
|
|
7821
|
+
} catch (error) {
|
|
7822
|
+
logger15.debug({ error }, "Failed to track agent reasoning");
|
|
7823
|
+
}
|
|
7938
7824
|
}
|
|
7939
|
-
|
|
7940
|
-
|
|
7941
|
-
|
|
7942
|
-
|
|
7943
|
-
|
|
7944
|
-
|
|
7945
|
-
|
|
7946
|
-
|
|
7947
|
-
|
|
7825
|
+
if (steps.length >= 2) {
|
|
7826
|
+
const previousStep = steps[steps.length - 2];
|
|
7827
|
+
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
7828
|
+
const hasTransferCall = previousStep.toolCalls.some(
|
|
7829
|
+
(tc) => tc.toolName.startsWith("transfer_to_")
|
|
7830
|
+
);
|
|
7831
|
+
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
7832
|
+
return true;
|
|
7833
|
+
}
|
|
7948
7834
|
}
|
|
7949
7835
|
}
|
|
7836
|
+
return steps.length >= this.getMaxGenerationSteps();
|
|
7837
|
+
},
|
|
7838
|
+
experimental_telemetry: {
|
|
7839
|
+
isEnabled: true,
|
|
7840
|
+
functionId: this.config.id,
|
|
7841
|
+
recordInputs: true,
|
|
7842
|
+
recordOutputs: true
|
|
7843
|
+
},
|
|
7844
|
+
abortSignal: AbortSignal.timeout(timeoutMs)
|
|
7845
|
+
});
|
|
7846
|
+
const streamHelper = this.getStreamingHelper();
|
|
7847
|
+
if (!streamHelper) {
|
|
7848
|
+
throw new Error("Stream helper is unexpectedly undefined in streaming context");
|
|
7849
|
+
}
|
|
7850
|
+
const session = toolSessionManager.getSession(sessionId);
|
|
7851
|
+
const artifactParserOptions = {
|
|
7852
|
+
sessionId,
|
|
7853
|
+
taskId: session?.taskId,
|
|
7854
|
+
projectId: session?.projectId,
|
|
7855
|
+
artifactComponents: this.artifactComponents,
|
|
7856
|
+
streamRequestId: this.getStreamRequestId(),
|
|
7857
|
+
subAgentId: this.config.id
|
|
7858
|
+
};
|
|
7859
|
+
const parser = new IncrementalStreamParser(
|
|
7860
|
+
streamHelper,
|
|
7861
|
+
this.config.tenantId,
|
|
7862
|
+
contextId,
|
|
7863
|
+
artifactParserOptions
|
|
7864
|
+
);
|
|
7865
|
+
for await (const event of streamResult.fullStream) {
|
|
7866
|
+
switch (event.type) {
|
|
7867
|
+
case "text-delta":
|
|
7868
|
+
await parser.processTextChunk(event.text);
|
|
7869
|
+
break;
|
|
7870
|
+
case "tool-call":
|
|
7871
|
+
parser.markToolResult();
|
|
7872
|
+
break;
|
|
7873
|
+
case "tool-result":
|
|
7874
|
+
parser.markToolResult();
|
|
7875
|
+
break;
|
|
7876
|
+
case "finish":
|
|
7877
|
+
if (event.finishReason === "tool-calls") {
|
|
7878
|
+
parser.markToolResult();
|
|
7879
|
+
}
|
|
7880
|
+
break;
|
|
7881
|
+
case "error":
|
|
7882
|
+
if (event.error instanceof Error) {
|
|
7883
|
+
throw event.error;
|
|
7884
|
+
} else {
|
|
7885
|
+
const errorMessage = event.error?.error?.message;
|
|
7886
|
+
throw new Error(errorMessage);
|
|
7887
|
+
}
|
|
7950
7888
|
}
|
|
7951
|
-
|
|
7952
|
-
|
|
7953
|
-
|
|
7954
|
-
|
|
7955
|
-
|
|
7956
|
-
|
|
7957
|
-
|
|
7958
|
-
|
|
7959
|
-
|
|
7960
|
-
|
|
7961
|
-
|
|
7962
|
-
|
|
7963
|
-
|
|
7964
|
-
|
|
7965
|
-
|
|
7966
|
-
|
|
7967
|
-
|
|
7968
|
-
|
|
7969
|
-
|
|
7970
|
-
|
|
7971
|
-
|
|
7972
|
-
|
|
7973
|
-
|
|
7974
|
-
|
|
7975
|
-
|
|
7976
|
-
|
|
7977
|
-
|
|
7978
|
-
|
|
7979
|
-
|
|
7980
|
-
|
|
7981
|
-
|
|
7982
|
-
|
|
7983
|
-
|
|
7984
|
-
|
|
7985
|
-
|
|
7889
|
+
}
|
|
7890
|
+
await parser.finalize();
|
|
7891
|
+
response = await streamResult;
|
|
7892
|
+
const collectedParts = parser.getCollectedParts();
|
|
7893
|
+
if (collectedParts.length > 0) {
|
|
7894
|
+
response.formattedContent = {
|
|
7895
|
+
parts: collectedParts.map((part) => ({
|
|
7896
|
+
kind: part.kind,
|
|
7897
|
+
...part.kind === "text" && { text: part.text },
|
|
7898
|
+
...part.kind === "data" && { data: part.data }
|
|
7899
|
+
}))
|
|
7900
|
+
};
|
|
7901
|
+
}
|
|
7902
|
+
const streamedContent = parser.getAllStreamedContent();
|
|
7903
|
+
if (streamedContent.length > 0) {
|
|
7904
|
+
response.streamedContent = {
|
|
7905
|
+
parts: streamedContent.map((part) => ({
|
|
7906
|
+
kind: part.kind,
|
|
7907
|
+
...part.kind === "text" && { text: part.text },
|
|
7908
|
+
...part.kind === "data" && { data: part.data }
|
|
7909
|
+
}))
|
|
7910
|
+
};
|
|
7911
|
+
}
|
|
7912
|
+
} else {
|
|
7913
|
+
let genConfig;
|
|
7914
|
+
if (hasStructuredOutput) {
|
|
7915
|
+
genConfig = {
|
|
7916
|
+
...modelSettings,
|
|
7917
|
+
toolChoice: "required"
|
|
7918
|
+
// Force tool usage, prevent text generation
|
|
7919
|
+
};
|
|
7920
|
+
} else {
|
|
7921
|
+
genConfig = {
|
|
7922
|
+
...modelSettings,
|
|
7923
|
+
toolChoice: "auto"
|
|
7924
|
+
// Allow both tools and text generation
|
|
7925
|
+
};
|
|
7926
|
+
}
|
|
7927
|
+
response = await generateText({
|
|
7928
|
+
...genConfig,
|
|
7929
|
+
messages,
|
|
7930
|
+
tools: sanitizedTools,
|
|
7931
|
+
stopWhen: async ({ steps }) => {
|
|
7932
|
+
const last = steps.at(-1);
|
|
7933
|
+
if (last && "text" in last && last.text) {
|
|
7934
|
+
try {
|
|
7935
|
+
await agentSessionManager.recordEvent(
|
|
7936
|
+
this.getStreamRequestId(),
|
|
7937
|
+
"agent_reasoning",
|
|
7938
|
+
this.config.id,
|
|
7939
|
+
{
|
|
7940
|
+
parts: [{ type: "text", content: last.text }]
|
|
7986
7941
|
}
|
|
7987
|
-
|
|
7988
|
-
|
|
7989
|
-
|
|
7990
|
-
|
|
7991
|
-
|
|
7992
|
-
|
|
7993
|
-
|
|
7994
|
-
|
|
7995
|
-
|
|
7996
|
-
|
|
7997
|
-
|
|
7998
|
-
|
|
7999
|
-
|
|
7942
|
+
);
|
|
7943
|
+
} catch (error) {
|
|
7944
|
+
logger15.debug({ error }, "Failed to track agent reasoning");
|
|
7945
|
+
}
|
|
7946
|
+
}
|
|
7947
|
+
if (steps.length >= 2) {
|
|
7948
|
+
const previousStep = steps[steps.length - 2];
|
|
7949
|
+
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
7950
|
+
const hasStopTool = previousStep.toolCalls.some(
|
|
7951
|
+
(tc) => tc.toolName.startsWith("transfer_to_") || tc.toolName === "thinking_complete"
|
|
7952
|
+
);
|
|
7953
|
+
if (hasStopTool && "toolResults" in previousStep && previousStep.toolResults) {
|
|
7954
|
+
return true;
|
|
7955
|
+
}
|
|
7956
|
+
}
|
|
7957
|
+
}
|
|
7958
|
+
return steps.length >= this.getMaxGenerationSteps();
|
|
7959
|
+
},
|
|
7960
|
+
experimental_telemetry: {
|
|
7961
|
+
isEnabled: true,
|
|
7962
|
+
functionId: this.config.id,
|
|
7963
|
+
recordInputs: true,
|
|
7964
|
+
recordOutputs: true,
|
|
7965
|
+
metadata: {
|
|
7966
|
+
phase: "planning"
|
|
7967
|
+
}
|
|
7968
|
+
},
|
|
7969
|
+
abortSignal: AbortSignal.timeout(timeoutMs)
|
|
7970
|
+
});
|
|
7971
|
+
}
|
|
7972
|
+
if (response.steps) {
|
|
7973
|
+
const resolvedSteps = await response.steps;
|
|
7974
|
+
response = { ...response, steps: resolvedSteps };
|
|
7975
|
+
}
|
|
7976
|
+
if (hasStructuredOutput && !hasToolCallWithPrefix("transfer_to_")(response)) {
|
|
7977
|
+
const thinkingCompleteCall = response.steps?.flatMap((s) => s.toolCalls || [])?.find((tc) => tc.toolName === "thinking_complete");
|
|
7978
|
+
if (thinkingCompleteCall) {
|
|
7979
|
+
const reasoningFlow = [];
|
|
7980
|
+
if (response.steps) {
|
|
7981
|
+
response.steps.forEach((step) => {
|
|
7982
|
+
if (step.toolCalls && step.toolResults) {
|
|
7983
|
+
step.toolCalls.forEach((call, index) => {
|
|
7984
|
+
const result = step.toolResults[index];
|
|
7985
|
+
if (result) {
|
|
7986
|
+
const storedResult = toolSessionManager.getToolResult(
|
|
7987
|
+
sessionId,
|
|
7988
|
+
result.toolCallId
|
|
7989
|
+
);
|
|
7990
|
+
const toolName = storedResult?.toolName || call.toolName;
|
|
7991
|
+
if (toolName === "thinking_complete") {
|
|
7992
|
+
return;
|
|
7993
|
+
}
|
|
7994
|
+
const actualResult = storedResult?.result || result.result || result;
|
|
7995
|
+
const actualArgs = storedResult?.args || call.args;
|
|
7996
|
+
const cleanResult = actualResult && typeof actualResult === "object" && !Array.isArray(actualResult) ? Object.fromEntries(
|
|
7997
|
+
Object.entries(actualResult).filter(
|
|
7998
|
+
([key]) => key !== "_structureHints"
|
|
7999
|
+
)
|
|
8000
|
+
) : actualResult;
|
|
8001
|
+
const input = actualArgs ? JSON.stringify(actualArgs, null, 2) : "No input";
|
|
8002
|
+
const output = typeof cleanResult === "string" ? cleanResult : JSON.stringify(cleanResult, null, 2);
|
|
8003
|
+
let structureHintsFormatted = "";
|
|
8004
|
+
if (actualResult?._structureHints && this.artifactComponents && this.artifactComponents.length > 0) {
|
|
8005
|
+
const hints = actualResult._structureHints;
|
|
8006
|
+
structureHintsFormatted = `
|
|
8000
8007
|
### \u{1F4CA} Structure Hints for Artifact Creation
|
|
8001
8008
|
|
|
8002
8009
|
**Terminal Field Paths (${hints.terminalPaths?.length || 0} found):**
|
|
@@ -8020,8 +8027,8 @@ ${hints.commonFields?.map((field) => ` \u2022 ${field}`).join("\n") || " None
|
|
|
8020
8027
|
|
|
8021
8028
|
**Forbidden Syntax:** ${hints.forbiddenSyntax || "Use these paths for artifact base selectors."}
|
|
8022
8029
|
`;
|
|
8023
|
-
|
|
8024
|
-
|
|
8030
|
+
}
|
|
8031
|
+
const formattedResult = `## Tool: ${call.toolName}
|
|
8025
8032
|
|
|
8026
8033
|
### \u{1F527} TOOL_CALL_ID: ${result.toolCallId}
|
|
8027
8034
|
|
|
@@ -8030,130 +8037,61 @@ ${input}
|
|
|
8030
8037
|
|
|
8031
8038
|
### Output
|
|
8032
8039
|
${output}${structureHintsFormatted}`;
|
|
8033
|
-
|
|
8034
|
-
|
|
8035
|
-
|
|
8036
|
-
|
|
8037
|
-
|
|
8038
|
-
|
|
8039
|
-
}
|
|
8040
|
-
});
|
|
8041
|
-
}
|
|
8042
|
-
const componentSchemas = [];
|
|
8043
|
-
if (this.config.dataComponents && this.config.dataComponents.length > 0) {
|
|
8044
|
-
this.config.dataComponents.forEach((dc) => {
|
|
8045
|
-
const propsSchema = jsonSchemaToZod(dc.props);
|
|
8046
|
-
componentSchemas.push(
|
|
8047
|
-
z.object({
|
|
8048
|
-
id: z.string(),
|
|
8049
|
-
name: z.literal(dc.name),
|
|
8050
|
-
props: propsSchema
|
|
8051
|
-
})
|
|
8052
|
-
);
|
|
8053
|
-
});
|
|
8054
|
-
}
|
|
8055
|
-
if (this.artifactComponents.length > 0) {
|
|
8056
|
-
const artifactCreateSchemas = ArtifactCreateSchema.getSchemas(
|
|
8057
|
-
this.artifactComponents
|
|
8058
|
-
);
|
|
8059
|
-
componentSchemas.push(...artifactCreateSchemas);
|
|
8060
|
-
componentSchemas.push(ArtifactReferenceSchema.getSchema());
|
|
8061
|
-
}
|
|
8062
|
-
let dataComponentsSchema;
|
|
8063
|
-
if (componentSchemas.length === 1) {
|
|
8064
|
-
dataComponentsSchema = componentSchemas[0];
|
|
8065
|
-
} else {
|
|
8066
|
-
dataComponentsSchema = z.union(
|
|
8067
|
-
componentSchemas
|
|
8068
|
-
);
|
|
8069
|
-
}
|
|
8070
|
-
const structuredModelSettings = ModelFactory.prepareGenerationConfig(
|
|
8071
|
-
this.getStructuredOutputModel()
|
|
8072
|
-
);
|
|
8073
|
-
const phase2TimeoutMs = structuredModelSettings.maxDuration ? structuredModelSettings.maxDuration * 1e3 : CONSTANTS.PHASE_2_TIMEOUT_MS;
|
|
8074
|
-
const shouldStreamPhase2 = this.getStreamingHelper();
|
|
8075
|
-
if (shouldStreamPhase2) {
|
|
8076
|
-
const phase2Messages = [
|
|
8077
|
-
{
|
|
8078
|
-
role: "system",
|
|
8079
|
-
content: await this.buildPhase2SystemPrompt(runtimeContext)
|
|
8080
|
-
}
|
|
8081
|
-
];
|
|
8082
|
-
if (conversationHistory.trim() !== "") {
|
|
8083
|
-
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
8084
|
-
}
|
|
8085
|
-
phase2Messages.push({ role: "user", content: userMessage });
|
|
8086
|
-
phase2Messages.push(...reasoningFlow);
|
|
8087
|
-
const streamResult = streamObject({
|
|
8088
|
-
...structuredModelSettings,
|
|
8089
|
-
messages: phase2Messages,
|
|
8090
|
-
schema: z.object({
|
|
8091
|
-
dataComponents: z.array(dataComponentsSchema)
|
|
8092
|
-
}),
|
|
8093
|
-
experimental_telemetry: {
|
|
8094
|
-
isEnabled: true,
|
|
8095
|
-
functionId: this.config.id,
|
|
8096
|
-
recordInputs: true,
|
|
8097
|
-
recordOutputs: true,
|
|
8098
|
-
metadata: {
|
|
8099
|
-
phase: "structured_generation"
|
|
8040
|
+
reasoningFlow.push({
|
|
8041
|
+
role: "assistant",
|
|
8042
|
+
content: formattedResult
|
|
8043
|
+
});
|
|
8044
|
+
}
|
|
8045
|
+
});
|
|
8100
8046
|
}
|
|
8101
|
-
}
|
|
8102
|
-
abortSignal: AbortSignal.timeout(phase2TimeoutMs)
|
|
8103
|
-
});
|
|
8104
|
-
const streamHelper = this.getStreamingHelper();
|
|
8105
|
-
if (!streamHelper) {
|
|
8106
|
-
throw new Error("Stream helper is unexpectedly undefined in streaming context");
|
|
8047
|
+
});
|
|
8107
8048
|
}
|
|
8108
|
-
const
|
|
8109
|
-
|
|
8110
|
-
|
|
8111
|
-
|
|
8112
|
-
|
|
8113
|
-
|
|
8114
|
-
|
|
8115
|
-
|
|
8116
|
-
|
|
8117
|
-
|
|
8118
|
-
|
|
8119
|
-
|
|
8120
|
-
contextId,
|
|
8121
|
-
artifactParserOptions
|
|
8122
|
-
);
|
|
8123
|
-
for await (const delta of streamResult.partialObjectStream) {
|
|
8124
|
-
if (delta) {
|
|
8125
|
-
await parser.processObjectDelta(delta);
|
|
8126
|
-
}
|
|
8049
|
+
const componentSchemas = [];
|
|
8050
|
+
if (this.config.dataComponents && this.config.dataComponents.length > 0) {
|
|
8051
|
+
this.config.dataComponents.forEach((dc) => {
|
|
8052
|
+
const propsSchema = jsonSchemaToZod(dc.props);
|
|
8053
|
+
componentSchemas.push(
|
|
8054
|
+
z.object({
|
|
8055
|
+
id: z.string(),
|
|
8056
|
+
name: z.literal(dc.name),
|
|
8057
|
+
props: propsSchema
|
|
8058
|
+
})
|
|
8059
|
+
);
|
|
8060
|
+
});
|
|
8127
8061
|
}
|
|
8128
|
-
|
|
8129
|
-
|
|
8130
|
-
|
|
8131
|
-
|
|
8132
|
-
|
|
8133
|
-
|
|
8134
|
-
kind: part.kind,
|
|
8135
|
-
...part.kind === "text" && { text: part.text },
|
|
8136
|
-
...part.kind === "data" && { data: part.data }
|
|
8137
|
-
}))
|
|
8138
|
-
};
|
|
8062
|
+
if (this.artifactComponents.length > 0) {
|
|
8063
|
+
const artifactCreateSchemas = ArtifactCreateSchema.getSchemas(
|
|
8064
|
+
this.artifactComponents
|
|
8065
|
+
);
|
|
8066
|
+
componentSchemas.push(...artifactCreateSchemas);
|
|
8067
|
+
componentSchemas.push(ArtifactReferenceSchema.getSchema());
|
|
8139
8068
|
}
|
|
8140
|
-
|
|
8141
|
-
|
|
8142
|
-
|
|
8143
|
-
}
|
|
8144
|
-
|
|
8145
|
-
|
|
8146
|
-
|
|
8147
|
-
const phase2Messages = [
|
|
8148
|
-
{ role: "system", content: await this.buildPhase2SystemPrompt(runtimeContext) }
|
|
8149
|
-
];
|
|
8150
|
-
if (conversationHistory.trim() !== "") {
|
|
8151
|
-
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
8069
|
+
let dataComponentsSchema;
|
|
8070
|
+
if (componentSchemas.length === 1) {
|
|
8071
|
+
dataComponentsSchema = componentSchemas[0];
|
|
8072
|
+
} else {
|
|
8073
|
+
dataComponentsSchema = z.union(
|
|
8074
|
+
componentSchemas
|
|
8075
|
+
);
|
|
8152
8076
|
}
|
|
8153
|
-
|
|
8154
|
-
|
|
8155
|
-
|
|
8156
|
-
|
|
8077
|
+
const structuredModelSettings = ModelFactory.prepareGenerationConfig(
|
|
8078
|
+
this.getStructuredOutputModel()
|
|
8079
|
+
);
|
|
8080
|
+
const phase2TimeoutMs = structuredModelSettings.maxDuration ? structuredModelSettings.maxDuration * 1e3 : CONSTANTS.PHASE_2_TIMEOUT_MS;
|
|
8081
|
+
const shouldStreamPhase2 = this.getStreamingHelper();
|
|
8082
|
+
if (shouldStreamPhase2) {
|
|
8083
|
+
const phase2Messages = [
|
|
8084
|
+
{
|
|
8085
|
+
role: "system",
|
|
8086
|
+
content: await this.buildPhase2SystemPrompt(runtimeContext)
|
|
8087
|
+
}
|
|
8088
|
+
];
|
|
8089
|
+
if (conversationHistory.trim() !== "") {
|
|
8090
|
+
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
8091
|
+
}
|
|
8092
|
+
phase2Messages.push({ role: "user", content: userMessage });
|
|
8093
|
+
phase2Messages.push(...reasoningFlow);
|
|
8094
|
+
const streamResult = streamObject({
|
|
8157
8095
|
...structuredModelSettings,
|
|
8158
8096
|
messages: phase2Messages,
|
|
8159
8097
|
schema: z.object({
|
|
@@ -8169,65 +8107,135 @@ ${output}${structureHintsFormatted}`;
|
|
|
8169
8107
|
}
|
|
8170
8108
|
},
|
|
8171
8109
|
abortSignal: AbortSignal.timeout(phase2TimeoutMs)
|
|
8172
|
-
})
|
|
8173
|
-
|
|
8174
|
-
|
|
8175
|
-
|
|
8176
|
-
|
|
8177
|
-
|
|
8178
|
-
|
|
8110
|
+
});
|
|
8111
|
+
const streamHelper = this.getStreamingHelper();
|
|
8112
|
+
if (!streamHelper) {
|
|
8113
|
+
throw new Error("Stream helper is unexpectedly undefined in streaming context");
|
|
8114
|
+
}
|
|
8115
|
+
const session = toolSessionManager.getSession(sessionId);
|
|
8116
|
+
const artifactParserOptions = {
|
|
8117
|
+
sessionId,
|
|
8118
|
+
taskId: session?.taskId,
|
|
8119
|
+
projectId: session?.projectId,
|
|
8120
|
+
artifactComponents: this.artifactComponents,
|
|
8121
|
+
streamRequestId: this.getStreamRequestId(),
|
|
8122
|
+
subAgentId: this.config.id
|
|
8123
|
+
};
|
|
8124
|
+
const parser = new IncrementalStreamParser(
|
|
8125
|
+
streamHelper,
|
|
8126
|
+
this.config.tenantId,
|
|
8127
|
+
contextId,
|
|
8128
|
+
artifactParserOptions
|
|
8129
|
+
);
|
|
8130
|
+
for await (const delta of streamResult.partialObjectStream) {
|
|
8131
|
+
if (delta) {
|
|
8132
|
+
await parser.processObjectDelta(delta);
|
|
8133
|
+
}
|
|
8134
|
+
}
|
|
8135
|
+
await parser.finalize();
|
|
8136
|
+
const structuredResponse = await streamResult;
|
|
8137
|
+
const collectedParts = parser.getCollectedParts();
|
|
8138
|
+
if (collectedParts.length > 0) {
|
|
8139
|
+
response.formattedContent = {
|
|
8140
|
+
parts: collectedParts.map((part) => ({
|
|
8141
|
+
kind: part.kind,
|
|
8142
|
+
...part.kind === "text" && { text: part.text },
|
|
8143
|
+
...part.kind === "data" && { data: part.data }
|
|
8144
|
+
}))
|
|
8145
|
+
};
|
|
8146
|
+
}
|
|
8147
|
+
response = {
|
|
8148
|
+
...response,
|
|
8149
|
+
object: structuredResponse.object
|
|
8150
|
+
};
|
|
8151
|
+
textResponse = JSON.stringify(structuredResponse.object, null, 2);
|
|
8152
|
+
} else {
|
|
8153
|
+
const { withJsonPostProcessing } = await import('./json-postprocessor-VOMU4E5L.js');
|
|
8154
|
+
const phase2Messages = [
|
|
8155
|
+
{ role: "system", content: await this.buildPhase2SystemPrompt(runtimeContext) }
|
|
8156
|
+
];
|
|
8157
|
+
if (conversationHistory.trim() !== "") {
|
|
8158
|
+
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
8159
|
+
}
|
|
8160
|
+
phase2Messages.push({ role: "user", content: userMessage });
|
|
8161
|
+
phase2Messages.push(...reasoningFlow);
|
|
8162
|
+
const structuredResponse = await generateObject(
|
|
8163
|
+
withJsonPostProcessing({
|
|
8164
|
+
...structuredModelSettings,
|
|
8165
|
+
messages: phase2Messages,
|
|
8166
|
+
schema: z.object({
|
|
8167
|
+
dataComponents: z.array(dataComponentsSchema)
|
|
8168
|
+
}),
|
|
8169
|
+
experimental_telemetry: {
|
|
8170
|
+
isEnabled: true,
|
|
8171
|
+
functionId: this.config.id,
|
|
8172
|
+
recordInputs: true,
|
|
8173
|
+
recordOutputs: true,
|
|
8174
|
+
metadata: {
|
|
8175
|
+
phase: "structured_generation"
|
|
8176
|
+
}
|
|
8177
|
+
},
|
|
8178
|
+
abortSignal: AbortSignal.timeout(phase2TimeoutMs)
|
|
8179
|
+
})
|
|
8180
|
+
);
|
|
8181
|
+
response = {
|
|
8182
|
+
...response,
|
|
8183
|
+
object: structuredResponse.object
|
|
8184
|
+
};
|
|
8185
|
+
textResponse = JSON.stringify(structuredResponse.object, null, 2);
|
|
8186
|
+
}
|
|
8187
|
+
} else {
|
|
8188
|
+
textResponse = response.text || "";
|
|
8179
8189
|
}
|
|
8180
8190
|
} else {
|
|
8181
|
-
textResponse = response.text || "";
|
|
8191
|
+
textResponse = response.steps[response.steps.length - 1].text || "";
|
|
8182
8192
|
}
|
|
8183
|
-
|
|
8184
|
-
|
|
8185
|
-
|
|
8186
|
-
|
|
8187
|
-
|
|
8188
|
-
|
|
8189
|
-
|
|
8190
|
-
|
|
8191
|
-
|
|
8192
|
-
|
|
8193
|
-
|
|
8194
|
-
|
|
8195
|
-
|
|
8196
|
-
|
|
8197
|
-
|
|
8198
|
-
|
|
8199
|
-
|
|
8200
|
-
|
|
8201
|
-
|
|
8202
|
-
|
|
8203
|
-
contextId
|
|
8204
|
-
|
|
8205
|
-
} else if (textResponse) {
|
|
8206
|
-
formattedContent = await responseFormatter.formatResponse(textResponse, contextId);
|
|
8193
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
8194
|
+
span.end();
|
|
8195
|
+
let formattedContent = response.formattedContent || null;
|
|
8196
|
+
if (!formattedContent) {
|
|
8197
|
+
const session = toolSessionManager.getSession(sessionId);
|
|
8198
|
+
const responseFormatter = new ResponseFormatter(this.config.tenantId, {
|
|
8199
|
+
sessionId,
|
|
8200
|
+
taskId: session?.taskId,
|
|
8201
|
+
projectId: session?.projectId,
|
|
8202
|
+
contextId,
|
|
8203
|
+
artifactComponents: this.artifactComponents,
|
|
8204
|
+
streamRequestId: this.getStreamRequestId(),
|
|
8205
|
+
subAgentId: this.config.id
|
|
8206
|
+
});
|
|
8207
|
+
if (response.object) {
|
|
8208
|
+
formattedContent = await responseFormatter.formatObjectResponse(
|
|
8209
|
+
response.object,
|
|
8210
|
+
contextId
|
|
8211
|
+
);
|
|
8212
|
+
} else if (textResponse) {
|
|
8213
|
+
formattedContent = await responseFormatter.formatResponse(textResponse, contextId);
|
|
8214
|
+
}
|
|
8207
8215
|
}
|
|
8216
|
+
const formattedResponse = {
|
|
8217
|
+
...response,
|
|
8218
|
+
formattedContent
|
|
8219
|
+
};
|
|
8220
|
+
if (streamRequestId) {
|
|
8221
|
+
const generationType = response.object ? "object_generation" : "text_generation";
|
|
8222
|
+
agentSessionManager.recordEvent(streamRequestId, "agent_generate", this.config.id, {
|
|
8223
|
+
parts: (formattedContent?.parts || []).map((part) => ({
|
|
8224
|
+
type: part.kind === "text" ? "text" : part.kind === "data" ? "tool_result" : "text",
|
|
8225
|
+
content: part.text || JSON.stringify(part.data)
|
|
8226
|
+
})),
|
|
8227
|
+
generationType
|
|
8228
|
+
});
|
|
8229
|
+
}
|
|
8230
|
+
return formattedResponse;
|
|
8231
|
+
} catch (error) {
|
|
8232
|
+
const errorToThrow = error instanceof Error ? error : new Error(String(error));
|
|
8233
|
+
setSpanWithError(span, errorToThrow);
|
|
8234
|
+
span.end();
|
|
8235
|
+
throw errorToThrow;
|
|
8208
8236
|
}
|
|
8209
|
-
const formattedResponse = {
|
|
8210
|
-
...response,
|
|
8211
|
-
formattedContent
|
|
8212
|
-
};
|
|
8213
|
-
if (streamRequestId) {
|
|
8214
|
-
const generationType = response.object ? "object_generation" : "text_generation";
|
|
8215
|
-
agentSessionManager.recordEvent(streamRequestId, "agent_generate", this.config.id, {
|
|
8216
|
-
parts: (formattedContent?.parts || []).map((part) => ({
|
|
8217
|
-
type: part.kind === "text" ? "text" : part.kind === "data" ? "tool_result" : "text",
|
|
8218
|
-
content: part.text || JSON.stringify(part.data)
|
|
8219
|
-
})),
|
|
8220
|
-
generationType
|
|
8221
|
-
});
|
|
8222
|
-
}
|
|
8223
|
-
return formattedResponse;
|
|
8224
|
-
} catch (error) {
|
|
8225
|
-
const errorToThrow = error instanceof Error ? error : new Error(String(error));
|
|
8226
|
-
setSpanWithError(span, errorToThrow);
|
|
8227
|
-
span.end();
|
|
8228
|
-
throw errorToThrow;
|
|
8229
8237
|
}
|
|
8230
|
-
|
|
8238
|
+
);
|
|
8231
8239
|
}
|
|
8232
8240
|
};
|
|
8233
8241
|
|
|
@@ -8518,7 +8526,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
8518
8526
|
},
|
|
8519
8527
|
artifacts: [
|
|
8520
8528
|
{
|
|
8521
|
-
artifactId:
|
|
8529
|
+
artifactId: generateId(),
|
|
8522
8530
|
parts: [
|
|
8523
8531
|
{
|
|
8524
8532
|
kind: "data",
|
|
@@ -8551,7 +8559,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
8551
8559
|
status: { state: TaskState.Completed },
|
|
8552
8560
|
artifacts: [
|
|
8553
8561
|
{
|
|
8554
|
-
artifactId:
|
|
8562
|
+
artifactId: generateId(),
|
|
8555
8563
|
parts
|
|
8556
8564
|
}
|
|
8557
8565
|
]
|
|
@@ -9880,7 +9888,7 @@ var ExecutionHandler = class {
|
|
|
9880
9888
|
"ai.subAgent.name": currentAgentId
|
|
9881
9889
|
});
|
|
9882
9890
|
await createMessage(dbClient_default)({
|
|
9883
|
-
id:
|
|
9891
|
+
id: generateId(),
|
|
9884
9892
|
tenantId,
|
|
9885
9893
|
projectId,
|
|
9886
9894
|
conversationId,
|
|
@@ -10269,7 +10277,7 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
10269
10277
|
});
|
|
10270
10278
|
}
|
|
10271
10279
|
await createMessage(dbClient_default)({
|
|
10272
|
-
id:
|
|
10280
|
+
id: generateId(),
|
|
10273
10281
|
tenantId,
|
|
10274
10282
|
projectId,
|
|
10275
10283
|
conversationId,
|
|
@@ -10498,7 +10506,7 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
10498
10506
|
});
|
|
10499
10507
|
}
|
|
10500
10508
|
await createMessage(dbClient_default)({
|
|
10501
|
-
id:
|
|
10509
|
+
id: generateId(),
|
|
10502
10510
|
tenantId,
|
|
10503
10511
|
projectId,
|
|
10504
10512
|
conversationId,
|
|
@@ -10948,7 +10956,7 @@ var processUserMessage = async (tenantId, projectId, conversationId, query) => {
|
|
|
10948
10956
|
});
|
|
10949
10957
|
}
|
|
10950
10958
|
await createMessage(dbClient_default)({
|
|
10951
|
-
id:
|
|
10959
|
+
id: generateId(),
|
|
10952
10960
|
tenantId,
|
|
10953
10961
|
projectId,
|
|
10954
10962
|
conversationId,
|