Documentation
¶
Index ¶
- Constants
- Variables
- func BuildReductionConfig(rootDir string, contextLimit int, compactThreshold float64, ...) *reduction.Config
- func CompactHistory(ctx context.Context, cm einomodel.BaseChatModel, history []adk.Message) []adk.Message
- func DrainBgNotifications(bm *tools.BackgroundManager, history []adk.Message) []adk.Message
- func NewAgent(ctx context.Context, chatmodel model.ToolCallingChatModel, ...) (*adk.ChatModelAgent, error)
- func NewBudgetMiddleware(manager *BudgetManager, tokenUsage *internalmodel.TokenUsage, ...) adk.ChatModelAgentMiddleware
- func NewCompactionMiddleware(strategy CompactionStrategy, contextLimit int, ...) adk.ChatModelAgentMiddleware
- func NewReminderMiddleware(cfg ReminderConfig, tokenUsage *internalmodel.TokenUsage) adk.ChatModelAgentMiddleware
- func NewTeammateHandlers(approvalFunc ApprovalFunc) []adk.ChatModelAgentMiddleware
- func NewTurnToolResultBudgetMiddleware(maxChars int) adk.ChatModelAgentMiddleware
- func ReductionThreshold(compactThreshold float64) float64
- func SyncSummarization(cap *SummarizationCapture, history []adk.Message, rec *session.Recorder) []adk.Message
- func TruncateStr(s string, maxLen int) string
- type ApprovalFunc
- type BudgetManager
- type BudgetStatus
- type CompactionState
- type CompactionStrategy
- type LocalReductionBackend
- type ReminderConfig
- type SummarizationCapture
- type ThresholdCompactionStrategy
- type WarningLevel
Constants ¶
const DefaultMaxLengthForTrunc = 50000
DefaultMaxLengthForTrunc is the per-result character cap: tool outputs above this are offloaded to a file and truncated in the conversation.
const DefaultTurnToolResultBudget = 150_000
DefaultTurnToolResultBudget is the default aggregate character budget for one turn's new tool results: three full-size (50k) results, ~37k tokens.
Variables ¶
var ReductionExcludeTools = []string{"ask_user", "load_skill"}
ReductionExcludeTools lists tools whose results must never be truncated: their content is irreplaceable direct input (a user's answer, a loaded skill body), not re-derivable command output. Shared by the reduction middleware's TruncExcludeTools and the per-turn budget middleware.
Functions ¶
func BuildReductionConfig ¶ added in v0.9.2
func BuildReductionConfig( rootDir string, contextLimit int, compactThreshold float64, counter func(ctx context.Context, msgs []*schema.Message, tools []*schema.ToolInfo) (int64, error), ) *reduction.Config
BuildReductionConfig is the single source for the reduction middleware configuration shared by all surfaces (TUI/ACP/web). rootDir is where offloaded content is written (both the Backend fallback root and the offload path base). contextLimit is the RAW model window; the clear trigger is computed on the EFFECTIVE window (output headroom reserved) so it uses the same base as the summarization trigger. counter is the TokenCounter to inject; nil keeps eino's default (len/4).
It returns the config rather than the constructed middleware so callers can wrap it in reduction.New and tests can assert fields directly.
func CompactHistory ¶
func CompactHistory(ctx context.Context, cm einomodel.BaseChatModel, history []adk.Message) []adk.Message
CompactHistory summarizes the conversation history using the model, replacing all messages with a system summary + the last few messages.
func DrainBgNotifications ¶
DrainBgNotifications injects any completed background task results into the conversation history so the agent is aware of them on the next turn.
func NewAgent ¶
func NewAgent( ctx context.Context, chatmodel model.ToolCallingChatModel, tools []tool.BaseTool, instruction string, approvalFunc ApprovalFunc, middlewares []adk.ChatModelAgentMiddleware, handlers []adk.ChatModelAgentMiddleware, ) (*adk.ChatModelAgent, error)
NewAgent creates a ChatModelAgent with the following handler stack (outermost to innermost):
Handlers: [langfuse tracing, ...caller handlers, approval+safeTool]
ModelRetryConfig is always enabled (3 retries with default exponential backoff).
func NewBudgetMiddleware ¶
func NewBudgetMiddleware(manager *BudgetManager, tokenUsage *internalmodel.TokenUsage, onWarn func(BudgetStatus)) adk.ChatModelAgentMiddleware
NewBudgetMiddleware creates a ChatModelAgentMiddleware that tracks budget. tokenUsage is the per-agent tracker to read from. onWarn is called when the budget warning level changes to WarningApproach or WarningExceeded. It may be nil.
func NewCompactionMiddleware ¶
func NewCompactionMiddleware(strategy CompactionStrategy, contextLimit int, tokenUsage *internalmodel.TokenUsage, onCompact func(int)) adk.ChatModelAgentMiddleware
NewCompactionMiddleware creates a ChatModelAgentMiddleware that monitors token usage and compacts the conversation when the strategy says to. tokenUsage is the per-agent tracker to read from. onCompact is an optional callback invoked after a successful compaction.
func NewReminderMiddleware ¶
func NewReminderMiddleware(cfg ReminderConfig, tokenUsage *internalmodel.TokenUsage) adk.ChatModelAgentMiddleware
NewReminderMiddleware creates a ChatModelAgentMiddleware that injects conditional reminders (todo check, token warning, error streak, external file changes, env drift, AGENTS.md reload) into the message stream before each model invocation. tokenUsage is the per-agent tracker to read from; may be nil.
func NewTeammateHandlers ¶
func NewTeammateHandlers(approvalFunc ApprovalFunc) []adk.ChatModelAgentMiddleware
NewTeammateHandlers returns the middleware stack for a teammate agent. It includes the approval + safe-tool-error middleware with the given approval function.
func NewTurnToolResultBudgetMiddleware ¶ added in v0.9.2
func NewTurnToolResultBudgetMiddleware(maxChars int) adk.ChatModelAgentMiddleware
NewTurnToolResultBudgetMiddleware creates a middleware enforcing an aggregate character budget across the trailing batch of tool results. maxChars <= 0 selects DefaultTurnToolResultBudget. Tools in ReductionExcludeTools are never truncated.
func ReductionThreshold ¶ added in v0.9.2
ReductionThreshold derives the reduction (tool-output clearing) trigger fraction from the compaction threshold: the lighter, earlier clearing sits 0.15 below compaction; when that would drop under 0.1 it falls back to 80% of the compaction threshold. Single source for what was previously copy-pasted (and drifting) across the TUI/ACP/web surfaces.
func SyncSummarization ¶
func SyncSummarization(cap *SummarizationCapture, history []adk.Message, rec *session.Recorder) []adk.Message
SyncSummarization checks whether Eino's summarization middleware fired during the last runner.Run() and, if so, replaces history with the compacted version so the next turn starts from the summarized state.
func TruncateStr ¶
TruncateStr truncates a string to maxLen characters, appending "..." if truncated.
Types ¶
type ApprovalFunc ¶
type BudgetManager ¶
type BudgetManager struct {
// contains filtered or unexported fields
}
BudgetManager tracks token consumption and cost against configurable limits.
func NewBudgetManager ¶
func NewBudgetManager(cfg *config.BudgetConfig, pricing internalmodel.ModelPricing) *BudgetManager
NewBudgetManager creates a BudgetManager from config and model pricing. A nil BudgetConfig results in a manager with no limits.
func (*BudgetManager) Check ¶
func (b *BudgetManager) Check() (BudgetStatus, bool)
Check returns the current budget status and whether the budget has been exceeded.
func (*BudgetManager) Status ¶
func (b *BudgetManager) Status() BudgetStatus
Status returns the current budget status without modifying state.
func (*BudgetManager) Track ¶
func (b *BudgetManager) Track(promptTokens, completionTokens int64) BudgetStatus
Track records a model call's token usage and returns the updated status.
type BudgetStatus ¶
type BudgetStatus struct {
PromptTokens int64
CompletionTokens int64
TotalTokens int64
EstimatedCost float64
RemainingBudget float64
WarningLevel WarningLevel
}
BudgetStatus is a snapshot of current token/cost usage.
type CompactionState ¶
type CompactionState struct {
// contains filtered or unexported fields
}
CompactionState tracks compaction history for diagnostics.
func (*CompactionState) CompactionCount ¶
func (cs *CompactionState) CompactionCount() int
CompactionCount returns how many compactions have occurred.
func (*CompactionState) SavedTokens ¶
func (cs *CompactionState) SavedTokens() int
SavedTokens returns the total estimated tokens saved by compaction.
type CompactionStrategy ¶
type CompactionStrategy interface {
// ShouldCompact returns true when the current token count warrants compaction.
ShouldCompact(currentTokens, limit int) bool
// Compact compresses the messages slice, keeping the most recent keepRecent
// messages intact and summarising the rest.
Compact(ctx context.Context, messages []*schema.Message, keepRecent int) ([]*schema.Message, error)
}
CompactionStrategy decides when and how to compact conversation history.
type LocalReductionBackend ¶
type LocalReductionBackend struct {
RootDir string
}
LocalReductionBackend implements reduction.Backend by writing truncated tool output to local files so the agent can re-read them via the read tool.
func (*LocalReductionBackend) Write ¶
func (b *LocalReductionBackend) Write(_ context.Context, req *filesystem.WriteRequest) error
type ReminderConfig ¶
type ReminderConfig struct {
TodoStore *tools.TodoStore
GoalStore *tools.GoalStore
PlanStore *tools.PlanStore
EnvLabel string
IsRemote bool
ContextLimit int
TaskManager *tools.SubagentTaskManager
// FileTracker, when non-nil, is swept before each model call for files
// modified outside the session since the agent last read them.
FileTracker *tools.FileTracker
// Env, when non-nil, is the live tool environment. switch_env mutates it
// in place (local ↔ SSH/Docker) without rebuilding the agent on every
// surface, so remote-ness is re-read from here each round: while remote,
// the env-drift / AGENTS.md / external-file sweeps are paused — they all
// inspect the LOCAL filesystem and would report wrong-host state.
Env *tools.Env
// Pwd enables the periodic environment refresh and the AGENTS.md reload
// check. Empty disables both (remote sessions, subagents, tests).
Pwd string
// Platform is the platform string used when serializing env snapshots.
Platform string
// EnvSnapshot is the SerializeEnvInfo baseline captured at startup. When
// empty, the first refresh cycle only establishes a baseline (no diff).
EnvSnapshot string
// EnvRefreshEvery is the env-refresh cadence in model iterations;
// 0 means defaultEnvRefreshEvery.
EnvRefreshEvery int
// EnvCollector overrides the environment collector (for tests); nil uses
// utils.CollectEnvInfoLight.
EnvCollector func(pwd string) *utils.EnvInfo
}
ReminderConfig holds the static configuration for the reminder middleware.
type SummarizationCapture ¶
type SummarizationCapture struct {
// contains filtered or unexported fields
}
SummarizationCapture captures the result when Eino's summarization middleware fires, so that the application-level history can be synced afterwards.
func (*SummarizationCapture) Capture ¶
func (c *SummarizationCapture) Capture(summary string, compactedN int)
Capture records a summarization event. Called from the Finalize callback.
type ThresholdCompactionStrategy ¶
type ThresholdCompactionStrategy struct {
// contains filtered or unexported fields
}
ThresholdCompactionStrategy triggers compaction when token usage exceeds a configurable fraction of the context limit and uses a chat model to generate a summary of older messages.
func NewThresholdCompactionStrategy ¶
func NewThresholdCompactionStrategy(threshold float64, summarizer einomodel.ToolCallingChatModel, keepRecent int) *ThresholdCompactionStrategy
NewThresholdCompactionStrategy creates a compaction strategy. threshold is the fraction (0-1) of the context limit that triggers compaction. summarizer is the model used to generate summaries. keepRecent is the number of recent messages to preserve verbatim.
func (*ThresholdCompactionStrategy) ShouldCompact ¶
func (s *ThresholdCompactionStrategy) ShouldCompact(currentTokens, limit int) bool
type WarningLevel ¶
type WarningLevel int
WarningLevel indicates the severity of a budget warning.
const ( WarningNone WarningLevel = iota WarningApproach // approaching budget limit WarningExceeded // budget limit exceeded )