All files / src/providers/anthropic Utils.ts

92.98% Statements 53/57
73.43% Branches 47/64
100% Functions 5/5
94.64% Lines 53/56

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            30x 34x 2x 1x 1x 1x 2x 2x             30x       32x   32x 36x   36x 36x   36x   1x       1x       1x   35x     32x       36x     36x 2x                           34x 2x     2x 2x 2x       2x 2x             2x     32x   36x 27x     5x         5x 5x 5x 9x 4x 5x 5x 5x 5x 5x 5x   5x 5x 5x   5x 3x                 2x                           5x    
import { Message } from "../../chat/Message.js";
import { ContentPart } from "../../chat/Content.js";
import { AnthropicMessage, AnthropicContentBlock } from "./types.js";
 
export function formatSystemPrompt(messages: Message[]): string | undefined {
  let systemPrompt: string | undefined;
  for (const msg of messages) {
    if (msg.role === "system" || msg.role === "developer") {
      if (typeof msg.content === "string") {
        systemPrompt = msg.content;
      } else if (Array.isArray(msg.content)) {
        systemPrompt = msg.content
          .filter((p): p is ContentPart & { type: "text" } => p.type === "text")
          .map((p) => p.text)
          .join("\n");
      E} else if (msg.content) {
        systemPrompt = String(msg.content);
      }
    }
  }
  return systemPrompt;
}
 
export function formatMessages(requestMessages: Message[]): AnthropicMessage[] {
  const messages: AnthropicMessage[] = [];
 
  for (const msg of requestMessages) {
    Iif (msg.role === "system" || msg.role === "developer") continue;
 
    const formatted = formatSingleMessage(msg);
    const lastMsg = messages[messages.length - 1];
 
    if (lastMsg && lastMsg.role === formatted.role) {
      // Merge content
      const existingContent = Array.isArray(lastMsg.content)
        ? lastMsg.content
        : [{ type: "text", text: lastMsg.content } as AnthropicContentBlock];
 
      const newContent = Array.isArray(formatted.content)
        ? formatted.content
        : [{ type: "text", text: formatted.content } as AnthropicContentBlock];
 
      lastMsg.content = [...existingContent, ...newContent];
    } else {
      messages.push(formatted);
    }
  }
  return messages;
}
 
function formatSingleMessage(msg: Message): AnthropicMessage {
  const role: "user" | "assistant" = msg.role === "user" ? "user" : "assistant";
 
  // Handle Tool Responses (role: "tool")
  if (msg.role === "tool") {
    return {
      role: "user",
      content: [
        {
          type: "tool_result",
          tool_use_id: msg.tool_call_id!,
          content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content),
          is_error: msg.isError
        }
      ]
    };
  }
 
  // Handle Assistant Messages (which might have tool_calls)
  if (msg.role === "assistant" && msg.tool_calls) {
    const blocks: AnthropicContentBlock[] = [];
 
    // Assistant text content
    const text = String(msg.content || "");
    Eif (text.length > 0) {
      blocks.push({ type: "text", text });
    }
 
    // Assistant tool uses
    for (const toolCall of msg.tool_calls) {
      blocks.push({
        type: "tool_use",
        id: toolCall.id,
        name: toolCall.function.name,
        input: JSON.parse(toolCall.function.arguments)
      });
    }
    return { role: "assistant", content: blocks };
  }
 
  const contentText = String(msg.content || "");
 
  if (contentText && typeof msg.content === "string") {
    return { role, content: contentText };
  }
 
  Iif (contentText && msg.content instanceof String) {
    return { role, content: contentText };
  }
 
  // Handle multimodal content (images)
  const blocks: AnthropicContentBlock[] = [];
  Eif (Array.isArray(msg.content)) {
    for (const part of msg.content) {
      if (part.type === "text") {
        blocks.push({ type: "text", text: part.text });
      E} else if (part.type === "image_url") {
        const url = part.image_url.url;
        Eif (url.startsWith("data:")) {
          const parts = url.split(",");
          const meta = parts[0];
          const data = parts.slice(1).join(",");
 
          Eif (meta && data) {
            const mimeMatch = meta.match(/:(.*?);/);
            const mediaType = mimeMatch ? mimeMatch[1] : "image/jpeg";
 
            if (mediaType === "application/pdf") {
              blocks.push({
                type: "document",
                source: {
                  type: "base64",
                  media_type: "application/pdf",
                  data: data
                }
              });
            } else {
              blocks.push({
                type: "image",
                source: {
                  type: "base64",
                  media_type: mediaType || "image/jpeg",
                  data: data
                }
              });
            }
          }
        }
      }
    }
  }
  return { role, content: blocks };
}