fix(llm): handle Anthropic thinking content blocks in multi-turn conversations

Anthropic models with extended thinking return type:"thinking" content
blocks which were silently dropped, causing empty assistant content in
conversation history. Extract thinking blocks into ReasoningContent so
the existing Content() fallback works correctly.
This commit is contained in:
kite 2026-06-07 17:09:31 +08:00
parent 5fad9ce2fa
commit 1b3c0ab19f

View file

@ -653,12 +653,17 @@ func buildToolInputSchema(params map[string]any) anthropic.ToolInputSchemaParam
// mapAnthropicResponse converts the SDK response into ChatResponse.
func (c *AnthropicClient) mapAnthropicResponse(sdkResp *anthropic.Message) *ChatResponse {
var textParts []string
var thinkingParts []string
var toolCalls []ToolCall
for _, block := range sdkResp.Content {
switch block.Type {
case "text":
textParts = append(textParts, block.Text)
case "thinking":
if block.Thinking != "" {
thinkingParts = append(thinkingParts, block.Thinking)
}
case "tool_use":
toolCalls = append(toolCalls, ToolCall{
ID: block.ID,
@ -677,6 +682,11 @@ func (c *AnthropicClient) mapAnthropicResponse(sdkResp *anthropic.Message) *Chat
contentStr = &s
}
var reasoningContent string
if len(thinkingParts) > 0 {
reasoningContent = strings.Join(thinkingParts, "\n")
}
finishReason := string(sdkResp.StopReason)
if finishReason == "" {
finishReason = "stop"
@ -701,9 +711,10 @@ func (c *AnthropicClient) mapAnthropicResponse(sdkResp *anthropic.Message) *Chat
Model: string(sdkResp.Model),
Choices: []Choice{{
Message: ResponseMessage{
Role: "assistant",
Content: contentStr,
ToolCalls: toolCalls,
Role: "assistant",
Content: contentStr,
ReasoningContent: reasoningContent,
ToolCalls: toolCalls,
},
FinishReason: finishReason,
}},