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
+46
View File
@@ -223,6 +223,52 @@ func TestBuildChatCompletionRequest_ToolChoice(t *testing.T) {
})
}
// TestConvertMessages_ReasoningContentRoundTrip verifies that assistant
// reasoning_content is propagated through ConvertMessages so that providers
// like MiMo / DeepSeek thinking-mode can read it back from prior turns.
// See issue #1302: MiMo rejects multi-turn requests with HTTP 400
// "The reasoning_content in the thinking mode must be passed back to the API."
// when this field is dropped.
func TestConvertMessages_ReasoningContentRoundTrip(t *testing.T) {
c := newTestRemoteChat(t)
t.Run("assistant reasoning_content propagated", func(t *testing.T) {
messages := []Message{
{Role: "user", Content: "hi"},
{
Role: "assistant",
Content: "the answer",
ReasoningContent: "let me think about this carefully",
},
{Role: "user", Content: "follow-up"},
}
out := c.ConvertMessages(messages)
require.Len(t, out, 3)
assert.Equal(t, "let me think about this carefully", out[1].ReasoningContent,
"assistant reasoning_content must be retained for multi-turn replay")
assert.Empty(t, out[0].ReasoningContent, "user message must not carry reasoning_content")
assert.Empty(t, out[2].ReasoningContent, "user message must not carry reasoning_content")
})
t.Run("non-assistant role drops reasoning_content even if set", func(t *testing.T) {
messages := []Message{
{Role: "user", Content: "hi", ReasoningContent: "should be dropped"},
}
out := c.ConvertMessages(messages)
require.Len(t, out, 1)
assert.Empty(t, out[0].ReasoningContent, "non-assistant roles must never carry reasoning_content upstream")
})
t.Run("empty assistant reasoning_content stays empty", func(t *testing.T) {
messages := []Message{
{Role: "assistant", Content: "no thinking"},
}
out := c.ConvertMessages(messages)
require.Len(t, out, 1)
assert.Empty(t, out[0].ReasoningContent)
})
}
// TestRemoteAPIChat 综合测试 Remote API Chat 的所有功能
func TestRemoteAPIChat(t *testing.T) {
// 获取环境变量