logstore

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: Apache-2.0 Imports: 9 Imported by: 4

Documentation

Overview

Package logstore provides a logs store for Bifrost.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = fmt.Errorf("log not found")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled bool         `json:"enabled"`
	Type    LogStoreType `json:"type"`
	Config  any          `json:"config"`
}

Config represents the configuration for the logs store.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON is the custom unmarshal logic for Config

type Log

type Log struct {
	ID                  string    `gorm:"primaryKey;type:varchar(255)" json:"id"`
	Timestamp           time.Time `gorm:"index;not null" json:"timestamp"`
	Object              string    `gorm:"type:varchar(255);index;not null;column:object_type" json:"object"` // text.completion, chat.completion, or embedding
	Provider            string    `gorm:"type:varchar(255);index;not null" json:"provider"`
	Model               string    `gorm:"type:varchar(255);index;not null" json:"model"`
	InputHistory        string    `gorm:"type:text" json:"-"` // JSON serialized []schemas.BifrostMessage
	OutputMessage       string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostMessage
	EmbeddingOutput     string    `gorm:"type:text" json:"-"` // JSON serialized *[][]float32
	Params              string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.ModelParameters
	Tools               string    `gorm:"type:text" json:"-"` // JSON serialized *[]schemas.Tool
	ToolCalls           string    `gorm:"type:text" json:"-"` // JSON serialized *[]schemas.ToolCall
	SpeechInput         string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.SpeechInput
	TranscriptionInput  string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.TranscriptionInput
	SpeechOutput        string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostSpeech
	TranscriptionOutput string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostTranscribe
	Latency             *float64  `json:"latency,omitempty"`
	TokenUsage          string    `gorm:"type:text" json:"-"`                            // JSON serialized *schemas.LLMUsage
	Status              string    `gorm:"type:varchar(50);index;not null" json:"status"` // "processing", "success", or "error"
	ErrorDetails        string    `gorm:"type:text" json:"-"`                            // JSON serialized *schemas.BifrostError
	Stream              bool      `gorm:"default:false" json:"stream"`                   // true if this was a streaming response
	ContentSummary      string    `gorm:"type:text" json:"-"`                            // For content search

	// Denormalized token fields for easier querying
	PromptTokens     int `gorm:"default:0" json:"-"`
	CompletionTokens int `gorm:"default:0" json:"-"`
	TotalTokens      int `gorm:"default:0" json:"-"`

	CreatedAt time.Time `gorm:"index;not null" json:"created_at"`

	// Virtual fields for JSON output - these will be populated when needed
	InputHistoryParsed        []schemas.BifrostMessage    `gorm:"-" json:"input_history,omitempty"`
	OutputMessageParsed       *schemas.BifrostMessage     `gorm:"-" json:"output_message,omitempty"`
	EmbeddingOutputParsed     *[]schemas.BifrostEmbedding `gorm:"-" json:"embedding_output,omitempty"`
	ParamsParsed              *schemas.ModelParameters    `gorm:"-" json:"params,omitempty"`
	ToolsParsed               *[]schemas.Tool             `gorm:"-" json:"tools,omitempty"`
	ToolCallsParsed           *[]schemas.ToolCall         `gorm:"-" json:"tool_calls,omitempty"`
	TokenUsageParsed          *schemas.LLMUsage           `gorm:"-" json:"token_usage,omitempty"`
	ErrorDetailsParsed        *schemas.BifrostError       `gorm:"-" json:"error_details,omitempty"`
	SpeechInputParsed         *schemas.SpeechInput        `gorm:"-" json:"speech_input,omitempty"`
	TranscriptionInputParsed  *schemas.TranscriptionInput `gorm:"-" json:"transcription_input,omitempty"`
	SpeechOutputParsed        *schemas.BifrostSpeech      `gorm:"-" json:"speech_output,omitempty"`
	TranscriptionOutputParsed *schemas.BifrostTranscribe  `gorm:"-" json:"transcription_output,omitempty"`
}

Log represents a complete log entry for a request/response cycle This is the GORM model with appropriate tags

func (*Log) AfterFind

func (l *Log) AfterFind(tx *gorm.DB) error

AfterFind GORM hook to deserialize JSON fields

func (*Log) BeforeCreate

func (l *Log) BeforeCreate(tx *gorm.DB) error

BeforeCreate GORM hook to set created_at and serialize JSON fields

func (*Log) BeforeSave

func (l *Log) BeforeSave(tx *gorm.DB) error

BeforeSave GORM hook to serialize JSON fields

func (*Log) BuildContentSummary

func (l *Log) BuildContentSummary() string

BuildContentSummary creates a searchable text summary

func (*Log) DeserializeFields

func (l *Log) DeserializeFields() error

DeserializeFields converts JSON strings back to Go structs

func (*Log) SerializeFields

func (l *Log) SerializeFields() error

SerializeFields converts Go structs to JSON strings for storage

func (Log) TableName

func (Log) TableName() string

TableName sets the table name for GORM

type LogStore

type LogStore interface {
	Create(entry *Log) error
	FindFirst(query any, fields ...string) (*Log, error)
	FindAll(query any, fields ...string) ([]*Log, error)
	SearchLogs(filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)
	Update(id string, entry any) error
	CleanupLogs(since time.Time) error
}

LogStore is the interface for the log store.

func NewLogStore

func NewLogStore(config *Config, logger schemas.Logger) (LogStore, error)

NewLogStore creates a new log store based on the configuration.

type LogStoreType

type LogStoreType string

LogStoreType represents the type of log store.

const (
	LogStoreTypeSQLite LogStoreType = "sqlite"
)

LogStoreTypeSQLite is the type of log store for SQLite.

type PaginationOptions

type PaginationOptions struct {
	Limit  int    `json:"limit"`
	Offset int    `json:"offset"`
	SortBy string `json:"sort_by"` // "timestamp", "latency", "tokens"
	Order  string `json:"order"`   // "asc", "desc"
}

PaginationOptions represents pagination parameters

type SQLiteConfig

type SQLiteConfig struct {
	Path string `json:"path"`
}

SQLiteConfig represents the configuration for a SQLite database.

type SQLiteLogStore

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

SQLiteLogStore represents a logs store that uses a SQLite database.

func (*SQLiteLogStore) CleanupLogs

func (s *SQLiteLogStore) CleanupLogs(since time.Time) error

CleanupLogs deletes old log entries from the database.

func (*SQLiteLogStore) Create

func (s *SQLiteLogStore) Create(entry *Log) error

Create inserts a new log entry into the database.

func (*SQLiteLogStore) FindAll

func (s *SQLiteLogStore) FindAll(query any, fields ...string) ([]*Log, error)

FindAll finds all log entries from the database.

func (*SQLiteLogStore) FindFirst

func (s *SQLiteLogStore) FindFirst(query any, fields ...string) (*Log, error)

FindFirst gets a log entry from the database.

func (*SQLiteLogStore) SearchLogs

func (s *SQLiteLogStore) SearchLogs(filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)

SearchLogs searches for logs in the database.

func (*SQLiteLogStore) Update

func (s *SQLiteLogStore) Update(id string, entry any) error

Update updates a log entry in the database.

type SearchFilters

type SearchFilters struct {
	Providers     []string   `json:"providers,omitempty"`
	Models        []string   `json:"models,omitempty"`
	Status        []string   `json:"status,omitempty"`
	Objects       []string   `json:"objects,omitempty"` // For filtering by request type (chat.completion, text.completion, embedding)
	StartTime     *time.Time `json:"start_time,omitempty"`
	EndTime       *time.Time `json:"end_time,omitempty"`
	MinLatency    *float64   `json:"min_latency,omitempty"`
	MaxLatency    *float64   `json:"max_latency,omitempty"`
	MinTokens     *int       `json:"min_tokens,omitempty"`
	MaxTokens     *int       `json:"max_tokens,omitempty"`
	ContentSearch string     `json:"content_search,omitempty"`
}

SearchFilters represents the available filters for log searches

type SearchResult

type SearchResult struct {
	Logs       []Log             `json:"logs"`
	Pagination PaginationOptions `json:"pagination"`
	Stats      SearchStats       `json:"stats"`
}

SearchResult represents the result of a log search

type SearchStats

type SearchStats struct {
	TotalRequests  int64   `json:"total_requests"`
	SuccessRate    float64 `json:"success_rate"`    // Percentage of successful requests
	AverageLatency float64 `json:"average_latency"` // Average latency in milliseconds
	TotalTokens    int64   `json:"total_tokens"`    // Total tokens used
}

type SortBy

type SortBy string
const (
	SortByTimestamp SortBy = "timestamp"
	SortByLatency   SortBy = "latency"
	SortByTokens    SortBy = "tokens"
)

type SortOrder

type SortOrder string
const (
	SortAsc  SortOrder = "asc"
	SortDesc SortOrder = "desc"
)

Jump to

Keyboard shortcuts

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