package service import ( "encoding/json" "testing" agenttools "github.com/Tencent/WeKnora/internal/agent/tools" "github.com/Tencent/WeKnora/internal/models/chat" "github.com/Tencent/WeKnora/internal/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TestBuildUserHistoryMessage_PrefersRenderedContent verifies the user side of // history rebuild: RenderedContent (the RAG-augmented version) wins over the // raw Content, and image captions are appended only when there's no rendered // override (the rendered version already carries any retrieval context that // would have included the caption upstream). func TestBuildUserHistoryMessage_PrefersRenderedContent(t *testing.T) { msg := &types.Message{ Role: "user", Content: "what about the chart?", RenderedContent: "what about the chart? [augmented]", Images: types.MessageImages{ {Caption: "a bar chart"}, }, } got := buildUserHistoryMessage(msg) assert.Equal(t, "user", got.Role) assert.Equal(t, "what about the chart? [augmented]", got.Content) } func TestBuildUserHistoryMessage_FallsBackToContentWithCaptions(t *testing.T) { msg := &types.Message{ Role: "user", Content: "look at this", Images: types.MessageImages{ {Caption: "a bar chart"}, {Caption: "a pie chart"}, }, } got := buildUserHistoryMessage(msg) assert.Equal(t, "user", got.Role) assert.Equal(t, "look at this\n\n[用户上传图片内容]\na bar chart\na pie chart", got.Content) } // TestBuildUserHistoryMessage_AppendsAttachmentsWhenNoRenderedContent covers // the Agent-mode multi-turn path: AgentQA does not persist RenderedContent, so // the next turn's history must reconstruct the original attachment prompt from // the stored Attachments column. Otherwise, follow-up questions like "what is // in there?" lose all reference to the uploaded file. func TestBuildUserHistoryMessage_AppendsAttachmentsWhenNoRenderedContent(t *testing.T) { msg := &types.Message{ Role: "user", Content: "summarize this", Attachments: types.MessageAttachments{ { FileName: "report.pdf", FileType: ".pdf", FileSize: 2048, Content: "hello world", }, }, } got := buildUserHistoryMessage(msg) assert.Equal(t, "user", got.Role) assert.Contains(t, got.Content, "summarize this") assert.Contains(t, got.Content, ``) assert.Contains(t, got.Content, "hello world") } // TestBuildUserHistoryMessage_RenderedContentSkipsAttachmentReplay ensures the // KnowledgeQA path (where RenderedContent already includes the attachment // prompt persisted by the pipeline) does not double-inject attachments. func TestBuildUserHistoryMessage_RenderedContentSkipsAttachmentReplay(t *testing.T) { msg := &types.Message{ Role: "user", Content: "summarize this", RenderedContent: "summarize this [with retrieval context already included]", Attachments: types.MessageAttachments{ {FileName: "report.pdf", FileType: ".pdf", Content: "hello"}, }, } got := buildUserHistoryMessage(msg) assert.Equal(t, "summarize this [with retrieval context already included]", got.Content) assert.NotContains(t, got.Content, "… blocks stripped, so // internal-reasoning text doesn't leak into the next turn's context. func TestBuildAssistantHistoryMessages_StripsThinkBlocks(t *testing.T) { msg := &types.Message{ Role: "assistant", Content: "plotting...The answer is 42.", } got := buildAssistantHistoryMessages(msg) if assert.Len(t, got, 1) { assert.Equal(t, "The answer is 42.", got[0].Content) } } // TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape covers the // option-B replay: non-terminal tool calls from AgentSteps become proper // assistant_with_tool_calls + tool messages, and the canonical final answer is // appended last. final_answer entries are filtered because they're terminal // signals — the trailing assistant message already carries the answer. func TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape(t *testing.T) { msg := &types.Message{ Role: "assistant", Content: "Found 3 matches in the docs.", AgentSteps: types.AgentSteps{ { Iteration: 0, Thought: "Let me search.", ToolCalls: []types.ToolCall{ { ID: "call_1", Name: agenttools.ToolKnowledgeSearch, Args: map[string]interface{}{"query": "foo"}, Result: &types.ToolResult{ Success: true, Output: "doc A, doc B, doc C", }, }, }, }, { Iteration: 1, Thought: "", ToolCalls: []types.ToolCall{ { // Legacy persisted data: old conversations recorded a // final_answer terminal tool call. The filter still drops it. ID: "call_2", Name: "final_answer", Args: map[string]interface{}{"answer": "Found 3 matches in the docs."}, Result: &types.ToolResult{Success: true}, }, }, }, }, } got := buildAssistantHistoryMessages(msg) if !assert.Len(t, got, 3) { return } // 1. assistant message announcing the tool call assert.Equal(t, "assistant", got[0].Role) assert.Equal(t, "Let me search.", got[0].Content) if assert.Len(t, got[0].ToolCalls, 1) { assert.Equal(t, "call_1", got[0].ToolCalls[0].ID) assert.Equal(t, agenttools.ToolKnowledgeSearch, got[0].ToolCalls[0].Function.Name) assert.Contains(t, got[0].ToolCalls[0].Function.Arguments, "foo") } // 2. tool result paired with the call ID assert.Equal(t, "tool", got[1].Role) assert.Equal(t, "call_1", got[1].ToolCallID) assert.Equal(t, "doc A, doc B, doc C", got[1].Content) // 3. canonical final answer (final_answer tool call itself was filtered) assert.Equal(t, "assistant", got[2].Role) assert.Equal(t, "Found 3 matches in the docs.", got[2].Content) assert.Empty(t, got[2].ToolCalls) } // TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError ensures a // historical failed tool call is replayed as an "Error: …" tool message so the // model can see (and avoid retrying) the same failure path. func TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError(t *testing.T) { msg := &types.Message{ Role: "assistant", Content: "Sorry, I could not complete the search.", AgentSteps: types.AgentSteps{ { Iteration: 0, Thought: "Trying search.", ToolCalls: []types.ToolCall{ { ID: "call_err", Name: agenttools.ToolKnowledgeSearch, Args: map[string]interface{}{"query": "x"}, Result: &types.ToolResult{ Success: false, Error: "kb unreachable", }, }, }, }, }, } got := buildAssistantHistoryMessages(msg) if !assert.Len(t, got, 3) { return } assert.Equal(t, chat.Message{ Role: "tool", Content: "Error: kb unreachable", ToolCallID: "call_err", Name: agenttools.ToolKnowledgeSearch, }, got[1]) } // TestFilterNonTerminalToolCalls confirms a legacy final_answer entry is // dropped — every other tool (KB search, web search, MCP tools…) must survive. func TestFilterNonTerminalToolCalls(t *testing.T) { in := []types.ToolCall{ {Name: agenttools.ToolKnowledgeSearch}, {Name: "final_answer"}, {Name: agenttools.ToolWebSearch}, } out := filterNonTerminalToolCalls(in) if assert.Len(t, out, 2) { assert.Equal(t, agenttools.ToolKnowledgeSearch, out[0].Name) 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"}, ProviderMetadata: types.ToolCallMetadata{"google": json.RawMessage(`{"thought_signature":"gemini-history-signature"}`)}, 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)") require.Len(t, got[0].ToolCalls, 1) assert.JSONEq(t, `{"thought_signature":"gemini-history-signature"}`, string(got[0].ToolCalls[0].ProviderMetadata["google"])) // Tool message and final answer message must NOT carry reasoning_content. assert.Empty(t, got[1].ReasoningContent) assert.Empty(t, got[2].ReasoningContent) }