xonecas.io
62159
Sy

symb / commit 490a0db

feat: streamed responses and stable opencode connection

xonecas2026-02-13 490a0db72a121f14bad42ebd6c90d3d86074c5e0 parent 2455182

10 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.

assets/Screenshot_20260213_175307.png+0−0
Binary file, not shown.
config.toml+1−1
@@ -1,7 +1,7 @@
11 # Symb Configuration File
22
33 # Default provider (optional - if not set, first provider in map is used)
4-default_provider = "ollama-qwen-small"
4+default_provider = "zen-pickle"
55
66 # Ollama providers (local)
77 [providers.ollama-qwen]
docs/DESIGN.md+5−0
@@ -98,6 +98,11 @@ File paths in conversation (tool calls, content) become clickable. Clicking
9898 opens the file in the editor pane at the referenced line.
9999 - Don't show tool response bodies in conversation pane once the user can click to see. Show a max 5 line preview for the user to click to see more. User clicks, the content of the tool response is sent to the editor.
100100
101+### Statusbar implementation
102+
103+- Needs design work
104+
105+
101106 ### Web/Search Tools
102107
103108 HTTP fetch cleans the html for less wasted tokens, preserving content.
internal/llm/loop.go+80−29
@@ -3,6 +3,7 @@ package llm
33
44 import (
55 "context"
6+ "encoding/json"
67 "fmt"
78 "time"
89
@@ -10,9 +11,12 @@ import (
1011 "github.com/xonecas/symb/internal/provider"
1112 )
1213
13-// MessageCallback is called when a message should be added to history and saved.
14+// MessageCallback is called when a complete message should be added to history.
1415 type MessageCallback func(msg provider.Message)
1516
17+// DeltaCallback is called for each streaming event (content/reasoning deltas).
18+type DeltaCallback func(evt provider.StreamEvent)
19+
1620 // ToolCallCallback is called when tool calls are about to be executed.
1721 type ToolCallCallback func()
1822
@@ -23,52 +27,42 @@ type ProcessTurnOptions struct {
2327 Tools []mcp.Tool
2428 History []provider.Message
2529 OnMessage MessageCallback
30+ OnDelta DeltaCallback // Optional: called for each stream event
2631 OnToolCall ToolCallCallback // Optional: called before executing tool calls
2732 MaxToolRounds int
2833 }
2934
3035 // ProcessTurn handles one conversation turn, which may involve tool calls.
31-// It returns an error if the LLM call fails or max rounds are exceeded.
36+// It streams events via OnDelta and emits complete messages via OnMessage.
3237 func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
3338 if opts.MaxToolRounds == 0 {
3439 opts.MaxToolRounds = 20
3540 }
3641
37- for round := 0; round < opts.MaxToolRounds; round++ {
38- // Convert MCP tools to provider format
39- providerTools := make([]provider.Tool, len(opts.Tools))
40- for i, t := range opts.Tools {
41- providerTools[i] = provider.Tool{
42- Name: t.Name,
43- Description: t.Description,
44- Parameters: t.InputSchema,
45- }
42+ // Convert MCP tools to provider format once
43+ providerTools := make([]provider.Tool, len(opts.Tools))
44+ for i, t := range opts.Tools {
45+ providerTools[i] = provider.Tool{
46+ Name: t.Name,
47+ Description: t.Description,
48+ Parameters: t.InputSchema,
4649 }
50+ }
4751
48- // Call LLM with history
49- resp, err := opts.Provider.ChatWithTools(ctx, opts.History, providerTools)
52+ for round := 0; round < opts.MaxToolRounds; round++ {
53+ // Start streaming from LLM
54+ stream, err := opts.Provider.ChatStream(ctx, opts.History, providerTools)
5055 if err != nil {
5156 return fmt.Errorf("LLM call failed: %w", err)
5257 }
5358
54- // If no tool calls, we're done
55- if len(resp.ToolCalls) == 0 {
56- // Add assistant response to history
57- assistantMsg := provider.Message{
58- Role: "assistant",
59- Content: resp.Content,
60- Reasoning: resp.Reasoning,
61- CreatedAt: time.Now(),
62- }
63- if opts.OnMessage != nil {
64- opts.OnMessage(assistantMsg)
65- }
66- opts.History = append(opts.History, assistantMsg)
67-
68- return nil
59+ // Read stream, forwarding deltas and accumulating the full response
60+ resp, err := collectWithDeltas(stream, opts.OnDelta)
61+ if err != nil {
62+ return fmt.Errorf("LLM stream failed: %w", err)
6963 }
7064
71- // Tool calls present - add assistant message with tool calls to history
65+ // Build assistant message from accumulated response
7266 assistantMsg := provider.Message{
7367 Role: "assistant",
7468 Content: resp.Content,
@@ -81,6 +75,11 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
8175 }
8276 opts.History = append(opts.History, assistantMsg)
8377
78+ // If no tool calls, we're done
79+ if len(resp.ToolCalls) == 0 {
80+ return nil
81+ }
82+
8483 // Notify about tool calls if callback provided
8584 if opts.OnToolCall != nil {
8685 opts.OnToolCall()
@@ -96,6 +95,58 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
9695 return fmt.Errorf("too many tool call rounds (limit: %d)", opts.MaxToolRounds)
9796 }
9897
98+// collectWithDeltas reads all events from a stream, forwarding each to onDelta,
99+// and assembles them into a ChatResponse.
100+func collectWithDeltas(ch <-chan provider.StreamEvent, onDelta DeltaCallback) (*provider.ChatResponse, error) {
101+ var result provider.ChatResponse
102+ // Map from tool call index to position in our slice
103+ toolCallsByIndex := make(map[int]int)
104+ var toolCalls []provider.ToolCall
105+ var toolArgBuilders []string
106+
107+ for evt := range ch {
108+ // Forward delta to callback
109+ if onDelta != nil {
110+ onDelta(evt)
111+ }
112+
113+ switch evt.Type {
114+ case provider.EventContentDelta:
115+ result.Content += evt.Content
116+ case provider.EventReasoningDelta:
117+ result.Reasoning += evt.Content
118+ case provider.EventToolCallBegin:
119+ pos := len(toolCalls)
120+ toolCallsByIndex[evt.ToolCallIndex] = pos
121+ toolCalls = append(toolCalls, provider.ToolCall{
122+ ID: evt.ToolCallID,
123+ Name: evt.ToolCallName,
124+ })
125+ toolArgBuilders = append(toolArgBuilders, "")
126+ case provider.EventToolCallDelta:
127+ if pos, ok := toolCallsByIndex[evt.ToolCallIndex]; ok {
128+ toolArgBuilders[pos] += evt.ToolCallArgs
129+ }
130+ case provider.EventError:
131+ return nil, evt.Err
132+ case provider.EventDone:
133+ // finalize
134+ }
135+ }
136+
137+ // Finalize tool call arguments
138+ for i := range toolCalls {
139+ if i < len(toolArgBuilders) {
140+ toolCalls[i].Arguments = json.RawMessage(toolArgBuilders[i])
141+ }
142+ }
143+ if len(toolCalls) > 0 {
144+ result.ToolCalls = toolCalls
145+ }
146+
147+ return &result, nil
148+}
149+
99150 // executeToolCalls executes a list of tool calls and adds results to history.
100151 // Returns the list of tool result messages that were added.
101152 func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provider.ToolCall, onMessage MessageCallback) []provider.Message {
internal/provider/mock.go+0−173
@@ -1,173 +0,0 @@
1-package provider
2-
3-import (
4- "context"
5- "sync"
6- "time"
7-)
8-
9-// MockProvider is a test provider that returns predefined responses.
10-type MockProvider struct {
11- mu sync.RWMutex
12-
13- name string
14- response string
15- toolCalls []ToolCall
16- streamErr error
17- chatErr error
18- reasoning string
19- delay time.Duration
20-}
21-
22-// NewMock creates a new mock provider.
23-func NewMock(name, response string) *MockProvider {
24- return &MockProvider{
25- name: name,
26- response: response,
27- }
28-}
29-
30-type MockFactory struct {
31- name string
32- response string
33-}
34-
35-func NewMockFactory(name, response string) *MockFactory {
36- return &MockFactory{name: name, response: response}
37-}
38-
39-func (f *MockFactory) Name() string { return f.name }
40-
41-func (f *MockFactory) Create(model string, temperature float64) Provider {
42- return NewMock(f.name, f.response)
43-}
44-
45-// WithChatError sets an error to return from Chat.
46-func (p *MockProvider) WithChatError(err error) *MockProvider {
47- p.mu.Lock()
48- defer p.mu.Unlock()
49- p.chatErr = err
50- return p
51-}
52-
53-// WithStreamError sets an error to return from Stream.
54-func (p *MockProvider) WithStreamError(err error) *MockProvider {
55- p.mu.Lock()
56- defer p.mu.Unlock()
57- p.streamErr = err
58- return p
59-}
60-
61-// WithToolCalls sets tool calls to return from ChatWithTools.
62-func (p *MockProvider) WithToolCalls(calls []ToolCall) *MockProvider {
63- p.mu.Lock()
64- defer p.mu.Unlock()
65- p.toolCalls = calls
66- return p
67-}
68-
69-func (p *MockProvider) WithReasoning(reasoning string) *MockProvider {
70- p.mu.Lock()
71- defer p.mu.Unlock()
72- p.reasoning = reasoning
73- return p
74-}
75-
76-func (p *MockProvider) SetDelay(delay time.Duration) *MockProvider {
77- p.mu.Lock()
78- defer p.mu.Unlock()
79- p.delay = delay
80- return p
81-}
82-
83-// WithResponse sets the predefined response to return from Chat.
84-func (p *MockProvider) WithResponse(response string) *MockProvider {
85- p.mu.Lock()
86- defer p.mu.Unlock()
87- p.response = response
88- return p
89-}
90-
91-// Name returns the provider identifier.
92-func (p *MockProvider) Name() string {
93- return p.name
94-}
95-
96-// Chat returns the predefined response or error.
97-func (p *MockProvider) Chat(ctx context.Context, messages []Message) (string, error) {
98- if err := p.waitDelay(ctx); err != nil {
99- return "", err
100- }
101-
102- p.mu.RLock()
103- defer p.mu.RUnlock()
104- if p.chatErr != nil {
105- return "", p.chatErr
106- }
107- return p.response, nil
108-}
109-
110-// ChatWithTools returns the predefined response or tool calls.
111-func (p *MockProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) {
112- if err := p.waitDelay(ctx); err != nil {
113- return nil, err
114- }
115-
116- p.mu.RLock()
117- defer p.mu.RUnlock()
118- if p.chatErr != nil {
119- return nil, p.chatErr
120- }
121- return &ChatResponse{
122- Content: p.response,
123- ToolCalls: p.toolCalls,
124- Reasoning: p.reasoning,
125- }, nil
126-}
127-
128-// Stream returns the predefined response as a single chunk.
129-func (p *MockProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) {
130- if err := p.waitDelay(ctx); err != nil {
131- return nil, err
132- }
133-
134- p.mu.RLock()
135- defer p.mu.RUnlock()
136- if p.streamErr != nil {
137- return nil, p.streamErr
138- }
139-
140- ch := make(chan StreamChunk, 2)
141- response := p.response
142- go func() {
143- defer close(ch)
144- ch <- StreamChunk{Content: response}
145- ch <- StreamChunk{Done: true}
146- }()
147-
148- return ch, nil
149-}
150-
151-func (p *MockProvider) waitDelay(ctx context.Context) error {
152- p.mu.RLock()
153- delay := p.delay
154- p.mu.RUnlock()
155- if delay <= 0 {
156- return nil
157- }
158-
159- timer := time.NewTimer(delay)
160- defer timer.Stop()
161-
162- select {
163- case <-ctx.Done():
164- return ctx.Err()
165- case <-timer.C:
166- return nil
167- }
168-}
169-
170-// Close is a no-op for mock provider (no resources to clean up).
171-func (p *MockProvider) Close() error {
172- return nil
173-}
internal/provider/ollama.go+22−255
@@ -1,32 +1,23 @@
11 package provider
22
33 import (
4- "bytes"
54 "context"
65 "encoding/json"
7- "errors"
8- "fmt"
9- "io"
106 "net/http"
117 "strings"
12- "time"
138
149 "github.com/rs/zerolog/log"
15- openai "github.com/sashabaranov/go-openai"
1610 )
1711
1812 // OllamaProvider implements the Provider interface for Ollama.
1913 type OllamaProvider struct {
2014 name string
21- client *openai.Client
2215 baseURL string
2316 httpClient *http.Client
2417 model string
2518 temperature float64
2619 }
2720
28-var ollamaRetryDelays = []time.Duration{5 * time.Second, 10 * time.Second, 15 * time.Second}
29-
3021 // NewOllama creates a new Ollama provider.
3122 // Ollama exposes an OpenAI-compatible API at /v1.
3223 func NewOllama(endpoint, model string) *OllamaProvider {
@@ -34,13 +25,10 @@ func NewOllama(endpoint, model string) *OllamaProvider {
3425 }
3526
3627 func NewOllamaWithTemp(name string, endpoint, model string, temperature float64) *OllamaProvider {
37- config := openai.DefaultConfig("")
3828 baseURL := strings.TrimRight(endpoint, "/") + "/v1"
39- config.BaseURL = baseURL
4029
4130 return &OllamaProvider{
4231 name: name,
43- client: openai.NewClientWithConfig(config),
4432 baseURL: baseURL,
4533 httpClient: &http.Client{},
4634 model: model,
@@ -53,86 +41,39 @@ func (p *OllamaProvider) Name() string {
5341 return p.name
5442 }
5543
56-// Chat sends messages and returns the complete response.
57-func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string, error) {
58- resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
44+// ChatStream sends messages with optional tools and returns a channel of streaming events.
45+func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
46+ req := ollamaChatRequest{
5947 Model: p.model,
6048 Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)),
49+ Tools: toOllamaTools(tools),
6150 Temperature: float32(p.temperature),
62- })
63- if err != nil {
64- return "", err
51+ Stream: true,
6552 }
66-
67- if len(resp.Choices) == 0 {
68- return "", errors.New("no response choices")
53+ body, err := json.Marshal(req)
54+ if err != nil {
55+ return nil, err
6956 }
7057
71- return resp.Choices[0].Message.Content, nil
72-}
73-
74-// ChatWithTools sends messages with available tools and returns response with potential tool calls.
75-func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) {
76- resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
77- Model: p.model,
78- Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)),
79- Tools: toOllamaTools(tools),
80- Temperature: float32(p.temperature),
58+ reader, err := httpDoSSE(ctx, httpRequestConfig{
59+ client: p.httpClient,
60+ url: p.baseURL + "/chat/completions",
61+ body: body,
62+ provider: "ollama",
63+ model: p.model,
8164 })
8265 if err != nil {
8366 return nil, err
8467 }
8568
86- if len(resp.Choices) == 0 {
87- return nil, errors.New("no response choices")
88- }
89-
90- choice := resp.Choices[0]
91- result := &ChatResponse{
92- Content: choice.Message.Content,
93- Reasoning: choice.Message.reasoning(),
94- }
95-
96- // Extract tool calls if present
97- if len(choice.Message.ToolCalls) > 0 {
98- result.ToolCalls = make([]ToolCall, len(choice.Message.ToolCalls))
99- for i, tc := range choice.Message.ToolCalls {
100- result.ToolCalls[i] = ToolCall{
101- ID: tc.ID,
102- Name: tc.Function.Name,
103- Arguments: json.RawMessage(tc.Function.Arguments),
104- }
105- }
106- }
107-
108- return result, nil
109-}
110-
111-type chatCompletionResponse struct {
112- Choices []chatCompletionChoice `json:"choices"`
113-}
114-
115-type chatCompletionChoice struct {
116- Message chatCompletionMessage `json:"message"`
117-}
118-
119-type chatCompletionMessage struct {
120- Role string `json:"role"`
121- Content string `json:"content"`
122- Reasoning string `json:"reasoning,omitempty"`
123- ReasoningContent string `json:"reasoning_content,omitempty"`
124- ToolCalls []chatCompletionToolCall `json:"tool_calls,omitempty"`
125-}
126-
127-type chatCompletionToolCall struct {
128- ID string `json:"id"`
129- Type string `json:"type"`
130- Function chatCompletionFunction `json:"function"`
131-}
69+ ch := make(chan StreamEvent)
70+ go func() {
71+ defer close(ch)
72+ defer reader.Close()
73+ parseSSEStream(ctx, reader, ch)
74+ }()
13275
133-type chatCompletionFunction struct {
134- Name string `json:"name"`
135- Arguments string `json:"arguments"`
76+ return ch, nil
13677 }
13778
13879 // OLLAMA-SPECIFIC TYPES
@@ -144,6 +85,7 @@ type ollamaChatRequest struct {
14485 Messages []ollamaReqMessage `json:"messages"`
14586 Tools []ollamaReqTool `json:"tools,omitempty"`
14687 Temperature float32 `json:"temperature,omitempty"`
88+ Stream bool `json:"stream"`
14789 }
14890
14991 type ollamaReqMessage struct {
@@ -175,181 +117,6 @@ type ollamaReqFuncCall struct {
175117 Arguments string `json:"arguments"`
176118 }
177119
178-// reasoning extracts reasoning content from Ollama response.
179-//
180-// OLLAMA-SPECIFIC: Ollama may return reasoning in either "reasoning" or "reasoning_content" fields.
181-// This is an Ollama extension not present in standard OpenAI Chat Completions API.
182-func (m chatCompletionMessage) reasoning() string {
183- if m.Reasoning != "" {
184- return m.Reasoning
185- }
186- return m.ReasoningContent
187-}
188-
189-func (p *OllamaProvider) createChatCompletion(ctx context.Context, req ollamaChatRequest) (*chatCompletionResponse, error) {
190- body, err := json.Marshal(req)
191- if err != nil {
192- return nil, err
193- }
194-
195- url := p.baseURL + "/chat/completions"
196-
197- log.Debug().
198- Str("provider", "ollama").
199- Str("model", p.model).
200- Int("messages", len(req.Messages)).
201- Int("body_bytes", len(body)).
202- Msg("Sending request to Ollama")
203-
204- maxRetries := len(ollamaRetryDelays)
205- var lastErr error
206- for attempt := 0; attempt <= maxRetries; attempt++ {
207- if attempt > 0 {
208- delay := ollamaRetryDelays[attempt-1]
209- log.Warn().
210- Str("provider", "ollama").
211- Int("attempt", attempt).
212- Dur("delay", delay).
213- Msg("Retrying Ollama request after transient error")
214-
215- select {
216- case <-time.After(delay):
217- case <-ctx.Done():
218- return nil, ctx.Err()
219- }
220- }
221-
222- // Log at Info level for first attempt, Debug for retries
223- if attempt == 0 {
224- log.Info().
225- Str("provider", "ollama").
226- Str("model", p.model).
227- Msg("Ollama request started")
228- }
229-
230- httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
231- if err != nil {
232- return nil, err
233- }
234- httpReq.Header.Set("Content-Type", "application/json")
235-
236- resp, err := p.httpClient.Do(httpReq)
237- if err != nil {
238- // Do not retry on context cancellation or timeout
239- if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
240- return nil, err
241- }
242-
243- lastErr = err
244- continue
245- }
246-
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- if err := resp.Body.Close(); err != nil {
251- log.Warn().Err(err).Msg("Failed to close response body")
252- }
253- lastErr = fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
254-
255- log.Warn().
256- Str("provider", "ollama").
257- Int("status", resp.StatusCode).
258- Int("attempt", attempt+1).
259- Str("body", string(payload)).
260- Msg("Ollama retryable error")
261- continue
262- }
263-
264- if resp.StatusCode < 200 || resp.StatusCode >= 300 {
265- payload, _ := io.ReadAll(resp.Body)
266- if err := resp.Body.Close(); err != nil {
267- log.Warn().Err(err).Msg("Failed to close response body")
268- }
269-
270- log.Error().
271- Str("provider", "ollama").
272- Int("status", resp.StatusCode).
273- Str("body", string(payload)).
274- Msg("Ollama non-retryable error")
275-
276- return nil, fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
277- }
278-
279- bodyBytes, readErr := io.ReadAll(resp.Body)
280- if err := resp.Body.Close(); err != nil {
281- log.Warn().Err(err).Msg("Failed to close response body")
282- }
283- if readErr != nil {
284- return nil, fmt.Errorf("read response body: %w", readErr)
285- }
286-
287- var decoded chatCompletionResponse
288- if err := json.Unmarshal(bodyBytes, &decoded); err != nil {
289- return nil, fmt.Errorf("decode response: %w", err)
290- }
291-
292- // Log successful request at Info level for visibility
293- log.Info().
294- Str("provider", "ollama").
295- Str("model", p.model).
296- Int("status", resp.StatusCode).
297- Int("attempt", attempt+1).
298- Msg("Ollama request successful")
299-
300- return &decoded, nil
301- }
302-
303- log.Error().
304- Str("provider", "ollama").
305- Str("model", p.model).
306- Int("max_retries", maxRetries).
307- Int("total_attempts", maxRetries+1).
308- Err(lastErr).
309- Msg("Ollama request failed after all retries")
310- return nil, fmt.Errorf("request failed after %d retries: %w", maxRetries, lastErr)
311-}
312-
313-// Stream sends messages and returns a channel that streams response chunks.
314-func (p *OllamaProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) {
315- stream, err := p.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
316- Model: p.model,
317- Messages: toOpenAIMessages(messages),
318- Temperature: float32(p.temperature),
319- })
320- if err != nil {
321- return nil, err
322- }
323-
324- ch := make(chan StreamChunk)
325- go func() {
326- defer close(ch)
327- defer func() {
328- if err := stream.Close(); err != nil {
329- log.Warn().Err(err).Msg("Failed to close stream")
330- }
331- }()
332-
333- for {
334- resp, err := stream.Recv()
335- if errors.Is(err, io.EOF) {
336- ch <- StreamChunk{Done: true}
337- return
338- }
339- if err != nil {
340- ch <- StreamChunk{Err: err}
341- return
342- }
343-
344- if len(resp.Choices) > 0 {
345- ch <- StreamChunk{Content: resp.Choices[0].Delta.Content}
346- }
347- }
348- }()
349-
350- return ch, nil
351-}
352-
353120 // toOllamaMessages converts provider messages to Ollama's custom request format.
354121 //
355122 // OLLAMA-SPECIFIC: Uses custom ollamaReqMessage type instead of OpenAI SDK types.
internal/provider/openai_common.go+210−35
@@ -1,46 +1,234 @@
11 package provider
22
33 import (
4+ "bufio"
5+ "bytes"
6+ "context"
47 "encoding/json"
8+ "errors"
9+ "fmt"
10+ "io"
11+ "net/http"
512 "strings"
13+ "time"
614
715 "github.com/rs/zerolog/log"
816 openai "github.com/sashabaranov/go-openai"
917 )
1018
11-// OpenAI-compliant response types for providers that follow OpenAI Chat Completions API spec.
12-// These types should NOT include provider-specific extensions.
19+// SSE streaming delta types for the OpenAI Chat Completions streaming format.
1320
14-type openaiChatResponse struct {
15- Choices []openaiChatChoice `json:"choices"`
21+type chatCompletionStreamResponse struct {
22+ Choices []chatCompletionStreamChoice `json:"choices"`
1623 }
1724
18-type openaiChatChoice struct {
19- Message openaiChatMessage `json:"message"`
25+type chatCompletionStreamChoice struct {
26+ Delta chatCompletionStreamDelta `json:"delta"`
27+ FinishReason *string `json:"finish_reason"`
2028 }
2129
22-type openaiChatMessage struct {
23- Role string `json:"role"`
24- Content string `json:"content"`
25- ToolCalls []openaiChatToolCall `json:"tool_calls,omitempty"`
30+type chatCompletionStreamDelta struct {
31+ Role string `json:"role,omitempty"`
32+ Content string `json:"content,omitempty"`
33+ Reasoning string `json:"reasoning,omitempty"`
34+ ReasoningContent string `json:"reasoning_content,omitempty"`
35+ ToolCalls []chatCompletionToolCall `json:"tool_calls,omitempty"`
2636 }
2737
28-type openaiChatToolCall struct {
29- ID string `json:"id"`
30- Type string `json:"type"`
31- Function openaiChatFunction `json:"function"`
38+type chatCompletionToolCall struct {
39+ Index int `json:"index"`
40+ ID string `json:"id"`
41+ Type string `json:"type"`
42+ Function chatCompletionFunction `json:"function"`
3243 }
3344
34-type openaiChatFunction struct {
45+type chatCompletionFunction struct {
3546 Name string `json:"name"`
3647 Arguments string `json:"arguments"`
3748 }
3849
50+// httpRequestConfig holds the parameters for an HTTP SSE request.
51+type httpRequestConfig struct {
52+ client *http.Client
53+ url string
54+ body []byte
55+ headers map[string]string
56+ provider string // for logging
57+ model string // for logging
58+}
59+
60+// sseRetryDelays defines backoff for transient errors on the initial SSE connection.
61+var sseRetryDelays = []time.Duration{5 * time.Second, 10 * time.Second, 15 * time.Second}
62+
63+// httpDoSSE executes an HTTP POST for SSE streaming with retry on the initial
64+// connection. Returns the response body as an io.ReadCloser that the caller
65+// must close.
66+func httpDoSSE(ctx context.Context, cfg httpRequestConfig) (io.ReadCloser, error) {
67+ maxRetries := len(sseRetryDelays)
68+ var lastErr error
69+
70+ for attempt := 0; attempt <= maxRetries; attempt++ {
71+ if attempt > 0 {
72+ delay := sseRetryDelays[attempt-1]
73+ log.Warn().
74+ Str("provider", cfg.provider).
75+ Int("attempt", attempt).
76+ Dur("delay", delay).
77+ Msg("Retrying SSE connection after transient error")
78+ select {
79+ case <-time.After(delay):
80+ case <-ctx.Done():
81+ return nil, ctx.Err()
82+ }
83+ }
84+
85+ if attempt == 0 {
86+ log.Info().
87+ Str("provider", cfg.provider).
88+ Str("model", cfg.model).
89+ Msg("SSE stream request started")
90+ }
91+
92+ httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.url, bytes.NewReader(cfg.body))
93+ if err != nil {
94+ return nil, err
95+ }
96+ httpReq.Header.Set("Content-Type", "application/json")
97+ httpReq.Header.Set("Accept", "text/event-stream")
98+ for k, v := range cfg.headers {
99+ httpReq.Header.Set(k, v)
100+ }
101+
102+ resp, err := cfg.client.Do(httpReq)
103+ if err != nil {
104+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
105+ return nil, err
106+ }
107+ lastErr = err
108+ continue
109+ }
110+
111+ // Retry on transient server errors
112+ if resp.StatusCode == 429 || resp.StatusCode == 500 || resp.StatusCode == 502 ||
113+ resp.StatusCode == 503 || resp.StatusCode == 504 {
114+ payload, _ := io.ReadAll(resp.Body)
115+ resp.Body.Close()
116+ lastErr = fmt.Errorf("stream request status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
117+ log.Warn().
118+ Str("provider", cfg.provider).
119+ Int("status", resp.StatusCode).
120+ Int("attempt", attempt+1).
121+ Msg("SSE retryable error")
122+ continue
123+ }
124+
125+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
126+ payload, _ := io.ReadAll(resp.Body)
127+ resp.Body.Close()
128+ return nil, fmt.Errorf("stream request status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
129+ }
130+
131+ return resp.Body, nil
132+ }
133+
134+ return nil, fmt.Errorf("SSE request failed after %d retries: %w", maxRetries, lastErr)
135+}
136+
137+// parseSSEStream reads SSE lines from a reader and sends parsed stream events on the channel.
138+// Returns when the stream ends, an error occurs, or ctx is cancelled.
139+// Caller must close the reader.
140+func parseSSEStream(ctx context.Context, reader io.Reader, ch chan<- StreamEvent) {
141+ scanner := bufio.NewScanner(reader)
142+ // 512KB buffer to handle large tool call arguments
143+ scanner.Buffer(make([]byte, 0, 64*1024), 512*1024)
144+
145+ for scanner.Scan() {
146+ line := scanner.Text()
147+
148+ if !strings.HasPrefix(line, "data: ") {
149+ continue
150+ }
151+ data := strings.TrimPrefix(line, "data: ")
152+
153+ if data == "[DONE]" {
154+ trySend(ctx, ch, StreamEvent{Type: EventDone})
155+ return
156+ }
157+
158+ var chunk chatCompletionStreamResponse
159+ if err := json.Unmarshal([]byte(data), &chunk); err != nil {
160+ log.Warn().Err(err).Str("data", data).Msg("Failed to parse SSE chunk")
161+ continue
162+ }
163+
164+ if len(chunk.Choices) == 0 {
165+ continue
166+ }
167+
168+ delta := chunk.Choices[0].Delta
169+
170+ // Emit reasoning delta
171+ reasoning := delta.Reasoning
172+ if reasoning == "" {
173+ reasoning = delta.ReasoningContent
174+ }
175+ if reasoning != "" {
176+ if !trySend(ctx, ch, StreamEvent{Type: EventReasoningDelta, Content: reasoning}) {
177+ return
178+ }
179+ }
180+
181+ // Emit content delta
182+ if delta.Content != "" {
183+ if !trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: delta.Content}) {
184+ return
185+ }
186+ }
187+
188+ // Emit tool call events
189+ for _, tc := range delta.ToolCalls {
190+ if tc.Function.Name != "" {
191+ if !trySend(ctx, ch, StreamEvent{
192+ Type: EventToolCallBegin,
193+ ToolCallIndex: tc.Index,
194+ ToolCallID: tc.ID,
195+ ToolCallName: tc.Function.Name,
196+ }) {
197+ return
198+ }
199+ }
200+ if tc.Function.Arguments != "" {
201+ if !trySend(ctx, ch, StreamEvent{
202+ Type: EventToolCallDelta,
203+ ToolCallIndex: tc.Index,
204+ ToolCallArgs: tc.Function.Arguments,
205+ }) {
206+ return
207+ }
208+ }
209+ }
210+ }
211+
212+ if err := scanner.Err(); err != nil {
213+ trySend(ctx, ch, StreamEvent{Type: EventError, Err: err})
214+ return
215+ }
216+
217+ // Stream ended without [DONE] marker
218+ trySend(ctx, ch, StreamEvent{Type: EventDone})
219+}
220+
221+// trySend sends an event on ch, aborting if ctx is cancelled. Returns false if cancelled.
222+func trySend(ctx context.Context, ch chan<- StreamEvent, evt StreamEvent) bool {
223+ select {
224+ case ch <- evt:
225+ return true
226+ case <-ctx.Done():
227+ return false
228+ }
229+}
230+
39231 // toOpenAIMessages converts provider-agnostic messages to OpenAI SDK message format.
40-// This function enforces OpenAI Chat Completions API requirements:
41-// - System messages must be first
42-// - User and assistant messages must alternate (as much as possible)
43-// - Tool messages must have tool_call_id and follow assistant messages with tool calls
44232 func toOpenAIMessages(messages []Message) []openai.ChatCompletionMessage {
45233 result := make([]openai.ChatCompletionMessage, len(messages))
46234 for i, m := range messages {
@@ -75,22 +263,11 @@ func toOpenAIMessages(messages []Message) []openai.ChatCompletionMessage {
75263 }
76264
77265 // mergeSystemMessagesOpenAI merges system messages intelligently while preserving conversation flow.
78-//
79-// Strategy:
80-// 1. Separate initial system messages (before any user/assistant messages)
81-// 2. Keep user/assistant conversation intact
82-// 3. Merge any mid-conversation system messages into the initial system prompt
83-//
84-// OpenAI requires:
85-// - System messages at the start
86-// - At least one non-system message
87-// - Proper user/assistant alternation
88266 func mergeSystemMessagesOpenAI(messages []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
89267 if len(messages) == 0 {
90268 return messages
91269 }
92270
93- // Separate system messages from conversation messages
94271 var systemMessages []string
95272 var conversationMessages []openai.ChatCompletionMessage
96273
@@ -102,10 +279,8 @@ func mergeSystemMessagesOpenAI(messages []openai.ChatCompletionMessage) []openai
102279 }
103280 }
104281
105- // Build result: merged system message + conversation
106282 result := make([]openai.ChatCompletionMessage, 0, len(messages))
107283
108- // Add merged system message if any system messages exist
109284 if len(systemMessages) > 0 {
110285 mergedSystem := strings.Join(systemMessages, "\n\n")
111286 result = append(result, openai.ChatCompletionMessage{
@@ -114,7 +289,6 @@ func mergeSystemMessagesOpenAI(messages []openai.ChatCompletionMessage) []openai
114289 })
115290 }
116291
117- // Add conversation messages
118292 result = append(result, conversationMessages...)
119293
120294 log.Debug().
Cut short: this commit's diff is over 1000 lines.
internal/provider/opencode.go+19−269
Cut short: this commit's diff is over 1000 lines.
internal/provider/provider.go+39−15
Cut short: this commit's diff is over 1000 lines.
internal/tui/tui.go+79−0
Cut short: this commit's diff is over 1000 lines.