xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / provider / provider.go

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