v0.1.86 d46f1d5

openai_codex.go RawHistoryFolder

1package provider
2
3import (
4	"bytes"
5	"context"
6	"encoding/base64"
7	"encoding/json"
8	"fmt"
9	"io"
10	"net/http"
11	"runtime"
12	"strings"
13	"time"
14)
15
16// Codex (ChatGPT subscription) client. Uses OpenAI's Responses API via
17// chatgpt.com/backend-api with the chatgpt-account-id handshake.
18//
19// Wire protocol notes:
20//   - Endpoint: POST https://chatgpt.com/backend-api/codex/responses
21//   - Headers: Authorization: Bearer <access_token>, chatgpt-account-id: <id>,
22//     OpenAI-Beta: responses=experimental, originator: zot
23//   - Body: OpenAI Responses API shape (not chat/completions).
24//     input: [{role, content: [{type: "input_text" | "input_image" | ... }]}]
25//     instructions: <system prompt>
26//     tools: [{type:"function", name, description, parameters, strict}]
27//     stream: true
28//   - SSE events: response.output_item.added, response.output_text.delta,
29//     response.function_call_arguments.delta, response.output_item.done,
30//     response.completed, response.failed, error
31//
32// The access_token comes from the OpenAI OAuth flow (auth.openai.com).
33// The accountID is parsed from the id_token JWT's `chatgpt_account_id`
34// claim; see auth/oauth.go.
35
36const codexDefaultBaseURL = "https://chatgpt.com/backend-api/codex/responses"
37
38type codexClient struct {
39	token     string
40	accountID string
41	baseURL   string
42	http      *http.Client
43}
44
45// NewOpenAICodex creates a client that talks to ChatGPT's Codex endpoint
46// using a subscription OAuth access token and the user's ChatGPT
47// account id. baseURL may be empty to use the default.
48func NewOpenAICodex(token, accountID, baseURL string) Client {
49	if baseURL == "" {
50		baseURL = codexDefaultBaseURL
51	}
52	return &codexClient{
53		token:     token,
54		accountID: accountID,
55		baseURL:   strings.TrimRight(baseURL, "/"),
56		http:      &http.Client{Timeout: 0},
57	}
58}
59
60func (c *codexClient) Name() string { return "openai" }
61
62// ---- Responses API wire types (subset needed for zot's surface) ----
63
64type codexInputText struct {
65	Type string `json:"type"` // "input_text"
66	Text string `json:"text"`
67}
68
69type codexInputImage struct {
70	Type     string `json:"type"` // "input_image"
71	Detail   string `json:"detail"`
72	ImageURL string `json:"image_url"`
73}
74
75type codexOutputText struct {
76	Type        string `json:"type"` // "output_text"
77	Text        string `json:"text"`
78	Annotations []any  `json:"annotations"`
79}
80
81type codexInputMessage struct {
82	Type    string `json:"type,omitempty"` // "message" (optional for input)
83	Role    string `json:"role"`
84	Content []any  `json:"content"`
85}
86
87type codexOutputMessage struct {
88	Type    string            `json:"type"` // "message"
89	Role    string            `json:"role"`
90	Status  string            `json:"status,omitempty"`
91	ID      string            `json:"id,omitempty"`
92	Content []codexOutputText `json:"content"`
93}
94
95type codexFunctionCall struct {
96	Type      string `json:"type"` // "function_call"
97	ID        string `json:"id,omitempty"`
98	CallID    string `json:"call_id"`
99	Name      string `json:"name"`
100	Arguments string `json:"arguments"` // JSON string
101}
102
103type codexFunctionCallOutput struct {
104	Type   string `json:"type"` // "function_call_output"
105	CallID string `json:"call_id"`
106	Output string `json:"output"` // string (or ResponseFunctionCallOutputItemList for images; v1 only uses string)
107}
108
109// codexReasoningItem mirrors the Responses API "reasoning" output item.
110// We capture it on incoming streams and replay it verbatim on follow-up
111// requests: the API rejects assistant tool-call replays without it when
112// thinking is enabled.
113type codexReasoningItem struct {
114	Type             string `json:"type"` // "reasoning"
115	ID               string `json:"id,omitempty"`
116	EncryptedContent string `json:"encrypted_content,omitempty"`
117	// Summary is required by the Responses API even when no summary text
118	// was streamed; encode an empty array rather than omitting the field.
119	Summary []codexReasoningSummary `json:"summary"`
120}
121
122type codexReasoningSummary struct {
123	Type string `json:"type"` // "summary_text"
124	Text string `json:"text"`
125}
126
127type codexTool struct {
128	Type        string          `json:"type"` // "function"
129	Name        string          `json:"name"`
130	Description string          `json:"description,omitempty"`
131	Parameters  json.RawMessage `json:"parameters"`
132}
133
134type codexRequest struct {
135	Model             string      `json:"model"`
136	Store             bool        `json:"store"`
137	Stream            bool        `json:"stream"`
138	Instructions      string      `json:"instructions,omitempty"`
139	Input             []any       `json:"input"`
140	Tools             []codexTool `json:"tools,omitempty"`
141	ToolChoice        string      `json:"tool_choice,omitempty"`
142	ParallelToolCalls bool        `json:"parallel_tool_calls"`
143	Include           []string    `json:"include,omitempty"`
144}
145
146// ---- Request building ----
147
148func (c *codexClient) buildRequest(req Request) (*codexRequest, error) {
149	m, err := FindModel("openai", req.Model)
150	if err != nil {
151		return nil, err
152	}
153	_ = m
154
155	body := &codexRequest{
156		Model:             req.Model,
157		Store:             false,
158		Stream:            true,
159		Instructions:      req.System,
160		ParallelToolCalls: true,
161		Include:           []string{"reasoning.encrypted_content"},
162	}
163	if len(req.Tools) > 0 {
164		body.ToolChoice = "auto"
165		for _, t := range req.Tools {
166			params := t.Schema
167			if len(params) == 0 {
168				params = json.RawMessage(`{"type":"object","properties":{}}`)
169			}
170			body.Tools = append(body.Tools, codexTool{
171				Type:        "function",
172				Name:        t.Name,
173				Description: t.Description,
174				Parameters:  params,
175			})
176		}
177	}
178
179	msgIdx := 0
180	req.Messages = RepairOrphanedToolResults(req.Messages)
181	for _, msg := range req.Messages {
182		switch msg.Role {
183		case RoleUser:
184			content := []any{}
185			for _, c := range msg.Content {
186				switch v := c.(type) {
187				case TextBlock:
188					if v.Text != "" {
189						content = append(content, codexInputText{Type: "input_text", Text: v.Text})
190					}
191				case ImageBlock:
192					url := "data:" + v.MimeType + ";base64," + base64.StdEncoding.EncodeToString(v.Data)
193					content = append(content, codexInputImage{Type: "input_image", Detail: "auto", ImageURL: url})
194				}
195			}
196			if len(content) == 0 {
197				continue
198			}
199			body.Input = append(body.Input, codexInputMessage{Role: "user", Content: content})
200		case RoleAssistant:
201			// Emit one output_message per text block, one function_call per
202			// tool call, and one reasoning item per ReasoningBlock,
203			// preserving the order so the model sees the same interleaving
204			// we captured. The reasoning replay is what keeps OpenAI
205			// Codex from rejecting follow-up tool calls with
206			// "thinking is enabled but reasoning_content is missing".
207			for _, c := range msg.Content {
208				switch v := c.(type) {
209				case ReasoningBlock:
210					item := codexReasoningItem{
211						Type:             "reasoning",
212						ID:               v.ID,
213						EncryptedContent: v.Encrypted,
214						Summary:          []codexReasoningSummary{},
215					}
216					if v.Summary != "" {
217						item.Summary = []codexReasoningSummary{{Type: "summary_text", Text: v.Summary}}
218					}
219					body.Input = append(body.Input, item)
220				case TextBlock:
221					if v.Text == "" {
222						continue
223					}
224					msgIdx++
225					body.Input = append(body.Input, codexOutputMessage{
226						Type:   "message",
227						Role:   "assistant",
228						Status: "completed",
229						ID:     fmt.Sprintf("msg_%d", msgIdx),
230						Content: []codexOutputText{
231							{Type: "output_text", Text: v.Text, Annotations: []any{}},
232						},
233					})
234				case ToolCallBlock:
235					args := string(v.Arguments)
236					if args == "" || !json.Valid([]byte(args)) {
237						args = "{}"
238					}
239					callID, _ := splitCallID(v.ID)
240					body.Input = append(body.Input, codexFunctionCall{
241						Type:      "function_call",
242						CallID:    callID,
243						Name:      v.Name,
244						Arguments: args,
245					})
246				}
247			}
248		case RoleTool:
249			for _, c := range msg.Content {
250				if tr, ok := c.(ToolResultBlock); ok {
251					var text strings.Builder
252					for _, inner := range tr.Content {
253						if tb, ok := inner.(TextBlock); ok {
254							if text.Len() > 0 {
255								text.WriteString("\n")
256							}
257							text.WriteString(tb.Text)
258						}
259					}
260					callID, _ := splitCallID(tr.CallID)
261					body.Input = append(body.Input, codexFunctionCallOutput{
262						Type:   "function_call_output",
263						CallID: callID,
264						Output: text.String(),
265					})
266				}
267			}
268		}
269	}
270
271	return body, nil
272}
273
274func splitCallID(id string) (string, string) {
275	if i := strings.Index(id, "|"); i >= 0 {
276		return id[:i], id[i+1:]
277	}
278	return id, ""
279}
280
281// ---- Streaming ----
282
283func (c *codexClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
284	wire, err := c.buildRequest(req)
285	if err != nil {
286		return nil, err
287	}
288	body, err := json.Marshal(wire)
289	if err != nil {
290		return nil, err
291	}
292
293	newReq := func() (*http.Request, error) {
294		httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL, bytes.NewReader(body))
295		if err != nil {
296			return nil, err
297		}
298		httpReq.Header.Set("content-type", "application/json")
299		httpReq.Header.Set("accept", "text/event-stream")
300		httpReq.Header.Set("authorization", "Bearer "+c.token)
301		httpReq.Header.Set("chatgpt-account-id", c.accountID)
302		httpReq.Header.Set("openai-beta", "responses=experimental")
303		httpReq.Header.Set("originator", "zot")
304		httpReq.Header.Set("user-agent", fmt.Sprintf("zot (%s %s)", runtime.GOOS, runtime.GOARCH))
305		return httpReq, nil
306	}
307
308	resp, err := doStreamWithRetry(ctx, c.http, newReq)
309	if err != nil {
310		return nil, fmt.Errorf("openai-codex: %w", err)
311	}
312	if resp.StatusCode != http.StatusOK {
313		b, _ := io.ReadAll(resp.Body)
314		resp.Body.Close()
315		return nil, fmt.Errorf("openai-codex: http %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
316	}
317
318	out := make(chan Event, 16)
319	go c.runStream(ctx, resp, req, out)
320	return out, nil
321}
322
323func (c *codexClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
324	defer close(out)
325	defer resp.Body.Close()
326
327	model, _ := FindModel("openai", req.Model)
328	out <- EventStart{Model: req.Model, Provider: "openai"}
329
330	raw := make(chan sseEvent, 16)
331	go readSSE(resp.Body, raw)
332
333	// Accumulators. The Responses API emits output_items in order; each
334	// item is either a "message" (text) or a "function_call". We track
335	// the in-flight item by its index.
336	type itemState struct {
337		kind      string // "message" | "function_call" | "reasoning"
338		callID    string
339		name      string
340		argsBuf   strings.Builder
341		textBuf   strings.Builder
342		summary   strings.Builder
343		rawID     string
344		encrypted string
345		announced bool
346	}
347	var (
348		items    = map[int]*itemState{}
349		order    []int
350		usage    Usage
351		stop     StopReason = StopEnd
352		finalErr error
353	)
354
355	assemble := func() Message {
356		content := []Content{}
357		for _, idx := range order {
358			it := items[idx]
359			switch it.kind {
360			case "message":
361				if it.textBuf.Len() > 0 {
362					content = append(content, TextBlock{Text: it.textBuf.String()})
363				}
364			case "function_call":
365				args := it.argsBuf.String()
366				if args == "" || !json.Valid([]byte(args)) {
367					args = "{}"
368				}
369				content = append(content, ToolCallBlock{
370					ID: it.callID, Name: it.name, Arguments: json.RawMessage(args),
371				})
372			case "reasoning":
373				if it.encrypted == "" && it.summary.Len() == 0 && it.rawID == "" {
374					continue
375				}
376				content = append(content, ReasoningBlock{
377					ID:        it.rawID,
378					Summary:   it.summary.String(),
379					Encrypted: it.encrypted,
380				})
381			}
382		}
383		return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
384	}
385
386	sendDone := func() {
387		usage.CostUSD = ComputeCost(model, usage)
388		out <- EventUsage{Usage: usage}
389		out <- EventDone{Stop: stop, Err: finalErr, Message: assemble()}
390	}
391
392	for {
393		select {
394		case <-ctx.Done():
395			stop = StopAborted
396			finalErr = ctx.Err()
397			sendDone()
398			return
399		case ev, ok := <-raw:
400			if !ok {
401				sendDone()
402				return
403			}
404			var head struct {
405				Type string `json:"type"`
406			}
407			if err := json.Unmarshal([]byte(ev.Data), &head); err != nil {
408				continue
409			}
410
411			switch head.Type {
412			case "response.output_item.added":
413				var p struct {
414					OutputIndex int `json:"output_index"`
415					Item        struct {
416						Type             string `json:"type"` // "message" | "function_call" | "reasoning"
417						ID               string `json:"id"`
418						CallID           string `json:"call_id"`
419						Name             string `json:"name"`
420						EncryptedContent string `json:"encrypted_content"`
421					} `json:"item"`
422				}
423				_ = json.Unmarshal([]byte(ev.Data), &p)
424				it := &itemState{}
425				switch p.Item.Type {
426				case "message":
427					it.kind = "message"
428				case "function_call":
429					it.kind = "function_call"
430					it.callID = p.Item.CallID
431					it.name = p.Item.Name
432					if !it.announced {
433						it.announced = true
434						out <- EventToolStart{ID: it.callID, Name: it.name}
435					}
436				case "reasoning":
437					it.kind = "reasoning"
438					it.rawID = p.Item.ID
439					it.encrypted = p.Item.EncryptedContent
440				default:
441					continue
442				}
443				items[p.OutputIndex] = it
444				order = append(order, p.OutputIndex)
445			case "response.output_text.delta":
446				var p struct {
447					OutputIndex int    `json:"output_index"`
448					Delta       string `json:"delta"`
449				}
450				_ = json.Unmarshal([]byte(ev.Data), &p)
451				if it, ok := items[p.OutputIndex]; ok && it.kind == "message" {
452					it.textBuf.WriteString(p.Delta)
453					out <- EventTextDelta{Delta: p.Delta}
454				}
455			case "response.reasoning_summary_text.delta":
456				var p struct {
457					OutputIndex int    `json:"output_index"`
458					Delta       string `json:"delta"`
459				}
460				_ = json.Unmarshal([]byte(ev.Data), &p)
461				if it, ok := items[p.OutputIndex]; ok && it.kind == "reasoning" {
462					it.summary.WriteString(p.Delta)
463				}
464			case "response.reasoning_summary_text.done":
465				// summary text already accumulated via deltas
466			case "response.function_call_arguments.delta":
467				var p struct {
468					OutputIndex int    `json:"output_index"`
469					Delta       string `json:"delta"`
470				}
471				_ = json.Unmarshal([]byte(ev.Data), &p)
472				if it, ok := items[p.OutputIndex]; ok && it.kind == "function_call" {
473					it.argsBuf.WriteString(p.Delta)
474					out <- EventToolArgs{ID: it.callID, Delta: p.Delta}
475				}
476			case "response.output_item.done":
477				var p struct {
478					OutputIndex int `json:"output_index"`
479					Item        struct {
480						Type             string `json:"type"`
481						ID               string `json:"id"`
482						EncryptedContent string `json:"encrypted_content"`
483						Summary          []struct {
484							Type string `json:"type"`
485							Text string `json:"text"`
486						} `json:"summary"`
487					} `json:"item"`
488				}
489				_ = json.Unmarshal([]byte(ev.Data), &p)
490				if it, ok := items[p.OutputIndex]; ok {
491					switch it.kind {
492					case "function_call":
493						out <- EventToolEnd{ID: it.callID}
494					case "reasoning":
495						if p.Item.EncryptedContent != "" {
496							it.encrypted = p.Item.EncryptedContent
497						}
498						if it.rawID == "" && p.Item.ID != "" {
499							it.rawID = p.Item.ID
500						}
501						for _, s := range p.Item.Summary {
502							if s.Text == "" {
503								continue
504							}
505							if it.summary.Len() > 0 {
506								it.summary.WriteString("\n")
507							}
508							it.summary.WriteString(s.Text)
509						}
510					}
511				}
512			case "response.completed", "response.done":
513				var p struct {
514					Response struct {
515						Usage struct {
516							InputTokens        int `json:"input_tokens"`
517							OutputTokens       int `json:"output_tokens"`
518							InputTokensDetails struct {
519								CachedTokens int `json:"cached_tokens"`
520							} `json:"input_tokens_details"`
521						} `json:"usage"`
522						Status string `json:"status"`
523					} `json:"response"`
524				}
525				_ = json.Unmarshal([]byte(ev.Data), &p)
526				usage.InputTokens = p.Response.Usage.InputTokens - p.Response.Usage.InputTokensDetails.CachedTokens
527				if usage.InputTokens < 0 {
528					usage.InputTokens = p.Response.Usage.InputTokens
529				}
530				usage.OutputTokens = p.Response.Usage.OutputTokens
531				usage.CacheReadTokens = p.Response.Usage.InputTokensDetails.CachedTokens
532
533				hadTool := false
534				for _, it := range items {
535					if it.kind == "function_call" {
536						hadTool = true
537						break
538					}
539				}
540				if hadTool {
541					stop = StopToolUse
542				} else {
543					stop = StopEnd
544				}
545				sendDone()
546				return
547			case "response.failed":
548				var p struct {
549					Response struct {
550						Error struct {
551							Message string `json:"message"`
552						} `json:"error"`
553					} `json:"response"`
554				}
555				_ = json.Unmarshal([]byte(ev.Data), &p)
556				stop = StopError
557				finalErr = fmt.Errorf("codex: %s", p.Response.Error.Message)
558				sendDone()
559				return
560			case "error":
561				var p struct {
562					Message string `json:"message"`
563					Code    string `json:"code"`
564				}
565				_ = json.Unmarshal([]byte(ev.Data), &p)
566				msg := p.Message
567				if msg == "" {
568					msg = p.Code
569				}
570				stop = StopError
571				finalErr = fmt.Errorf("codex error: %s", msg)
572				sendDone()
573				return
574			}
575		}
576	}
577}

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