transform.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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[], providerID: string): 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 = providerID === "anthropic" || 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. ) {
  234. msgs = applyCaching(msgs, model.providerID)
  235. }
  236. // Remap providerOptions keys from stored providerID to expected SDK key
  237. const key = sdkKey(model.api.npm)
  238. if (key && key !== model.providerID && model.api.npm !== "@ai-sdk/azure") {
  239. const remap = (opts: Record<string, any> | undefined) => {
  240. if (!opts) return opts
  241. if (!(model.providerID in opts)) return opts
  242. const result = { ...opts }
  243. result[key] = result[model.providerID]
  244. delete result[model.providerID]
  245. return result
  246. }
  247. msgs = msgs.map((msg) => {
  248. if (!Array.isArray(msg.content)) return { ...msg, providerOptions: remap(msg.providerOptions) }
  249. return {
  250. ...msg,
  251. providerOptions: remap(msg.providerOptions),
  252. content: msg.content.map((part) => ({ ...part, providerOptions: remap(part.providerOptions) })),
  253. } as typeof msg
  254. })
  255. }
  256. return msgs
  257. }
  258. export function temperature(model: Provider.Model) {
  259. const id = model.id.toLowerCase()
  260. if (id.includes("qwen")) return 0.55
  261. if (id.includes("claude")) return undefined
  262. if (id.includes("gemini")) return 1.0
  263. if (id.includes("glm-4.6")) return 1.0
  264. if (id.includes("glm-4.7")) return 1.0
  265. if (id.includes("minimax-m2")) return 1.0
  266. if (id.includes("kimi-k2")) {
  267. // kimi-k2-thinking & kimi-k2.5 && kimi-k2p5
  268. if (id.includes("thinking") || id.includes("k2.") || id.includes("k2p")) {
  269. return 1.0
  270. }
  271. return 0.6
  272. }
  273. return undefined
  274. }
  275. export function topP(model: Provider.Model) {
  276. const id = model.id.toLowerCase()
  277. if (id.includes("qwen")) return 1
  278. if (id.includes("minimax-m2") || id.includes("kimi-k2.5") || id.includes("kimi-k2p5") || id.includes("gemini")) {
  279. return 0.95
  280. }
  281. return undefined
  282. }
  283. export function topK(model: Provider.Model) {
  284. const id = model.id.toLowerCase()
  285. if (id.includes("minimax-m2")) {
  286. if (id.includes("m2.1")) return 40
  287. return 20
  288. }
  289. if (id.includes("gemini")) return 64
  290. return undefined
  291. }
  292. const WIDELY_SUPPORTED_EFFORTS = ["low", "medium", "high"]
  293. const OPENAI_EFFORTS = ["none", "minimal", ...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
  294. export function variants(model: Provider.Model): Record<string, Record<string, any>> {
  295. if (!model.capabilities.reasoning) return {}
  296. const id = model.id.toLowerCase()
  297. if (
  298. id.includes("deepseek") ||
  299. id.includes("minimax") ||
  300. id.includes("glm") ||
  301. id.includes("mistral") ||
  302. id.includes("kimi") ||
  303. // TODO: Remove this after models.dev data is fixed to use "kimi-k2.5" instead of "k2p5"
  304. id.includes("k2p5")
  305. )
  306. return {}
  307. // see: https://docs.x.ai/docs/guides/reasoning#control-how-hard-the-model-thinks
  308. if (id.includes("grok") && id.includes("grok-3-mini")) {
  309. if (model.api.npm === "@openrouter/ai-sdk-provider") {
  310. return {
  311. low: { reasoning: { effort: "low" } },
  312. high: { reasoning: { effort: "high" } },
  313. }
  314. }
  315. return {
  316. low: { reasoningEffort: "low" },
  317. high: { reasoningEffort: "high" },
  318. }
  319. }
  320. if (id.includes("grok")) return {}
  321. switch (model.api.npm) {
  322. case "@openrouter/ai-sdk-provider":
  323. if (!model.id.includes("gpt") && !model.id.includes("gemini-3")) return {}
  324. return Object.fromEntries(OPENAI_EFFORTS.map((effort) => [effort, { reasoning: { effort } }]))
  325. // TODO: YOU CANNOT SET max_tokens if this is set!!!
  326. case "@ai-sdk/gateway":
  327. return Object.fromEntries(OPENAI_EFFORTS.map((effort) => [effort, { reasoningEffort: effort }]))
  328. case "@ai-sdk/github-copilot":
  329. if (model.id.includes("gemini")) {
  330. // currently github copilot only returns thinking
  331. return {}
  332. }
  333. if (model.id.includes("claude")) {
  334. return {
  335. thinking: { thinking_budget: 4000 },
  336. }
  337. }
  338. const copilotEfforts = iife(() => {
  339. if (id.includes("5.1-codex-max") || id.includes("5.2") || id.includes("5.3"))
  340. return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
  341. return WIDELY_SUPPORTED_EFFORTS
  342. })
  343. return Object.fromEntries(
  344. copilotEfforts.map((effort) => [
  345. effort,
  346. {
  347. reasoningEffort: effort,
  348. reasoningSummary: "auto",
  349. include: ["reasoning.encrypted_content"],
  350. },
  351. ]),
  352. )
  353. case "@ai-sdk/cerebras":
  354. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/cerebras
  355. case "@ai-sdk/togetherai":
  356. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/togetherai
  357. case "@ai-sdk/xai":
  358. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/xai
  359. case "@ai-sdk/deepinfra":
  360. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/deepinfra
  361. case "venice-ai-sdk-provider":
  362. // https://docs.venice.ai/overview/guides/reasoning-models#reasoning-effort
  363. case "@ai-sdk/openai-compatible":
  364. return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((effort) => [effort, { reasoningEffort: effort }]))
  365. case "@ai-sdk/azure":
  366. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/azure
  367. if (id === "o1-mini") return {}
  368. const azureEfforts = ["low", "medium", "high"]
  369. if (id.includes("gpt-5-") || id === "gpt-5") {
  370. azureEfforts.unshift("minimal")
  371. }
  372. return Object.fromEntries(
  373. azureEfforts.map((effort) => [
  374. effort,
  375. {
  376. reasoningEffort: effort,
  377. reasoningSummary: "auto",
  378. include: ["reasoning.encrypted_content"],
  379. },
  380. ]),
  381. )
  382. case "@ai-sdk/openai":
  383. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/openai
  384. if (id === "gpt-5-pro") return {}
  385. const openaiEfforts = iife(() => {
  386. if (id.includes("codex")) {
  387. if (id.includes("5.2") || id.includes("5.3")) return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
  388. return WIDELY_SUPPORTED_EFFORTS
  389. }
  390. const arr = [...WIDELY_SUPPORTED_EFFORTS]
  391. if (id.includes("gpt-5-") || id === "gpt-5") {
  392. arr.unshift("minimal")
  393. }
  394. if (model.release_date >= "2025-11-13") {
  395. arr.unshift("none")
  396. }
  397. if (model.release_date >= "2025-12-04") {
  398. arr.push("xhigh")
  399. }
  400. return arr
  401. })
  402. return Object.fromEntries(
  403. openaiEfforts.map((effort) => [
  404. effort,
  405. {
  406. reasoningEffort: effort,
  407. reasoningSummary: "auto",
  408. include: ["reasoning.encrypted_content"],
  409. },
  410. ]),
  411. )
  412. case "@ai-sdk/anthropic":
  413. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/anthropic
  414. case "@ai-sdk/google-vertex/anthropic":
  415. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/google-vertex#anthropic-provider
  416. return {
  417. high: {
  418. thinking: {
  419. type: "enabled",
  420. budgetTokens: Math.min(16_000, Math.floor(model.limit.output / 2 - 1)),
  421. },
  422. },
  423. max: {
  424. thinking: {
  425. type: "enabled",
  426. budgetTokens: Math.min(31_999, model.limit.output - 1),
  427. },
  428. },
  429. }
  430. case "@ai-sdk/amazon-bedrock":
  431. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock
  432. // For Anthropic models on Bedrock, use reasoningConfig with budgetTokens
  433. if (model.api.id.includes("anthropic")) {
  434. return {
  435. high: {
  436. reasoningConfig: {
  437. type: "enabled",
  438. budgetTokens: 16000,
  439. },
  440. },
  441. max: {
  442. reasoningConfig: {
  443. type: "enabled",
  444. budgetTokens: 31999,
  445. },
  446. },
  447. }
  448. }
  449. // For Amazon Nova models, use reasoningConfig with maxReasoningEffort
  450. return Object.fromEntries(
  451. WIDELY_SUPPORTED_EFFORTS.map((effort) => [
  452. effort,
  453. {
  454. reasoningConfig: {
  455. type: "enabled",
  456. maxReasoningEffort: effort,
  457. },
  458. },
  459. ]),
  460. )
  461. case "@ai-sdk/google-vertex":
  462. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/google-vertex
  463. case "@ai-sdk/google":
  464. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai
  465. if (id.includes("2.5")) {
  466. return {
  467. high: {
  468. thinkingConfig: {
  469. includeThoughts: true,
  470. thinkingBudget: 16000,
  471. },
  472. },
  473. max: {
  474. thinkingConfig: {
  475. includeThoughts: true,
  476. thinkingBudget: 24576,
  477. },
  478. },
  479. }
  480. }
  481. return Object.fromEntries(
  482. ["low", "high"].map((effort) => [
  483. effort,
  484. {
  485. includeThoughts: true,
  486. thinkingLevel: effort,
  487. },
  488. ]),
  489. )
  490. case "@ai-sdk/mistral":
  491. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/mistral
  492. return {}
  493. case "@ai-sdk/cohere":
  494. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/cohere
  495. return {}
  496. case "@ai-sdk/groq":
  497. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/groq
  498. const groqEffort = ["none", ...WIDELY_SUPPORTED_EFFORTS]
  499. return Object.fromEntries(
  500. groqEffort.map((effort) => [
  501. effort,
  502. {
  503. includeThoughts: true,
  504. thinkingLevel: effort,
  505. },
  506. ]),
  507. )
  508. case "@ai-sdk/perplexity":
  509. // https://v5.ai-sdk.dev/providers/ai-sdk-providers/perplexity
  510. return {}
  511. case "@mymediset/sap-ai-provider":
  512. case "@jerome-benoit/sap-ai-provider-v2":
  513. if (model.api.id.includes("anthropic")) {
  514. return {
  515. high: {
  516. thinking: {
  517. type: "enabled",
  518. budgetTokens: 16000,
  519. },
  520. },
  521. max: {
  522. thinking: {
  523. type: "enabled",
  524. budgetTokens: 31999,
  525. },
  526. },
  527. }
  528. }
  529. return Object.fromEntries(WIDELY_SUPPORTED_EFFORTS.map((effort) => [effort, { reasoningEffort: effort }]))
  530. }
  531. return {}
  532. }
  533. export function options(input: {
  534. model: Provider.Model
  535. sessionID: string
  536. providerOptions?: Record<string, any>
  537. }): Record<string, any> {
  538. const result: Record<string, any> = {}
  539. // openai and providers using openai package should set store to false by default.
  540. if (
  541. input.model.providerID === "openai" ||
  542. input.model.api.npm === "@ai-sdk/openai" ||
  543. input.model.api.npm === "@ai-sdk/github-copilot"
  544. ) {
  545. result["store"] = false
  546. }
  547. if (input.model.api.npm === "@openrouter/ai-sdk-provider") {
  548. result["usage"] = {
  549. include: true,
  550. }
  551. if (input.model.api.id.includes("gemini-3")) {
  552. result["reasoning"] = { effort: "high" }
  553. }
  554. }
  555. if (
  556. input.model.providerID === "baseten" ||
  557. (input.model.providerID === "opencode" && ["kimi-k2-thinking", "glm-4.6"].includes(input.model.api.id))
  558. ) {
  559. result["chat_template_args"] = { enable_thinking: true }
  560. }
  561. if (["zai", "zhipuai"].includes(input.model.providerID) && input.model.api.npm === "@ai-sdk/openai-compatible") {
  562. result["thinking"] = {
  563. type: "enabled",
  564. clear_thinking: false,
  565. }
  566. }
  567. if (input.model.providerID === "openai" || input.providerOptions?.setCacheKey) {
  568. result["promptCacheKey"] = input.sessionID
  569. }
  570. if (input.model.api.npm === "@ai-sdk/google" || input.model.api.npm === "@ai-sdk/google-vertex") {
  571. result["thinkingConfig"] = {
  572. includeThoughts: true,
  573. }
  574. if (input.model.api.id.includes("gemini-3")) {
  575. result["thinkingConfig"]["thinkingLevel"] = "high"
  576. }
  577. }
  578. // Enable thinking by default for kimi-k2.5/k2p5 models using anthropic SDK
  579. const modelId = input.model.api.id.toLowerCase()
  580. if (
  581. (input.model.api.npm === "@ai-sdk/anthropic" || input.model.api.npm === "@ai-sdk/google-vertex/anthropic") &&
  582. (modelId.includes("k2p5") || modelId.includes("kimi-k2.5") || modelId.includes("kimi-k2p5"))
  583. ) {
  584. result["thinking"] = {
  585. type: "enabled",
  586. budgetTokens: Math.min(16_000, Math.floor(input.model.limit.output / 2 - 1)),
  587. }
  588. }
  589. // Enable thinking for reasoning models on alibaba-cn (DashScope).
  590. // DashScope's OpenAI-compatible API requires `enable_thinking: true` in the request body
  591. // to return reasoning_content. Without it, models like kimi-k2.5, qwen-plus, qwen3, qwq,
  592. // deepseek-r1, etc. never output thinking/reasoning tokens.
  593. // Note: kimi-k2-thinking is excluded as it returns reasoning_content by default.
  594. if (
  595. input.model.providerID === "alibaba-cn" &&
  596. input.model.capabilities.reasoning &&
  597. input.model.api.npm === "@ai-sdk/openai-compatible" &&
  598. !modelId.includes("kimi-k2-thinking")
  599. ) {
  600. result["enable_thinking"] = true
  601. }
  602. if (input.model.api.id.includes("gpt-5") && !input.model.api.id.includes("gpt-5-chat")) {
  603. if (!input.model.api.id.includes("gpt-5-pro")) {
  604. result["reasoningEffort"] = "medium"
  605. result["reasoningSummary"] = "auto"
  606. }
  607. // Only set textVerbosity for non-chat gpt-5.x models
  608. // Chat models (e.g. gpt-5.2-chat-latest) only support "medium" verbosity
  609. if (
  610. input.model.api.id.includes("gpt-5.") &&
  611. !input.model.api.id.includes("codex") &&
  612. !input.model.api.id.includes("-chat") &&
  613. input.model.providerID !== "azure"
  614. ) {
  615. result["textVerbosity"] = "low"
  616. }
  617. if (input.model.providerID.startsWith("opencode")) {
  618. result["promptCacheKey"] = input.sessionID
  619. result["include"] = ["reasoning.encrypted_content"]
  620. result["reasoningSummary"] = "auto"
  621. }
  622. }
  623. if (input.model.providerID === "venice") {
  624. result["promptCacheKey"] = input.sessionID
  625. }
  626. return result
  627. }
  628. export function smallOptions(model: Provider.Model) {
  629. if (
  630. model.providerID === "openai" ||
  631. model.api.npm === "@ai-sdk/openai" ||
  632. model.api.npm === "@ai-sdk/github-copilot"
  633. ) {
  634. if (model.api.id.includes("gpt-5")) {
  635. if (model.api.id.includes("5.")) {
  636. return { store: false, reasoningEffort: "low" }
  637. }
  638. return { store: false, reasoningEffort: "minimal" }
  639. }
  640. return { store: false }
  641. }
  642. if (model.providerID === "google") {
  643. // gemini-3 uses thinkingLevel, gemini-2.5 uses thinkingBudget
  644. if (model.api.id.includes("gemini-3")) {
  645. return { thinkingConfig: { thinkingLevel: "minimal" } }
  646. }
  647. return { thinkingConfig: { thinkingBudget: 0 } }
  648. }
  649. if (model.providerID === "openrouter") {
  650. if (model.api.id.includes("google")) {
  651. return { reasoning: { enabled: false } }
  652. }
  653. return { reasoningEffort: "minimal" }
  654. }
  655. return {}
  656. }
  657. export function providerOptions(model: Provider.Model, options: { [x: string]: any }) {
  658. const key = sdkKey(model.api.npm) ?? model.providerID
  659. return { [key]: options }
  660. }
  661. export function maxOutputTokens(model: Provider.Model): number {
  662. return Math.min(model.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
  663. }
  664. export function schema(model: Provider.Model, schema: JSONSchema.BaseSchema | JSONSchema7): JSONSchema7 {
  665. /*
  666. if (["openai", "azure"].includes(providerID)) {
  667. if (schema.type === "object" && schema.properties) {
  668. for (const [key, value] of Object.entries(schema.properties)) {
  669. if (schema.required?.includes(key)) continue
  670. schema.properties[key] = {
  671. anyOf: [
  672. value as JSONSchema.JSONSchema,
  673. {
  674. type: "null",
  675. },
  676. ],
  677. }
  678. }
  679. }
  680. }
  681. */
  682. // Convert integer enums to string enums for Google/Gemini
  683. if (model.providerID === "google" || model.api.id.includes("gemini")) {
  684. const sanitizeGemini = (obj: any): any => {
  685. if (obj === null || typeof obj !== "object") {
  686. return obj
  687. }
  688. if (Array.isArray(obj)) {
  689. return obj.map(sanitizeGemini)
  690. }
  691. const result: any = {}
  692. for (const [key, value] of Object.entries(obj)) {
  693. if (key === "enum" && Array.isArray(value)) {
  694. // Convert all enum values to strings
  695. result[key] = value.map((v) => String(v))
  696. // If we have integer type with enum, change type to string
  697. if (result.type === "integer" || result.type === "number") {
  698. result.type = "string"
  699. }
  700. } else if (typeof value === "object" && value !== null) {
  701. result[key] = sanitizeGemini(value)
  702. } else {
  703. result[key] = value
  704. }
  705. }
  706. // Filter required array to only include fields that exist in properties
  707. if (result.type === "object" && result.properties && Array.isArray(result.required)) {
  708. result.required = result.required.filter((field: any) => field in result.properties)
  709. }
  710. if (result.type === "array") {
  711. if (result.items == null) {
  712. result.items = {}
  713. }
  714. // Ensure items has at least a type if it's an empty object
  715. // This handles nested arrays like { type: "array", items: { type: "array", items: {} } }
  716. if (typeof result.items === "object" && !Array.isArray(result.items) && !result.items.type) {
  717. result.items.type = "string"
  718. }
  719. }
  720. // Remove properties/required from non-object types (Gemini rejects these)
  721. if (result.type && result.type !== "object") {
  722. delete result.properties
  723. delete result.required
  724. }
  725. return result
  726. }
  727. schema = sanitizeGemini(schema)
  728. }
  729. return schema as JSONSchema7
  730. }
  731. }