All files / src/providers/gemini ChatUtils.ts

82.35% Statements 28/34
71.42% Branches 30/42
100% Functions 1/1
82.35% Lines 28/34

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                25x 25x   25x 31x 1x 1x   30x 30x   30x 2x           2x   28x   28x 24x 4x 2x 4x 2x 2x 2x 2x 2x                                                       28x 3x 2x                 28x 28x           25x      
import { Message } from "../../chat/Message.js";
import { GeminiContent, GeminiPart } from "./types.js";
import { BinaryUtils } from "../../utils/Binary.js";
 
export class GeminiChatUtils {
  static async convertMessages(
    messages: Message[]
  ): Promise<{ contents: GeminiContent[]; systemInstructionParts: GeminiPart[] }> {
    const contents: GeminiContent[] = [];
    const systemInstructionParts: GeminiPart[] = [];
 
    for (const msg of messages) {
      if (msg.role === "system" || msg.role === "developer") {
        Eif (msg.content) {
          systemInstructionParts.push({ text: String(msg.content) });
        }
      E} else if (msg.role === "user" || msg.role === "assistant" || msg.role === "tool") {
        const parts: GeminiPart[] = [];
 
        if (msg.role === "tool") {
          parts.push({
            functionResponse: {
              name: msg.tool_call_id || "unknown",
              response: { result: msg.content }
            }
          });
          contents.push({ role: "user", parts });
        } else {
          const role = msg.role === "assistant" ? "model" : "user";
 
          if (msg.content && (typeof msg.content === "string" || msg.content instanceof String)) {
            parts.push({ text: String(msg.content) });
          } else if (Array.isArray(msg.content)) {
            for (const part of msg.content) {
              if (part.type === "text") {
                parts.push({ text: part.text });
              } else if (part.type === "image_url") {
                const binary = await BinaryUtils.toBase64(part.image_url.url);
                Eif (binary) {
                  parts.push({
                    inlineData: {
                      mimeType: binary.mimeType,
                      data: binary.data
                    }
                  });
                }
              E} else if (part.type === "input_audio") {
                parts.push({
                  inlineData: {
                    mimeType: `audio/${part.input_audio.format}`,
                    data: part.input_audio.data
                  }
                });
              } else if (part.type === "video_url") {
                const binary = await BinaryUtils.toBase64(part.video_url.url);
                if (binary) {
                  parts.push({
                    inlineData: {
                      mimeType: binary.mimeType,
                      data: binary.data
                    }
                  });
                }
              }
            }
          }
 
          if (msg.role === "assistant" && msg.tool_calls) {
            for (const call of msg.tool_calls) {
              parts.push({
                functionCall: {
                  name: call.function.name,
                  args: JSON.parse(call.function.arguments)
                }
              });
            }
          }
 
          Eif (parts.length > 0) {
            contents.push({ role, parts });
          }
        }
      }
    }
 
    return { contents, systemInstructionParts };
  }
}