Documentation
¶
Overview ¶
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 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.
resources.go is the durable-resource half of the model-context registry: it assigns request-local res://NNNN handles to stored resource references and restores them before application code consumes model output.
sources.go is the source-reference half of the model-context registry: request-local chunk://N, doc://N, kb://N and url://N 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.
stream.go owns streaming-safe handle decoding: the shared suffix-hold primitive, the per-space decoders built on it, and the composed public StreamDecoder that applies every stage in the only safe order.
Index ¶
- Constants
- func HasToolPolicy(toolName string) bool
- func SummarizeLeaks(leaks []IdentifierLeak) string
- type ChunkReference
- type HandleStreamDecoder
- type HandleTable
- func (t *HandleTable) DecodeKnownText(value string) string
- func (t *HandleTable) Empty() bool
- func (t *HandleTable) EncodeKnownText(value string) string
- func (t *HandleTable) Handle(value string) (string, bool)
- func (t *HandleTable) Len() int
- func (t *HandleTable) Register(value string) string
- func (t *HandleTable) Resolve(handle string) (string, bool)
- type IdentifierLeak
- type Registry
- func (r *Registry) ChunkHandle(id string) string
- func (r *Registry) CompactKnownText(text string) string
- func (r *Registry) DecodeOutputText(text string) string
- func (r *Registry) DecodeResponse(response *types.ChatResponse)
- func (r *Registry) DecodeToolCalls(toolCalls []types.LLMToolCall)
- func (r *Registry) EncodeKnownIssues(text string) string
- func (r *Registry) EncodeMessages(messages []chat.Message) []chat.Message
- func (r *Registry) EncodeTools(tools []chat.Tool) []chat.Tool
- func (r *Registry) ImageSourcesNote(chunkIDs []string, offset int) string
- func (r *Registry) LeakedIdentifiers(messages []chat.Message) []IdentifierLeak
- func (r *Registry) ModelToolResult(result *types.ToolResult) string
- func (r *Registry) ModelToolResultForTool(toolName string, result *types.ToolResult) string
- func (r *Registry) OrphanResourceHandles(decoded string) []string
- func (r *Registry) ProtocolPrompt() string
- func (r *Registry) RegisterChunk(ref ChunkReference) string
- func (r *Registry) RegisterContextChunk(ref ChunkReference) string
- func (r *Registry) RegisterDocument(id string) string
- func (r *Registry) RegisterIssue(id string) string
- func (r *Registry) RegisterKnowledgeBase(id string) string
- func (r *Registry) RegisterSearchResults(results []*types.SearchResult)
- func (r *Registry) RegisterWeb(rawURL, title string) string
- func (r *Registry) StreamDecoder() *StreamDecoder
- func (r *Registry) ToolImageSourcesNote(result *types.ToolResult) string
- type StreamDecoder
Constants ¶
const ( ArgumentResolutionUnchanged = "unchanged" ArgumentResolutionResolved = "resolved" ArgumentResolutionPartiallyResolved = "partially_resolved" ArgumentResolutionUnresolved = "unresolved" )
Variables ¶
This section is empty.
Functions ¶
func HasToolPolicy ¶
HasToolPolicy reports whether a tool has an explicit model-handle policy. Built-in tools must declare one (even an empty policy) so that adding a tool is a deliberate decision about what the model may see, rather than a silent fall-through to fully opaque output.
func SummarizeLeaks ¶
func SummarizeLeaks(leaks []IdentifierLeak) string
SummarizeLeaks renders leaks as one log line per message field so the producing tool or role can be identified without dumping the context.
Types ¶
type ChunkReference ¶
type HandleStreamDecoder ¶
type HandleStreamDecoder struct {
// contains filtered or unexported fields
}
HandleStreamDecoder restores HandleTable values without leaking a handle split across provider chunks.
func NewHandleStreamDecoder ¶
func NewHandleStreamDecoder(table *HandleTable) *HandleStreamDecoder
func (*HandleStreamDecoder) Feed ¶
func (d *HandleStreamDecoder) Feed(chunk string) string
func (*HandleStreamDecoder) Flush ¶
func (d *HandleStreamDecoder) Flush() string
type HandleTable ¶
type HandleTable struct {
// contains filtered or unexported fields
}
HandleTable is a typed, invocation-local bidirectional mapping used when a prompt needs compact handles for durable values (wiki issue iN handles, ingest ref-N slug handles, c000 citation-batch handles). Handles must never be persisted; Resolve converts model output back to the durable value first. It is the exported word-boundary-aware wrapper over handleTable.
func NewHandleTable ¶
func NewHandleTable(prefix string, width, start int) *HandleTable
NewHandleTable creates a handle space such as c000 (prefix=c, width=3, start=0) or ref-1 (prefix=ref-, width=0, start=1).
func (*HandleTable) DecodeKnownText ¶
func (t *HandleTable) DecodeKnownText(value string) string
DecodeKnownText restores registered handles in complete text. Unlike the resource codec, replacement is word-boundary-aware so short handles such as i1 cannot fire inside ordinary prose tokens.
func (*HandleTable) Empty ¶
func (t *HandleTable) Empty() bool
func (*HandleTable) EncodeKnownText ¶
func (t *HandleTable) EncodeKnownText(value string) string
EncodeKnownText replaces already-registered durable values with their model handles. Longer values are processed first to avoid substring shadowing.
func (*HandleTable) Handle ¶
func (t *HandleTable) Handle(value string) (string, bool)
Handle returns an already registered handle without creating one.
func (*HandleTable) Len ¶
func (t *HandleTable) Len() int
func (*HandleTable) Register ¶
func (t *HandleTable) Register(value string) string
Register returns the stable handle assigned to value in this table.
type IdentifierLeak ¶
type IdentifierLeak struct {
MessageIndex int
Role string
// ToolName is the tool that produced a tool message or the tool an
// assistant call targets; empty for plain content.
ToolName string
// Field names the message part: content, reasoning, multi_content, or
// tool_call_arguments.
Field string
// Registered holds leaked values the registry knows a handle for: the
// codec missed a rewrite site. Unregistered holds values that never
// entered the registry: the producer emitted a raw identifier.
Registered []string
Unregistered []string
}
IdentifierLeak is one message field that still carries UUID-shaped values after EncodeMessages.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
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.
func NewRegistry ¶
NewRegistry creates a registry for one model request/Agent execution.
func (*Registry) ChunkHandle ¶
func (*Registry) CompactKnownText ¶
CompactKnownText replaces only previously registered durable source IDs.
func (*Registry) DecodeOutputText ¶
DecodeOutputText applies the public citation policy to complete text. It is primarily used by non-streaming cleanup/fallback paths.
func (*Registry) DecodeResponse ¶
func (r *Registry) DecodeResponse(response *types.ChatResponse)
DecodeResponse restores resources, expands citations, and decodes tool arguments in a non-streaming response.
func (*Registry) DecodeToolCalls ¶
func (r *Registry) DecodeToolCalls(toolCalls []types.LLMToolCall)
DecodeToolCalls restores all temporary handles in tool-call arguments.
func (*Registry) EncodeKnownIssues ¶ added in v0.9.0
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 (*Registry) EncodeMessages ¶
EncodeMessages returns a model-facing copy with every temporary handle encoded in the only safe order.
func (*Registry) EncodeTools ¶
EncodeTools returns a model-facing copy. The registry/executor retains the original UUID enum and validates only decoded durable identities.
func (*Registry) ImageSourcesNote ¶
ImageSourcesNote binds images to handles already present in the final model context. offset counts user images preceding the retrieved ones in this message. Unknown sources stay explicitly unavailable, never fabricated.
func (*Registry) LeakedIdentifiers ¶
func (r *Registry) LeakedIdentifiers(messages []chat.Message) []IdentifierLeak
LeakedIdentifiers scans an encoded message list for UUID-shaped values. User-authored messages are skipped: a user may legitimately paste an ID. Everything else the application composed, so any UUID there is a leak.
func (*Registry) ModelToolResult ¶
func (r *Registry) ModelToolResult(result *types.ToolResult) string
ModelToolResult renders a tool result using registered model handles.
func (*Registry) ModelToolResultForTool ¶
func (r *Registry) ModelToolResultForTool(toolName string, result *types.ToolResult) string
ModelToolResultForTool renders a result and applies any explicit private-ID policy owned by that built-in tool family.
func (*Registry) OrphanResourceHandles ¶
OrphanResourceHandles reports model-generated resource handles with no backing durable reference.
func (*Registry) ProtocolPrompt ¶
ProtocolPrompt returns the system-owned model handle and citation protocol.
func (*Registry) RegisterChunk ¶
func (r *Registry) RegisterChunk(ref ChunkReference) string
func (*Registry) RegisterContextChunk ¶
func (r *Registry) RegisterContextChunk(ref ChunkReference) string
RegisterContextChunk makes a directory entry addressable by tools without allowing it to substantiate an answer before retrieval.
func (*Registry) RegisterDocument ¶
func (*Registry) RegisterIssue ¶ added in v0.9.0
RegisterIssue allocates the iN handle the Wiki issue tools accept for id.
func (*Registry) RegisterKnowledgeBase ¶
func (*Registry) RegisterSearchResults ¶
func (r *Registry) RegisterSearchResults(results []*types.SearchResult)
func (*Registry) RegisterWeb ¶
func (*Registry) StreamDecoder ¶
func (r *Registry) StreamDecoder() *StreamDecoder
StreamDecoder creates one ordered decoder for a response text channel.
func (*Registry) ToolImageSourcesNote ¶
func (r *Registry) ToolImageSourcesNote(result *types.ToolResult) string
ToolImageSourcesNote is rendered beside the actual images after tool result rendering has registered its source handles. Stored results have no images and must not claim to attach them on replay.
type StreamDecoder ¶
type StreamDecoder struct {
// contains filtered or unexported fields
}
StreamDecoder composes resource restoration, source-citation expansion, source-handle expansion, issue-handle decoding and orphan filtering so callers cannot split handle processing or apply stages in the wrong order.
func (*StreamDecoder) Feed ¶
func (d *StreamDecoder) Feed(chunk string) string
func (*StreamDecoder) Flush ¶
func (d *StreamDecoder) Flush() string