Documentation
¶
Index ¶
- Constants
- func ApplyProviderRunConfig(ctx context.Context, logger *slog.Logger, cfg agentpkg.RunConfig) agentpkg.RunConfig
- func CollectNonSystemProviderSourceFrags(ctx context.Context, cfg agentpkg.RunConfig) []contextfrag.ContextFrag
- func CollectProviderSourceFrags(ctx context.Context, cfg agentpkg.RunConfig) []contextfrag.ContextFrag
- func ComputeContextBudgetPlan(window, outputReserve, toolDefsCost, currentRequestCost int) (*contextfrag.ContextBudgetPlan, error)
- func FinalizeRuntimeContextMarkdown(blocks []string) string
- func FinalizeRuntimeContextMarkdownFromJoined(joined string) string
- func FormatMemoryContext(text string) string
- func JoinRuntimeContextBlocks(blocks []string) string
- func ProviderRunConfigApplier(logger *slog.Logger) agentpkg.ContextViewApplier
- func SelectProviderStepMessages(ctx context.Context, input agentpkg.ContextStepSelectionInput) agentpkg.ContextStepSelectionResult
- func ToolUsageFrag(usage string, scope contextfrag.Scope) contextfrag.ContextFrag
- func TrimNoticeFrag(scope contextfrag.Scope) contextfrag.ContextFrag
- type BudgetEnvelope
- type BuildInput
- type BuildOptions
- type BuildTrace
- type Builder
- type CollectRequest
- type Collector
- type CollectorRegistry
- type ContextView
- type CurrentUserCollector
- type CurrentUserConfig
- type DiscussContextCollector
- type DiscussContextConfig
- type DiscussContextInput
- type DiscussSDKContextBuilder
- type DropRecord
- type FragmentSelector
- type HistoryMessagesCollector
- type HistoryMessagesConfig
- type HookContextCollector
- type HookContextConfig
- type IdentityPlacer
- type InlineImageCollector
- type InlineImageConfig
- type IntentProfile
- type MapCollectorRegistry
- type MapRendererRegistry
- type MemoryContextCollector
- type MemoryContextConfig
- type NoopRenderer
- type PassthroughSelector
- type PlacementItem
- type PlacementPlan
- type PlacementSummary
- type Placer
- type RenderInput
- type RenderSummary
- type RenderedPayload
- type Renderer
- type RendererRegistry
- type RuntimeFullContextRenderer
- type RuntimeRenderConfig
- type RuntimeRenderMode
- type RuntimeRenderedPayload
- type RuntimeSection
- type RuntimeSectionsCollector
- type RuntimeSectionsConfig
- type SDKMessagesRenderer
- type SDKRenderedPayload
- type SelectionResult
- type SelectionSummary
- type SelectionTag
- type Selector
- type SourceSpec
- type StablePrefixPlacer
- type StaticCollector
- type SystemPromptCollector
- type SystemPromptConfig
- type TaggedFrag
Constants ¶
const DefaultRecentProtectTokens = 20000
const HistoryTrimNotice = "[System Notice] Some earlier and intervening messages were trimmed to fit the context window. " +
"If you need information from the trimmed messages, use the available tools " +
"(such as memory_read or web search) to retrieve it."
HistoryTrimNotice tells the model that budget pressure removed messages. Tiered drops can leave holes mid-history, not only at the head, so the wording stays honest about that. The text is model-visible; change it only deliberately.
const ( // MinimumSystemBudgetTokens prevents an active plan from treating a tiny // positive remainder as a usable system envelope. MinimumSystemBudgetTokens = 256 )
Variables ¶
This section is empty.
Functions ¶
func ApplyProviderRunConfig ¶
func ApplyProviderRunConfig(ctx context.Context, logger *slog.Logger, cfg agentpkg.RunConfig) agentpkg.RunConfig
ApplyProviderRunConfig keeps the direct compiler helper source-compatible. Native execution uses ProviderRunConfigApplier so typed budget failures are returned to the runtime and fail closed before any provider call.
func CollectNonSystemProviderSourceFrags ¶
func CollectNonSystemProviderSourceFrags(ctx context.Context, cfg agentpkg.RunConfig) []contextfrag.ContextFrag
CollectNonSystemProviderSourceFrags preserves the exact SDK message order. A marked current message keeps its original index even though it receives a current-user slot, and raw Query/InlineImages are collected only when the caller has not materialized them yet.
func CollectProviderSourceFrags ¶
func CollectProviderSourceFrags(ctx context.Context, cfg agentpkg.RunConfig) []contextfrag.ContextFrag
CollectProviderSourceFrags derives typed fragments from already-materialized provider fields. It is the legacy fallback; application code normally supplies typed system sections directly.
func ComputeContextBudgetPlan ¶
func ComputeContextBudgetPlan(window, outputReserve, toolDefsCost, currentRequestCost int) (*contextfrag.ContextBudgetPlan, error)
ComputeContextBudgetPlan allocates the fixed reserves named by the provider envelope contract. outputReserve is the resolved generation limit, so the plan reserves exactly what the provider request may emit.
func FinalizeRuntimeContextMarkdown ¶ added in v0.20.0
FinalizeRuntimeContextMarkdown joins section blocks with the runtime context document spacing and bounds the complete resource.
func FinalizeRuntimeContextMarkdownFromJoined ¶ added in v0.20.0
finalizeRuntimeContextMarkdownWithAudit bounds the joined document and reports the pre-prune byte size so renderers can audit a raw head/tail cut that happened after selection. FinalizeRuntimeContextMarkdownFromJoined bounds an already-joined document.
func FormatMemoryContext ¶ added in v0.20.0
FormatMemoryContext frames provider recall as escaped, untrusted reference data without changing the legacy MemoryContextCollector payload.
func JoinRuntimeContextBlocks ¶ added in v0.20.0
JoinRuntimeContextBlocks joins section blocks with the runtime context document spacing without bounding the result.
func ProviderRunConfigApplier ¶
func ProviderRunConfigApplier(logger *slog.Logger) agentpkg.ContextViewApplier
func SelectProviderStepMessages ¶
func SelectProviderStepMessages(ctx context.Context, input agentpkg.ContextStepSelectionInput) agentpkg.ContextStepSelectionResult
func ToolUsageFrag ¶
func ToolUsageFrag(usage string, scope contextfrag.Scope) contextfrag.ContextFrag
func TrimNoticeFrag ¶
func TrimNoticeFrag(scope contextfrag.Scope) contextfrag.ContextFrag
TrimNoticeFrag is the synthetic fragment the builder splices in when budget trimming dropped history, mirroring the legacy resolver notice message.
Types ¶
type BudgetEnvelope ¶
type BudgetEnvelope struct {
MaxTokens int
// Plan activates unified provider-envelope budgeting. Nil preserves the
// legacy unbudgeted provider selection path. Either Plan or
// EnforceProtectedBudget makes the budget a provider allowance, which
// charges fragments at least the provider envelope estimate.
Plan *contextfrag.ContextBudgetPlan
// EnforceProtectedBudget makes MaxTokens a hard allowance that includes
// must-keep fragments without reactivating the turn-start system pass.
// Step reselection uses it to fail closed when newly injected protected
// context cannot fit the remaining provider envelope.
EnforceProtectedBudget bool
// RecentProtectTokens bands the newest droppable history within this many
// charged tokens to drop last. Zero disables the window.
RecentProtectTokens int
ToolExchange *contextfrag.ToolExchangePolicy
}
type BuildInput ¶
type BuildInput struct {
Scope contextfrag.Scope
Intent contextfrag.Intent
Sources []SourceSpec
Targets []contextfrag.RenderTarget
Budget BudgetEnvelope
DynamicMutators []contextfrag.DynamicMutator
Options BuildOptions
// Mutations is the run's live mutation ledger; renderers record
// post-selection changes such as a final render prune onto it.
Mutations *contextfrag.MutationLedger
}
type BuildOptions ¶
type BuildOptions struct {
DryRun bool
}
type BuildTrace ¶
type BuildTrace struct {
CollectDurations map[string]int64
SelectionSummary SelectionSummary
PlacementSummary PlacementSummary
RenderSummaries map[contextfrag.RenderTarget]RenderSummary
Warnings []contextfrag.ValidationWarning
}
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func NewBuilder ¶
func NewBuilder(collectors CollectorRegistry, selector Selector, placer Placer, renderers RendererRegistry) *Builder
func (*Builder) Build ¶
func (b *Builder) Build(ctx context.Context, input BuildInput) (*ContextView, error)
type CollectRequest ¶
type CollectRequest struct {
Scope contextfrag.Scope
Intent contextfrag.Intent
Config any
}
type Collector ¶
type Collector interface {
Name() string
Collect(context.Context, CollectRequest) ([]contextfrag.ContextFrag, error)
}
type CollectorRegistry ¶
type ContextView ¶
type ContextView struct {
Intent contextfrag.Intent
SourceFrags []contextfrag.ContextFrag
Selected []contextfrag.ContextFrag
Placement PlacementPlan
Manifest contextfrag.Manifest
Rendered map[contextfrag.RenderTarget]RenderedPayload
Trace BuildTrace
}
type CurrentUserCollector ¶
type CurrentUserCollector struct{}
func (*CurrentUserCollector) Collect ¶
func (*CurrentUserCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*CurrentUserCollector) Name ¶
func (*CurrentUserCollector) Name() string
type CurrentUserConfig ¶
type CurrentUserConfig struct {
Query string
}
type DiscussContextCollector ¶
type DiscussContextCollector struct{}
func (*DiscussContextCollector) Collect ¶
func (*DiscussContextCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*DiscussContextCollector) Name ¶
func (*DiscussContextCollector) Name() string
type DiscussContextConfig ¶
type DiscussContextConfig struct {
// ComposedMessages is the authoritative output of timeline composition
// when non-nil.
ComposedMessages []timeline.ContextMessage
// InlineImages are freshly surfaced attachments delivered as native vision
// input on the latest user message.
InlineImages []sdk.ImagePart
}
type DiscussContextInput ¶
type DiscussContextInput struct {
ComposedMessages []timeline.ContextMessage
InlineImages []sdk.ImagePart
SystemFrags []contextfrag.ContextFrag
}
DiscussContextInput carries the authoritative timeline composition and already-typed system fragments for one discuss turn.
type DiscussSDKContextBuilder ¶
type DiscussSDKContextBuilder struct{}
DiscussSDKContextBuilder assembles typed source fragments for a discuss turn without reverse-parsing a system prompt that is already structured.
func (*DiscussSDKContextBuilder) CollectDiscussSourceFrags ¶
func (*DiscussSDKContextBuilder) CollectDiscussSourceFrags( ctx context.Context, scope contextfrag.Scope, system string, input DiscussContextInput, ) ([]contextfrag.ContextFrag, error)
type DropRecord ¶
type DropRecord struct {
FragID string
Ref contextfrag.ContextRef
Reason string
}
type FragmentSelector ¶
type FragmentSelector struct{}
func (*FragmentSelector) ProfileFor ¶
func (*FragmentSelector) ProfileFor(intent contextfrag.Intent) IntentProfile
func (*FragmentSelector) Select ¶
func (*FragmentSelector) Select(frags []contextfrag.ContextFrag, profile IntentProfile, budget BudgetEnvelope) SelectionResult
type HistoryMessagesCollector ¶
type HistoryMessagesCollector struct{}
func (*HistoryMessagesCollector) Collect ¶
func (*HistoryMessagesCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*HistoryMessagesCollector) Name ¶
func (*HistoryMessagesCollector) Name() string
type HistoryMessagesConfig ¶
type HistoryMessagesConfig struct {
Messages []sdk.Message
// CurrentUserMessageIndex identifies a current request already carried in
// Messages. The history collector omits it and the current-user collector
// retains it in the current-user slot.
CurrentUserMessageIndex *int
// MemoryMessageIndex identifies materialized memory recall that must be
// collected separately without changing its provider message position.
MemoryMessageIndex *int
// TokenEstimates carries per-message token estimates computed at the
// source. Missing entries fall back to fragment-side estimation.
TokenEstimates []int
// TrimmablePrefix marks the leading message count that may be dropped.
// Messages at or after the boundary are protected.
TrimmablePrefix int
// RepairToolClosures applies the shared repair when a caller has not
// already repaired its materialized message stream.
RepairToolClosures bool
}
type HookContextCollector ¶
type HookContextCollector struct{}
func (*HookContextCollector) Collect ¶
func (*HookContextCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*HookContextCollector) Name ¶
func (*HookContextCollector) Name() string
type HookContextConfig ¶
type HookContextConfig struct {
// Text is the combined resolver-hook AppendContext text materialized by
// the application. The collector owns its role, placement, and cap.
Text string
}
type IdentityPlacer ¶
type IdentityPlacer struct{}
func (IdentityPlacer) Place ¶
func (IdentityPlacer) Place(selected []contextfrag.ContextFrag, _ contextfrag.Intent) PlacementPlan
type InlineImageCollector ¶
type InlineImageCollector struct{}
func (*InlineImageCollector) Collect ¶
func (*InlineImageCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*InlineImageCollector) Name ¶
func (*InlineImageCollector) Name() string
type InlineImageConfig ¶
type IntentProfile ¶
type IntentProfile struct {
Intent contextfrag.Intent
MustKeepSlots []contextfrag.Slot
// MustKeepFrag evaluates retention that depends on fragment policy rather
// than provider placement alone.
MustKeepFrag func(contextfrag.ContextFrag) bool
// SlotTrustFloors declares the minimum trust level per slot.
SlotTrustFloors map[contextfrag.Slot]contextfrag.TrustLevel
}
type MapCollectorRegistry ¶
type MapCollectorRegistry struct {
// contains filtered or unexported fields
}
func NewMapCollectorRegistry ¶
func NewMapCollectorRegistry(collectors ...Collector) *MapCollectorRegistry
func (*MapCollectorRegistry) Get ¶
func (r *MapCollectorRegistry) Get(name string) (Collector, bool)
func (*MapCollectorRegistry) Names ¶
func (r *MapCollectorRegistry) Names() []string
type MapRendererRegistry ¶
type MapRendererRegistry struct {
// contains filtered or unexported fields
}
func NewMapRendererRegistry ¶
func NewMapRendererRegistry(renderers ...Renderer) *MapRendererRegistry
func (*MapRendererRegistry) Get ¶
func (r *MapRendererRegistry) Get(target contextfrag.RenderTarget) (Renderer, bool)
type MemoryContextCollector ¶
type MemoryContextCollector struct{}
func (*MemoryContextCollector) Collect ¶
func (*MemoryContextCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*MemoryContextCollector) Name ¶
func (*MemoryContextCollector) Name() string
type MemoryContextConfig ¶
type NoopRenderer ¶
type NoopRenderer struct {
TargetName contextfrag.RenderTarget
}
func (NoopRenderer) Render ¶
func (r NoopRenderer) Render(_ context.Context, input RenderInput) (RenderedPayload, error)
func (NoopRenderer) Target ¶
func (r NoopRenderer) Target() contextfrag.RenderTarget
type PassthroughSelector ¶
type PassthroughSelector struct{}
func (PassthroughSelector) ProfileFor ¶
func (PassthroughSelector) ProfileFor(intent contextfrag.Intent) IntentProfile
func (PassthroughSelector) Select ¶
func (PassthroughSelector) Select(frags []contextfrag.ContextFrag, _ IntentProfile, _ BudgetEnvelope) SelectionResult
type PlacementItem ¶
type PlacementItem struct {
FragID string
Slot contextfrag.Slot
Position int
CacheHint contextfrag.CacheClass
Ref contextfrag.ContextRef
}
type PlacementPlan ¶
type PlacementPlan struct {
StablePrefixHash string
FirstVolatileIndex int
Items []PlacementItem
}
type PlacementSummary ¶
type Placer ¶
type Placer interface {
Place([]contextfrag.ContextFrag, contextfrag.Intent) PlacementPlan
}
type RenderInput ¶
type RenderInput struct {
Intent contextfrag.Intent
Selected []contextfrag.ContextFrag
Placement PlacementPlan
Manifest *contextfrag.Manifest
Scope contextfrag.Scope
Target contextfrag.RenderTarget
}
type RenderSummary ¶
type RenderSummary struct {
Target contextfrag.RenderTarget
ContentHash string
ItemCount int
}
type RenderedPayload ¶
type RenderedPayload struct {
Target contextfrag.RenderTarget
ContentHash string
Data any
}
type Renderer ¶
type Renderer interface {
Target() contextfrag.RenderTarget
Render(context.Context, RenderInput) (RenderedPayload, error)
}
type RendererRegistry ¶
type RendererRegistry interface {
Get(contextfrag.RenderTarget) (Renderer, bool)
}
type RuntimeFullContextRenderer ¶ added in v0.20.0
type RuntimeFullContextRenderer struct {
Config RuntimeRenderConfig
}
func (*RuntimeFullContextRenderer) Render ¶ added in v0.20.0
func (r *RuntimeFullContextRenderer) Render(_ context.Context, input RenderInput) (RenderedPayload, error)
func (*RuntimeFullContextRenderer) Target ¶ added in v0.20.0
func (*RuntimeFullContextRenderer) Target() contextfrag.RenderTarget
type RuntimeRenderConfig ¶ added in v0.20.0
type RuntimeRenderConfig struct {
Mode RuntimeRenderMode
ContextMarkdown string
ContextURI string
}
type RuntimeRenderMode ¶ added in v0.20.0
type RuntimeRenderMode string
const RuntimeRenderModeChat RuntimeRenderMode = "chat"
type RuntimeRenderedPayload ¶ added in v0.20.0
type RuntimeSection ¶ added in v0.20.0
type RuntimeSection struct {
ID string
Text string
Kind contextfrag.Kind
Trust contextfrag.TrustLevel
Priority int
CacheClass contextfrag.CacheClass
Budget contextfrag.BudgetPolicy
}
RuntimeSection is one structurally assembled block of the runtime context resource.
type RuntimeSectionsCollector ¶ added in v0.20.0
type RuntimeSectionsCollector struct{}
RuntimeSectionsCollector maps structurally assembled runtime sections to fragments without reparsing their Markdown content.
func (*RuntimeSectionsCollector) Collect ¶ added in v0.20.0
func (*RuntimeSectionsCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*RuntimeSectionsCollector) Name ¶ added in v0.20.0
func (*RuntimeSectionsCollector) Name() string
type RuntimeSectionsConfig ¶ added in v0.20.0
type RuntimeSectionsConfig struct {
Sections []RuntimeSection
}
type SDKMessagesRenderer ¶
type SDKMessagesRenderer struct{}
func (*SDKMessagesRenderer) Render ¶
func (*SDKMessagesRenderer) Render(_ context.Context, input RenderInput) (RenderedPayload, error)
func (*SDKMessagesRenderer) Target ¶
func (*SDKMessagesRenderer) Target() contextfrag.RenderTarget
type SDKRenderedPayload ¶
type SelectionResult ¶
type SelectionResult struct {
Selected []contextfrag.ContextFrag
Dropped []contextfrag.ContextFrag
// FatalError stops rendering while allowing Builder to return the partial,
// content-light selection audit accumulated before the failure.
FatalError error
// TrimNotice reports that budget trimming dropped history and the builder
// must splice the trim notice into Selected at TrimNoticeIndex.
TrimNotice bool
TrimNoticeIndex int
Edited []contextfrag.ContextEditTrace
// EditReasons maps an edited fragment ID to its stable selection reason.
EditReasons map[string]string
Warnings []contextfrag.ValidationWarning
Summary SelectionSummary
}
type SelectionSummary ¶
type SelectionSummary struct {
TotalCollected int
TotalSelected int
TotalDropped int
DropReasons []DropRecord
}
type SelectionTag ¶
type SelectionTag string
const ( TagMustKeep SelectionTag = "must_keep" TagPreserveRecent SelectionTag = "preserve_recent" TagPreserveToolClosure SelectionTag = "preserve_tool_closure" TagCanDrop SelectionTag = "can_drop" )
type Selector ¶
type Selector interface {
ProfileFor(contextfrag.Intent) IntentProfile
Select([]contextfrag.ContextFrag, IntentProfile, BudgetEnvelope) SelectionResult
}
type SourceSpec ¶
type StablePrefixPlacer ¶
type StablePrefixPlacer struct{}
func (StablePrefixPlacer) Place ¶
func (StablePrefixPlacer) Place(selected []contextfrag.ContextFrag, _ contextfrag.Intent) PlacementPlan
type StaticCollector ¶
type StaticCollector struct {
CollectorName string
Frags []contextfrag.ContextFrag
}
func (StaticCollector) Collect ¶
func (c StaticCollector) Collect(_ context.Context, _ CollectRequest) ([]contextfrag.ContextFrag, error)
func (StaticCollector) Name ¶
func (c StaticCollector) Name() string
type SystemPromptCollector ¶
type SystemPromptCollector struct{}
SystemPromptCollector reverse-parses a flat system prompt string for callers that do not already provide typed source fragments.
func (*SystemPromptCollector) Collect ¶
func (*SystemPromptCollector) Collect(_ context.Context, req CollectRequest) ([]contextfrag.ContextFrag, error)
func (*SystemPromptCollector) Name ¶
func (*SystemPromptCollector) Name() string
type SystemPromptConfig ¶
type TaggedFrag ¶
type TaggedFrag struct {
Frag contextfrag.ContextFrag
Tags []SelectionTag
// Tokens is the cost budget arithmetic charges for Frag. A hard provider
// budget prices it with the envelope estimator so selection and the
// rendered-envelope check agree; the fragment's own estimate is left
// untouched so selection decisions do not read as trims.
Tokens int
}
func (TaggedFrag) HasTag ¶
func (t TaggedFrag) HasTag(tag SelectionTag) bool
Source Files
¶
- builder.go
- capability_gate.go
- collector_config.go
- collector_current_user.go
- collector_discuss.go
- collector_history.go
- collector_hook.go
- collector_inline_images.go
- collector_memory.go
- collector_runtime.go
- collector_system.go
- discuss_builder.go
- discuss_context_types.go
- interfaces.go
- passthrough.go
- placement.go
- provider_envelope_budget.go
- provider_run_config.go
- provider_step_reselection.go
- render_runtime.go
- render_sdk.go
- selection_tag.go
- selector.go
- selector_budget.go
- selector_frag_budget.go
- selector_summary_shrink.go
- selector_tool_exchange.go
- system_budget.go
- types.go