# All files / src/chat Content.ts

90.47% Statements 19/21
70.58% Branches 12/17
100% Functions 5/5
89.47% Lines 17/19

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

    80x
    22x

    80x

    80x
    9x

    80x

    9x

    9x
    9x

    9x
    1x
    1x
    1x

    9x
    8x
    8x

    1x

     export type ContentPart =
      | { type: "text"; text: string }
      | { type: "image_url"; image_url: { url: string } }
      | { type: "input_audio"; input_audio: { data: string; format: string } }
      | { type: "video_url"; video_url: { url: string } };

    export const isBinaryContent = (part: ContentPart): boolean =>
      part.type === "image_url" || part.type === "input_audio" || part.type === "video_url";

    export const isTextContent = (part: ContentPart): boolean => part.type === "text";

    export const partitionContentParts = (parts: ContentPart[]) => {
      return {
        textParts: parts.filter(isTextContent) as { type: "text"; text: string }[],
        binaryParts: parts.filter(isBinaryContent)
      };
    };

    export const formatMultimodalContent = (
      content: string | ContentPart[],
      parts: ContentPart[]
    ): MessageContent => {
      const { textParts, binaryParts } = partitionContentParts(parts);

      let fullText = typeof content === "string" ? content : "";
      const currentParts: ContentPart[] = typeof content === "string" ? [] : content;

      if (textParts.length > 0) {
        const additionalText = textParts.map((f) => f.text).join("\n");
        if (typeof content === "string") {
          fullText += "\n" + additionalText;
        } else E{
          currentParts.push({ type: "text", text: additionalText });
        }
      }

      if (binaryParts.length > 0) {
        if (typeof content === "string") {
          return [{ type: "text", text: fullText }, ...binaryParts];
        } else E{
          return [...currentParts, ...binaryParts];
        }
      }

      return typeof content === "string" ? fullText : currentParts;
    };

    export type MessageContent = string | string | ContentPart[];