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