UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

195 lines (194 loc) 5.9 kB
import { LangMessages } from "../../messages.js"; function prepareBodyPartForOpenAIResponsesAPI(messages) { var _a; let lastMessageWithResponseId; let lastMessageWithResponseIdIndex = -1; const bodyPart = { instructions: messages.instructions, tools: transformToolsForProvider(messages.availableTools || []) }; if (bodyPart.tools.length > 0) { bodyPart.tool_choice = "auto"; } for (let i = messages.length - 1; i >= 0; i--) { if (messages[i].role === "assistant") { if ((_a = messages[i].meta) == null ? void 0 : _a.openaiResponseId) { lastMessageWithResponseId = messages[i]; lastMessageWithResponseIdIndex = i; } break; } } if (lastMessageWithResponseId) { if (lastMessageWithResponseIdIndex < messages.length - 1) { const newMessages = messages.slice(lastMessageWithResponseIdIndex + 1); const newInput = transformMessagesToResponsesInput(new LangMessages(newMessages)); bodyPart.previous_response_id = lastMessageWithResponseId.meta.openaiResponseId; bodyPart.input = newInput; } else { bodyPart.previous_response_id = lastMessageWithResponseId.meta.openaiResponseId; bodyPart.input = []; } } else { bodyPart.input = transformMessagesToResponsesInput(messages); } return bodyPart; } function transformMessagesToResponsesInput(messages) { const input = []; for (const message of messages) { switch (message.role) { case "user": case "assistant": input.push(transformMessageToResponsesItems(message)); break; case "tool-results": input.push(...transformToolResultsToResponsesItems(message)); break; } } return input; } function transformMessageToResponsesItems(message) { var _a; const isAssistant = message.role === "assistant"; const msgItems = message.items; const entry = { role: message.role, content: [] }; for (const msgItem of msgItems) { switch (msgItem.type) { case "text": entry.content.push({ type: isAssistant ? "output_text" : "input_text", text: msgItem.text }); break; case "tool": entry.content.push({ type: "function_call", call_id: msgItem.callId, name: msgItem.name, arguments: JSON.stringify(msgItem.arguments || {}) }); break; case "image": { const mimeType = msgItem.mimeType || "image/png"; const imageUrl = (_a = msgItem.url) != null ? _a : msgItem.base64 ? `data:${mimeType};base64,${msgItem.base64}` : void 0; if (!imageUrl) { throw new Error("Image item must include either url or base64 data."); } if (isAssistant) { const revisedPrompt = "\n\nPrompt used to generate the image: " + msgItem.metadata.revisedPrompt; let text = `<revised>I generated an image but no longer have a reference to it.${revisedPrompt}</revised>`; entry.content.push({ type: "output_text", text }); } else { entry.content.push({ type: "input_image", image_url: imageUrl }); } break; } } } return entry; } function transformToolResultsToResponsesItems(message) { const items = []; for (const toolResult of message.toolResults) { if (toolResult.name === "apply_patch") { const result = toolResult.result; if (result && typeof result === "object" && "status" in result) { items.push({ type: "apply_patch_call_output", call_id: toolResult.callId, status: result.status, output: result.output || "" }); } else { items.push({ type: "apply_patch_call_output", call_id: toolResult.callId, status: "completed", output: typeof result === "string" ? result : JSON.stringify(result || {}) }); } } else { items.push({ type: "function_call_output", call_id: toolResult.callId, output: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result) }); } } return items; } function mapImageInput(image) { const kind = image.kind; if (kind === "url") { const url = image.url; return { type: "input_image", image_url: url }; } if (kind === "base64") { const base64 = image.base64; const mimeType = image.mimeType || "image/png"; const dataUrl = `data:${mimeType};base64,${base64}`; return { type: "input_image", image_url: dataUrl }; } throw new Error("Unsupported image kind for Responses mapping"); } function mapImageOutput(image) { const kind = image.kind; if (kind === "url") { const url = image.url; return { type: "output_text", text: url }; } if (kind === "base64") { const base64 = image.base64; const mimeType = image.mimeType || "image/png"; const dataUrl = `data:${mimeType};base64,${base64}`; return { type: "output_text", text: dataUrl }; } throw new Error(`Unsupported image kind '${kind}' for assistant messages in Responses API`); } function transformToolsForProvider(tools) { return tools.map((tool) => { if (tool.name === "apply_patch") { return { type: "apply_patch" }; } if ("handler" in tool) { return { type: "function", name: tool.name, description: tool.description, parameters: tool.parameters }; } return { type: tool.name }; }); } export { mapImageInput, mapImageOutput, prepareBodyPartForOpenAIResponsesAPI, transformMessageToResponsesItems, transformMessagesToResponsesInput, transformToolResultsToResponsesItems, transformToolsForProvider }; //# sourceMappingURL=openai-responses-messages.js.map