6119
My
mysis / files
mysis / internal / provider / provider.go
3.0 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 11// ErrProviderNotFound is returned when a requested provider doesn't exist. 12var ErrProviderNotFound = errors.New("provider not found") 13 14// Message represents a chat message. 15type Message struct { 16 Role string 17 Content string 18 Reasoning string // Model reasoning/thinking content (optional) 19 ToolCalls []ToolCall // For assistant messages with tool calls 20 ToolCallID string // For tool result messages 21 CreatedAt time.Time // Message timestamp 22} 23 24// Tool represents a tool/function definition for the LLM. 25type Tool struct { 26 Name string `json:"name"` 27 Description string `json:"description"` 28 Parameters json.RawMessage `json:"parameters"` // JSON Schema 29} 30 31// ToolCall represents a tool call made by the LLM. 32type ToolCall struct { 33 ID string `json:"id"` 34 Name string `json:"name"` 35 Arguments json.RawMessage `json:"arguments"` 36} 37 38// ChatResponse represents the response from a chat completion. 39type ChatResponse struct { 40 Content string // Text content (may be empty if tool calls) 41 ToolCalls []ToolCall // Tool calls (may be empty if text response) 42 Reasoning string // Model reasoning content (optional) 43} 44 45// Provider defines the interface for LLM providers. 46type Provider interface { 47 // Name returns the provider's identifier. 48 Name() string 49 50 // Chat sends messages and returns the complete response. 51 Chat(ctx context.Context, messages []Message) (string, error) 52 53 // ChatWithTools sends messages with available tools and returns response with potential tool calls. 54 ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) 55 56 // Stream sends messages and returns a channel that streams response chunks. 57 Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) 58 59 // Close closes idle HTTP connections and cleans up resources. 60 Close() error 61} 62 63type ProviderFactory interface { 64 Name() string 65 Create(model string, temperature float64) Provider 66} 67 68// StreamChunk represents a chunk of streamed response. 69type StreamChunk struct { 70 Content string 71 Done bool 72 Err error 73} 74 75// Registry holds available providers. 76type Registry struct { 77 factories map[string]ProviderFactory 78} 79 80// NewRegistry creates a new provider registry. 81func NewRegistry() *Registry { 82 return &Registry{ 83 factories: make(map[string]ProviderFactory), 84 } 85} 86 87func (r *Registry) RegisterFactory(name string, f ProviderFactory) { 88 r.factories[name] = f 89} 90 91func (r *Registry) Create(name, model string, temperature float64) (Provider, error) { 92 f, ok := r.factories[name] 93 if !ok { 94 return nil, ErrProviderNotFound 95 } 96 return f.Create(model, temperature), nil 97} 98 99// List returns all registered provider names. 100func (r *Registry) List() []string { 101 names := make([]string, 0, len(r.factories)) 102 for name := range r.factories { 103 names = append(names, name) 104 } 105 return names 106}