UNPKG

jorel

Version:

The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.

87 lines (86 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertLlmMessagesToMistralMessages = void 0; const tools_1 = require("../../tools"); /** Convert unified LLM messages to Mistral messages */ const convertLlmMessagesToMistralMessages = async (messages, detail) => { const convertedMessages = []; for (const message of messages) { switch (message.role) { case "system": case "assistant": convertedMessages.push({ role: message.role, content: message.content, }); break; case "assistant_with_tools": convertedMessages.push({ role: "assistant", content: message.content, toolCalls: message.toolCalls.map((toolCall) => ({ id: toolCall.request.id, type: "function", function: { name: toolCall.request.function.name, arguments: tools_1.LlmToolKit.serialize(toolCall.request.function.arguments), }, })), }); // Handle tool responses for (const toolCall of message.toolCalls) { if (toolCall.executionState === "completed") { convertedMessages.push({ role: "tool", content: tools_1.LlmToolKit.serialize(toolCall.result), toolCallId: toolCall.request.id, }); } else if (toolCall.executionState === "error") { convertedMessages.push({ role: "tool", content: toolCall.error.message, toolCallId: toolCall.request.id, }); } } break; case "user": convertedMessages.push(await convertUserMessage(message, detail)); break; default: throw new Error(`Unsupported message role: ${message.role}`); } } return convertedMessages; }; exports.convertLlmMessagesToMistralMessages = convertLlmMessagesToMistralMessages; /** Convert user message content to Mistral format */ async function convertUserMessage(message, detail) { if (!Array.isArray(message.content)) { throw new Error("User message content must be string or array"); } const content = []; for (const entry of message.content) { switch (entry.type) { case "text": content.push({ type: "text", text: entry.text }); break; case "imageUrl": content.push({ type: "image_url", imageUrl: { url: entry.url, detail }, }); break; case "imageData": content.push({ type: "image_url", imageUrl: { url: entry.data, detail }, }); break; default: throw new Error(`Unsupported content type: ${entry.type}`); } } return { role: "user", content }; }