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