fix(agent): pass reasoning_content back to providers that require it

MiMo and DeepSeek V3.2/V4 reject multi-turn requests in thinking mode
when the prior assistant message lacks reasoning_content with HTTP 400:
"The reasoning_content in the thinking mode must be passed back to the API."

Agent's ReAct loop is the worst-case scenario — every round produces tool
calls, exactly the case DeepSeek's docs specify reasoning_content MUST
participate in subsequent context.

Plumb reasoning_content through the full assistant-message round-trip:
- chat.Message / types.ChatResponse / types.AgentStep gain a
  reasoning_content field (AgentStep persists via the existing
  Message.AgentSteps jsonb column, no migration needed).
- streamLLMToEventBus accumulates reasoning chunks into
  result.ReasoningContent and surfaces it on the round's ChatResponse.
- engine.runReActIteration writes it onto AgentStep so cross-turn replay
  preserves it.
- observe.appendToolResults attaches it to the same-turn assistant
  message; agent_history.buildAssistantHistoryMessages does the same on
  cross-turn replay.
- RemoteAPIChat.ConvertMessages forwards it on assistant turns to
  openai.ChatCompletionMessage.ReasoningContent (already supported by
  go-openai); providers that don't recognize the field ignore it.

Tests cover the three boundaries: ConvertMessages serializes it for
upstream, appendToolResults preserves it within the same turn, and
buildAssistantHistoryMessages replays it across turns.

Scope is intentionally limited to Agent mode — KnowledgeQA's chat
pipeline and Anthropic's signed thinking_blocks are separate fixes that
require schema changes (rendered_content / thinking_blocks columns).

Fixes #1302
This commit is contained in:
wizardchen
2026-05-13 12:26:11 +08:00
committed by lyingbug
parent 1ae06fb857
commit b00bc84f35
11 changed files with 225 additions and 28 deletions
@@ -158,9 +158,10 @@ func buildAssistantHistoryMessages(m *types.Message) []chat.Message {
continue
}
assistantMsg := chat.Message{
Role: "assistant",
Content: step.Thought,
ToolCalls: make([]chat.ToolCall, 0, len(nonTerminalCalls)),
Role: "assistant",
Content: step.Thought,
ReasoningContent: step.ReasoningContent,
ToolCalls: make([]chat.ToolCall, 0, len(nonTerminalCalls)),
}
for _, tc := range nonTerminalCalls {
argsJSON, _ := json.Marshal(tc.Args)
@@ -230,3 +230,40 @@ func TestFilterNonTerminalToolCalls(t *testing.T) {
assert.Equal(t, agenttools.ToolWebSearch, out[1].Name)
}
}
// TestBuildAssistantHistoryMessages_ReplaysReasoningContent guards the
// cross-turn replay path: AgentStep.ReasoningContent persisted on a prior turn
// must be re-attached to the rebuilt assistant message, otherwise MiMo and
// DeepSeek thinking-mode reject the next turn with HTTP 400 (issue #1302).
func TestBuildAssistantHistoryMessages_ReplaysReasoningContent(t *testing.T) {
msg := &types.Message{
Role: "assistant",
Content: "Found 3 matches in the docs.",
AgentSteps: types.AgentSteps{
{
Iteration: 0,
Thought: "Let me search.",
ReasoningContent: "model's chain of thought",
ToolCalls: []types.ToolCall{{
ID: "call_1",
Name: agenttools.ToolKnowledgeSearch,
Args: map[string]interface{}{"query": "foo"},
Result: &types.ToolResult{
Success: true,
Output: "doc A",
},
}},
},
},
}
got := buildAssistantHistoryMessages(msg)
if !assert.Len(t, got, 3) {
return
}
assert.Equal(t, "model's chain of thought", got[0].ReasoningContent,
"reasoning_content from AgentStep must be replayed onto the rebuilt assistant message "+
"so MiMo/DeepSeek thinking-mode does not 400 on multi-turn (issue #1302)")
// Tool message and final answer message must NOT carry reasoning_content.
assert.Empty(t, got[1].ReasoningContent)
assert.Empty(t, got[2].ReasoningContent)
}