agent

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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

func DrainBgNotifications(bm *tools.BackgroundManager, history []adk.Message) []adk.Message

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.AgentMiddleware,
	handlers []adk.ChatModelAgentMiddleware,
	opts ...AgentOption,
) (*adk.ChatModelAgent, error)

NewAgent creates a ChatModelAgent with the following middleware stack (outermost to innermost):

Middlewares (old-style): [langfuse]
Handlers (new-style):   [budget?, compaction?, recovery?, ..., approval+safeTool]

ModelRetryConfig is always enabled (3 retries with default exponential backoff). The opts parameter is variadic so existing callers remain unchanged.

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 NewRecoveryMiddleware

func NewRecoveryMiddleware(layers []RecoveryLayer) adk.ChatModelAgentMiddleware

NewRecoveryMiddleware creates a ChatModelAgentMiddleware with the given recovery layers tried in order.

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) 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 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

func TruncateStr(s string, maxLen int) string

TruncateStr truncates a string to maxLen characters, appending "..." if truncated.

Types

type AgentOption

type AgentOption func(*AgentOptions)

AgentOption is a functional option for NewAgent.

func WithBudget

WithBudget injects a budget tracking middleware.

func WithCompaction

func WithCompaction(m adk.ChatModelAgentMiddleware) AgentOption

WithCompaction injects a context compaction middleware.

func WithRecovery

WithRecovery injects an error recovery middleware.

type AgentOptions

type AgentOptions struct {
	// contains filtered or unexported fields
}

AgentOptions holds optional middlewares injected via functional options.

type ApprovalFunc

type ApprovalFunc func(ctx context.Context, toolName, toolArgs string) (bool, error)

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 ContextOverflowLayer

type ContextOverflowLayer struct {
	KeepRecent int
}

ContextOverflowLayer handles context-too-long errors by trimming older messages.

func (*ContextOverflowLayer) CanHandle

func (l *ContextOverflowLayer) CanHandle(err error, state *adk.ChatModelAgentState) bool

func (*ContextOverflowLayer) Recover

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

type MaxOutputContinuationLayer

type MaxOutputContinuationLayer struct{}

MaxOutputContinuationLayer handles "max output tokens" errors by asking the model to continue from where it stopped.

func (*MaxOutputContinuationLayer) CanHandle

func (l *MaxOutputContinuationLayer) CanHandle(err error, state *adk.ChatModelAgentState) bool

func (*MaxOutputContinuationLayer) Recover

type RecoveryAction

type RecoveryAction struct {
	Type     RecoveryActionType
	Messages []*schema.Message // replacement or patched messages
}

RecoveryAction describes what the recovery middleware should do.

type RecoveryActionType

type RecoveryActionType int

RecoveryActionType categorises the kinds of recovery a layer can perform.

const (
	ActionRetryWithContinuation RecoveryActionType = iota
	ActionRetryWithCompaction
	ActionFallbackModel
)

type RecoveryLayer

type RecoveryLayer interface {
	CanHandle(err error, state *adk.ChatModelAgentState) bool
	Recover(ctx context.Context, err error, state *adk.ChatModelAgentState) (*RecoveryAction, error)
}

RecoveryLayer is a single tier of error recovery.

type RecoveryTracker

type RecoveryTracker struct {
	// contains filtered or unexported fields
}

RecoveryTracker keeps per-action-type retry counts so we don't loop forever.

func NewRecoveryTracker

func NewRecoveryTracker() *RecoveryTracker

NewRecoveryTracker creates a tracker with default max retries.

type ReminderConfig

type ReminderConfig struct {
	TodoStore    *tools.TodoStore
	PlanStore    *tools.PlanStore
	EnvLabel     string
	IsRemote     bool
	ContextLimit int
	TaskManager  *tools.SubagentTaskManager
}

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) Compact

func (s *ThresholdCompactionStrategy) Compact(ctx context.Context, messages []*schema.Message, keepRecent int) ([]*schema.Message, error)

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
)

Jump to

Keyboard shortcuts

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