1package provider
2
3import (
4 "bytes"
5 "context"
6 "encoding/base64"
7 "encoding/json"
8 "fmt"
9 "io"
10 "net/http"
11 "os"
12 "path/filepath"
13 "strings"
14 "time"
15
16 "github.com/google/uuid"
17)
18
19// Google Gemini provider, talking directly to the Generative Language
20// REST API at generativelanguage.googleapis.com. We hand-roll the wire
21// format instead of pulling in @google/genai (the SDK is large and we
22// only need streamGenerateContent + the models list).
23//
24// Auth model: API key only. Google does NOT issue OAuth tokens for
25// consumer Gemini Advanced / Google One AI subscriptions; programmatic
26// access requires either an AI Studio API key (this client) or Vertex
27// AI / GCP service-account credentials (separate provider, not yet
28// implemented in zot).
29
30const geminiDefaultBaseURL = "https://generativelanguage.googleapis.com"
31
32// geminiClient implements Client against the Gemini Generative Language API.
33type geminiClient struct {
34 apiKey string
35 baseURL string
36 http *http.Client
37}
38
39// NewGemini creates a Gemini client using an AI Studio API key.
40// baseURL may be empty; defaults to https://generativelanguage.googleapis.com.
41func NewGemini(apiKey, baseURL string) Client {
42 if baseURL == "" {
43 baseURL = geminiDefaultBaseURL
44 }
45 return &geminiClient{
46 apiKey: apiKey,
47 baseURL: strings.TrimRight(baseURL, "/"),
48 http: &http.Client{Timeout: 0},
49 }
50}
51
52func (c *geminiClient) Name() string { return "google" }
53
54// ---- wire types ----
55//
56// Subset of Gemini's Content / Part / GenerateContentRequest schema.
57// Only the fields zot actually emits or consumes are declared here.
58
59type gemInlineData struct {
60 MimeType string `json:"mimeType"`
61 Data string `json:"data"` // base64
62}
63
64type gemFunctionCall struct {
65 Name string `json:"name"`
66 Args json.RawMessage `json:"args,omitempty"`
67}
68
69type gemFunctionResponse struct {
70 Name string `json:"name"`
71 Response json.RawMessage `json:"response"`
72}
73
74// gemPart is one element of a Content. Exactly one of the optional
75// fields is populated per part. We use pointers so empty values are
76// omitted from the wire, which matters for tool responses (Gemini
77// rejects parts with both `text` and `functionResponse` set).
78type gemPart struct {
79 Text string `json:"text,omitempty"`
80 InlineData *gemInlineData `json:"inlineData,omitempty"`
81 FunctionCall *gemFunctionCall `json:"functionCall,omitempty"`
82 FunctionResponse *gemFunctionResponse `json:"functionResponse,omitempty"`
83 // Thought marks a thought-summary part. ThoughtSignature is opaque
84 // provider state that must be replayed on the same part.
85 Thought bool `json:"thought,omitempty"`
86 ThoughtSignature string `json:"thoughtSignature,omitempty"`
87}
88
89type gemContent struct {
90 Role string `json:"role"` // "user" | "model"
91 Parts []gemPart `json:"parts"`
92}
93
94type gemFunctionDecl struct {
95 Name string `json:"name"`
96 Description string `json:"description,omitempty"`
97 Parameters json.RawMessage `json:"parameters,omitempty"`
98}
99
100type gemTool struct {
101 FunctionDeclarations []gemFunctionDecl `json:"functionDeclarations,omitempty"`
102}
103
104type gemThinkingConfig struct {
105 IncludeThoughts bool `json:"includeThoughts,omitempty"`
106 ThinkingBudget *int `json:"thinkingBudget,omitempty"`
107 ThinkingLevel string `json:"thinkingLevel,omitempty"`
108}
109
110type gemGenerationConfig struct {
111 Temperature *float32 `json:"temperature,omitempty"`
112 MaxOutputTokens *int `json:"maxOutputTokens,omitempty"`
113 ThinkingConfig *gemThinkingConfig `json:"thinkingConfig,omitempty"`
114}
115
116type gemSystemInstruction struct {
117 Parts []gemPart `json:"parts"`
118}
119
120type gemRequest struct {
121 Contents []gemContent `json:"contents"`
122 SystemInstruction *gemSystemInstruction `json:"systemInstruction,omitempty"`
123 Tools []gemTool `json:"tools,omitempty"`
124 GenerationConfig *gemGenerationConfig `json:"generationConfig,omitempty"`
125}
126
127// ---- request building ----
128
129func (c *geminiClient) buildRequest(req Request) (*gemRequest, string, error) {
130 m, err := FindModel("google", req.Model)
131 if err != nil {
132 // Not in the catalog — still allow custom ids by falling back
133 // to defaults so users can point at unreleased models or
134 // alternate base URLs.
135 m = Model{
136 Provider: "google",
137 ID: req.Model,
138 ContextWindow: 1_000_000,
139 MaxOutput: 8192,
140 Reasoning: strings.Contains(req.Model, "2.5") || strings.Contains(req.Model, "3"),
141 }
142 }
143
144 reasoning := ClampReasoningForModel(m, req.Reasoning)
145 out := &gemRequest{}
146
147 // System prompt → systemInstruction.parts[0].text.
148 if strings.TrimSpace(req.System) != "" {
149 out.SystemInstruction = &gemSystemInstruction{
150 Parts: []gemPart{{Text: req.System}},
151 }
152 }
153
154 functionsEnabled := geminiSupportsFunctionCalling(m.ID)
155 activeTools := activeToolDefinitions(req.Tools, req.Messages)
156
157 // Convert tool defs. Gemini image-generation models reject function
158 // declarations with "Function calling is not enabled for this model";
159 // for those models, send a direct multimodal prompt instead.
160 if functionsEnabled && len(activeTools) > 0 {
161 decls := make([]gemFunctionDecl, 0, len(activeTools))
162 for _, t := range activeTools {
163 schema := sanitizeGeminiToolSchema(t.Schema)
164 decls = append(decls, gemFunctionDecl{
165 Name: t.Name,
166 Description: t.Description,
167 Parameters: schema,
168 })
169 }
170 out.Tools = []gemTool{{FunctionDeclarations: decls}}
171 }
172
173 // Generation config.
174 maxTok := req.MaxTokens
175 if maxTok <= 0 {
176 maxTok = m.MaxOutput
177 }
178 gc := &gemGenerationConfig{Temperature: req.Temperature}
179 if maxTok > 0 {
180 gc.MaxOutputTokens = &maxTok
181 }
182 if reasoning != "" && m.Reasoning {
183 tc := geminiThinkingConfig(m.ID, reasoning)
184 if tc != nil {
185 gc.ThinkingConfig = tc
186 }
187 }
188 out.GenerationConfig = gc
189
190 // Convert messages. When function calling is disabled for the target
191 // model, also remove historical functionCall/functionResponse parts;
192 // image models should receive only text/image content.
193 msgs := req.Messages
194 if functionsEnabled {
195 msgs = RepairOrphanedToolResults(req.Messages)
196 }
197 for _, msg := range msgs {
198 switch msg.Role {
199 case RoleUser:
200 parts := convertGemUserParts(msg.Content)
201 if len(parts) == 0 {
202 continue
203 }
204 out.Contents = append(out.Contents, gemContent{Role: "user", Parts: parts})
205 case RoleAssistant:
206 parts := convertGemAssistantParts(msg.Content, functionsEnabled)
207 // Gemini 3 requires a thought signature on client-injected function
208 // calls. Google's documented placeholder opts out of validation
209 // for calls that were not generated by Gemini. Do not alter genuine
210 // model responses or replace signatures obtained from the API.
211 if strings.HasPrefix(strings.ToLower(m.ID), "gemini-3") && msg.Meta["synthetic_tool_call"] == "true" {
212 for i := range parts {
213 if parts[i].FunctionCall != nil && parts[i].ThoughtSignature == "" {
214 parts[i].ThoughtSignature = "skip_thought_signature_validator"
215 }
216 }
217 }
218 if len(parts) == 0 {
219 continue
220 }
221 out.Contents = append(out.Contents, gemContent{Role: "model", Parts: parts})
222 case RoleTool:
223 if !functionsEnabled {
224 continue
225 }
226 // Each tool_result becomes a user-role message with a
227 // functionResponse part. Gemini's protocol uses
228 // "user" role for tool replies.
229 parts := convertGemToolResultParts(msg.Content)
230 if len(parts) == 0 {
231 continue
232 }
233 out.Contents = append(out.Contents, gemContent{Role: "user", Parts: parts})
234 }
235 }
236
237 return out, m.ID, nil
238}
239
240func sanitizeGeminiToolSchema(schema json.RawMessage) json.RawMessage {
241 if len(schema) == 0 || !json.Valid(schema) {
242 return json.RawMessage(`{"type":"object","properties":{}}`)
243 }
244 var v any
245 if err := json.Unmarshal(schema, &v); err != nil {
246 return json.RawMessage(`{"type":"object","properties":{}}`)
247 }
248 v = stripGeminiUnsupportedSchemaFields(v)
249 out, err := json.Marshal(v)
250 if err != nil || len(out) == 0 || !json.Valid(out) {
251 return json.RawMessage(`{"type":"object","properties":{}}`)
252 }
253 return out
254}
255
256func stripGeminiUnsupportedSchemaFields(v any) any {
257 switch x := v.(type) {
258 case map[string]any:
259 out := make(map[string]any, len(x))
260 for k, val := range x {
261 switch k {
262 case "additionalProperties", "$schema":
263 continue
264 default:
265 out[k] = stripGeminiUnsupportedSchemaFields(val)
266 }
267 }
268 return out
269 case []any:
270 out := make([]any, len(x))
271 for i, val := range x {
272 out[i] = stripGeminiUnsupportedSchemaFields(val)
273 }
274 return out
275 default:
276 return v
277 }
278}
279
280func convertGemUserParts(blocks []Content) []gemPart {
281 var parts []gemPart
282 for _, b := range blocks {
283 switch v := b.(type) {
284 case TextBlock:
285 if v.Text == "" {
286 continue
287 }
288 parts = append(parts, gemPart{Text: v.Text})
289 case ImageBlock:
290 parts = append(parts, gemPart{
291 InlineData: &gemInlineData{
292 MimeType: v.MimeType,
293 Data: base64.StdEncoding.EncodeToString(v.Data),
294 },
295 })
296 }
297 }
298 return parts
299}
300
301func convertGemAssistantParts(blocks []Content, functionsEnabled bool) []gemPart {
302 var parts []gemPart
303 for _, b := range blocks {
304 switch v := b.(type) {
305 case ReasoningBlock:
306 if v.Encrypted != "" || v.Summary != "" {
307 parts = append(parts, gemPart{
308 Text: v.Summary,
309 Thought: true,
310 ThoughtSignature: v.Encrypted,
311 })
312 }
313 case TextBlock:
314 if strings.TrimSpace(v.Text) == "" {
315 continue
316 }
317 parts = append(parts, gemPart{Text: v.Text, ThoughtSignature: v.ThoughtSignature})
318 case ImageBlock:
319 parts = append(parts, gemPart{
320 InlineData: &gemInlineData{
321 MimeType: v.MimeType,
322 Data: base64.StdEncoding.EncodeToString(v.Data),
323 },
324 ThoughtSignature: v.ThoughtSignature,
325 })
326 case ToolCallBlock:
327 if !functionsEnabled {
328 continue
329 }
330 args := v.Arguments
331 if len(args) == 0 || !json.Valid(args) {
332 args = json.RawMessage("{}")
333 }
334 parts = append(parts, gemPart{
335 FunctionCall: &gemFunctionCall{
336 Name: v.Name,
337 Args: args,
338 },
339 ThoughtSignature: v.ThoughtSignature,
340 })
341 }
342 }
343 return parts
344}
345
346func geminiSupportsFunctionCalling(modelID string) bool {
347 id := strings.ToLower(modelID)
348 // Gemini image generation/editing models accept direct multimodal
349 // prompts but reject tools/function declarations.
350 if strings.Contains(id, "flash-image") || strings.Contains(id, "image-generation") {
351 return false
352 }
353 return true
354}
355
356func saveGeminiImageToWorkingDir(mimeType string, data []byte) (string, error) {
357 ext := ".png"
358 switch strings.ToLower(mimeType) {
359 case "image/jpeg", "image/jpg":
360 ext = ".jpg"
361 case "image/webp":
362 ext = ".webp"
363 case "image/gif":
364 ext = ".gif"
365 }
366 name := "zot-gemini-image-" + uuid.NewString() + ext
367 path := filepath.Join(".", name)
368 if err := os.WriteFile(path, data, 0o644); err != nil {
369 return "", err
370 }
371 return path, nil
372}
373
374func convertGemToolResultParts(blocks []Content) []gemPart {
375 var parts []gemPart
376 for _, b := range blocks {
377 tr, ok := b.(ToolResultBlock)
378 if !ok {
379 continue
380 }
381 // Flatten text content. Image content in tool results is dropped
382 // for Gemini < 3 (multimodal function responses are 3+); for the
383 // common path (text output) we wrap as {"output": "..."} or
384 // {"error": "..."} per the SDK's convention.
385 var sb strings.Builder
386 for _, c := range tr.Content {
387 if tb, ok := c.(TextBlock); ok {
388 if sb.Len() > 0 {
389 sb.WriteString("\n")
390 }
391 sb.WriteString(tb.Text)
392 }
393 }
394 key := "output"
395 if tr.IsError {
396 key = "error"
397 }
398 // Gemini wants response to be an object, not a string.
399 respObj := map[string]string{key: sb.String()}
400 respBytes, err := json.Marshal(respObj)
401 if err != nil {
402 respBytes = []byte(`{"output":""}`)
403 }
404 // Tool name is required on functionResponse; the original
405 // ToolCallBlock has it but ToolResultBlock only carries the
406 // call id. We thread the name back via the call id by using
407 // the id as the name fallback — Gemini ignores the name on
408 // the response side as long as it's non-empty.
409 name := tr.CallID
410 parts = append(parts, gemPart{
411 FunctionResponse: &gemFunctionResponse{
412 Name: name,
413 Response: respBytes,
414 },
415 })
416 }
417 return parts
418}
419
420// geminiThinkingConfig maps zot's reasoning level ("low"/"medium"/"high")
421// to Gemini's thinkingConfig. The right knob depends on the model
422// generation: 2.5 family uses thinkingBudget (tokens), 3.x uses
423// thinkingLevel (enum). Returns nil when the level is unrecognised.
424func geminiThinkingConfig(modelID, level string) *gemThinkingConfig {
425 level = NormalizeReasoning(level)
426 id := strings.ToLower(modelID)
427
428 // Gemini 3.x: enum-based thinkingLevel. Pro can't go below LOW.
429 if strings.Contains(id, "gemini-3") {
430 isPro := strings.Contains(id, "-pro")
431 var lvl string
432 switch level {
433 case "minimum":
434 if isPro {
435 lvl = "LOW"
436 } else {
437 lvl = "MINIMAL"
438 }
439 case "low":
440 lvl = "LOW"
441 case "medium":
442 if isPro {
443 lvl = "HIGH"
444 } else {
445 lvl = "MEDIUM"
446 }
447 case "high", "xhigh", "max":
448 lvl = "HIGH"
449 default:
450 return nil
451 }
452 return &gemThinkingConfig{IncludeThoughts: true, ThinkingLevel: lvl}
453 }
454
455 // Gemini 2.5 family: token-budget per-model.
456 budget := ReasoningBudget(level)
457 switch {
458 case strings.Contains(id, "2.5-pro"):
459 if budget > 32768 {
460 budget = 32768
461 }
462 case strings.Contains(id, "2.5-flash-lite"):
463 if budget > 24576 {
464 budget = 24576
465 }
466 case strings.Contains(id, "2.5-flash"):
467 if budget > 24576 {
468 budget = 24576
469 }
470 default:
471 return nil
472 }
473 if budget <= 0 {
474 return nil
475 }
476 return &gemThinkingConfig{IncludeThoughts: true, ThinkingBudget: &budget}
477}
478
479// ---- streaming ----
480
481func (c *geminiClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
482 wire, modelID, err := c.buildRequest(req)
483 if err != nil {
484 return nil, err
485 }
486 body, err := json.Marshal(wire)
487 if err != nil {
488 return nil, err
489 }
490
491 // Gemini's streaming endpoint is a POST with ?alt=sse to get
492 // an EventSource-compatible response. Without alt=sse the
493 // server returns a JSON array (one element per chunk), which
494 // we'd need a different parser for.
495 apiPath := fmt.Sprintf("/v1beta/models/%s:streamGenerateContent", modelID)
496 url := c.baseURL + apiPath + "?alt=sse"
497
498 newReq := func() (*http.Request, error) {
499 httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
500 if err != nil {
501 return nil, err
502 }
503 httpReq.Header.Set("content-type", "application/json")
504 httpReq.Header.Set("accept", "text/event-stream")
505 // Two ways to authenticate against the Generative Language
506 // API: x-goog-api-key header or ?key= query param. We use
507 // the header so the key never lands in proxy access logs.
508 httpReq.Header.Set("x-goog-api-key", c.apiKey)
509 return httpReq, nil
510 }
511
512 resp, err := doStreamWithRetry(ctx, c.http, newReq)
513 if err != nil {
514 return nil, fmt.Errorf("google: %w", err)
515 }
516 if resp.StatusCode != http.StatusOK {
517 b, _ := io.ReadAll(resp.Body)
518 resp.Body.Close()
519 return nil, fmt.Errorf("google: http %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
520 }
521
522 out := make(chan Event, 16)
523 go c.runStream(ctx, resp, req, out)
524 return out, nil
525}
526
527func (c *geminiClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
528 defer close(out)
529 defer resp.Body.Close()
530
531 model, _ := FindModel("google", req.Model)
532 out <- EventStart{Model: req.Model, Provider: "google"}
533
534 raw := make(chan sseEvent, 16)
535 go readSSE(resp.Body, raw)
536
537 // Gemini's SSE stream is a sequence of complete JSON
538 // GenerateContentResponse objects, one per data: line. Each
539 // candidate carries a list of parts (text or functionCall),
540 // possibly accumulating across chunks.
541 type blockEntry struct {
542 kind string // "text" | "image" | "tool_use" | "reasoning"
543 textBuf strings.Builder
544 thoughtSignature string
545 image *ImageBlock
546 imagePath string
547 toolID string
548 toolName string
549 toolArgs strings.Builder
550 }
551 var (
552 blocks []*blockEntry
553 currentText *blockEntry
554 currentReasoning *blockEntry
555 usage Usage
556 stop StopReason = StopEnd
557 finalErr error
558 toolCounter int
559 )
560
561 appendPartText := func(kind, delta, signature string) {
562 current := ¤tText
563 if kind == "reasoning" {
564 current = ¤tReasoning
565 }
566 if *current == nil {
567 *current = &blockEntry{kind: kind}
568 blocks = append(blocks, *current)
569 }
570 (*current).textBuf.WriteString(delta)
571 if signature != "" {
572 (*current).thoughtSignature = signature
573 // A signature terminates this wire part. A later delta of the
574 // same kind belongs to a new part and must not be merged into it.
575 *current = nil
576 }
577 }
578
579 appendImage := func(mimeType, dataB64, signature string) {
580 data, err := base64.StdEncoding.DecodeString(dataB64)
581 if err != nil || len(data) == 0 {
582 return
583 }
584 img := ImageBlock{MimeType: mimeType, Data: data, ThoughtSignature: signature}
585 path, _ := saveGeminiImageToWorkingDir(mimeType, data)
586 blocks = append(blocks, &blockEntry{kind: "image", image: &img, imagePath: path})
587 // Non-text parts break both streamed text runs.
588 currentText = nil
589 currentReasoning = nil
590 }
591
592 startTool := func(name string, providedID string, args json.RawMessage) *blockEntry {
593 toolCounter++
594 id := providedID
595 if id == "" {
596 id = fmt.Sprintf("%s_%d_%d", name, time.Now().UnixNano(), toolCounter)
597 }
598 t := &blockEntry{
599 kind: "tool_use",
600 toolID: id,
601 toolName: name,
602 }
603 if len(args) > 0 && json.Valid(args) {
604 t.toolArgs.Write(args)
605 }
606 blocks = append(blocks, t)
607 // New tool blocks break both streamed text runs.
608 currentText = nil
609 currentReasoning = nil
610 return t
611 }
612
613 assembleMsg := func() Message {
614 content := []Content{}
615 for _, b := range blocks {
616 switch b.kind {
617 case "text":
618 if b.textBuf.Len() > 0 {
619 content = append(content, TextBlock{
620 Text: b.textBuf.String(),
621 ThoughtSignature: b.thoughtSignature,
622 })
623 }
624 case "reasoning":
625 if b.textBuf.Len() > 0 || b.thoughtSignature != "" {
626 content = append(content, ReasoningBlock{
627 Summary: b.textBuf.String(),
628 Encrypted: b.thoughtSignature,
629 })
630 }
631 case "image":
632 if b.image != nil && len(b.image.Data) > 0 {
633 content = append(content, *b.image)
634 if b.imagePath != "" {
635 content = append(content, TextBlock{Text: fmt.Sprintf("Saved image: `%s`", b.imagePath)})
636 }
637 }
638 case "tool_use":
639 args := b.toolArgs.String()
640 if args == "" || !json.Valid([]byte(args)) {
641 args = "{}"
642 }
643 content = append(content, ToolCallBlock{
644 ID: b.toolID,
645 Name: b.toolName,
646 Arguments: json.RawMessage(args),
647 ThoughtSignature: b.thoughtSignature,
648 })
649 }
650 }
651 return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
652 }
653
654 sendDone := func() {
655 usage.CostUSD = ComputeCost(model, usage)
656 out <- EventUsage{Usage: usage}
657 out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
658 }
659
660 for {
661 select {
662 case <-ctx.Done():
663 stop = StopAborted
664 finalErr = ctx.Err()
665 sendDone()
666 return
667 case ev, ok := <-raw:
668 if !ok {
669 sendDone()
670 return
671 }
672 if strings.TrimSpace(ev.Data) == "" {
673 continue
674 }
675 var chunk struct {
676 Candidates []struct {
677 Content struct {
678 Role string `json:"role"`
679 Parts []struct {
680 Text string `json:"text"`
681 InlineData *gemInlineData `json:"inlineData"`
682 Thought bool `json:"thought"`
683 ThoughtSignature string `json:"thoughtSignature"`
684 FunctionCall *gemFunctionCall `json:"functionCall"`
685 FunctionCallID string `json:"id"`
686 FunctionCallName string `json:"name"`
687 } `json:"parts"`
688 } `json:"content"`
689 FinishReason string `json:"finishReason"`
690 } `json:"candidates"`
691 UsageMetadata *struct {
692 PromptTokenCount int `json:"promptTokenCount"`
693 CandidatesTokenCount int `json:"candidatesTokenCount"`
694 ThoughtsTokenCount *int `json:"thoughtsTokenCount"`
695 CachedContentTokenCount int `json:"cachedContentTokenCount"`
696 TotalTokenCount int `json:"totalTokenCount"`
697 } `json:"usageMetadata"`
698 Error *struct {
699 Code int `json:"code"`
700 Message string `json:"message"`
701 Status string `json:"status"`
702 } `json:"error"`
703 }
704 if err := json.Unmarshal([]byte(ev.Data), &chunk); err != nil {
705 continue
706 }
707 if chunk.Error != nil {
708 stop = StopError
709 finalErr = fmt.Errorf("google: %s", chunk.Error.Message)
710 sendDone()
711 return
712 }
713 for _, cand := range chunk.Candidates {
714 for _, part := range cand.Content.Parts {
715 if part.InlineData != nil {
716 appendImage(part.InlineData.MimeType, part.InlineData.Data, part.ThoughtSignature)
717 continue
718 }
719 if part.FunctionCall != nil {
720 var args json.RawMessage
721 if len(part.FunctionCall.Args) > 0 {
722 args = part.FunctionCall.Args
723 } else {
724 args = json.RawMessage("{}")
725 }
726 t := startTool(part.FunctionCall.Name, part.FunctionCallID, args)
727 t.thoughtSignature = part.ThoughtSignature
728 out <- EventToolStart{ID: t.toolID, Name: t.toolName}
729 out <- EventToolArgs{ID: t.toolID, Delta: t.toolArgs.String()}
730 out <- EventToolEnd{ID: t.toolID}
731 continue
732 }
733 if part.Thought {
734 appendPartText("reasoning", part.Text, part.ThoughtSignature)
735 currentText = nil
736 continue
737 }
738 if part.Text == "" {
739 continue
740 }
741 appendPartText("text", part.Text, part.ThoughtSignature)
742 currentReasoning = nil
743 out <- EventTextDelta{Delta: part.Text}
744 }
745 switch cand.FinishReason {
746 case "STOP", "":
747 // "" arrives on intermediate chunks; only
748 // promote the explicit terminal value.
749 if cand.FinishReason == "STOP" {
750 stop = StopEnd
751 }
752 case "MAX_TOKENS":
753 stop = StopLength
754 case "SAFETY", "RECITATION", "BLOCKLIST", "PROHIBITED_CONTENT", "SPII", "IMAGE_SAFETY":
755 stop = StopError
756 finalErr = fmt.Errorf("google: response blocked (%s)", cand.FinishReason)
757 }
758 }
759 if chunk.UsageMetadata != nil {
760 // Gemini reports cumulative totals on every chunk
761 // (or close to it), so assign rather than sum.
762 um := chunk.UsageMetadata
763 input := um.PromptTokenCount - um.CachedContentTokenCount
764 if input < 0 {
765 input = um.PromptTokenCount
766 }
767 usage.InputTokens = input
768 usage.OutputTokens = um.CandidatesTokenCount
769 if um.ThoughtsTokenCount != nil {
770 usage.ReasoningTokens = *um.ThoughtsTokenCount
771 usage.ReasoningTokensKnown = true
772 usage.OutputTokens += *um.ThoughtsTokenCount
773 }
774 usage.CacheReadTokens = um.CachedContentTokenCount
775 }
776 // Promote ToolUse stop when tool calls are present and
777 // the candidate finished cleanly.
778 if stop == StopEnd {
779 for _, b := range blocks {
780 if b.kind == "tool_use" {
781 stop = StopToolUse
782 break
783 }
784 }
785 }
786 }
787 }
788}