|
| 1 | +import { |
| 2 | + type LanguageModelV2Prompt, |
| 3 | + type SharedV2ProviderMetadata, |
| 4 | + UnsupportedFunctionalityError, |
| 5 | +} from "@ai-sdk/provider" |
| 6 | +import type { OpenAICompatibleChatPrompt } from "./openai-compatible-api-types" |
| 7 | +import { convertToBase64 } from "@ai-sdk/provider-utils" |
| 8 | + |
| 9 | +function getOpenAIMetadata(message: { providerOptions?: SharedV2ProviderMetadata }) { |
| 10 | + return message?.providerOptions?.copilot ?? {} |
| 11 | +} |
| 12 | + |
| 13 | +export function convertToOpenAICompatibleChatMessages(prompt: LanguageModelV2Prompt): OpenAICompatibleChatPrompt { |
| 14 | + const messages: OpenAICompatibleChatPrompt = [] |
| 15 | + for (const { role, content, ...message } of prompt) { |
| 16 | + const metadata = getOpenAIMetadata({ ...message }) |
| 17 | + switch (role) { |
| 18 | + case "system": { |
| 19 | + messages.push({ |
| 20 | + role: "system", |
| 21 | + content: [ |
| 22 | + { |
| 23 | + type: "text", |
| 24 | + text: content, |
| 25 | + }, |
| 26 | + ], |
| 27 | + ...metadata, |
| 28 | + }) |
| 29 | + break |
| 30 | + } |
| 31 | + |
| 32 | + case "user": { |
| 33 | + if (content.length === 1 && content[0].type === "text") { |
| 34 | + messages.push({ |
| 35 | + role: "user", |
| 36 | + content: content[0].text, |
| 37 | + ...getOpenAIMetadata(content[0]), |
| 38 | + }) |
| 39 | + break |
| 40 | + } |
| 41 | + |
| 42 | + messages.push({ |
| 43 | + role: "user", |
| 44 | + content: content.map((part) => { |
| 45 | + const partMetadata = getOpenAIMetadata(part) |
| 46 | + switch (part.type) { |
| 47 | + case "text": { |
| 48 | + return { type: "text", text: part.text, ...partMetadata } |
| 49 | + } |
| 50 | + case "file": { |
| 51 | + if (part.mediaType.startsWith("image/")) { |
| 52 | + const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType |
| 53 | + |
| 54 | + return { |
| 55 | + type: "image_url", |
| 56 | + image_url: { |
| 57 | + url: |
| 58 | + part.data instanceof URL |
| 59 | + ? part.data.toString() |
| 60 | + : `data:${mediaType};base64,${convertToBase64(part.data)}`, |
| 61 | + }, |
| 62 | + ...partMetadata, |
| 63 | + } |
| 64 | + } else { |
| 65 | + throw new UnsupportedFunctionalityError({ |
| 66 | + functionality: `file part media type ${part.mediaType}`, |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }), |
| 72 | + ...metadata, |
| 73 | + }) |
| 74 | + |
| 75 | + break |
| 76 | + } |
| 77 | + |
| 78 | + case "assistant": { |
| 79 | + let text = "" |
| 80 | + let reasoningText: string | undefined |
| 81 | + let reasoningOpaque: string | undefined |
| 82 | + const toolCalls: Array<{ |
| 83 | + id: string |
| 84 | + type: "function" |
| 85 | + function: { name: string; arguments: string } |
| 86 | + }> = [] |
| 87 | + |
| 88 | + for (const part of content) { |
| 89 | + const partMetadata = getOpenAIMetadata(part) |
| 90 | + // Check for reasoningOpaque on any part (may be attached to text/tool-call) |
| 91 | + const partOpaque = (part.providerOptions as { copilot?: { reasoningOpaque?: string } })?.copilot |
| 92 | + ?.reasoningOpaque |
| 93 | + if (partOpaque && !reasoningOpaque) { |
| 94 | + reasoningOpaque = partOpaque |
| 95 | + } |
| 96 | + |
| 97 | + switch (part.type) { |
| 98 | + case "text": { |
| 99 | + text += part.text |
| 100 | + break |
| 101 | + } |
| 102 | + case "reasoning": { |
| 103 | + reasoningText = part.text |
| 104 | + break |
| 105 | + } |
| 106 | + case "tool-call": { |
| 107 | + toolCalls.push({ |
| 108 | + id: part.toolCallId, |
| 109 | + type: "function", |
| 110 | + function: { |
| 111 | + name: part.toolName, |
| 112 | + arguments: JSON.stringify(part.input), |
| 113 | + }, |
| 114 | + ...partMetadata, |
| 115 | + }) |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + messages.push({ |
| 122 | + role: "assistant", |
| 123 | + content: text || null, |
| 124 | + tool_calls: toolCalls.length > 0 ? toolCalls : undefined, |
| 125 | + reasoning_text: reasoningText, |
| 126 | + reasoning_opaque: reasoningOpaque, |
| 127 | + ...metadata, |
| 128 | + }) |
| 129 | + |
| 130 | + break |
| 131 | + } |
| 132 | + |
| 133 | + case "tool": { |
| 134 | + for (const toolResponse of content) { |
| 135 | + const output = toolResponse.output |
| 136 | + |
| 137 | + let contentValue: string |
| 138 | + switch (output.type) { |
| 139 | + case "text": |
| 140 | + case "error-text": |
| 141 | + contentValue = output.value |
| 142 | + break |
| 143 | + case "content": |
| 144 | + case "json": |
| 145 | + case "error-json": |
| 146 | + contentValue = JSON.stringify(output.value) |
| 147 | + break |
| 148 | + } |
| 149 | + |
| 150 | + const toolResponseMetadata = getOpenAIMetadata(toolResponse) |
| 151 | + messages.push({ |
| 152 | + role: "tool", |
| 153 | + tool_call_id: toolResponse.toolCallId, |
| 154 | + content: contentValue, |
| 155 | + ...toolResponseMetadata, |
| 156 | + }) |
| 157 | + } |
| 158 | + break |
| 159 | + } |
| 160 | + |
| 161 | + default: { |
| 162 | + const _exhaustiveCheck: never = role |
| 163 | + throw new Error(`Unsupported role: ${_exhaustiveCheck}`) |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return messages |
| 169 | +} |
0 commit comments