@standardagents/builder 0.13.2 → 0.14.1

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.
@@ -1066,6 +1066,68 @@ function filterEmptyAssistantProviderMessages(messages) {
1066
1066
  return hasToolCalls || hasReasoning || hasContent;
1067
1067
  });
1068
1068
  }
1069
+ function buildSyntheticMissingToolResult(toolCall) {
1070
+ return {
1071
+ role: "tool",
1072
+ tool_call_id: toolCall.id,
1073
+ name: toolCall.function.name,
1074
+ toolName: toolCall.function.name,
1075
+ content: `Tool result missing from conversation history for prior call "${toolCall.function.name}". Treat this as a failed tool call.`
1076
+ };
1077
+ }
1078
+ function normalizeToolCallMessageSequence(messages) {
1079
+ const normalized = [];
1080
+ let pending = null;
1081
+ const flushPending = () => {
1082
+ if (!pending) {
1083
+ return;
1084
+ }
1085
+ normalized.push(pending.assistantMessage);
1086
+ for (const toolCall of pending.expectedToolCalls) {
1087
+ const matchedResult = pending.matchedResults.get(toolCall.id);
1088
+ normalized.push(matchedResult ?? buildSyntheticMissingToolResult(toolCall));
1089
+ }
1090
+ if (pending.deferredMessages.length > 0) {
1091
+ normalized.push(...normalizeToolCallMessageSequence(pending.deferredMessages));
1092
+ }
1093
+ pending = null;
1094
+ };
1095
+ for (const message of messages) {
1096
+ const isAssistantToolCallMessage = message.role === "assistant" && Array.isArray(message.tool_calls) && message.tool_calls.length > 0;
1097
+ if (!pending) {
1098
+ if (!isAssistantToolCallMessage) {
1099
+ normalized.push(message);
1100
+ continue;
1101
+ }
1102
+ const toolCalls = message.tool_calls ?? [];
1103
+ const expectedToolCalls = toolCalls.filter(
1104
+ (toolCall) => Boolean(toolCall && typeof toolCall.id === "string" && toolCall.function?.name)
1105
+ );
1106
+ if (expectedToolCalls.length === 0) {
1107
+ normalized.push(message);
1108
+ continue;
1109
+ }
1110
+ pending = {
1111
+ assistantMessage: message,
1112
+ expectedToolCalls,
1113
+ matchedResults: /* @__PURE__ */ new Map(),
1114
+ deferredMessages: []
1115
+ };
1116
+ continue;
1117
+ }
1118
+ const matchesPendingToolResult = message.role === "tool" && typeof message.tool_call_id === "string" && pending.expectedToolCalls.some((toolCall) => toolCall.id === message.tool_call_id) && !pending.matchedResults.has(message.tool_call_id);
1119
+ if (matchesPendingToolResult) {
1120
+ pending.matchedResults.set(message.tool_call_id, message);
1121
+ if (pending.matchedResults.size === pending.expectedToolCalls.length) {
1122
+ flushPending();
1123
+ }
1124
+ continue;
1125
+ }
1126
+ pending.deferredMessages.push(message);
1127
+ }
1128
+ flushPending();
1129
+ return normalized;
1130
+ }
1069
1131
  function stripBase64FromMessages(messages, imagePathMap) {
1070
1132
  return messages.map((msg) => {
1071
1133
  if (msg.role === "user" && Array.isArray(msg.content)) {
@@ -1209,7 +1271,8 @@ function transformToolChoice(toolChoice) {
1209
1271
  return void 0;
1210
1272
  }
1211
1273
  function buildRequestBody(context, modelDef) {
1212
- const transformedMessages = transformMessages(context.messages);
1274
+ const normalizedMessages = normalizeToolCallMessageSequence(context.messages);
1275
+ const transformedMessages = transformMessages(normalizedMessages);
1213
1276
  const supportsImageInput = modelSupportsImageInput(modelDef);
1214
1277
  const supportsToolCalls = modelSupportsToolCalls(modelDef);
1215
1278
  let requestMessages = transformedMessages;
@@ -2152,7 +2215,7 @@ function parseBindingObject(binding) {
2152
2215
  attachmentsProperty: typeof binding.attachmentsProperty === "string" ? binding.attachmentsProperty : null
2153
2216
  };
2154
2217
  }
2155
- function resolveSessionToolBinding(binding, legacyTool) {
2218
+ function resolveSessionToolBinding(binding) {
2156
2219
  if (typeof binding === "string") {
2157
2220
  return {
2158
2221
  toolName: binding,
@@ -2167,7 +2230,7 @@ function resolveSessionToolBinding(binding, legacyTool) {
2167
2230
  }
2168
2231
  }
2169
2232
  return {
2170
- toolName: typeof legacyTool === "string" ? legacyTool : null,
2233
+ toolName: null,
2171
2234
  messageProperty: null,
2172
2235
  attachmentsProperty: null
2173
2236
  };
@@ -2175,60 +2238,51 @@ function resolveSessionToolBinding(binding, legacyTool) {
2175
2238
  function getResolvedSessionBindingsFromSide(side) {
2176
2239
  const config = side ?? {};
2177
2240
  return {
2178
- stop: resolveSessionToolBinding(
2179
- config.sessionStop,
2180
- config.endSessionTool
2181
- ),
2182
- fail: resolveSessionToolBinding(
2183
- config.sessionFail,
2184
- config.failSessionTool
2185
- ),
2186
- status: resolveSessionToolBinding(
2187
- config.sessionStatus,
2188
- config.statusTool
2189
- )
2241
+ stop: resolveSessionToolBinding(config.sessionStop),
2242
+ fail: resolveSessionToolBinding(config.sessionFail),
2243
+ status: resolveSessionToolBinding(config.sessionStatus)
2190
2244
  };
2191
2245
  }
2192
2246
  function getAgentSessionBinding(agent, side, kind) {
2193
2247
  if (side === "a") {
2194
2248
  if (kind === "stop") {
2195
2249
  return {
2196
- toolName: agent.side_a_end_session_tool,
2197
- messageProperty: agent.side_a_end_session_message_property,
2198
- attachmentsProperty: agent.side_a_end_session_attachments_property
2250
+ toolName: agent.side_a_session_stop_tool,
2251
+ messageProperty: agent.side_a_session_stop_message_property,
2252
+ attachmentsProperty: agent.side_a_session_stop_attachments_property
2199
2253
  };
2200
2254
  }
2201
2255
  if (kind === "fail") {
2202
2256
  return {
2203
- toolName: agent.side_a_fail_session_tool,
2204
- messageProperty: agent.side_a_fail_session_message_property,
2205
- attachmentsProperty: agent.side_a_fail_session_attachments_property
2257
+ toolName: agent.side_a_session_fail_tool,
2258
+ messageProperty: agent.side_a_session_fail_message_property,
2259
+ attachmentsProperty: agent.side_a_session_fail_attachments_property
2206
2260
  };
2207
2261
  }
2208
2262
  return {
2209
- toolName: agent.side_a_status_tool,
2210
- messageProperty: agent.side_a_status_message_property,
2211
- attachmentsProperty: agent.side_a_status_attachments_property
2263
+ toolName: agent.side_a_session_status_tool,
2264
+ messageProperty: agent.side_a_session_status_message_property,
2265
+ attachmentsProperty: agent.side_a_session_status_attachments_property
2212
2266
  };
2213
2267
  }
2214
2268
  if (kind === "stop") {
2215
2269
  return {
2216
- toolName: agent.side_b_end_session_tool,
2217
- messageProperty: agent.side_b_end_session_message_property,
2218
- attachmentsProperty: agent.side_b_end_session_attachments_property
2270
+ toolName: agent.side_b_session_stop_tool,
2271
+ messageProperty: agent.side_b_session_stop_message_property,
2272
+ attachmentsProperty: agent.side_b_session_stop_attachments_property
2219
2273
  };
2220
2274
  }
2221
2275
  if (kind === "fail") {
2222
2276
  return {
2223
- toolName: agent.side_b_fail_session_tool,
2224
- messageProperty: agent.side_b_fail_session_message_property,
2225
- attachmentsProperty: agent.side_b_fail_session_attachments_property
2277
+ toolName: agent.side_b_session_fail_tool,
2278
+ messageProperty: agent.side_b_session_fail_message_property,
2279
+ attachmentsProperty: agent.side_b_session_fail_attachments_property
2226
2280
  };
2227
2281
  }
2228
2282
  return {
2229
- toolName: agent.side_b_status_tool,
2230
- messageProperty: agent.side_b_status_message_property,
2231
- attachmentsProperty: agent.side_b_status_attachments_property
2283
+ toolName: agent.side_b_session_status_tool,
2284
+ messageProperty: agent.side_b_session_status_message_property,
2285
+ attachmentsProperty: agent.side_b_session_status_attachments_property
2232
2286
  };
2233
2287
  }
2234
2288
  var init_sessionTools = __esm({
@@ -3255,15 +3309,15 @@ var init_ToolExecutor = __esm({
3255
3309
  side_a_stop_tool: null,
3256
3310
  side_a_stop_tool_response_property: null,
3257
3311
  side_a_max_steps: null,
3258
- side_a_end_session_tool: null,
3259
- side_a_end_session_message_property: null,
3260
- side_a_end_session_attachments_property: null,
3261
- side_a_fail_session_tool: null,
3262
- side_a_fail_session_message_property: null,
3263
- side_a_fail_session_attachments_property: null,
3264
- side_a_status_tool: null,
3265
- side_a_status_message_property: null,
3266
- side_a_status_attachments_property: null,
3312
+ side_a_session_stop_tool: null,
3313
+ side_a_session_stop_message_property: null,
3314
+ side_a_session_stop_attachments_property: null,
3315
+ side_a_session_fail_tool: null,
3316
+ side_a_session_fail_message_property: null,
3317
+ side_a_session_fail_attachments_property: null,
3318
+ side_a_session_status_tool: null,
3319
+ side_a_session_status_message_property: null,
3320
+ side_a_session_status_attachments_property: null,
3267
3321
  // Side B configuration (unused for prompt tools)
3268
3322
  side_b_label: null,
3269
3323
  side_b_agent_prompt: null,
@@ -3271,15 +3325,15 @@ var init_ToolExecutor = __esm({
3271
3325
  side_b_stop_tool: null,
3272
3326
  side_b_stop_tool_response_property: null,
3273
3327
  side_b_max_steps: null,
3274
- side_b_end_session_tool: null,
3275
- side_b_end_session_message_property: null,
3276
- side_b_end_session_attachments_property: null,
3277
- side_b_fail_session_tool: null,
3278
- side_b_fail_session_message_property: null,
3279
- side_b_fail_session_attachments_property: null,
3280
- side_b_status_tool: null,
3281
- side_b_status_message_property: null,
3282
- side_b_status_attachments_property: null
3328
+ side_b_session_stop_tool: null,
3329
+ side_b_session_stop_message_property: null,
3330
+ side_b_session_stop_attachments_property: null,
3331
+ side_b_session_fail_tool: null,
3332
+ side_b_session_fail_message_property: null,
3333
+ side_b_session_fail_attachments_property: null,
3334
+ side_b_session_status_tool: null,
3335
+ side_b_session_status_message_property: null,
3336
+ side_b_session_status_attachments_property: null
3283
3337
  };
3284
3338
  let userMessageContent;
3285
3339
  let userMessageAttachments;
@@ -4492,8 +4546,8 @@ ${result ?? error ?? "No result content."}${attachmentSummary}`;
4492
4546
  state.currentSide,
4493
4547
  "status"
4494
4548
  );
4495
- const statusTool = statusBinding.toolName;
4496
- if (!statusTool || statusTool !== toolName) {
4549
+ const sessionStatusToolName = statusBinding.toolName;
4550
+ if (!sessionStatusToolName || sessionStatusToolName !== toolName) {
4497
4551
  return;
4498
4552
  }
4499
4553
  const mappedValue = statusBinding.messageProperty && args ? args[statusBinding.messageProperty] : void 0;
@@ -4677,9 +4731,9 @@ ${result ?? error ?? "No result content."}${attachmentSummary}`;
4677
4731
  const baseError = processedResult.error?.trim() || "Tool execution failed";
4678
4732
  content = `Failed to execute tool: ${baseError}`;
4679
4733
  }
4680
- const endSessionTool = stopBinding.toolName;
4681
- const failSessionTool = failBinding.toolName;
4682
- const shouldAppendAttachmentPaths = (!endSessionTool || call.function.name !== endSessionTool) && (!failSessionTool || call.function.name !== failSessionTool);
4734
+ const sessionStopToolName = stopBinding.toolName;
4735
+ const sessionFailToolName = failBinding.toolName;
4736
+ const shouldAppendAttachmentPaths = (!sessionStopToolName || call.function.name !== sessionStopToolName) && (!sessionFailToolName || call.function.name !== sessionFailToolName);
4683
4737
  if (shouldAppendAttachmentPaths && attachmentRefs && attachmentRefs.length > 0) {
4684
4738
  const attachmentPaths = attachmentRefs.map((ref) => `Attachment path for "${ref.name}": ${ref.path}`).join("\n");
4685
4739
  content += `
@@ -6815,30 +6869,30 @@ var init_FlowEngine = __esm({
6815
6869
  side_a_stop_tool: newAgentDef.sideA?.stopTool ?? null,
6816
6870
  side_a_stop_tool_response_property: newAgentDef.sideA?.stopToolResponseProperty ?? null,
6817
6871
  side_a_max_steps: newAgentDef.sideA?.maxSteps ?? null,
6818
- side_a_end_session_tool: sideASession.stop.toolName,
6819
- side_a_end_session_message_property: sideASession.stop.messageProperty,
6820
- side_a_end_session_attachments_property: sideASession.stop.attachmentsProperty,
6821
- side_a_fail_session_tool: sideASession.fail.toolName,
6822
- side_a_fail_session_message_property: sideASession.fail.messageProperty,
6823
- side_a_fail_session_attachments_property: sideASession.fail.attachmentsProperty,
6824
- side_a_status_tool: sideASession.status.toolName,
6825
- side_a_status_message_property: sideASession.status.messageProperty,
6826
- side_a_status_attachments_property: sideASession.status.attachmentsProperty,
6872
+ side_a_session_stop_tool: sideASession.stop.toolName,
6873
+ side_a_session_stop_message_property: sideASession.stop.messageProperty,
6874
+ side_a_session_stop_attachments_property: sideASession.stop.attachmentsProperty,
6875
+ side_a_session_fail_tool: sideASession.fail.toolName,
6876
+ side_a_session_fail_message_property: sideASession.fail.messageProperty,
6877
+ side_a_session_fail_attachments_property: sideASession.fail.attachmentsProperty,
6878
+ side_a_session_status_tool: sideASession.status.toolName,
6879
+ side_a_session_status_message_property: sideASession.status.messageProperty,
6880
+ side_a_session_status_attachments_property: sideASession.status.attachmentsProperty,
6827
6881
  side_b_label: newAgentDef.sideB?.label ?? null,
6828
6882
  side_b_agent_prompt: newAgentDef.sideB?.prompt ?? null,
6829
6883
  side_b_stop_on_response: newAgentDef.sideB?.stopOnResponse ?? false,
6830
6884
  side_b_stop_tool: newAgentDef.sideB?.stopTool ?? null,
6831
6885
  side_b_stop_tool_response_property: newAgentDef.sideB?.stopToolResponseProperty ?? null,
6832
6886
  side_b_max_steps: newAgentDef.sideB?.maxSteps ?? null,
6833
- side_b_end_session_tool: sideBSession.stop.toolName,
6834
- side_b_end_session_message_property: sideBSession.stop.messageProperty,
6835
- side_b_end_session_attachments_property: sideBSession.stop.attachmentsProperty,
6836
- side_b_fail_session_tool: sideBSession.fail.toolName,
6837
- side_b_fail_session_message_property: sideBSession.fail.messageProperty,
6838
- side_b_fail_session_attachments_property: sideBSession.fail.attachmentsProperty,
6839
- side_b_status_tool: sideBSession.status.toolName,
6840
- side_b_status_message_property: sideBSession.status.messageProperty,
6841
- side_b_status_attachments_property: sideBSession.status.attachmentsProperty
6887
+ side_b_session_stop_tool: sideBSession.stop.toolName,
6888
+ side_b_session_stop_message_property: sideBSession.stop.messageProperty,
6889
+ side_b_session_stop_attachments_property: sideBSession.stop.attachmentsProperty,
6890
+ side_b_session_fail_tool: sideBSession.fail.toolName,
6891
+ side_b_session_fail_message_property: sideBSession.fail.messageProperty,
6892
+ side_b_session_fail_attachments_property: sideBSession.fail.attachmentsProperty,
6893
+ side_b_session_status_tool: sideBSession.status.toolName,
6894
+ side_b_session_status_message_property: sideBSession.status.messageProperty,
6895
+ side_b_session_status_attachments_property: sideBSession.status.attachmentsProperty
6842
6896
  };
6843
6897
  const { sideAPrompt, sideBPrompt } = await this.loadAgentAndPrompts(
6844
6898
  agentConfig,
@@ -7629,7 +7683,7 @@ var init_FlowEngine = __esm({
7629
7683
  role: row.role,
7630
7684
  content: row.content,
7631
7685
  created_at: row.created_at,
7632
- silent: row.silent === 1,
7686
+ silent: this.isSilentMessageRow(row.silent),
7633
7687
  attachments: row.attachments,
7634
7688
  metadata: parsedMetadata,
7635
7689
  subagent_id: this.extractSubagentIdFromMetadata(row.metadata)
@@ -7782,12 +7836,6 @@ var init_FlowEngine = __esm({
7782
7836
  const completedMessages = state.messageHistory.filter(
7783
7837
  (msg) => msg.status !== "pending"
7784
7838
  );
7785
- const toolCallsWithResponses = /* @__PURE__ */ new Set();
7786
- for (const msg of completedMessages) {
7787
- if (msg.role === "tool" && msg.tool_call_id) {
7788
- toolCallsWithResponses.add(msg.tool_call_id);
7789
- }
7790
- }
7791
7839
  const recentMessageThreshold = state.prompt.recentImageThreshold ?? 10;
7792
7840
  const oldMessageThreshold = completedMessages.length - recentMessageThreshold;
7793
7841
  let messageIndex = 0;
@@ -7850,11 +7898,12 @@ ${msg.content}` : `${imageDescriptions}${nonImageList}`;
7850
7898
  messageContent = msg.content || "";
7851
7899
  }
7852
7900
  messageIndex++;
7901
+ const shouldIncludeAssistantToolCalls = includePastTools && role === "assistant" && Boolean(msg.tool_calls);
7853
7902
  const messageToAdd = {
7854
7903
  role,
7855
7904
  content: messageContent,
7856
- tool_calls: includePastTools && msg.tool_calls ? JSON.parse(msg.tool_calls) : void 0,
7857
- tool_call_id: includePastTools && msg.tool_call_id ? msg.tool_call_id : void 0,
7905
+ tool_calls: shouldIncludeAssistantToolCalls ? JSON.parse(msg.tool_calls) : void 0,
7906
+ tool_call_id: includePastTools && msg.role === "tool" && msg.tool_call_id ? msg.tool_call_id : void 0,
7858
7907
  name: msg.name || void 0,
7859
7908
  // For tool messages, also set toolName for provider transformers (e.g., OpenAI image_generation)
7860
7909
  toolName: msg.role === "tool" ? msg.name : void 0
@@ -7904,24 +7953,7 @@ ${msg.content}` : `${imageDescriptions}${nonImageList}`;
7904
7953
  messageToAdd.reasoning_details = reasoningDetailsArray;
7905
7954
  }
7906
7955
  }
7907
- if (includePastTools && msg.role === "assistant" && msg.tool_calls) {
7908
- const toolCalls = JSON.parse(msg.tool_calls);
7909
- const matchedToolCalls = toolCalls.filter(
7910
- (toolCall) => toolCallsWithResponses.has(toolCall.id)
7911
- );
7912
- if (msg.content || matchedToolCalls.length > 0) {
7913
- if (matchedToolCalls.length === 0) {
7914
- delete messageToAdd.tool_calls;
7915
- } else if (matchedToolCalls.length !== toolCalls.length) {
7916
- messageToAdd.tool_calls = matchedToolCalls;
7917
- }
7918
- messages.push(messageToAdd);
7919
- } else {
7920
- console.warn(`[FlowEngine] Skipping assistant message ${msg.id} - no content and no tool calls with responses`);
7921
- }
7922
- } else {
7923
- messages.push(messageToAdd);
7924
- }
7956
+ messages.push(messageToAdd);
7925
7957
  }
7926
7958
  const maxSteps = state.currentSide === "a" ? state.agentConfig.side_a_max_steps : state.agentConfig.side_b_max_steps;
7927
7959
  if (typeof maxSteps === "number" && !isNaN(maxSteps)) {
@@ -9587,14 +9619,14 @@ ${lines.join("\n\n")}`
9587
9619
  * Check if stop condition is met
9588
9620
  */
9589
9621
  static checkStopCondition(state, response) {
9590
- const failSessionTool = getAgentSessionBinding(
9622
+ const sessionFailToolName = getAgentSessionBinding(
9591
9623
  state.agentConfig,
9592
9624
  state.currentSide,
9593
9625
  "fail"
9594
9626
  ).toolName;
9595
- if (failSessionTool) {
9627
+ if (sessionFailToolName) {
9596
9628
  const matchingFailSessionCalls = response.tool_calls?.filter(
9597
- (call) => call.function.name === failSessionTool
9629
+ (call) => call.function.name === sessionFailToolName
9598
9630
  ) ?? [];
9599
9631
  if (matchingFailSessionCalls.length > 0) {
9600
9632
  const hasSuccessfulFailSessionCall = matchingFailSessionCalls.some(
@@ -9603,11 +9635,11 @@ ${lines.join("\n\n")}`
9603
9635
  if (hasSuccessfulFailSessionCall) {
9604
9636
  state.stopped = true;
9605
9637
  state.stoppedBy = state.currentSide;
9606
- state.stopReason = `Fail session tool called: ${failSessionTool}`;
9638
+ state.stopReason = `sessionFail tool called: ${sessionFailToolName}`;
9607
9639
  state.stopReasonCode = "session_fail";
9608
9640
  state.emitTelemetry?.({
9609
9641
  type: "stopped",
9610
- reason: `Fail session tool called: ${failSessionTool}`,
9642
+ reason: `sessionFail tool called: ${sessionFailToolName}`,
9611
9643
  side: state.currentSide,
9612
9644
  timestamp: Date.now()
9613
9645
  });
@@ -9622,14 +9654,14 @@ ${lines.join("\n\n")}`
9622
9654
  };
9623
9655
  }
9624
9656
  }
9625
- const endSessionTool = getAgentSessionBinding(
9657
+ const sessionStopToolName = getAgentSessionBinding(
9626
9658
  state.agentConfig,
9627
9659
  state.currentSide,
9628
9660
  "stop"
9629
9661
  ).toolName;
9630
- if (endSessionTool) {
9662
+ if (sessionStopToolName) {
9631
9663
  const matchingEndSessionCalls = response.tool_calls?.filter(
9632
- (call) => call.function.name === endSessionTool
9664
+ (call) => call.function.name === sessionStopToolName
9633
9665
  ) ?? [];
9634
9666
  if (matchingEndSessionCalls.length > 0) {
9635
9667
  const hasSuccessfulEndSessionCall = matchingEndSessionCalls.some(
@@ -9638,11 +9670,11 @@ ${lines.join("\n\n")}`
9638
9670
  if (hasSuccessfulEndSessionCall) {
9639
9671
  state.stopped = true;
9640
9672
  state.stoppedBy = state.currentSide;
9641
- state.stopReason = `End session tool called: ${endSessionTool}`;
9673
+ state.stopReason = `sessionStop tool called: ${sessionStopToolName}`;
9642
9674
  state.stopReasonCode = "session_stop";
9643
9675
  state.emitTelemetry?.({
9644
9676
  type: "stopped",
9645
- reason: `End session tool called: ${endSessionTool}`,
9677
+ reason: `sessionStop tool called: ${sessionStopToolName}`,
9646
9678
  side: state.currentSide,
9647
9679
  timestamp: Date.now()
9648
9680
  });
@@ -9734,16 +9766,13 @@ ${lines.join("\n\n")}`
9734
9766
  )?.[1];
9735
9767
  return msg.role === "tool" && referencedSubagentId !== void 0 && (typeof msg.subagent_id === "string" && msg.subagent_id.trim().length > 0 || typeof msg.name === "string");
9736
9768
  }
9769
+ static isSilentMessageRow(value) {
9770
+ return value === 1 || value === true;
9771
+ }
9737
9772
  static filterOrphanedToolCalls(messages) {
9738
- const toolResultIds = /* @__PURE__ */ new Set();
9739
- for (const msg of messages) {
9740
- if (msg.role === "tool" && msg.tool_call_id) {
9741
- toolResultIds.add(msg.tool_call_id);
9742
- }
9743
- }
9744
9773
  const assistantToolCallIds = /* @__PURE__ */ new Set();
9745
9774
  for (const msg of messages) {
9746
- if (msg.role === "assistant" && msg.tool_calls) {
9775
+ if ((msg.role === "assistant" || msg.role === "user") && msg.tool_calls) {
9747
9776
  try {
9748
9777
  const toolCalls = JSON.parse(msg.tool_calls);
9749
9778
  for (const call of toolCalls) {
@@ -9769,40 +9798,6 @@ ${lines.join("\n\n")}`
9769
9798
  continue;
9770
9799
  }
9771
9800
  }
9772
- if (msg.role === "assistant" && msg.tool_calls) {
9773
- try {
9774
- const toolCalls = JSON.parse(msg.tool_calls);
9775
- const matchedToolCalls = toolCalls.filter(
9776
- (call) => toolResultIds.has(call.id)
9777
- );
9778
- if (matchedToolCalls.length === 0 && toolCalls.length > 0) {
9779
- if (msg.content) {
9780
- console.warn(`[FlowEngine] Filtering tool_calls from message ${msg.id} - no matching tool results`);
9781
- warnedRemovedCount += 1;
9782
- filtered.push({
9783
- ...msg,
9784
- tool_calls: null
9785
- });
9786
- continue;
9787
- } else {
9788
- console.warn(`[FlowEngine] Filtering assistant message ${msg.id} - no content and no tool calls with results`);
9789
- warnedRemovedCount += 1;
9790
- continue;
9791
- }
9792
- }
9793
- if (matchedToolCalls.length !== toolCalls.length) {
9794
- console.warn(`[FlowEngine] Filtering ${toolCalls.length - matchedToolCalls.length} orphaned tool_calls from message ${msg.id}`);
9795
- warnedRemovedCount += 1;
9796
- filtered.push({
9797
- ...msg,
9798
- tool_calls: JSON.stringify(matchedToolCalls)
9799
- });
9800
- continue;
9801
- }
9802
- } catch (error) {
9803
- console.error(`Failed to parse tool_calls for message ${msg.id}:`, error);
9804
- }
9805
- }
9806
9801
  filtered.push(msg);
9807
9802
  }
9808
9803
  const removedCount = messages.length - filtered.length;
@@ -9873,14 +9868,18 @@ ${lines.join("\n\n")}`
9873
9868
  request_sent_at: row.request_sent_at,
9874
9869
  response_completed_at: row.response_completed_at,
9875
9870
  status: row.status,
9876
- silent: row.silent === 1,
9871
+ silent: this.isSilentMessageRow(row.silent),
9877
9872
  tool_status: row.tool_status,
9878
9873
  parent_id: row.parent_id,
9879
9874
  depth: row.depth,
9880
9875
  reasoning_content: row.reasoning_content,
9881
9876
  reasoning_details: row.reasoning_details
9882
9877
  }));
9883
- return this.runFilterMessagesHook(thread.instance, state, this.filterOrphanedToolCalls(messages));
9878
+ return this.runFilterMessagesHook(
9879
+ thread.instance,
9880
+ state,
9881
+ this.filterOrphanedToolCalls(messages)
9882
+ );
9884
9883
  } catch (error) {
9885
9884
  console.error("Error loading message history:", error);
9886
9885
  return [];
@@ -59499,10 +59498,10 @@ var name_get_default = defineController(async ({ params, agents, prompts, prompt
59499
59498
  const agentEnv = definition.env ?? definition.tenvs ?? {};
59500
59499
  const sideAPrompt = promptMap[definition.sideA?.prompt];
59501
59500
  const sideBPrompt = definition.sideB ? promptMap[definition.sideB?.prompt] : null;
59502
- const resolveSessionBinding = (binding, legacyTool) => {
59501
+ const resolveSessionBinding = (binding) => {
59503
59502
  if (binding && typeof binding === "object" && !Array.isArray(binding)) {
59504
59503
  const b = binding;
59505
- const name16 = typeof b.name === "string" ? b.name : legacyTool ?? null;
59504
+ const name16 = typeof b.name === "string" ? b.name : null;
59506
59505
  return {
59507
59506
  tool: name16,
59508
59507
  messageProperty: typeof b.messageProperty === "string" ? b.messageProperty : null,
@@ -59512,32 +59511,14 @@ var name_get_default = defineController(async ({ params, agents, prompts, prompt
59512
59511
  if (typeof binding === "string") {
59513
59512
  return { tool: binding, messageProperty: null, attachmentsProperty: null };
59514
59513
  }
59515
- return { tool: legacyTool ?? null, messageProperty: null, attachmentsProperty: null };
59514
+ return { tool: null, messageProperty: null, attachmentsProperty: null };
59516
59515
  };
59517
- const sideASessionStop = resolveSessionBinding(
59518
- definition.sideA?.sessionStop,
59519
- definition.sideA?.endSessionTool
59520
- );
59521
- const sideASessionFail = resolveSessionBinding(
59522
- definition.sideA?.sessionFail,
59523
- definition.sideA?.failSessionTool
59524
- );
59525
- const sideASessionStatus = resolveSessionBinding(
59526
- definition.sideA?.sessionStatus,
59527
- definition.sideA?.statusTool
59528
- );
59529
- const sideBSessionStop = resolveSessionBinding(
59530
- definition.sideB?.sessionStop,
59531
- definition.sideB?.endSessionTool
59532
- );
59533
- const sideBSessionFail = resolveSessionBinding(
59534
- definition.sideB?.sessionFail,
59535
- definition.sideB?.failSessionTool
59536
- );
59537
- const sideBSessionStatus = resolveSessionBinding(
59538
- definition.sideB?.sessionStatus,
59539
- definition.sideB?.statusTool
59540
- );
59516
+ const sideASessionStop = resolveSessionBinding(definition.sideA?.sessionStop);
59517
+ const sideASessionFail = resolveSessionBinding(definition.sideA?.sessionFail);
59518
+ const sideASessionStatus = resolveSessionBinding(definition.sideA?.sessionStatus);
59519
+ const sideBSessionStop = resolveSessionBinding(definition.sideB?.sessionStop);
59520
+ const sideBSessionFail = resolveSessionBinding(definition.sideB?.sessionFail);
59521
+ const sideBSessionStatus = resolveSessionBinding(definition.sideB?.sessionStatus);
59541
59522
  const agent = {
59542
59523
  id: definition.name,
59543
59524
  name: definition.name,
@@ -59561,10 +59542,6 @@ var name_get_default = defineController(async ({ params, agents, prompts, prompt
59561
59542
  side_a_session_status_tool: sideASessionStatus.tool,
59562
59543
  side_a_session_status_message_property: sideASessionStatus.messageProperty,
59563
59544
  side_a_session_status_attachments_property: sideASessionStatus.attachmentsProperty,
59564
- // Legacy aliases
59565
- side_a_end_session_tool: sideASessionStop.tool,
59566
- side_a_fail_session_tool: sideASessionFail.tool,
59567
- side_a_status_tool: sideASessionStatus.tool,
59568
59545
  side_a_manual_stop_condition: definition.sideA?.manualStopCondition || false,
59569
59546
  // Side B configuration (if dual_ai)
59570
59547
  side_b_label: definition.sideB?.label || null,
@@ -59583,10 +59560,6 @@ var name_get_default = defineController(async ({ params, agents, prompts, prompt
59583
59560
  side_b_session_status_tool: sideBSessionStatus.tool,
59584
59561
  side_b_session_status_message_property: sideBSessionStatus.messageProperty,
59585
59562
  side_b_session_status_attachments_property: sideBSessionStatus.attachmentsProperty,
59586
- // Legacy aliases
59587
- side_b_end_session_tool: sideBSessionStop.tool,
59588
- side_b_fail_session_tool: sideBSessionFail.tool,
59589
- side_b_status_tool: sideBSessionStatus.tool,
59590
59563
  side_b_manual_stop_condition: definition.sideB?.manualStopCondition || false,
59591
59564
  // Session configuration
59592
59565
  max_session_turns: definition.maxSessionTurns || null,
@@ -62755,6 +62728,12 @@ var logId_get_default = defineController(async ({ req, params, env: env2 }) => {
62755
62728
  const durableId = env2.AGENT_BUILDER_THREAD.idFromName(threadId);
62756
62729
  const stub = env2.AGENT_BUILDER_THREAD.get(durableId);
62757
62730
  const logDetails = await stub.getLogDetails(logId);
62731
+ if (!logDetails) {
62732
+ return Response.json(
62733
+ { error: `Log not found: ${logId}` },
62734
+ { status: 404 }
62735
+ );
62736
+ }
62758
62737
  return Response.json(logDetails);
62759
62738
  } catch (error) {
62760
62739
  console.error(`Error fetching log details for ${logId}:`, error);