Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

12.9 KB
Raw Blame
// Package modelcontext owns every request-local handle exposed to a language
// model. Application code should use Registry instead of coordinating source
// references and durable-resource codecs independently.
//
// Its boundaries are deliberate:
//   - UUIDs, wiki slugs, URLs, and resource:// handles are durable identities.
//   - kb://N, doc://N, chunk://N, url://N, iN, res://NNNN and ref-N values are
//     temporary model handles; weknora:// URIs are their durable text form.
//   - temporary handles are never persisted or accepted outside their registry.
//   - every model response is decoded before tools, storage, or UI consume it.
package modelcontext

import (
	"encoding/json"
	"reflect"
	"sort"
	"strings"

	"github.com/Tencent/WeKnora/internal/models/api"
	"github.com/Tencent/WeKnora/internal/models/chat"
	"github.com/Tencent/WeKnora/internal/types"
)

const (
	ArgumentResolutionUnchanged         = "unchanged"
	ArgumentResolutionResolved          = "resolved"
	ArgumentResolutionPartiallyResolved = "partially_resolved"
	ArgumentResolutionUnresolved        = "unresolved"
)

const resourceHandleProtocolPrompt = `

## Resource handle protocol (system-owned)
Some durable resources and high-entropy Wiki slugs are represented by request-local res://NNNN handles. Wiki issues may use iN handles.
- Copy supplied handles exactly in links, images, and tool arguments; they refer only to the supplied resource versions.
- For downloadable deliverables generated in the session workspace, use sandbox:<file name>; ` +
	`never reuse or invent a resource handle. This download convention does not apply to ` +
	`editing installed skill files.`

// Registry is the single request-scoped boundary between durable application
// identities and temporary model handles.
//
// Durable resource references are encoded before source identifiers. This
// ordering is intentionally private: summary/<knowledge-id> wiki slugs must be
// protected as one resource-like handle before the embedded document ID can be
// compacted to doc://N. Callers therefore cannot accidentally reverse the codecs.
type Registry struct {
	sources    *sourceRegistry
	resources  *resourceRegistry
	issues     *HandleTable
	mcpServers *HandleTable
	mcpTools   *HandleTable
}

// NewRegistry creates a registry for one model request/Agent execution.
func NewRegistry(citationsEnabled bool) *Registry {
	return &Registry{
		sources:    newSourceRegistry(citationsEnabled),
		resources:  newResourceRegistry(),
		issues:     NewHandleTable("i", 0, 1),
		mcpServers: NewHandleTable("ms", 0, 1),
		mcpTools:   NewHandleTable("mt", 0, 1),
	}
}

// ProtocolPrompt returns the system-owned model handle and citation protocol.
func (r *Registry) ProtocolPrompt() string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.ProtocolPrompt() + resourceHandleProtocolPrompt +
		"\nMCP routing uses request-local msN server IDs and mtN tool references. " +
		"Copy them exactly from the directory or describe result; never invent them.\n"
}

// EncodeMessages returns a model-facing copy with every temporary handle
// encoded in the only safe order.
func (r *Registry) EncodeMessages(messages []chat.Message) []chat.Message {
	if r == nil {
		return messages
	}
	messages = r.resources.EncodeMessages(messages)
	// Register tool-private IDs from all replayed results before encoding any
	// assistant call. The scan is intentionally order-independent, matching the
	// source codec's two-pass replay behavior.
	for i := range messages {
		if messages[i].Role == "system" || messages[i].Role == "user" {
			messages[i].Content = r.encodeMCPRoutingText(messages[i].Content)
		}
		if messages[i].Role == "tool" {
			messages[i].Content = r.encodeToolPrivateResult(messages[i].Name, messages[i].Content)
		}
	}
	messages = r.sources.EncodeMessagesWithPolicies(messages, sourceArgumentAllowed, sourceOutputAllowed)
	for i := range messages {
		if len(messages[i].ToolCalls) == 0 {
			continue
		}
		messages[i].ToolCalls = append([]chat.ToolCall(nil), messages[i].ToolCalls...)
		for j := range messages[i].ToolCalls {
			r.encodeReplayedToolPolicies(&messages[i].ToolCalls[j])
		}
	}
	return messages
}

// DecodeToolCalls restores all temporary handles in tool-call arguments.
func (r *Registry) DecodeToolCalls(toolCalls []types.LLMToolCall) {
	if r == nil {
		return
	}
	for i := range toolCalls {
		if toolCalls[i].ModelArguments == "" {
			toolCalls[i].ModelArguments = toolCalls[i].Function.Arguments
		}
	}
	normalizeWebFetchItems(toolCalls)
	normalizeMCPCallArguments(toolCalls)
	r.resources.DecodeToolCalls(toolCalls)
	r.sources.DecodeToolCallsWithPolicy(toolCalls, sourceArgumentAllowed, sourceBareTextAllowed)
	for i := range toolCalls {
		r.decodeToolPolicies(&toolCalls[i])
		decodedMCP, unresolvedMCP := r.decodeMCPArguments(toolCalls[i].Function.Name, toolCalls[i].Function.Arguments)
		toolCalls[i].Function.Arguments = decodedMCP
		resolved := toolCalls[i].Function.Arguments
		unresolved := append(
			r.resources.OrphanHandles(resolved),
			r.sources.UnresolvedToolHandlesWithPolicy(
				toolCalls[i].Function.Name, resolved, sourceArgumentAllowed,
			)...,
		)
		unresolved = append(unresolved, r.unresolvedPrivateToolHandles(toolCalls[i].Function.Name, resolved)...)
		unresolved = append(unresolved, unresolvedMCP...)
		toolCalls[i].UnresolvedHandles = uniqueSorted(unresolved)
		changed := !jsonEquivalent(toolCalls[i].ModelArguments, resolved)
		switch {
		case changed && len(toolCalls[i].UnresolvedHandles) > 0:
			toolCalls[i].ArgumentResolution = ArgumentResolutionPartiallyResolved
		case len(toolCalls[i].UnresolvedHandles) > 0:
			toolCalls[i].ArgumentResolution = ArgumentResolutionUnresolved
		case changed:
			toolCalls[i].ArgumentResolution = ArgumentResolutionResolved
		default:
			toolCalls[i].ArgumentResolution = ArgumentResolutionUnchanged
		}
	}
}

func jsonEquivalent(left, right string) bool {
	var leftValue interface{}
	var rightValue interface{}
	if json.Unmarshal([]byte(left), &leftValue) != nil || json.Unmarshal([]byte(right), &rightValue) != nil {
		return left == right
	}
	return reflect.DeepEqual(leftValue, rightValue)
}

func uniqueSorted(values []string) []string {
	seen := make(map[string]struct{}, len(values))
	for _, value := range values {
		if value != "" {
			seen[value] = struct{}{}
		}
	}
	result := make([]string, 0, len(seen))
	for value := range seen {
		result = append(result, value)
	}
	sort.Strings(result)
	return result
}

// DecodeResponse restores resources, expands citations, and decodes tool
// arguments in a non-streaming response.
func (r *Registry) DecodeResponse(response *types.ChatResponse) {
	if r == nil || response == nil {
		return
	}
	response.Content = r.DecodeOutputText(response.Content)
	reasoningBefore := response.ReasoningContent
	response.ReasoningContent = r.DecodeOutputText(response.ReasoningContent)
	if response.ReasoningContent != reasoningBefore &&
		api.SignatureFor(api.APIAnthropicMessages, response.ReasoningSignature) != "" {
		// Same reason and same protocol limit as dropStaleReasoningSignature:
		// the decoded text is no longer what Claude signed, and this response
		// is what lands on the agent step. Anthropic turns replay from
		// ReasoningMetadata, which decoding never touches, so this only
		// affects turns stored before that metadata existed.
		response.ReasoningSignature = ""
	}
	r.DecodeToolCalls(response.ToolCalls)
}

// StreamDecoder creates one ordered decoder for a response text channel.
func (r *Registry) StreamDecoder() *StreamDecoder {
	if r == nil {
		return &StreamDecoder{}
	}
	return &StreamDecoder{
		resources:  newResourceStreamDecoder(r.resources),
		sources:    newCitationStreamExpander(r.sources),
		handles:    newSourceHandleStreamDecoder(r.sources),
		issues:     NewHandleStreamDecoder(r.issues),
		mcpServers: NewHandleStreamDecoder(r.mcpServers),
		mcpTools:   NewHandleStreamDecoder(r.mcpTools),
		orphans:    newOrphanResourceStreamFilter(),
	}
}

// OrphanResourceHandles reports model-generated resource handles with no
// backing durable reference.
func (r *Registry) OrphanResourceHandles(decoded string) []string {
	if r == nil {
		return nil
	}
	return r.resources.OrphanHandles(decoded)
}

func (r *Registry) RegisterChunk(ref ChunkReference) string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.RegisterChunk(ref)
}

// RegisterContextChunk makes a directory entry addressable by tools without
// allowing it to substantiate an answer before retrieval.
func (r *Registry) RegisterContextChunk(ref ChunkReference) string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.registerChunk(ref, false)
}

func (r *Registry) RegisterDocument(id string) string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.RegisterDocument(id)
}

func (r *Registry) RegisterKnowledgeBase(id string) string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.RegisterKnowledgeBase(id)
}

// RegisterIssue allocates the iN handle the Wiki issue tools accept for id.
func (r *Registry) RegisterIssue(id string) string {
	if r == nil || r.issues == nil || strings.TrimSpace(id) == "" {
		return ""
	}
	return r.issues.Register(id)
}

// EncodeKnownIssues replaces registered Wiki issue IDs with their handles.
// CompactKnownText leaves them alone because tools opt into issue encoding
// per policy; runtime context the engine renders opts in explicitly.
func (r *Registry) EncodeKnownIssues(text string) string {
	if r == nil || r.issues == nil {
		return text
	}
	return r.issues.EncodeKnownText(text)
}

func (r *Registry) RegisterWeb(rawURL, title string) string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.RegisterWeb(rawURL, title)
}

func (r *Registry) RegisterSearchResults(results []*types.SearchResult) {
	if r == nil || r.sources == nil {
		return
	}
	r.sources.RegisterSearchResults(results)
}

func (r *Registry) ChunkHandle(id string) string {
	if r == nil || r.sources == nil {
		return ""
	}
	return r.sources.ChunkHandle(id)
}

// CompactKnownText replaces only previously registered durable source IDs.
func (r *Registry) CompactKnownText(text string) string {
	if r == nil || r.sources == nil {
		return text
	}
	text = r.resources.EncodeText(text)
	return r.sources.CompactKnownText(text)
}

// ModelToolResult renders a tool result using registered model handles.
func (r *Registry) ModelToolResult(result *types.ToolResult) string {
	return r.ModelToolResultForTool("", result)
}

// ModelToolResultForTool renders a result and applies any explicit private-ID
// policy owned by that built-in tool family.
func (r *Registry) ModelToolResultForTool(toolName string, result *types.ToolResult) string {
	if result == nil {
		return ""
	}
	if r == nil || r.sources == nil {
		if result.Success {
			return result.Output + outputFilesPrompt(result)
		}
		return failedToolModelText(result.Output, result.Error) + outputFilesPrompt(result)
	}
	// Protect durable resources and UUID-bearing summary slugs before the
	// source codec sees any embedded document IDs. Encode once more afterwards
	// for resource references rendered from structured ToolResult.Data.
	copyResult := *result
	// Error text is encoded on the same path as Output: a failed tool call
	// routinely echoes the offending argument, so a raw durable ID would leak
	// through the error branch of an otherwise handle-only tool.
	copyResult.Output = r.resources.EncodeText(r.encodeToolPrivateResult(toolName, result.Output))
	copyResult.Error = r.resources.EncodeText(r.encodeToolPrivateResult(toolName, result.Error))
	var modelOutput string
	if sourceOutputAllowed(toolName) {
		modelOutput = r.sources.ModelOutput(&copyResult)
	} else if copyResult.Success {
		modelOutput = copyResult.Output
	} else {
		modelOutput = failedToolModelText(copyResult.Output, copyResult.Error)
	}
	// Even tools without structured source results can surface a known durable
	// ID in validation errors or status text. Compact only explicitly declared
	// built-ins; dynamic MCP output remains fully opaque.
	if sourceCompactionAllowed(toolName) {
		modelOutput = r.sources.CompactKnownText(modelOutput)
	}
	if result.Success && (toolName == "call_mcp_tool" || strings.HasPrefix(toolName, "mcp_")) {
		modelOutput += r.mcpSourceCandidates(result.Output)
	}
	return r.resources.EncodeText(modelOutput) + outputFilesPrompt(result)
}

func outputFilesPrompt(result *types.ToolResult) string {
	if result == nil || result.OutputFiles == nil {
		return ""
	}
	if len(result.OutputFiles) == 0 {
		return "\nOutput files: none identified by this call."
	}
	return "\nOutput files: `" + strings.Join(result.OutputFiles, "`, `") + "`"
}

// DecodeOutputText applies the public citation policy to complete text. It is
// primarily used by non-streaming cleanup/fallback paths.
func (r *Registry) DecodeOutputText(text string) string {
	if r == nil {
		return text
	}
	text = r.resources.DecodeText(text)
	text = r.resources.StripOrphanHandles(text)
	text = r.sources.ExpandText(text)
	text = r.sources.ExpandHandleText(text)
	return r.mcpTools.DecodeKnownText(r.mcpServers.DecodeKnownText(r.issues.DecodeKnownText(text)))
}