utils

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

budget.go: session-level quota for tokens, tool calls, and time.

loopdetect.go: loop detection for repeated/similar actions.

permission.go: permission evaluation (allow/deny/ask) for tools and operations.

ratelimit.go: token-bucket rate limiting for request throttling.

Index

Constants

View Source
const (
	LoopDetectWindowSize     = 10
	LoopDetectMaxRepeats     = 5
	LoopDetectWarningRepeats = 3
	LoopDetectExactMatchSize = 5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Budget

type Budget interface {
	Check(ctx context.Context, req BudgetRequest) (BudgetResult, error)
	Consume(ctx context.Context, sessionID string, amount Cost) error
	GetState(sessionID string) BudgetState
	Reset(sessionID string)
	ResetAll()
	RecordToolCall(ctx context.Context, sessionID string)
	RecordTokens(ctx context.Context, sessionID string, tokens int)
	CanExecute(sessionID string) bool
}

func NewBudget

func NewBudget(cfg *BudgetConfig) Budget

type BudgetConfig

type BudgetConfig struct {
	Enabled      bool
	MaxTokens    int
	MaxToolCalls int
	MaxTimeMs    int64
}

type BudgetRequest

type BudgetRequest struct {
	SessionID string
	ToolCalls []string
	Estimated Cost
}

type BudgetResult

type BudgetResult struct {
	Allowed bool   `json:"allowed"`
	Reason  string `json:"reason"`
	Remain  Cost   `json:"remain"`
}

type BudgetState

type BudgetState struct {
	UsedTokens int   `json:"used_tokens"`
	UsedCalls  int   `json:"used_calls"`
	UsedTimeMs int64 `json:"used_time_ms"`
	MaxTokens  int   `json:"max_tokens"`
	MaxCalls   int   `json:"max_calls"`
	MaxTimeMs  int64 `json:"max_time_ms"`
}

type Cost

type Cost struct {
	Tokens int   `json:"tokens"`
	Calls  int   `json:"calls"`
	TimeMs int64 `json:"time_ms"`
}

type LoopDetectAction

type LoopDetectAction struct {
	Type      string    `json:"type"`
	Content   string    `json:"content"`
	Timestamp time.Time `json:"timestamp"`
}

type LoopDetectConfig

type LoopDetectConfig struct {
	Enabled             bool
	MaxRepeats          int
	WarningRepeats      int
	SimilarityThreshold float64
	TrackResults        bool
	InputMaxRepeats     int
}

func DefaultLoopDetectConfig

func DefaultLoopDetectConfig() *LoopDetectConfig

type LoopDetectResult

type LoopDetectResult struct {
	IsLoop     bool    `json:"is_loop"`
	IsWarning  bool    `json:"is_warning"`
	Count      int     `json:"count"`
	Similarity float64 `json:"similarity"`
	Suggestion string  `json:"suggestion"`
	Level      string  `json:"level"` // "warning" or "critical"
}

type LoopDetectStats

type LoopDetectStats struct {
	TotalActions int            `json:"total_actions"`
	ActionCounts map[string]int `json:"action_counts"`
}

type LoopDetector

type LoopDetector interface {
	Detect(ctx context.Context, sessionID string, action LoopDetectAction) *LoopDetectResult
	Record(sessionID string, action LoopDetectAction)
	RecordWithResult(sessionID string, action LoopDetectAction, resultHash string)
	Reset(sessionID string)
	GetStats(sessionID string) LoopDetectStats
}

func NewLoopDetector

func NewLoopDetector(cfg *LoopDetectConfig) LoopDetector

type PermAction

type PermAction string
const (
	PermActionAllow PermAction = "allow"
	PermActionDeny  PermAction = "deny"
	PermActionAsk   PermAction = "ask"
)

type PermEvaluator

type PermEvaluator struct {
	Ruleset PermRuleset
	// contains filtered or unexported fields
}

func NewPermEvaluator

func NewPermEvaluator(ruleset PermRuleset) *PermEvaluator

func (*PermEvaluator) Evaluate

func (e *PermEvaluator) Evaluate(permission string, input map[string]interface{}) PermAction

func (*PermEvaluator) Merge

func (e *PermEvaluator) Merge(other PermRuleset)

type PermRule

type PermRule struct {
	Permission string                 `json:"permission"`
	Action     PermAction             `json:"action"`
	Pattern    string                 `json:"pattern,omitempty"`
	Extra      map[string]interface{} `json:"extra,omitempty"`
}

func PermEvaluate

func PermEvaluate(permission string, name string, ruleset PermRuleset) PermRule

type PermRuleset

type PermRuleset []PermRule

func PermFromAllowDenyAsk

func PermFromAllowDenyAsk(denied, ask, allowed []string) PermRuleset

func PermFromConfig

func PermFromConfig(config map[string]interface{}) PermRuleset

func PermMerge

func PermMerge(rulesets ...PermRuleset) PermRuleset

type RateLimitStats

type RateLimitStats struct {
	Allowed  int64
	Rejected int64
	WaitTime time.Duration
	HitRate  float64
}

type RateLimiter

type RateLimiter interface {
	Allow(ctx context.Context) error
	Wait(ctx context.Context) error
	Stats() RateLimitStats
}

func NewTokenBucket

func NewTokenBucket(capacity int64, refillRate int64) RateLimiter

Jump to

Keyboard shortcuts

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