v0.1.60 0f332fa

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	httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+apiPath, bytes.NewReader(body))
358	if err != nil {
359		return nil, err
360	}
361	httpReq.Header.Set("content-type", "application/json")
362	httpReq.Header.Set("accept", "text/event-stream")
363	httpReq.Header.Set("authorization", "Bearer "+c.apiKey)
364	for k, v := range c.headers {
365		httpReq.Header.Set(k, v)
366	}
367
368	resp, err := c.http.Do(httpReq)
369	if err != nil {
370		return nil, fmt.Errorf("%s: %w", c.Name(), err)
371	}
372	if resp.StatusCode != http.StatusOK {
373		b, _ := io.ReadAll(resp.Body)
374		resp.Body.Close()
375		return nil, fmt.Errorf("%s: http %d: %s", c.Name(), resp.StatusCode, strings.TrimSpace(string(b)))
376	}
377
378	out := make(chan Event, 16)
379	go c.runStream(ctx, resp, req, out)
380	return out, nil
381}
382
383func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
384	defer close(out)
385	defer resp.Body.Close()
386
387	model, _ := FindModel("", req.Model)
388	out <- EventStart{Model: req.Model, Provider: c.Name()}
389
390	raw := make(chan sseEvent, 16)
391	go readSSE(resp.Body, raw)
392
393	// Interleaved block tracking: text and tool_calls preserve their
394	// emission order so the assistant message renders in the same order
395	// the model produced it. The builder fires one kind of block at a
396	// time — incoming text deltas after a tool_call split into a fresh
397	// text block; subsequent tool_calls each get their own slot.
398	type blockEntry struct {
399		kind      string // "text" | "tool_use"
400		textBuf   strings.Builder
401		toolID    string
402		toolName  string
403		toolArgs  strings.Builder
404		announced bool
405	}
406	var (
407		blocks       []*blockEntry
408		currentText  *blockEntry             // most-recent text block, nil if none
409		toolByIdx    = map[int]*blockEntry{} // openai tool_call index -> block
410		reasoningBuf strings.Builder
411		usage        Usage
412		stop         StopReason = StopEnd
413		finalErr     error
414	)
415
416	appendText := func(delta string) {
417		if currentText == nil {
418			currentText = &blockEntry{kind: "text"}
419			blocks = append(blocks, currentText)
420		}
421		currentText.textBuf.WriteString(delta)
422	}
423
424	getOrCreateTool := func(idx int) *blockEntry {
425		if t, ok := toolByIdx[idx]; ok {
426			return t
427		}
428		t := &blockEntry{kind: "tool_use"}
429		toolByIdx[idx] = t
430		blocks = append(blocks, t)
431		// A new tool block breaks the current text block. Subsequent text
432		// deltas will start a fresh text block after this tool.
433		currentText = nil
434		return t
435	}
436
437	assembleMsg := func() Message {
438		content := []Content{}
439		for _, b := range blocks {
440			switch b.kind {
441			case "text":
442				if b.textBuf.Len() > 0 {
443					content = append(content, TextBlock{Text: b.textBuf.String()})
444				}
445			case "tool_use":
446				args := b.toolArgs.String()
447				if args == "" || !json.Valid([]byte(args)) {
448					args = "{}"
449				}
450				content = append(content, ToolCallBlock{
451					ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args),
452				})
453			}
454		}
455		if reasoningBuf.Len() > 0 {
456			content = append(content, ReasoningBlock{Summary: reasoningBuf.String()})
457		}
458		return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
459	}
460
461	sendDone := func() {
462		usage.CostUSD = ComputeCost(model, usage)
463		out <- EventUsage{Usage: usage}
464		out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
465	}
466
467	for {
468		select {
469		case <-ctx.Done():
470			stop = StopAborted
471			finalErr = ctx.Err()
472			sendDone()
473			return
474		case ev, ok := <-raw:
475			if !ok {
476				sendDone()
477				return
478			}
479			if ev.Data == "[DONE]" {
480				sendDone()
481				return
482			}
483			var chunk struct {
484				Choices []struct {
485					Index int `json:"index"`
486					Delta struct {
487						Content          string `json:"content"`
488						ReasoningContent string `json:"reasoning_content"`
489						ToolCalls        []struct {
490							Index    int    `json:"index"`
491							ID       string `json:"id"`
492							Type     string `json:"type"`
493							Function struct {
494								Name      string `json:"name"`
495								Arguments string `json:"arguments"`
496							} `json:"function"`
497						} `json:"tool_calls"`
498					} `json:"delta"`
499					FinishReason string `json:"finish_reason"`
500				} `json:"choices"`
501				Usage *struct {
502					PromptTokens        int `json:"prompt_tokens"`
503					CompletionTokens    int `json:"completion_tokens"`
504					PromptTokensDetails struct {
505						CachedTokens int `json:"cached_tokens"`
506					} `json:"prompt_tokens_details"`
507				} `json:"usage"`
508				Error *struct {
509					Message string `json:"message"`
510					Type    string `json:"type"`
511				} `json:"error"`
512			}
513			if err := json.Unmarshal([]byte(ev.Data), &chunk); err != nil {
514				continue
515			}
516			if chunk.Error != nil {
517				stop = StopError
518				finalErr = fmt.Errorf("openai: %s", chunk.Error.Message)
519				sendDone()
520				return
521			}
522			if chunk.Usage != nil {
523				usage.InputTokens = chunk.Usage.PromptTokens - chunk.Usage.PromptTokensDetails.CachedTokens
524				if usage.InputTokens < 0 {
525					usage.InputTokens = chunk.Usage.PromptTokens
526				}
527				usage.OutputTokens = chunk.Usage.CompletionTokens
528				usage.CacheReadTokens = chunk.Usage.PromptTokensDetails.CachedTokens
529			}
530			for _, ch := range chunk.Choices {
531				if ch.Delta.ReasoningContent != "" {
532					reasoningBuf.WriteString(ch.Delta.ReasoningContent)
533				}
534				if ch.Delta.Content != "" {
535					appendText(ch.Delta.Content)
536					out <- EventTextDelta{Delta: ch.Delta.Content}
537				}
538				for _, tc := range ch.Delta.ToolCalls {
539					t := getOrCreateTool(tc.Index)
540					if tc.ID != "" {
541						t.toolID = tc.ID
542					}
543					if tc.Function.Name != "" {
544						t.toolName = tc.Function.Name
545					}
546					if !t.announced && t.toolID != "" && t.toolName != "" {
547						t.announced = true
548						out <- EventToolStart{ID: t.toolID, Name: t.toolName}
549					}
550					if tc.Function.Arguments != "" {
551						t.toolArgs.WriteString(tc.Function.Arguments)
552						if t.announced {
553							out <- EventToolArgs{ID: t.toolID, Delta: tc.Function.Arguments}
554						}
555					}
556				}
557				switch ch.FinishReason {
558				case "stop":
559					stop = StopEnd
560				case "length":
561					stop = StopLength
562				case "tool_calls", "function_call":
563					stop = StopToolUse
564					for _, b := range blocks {
565						if b.kind == "tool_use" && b.announced {
566							out <- EventToolEnd{ID: b.toolID}
567						}
568					}
569				}
570			}
571		}
572	}
573}

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