recorder

package
v2.36.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ThoughtSourceThinking         = "thinking"
	ThoughtSourceReasoningSummary = "reasoning_summary"
	ThoughtSourceCommentary       = "commentary"
)

Model thought source constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncRecorder

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

AsyncRecorder calls Recorder methods asynchronously and logs any errors which may occur.

func NewAsyncRecorder

func NewAsyncRecorder(logger slog.Logger, wrapped Recorder, timeout time.Duration) *AsyncRecorder

func (*AsyncRecorder) RecordInterception

func (*AsyncRecorder) RecordInterception(context.Context, *InterceptionRecord) error

RecordInterception must NOT be called asynchronously. If an interception cannot be recorded, the whole request should fail.

func (*AsyncRecorder) RecordInterceptionEnded

func (a *AsyncRecorder) RecordInterceptionEnded(ctx context.Context, req *InterceptionRecordEnded) error

func (*AsyncRecorder) RecordModelThought

func (a *AsyncRecorder) RecordModelThought(ctx context.Context, req *ModelThoughtRecord) error

func (*AsyncRecorder) RecordPromptUsage

func (a *AsyncRecorder) RecordPromptUsage(ctx context.Context, req *PromptUsageRecord) error

func (*AsyncRecorder) RecordTokenUsage

func (a *AsyncRecorder) RecordTokenUsage(ctx context.Context, req *TokenUsageRecord) error

func (*AsyncRecorder) RecordToolUsage

func (a *AsyncRecorder) RecordToolUsage(ctx context.Context, req *ToolUsageRecord) error

func (*AsyncRecorder) Wait

func (a *AsyncRecorder) Wait()

func (*AsyncRecorder) WithClient

func (a *AsyncRecorder) WithClient(client string)

func (*AsyncRecorder) WithInitiatorID

func (a *AsyncRecorder) WithInitiatorID(initiatorID string)

func (*AsyncRecorder) WithMetrics

func (a *AsyncRecorder) WithMetrics(m any)

func (*AsyncRecorder) WithModel

func (a *AsyncRecorder) WithModel(model string)

func (*AsyncRecorder) WithProvider

func (a *AsyncRecorder) WithProvider(provider string)

type ErrorType added in v2.36.0

type ErrorType string

ErrorType categorizes the terminal upstream error observed when an interception fails. The empty value means the interception succeeded and no error should be recorded. Values must match the aibridge_interception_error_type Postgres enum.

const (
	// ErrorTypeBadRequest is a malformed or otherwise rejected request (HTTP 400).
	ErrorTypeBadRequest ErrorType = "bad_request"
	// ErrorTypeUnauthorized is an authentication or authorization failure (HTTP 401/403).
	ErrorTypeUnauthorized ErrorType = "unauthorized"
	// ErrorTypeRateLimited is an upstream rate-limit response (HTTP 429).
	ErrorTypeRateLimited ErrorType = "rate_limited"
	// ErrorTypeOverloaded is an upstream overloaded response (Anthropic's HTTP
	// 529 or OpenAI's HTTP 503).
	ErrorTypeOverloaded ErrorType = "overloaded"
	// ErrorTypeServerError is an upstream or gateway server error (HTTP 5xx).
	ErrorTypeServerError ErrorType = "server_error"
	// ErrorTypeTimeout is an upstream request timeout (HTTP 408).
	ErrorTypeTimeout ErrorType = "timeout"
	// ErrorTypeUnknown is any error that could not be categorized.
	ErrorTypeUnknown ErrorType = "unknown"
)

func ErrorTypeFromStatus added in v2.36.0

func ErrorTypeFromStatus(status int) ErrorType

ErrorTypeFromStatus maps a standard upstream HTTP status code to an ErrorType. Provider-specific statuses (e.g. Anthropic's 529) are handled by the provider before calling this. Unrecognized codes yield ErrorTypeUnknown.

type InterceptionRecord

type InterceptionRecord struct {
	ID                    string
	InitiatorID           string
	Metadata              Metadata
	Model                 string
	Provider              string
	ProviderName          string
	StartedAt             time.Time
	ClientSessionID       *string
	Client                string
	UserAgent             string
	CorrelatingToolCallID *string
	// AgentFirewallSessionID is the UUID of the Agent Firewall session
	// that produced this request. Nil when the request did not pass
	// through Agent Firewall.
	AgentFirewallSessionID *string
	// AgentFirewallSequenceNumber is the monotonically increasing
	// sequence number assigned by Agent Firewall. Nil when the request
	// did not pass through Agent Firewall.
	AgentFirewallSequenceNumber *int32
	// CredentialKind is always set: either BYOK or centralized.
	CredentialKind string
	// CredentialHint is only set for BYOK, where the key is known
	// from the request. Centralized uses key failover, so the hint
	// can only be determined at end-of-interception.
	CredentialHint string
}

type InterceptionRecordEnded

type InterceptionRecordEnded struct {
	ID      string
	EndedAt time.Time
	// CredentialHint is the hint observed at end-of-interception.
	// Only applied to the DB row for centralized; ignored for BYOK.
	CredentialHint string
	// ErrorType is the categorized terminal upstream error. Empty when the
	// interception succeeded.
	ErrorType ErrorType
	// ErrorMessage is the raw terminal upstream error message. Empty when the
	// interception succeeded.
	ErrorMessage string
}

type Metadata

type Metadata map[string]any

type ModelThoughtRecord

type ModelThoughtRecord struct {
	InterceptionID string
	Content        string
	Metadata       Metadata
	CreatedAt      time.Time
}

type PromptUsageRecord

type PromptUsageRecord struct {
	InterceptionID string
	MsgID          string
	Prompt         string
	Metadata       Metadata
	CreatedAt      time.Time
}

type Recorder

type Recorder interface {
	// RecordInterception records metadata about an interception with an upstream AI provider.
	RecordInterception(ctx context.Context, req *InterceptionRecord) error
	// RecordInterceptionEnded records that given interception has completed.
	RecordInterceptionEnded(ctx context.Context, req *InterceptionRecordEnded) error
	// RecordTokenUsage records the tokens used in an interception with an upstream AI provider.
	RecordTokenUsage(ctx context.Context, req *TokenUsageRecord) error
	// RecordPromptUsage records the prompts used in an interception with an upstream AI provider.
	RecordPromptUsage(ctx context.Context, req *PromptUsageRecord) error
	// RecordToolUsage records the tools used in an interception with an upstream AI provider.
	RecordToolUsage(ctx context.Context, req *ToolUsageRecord) error
	// RecordModelThought records model thoughts produced in an interception with an upstream AI provider.
	RecordModelThought(ctx context.Context, req *ModelThoughtRecord) error
}

Recorder describes all the possible usage information we need to capture during interactions with AI providers. Additionally, it introduces the concept of an "Interception", which includes information about which provider/model was used and by whom. All usage records should reference this Interception by ID.

type TokenUsageRecord

type TokenUsageRecord struct {
	InterceptionID        string
	MsgID                 string
	Input                 int64
	Output                int64
	CacheReadInputTokens  int64
	CacheWriteInputTokens int64
	// ExtraTokenTypes holds token types which *may* exist over and above input/output.
	// These should ultimately get merged into [Metadata], but it's useful to keep these
	// with their actual type (int64) since [Metadata] is a map[string]any.
	ExtraTokenTypes map[string]int64
	Metadata        Metadata
	CreatedAt       time.Time
}

type ToolArgs

type ToolArgs any

type ToolUsageRecord

type ToolUsageRecord struct {
	InterceptionID string
	MsgID          string
	Tool           string
	// ToolCallID is the correlation ID used to match a tool call to its
	// result (call_id in the Responses API, id in chat completions and
	// Anthropic messages). It is empty for hosted Responses tools (e.g.
	// web_search_call) which the provider executes internally.
	ToolCallID string
	// ItemID is the provider's unique ID for the output item that carried
	// the tool call. It is specific to the OpenAI Responses API, where an
	// output item has both an id and a call_id. It is empty for the chat
	// completions and Anthropic messages APIs, which have no separate item
	// ID concept.
	ItemID          string
	ServerURL       *string
	Args            ToolArgs
	Injected        bool
	InvocationError error
	Metadata        Metadata
	CreatedAt       time.Time
}

type WrappedRecorder

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

WrappedRecorder is a convenience struct which implements RecorderClient and resolves a client before calling each method. It also sets the start/creation time of each record.

func NewWrappedRecorder

func NewWrappedRecorder(logger slog.Logger, tracer trace.Tracer, clientFn func() (Recorder, error)) *WrappedRecorder

func (*WrappedRecorder) RecordInterception

func (r *WrappedRecorder) RecordInterception(ctx context.Context, req *InterceptionRecord) (outErr error)

func (*WrappedRecorder) RecordInterceptionEnded

func (r *WrappedRecorder) RecordInterceptionEnded(ctx context.Context, req *InterceptionRecordEnded) (outErr error)

func (*WrappedRecorder) RecordModelThought

func (r *WrappedRecorder) RecordModelThought(ctx context.Context, req *ModelThoughtRecord) (outErr error)

func (*WrappedRecorder) RecordPromptUsage

func (r *WrappedRecorder) RecordPromptUsage(ctx context.Context, req *PromptUsageRecord) (outErr error)

func (*WrappedRecorder) RecordTokenUsage

func (r *WrappedRecorder) RecordTokenUsage(ctx context.Context, req *TokenUsageRecord) (outErr error)

func (*WrappedRecorder) RecordToolUsage

func (r *WrappedRecorder) RecordToolUsage(ctx context.Context, req *ToolUsageRecord) (outErr error)

Jump to

Keyboard shortcuts

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