xonecas.io
62159
Sy

symb / commit 0cfcba8

fix: remove broken opencode provider

xonecas2026-02-18 0cfcba8605dec323a36c4a41fa26bc12ec3ed3d3 parent 89e8084

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.

README.md+1−2
@@ -32,7 +32,7 @@ Symb pairs you with an LLM, providing an agentic UI with tools the LLM can use t
3232 - **Tree-sitter**: Tree sitter output once, to sped up filesystem awareness (10% less grepping/searching overall compensates for the initial token cost)
3333 - **Deterministic schemas**: KV-cache optimized tool definitions for reproducible LLM behavior
3434 - **Session storage**: Sessions saved to app data in sqlite file. Session management: `-c` continue, `-s <ID>` specific session, `-l` list sessions.
35-- **LLM integration**: Ollama local + Opencode Zen support
35+- **LLM integration**: Ollama local support
3636 - **ELM architecture**: Built with BubbleTea for solid state management
3737
3838 ## Syntax Theme
@@ -68,7 +68,6 @@ API keys are stored separately in `~/.config/symb/credentials.json`:
6868 ```json
6969 {
7070 "providers": {
71- "opencode_zen": { "api_key": "your-key" },
7271 "exa_ai": { "api_key": "your-exa-key" }
7372 }
7473 }
cmd/symb/main.go+3−19
@@ -64,10 +64,7 @@ func main() {
6464 providerName, providerCfg := resolveProvider(cfg, registry)
6565
6666 prov, err := registry.Create(providerName, providerCfg.Model, provider.Options{
67- Temperature: providerCfg.Temperature,
68- TopP: providerCfg.TopP,
69- RepeatPenalty: providerCfg.RepeatPenalty,
70- MaxTokens: providerCfg.MaxTokens,
67+ Temperature: providerCfg.Temperature,
7168 })
7269 if err != nil {
7370 fmt.Printf("Error creating provider: %v\n", err)
@@ -150,23 +147,10 @@ func main() {
150147 }
151148 }
152149
153-func buildRegistry(cfg *config.Config, creds *config.Credentials) *provider.Registry {
150+func buildRegistry(cfg *config.Config, _ *config.Credentials) *provider.Registry {
154151 registry := provider.NewRegistry()
155152 for name, providerCfg := range cfg.Providers {
156- var factory provider.Factory
157- switch providerCfg.Type {
158- case "openai":
159- factory = provider.NewVLLMFactory(name, providerCfg.Endpoint, creds.GetAPIKey(providerCfg.APIKeyName))
160- case "ollama":
161- factory = provider.NewOllamaFactory(name, providerCfg.Endpoint)
162- default:
163- if providerCfg.APIKeyName != "" {
164- factory = provider.NewOpenCodeFactory(name, providerCfg.Endpoint, creds.GetAPIKey(providerCfg.APIKeyName))
165- } else {
166- factory = provider.NewOllamaFactory(name, providerCfg.Endpoint)
167- }
168- }
169- registry.RegisterFactory(name, factory)
153+ registry.RegisterFactory(name, provider.NewOllamaFactory(name, providerCfg.Endpoint))
170154 }
171155 return registry
172156 }
config.toml+2−74
@@ -1,87 +1,15 @@
11 # Symb Configuration File
22
33 # Default provider (optional - if not set, first provider in map is used)
4-default_provider = "zen-codex"
4+default_provider = "ollama-qwen"
55
66 # Ollama providers (local)
77 [providers.ollama-qwen]
88 endpoint = "http://localhost:11434"
99 model = "qwen3:8b"
10-#temperature = 0.2
11-
12-[providers.ollama-qwen-small]
13-endpoint = "http://localhost:11434"
14-model = "qwen3:4b"
15-#temperature = 0.2
16-
17-# OpenCode Zen providers (cloud)
18-[providers.zen-nano]
19-endpoint = "https://opencode.ai/zen/v1"
20-model = "gpt-5-nano"
21-api_key_name = "opencode_zen"
22-
23-[providers.zen-pickle]
24-endpoint = "https://opencode.ai/zen/v1"
25-model = "big-pickle"
26-api_key_name = "opencode_zen"
27-
28-[providers.zen-codex]
29-endpoint = "https://opencode.ai/zen/v1"
30-model = "gpt-5.2-codex"
31-api_key_name = "opencode_zen"
32-
33-[providers.zen-opus]
34-endpoint = "https://opencode.ai/zen/v1"
35-model = "claude-opus-4-6"
36-api_key_name = "opencode_zen"
37-
38-[providers.zen-sonnet]
39-endpoint = "https://opencode.ai/zen/v1"
40-model = "claude-sonnet-4-5"
41-api_key_name = "opencode_zen"
42-
43-[providers.zen-kimi]
44-endpoint = "https://opencode.ai/zen/v1"
45-model = "kimi-k2.5"
46-api_key_name = "opencode_zen"
47-
48-[providers.zen-glm]
49-endpoint = "https://opencode.ai/zen/v1"
50-model = "glm-4.7"
51-api_key_name = "opencode_zen"
52-
53-# Error: LLM stream failed: stream request status 401: {"type":"error","error":{"type":"AuthError","message":"Missing API key."}}
54-# Well fuck.
55-[providers.zen-gemini]
56-endpoint = "https://opencode.ai/zen/v1"
57-model = "gemini-3-pro"
58-api_key_name = "opencode_zen"
59-
60-# Local vLLM (OpenAI-compatible)
61-[providers.vllm-nanbeige]
62-endpoint = "http://localhost:8000/v1"
63-model = "Nanbeige/Nanbeige4.1-3B"
64-type = "openai"
65-temperature = 0.6
66-top_p = 0.95
67-repeat_penalty = 1.0
68-max_tokens = 131072
69-
70-
71-[mcp]
72-upstream = ""
7310
7411 [ui]
75-# TODO: Code needs to pull these constants.
76-# TODO: highlight color
77-# selection background
78-# link background
79-# grayscale used
80-# syntax theme
12+# TODO
8113
8214 [cache]
8315 ttl_hours = 24
84-
85-[git]
86-# TODO: When adding worktrees later
87-# enable_sandbox = false
docs/DESIGN.md+2−3
@@ -46,8 +46,7 @@ prepends to system prompt. Checks `~/.config/symb/AGENTS.md` too.
4646
4747 - **Ollama** — local, OpenAI-compatible `/v1` endpoint. Extracts reasoning from
4848 `reasoning`/`reasoning_content` fields.
49-- **OpenCode** — remote, API key auth. Model-specific endpoint routing.
50-- Both use SSE streaming with retry on initial connection (3 retries, 429/5xx).
49+- SSE streaming with retry on initial connection (3 retries, 429/5xx).
5150 - Single `ChatStream()` interface method replaces `Chat`/`ChatWithTools`/`Stream`.
5251 - Deterministic JSON tool schemas, provider kv cache support
5352
@@ -202,7 +201,7 @@ Testing, cleaning up.
202201 ### Community configs support:
203202
204203 - CLAUDE/AGENTS/CURSOR/ETC.md (or just AGENTS.md at this point?) -- Is there a standard?
205-- Custom subagent prompts for opencode/others custom agents
204+- Custom subagent prompts for custom agents
206205 - Commands and skills implementation to match community expectations
207206
208207 ### Agent input interactivity:
docs/EDIT_TOOLS.md+2−2
@@ -5,7 +5,7 @@
55 The standard approaches to LLM-driven file editing all share the same fundamental
66 weakness: they require the model to reproduce file content it already saw.
77
8-- **str_replace** (Claude Code, OpenCode, most agents) makes the model reproduce
8+- **str_replace** (Claude Code, most agents) makes the model reproduce
99 the exact old text including whitespace. Multiple matches or slight misrecall
1010 cause failures. The "string not found" error is one of the most common complaints.
1111
@@ -199,4 +199,4 @@ insert, delete, create, create-fails-if-exists, hash mismatch rejection, no
199199 operation, multiple operations, path traversal rejection, read-before-edit
200200 enforcement, and create bypassing the read check.
201201
202-References: https://blog.can.ac/2026/02/12/the-harness-problem/ and the opencode source code.
202+References: https://blog.can.ac/2026/02/12/the-harness-problem/
internal/config/config.go+3−27
@@ -34,14 +34,9 @@ func (c CacheConfig) CacheTTLOrDefault() int {
3434
3535 // ProviderConfig holds LLM provider settings.
3636 type ProviderConfig struct {
37- Endpoint string `toml:"endpoint"`
38- Model string `toml:"model"`
39- APIKeyName string `toml:"api_key_name"`
40- Type string `toml:"type"`
41- Temperature float64 `toml:"temperature"`
42- TopP float64 `toml:"top_p"`
43- RepeatPenalty float64 `toml:"repeat_penalty"`
44- MaxTokens int `toml:"max_tokens"`
37+ Endpoint string `toml:"endpoint"`
38+ Model string `toml:"model"`
39+ Temperature float64 `toml:"temperature"`
4540 }
4641
4742 // MCPConfig holds MCP proxy settings.
@@ -119,29 +114,10 @@ func validateProviderConfig(name string, cfg ProviderConfig) []error {
119114 errs = append(errs, fmt.Errorf("providers.%s.model is required", name))
120115 }
121116
122- switch cfg.Type {
123- case "", "openai", "ollama":
124- // allowed
125- default:
126- errs = append(errs, fmt.Errorf("providers.%s.type=%q is invalid", name, cfg.Type))
127- }
128-
129117 if cfg.Temperature < 0.0 || cfg.Temperature > 2.0 {
130118 errs = append(errs, fmt.Errorf("providers.%s.temperature=%v must be between 0.0 and 2.0", name, cfg.Temperature))
131119 }
132120
133- if cfg.TopP < 0.0 || cfg.TopP > 1.0 {
134- errs = append(errs, fmt.Errorf("providers.%s.top_p=%v must be between 0.0 and 1.0", name, cfg.TopP))
135- }
136-
137- if cfg.RepeatPenalty < 0.0 {
138- errs = append(errs, fmt.Errorf("providers.%s.repeat_penalty=%v must be 0.0 or higher", name, cfg.RepeatPenalty))
139- }
140-
141- if cfg.MaxTokens < 0 {
142- errs = append(errs, fmt.Errorf("providers.%s.max_tokens=%v must be 0 or higher", name, cfg.MaxTokens))
143- }
144-
145121 return errs
146122 }
147123
internal/provider/anthropic.go+0−365
@@ -1,365 +0,0 @@
1-package provider
2-
3-import (
4- "bufio"
5- "context"
6- "encoding/json"
7- "io"
8- "strings"
9-
10- "github.com/rs/zerolog/log"
11-)
12-
13-const roleSystem = "system"
14-
15-// Anthropic Messages API request types.
16-
17-type anthropicRequest struct {
18- Model string `json:"model"`
19- Messages []anthropicMessage `json:"messages"`
20- System []anthropicCacheBlock `json:"system,omitempty"`
21- MaxTokens int `json:"max_tokens"`
22- Temperature float64 `json:"temperature,omitempty"`
23- Stream bool `json:"stream"`
24- Tools []anthropicTool `json:"tools,omitempty"`
25-}
26-
27-// anthropicCacheControl marks a block for prompt caching.
28-type anthropicCacheControl struct {
29- Type string `json:"type"` // "ephemeral"
30-}
31-
32-// anthropicCacheBlock is a system prompt content block with optional cache_control.
33-type anthropicCacheBlock struct {
34- Type string `json:"type"` // "text"
35- Text string `json:"text"`
36- CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
37-}
38-
39-type anthropicMessage struct {
40- Role string `json:"role"`
41- Content interface{} `json:"content"` // string or []anthropicContentBlock
42-}
43-
44-// anthropicTextBlock is a "text" content block.
45-type anthropicTextBlock struct {
46- Type string `json:"type"` // "text"
47- Text string `json:"text"`
48-}
49-
50-// anthropicToolUseBlock is a "tool_use" content block.
51-type anthropicToolUseBlock struct {
52- Type string `json:"type"` // "tool_use"
53- ID string `json:"id"`
54- Name string `json:"name"`
55- Input json.RawMessage `json:"input"`
56-}
57-
58-// anthropicToolResultBlock is a "tool_result" content block.
59-type anthropicToolResultBlock struct {
60- Type string `json:"type"` // "tool_result"
61- ToolUseID string `json:"tool_use_id"`
62- Content string `json:"content"`
63-}
64-
65-type anthropicTool struct {
66- Name string `json:"name"`
67- Description string `json:"description,omitempty"`
68- InputSchema json.RawMessage `json:"input_schema"`
69- CacheControl *anthropicCacheControl `json:"cache_control,omitempty"`
70-}
71-
72-// Anthropic SSE streaming response types.
73-
74-// anthropicMessageStart wraps the message_start event payload.
75-type anthropicMessageStart struct {
76- Message struct {
77- Usage struct {
78- InputTokens int `json:"input_tokens"`
79- OutputTokens int `json:"output_tokens"`
80- } `json:"usage"`
81- } `json:"message"`
82-}
83-
84-// anthropicMessageDelta wraps the message_delta event payload.
85-type anthropicMessageDelta struct {
86- Usage struct {
87- OutputTokens int `json:"output_tokens"`
88- } `json:"usage"`
89-}
90-
91-type anthropicContentBlockStart struct {
92- Type string `json:"type"`
93- Index int `json:"index"`
94- ContentBlock struct {
95- Type string `json:"type"` // "text" or "tool_use"
96- Text string `json:"text,omitempty"`
97- ID string `json:"id,omitempty"`
98- Name string `json:"name,omitempty"`
99- } `json:"content_block"`
100-}
101-
102-type anthropicContentBlockDelta struct {
103- Type string `json:"type"`
104- Index int `json:"index"`
105- Delta struct {
106- Type string `json:"type"` // "text_delta", "thinking_delta", "input_json_delta", "signature_delta"
107- Text string `json:"text,omitempty"`
108- Thinking string `json:"thinking,omitempty"`
109- PartialJSON string `json:"partial_json,omitempty"`
110- } `json:"delta"`
111-}
112-
113-// toAnthropicMessages converts provider-agnostic messages to Anthropic Messages API format.
114-// Returns (system blocks, messages) — system is extracted and hoisted out.
115-// The last system block gets cache_control for prompt caching.
116-func toAnthropicMessages(messages []Message) ([]anthropicCacheBlock, []anthropicMessage) {
117- var systemParts []string
118- var result []anthropicMessage
119-
120- for _, m := range messages {
121- if m.Role == roleSystem {
122- systemParts = append(systemParts, m.Content)
123- continue
124- }
125-
126- if m.Role == "tool" {
127- // Tool results become user messages with tool_result content blocks
128- result = append(result, anthropicMessage{
129- Role: "user",
130- Content: []anthropicToolResultBlock{
131- {
132- Type: "tool_result",
133- ToolUseID: m.ToolCallID,
134- Content: m.Content,
135- },
136- },
137- })
138- continue
139- }
140-
141- if m.Role == "assistant" && len(m.ToolCalls) > 0 {
142- // Assistant message with tool calls
143- var blocks []interface{}
144- if m.Content != "" {
145- blocks = append(blocks, anthropicTextBlock{
146- Type: "text",
147- Text: m.Content,
148- })
149- }
150- for _, tc := range m.ToolCalls {
151- input := tc.Arguments
152- if len(input) == 0 {
153- input = json.RawMessage(`{}`)
154- }
155- blocks = append(blocks, anthropicToolUseBlock{
156- Type: "tool_use",
157- ID: tc.ID,
158- Name: tc.Name,
159- Input: input,
160- })
161- }
162- result = append(result, anthropicMessage{
163- Role: "assistant",
164- Content: blocks,
165- })
166- continue
167- }
168-
169- // Simple text message
170- result = append(result, anthropicMessage{
171- Role: m.Role,
172- Content: m.Content,
173- })
174- }
175-
176- var system []anthropicCacheBlock
177- if len(systemParts) > 0 {
178- system = make([]anthropicCacheBlock, len(systemParts))
179- for i, part := range systemParts {
180- system[i] = anthropicCacheBlock{Type: "text", Text: part}
181- }
182- // Mark last system block for prompt caching.
183- system[len(system)-1].CacheControl = &anthropicCacheControl{Type: "ephemeral"}
184- }
185- return system, result
186-}
187-
188-// toAnthropicTools converts provider-agnostic tools to Anthropic tool format.
189-// InputSchema is passed through as json.RawMessage to preserve deterministic
190-// serialization order (important for KV-cache hit rate).
191-func toAnthropicTools(tools []Tool) []anthropicTool {
192- if tools == nil {
193- return nil
194- }
195- emptySchema := json.RawMessage(`{"type":"object","properties":{}}`)
196- result := make([]anthropicTool, len(tools))
197- for i, t := range tools {
198- schema := t.Parameters
199- if len(schema) == 0 {
200- schema = emptySchema
201- }
202- result[i] = anthropicTool{
203- Name: t.Name,
204- Description: t.Description,
205- InputSchema: schema,
206- }
207- }
208- // Mark last tool for prompt caching. Anthropic caches the prefix up to
209- // and including blocks with cache_control, so tools + system form a
210- // stable cached prefix across turns.
211- if len(result) > 0 {
212- result[len(result)-1].CacheControl = &anthropicCacheControl{Type: "ephemeral"}
213- }
214- return result
215-}
216-
217-// parseAnthropicSSEStream reads Anthropic Messages API SSE events and emits StreamEvents.
218-//
219-// Anthropic SSE format:
220-//
221-// event: message_start / content_block_start / content_block_delta /
222-// content_block_stop / message_delta / message_stop / ping
223-// data: { JSON payload }
224-//
225-// anthropicBlockTracker maps Anthropic block indices to tool call indices.
226-type anthropicBlockTracker struct {
227- toolCallCount int
228- blockIsToolUse map[int]bool
229- blockToolIndex map[int]int
230-}
231-
232-func newAnthropicBlockTracker() *anthropicBlockTracker {
233- return &anthropicBlockTracker{
234- blockIsToolUse: make(map[int]bool),
235- blockToolIndex: make(map[int]int),
236- }
237-}
238-
239-func parseAnthropicSSEStream(ctx context.Context, reader io.Reader, ch chan<- StreamEvent) {
240- scanner := bufio.NewScanner(reader)
241- scanner.Buffer(make([]byte, 0, 64*1024), 512*1024)
242-
243- bt := newAnthropicBlockTracker()
244- var currentEventType string
245-
246- for scanner.Scan() {
247- line := scanner.Text()
248-
249- if strings.HasPrefix(line, "event: ") {
250- currentEventType = strings.TrimPrefix(line, "event: ")
251- continue
252- }
253- if !strings.HasPrefix(line, "data: ") {
254- continue
255- }
256- data := strings.TrimPrefix(line, "data: ")
257-
258- switch currentEventType {
259- case "message_stop":
260- trySend(ctx, ch, StreamEvent{Type: EventDone})
261- return
262- case "content_block_start":
263- if !bt.handleBlockStart(ctx, ch, data) {
264- return
265- }
266- case "content_block_delta":
267- if !bt.handleBlockDelta(ctx, ch, data) {
268- return
269- }
270- case "message_start":
271- handleAnthropicMessageStart(ctx, ch, data)
272- case "message_delta":
273- handleAnthropicMessageDelta(ctx, ch, data)
274- case "ping", "content_block_stop":
275- // Ignored
276- }
277-
278- currentEventType = ""
279- }
280-
281- if err := scanner.Err(); err != nil {
282- trySend(ctx, ch, StreamEvent{Type: EventError, Err: err})
283- return
284- }
285- trySend(ctx, ch, StreamEvent{Type: EventDone})
286-}
287-
288-// handleBlockStart processes a content_block_start event. Returns false if ctx cancelled.
289-func (bt *anthropicBlockTracker) handleBlockStart(ctx context.Context, ch chan<- StreamEvent, data string) bool {
290- var evt anthropicContentBlockStart
291- if err := json.Unmarshal([]byte(data), &evt); err != nil {
292- log.Warn().Err(err).Msg("Failed to parse anthropic content_block_start")
293- return true // continue scanning
294- }
295- if evt.ContentBlock.Type != "tool_use" {
296- return true
297- }
298- idx := bt.toolCallCount
299- bt.toolCallCount++
300- bt.blockIsToolUse[evt.Index] = true
301- bt.blockToolIndex[evt.Index] = idx
302- return trySend(ctx, ch, StreamEvent{
303- Type: EventToolCallBegin,
304- ToolCallIndex: idx,
305- ToolCallID: evt.ContentBlock.ID,
306- ToolCallName: evt.ContentBlock.Name,
307- })
308-}
309-
310-// handleBlockDelta processes a content_block_delta event. Returns false if ctx cancelled.
311-func (bt *anthropicBlockTracker) handleBlockDelta(ctx context.Context, ch chan<- StreamEvent, data string) bool {
312- var evt anthropicContentBlockDelta
313- if err := json.Unmarshal([]byte(data), &evt); err != nil {
314- log.Warn().Err(err).Msg("Failed to parse anthropic content_block_delta")
315- return true
316- }
317- switch evt.Delta.Type {
318- case "text_delta":
319- if evt.Delta.Text != "" {
320- return trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: evt.Delta.Text})
321- }
322- case "thinking_delta":
323- if evt.Delta.Thinking != "" {
324- return trySend(ctx, ch, StreamEvent{Type: EventReasoningDelta, Content: evt.Delta.Thinking})
325- }
326- case "input_json_delta":
327- if evt.Delta.PartialJSON != "" && bt.blockIsToolUse[evt.Index] {
328- return trySend(ctx, ch, StreamEvent{
329- Type: EventToolCallDelta,
330- ToolCallIndex: bt.blockToolIndex[evt.Index],
331- ToolCallArgs: evt.Delta.PartialJSON,
332- })
333- }
334- }
335- return true
336-}
337-
338-// handleAnthropicMessageStart extracts input token usage from message_start events.
339-func handleAnthropicMessageStart(ctx context.Context, ch chan<- StreamEvent, data string) {
340- var ms anthropicMessageStart
341- if err := json.Unmarshal([]byte(data), &ms); err != nil {
342- return
343- }
344- if ms.Message.Usage.InputTokens > 0 || ms.Message.Usage.OutputTokens > 0 {
345- trySend(ctx, ch, StreamEvent{
346- Type: EventUsage,
347- InputTokens: ms.Message.Usage.InputTokens,
348- OutputTokens: ms.Message.Usage.OutputTokens,
349- })
350- }
351-}
352-
353-// handleAnthropicMessageDelta extracts output token usage from message_delta events.
354-func handleAnthropicMessageDelta(ctx context.Context, ch chan<- StreamEvent, data string) {
355- var md anthropicMessageDelta
356- if err := json.Unmarshal([]byte(data), &md); err != nil {
357- return
358- }
359- if md.Usage.OutputTokens > 0 {
360- trySend(ctx, ch, StreamEvent{
361- Type: EventUsage,
362- OutputTokens: md.Usage.OutputTokens,
363- })
364- }
365-}
internal/provider/factory.go+0−40
@@ -17,43 +17,3 @@ func (f *OllamaFactory) Name() string { return f.name }
1717 func (f *OllamaFactory) Create(model string, opts Options) Provider {
1818 return NewOllamaWithTemp(f.name, f.endpoint, model, opts.Temperature)
1919 }
20-
21-type OpenCodeFactory struct {
22- name string
23- endpoint string
24- apiKey string
25-}
26-
27-func NewOpenCodeFactory(name string, endpoint, apiKey string) *OpenCodeFactory {
28- return &OpenCodeFactory{
29- name: name,
30- endpoint: endpoint,
31- apiKey: apiKey,
32- }
33-}
34-
35-func (f *OpenCodeFactory) Name() string { return f.name }
36-
37-func (f *OpenCodeFactory) Create(model string, opts Options) Provider {
38- return NewOpenCodeWithTemp(f.name, f.endpoint, model, f.apiKey, opts.Temperature)
39-}
40-
41-type VLLMFactory struct {
42- name string
43- endpoint string
44- apiKey string
45-}
46-
47-func NewVLLMFactory(name string, endpoint, apiKey string) *VLLMFactory {
48- return &VLLMFactory{
49- name: name,
50- endpoint: endpoint,
51- apiKey: apiKey,
52- }
53-}
54-
55-func (f *VLLMFactory) Name() string { return f.name }
56-
57-func (f *VLLMFactory) Create(model string, opts Options) Provider {
58- return NewVLLMWithTemp(f.name, f.endpoint, model, f.apiKey, opts)
59-}
internal/provider/ollama.go+231−36
@@ -1,15 +1,22 @@
11 package provider
22
33 import (
4+ "bufio"
5+ "bytes"
46 "context"
57 "encoding/json"
8+ "errors"
9+ "fmt"
10+ "io"
611 "net/http"
712 "strings"
13+ "time"
814
915 "github.com/rs/zerolog/log"
1016 )
1117
12-// OllamaProvider implements the Provider interface for Ollama.
18+const roleSystem = "system"
19+
1320 type OllamaProvider struct {
1421 name string
1522 baseURL string
@@ -18,8 +25,6 @@ type OllamaProvider struct {
1825 temperature float64
1926 }
2027
21-// NewOllama creates a new Ollama provider.
22-// Ollama exposes an OpenAI-compatible API at /v1.
2328 func NewOllama(endpoint, model string) *OllamaProvider {
2429 return NewOllamaWithTemp("ollama", endpoint, model, 0.7)
2530 }
@@ -36,16 +41,14 @@ func NewOllamaWithTemp(name string, endpoint, model string, temperature float64)
3641 }
3742 }
3843
39-// Name returns the provider identifier.
4044 func (p *OllamaProvider) Name() string {
4145 return p.name
4246 }
4347
44-// ChatStream sends messages with optional tools and returns a channel of streaming events.
4548 func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
4649 req := ollamaChatRequest{
4750 Model: p.model,
48- Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)),
51+ Messages: mergeConsecutiveSystemMessages(toOllamaMessages(messages)),
4952 Tools: toOllamaTools(tools),
5053 Temperature: float32(p.temperature),
5154 Stream: true,
@@ -60,7 +63,7 @@ func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, too
6063 client: p.httpClient,
6164 url: p.baseURL + "/chat/completions",
6265 body: body,
63- provider: "ollama",
66+ provider: p.name,
6467 model: p.model,
6568 })
6669 if err != nil {
@@ -77,9 +80,12 @@ func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, too
7780 return ch, nil
7881 }
7982
80-// OLLAMA-SPECIFIC TYPES
81-// These types are custom to Ollama and should NOT be used by other providers.
82-// They differ from OpenAI standard in structure and field names.
83+func (p *OllamaProvider) Close() error {
84+ if p.httpClient != nil {
85+ p.httpClient.CloseIdleConnections()
86+ }
87+ return nil
88+}
8389
8490 type ollamaChatRequest struct {
8591 Model string `json:"model"`
@@ -119,10 +125,6 @@ type ollamaReqFuncCall struct {
119125 Arguments string `json:"arguments"`
120126 }
121127
122-// toOllamaMessages converts provider messages to Ollama's custom request format.
123-//
124-// OLLAMA-SPECIFIC: Uses custom ollamaReqMessage type instead of OpenAI SDK types.
125-// Ollama has its own message format and tool call structure.
126128 func toOllamaMessages(messages []Message) []ollamaReqMessage {
127129 result := make([]ollamaReqMessage, len(messages))
128130 for i, m := range messages {
@@ -154,9 +156,6 @@ func toOllamaMessages(messages []Message) []ollamaReqMessage {
154156 return result
155157 }
156158
157-// toOllamaTools converts provider-agnostic tools to Ollama tool format.
158-// Parameters is passed through as json.RawMessage to preserve deterministic
159-// serialization order (important for KV-cache hit rate).
160159 func toOllamaTools(tools []Tool) []ollamaReqTool {
161160 emptyParams := json.RawMessage(`{"type":"object","properties":{}}`)
162161 result := make([]ollamaReqTool, len(tools))
@@ -178,15 +177,7 @@ func toOllamaTools(tools []Tool) []ollamaReqTool {
178177 return result
179178 }
180179
181-// mergeConsecutiveSystemMessagesOllama merges consecutive system messages into a single message.
182-//
183-// OLLAMA-SPECIFIC BEHAVIOR:
184-// Unlike OpenAI, Ollama allows system messages anywhere in the conversation.
185-// This function merges consecutive system messages IN PLACE without moving them to the start.
186-// This preserves Ollama's flexible system message handling.
187-//
188-// DO NOT use this function for OpenAI-compatible providers - use mergeSystemMessagesOpenAI instead.
189-func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaReqMessage {
180+func mergeConsecutiveSystemMessages(messages []ollamaReqMessage) []ollamaReqMessage {
190181 if len(messages) == 0 {
191182 return messages
192183 }
@@ -197,7 +188,6 @@ func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaR
197188
198189 for i, msg := range messages {
199190 if msg.Role == roleSystem {
200- // Start or continue system message accumulation
201191 if inSystemRun {
202192 systemBuffer.WriteString("\n\n")
203193 } else {
@@ -205,7 +195,6 @@ func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaR
205195 }
206196 systemBuffer.WriteString(msg.Content)
207197 } else {
208- // Flush accumulated system messages if any
209198 if inSystemRun {
210199 result = append(result, ollamaReqMessage{
211200 Role: roleSystem,
@@ -214,11 +203,9 @@ func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaR
214203 systemBuffer.Reset()
215204 inSystemRun = false
216205 }
217- // Add non-system message
218206 result = append(result, msg)
219207 }
220208
221- // Flush at end of array
222209 if i == len(messages)-1 && inSystemRun {
223210 result = append(result, ollamaReqMessage{
224211 Role: roleSystem,
@@ -230,15 +217,223 @@ func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaR
230217 log.Debug().
231218 Int("original_count", len(messages)).
232219 Int("merged_count", len(result)).
233- Msg("Ollama: Merged consecutive system messages")
220+ Msg("Merged consecutive system messages")
234221
235222 return result
236223 }
237224
238-// Close closes idle HTTP connections
239-func (p *OllamaProvider) Close() error {
240- if p.httpClient != nil {
241- p.httpClient.CloseIdleConnections()
225+type chatCompletionStreamResponse struct {
226+ Choices []chatCompletionStreamChoice `json:"choices"`
227+ Usage *chatCompletionUsage `json:"usage,omitempty"`
228+}
229+
230+type chatCompletionUsage struct {
231+ PromptTokens int `json:"prompt_tokens"`
232+ CompletionTokens int `json:"completion_tokens"`
233+}
234+
235+type chatStreamOptions struct {
236+ IncludeUsage bool `json:"include_usage"`
237+}
238+
239+type chatCompletionStreamChoice struct {
240+ Delta chatCompletionStreamDelta `json:"delta"`
241+ FinishReason *string `json:"finish_reason"`
242+}
243+
244+type chatCompletionStreamDelta struct {
245+ Role string `json:"role,omitempty"`
246+ Content string `json:"content,omitempty"`
247+ Reasoning string `json:"reasoning,omitempty"`
248+ ReasoningContent string `json:"reasoning_content,omitempty"`
249+ ToolCalls []chatCompletionToolCall `json:"tool_calls,omitempty"`
250+}
251+
252+type chatCompletionToolCall struct {
253+ Index int `json:"index"`
254+ ID string `json:"id"`
255+ Type string `json:"type"`
256+ Function chatCompletionFunction `json:"function"`
257+}
258+
259+type chatCompletionFunction struct {
260+ Name string `json:"name"`
261+ Arguments string `json:"arguments"`
262+}
263+
264+type httpRequestConfig struct {
265+ client *http.Client
266+ url string
267+ body []byte
268+ headers map[string]string
269+ provider string
270+ model string
271+}
272+
273+var sseRetryDelays = []time.Duration{5 * time.Second, 10 * time.Second, 15 * time.Second}
274+
275+func httpDoSSE(ctx context.Context, cfg httpRequestConfig) (io.ReadCloser, error) {
276+ maxRetries := len(sseRetryDelays)
277+ var lastErr error
278+
279+ for attempt := 0; attempt <= maxRetries; attempt++ {
280+ if err := sseRetryWait(ctx, cfg, attempt); err != nil {
281+ return nil, err
282+ }
283+
284+ body, err, retry := sseAttempt(ctx, cfg, attempt)
285+ if err != nil {
286+ return nil, err
287+ }
288+ if retry != nil {
289+ lastErr = retry
290+ continue
291+ }
292+ return body, nil
293+ }
294+
295+ return nil, fmt.Errorf("SSE request failed after %d retries: %w", maxRetries, lastErr)
296+}
297+
298+func sseRetryWait(ctx context.Context, cfg httpRequestConfig, attempt int) error {
299+ if attempt == 0 {
300+ log.Info().Str("provider", cfg.provider).Str("model", cfg.model).Msg("SSE stream request started")
301+ return nil
302+ }
303+ delay := sseRetryDelays[attempt-1]
304+ log.Warn().Str("provider", cfg.provider).Int("attempt", attempt).Dur("delay", delay).Msg("Retrying SSE connection after transient error")
305+ select {
306+ case <-time.After(delay):
307+ return nil
308+ case <-ctx.Done():
309+ return ctx.Err()
310+ }
311+}
312+
313+func isTransientStatus(code int) bool {
314+ return code == 429 || code == 500 || code == 502 || code == 503 || code == 504
315+}
316+
317+func sseAttempt(ctx context.Context, cfg httpRequestConfig, attempt int) (io.ReadCloser, error, error) {
318+ httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.url, bytes.NewReader(cfg.body))
319+ if err != nil {
320+ return nil, err, nil
321+ }
322+ httpReq.Header.Set("Content-Type", "application/json")
323+ httpReq.Header.Set("Accept", "text/event-stream")
324+ for k, v := range cfg.headers {
325+ httpReq.Header.Set(k, v)
326+ }
327+
328+ resp, err := cfg.client.Do(httpReq)
329+ if err != nil {
330+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
331+ return nil, err, nil
332+ }
333+ return nil, nil, err
334+ }
335+
336+ if isTransientStatus(resp.StatusCode) {
337+ payload, _ := io.ReadAll(resp.Body)
338+ resp.Body.Close()
339+ retryErr := fmt.Errorf("stream request status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
340+ log.Warn().Str("provider", cfg.provider).Int("status", resp.StatusCode).Int("attempt", attempt+1).Msg("SSE retryable error")
341+ return nil, nil, retryErr
342+ }
343+
344+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
345+ payload, _ := io.ReadAll(resp.Body)
346+ resp.Body.Close()
347+ return nil, fmt.Errorf("stream request status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload))), nil
348+ }
349+
350+ return resp.Body, nil, nil
351+}
352+
353+func parseSSEStream(ctx context.Context, reader io.Reader, ch chan<- StreamEvent) {
354+ scanner := bufio.NewScanner(reader)
355+ scanner.Buffer(make([]byte, 0, 64*1024), 512*1024)
356+
357+ for scanner.Scan() {
358+ line := scanner.Text()
359+ if !strings.HasPrefix(line, "data: ") {
360+ continue
361+ }
362+ data := strings.TrimPrefix(line, "data: ")
363+ if data == "[DONE]" {
364+ trySend(ctx, ch, StreamEvent{Type: EventDone})
365+ return
366+ }
367+
368+ var chunk chatCompletionStreamResponse
369+ if err := json.Unmarshal([]byte(data), &chunk); err != nil {
370+ log.Warn().Err(err).Str("data", data).Msg("Failed to parse SSE chunk")
371+ continue
372+ }
373+ if chunk.Usage != nil {
374+ trySend(ctx, ch, StreamEvent{
375+ Type: EventUsage,
376+ InputTokens: chunk.Usage.PromptTokens,
377+ OutputTokens: chunk.Usage.CompletionTokens,
378+ })
379+ }
380+ if len(chunk.Choices) == 0 {
381+ continue
382+ }
383+
384+ if !emitDelta(ctx, ch, chunk.Choices[0].Delta) {
385+ return
386+ }
387+ }
388+
389+ if err := scanner.Err(); err != nil {
390+ trySend(ctx, ch, StreamEvent{Type: EventError, Err: err})
391+ return
392+ }
393+ trySend(ctx, ch, StreamEvent{Type: EventDone})
394+}
395+
396+func emitDelta(ctx context.Context, ch chan<- StreamEvent, delta chatCompletionStreamDelta) bool {
397+ reasoning := delta.Reasoning
398+ if reasoning == "" {
399+ reasoning = delta.ReasoningContent
400+ }
401+ if reasoning != "" {
402+ if !trySend(ctx, ch, StreamEvent{Type: EventReasoningDelta, Content: reasoning}) {
403+ return false
404+ }
405+ }
406+ if delta.Content != "" {
407+ if !trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: delta.Content}) {
408+ return false
409+ }
410+ }
411+ for _, tc := range delta.ToolCalls {
412+ if tc.Function.Name != "" {
413+ if !trySend(ctx, ch, StreamEvent{
414+ Type: EventToolCallBegin, ToolCallIndex: tc.Index,
415+ ToolCallID: tc.ID, ToolCallName: tc.Function.Name,
416+ }) {
417+ return false
418+ }
419+ }
420+ if tc.Function.Arguments != "" {
421+ if !trySend(ctx, ch, StreamEvent{
422+ Type: EventToolCallDelta, ToolCallIndex: tc.Index,
423+ ToolCallArgs: tc.Function.Arguments,
424+ }) {
425+ return false
426+ }
427+ }
428+ }
429+ return true
430+}
431+
Cut short: this commit's diff is over 1000 lines.
internal/provider/openai_common.go+0−677
Cut short: this commit's diff is over 1000 lines.
internal/provider/opencode.go+0−242
Cut short: this commit's diff is over 1000 lines.
internal/provider/provider.go+1−4
Cut short: this commit's diff is over 1000 lines.
internal/provider/vllm.go+0−117
Cut short: this commit's diff is over 1000 lines.
internal/tui/editor/editor_test.go+1−1
Cut short: this commit's diff is over 1000 lines.
scripts/vllm_nanbeige.sh+0−24
Cut short: this commit's diff is over 1000 lines.