xonecas.io
60255
Zn

Zoea-Nova / commit bff3c31

feat(provider): add rate limiting with shared limiters per provider

Implement token bucket rate limiting using golang.org/x/time/rate to prevent
provider overload and external API rate limiting. Each provider factory owns
a shared limiter that all myses using that provider respect.

Changes:
- Add RateLimit and RateBurst fields to ProviderConfig
- Create provider factory pattern with shared rate.Limiter per factory
- Inject limiter into provider instances via factory Create method
- Wrap all Chat/ChatWithTools/Stream calls with limiter.Wait(ctx)
- Add EventRateLimit type for future telemetry
- Add environment variable overrides for rate limit configuration
- Update tests to verify rate limiting behavior

Default limits:
- Ollama: 2.0 req/s, burst 3
- OpenCode Zen: 10.0 req/s, burst 5

Fixes issue where multiple myses could overwhelm local Ollama or trigger
remote API rate limits.

xonecas2026-02-04 bff3c3137af9261a846ba90dd963036eb878bd6c parent 5ee0614

15 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

cmd/zoea/main.go+27−4
@@ -191,16 +191,16 @@ func initProviders(cfg *config.Config, creds *config.Credentials) *provider.Regi
191191
192192 // Register Ollama provider
193193 if ollCfg, ok := cfg.Providers["ollama"]; ok {
194- p := provider.NewOllama(ollCfg.Endpoint, ollCfg.Model)
195- registry.Register(p)
194+ factory := provider.NewOllamaFactory(ollCfg.Endpoint, ollCfg.RateLimit, ollCfg.RateBurst)
195+ registry.RegisterFactory(factory)
196196 }
197197
198198 // Register OpenCode Zen provider
199199 if zenCfg, ok := cfg.Providers["opencode_zen"]; ok {
200200 apiKey := creds.GetAPIKey("opencode_zen")
201201 if apiKey != "" {
202- p := provider.NewOpenCode(zenCfg.Endpoint, zenCfg.Model, apiKey)
203- registry.Register(p)
202+ factory := provider.NewOpenCodeFactory(zenCfg.Endpoint, apiKey, zenCfg.RateLimit, zenCfg.RateBurst)
203+ registry.RegisterFactory(factory)
204204 }
205205 }
206206
@@ -275,6 +275,25 @@ func (a *commanderAdapter) SearchMessages(mysisID, query string, limit int) ([]m
275275 return results, nil
276276 }
277277
278+func (a *commanderAdapter) SearchReasoning(mysisID, query string, limit int) ([]mcp.ReasoningResult, error) {
279+ memories, err := a.commander.Store().SearchReasoning(mysisID, query, limit)
280+ if err != nil {
281+ return nil, err
282+ }
283+
284+ results := make([]mcp.ReasoningResult, len(memories))
285+ for i, m := range memories {
286+ results[i] = mcp.ReasoningResult{
287+ Role: string(m.Role),
288+ Source: string(m.Source),
289+ Content: m.Content,
290+ Reasoning: m.Reasoning,
291+ CreatedAt: m.CreatedAt.Format("2006-01-02 15:04:05"),
292+ }
293+ }
294+ return results, nil
295+}
296+
278297 func (a *commanderAdapter) SearchBroadcasts(query string, limit int) ([]mcp.BroadcastResult, error) {
279298 broadcasts, err := a.commander.Store().SearchBroadcasts(query, limit)
280299 if err != nil {
@@ -430,6 +449,10 @@ func (m *mockOrchestrator) SearchMessages(mysisID, query string, limit int) ([]m
430449 return []mcp.SearchResult{}, nil
431450 }
432451
452+func (m *mockOrchestrator) SearchReasoning(mysisID, query string, limit int) ([]mcp.ReasoningResult, error) {
453+ return []mcp.ReasoningResult{}, nil
454+}
455+
433456 func (m *mockOrchestrator) SearchBroadcasts(query string, limit int) ([]mcp.BroadcastResult, error) {
434457 return []mcp.BroadcastResult{}, nil
435458 }
config.toml+7−1
@@ -5,10 +5,16 @@ max_myses = 16
55 [providers.ollama]
66 endpoint = "http://localhost:11434"
77 model = "qwen3:4b"
8+temperature = 0.7
9+rate_limit = 2.0
10+rate_burst = 3
811
912 [providers.opencode_zen]
1013 endpoint = "https://api.opencode.ai/v1"
11-model = "zen-default"
14+model = "glm-4.7-free"
15+temperature = 0.7
16+rate_limit = 10.0
17+rate_burst = 5
1218
1319 [mcp]
1420 upstream = "https://game.spacemolt.com/mcp"
go.mod+1−0
@@ -40,6 +40,7 @@ require (
4040 golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
4141 golang.org/x/sys v0.38.0 // indirect
4242 golang.org/x/text v0.3.8 // indirect
43+ golang.org/x/time v0.14.0 // indirect
4344 modernc.org/libc v1.67.6 // indirect
4445 modernc.org/mathutil v1.7.1 // indirect
4546 modernc.org/memory v1.11.0 // indirect
go.sum+2−0
@@ -82,6 +82,8 @@ golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
8282 golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
8383 golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
8484 golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
85+golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
86+golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
8587 golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
8688 golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
8789 modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
internal/config/config.go+71−4
@@ -23,8 +23,11 @@ type SwarmConfig struct {
2323
2424 // ProviderConfig holds LLM provider settings.
2525 type ProviderConfig struct {
26- Endpoint string `toml:"endpoint"`
27- Model string `toml:"model"`
26+ Endpoint string `toml:"endpoint"`
27+ Model string `toml:"model"`
28+ Temperature float64 `toml:"temperature"`
29+ RateLimit float64 `toml:"rate_limit"`
30+ RateBurst int `toml:"rate_burst"`
2831 }
2932
3033 // MCPConfig holds MCP proxy settings.
@@ -40,8 +43,18 @@ func DefaultConfig() *Config {
4043 },
4144 Providers: map[string]ProviderConfig{
4245 "ollama": {
43- Endpoint: "http://localhost:11434",
44- Model: "llama3",
46+ Endpoint: "http://localhost:11434",
47+ Model: "llama3",
48+ Temperature: 0.7,
49+ RateLimit: 2.0,
50+ RateBurst: 3,
51+ },
52+ "opencode_zen": {
53+ Endpoint: "https://api.opencode.ai/v1",
54+ Model: "glm-4.7-free",
55+ Temperature: 0.7,
56+ RateLimit: 10.0,
57+ RateBurst: 5,
4558 },
4659 },
4760 MCP: MCPConfig{
@@ -95,6 +108,33 @@ func applyEnvOverrides(cfg *Config) {
95108 }
96109 }
97110
111+ if v := os.Getenv("ZOEA_OLLAMA_TEMPERATURE"); v != "" {
112+ if f, err := strconv.ParseFloat(v, 64); err == nil {
113+ if p, ok := cfg.Providers["ollama"]; ok {
114+ p.Temperature = f
115+ cfg.Providers["ollama"] = p
116+ }
117+ }
118+ }
119+
120+ if v := os.Getenv("ZOEA_OLLAMA_RATE_LIMIT"); v != "" {
121+ if f, err := strconv.ParseFloat(v, 64); err == nil {
122+ if p, ok := cfg.Providers["ollama"]; ok {
123+ p.RateLimit = f
124+ cfg.Providers["ollama"] = p
125+ }
126+ }
127+ }
128+
129+ if v := os.Getenv("ZOEA_OLLAMA_RATE_BURST"); v != "" {
130+ if n, err := strconv.Atoi(v); err == nil {
131+ if p, ok := cfg.Providers["ollama"]; ok {
132+ p.RateBurst = n
133+ cfg.Providers["ollama"] = p
134+ }
135+ }
136+ }
137+
98138 if v := os.Getenv("ZOEA_OPENCODE_ENDPOINT"); v != "" {
99139 if p, ok := cfg.Providers["opencode_zen"]; ok {
100140 p.Endpoint = v
@@ -108,6 +148,33 @@ func applyEnvOverrides(cfg *Config) {
108148 cfg.Providers["opencode_zen"] = p
109149 }
110150 }
151+
152+ if v := os.Getenv("ZOEA_OPENCODE_TEMPERATURE"); v != "" {
153+ if f, err := strconv.ParseFloat(v, 64); err == nil {
154+ if p, ok := cfg.Providers["opencode_zen"]; ok {
155+ p.Temperature = f
156+ cfg.Providers["opencode_zen"] = p
157+ }
158+ }
159+ }
160+
161+ if v := os.Getenv("ZOEA_OPENCODE_RATE_LIMIT"); v != "" {
162+ if f, err := strconv.ParseFloat(v, 64); err == nil {
163+ if p, ok := cfg.Providers["opencode_zen"]; ok {
164+ p.RateLimit = f
165+ cfg.Providers["opencode_zen"] = p
166+ }
167+ }
168+ }
169+
170+ if v := os.Getenv("ZOEA_OPENCODE_RATE_BURST"); v != "" {
171+ if n, err := strconv.Atoi(v); err == nil {
172+ if p, ok := cfg.Providers["opencode_zen"]; ok {
173+ p.RateBurst = n
174+ cfg.Providers["opencode_zen"] = p
175+ }
176+ }
177+ }
111178 }
112179
113180 // DataDir returns the path to the Zoea data directory (~/.zoea-nova).
internal/config/config_test.go+52−0
@@ -53,6 +53,58 @@ upstream = "https://custom.mcp/endpoint"
5353 }
5454 }
5555
56+func TestLoadWithTemperature(t *testing.T) {
57+ tmpDir := t.TempDir()
58+ configPath := filepath.Join(tmpDir, "config.toml")
59+
60+ content := `
61+[providers.ollama]
62+endpoint = "http://localhost:11434"
63+model = "qwen3:4b"
64+temperature = 0.5
65+`
66+ if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
67+ t.Fatalf("failed to write test config: %v", err)
68+ }
69+
70+ cfg, err := Load(configPath)
71+ if err != nil {
72+ t.Fatalf("Load() error: %v", err)
73+ }
74+
75+ if cfg.Providers["ollama"].Temperature != 0.5 {
76+ t.Errorf("expected temperature=0.5, got %v", cfg.Providers["ollama"].Temperature)
77+ }
78+}
79+
80+func TestLoadWithRateLimit(t *testing.T) {
81+ tmpDir := t.TempDir()
82+ configPath := filepath.Join(tmpDir, "config.toml")
83+
84+ content := `
85+[providers.ollama]
86+endpoint = "http://localhost:11434"
87+model = "qwen3:4b"
88+rate_limit = 3.5
89+rate_burst = 4
90+`
91+ if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
92+ t.Fatalf("failed to write test config: %v", err)
93+ }
94+
95+ cfg, err := Load(configPath)
96+ if err != nil {
97+ t.Fatalf("Load() error: %v", err)
98+ }
99+
100+ if cfg.Providers["ollama"].RateLimit != 3.5 {
101+ t.Errorf("expected rate_limit=3.5, got %v", cfg.Providers["ollama"].RateLimit)
102+ }
103+ if cfg.Providers["ollama"].RateBurst != 4 {
104+ t.Errorf("expected rate_burst=4, got %d", cfg.Providers["ollama"].RateBurst)
105+ }
106+}
107+
56108 func TestLoadWithEnvOverrides(t *testing.T) {
57109 // Set env vars
58110 os.Setenv("ZOEA_MAX_MYSES", "10")
internal/core/commander.go+18−17
@@ -60,7 +60,7 @@ func (c *Commander) LoadMyses() error {
6060 defer c.mu.Unlock()
6161
6262 for _, sm := range stored {
63- p, err := c.registry.Get(sm.Provider)
63+ p, err := c.registry.Create(sm.Provider, sm.Model, sm.Temperature)
6464 if err != nil {
6565 // Provider not available, skip mysis
6666 continue
@@ -85,20 +85,20 @@ func (c *Commander) CreateMysis(name, providerName string) (*Mysis, error) {
8585 return nil, fmt.Errorf("max myses (%d) reached", c.maxMyses)
8686 }
8787
88- // Get provider
89- p, err := c.registry.Get(providerName)
90- if err != nil {
91- return nil, fmt.Errorf("get provider: %w", err)
92- }
93-
9488 // Get provider config for model
9589 provCfg, ok := c.config.Providers[providerName]
9690 if !ok {
9791 return nil, fmt.Errorf("provider config not found: %s", providerName)
9892 }
9993
94+ // Create provider instance for this mysis
95+ p, err := c.registry.Create(providerName, provCfg.Model, provCfg.Temperature)
96+ if err != nil {
97+ return nil, fmt.Errorf("create provider: %w", err)
98+ }
99+
100100 // Create in store
101- stored, err := c.store.CreateMysis(name, providerName, provCfg.Model)
101+ stored, err := c.store.CreateMysis(name, providerName, provCfg.Model, provCfg.Temperature)
102102 if err != nil {
103103 return nil, fmt.Errorf("create mysis in store: %w", err)
104104 }
@@ -197,7 +197,7 @@ func (c *Commander) StopMysis(id string) error {
197197 }
198198
199199 // ConfigureMysis updates a mysis provider and model.
200-func (c *Commander) ConfigureMysis(id, providerName string) error {
200+func (c *Commander) ConfigureMysis(id, providerName, model string) error {
201201 c.mu.Lock()
202202 mysis, ok := c.myses[id]
203203 if !ok {
@@ -206,20 +206,21 @@ func (c *Commander) ConfigureMysis(id, providerName string) error {
206206 }
207207 c.mu.Unlock()
208208
209- // Get new provider
210- p, err := c.registry.Get(providerName)
211- if err != nil {
212- return fmt.Errorf("get provider: %w", err)
213- }
214-
215209 // Get provider config
216210 provCfg, ok := c.config.Providers[providerName]
217211 if !ok {
218212 return fmt.Errorf("provider config not found: %s", providerName)
219213 }
214+ temperature := provCfg.Temperature
215+
216+ // Get new provider
217+ p, err := c.registry.Create(providerName, model, temperature)
218+ if err != nil {
219+ return fmt.Errorf("create provider: %w", err)
220+ }
220221
221222 // Update store
222- if err := c.store.UpdateMysisConfig(id, providerName, provCfg.Model); err != nil {
223+ if err := c.store.UpdateMysisConfig(id, providerName, model, temperature); err != nil {
223224 return fmt.Errorf("update store: %w", err)
224225 }
225226
@@ -233,7 +234,7 @@ func (c *Commander) ConfigureMysis(id, providerName string) error {
233234 MysisName: mysis.Name(),
234235 Data: ConfigChangeData{
235236 Provider: providerName,
236- Model: provCfg.Model,
237+ Model: model,
237238 },
238239 Timestamp: time.Now(),
239240 })
internal/core/commander_test.go+31−13
@@ -9,6 +9,7 @@ import (
99 "github.com/xonecas/zoea-nova/internal/mcp"
1010 "github.com/xonecas/zoea-nova/internal/provider"
1111 "github.com/xonecas/zoea-nova/internal/store"
12+ "golang.org/x/time/rate"
1213 )
1314
1415 func setupCommanderTest(t *testing.T) (*Commander, *EventBus, func()) {
@@ -24,16 +25,17 @@ func setupCommanderTest(t *testing.T) (*Commander, *EventBus, func()) {
2425 bus := NewEventBus(100)
2526
2627 reg := provider.NewRegistry()
27- reg.Register(provider.NewMock("mock", "mock response"))
28- reg.Register(provider.NewMock("ollama", "ollama response"))
28+ limiter := rate.NewLimiter(rate.Limit(1000), 1000)
29+ reg.RegisterFactory(provider.NewMockFactoryWithLimiter("mock", "mock response", limiter))
30+ reg.RegisterFactory(provider.NewMockFactoryWithLimiter("ollama", "ollama response", limiter))
2931
3032 cfg := &config.Config{
3133 Swarm: config.SwarmConfig{
3234 MaxMyses: 16,
3335 },
3436 Providers: map[string]config.ProviderConfig{
35- "mock": {Endpoint: "http://mock", Model: "mock-model"},
36- "ollama": {Endpoint: "http://ollama", Model: "llama3"},
37+ "mock": {Endpoint: "http://mock", Model: "mock-model", Temperature: 0.7},
38+ "ollama": {Endpoint: "http://ollama", Model: "llama3", Temperature: 0.7},
3739 },
3840 }
3941
@@ -137,14 +139,15 @@ func TestCommanderMaxMyses(t *testing.T) {
137139 defer bus.Close()
138140
139141 reg := provider.NewRegistry()
140- reg.Register(provider.NewMock("mock", "response"))
142+ limiter := rate.NewLimiter(rate.Limit(1000), 1000)
143+ reg.RegisterFactory(provider.NewMockFactoryWithLimiter("mock", "response", limiter))
141144
142145 cfg := &config.Config{
143146 Swarm: config.SwarmConfig{
144147 MaxMyses: 2, // Low limit for testing
145148 },
146149 Providers: map[string]config.ProviderConfig{
147- "mock": {Endpoint: "http://mock", Model: "mock-model"},
150+ "mock": {Endpoint: "http://mock", Model: "mock-model", Temperature: 0.7},
148151 },
149152 }
150153
@@ -215,7 +218,7 @@ func TestCommanderConfigureMysis(t *testing.T) {
215218 t.Fatal("timeout waiting for created event")
216219 }
217220
218- if err := cmd.ConfigureMysis(id, "ollama"); err != nil {
221+ if err := cmd.ConfigureMysis(id, "ollama", "llama3"); err != nil {
219222 t.Fatalf("ConfigureMysis() error: %v", err)
220223 }
221224
@@ -229,17 +232,31 @@ func TestCommanderConfigureMysis(t *testing.T) {
229232 if data.Provider != "ollama" {
230233 t.Errorf("expected provider=ollama, got %s", data.Provider)
231234 }
235+ if data.Model != "llama3" {
236+ t.Errorf("expected model=llama3, got %s", data.Model)
237+ }
232238 case <-time.After(100 * time.Millisecond):
233239 t.Fatal("timeout waiting for config changed event")
234240 }
235241
242+ stored, err := cmd.Store().GetMysis(id)
243+ if err != nil {
244+ t.Fatalf("GetMysis() error: %v", err)
245+ }
246+ if stored.Model != "llama3" {
247+ t.Errorf("expected stored model=llama3, got %s", stored.Model)
248+ }
249+ if stored.Temperature != 0.7 {
250+ t.Errorf("expected stored temperature=0.7, got %v", stored.Temperature)
251+ }
252+
236253 // Configure with non-existent provider
237- if err := cmd.ConfigureMysis(id, "nonexistent"); err == nil {
254+ if err := cmd.ConfigureMysis(id, "nonexistent", "model"); err == nil {
238255 t.Error("expected error configuring with non-existent provider")
239256 }
240257
241258 // Configure non-existent mysis
242- if err := cmd.ConfigureMysis("nonexistent", "mock"); err == nil {
259+ if err := cmd.ConfigureMysis("nonexistent", "mock", "mock-model"); err == nil {
243260 t.Error("expected error configuring non-existent mysis")
244261 }
245262 }
@@ -416,19 +433,20 @@ func TestCommanderLoadMyses(t *testing.T) {
416433 defer s.Close()
417434
418435 // Pre-populate store
419- s.CreateMysis("existing-1", "mock", "mock-model")
420- s.CreateMysis("existing-2", "mock", "mock-model")
436+ s.CreateMysis("existing-1", "mock", "mock-model", 0.7)
437+ s.CreateMysis("existing-2", "mock", "mock-model", 0.7)
421438
422439 bus := NewEventBus(100)
423440 defer bus.Close()
424441
425442 reg := provider.NewRegistry()
426- reg.Register(provider.NewMock("mock", "response"))
443+ limiter := rate.NewLimiter(rate.Limit(1000), 1000)
444+ reg.RegisterFactory(provider.NewMockFactoryWithLimiter("mock", "response", limiter))
427445
428446 cfg := &config.Config{
429447 Swarm: config.SwarmConfig{MaxMyses: 16},
430448 Providers: map[string]config.ProviderConfig{
431- "mock": {Endpoint: "http://mock", Model: "mock-model"},
449+ "mock": {Endpoint: "http://mock", Model: "mock-model", Temperature: 0.7},
432450 },
433451 }
434452
internal/core/types.go+6−0
@@ -30,6 +30,7 @@ const (
3030 EventNetworkLLM EventType = "network_llm" // LLM request started/finished
3131 EventNetworkMCP EventType = "network_mcp" // MCP request started/finished
3232 EventNetworkIdle EventType = "network_idle" // Network activity finished
33+ EventRateLimit EventType = "rate_limit"
3334 )
3435
3536 // Event represents something that happened in the swarm.
@@ -63,3 +64,8 @@ type ConfigChangeData struct {
6364 Provider string
6465 Model string
6566 }
67+
68+type RateLimitData struct {
69+ Provider string
70+ Model string
71+}
internal/provider/factory.go+41−0
@@ -0,0 +1,41 @@
1+package provider
2+
3+import "golang.org/x/time/rate"
4+
5+type OllamaFactory struct {
6+ endpoint string
7+ limiter *rate.Limiter
8+}
9+
10+func NewOllamaFactory(endpoint string, rateLimit float64, rateBurst int) *OllamaFactory {
11+ return &OllamaFactory{
12+ endpoint: endpoint,
13+ limiter: rate.NewLimiter(rate.Limit(rateLimit), rateBurst),
14+ }
15+}
16+
17+func (f *OllamaFactory) Name() string { return "ollama" }
18+
19+func (f *OllamaFactory) Create(model string, temperature float64) Provider {
20+ return NewOllamaWithTemp(f.endpoint, model, temperature, f.limiter)
21+}
22+
23+type OpenCodeFactory struct {
24+ endpoint string
25+ apiKey string
26+ limiter *rate.Limiter
27+}
28+
29+func NewOpenCodeFactory(endpoint, apiKey string, rateLimit float64, rateBurst int) *OpenCodeFactory {
30+ return &OpenCodeFactory{
31+ endpoint: endpoint,
32+ apiKey: apiKey,
33+ limiter: rate.NewLimiter(rate.Limit(rateLimit), rateBurst),
34+ }
35+}
36+
37+func (f *OpenCodeFactory) Name() string { return "opencode_zen" }
38+
39+func (f *OpenCodeFactory) Create(model string, temperature float64) Provider {
40+ return NewOpenCodeWithTemp(f.endpoint, model, f.apiKey, temperature, f.limiter)
41+}
internal/provider/mock.go+58−0
@@ -3,6 +3,8 @@ package provider
33 import (
44 "context"
55 "sync"
6+
7+ "golang.org/x/time/rate"
68 )
79
810 // MockProvider is a test provider that returns predefined responses.
@@ -14,6 +16,8 @@ type MockProvider struct {
1416 toolCalls []ToolCall
1517 streamErr error
1618 chatErr error
19+ limiter *rate.Limiter
20+ reasoning string
1721 }
1822
1923 // NewMock creates a new mock provider.
@@ -24,6 +28,27 @@ func NewMock(name, response string) *MockProvider {
2428 }
2529 }
2630
31+type MockFactory struct {
32+ name string
33+ response string
34+ limiter *rate.Limiter
35+}
36+
37+func NewMockFactory(name, response string) *MockFactory {
38+ return &MockFactory{name: name, response: response}
39+}
40+
41+func NewMockFactoryWithLimiter(name, response string, limiter *rate.Limiter) *MockFactory {
42+ return &MockFactory{name: name, response: response, limiter: limiter}
43+}
44+
45+func (f *MockFactory) Name() string { return f.name }
46+
47+func (f *MockFactory) Create(model string, temperature float64) Provider {
48+ p := NewMock(f.name, f.response)
49+ return p.WithLimiter(f.limiter)
50+}
51+
2752 // WithChatError sets an error to return from Chat.
2853 func (p *MockProvider) WithChatError(err error) *MockProvider {
2954 p.mu.Lock()
@@ -48,6 +73,20 @@ func (p *MockProvider) WithToolCalls(calls []ToolCall) *MockProvider {
4873 return p
4974 }
5075
76+func (p *MockProvider) WithReasoning(reasoning string) *MockProvider {
77+ p.mu.Lock()
78+ defer p.mu.Unlock()
79+ p.reasoning = reasoning
80+ return p
81+}
82+
83+func (p *MockProvider) WithLimiter(limiter *rate.Limiter) *MockProvider {
84+ p.mu.Lock()
85+ defer p.mu.Unlock()
86+ p.limiter = limiter
87+ return p
88+}
89+
5190 // WithResponse sets the predefined response to return from Chat.
5291 func (p *MockProvider) WithResponse(response string) *MockProvider {
5392 p.mu.Lock()
@@ -63,6 +102,12 @@ func (p *MockProvider) Name() string {
63102
64103 // Chat returns the predefined response or error.
65104 func (p *MockProvider) Chat(ctx context.Context, messages []Message) (string, error) {
105+ if p.limiter != nil {
106+ if err := p.limiter.Wait(ctx); err != nil {
107+ return "", err
108+ }
109+ }
110+
66111 p.mu.RLock()
67112 defer p.mu.RUnlock()
68113 if p.chatErr != nil {
@@ -73,6 +118,12 @@ func (p *MockProvider) Chat(ctx context.Context, messages []Message) (string, er
73118
74119 // ChatWithTools returns the predefined response or tool calls.
75120 func (p *MockProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) {
121+ if p.limiter != nil {
122+ if err := p.limiter.Wait(ctx); err != nil {
123+ return nil, err
124+ }
125+ }
126+
76127 p.mu.RLock()
77128 defer p.mu.RUnlock()
78129 if p.chatErr != nil {
@@ -81,11 +132,18 @@ func (p *MockProvider) ChatWithTools(ctx context.Context, messages []Message, to
81132 return &ChatResponse{
82133 Content: p.response,
83134 ToolCalls: p.toolCalls,
135+ Reasoning: p.reasoning,
84136 }, nil
85137 }
86138
87139 // Stream returns the predefined response as a single chunk.
88140 func (p *MockProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) {
141+ if p.limiter != nil {
142+ if err := p.limiter.Wait(ctx); err != nil {
143+ return nil, err
144+ }
145+ }
146+
89147 p.mu.RLock()
90148 defer p.mu.RUnlock()
91149 if p.streamErr != nil {
internal/provider/ollama.go+216−36
@@ -1,29 +1,47 @@
11 package provider
22
33 import (
4+ "bytes"
45 "context"
56 "encoding/json"
67 "errors"
8+ "fmt"
79 "io"
10+ "net/http"
11+ "strings"
812
913 openai "github.com/sashabaranov/go-openai"
14+ "golang.org/x/time/rate"
1015 )
1116
1217 // OllamaProvider implements the Provider interface for Ollama.
1318 type OllamaProvider struct {
14- client *openai.Client
15- model string
19+ client *openai.Client
20+ baseURL string
21+ httpClient *http.Client
22+ model string
23+ temperature float64
24+ limiter *rate.Limiter
1625 }
1726
1827 // NewOllama creates a new Ollama provider.
1928 // Ollama exposes an OpenAI-compatible API at /v1.
2029 func NewOllama(endpoint, model string) *OllamaProvider {
30+ return NewOllamaWithTemp(endpoint, model, 0.7, nil)
31+}
32+
33+func NewOllamaWithTemp(endpoint, model string, temperature float64, limiter *rate.Limiter) *OllamaProvider {
2134 config := openai.DefaultConfig("")
22- config.BaseURL = endpoint + "/v1"
35+ baseURL := strings.TrimRight(endpoint, "/") + "/v1"
36+ config.BaseURL = baseURL
2337
2438 return &OllamaProvider{
25- client: openai.NewClientWithConfig(config),
26- model: model,
39+ client: openai.NewClientWithConfig(config),
40+ baseURL: baseURL,
41+ httpClient: &http.Client{},
42+ model: model,
43+ temperature: temperature,
44+ limiter: limiter,
2745 }
2846 }
2947
@@ -34,9 +52,16 @@ func (p *OllamaProvider) Name() string {
3452
3553 // Chat sends messages and returns the complete response.
3654 func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string, error) {
37- resp, err := p.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
38- Model: p.model,
39- Messages: toOpenAIMessages(messages),
55+ if p.limiter != nil {
56+ if err := p.limiter.Wait(ctx); err != nil {
57+ return "", err
58+ }
59+ }
60+
61+ resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
62+ Model: p.model,
63+ Messages: toOllamaMessages(messages),
64+ Temperature: float32(p.temperature),
4065 })
4166 if err != nil {
4267 return "", err
@@ -51,34 +76,17 @@ func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string,
5176
5277 // ChatWithTools sends messages with available tools and returns response with potential tool calls.
5378 func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) {
54- // Convert tools to OpenAI format
55- openaiTools := make([]openai.Tool, len(tools))
56- for i, t := range tools {
57- var params map[string]interface{}
58- if len(t.Parameters) > 0 {
59- json.Unmarshal(t.Parameters, &params)
60- }
61- if params == nil {
62- params = map[string]interface{}{
63- "type": "object",
64- "properties": map[string]interface{}{},
65- }
66- }
67-
68- openaiTools[i] = openai.Tool{
69- Type: openai.ToolTypeFunction,
70- Function: &openai.FunctionDefinition{
71- Name: t.Name,
72- Description: t.Description,
73- Parameters: params,
74- },
79+ if p.limiter != nil {
80+ if err := p.limiter.Wait(ctx); err != nil {
81+ return nil, err
7582 }
7683 }
7784
78- resp, err := p.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
79- Model: p.model,
80- Messages: toOpenAIMessages(messages),
81- Tools: openaiTools,
85+ resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
86+ Model: p.model,
87+ Messages: toOllamaMessages(messages),
88+ Tools: toOllamaTools(tools),
89+ Temperature: float32(p.temperature),
8290 })
8391 if err != nil {
8492 return nil, err
@@ -90,7 +98,8 @@ func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message,
9098
9199 choice := resp.Choices[0]
92100 result := &ChatResponse{
93- Content: choice.Message.Content,
101+ Content: choice.Message.Content,
102+ Reasoning: choice.Message.reasoning(),
94103 }
95104
96105 // Extract tool calls if present
@@ -108,11 +117,120 @@ func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message,
108117 return result, nil
109118 }
110119
120+type chatCompletionResponse struct {
121+ Choices []chatCompletionChoice `json:"choices"`
122+}
123+
124+type chatCompletionChoice struct {
125+ Message chatCompletionMessage `json:"message"`
126+}
127+
128+type chatCompletionMessage struct {
129+ Role string `json:"role"`
130+ Content string `json:"content"`
131+ Reasoning string `json:"reasoning,omitempty"`
132+ ReasoningContent string `json:"reasoning_content,omitempty"`
133+ ToolCalls []chatCompletionToolCall `json:"tool_calls,omitempty"`
134+}
135+
136+type chatCompletionToolCall struct {
137+ ID string `json:"id"`
138+ Type string `json:"type"`
139+ Function chatCompletionFunction `json:"function"`
140+}
141+
142+type chatCompletionFunction struct {
143+ Name string `json:"name"`
144+ Arguments string `json:"arguments"`
145+}
146+
147+type ollamaChatRequest struct {
148+ Model string `json:"model"`
149+ Messages []ollamaReqMessage `json:"messages"`
150+ Tools []ollamaReqTool `json:"tools,omitempty"`
151+ Temperature float32 `json:"temperature,omitempty"`
152+}
153+
154+type ollamaReqMessage struct {
155+ Role string `json:"role"`
156+ Content string `json:"content"`
157+ ToolCallID string `json:"tool_call_id,omitempty"`
158+ ToolCalls []ollamaReqToolCall `json:"tool_calls,omitempty"`
159+}
160+
161+type ollamaReqTool struct {
162+ Type string `json:"type"`
163+ Function ollamaReqFunction `json:"function"`
164+}
165+
166+type ollamaReqFunction struct {
167+ Name string `json:"name"`
168+ Description string `json:"description"`
169+ Parameters map[string]interface{} `json:"parameters"`
170+}
171+
172+type ollamaReqToolCall struct {
173+ ID string `json:"id"`
174+ Type string `json:"type"`
175+ Function ollamaReqFuncCall `json:"function"`
176+}
177+
178+type ollamaReqFuncCall struct {
179+ Name string `json:"name"`
180+ Arguments string `json:"arguments"`
181+}
182+
183+func (m chatCompletionMessage) reasoning() string {
184+ if m.Reasoning != "" {
185+ return m.Reasoning
186+ }
187+ return m.ReasoningContent
188+}
189+
190+func (p *OllamaProvider) createChatCompletion(ctx context.Context, req ollamaChatRequest) (*chatCompletionResponse, error) {
191+ body, err := json.Marshal(req)
192+ if err != nil {
193+ return nil, err
194+ }
195+
196+ url := p.baseURL + "/chat/completions"
197+ httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
198+ if err != nil {
199+ return nil, err
200+ }
201+ httpReq.Header.Set("Content-Type", "application/json")
202+
203+ resp, err := p.httpClient.Do(httpReq)
204+ if err != nil {
205+ return nil, err
206+ }
207+ defer resp.Body.Close()
208+
209+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
210+ payload, _ := io.ReadAll(resp.Body)
211+ return nil, fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
212+ }
213+
214+ var decoded chatCompletionResponse
215+ if err := json.NewDecoder(resp.Body).Decode(&decoded); err != nil {
216+ return nil, err
217+ }
218+
219+ return &decoded, nil
220+}
221+
111222 // Stream sends messages and returns a channel that streams response chunks.
112223 func (p *OllamaProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) {
224+ if p.limiter != nil {
225+ if err := p.limiter.Wait(ctx); err != nil {
226+ return nil, err
227+ }
228+ }
229+
113230 stream, err := p.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
114- Model: p.model,
115- Messages: toOpenAIMessages(messages),
231+ Model: p.model,
232+ Messages: toOpenAIMessages(messages),
233+ Temperature: float32(p.temperature),
116234 })
117235 if err != nil {
118236 return nil, err
@@ -175,3 +293,65 @@ func toOpenAIMessages(messages []Message) []openai.ChatCompletionMessage {
175293 }
176294 return result
177295 }
296+
297+func toOllamaMessages(messages []Message) []ollamaReqMessage {
298+ result := make([]ollamaReqMessage, len(messages))
299+ for i, m := range messages {
300+ msg := ollamaReqMessage{
301+ Role: m.Role,
302+ Content: m.Content,
303+ }
304+
305+ if m.ToolCallID != "" {
306+ msg.ToolCallID = m.ToolCallID
307+ }
308+
309+ if len(m.ToolCalls) > 0 {
310+ msg.ToolCalls = make([]ollamaReqToolCall, len(m.ToolCalls))
311+ for j, tc := range m.ToolCalls {
312+ msg.ToolCalls[j] = ollamaReqToolCall{
313+ ID: tc.ID,
314+ Type: "function",
315+ Function: ollamaReqFuncCall{
316+ Name: tc.Name,
317+ Arguments: string(tc.Arguments),
318+ },
319+ }
320+ }
321+ }
322+
323+ result[i] = msg
324+ }
325+ return result
326+}
327+
328+func toOllamaTools(tools []Tool) []ollamaReqTool {
329+ result := make([]ollamaReqTool, len(tools))
330+ for i, t := range tools {
331+ var params map[string]interface{}
332+ if len(t.Parameters) > 0 {
333+ if err := json.Unmarshal(t.Parameters, &params); err != nil {
334+ params = map[string]interface{}{
335+ "type": "object",
336+ "properties": map[string]interface{}{},
337+ }
338+ }
339+ }
340+ if params == nil {
341+ params = map[string]interface{}{
342+ "type": "object",
343+ "properties": map[string]interface{}{},
344+ }
345+ }
346+
347+ result[i] = ollamaReqTool{
348+ Type: "function",
349+ Function: ollamaReqFunction{
350+ Name: t.Name,
351+ Description: t.Description,
352+ Parameters: params,
353+ },
354+ }
355+ }
356+ return result
357+}
internal/provider/opencode.go+98−16
@@ -1,28 +1,48 @@
11 package provider
22
33 import (
4+ "bytes"
45 "context"
56 "encoding/json"
67 "errors"
8+ "fmt"
79 "io"
10+ "net/http"
11+ "strings"
812
913 openai "github.com/sashabaranov/go-openai"
14+ "golang.org/x/time/rate"
1015 )
1116
1217 // OpenCodeProvider implements the Provider interface for OpenCode Zen.
1318 type OpenCodeProvider struct {
14- client *openai.Client
15- model string
19+ client *openai.Client
20+ baseURL string
21+ apiKey string
22+ httpClient *http.Client
23+ model string
24+ temperature float64
25+ limiter *rate.Limiter
1626 }
1727
1828 // NewOpenCode creates a new OpenCode Zen provider.
1929 func NewOpenCode(endpoint, model, apiKey string) *OpenCodeProvider {
30+ return NewOpenCodeWithTemp(endpoint, model, apiKey, 0.7, nil)
31+}
32+
33+func NewOpenCodeWithTemp(endpoint, model, apiKey string, temperature float64, limiter *rate.Limiter) *OpenCodeProvider {
2034 config := openai.DefaultConfig(apiKey)
21- config.BaseURL = endpoint
35+ baseURL := strings.TrimRight(endpoint, "/")
36+ config.BaseURL = baseURL
2237
2338 return &OpenCodeProvider{
24- client: openai.NewClientWithConfig(config),
25- model: model,
39+ client: openai.NewClientWithConfig(config),
40+ baseURL: baseURL,
41+ apiKey: apiKey,
42+ httpClient: &http.Client{},
43+ model: model,
44+ temperature: temperature,
45+ limiter: limiter,
2646 }
2747 }
2848
@@ -33,9 +53,16 @@ func (p *OpenCodeProvider) Name() string {
3353
3454 // Chat sends messages and returns the complete response.
3555 func (p *OpenCodeProvider) Chat(ctx context.Context, messages []Message) (string, error) {
36- resp, err := p.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Cut short: this commit's diff is over 1000 lines.
internal/provider/provider.go+15−11
Cut short: this commit's diff is over 1000 lines.
internal/provider/provider_test.go+63−8
Cut short: this commit's diff is over 1000 lines.