v0.1.66 9035106

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// NewKimiWithHeaders creates a Kimi/Moonshot client with extra headers.
60// Subscription tokens from Kimi Code need the official CLI's X-Msh-* headers.
61func NewKimiWithHeaders(apiKey, baseURL string, headers map[string]string) Client {
62	if baseURL == "" {
63		baseURL = "https://api.kimi.com/coding/v1"
64	}
65	return &openaiClient{
66		apiKey:  apiKey,
67		baseURL: strings.TrimRight(baseURL, "/"),
68		name:    "kimi",
69		headers: headers,
70		http:    &http.Client{Timeout: 0},
71	}
72}
73
74func (c *openaiClient) Name() string {
75	if c.name != "" {
76		return c.name
77	}
78	return "openai"
79}
80
81// ---- wire types ----
82
83type oaiContentText struct {
84	Type string `json:"type"` // "text"
85	Text string `json:"text"`
86}
87
88type oaiContentImage struct {
89	Type     string `json:"type"` // "image_url"
90	ImageURL struct {
91		URL string `json:"url"`
92	} `json:"image_url"`
93}
94
95type oaiToolCallFn struct {
96	Name      string `json:"name"`
97	Arguments string `json:"arguments"` // JSON string
98}
99
100type oaiToolCall struct {
101	ID       string        `json:"id"`
102	Type     string        `json:"type"` // "function"
103	Function oaiToolCallFn `json:"function"`
104}
105
106type oaiMessage struct {
107	Role       string        `json:"role"`
108	Content    interface{}   `json:"content,omitempty"` // string or []block
109	Name       string        `json:"name,omitempty"`
110	ToolCalls  []oaiToolCall `json:"tool_calls,omitempty"`
111	ToolCallID string        `json:"tool_call_id,omitempty"`
112	// ReasoningContent carries the model's chain-of-thought summary
113	// alongside an assistant tool-call message. Required by Kimi's
114	// chat completions endpoint when thinking is enabled and the
115	// assistant message contains a tool call; OpenAI ignores it.
116	ReasoningContent string `json:"reasoning_content,omitempty"`
117}
118
119type oaiTool struct {
120	Type     string `json:"type"` // "function"
121	Function struct {
122		Name        string          `json:"name"`
123		Description string          `json:"description,omitempty"`
124		Parameters  json.RawMessage `json:"parameters"`
125	} `json:"function"`
126}
127
128type oaiStreamOptions struct {
129	IncludeUsage bool `json:"include_usage"`
130}
131
132type oaiRequest struct {
133	Model            string            `json:"model"`
134	Messages         []oaiMessage      `json:"messages"`
135	Tools            []oaiTool         `json:"tools,omitempty"`
136	ToolChoice       string            `json:"tool_choice,omitempty"`
137	Stream           bool              `json:"stream"`
138	StreamOptions    *oaiStreamOptions `json:"stream_options,omitempty"`
139	Temperature      *float32          `json:"temperature,omitempty"`
140	MaxTokens        *int              `json:"max_tokens,omitempty"`
141	MaxCompletionTok *int              `json:"max_completion_tokens,omitempty"`
142	ReasoningEffort  string            `json:"reasoning_effort,omitempty"`
143}
144
145// ---- request building ----
146
147func (c *openaiClient) buildRequest(req Request) (*oaiRequest, error) {
148	// Look up the model by id across all providers (not just openai)
149	// because the OpenAI client is also used for ollama and other
150	// OpenAI-compatible backends.
151	m, err := FindModel("", req.Model)
152	if err != nil {
153		// Unknown model: use sensible defaults so local/custom
154		// models still work without a catalog entry.
155		m = Model{
156			ID:            req.Model,
157			ContextWindow: 32768,
158			MaxOutput:     8192,
159		}
160	}
161	out := &oaiRequest{
162		Model:         req.Model,
163		Stream:        true,
164		StreamOptions: &oaiStreamOptions{IncludeUsage: true},
165		Temperature:   req.Temperature,
166	}
167
168	maxTok := req.MaxTokens
169	if maxTok <= 0 {
170		maxTok = m.MaxOutput
171	}
172	if m.Reasoning {
173		out.MaxCompletionTok = &maxTok
174		if req.Reasoning != "" {
175			out.ReasoningEffort = strings.ToLower(req.Reasoning)
176		}
177	} else {
178		out.MaxTokens = &maxTok
179	}
180
181	if req.System != "" {
182		out.Messages = append(out.Messages, oaiMessage{Role: "system", Content: req.System})
183	}
184
185	req.Messages = RepairOrphanedToolResults(req.Messages)
186	for _, msg := range req.Messages {
187		switch msg.Role {
188		case RoleUser:
189			content := buildOAIUserContent(msg.Content)
190			out.Messages = append(out.Messages, oaiMessage{Role: "user", Content: content})
191		case RoleAssistant:
192			am := oaiMessage{Role: "assistant"}
193			var text strings.Builder
194			var reasoning strings.Builder
195			for _, b := range msg.Content {
196				switch v := b.(type) {
197				case TextBlock:
198					if strings.TrimSpace(v.Text) == "" {
199						continue
200					}
201					if text.Len() > 0 {
202						text.WriteString("\n")
203					}
204					text.WriteString(v.Text)
205				case ToolCallBlock:
206					args := v.Arguments
207					if len(args) == 0 || !json.Valid(args) {
208						args = json.RawMessage("{}")
209					}
210					am.ToolCalls = append(am.ToolCalls, oaiToolCall{
211						ID:   v.ID,
212						Type: "function",
213						Function: oaiToolCallFn{
214							Name:      v.Name,
215							Arguments: string(args),
216						},
217					})
218				case ReasoningBlock:
219					if v.Summary != "" {
220						if reasoning.Len() > 0 {
221							reasoning.WriteString("\n")
222						}
223						reasoning.WriteString(v.Summary)
224					}
225				}
226			}
227			if text.Len() > 0 {
228				am.Content = text.String()
229			}
230			if reasoning.Len() > 0 && len(am.ToolCalls) > 0 {
231				am.ReasoningContent = reasoning.String()
232			}
233			// Kimi rejects assistant messages with neither visible text nor
234			// tool calls ("assistant must not be empty"). This can happen when
235			// a previous stream produced only reasoning_content, which zot keeps
236			// internally for provider replay but cannot send back as standalone
237			// assistant content on OpenAI-compatible chat-completions APIs.
238			if am.Content == nil && len(am.ToolCalls) == 0 {
239				continue
240			}
241			out.Messages = append(out.Messages, am)
242		case RoleTool:
243			// Each ToolResultBlock becomes its own tool message. Preserve
244			// image blocks for vision-capable OpenAI models instead of
245			// flattening the tool output to plain text.
246			for _, b := range msg.Content {
247				if tr, ok := b.(ToolResultBlock); ok {
248					content := buildOAIToolContent(tr.Content, tr.IsError)
249					out.Messages = append(out.Messages, oaiMessage{
250						Role:       "tool",
251						ToolCallID: tr.CallID,
252						Content:    content,
253					})
254				}
255			}
256		}
257	}
258
259	for _, t := range req.Tools {
260		var tool oaiTool
261		tool.Type = "function"
262		tool.Function.Name = t.Name
263		tool.Function.Description = t.Description
264		tool.Function.Parameters = t.Schema
265		out.Tools = append(out.Tools, tool)
266	}
267	if len(out.Tools) > 0 {
268		out.ToolChoice = "auto"
269	}
270
271	return out, nil
272}
273
274func buildOAIUserContent(blocks []Content) interface{} {
275	hasImage := false
276	for _, b := range blocks {
277		if _, ok := b.(ImageBlock); ok {
278			hasImage = true
279			break
280		}
281	}
282	if !hasImage {
283		var sb strings.Builder
284		for _, b := range blocks {
285			if tb, ok := b.(TextBlock); ok {
286				if sb.Len() > 0 {
287					sb.WriteString("\n")
288				}
289				sb.WriteString(tb.Text)
290			}
291		}
292		return sb.String()
293	}
294	return buildOAIContentBlocks(blocks, false)
295}
296
297func buildOAIToolContent(blocks []Content, isError bool) interface{} {
298	hasImage := false
299	for _, b := range blocks {
300		if _, ok := b.(ImageBlock); ok {
301			hasImage = true
302			break
303		}
304	}
305	if !hasImage {
306		var sb strings.Builder
307		for _, b := range blocks {
308			if tb, ok := b.(TextBlock); ok {
309				if sb.Len() > 0 {
310					sb.WriteString("\n")
311				}
312				sb.WriteString(tb.Text)
313			}
314		}
315		if isError && sb.Len() > 0 {
316			sb.WriteString(" [error]")
317		}
318		return sb.String()
319	}
320	return buildOAIContentBlocks(blocks, isError)
321}
322
323func buildOAIContentBlocks(blocks []Content, isError bool) []interface{} {
324	var arr []interface{}
325	for _, b := range blocks {
326		switch v := b.(type) {
327		case TextBlock:
328			arr = append(arr, oaiContentText{Type: "text", Text: v.Text})
329		case ImageBlock:
330			var img oaiContentImage
331			img.Type = "image_url"
332			img.ImageURL.URL = "data:" + v.MimeType + ";base64," + base64.StdEncoding.EncodeToString(v.Data)
333			arr = append(arr, img)
334		}
335	}
336	if isError {
337		arr = append(arr, oaiContentText{Type: "text", Text: "[error]"})
338	}
339	return arr
340}
341
342// ---- streaming ----
343
344func (c *openaiClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
345	apiPath := "/v1/chat/completions"
346	if strings.HasSuffix(c.baseURL, "/v1") {
347		apiPath = "/chat/completions"
348	}
349	wire, err := c.buildRequest(req)
350	if err != nil {
351		return nil, err
352	}
353	body, err := json.Marshal(wire)
354	if err != nil {
355		return nil, err
356	}
357	newReq := func() (*http.Request, error) {
358		httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+apiPath, bytes.NewReader(body))
359		if err != nil {
360			return nil, err
361		}
362		httpReq.Header.Set("content-type", "application/json")
363		httpReq.Header.Set("accept", "text/event-stream")
364		httpReq.Header.Set("authorization", "Bearer "+c.apiKey)
365		for k, v := range c.headers {
366			httpReq.Header.Set(k, v)
367		}
368		return httpReq, nil
369	}
370
371	resp, err := doStreamWithRetry(ctx, c.http, newReq)
372	if err != nil {
373		return nil, fmt.Errorf("%s: %w", c.Name(), err)
374	}
375	if resp.StatusCode != http.StatusOK {
376		b, _ := io.ReadAll(resp.Body)
377		resp.Body.Close()
378		return nil, fmt.Errorf("%s: http %d: %s", c.Name(), resp.StatusCode, strings.TrimSpace(string(b)))
379	}
380
381	out := make(chan Event, 16)
382	go c.runStream(ctx, resp, req, out)
383	return out, nil
384}
385
386func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
387	defer close(out)
388	defer resp.Body.Close()
389
390	model, _ := FindModel("", req.Model)
391	out <- EventStart{Model: req.Model, Provider: c.Name()}
392
393	raw := make(chan sseEvent, 16)
394	go readSSE(resp.Body, raw)
395
396	// Interleaved block tracking: text and tool_calls preserve their
397	// emission order so the assistant message renders in the same order
398	// the model produced it. The builder fires one kind of block at a
399	// time — incoming text deltas after a tool_call split into a fresh
400	// text block; subsequent tool_calls each get their own slot.
401	type blockEntry struct {
402		kind      string // "text" | "tool_use"
403		textBuf   strings.Builder
404		toolID    string
405		toolName  string
406		toolArgs  strings.Builder
407		announced bool
408	}
409	var (
410		blocks       []*blockEntry
411		currentText  *blockEntry             // most-recent text block, nil if none
412		toolByIdx    = map[int]*blockEntry{} // openai tool_call index -> block
413		reasoningBuf strings.Builder
414		usage        Usage
415		stop         StopReason = StopEnd
416		finalErr     error
417	)
418
419	appendText := func(delta string) {
420		if currentText == nil {
421			currentText = &blockEntry{kind: "text"}
422			blocks = append(blocks, currentText)
423		}
424		currentText.textBuf.WriteString(delta)
425	}
426
427	getOrCreateTool := func(idx int) *blockEntry {
428		if t, ok := toolByIdx[idx]; ok {
429			return t
430		}
431		t := &blockEntry{kind: "tool_use"}
432		toolByIdx[idx] = t
433		blocks = append(blocks, t)
434		// A new tool block breaks the current text block. Subsequent text
435		// deltas will start a fresh text block after this tool.
436		currentText = nil
437		return t
438	}
439
440	assembleMsg := func() Message {
441		content := []Content{}
442		for _, b := range blocks {
443			switch b.kind {
444			case "text":
445				if b.textBuf.Len() > 0 {
446					content = append(content, TextBlock{Text: b.textBuf.String()})
447				}
448			case "tool_use":
449				args := b.toolArgs.String()
450				if args == "" || !json.Valid([]byte(args)) {
451					args = "{}"
452				}
453				content = append(content, ToolCallBlock{
454					ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args),
455				})
456			}
457		}
458		if reasoningBuf.Len() > 0 {
459			content = append(content, ReasoningBlock{Summary: reasoningBuf.String()})
460		}
461		return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
462	}
463
464	sendDone := func() {
465		usage.CostUSD = ComputeCost(model, usage)
466		out <- EventUsage{Usage: usage}
467		out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
468	}
469
470	for {
471		select {
472		case <-ctx.Done():
473			stop = StopAborted
474			finalErr = ctx.Err()
475			sendDone()
476			return
477		case ev, ok := <-raw:
478			if !ok {
479				sendDone()
480				return
481			}
482			if ev.Data == "[DONE]" {
483				sendDone()
484				return
485			}
486			var chunk struct {
487				Choices []struct {
488					Index int `json:"index"`
489					Delta struct {
490						Content          string `json:"content"`
491						ReasoningContent string `json:"reasoning_content"`
492						ToolCalls        []struct {
493							Index    int    `json:"index"`
494							ID       string `json:"id"`
495							Type     string `json:"type"`
496							Function struct {
497								Name      string `json:"name"`
498								Arguments string `json:"arguments"`
499							} `json:"function"`
500						} `json:"tool_calls"`
501					} `json:"delta"`
502					FinishReason string `json:"finish_reason"`
503				} `json:"choices"`
504				Usage *struct {
505					PromptTokens        int `json:"prompt_tokens"`
506					CompletionTokens    int `json:"completion_tokens"`
507					PromptTokensDetails struct {
508						CachedTokens int `json:"cached_tokens"`
509					} `json:"prompt_tokens_details"`
510				} `json:"usage"`
511				Error *struct {
512					Message string `json:"message"`
513					Type    string `json:"type"`
514				} `json:"error"`
515			}
516			if err := json.Unmarshal([]byte(ev.Data), &chunk); err != nil {
517				continue
518			}
519			if chunk.Error != nil {
520				stop = StopError
521				finalErr = fmt.Errorf("openai: %s", chunk.Error.Message)
522				sendDone()
523				return
524			}
525			if chunk.Usage != nil {
526				usage.InputTokens = chunk.Usage.PromptTokens - chunk.Usage.PromptTokensDetails.CachedTokens
527				if usage.InputTokens < 0 {
528					usage.InputTokens = chunk.Usage.PromptTokens
529				}
530				usage.OutputTokens = chunk.Usage.CompletionTokens
531				usage.CacheReadTokens = chunk.Usage.PromptTokensDetails.CachedTokens
532			}
533			for _, ch := range chunk.Choices {
534				if ch.Delta.ReasoningContent != "" {
535					reasoningBuf.WriteString(ch.Delta.ReasoningContent)
536				}
537				if ch.Delta.Content != "" {
538					appendText(ch.Delta.Content)
539					out <- EventTextDelta{Delta: ch.Delta.Content}
540				}
541				for _, tc := range ch.Delta.ToolCalls {
542					t := getOrCreateTool(tc.Index)
543					if tc.ID != "" {
544						t.toolID = tc.ID
545					}
546					if tc.Function.Name != "" {
547						t.toolName = tc.Function.Name
548					}
549					if !t.announced && t.toolID != "" && t.toolName != "" {
550						t.announced = true
551						out <- EventToolStart{ID: t.toolID, Name: t.toolName}
552					}
553					if tc.Function.Arguments != "" {
554						t.toolArgs.WriteString(tc.Function.Arguments)
555						if t.announced {
556							out <- EventToolArgs{ID: t.toolID, Delta: tc.Function.Arguments}
557						}
558					}
559				}
560				switch ch.FinishReason {
561				case "stop":
562					stop = StopEnd
563				case "length":
564					stop = StopLength
565				case "tool_calls", "function_call":
566					stop = StopToolUse
567					for _, b := range blocks {
568						if b.kind == "tool_use" && b.announced {
569							out <- EventToolEnd{ID: b.toolID}
570						}
571					}
572				}
573			}
574		}
575	}
576}

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 476

↑↓ moveenter openesc close