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

90.9% Statements 50/55
85.71% Branches 36/42
90% Functions 9/10
92.59% Lines 50/54

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

    15x
    15x

    11x
    11x

    11x

    11x

    11x
    1x
    10x
    2x
    2x
    2x

    11x

    11x

    11x

    11x

    11x
    2x

    2x

    11x

    11x

    11x

    11x
    11x
    11x

    11x
    9x

    11x
    9x
    8x

    11x
    9x
    1x

    11x

    11x

    11x

    11x

    11x

    11x

    11x
    11x
    11x
    11x

    11x
    4x
    4x
    6x

    11x
    1x

    11x

     import { ChatRequest, ChatResponse } from "../Provider.js";
    import { GeminiGenerateContentResponse } from "./types.js";
    import { Capabilities } from "./Capabilities.js";
    import { handleGeminiError } from "./Errors.js";
    import { GeminiChatUtils } from "./ChatUtils.js";
    import { ModelRegistry } from "../../models/ModelRegistry.js";
    import { logger } from "../../utils/logger.js";
    import { fetchWithTimeout } from "../../utils/fetch.js";

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

      async execute(request: ChatRequest): Promise<ChatResponse> {
        const temperature = Capabilities.normalizeTemperature(request.temperature, request.model);
        const url = `${this.baseUrl}/models/${request.model}:generateContent?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,
          ...rest
        } = request;

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

        Iif (request.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: this.sanitizeSchema(t.function.parameters)
              }))
            }
          ];
        }

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

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

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

        const json = (await response.json()) as GeminiGenerateContentResponse;
        logger.logResponse("Gemini", response.status, response.statusText, json);
        const candidate = json.candidates?.[0];

        const reasoningText =
          candidate?.content?.parts
            ?.filter((p) => p.thought)
            .map((p) => p.text)
            .join("\n") || null;

        const content =
          candidate?.content?.parts
            ?.filter((p) => !p.thought && p.text)
            .map((p) => p.text)
            .join("\n") || null;

        const tool_calls = candidate?.content?.parts
          ?.filter((p) => p.functionCall)
          .map((p) => ({
            id: p.functionCall!.name,
            type: "function" as const,
            function: {
              name: p.functionCall!.name,
              arguments: JSON.stringify(p.functionCall!.args)
            }
          }));

        const usage = json.usageMetadata
          ? {
              input_tokens: json.usageMetadata.promptTokenCount,
              output_tokens: json.usageMetadata.candidatesTokenCount,
              total_tokens: json.usageMetadata.totalTokenCount
            }
          : undefined;

        const calculatedUsage = usage
          ? ModelRegistry.calculateCost(usage, request.model, "gemini")
          : undefined;

        const thinkingResult = reasoningText ? { text: reasoningText } : undefined;

        return {
          content,
          tool_calls,
          usage: calculatedUsage,
          thinking: thinkingResult,
          reasoning: reasoningText
        };
      }

      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]);
          }
        }

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

        return sanitized;
      }
    }