62159
Sy
symb / files
symb / internal / provider / provider.go
6.2 KB
1// Package provider defines the LLM provider interface and implementations. 2package provider 3 4import ( 5 "context" 6 "encoding/json" 7 "errors" 8 "time" 9 10 "github.com/rs/zerolog/log" 11) 12 13// ErrProviderNotFound is returned when a requested provider doesn't exist. 14var ErrProviderNotFound = errors.New("provider not found") 15 16// Message represents a chat message. 17type Message struct { 18 Role string 19 Content string 20 Reasoning string // Model reasoning/thinking content (optional) 21 ToolCalls []ToolCall // For assistant messages with tool calls 22 ToolCallID string // For tool result messages 23 FunctionName string // For tool result messages: name of the called function (required by Gemini) 24 CreatedAt time.Time // Message timestamp 25 InputTokens int // Token usage for this LLM call (assistant messages only) 26 OutputTokens int // Token usage for this LLM call (assistant messages only) 27} 28 29// Tool represents a tool/function definition for the LLM. 30type Tool struct { 31 Name string `json:"name"` 32 Description string `json:"description"` 33 Parameters json.RawMessage `json:"parameters"` // JSON Schema 34} 35 36// ToolCall represents a tool call made by the LLM. 37type ToolCall struct { 38 ID string `json:"id"` 39 Name string `json:"name"` 40 Arguments json.RawMessage `json:"arguments"` 41 ThoughtSignature string `json:"thought_signature,omitempty"` 42} 43 44// ChatResponse represents the response from a chat completion. 45type ChatResponse struct { 46 Content string // Text content (may be empty if tool calls) 47 ToolCalls []ToolCall // Tool calls (may be empty if text response) 48 Reasoning string // Model reasoning content (optional) 49 InputTokens int // Input/prompt token count (0 if unavailable) 50 OutputTokens int // Output/completion token count (0 if unavailable) 51} 52 53// StreamEventType identifies the kind of streaming event. 54type StreamEventType int 55 56const ( 57 // EventContentDelta carries a chunk of text content. 58 EventContentDelta StreamEventType = iota 59 // EventReasoningDelta carries a chunk of reasoning/thinking content. 60 EventReasoningDelta 61 // EventToolCallBegin signals the start of a new tool call with ID and name. 62 EventToolCallBegin 63 // EventToolCallDelta carries a chunk of tool call arguments. 64 EventToolCallDelta 65 // EventUsage carries token usage statistics. 66 EventUsage 67 // EventDone signals the stream is complete. 68 EventDone 69 // EventError signals a stream error. 70 EventError 71) 72 73// StreamEvent represents a single event in a streamed LLM response. 74type StreamEvent struct { 75 Type StreamEventType 76 77 // Content or reasoning text delta (for EventContentDelta, EventReasoningDelta). 78 Content string 79 80 // Tool call fields (for EventToolCallBegin, EventToolCallDelta). 81 ToolCallIndex int // Index of the tool call in the response (from OpenAI spec) 82 ToolCallID string // Set on EventToolCallBegin 83 ToolCallName string // Set on EventToolCallBegin 84 ToolCallSignature string // Optional thought signature for Gemini tool calls 85 ToolCallArgs string // Argument fragment on EventToolCallDelta 86 87 // Token usage (for EventUsage). 88 InputTokens int 89 OutputTokens int 90 91 // Error (for EventError). 92 Err error 93} 94 95type Model struct { 96 Name string 97 Size int64 98 Digest string 99 ModifiedAt time.Time 100 Format string 101 Family string 102 ParamSize string 103 QuantLevel string 104} 105 106// Provider defines the interface for LLM providers. 107type Provider interface { 108 // Name returns the provider's identifier. 109 Name() string 110 111 // ChatStream sends messages with optional tools and returns a channel of streaming events. 112 // The channel is closed after EventDone or EventError is sent. 113 // Pass nil tools for simple chat without tool calling. 114 ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) 115 116 // ListModels returns available models from the provider. 117 ListModels(ctx context.Context) ([]Model, error) 118 119 // Close closes idle HTTP connections and cleans up resources. 120 Close() error 121} 122 123type Factory interface { 124 Name() string 125 Create(model string, opts Options) Provider 126} 127 128// Registry holds available providers. 129type Registry struct { 130 factories map[string]Factory 131} 132 133// NewRegistry creates a new provider registry. 134func NewRegistry() *Registry { 135 return &Registry{ 136 factories: make(map[string]Factory), 137 } 138} 139 140func (r *Registry) RegisterFactory(name string, f Factory) { 141 r.factories[name] = f 142} 143 144func (r *Registry) Create(name, model string, opts Options) (Provider, error) { 145 f, ok := r.factories[name] 146 if !ok { 147 log.Error().Str("name", name).Str("model", model).Msg("Registry.Create: factory not found") 148 return nil, ErrProviderNotFound 149 } 150 log.Info().Str("name", name).Str("model", model).Str("factory_type", "unknown").Msg("Registry.Create: calling factory") 151 return f.Create(model, opts), nil 152} 153 154// Options holds provider generation settings. 155type Options struct { 156 Temperature float64 157} 158 159// List returns all registered provider names. 160func (r *Registry) List() []string { 161 names := make([]string, 0, len(r.factories)) 162 for name := range r.factories { 163 names = append(names, name) 164 } 165 return names 166} 167 168// TaggedModel pairs a provider config name with a model. 169type TaggedModel struct { 170 ProviderName string 171 Model Model 172} 173 174// ListAllModels concurrently fetches models from every registered provider and 175// returns the combined list. Errors from individual providers are logged and 176// skipped so a single unavailable provider does not block the rest. 177func (r *Registry) ListAllModels(ctx context.Context, opts Options) []TaggedModel { 178 type result struct { 179 name string 180 models []Model 181 } 182 ch := make(chan result, len(r.factories)) 183 for name := range r.factories { 184 name := name 185 go func() { 186 prov := r.factories[name].Create("", opts) 187 models, err := prov.ListModels(ctx) 188 prov.Close() 189 if err != nil { 190 log.Warn().Str("provider", name).Err(err).Msg("ListAllModels: provider error") 191 ch <- result{name: name} 192 return 193 } 194 ch <- result{name: name, models: models} 195 }() 196 } 197 var all []TaggedModel 198 for range r.factories { 199 res := <-ch 200 for _, m := range res.models { 201 all = append(all, TaggedModel{ProviderName: res.name, Model: m}) 202 } 203 } 204 return all 205}