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