Documentation
¶
Overview ¶
Package expand holds the host-agnostic half of reversibility (design D6, after headroom's CCR): the marker format Offload components write, the expand(id) tool definition injected per provider, and resolution of a stashed original from the Store.
The continuation LOOP that actually answers an expand tool call is host glue (the bifrost proxy wraps its chat route; the AuthBridge plugin does it in OnResponse) — but every host reuses ParseMarkers, ToolDef, and Resolve from here so the wire contract stays identical across integrations.
Index ¶
- Constants
- func AggregateSSE(provider string, raw []byte) (msg []byte, ok bool)
- func Continuation(provider string, reqBody, resp []byte, resolved map[string]string) ([]byte, bool)
- func HasPlaceholder(s string) bool
- func Inject(provider, mode string, body []byte, storePersists bool) (out []byte, injected bool)
- func Marker(key string) string
- func ParseMarkers(s string) []string
- func Resolve(s store.Store, key string) (string, bool)
- func ToolDef(provider string) map[string]any
- func ToolDefRaw(provider string) json.RawMessage
- type Call
Constants ¶
const ( InjectAuto = "auto" InjectAlways = "always" InjectNever = "never" )
InjectMode controls whether the expand tool is advertised on outgoing requests.
- "auto" (default): inject only when the request already declares tools AND the store can persist stashes. This is the safe default — it never perturbs a request that uses no tools (the riskiest case for models that penalize an unexpected tool), and never advertises a tool that can't be resolved.
- "always": inject whenever the store persists (create the tools array if absent).
- "never": never inject (the pre-D2 behavior; pair with marker_mode: summary).
const SummaryMarker = "⟪cg⟫"
SummaryMarker is the non-resolvable sentinel an Offload component writes under marker_mode "summary": it signals "content was compacted here" — to the model and to cross-turn skip-detection — but carries no store key, so there is nothing to expand (restoration is off). The human-readable description of what was dropped stays inline in the message (the component's own note).
const ToolName = "context_guru_expand"
ToolName is the model-callable tool that retrieves offloaded content.
Variables ¶
This section is empty.
Functions ¶
func AggregateSSE ¶
AggregateSSE reconstructs the final provider message JSON from a buffered Server-Sent Events stream, so the expand loop can inspect it with ResponseCalls / Continuation (which operate on the non-streaming response shape). ok=false when the stream can't be reconstructed (unknown dialect / malformed) — the caller then streams the bytes through unchanged (fail-open).
Only the Anthropic Messages event stream is reconstructed (the streaming coding agents in scope use it). For other dialects it returns ok=false.
func Continuation ¶
Continuation builds the next request body: it appends the assistant's tool-call turn and a tool_result turn carrying each resolved original, so the model can finish with the full content in hand. resolved maps CallID -> original text. ok=false if the shapes weren't as expected (caller returns the response unchanged — fail open).
func HasPlaceholder ¶
HasPlaceholder reports whether s already carries a context-guru placeholder — a resolvable <<cg:HASH>> marker (full mode) OR a SummaryMarker sentinel (summary mode). Components use it to skip content an earlier component/turn already reduced, so a later turn re-sending the same message reduces to the same bytes (provider prefix-cache stays warm).
func Inject ¶
Inject appends the expand tool definition to the request body's tools array in a byte-stable way, so a model that offloaded content can call context_guru_expand to get it back (closing the reversibility loop the proxy's continuation handler drives).
It is idempotent (a request that already declares the tool is returned unchanged), appends the tool LAST so the client's own tools keep their exact order and the provider prefix cache stays warm, and respects a forcing tool_choice (skips, so it never changes which tool the model is compelled to call). Fail-open: any error returns the original body with injected=false.
func ParseMarkers ¶
ParseMarkers returns the distinct store keys referenced by any markers in s, in first-seen order.
func Resolve ¶
Resolve returns the original stashed under key, or ("",false) if it's gone (expired/evicted). Callers must handle the miss gracefully — an expired original silently turns a lossless offload lossy (headroom's known TTL edge).
func ToolDef ¶
ToolDef returns the expand tool definition shaped for the given provider dialect. OpenAI/Anthropic differ (parameters vs input_schema); the returned map is ready to append to the request's tools array.
func ToolDefRaw ¶
func ToolDefRaw(provider string) json.RawMessage
ToolDefRaw returns the expand tool definition for a provider as deterministic JSON bytes (stable key order). Inject appends these to the request's tools array.
Types ¶
type Call ¶
Call is one model request to expand offloaded content: CallID identifies the tool call (for the continuation's tool_result), HashID is the store key.
func ResponseCalls ¶
ResponseCalls parses a provider response for expand tool calls. otherTools is true if the model also called some OTHER tool — in that case the host cannot safely auto-continue (the client must resolve the other tools), so the loop bails and returns the response as-is.