fix(chat): enforce retrieved image output in answers

When retrieved context or tool results contain Markdown images, dynamically
append output requirements to system prompts and final-answer instructions.
Also normalize fullwidth image parentheses in the frontend renderer so
localized model output still renders correctly.
This commit is contained in:
wizardchen
2026-07-15 18:09:17 +08:00
committed by lyingbug
parent 951f95903b
commit fbc9db85d1
11 changed files with 264 additions and 5 deletions
+32
View File
@@ -3,6 +3,7 @@ package agent
import (
"context"
"encoding/json"
"strings"
"testing"
"time"
@@ -162,6 +163,37 @@ func TestAppendToolResults_PreservesReasoningContent(t *testing.T) {
})
}
func TestAppendToolResults_AddsDynamicImageRequirementToCustomSystemPrompt(t *testing.T) {
engine := &AgentEngine{}
prior := []chat.Message{
{Role: "system", Content: "Custom agent prompt."},
{Role: "user", Content: "解释流程"},
}
step := types.AgentStep{
ToolCalls: []types.ToolCall{{
ID: "call-image",
Name: "knowledge_search",
Result: &types.ToolResult{
Success: true,
Output: "结果\n![流程图](resource://AbCdEfGhIjKlMnOpQrStUv)",
},
}},
}
out := engine.appendToolResults(prior, step)
require.Len(t, out, 4)
assert.Contains(t, out[0].Content, "Custom agent prompt.")
assert.Contains(t, out[0].Content, agentRetrievedImageRequirementMarker)
assert.Contains(t, out[0].Content, "MUST include at least one relevant Markdown image")
assert.Contains(t, out[0].Content, "ASCII half-width parentheses")
assert.Equal(t, "tool", out[3].Role)
assert.Contains(t, out[3].Content, "![流程图](resource://AbCdEfGhIjKlMnOpQrStUv)")
// A later image-bearing step must not duplicate the system requirement.
out = engine.appendToolResults(out, step)
assert.Equal(t, 1, strings.Count(out[0].Content, agentRetrievedImageRequirementMarker))
}
func TestBuildRuntimeContextBlock_PinnedDocuments(t *testing.T) {
block := buildRuntimeContextBlock(
"sess-1",