openai

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

Package openai provides an OpenAI-compatible API server for OmniAgent.

Index

Constants

View Source
const (
	NamingSourceOpenAI    = "openai"
	NamingSourceEchartify = "echartify"
	NamingSourceOmniAgent = "omniagent"
)

Naming convention sources

Variables

View Source
var ErrUnauthorized = &unauthorizedError{}

ErrUnauthorized is returned when authentication fails.

Functions

func CalculateCost

func CalculateCost(model string, promptTokens, completionTokens int) float64

CalculateCost estimates the cost based on model and tokens.

func GenerateCostChart

func GenerateCostChart(ts *UsageTimeseries) *chartir.ChartIR

GenerateCostChart generates a ChartIR for cost over time.

func GenerateModelPieChart

func GenerateModelPieChart(summary *UsageSummary) *chartir.ChartIR

GenerateModelPieChart generates a ChartIR for usage by model.

func GenerateTokensChart

func GenerateTokensChart(ts *UsageTimeseries) *chartir.ChartIR

GenerateTokensChart generates a ChartIR for token usage over time.

func GetAAuthClaims

func GetAAuthClaims(ctx context.Context) *auth.AAuthClaims

GetAAuthClaims retrieves AAuth claims from the context. Deprecated: Use GetAgentAuthClaims for unified claim access.

func GetAgentAuthClaims

func GetAgentAuthClaims(ctx context.Context) *auth.AgentAuthClaims

GetAgentAuthClaims retrieves agent authentication claims from the context. Works for both ID-JAG and AAuth tokens.

func LoadImageConfigFromEnv added in v0.12.0

func LoadImageConfigFromEnv() *config.ImageConfig

LoadImageConfigFromEnv loads image configuration from environment variables.

Types

type AgentHandler

type AgentHandler interface {
	// ChatCompletion handles a chat completion request.
	// Returns the response text.
	ChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)

	// ChatCompletionStream handles streaming chat completion.
	// Calls onDelta for each chunk, should return after all chunks are sent.
	ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest,
		onDelta func(delta *ChatCompletionChunk) error) error

	// ListModels returns available models/agents.
	ListModels(ctx context.Context) ([]Model, error)

	// GetModel returns details for a specific model/agent.
	GetModel(ctx context.Context, modelID string) (*Model, error)

	// ListTools returns available tools registered with the agent.
	ListTools(ctx context.Context) ([]ToolInfo, error)

	// ListCronJobs returns all scheduled jobs.
	ListCronJobs(ctx context.Context) ([]CronJobInfo, error)

	// GetCronJob returns a specific job by ID.
	GetCronJob(ctx context.Context, id string) (*CronJobInfo, error)

	// CreateCronJob creates a new scheduled job.
	CreateCronJob(ctx context.Context, req *CreateCronJobRequest) (*CronJobInfo, error)

	// UpdateCronJob updates an existing job.
	UpdateCronJob(ctx context.Context, id string, req *UpdateCronJobRequest) (*CronJobInfo, error)

	// DeleteCronJob removes a job.
	DeleteCronJob(ctx context.Context, id string) error

	// TriggerCronJob runs a job immediately.
	TriggerCronJob(ctx context.Context, id string) (*CronJobResult, error)

	// EnableCronJob enables a disabled job.
	EnableCronJob(ctx context.Context, id string) error

	// DisableCronJob disables a job without deleting it.
	DisableCronJob(ctx context.Context, id string) error
}

AgentHandler defines the interface that agent implementations must satisfy. OmniAgent's agent.Agent will implement this interface via an adapter.

type AgentInfo

type AgentInfo struct {
	ID           string    `json:"id"`
	Name         string    `json:"name"`
	Description  string    `json:"description,omitempty"`
	Provider     string    `json:"provider"`
	Model        string    `json:"model"`
	Temperature  float64   `json:"temperature,omitempty"`
	MaxTokens    int       `json:"max_tokens,omitempty"`
	SystemPrompt string    `json:"system_prompt,omitempty"`
	AllowedTools []string  `json:"allowed_tools,omitempty"`
	DeniedTools  []string  `json:"denied_tools,omitempty"`
	Enabled      bool      `json:"enabled"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
}

AgentInfo represents information about a configured agent.

type AgentManager

type AgentManager interface {
	// ListAgents returns all configured agents.
	ListAgents(ctx context.Context) ([]AgentInfo, error)

	// GetAgent returns details for a specific agent.
	GetAgent(ctx context.Context, id string) (*AgentInfo, error)

	// CreateAgent creates a new agent configuration.
	CreateAgent(ctx context.Context, req *CreateAgentRequest) (*AgentInfo, error)

	// UpdateAgent updates an existing agent configuration.
	UpdateAgent(ctx context.Context, id string, req *UpdateAgentRequest) (*AgentInfo, error)

	// DeleteAgent removes an agent.
	DeleteAgent(ctx context.Context, id string) error

	// CloneAgent duplicates an existing agent.
	CloneAgent(ctx context.Context, id string, req *CloneAgentRequest) (*AgentInfo, error)
}

AgentManager is an optional interface for managing multiple agents. Adapters that support multi-agent can implement this interface.

type AgentUsage

type AgentUsage struct {
	AgentID          string  `json:"agent_id"`
	Requests         int64   `json:"requests"`
	PromptTokens     int64   `json:"prompt_tokens"`
	CompletionTokens int64   `json:"completion_tokens"`
	TotalTokens      int64   `json:"total_tokens"`
	Cost             float64 `json:"cost"`
}

AgentUsage tracks usage per agent.

type ChatCompletionChunk

type ChatCompletionChunk struct {
	ID                string        `json:"id"`
	Object            string        `json:"object"`
	Created           int64         `json:"created"`
	Model             string        `json:"model"`
	Choices           []ChunkChoice `json:"choices"`
	Usage             *Usage        `json:"usage,omitempty"`
	SystemFingerprint string        `json:"system_fingerprint,omitempty"`
}

ChatCompletionChunk represents a streaming chunk.

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model            string    `json:"model"`
	Messages         []Message `json:"messages"`
	Temperature      *float64  `json:"temperature,omitempty"`
	TopP             *float64  `json:"top_p,omitempty"`
	N                *int      `json:"n,omitempty"`
	Stream           bool      `json:"stream,omitempty"`
	Stop             []string  `json:"stop,omitempty"`
	MaxTokens        *int      `json:"max_tokens,omitempty"`
	PresencePenalty  *float64  `json:"presence_penalty,omitempty"`
	FrequencyPenalty *float64  `json:"frequency_penalty,omitempty"`
	User             string    `json:"user,omitempty"`
	Tools            []Tool    `json:"tools,omitempty"`
	ToolChoice       any       `json:"tool_choice,omitempty"`
	Seed             *int      `json:"seed,omitempty"`
}

ChatCompletionRequest represents a chat completion request.

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID                string   `json:"id"`
	Object            string   `json:"object"`
	Created           int64    `json:"created"`
	Model             string   `json:"model"`
	Choices           []Choice `json:"choices"`
	Usage             *Usage   `json:"usage,omitempty"`
	SystemFingerprint string   `json:"system_fingerprint,omitempty"`
}

ChatCompletionResponse represents a non-streaming chat completion response.

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
	Logprobs     any     `json:"logprobs,omitempty"`
}

Choice represents a completion choice.

type ChunkChoice

type ChunkChoice struct {
	Index        int        `json:"index"`
	Delta        ChunkDelta `json:"delta"`
	FinishReason *string    `json:"finish_reason"`
	Logprobs     any        `json:"logprobs,omitempty"`
}

ChunkChoice represents a choice in a streaming chunk.

type ChunkDelta

type ChunkDelta struct {
	Role      string     `json:"role,omitempty"`
	Content   string     `json:"content,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

ChunkDelta represents the delta content in a streaming chunk.

type CloneAgentRequest

type CloneAgentRequest struct {
	NewID   string `json:"new_id,omitempty"`
	NewName string `json:"new_name"`
}

CloneAgentRequest represents a request to clone an agent.

type Config

type Config struct {
	// APIKeys is a list of valid API keys. If empty, no authentication is required.
	APIKeys []string

	// OpenAIPrefix is the URL prefix for OpenAI-compatible routes (default: "/openai/v1").
	OpenAIPrefix string

	// APIPrefix is the URL prefix for OmniAgent API routes (default: "/api").
	APIPrefix string

	// WebUI enables the embedded chat web UI at the root path.
	WebUI bool

	// PhoneNumber is the phone number to display in the web UI for voice calls.
	PhoneNumber string

	// Auth holds OAuth authentication configuration for the web UI.
	Auth *auth.Config

	// AAuth holds AAuth token validation configuration.
	// Deprecated: Use AgentAuth for unified ID-JAG and AAuth support.
	AAuth *auth.AAuthConfig

	// AgentAuth holds agent authentication configuration for both ID-JAG and AAuth.
	// This enables policy-based routing: ID-JAG for automatic auth, AAuth for sensitive actions.
	AgentAuth *auth.AgentAuthConfig

	// BaseURL is the public URL of the server (required for OAuth callbacks).
	BaseURL string

	// Logger is the logger for the server.
	Logger *slog.Logger

	// ImageHandler is an optional handler for image generation operations.
	ImageHandler ImageHandler
}

Config configures the server.

type CreateAgentRequest

type CreateAgentRequest struct {
	ID           string   `json:"id,omitempty"`
	Name         string   `json:"name"`
	Description  string   `json:"description,omitempty"`
	Provider     string   `json:"provider,omitempty"`
	Model        string   `json:"model,omitempty"`
	APIKey       string   `json:"api_key,omitempty"` //nolint:gosec // G101: API key in request
	BaseURL      string   `json:"base_url,omitempty"`
	Temperature  float64  `json:"temperature,omitempty"`
	MaxTokens    int      `json:"max_tokens,omitempty"`
	SystemPrompt string   `json:"system_prompt,omitempty"`
	AllowedTools []string `json:"allowed_tools,omitempty"`
	DeniedTools  []string `json:"denied_tools,omitempty"`
}

CreateAgentRequest represents a request to create an agent.

type CreateCronJobRequest

type CreateCronJobRequest struct {
	Name        string           `json:"name"`
	Description string           `json:"description,omitempty"`
	Schedule    CronScheduleInfo `json:"schedule"`
	Action      CronActionInfo   `json:"action"`
}

CreateCronJobRequest represents a request to create a scheduled job.

type CreateImageEditRequest added in v0.12.0

type CreateImageEditRequest struct {
	Model          string `json:"model,omitempty"`
	Image          string `json:"image"`
	Mask           string `json:"mask,omitempty"`
	Prompt         string `json:"prompt"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

CreateImageEditRequest represents a request to edit an image.

type CreateImageRequest added in v0.12.0

type CreateImageRequest struct {
	Model          string `json:"model,omitempty"`
	Prompt         string `json:"prompt"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	Quality        string `json:"quality,omitempty"`
	Style          string `json:"style,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

CreateImageRequest represents a request to generate images.

type CreateImageVariationRequest added in v0.12.0

type CreateImageVariationRequest struct {
	Model          string `json:"model,omitempty"`
	Image          string `json:"image"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

CreateImageVariationRequest represents a request to create image variations.

type CronActionInfo

type CronActionInfo struct {
	Type           string            `json:"type"`
	SessionID      string            `json:"session_id,omitempty"`
	Message        string            `json:"message,omitempty"`
	WebhookURL     string            `json:"webhook_url,omitempty"`
	WebhookMethod  string            `json:"webhook_method,omitempty"`
	WebhookHeaders map[string]string `json:"webhook_headers,omitempty"`
	WebhookBody    string            `json:"webhook_body,omitempty"`
	ToolName       string            `json:"tool_name,omitempty"`
	ToolParams     map[string]any    `json:"tool_params,omitempty"`
}

CronActionInfo represents job action information.

type CronJobInfo

type CronJobInfo struct {
	ID          string           `json:"id"`
	Name        string           `json:"name"`
	Description string           `json:"description,omitempty"`
	Schedule    CronScheduleInfo `json:"schedule"`
	Action      CronActionInfo   `json:"action"`
	Status      string           `json:"status"`
	CreatedAt   time.Time        `json:"created_at"`
	UpdatedAt   time.Time        `json:"updated_at"`
	LastRunAt   *time.Time       `json:"last_run_at,omitempty"`
	NextRunAt   *time.Time       `json:"next_run_at,omitempty"`
	RunCount    int64            `json:"run_count"`
	LastError   string           `json:"last_error,omitempty"`
}

CronJobInfo represents information about a scheduled job.

type CronJobResult

type CronJobResult struct {
	Success   bool   `json:"success"`
	Output    any    `json:"output,omitempty"`
	Error     string `json:"error,omitempty"`
	Duration  string `json:"duration"`
	StartedAt string `json:"started_at"`
}

CronJobResult represents the result of triggering a job.

type CronScheduleInfo

type CronScheduleInfo struct {
	Cron     string `json:"cron,omitempty"`
	Once     string `json:"once,omitempty"` // RFC3339 timestamp
	Interval string `json:"interval,omitempty"`
}

CronScheduleInfo represents job schedule information.

type Error

type Error struct {
	Message string  `json:"message"`
	Type    string  `json:"type"`
	Param   *string `json:"param,omitempty"`
	Code    *string `json:"code,omitempty"`
}

Error represents an API error.

type ErrorResponse

type ErrorResponse struct {
	Error Error `json:"error"`
}

ErrorResponse wraps an error in the standard format.

type Function

type Function struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description,omitempty"`
	Parameters  map[string]interface{} `json:"parameters,omitempty"`
	Strict      bool                   `json:"strict,omitempty"`
}

Function represents a function definition.

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

FunctionCall represents a function call within a tool call.

type ImageError added in v0.12.0

type ImageError struct {
	StatusCode int
	Code       string
	Message    string
}

ImageError represents an OpenAI-compatible image API error.

func (*ImageError) Error added in v0.12.0

func (e *ImageError) Error() string

type ImageHandler added in v0.12.0

type ImageHandler interface {
	// CreateImage generates images from a text prompt.
	CreateImage(ctx context.Context, req *CreateImageRequest) (*ImageResponse, error)

	// CreateImageEdit modifies an existing image based on a prompt.
	CreateImageEdit(ctx context.Context, req *CreateImageEditRequest) (*ImageResponse, error)

	// CreateImageVariation creates variations of an existing image.
	CreateImageVariation(ctx context.Context, req *CreateImageVariationRequest) (*ImageResponse, error)
}

ImageHandler defines the interface for image generation operations.

type ImageObject added in v0.12.0

type ImageObject struct {
	URL           string `json:"url,omitempty"`
	B64JSON       string `json:"b64_json,omitempty"`
	RevisedPrompt string `json:"revised_prompt,omitempty"`
}

ImageObject represents a generated image.

type ImageResponse added in v0.12.0

type ImageResponse struct {
	Created int64         `json:"created"`
	Data    []ImageObject `json:"data"`
}

ImageResponse represents the response from image generation.

type MemoryCollection

type MemoryCollection struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Count       int    `json:"count"`
}

MemoryCollection represents a memory collection.

type MemoryHandler

type MemoryHandler interface {
	// ListMemories returns memories from a collection.
	ListMemories(ctx context.Context, collection string, limit int) ([]MemoryRecord, error)

	// SearchMemories performs semantic search on memories.
	SearchMemories(ctx context.Context, collection, query string, limit int) ([]MemorySearchResult, error)

	// StoreMemory stores a new memory.
	StoreMemory(ctx context.Context, req *StoreMemoryRequest) (*MemoryRecord, error)

	// DeleteMemory deletes a memory by key.
	DeleteMemory(ctx context.Context, collection, key string) error

	// ListCollections returns all memory collections.
	ListCollections(ctx context.Context) ([]MemoryCollection, error)
}

MemoryHandler is an optional interface for semantic memory operations. Adapters that support memory can implement this interface.

type MemoryRecord

type MemoryRecord struct {
	Key        string            `json:"key"`
	Content    string            `json:"content"`
	Collection string            `json:"collection"`
	Metadata   map[string]string `json:"metadata,omitempty"`
	CreatedAt  time.Time         `json:"created_at"`
}

MemoryRecord represents a stored memory.

type MemorySearchResult

type MemorySearchResult struct {
	MemoryRecord
	Score float64 `json:"score"`
}

MemorySearchResult represents a memory search result with score.

type Message

type Message struct {
	Role       string     `json:"role"`
	Content    string     `json:"content"`
	Name       string     `json:"name,omitempty"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	ToolCallID string     `json:"tool_call_id,omitempty"`
}

Message represents a chat message.

type Model

type Model struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
}

Model represents an available model.

type ModelPricing

type ModelPricing struct {
	PromptPer1K     float64
	CompletionPer1K float64
}

ModelPricing defines token costs per model.

type ModelUsage

type ModelUsage struct {
	Model            string  `json:"model"`
	Requests         int64   `json:"requests"`
	PromptTokens     int64   `json:"prompt_tokens"`
	CompletionTokens int64   `json:"completion_tokens"`
	TotalTokens      int64   `json:"total_tokens"`
	Cost             float64 `json:"cost"`
}

ModelUsage tracks usage per model.

type OmniImageHandler added in v0.12.0

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

OmniImageHandler implements ImageHandler using the OmniImage library.

func NewOmniImageHandler added in v0.12.0

func NewOmniImageHandler(cfg config.ImageConfig, logger *slog.Logger) (*OmniImageHandler, error)

NewOmniImageHandler creates a new ImageHandler using OmniImage.

func (*OmniImageHandler) Close added in v0.12.0

func (h *OmniImageHandler) Close() error

Close closes the underlying OmniImage client.

func (*OmniImageHandler) CreateImage added in v0.12.0

func (h *OmniImageHandler) CreateImage(ctx context.Context, req *CreateImageRequest) (*ImageResponse, error)

CreateImage generates images from a text prompt.

func (*OmniImageHandler) CreateImageEdit added in v0.12.0

func (h *OmniImageHandler) CreateImageEdit(ctx context.Context, req *CreateImageEditRequest) (*ImageResponse, error)

CreateImageEdit modifies an existing image based on a prompt.

func (*OmniImageHandler) CreateImageVariation added in v0.12.0

func (h *OmniImageHandler) CreateImageVariation(ctx context.Context, req *CreateImageVariationRequest) (*ImageResponse, error)

CreateImageVariation creates variations of an existing image.

type Option

type Option func(*Config)

Option configures the server.

func WithAAuth

func WithAAuth(cfg *auth.AAuthConfig) Option

WithAAuth sets the AAuth token validation configuration. Deprecated: Use WithAgentAuth for unified ID-JAG and AAuth support.

func WithAPIKeys

func WithAPIKeys(keys ...string) Option

WithAPIKeys sets the valid API keys.

func WithAPIPrefix

func WithAPIPrefix(prefix string) Option

WithAPIPrefix sets the URL prefix for OmniAgent API endpoints.

func WithAgentAuth

func WithAgentAuth(cfg *auth.AgentAuthConfig) Option

WithAgentAuth sets the agent authentication configuration. This enables both ID-JAG (automatic) and AAuth (human consent) token validation with policy-based routing based on action sensitivity.

func WithAuth

func WithAuth(cfg *auth.Config) Option

WithAuth sets the OAuth authentication configuration.

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL sets the public URL of the server for OAuth callbacks.

func WithImageHandler added in v0.12.0

func WithImageHandler(handler ImageHandler) Option

WithImageHandler sets the image generation handler.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the server.

func WithOpenAIPrefix

func WithOpenAIPrefix(prefix string) Option

WithOpenAIPrefix sets the URL prefix for OpenAI-compatible endpoints.

func WithPhoneNumber

func WithPhoneNumber(phone string) Option

WithPhoneNumber sets the phone number to display in the web UI.

func WithWebUI

func WithWebUI(enabled bool) Option

WithWebUI enables the embedded chat web UI.

type SSEWriter

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

SSEWriter wraps http.ResponseWriter for Server-Sent Events.

func NewSSEWriter

func NewSSEWriter(w http.ResponseWriter) (*SSEWriter, error)

NewSSEWriter creates a new SSE writer.

func (*SSEWriter) WriteDone

func (s *SSEWriter) WriteDone() error

WriteDone writes the final [DONE] event.

func (*SSEWriter) WriteError

func (s *SSEWriter) WriteError(err error) error

WriteError writes an error as an SSE event.

func (*SSEWriter) WriteEvent

func (s *SSEWriter) WriteEvent(data any) error

WriteEvent writes an SSE event with JSON data.

type Server

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

Server wraps the ogen-generated server with Chi router and Huma API.

func New

func New(handler AgentHandler, opts ...Option) (*Server, error)

New creates a new OpenAI-compatible API server.

func (*Server) GetMergedSpec

func (s *Server) GetMergedSpec() (any, error)

GetMergedSpec returns the merged OpenAPI specification. This combines the ogen spec (OpenAI-compatible endpoints) with the Huma-generated spec (OmniAgent extension endpoints).

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the http.Handler for embedding in other servers.

func (*Server) HumaAPI

func (s *Server) HumaAPI() huma.API

HumaAPI returns the Huma API for external access to the OpenAPI spec.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(addr string) error

ListenAndServe starts the HTTP server with appropriate timeouts.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

func (*Server) ToolUsageStore added in v0.12.0

func (s *Server) ToolUsageStore() *ToolUsageStore

ToolUsageStore returns the server's tool usage store for external recording.

func (*Server) UsageStore

func (s *Server) UsageStore() *UsageStore

UsageStore returns the server's usage store for external recording.

type StoreMemoryRequest

type StoreMemoryRequest struct {
	Content    string            `json:"content"`
	Key        string            `json:"key,omitempty"`
	Collection string            `json:"collection,omitempty"`
	Metadata   map[string]string `json:"metadata,omitempty"`
}

StoreMemoryRequest represents a request to store a memory.

type StreamingHandler

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

StreamingHandler creates an HTTP handler that supports both streaming and non-streaming requests. This is used to bypass ogen's limitations with SSE.

func NewStreamingHandler

func NewStreamingHandler(agent AgentHandler, ogenHandler http.Handler) *StreamingHandler

NewStreamingHandler creates a new streaming handler.

func (*StreamingHandler) ServeHTTP

func (h *StreamingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

func (*StreamingHandler) SetUsageStore

func (h *StreamingHandler) SetUsageStore(store *UsageStore)

SetUsageStore sets the usage store for tracking.

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

Tool represents a tool that can be called.

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

ToolCall represents a tool/function call.

type ToolInfo

type ToolInfo struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters,omitempty"`
	Category    string                 `json:"category,omitempty"`
}

ToolInfo represents information about an available tool.

type ToolUsageRecord added in v0.12.0

type ToolUsageRecord struct {
	ToolName  string    `json:"tool_name"`
	Timestamp time.Time `json:"timestamp"`
	SessionID string    `json:"session_id,omitempty"`
	Latency   int64     `json:"latency_ms,omitempty"`
	Success   bool      `json:"success"`
}

ToolUsageRecord represents a single tool invocation.

type ToolUsageStats added in v0.12.0

type ToolUsageStats struct {
	ToolName    string    `json:"tool_name"`
	CallCount   int64     `json:"call_count"`
	LastUsed    time.Time `json:"last_used"`
	AvgLatency  float64   `json:"avg_latency_ms,omitempty"`
	SuccessRate float64   `json:"success_rate"`
}

ToolUsageStats represents aggregated statistics for a tool.

type ToolUsageStore added in v0.12.0

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

ToolUsageStore stores tool usage records in memory.

func NewToolUsageStore added in v0.12.0

func NewToolUsageStore(maxSize int) *ToolUsageStore

NewToolUsageStore creates a new tool usage store.

func (*ToolUsageStore) GetSummary added in v0.12.0

func (s *ToolUsageStore) GetSummary(since, until time.Time) *ToolUsageSummary

GetSummary returns aggregated tool usage statistics.

func (*ToolUsageStore) GetToolStats added in v0.12.0

func (s *ToolUsageStore) GetToolStats(toolName string, since, until time.Time) *ToolUsageStats

GetToolStats returns statistics for a specific tool.

func (*ToolUsageStore) Record added in v0.12.0

func (s *ToolUsageStore) Record(r ToolUsageRecord)

Record adds a tool usage record.

type ToolUsageSummary added in v0.12.0

type ToolUsageSummary struct {
	TotalCalls int64                      `json:"total_calls"`
	ByTool     map[string]*ToolUsageStats `json:"by_tool"`
	TopTools   []*ToolUsageStats          `json:"top_tools"`
}

ToolUsageSummary provides overall tool usage statistics.

type UpdateAgentRequest

type UpdateAgentRequest struct {
	Name         *string  `json:"name,omitempty"`
	Description  *string  `json:"description,omitempty"`
	Provider     *string  `json:"provider,omitempty"`
	Model        *string  `json:"model,omitempty"`
	APIKey       *string  `json:"api_key,omitempty"` //nolint:gosec // G101: API key in request
	BaseURL      *string  `json:"base_url,omitempty"`
	Temperature  *float64 `json:"temperature,omitempty"`
	MaxTokens    *int     `json:"max_tokens,omitempty"`
	SystemPrompt *string  `json:"system_prompt,omitempty"`
	AllowedTools []string `json:"allowed_tools,omitempty"`
	DeniedTools  []string `json:"denied_tools,omitempty"`
	Enabled      *bool    `json:"enabled,omitempty"`
}

UpdateAgentRequest represents a request to update an agent.

type UpdateCronJobRequest

type UpdateCronJobRequest struct {
	Name        *string           `json:"name,omitempty"`
	Description *string           `json:"description,omitempty"`
	Schedule    *CronScheduleInfo `json:"schedule,omitempty"`
	Action      *CronActionInfo   `json:"action,omitempty"`
}

UpdateCronJobRequest represents a request to update a scheduled job.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage represents token usage statistics.

type UsageBucket

type UsageBucket struct {
	Timestamp        time.Time `json:"timestamp"`
	Requests         int64     `json:"requests"`
	PromptTokens     int64     `json:"prompt_tokens"`
	CompletionTokens int64     `json:"completion_tokens"`
	TotalTokens      int64     `json:"total_tokens"`
	Cost             float64   `json:"cost"`
}

UsageBucket represents usage for a single time bucket.

type UsageHandler

type UsageHandler interface {
	// GetUsageSummary returns aggregated usage statistics.
	GetUsageSummary(ctx context.Context, since, until time.Time) (*UsageSummary, error)

	// GetUsageTimeseries returns time-bucketed usage data.
	GetUsageTimeseries(ctx context.Context, since, until time.Time, interval string) (*UsageTimeseries, error)

	// GetUsageRecords returns recent usage records.
	GetUsageRecords(ctx context.Context, limit int) ([]UsageRecord, error)

	// RecordUsage records a usage event.
	RecordUsage(record UsageRecord)
}

UsageHandler is an optional interface for usage tracking. Adapters that support usage tracking can implement this interface.

type UsageRecord

type UsageRecord struct {
	ID               string    `json:"id"`
	Timestamp        time.Time `json:"timestamp"`
	Model            string    `json:"model"`
	AgentID          string    `json:"agent_id,omitempty"`
	SessionID        string    `json:"session_id,omitempty"`
	PromptTokens     int       `json:"prompt_tokens"`
	CompletionTokens int       `json:"completion_tokens"`
	TotalTokens      int       `json:"total_tokens"`
	Cost             float64   `json:"cost"`
	Latency          int64     `json:"latency_ms"`
}

UsageRecord represents a single usage event.

type UsageStore

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

UsageStore stores usage records in memory.

func NewUsageStore

func NewUsageStore(maxSize int) *UsageStore

NewUsageStore creates a new usage store.

func (*UsageStore) Clear

func (s *UsageStore) Clear()

Clear removes all records.

func (*UsageStore) Count

func (s *UsageStore) Count() int

Count returns the number of records.

func (*UsageStore) GetRecords

func (s *UsageStore) GetRecords(limit int) []UsageRecord

GetRecords returns recent usage records.

func (*UsageStore) GetSummary

func (s *UsageStore) GetSummary(since, until time.Time) *UsageSummary

GetSummary returns aggregated usage statistics.

func (*UsageStore) GetTimeseries

func (s *UsageStore) GetTimeseries(since, until time.Time, interval string) *UsageTimeseries

GetTimeseries returns time-bucketed usage data.

func (*UsageStore) Record

func (s *UsageStore) Record(r UsageRecord)

Record adds a usage record.

type UsageSummary

type UsageSummary struct {
	TotalRequests     int64                  `json:"total_requests"`
	TotalPromptTokens int64                  `json:"total_prompt_tokens"`
	TotalCompTokens   int64                  `json:"total_completion_tokens"`
	TotalTokens       int64                  `json:"total_tokens"`
	TotalCost         float64                `json:"total_cost"`
	AvgLatency        float64                `json:"avg_latency_ms"`
	ByModel           map[string]*ModelUsage `json:"by_model"`
	ByAgent           map[string]*AgentUsage `json:"by_agent"`
	PeriodStart       time.Time              `json:"period_start"`
	PeriodEnd         time.Time              `json:"period_end"`
}

UsageSummary provides aggregated usage statistics.

type UsageTimeseries

type UsageTimeseries struct {
	Interval string        `json:"interval"` // "hour", "day"
	Buckets  []UsageBucket `json:"buckets"`
}

UsageTimeseries represents time-bucketed usage data.

Directories

Path Synopsis
Package auth provides OAuth 2.0 authentication for the OmniAgent web UI.
Package auth provides OAuth 2.0 authentication for the OmniAgent web UI.
internal
ogen
Code generated by ogen, DO NOT EDIT.
Code generated by ogen, DO NOT EDIT.
Package operations provides Huma-compatible request/response types and operation registrations.
Package operations provides Huma-compatible request/response types and operation registrations.
Package web provides embedded web assets for the OpenAI-compatible API server.
Package web provides embedded web assets for the OpenAI-compatible API server.

Jump to

Keyboard shortcuts

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