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