# All files / src/providers/gemini Streaming.ts

93.82% Statements 76/81
86.66% Branches 52/60
100% Functions 4/4
96.2% Lines 76/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217

    24x
    24x

    13x
    13x
    13x

    13x

    13x

    13x
    1x
    12x
    1x
    1x
    1x

    13x

    13x

    13x
    1x

    13x

    13x
    1x

    1x

    13x
    13x

    13x
    13x

    13x

    12x
    1x

    11x

    11x
    1x

    10x
    10x
    10x

    10x
    24x
    24x

    10x
    1x

    10x

    14x

    14x
    17x
    17x

    17x

    17x
    14x
    14x

    14x
    14x
    14x
    14x
    14x
    13x
    1x

    12x

    14x
    1x

    10x

    3x
    1x

    2x

    13x
    3x

    2x

    2x

    2x
    2x
    2x
    2x

    2x
    1x
    1x
    1x

    2x

    2x

     import { ChatRequest, ChatChunk } from "../Provider.js";
    import { ToolCall } from "../../chat/Tool.js";
    import { Capabilities } from "./Capabilities.js";
    import { handleGeminiError } from "./Errors.js";
    import { GeminiGenerateContentResponse } from "./types.js";
    import { GeminiChatUtils } from "./ChatUtils.js";
    import { logger } from "../../utils/logger.js";
    import { fetchWithTimeout } from "../../utils/fetch.js";

    export class GeminiStreaming {
      constructor(
        private readonly baseUrl: string,
        private readonly apiKey: string
      ) {}

      async *execute(request: ChatRequest, controller?: AbortController): AsyncGenerator<ChatChunk> {
        const abortController = controller || new AbortController();
        const temperature = Capabilities.normalizeTemperature(request.temperature, request.model);
        const url = `${this.baseUrl}/models/${request.model}:streamGenerateContent?alt=sse&key=${this.apiKey}`;

        const { contents, systemInstructionParts } = await GeminiChatUtils.convertMessages(
          request.messages
        );

        const generationConfig: Record<string, unknown> = {
          temperature: temperature ?? undefined,
          maxOutputTokens: request.max_tokens
        };

        if (request.response_format?.type === "json_object") {
          generationConfig.responseMimeType = "application/json";
        } else if (request.response_format?.type === "json_schema") {
          generationConfig.responseMimeType = "application/json";
          Eif (request.response_format.json_schema?.schema) {
            generationConfig.responseSchema = this.sanitizeSchema(
              request.response_format.json_schema.schema
            );
          }
        }

        const {
          model: _model,
          messages: _messages,
          tools: _tools,
          temperature: _temp,
          max_tokens: _max,
          response_format: _format,
          headers: _headers,
          requestTimeout: _requestTimeout,
          thinking: _thinking,
          ...rest
        } = request;

        const payload: Record<string, unknown> = {
          contents,
          generationConfig: {
            ...generationConfig,
            ...((rest.generationConfig as Record<string, unknown>) || {})
          },
          ...rest
        };

        if (_thinking) {
          payload.thinkingConfig = {
            includeThoughts: true
          };
        }

        Iif (systemInstructionParts.length > 0) {
          payload.systemInstruction = { parts: systemInstructionParts };
        }

        if (request.tools && request.tools.length > 0) {
          payload.tools = [
            {
              functionDeclarations: request.tools.map((t) => ({
                name: t.function.name,
                description: t.function.description,
                parameters: t.function.parameters
              }))
            }
          ];
        }

        let done = false;
        const toolCalls: ToolCall[] = [];

        try {
          logger.logRequest("Gemini", "POST", url, payload);

          const response = await fetchWithTimeout(
            url,
            {
              method: "POST",
              headers: {
                "Content-Type": "application/json"
              },
              body: JSON.stringify(payload),
              signal: abortController.signal
            },
            request.requestTimeout
          );

          if (!response.ok) {
            await handleGeminiError(response, request.model);
          }

          logger.debug("Gemini streaming started", {
            status: response.status,
            statusText: response.statusText
          });

          if (!response.body) {
            throw new Error("No response body for streaming");
          }

          const reader = response.body.getReader();
          const decoder = new TextDecoder();
          let buffer = "";

          while (true) {
            const { value, done: readerDone } = await reader.read();
            if (readerDone) {
              // Yield tool calls if any were collected
              if (toolCalls.length > 0) {
                yield { content: "", tool_calls: toolCalls, done: true };
              }
              break;
            }

            buffer += decoder.decode(value, { stream: true });

            let lineEnd;
            while ((lineEnd = buffer.indexOf("\n")) !== -1) {
              let line = buffer.substring(0, lineEnd).trim();
              buffer = buffer.substring(lineEnd + 1);

              // Handle carriage returns
              Iif (line.endsWith("\r")) {
                line = line.substring(0, line.length - 1);
              }

              if (line.startsWith("data: ")) {
                const data = line.substring(6).trim();
                Iif (!data) continue;

                try {
                  const json = JSON.parse(data) as GeminiGenerateContentResponse;
                  const parts = json.candidates?.[0]?.content?.parts || [];
                  for (const part of parts) {
                    if (part.text) {
                      if (part.thought) {
                        yield { content: "", thinking: { text: part.text } };
                      } else {
                        yield { content: part.text };
                      }
                    }
                    // Handle function calls
                    if (part.functionCall) {
                      toolCalls.push({
                        id: part.functionCall.name, // Gemini uses name as ID
                        type: "function" as const,
                        function: {
                          name: part.functionCall.name,
                          arguments: JSON.stringify(part.functionCall.args || {})
                        }
                      });
                    }
                  }
                } catch {
                  // Ignore parse errors
                }
              }
            }
          }
          done = true;
        } catch (e) {
          // Graceful exit on abort
          if (e instanceof Error && e.name === "AbortError") {
            return;
          }
          throw e;
        } finally {
          // Cleanup: abort if user breaks early
          if (!done) {
            abortController.abort();
          }
        }
      }

      private sanitizeSchema(schema: unknown): unknown {
        Iif (typeof schema !== "object" || schema === null) return schema;

        const sanitized = { ...(schema as Record<string, unknown>) };

        // Remove unsupported fields
        delete sanitized.additionalProperties;
        delete sanitized.$schema;
        delete sanitized.$id;
        delete sanitized.definitions;

        // Recursively sanitize
        if (sanitized.properties && typeof sanitized.properties === "object") {
          const props = sanitized.properties as Record<string, unknown>;
          for (const key in props) {
            props[key] = this.sanitizeSchema(props[key]);
          }
        }

        Iif (sanitized.items) {
          sanitized.items = this.sanitizeSchema(sanitized.items);
        }

        return sanitized;
      }
    }