transform.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. import type { ModelMessage } from "ai"
  2. import { mergeDeep, unique } from "remeda"
  3. import type { JSONSchema7 } from "@ai-sdk/provider"
  4. import type { JSONSchema } from "zod/v4/core"
  5. import type { Provider } from "./provider"
  6. import type { ModelsDev } from "./models"
  7. import { iife } from "@/util/iife"
  8. import { Flag } from "@/flag/flag"
  9. type Modality = NonNullable<ModelsDev.Model["modalities"]>["input"][number]
  10. function mimeToModality(mime: string): Modality | undefined {
  11. if (mime.startsWith("image/")) return "image"
  12. if (mime.startsWith("audio/")) return "audio"
  13. if (mime.startsWith("video/")) return "video"
  14. if (mime === "application/pdf") return "pdf"
  15. return undefined
  16. }
  17. export namespace ProviderTransform {
  18. export const OUTPUT_TOKEN_MAX = Flag.OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX || 32_000
  19. // Maps npm package to the key the AI SDK expects for providerOptions
  20. function sdkKey(npm: string): string | undefined {
  21. switch (npm) {
  22. case "@ai-sdk/github-copilot":
  23. return "copilot"
  24. case "@ai-sdk/openai":
  25. case "@ai-sdk/azure":
  26. return "openai"
  27. case "@ai-sdk/amazon-bedrock":
  28. return "bedrock"
  29. case "@ai-sdk/anthropic":
  30. case "@ai-sdk/google-vertex/anthropic":
  31. return "anthropic"
  32. case "@ai-sdk/google-vertex":
  33. case "@ai-sdk/google":
  34. return "google"
  35. case "@ai-sdk/gateway":
  36. return "gateway"
  37. case "@openrouter/ai-sdk-provider":
  38. return "openrouter"
  39. }
  40. return undefined
  41. }
  42. function normalizeMessages(
  43. msgs: ModelMessage[],
  44. model: Provider.Model,
  45. options: Record<string, unknown>,
  46. ): ModelMessage[] {
  47. // Anthropic rejects messages with empty content - filter out empty string messages
  48. // and remove empty text/reasoning parts from array content
  49. if (model.api.npm === "@ai-sdk/anthropic") {
  50. msgs = msgs
  51. .map((msg) => {
  52. if (typeof msg.content === "string") {
  53. if (msg.content === "") return undefined
  54. return msg
  55. }
  56. if (!Array.isArray(msg.content)) return msg
  57. const filtered = msg.content.filter((part) => {
  58. if (part.type === "text" || part.type === "reasoning") {
  59. return part.text !== ""
  60. }
  61. return true
  62. })
  63. if (filtered.length === 0) return undefined
  64. return { ...msg, content: filtered }
  65. })
  66. .filter((msg): msg is ModelMessage => msg !== undefined && msg.content !== "")
  67. }
  68. if (model.api.id.includes("claude")) {
  69. return msgs.map((msg) => {
  70. if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) {
  71. msg.content = msg.content.map((part) => {
  72. if ((part.type === "tool-call" || part.type === "tool-result") && "toolCallId" in part) {
  73. return {
  74. ...part,
  75. toolCallId: part.toolCallId.replace(/[^a-zA-Z0-9_-]/g, "_"),
  76. }
  77. }
  78. return part
  79. })
  80. }
  81. return msg
  82. })
  83. }
  84. if (
  85. model.providerID === "mistral" ||
  86. model.api.id.toLowerCase().includes("mistral") ||
  87. model.api.id.toLocaleLowerCase().includes("devstral")
  88. ) {
  89. const result: ModelMessage[] = []
  90. for (let i = 0; i < msgs.length; i++) {
  91. const msg = msgs[i]
  92. const nextMsg = msgs[i + 1]
  93. if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) {
  94. msg.content = msg.content.map((part) => {
  95. if ((part.type === "tool-call" || part.type === "tool-result") && "toolCallId" in part) {
  96. // Mistral requires alphanumeric tool call IDs with exactly 9 characters
  97. const normalizedId = part.toolCallId
  98. .replace(/[^a-zA-Z0-9]/g, "") // Remove non-alphanumeric characters
  99. .substring(0, 9) // Take first 9 characters
  100. .padEnd(9, "0") // Pad with zeros if less than 9 characters
  101. return {
  102. ...part,
  103. toolCallId: normalizedId,
  104. }
  105. }
  106. return part
  107. })
  108. }
  109. result.push(msg)
  110. // Fix message sequence: tool messages cannot be followed by user messages
  111. if (msg.role === "tool" && nextMsg?.role === "user") {
  112. result.push({
  113. role: "assistant",
  114. content: [
  115. {
  116. type: "text",
  117. text: "Done.",
  118. },
  119. ],
  120. })
  121. }
  122. }
  123. return result
  124. }
  125. if (typeof model.capabilities.interleaved === "object" && model.capabilities.interleaved.field) {
  126. const field = model.capabilities.interleaved.field
  127. return msgs.map((msg) => {
  128. if (msg.role === "assistant" && Array.isArray(msg.content)) {
  129. const reasoningParts = msg.content.filter((part: any) => part.type === "reasoning")
  130. const reasoningText = reasoningParts.map((part: any) => part.text).join("")
  131. // Filter out reasoning parts from content
  132. const filteredContent = msg.content.filter((part: any) => part.type !== "reasoning")
  133. // Include reasoning_content | reasoning_details directly on the message for all assistant messages
  134. if (reasoningText) {
  135. return {
  136. ...msg,
  137. content: filteredContent,
  138. providerOptions: {
  139. ...msg.providerOptions,
  140. openaiCompatible: {
  141. ...(msg.providerOptions as any)?.openaiCompatible,
  142. [field]: reasoningText,
  143. },
  144. },
  145. }
  146. }
  147. return {
  148. ...msg,
  149. content: filteredContent,
  150. }
  151. }
  152. return msg
  153. })
  154. }
  155. return msgs
  156. }
  157. function applyCaching(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
  158. const system = msgs.filter((msg) => msg.role === "system").slice(0, 2)
  159. const final = msgs.filter((msg) => msg.role !== "system").slice(-2)
  160. const providerOptions = {
  161. anthropic: {
  162. cacheControl: { type: "ephemeral" },
  163. },
  164. openrouter: {
  165. cacheControl: { type: "ephemeral" },
  166. },
  167. bedrock: {
  168. cachePoint: { type: "default" },
  169. },
  170. openaiCompatible: {
  171. cache_control: { type: "ephemeral" },
  172. },
  173. copilot: {
  174. copilot_cache_control: { type: "ephemeral" },
  175. },
  176. }
  177. for (const msg of unique([...system, ...final])) {
  178. const useMessageLevelOptions = model.providerID === "anthropic" || model.providerID.includes("bedrock")
  179. const shouldUseContentOptions = !useMessageLevelOptions && Array.isArray(msg.content) && msg.content.length > 0
  180. if (shouldUseContentOptions) {
  181. const lastContent = msg.content[msg.content.length - 1]
  182. if (lastContent && typeof lastContent === "object") {
  183. lastContent.providerOptions = mergeDeep(lastContent.providerOptions ?? {}, providerOptions)
  184. continue
  185. }
  186. }
  187. msg.providerOptions = mergeDeep(msg.providerOptions ?? {}, providerOptions)
  188. }
  189. return msgs
  190. }
  191. function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
  192. return msgs.map((msg) => {
  193. if (msg.role !== "user" || !Array.isArray(msg.content)) return msg
  194. const filtered = msg.content.map((part) => {
  195. if (part.type !== "file" && part.type !== "image") return part
  196. // Check for empty base64 image data
  197. if (part.type === "image") {
  198. const imageStr = part.image.toString()
  199. if (imageStr.startsWith("data:")) {
  200. const match = imageStr.match(/^data:([^;]+);base64,(.*)$/)
  201. if (match && (!match[2] || match[2].length === 0)) {
  202. return {
  203. type: "text" as const,
  204. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  205. }
  206. }
  207. }
  208. }
  209. const mime = part.type === "image" ? part.image.toString().split(";")[0].replace("data:", "") : part.mediaType
  210. const filename = part.type === "file" ? part.filename : undefined
  211. const modality = mimeToModality(mime)
  212. if (!modality) return part
  213. if (model.capabilities.input[modality]) return part
  214. const name = filename ? `"${filename}"` : modality
  215. return {
  216. type: "text" as const,
  217. text: `ERROR: Cannot read ${name} (this model does not support ${modality} input). Inform the user.`,
  218. }
  219. })
  220. return { ...msg, content: filtered }
  221. })
  222. }
  223. export function message(msgs: ModelMessage[], model: Provider.Model, options: Record<string, unknown>) {
  224. msgs = unsupportedParts(msgs, model)
  225. msgs = normalizeMessages(msgs, model, options)
  226. if (
  227. (model.providerID === "anthropic" ||
  228. model.api.id.includes("anthropic") ||
  229. model.api.id.includes("claude") ||
  230. model.id.includes("anthropic") ||
  231. model.id.includes("claude") ||
  232. model.api.npm === "@ai-sdk/anthropic") &&
  233. model.api.npm !== "@ai-sdk/gateway"
  234. ) {
  235. msgs = applyCaching(msgs, model)
  236. }
  237. // Remap providerOptions keys from stored providerID to expected SDK key
  238. const key = sdkKey(model.api.npm)
  239. if (key && key !== model.providerID && model.api.npm !== "@ai-sdk/azure") {
  240. const remap = (opts: Record<string, any> | undefined) => {
  241. if (!opts) return opts
  242. if (!(model.providerID in opts)) return opts
  243. const result = { ...opts }
  244. result[key] = result[model.providerID]
  245. delete result[model.providerID]
  246. return result
  247. }
  248. msgs = msgs.map((msg) => {
  249. if (!Array.isArray(msg.content)) return { ...msg, providerOptions: remap(msg.providerOptions) }
  250. return {
  251. ...msg,
  252. providerOptions: remap(msg.providerOptions),
  253. content: msg.content.map((part) => ({ ...part, providerOptions: remap(part.providerOptions) })),
  254. } as typeof msg
  255. })
  256. }
  257. return msgs
  258. }
  259. export function temperature(model: Provider.Model) {
  260. const id = model.id.toLowerCase()
  261. if (id.includes("qwen")) return 0.55
  262. if (id.includes("claude")) return undefined
  263. if (id.includes("gemini")) return 1.0
  264. if (id.includes("glm-4.6")) return 1.0
  265. if (id.includes("glm-4.7")) return 1.0
  266. if (id.includes("minimax-m2")) return 1.0
  267. if (id.includes("kimi-k2")) {
  268. // kimi-k2-thinking & kimi-k2.5 && kimi-k2p5
  269. if (id.includes("thinking") || id.includes("k2.") || id.includes("k2p")) {
  270. return 1.0
  271. }
  272. return 0.6
  273. }
  274. return undefined
  275. }
  276. export function topP(model: Provider.Model) {
  277. const id = model.id.toLowerCase()
  278. if (id.includes("qwen")) return 1
  279. if (id.includes("minimax-m2") || id.includes("kimi-k2.5") || id.includes("kimi-k2p5") || id.includes("gemini")) {
  280. return 0.95
  281. }
  282. return undefined
  283. }
  284. export function topK(model: Provider.Model) {
  285. const id = model.id.toLowerCase()
  286. if (id.includes("minimax-m2")) {
  287. if (id.includes("m2.1")) return 40
  288. return 20
  289. }
  290. if (id.includes("gemini")) return 64
  291. return undefined
  292. }
  293. const WIDELY_SUPPORTED_EFFORTS = ["low", "medium", "high"]
  294. const OPENAI_EFFORTS = ["none", "minimal", ...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
  295. export function variants(model: Provider.Model): Record<string, Record<string, any>> {
  296. if (!model.capabilities.reasoning) return {}
  297. const id = model.id.toLowerCase()
  298. if (
  299. id.includes("deepseek") ||
  300. id.includes("minimax") ||
  301. id.includes("glm") ||
  302. id.includes("mistral") ||
  303. id.includes("kimi") ||
  304. // TODO: Remove this after models.dev data is fixed to use "kimi-k2.5" instead of "k2p5"
  305. id.includes("k2p5")
  306. )
  307. return {}
  308. // see: https://docs.x.ai/docs/guides/reasoning#control-how-hard-the-model-thinks
  309. if (id.includes("grok") && id.includes("grok-3-mini")) {
  310. if (model.api.npm === "@openrouter/ai-sdk-provider") {
  311. return {
  312. low: { reasoning: { effort: "low" } },
  313. high: { reasoning: { effort: "high" } },
  314. }
  315. }
  316. return {
  317. low: { reasoningEffort: "low" },
  318. high: { reasoningEffort: "high" },
  319. }
  320. }
  321. if (id.includes("grok")) return {}
  322. switch (model.api.npm) {
  323. case "@openrouter/ai-sdk-provider":
  324. if (!model.id.includes("gpt") && !model.id.includes("gemini-3") && !model.id.includes("claude")) return {}
  325. return Object.fromEntries(OPENAI_EFFORTS.map((effort) => [effort, { reasoning: { effort } }]))
  326. case "@ai-sdk/gateway":
  327. if (model.id.includes("anthropic")) {
  328. return {
  329. high: {
  330. thinking: {
  331. type: "enabled",
  332. budgetTokens: 16000,
  333. },
  334. },
  335. max: {
  336. thinking: {
  337. type: "enabled",
  338. budgetTokens: 31999,
  339. },
  340. },
  341. }
  342. }
  343. if (model.id.includes("google")) {
  344. if (id.includes("2.5")) {
  345. return {
  346. high: {
  347. thinkingConfig: {
  348. includeThoughts: true,
  349. thinkingBudget: 16000,
  350. },
  351. },
  352. max: {
  353. thinkingConfig: {
  354. includeThoughts: true,
  355. thinkingBudget: 24576,
  356. },
  357. },
  358. }
  359. }
  360. return Object.fromEntries(
  361. ["low", "high"].map((effort) => [
  362. effort,
  363. {
  364. includeThoughts: true,
  365. thinkingLevel: effort,
  366. },
  367. ]),
  368. )
  369. }
  370. return Object.fromEntries(OPENAI_EFFORTS.map((effort) => [effort, { reasoningEffort: effort }]))
  371. case "@ai-sdk/github-copilot":
  372. if (model.id.includes("gemini")) {
  373. // currently github copilot only returns thinking
  374. return {}
  375. }
  376. if (model.id.includes("claude")) {
  377. return {
  378. thinking: { thinking_budget: 4000 },
  379. }
  380. }
  381. const copilotEfforts = iife(() => {
  382. if (id.includes("5.1-codex-max") || id.includes("5.2") || id.includes("5.3"))
  383. return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
  384. return WIDELY_SUPPORTED_EFFORTS
  385. })
  386. return Object.fromEntries(
  387. copilotEfforts.map((effort) => [
  388. effort,
  389. {
  390. reasoningEffort: effort,
  391. reasoningSummary: "auto",
  392. include: ["reasoning.encrypted_content"],
  393. },
  394. ]),
  395. )
  396. case "@ai-sdk/cerebras":
  397. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/cerebras
  398. case "@ai-sdk/togetherai":
  399. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/togetherai
  400. case "@ai-sdk/xai":
  401. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/xai
  402. case "@ai-sdk/deepinfra":
  403. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/deepinfra
  404. case "venice-ai-sdk-provider":
  405. // https://docs.venice.ai/overview/guides/reasoning-models#reasoning-effort
  406. case "@ai-sdk/openai-compatible":
  407. return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((effort) => [effort, { reasoningEffort: effort }]))
  408. case "@ai-sdk/azure":
  409. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/azure
  410. if (id === "o1-mini") return {}
  411. const azureEfforts = ["low", "medium", "high"]
  412. if (id.includes("gpt-5-") || id === "gpt-5") {
  413. azureEfforts.unshift("minimal")
  414. }
  415. return Object.fromEntries(
  416. azureEfforts.map((effort) => [
  417. effort,
  418. {
  419. reasoningEffort: effort,
  420. reasoningSummary: "auto",
  421. include: ["reasoning.encrypted_content"],
  422. },
  423. ]),
  424. )
  425. case "@ai-sdk/openai":
  426. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/openai
  427. if (id === "gpt-5-pro") return {}
  428. const openaiEfforts = iife(() => {
  429. if (id.includes("codex")) {
  430. if (id.includes("5.2") || id.includes("5.3")) return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
  431. return WIDELY_SUPPORTED_EFFORTS
  432. }
  433. const arr = [...WIDELY_SUPPORTED_EFFORTS]
  434. if (id.includes("gpt-5-") || id === "gpt-5") {
  435. arr.unshift("minimal")
  436. }
  437. if (model.release_date >= "2025-11-13") {
  438. arr.unshift("none")
  439. }
  440. if (model.release_date >= "2025-12-04") {
  441. arr.push("xhigh")
  442. }
  443. return arr
  444. })
  445. return Object.fromEntries(
  446. openaiEfforts.map((effort) => [
  447. effort,
  448. {
  449. reasoningEffort: effort,
  450. reasoningSummary: "auto",
  451. include: ["reasoning.encrypted_content"],
  452. },
  453. ]),
  454. )
  455. case "@ai-sdk/anthropic":
  456. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/anthropic
  457. case "@ai-sdk/google-vertex/anthropic":
  458. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/google-vertex#anthropic-provider
  459. if (model.api.id.includes("opus-4-6") || model.api.id.includes("opus-4.6")) {
  460. const efforts = ["low", "medium", "high", "max"]
  461. return Object.fromEntries(
  462. efforts.map((effort) => [
  463. effort,
  464. {
  465. thinking: {
  466. type: "adaptive",
  467. },
  468. effort,
  469. },
  470. ]),
  471. )
  472. }
  473. return {
  474. high: {
  475. thinking: {
  476. type: "enabled",
  477. budgetTokens: Math.min(16_000, Math.floor(model.limit.output / 2 - 1)),
  478. },
  479. },
  480. max: {
  481. thinking: {
  482. type: "enabled",
  483. budgetTokens: Math.min(31_999, model.limit.output - 1),
  484. },
  485. },
  486. }
  487. case "@ai-sdk/amazon-bedrock":
  488. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock
  489. if (model.api.id.includes("opus-4-6") || model.api.id.includes("opus-4.6")) {
  490. const efforts = ["low", "medium", "high", "max"]
  491. return Object.fromEntries(
  492. efforts.map((effort) => [
  493. effort,
  494. {
  495. reasoningConfig: {
  496. type: "adaptive",
  497. maxReasoningEffort: effort,
  498. },
  499. },
  500. ]),
  501. )
  502. }
  503. // For Anthropic models on Bedrock, use reasoningConfig with budgetTokens
  504. if (model.api.id.includes("anthropic")) {
  505. return {
  506. high: {
  507. reasoningConfig: {
  508. type: "enabled",
  509. budgetTokens: 16000,
  510. },
  511. },
  512. max: {
  513. reasoningConfig: {
  514. type: "enabled",
  515. budgetTokens: 31999,
  516. },
  517. },
  518. }
  519. }
  520. // For Amazon Nova models, use reasoningConfig with maxReasoningEffort
  521. return Object.fromEntries(
  522. WIDELY_SUPPORTED_EFFORTS.map((effort) => [
  523. effort,
  524. {
  525. reasoningConfig: {
  526. type: "enabled",
  527. maxReasoningEffort: effort,
  528. },
  529. },
  530. ]),
  531. )
  532. case "@ai-sdk/google-vertex":
  533. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/google-vertex
  534. case "@ai-sdk/google":
  535. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai
  536. if (id.includes("2.5")) {
  537. return {
  538. high: {
  539. thinkingConfig: {
  540. includeThoughts: true,
  541. thinkingBudget: 16000,
  542. },
  543. },
  544. max: {
  545. thinkingConfig: {
  546. includeThoughts: true,
  547. thinkingBudget: 24576,
  548. },
  549. },
  550. }
  551. }
  552. return Object.fromEntries(
  553. ["low", "high"].map((effort) => [
  554. effort,
  555. {
  556. includeThoughts: true,
  557. thinkingLevel: effort,
  558. },
  559. ]),
  560. )
  561. case "@ai-sdk/mistral":
  562. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/mistral
  563. return {}
  564. case "@ai-sdk/cohere":
  565. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/cohere
  566. return {}
  567. case "@ai-sdk/groq":
  568. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/groq
  569. const groqEffort = ["none", ...WIDELY_SUPPORTED_EFFORTS]
  570. return Object.fromEntries(
  571. groqEffort.map((effort) => [
  572. effort,
  573. {
  574. includeThoughts: true,
  575. thinkingLevel: effort,
  576. },
  577. ]),
  578. )
  579. case "@ai-sdk/perplexity":
  580. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/perplexity
  581. return {}
  582. case "@mymediset/sap-ai-provider":
  583. case "@jerome-benoit/sap-ai-provider-v2":
  584. if (model.api.id.includes("anthropic")) {
  585. return {
  586. high: {
  587. thinking: {
  588. type: "enabled",
  589. budgetTokens: 16000,
  590. },
  591. },
  592. max: {
  593. thinking: {
  594. type: "enabled",
  595. budgetTokens: 31999,
  596. },
  597. },
  598. }
  599. }
  600. return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((effort) => [effort, { reasoningEffort: effort }]))
  601. }
  602. return {}
  603. }
  604. export function options(input: {
  605. model: Provider.Model
  606. sessionID: string
  607. providerOptions?: Record<string, any>
  608. }): Record<string, any> {
  609. const result: Record<string, any> = {}
  610. // openai and providers using openai package should set store to false by default.
  611. if (
  612. input.model.providerID === "openai" ||
  613. input.model.api.npm === "@ai-sdk/openai" ||
  614. input.model.api.npm === "@ai-sdk/github-copilot"
  615. ) {
  616. result["store"] = false
  617. }
  618. if (input.model.api.npm === "@openrouter/ai-sdk-provider") {
  619. result["usage"] = {
  620. include: true,
  621. }
  622. if (input.model.api.id.includes("gemini-3")) {
  623. result["reasoning"] = { effort: "high" }
  624. }
  625. }
  626. if (
  627. input.model.providerID === "baseten" ||
  628. (input.model.providerID === "opencode" && ["kimi-k2-thinking", "glm-4.6"].includes(input.model.api.id))
  629. ) {
  630. result["chat_template_args"] = { enable_thinking: true }
  631. }
  632. if (["zai", "zhipuai"].includes(input.model.providerID) && input.model.api.npm === "@ai-sdk/openai-compatible") {
  633. result["thinking"] = {
  634. type: "enabled",
  635. clear_thinking: false,
  636. }
  637. }
  638. if (input.model.providerID === "openai" || input.providerOptions?.setCacheKey) {
  639. result["promptCacheKey"] = input.sessionID
  640. }
  641. if (input.model.api.npm === "@ai-sdk/google" || input.model.api.npm === "@ai-sdk/google-vertex") {
  642. result["thinkingConfig"] = {
  643. includeThoughts: true,
  644. }
  645. if (input.model.api.id.includes("gemini-3")) {
  646. result["thinkingConfig"]["thinkingLevel"] = "high"
  647. }
  648. }
  649. // Enable thinking by default for kimi-k2.5/k2p5 models using anthropic SDK
  650. const modelId = input.model.api.id.toLowerCase()
  651. if (
  652. (input.model.api.npm === "@ai-sdk/anthropic" || input.model.api.npm === "@ai-sdk/google-vertex/anthropic") &&
  653. (modelId.includes("k2p5") || modelId.includes("kimi-k2.5") || modelId.includes("kimi-k2p5"))
  654. ) {
  655. result["thinking"] = {
  656. type: "enabled",
  657. budgetTokens: Math.min(16_000, Math.floor(input.model.limit.output / 2 - 1)),
  658. }
  659. }
  660. // Enable thinking for reasoning models on alibaba-cn (DashScope).
  661. // DashScope's OpenAI-compatible API requires `enable_thinking: true` in the request body
  662. // to return reasoning_content. Without it, models like kimi-k2.5, qwen-plus, qwen3, qwq,
  663. // deepseek-r1, etc. never output thinking/reasoning tokens.
  664. // Note: kimi-k2-thinking is excluded as it returns reasoning_content by default.
  665. if (
  666. input.model.providerID === "alibaba-cn" &&
  667. input.model.capabilities.reasoning &&
  668. input.model.api.npm === "@ai-sdk/openai-compatible" &&
  669. !modelId.includes("kimi-k2-thinking")
  670. ) {
  671. result["enable_thinking"] = true
  672. }
  673. if (input.model.api.id.includes("gpt-5") && !input.model.api.id.includes("gpt-5-chat")) {
  674. if (!input.model.api.id.includes("gpt-5-pro")) {
  675. result["reasoningEffort"] = "medium"
  676. result["reasoningSummary"] = "auto"
  677. }
  678. // Only set textVerbosity for non-chat gpt-5.x models
  679. // Chat models (e.g. gpt-5.2-chat-latest) only support "medium" verbosity
  680. if (
  681. input.model.api.id.includes("gpt-5.") &&
  682. !input.model.api.id.includes("codex") &&
  683. !input.model.api.id.includes("-chat") &&
  684. input.model.providerID !== "azure"
  685. ) {
  686. result["textVerbosity"] = "low"
  687. }
  688. if (input.model.providerID.startsWith("opencode")) {
  689. result["promptCacheKey"] = input.sessionID
  690. result["include"] = ["reasoning.encrypted_content"]
  691. result["reasoningSummary"] = "auto"
  692. }
  693. }
  694. if (input.model.providerID === "venice") {
  695. result["promptCacheKey"] = input.sessionID
  696. }
  697. if (input.model.providerID === "openrouter") {
  698. result["prompt_cache_key"] = input.sessionID
  699. }
  700. if (input.model.api.npm === "@ai-sdk/gateway") {
  701. result["gateway"] = {
  702. caching: "auto",
  703. }
  704. }
  705. return result
  706. }
  707. export function smallOptions(model: Provider.Model) {
  708. if (
  709. model.providerID === "openai" ||
  710. model.api.npm === "@ai-sdk/openai" ||
  711. model.api.npm === "@ai-sdk/github-copilot"
  712. ) {
  713. if (model.api.id.includes("gpt-5")) {
  714. if (model.api.id.includes("5.")) {
  715. return { store: false, reasoningEffort: "low" }
  716. }
  717. return { store: false, reasoningEffort: "minimal" }
  718. }
  719. return { store: false }
  720. }
  721. if (model.providerID === "google") {
  722. // gemini-3 uses thinkingLevel, gemini-2.5 uses thinkingBudget
  723. if (model.api.id.includes("gemini-3")) {
  724. return { thinkingConfig: { thinkingLevel: "minimal" } }
  725. }
  726. return { thinkingConfig: { thinkingBudget: 0 } }
  727. }
  728. if (model.providerID === "openrouter") {
  729. if (model.api.id.includes("google")) {
  730. return { reasoning: { enabled: false } }
  731. }
  732. return { reasoningEffort: "minimal" }
  733. }
  734. return {}
  735. }
  736. // Maps model ID prefix to provider slug used in providerOptions.
  737. // Example: "amazon/nova-2-lite" → "bedrock"
  738. const SLUG_OVERRIDES: Record<string, string> = {
  739. amazon: "bedrock",
  740. }
  741. export function providerOptions(model: Provider.Model, options: { [x: string]: any }) {
  742. if (model.api.npm === "@ai-sdk/gateway") {
  743. // Gateway providerOptions are split across two namespaces:
  744. // - `gateway`: gateway-native routing/caching controls (order, only, byok, etc.)
  745. // - `<upstream slug>`: provider-specific model options (anthropic/openai/...)
  746. // We keep `gateway` as-is and route every other top-level option under the
  747. // model-derived upstream slug.
  748. const i = model.api.id.indexOf("/")
  749. const rawSlug = i > 0 ? model.api.id.slice(0, i) : undefined
  750. const slug = rawSlug ? (SLUG_OVERRIDES[rawSlug] ?? rawSlug) : undefined
  751. const gateway = options.gateway
  752. const rest = Object.fromEntries(Object.entries(options).filter(([k]) => k !== "gateway"))
  753. const has = Object.keys(rest).length > 0
  754. const result: Record<string, any> = {}
  755. if (gateway !== undefined) result.gateway = gateway
  756. if (has) {
  757. if (slug) {
  758. // Route model-specific options under the provider slug
  759. result[slug] = rest
  760. } else if (gateway && typeof gateway === "object" && !Array.isArray(gateway)) {
  761. result.gateway = { ...gateway, ...rest }
  762. } else {
  763. result.gateway = rest
  764. }
  765. }
  766. return result
  767. }
  768. const key = sdkKey(model.api.npm) ?? model.providerID
  769. return { [key]: options }
  770. }
  771. export function maxOutputTokens(model: Provider.Model): number {
  772. return Math.min(model.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
  773. }
  774. export function schema(model: Provider.Model, schema: JSONSchema.BaseSchema | JSONSchema7): JSONSchema7 {
  775. /*
  776. if (["openai", "azure"].includes(providerID)) {
  777. if (schema.type === "object" && schema.properties) {
  778. for (const [key, value] of Object.entries(schema.properties)) {
  779. if (schema.required?.includes(key)) continue
  780. schema.properties[key] = {
  781. anyOf: [
  782. value as JSONSchema.JSONSchema,
  783. {
  784. type: "null",
  785. },
  786. ],
  787. }
  788. }
  789. }
  790. }
  791. */
  792. // Convert integer enums to string enums for Google/Gemini
  793. if (model.providerID === "google" || model.api.id.includes("gemini")) {
  794. const sanitizeGemini = (obj: any): any => {
  795. if (obj === null || typeof obj !== "object") {
  796. return obj
  797. }
  798. if (Array.isArray(obj)) {
  799. return obj.map(sanitizeGemini)
  800. }
  801. const result: any = {}
  802. for (const [key, value] of Object.entries(obj)) {
  803. if (key === "enum" && Array.isArray(value)) {
  804. // Convert all enum values to strings
  805. result[key] = value.map((v) => String(v))
  806. // If we have integer type with enum, change type to string
  807. if (result.type === "integer" || result.type === "number") {
  808. result.type = "string"
  809. }
  810. } else if (typeof value === "object" && value !== null) {
  811. result[key] = sanitizeGemini(value)
  812. } else {
  813. result[key] = value
  814. }
  815. }
  816. // Filter required array to only include fields that exist in properties
  817. if (result.type === "object" && result.properties && Array.isArray(result.required)) {
  818. result.required = result.required.filter((field: any) => field in result.properties)
  819. }
  820. if (result.type === "array") {
  821. if (result.items == null) {
  822. result.items = {}
  823. }
  824. // Ensure items has at least a type if it's an empty object
  825. // This handles nested arrays like { type: "array", items: { type: "array", items: {} } }
  826. if (typeof result.items === "object" && !Array.isArray(result.items) && !result.items.type) {
  827. result.items.type = "string"
  828. }
  829. }
  830. // Remove properties/required from non-object types (Gemini rejects these)
  831. if (result.type && result.type !== "object") {
  832. delete result.properties
  833. delete result.required
  834. }
  835. return result
  836. }
  837. schema = sanitizeGemini(schema)
  838. }
  839. return schema as JSONSchema7
  840. }
  841. }