Spaces:
Sleeping
Sleeping
File size: 1,366 Bytes
5b1a9aa 06feee8 5b1a9aa |
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 |
import { makeImageProcessor, type ImageProcessorOptions } from "../images";
import type { EndpointMessage } from "../endpoints";
import type { MessageFile } from "$lib/types/Message";
import type { ImageBlockParam, MessageParam } from "@anthropic-ai/sdk/resources/messages.mjs";
export async function fileToImageBlock(
file: MessageFile,
opts: ImageProcessorOptions<"image/png" | "image/jpeg" | "image/webp">
): Promise<ImageBlockParam> {
const processor = makeImageProcessor(opts);
const { image, mime } = await processor(file);
return {
type: "image",
source: {
type: "base64",
media_type: mime,
data: image.toString("base64"),
},
};
}
type NonSystemMessage = EndpointMessage & { from: "user" | "assistant" };
export async function endpointMessagesToAnthropicMessages(
messages: EndpointMessage[],
multimodal: { image: ImageProcessorOptions<"image/png" | "image/jpeg" | "image/webp"> }
): Promise<MessageParam[]> {
return await Promise.all(
messages
.filter((message): message is NonSystemMessage => message.from !== "system")
.map<Promise<MessageParam>>(async (message) => {
return {
role: message.from,
content: [
...(await Promise.all(
(message.files ?? []).map((file) => fileToImageBlock(file, multimodal.image))
)),
{ type: "text", text: message.content },
],
};
})
);
}
|