router

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package router dispatches AITasks to the right AIAgent and wraps each call with per-kind caching and per-kind rate limiting.

The router is intentionally thin: it owns no model code itself. It holds one AIAgent per AITaskKind, an optional *ai.ResultCache per kind, and an optional *ai.RateLimiter per kind. Callers (the agent worker for detect, the admin controller for analyze) pass the constructed task in; the router does cache lookup → rate check → agent.Run → cache write.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAgent = errors.New("router: no agent registered for kind")

ErrNoAgent is returned by Run when no AIAgent is registered for the task's kind. The router never falls back across kinds.

View Source
var ErrRateLimited = errors.New("router: rate limited")

ErrRateLimited is returned by Run when the per-kind rate limiter rejected the call. Callers should treat this as a soft skip (record an "emit_quota" outcome, do not retry within the bucket).

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Agent core.AIAgent
	Cache *ai.ResultCache
	Rate  *ai.RateLimiter
}

Entry is one wired-up (agent, cache, rate) tuple for a single kind. Cache and Rate may be nil — the router treats nil as "no cache" / "no rate cap" respectively.

type Router

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

Router fans an AITask out to the entry for its kind.

func New

func New(entries map[core.AITaskKind]Entry) *Router

New builds a Router from the given per-kind entries. nil agents are dropped so callers can pass `Entry{}` for kinds they have not wired up yet (e.g. analyze remains unset until E4).

func (*Router) Has

func (r *Router) Has(kind core.AITaskKind) bool

Has reports whether an agent is registered for the given kind.

func (*Router) Run

func (r *Router) Run(ctx context.Context, task core.AITask) (*core.AICallResult, error)

Run executes the task on the kind-specific agent, wrapped with cache + rate. Order of operations:

  1. Cache lookup on task.CacheKey() — hit short-circuits and returns a *AICallResult with the cached finding (UserPrompt/RawResponse left empty because the model was not called).
  2. Rate check — false returns ErrRateLimited without calling the agent.
  3. agent.Run — failure is propagated.
  4. Cache write on success.

Run never panics on a zero receiver; it returns ErrNoAgent instead.

Jump to

Keyboard shortcuts

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