feat(llm): introduce request-local source reference aliases

Replace raw chunk/document/KB IDs in LLM context with short cN/dN/bN/wN
aliases, decode tool arguments on the way in, and expand compact <ref/>
citations to the public <kb>/<web> format before user-facing output.
This commit is contained in:
wizardchen
2026-07-16 21:23:20 +08:00
committed by lyingbug
parent 79e8f3d894
commit 014164ae44
33 changed files with 1962 additions and 149 deletions
@@ -11,12 +11,9 @@ import (
"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) {
// TestBuildUserHistoryMessage_IgnoresLegacyRenderedContent verifies that old
// prompt/context snapshots never re-enter the current model context.
func TestBuildUserHistoryMessage_IgnoresLegacyRenderedContent(t *testing.T) {
msg := &types.Message{
Role: "user",
Content: "what about the chart?",
@@ -27,7 +24,8 @@ func TestBuildUserHistoryMessage_PrefersRenderedContent(t *testing.T) {
}
got := buildUserHistoryMessage(msg)
assert.Equal(t, "user", got.Role)
assert.Equal(t, "what about the chart? [augmented]", got.Content)
assert.Equal(t, "what about the chart?\n\n[用户上传图片内容]\na bar chart", got.Content)
assert.NotContains(t, got.Content, "[augmented]")
}
func TestBuildUserHistoryMessage_FallsBackToContentWithCaptions(t *testing.T) {
@@ -69,10 +67,10 @@ func TestBuildUserHistoryMessage_AppendsAttachmentsWhenNoRenderedContent(t *test
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) {
// TestBuildUserHistoryMessage_RenderedContentRebuildsAttachment verifies that
// canonical attachment data is retained while the legacy rendered snapshot is
// discarded.
func TestBuildUserHistoryMessage_RenderedContentRebuildsAttachment(t *testing.T) {
msg := &types.Message{
Role: "user",
Content: "summarize this",
@@ -82,8 +80,9 @@ func TestBuildUserHistoryMessage_RenderedContentSkipsAttachmentReplay(t *testing
},
}
got := buildUserHistoryMessage(msg)
assert.Equal(t, "summarize this [with retrieval context already included]", got.Content)
assert.NotContains(t, got.Content, "<attachment")
assert.Contains(t, got.Content, "summarize this")
assert.NotContains(t, got.Content, "[with retrieval context already included]")
assert.Contains(t, got.Content, "<attachment")
}
// TestBuildAssistantHistoryMessages_NaturalFinishEmitsSingleAnswer covers the