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