Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/agent-core/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,28 @@ export function inlineFileAttachments(
*
* Rewrite it to `output-error` so `convertToModelMessages` emits the
* matching tool-result with an interrupt marker the model can see.
*
* Also backstops a missing `input`: a tool call aborted mid input-stream
* (or persisted by an older path) can carry no `input` at all. Anthropic
* requires every `tool_use` block to have an `input` object, so an absent
* one yields `tool_use.input: Field required` 400s on replay. Default it
* to `{}` for any tool part regardless of state.
*/
const INTERRUPT_MARKER = "[interrupted]";
export function finalizeOrphanToolParts(
parts: UIMessage["parts"]
): UIMessage["parts"] {
return parts.map((p) => {
const tp = p as { type?: string; state?: string };
const tp = p as { type?: string; state?: string; input?: unknown };
if (!tp.type?.startsWith("tool-")) return p;
if (tp.state !== "input-streaming" && tp.state !== "input-available") return p;
const needsInput = tp.input === undefined || tp.input === null;
const isOrphan =
tp.state === "input-streaming" || tp.state === "input-available";
if (!isOrphan && !needsInput) return p;
return {
...(p as object),
state: "output-error",
errorText: INTERRUPT_MARKER,
...(needsInput ? { input: {} } : {}),
...(isOrphan ? { state: "output-error", errorText: INTERRUPT_MARKER } : {}),
} as UIMessage["parts"][number];
});
}
Expand Down
Loading