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
+33
View File
@@ -8,6 +8,7 @@ import (
"time"
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/llmreference"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/assert"
@@ -271,6 +272,38 @@ func TestRenderUserTurnContent_IncludesScopeBlocks(t *testing.T) {
assert.Contains(t, out, "hello")
}
func TestBuildMessagesWithLLMContextRegistersBoundScopeBeforeFirstModelCall(t *testing.T) {
engine := &AgentEngine{
sourceRefs: llmreference.NewRegistry(),
knowledgeBasesInfo: []*KnowledgeBaseInfo{{
ID: "kb-real-id",
Name: "Docs",
RecentDocs: []RecentDocInfo{{
ChunkID: "chunk-real-id",
KnowledgeID: "doc-real-id",
KnowledgeBaseID: "kb-real-id",
Title: "Guide",
}},
}},
selectedDocs: []*SelectedDocumentInfo{{
KnowledgeID: "selected-doc-real-id",
KnowledgeBaseID: "kb-real-id",
Title: "Selected",
}},
}
messages := engine.buildMessagesWithLLMContext("system", "question", "session", nil, nil)
require.Len(t, messages, 2)
userContent := messages[1].Content
assert.Contains(t, userContent, `knowledge_base id="b1"`)
assert.Contains(t, userContent, `knowledge_id="d1"`)
assert.Contains(t, userContent, `knowledge_id="d2"`)
assert.Equal(t, "c1", engine.sourceRefs.ChunkAlias("chunk-real-id"))
assert.NotContains(t, userContent, "kb-real-id")
assert.NotContains(t, userContent, "chunk-real-id")
assert.NotContains(t, userContent, "doc-real-id")
}
func TestBuildMustUseBlock_MultiWordServicePrefix(t *testing.T) {
// Service "My Service" -> tools mcp_my_service_*; the prefix must be the
// full service slug, not the first underscore segment (mcp_my_).