refactor(modelcontext): fold llmreference into modelcontext as source codec

Mechanical move: internal/llmreference becomes in-package code.
registry.go splits into sources.go (handle registration, tool-argument
codec, replay encoding) and citations.go (protocol prompts, <ref/> ->
<kb/>/<web/> expansion, stream expander); model_output.go moves verbatim.

Renames only: Registry -> sourceRegistry, StreamExpander ->
citationStreamExpander, ProtocolPrompt -> sourceProtocolPrompt,
ToolArgumentPolicy -> toolArgumentPolicy. ChunkReference becomes a native
modelcontext type (was an alias). Unreferenced DocumentAlias,
KnowledgeBaseAlias and ExpandResponse are dropped. Tests move intact.
main
wizardchen 2026-07-27 01:04:19 +08:00 committed by lyingbug
parent c01b00313b
commit 3b9de5de8e
5 changed files with 364 additions and 375 deletions

View File

@ -0,0 +1,291 @@
// citations.go owns the public-citation surface of the model context: the
// system protocol prompt, expansion of private <ref/> handles into canonical
// <kb/> / <web/> tags, re-compaction of canonical tags replayed from history,
// and the stream expander that keeps partial tags off the wire.
package modelcontext
import (
"fmt"
"html"
"regexp"
"strings"
)
const sourceAliasProtocolPrompt = `
## Source handling protocol (system-owned)
Retrieved content uses request-local source handles: cN identifies a knowledge chunk, wN a web page, dN a document, and bN a knowledge base.
- Use dN and bN only as tool arguments when a tool requests a document or knowledge base.
- Never reveal raw chunk IDs, knowledge IDs, knowledge-base IDs, or private source handles in user-visible output. This does not change separate instructions to preserve retrieved Markdown image URLs.`
const citationEnabledProtocolPrompt = `
- Source citations are enabled for this answer. Cite a knowledge chunk with exactly <ref id="cN"/> and a web page with exactly <ref id="wN"/>.
- Copy only cN/wN handles that appeared in supplied context or tool results. Never cite dN/bN.
- Never output <kb> or <web> tags yourself; the system expands valid <ref/> tags after generation.
- Keep each <ref/> inline on the same line as the claim it supports. Do not group citations at the end.
- These rules supersede earlier, saved, or custom prompt instructions about citation syntax.`
const citationDisabledProtocolPrompt = `
- Source citations are disabled for this answer. Do not output <ref>, <kb>, <web>, raw source URLs, or source-handle citations.
- These rules supersede earlier, saved, or custom prompt instructions that require source citations.`
// ProtocolPrompt returns the internal, non-user-editable source protocol for a
// model call. Citation formatting stays out of custom and template prompts.
func sourceProtocolPrompt(citationsEnabled bool) string {
if citationsEnabled {
return sourceAliasProtocolPrompt + citationEnabledProtocolPrompt
}
return sourceAliasProtocolPrompt + citationDisabledProtocolPrompt
}
// ProtocolPrompt returns the source protocol configured for this registry.
// Request lifecycle code should normally call this through Registry.
func (r *sourceRegistry) ProtocolPrompt() string {
if r == nil {
return ""
}
return sourceProtocolPrompt(r.citationsEnabled)
}
var (
publicKBTagRE = regexp.MustCompile(`(?is)<kb\b[^>]*>`)
publicWebTagRE = regexp.MustCompile(`(?is)<web\b[^>]*>`)
docAttrRE = regexp.MustCompile(`(?i)\bdoc\s*=\s*"([^"]*)"`)
chunkAttrRE = regexp.MustCompile(`(?i)\bchunk_id\s*=\s*"([^"]+)"`)
publicKBAttrRE = regexp.MustCompile(`(?i)\bkb_id\s*=\s*"([^"]*)"`)
urlAttrRE = regexp.MustCompile(`(?i)\burl\s*=\s*"([^"]+)"`)
titleAttrRE = regexp.MustCompile(`(?i)\btitle\s*=\s*"([^"]*)"`)
legacyChunkRE = regexp.MustCompile(`(?is)<(?:chunk|faq)\b[^>]*>`)
faqAttrRE = regexp.MustCompile(`(?i)\bfaq_id\s*=\s*"([^"]+)"`)
knowledgeTitleAttrRE = regexp.MustCompile(`(?i)\bknowledge_title\s*=\s*"([^"]*)"`)
)
func (r *sourceRegistry) registerLegacyToolReferences(text string) {
if r == nil || text == "" {
return
}
r.registerLabeledReferences(text)
for _, tag := range legacyChunkRE.FindAllString(text, -1) {
chunkID := firstNonEmpty(publicAttr(chunkAttrRE, tag), publicAttr(faqAttrRE, tag))
if chunkID == "" {
continue
}
r.RegisterChunk(ChunkReference{
ChunkID: chunkID,
KnowledgeID: publicAttr(documentAttrRE, tag),
KnowledgeBaseID: firstNonEmpty(publicAttr(kbAttrRE, tag), publicAttr(publicKBAttrRE, tag)),
DocumentTitle: firstNonEmpty(publicAttr(knowledgeTitleAttrRE, tag), publicAttr(docAttrRE, tag)),
})
}
}
// CompactPublicCitations folds canonical citations from prior assistant turns
// back into this request's private protocol. This prevents durable chunk IDs
// and web URLs in conversation history from becoming model-visible again.
func (r *sourceRegistry) CompactPublicCitations(text string) string {
if r == nil || text == "" {
return text
}
text = publicKBTagRE.ReplaceAllStringFunc(text, func(tag string) string {
chunkID := publicAttr(chunkAttrRE, tag)
if chunkID == "" {
return tag
}
alias := r.RegisterChunk(ChunkReference{
ChunkID: chunkID,
KnowledgeBaseID: publicAttr(publicKBAttrRE, tag),
DocumentTitle: publicAttr(docAttrRE, tag),
})
return `<ref id="` + alias + `"/>`
})
return publicWebTagRE.ReplaceAllStringFunc(text, func(tag string) string {
rawURL := publicAttr(urlAttrRE, tag)
if rawURL == "" {
return tag
}
alias := r.RegisterWeb(rawURL, publicAttr(titleAttrRE, tag))
return `<ref id="` + alias + `"/>`
})
}
func publicAttr(expression *regexp.Regexp, tag string) string {
match := expression.FindStringSubmatch(tag)
if len(match) != 2 {
return ""
}
return html.UnescapeString(match[1])
}
var (
refTagRE = regexp.MustCompile(`(?i)<ref\s+id\s*=\s*"([^"]+)"\s*/?>`)
refCandidateRE = regexp.MustCompile(`(?is)<ref(?:\s|$)[^>]*(?:>|$)`)
modelKBTagRE = regexp.MustCompile(`(?is)<kb(?:\s|$)[^>]*(?:>|$)`)
modelWebTagRE = regexp.MustCompile(`(?is)<web(?:\s|$)[^>]*(?:>|$)`)
)
var (
documentAttrRE = regexp.MustCompile(`(?i)\bknowledge_id\s*=\s*"([^"]+)"`)
documentElementRE = regexp.MustCompile(`(?is)<knowledge_id>\s*([^<]+?)\s*</knowledge_id>`)
kbAttrRE = regexp.MustCompile(`(?i)\b(?:knowledge_base_id|kb_id)\s*=\s*"([^"]+)"`)
kbElementRE = regexp.MustCompile(`(?is)<(?:knowledge_base_id|kb_id)>\s*([^<]+?)\s*</(?:knowledge_base_id|kb_id)>`)
)
// registerLabeledReferences covers metadata-oriented tools that do not have a
// dedicated compact renderer. Only explicit ID labels are recognized; UUID-like
// text in retrieved content is never guessed to be a source identifier.
func (r *sourceRegistry) registerLabeledReferences(text string) {
if r == nil || text == "" {
return
}
for _, expression := range []*regexp.Regexp{documentAttrRE, documentElementRE} {
for _, match := range expression.FindAllStringSubmatch(text, -1) {
if len(match) == 2 {
r.RegisterDocument(strings.TrimSpace(match[1]))
}
}
}
for _, expression := range []*regexp.Regexp{kbAttrRE, kbElementRE} {
for _, match := range expression.FindAllStringSubmatch(text, -1) {
if len(match) == 2 {
r.RegisterKnowledgeBase(strings.TrimSpace(match[1]))
}
}
}
}
// ExpandText converts the private model protocol into the existing public
// <kb/> / <web/> contract. Unknown aliases fail closed and disappear.
func (r *sourceRegistry) ExpandText(text string) string {
if r == nil || text == "" {
return text
}
// Public citation tags are output-only. Drop any instance written directly
// by the model, then create canonical tags solely from registered aliases.
text = modelKBTagRE.ReplaceAllString(text, "")
text = modelWebTagRE.ReplaceAllString(text, "")
if !r.citationsEnabled {
return refCandidateRE.ReplaceAllString(text, "")
}
return refCandidateRE.ReplaceAllStringFunc(text, func(tag string) string {
match := refTagRE.FindStringSubmatch(tag)
if len(match) != 2 {
return ""
}
alias := strings.ToLower(match[1])
r.mu.RLock()
chunkRef := r.chunkByAlias[alias]
webRef := r.webByAlias[alias]
r.mu.RUnlock()
if chunkRef != nil {
attrs := fmt.Sprintf(`doc="%s" chunk_id="%s"`, escapeAttr(chunkRef.DocumentTitle), escapeAttr(chunkRef.ChunkID))
if chunkRef.KnowledgeBaseID != "" {
attrs += fmt.Sprintf(` kb_id="%s"`, escapeAttr(chunkRef.KnowledgeBaseID))
}
return "<kb " + attrs + " />"
}
if webRef != nil {
return fmt.Sprintf(`<web url="%s" title="%s" />`, escapeAttr(webRef.URL), escapeAttr(webRef.Title))
}
return ""
})
}
func escapeAttr(value string) string { return html.EscapeString(value) }
// citationStreamExpander prevents partial private <ref/> tags from reaching SSE while
// preserving normal streaming for all other content.
type citationStreamExpander struct {
registry *sourceRegistry
pending string
}
func newCitationStreamExpander(registry *sourceRegistry) *citationStreamExpander {
return &citationStreamExpander{registry: registry}
}
func (d *citationStreamExpander) Feed(chunk string) string {
if d == nil || d.registry == nil {
return chunk
}
data := d.pending + chunk
d.pending = ""
var out strings.Builder
for data != "" {
idx := strings.Index(data, "<")
if idx < 0 {
out.WriteString(data)
break
}
out.WriteString(data[:idx])
data = data[idx:]
lower := strings.ToLower(data)
if isSourceTagPending(lower) && !strings.Contains(data, ">") {
d.pending = data
break
}
if isRefTagStart(lower) {
end := strings.IndexByte(data, '>')
if end < 0 {
d.pending = data
break
}
tag := data[:end+1]
if refTagRE.MatchString(tag) {
out.WriteString(d.registry.ExpandText(tag))
}
data = data[end+1:]
continue
}
if isNamedTagStart(lower, "kb") || isNamedTagStart(lower, "web") {
end := strings.IndexByte(data, '>')
if end < 0 {
d.pending = data
break
}
data = data[end+1:]
continue
}
out.WriteByte('<')
data = data[1:]
}
return out.String()
}
func isRefTagStart(value string) bool {
return isNamedTagStart(value, "ref")
}
func isNamedTagStart(value, name string) bool {
prefix := "<" + name
if !strings.HasPrefix(value, prefix) {
return false
}
if len(value) == len(prefix) {
return true
}
next := value[len(prefix)]
return next == ' ' || next == '\t' || next == '\r' || next == '\n' || next == '>'
}
func isSourceTagPending(value string) bool {
for _, name := range []string{"ref", "kb", "web"} {
prefix := "<" + name
if (len(value) <= len(prefix) && strings.HasPrefix(prefix, value)) || isNamedTagStart(value, name) {
return true
}
}
return false
}
func (d *citationStreamExpander) Flush() string {
if d == nil {
return ""
}
pending := d.pending
d.pending = ""
lower := strings.ToLower(pending)
if isSourceTagPending(lower) {
return ""
}
return d.registry.ExpandText(pending)
}

View File

@ -1,4 +1,4 @@
package llmreference
package modelcontext
import (
"encoding/json"
@ -11,7 +11,7 @@ import (
// ModelOutput returns a compact, source-centric representation for the LLM.
// The canonical ToolResult.Output remains untouched for UI, logs and storage.
func (r *Registry) ModelOutput(result *types.ToolResult) string {
func (r *sourceRegistry) ModelOutput(result *types.ToolResult) string {
if result == nil {
return ""
}
@ -49,7 +49,7 @@ func (r *Registry) ModelOutput(result *types.ToolResult) string {
// registerStructuredReferences registers durable IDs found under explicitly
// labeled keys of a JSON tool result, using the same key dispatch as tool
// arguments. Non-JSON output is a no-op.
func (r *Registry) registerStructuredReferences(raw string) {
func (r *sourceRegistry) registerStructuredReferences(raw string) {
var value interface{}
if json.Unmarshal([]byte(raw), &value) != nil {
return
@ -72,7 +72,7 @@ func (r *Registry) registerStructuredReferences(raw string) {
walk("", value)
}
func (r *Registry) modelDatabaseQueryOutput(rows []map[string]interface{}, fallback string) string {
func (r *sourceRegistry) modelDatabaseQueryOutput(rows []map[string]interface{}, fallback string) string {
for _, row := range rows {
for key, raw := range row {
if value, ok := raw.(string); ok {
@ -83,7 +83,7 @@ func (r *Registry) modelDatabaseQueryOutput(rows []map[string]interface{}, fallb
return r.CompactKnownText(fallback)
}
func (r *Registry) modelDocumentInfoOutput(rows []map[string]interface{}, fallback string) string {
func (r *sourceRegistry) modelDocumentInfoOutput(rows []map[string]interface{}, fallback string) string {
if len(rows) == 0 {
return r.CompactKnownText(fallback)
}
@ -164,7 +164,7 @@ type modelChunk struct {
inputOrder int
}
func (r *Registry) modelKnowledgeOutput(mode string, rows []map[string]interface{}, fallback string) string {
func (r *sourceRegistry) modelKnowledgeOutput(mode string, rows []map[string]interface{}, fallback string) string {
chunks := make([]modelChunk, 0, len(rows))
for idx, row := range rows {
chunkID := firstNonEmpty(stringValue(row, "chunk_id"), stringValue(row, "faq_id"), stringValue(row, "id"))
@ -225,7 +225,7 @@ func viewForRow(row map[string]interface{}, mode string) string {
return "match"
}
func (r *Registry) modelKnowledgeChunksOutput(data map[string]interface{}, fallback string) string {
func (r *sourceRegistry) modelKnowledgeChunksOutput(data map[string]interface{}, fallback string) string {
rows := mapsValue(data["chunks"])
title := stringValue(data, "knowledge_title")
knowledgeID := stringValue(data, "knowledge_id")
@ -323,7 +323,7 @@ func renderKnowledgeChunks(mode string, chunks []modelChunk) string {
return b.String()
}
func (r *Registry) modelWebSearchOutput(rows []map[string]interface{}, fallback string) string {
func (r *sourceRegistry) modelWebSearchOutput(rows []map[string]interface{}, fallback string) string {
if len(rows) == 0 {
return r.CompactKnownText(fallback)
}
@ -353,7 +353,7 @@ func (r *Registry) modelWebSearchOutput(rows []map[string]interface{}, fallback
return b.String()
}
func (r *Registry) modelWebFetchOutput(rows []map[string]interface{}, fallback string) string {
func (r *sourceRegistry) modelWebFetchOutput(rows []map[string]interface{}, fallback string) string {
if len(rows) == 0 {
return r.CompactKnownText(fallback)
}

View File

@ -14,7 +14,6 @@ import (
"reflect"
"sort"
"github.com/Tencent/WeKnora/internal/llmreference"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
)
@ -33,10 +32,6 @@ Some durable resources and high-entropy Wiki slugs are represented by request-lo
- Copy only handles that appeared in supplied context or tool results, preserving them exactly in links, images, and tool arguments.
- Never invent, edit, or expand any handle. The system restores it after generation.`
// ChunkReference describes a retrieved chunk registered in model context.
// It aliases the codec type so callers do not need to import llmreference.
type ChunkReference = llmreference.ChunkReference
// Registry is the single request-scoped boundary between durable application
// identities and temporary model handles.
//
@ -45,7 +40,7 @@ type ChunkReference = llmreference.ChunkReference
// protected as one resource-like handle before the embedded document ID can be
// compacted to dN. Callers therefore cannot accidentally reverse the codecs.
type Registry struct {
sources *llmreference.Registry
sources *sourceRegistry
resources *resourceRegistry
issues *HandleTable
}
@ -53,7 +48,7 @@ type Registry struct {
// NewRegistry creates a registry for one model request/Agent execution.
func NewRegistry(citationsEnabled bool) *Registry {
return &Registry{
sources: llmreference.NewRegistry(citationsEnabled),
sources: newSourceRegistry(citationsEnabled),
resources: newResourceRegistry(),
issues: NewHandleTable("i", 0, 1),
}
@ -174,7 +169,7 @@ func (r *Registry) StreamDecoder() *StreamDecoder {
}
return &StreamDecoder{
resources: newResourceStreamDecoder(r.resources),
sources: llmreference.NewStreamExpander(r.sources),
sources: newCitationStreamExpander(r.sources),
issues: NewHandleStreamDecoder(r.issues),
orphans: &orphanResourceStreamFilter{},
}
@ -299,7 +294,7 @@ func (r *Registry) DecodeOutputText(text string) string {
// so callers cannot split handle processing or apply stages in the wrong order.
type StreamDecoder struct {
resources *resourceStreamDecoder
sources *llmreference.StreamExpander
sources *citationStreamExpander
issues *HandleStreamDecoder
orphans *orphanResourceStreamFilter
}

View File

@ -1,12 +1,13 @@
// Package llmreference is the low-level source codec used by modelcontext.
// Application request lifecycles should depend on modelcontext.Registry so
// source and resource handles cannot be encoded or decoded out of order.
package llmreference
// sources.go is the source-reference half of the model-context registry:
// request-local cN/dN/bN/wN handles for chunks, documents, knowledge bases
// and web pages, plus the tool-argument codec that maps them back to durable
// identifiers. Request lifecycles use Registry so source and resource handles
// cannot be encoded or decoded out of order.
package modelcontext
import (
"encoding/json"
"fmt"
"html"
"net/url"
"regexp"
"sort"
@ -17,33 +18,6 @@ import (
"github.com/Tencent/WeKnora/internal/types"
)
const sourceAliasProtocolPrompt = `
## Source handling protocol (system-owned)
Retrieved content uses request-local source handles: cN identifies a knowledge chunk, wN a web page, dN a document, and bN a knowledge base.
- Use dN and bN only as tool arguments when a tool requests a document or knowledge base.
- Never reveal raw chunk IDs, knowledge IDs, knowledge-base IDs, or private source handles in user-visible output. This does not change separate instructions to preserve retrieved Markdown image URLs.`
const citationEnabledProtocolPrompt = `
- Source citations are enabled for this answer. Cite a knowledge chunk with exactly <ref id="cN"/> and a web page with exactly <ref id="wN"/>.
- Copy only cN/wN handles that appeared in supplied context or tool results. Never cite dN/bN.
- Never output <kb> or <web> tags yourself; the system expands valid <ref/> tags after generation.
- Keep each <ref/> inline on the same line as the claim it supports. Do not group citations at the end.
- These rules supersede earlier, saved, or custom prompt instructions about citation syntax.`
const citationDisabledProtocolPrompt = `
- Source citations are disabled for this answer. Do not output <ref>, <kb>, <web>, raw source URLs, or source-handle citations.
- These rules supersede earlier, saved, or custom prompt instructions that require source citations.`
// ProtocolPrompt returns the internal, non-user-editable source protocol for a
// model call. Citation formatting stays out of custom and template prompts.
func ProtocolPrompt(citationsEnabled bool) string {
if citationsEnabled {
return sourceAliasProtocolPrompt + citationEnabledProtocolPrompt
}
return sourceAliasProtocolPrompt + citationDisabledProtocolPrompt
}
type ChunkReference struct {
Alias string
ChunkID string
@ -60,9 +34,9 @@ type WebReference struct {
Title string
}
// Registry is scoped to one assistant response (including every Agent tool
// sourceRegistry is scoped to one assistant response (including every Agent tool
// round). Aliases are never persisted or accepted across requests.
type Registry struct {
type sourceRegistry struct {
mu sync.RWMutex
citationsEnabled bool
@ -77,12 +51,12 @@ type Registry struct {
webByAlias map[string]*WebReference
}
func NewRegistry(citationsEnabled ...bool) *Registry {
func newSourceRegistry(citationsEnabled ...bool) *sourceRegistry {
enabled := true
if len(citationsEnabled) > 0 {
enabled = citationsEnabled[0]
}
return &Registry{
return &sourceRegistry{
citationsEnabled: enabled,
chunkByID: make(map[string]*ChunkReference),
chunkByAlias: make(map[string]*ChunkReference),
@ -95,16 +69,7 @@ func NewRegistry(citationsEnabled ...bool) *Registry {
}
}
// ProtocolPrompt returns the source protocol configured for this registry.
// Request lifecycle code should normally call this through modelcontext.Registry.
func (r *Registry) ProtocolPrompt() string {
if r == nil {
return ""
}
return ProtocolPrompt(r.citationsEnabled)
}
func (r *Registry) Count() int {
func (r *sourceRegistry) Count() int {
if r == nil {
return 0
}
@ -113,7 +78,7 @@ func (r *Registry) Count() int {
return len(r.chunkByAlias) + len(r.webByAlias)
}
func (r *Registry) RegisterChunk(ref ChunkReference) string {
func (r *sourceRegistry) RegisterChunk(ref ChunkReference) string {
if r == nil {
return ""
}
@ -162,7 +127,7 @@ func mergeChunkReference(dst *ChunkReference, src ChunkReference) {
}
}
func (r *Registry) RegisterDocument(id string) string {
func (r *sourceRegistry) RegisterDocument(id string) string {
id = strings.TrimSpace(id)
if r == nil || id == "" {
return ""
@ -188,7 +153,7 @@ func (r *Registry) RegisterDocument(id string) string {
return alias
}
func (r *Registry) RegisterKnowledgeBase(id string) string {
func (r *sourceRegistry) RegisterKnowledgeBase(id string) string {
id = strings.TrimSpace(id)
if r == nil || id == "" {
return ""
@ -214,7 +179,7 @@ func (r *Registry) RegisterKnowledgeBase(id string) string {
return alias
}
func (r *Registry) RegisterWeb(rawURL, title string) string {
func (r *sourceRegistry) RegisterWeb(rawURL, title string) string {
rawURL = strings.TrimSpace(rawURL)
if r == nil || rawURL == "" {
return ""
@ -257,7 +222,7 @@ func canonicalWebURL(raw string) string {
return parsed.String()
}
func (r *Registry) RegisterSearchResults(results []*types.SearchResult) {
func (r *sourceRegistry) RegisterSearchResults(results []*types.SearchResult) {
for _, result := range results {
if result == nil {
continue
@ -284,7 +249,7 @@ func firstNonEmpty(values ...string) string {
return ""
}
func (r *Registry) ChunkAlias(id string) string {
func (r *sourceRegistry) ChunkAlias(id string) string {
r.mu.RLock()
defer r.mu.RUnlock()
if ref := r.chunkByID[id]; ref != nil {
@ -293,34 +258,22 @@ func (r *Registry) ChunkAlias(id string) string {
return ""
}
func (r *Registry) DocumentAlias(id string) string {
r.mu.RLock()
defer r.mu.RUnlock()
return r.docToAlias[id]
}
func (r *Registry) KnowledgeBaseAlias(id string) string {
r.mu.RLock()
defer r.mu.RUnlock()
return r.kbToAlias[id]
}
// DecodeToolCalls restores exact alias-valued JSON strings before tools parse
// or validate their arguments. It never performs substring replacement.
func (r *Registry) DecodeToolCalls(toolCalls []types.LLMToolCall) {
func (r *sourceRegistry) DecodeToolCalls(toolCalls []types.LLMToolCall) {
r.DecodeToolCallsWithPolicy(toolCalls, nil)
}
// ToolArgumentPolicy decides whether a source-bearing JSON key belongs to a
// toolArgumentPolicy decides whether a source-bearing JSON key belongs to a
// particular tool contract. A nil policy retains the codec's legacy generic
// behavior; application request lifecycles should provide an explicit policy
// through modelcontext.Registry.
type ToolArgumentPolicy func(toolName, key string) bool
// through Registry.
type toolArgumentPolicy func(toolName, key string) bool
// DecodeToolCallsWithPolicy restores aliases only for fields explicitly owned
// by the named tool. This prevents dynamic tools with coincidentally named
// fields from inheriting built-in source semantics.
func (r *Registry) DecodeToolCallsWithPolicy(toolCalls []types.LLMToolCall, policy ToolArgumentPolicy) {
func (r *sourceRegistry) DecodeToolCallsWithPolicy(toolCalls []types.LLMToolCall, policy toolArgumentPolicy) {
for i := range toolCalls {
toolName := toolCalls[i].Function.Name
toolCalls[i].Function.Arguments = r.decodeJSONWithPolicy(
@ -333,9 +286,9 @@ func (r *Registry) DecodeToolCallsWithPolicy(toolCalls []types.LLMToolCall, poli
// UnresolvedToolHandlesWithPolicy reports unknown handles only in fields that
// belong to the named tool's declared source contract.
func (r *Registry) UnresolvedToolHandlesWithPolicy(
func (r *sourceRegistry) UnresolvedToolHandlesWithPolicy(
toolName, raw string,
policy ToolArgumentPolicy,
policy toolArgumentPolicy,
) []string {
if strings.TrimSpace(raw) == "" {
return nil
@ -357,7 +310,7 @@ func (r *Registry) UnresolvedToolHandlesWithPolicy(
return result
}
func (r *Registry) collectUnresolvedToolHandles(
func (r *sourceRegistry) collectUnresolvedToolHandles(
key string,
value interface{},
seen map[string]struct{},
@ -385,15 +338,15 @@ func (r *Registry) collectUnresolvedToolHandles(
}
// EncodeMessages compacts known real identifiers in assistant tool-call replay.
func (r *Registry) EncodeMessages(messages []chat.Message) []chat.Message {
func (r *sourceRegistry) EncodeMessages(messages []chat.Message) []chat.Message {
return r.EncodeMessagesWithPolicies(messages, nil, nil)
}
// EncodeMessagesWithPolicies additionally gates source processing for tool
// result messages. A nil result policy retains the legacy generic behavior.
func (r *Registry) EncodeMessagesWithPolicies(
func (r *sourceRegistry) EncodeMessagesWithPolicies(
messages []chat.Message,
argumentPolicy ToolArgumentPolicy,
argumentPolicy toolArgumentPolicy,
resultPolicy func(toolName string) bool,
) []chat.Message {
if r == nil || len(messages) == 0 {
@ -447,12 +400,13 @@ func (r *Registry) EncodeMessagesWithPolicies(
}
var shortSourceAliasRE = regexp.MustCompile(`(?i)^[cdbw][1-9][0-9]*$`)
var shortSourceAliasInTextRE = regexp.MustCompile(`(?i)\b[cdbw][1-9][0-9]*\b`)
// DecodeKnownText restores registered source handles embedded in a structured
// expression such as a built-in SQL tool argument. It must not be used for
// arbitrary prose; modelcontext owns the small tool/key policy that calls it.
func (r *Registry) DecodeKnownText(text string) string {
func (r *sourceRegistry) DecodeKnownText(text string) string {
if r == nil || text == "" {
return text
}
@ -468,7 +422,7 @@ func (r *Registry) DecodeKnownText(text string) string {
// double-quoted, or backtick-quoted segments. It is intended for structured
// expressions such as SQL, where replacing an unquoted token could corrupt a
// legitimate table/column alias that happens to look like d1 or b2.
func (r *Registry) DecodeKnownQuotedText(text string) string {
func (r *sourceRegistry) DecodeKnownQuotedText(text string) string {
if r == nil || text == "" {
return text
}
@ -484,7 +438,7 @@ func (r *Registry) DecodeKnownQuotedText(text string) string {
// UnresolvedQuotedTextHandles reports alias-shaped values inside quoted
// structured-text segments that do not exist in this request registry.
func (r *Registry) UnresolvedQuotedTextHandles(text string) []string {
func (r *sourceRegistry) UnresolvedQuotedTextHandles(text string) []string {
if text == "" {
return nil
}
@ -540,7 +494,7 @@ func rewriteQuotedText(text string, rewrite func(string) string) string {
return out.String()
}
func (r *Registry) registerToolArguments(raw string, allowed func(string) bool) {
func (r *sourceRegistry) registerToolArguments(raw string, allowed func(string) bool) {
if r == nil || strings.TrimSpace(raw) == "" {
return
}
@ -551,7 +505,7 @@ func (r *Registry) registerToolArguments(raw string, allowed func(string) bool)
r.registerToolArgumentValue("", value, allowed)
}
func (r *Registry) registerToolArgumentValue(key string, value interface{}, allowed func(string) bool) {
func (r *sourceRegistry) registerToolArgumentValue(key string, value interface{}, allowed func(string) bool) {
switch typed := value.(type) {
case string:
if allowed(strings.ToLower(key)) {
@ -572,7 +526,7 @@ func (r *Registry) registerToolArgumentValue(key string, value interface{}, allo
// arguments, structured tool results, and database rows. Keeping one dispatch
// stops the recognized key set (and the http/https guard for web references)
// from drifting between call sites; it must stay in sync with decodableKeys.
func (r *Registry) registerSourceIDByKey(key, value string) {
func (r *sourceRegistry) registerSourceIDByKey(key, value string) {
value = strings.TrimSpace(value)
if value == "" || shortSourceAliasRE.MatchString(value) {
return
@ -597,76 +551,7 @@ func (r *Registry) registerSourceIDByKey(key, value string) {
}
}
var (
publicKBTagRE = regexp.MustCompile(`(?is)<kb\b[^>]*>`)
publicWebTagRE = regexp.MustCompile(`(?is)<web\b[^>]*>`)
docAttrRE = regexp.MustCompile(`(?i)\bdoc\s*=\s*"([^"]*)"`)
chunkAttrRE = regexp.MustCompile(`(?i)\bchunk_id\s*=\s*"([^"]+)"`)
publicKBAttrRE = regexp.MustCompile(`(?i)\bkb_id\s*=\s*"([^"]*)"`)
urlAttrRE = regexp.MustCompile(`(?i)\burl\s*=\s*"([^"]+)"`)
titleAttrRE = regexp.MustCompile(`(?i)\btitle\s*=\s*"([^"]*)"`)
legacyChunkRE = regexp.MustCompile(`(?is)<(?:chunk|faq)\b[^>]*>`)
faqAttrRE = regexp.MustCompile(`(?i)\bfaq_id\s*=\s*"([^"]+)"`)
knowledgeTitleAttrRE = regexp.MustCompile(`(?i)\bknowledge_title\s*=\s*"([^"]*)"`)
)
func (r *Registry) registerLegacyToolReferences(text string) {
if r == nil || text == "" {
return
}
r.registerLabeledReferences(text)
for _, tag := range legacyChunkRE.FindAllString(text, -1) {
chunkID := firstNonEmpty(publicAttr(chunkAttrRE, tag), publicAttr(faqAttrRE, tag))
if chunkID == "" {
continue
}
r.RegisterChunk(ChunkReference{
ChunkID: chunkID,
KnowledgeID: publicAttr(documentAttrRE, tag),
KnowledgeBaseID: firstNonEmpty(publicAttr(kbAttrRE, tag), publicAttr(publicKBAttrRE, tag)),
DocumentTitle: firstNonEmpty(publicAttr(knowledgeTitleAttrRE, tag), publicAttr(docAttrRE, tag)),
})
}
}
// CompactPublicCitations folds canonical citations from prior assistant turns
// back into this request's private protocol. This prevents durable chunk IDs
// and web URLs in conversation history from becoming model-visible again.
func (r *Registry) CompactPublicCitations(text string) string {
if r == nil || text == "" {
return text
}
text = publicKBTagRE.ReplaceAllStringFunc(text, func(tag string) string {
chunkID := publicAttr(chunkAttrRE, tag)
if chunkID == "" {
return tag
}
alias := r.RegisterChunk(ChunkReference{
ChunkID: chunkID,
KnowledgeBaseID: publicAttr(publicKBAttrRE, tag),
DocumentTitle: publicAttr(docAttrRE, tag),
})
return `<ref id="` + alias + `"/>`
})
return publicWebTagRE.ReplaceAllStringFunc(text, func(tag string) string {
rawURL := publicAttr(urlAttrRE, tag)
if rawURL == "" {
return tag
}
alias := r.RegisterWeb(rawURL, publicAttr(titleAttrRE, tag))
return `<ref id="` + alias + `"/>`
})
}
func publicAttr(expression *regexp.Regexp, tag string) string {
match := expression.FindStringSubmatch(tag)
if len(match) != 2 {
return ""
}
return html.UnescapeString(match[1])
}
func (r *Registry) decodeJSONWithPolicy(raw string, encode bool, allowed func(string) bool) string {
func (r *sourceRegistry) decodeJSONWithPolicy(raw string, encode bool, allowed func(string) bool) string {
if r == nil || strings.TrimSpace(raw) == "" {
return raw
}
@ -687,13 +572,14 @@ func (r *Registry) decodeJSONWithPolicy(raw string, encode bool, allowed func(st
// to these keys so that free-text values (e.g. a grep/search query that happens
// to equal "d1") are never rewritten into internal identifiers.
var decodableKeys = map[string]struct{}{
"chunk_id": {}, "faq_id": {}, "chunk_ids": {}, "faq_ids": {},
"knowledge_id": {}, "knowledge_ids": {}, "suspected_knowledge_ids": {}, "source_refs": {},
"knowledge_base": {}, "knowledge_base_id": {}, "knowledge_base_ids": {}, "kb_id": {}, "kb_ids": {},
"url": {}, "urls": {},
}
func (r *Registry) walkJSON(key string, value interface{}, encode bool, allowed func(string) bool) interface{} {
func (r *sourceRegistry) walkJSON(key string, value interface{}, encode bool, allowed func(string) bool) interface{} {
switch typed := value.(type) {
case string:
if !allowed(strings.ToLower(key)) {
@ -731,7 +617,7 @@ func (r *Registry) walkJSON(key string, value interface{}, encode bool, allowed
return value
}
func (r *Registry) aliasForRealValue(real string) string {
func (r *sourceRegistry) aliasForRealValue(real string) string {
r.mu.RLock()
defer r.mu.RUnlock()
if ref := r.chunkByID[real]; ref != nil {
@ -749,7 +635,7 @@ func (r *Registry) aliasForRealValue(real string) string {
return ""
}
func (r *Registry) realForAlias(alias string) string {
func (r *sourceRegistry) realForAlias(alias string) string {
alias = strings.ToLower(strings.TrimSpace(alias))
r.mu.RLock()
defer r.mu.RUnlock()
@ -771,7 +657,7 @@ func (r *Registry) realForAlias(alias string) string {
// CompactKnownText is intentionally limited to identifiers already registered
// from structured runtime/tool data. It is used for metadata envelopes, not
// arbitrary retrieved prose.
func (r *Registry) CompactKnownText(text string) string {
func (r *sourceRegistry) CompactKnownText(text string) string {
if r == nil || text == "" {
return text
}
@ -799,186 +685,3 @@ func (r *Registry) CompactKnownText(text string) string {
}
return text
}
var (
refTagRE = regexp.MustCompile(`(?i)<ref\s+id\s*=\s*"([^"]+)"\s*/?>`)
refCandidateRE = regexp.MustCompile(`(?is)<ref(?:\s|$)[^>]*(?:>|$)`)
modelKBTagRE = regexp.MustCompile(`(?is)<kb(?:\s|$)[^>]*(?:>|$)`)
modelWebTagRE = regexp.MustCompile(`(?is)<web(?:\s|$)[^>]*(?:>|$)`)
)
var (
documentAttrRE = regexp.MustCompile(`(?i)\bknowledge_id\s*=\s*"([^"]+)"`)
documentElementRE = regexp.MustCompile(`(?is)<knowledge_id>\s*([^<]+?)\s*</knowledge_id>`)
kbAttrRE = regexp.MustCompile(`(?i)\b(?:knowledge_base_id|kb_id)\s*=\s*"([^"]+)"`)
kbElementRE = regexp.MustCompile(`(?is)<(?:knowledge_base_id|kb_id)>\s*([^<]+?)\s*</(?:knowledge_base_id|kb_id)>`)
)
// registerLabeledReferences covers metadata-oriented tools that do not have a
// dedicated compact renderer. Only explicit ID labels are recognized; UUID-like
// text in retrieved content is never guessed to be a source identifier.
func (r *Registry) registerLabeledReferences(text string) {
if r == nil || text == "" {
return
}
for _, expression := range []*regexp.Regexp{documentAttrRE, documentElementRE} {
for _, match := range expression.FindAllStringSubmatch(text, -1) {
if len(match) == 2 {
r.RegisterDocument(strings.TrimSpace(match[1]))
}
}
}
for _, expression := range []*regexp.Regexp{kbAttrRE, kbElementRE} {
for _, match := range expression.FindAllStringSubmatch(text, -1) {
if len(match) == 2 {
r.RegisterKnowledgeBase(strings.TrimSpace(match[1]))
}
}
}
}
// ExpandText converts the private model protocol into the existing public
// <kb/> / <web/> contract. Unknown aliases fail closed and disappear.
func (r *Registry) ExpandText(text string) string {
if r == nil || text == "" {
return text
}
// Public citation tags are output-only. Drop any instance written directly
// by the model, then create canonical tags solely from registered aliases.
text = modelKBTagRE.ReplaceAllString(text, "")
text = modelWebTagRE.ReplaceAllString(text, "")
if !r.citationsEnabled {
return refCandidateRE.ReplaceAllString(text, "")
}
return refCandidateRE.ReplaceAllStringFunc(text, func(tag string) string {
match := refTagRE.FindStringSubmatch(tag)
if len(match) != 2 {
return ""
}
alias := strings.ToLower(match[1])
r.mu.RLock()
chunkRef := r.chunkByAlias[alias]
webRef := r.webByAlias[alias]
r.mu.RUnlock()
if chunkRef != nil {
attrs := fmt.Sprintf(`doc="%s" chunk_id="%s"`, escapeAttr(chunkRef.DocumentTitle), escapeAttr(chunkRef.ChunkID))
if chunkRef.KnowledgeBaseID != "" {
attrs += fmt.Sprintf(` kb_id="%s"`, escapeAttr(chunkRef.KnowledgeBaseID))
}
return "<kb " + attrs + " />"
}
if webRef != nil {
return fmt.Sprintf(`<web url="%s" title="%s" />`, escapeAttr(webRef.URL), escapeAttr(webRef.Title))
}
return ""
})
}
func escapeAttr(value string) string { return html.EscapeString(value) }
func (r *Registry) ExpandResponse(response *types.ChatResponse) {
if response == nil {
return
}
response.Content = r.ExpandText(response.Content)
response.ReasoningContent = r.ExpandText(response.ReasoningContent)
r.DecodeToolCalls(response.ToolCalls)
}
// StreamExpander prevents partial private <ref/> tags from reaching SSE while
// preserving normal streaming for all other content.
type StreamExpander struct {
registry *Registry
pending string
}
func NewStreamExpander(registry *Registry) *StreamExpander {
return &StreamExpander{registry: registry}
}
func (d *StreamExpander) Feed(chunk string) string {
if d == nil || d.registry == nil {
return chunk
}
data := d.pending + chunk
d.pending = ""
var out strings.Builder
for data != "" {
idx := strings.Index(data, "<")
if idx < 0 {
out.WriteString(data)
break
}
out.WriteString(data[:idx])
data = data[idx:]
lower := strings.ToLower(data)
if isSourceTagPending(lower) && !strings.Contains(data, ">") {
d.pending = data
break
}
if isRefTagStart(lower) {
end := strings.IndexByte(data, '>')
if end < 0 {
d.pending = data
break
}
tag := data[:end+1]
if refTagRE.MatchString(tag) {
out.WriteString(d.registry.ExpandText(tag))
}
data = data[end+1:]
continue
}
if isNamedTagStart(lower, "kb") || isNamedTagStart(lower, "web") {
end := strings.IndexByte(data, '>')
if end < 0 {
d.pending = data
break
}
data = data[end+1:]
continue
}
out.WriteByte('<')
data = data[1:]
}
return out.String()
}
func isRefTagStart(value string) bool {
return isNamedTagStart(value, "ref")
}
func isNamedTagStart(value, name string) bool {
prefix := "<" + name
if !strings.HasPrefix(value, prefix) {
return false
}
if len(value) == len(prefix) {
return true
}
next := value[len(prefix)]
return next == ' ' || next == '\t' || next == '\r' || next == '\n' || next == '>'
}
func isSourceTagPending(value string) bool {
for _, name := range []string{"ref", "kb", "web"} {
prefix := "<" + name
if (len(value) <= len(prefix) && strings.HasPrefix(prefix, value)) || isNamedTagStart(value, name) {
return true
}
}
return false
}
func (d *StreamExpander) Flush() string {
if d == nil {
return ""
}
pending := d.pending
d.pending = ""
lower := strings.ToLower(pending)
if isSourceTagPending(lower) {
return ""
}
return d.registry.ExpandText(pending)
}

View File

@ -1,4 +1,4 @@
package llmreference
package modelcontext
import (
"strings"
@ -10,7 +10,7 @@ import (
)
func TestRegistryChunkAliasIsStableAndExpandsCanonicalCitation(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
first := registry.RegisterChunk(ChunkReference{
ChunkID: "chunk-uuid-1",
KnowledgeID: "knowledge-uuid-1",
@ -33,7 +33,7 @@ func TestRegistryChunkAliasIsStableAndExpandsCanonicalCitation(t *testing.T) {
}
func TestRegisterDoesNotTreatModelAliasesAsNewDurableIdentities(t *testing.T) {
r := NewRegistry()
r := newSourceRegistry()
require.Equal(t, "c1", r.RegisterChunk(ChunkReference{ChunkID: "chunk-real"}))
require.Equal(t, "d1", r.RegisterDocument("doc-real"))
require.Equal(t, "b1", r.RegisterKnowledgeBase("kb-real"))
@ -52,12 +52,12 @@ func TestRegisterDoesNotTreatModelAliasesAsNewDurableIdentities(t *testing.T) {
}
func TestRegistrySuppressesSourceCitationsWhenDisabled(t *testing.T) {
registry := NewRegistry(false)
registry := newSourceRegistry(false)
registry.RegisterChunk(ChunkReference{ChunkID: "chunk-1", DocumentTitle: "Doc"})
registry.RegisterWeb("https://example.com", "Example")
require.Contains(t, ProtocolPrompt(false), "Source citations are disabled")
require.NotContains(t, ProtocolPrompt(false), `Cite a knowledge chunk with exactly`)
require.Contains(t, sourceProtocolPrompt(false), "Source citations are disabled")
require.NotContains(t, sourceProtocolPrompt(false), `Cite a knowledge chunk with exactly`)
require.Equal(t, "knowledge web ", registry.ExpandText(
`knowledge <ref id="c1"/> web <ref id="w1"/>`,
))
@ -67,7 +67,7 @@ func TestRegistrySuppressesSourceCitationsWhenDisabled(t *testing.T) {
}
func TestRegistryDecodesAliasesInNestedToolArguments(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
registry.RegisterDocument("knowledge-uuid-1")
registry.RegisterKnowledgeBase("kb-uuid-1")
registry.RegisterWeb("https://example.com/page", "Example")
@ -87,7 +87,7 @@ func TestRegistryDecodesAliasesInNestedToolArguments(t *testing.T) {
}
func TestDecodeToolCallsOnlyRewritesAliasBearingKeys(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
registry.RegisterDocument("knowledge-uuid-1")
registry.RegisterChunk(ChunkReference{ChunkID: "chunk-uuid-1"})
@ -107,9 +107,9 @@ func TestDecodeToolCallsOnlyRewritesAliasBearingKeys(t *testing.T) {
}
func TestStreamExpanderHoldsSplitReferenceAndDropsUnknown(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
registry.RegisterChunk(ChunkReference{ChunkID: "chunk-1", DocumentTitle: "Doc"})
expander := NewStreamExpander(registry)
expander := newCitationStreamExpander(registry)
require.Equal(t, "before ", expander.Feed(`before <ref id="`))
require.Equal(t, `<kb doc="Doc" chunk_id="chunk-1" /> after`, expander.Feed(`c1"/> after`))
@ -124,7 +124,7 @@ func TestStreamExpanderHoldsSplitReferenceAndDropsUnknown(t *testing.T) {
}
func TestEncodeMessagesCompactsCanonicalCitationsFromHistory(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
messages := []chat.Message{{
Role: "assistant",
Content: `Knowledge <kb doc="A &amp; B.pdf" chunk_id="chunk-real" kb_id="kb-real" />; ` +
@ -142,7 +142,7 @@ func TestEncodeMessagesCompactsCanonicalCitationsFromHistory(t *testing.T) {
}
func TestEncodeMessagesMigratesLegacyToolHistoryAtReadTime(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
messages := []chat.Message{
{
Role: "assistant",
@ -179,7 +179,7 @@ func TestEncodeMessagesMigratesLegacyToolHistoryAtReadTime(t *testing.T) {
}
func TestEncodeMessagesDoesNotTreatLegacyPromptExampleAsARealSource(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
messages := []chat.Message{{
Role: "system",
Content: `Old rule: cite <kb doc="..." chunk_id="..." />`,
@ -191,7 +191,7 @@ func TestEncodeMessagesDoesNotTreatLegacyPromptExampleAsARealSource(t *testing.T
}
func TestModelOutputGroupsChunksAndReusesAliasAcrossTools(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
search := &types.ToolResult{
Success: true,
Output: "raw UUID output",
@ -234,7 +234,7 @@ func TestModelOutputGroupsChunksAndReusesAliasAcrossTools(t *testing.T) {
}
func TestModelOutputWebAliasExpandsToWebCitation(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
output := registry.ModelOutput(&types.ToolResult{
Success: true,
Data: map[string]interface{}{
@ -253,7 +253,7 @@ func TestModelOutputWebAliasExpandsToWebCitation(t *testing.T) {
}
func TestModelOutputDocumentInfoUsesDocumentAndFAQAliases(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
result := &types.ToolResult{
Success: true,
Output: "raw IDs must not be used",
@ -288,7 +288,7 @@ func TestModelOutputDocumentInfoUsesDocumentAndFAQAliases(t *testing.T) {
}
func TestModelOutputCompactsLabeledWikiReferences(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
result := &types.ToolResult{
Success: true,
Output: `<wiki_page>
@ -309,7 +309,7 @@ func TestModelOutputCompactsLabeledWikiReferences(t *testing.T) {
}
func TestModelOutputGraphResultsUseChunkAliases(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
output := registry.ModelOutput(&types.ToolResult{
Success: true,
Data: map[string]interface{}{
@ -335,7 +335,7 @@ func TestModelOutputGraphResultsUseChunkAliases(t *testing.T) {
}
func TestModelOutputDoesNotRegisterInternalSchemesAsWebSources(t *testing.T) {
registry := NewRegistry()
registry := newSourceRegistry()
output := registry.ModelOutput(&types.ToolResult{
Success: true,
Output: `{"url":"res://0001","knowledge_id":"doc-real-id"}`,