v0.2.30 798174c

openai.go RawHistoryFolder

1package provider
2
3import (
4	"bytes"
5	"context"
6	"encoding/base64"
7	"encoding/json"
8	"fmt"
9	"io"
10	"net/http"
11	"strings"
12	"time"
13)
14
15const openaiDefaultBaseURL = "https://api.openai.com"
16
17type openaiClient struct {
18	apiKey  string
19	baseURL string
20	name    string
21	oauth   bool // when true, apiKey actually holds an OAuth access token
22	headers map[string]string
23	http    *http.Client
24}
25
26// NewOpenAI creates an OpenAI client using an API key. baseURL may be empty.
27func NewOpenAI(apiKey, baseURL string) Client {
28	if baseURL == "" {
29		baseURL = openaiDefaultBaseURL
30	}
31	return &openaiClient{
32		apiKey:  apiKey,
33		baseURL: strings.TrimRight(baseURL, "/"),
34		name:    "openai",
35		http:    &http.Client{Timeout: 0},
36	}
37}
38
39// NewOpenAIOAuth creates an OpenAI client using a subscription OAuth access token.
40// The token is sent as an HTTP Bearer credential on the standard chat/completions endpoint.
41func NewOpenAIOAuth(accessToken, baseURL string) Client {
42	if baseURL == "" {
43		baseURL = openaiDefaultBaseURL
44	}
45	return &openaiClient{
46		apiKey:  accessToken,
47		baseURL: strings.TrimRight(baseURL, "/"),
48		name:    "openai",
49		oauth:   true,
50		http:    &http.Client{Timeout: 0},
51	}
52}
53
54// NewKimi creates a Kimi/Moonshot client. Kimi's chat API is OpenAI-compatible.
55func NewKimi(apiKey, baseURL string) Client {
56	return NewKimiWithHeaders(apiKey, baseURL, nil)
57}
58
59// NewDeepSeek creates a DeepSeek client. DeepSeek's chat API is
60// OpenAI-compatible at https://api.deepseek.com/v1.
61func NewDeepSeek(apiKey, baseURL string) Client {
62	if baseURL == "" {
63		baseURL = "https://api.deepseek.com/v1"
64	}
65	return &openaiClient{
66		apiKey:  apiKey,
67		baseURL: strings.TrimRight(baseURL, "/"),
68		name:    "deepseek",
69		http:    &http.Client{Timeout: 0},
70	}
71}
72
73// NewKimiWithHeaders creates a Kimi/Moonshot client with extra headers.
74// Subscription tokens from Kimi Code need the official CLI's X-Msh-* headers.
75func NewKimiWithHeaders(apiKey, baseURL string, headers map[string]string) Client {
76	if baseURL == "" {
77		baseURL = "https://api.kimi.com/coding/v1"
78	}
79	return &openaiClient{
80		apiKey:  apiKey,
81		baseURL: strings.TrimRight(baseURL, "/"),
82		name:    "kimi",
83		headers: headers,
84		http:    &http.Client{Timeout: 0},
85	}
86}
87
88func (c *openaiClient) Name() string {
89	if c.name != "" {
90		return c.name
91	}
92	return "openai"
93}
94
95// ---- wire types ----
96
97type oaiContentText struct {
98	Type string `json:"type"` // "text"
99	Text string `json:"text"`
100}
101
102type oaiContentImage struct {
103	Type     string `json:"type"` // "image_url"
104	ImageURL struct {
105		URL string `json:"url"`
106	} `json:"image_url"`
107}
108
109type oaiToolCallFn struct {
110	Name      string `json:"name"`
111	Arguments string `json:"arguments"` // JSON string
112}
113
114type oaiToolCall struct {
115	ID       string        `json:"id"`
116	Type     string        `json:"type"` // "function"
117	Function oaiToolCallFn `json:"function"`
118}
119
120type oaiMessage struct {
121	Role       string        `json:"role"`
122	Content    interface{}   `json:"content,omitempty"` // string or []block
123	Name       string        `json:"name,omitempty"`
124	ToolCalls  []oaiToolCall `json:"tool_calls,omitempty"`
125	ToolCallID string        `json:"tool_call_id,omitempty"`
126	// ReasoningContent carries the model's chain-of-thought summary
127	// alongside an assistant tool-call message. Required by Kimi's
128	// chat completions endpoint when thinking is enabled and the
129	// assistant message contains a tool call; OpenAI ignores it.
130	ReasoningContent string `json:"reasoning_content,omitempty"`
131}
132
133type oaiTool struct {
134	Type     string `json:"type"` // "function"
135	Function struct {
136		Name        string          `json:"name"`
137		Description string          `json:"description,omitempty"`
138		Parameters  json.RawMessage `json:"parameters"`
139	} `json:"function"`
140}
141
142type oaiStreamOptions struct {
143	IncludeUsage bool `json:"include_usage"`
144}
145
146type oaiRequest struct {
147	Model            string            `json:"model"`
148	Messages         []oaiMessage      `json:"messages"`
149	Tools            []oaiTool         `json:"tools,omitempty"`
150	ToolChoice       string            `json:"tool_choice,omitempty"`
151	Stream           bool              `json:"stream"`
152	StreamOptions    *oaiStreamOptions `json:"stream_options,omitempty"`
153	Temperature      *float32          `json:"temperature,omitempty"`
154	MaxTokens        *int              `json:"max_tokens,omitempty"`
155	MaxCompletionTok *int              `json:"max_completion_tokens,omitempty"`
156	ReasoningEffort  string            `json:"reasoning_effort,omitempty"`
157}
158
159// ---- request building ----
160
161func (c *openaiClient) buildRequest(req Request) (*oaiRequest, error) {
162	// Look up the model by id across all providers (not just openai)
163	// because the OpenAI client is also used for ollama and other
164	// OpenAI-compatible backends.
165	m, err := FindModel("", req.Model)
166	if err != nil {
167		// Unknown model: use sensible defaults so local/custom
168		// models still work without a catalog entry.
169		m = Model{
170			ID:            req.Model,
171			ContextWindow: 32768,
172			MaxOutput:     8192,
173		}
174	}
175	out := &oaiRequest{
176		Model:         req.Model,
177		Stream:        true,
178		StreamOptions: &oaiStreamOptions{IncludeUsage: true},
179		Temperature:   req.Temperature,
180	}
181
182	maxTok := req.MaxTokens
183	if maxTok <= 0 {
184		maxTok = m.MaxOutput
185	}
186	// Clamp max_tokens so output plus a minimum input reservation fits
187	// within the context window. Some providers (OpenRouter) enforce
188	// input + max_output <= context_length and reject requests where the
189	// total exceeds it. Reserving headroom guarantees the system prompt,
190	// first message, and tool definitions have room.
191	//
192	// The cap is derived from the context window, never from MaxOutput:
193	// MaxOutput is already the output ceiling, so subtracting from it
194	// would shrink every model's budget even when its output comfortably
195	// fits the window. We only lower maxTok when ContextWindow - reserve
196	// is actually tighter than the requested budget, which is exactly the
197	// pathological case (e.g. a model whose MaxOutput equals its window).
198	//
199	// The reserve is proportional (window/8, capped at 4096) rather than a
200	// flat 4096 so small-window models aren't over-penalized: a flat 4096
201	// would halve gpt-4's 8192 budget, while window/8 reserves a sensible
202	// 1024 there and still tops out at 4096 for large contexts.
203	//
204	// Some providers (OpenRouter) report inflated model-level context
205	// windows (e.g. 1000000) while the serving provider enforces a much
206	// tighter limit (e.g. 262144). Discovery already prefers the serving
207	// provider's smaller context_length, so m.ContextWindow is the real
208	// limit by the time we get here.
209	if m.ContextWindow > 0 {
210		reserve := m.ContextWindow / 8
211		const maxReserve = 4096
212		if reserve > maxReserve {
213			reserve = maxReserve
214		}
215		clamped := m.ContextWindow - reserve
216		if clamped < 1 {
217			clamped = 1
218		}
219		if maxTok > clamped {
220			maxTok = clamped
221		}
222	}
223	if m.Reasoning {
224		if maxTok > 0 {
225			out.MaxCompletionTok = &maxTok
226		}
227		effort := OpenAIReasoningEffort(req.Reasoning)
228		if usesAdaptiveThinking(m) {
229			// Some gateways expose adaptive-thinking Anthropic models through
230			// the OpenAI-compatible chat-completions wire. They accept the
231			// same reasoning_effort knob, including the top "xhigh" tier;
232			// don't clamp zot's "maximum" to "high" for those models.
233			effort = OpenAICompatAnthropicEffort(req.Reasoning)
234		}
235		if effort != "" {
236			out.ReasoningEffort = effort
237		}
238	} else if maxTok > 0 {
239		// Omit max_tokens when unknown (some discovered models don't
240		// advertise an output cap) so the server applies its own default
241		// instead of receiving an invalid max_tokens: 0.
242		out.MaxTokens = &maxTok
243	}
244
245	if req.System != "" {
246		out.Messages = append(out.Messages, oaiMessage{Role: "system", Content: req.System})
247	}
248
249	// DeepSeek's chat-completions API rejects the multimodal content
250	// schema (parts arrays containing image_url). Force every user/tool
251	// message to a plain string and silently drop image blocks for
252	// this provider so historical sessions with screenshots still replay.
253	textOnly := c.name == "deepseek"
254
255	req.Messages = RepairOrphanedToolResults(req.Messages)
256	for _, msg := range req.Messages {
257		switch msg.Role {
258		case RoleUser:
259			content := buildOAIUserContent(msg.Content, textOnly)
260			out.Messages = append(out.Messages, oaiMessage{Role: "user", Content: content})
261		case RoleAssistant:
262			am := oaiMessage{Role: "assistant"}
263			var text strings.Builder
264			var reasoning strings.Builder
265			for _, b := range msg.Content {
266				switch v := b.(type) {
267				case TextBlock:
268					if strings.TrimSpace(v.Text) == "" {
269						continue
270					}
271					if text.Len() > 0 {
272						text.WriteString("\n")
273					}
274					text.WriteString(v.Text)
275				case ToolCallBlock:
276					args := v.Arguments
277					if len(args) == 0 || !json.Valid(args) {
278						args = json.RawMessage("{}")
279					}
280					am.ToolCalls = append(am.ToolCalls, oaiToolCall{
281						ID:   v.ID,
282						Type: "function",
283						Function: oaiToolCallFn{
284							Name:      v.Name,
285							Arguments: string(args),
286						},
287					})
288				case ReasoningBlock:
289					if v.Summary != "" {
290						if reasoning.Len() > 0 {
291							reasoning.WriteString("\n")
292						}
293						reasoning.WriteString(v.Summary)
294					}
295				}
296			}
297			if text.Len() > 0 {
298				am.Content = text.String()
299			}
300			if reasoning.Len() > 0 && len(am.ToolCalls) > 0 {
301				am.ReasoningContent = reasoning.String()
302			}
303			// Kimi rejects assistant messages with neither visible text nor
304			// tool calls ("assistant must not be empty"). This can happen when
305			// a previous stream produced only reasoning_content, which zot keeps
306			// internally for provider replay but cannot send back as standalone
307			// assistant content on OpenAI-compatible chat-completions APIs.
308			if am.Content == nil && len(am.ToolCalls) == 0 {
309				continue
310			}
311			out.Messages = append(out.Messages, am)
312		case RoleTool:
313			// Each ToolResultBlock becomes its own tool message. Preserve
314			// image blocks for vision-capable OpenAI models instead of
315			// flattening the tool output to plain text.
316			for _, b := range msg.Content {
317				if tr, ok := b.(ToolResultBlock); ok {
318					content := buildOAIToolContent(tr.Content, tr.IsError, textOnly)
319					out.Messages = append(out.Messages, oaiMessage{
320						Role:       "tool",
321						ToolCallID: tr.CallID,
322						Content:    content,
323					})
324				}
325			}
326		}
327	}
328
329	for _, t := range req.Tools {
330		var tool oaiTool
331		tool.Type = "function"
332		tool.Function.Name = t.Name
333		tool.Function.Description = t.Description
334		tool.Function.Parameters = t.Schema
335		out.Tools = append(out.Tools, tool)
336	}
337	if len(out.Tools) > 0 {
338		out.ToolChoice = "auto"
339	}
340
341	return out, nil
342}
343
344func buildOAIUserContent(blocks []Content, textOnly bool) interface{} {
345	hasImage := false
346	for _, b := range blocks {
347		if _, ok := b.(ImageBlock); ok {
348			hasImage = true
349			break
350		}
351	}
352	if textOnly || !hasImage {
353		var sb strings.Builder
354		for _, b := range blocks {
355			if tb, ok := b.(TextBlock); ok {
356				if sb.Len() > 0 {
357					sb.WriteString("\n")
358				}
359				sb.WriteString(tb.Text)
360			}
361		}
362		return sb.String()
363	}
364	return buildOAIContentBlocks(blocks, false)
365}
366
367func buildOAIToolContent(blocks []Content, isError, textOnly bool) interface{} {
368	hasImage := false
369	for _, b := range blocks {
370		if _, ok := b.(ImageBlock); ok {
371			hasImage = true
372			break
373		}
374	}
375	if textOnly || !hasImage {
376		var sb strings.Builder
377		for _, b := range blocks {
378			if tb, ok := b.(TextBlock); ok {
379				if sb.Len() > 0 {
380					sb.WriteString("\n")
381				}
382				sb.WriteString(tb.Text)
383			}
384		}
385		if isError && sb.Len() > 0 {
386			sb.WriteString(" [error]")
387		}
388		return sb.String()
389	}
390	return buildOAIContentBlocks(blocks, isError)
391}
392
393func buildOAIContentBlocks(blocks []Content, isError bool) []interface{} {
394	var arr []interface{}
395	for _, b := range blocks {
396		switch v := b.(type) {
397		case TextBlock:
398			arr = append(arr, oaiContentText{Type: "text", Text: v.Text})
399		case ImageBlock:
400			var img oaiContentImage
401			img.Type = "image_url"
402			img.ImageURL.URL = "data:" + v.MimeType + ";base64," + base64.StdEncoding.EncodeToString(v.Data)
403			arr = append(arr, img)
404		}
405	}
406	if isError {
407		arr = append(arr, oaiContentText{Type: "text", Text: "[error]"})
408	}
409	return arr
410}
411
412// ---- streaming ----
413
414func (c *openaiClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
415	apiPath := "/v1/chat/completions"
416	if strings.HasSuffix(c.baseURL, "/v1") {
417		apiPath = "/chat/completions"
418	}
419	wire, err := c.buildRequest(req)
420	if err != nil {
421		return nil, err
422	}
423	body, err := json.Marshal(wire)
424	if err != nil {
425		return nil, err
426	}
427	newReq := func() (*http.Request, error) {
428		httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+apiPath, bytes.NewReader(body))
429		if err != nil {
430			return nil, err
431		}
432		httpReq.Header.Set("content-type", "application/json")
433		httpReq.Header.Set("accept", "text/event-stream")
434		httpReq.Header.Set("authorization", "Bearer "+c.apiKey)
435		for k, v := range c.headers {
436			httpReq.Header.Set(k, v)
437		}
438		return httpReq, nil
439	}
440
441	resp, err := doStreamWithRetry(ctx, c.http, newReq)
442	if err != nil {
443		return nil, fmt.Errorf("%s: %w", c.Name(), err)
444	}
445	if resp.StatusCode != http.StatusOK {
446		b, _ := io.ReadAll(resp.Body)
447		resp.Body.Close()
448		return nil, fmt.Errorf("%s: http %d: %s", c.Name(), resp.StatusCode, strings.TrimSpace(string(b)))
449	}
450
451	out := make(chan Event, 16)
452	go c.runStream(ctx, resp, req, out)
453	return out, nil
454}
455
456func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
457	defer close(out)
458	defer resp.Body.Close()
459
460	model, _ := FindModel("", req.Model)
461	out <- EventStart{Model: req.Model, Provider: c.Name()}
462
463	raw := make(chan sseEvent, 16)
464	go readSSE(resp.Body, raw)
465
466	// Interleaved block tracking: text and tool_calls preserve their
467	// emission order so the assistant message renders in the same order
468	// the model produced it. The builder fires one kind of block at a
469	// time — incoming text deltas after a tool_call split into a fresh
470	// text block; subsequent tool_calls each get their own slot.
471	type blockEntry struct {
472		kind      string // "text" | "tool_use"
473		textBuf   strings.Builder
474		toolID    string
475		toolName  string
476		toolArgs  strings.Builder
477		announced bool
478	}
479	var (
480		blocks       []*blockEntry
481		currentText  *blockEntry             // most-recent text block, nil if none
482		toolByIdx    = map[int]*blockEntry{} // openai tool_call index -> block
483		reasoningBuf strings.Builder
484		usage        Usage
485		stop         StopReason = StopEnd
486		finalErr     error
487	)
488
489	appendText := func(delta string) {
490		if currentText == nil {
491			currentText = &blockEntry{kind: "text"}
492			blocks = append(blocks, currentText)
493		}
494		currentText.textBuf.WriteString(delta)
495	}
496
497	getOrCreateTool := func(idx int) *blockEntry {
498		if t, ok := toolByIdx[idx]; ok {
499			return t
500		}
501		t := &blockEntry{kind: "tool_use"}
502		toolByIdx[idx] = t
503		blocks = append(blocks, t)
504		// A new tool block breaks the current text block. Subsequent text
505		// deltas will start a fresh text block after this tool.
506		currentText = nil
507		return t
508	}
509
510	assembleMsg := func() Message {
511		content := []Content{}
512		for _, b := range blocks {
513			switch b.kind {
514			case "text":
515				if b.textBuf.Len() > 0 {
516					content = append(content, TextBlock{Text: b.textBuf.String()})
517				}
518			case "tool_use":
519				args := b.toolArgs.String()
520				if args == "" || !json.Valid([]byte(args)) {
521					args = "{}"
522				}
523				content = append(content, ToolCallBlock{
524					ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args),
525				})
526			}
527		}
528		if reasoningBuf.Len() > 0 {
529			content = append(content, ReasoningBlock{Summary: reasoningBuf.String()})
530		}
531		return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
532	}
533
534	sendDone := func() {
535		usage.CostUSD = ComputeCost(model, usage)
536		out <- EventUsage{Usage: usage}
537		out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
538	}
539
540	for {
541		select {
542		case <-ctx.Done():
543			stop = StopAborted
544			finalErr = ctx.Err()
545			sendDone()
546			return
547		case ev, ok := <-raw:
548			if !ok {
549				sendDone()
550				return
551			}
552			if ev.Data == "[DONE]" {
553				sendDone()
554				return
555			}
556			var chunk struct {
557				Choices []struct {
558					Index int `json:"index"`
559					Delta struct {
560						Content          string `json:"content"`
561						ReasoningContent string `json:"reasoning_content"`
562						ToolCalls        []struct {
563							Index    int    `json:"index"`
564							ID       string `json:"id"`
565							Type     string `json:"type"`
566							Function struct {
567								Name      string `json:"name"`
568								Arguments string `json:"arguments"`
569							} `json:"function"`
570						} `json:"tool_calls"`
571					} `json:"delta"`
572					FinishReason string `json:"finish_reason"`
573				} `json:"choices"`
574				Usage *struct {
575					PromptTokens        int `json:"prompt_tokens"`
576					CompletionTokens    int `json:"completion_tokens"`
577					PromptTokensDetails struct {
578						CachedTokens int `json:"cached_tokens"`
579					} `json:"prompt_tokens_details"`
580				} `json:"usage"`
581				Error *struct {
582					Message string `json:"message"`
583					Type    string `json:"type"`
584				} `json:"error"`
585			}
586			if err := json.Unmarshal([]byte(ev.Data), &chunk); err != nil {
587				continue
588			}
589			if chunk.Error != nil {
590				stop = StopError
591				finalErr = fmt.Errorf("openai: %s", chunk.Error.Message)
592				sendDone()
593				return
594			}
595			if chunk.Usage != nil {
596				usage.InputTokens = chunk.Usage.PromptTokens - chunk.Usage.PromptTokensDetails.CachedTokens
597				if usage.InputTokens < 0 {
598					usage.InputTokens = chunk.Usage.PromptTokens
599				}
600				usage.OutputTokens = chunk.Usage.CompletionTokens
601				usage.CacheReadTokens = chunk.Usage.PromptTokensDetails.CachedTokens
602			}
603			for _, ch := range chunk.Choices {
604				if ch.Delta.ReasoningContent != "" {
605					reasoningBuf.WriteString(ch.Delta.ReasoningContent)
606				}
607				if ch.Delta.Content != "" {
608					appendText(ch.Delta.Content)
609					out <- EventTextDelta{Delta: ch.Delta.Content}
610				}
611				for _, tc := range ch.Delta.ToolCalls {
612					t := getOrCreateTool(tc.Index)
613					if tc.ID != "" {
614						t.toolID = tc.ID
615					}
616					if tc.Function.Name != "" {
617						t.toolName = tc.Function.Name
618					}
619					if !t.announced && t.toolID != "" && t.toolName != "" {
620						t.announced = true
621						out <- EventToolStart{ID: t.toolID, Name: t.toolName}
622					}
623					if tc.Function.Arguments != "" {
624						t.toolArgs.WriteString(tc.Function.Arguments)
625						if t.announced {
626							out <- EventToolArgs{ID: t.toolID, Delta: tc.Function.Arguments}
627						}
628					}
629				}
630				switch ch.FinishReason {
631				case "stop":
632					stop = StopEnd
633				case "length":
634					stop = StopLength
635				case "tool_calls", "function_call":
636					stop = StopToolUse
637					for _, b := range blocks {
638						if b.kind == "tool_use" && b.announced {
639							out <- EventToolEnd{ID: b.toolID}
640						}
641					}
642				}
643			}
644		}
645	}
646}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 477

↑↓ moveenter openesc close