expand

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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).
View Source
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).

View Source
const ToolName = "context_guru_expand"

ToolName is the model-callable tool that retrieves offloaded content.

Variables

This section is empty.

Functions

func AggregateSSE

func AggregateSSE(provider string, raw []byte) (msg []byte, ok bool)

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

func Continuation(provider string, reqBody, resp []byte, resolved map[string]string) ([]byte, bool)

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

func HasPlaceholder(s string) bool

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

func Inject(provider, mode string, body []byte, storePersists bool) (out []byte, injected bool)

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 Marker

func Marker(key string) string

Marker renders the sentinel for a given store key.

func ParseMarkers

func ParseMarkers(s string) []string

ParseMarkers returns the distinct store keys referenced by any markers in s, in first-seen order.

func Resolve

func Resolve(s store.Store, key string) (string, bool)

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

func ToolDef(provider string) map[string]any

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

type Call struct {
	CallID string
	HashID string
}

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

func ResponseCalls(provider string, resp []byte) (calls []Call, otherTools bool)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL