transform.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import type { APICallError, ModelMessage } from "ai"
  2. import { unique } from "remeda"
  3. import type { JSONSchema } from "zod/v4/core"
  4. import type { Provider } from "./provider"
  5. import type { ModelsDev } from "./models"
  6. type Modality = NonNullable<ModelsDev.Model["modalities"]>["input"][number]
  7. function mimeToModality(mime: string): Modality | undefined {
  8. if (mime.startsWith("image/")) return "image"
  9. if (mime.startsWith("audio/")) return "audio"
  10. if (mime.startsWith("video/")) return "video"
  11. if (mime === "application/pdf") return "pdf"
  12. return undefined
  13. }
  14. export namespace ProviderTransform {
  15. function normalizeMessages(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
  16. if (model.api.id.includes("claude")) {
  17. return msgs.map((msg) => {
  18. if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) {
  19. msg.content = msg.content.map((part) => {
  20. if ((part.type === "tool-call" || part.type === "tool-result") && "toolCallId" in part) {
  21. return {
  22. ...part,
  23. toolCallId: part.toolCallId.replace(/[^a-zA-Z0-9_-]/g, "_"),
  24. }
  25. }
  26. return part
  27. })
  28. }
  29. return msg
  30. })
  31. }
  32. if (model.providerID === "mistral" || model.api.id.toLowerCase().includes("mistral")) {
  33. const result: ModelMessage[] = []
  34. for (let i = 0; i < msgs.length; i++) {
  35. const msg = msgs[i]
  36. const nextMsg = msgs[i + 1]
  37. if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) {
  38. msg.content = msg.content.map((part) => {
  39. if ((part.type === "tool-call" || part.type === "tool-result") && "toolCallId" in part) {
  40. // Mistral requires alphanumeric tool call IDs with exactly 9 characters
  41. const normalizedId = part.toolCallId
  42. .replace(/[^a-zA-Z0-9]/g, "") // Remove non-alphanumeric characters
  43. .substring(0, 9) // Take first 9 characters
  44. .padEnd(9, "0") // Pad with zeros if less than 9 characters
  45. return {
  46. ...part,
  47. toolCallId: normalizedId,
  48. }
  49. }
  50. return part
  51. })
  52. }
  53. result.push(msg)
  54. // Fix message sequence: tool messages cannot be followed by user messages
  55. if (msg.role === "tool" && nextMsg?.role === "user") {
  56. result.push({
  57. role: "assistant",
  58. content: [
  59. {
  60. type: "text",
  61. text: "Done.",
  62. },
  63. ],
  64. })
  65. }
  66. }
  67. return result
  68. }
  69. if (
  70. model.capabilities.interleaved &&
  71. typeof model.capabilities.interleaved === "object" &&
  72. model.capabilities.interleaved.field === "reasoning_content"
  73. ) {
  74. return msgs.map((msg) => {
  75. if (msg.role === "assistant" && Array.isArray(msg.content)) {
  76. const reasoningParts = msg.content.filter((part: any) => part.type === "reasoning")
  77. const reasoningText = reasoningParts.map((part: any) => part.text).join("")
  78. // Filter out reasoning parts from content
  79. const filteredContent = msg.content.filter((part: any) => part.type !== "reasoning")
  80. // Include reasoning_content directly on the message for all assistant messages
  81. if (reasoningText) {
  82. return {
  83. ...msg,
  84. content: filteredContent,
  85. providerOptions: {
  86. ...msg.providerOptions,
  87. openaiCompatible: {
  88. ...(msg.providerOptions as any)?.openaiCompatible,
  89. reasoning_content: reasoningText,
  90. },
  91. },
  92. }
  93. }
  94. return {
  95. ...msg,
  96. content: filteredContent,
  97. }
  98. }
  99. return msg
  100. })
  101. }
  102. return msgs
  103. }
  104. function applyCaching(msgs: ModelMessage[], providerID: string): ModelMessage[] {
  105. const system = msgs.filter((msg) => msg.role === "system").slice(0, 2)
  106. const final = msgs.filter((msg) => msg.role !== "system").slice(-2)
  107. const providerOptions = {
  108. anthropic: {
  109. cacheControl: { type: "ephemeral" },
  110. },
  111. openrouter: {
  112. cache_control: { type: "ephemeral" },
  113. },
  114. bedrock: {
  115. cachePoint: { type: "ephemeral" },
  116. },
  117. openaiCompatible: {
  118. cache_control: { type: "ephemeral" },
  119. },
  120. }
  121. for (const msg of unique([...system, ...final])) {
  122. const shouldUseContentOptions = providerID !== "anthropic" && Array.isArray(msg.content) && msg.content.length > 0
  123. if (shouldUseContentOptions) {
  124. const lastContent = msg.content[msg.content.length - 1]
  125. if (lastContent && typeof lastContent === "object") {
  126. lastContent.providerOptions = {
  127. ...lastContent.providerOptions,
  128. ...providerOptions,
  129. }
  130. continue
  131. }
  132. }
  133. msg.providerOptions = {
  134. ...msg.providerOptions,
  135. ...providerOptions,
  136. }
  137. }
  138. return msgs
  139. }
  140. function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
  141. return msgs.map((msg) => {
  142. if (msg.role !== "user" || !Array.isArray(msg.content)) return msg
  143. const filtered = msg.content.map((part) => {
  144. if (part.type !== "file" && part.type !== "image") return part
  145. // Check for empty base64 image data
  146. if (part.type === "image") {
  147. const imageStr = part.image.toString()
  148. if (imageStr.startsWith("data:")) {
  149. const match = imageStr.match(/^data:([^;]+);base64,(.*)$/)
  150. if (match && (!match[2] || match[2].length === 0)) {
  151. return {
  152. type: "text" as const,
  153. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  154. }
  155. }
  156. }
  157. }
  158. const mime = part.type === "image" ? part.image.toString().split(";")[0].replace("data:", "") : part.mediaType
  159. const filename = part.type === "file" ? part.filename : undefined
  160. const modality = mimeToModality(mime)
  161. if (!modality) return part
  162. if (model.capabilities.input[modality]) return part
  163. const name = filename ? `"${filename}"` : modality
  164. return {
  165. type: "text" as const,
  166. text: `ERROR: Cannot read ${name} (this model does not support ${modality} input). Inform the user.`,
  167. }
  168. })
  169. return { ...msg, content: filtered }
  170. })
  171. }
  172. export function message(msgs: ModelMessage[], model: Provider.Model) {
  173. msgs = unsupportedParts(msgs, model)
  174. msgs = normalizeMessages(msgs, model)
  175. if (
  176. model.providerID === "anthropic" ||
  177. model.api.id.includes("anthropic") ||
  178. model.api.id.includes("claude") ||
  179. model.api.npm === "@ai-sdk/anthropic"
  180. ) {
  181. msgs = applyCaching(msgs, model.providerID)
  182. }
  183. return msgs
  184. }
  185. export function temperature(model: Provider.Model) {
  186. const id = model.id.toLowerCase()
  187. if (id.includes("qwen")) return 0.55
  188. if (id.includes("claude")) return undefined
  189. if (id.includes("gemini-3-pro")) return 1.0
  190. if (id.includes("glm-4.6")) return 1.0
  191. if (id.includes("minimax-m2")) return 1.0
  192. if (id.includes("kimi-k2")) {
  193. if (id.includes("thinking")) return 1.0
  194. return 0.6
  195. }
  196. return undefined
  197. }
  198. export function topP(model: Provider.Model) {
  199. const id = model.id.toLowerCase()
  200. if (id.includes("qwen")) return 1
  201. if (id.includes("minimax-m2")) return 0.95
  202. return undefined
  203. }
  204. export function topK(model: Provider.Model) {
  205. const id = model.id.toLowerCase()
  206. if (id.includes("minimax-m2")) return 40
  207. return undefined
  208. }
  209. export function options(
  210. model: Provider.Model,
  211. sessionID: string,
  212. providerOptions?: Record<string, any>,
  213. ): Record<string, any> {
  214. const result: Record<string, any> = {}
  215. if (model.api.npm === "@openrouter/ai-sdk-provider") {
  216. result["usage"] = {
  217. include: true,
  218. }
  219. if (model.api.id.includes("gemini-3")) {
  220. result["reasoning"] = { effort: "high" }
  221. }
  222. }
  223. if (
  224. model.providerID === "baseten" ||
  225. (model.providerID === "opencode" && ["kimi-k2-thinking", "glm-4.6"].includes(model.api.id))
  226. ) {
  227. result["chat_template_args"] = { enable_thinking: true }
  228. }
  229. if (model.providerID === "openai" || providerOptions?.setCacheKey) {
  230. result["promptCacheKey"] = sessionID
  231. }
  232. if (model.api.npm === "@ai-sdk/google" || model.api.npm === "@ai-sdk/google-vertex") {
  233. result["thinkingConfig"] = {
  234. includeThoughts: true,
  235. }
  236. if (model.api.id.includes("gemini-3")) {
  237. result["thinkingConfig"]["thinkingLevel"] = "high"
  238. }
  239. }
  240. if (model.api.id.includes("gpt-5") && !model.api.id.includes("gpt-5-chat")) {
  241. if (model.providerID.includes("codex")) {
  242. result["store"] = false
  243. }
  244. if (!model.api.id.includes("codex") && !model.api.id.includes("gpt-5-pro")) {
  245. result["reasoningEffort"] = "medium"
  246. }
  247. if (model.api.id.endsWith("gpt-5.") && model.providerID !== "azure") {
  248. result["textVerbosity"] = "low"
  249. }
  250. if (model.providerID.startsWith("opencode")) {
  251. result["promptCacheKey"] = sessionID
  252. result["include"] = ["reasoning.encrypted_content"]
  253. result["reasoningSummary"] = "auto"
  254. }
  255. }
  256. return result
  257. }
  258. export function smallOptions(model: Provider.Model) {
  259. const options: Record<string, any> = {}
  260. if (model.providerID === "openai" || model.api.id.includes("gpt-5")) {
  261. if (model.api.id.includes("5.")) {
  262. options["reasoningEffort"] = "low"
  263. } else {
  264. options["reasoningEffort"] = "minimal"
  265. }
  266. }
  267. if (model.providerID === "google") {
  268. options["thinkingConfig"] = {
  269. thinkingBudget: 0,
  270. }
  271. }
  272. return options
  273. }
  274. export function providerOptions(model: Provider.Model, options: { [x: string]: any }) {
  275. switch (model.api.npm) {
  276. case "@ai-sdk/openai":
  277. case "@ai-sdk/azure":
  278. return {
  279. ["openai" as string]: options,
  280. }
  281. case "@ai-sdk/amazon-bedrock":
  282. return {
  283. ["bedrock" as string]: options,
  284. }
  285. case "@ai-sdk/anthropic":
  286. return {
  287. ["anthropic" as string]: options,
  288. }
  289. case "@ai-sdk/google":
  290. return {
  291. ["google" as string]: options,
  292. }
  293. case "@ai-sdk/gateway":
  294. return {
  295. ["gateway" as string]: options,
  296. }
  297. case "@openrouter/ai-sdk-provider":
  298. return {
  299. ["openrouter" as string]: options,
  300. }
  301. default:
  302. return {
  303. [model.providerID]: options,
  304. }
  305. }
  306. }
  307. export function maxOutputTokens(
  308. npm: string,
  309. options: Record<string, any>,
  310. modelLimit: number,
  311. globalLimit: number,
  312. ): number {
  313. const modelCap = modelLimit || globalLimit
  314. const standardLimit = Math.min(modelCap, globalLimit)
  315. if (npm === "@ai-sdk/anthropic") {
  316. const thinking = options?.["thinking"]
  317. const budgetTokens = typeof thinking?.["budgetTokens"] === "number" ? thinking["budgetTokens"] : 0
  318. const enabled = thinking?.["type"] === "enabled"
  319. if (enabled && budgetTokens > 0) {
  320. // Return text tokens so that text + thinking <= model cap, preferring 32k text when possible.
  321. if (budgetTokens + standardLimit <= modelCap) {
  322. return standardLimit
  323. }
  324. return modelCap - budgetTokens
  325. }
  326. }
  327. return standardLimit
  328. }
  329. export function schema(model: Provider.Model, schema: JSONSchema.BaseSchema) {
  330. /*
  331. if (["openai", "azure"].includes(providerID)) {
  332. if (schema.type === "object" && schema.properties) {
  333. for (const [key, value] of Object.entries(schema.properties)) {
  334. if (schema.required?.includes(key)) continue
  335. schema.properties[key] = {
  336. anyOf: [
  337. value as JSONSchema.JSONSchema,
  338. {
  339. type: "null",
  340. },
  341. ],
  342. }
  343. }
  344. }
  345. }
  346. */
  347. // Convert integer enums to string enums for Google/Gemini
  348. if (model.providerID === "google" || model.api.id.includes("gemini")) {
  349. const sanitizeGemini = (obj: any): any => {
  350. if (obj === null || typeof obj !== "object") {
  351. return obj
  352. }
  353. if (Array.isArray(obj)) {
  354. return obj.map(sanitizeGemini)
  355. }
  356. const result: any = {}
  357. for (const [key, value] of Object.entries(obj)) {
  358. if (key === "enum" && Array.isArray(value)) {
  359. // Convert all enum values to strings
  360. result[key] = value.map((v) => String(v))
  361. // If we have integer type with enum, change type to string
  362. if (result.type === "integer" || result.type === "number") {
  363. result.type = "string"
  364. }
  365. } else if (typeof value === "object" && value !== null) {
  366. result[key] = sanitizeGemini(value)
  367. } else {
  368. result[key] = value
  369. }
  370. }
  371. // Filter required array to only include fields that exist in properties
  372. if (result.type === "object" && result.properties && Array.isArray(result.required)) {
  373. result.required = result.required.filter((field: any) => field in result.properties)
  374. }
  375. if (result.type === "array" && result.items == null) {
  376. result.items = {}
  377. }
  378. return result
  379. }
  380. schema = sanitizeGemini(schema)
  381. }
  382. return schema
  383. }
  384. export function error(providerID: string, error: APICallError) {
  385. let message = error.message
  386. if (providerID === "github-copilot" && message.includes("The requested model is not supported")) {
  387. return (
  388. message +
  389. "\n\nMake sure the model is enabled in your copilot settings: https://github.com/settings/copilot/features"
  390. )
  391. }
  392. return message
  393. }
  394. }