fix: Send plain text history to prevent orphaned tool_use block errors

Claude's API requires every tool_use block to be followed by a matching
tool_result. Since tool interactions are fully resolved server-side within
a single request, the client should only send plain text in conversation
history — never structured content blocks that contain tool_use references.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Leivur Djurhuus 2026-03-12 15:29:20 -05:00
parent 7c70c3aba3
commit 19cd24c07a

View file

@ -26,8 +26,6 @@ export interface ChatMessage {
isLoading?: boolean;
/** Tool calls in progress or completed during this assistant turn */
toolCalls?: ToolStatus[];
/** Structured content blocks for multi-turn context (sent to API) */
contentBlocks?: any[];
/** Clickable entity suggestions for disambiguation */
suggestions?: EntitySuggestion[];
}
@ -115,10 +113,12 @@ export function useChat(context?: ChatContext) {
setIsLoading(true);
try {
// Build message history for the API, preserving structured content blocks
// Build message history for the API — send plain text only.
// Tool interactions are resolved server-side within a single request,
// so we never send tool_use/tool_result blocks back in history.
const apiMessages = [...messages, userMsg].map((m) => ({
role: m.role,
content: m.contentBlocks || m.content,
content: m.content,
}));
abortRef.current = new AbortController();
@ -194,10 +194,6 @@ export function useChat(context?: ChatContext) {
provider: data.provider,
isLoading: false,
suggestions: data.suggestions,
// Store structured blocks for multi-turn context
contentBlocks: data.content
? [{ type: "text", text: data.content }]
: undefined,
}
: m
)