2026-07-26 17:04:19 +00:00
|
|
|
// 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
|
2026-07-16 13:23:20 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/url"
|
|
|
|
|
"regexp"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
2026-09-09 16:31:43 +00:00
|
|
|
"sync"
|
2026-07-16 13:23:20 +00:00
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ChunkReference struct {
|
|
|
|
|
ChunkID string
|
|
|
|
|
KnowledgeID string
|
|
|
|
|
KnowledgeBaseID string
|
|
|
|
|
DocumentTitle string
|
|
|
|
|
ChunkIndex int
|
|
|
|
|
ChunkType string
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:13:08 +00:00
|
|
|
// webMeta is the per-web-page metadata stored next to the raw URL.
|
|
|
|
|
type webMeta struct {
|
|
|
|
|
title string
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
// sourceRegistry is scoped to one assistant response (including every Agent tool
|
2026-07-26 17:13:08 +00:00
|
|
|
// round). Handles are never persisted or accepted across requests.
|
2026-07-26 17:04:19 +00:00
|
|
|
type sourceRegistry struct {
|
2026-07-16 13:46:52 +00:00
|
|
|
citationsEnabled bool
|
2026-09-09 16:31:43 +00:00
|
|
|
// Addressable IDs from history, directory entries, and tool arguments are
|
|
|
|
|
// not evidence until a current tool result supplies the source.
|
|
|
|
|
citable sync.Map // handle -> true; registrations may run concurrently
|
2026-07-16 13:46:52 +00:00
|
|
|
|
2026-07-26 17:13:08 +00:00
|
|
|
chunks *handleTable[ChunkReference]
|
|
|
|
|
docs *handleTable[struct{}]
|
|
|
|
|
kbs *handleTable[struct{}]
|
|
|
|
|
webs *handleTable[webMeta]
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func newSourceRegistry(citationsEnabled ...bool) *sourceRegistry {
|
2026-07-16 13:46:52 +00:00
|
|
|
enabled := true
|
|
|
|
|
if len(citationsEnabled) > 0 {
|
|
|
|
|
enabled = citationsEnabled[0]
|
|
|
|
|
}
|
2026-07-26 17:04:19 +00:00
|
|
|
return &sourceRegistry{
|
2026-07-16 13:46:52 +00:00
|
|
|
citationsEnabled: enabled,
|
2026-07-26 17:13:08 +00:00
|
|
|
chunks: newHandleTable[ChunkReference]("c", 0, 1),
|
|
|
|
|
docs: newHandleTable[struct{}]("d", 0, 1),
|
|
|
|
|
kbs: newHandleTable[struct{}]("b", 0, 1),
|
|
|
|
|
webs: newHandleTable[webMeta]("w", 0, 1),
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) Count() int {
|
2026-07-16 13:23:20 +00:00
|
|
|
if r == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
return r.chunks.size() + r.webs.size()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// knownHandle implements the shared guard for handle-shaped registration
|
|
|
|
|
// input: a model-emitted handle is echoed back only when it already exists,
|
|
|
|
|
// and is never accepted as a new durable identity.
|
|
|
|
|
func knownHandle[M any](table *handleTable[M], id string) string {
|
|
|
|
|
handle := strings.ToLower(id)
|
|
|
|
|
if table.has(handle) {
|
|
|
|
|
return handle
|
|
|
|
|
}
|
|
|
|
|
return ""
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) RegisterChunk(ref ChunkReference) string {
|
2026-09-09 16:31:43 +00:00
|
|
|
return r.registerChunk(ref, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *sourceRegistry) registerChunk(ref ChunkReference, evidence bool) string {
|
2026-07-26 16:59:38 +00:00
|
|
|
if r == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
ref.ChunkID = strings.TrimSpace(ref.ChunkID)
|
|
|
|
|
if ref.ChunkID == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if shortSourceHandleRE.MatchString(ref.ChunkID) {
|
2026-07-26 17:13:08 +00:00
|
|
|
return knownHandle(r.chunks, ref.ChunkID)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-09-09 16:31:43 +00:00
|
|
|
handle := r.chunks.register(ref.ChunkID, ref.ChunkID, ref, mergeChunkReference)
|
|
|
|
|
if evidence {
|
|
|
|
|
r.citable.Store(handle, true)
|
|
|
|
|
}
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mergeChunkReference(dst *ChunkReference, src ChunkReference) {
|
|
|
|
|
if dst.KnowledgeID == "" {
|
|
|
|
|
dst.KnowledgeID = src.KnowledgeID
|
|
|
|
|
}
|
|
|
|
|
if dst.KnowledgeBaseID == "" {
|
|
|
|
|
dst.KnowledgeBaseID = src.KnowledgeBaseID
|
|
|
|
|
}
|
|
|
|
|
if dst.DocumentTitle == "" {
|
|
|
|
|
dst.DocumentTitle = src.DocumentTitle
|
|
|
|
|
}
|
|
|
|
|
if dst.ChunkIndex == 0 {
|
|
|
|
|
dst.ChunkIndex = src.ChunkIndex
|
|
|
|
|
}
|
|
|
|
|
if dst.ChunkType == "" {
|
|
|
|
|
dst.ChunkType = src.ChunkType
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) RegisterDocument(id string) string {
|
2026-07-26 16:59:38 +00:00
|
|
|
id = strings.TrimSpace(id)
|
|
|
|
|
if r == nil || id == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if shortSourceHandleRE.MatchString(id) {
|
2026-07-26 17:13:08 +00:00
|
|
|
return knownHandle(r.docs, id)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
return r.docs.register(id, id, struct{}{}, nil)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) RegisterKnowledgeBase(id string) string {
|
2026-07-26 16:59:38 +00:00
|
|
|
id = strings.TrimSpace(id)
|
|
|
|
|
if r == nil || id == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if shortSourceHandleRE.MatchString(id) {
|
2026-07-26 17:13:08 +00:00
|
|
|
return knownHandle(r.kbs, id)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
return r.kbs.register(id, id, struct{}{}, nil)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) RegisterWeb(rawURL, title string) string {
|
2026-09-09 16:31:43 +00:00
|
|
|
return r.registerWeb(rawURL, title, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *sourceRegistry) registerWeb(rawURL, title string, evidence bool) string {
|
2026-07-26 16:59:38 +00:00
|
|
|
rawURL = strings.TrimSpace(rawURL)
|
|
|
|
|
if r == nil || rawURL == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if shortSourceHandleRE.MatchString(rawURL) {
|
2026-07-26 17:13:08 +00:00
|
|
|
return knownHandle(r.webs, rawURL)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
// Dedup on the canonical (fragment-stripped) URL while decoding back to
|
|
|
|
|
// the raw URL the model was originally shown.
|
2026-09-09 16:31:43 +00:00
|
|
|
handle := r.webs.register(canonicalWebURL(rawURL), rawURL, webMeta{title: title}, func(dst *webMeta, src webMeta) {
|
2026-07-26 17:13:08 +00:00
|
|
|
if dst.title == "" && src.title != "" {
|
|
|
|
|
dst.title = src.title
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
})
|
2026-09-09 16:31:43 +00:00
|
|
|
if evidence {
|
|
|
|
|
r.citable.Store(handle, true)
|
|
|
|
|
}
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func canonicalWebURL(raw string) string {
|
|
|
|
|
parsed, err := url.Parse(strings.TrimSpace(raw))
|
|
|
|
|
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
|
|
|
|
return strings.TrimSpace(raw)
|
|
|
|
|
}
|
|
|
|
|
parsed.Fragment = ""
|
|
|
|
|
return parsed.String()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) RegisterSearchResults(results []*types.SearchResult) {
|
2026-07-16 13:23:20 +00:00
|
|
|
for _, result := range results {
|
|
|
|
|
if result == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
r.RegisterDocument(result.KnowledgeID)
|
|
|
|
|
r.RegisterKnowledgeBase(result.KnowledgeBaseID)
|
|
|
|
|
r.RegisterChunk(ChunkReference{
|
|
|
|
|
ChunkID: result.ID,
|
|
|
|
|
KnowledgeID: result.KnowledgeID,
|
|
|
|
|
KnowledgeBaseID: result.KnowledgeBaseID,
|
|
|
|
|
DocumentTitle: firstNonEmpty(result.KnowledgeTitle, result.KnowledgeFilename),
|
|
|
|
|
ChunkIndex: result.ChunkIndex,
|
|
|
|
|
ChunkType: result.ChunkType,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
if strings.TrimSpace(value) != "" {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
func (r *sourceRegistry) ChunkHandle(id string) string {
|
2026-07-26 17:13:08 +00:00
|
|
|
handle, _ := r.chunks.handleForKey(id)
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
// toolArgumentPolicy decides whether a source-bearing JSON key belongs to a
|
2026-07-26 17:08:11 +00:00
|
|
|
// particular tool contract. Request lifecycles always pass the per-tool policy
|
|
|
|
|
// (sourceArgumentAllowed); a nil policy allows every key and exists only for
|
|
|
|
|
// package-internal replay paths that predate per-tool contracts.
|
2026-07-26 17:04:19 +00:00
|
|
|
type toolArgumentPolicy func(toolName, key string) bool
|
2026-07-26 16:59:38 +00:00
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
// DecodeToolCallsWithPolicy restores handles only for fields explicitly owned
|
2026-07-26 16:59:38 +00:00
|
|
|
// by the named tool. This prevents dynamic tools with coincidentally named
|
|
|
|
|
// fields from inheriting built-in source semantics.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) DecodeToolCallsWithPolicy(toolCalls []types.LLMToolCall, policy toolArgumentPolicy) {
|
2026-07-16 13:23:20 +00:00
|
|
|
for i := range toolCalls {
|
2026-07-26 16:59:38 +00:00
|
|
|
toolName := toolCalls[i].Function.Name
|
|
|
|
|
toolCalls[i].Function.Arguments = r.decodeJSONWithPolicy(
|
|
|
|
|
toolCalls[i].Function.Arguments,
|
|
|
|
|
false,
|
|
|
|
|
func(key string) bool { return policy == nil || policy(toolName, key) },
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnresolvedToolHandlesWithPolicy reports unknown handles only in fields that
|
|
|
|
|
// belong to the named tool's declared source contract.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) UnresolvedToolHandlesWithPolicy(
|
2026-07-26 16:59:38 +00:00
|
|
|
toolName, raw string,
|
2026-07-26 17:04:19 +00:00
|
|
|
policy toolArgumentPolicy,
|
2026-07-26 16:59:38 +00:00
|
|
|
) []string {
|
|
|
|
|
if strings.TrimSpace(raw) == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var value interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
|
r.collectUnresolvedToolHandles(
|
|
|
|
|
"", value, seen,
|
|
|
|
|
func(key string) bool { return policy == nil || policy(toolName, key) },
|
|
|
|
|
)
|
|
|
|
|
result := make([]string, 0, len(seen))
|
|
|
|
|
for handle := range seen {
|
|
|
|
|
result = append(result, handle)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(result)
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) collectUnresolvedToolHandles(
|
2026-07-26 16:59:38 +00:00
|
|
|
key string,
|
|
|
|
|
value interface{},
|
|
|
|
|
seen map[string]struct{},
|
|
|
|
|
allowed func(string) bool,
|
|
|
|
|
) {
|
|
|
|
|
switch typed := value.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
key = strings.ToLower(key)
|
2026-07-26 17:08:11 +00:00
|
|
|
if _, ok := sourceKeySpaces[key]; !ok || !allowed(key) {
|
2026-07-26 16:59:38 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
handle := strings.TrimSpace(typed)
|
2026-07-26 17:17:36 +00:00
|
|
|
if shortSourceHandleRE.MatchString(handle) && (r == nil || r.durableForHandle(handle) == "") {
|
2026-07-26 16:59:38 +00:00
|
|
|
seen[handle] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
case []interface{}:
|
|
|
|
|
for _, item := range typed {
|
|
|
|
|
r.collectUnresolvedToolHandles(key, item, seen, allowed)
|
|
|
|
|
}
|
|
|
|
|
case map[string]interface{}:
|
|
|
|
|
for childKey, item := range typed {
|
|
|
|
|
r.collectUnresolvedToolHandles(childKey, item, seen, allowed)
|
|
|
|
|
}
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:08:11 +00:00
|
|
|
// EncodeMessagesWithPolicies compacts known real identifiers in replayed
|
|
|
|
|
// messages and gates source processing for tool results by tool name. A nil
|
|
|
|
|
// policy retains the legacy generic behavior for package-internal callers.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) EncodeMessagesWithPolicies(
|
2026-07-26 16:59:38 +00:00
|
|
|
messages []chat.Message,
|
2026-07-26 17:04:19 +00:00
|
|
|
argumentPolicy toolArgumentPolicy,
|
2026-07-26 16:59:38 +00:00
|
|
|
resultPolicy func(toolName string) bool,
|
|
|
|
|
) []chat.Message {
|
2026-07-16 13:23:20 +00:00
|
|
|
if r == nil || len(messages) == 0 {
|
|
|
|
|
return messages
|
|
|
|
|
}
|
|
|
|
|
out := make([]chat.Message, len(messages))
|
|
|
|
|
copy(out, messages)
|
|
|
|
|
// First register every durable identifier present in historical tool calls
|
|
|
|
|
// and canonical assistant citations. This two-pass shape lets an early tool
|
|
|
|
|
// message reuse metadata that appears only in the turn's final answer.
|
|
|
|
|
for i := range out {
|
2026-07-26 16:59:38 +00:00
|
|
|
processToolResult := out[i].Role == "tool" && (resultPolicy == nil || resultPolicy(out[i].Name))
|
|
|
|
|
if out[i].Role == "assistant" || processToolResult {
|
2026-09-09 16:31:43 +00:00
|
|
|
out[i].Content = r.CompactPublicCitations(out[i].Content, false)
|
2026-09-20 13:28:23 +00:00
|
|
|
reasoningBefore := out[i].ReasoningContent
|
2026-09-09 16:31:43 +00:00
|
|
|
out[i].ReasoningContent = r.CompactPublicCitations(out[i].ReasoningContent, false)
|
2026-09-20 13:28:23 +00:00
|
|
|
dropStaleReasoningSignature(&out[i], reasoningBefore)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
if len(out[i].MultiContent) > 0 {
|
|
|
|
|
out[i].MultiContent = append([]chat.MessageContentPart(nil), out[i].MultiContent...)
|
|
|
|
|
for j := range out[i].MultiContent {
|
2026-07-26 16:59:38 +00:00
|
|
|
if out[i].MultiContent[j].Type == "text" && (out[i].Role == "assistant" || processToolResult) {
|
2026-09-09 16:31:43 +00:00
|
|
|
out[i].MultiContent[j].Text = r.CompactPublicCitations(out[i].MultiContent[j].Text, false)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(out[i].ToolCalls) > 0 {
|
|
|
|
|
out[i].ToolCalls = append([]chat.ToolCall(nil), out[i].ToolCalls...)
|
|
|
|
|
for j := range out[i].ToolCalls {
|
2026-07-26 16:59:38 +00:00
|
|
|
toolName := out[i].ToolCalls[j].Function.Name
|
|
|
|
|
r.registerToolArguments(
|
|
|
|
|
out[i].ToolCalls[j].Function.Arguments,
|
|
|
|
|
func(key string) bool { return argumentPolicy == nil || argumentPolicy(toolName, key) },
|
|
|
|
|
)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for i := range out {
|
2026-07-26 16:59:38 +00:00
|
|
|
if out[i].Role == "tool" && (resultPolicy == nil || resultPolicy(out[i].Name)) {
|
2026-09-09 16:31:43 +00:00
|
|
|
r.registerLegacyToolReferences(out[i].Content, false)
|
2026-07-16 13:23:20 +00:00
|
|
|
out[i].Content = r.CompactKnownText(out[i].Content)
|
|
|
|
|
}
|
|
|
|
|
for j := range out[i].ToolCalls {
|
2026-07-26 16:59:38 +00:00
|
|
|
toolName := out[i].ToolCalls[j].Function.Name
|
|
|
|
|
out[i].ToolCalls[j].Function.Arguments = r.decodeJSONWithPolicy(
|
|
|
|
|
out[i].ToolCalls[j].Function.Arguments,
|
|
|
|
|
true,
|
|
|
|
|
func(key string) bool { return argumentPolicy == nil || argumentPolicy(toolName, key) },
|
|
|
|
|
)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
var shortSourceHandleRE = regexp.MustCompile(`(?i)^[cdbw][1-9][0-9]*$`)
|
2026-07-26 17:04:19 +00:00
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
var shortSourceHandleInTextRE = regexp.MustCompile(`(?i)\b[cdbw][1-9][0-9]*\b`)
|
2026-07-16 13:23:20 +00:00
|
|
|
|
2026-07-26 16:59:38 +00:00
|
|
|
// 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.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) DecodeKnownText(text string) string {
|
2026-07-26 16:59:38 +00:00
|
|
|
if r == nil || text == "" {
|
|
|
|
|
return text
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
return shortSourceHandleInTextRE.ReplaceAllStringFunc(text, func(handle string) string {
|
|
|
|
|
if real := r.durableForHandle(handle); real != "" {
|
2026-07-26 16:59:38 +00:00
|
|
|
return real
|
|
|
|
|
}
|
|
|
|
|
return handle
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DecodeKnownQuotedText restores source handles only inside single-quoted,
|
|
|
|
|
// double-quoted, or backtick-quoted segments. It is intended for structured
|
|
|
|
|
// expressions such as SQL, where replacing an unquoted token could corrupt a
|
2026-07-26 17:17:36 +00:00
|
|
|
// legitimate table/column handle that happens to look like d1 or b2.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) DecodeKnownQuotedText(text string) string {
|
2026-07-26 16:59:38 +00:00
|
|
|
if r == nil || text == "" {
|
|
|
|
|
return text
|
|
|
|
|
}
|
|
|
|
|
return rewriteQuotedText(text, func(segment string) string {
|
2026-07-26 17:17:36 +00:00
|
|
|
return shortSourceHandleInTextRE.ReplaceAllStringFunc(segment, func(handle string) string {
|
|
|
|
|
if real := r.durableForHandle(handle); real != "" {
|
2026-07-26 16:59:38 +00:00
|
|
|
return real
|
|
|
|
|
}
|
|
|
|
|
return handle
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
// UnresolvedQuotedTextHandles reports handle-shaped values inside quoted
|
2026-07-26 16:59:38 +00:00
|
|
|
// structured-text segments that do not exist in this request registry.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) UnresolvedQuotedTextHandles(text string) []string {
|
2026-07-26 16:59:38 +00:00
|
|
|
if text == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
|
rewriteQuotedText(text, func(segment string) string {
|
2026-07-26 17:17:36 +00:00
|
|
|
for _, handle := range shortSourceHandleInTextRE.FindAllString(segment, -1) {
|
|
|
|
|
if r == nil || r.durableForHandle(handle) == "" {
|
2026-07-26 16:59:38 +00:00
|
|
|
seen[handle] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return segment
|
|
|
|
|
})
|
|
|
|
|
result := make([]string, 0, len(seen))
|
|
|
|
|
for handle := range seen {
|
|
|
|
|
result = append(result, handle)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(result)
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rewriteQuotedText(text string, rewrite func(string) string) string {
|
|
|
|
|
var out strings.Builder
|
|
|
|
|
out.Grow(len(text))
|
|
|
|
|
for i := 0; i < len(text); {
|
|
|
|
|
quote := text[i]
|
|
|
|
|
if quote != '\'' && quote != '"' && quote != '`' {
|
|
|
|
|
out.WriteByte(text[i])
|
|
|
|
|
i++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
start := i
|
|
|
|
|
i++
|
|
|
|
|
for i < len(text) {
|
|
|
|
|
if text[i] == '\\' && i+1 < len(text) {
|
|
|
|
|
i += 2
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if text[i] != quote {
|
|
|
|
|
i++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// SQL escapes a quote by doubling it (''). Keep scanning the
|
|
|
|
|
// same literal instead of treating the first quote as its end.
|
|
|
|
|
if i+1 < len(text) && text[i+1] == quote {
|
|
|
|
|
i += 2
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
i++
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
out.WriteString(rewrite(text[start:i]))
|
|
|
|
|
}
|
|
|
|
|
return out.String()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) registerToolArguments(raw string, allowed func(string) bool) {
|
2026-07-16 13:23:20 +00:00
|
|
|
if r == nil || strings.TrimSpace(raw) == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var value interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-26 16:59:38 +00:00
|
|
|
r.registerToolArgumentValue("", value, allowed)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) registerToolArgumentValue(key string, value interface{}, allowed func(string) bool) {
|
2026-07-16 13:23:20 +00:00
|
|
|
switch typed := value.(type) {
|
|
|
|
|
case string:
|
2026-07-26 16:59:38 +00:00
|
|
|
if allowed(strings.ToLower(key)) {
|
2026-09-09 16:31:43 +00:00
|
|
|
r.registerSourceIDByKey(key, typed, false)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
case []interface{}:
|
|
|
|
|
for _, item := range typed {
|
2026-07-26 16:59:38 +00:00
|
|
|
r.registerToolArgumentValue(key, item, allowed)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
case map[string]interface{}:
|
|
|
|
|
for childKey, item := range typed {
|
2026-07-26 16:59:38 +00:00
|
|
|
r.registerToolArgumentValue(childKey, item, allowed)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// registerSourceIDByKey is the single key→source-space dispatch used for tool
|
2026-07-26 17:08:11 +00:00
|
|
|
// arguments, structured tool results, and database rows. It is driven by
|
2026-07-26 17:17:36 +00:00
|
|
|
// sourceKeySpaces — the same table that gates handle decode — so the recognized
|
2026-07-26 17:08:11 +00:00
|
|
|
// key set (and the http/https guard for web references) cannot drift between
|
|
|
|
|
// registration and decoding.
|
2026-09-09 16:31:43 +00:00
|
|
|
func (r *sourceRegistry) registerSourceIDByKey(key, value string, evidence bool) {
|
2026-07-26 16:59:38 +00:00
|
|
|
value = strings.TrimSpace(value)
|
2026-07-26 17:17:36 +00:00
|
|
|
if value == "" || shortSourceHandleRE.MatchString(value) {
|
2026-07-26 16:59:38 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-26 17:08:11 +00:00
|
|
|
space, ok := sourceKeySpaces[strings.ToLower(key)]
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch space {
|
|
|
|
|
case spaceChunk:
|
2026-09-09 16:31:43 +00:00
|
|
|
r.registerChunk(ChunkReference{ChunkID: value}, evidence)
|
2026-07-26 17:08:11 +00:00
|
|
|
case spaceDocument:
|
2026-07-26 16:59:38 +00:00
|
|
|
r.RegisterDocument(value)
|
2026-07-26 17:08:11 +00:00
|
|
|
case spaceDocumentRef:
|
2026-07-26 16:59:38 +00:00
|
|
|
// Stored refs use "knowledgeID|title"; only the ID part is durable.
|
refactor(modelcontext): consolidate source_refs parsing, fixed data_analysis table, and identifier leak report (#3372)
* refactor(wiki): consolidate source_refs parsing and resolution
source_refs entries ("uuid" or "uuid|title") were parsed by ten
independent implementations that disagreed on whitespace handling, on
whether a model-supplied title could be trusted, and on how a leading
pipe was treated. The scope-enforced and legacy agent resolvers also
duplicated the lookup and the routing step looked every document up a
second time.
- types.ParseWikiSourceRef / FormatWikiSourceRef / WikiSourceRefMatchesKnowledge
are now the only readers and writer of the format.
- resolveWikiSourceDocuments replaces resolveAuthorizedSourceRefs,
resolveSourceRefs and wikiKnowledgeBasesForSourceRefs: one lookup per
document, titles always rebuilt from server data, dedupe by canonical ID,
unknown documents fail closed in every mode.
- removeSourceRef now matches padded or titled refs of a deleted document.
* refactor(data_analysis): expose the selected document as a fixed table name
The tool required the document ID inside the SQL text and blindly
string-replaced it with the physical DuckDB table name, which forced the
model-context codec to rewrite handles inside quoted SQL and let the
UUID-derived k_<uuid> table name reach the model through stored schema
summaries.
The document is now selected only by knowledge_id. Every query runs on a
dedicated connection where "dataset" is a temporary view over the
physical table (DuckDB temporary objects are connection-local, so
concurrent sessions stay isolated). The physical name remains accepted
for internal callers; any other table fails validation with an error
naming the table the model should use. Schema summaries, the schema
tool, the analyst prompt and the tool schema all name the same table, so
data_analysis.sql no longer carries source handles and its text codec
policy is removed.
* feat(modelcontext): report raw identifiers that survive encoding
Text-level compaction exists because raw UUIDs reach model context through
producers the registry never sees: error strings, slugs, results of
unknown shape. Retiring it requires knowing where those producers are.
Registry.LeakedIdentifiers scans an encoded message list for UUID-shaped
values in every application-composed field (user messages are skipped),
classifies each as registered (codec missed a rewrite) or unregistered
(producer emitted a raw ID), and SummarizeLeaks renders one line per
field. The agent engine and the three RAG pipeline call sites log the
report as a warning after EncodeMessages.
* fix(chat_pipeline): align RAG data analysis with the fixed table contract
The RAG data-analysis plugin still asked the planning model to fill in
knowledge_id and to write SQL from a prompt that exposed the raw
knowledge UUID, then passed the model's JSON straight to Execute. Under
the new data_analysis contract a query that names anything but the
dataset table is rejected, and the plugin logged that rejection at error
level and moved on, so the analysis vanished without any signal. A
"no analysis needed" plan took the same error path.
The pipeline now owns document selection: the model returns only
needs_analysis and sql, the prompt names the dataset table and never
shows the knowledge ID or physical table, and the plugin builds the tool
input itself. A rejected query is fed back to the model once for repair;
a query that still fails is reported through pipelineWarn with the SQL
and the tool error, and "not needed" is logged as such instead of as a
failure.
Covered by unit tests with a scripted model and executor: not-needed,
pipeline-owned knowledge_id, one-shot repair, persistent failure
surfaced, unparseable plan rejected.
2026-09-17 14:08:16 +00:00
|
|
|
r.RegisterDocument(types.WikiSourceKnowledgeID(value))
|
2026-07-26 17:08:11 +00:00
|
|
|
case spaceKnowledgeBase:
|
2026-07-26 16:59:38 +00:00
|
|
|
r.RegisterKnowledgeBase(value)
|
2026-07-26 17:08:11 +00:00
|
|
|
case spaceWeb:
|
2026-07-26 16:59:38 +00:00
|
|
|
// Only public web pages become web references. Internal schemes
|
2026-07-26 17:08:11 +00:00
|
|
|
// (res://, storage providers) must never enter the web handle space,
|
2026-07-26 16:59:38 +00:00
|
|
|
// where CompactKnownText would rewrite them a second time.
|
|
|
|
|
if parsed, err := url.Parse(value); err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") {
|
2026-09-09 16:31:43 +00:00
|
|
|
r.registerWeb(value, "", evidence)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
refactor(agent): consolidate knowledge retrieval tools into search_knowledge, read_document, list_documents (#3381)
* refactor(agent): consolidate knowledge retrieval tools into search_knowledge, read_document, list_documents
The agent's retrieval surface had grown to five overlapping tools whose
behaviour was scattered across long descriptions, interleaved "tips" in
tool output (most of which modelcontext discarded before the model saw
them), and a grep_chunks implementation that ran an unindexed regex scan
over the chunks table ordered by created_at, so large knowledge bases lost
recall and paid a full-table scan on every call.
Tool surface
- search_knowledge replaces knowledge_search and grep_chunks: one query,
mode = hybrid | semantic | keyword, optional knowledge_base_ids, limit.
Keyword mode runs through the retrieval engine's keyword index instead of
the DB regex scan. Modes unavailable for the scope are refused with a
reason. Rerank, MMR and de-duplication are kept; already_seen markers,
retrieval_statistics and the zero-result "CRITICAL" block are gone.
- read_document replaces list_knowledge_chunks, get_document_info and
wiki_read_source_doc: id accepts a dN or cN handle, always returns the
document header, pages by exact offset (fixing the old floor-to-page
bug), finds passages with query (literal by default, regex opt-in) and
reads neighbours around a chunk with context.
- list_documents pages through one knowledge base, honouring @file/@tag
scope.
- wiki_search takes a single query plus regex, knowledge_base_ids, limit;
literal queries are escaped so the model never reasons about JSON
escaping. Legacy queries / knowledge_base_id are still accepted.
- query_knowledge_graph is registered only when a graph-enabled KB is in
scope and its description/tips were trimmed.
Compatibility
- Retired names in stored allowlists, presets and API calls are mapped to
their successors by NormalizeAllowedTools at registration time; no data
migration. Stored histories with old names still decode, render and
redact. Calls to retired names return a replacement hint.
- modelcontext gains an "id" key space that decodes against both the chunk
and document handle tables (decode-only), plus rendering for the
document header, chunk roles, in-document matches and next_offset.
Prompts and surfaces
- The RAG, wiki researcher and hybrid templates now name the tools and the
search -> read -> cite order; tool descriptions are short and contain no
workflow instructions.
- Engine display names, history redaction, grounding prompt, IM rendering,
the built-in MCP server (search_knowledge gains mode/limit, grep_chunks
becomes keyword search, read_document gains query, wiki_search gains
regex), frontend editor/renderers/i18n, presets, website docs and the
changelog are updated accordingly.
Tests cover name mapping, mode validation and refusal, output shape,
read_document paging/context/query/scope, list_documents, wiki_search
escaping and legacy arguments, modelcontext decoding and rendering, and
registration-time aliasing and graph gating.
* fix(agent): address review findings on read_document, list_documents and model rendering
- read_document: fetch the second page whenever the first page did not
reach the end of the document, so an unaligned window near the end no
longer drops the trailing chunks (also fixes context windows around a
chunk near the end); a query passed together with a cN handle now
searches the owning document instead of being ignored; query mode stops
collecting once the output budget is nearly spent and reports truncated;
the document row is authorized in place instead of being fetched twice;
the context read takes the total from the window fetch instead of a
separate count query.
- list_documents: resolve the page's document scope in one batched tag
lookup instead of one call per row.
- modelcontext: render read_document output once and read next_offset
through the existing intValue helper.
- search_knowledge: point title/file-name lookups at list_documents.
- Tests for the boundary window, chunk context near the end, cN + query,
and budget-bound query results.
* fix(agent): close compatibility gaps in the consolidated retrieval tools
- Wiki-only scopes: read_document and list_documents are registered
whenever a wiki KB is in scope, since every KB stores chunks. Their
capability requirement includes wiki but is marked auxiliary, so they do
not widen a RAG agent's derived "all knowledge bases" filter to wiki-only
bases (Go and frontend derivations both honour it).
- search_knowledge resolves the retrieval mode per knowledge base instead
of refusing the call: FAQ and vector-only bases answer keyword requests
semantically, keyword-only bases answer semantic requests by keyword.
Results carry requested_mode and mode_fallbacks, the empty-result text
names the fallback, and the call is refused only when no base in scope
has a chunk index.
- The model rendering uses the mode the search actually ran with (it was
always labelled semantic) and emits <mode_fallback> per degraded base.
- list_documents applies the @file/@tag scope before paging: tags are
pushed into the database filter, pinned documents are loaded by ID, and
total/next_page count only readable documents.
- read_document resolves chunk context by chunk_index through a new
ChunkRepository.ListChunkNeighbors, so parent, summary and image chunks
sharing the index sequence no longer shift the neighbourhood; offset is
documented as a reading-order position, not a chunk index.
- wiki_search (agent and MCP) keeps its regular-expression default and
only matches literally when the text is not a valid expression;
regex=false forces a literal match and regex=true rejects invalid input.
- MCP grep_chunks keeps grep semantics: literal terms from the pattern
fetch candidates from the keyword index (semantic for bases without
one), and every returned chunk is verified against the pattern.
* docs(changelog): align the consolidation entry with regex wiki_search and grep_chunks semantics
* docs(mcp): note that grep_chunks recall is bounded by index candidates
2026-09-18 06:12:22 +00:00
|
|
|
case spaceAnySource:
|
|
|
|
|
// Decode-only: a bare "id" never registers a new source.
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) decodeJSONWithPolicy(raw string, encode bool, allowed func(string) bool) string {
|
2026-07-16 13:23:20 +00:00
|
|
|
if r == nil || strings.TrimSpace(raw) == "" {
|
|
|
|
|
return raw
|
|
|
|
|
}
|
|
|
|
|
var value interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
|
|
|
|
return raw
|
|
|
|
|
}
|
2026-07-26 16:59:38 +00:00
|
|
|
value = r.walkJSON("", value, encode, allowed)
|
2026-07-16 13:23:20 +00:00
|
|
|
encoded, err := json.Marshal(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return raw
|
|
|
|
|
}
|
|
|
|
|
return string(encoded)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) walkJSON(key string, value interface{}, encode bool, allowed func(string) bool) interface{} {
|
2026-07-16 13:23:20 +00:00
|
|
|
switch typed := value.(type) {
|
|
|
|
|
case string:
|
2026-07-26 16:59:38 +00:00
|
|
|
if !allowed(strings.ToLower(key)) {
|
|
|
|
|
return typed
|
|
|
|
|
}
|
2026-07-16 13:23:20 +00:00
|
|
|
if encode {
|
2026-07-16 13:46:52 +00:00
|
|
|
// Encode matches on exact real identifiers (UUIDs/URLs), which do
|
|
|
|
|
// not collide with prose, so it stays key-agnostic.
|
2026-07-26 17:17:36 +00:00
|
|
|
if handle := r.handleForDurable(typed); handle != "" {
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
return typed
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
// Decode only ID-bearing keys, and only when the value is handle-shaped,
|
|
|
|
|
// so ordinary strings that coincidentally equal an handle are preserved.
|
2026-07-26 17:08:11 +00:00
|
|
|
if _, ok := sourceKeySpaces[strings.ToLower(key)]; !ok {
|
2026-07-16 13:46:52 +00:00
|
|
|
return typed
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if !shortSourceHandleRE.MatchString(strings.TrimSpace(typed)) {
|
2026-07-16 13:46:52 +00:00
|
|
|
return typed
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if real := r.durableForHandle(typed); real != "" {
|
2026-07-16 13:23:20 +00:00
|
|
|
return real
|
|
|
|
|
}
|
|
|
|
|
return typed
|
|
|
|
|
case []interface{}:
|
|
|
|
|
for i := range typed {
|
2026-07-26 16:59:38 +00:00
|
|
|
typed[i] = r.walkJSON(key, typed[i], encode, allowed)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
case map[string]interface{}:
|
2026-07-16 13:46:52 +00:00
|
|
|
for childKey, item := range typed {
|
2026-07-26 16:59:38 +00:00
|
|
|
typed[childKey] = r.walkJSON(childKey, item, encode, allowed)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
func (r *sourceRegistry) handleForDurable(real string) string {
|
2026-07-26 17:13:08 +00:00
|
|
|
if handle, ok := r.chunks.handleForKey(real); ok {
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
if handle, ok := r.docs.handleForKey(real); ok {
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
if handle, ok := r.kbs.handleForKey(real); ok {
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
if handle, ok := r.webs.handleForKey(canonicalWebURL(real)); ok {
|
|
|
|
|
return handle
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
func (r *sourceRegistry) durableForHandle(handle string) string {
|
|
|
|
|
handle = strings.ToLower(strings.TrimSpace(handle))
|
|
|
|
|
if real, _, ok := r.chunks.resolve(handle); ok {
|
2026-07-26 17:13:08 +00:00
|
|
|
return real
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if real, _, ok := r.docs.resolve(handle); ok {
|
2026-07-16 13:23:20 +00:00
|
|
|
return real
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if real, _, ok := r.kbs.resolve(handle); ok {
|
2026-07-16 13:23:20 +00:00
|
|
|
return real
|
|
|
|
|
}
|
2026-07-26 17:17:36 +00:00
|
|
|
if real, _, ok := r.webs.resolve(handle); ok {
|
2026-07-26 17:13:08 +00:00
|
|
|
return real
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CompactKnownText is intentionally limited to identifiers already registered
|
|
|
|
|
// from structured runtime/tool data. It is used for metadata envelopes, not
|
|
|
|
|
// arbitrary retrieved prose.
|
2026-07-26 17:04:19 +00:00
|
|
|
func (r *sourceRegistry) CompactKnownText(text string) string {
|
2026-07-16 13:23:20 +00:00
|
|
|
if r == nil || text == "" {
|
|
|
|
|
return text
|
|
|
|
|
}
|
2026-07-26 17:13:08 +00:00
|
|
|
// The snapshot spans all four source tables and is sorted longest-value
|
|
|
|
|
// first GLOBALLY: a web URL may contain a registered document UUID as a
|
|
|
|
|
// substring, so per-table passes could corrupt the longer value.
|
|
|
|
|
pairs := r.chunks.pairs()
|
|
|
|
|
pairs = append(pairs, r.docs.pairs()...)
|
|
|
|
|
pairs = append(pairs, r.kbs.pairs()...)
|
|
|
|
|
pairs = append(pairs, r.webs.pairs()...)
|
|
|
|
|
sort.SliceStable(pairs, func(i, j int) bool { return len(pairs[i].value) > len(pairs[j].value) })
|
2026-07-16 13:23:20 +00:00
|
|
|
for _, item := range pairs {
|
2026-07-26 17:13:08 +00:00
|
|
|
if item.value != "" {
|
|
|
|
|
text = strings.ReplaceAll(text, item.value, item.handle)
|
2026-07-16 13:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return text
|
|
|
|
|
}
|