xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / provider / opencode.go

10.9 KB
1package provider
2
3import (
4	"bytes"
5	"context"
6	"encoding/json"
7	"errors"
8	"fmt"
9	"io"
10	"net/http"
11	"strings"
12	"time"
13
14	"github.com/rs/zerolog/log"
15	openai "github.com/sashabaranov/go-openai"
16)
17
18// openCodeRequest is a custom request struct to ensure stream:false is serialized
19// The openai.ChatCompletionRequest has omitempty on Stream, which omits false values
20type openCodeRequest struct {
21	Model       string                         `json:"model"`
22	Messages    []openai.ChatCompletionMessage `json:"messages"`
23	Tools       []openai.Tool                  `json:"tools,omitempty"`
24	Temperature float32                        `json:"temperature,omitempty"`
25	Stream      bool                           `json:"stream"` // NO omitempty - always serialize
26}
27
28// OpenCodeProvider implements the Provider interface for OpenCode Zen.
29type OpenCodeProvider struct {
30	name        string
31	client      *openai.Client
32	baseURL     string
33	apiKey      string
34	httpClient  *http.Client
35	model       string
36	temperature float64
37}
38
39var opencodeRetryDelays = []time.Duration{5 * time.Second, 10 * time.Second, 15 * time.Second}
40
41const (
42	opencodeChatCompletionsEndpoint = "/chat/completions"
43	opencodeMessagesEndpoint        = "/messages"
44	opencodeResponsesEndpoint       = "/responses"
45)
46
47var opencodeModelEndpoints = map[string]string{
48	"big-pickle":                 opencodeChatCompletionsEndpoint,
49	"gemini-3-pro":               "/models/gemini-3-pro",
50	"gemini-3-flash":             "/models/gemini-3-flash",
51	"glm-4.7-free":               opencodeChatCompletionsEndpoint,
52	"gpt-5-nano":                 opencodeChatCompletionsEndpoint, // Using chat/completions despite docs saying /responses (500 errors)
53	"kimi-k2.5-free":             opencodeChatCompletionsEndpoint,
54	"minimax-m2.1-free":          opencodeMessagesEndpoint,
55	"trinity-large-preview-free": opencodeChatCompletionsEndpoint,
56}
57
58// NewOpenCode creates a new OpenCode Zen provider.
59func NewOpenCode(endpoint, model, apiKey string) *OpenCodeProvider {
60	return NewOpenCodeWithTemp("opencode_zen", endpoint, model, apiKey, 0.7)
61}
62
63func NewOpenCodeWithTemp(name string, endpoint, model, apiKey string, temperature float64) *OpenCodeProvider {
64	config := openai.DefaultConfig(apiKey)
65	baseURL := strings.TrimRight(endpoint, "/")
66	config.BaseURL = baseURL
67
68	return &OpenCodeProvider{
69		name:        name,
70		client:      openai.NewClientWithConfig(config),
71		baseURL:     baseURL,
72		apiKey:      apiKey,
73		httpClient:  &http.Client{},
74		model:       model,
75		temperature: temperature,
76	}
77}
78
79// Name returns the provider identifier.
80func (p *OpenCodeProvider) Name() string {
81	return p.name
82}
83
84// Chat sends messages and returns the complete response.
85func (p *OpenCodeProvider) Chat(ctx context.Context, messages []Message) (string, error) {
86	resp, err := p.createChatCompletion(ctx, openai.ChatCompletionRequest{
87		Model:       p.model,
88		Messages:    mergeSystemMessagesOpenAI(toOpenAIMessages(messages)),
89		Temperature: float32(p.temperature),
90		Stream:      false,
91	})
92	if err != nil {
93		return "", err
94	}
95
96	if len(resp.Choices) == 0 {
97		return "", errors.New("no response choices")
98	}
99
100	return resp.Choices[0].Message.Content, nil
101}
102
103// ChatWithTools sends messages with available tools and returns response with potential tool calls.
104func (p *OpenCodeProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) {
105	// Convert tools to OpenAI format
106	openaiTools, err := toOpenAITools(tools)
107	if err != nil {
108		return nil, fmt.Errorf("invalid tool schema: %w", err)
109	}
110
111	resp, err := p.createChatCompletion(ctx, openai.ChatCompletionRequest{
112		Model:       p.model,
113		Messages:    mergeSystemMessagesOpenAI(toOpenAIMessages(messages)),
114		Tools:       openaiTools,
115		Temperature: float32(p.temperature),
116		Stream:      false,
117	})
118	if err != nil {
119		return nil, err
120	}
121
122	if len(resp.Choices) == 0 {
123		log.Error().
124			Str("provider", p.name).
125			Msg("OpenCode returned empty choices array")
126		return nil, errors.New("no response choices")
127	}
128
129	choice := resp.Choices[0]
130	result := &ChatResponse{
131		Content:   choice.Message.Content,
132		Reasoning: "", // OpenAI standard doesn't provide reasoning field
133	}
134
135	log.Debug().
136		Str("provider", p.name).
137		Str("content", choice.Message.Content).
138		Int("tool_call_count", len(choice.Message.ToolCalls)).
139		Msg("OpenCode ChatWithTools result")
140
141	// Extract tool calls if present
142	if len(choice.Message.ToolCalls) > 0 {
143		result.ToolCalls = make([]ToolCall, len(choice.Message.ToolCalls))
144		for i, tc := range choice.Message.ToolCalls {
145			result.ToolCalls[i] = ToolCall{
146				ID:        tc.ID,
147				Name:      tc.Function.Name,
148				Arguments: json.RawMessage(tc.Function.Arguments),
149			}
150			log.Debug().
151				Str("provider", p.name).
152				Str("tool_call_id", tc.ID).
153				Str("tool_name", tc.Function.Name).
154				Str("arguments", tc.Function.Arguments).
155				Msg("OpenCode tool call extracted")
156		}
157	}
158
159	return result, nil
160}
161
162func (p *OpenCodeProvider) createChatCompletion(ctx context.Context, req openai.ChatCompletionRequest) (*openaiChatResponse, error) {
163	// Use custom struct to ensure stream:false is serialized
164	customReq := openCodeRequest{
165		Model:       req.Model,
166		Messages:    req.Messages,
167		Tools:       req.Tools,
168		Temperature: req.Temperature,
169		Stream:      req.Stream,
170	}
171	body, err := json.Marshal(customReq)
172	if err != nil {
173		return nil, err
174	}
175
176	url := p.baseURL + opencodeEndpointForModel(p.model)
177
178	// Retry logic for transient errors (rate limits, server outages)
179	maxRetries := len(opencodeRetryDelays)
180
181	var lastErr error
182	for attempt := 0; attempt <= maxRetries; attempt++ {
183		if attempt > 0 {
184			delay := opencodeRetryDelays[attempt-1]
185			log.Warn().
186				Str("provider", p.name).
187				Int("attempt", attempt).
188				Dur("delay", delay).
189				Msg("Retrying OpenCode request after transient error")
190
191			select {
192			case <-time.After(delay):
193			case <-ctx.Done():
194				return nil, ctx.Err()
195			}
196		}
197
198		// Log at Info level for first attempt, Debug for retries
199		if attempt == 0 {
200			log.Info().
201				Str("provider", p.name).
202				Str("model", req.Model).
203				Int("message_count", len(req.Messages)).
204				Int("tool_count", len(req.Tools)).
205				Msg("OpenCode request started")
206		}
207
208		log.Debug().
209			Str("provider", p.name).
210			Str("url", url).
211			Str("model", req.Model).
212			Bool("has_api_key", p.apiKey != "").
213			Int("message_count", len(req.Messages)).
214			Int("tool_count", len(req.Tools)).
215			Int("attempt", attempt+1).
216			Str("request_body", string(body)).
217			Msg("OpenCode chat completion request")
218
219		httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
220		if err != nil {
221			return nil, err
222		}
223		httpReq.Header.Set("Content-Type", "application/json")
224		if p.apiKey != "" {
225			httpReq.Header.Set("Authorization", "Bearer "+p.apiKey)
226		}
227
228		resp, err := p.httpClient.Do(httpReq)
229		if err != nil {
230			// Do not retry on context cancellation or timeout
231			if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
232				return nil, err
233			}
234			lastErr = err
235			continue // Network error - retry
236		}
237		defer resp.Body.Close()
238
239		log.Debug().
240			Str("provider", p.name).
241			Str("url", url).
242			Int("status", resp.StatusCode).
243			Int("attempt", attempt+1).
244			Msg("OpenCode chat completion response")
245
246		// Check for retryable status codes
247		if resp.StatusCode == 429 || resp.StatusCode == 500 || resp.StatusCode == 502 ||
248			resp.StatusCode == 503 || resp.StatusCode == 504 {
249			payload, _ := io.ReadAll(resp.Body)
250			lastErr = fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
251
252			log.Warn().
253				Str("provider", p.name).
254				Int("status", resp.StatusCode).
255				Int("attempt", attempt+1).
256				Str("body", string(payload)).
257				Msg("OpenCode retryable error")
258
259			resp.Body.Close()
260			continue // Retry on transient server errors and rate limits
261		}
262
263		// Non-retryable client error (4xx except 429)
264		if resp.StatusCode < 200 || resp.StatusCode >= 300 {
265			payload, _ := io.ReadAll(resp.Body)
266			log.Error().
267				Str("provider", p.name).
268				Int("status", resp.StatusCode).
269				Str("body", string(payload)).
270				Msg("OpenCode non-2xx response")
271			return nil, fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
272		}
273
274		// Success - read and decode body
275		bodyBytes, err := io.ReadAll(resp.Body)
276		if err != nil {
277			log.Error().
278				Str("provider", p.name).
279				Err(err).
280				Msg("OpenCode failed to read response body")
281			return nil, fmt.Errorf("read response body: %w", err)
282		}
283
284		var decoded openaiChatResponse
285		if err := json.Unmarshal(bodyBytes, &decoded); err != nil {
286			log.Error().
287				Str("provider", p.name).
288				Err(err).
289				Str("body", string(bodyBytes)).
290				Msg("OpenCode JSON decode failed")
291			return nil, fmt.Errorf("decode response: %w", err)
292		}
293
294		log.Debug().
295			Str("provider", p.name).
296			Int("choice_count", len(decoded.Choices)).
297			Msg("OpenCode response decoded")
298
299		// Log successful request at Info level for visibility
300		log.Info().
301			Str("provider", p.name).
302			Str("model", p.model).
303			Int("status", resp.StatusCode).
304			Int("attempt", attempt+1).
305			Int("message_count", len(req.Messages)).
306			Int("choice_count", len(decoded.Choices)).
307			Msg("OpenCode request successful")
308
309		return &decoded, nil
310	}
311
312	// All retries exhausted
313	log.Error().
314		Str("provider", p.name).
315		Str("model", p.model).
316		Int("max_retries", maxRetries).
317		Int("total_attempts", maxRetries+1).
318		Err(lastErr).
319		Msg("OpenCode request failed after all retries")
320	return nil, fmt.Errorf("request failed after %d retries: %w", maxRetries, lastErr)
321}
322
323// Stream sends messages and returns a channel that streams response chunks.
324func (p *OpenCodeProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) {
325	if opencodeEndpointForModel(p.model) != opencodeChatCompletionsEndpoint {
326		return nil, fmt.Errorf("opencode model %q does not support streaming via chat completions endpoint", p.model)
327	}
328
329	stream, err := p.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
330		Model:       p.model,
331		Messages:    mergeSystemMessagesOpenAI(toOpenAIMessages(messages)),
332		Temperature: float32(p.temperature),
333	})
334	if err != nil {
335		return nil, err
336	}
337
338	ch := make(chan StreamChunk)
339	go func() {
340		defer close(ch)
341		defer stream.Close()
342
343		for {
344			resp, err := stream.Recv()
345			if errors.Is(err, io.EOF) {
346				ch <- StreamChunk{Done: true}
347				return
348			}
349			if err != nil {
350				ch <- StreamChunk{Err: err}
351				return
352			}
353
354			if len(resp.Choices) > 0 {
355				ch <- StreamChunk{Content: resp.Choices[0].Delta.Content}
356			}
357		}
358	}()
359
360	return ch, nil
361}
362
363func opencodeEndpointForModel(model string) string {
364	if endpoint, ok := opencodeModelEndpoints[model]; ok {
365		return endpoint
366	}
367
368	switch {
369	case strings.HasPrefix(model, "gpt-"):
370		return opencodeResponsesEndpoint
371	case strings.HasPrefix(model, "claude-"):
372		return opencodeMessagesEndpoint
373	default:
374		return opencodeChatCompletionsEndpoint
375	}
376}
377
378// Close closes idle HTTP connections
379func (p *OpenCodeProvider) Close() error {
380	if p.httpClient != nil {
381		p.httpClient.CloseIdleConnections()
382	}
383	return nil
384}