plugin

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package plugin defines the extension system for Nexus. Extensions are notified of lifecycle events (request received, completed, failed, cached, etc.) and can react to them — audit logging, metrics, tracing, webhooks, etc.

Each lifecycle hook is a separate interface so extensions opt in only to the events they care about.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BudgetExceeded

type BudgetExceeded interface {
	OnBudgetExceeded(ctx context.Context, tenantID id.TenantID) error
}

BudgetExceeded is called when budget is exceeded.

type BudgetWarning

type BudgetWarning interface {
	OnBudgetWarning(ctx context.Context, tenantID id.TenantID, usedPct float64) error
}

BudgetWarning is called at 80% budget threshold.

type ChunkReceived

type ChunkReceived interface {
	OnChunkReceived(ctx context.Context, requestID id.RequestID, kind provider.EventKind, byteSize int) error
}

ChunkReceived is called per chunk (or per N chunks, configurable). The payload is intentionally tiny (kind + byte size) — full chunk payloads stay off the hot path. Implementations must return promptly.

type CircuitOpened

type CircuitOpened interface {
	OnCircuitOpened(ctx context.Context, providerName string) error
}

CircuitOpened is called when a circuit breaker opens for a provider.

type Extension

type Extension interface {
	// Name returns a unique human-readable name for the extension.
	Name() string
}

Extension is the base interface all extensions must implement.

type FallbackTriggered

type FallbackTriggered interface {
	OnFallbackTriggered(ctx context.Context, from string, to string) error
}

FallbackTriggered is called when a fallback provider is used.

type GuardrailBlocked

type GuardrailBlocked interface {
	OnGuardrailBlocked(ctx context.Context, guardName string, requestID id.RequestID) error
}

GuardrailBlocked is called when a guardrail blocks a request.

type GuardrailRedacted

type GuardrailRedacted interface {
	OnGuardrailRedacted(ctx context.Context, guardName string, field string) error
}

GuardrailRedacted is called when a guardrail redacts content.

type KeyCreated

type KeyCreated interface {
	OnKeyCreated(ctx context.Context, keyID id.KeyID, tenantID id.TenantID) error
}

KeyCreated is called when an API key is created.

type KeyRevoked

type KeyRevoked interface {
	OnKeyRevoked(ctx context.Context, keyID id.KeyID) error
}

KeyRevoked is called when an API key is revoked.

type ProviderFailed

type ProviderFailed interface {
	OnProviderFailed(ctx context.Context, providerName string, model string, err error) error
}

ProviderFailed is called when a provider call fails (before retry/fallback).

type Registry

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

Registry holds registered extensions and dispatches lifecycle events to them. It type-caches extensions at registration time so emit calls iterate only over extensions that implement the relevant hook.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an extension registry with a default logger.

func NewRegistryWithLogger

func NewRegistryWithLogger(logger log.Logger) *Registry

NewRegistryWithLogger creates an extension registry with the given logger.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of registered extensions.

func (*Registry) EmitBudgetExceeded

func (r *Registry) EmitBudgetExceeded(ctx context.Context, tenantID id.TenantID)

EmitBudgetExceeded notifies all extensions that implement BudgetExceeded.

func (*Registry) EmitBudgetWarning

func (r *Registry) EmitBudgetWarning(ctx context.Context, tenantID id.TenantID, usedPct float64)

EmitBudgetWarning notifies all extensions that implement BudgetWarning.

func (*Registry) EmitChunkReceived

func (r *Registry) EmitChunkReceived(ctx context.Context, requestID id.RequestID, kind provider.EventKind, byteSize int)

EmitChunkReceived notifies all extensions that implement ChunkReceived. Hot-path: skip the loop entirely if nobody is listening.

func (*Registry) EmitCircuitOpened

func (r *Registry) EmitCircuitOpened(ctx context.Context, providerName string)

EmitCircuitOpened notifies all extensions that implement CircuitOpened.

func (*Registry) EmitFallbackTriggered

func (r *Registry) EmitFallbackTriggered(ctx context.Context, from, to string)

EmitFallbackTriggered notifies all extensions that implement FallbackTriggered.

func (*Registry) EmitGuardrailBlocked

func (r *Registry) EmitGuardrailBlocked(ctx context.Context, guardName string, requestID id.RequestID)

EmitGuardrailBlocked notifies all extensions that implement GuardrailBlocked.

func (*Registry) EmitGuardrailRedacted

func (r *Registry) EmitGuardrailRedacted(ctx context.Context, guardName, field string)

EmitGuardrailRedacted notifies all extensions that implement GuardrailRedacted.

func (*Registry) EmitKeyCreated

func (r *Registry) EmitKeyCreated(ctx context.Context, keyID id.KeyID, tenantID id.TenantID)

EmitKeyCreated notifies all extensions that implement KeyCreated.

func (*Registry) EmitKeyRevoked

func (r *Registry) EmitKeyRevoked(ctx context.Context, keyID id.KeyID)

EmitKeyRevoked notifies all extensions that implement KeyRevoked.

func (*Registry) EmitProviderFailed

func (r *Registry) EmitProviderFailed(ctx context.Context, providerName, model string, provErr error)

EmitProviderFailed notifies all extensions that implement ProviderFailed.

func (*Registry) EmitRequestCached

func (r *Registry) EmitRequestCached(ctx context.Context, requestID id.RequestID, model string)

EmitRequestCached notifies all extensions that implement RequestCached.

func (*Registry) EmitRequestCompleted

func (r *Registry) EmitRequestCompleted(ctx context.Context, requestID id.RequestID, model, providerName string, elapsed time.Duration, inputTokens, outputTokens int)

EmitRequestCompleted notifies all extensions that implement RequestCompleted.

func (*Registry) EmitRequestFailed

func (r *Registry) EmitRequestFailed(ctx context.Context, requestID id.RequestID, model string, reqErr error)

EmitRequestFailed notifies all extensions that implement RequestFailed.

func (*Registry) EmitRequestReceived

func (r *Registry) EmitRequestReceived(ctx context.Context, requestID id.RequestID, model, tenantID string)

EmitRequestReceived notifies all extensions that implement RequestReceived.

func (*Registry) EmitStreamCompleted

func (r *Registry) EmitStreamCompleted(ctx context.Context, requestID id.RequestID, model, providerName string, elapsed time.Duration, final *provider.CompletionResponse)

EmitStreamCompleted notifies all extensions that implement StreamCompleted.

func (*Registry) EmitStreamFailed

func (r *Registry) EmitStreamFailed(ctx context.Context, requestID id.RequestID, model string, streamErr error)

EmitStreamFailed notifies all extensions that implement StreamFailed.

func (*Registry) EmitStreamStarted

func (r *Registry) EmitStreamStarted(ctx context.Context, requestID id.RequestID, model, providerName string)

EmitStreamStarted notifies all extensions that implement StreamStarted.

func (*Registry) EmitTenantCreated

func (r *Registry) EmitTenantCreated(ctx context.Context, tenantID id.TenantID)

EmitTenantCreated notifies all extensions that implement TenantCreated.

func (*Registry) EmitTenantDisabled

func (r *Registry) EmitTenantDisabled(ctx context.Context, tenantID id.TenantID)

EmitTenantDisabled notifies all extensions that implement TenantDisabled.

func (*Registry) Extensions

func (r *Registry) Extensions() []Extension

Extensions returns all registered extensions.

func (*Registry) HasChunkReceived

func (r *Registry) HasChunkReceived() bool

HasChunkReceived reports whether at least one extension implements ChunkReceived. Used by the stream lifecycle middleware to skip per-chunk instrumentation when no one is listening.

func (*Registry) Register

func (r *Registry) Register(e Extension)

Register adds an extension and type-asserts it into all applicable hook caches. Extensions are notified in registration order.

type RequestCached

type RequestCached interface {
	OnRequestCached(ctx context.Context, requestID id.RequestID, model string) error
}

RequestCached is called when a response is served from cache.

type RequestCompleted

type RequestCompleted interface {
	OnRequestCompleted(ctx context.Context, requestID id.RequestID, model string, providerName string, elapsed time.Duration, inputTokens int, outputTokens int) error
}

RequestCompleted is called after a successful provider response.

type RequestFailed

type RequestFailed interface {
	OnRequestFailed(ctx context.Context, requestID id.RequestID, model string, err error) error
}

RequestFailed is called when a request fails.

type RequestReceived

type RequestReceived interface {
	OnRequestReceived(ctx context.Context, requestID id.RequestID, model string, tenantID string) error
}

RequestReceived is called when a request enters the pipeline.

type StreamCompleted

type StreamCompleted interface {
	OnStreamCompleted(ctx context.Context, requestID id.RequestID, model, providerName string, elapsed time.Duration, final *provider.CompletionResponse) error
}

StreamCompleted is called after a stream drains successfully. The final argument carries the merged CompletionResponse (Content concat, ToolCalls merged, Usage from the last chunk) — same shape Provider.Complete would have produced. Implementations may treat it as authoritative.

type StreamFailed

type StreamFailed interface {
	OnStreamFailed(ctx context.Context, requestID id.RequestID, model string, err error) error
}

StreamFailed is called when a stream errors mid-flight or fails to start.

type StreamStarted

type StreamStarted interface {
	OnStreamStarted(ctx context.Context, requestID id.RequestID, model, providerName string) error
}

StreamStarted is called once per streaming request, when the first chunk arrives from the provider. Use this to start spans, open audit records, etc.

type TenantCreated

type TenantCreated interface {
	OnTenantCreated(ctx context.Context, tenantID id.TenantID) error
}

TenantCreated is called when a new tenant is created.

type TenantDisabled

type TenantDisabled interface {
	OnTenantDisabled(ctx context.Context, tenantID id.TenantID) error
}

TenantDisabled is called when a tenant is disabled.

Jump to

Keyboard shortcuts

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