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