@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.cjs
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var agentsCore = require('@inkeep/agents-core');
|
|
6
6
|
var z6 = require('zod');
|
|
7
|
-
var nanoid = require('nanoid');
|
|
8
7
|
var child_process = require('child_process');
|
|
9
8
|
var crypto = require('crypto');
|
|
10
9
|
var fs = require('fs');
|
|
@@ -192,7 +191,7 @@ async function saveA2AMessageResponse(response, params) {
|
|
|
192
191
|
return null;
|
|
193
192
|
}
|
|
194
193
|
return await agentsCore.createMessage(dbClient_default)({
|
|
195
|
-
id:
|
|
194
|
+
id: agentsCore.generateId(),
|
|
196
195
|
tenantId: params.tenantId,
|
|
197
196
|
projectId: params.projectId,
|
|
198
197
|
conversationId: params.conversationId,
|
|
@@ -1704,7 +1703,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1704
1703
|
const executionContext = agentsCore.getRequestExecutionContext(c);
|
|
1705
1704
|
const { agentId } = executionContext;
|
|
1706
1705
|
const task = {
|
|
1707
|
-
id:
|
|
1706
|
+
id: agentsCore.generateId(),
|
|
1708
1707
|
input: {
|
|
1709
1708
|
parts: params.message.parts.map((part) => ({
|
|
1710
1709
|
kind: part.kind,
|
|
@@ -1797,7 +1796,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1797
1796
|
const messageText = params.message.parts.filter((part) => part.kind === "text" && "text" in part && part.text).map((part) => part.text).join(" ");
|
|
1798
1797
|
try {
|
|
1799
1798
|
const messageData = {
|
|
1800
|
-
id:
|
|
1799
|
+
id: agentsCore.generateId(),
|
|
1801
1800
|
tenantId: agent.tenantId,
|
|
1802
1801
|
projectId: agent.projectId,
|
|
1803
1802
|
conversationId: effectiveContextId,
|
|
@@ -1879,7 +1878,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1879
1878
|
},
|
|
1880
1879
|
artifacts: [
|
|
1881
1880
|
{
|
|
1882
|
-
artifactId:
|
|
1881
|
+
artifactId: agentsCore.generateId(),
|
|
1883
1882
|
parts: [
|
|
1884
1883
|
{
|
|
1885
1884
|
kind: "data",
|
|
@@ -1908,7 +1907,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1908
1907
|
if (params.configuration?.blocking === false) {
|
|
1909
1908
|
const taskResponse = {
|
|
1910
1909
|
id: task.id,
|
|
1911
|
-
contextId: params.message.contextId ||
|
|
1910
|
+
contextId: params.message.contextId || agentsCore.generateId(),
|
|
1912
1911
|
status: taskStatus,
|
|
1913
1912
|
artifacts: result.artifacts,
|
|
1914
1913
|
kind: "task"
|
|
@@ -1920,7 +1919,7 @@ async function handleMessageSend(c, agent, request) {
|
|
|
1920
1919
|
});
|
|
1921
1920
|
}
|
|
1922
1921
|
const messageResponse = {
|
|
1923
|
-
messageId:
|
|
1922
|
+
messageId: agentsCore.generateId(),
|
|
1924
1923
|
parts: result.artifacts?.[0]?.parts || [
|
|
1925
1924
|
{
|
|
1926
1925
|
kind: "text",
|
|
@@ -1965,7 +1964,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
1965
1964
|
});
|
|
1966
1965
|
}
|
|
1967
1966
|
const task = {
|
|
1968
|
-
id:
|
|
1967
|
+
id: agentsCore.generateId(),
|
|
1969
1968
|
input: {
|
|
1970
1969
|
parts: params.message.parts.map((part) => ({
|
|
1971
1970
|
kind: part.kind,
|
|
@@ -1986,7 +1985,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
1986
1985
|
try {
|
|
1987
1986
|
const initialTask = {
|
|
1988
1987
|
id: task.id,
|
|
1989
|
-
contextId: params.message.contextId ||
|
|
1988
|
+
contextId: params.message.contextId || agentsCore.generateId(),
|
|
1990
1989
|
status: {
|
|
1991
1990
|
state: agentsCore.TaskState.Working,
|
|
1992
1991
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -2034,7 +2033,7 @@ async function handleMessageStream(c, agent, request) {
|
|
|
2034
2033
|
}
|
|
2035
2034
|
}
|
|
2036
2035
|
const messageResponse = {
|
|
2037
|
-
messageId:
|
|
2036
|
+
messageId: agentsCore.generateId(),
|
|
2038
2037
|
parts: result.artifacts?.[0]?.parts || [
|
|
2039
2038
|
{
|
|
2040
2039
|
kind: "text",
|
|
@@ -2100,14 +2099,14 @@ async function handleTasksGet(c, _agent, request) {
|
|
|
2100
2099
|
const params = request.params;
|
|
2101
2100
|
const task = {
|
|
2102
2101
|
id: params.id,
|
|
2103
|
-
contextId:
|
|
2102
|
+
contextId: agentsCore.generateId(),
|
|
2104
2103
|
status: {
|
|
2105
2104
|
state: agentsCore.TaskState.Completed,
|
|
2106
2105
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2107
2106
|
},
|
|
2108
2107
|
artifacts: [
|
|
2109
2108
|
{
|
|
2110
|
-
artifactId:
|
|
2109
|
+
artifactId: agentsCore.generateId(),
|
|
2111
2110
|
parts: [
|
|
2112
2111
|
{
|
|
2113
2112
|
kind: "text",
|
|
@@ -2206,7 +2205,7 @@ async function handleTasksResubscribe(c, agent, request) {
|
|
|
2206
2205
|
try {
|
|
2207
2206
|
const task = {
|
|
2208
2207
|
id: params.taskId,
|
|
2209
|
-
contextId:
|
|
2208
|
+
contextId: agentsCore.generateId(),
|
|
2210
2209
|
status: {
|
|
2211
2210
|
state: agentsCore.TaskState.Completed,
|
|
2212
2211
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -2463,7 +2462,7 @@ var _ToolSessionManager = class _ToolSessionManager {
|
|
|
2463
2462
|
* Create a new tool session for an agent execution
|
|
2464
2463
|
*/
|
|
2465
2464
|
createSession(tenantId, projectId, contextId, taskId) {
|
|
2466
|
-
const sessionId =
|
|
2465
|
+
const sessionId = agentsCore.generateId();
|
|
2467
2466
|
return this.createSessionWithId(sessionId, tenantId, projectId, contextId, taskId);
|
|
2468
2467
|
}
|
|
2469
2468
|
/**
|
|
@@ -5562,7 +5561,7 @@ function errorOp(message, subAgentId, severity = "error", code) {
|
|
|
5562
5561
|
};
|
|
5563
5562
|
}
|
|
5564
5563
|
function generateToolId() {
|
|
5565
|
-
return `tool_${
|
|
5564
|
+
return `tool_${agentsCore.generateId(8)}`;
|
|
5566
5565
|
}
|
|
5567
5566
|
|
|
5568
5567
|
// src/utils/artifact-component-schema.ts
|
|
@@ -6672,7 +6671,7 @@ function createDelegateToAgentTool({
|
|
|
6672
6671
|
description: generateDelegateToolDescription(delegateConfig.config),
|
|
6673
6672
|
inputSchema: z6__default.default.object({ message: z6__default.default.string() }),
|
|
6674
6673
|
execute: async (input, context) => {
|
|
6675
|
-
const delegationId = `del_${
|
|
6674
|
+
const delegationId = `del_${agentsCore.generateId()}`;
|
|
6676
6675
|
const activeSpan = api.trace.getActiveSpan();
|
|
6677
6676
|
if (activeSpan) {
|
|
6678
6677
|
activeSpan.setAttributes({
|
|
@@ -6769,7 +6768,7 @@ function createDelegateToAgentTool({
|
|
|
6769
6768
|
const messageToSend = {
|
|
6770
6769
|
role: "agent",
|
|
6771
6770
|
parts: [{ text: input.message, kind: "text" }],
|
|
6772
|
-
messageId:
|
|
6771
|
+
messageId: agentsCore.generateId(),
|
|
6773
6772
|
kind: "message",
|
|
6774
6773
|
contextId,
|
|
6775
6774
|
metadata: {
|
|
@@ -6784,7 +6783,7 @@ function createDelegateToAgentTool({
|
|
|
6784
6783
|
};
|
|
6785
6784
|
logger14.info({ messageToSend }, "messageToSend");
|
|
6786
6785
|
await agentsCore.createMessage(dbClient_default)({
|
|
6787
|
-
id:
|
|
6786
|
+
id: agentsCore.generateId(),
|
|
6788
6787
|
tenantId,
|
|
6789
6788
|
projectId,
|
|
6790
6789
|
conversationId: contextId,
|
|
@@ -9149,326 +9148,334 @@ var Agent = class {
|
|
|
9149
9148
|
}
|
|
9150
9149
|
}
|
|
9151
9150
|
async generate(userMessage, runtimeContext) {
|
|
9152
|
-
return tracer.startActiveSpan(
|
|
9153
|
-
|
|
9154
|
-
|
|
9155
|
-
|
|
9156
|
-
|
|
9157
|
-
|
|
9158
|
-
this.streamRequestId = streamRequestId;
|
|
9159
|
-
this.streamHelper = streamRequestId ? getStreamHelper(streamRequestId) : void 0;
|
|
9160
|
-
const conversationId = runtimeContext?.metadata?.conversationId;
|
|
9161
|
-
if (conversationId) {
|
|
9162
|
-
this.setConversationId(conversationId);
|
|
9163
|
-
}
|
|
9164
|
-
const [
|
|
9165
|
-
mcpTools,
|
|
9166
|
-
systemPrompt,
|
|
9167
|
-
thinkingSystemPrompt,
|
|
9168
|
-
functionTools,
|
|
9169
|
-
relationTools,
|
|
9170
|
-
defaultTools
|
|
9171
|
-
] = await tracer.startActiveSpan(
|
|
9172
|
-
"agent.load_tools",
|
|
9173
|
-
{
|
|
9174
|
-
attributes: {
|
|
9175
|
-
"subAgent.name": this.config.name,
|
|
9176
|
-
"session.id": sessionId
|
|
9177
|
-
}
|
|
9178
|
-
},
|
|
9179
|
-
async (childSpan) => {
|
|
9180
|
-
try {
|
|
9181
|
-
const result = await Promise.all([
|
|
9182
|
-
this.getMcpTools(sessionId, streamRequestId),
|
|
9183
|
-
this.buildSystemPrompt(runtimeContext, false),
|
|
9184
|
-
// Normal prompt with data components
|
|
9185
|
-
this.buildSystemPrompt(runtimeContext, true),
|
|
9186
|
-
// Thinking prompt without data components
|
|
9187
|
-
this.getFunctionTools(sessionId, streamRequestId),
|
|
9188
|
-
Promise.resolve(this.getRelationTools(runtimeContext, sessionId)),
|
|
9189
|
-
this.getDefaultTools(streamRequestId)
|
|
9190
|
-
]);
|
|
9191
|
-
childSpan.setStatus({ code: api.SpanStatusCode.OK });
|
|
9192
|
-
return result;
|
|
9193
|
-
} catch (err) {
|
|
9194
|
-
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
9195
|
-
agentsCore.setSpanWithError(childSpan, errorObj);
|
|
9196
|
-
throw err;
|
|
9197
|
-
} finally {
|
|
9198
|
-
childSpan.end();
|
|
9199
|
-
}
|
|
9200
|
-
}
|
|
9201
|
-
);
|
|
9202
|
-
const allTools = {
|
|
9203
|
-
...mcpTools,
|
|
9204
|
-
...functionTools,
|
|
9205
|
-
...relationTools,
|
|
9206
|
-
...defaultTools
|
|
9207
|
-
};
|
|
9208
|
-
const sanitizedTools = this.sanitizeToolsForAISDK(allTools);
|
|
9209
|
-
let conversationHistory = "";
|
|
9210
|
-
const historyConfig = this.config.conversationHistoryConfig ?? createDefaultConversationHistoryConfig();
|
|
9211
|
-
if (historyConfig && historyConfig.mode !== "none") {
|
|
9212
|
-
if (historyConfig.mode === "full") {
|
|
9213
|
-
conversationHistory = await getFormattedConversationHistory({
|
|
9214
|
-
tenantId: this.config.tenantId,
|
|
9215
|
-
projectId: this.config.projectId,
|
|
9216
|
-
conversationId: contextId,
|
|
9217
|
-
currentMessage: userMessage,
|
|
9218
|
-
options: historyConfig,
|
|
9219
|
-
filters: {}
|
|
9220
|
-
});
|
|
9221
|
-
} else if (historyConfig.mode === "scoped") {
|
|
9222
|
-
conversationHistory = await getFormattedConversationHistory({
|
|
9223
|
-
tenantId: this.config.tenantId,
|
|
9224
|
-
projectId: this.config.projectId,
|
|
9225
|
-
conversationId: contextId,
|
|
9226
|
-
currentMessage: userMessage,
|
|
9227
|
-
options: historyConfig,
|
|
9228
|
-
filters: {
|
|
9229
|
-
subAgentId: this.config.id,
|
|
9230
|
-
taskId
|
|
9231
|
-
}
|
|
9232
|
-
});
|
|
9233
|
-
}
|
|
9151
|
+
return tracer.startActiveSpan(
|
|
9152
|
+
"agent.generate",
|
|
9153
|
+
{
|
|
9154
|
+
attributes: {
|
|
9155
|
+
"subagent.id": this.config.id,
|
|
9156
|
+
"subagent.name": this.config.name
|
|
9234
9157
|
}
|
|
9235
|
-
|
|
9236
|
-
|
|
9237
|
-
|
|
9238
|
-
|
|
9239
|
-
const
|
|
9240
|
-
const
|
|
9241
|
-
|
|
9242
|
-
|
|
9243
|
-
|
|
9244
|
-
|
|
9245
|
-
|
|
9158
|
+
},
|
|
9159
|
+
async (span) => {
|
|
9160
|
+
const contextId = runtimeContext?.contextId || "default";
|
|
9161
|
+
const taskId = runtimeContext?.metadata?.taskId || "unknown";
|
|
9162
|
+
const streamRequestId = runtimeContext?.metadata?.streamRequestId;
|
|
9163
|
+
const sessionId = streamRequestId || "fallback-session";
|
|
9164
|
+
try {
|
|
9165
|
+
this.streamRequestId = streamRequestId;
|
|
9166
|
+
this.streamHelper = streamRequestId ? getStreamHelper(streamRequestId) : void 0;
|
|
9167
|
+
const conversationId = runtimeContext?.metadata?.conversationId;
|
|
9168
|
+
if (conversationId) {
|
|
9169
|
+
this.setConversationId(conversationId);
|
|
9170
|
+
}
|
|
9171
|
+
const [
|
|
9172
|
+
mcpTools,
|
|
9173
|
+
systemPrompt,
|
|
9174
|
+
thinkingSystemPrompt,
|
|
9175
|
+
functionTools,
|
|
9176
|
+
relationTools,
|
|
9177
|
+
defaultTools
|
|
9178
|
+
] = await tracer.startActiveSpan(
|
|
9179
|
+
"agent.load_tools",
|
|
9246
9180
|
{
|
|
9247
|
-
|
|
9248
|
-
|
|
9249
|
-
|
|
9250
|
-
},
|
|
9251
|
-
"Requested timeout exceeded maximum allowed, capping to 10 minutes"
|
|
9252
|
-
);
|
|
9253
|
-
}
|
|
9254
|
-
const phase1SystemPrompt = hasStructuredOutput ? thinkingSystemPrompt : systemPrompt;
|
|
9255
|
-
const messages = [];
|
|
9256
|
-
messages.push({ role: "system", content: phase1SystemPrompt });
|
|
9257
|
-
if (conversationHistory.trim() !== "") {
|
|
9258
|
-
messages.push({ role: "user", content: conversationHistory });
|
|
9259
|
-
}
|
|
9260
|
-
messages.push({
|
|
9261
|
-
role: "user",
|
|
9262
|
-
content: userMessage
|
|
9263
|
-
});
|
|
9264
|
-
if (shouldStreamPhase1) {
|
|
9265
|
-
const streamConfig = {
|
|
9266
|
-
...modelSettings,
|
|
9267
|
-
toolChoice: "auto"
|
|
9268
|
-
// Allow natural text + tools
|
|
9269
|
-
};
|
|
9270
|
-
const streamResult = ai.streamText({
|
|
9271
|
-
...streamConfig,
|
|
9272
|
-
messages,
|
|
9273
|
-
tools: sanitizedTools,
|
|
9274
|
-
stopWhen: async ({ steps }) => {
|
|
9275
|
-
const last = steps.at(-1);
|
|
9276
|
-
if (last && "text" in last && last.text) {
|
|
9277
|
-
try {
|
|
9278
|
-
await agentSessionManager.recordEvent(
|
|
9279
|
-
this.getStreamRequestId(),
|
|
9280
|
-
"agent_reasoning",
|
|
9281
|
-
this.config.id,
|
|
9282
|
-
{
|
|
9283
|
-
parts: [{ type: "text", content: last.text }]
|
|
9284
|
-
}
|
|
9285
|
-
);
|
|
9286
|
-
} catch (error) {
|
|
9287
|
-
logger19.debug({ error }, "Failed to track agent reasoning");
|
|
9288
|
-
}
|
|
9289
|
-
}
|
|
9290
|
-
if (steps.length >= 2) {
|
|
9291
|
-
const previousStep = steps[steps.length - 2];
|
|
9292
|
-
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
9293
|
-
const hasTransferCall = previousStep.toolCalls.some(
|
|
9294
|
-
(tc) => tc.toolName.startsWith("transfer_to_")
|
|
9295
|
-
);
|
|
9296
|
-
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
9297
|
-
return true;
|
|
9298
|
-
}
|
|
9299
|
-
}
|
|
9181
|
+
attributes: {
|
|
9182
|
+
"subAgent.name": this.config.name,
|
|
9183
|
+
"session.id": sessionId
|
|
9300
9184
|
}
|
|
9301
|
-
return steps.length >= this.getMaxGenerationSteps();
|
|
9302
9185
|
},
|
|
9303
|
-
|
|
9304
|
-
|
|
9305
|
-
|
|
9306
|
-
|
|
9307
|
-
|
|
9308
|
-
|
|
9309
|
-
|
|
9310
|
-
|
|
9311
|
-
|
|
9312
|
-
|
|
9313
|
-
|
|
9314
|
-
|
|
9315
|
-
|
|
9316
|
-
|
|
9317
|
-
|
|
9318
|
-
|
|
9319
|
-
|
|
9320
|
-
|
|
9321
|
-
|
|
9322
|
-
|
|
9323
|
-
|
|
9324
|
-
|
|
9325
|
-
streamHelper,
|
|
9326
|
-
this.config.tenantId,
|
|
9327
|
-
contextId,
|
|
9328
|
-
artifactParserOptions
|
|
9186
|
+
async (childSpan) => {
|
|
9187
|
+
try {
|
|
9188
|
+
const result = await Promise.all([
|
|
9189
|
+
this.getMcpTools(sessionId, streamRequestId),
|
|
9190
|
+
this.buildSystemPrompt(runtimeContext, false),
|
|
9191
|
+
// Normal prompt with data components
|
|
9192
|
+
this.buildSystemPrompt(runtimeContext, true),
|
|
9193
|
+
// Thinking prompt without data components
|
|
9194
|
+
this.getFunctionTools(sessionId, streamRequestId),
|
|
9195
|
+
Promise.resolve(this.getRelationTools(runtimeContext, sessionId)),
|
|
9196
|
+
this.getDefaultTools(streamRequestId)
|
|
9197
|
+
]);
|
|
9198
|
+
childSpan.setStatus({ code: api.SpanStatusCode.OK });
|
|
9199
|
+
return result;
|
|
9200
|
+
} catch (err) {
|
|
9201
|
+
const errorObj = err instanceof Error ? err : new Error(String(err));
|
|
9202
|
+
agentsCore.setSpanWithError(childSpan, errorObj);
|
|
9203
|
+
throw err;
|
|
9204
|
+
} finally {
|
|
9205
|
+
childSpan.end();
|
|
9206
|
+
}
|
|
9207
|
+
}
|
|
9329
9208
|
);
|
|
9330
|
-
|
|
9331
|
-
|
|
9332
|
-
|
|
9333
|
-
|
|
9334
|
-
|
|
9335
|
-
|
|
9336
|
-
|
|
9337
|
-
|
|
9338
|
-
|
|
9339
|
-
|
|
9340
|
-
|
|
9341
|
-
|
|
9342
|
-
|
|
9343
|
-
|
|
9344
|
-
|
|
9345
|
-
|
|
9346
|
-
|
|
9347
|
-
|
|
9348
|
-
|
|
9349
|
-
|
|
9350
|
-
|
|
9351
|
-
|
|
9209
|
+
const allTools = {
|
|
9210
|
+
...mcpTools,
|
|
9211
|
+
...functionTools,
|
|
9212
|
+
...relationTools,
|
|
9213
|
+
...defaultTools
|
|
9214
|
+
};
|
|
9215
|
+
const sanitizedTools = this.sanitizeToolsForAISDK(allTools);
|
|
9216
|
+
let conversationHistory = "";
|
|
9217
|
+
const historyConfig = this.config.conversationHistoryConfig ?? createDefaultConversationHistoryConfig();
|
|
9218
|
+
if (historyConfig && historyConfig.mode !== "none") {
|
|
9219
|
+
if (historyConfig.mode === "full") {
|
|
9220
|
+
conversationHistory = await getFormattedConversationHistory({
|
|
9221
|
+
tenantId: this.config.tenantId,
|
|
9222
|
+
projectId: this.config.projectId,
|
|
9223
|
+
conversationId: contextId,
|
|
9224
|
+
currentMessage: userMessage,
|
|
9225
|
+
options: historyConfig,
|
|
9226
|
+
filters: {}
|
|
9227
|
+
});
|
|
9228
|
+
} else if (historyConfig.mode === "scoped") {
|
|
9229
|
+
conversationHistory = await getFormattedConversationHistory({
|
|
9230
|
+
tenantId: this.config.tenantId,
|
|
9231
|
+
projectId: this.config.projectId,
|
|
9232
|
+
conversationId: contextId,
|
|
9233
|
+
currentMessage: userMessage,
|
|
9234
|
+
options: historyConfig,
|
|
9235
|
+
filters: {
|
|
9236
|
+
subAgentId: this.config.id,
|
|
9237
|
+
taskId
|
|
9352
9238
|
}
|
|
9239
|
+
});
|
|
9353
9240
|
}
|
|
9354
9241
|
}
|
|
9355
|
-
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
9359
|
-
|
|
9360
|
-
|
|
9361
|
-
|
|
9362
|
-
|
|
9363
|
-
|
|
9364
|
-
|
|
9365
|
-
|
|
9242
|
+
const primaryModelSettings = this.getPrimaryModel();
|
|
9243
|
+
const modelSettings = ModelFactory.prepareGenerationConfig(primaryModelSettings);
|
|
9244
|
+
let response;
|
|
9245
|
+
let textResponse;
|
|
9246
|
+
const hasStructuredOutput = this.config.dataComponents && this.config.dataComponents.length > 0;
|
|
9247
|
+
const shouldStreamPhase1 = this.getStreamingHelper() && !hasStructuredOutput;
|
|
9248
|
+
const MAX_ALLOWED_TIMEOUT_MS = 6e5;
|
|
9249
|
+
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;
|
|
9250
|
+
const timeoutMs = Math.min(configuredTimeout, MAX_ALLOWED_TIMEOUT_MS);
|
|
9251
|
+
if (modelSettings.maxDuration && modelSettings.maxDuration * 1e3 > MAX_ALLOWED_TIMEOUT_MS) {
|
|
9252
|
+
logger19.warn(
|
|
9253
|
+
{
|
|
9254
|
+
requestedTimeout: modelSettings.maxDuration * 1e3,
|
|
9255
|
+
appliedTimeout: timeoutMs,
|
|
9256
|
+
maxAllowed: MAX_ALLOWED_TIMEOUT_MS
|
|
9257
|
+
},
|
|
9258
|
+
"Requested timeout exceeded maximum allowed, capping to 10 minutes"
|
|
9259
|
+
);
|
|
9366
9260
|
}
|
|
9367
|
-
const
|
|
9368
|
-
|
|
9369
|
-
|
|
9370
|
-
|
|
9371
|
-
|
|
9372
|
-
...part.kind === "text" && { text: part.text },
|
|
9373
|
-
...part.kind === "data" && { data: part.data }
|
|
9374
|
-
}))
|
|
9375
|
-
};
|
|
9261
|
+
const phase1SystemPrompt = hasStructuredOutput ? thinkingSystemPrompt : systemPrompt;
|
|
9262
|
+
const messages = [];
|
|
9263
|
+
messages.push({ role: "system", content: phase1SystemPrompt });
|
|
9264
|
+
if (conversationHistory.trim() !== "") {
|
|
9265
|
+
messages.push({ role: "user", content: conversationHistory });
|
|
9376
9266
|
}
|
|
9377
|
-
|
|
9378
|
-
|
|
9379
|
-
|
|
9380
|
-
|
|
9381
|
-
|
|
9382
|
-
|
|
9383
|
-
// Force tool usage, prevent text generation
|
|
9384
|
-
};
|
|
9385
|
-
} else {
|
|
9386
|
-
genConfig = {
|
|
9267
|
+
messages.push({
|
|
9268
|
+
role: "user",
|
|
9269
|
+
content: userMessage
|
|
9270
|
+
});
|
|
9271
|
+
if (shouldStreamPhase1) {
|
|
9272
|
+
const streamConfig = {
|
|
9387
9273
|
...modelSettings,
|
|
9388
9274
|
toolChoice: "auto"
|
|
9389
|
-
// Allow
|
|
9275
|
+
// Allow natural text + tools
|
|
9390
9276
|
};
|
|
9391
|
-
|
|
9392
|
-
|
|
9393
|
-
|
|
9394
|
-
|
|
9395
|
-
|
|
9396
|
-
|
|
9397
|
-
|
|
9398
|
-
|
|
9399
|
-
|
|
9400
|
-
|
|
9401
|
-
|
|
9402
|
-
|
|
9403
|
-
|
|
9404
|
-
|
|
9405
|
-
|
|
9406
|
-
|
|
9407
|
-
)
|
|
9408
|
-
|
|
9409
|
-
|
|
9277
|
+
const streamResult = ai.streamText({
|
|
9278
|
+
...streamConfig,
|
|
9279
|
+
messages,
|
|
9280
|
+
tools: sanitizedTools,
|
|
9281
|
+
stopWhen: async ({ steps }) => {
|
|
9282
|
+
const last = steps.at(-1);
|
|
9283
|
+
if (last && "text" in last && last.text) {
|
|
9284
|
+
try {
|
|
9285
|
+
await agentSessionManager.recordEvent(
|
|
9286
|
+
this.getStreamRequestId(),
|
|
9287
|
+
"agent_reasoning",
|
|
9288
|
+
this.config.id,
|
|
9289
|
+
{
|
|
9290
|
+
parts: [{ type: "text", content: last.text }]
|
|
9291
|
+
}
|
|
9292
|
+
);
|
|
9293
|
+
} catch (error) {
|
|
9294
|
+
logger19.debug({ error }, "Failed to track agent reasoning");
|
|
9295
|
+
}
|
|
9410
9296
|
}
|
|
9411
|
-
|
|
9412
|
-
|
|
9413
|
-
|
|
9414
|
-
|
|
9415
|
-
|
|
9416
|
-
|
|
9417
|
-
|
|
9418
|
-
|
|
9419
|
-
|
|
9297
|
+
if (steps.length >= 2) {
|
|
9298
|
+
const previousStep = steps[steps.length - 2];
|
|
9299
|
+
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
9300
|
+
const hasTransferCall = previousStep.toolCalls.some(
|
|
9301
|
+
(tc) => tc.toolName.startsWith("transfer_to_")
|
|
9302
|
+
);
|
|
9303
|
+
if (hasTransferCall && "toolResults" in previousStep && previousStep.toolResults) {
|
|
9304
|
+
return true;
|
|
9305
|
+
}
|
|
9420
9306
|
}
|
|
9421
9307
|
}
|
|
9308
|
+
return steps.length >= this.getMaxGenerationSteps();
|
|
9309
|
+
},
|
|
9310
|
+
experimental_telemetry: {
|
|
9311
|
+
isEnabled: true,
|
|
9312
|
+
functionId: this.config.id,
|
|
9313
|
+
recordInputs: true,
|
|
9314
|
+
recordOutputs: true
|
|
9315
|
+
},
|
|
9316
|
+
abortSignal: AbortSignal.timeout(timeoutMs)
|
|
9317
|
+
});
|
|
9318
|
+
const streamHelper = this.getStreamingHelper();
|
|
9319
|
+
if (!streamHelper) {
|
|
9320
|
+
throw new Error("Stream helper is unexpectedly undefined in streaming context");
|
|
9321
|
+
}
|
|
9322
|
+
const session = toolSessionManager.getSession(sessionId);
|
|
9323
|
+
const artifactParserOptions = {
|
|
9324
|
+
sessionId,
|
|
9325
|
+
taskId: session?.taskId,
|
|
9326
|
+
projectId: session?.projectId,
|
|
9327
|
+
artifactComponents: this.artifactComponents,
|
|
9328
|
+
streamRequestId: this.getStreamRequestId(),
|
|
9329
|
+
subAgentId: this.config.id
|
|
9330
|
+
};
|
|
9331
|
+
const parser = new IncrementalStreamParser(
|
|
9332
|
+
streamHelper,
|
|
9333
|
+
this.config.tenantId,
|
|
9334
|
+
contextId,
|
|
9335
|
+
artifactParserOptions
|
|
9336
|
+
);
|
|
9337
|
+
for await (const event of streamResult.fullStream) {
|
|
9338
|
+
switch (event.type) {
|
|
9339
|
+
case "text-delta":
|
|
9340
|
+
await parser.processTextChunk(event.text);
|
|
9341
|
+
break;
|
|
9342
|
+
case "tool-call":
|
|
9343
|
+
parser.markToolResult();
|
|
9344
|
+
break;
|
|
9345
|
+
case "tool-result":
|
|
9346
|
+
parser.markToolResult();
|
|
9347
|
+
break;
|
|
9348
|
+
case "finish":
|
|
9349
|
+
if (event.finishReason === "tool-calls") {
|
|
9350
|
+
parser.markToolResult();
|
|
9351
|
+
}
|
|
9352
|
+
break;
|
|
9353
|
+
case "error":
|
|
9354
|
+
if (event.error instanceof Error) {
|
|
9355
|
+
throw event.error;
|
|
9356
|
+
} else {
|
|
9357
|
+
const errorMessage = event.error?.error?.message;
|
|
9358
|
+
throw new Error(errorMessage);
|
|
9359
|
+
}
|
|
9422
9360
|
}
|
|
9423
|
-
|
|
9424
|
-
|
|
9425
|
-
|
|
9426
|
-
|
|
9427
|
-
|
|
9428
|
-
|
|
9429
|
-
|
|
9430
|
-
|
|
9431
|
-
|
|
9432
|
-
|
|
9433
|
-
|
|
9434
|
-
|
|
9435
|
-
|
|
9436
|
-
|
|
9437
|
-
|
|
9438
|
-
|
|
9439
|
-
|
|
9440
|
-
|
|
9441
|
-
|
|
9442
|
-
|
|
9443
|
-
|
|
9444
|
-
|
|
9445
|
-
|
|
9446
|
-
|
|
9447
|
-
|
|
9448
|
-
|
|
9449
|
-
|
|
9450
|
-
|
|
9451
|
-
|
|
9452
|
-
|
|
9453
|
-
|
|
9454
|
-
|
|
9455
|
-
|
|
9456
|
-
|
|
9457
|
-
|
|
9361
|
+
}
|
|
9362
|
+
await parser.finalize();
|
|
9363
|
+
response = await streamResult;
|
|
9364
|
+
const collectedParts = parser.getCollectedParts();
|
|
9365
|
+
if (collectedParts.length > 0) {
|
|
9366
|
+
response.formattedContent = {
|
|
9367
|
+
parts: collectedParts.map((part) => ({
|
|
9368
|
+
kind: part.kind,
|
|
9369
|
+
...part.kind === "text" && { text: part.text },
|
|
9370
|
+
...part.kind === "data" && { data: part.data }
|
|
9371
|
+
}))
|
|
9372
|
+
};
|
|
9373
|
+
}
|
|
9374
|
+
const streamedContent = parser.getAllStreamedContent();
|
|
9375
|
+
if (streamedContent.length > 0) {
|
|
9376
|
+
response.streamedContent = {
|
|
9377
|
+
parts: streamedContent.map((part) => ({
|
|
9378
|
+
kind: part.kind,
|
|
9379
|
+
...part.kind === "text" && { text: part.text },
|
|
9380
|
+
...part.kind === "data" && { data: part.data }
|
|
9381
|
+
}))
|
|
9382
|
+
};
|
|
9383
|
+
}
|
|
9384
|
+
} else {
|
|
9385
|
+
let genConfig;
|
|
9386
|
+
if (hasStructuredOutput) {
|
|
9387
|
+
genConfig = {
|
|
9388
|
+
...modelSettings,
|
|
9389
|
+
toolChoice: "required"
|
|
9390
|
+
// Force tool usage, prevent text generation
|
|
9391
|
+
};
|
|
9392
|
+
} else {
|
|
9393
|
+
genConfig = {
|
|
9394
|
+
...modelSettings,
|
|
9395
|
+
toolChoice: "auto"
|
|
9396
|
+
// Allow both tools and text generation
|
|
9397
|
+
};
|
|
9398
|
+
}
|
|
9399
|
+
response = await ai.generateText({
|
|
9400
|
+
...genConfig,
|
|
9401
|
+
messages,
|
|
9402
|
+
tools: sanitizedTools,
|
|
9403
|
+
stopWhen: async ({ steps }) => {
|
|
9404
|
+
const last = steps.at(-1);
|
|
9405
|
+
if (last && "text" in last && last.text) {
|
|
9406
|
+
try {
|
|
9407
|
+
await agentSessionManager.recordEvent(
|
|
9408
|
+
this.getStreamRequestId(),
|
|
9409
|
+
"agent_reasoning",
|
|
9410
|
+
this.config.id,
|
|
9411
|
+
{
|
|
9412
|
+
parts: [{ type: "text", content: last.text }]
|
|
9458
9413
|
}
|
|
9459
|
-
|
|
9460
|
-
|
|
9461
|
-
|
|
9462
|
-
|
|
9463
|
-
|
|
9464
|
-
|
|
9465
|
-
|
|
9466
|
-
|
|
9467
|
-
|
|
9468
|
-
|
|
9469
|
-
|
|
9470
|
-
|
|
9471
|
-
|
|
9414
|
+
);
|
|
9415
|
+
} catch (error) {
|
|
9416
|
+
logger19.debug({ error }, "Failed to track agent reasoning");
|
|
9417
|
+
}
|
|
9418
|
+
}
|
|
9419
|
+
if (steps.length >= 2) {
|
|
9420
|
+
const previousStep = steps[steps.length - 2];
|
|
9421
|
+
if (previousStep && "toolCalls" in previousStep && previousStep.toolCalls) {
|
|
9422
|
+
const hasStopTool = previousStep.toolCalls.some(
|
|
9423
|
+
(tc) => tc.toolName.startsWith("transfer_to_") || tc.toolName === "thinking_complete"
|
|
9424
|
+
);
|
|
9425
|
+
if (hasStopTool && "toolResults" in previousStep && previousStep.toolResults) {
|
|
9426
|
+
return true;
|
|
9427
|
+
}
|
|
9428
|
+
}
|
|
9429
|
+
}
|
|
9430
|
+
return steps.length >= this.getMaxGenerationSteps();
|
|
9431
|
+
},
|
|
9432
|
+
experimental_telemetry: {
|
|
9433
|
+
isEnabled: true,
|
|
9434
|
+
functionId: this.config.id,
|
|
9435
|
+
recordInputs: true,
|
|
9436
|
+
recordOutputs: true,
|
|
9437
|
+
metadata: {
|
|
9438
|
+
phase: "planning"
|
|
9439
|
+
}
|
|
9440
|
+
},
|
|
9441
|
+
abortSignal: AbortSignal.timeout(timeoutMs)
|
|
9442
|
+
});
|
|
9443
|
+
}
|
|
9444
|
+
if (response.steps) {
|
|
9445
|
+
const resolvedSteps = await response.steps;
|
|
9446
|
+
response = { ...response, steps: resolvedSteps };
|
|
9447
|
+
}
|
|
9448
|
+
if (hasStructuredOutput && !hasToolCallWithPrefix("transfer_to_")(response)) {
|
|
9449
|
+
const thinkingCompleteCall = response.steps?.flatMap((s) => s.toolCalls || [])?.find((tc) => tc.toolName === "thinking_complete");
|
|
9450
|
+
if (thinkingCompleteCall) {
|
|
9451
|
+
const reasoningFlow = [];
|
|
9452
|
+
if (response.steps) {
|
|
9453
|
+
response.steps.forEach((step) => {
|
|
9454
|
+
if (step.toolCalls && step.toolResults) {
|
|
9455
|
+
step.toolCalls.forEach((call, index) => {
|
|
9456
|
+
const result = step.toolResults[index];
|
|
9457
|
+
if (result) {
|
|
9458
|
+
const storedResult = toolSessionManager.getToolResult(
|
|
9459
|
+
sessionId,
|
|
9460
|
+
result.toolCallId
|
|
9461
|
+
);
|
|
9462
|
+
const toolName = storedResult?.toolName || call.toolName;
|
|
9463
|
+
if (toolName === "thinking_complete") {
|
|
9464
|
+
return;
|
|
9465
|
+
}
|
|
9466
|
+
const actualResult = storedResult?.result || result.result || result;
|
|
9467
|
+
const actualArgs = storedResult?.args || call.args;
|
|
9468
|
+
const cleanResult = actualResult && typeof actualResult === "object" && !Array.isArray(actualResult) ? Object.fromEntries(
|
|
9469
|
+
Object.entries(actualResult).filter(
|
|
9470
|
+
([key]) => key !== "_structureHints"
|
|
9471
|
+
)
|
|
9472
|
+
) : actualResult;
|
|
9473
|
+
const input = actualArgs ? JSON.stringify(actualArgs, null, 2) : "No input";
|
|
9474
|
+
const output = typeof cleanResult === "string" ? cleanResult : JSON.stringify(cleanResult, null, 2);
|
|
9475
|
+
let structureHintsFormatted = "";
|
|
9476
|
+
if (actualResult?._structureHints && this.artifactComponents && this.artifactComponents.length > 0) {
|
|
9477
|
+
const hints = actualResult._structureHints;
|
|
9478
|
+
structureHintsFormatted = `
|
|
9472
9479
|
### \u{1F4CA} Structure Hints for Artifact Creation
|
|
9473
9480
|
|
|
9474
9481
|
**Terminal Field Paths (${hints.terminalPaths?.length || 0} found):**
|
|
@@ -9492,8 +9499,8 @@ ${hints.commonFields?.map((field) => ` \u2022 ${field}`).join("\n") || " None
|
|
|
9492
9499
|
|
|
9493
9500
|
**Forbidden Syntax:** ${hints.forbiddenSyntax || "Use these paths for artifact base selectors."}
|
|
9494
9501
|
`;
|
|
9495
|
-
|
|
9496
|
-
|
|
9502
|
+
}
|
|
9503
|
+
const formattedResult = `## Tool: ${call.toolName}
|
|
9497
9504
|
|
|
9498
9505
|
### \u{1F527} TOOL_CALL_ID: ${result.toolCallId}
|
|
9499
9506
|
|
|
@@ -9502,130 +9509,61 @@ ${input}
|
|
|
9502
9509
|
|
|
9503
9510
|
### Output
|
|
9504
9511
|
${output}${structureHintsFormatted}`;
|
|
9505
|
-
|
|
9506
|
-
|
|
9507
|
-
|
|
9508
|
-
|
|
9509
|
-
|
|
9510
|
-
|
|
9511
|
-
}
|
|
9512
|
-
});
|
|
9513
|
-
}
|
|
9514
|
-
const componentSchemas = [];
|
|
9515
|
-
if (this.config.dataComponents && this.config.dataComponents.length > 0) {
|
|
9516
|
-
this.config.dataComponents.forEach((dc) => {
|
|
9517
|
-
const propsSchema = jsonSchemaToZod(dc.props);
|
|
9518
|
-
componentSchemas.push(
|
|
9519
|
-
z6.z.object({
|
|
9520
|
-
id: z6.z.string(),
|
|
9521
|
-
name: z6.z.literal(dc.name),
|
|
9522
|
-
props: propsSchema
|
|
9523
|
-
})
|
|
9524
|
-
);
|
|
9525
|
-
});
|
|
9526
|
-
}
|
|
9527
|
-
if (this.artifactComponents.length > 0) {
|
|
9528
|
-
const artifactCreateSchemas = ArtifactCreateSchema.getSchemas(
|
|
9529
|
-
this.artifactComponents
|
|
9530
|
-
);
|
|
9531
|
-
componentSchemas.push(...artifactCreateSchemas);
|
|
9532
|
-
componentSchemas.push(ArtifactReferenceSchema.getSchema());
|
|
9533
|
-
}
|
|
9534
|
-
let dataComponentsSchema;
|
|
9535
|
-
if (componentSchemas.length === 1) {
|
|
9536
|
-
dataComponentsSchema = componentSchemas[0];
|
|
9537
|
-
} else {
|
|
9538
|
-
dataComponentsSchema = z6.z.union(
|
|
9539
|
-
componentSchemas
|
|
9540
|
-
);
|
|
9541
|
-
}
|
|
9542
|
-
const structuredModelSettings = ModelFactory.prepareGenerationConfig(
|
|
9543
|
-
this.getStructuredOutputModel()
|
|
9544
|
-
);
|
|
9545
|
-
const phase2TimeoutMs = structuredModelSettings.maxDuration ? structuredModelSettings.maxDuration * 1e3 : CONSTANTS.PHASE_2_TIMEOUT_MS;
|
|
9546
|
-
const shouldStreamPhase2 = this.getStreamingHelper();
|
|
9547
|
-
if (shouldStreamPhase2) {
|
|
9548
|
-
const phase2Messages = [
|
|
9549
|
-
{
|
|
9550
|
-
role: "system",
|
|
9551
|
-
content: await this.buildPhase2SystemPrompt(runtimeContext)
|
|
9552
|
-
}
|
|
9553
|
-
];
|
|
9554
|
-
if (conversationHistory.trim() !== "") {
|
|
9555
|
-
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
9556
|
-
}
|
|
9557
|
-
phase2Messages.push({ role: "user", content: userMessage });
|
|
9558
|
-
phase2Messages.push(...reasoningFlow);
|
|
9559
|
-
const streamResult = ai.streamObject({
|
|
9560
|
-
...structuredModelSettings,
|
|
9561
|
-
messages: phase2Messages,
|
|
9562
|
-
schema: z6.z.object({
|
|
9563
|
-
dataComponents: z6.z.array(dataComponentsSchema)
|
|
9564
|
-
}),
|
|
9565
|
-
experimental_telemetry: {
|
|
9566
|
-
isEnabled: true,
|
|
9567
|
-
functionId: this.config.id,
|
|
9568
|
-
recordInputs: true,
|
|
9569
|
-
recordOutputs: true,
|
|
9570
|
-
metadata: {
|
|
9571
|
-
phase: "structured_generation"
|
|
9512
|
+
reasoningFlow.push({
|
|
9513
|
+
role: "assistant",
|
|
9514
|
+
content: formattedResult
|
|
9515
|
+
});
|
|
9516
|
+
}
|
|
9517
|
+
});
|
|
9572
9518
|
}
|
|
9573
|
-
}
|
|
9574
|
-
abortSignal: AbortSignal.timeout(phase2TimeoutMs)
|
|
9575
|
-
});
|
|
9576
|
-
const streamHelper = this.getStreamingHelper();
|
|
9577
|
-
if (!streamHelper) {
|
|
9578
|
-
throw new Error("Stream helper is unexpectedly undefined in streaming context");
|
|
9519
|
+
});
|
|
9579
9520
|
}
|
|
9580
|
-
const
|
|
9581
|
-
|
|
9582
|
-
|
|
9583
|
-
|
|
9584
|
-
|
|
9585
|
-
|
|
9586
|
-
|
|
9587
|
-
|
|
9588
|
-
|
|
9589
|
-
|
|
9590
|
-
|
|
9591
|
-
|
|
9592
|
-
contextId,
|
|
9593
|
-
artifactParserOptions
|
|
9594
|
-
);
|
|
9595
|
-
for await (const delta of streamResult.partialObjectStream) {
|
|
9596
|
-
if (delta) {
|
|
9597
|
-
await parser.processObjectDelta(delta);
|
|
9598
|
-
}
|
|
9521
|
+
const componentSchemas = [];
|
|
9522
|
+
if (this.config.dataComponents && this.config.dataComponents.length > 0) {
|
|
9523
|
+
this.config.dataComponents.forEach((dc) => {
|
|
9524
|
+
const propsSchema = jsonSchemaToZod(dc.props);
|
|
9525
|
+
componentSchemas.push(
|
|
9526
|
+
z6.z.object({
|
|
9527
|
+
id: z6.z.string(),
|
|
9528
|
+
name: z6.z.literal(dc.name),
|
|
9529
|
+
props: propsSchema
|
|
9530
|
+
})
|
|
9531
|
+
);
|
|
9532
|
+
});
|
|
9599
9533
|
}
|
|
9600
|
-
|
|
9601
|
-
|
|
9602
|
-
|
|
9603
|
-
|
|
9604
|
-
|
|
9605
|
-
|
|
9606
|
-
kind: part.kind,
|
|
9607
|
-
...part.kind === "text" && { text: part.text },
|
|
9608
|
-
...part.kind === "data" && { data: part.data }
|
|
9609
|
-
}))
|
|
9610
|
-
};
|
|
9534
|
+
if (this.artifactComponents.length > 0) {
|
|
9535
|
+
const artifactCreateSchemas = ArtifactCreateSchema.getSchemas(
|
|
9536
|
+
this.artifactComponents
|
|
9537
|
+
);
|
|
9538
|
+
componentSchemas.push(...artifactCreateSchemas);
|
|
9539
|
+
componentSchemas.push(ArtifactReferenceSchema.getSchema());
|
|
9611
9540
|
}
|
|
9612
|
-
|
|
9613
|
-
|
|
9614
|
-
|
|
9615
|
-
}
|
|
9616
|
-
|
|
9617
|
-
|
|
9618
|
-
|
|
9619
|
-
const phase2Messages = [
|
|
9620
|
-
{ role: "system", content: await this.buildPhase2SystemPrompt(runtimeContext) }
|
|
9621
|
-
];
|
|
9622
|
-
if (conversationHistory.trim() !== "") {
|
|
9623
|
-
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
9541
|
+
let dataComponentsSchema;
|
|
9542
|
+
if (componentSchemas.length === 1) {
|
|
9543
|
+
dataComponentsSchema = componentSchemas[0];
|
|
9544
|
+
} else {
|
|
9545
|
+
dataComponentsSchema = z6.z.union(
|
|
9546
|
+
componentSchemas
|
|
9547
|
+
);
|
|
9624
9548
|
}
|
|
9625
|
-
|
|
9626
|
-
|
|
9627
|
-
|
|
9628
|
-
|
|
9549
|
+
const structuredModelSettings = ModelFactory.prepareGenerationConfig(
|
|
9550
|
+
this.getStructuredOutputModel()
|
|
9551
|
+
);
|
|
9552
|
+
const phase2TimeoutMs = structuredModelSettings.maxDuration ? structuredModelSettings.maxDuration * 1e3 : CONSTANTS.PHASE_2_TIMEOUT_MS;
|
|
9553
|
+
const shouldStreamPhase2 = this.getStreamingHelper();
|
|
9554
|
+
if (shouldStreamPhase2) {
|
|
9555
|
+
const phase2Messages = [
|
|
9556
|
+
{
|
|
9557
|
+
role: "system",
|
|
9558
|
+
content: await this.buildPhase2SystemPrompt(runtimeContext)
|
|
9559
|
+
}
|
|
9560
|
+
];
|
|
9561
|
+
if (conversationHistory.trim() !== "") {
|
|
9562
|
+
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
9563
|
+
}
|
|
9564
|
+
phase2Messages.push({ role: "user", content: userMessage });
|
|
9565
|
+
phase2Messages.push(...reasoningFlow);
|
|
9566
|
+
const streamResult = ai.streamObject({
|
|
9629
9567
|
...structuredModelSettings,
|
|
9630
9568
|
messages: phase2Messages,
|
|
9631
9569
|
schema: z6.z.object({
|
|
@@ -9641,65 +9579,135 @@ ${output}${structureHintsFormatted}`;
|
|
|
9641
9579
|
}
|
|
9642
9580
|
},
|
|
9643
9581
|
abortSignal: AbortSignal.timeout(phase2TimeoutMs)
|
|
9644
|
-
})
|
|
9645
|
-
|
|
9646
|
-
|
|
9647
|
-
|
|
9648
|
-
|
|
9649
|
-
|
|
9650
|
-
|
|
9582
|
+
});
|
|
9583
|
+
const streamHelper = this.getStreamingHelper();
|
|
9584
|
+
if (!streamHelper) {
|
|
9585
|
+
throw new Error("Stream helper is unexpectedly undefined in streaming context");
|
|
9586
|
+
}
|
|
9587
|
+
const session = toolSessionManager.getSession(sessionId);
|
|
9588
|
+
const artifactParserOptions = {
|
|
9589
|
+
sessionId,
|
|
9590
|
+
taskId: session?.taskId,
|
|
9591
|
+
projectId: session?.projectId,
|
|
9592
|
+
artifactComponents: this.artifactComponents,
|
|
9593
|
+
streamRequestId: this.getStreamRequestId(),
|
|
9594
|
+
subAgentId: this.config.id
|
|
9595
|
+
};
|
|
9596
|
+
const parser = new IncrementalStreamParser(
|
|
9597
|
+
streamHelper,
|
|
9598
|
+
this.config.tenantId,
|
|
9599
|
+
contextId,
|
|
9600
|
+
artifactParserOptions
|
|
9601
|
+
);
|
|
9602
|
+
for await (const delta of streamResult.partialObjectStream) {
|
|
9603
|
+
if (delta) {
|
|
9604
|
+
await parser.processObjectDelta(delta);
|
|
9605
|
+
}
|
|
9606
|
+
}
|
|
9607
|
+
await parser.finalize();
|
|
9608
|
+
const structuredResponse = await streamResult;
|
|
9609
|
+
const collectedParts = parser.getCollectedParts();
|
|
9610
|
+
if (collectedParts.length > 0) {
|
|
9611
|
+
response.formattedContent = {
|
|
9612
|
+
parts: collectedParts.map((part) => ({
|
|
9613
|
+
kind: part.kind,
|
|
9614
|
+
...part.kind === "text" && { text: part.text },
|
|
9615
|
+
...part.kind === "data" && { data: part.data }
|
|
9616
|
+
}))
|
|
9617
|
+
};
|
|
9618
|
+
}
|
|
9619
|
+
response = {
|
|
9620
|
+
...response,
|
|
9621
|
+
object: structuredResponse.object
|
|
9622
|
+
};
|
|
9623
|
+
textResponse = JSON.stringify(structuredResponse.object, null, 2);
|
|
9624
|
+
} else {
|
|
9625
|
+
const { withJsonPostProcessing: withJsonPostProcessing2 } = await Promise.resolve().then(() => (init_json_postprocessor(), json_postprocessor_exports));
|
|
9626
|
+
const phase2Messages = [
|
|
9627
|
+
{ role: "system", content: await this.buildPhase2SystemPrompt(runtimeContext) }
|
|
9628
|
+
];
|
|
9629
|
+
if (conversationHistory.trim() !== "") {
|
|
9630
|
+
phase2Messages.push({ role: "user", content: conversationHistory });
|
|
9631
|
+
}
|
|
9632
|
+
phase2Messages.push({ role: "user", content: userMessage });
|
|
9633
|
+
phase2Messages.push(...reasoningFlow);
|
|
9634
|
+
const structuredResponse = await ai.generateObject(
|
|
9635
|
+
withJsonPostProcessing2({
|
|
9636
|
+
...structuredModelSettings,
|
|
9637
|
+
messages: phase2Messages,
|
|
9638
|
+
schema: z6.z.object({
|
|
9639
|
+
dataComponents: z6.z.array(dataComponentsSchema)
|
|
9640
|
+
}),
|
|
9641
|
+
experimental_telemetry: {
|
|
9642
|
+
isEnabled: true,
|
|
9643
|
+
functionId: this.config.id,
|
|
9644
|
+
recordInputs: true,
|
|
9645
|
+
recordOutputs: true,
|
|
9646
|
+
metadata: {
|
|
9647
|
+
phase: "structured_generation"
|
|
9648
|
+
}
|
|
9649
|
+
},
|
|
9650
|
+
abortSignal: AbortSignal.timeout(phase2TimeoutMs)
|
|
9651
|
+
})
|
|
9652
|
+
);
|
|
9653
|
+
response = {
|
|
9654
|
+
...response,
|
|
9655
|
+
object: structuredResponse.object
|
|
9656
|
+
};
|
|
9657
|
+
textResponse = JSON.stringify(structuredResponse.object, null, 2);
|
|
9658
|
+
}
|
|
9659
|
+
} else {
|
|
9660
|
+
textResponse = response.text || "";
|
|
9651
9661
|
}
|
|
9652
9662
|
} else {
|
|
9653
|
-
textResponse = response.text || "";
|
|
9663
|
+
textResponse = response.steps[response.steps.length - 1].text || "";
|
|
9654
9664
|
}
|
|
9655
|
-
|
|
9656
|
-
|
|
9657
|
-
|
|
9658
|
-
|
|
9659
|
-
|
|
9660
|
-
|
|
9661
|
-
|
|
9662
|
-
|
|
9663
|
-
|
|
9664
|
-
|
|
9665
|
-
|
|
9666
|
-
|
|
9667
|
-
|
|
9668
|
-
|
|
9669
|
-
|
|
9670
|
-
|
|
9671
|
-
|
|
9672
|
-
|
|
9673
|
-
|
|
9674
|
-
|
|
9675
|
-
contextId
|
|
9676
|
-
|
|
9677
|
-
} else if (textResponse) {
|
|
9678
|
-
formattedContent = await responseFormatter.formatResponse(textResponse, contextId);
|
|
9665
|
+
span.setStatus({ code: api.SpanStatusCode.OK });
|
|
9666
|
+
span.end();
|
|
9667
|
+
let formattedContent = response.formattedContent || null;
|
|
9668
|
+
if (!formattedContent) {
|
|
9669
|
+
const session = toolSessionManager.getSession(sessionId);
|
|
9670
|
+
const responseFormatter = new ResponseFormatter(this.config.tenantId, {
|
|
9671
|
+
sessionId,
|
|
9672
|
+
taskId: session?.taskId,
|
|
9673
|
+
projectId: session?.projectId,
|
|
9674
|
+
contextId,
|
|
9675
|
+
artifactComponents: this.artifactComponents,
|
|
9676
|
+
streamRequestId: this.getStreamRequestId(),
|
|
9677
|
+
subAgentId: this.config.id
|
|
9678
|
+
});
|
|
9679
|
+
if (response.object) {
|
|
9680
|
+
formattedContent = await responseFormatter.formatObjectResponse(
|
|
9681
|
+
response.object,
|
|
9682
|
+
contextId
|
|
9683
|
+
);
|
|
9684
|
+
} else if (textResponse) {
|
|
9685
|
+
formattedContent = await responseFormatter.formatResponse(textResponse, contextId);
|
|
9686
|
+
}
|
|
9679
9687
|
}
|
|
9688
|
+
const formattedResponse = {
|
|
9689
|
+
...response,
|
|
9690
|
+
formattedContent
|
|
9691
|
+
};
|
|
9692
|
+
if (streamRequestId) {
|
|
9693
|
+
const generationType = response.object ? "object_generation" : "text_generation";
|
|
9694
|
+
agentSessionManager.recordEvent(streamRequestId, "agent_generate", this.config.id, {
|
|
9695
|
+
parts: (formattedContent?.parts || []).map((part) => ({
|
|
9696
|
+
type: part.kind === "text" ? "text" : part.kind === "data" ? "tool_result" : "text",
|
|
9697
|
+
content: part.text || JSON.stringify(part.data)
|
|
9698
|
+
})),
|
|
9699
|
+
generationType
|
|
9700
|
+
});
|
|
9701
|
+
}
|
|
9702
|
+
return formattedResponse;
|
|
9703
|
+
} catch (error) {
|
|
9704
|
+
const errorToThrow = error instanceof Error ? error : new Error(String(error));
|
|
9705
|
+
agentsCore.setSpanWithError(span, errorToThrow);
|
|
9706
|
+
span.end();
|
|
9707
|
+
throw errorToThrow;
|
|
9680
9708
|
}
|
|
9681
|
-
const formattedResponse = {
|
|
9682
|
-
...response,
|
|
9683
|
-
formattedContent
|
|
9684
|
-
};
|
|
9685
|
-
if (streamRequestId) {
|
|
9686
|
-
const generationType = response.object ? "object_generation" : "text_generation";
|
|
9687
|
-
agentSessionManager.recordEvent(streamRequestId, "agent_generate", this.config.id, {
|
|
9688
|
-
parts: (formattedContent?.parts || []).map((part) => ({
|
|
9689
|
-
type: part.kind === "text" ? "text" : part.kind === "data" ? "tool_result" : "text",
|
|
9690
|
-
content: part.text || JSON.stringify(part.data)
|
|
9691
|
-
})),
|
|
9692
|
-
generationType
|
|
9693
|
-
});
|
|
9694
|
-
}
|
|
9695
|
-
return formattedResponse;
|
|
9696
|
-
} catch (error) {
|
|
9697
|
-
const errorToThrow = error instanceof Error ? error : new Error(String(error));
|
|
9698
|
-
agentsCore.setSpanWithError(span, errorToThrow);
|
|
9699
|
-
span.end();
|
|
9700
|
-
throw errorToThrow;
|
|
9701
9709
|
}
|
|
9702
|
-
|
|
9710
|
+
);
|
|
9703
9711
|
}
|
|
9704
9712
|
};
|
|
9705
9713
|
|
|
@@ -9990,7 +9998,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
9990
9998
|
},
|
|
9991
9999
|
artifacts: [
|
|
9992
10000
|
{
|
|
9993
|
-
artifactId:
|
|
10001
|
+
artifactId: agentsCore.generateId(),
|
|
9994
10002
|
parts: [
|
|
9995
10003
|
{
|
|
9996
10004
|
kind: "data",
|
|
@@ -10023,7 +10031,7 @@ var createTaskHandler = (config, credentialStoreRegistry) => {
|
|
|
10023
10031
|
status: { state: agentsCore.TaskState.Completed },
|
|
10024
10032
|
artifacts: [
|
|
10025
10033
|
{
|
|
10026
|
-
artifactId:
|
|
10034
|
+
artifactId: agentsCore.generateId(),
|
|
10027
10035
|
parts
|
|
10028
10036
|
}
|
|
10029
10037
|
]
|
|
@@ -11366,7 +11374,7 @@ var ExecutionHandler = class {
|
|
|
11366
11374
|
"ai.subAgent.name": currentAgentId
|
|
11367
11375
|
});
|
|
11368
11376
|
await agentsCore.createMessage(dbClient_default)({
|
|
11369
|
-
id:
|
|
11377
|
+
id: agentsCore.generateId(),
|
|
11370
11378
|
tenantId,
|
|
11371
11379
|
projectId,
|
|
11372
11380
|
conversationId,
|
|
@@ -11756,7 +11764,7 @@ app2.openapi(chatCompletionsRoute, async (c) => {
|
|
|
11756
11764
|
});
|
|
11757
11765
|
}
|
|
11758
11766
|
await agentsCore.createMessage(dbClient_default)({
|
|
11759
|
-
id:
|
|
11767
|
+
id: agentsCore.generateId(),
|
|
11760
11768
|
tenantId,
|
|
11761
11769
|
projectId,
|
|
11762
11770
|
conversationId,
|
|
@@ -11989,7 +11997,7 @@ app3.openapi(chatDataStreamRoute, async (c) => {
|
|
|
11989
11997
|
});
|
|
11990
11998
|
}
|
|
11991
11999
|
await agentsCore.createMessage(dbClient_default)({
|
|
11992
|
-
id:
|
|
12000
|
+
id: agentsCore.generateId(),
|
|
11993
12001
|
tenantId,
|
|
11994
12002
|
projectId,
|
|
11995
12003
|
conversationId,
|
|
@@ -12445,7 +12453,7 @@ var processUserMessage = async (tenantId, projectId, conversationId, query) => {
|
|
|
12445
12453
|
});
|
|
12446
12454
|
}
|
|
12447
12455
|
await agentsCore.createMessage(dbClient_default)({
|
|
12448
|
-
id:
|
|
12456
|
+
id: agentsCore.generateId(),
|
|
12449
12457
|
tenantId,
|
|
12450
12458
|
projectId,
|
|
12451
12459
|
conversationId,
|