transform.test.ts 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. import { describe, expect, test } from "bun:test"
  2. import { ProviderTransform } from "../../src/provider/transform"
  3. const OUTPUT_TOKEN_MAX = 32000
  4. describe("ProviderTransform.options - setCacheKey", () => {
  5. const sessionID = "test-session-123"
  6. const mockModel = {
  7. id: "anthropic/claude-3-5-sonnet",
  8. providerID: "anthropic",
  9. api: {
  10. id: "claude-3-5-sonnet-20241022",
  11. url: "https://api.anthropic.com",
  12. npm: "@ai-sdk/anthropic",
  13. },
  14. name: "Claude 3.5 Sonnet",
  15. capabilities: {
  16. temperature: true,
  17. reasoning: false,
  18. attachment: true,
  19. toolcall: true,
  20. input: { text: true, audio: false, image: true, video: false, pdf: true },
  21. output: { text: true, audio: false, image: false, video: false, pdf: false },
  22. interleaved: false,
  23. },
  24. cost: {
  25. input: 0.003,
  26. output: 0.015,
  27. cache: { read: 0.0003, write: 0.00375 },
  28. },
  29. limit: {
  30. context: 200000,
  31. output: 8192,
  32. },
  33. status: "active",
  34. options: {},
  35. headers: {},
  36. } as any
  37. test("should set promptCacheKey when providerOptions.setCacheKey is true", () => {
  38. const result = ProviderTransform.options({
  39. model: mockModel,
  40. sessionID,
  41. providerOptions: { setCacheKey: true },
  42. })
  43. expect(result.promptCacheKey).toBe(sessionID)
  44. })
  45. test("should not set promptCacheKey when providerOptions.setCacheKey is false", () => {
  46. const result = ProviderTransform.options({
  47. model: mockModel,
  48. sessionID,
  49. providerOptions: { setCacheKey: false },
  50. })
  51. expect(result.promptCacheKey).toBeUndefined()
  52. })
  53. test("should not set promptCacheKey when providerOptions is undefined", () => {
  54. const result = ProviderTransform.options({
  55. model: mockModel,
  56. sessionID,
  57. providerOptions: undefined,
  58. })
  59. expect(result.promptCacheKey).toBeUndefined()
  60. })
  61. test("should not set promptCacheKey when providerOptions does not have setCacheKey", () => {
  62. const result = ProviderTransform.options({ model: mockModel, sessionID, providerOptions: {} })
  63. expect(result.promptCacheKey).toBeUndefined()
  64. })
  65. test("should set promptCacheKey for openai provider regardless of setCacheKey", () => {
  66. const openaiModel = {
  67. ...mockModel,
  68. providerID: "openai",
  69. api: {
  70. id: "gpt-4",
  71. url: "https://api.openai.com",
  72. npm: "@ai-sdk/openai",
  73. },
  74. }
  75. const result = ProviderTransform.options({ model: openaiModel, sessionID, providerOptions: {} })
  76. expect(result.promptCacheKey).toBe(sessionID)
  77. })
  78. test("should set store=false for openai provider", () => {
  79. const openaiModel = {
  80. ...mockModel,
  81. providerID: "openai",
  82. api: {
  83. id: "gpt-4",
  84. url: "https://api.openai.com",
  85. npm: "@ai-sdk/openai",
  86. },
  87. }
  88. const result = ProviderTransform.options({
  89. model: openaiModel,
  90. sessionID,
  91. providerOptions: {},
  92. })
  93. expect(result.store).toBe(false)
  94. })
  95. })
  96. describe("ProviderTransform.options - gpt-5 textVerbosity", () => {
  97. const sessionID = "test-session-123"
  98. const createGpt5Model = (apiId: string) =>
  99. ({
  100. id: `openai/${apiId}`,
  101. providerID: "openai",
  102. api: {
  103. id: apiId,
  104. url: "https://api.openai.com",
  105. npm: "@ai-sdk/openai",
  106. },
  107. name: apiId,
  108. capabilities: {
  109. temperature: true,
  110. reasoning: true,
  111. attachment: true,
  112. toolcall: true,
  113. input: { text: true, audio: false, image: true, video: false, pdf: false },
  114. output: { text: true, audio: false, image: false, video: false, pdf: false },
  115. interleaved: false,
  116. },
  117. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  118. limit: { context: 128000, output: 4096 },
  119. status: "active",
  120. options: {},
  121. headers: {},
  122. }) as any
  123. test("gpt-5.2 should have textVerbosity set to low", () => {
  124. const model = createGpt5Model("gpt-5.2")
  125. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  126. expect(result.textVerbosity).toBe("low")
  127. })
  128. test("gpt-5.1 should have textVerbosity set to low", () => {
  129. const model = createGpt5Model("gpt-5.1")
  130. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  131. expect(result.textVerbosity).toBe("low")
  132. })
  133. test("gpt-5.2-chat-latest should NOT have textVerbosity set (only supports medium)", () => {
  134. const model = createGpt5Model("gpt-5.2-chat-latest")
  135. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  136. expect(result.textVerbosity).toBeUndefined()
  137. })
  138. test("gpt-5.1-chat-latest should NOT have textVerbosity set (only supports medium)", () => {
  139. const model = createGpt5Model("gpt-5.1-chat-latest")
  140. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  141. expect(result.textVerbosity).toBeUndefined()
  142. })
  143. test("gpt-5.2-chat should NOT have textVerbosity set", () => {
  144. const model = createGpt5Model("gpt-5.2-chat")
  145. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  146. expect(result.textVerbosity).toBeUndefined()
  147. })
  148. test("gpt-5-chat should NOT have textVerbosity set", () => {
  149. const model = createGpt5Model("gpt-5-chat")
  150. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  151. expect(result.textVerbosity).toBeUndefined()
  152. })
  153. test("gpt-5.2-codex should NOT have textVerbosity set (codex models excluded)", () => {
  154. const model = createGpt5Model("gpt-5.2-codex")
  155. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  156. expect(result.textVerbosity).toBeUndefined()
  157. })
  158. })
  159. describe("ProviderTransform.maxOutputTokens", () => {
  160. test("returns 32k when modelLimit > 32k", () => {
  161. const modelLimit = 100000
  162. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai", {}, modelLimit, OUTPUT_TOKEN_MAX)
  163. expect(result).toBe(OUTPUT_TOKEN_MAX)
  164. })
  165. test("returns modelLimit when modelLimit < 32k", () => {
  166. const modelLimit = 16000
  167. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai", {}, modelLimit, OUTPUT_TOKEN_MAX)
  168. expect(result).toBe(16000)
  169. })
  170. describe("azure", () => {
  171. test("returns 32k when modelLimit > 32k", () => {
  172. const modelLimit = 100000
  173. const result = ProviderTransform.maxOutputTokens("@ai-sdk/azure", {}, modelLimit, OUTPUT_TOKEN_MAX)
  174. expect(result).toBe(OUTPUT_TOKEN_MAX)
  175. })
  176. test("returns modelLimit when modelLimit < 32k", () => {
  177. const modelLimit = 16000
  178. const result = ProviderTransform.maxOutputTokens("@ai-sdk/azure", {}, modelLimit, OUTPUT_TOKEN_MAX)
  179. expect(result).toBe(16000)
  180. })
  181. })
  182. describe("bedrock", () => {
  183. test("returns 32k when modelLimit > 32k", () => {
  184. const modelLimit = 100000
  185. const result = ProviderTransform.maxOutputTokens("@ai-sdk/amazon-bedrock", {}, modelLimit, OUTPUT_TOKEN_MAX)
  186. expect(result).toBe(OUTPUT_TOKEN_MAX)
  187. })
  188. test("returns modelLimit when modelLimit < 32k", () => {
  189. const modelLimit = 16000
  190. const result = ProviderTransform.maxOutputTokens("@ai-sdk/amazon-bedrock", {}, modelLimit, OUTPUT_TOKEN_MAX)
  191. expect(result).toBe(16000)
  192. })
  193. })
  194. describe("anthropic without thinking options", () => {
  195. test("returns 32k when modelLimit > 32k", () => {
  196. const modelLimit = 100000
  197. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", {}, modelLimit, OUTPUT_TOKEN_MAX)
  198. expect(result).toBe(OUTPUT_TOKEN_MAX)
  199. })
  200. test("returns modelLimit when modelLimit < 32k", () => {
  201. const modelLimit = 16000
  202. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", {}, modelLimit, OUTPUT_TOKEN_MAX)
  203. expect(result).toBe(16000)
  204. })
  205. })
  206. describe("anthropic with thinking options", () => {
  207. test("returns 32k when budgetTokens + 32k <= modelLimit", () => {
  208. const modelLimit = 100000
  209. const options = {
  210. thinking: {
  211. type: "enabled",
  212. budgetTokens: 10000,
  213. },
  214. }
  215. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", options, modelLimit, OUTPUT_TOKEN_MAX)
  216. expect(result).toBe(OUTPUT_TOKEN_MAX)
  217. })
  218. test("returns modelLimit - budgetTokens when budgetTokens + 32k > modelLimit", () => {
  219. const modelLimit = 50000
  220. const options = {
  221. thinking: {
  222. type: "enabled",
  223. budgetTokens: 30000,
  224. },
  225. }
  226. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", options, modelLimit, OUTPUT_TOKEN_MAX)
  227. expect(result).toBe(20000)
  228. })
  229. test("returns 32k when thinking type is not enabled", () => {
  230. const modelLimit = 100000
  231. const options = {
  232. thinking: {
  233. type: "disabled",
  234. budgetTokens: 10000,
  235. },
  236. }
  237. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", options, modelLimit, OUTPUT_TOKEN_MAX)
  238. expect(result).toBe(OUTPUT_TOKEN_MAX)
  239. })
  240. })
  241. describe("openai-compatible with thinking options (snake_case)", () => {
  242. test("returns 32k when budget_tokens + 32k <= modelLimit", () => {
  243. const modelLimit = 100000
  244. const options = {
  245. thinking: {
  246. type: "enabled",
  247. budget_tokens: 10000,
  248. },
  249. }
  250. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai-compatible", options, modelLimit, OUTPUT_TOKEN_MAX)
  251. expect(result).toBe(OUTPUT_TOKEN_MAX)
  252. })
  253. test("returns modelLimit - budget_tokens when budget_tokens + 32k > modelLimit", () => {
  254. const modelLimit = 50000
  255. const options = {
  256. thinking: {
  257. type: "enabled",
  258. budget_tokens: 30000,
  259. },
  260. }
  261. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai-compatible", options, modelLimit, OUTPUT_TOKEN_MAX)
  262. expect(result).toBe(20000)
  263. })
  264. test("returns 32k when thinking type is not enabled", () => {
  265. const modelLimit = 100000
  266. const options = {
  267. thinking: {
  268. type: "disabled",
  269. budget_tokens: 10000,
  270. },
  271. }
  272. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai-compatible", options, modelLimit, OUTPUT_TOKEN_MAX)
  273. expect(result).toBe(OUTPUT_TOKEN_MAX)
  274. })
  275. test("returns 32k when budget_tokens is 0", () => {
  276. const modelLimit = 100000
  277. const options = {
  278. thinking: {
  279. type: "enabled",
  280. budget_tokens: 0,
  281. },
  282. }
  283. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai-compatible", options, modelLimit, OUTPUT_TOKEN_MAX)
  284. expect(result).toBe(OUTPUT_TOKEN_MAX)
  285. })
  286. })
  287. })
  288. describe("ProviderTransform.schema - gemini array items", () => {
  289. test("adds missing items for array properties", () => {
  290. const geminiModel = {
  291. providerID: "google",
  292. api: {
  293. id: "gemini-3-pro",
  294. },
  295. } as any
  296. const schema = {
  297. type: "object",
  298. properties: {
  299. nodes: { type: "array" },
  300. edges: { type: "array", items: { type: "string" } },
  301. },
  302. } as any
  303. const result = ProviderTransform.schema(geminiModel, schema) as any
  304. expect(result.properties.nodes.items).toBeDefined()
  305. expect(result.properties.edges.items.type).toBe("string")
  306. })
  307. })
  308. describe("ProviderTransform.message - DeepSeek reasoning content", () => {
  309. test("DeepSeek with tool calls includes reasoning_content in providerOptions", () => {
  310. const msgs = [
  311. {
  312. role: "assistant",
  313. content: [
  314. { type: "reasoning", text: "Let me think about this..." },
  315. {
  316. type: "tool-call",
  317. toolCallId: "test",
  318. toolName: "bash",
  319. input: { command: "echo hello" },
  320. },
  321. ],
  322. },
  323. ] as any[]
  324. const result = ProviderTransform.message(
  325. msgs,
  326. {
  327. id: "deepseek/deepseek-chat",
  328. providerID: "deepseek",
  329. api: {
  330. id: "deepseek-chat",
  331. url: "https://api.deepseek.com",
  332. npm: "@ai-sdk/openai-compatible",
  333. },
  334. name: "DeepSeek Chat",
  335. capabilities: {
  336. temperature: true,
  337. reasoning: true,
  338. attachment: false,
  339. toolcall: true,
  340. input: { text: true, audio: false, image: false, video: false, pdf: false },
  341. output: { text: true, audio: false, image: false, video: false, pdf: false },
  342. interleaved: {
  343. field: "reasoning_content",
  344. },
  345. },
  346. cost: {
  347. input: 0.001,
  348. output: 0.002,
  349. cache: { read: 0.0001, write: 0.0002 },
  350. },
  351. limit: {
  352. context: 128000,
  353. output: 8192,
  354. },
  355. status: "active",
  356. options: {},
  357. headers: {},
  358. release_date: "2023-04-01",
  359. },
  360. {},
  361. )
  362. expect(result).toHaveLength(1)
  363. expect(result[0].content).toEqual([
  364. {
  365. type: "tool-call",
  366. toolCallId: "test",
  367. toolName: "bash",
  368. input: { command: "echo hello" },
  369. },
  370. ])
  371. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBe("Let me think about this...")
  372. })
  373. test("Non-DeepSeek providers leave reasoning content unchanged", () => {
  374. const msgs = [
  375. {
  376. role: "assistant",
  377. content: [
  378. { type: "reasoning", text: "Should not be processed" },
  379. { type: "text", text: "Answer" },
  380. ],
  381. },
  382. ] as any[]
  383. const result = ProviderTransform.message(
  384. msgs,
  385. {
  386. id: "openai/gpt-4",
  387. providerID: "openai",
  388. api: {
  389. id: "gpt-4",
  390. url: "https://api.openai.com",
  391. npm: "@ai-sdk/openai",
  392. },
  393. name: "GPT-4",
  394. capabilities: {
  395. temperature: true,
  396. reasoning: false,
  397. attachment: true,
  398. toolcall: true,
  399. input: { text: true, audio: false, image: true, video: false, pdf: false },
  400. output: { text: true, audio: false, image: false, video: false, pdf: false },
  401. interleaved: false,
  402. },
  403. cost: {
  404. input: 0.03,
  405. output: 0.06,
  406. cache: { read: 0.001, write: 0.002 },
  407. },
  408. limit: {
  409. context: 128000,
  410. output: 4096,
  411. },
  412. status: "active",
  413. options: {},
  414. headers: {},
  415. release_date: "2023-04-01",
  416. },
  417. {},
  418. )
  419. expect(result[0].content).toEqual([
  420. { type: "reasoning", text: "Should not be processed" },
  421. { type: "text", text: "Answer" },
  422. ])
  423. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBeUndefined()
  424. })
  425. })
  426. describe("ProviderTransform.message - empty image handling", () => {
  427. const mockModel = {
  428. id: "anthropic/claude-3-5-sonnet",
  429. providerID: "anthropic",
  430. api: {
  431. id: "claude-3-5-sonnet-20241022",
  432. url: "https://api.anthropic.com",
  433. npm: "@ai-sdk/anthropic",
  434. },
  435. name: "Claude 3.5 Sonnet",
  436. capabilities: {
  437. temperature: true,
  438. reasoning: false,
  439. attachment: true,
  440. toolcall: true,
  441. input: { text: true, audio: false, image: true, video: false, pdf: true },
  442. output: { text: true, audio: false, image: false, video: false, pdf: false },
  443. interleaved: false,
  444. },
  445. cost: {
  446. input: 0.003,
  447. output: 0.015,
  448. cache: { read: 0.0003, write: 0.00375 },
  449. },
  450. limit: {
  451. context: 200000,
  452. output: 8192,
  453. },
  454. status: "active",
  455. options: {},
  456. headers: {},
  457. } as any
  458. test("should replace empty base64 image with error text", () => {
  459. const msgs = [
  460. {
  461. role: "user",
  462. content: [
  463. { type: "text", text: "What is in this image?" },
  464. { type: "image", image: "data:image/png;base64," },
  465. ],
  466. },
  467. ] as any[]
  468. const result = ProviderTransform.message(msgs, mockModel, {})
  469. expect(result).toHaveLength(1)
  470. expect(result[0].content).toHaveLength(2)
  471. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  472. expect(result[0].content[1]).toEqual({
  473. type: "text",
  474. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  475. })
  476. })
  477. test("should keep valid base64 images unchanged", () => {
  478. const validBase64 =
  479. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  480. const msgs = [
  481. {
  482. role: "user",
  483. content: [
  484. { type: "text", text: "What is in this image?" },
  485. { type: "image", image: `data:image/png;base64,${validBase64}` },
  486. ],
  487. },
  488. ] as any[]
  489. const result = ProviderTransform.message(msgs, mockModel, {})
  490. expect(result).toHaveLength(1)
  491. expect(result[0].content).toHaveLength(2)
  492. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  493. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  494. })
  495. test("should handle mixed valid and empty images", () => {
  496. const validBase64 =
  497. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  498. const msgs = [
  499. {
  500. role: "user",
  501. content: [
  502. { type: "text", text: "Compare these images" },
  503. { type: "image", image: `data:image/png;base64,${validBase64}` },
  504. { type: "image", image: "data:image/jpeg;base64," },
  505. ],
  506. },
  507. ] as any[]
  508. const result = ProviderTransform.message(msgs, mockModel, {})
  509. expect(result).toHaveLength(1)
  510. expect(result[0].content).toHaveLength(3)
  511. expect(result[0].content[0]).toEqual({ type: "text", text: "Compare these images" })
  512. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  513. expect(result[0].content[2]).toEqual({
  514. type: "text",
  515. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  516. })
  517. })
  518. })
  519. describe("ProviderTransform.message - anthropic empty content filtering", () => {
  520. const anthropicModel = {
  521. id: "anthropic/claude-3-5-sonnet",
  522. providerID: "anthropic",
  523. api: {
  524. id: "claude-3-5-sonnet-20241022",
  525. url: "https://api.anthropic.com",
  526. npm: "@ai-sdk/anthropic",
  527. },
  528. name: "Claude 3.5 Sonnet",
  529. capabilities: {
  530. temperature: true,
  531. reasoning: false,
  532. attachment: true,
  533. toolcall: true,
  534. input: { text: true, audio: false, image: true, video: false, pdf: true },
  535. output: { text: true, audio: false, image: false, video: false, pdf: false },
  536. interleaved: false,
  537. },
  538. cost: {
  539. input: 0.003,
  540. output: 0.015,
  541. cache: { read: 0.0003, write: 0.00375 },
  542. },
  543. limit: {
  544. context: 200000,
  545. output: 8192,
  546. },
  547. status: "active",
  548. options: {},
  549. headers: {},
  550. } as any
  551. test("filters out messages with empty string content", () => {
  552. const msgs = [
  553. { role: "user", content: "Hello" },
  554. { role: "assistant", content: "" },
  555. { role: "user", content: "World" },
  556. ] as any[]
  557. const result = ProviderTransform.message(msgs, anthropicModel, {})
  558. expect(result).toHaveLength(2)
  559. expect(result[0].content).toBe("Hello")
  560. expect(result[1].content).toBe("World")
  561. })
  562. test("filters out empty text parts from array content", () => {
  563. const msgs = [
  564. {
  565. role: "assistant",
  566. content: [
  567. { type: "text", text: "" },
  568. { type: "text", text: "Hello" },
  569. { type: "text", text: "" },
  570. ],
  571. },
  572. ] as any[]
  573. const result = ProviderTransform.message(msgs, anthropicModel, {})
  574. expect(result).toHaveLength(1)
  575. expect(result[0].content).toHaveLength(1)
  576. expect(result[0].content[0]).toEqual({ type: "text", text: "Hello" })
  577. })
  578. test("filters out empty reasoning parts from array content", () => {
  579. const msgs = [
  580. {
  581. role: "assistant",
  582. content: [
  583. { type: "reasoning", text: "" },
  584. { type: "text", text: "Answer" },
  585. { type: "reasoning", text: "" },
  586. ],
  587. },
  588. ] as any[]
  589. const result = ProviderTransform.message(msgs, anthropicModel, {})
  590. expect(result).toHaveLength(1)
  591. expect(result[0].content).toHaveLength(1)
  592. expect(result[0].content[0]).toEqual({ type: "text", text: "Answer" })
  593. })
  594. test("removes entire message when all parts are empty", () => {
  595. const msgs = [
  596. { role: "user", content: "Hello" },
  597. {
  598. role: "assistant",
  599. content: [
  600. { type: "text", text: "" },
  601. { type: "reasoning", text: "" },
  602. ],
  603. },
  604. { role: "user", content: "World" },
  605. ] as any[]
  606. const result = ProviderTransform.message(msgs, anthropicModel, {})
  607. expect(result).toHaveLength(2)
  608. expect(result[0].content).toBe("Hello")
  609. expect(result[1].content).toBe("World")
  610. })
  611. test("keeps non-text/reasoning parts even if text parts are empty", () => {
  612. const msgs = [
  613. {
  614. role: "assistant",
  615. content: [
  616. { type: "text", text: "" },
  617. { type: "tool-call", toolCallId: "123", toolName: "bash", input: { command: "ls" } },
  618. ],
  619. },
  620. ] as any[]
  621. const result = ProviderTransform.message(msgs, anthropicModel, {})
  622. expect(result).toHaveLength(1)
  623. expect(result[0].content).toHaveLength(1)
  624. expect(result[0].content[0]).toEqual({
  625. type: "tool-call",
  626. toolCallId: "123",
  627. toolName: "bash",
  628. input: { command: "ls" },
  629. })
  630. })
  631. test("keeps messages with valid text alongside empty parts", () => {
  632. const msgs = [
  633. {
  634. role: "assistant",
  635. content: [
  636. { type: "reasoning", text: "Thinking..." },
  637. { type: "text", text: "" },
  638. { type: "text", text: "Result" },
  639. ],
  640. },
  641. ] as any[]
  642. const result = ProviderTransform.message(msgs, anthropicModel, {})
  643. expect(result).toHaveLength(1)
  644. expect(result[0].content).toHaveLength(2)
  645. expect(result[0].content[0]).toEqual({ type: "reasoning", text: "Thinking..." })
  646. expect(result[0].content[1]).toEqual({ type: "text", text: "Result" })
  647. })
  648. test("does not filter for non-anthropic providers", () => {
  649. const openaiModel = {
  650. ...anthropicModel,
  651. providerID: "openai",
  652. api: {
  653. id: "gpt-4",
  654. url: "https://api.openai.com",
  655. npm: "@ai-sdk/openai",
  656. },
  657. }
  658. const msgs = [
  659. { role: "assistant", content: "" },
  660. {
  661. role: "assistant",
  662. content: [{ type: "text", text: "" }],
  663. },
  664. ] as any[]
  665. const result = ProviderTransform.message(msgs, openaiModel, {})
  666. expect(result).toHaveLength(2)
  667. expect(result[0].content).toBe("")
  668. expect(result[1].content).toHaveLength(1)
  669. })
  670. })
  671. describe("ProviderTransform.message - strip openai metadata when store=false", () => {
  672. const openaiModel = {
  673. id: "openai/gpt-5",
  674. providerID: "openai",
  675. api: {
  676. id: "gpt-5",
  677. url: "https://api.openai.com",
  678. npm: "@ai-sdk/openai",
  679. },
  680. name: "GPT-5",
  681. capabilities: {
  682. temperature: true,
  683. reasoning: true,
  684. attachment: true,
  685. toolcall: true,
  686. input: { text: true, audio: false, image: true, video: false, pdf: false },
  687. output: { text: true, audio: false, image: false, video: false, pdf: false },
  688. interleaved: false,
  689. },
  690. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  691. limit: { context: 128000, output: 4096 },
  692. status: "active",
  693. options: {},
  694. headers: {},
  695. } as any
  696. test("preserves itemId and reasoningEncryptedContent when store=false", () => {
  697. const msgs = [
  698. {
  699. role: "assistant",
  700. content: [
  701. {
  702. type: "reasoning",
  703. text: "thinking...",
  704. providerOptions: {
  705. openai: {
  706. itemId: "rs_123",
  707. reasoningEncryptedContent: "encrypted",
  708. },
  709. },
  710. },
  711. {
  712. type: "text",
  713. text: "Hello",
  714. providerOptions: {
  715. openai: {
  716. itemId: "msg_456",
  717. },
  718. },
  719. },
  720. ],
  721. },
  722. ] as any[]
  723. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  724. expect(result).toHaveLength(1)
  725. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("rs_123")
  726. expect(result[0].content[1].providerOptions?.openai?.itemId).toBe("msg_456")
  727. })
  728. test("preserves itemId and reasoningEncryptedContent when store=false even when not openai", () => {
  729. const zenModel = {
  730. ...openaiModel,
  731. providerID: "zen",
  732. }
  733. const msgs = [
  734. {
  735. role: "assistant",
  736. content: [
  737. {
  738. type: "reasoning",
  739. text: "thinking...",
  740. providerOptions: {
  741. openai: {
  742. itemId: "rs_123",
  743. reasoningEncryptedContent: "encrypted",
  744. },
  745. },
  746. },
  747. {
  748. type: "text",
  749. text: "Hello",
  750. providerOptions: {
  751. openai: {
  752. itemId: "msg_456",
  753. },
  754. },
  755. },
  756. ],
  757. },
  758. ] as any[]
  759. const result = ProviderTransform.message(msgs, zenModel, { store: false }) as any[]
  760. expect(result).toHaveLength(1)
  761. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("rs_123")
  762. expect(result[0].content[1].providerOptions?.openai?.itemId).toBe("msg_456")
  763. })
  764. test("preserves other openai options including itemId", () => {
  765. const msgs = [
  766. {
  767. role: "assistant",
  768. content: [
  769. {
  770. type: "text",
  771. text: "Hello",
  772. providerOptions: {
  773. openai: {
  774. itemId: "msg_123",
  775. otherOption: "value",
  776. },
  777. },
  778. },
  779. ],
  780. },
  781. ] as any[]
  782. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  783. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  784. expect(result[0].content[0].providerOptions?.openai?.otherOption).toBe("value")
  785. })
  786. test("preserves metadata for openai package when store is true", () => {
  787. const msgs = [
  788. {
  789. role: "assistant",
  790. content: [
  791. {
  792. type: "text",
  793. text: "Hello",
  794. providerOptions: {
  795. openai: {
  796. itemId: "msg_123",
  797. },
  798. },
  799. },
  800. ],
  801. },
  802. ] as any[]
  803. // openai package preserves itemId regardless of store value
  804. const result = ProviderTransform.message(msgs, openaiModel, { store: true }) as any[]
  805. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  806. })
  807. test("preserves metadata for non-openai packages when store is false", () => {
  808. const anthropicModel = {
  809. ...openaiModel,
  810. providerID: "anthropic",
  811. api: {
  812. id: "claude-3",
  813. url: "https://api.anthropic.com",
  814. npm: "@ai-sdk/anthropic",
  815. },
  816. }
  817. const msgs = [
  818. {
  819. role: "assistant",
  820. content: [
  821. {
  822. type: "text",
  823. text: "Hello",
  824. providerOptions: {
  825. openai: {
  826. itemId: "msg_123",
  827. },
  828. },
  829. },
  830. ],
  831. },
  832. ] as any[]
  833. // store=false preserves metadata for non-openai packages
  834. const result = ProviderTransform.message(msgs, anthropicModel, { store: false }) as any[]
  835. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  836. })
  837. test("preserves metadata using providerID key when store is false", () => {
  838. const opencodeModel = {
  839. ...openaiModel,
  840. providerID: "opencode",
  841. api: {
  842. id: "opencode-test",
  843. url: "https://api.opencode.ai",
  844. npm: "@ai-sdk/openai-compatible",
  845. },
  846. }
  847. const msgs = [
  848. {
  849. role: "assistant",
  850. content: [
  851. {
  852. type: "text",
  853. text: "Hello",
  854. providerOptions: {
  855. opencode: {
  856. itemId: "msg_123",
  857. otherOption: "value",
  858. },
  859. },
  860. },
  861. ],
  862. },
  863. ] as any[]
  864. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  865. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_123")
  866. expect(result[0].content[0].providerOptions?.opencode?.otherOption).toBe("value")
  867. })
  868. test("preserves itemId across all providerOptions keys", () => {
  869. const opencodeModel = {
  870. ...openaiModel,
  871. providerID: "opencode",
  872. api: {
  873. id: "opencode-test",
  874. url: "https://api.opencode.ai",
  875. npm: "@ai-sdk/openai-compatible",
  876. },
  877. }
  878. const msgs = [
  879. {
  880. role: "assistant",
  881. providerOptions: {
  882. openai: { itemId: "msg_root" },
  883. opencode: { itemId: "msg_opencode" },
  884. extra: { itemId: "msg_extra" },
  885. },
  886. content: [
  887. {
  888. type: "text",
  889. text: "Hello",
  890. providerOptions: {
  891. openai: { itemId: "msg_openai_part" },
  892. opencode: { itemId: "msg_opencode_part" },
  893. extra: { itemId: "msg_extra_part" },
  894. },
  895. },
  896. ],
  897. },
  898. ] as any[]
  899. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  900. expect(result[0].providerOptions?.openai?.itemId).toBe("msg_root")
  901. expect(result[0].providerOptions?.opencode?.itemId).toBe("msg_opencode")
  902. expect(result[0].providerOptions?.extra?.itemId).toBe("msg_extra")
  903. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_openai_part")
  904. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_opencode_part")
  905. expect(result[0].content[0].providerOptions?.extra?.itemId).toBe("msg_extra_part")
  906. })
  907. test("does not strip metadata for non-openai packages when store is not false", () => {
  908. const anthropicModel = {
  909. ...openaiModel,
  910. providerID: "anthropic",
  911. api: {
  912. id: "claude-3",
  913. url: "https://api.anthropic.com",
  914. npm: "@ai-sdk/anthropic",
  915. },
  916. }
  917. const msgs = [
  918. {
  919. role: "assistant",
  920. content: [
  921. {
  922. type: "text",
  923. text: "Hello",
  924. providerOptions: {
  925. openai: {
  926. itemId: "msg_123",
  927. },
  928. },
  929. },
  930. ],
  931. },
  932. ] as any[]
  933. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  934. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  935. })
  936. })
  937. describe("ProviderTransform.message - providerOptions key remapping", () => {
  938. const createModel = (providerID: string, npm: string) =>
  939. ({
  940. id: `${providerID}/test-model`,
  941. providerID,
  942. api: {
  943. id: "test-model",
  944. url: "https://api.test.com",
  945. npm,
  946. },
  947. name: "Test Model",
  948. capabilities: {
  949. temperature: true,
  950. reasoning: false,
  951. attachment: true,
  952. toolcall: true,
  953. input: { text: true, audio: false, image: true, video: false, pdf: true },
  954. output: { text: true, audio: false, image: false, video: false, pdf: false },
  955. interleaved: false,
  956. },
  957. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  958. limit: { context: 128000, output: 8192 },
  959. status: "active",
  960. options: {},
  961. headers: {},
  962. }) as any
  963. test("azure keeps 'azure' key and does not remap to 'openai'", () => {
  964. const model = createModel("azure", "@ai-sdk/azure")
  965. const msgs = [
  966. {
  967. role: "user",
  968. content: "Hello",
  969. providerOptions: {
  970. azure: { someOption: "value" },
  971. },
  972. },
  973. ] as any[]
  974. const result = ProviderTransform.message(msgs, model, {})
  975. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  976. expect(result[0].providerOptions?.openai).toBeUndefined()
  977. })
  978. test("openai with github-copilot npm remaps providerID to 'openai'", () => {
  979. const model = createModel("github-copilot", "@ai-sdk/github-copilot")
  980. const msgs = [
  981. {
  982. role: "user",
  983. content: "Hello",
  984. providerOptions: {
  985. "github-copilot": { someOption: "value" },
  986. },
  987. },
  988. ] as any[]
  989. const result = ProviderTransform.message(msgs, model, {})
  990. expect(result[0].providerOptions?.openai).toEqual({ someOption: "value" })
  991. expect(result[0].providerOptions?.["github-copilot"]).toBeUndefined()
  992. })
  993. test("bedrock remaps providerID to 'bedrock' key", () => {
  994. const model = createModel("my-bedrock", "@ai-sdk/amazon-bedrock")
  995. const msgs = [
  996. {
  997. role: "user",
  998. content: "Hello",
  999. providerOptions: {
  1000. "my-bedrock": { someOption: "value" },
  1001. },
  1002. },
  1003. ] as any[]
  1004. const result = ProviderTransform.message(msgs, model, {})
  1005. expect(result[0].providerOptions?.bedrock).toEqual({ someOption: "value" })
  1006. expect(result[0].providerOptions?.["my-bedrock"]).toBeUndefined()
  1007. })
  1008. })
  1009. describe("ProviderTransform.message - claude w/bedrock custom inference profile", () => {
  1010. test("adds cachePoint", () => {
  1011. const model = {
  1012. id: "amazon-bedrock/custom-claude-sonnet-4.5",
  1013. providerID: "amazon-bedrock",
  1014. api: {
  1015. id: "arn:aws:bedrock:xxx:yyy:application-inference-profile/zzz",
  1016. url: "https://api.test.com",
  1017. npm: "@ai-sdk/amazon-bedrock",
  1018. },
  1019. name: "Custom inference profile",
  1020. capabilities: {},
  1021. options: {},
  1022. headers: {},
  1023. } as any
  1024. const msgs = [
  1025. {
  1026. role: "user",
  1027. content: "Hello",
  1028. },
  1029. ] as any[]
  1030. const result = ProviderTransform.message(msgs, model, {})
  1031. expect(result[0].providerOptions?.bedrock).toEqual(
  1032. expect.objectContaining({
  1033. cachePoint: {
  1034. type: "ephemeral",
  1035. },
  1036. }),
  1037. )
  1038. })
  1039. })
  1040. describe("ProviderTransform.variants", () => {
  1041. const createMockModel = (overrides: Partial<any> = {}): any => ({
  1042. id: "test/test-model",
  1043. providerID: "test",
  1044. api: {
  1045. id: "test-model",
  1046. url: "https://api.test.com",
  1047. npm: "@ai-sdk/openai",
  1048. },
  1049. name: "Test Model",
  1050. capabilities: {
  1051. temperature: true,
  1052. reasoning: true,
  1053. attachment: true,
  1054. toolcall: true,
  1055. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1056. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1057. interleaved: false,
  1058. },
  1059. cost: {
  1060. input: 0.001,
  1061. output: 0.002,
  1062. cache: { read: 0.0001, write: 0.0002 },
  1063. },
  1064. limit: {
  1065. context: 200_000,
  1066. output: 64_000,
  1067. },
  1068. status: "active",
  1069. options: {},
  1070. headers: {},
  1071. release_date: "2024-01-01",
  1072. ...overrides,
  1073. })
  1074. test("returns empty object when model has no reasoning capabilities", () => {
  1075. const model = createMockModel({
  1076. capabilities: { reasoning: false },
  1077. })
  1078. const result = ProviderTransform.variants(model)
  1079. expect(result).toEqual({})
  1080. })
  1081. test("deepseek returns empty object", () => {
  1082. const model = createMockModel({
  1083. id: "deepseek/deepseek-chat",
  1084. providerID: "deepseek",
  1085. api: {
  1086. id: "deepseek-chat",
  1087. url: "https://api.deepseek.com",
  1088. npm: "@ai-sdk/openai-compatible",
  1089. },
  1090. })
  1091. const result = ProviderTransform.variants(model)
  1092. expect(result).toEqual({})
  1093. })
  1094. test("minimax returns empty object", () => {
  1095. const model = createMockModel({
  1096. id: "minimax/minimax-model",
  1097. providerID: "minimax",
  1098. api: {
  1099. id: "minimax-model",
  1100. url: "https://api.minimax.com",
  1101. npm: "@ai-sdk/openai-compatible",
  1102. },
  1103. })
  1104. const result = ProviderTransform.variants(model)
  1105. expect(result).toEqual({})
  1106. })
  1107. test("glm returns empty object", () => {
  1108. const model = createMockModel({
  1109. id: "glm/glm-4",
  1110. providerID: "glm",
  1111. api: {
  1112. id: "glm-4",
  1113. url: "https://api.glm.com",
  1114. npm: "@ai-sdk/openai-compatible",
  1115. },
  1116. })
  1117. const result = ProviderTransform.variants(model)
  1118. expect(result).toEqual({})
  1119. })
  1120. test("mistral returns empty object", () => {
  1121. const model = createMockModel({
  1122. id: "mistral/mistral-large",
  1123. providerID: "mistral",
  1124. api: {
  1125. id: "mistral-large-latest",
  1126. url: "https://api.mistral.com",
  1127. npm: "@ai-sdk/mistral",
  1128. },
  1129. })
  1130. const result = ProviderTransform.variants(model)
  1131. expect(result).toEqual({})
  1132. })
  1133. describe("@openrouter/ai-sdk-provider", () => {
  1134. test("returns empty object for non-qualifying models", () => {
  1135. const model = createMockModel({
  1136. id: "openrouter/test-model",
  1137. providerID: "openrouter",
  1138. api: {
  1139. id: "test-model",
  1140. url: "https://openrouter.ai",
  1141. npm: "@openrouter/ai-sdk-provider",
  1142. },
  1143. })
  1144. const result = ProviderTransform.variants(model)
  1145. expect(result).toEqual({})
  1146. })
  1147. test("gpt models return OPENAI_EFFORTS with reasoning", () => {
  1148. const model = createMockModel({
  1149. id: "openrouter/gpt-4",
  1150. providerID: "openrouter",
  1151. api: {
  1152. id: "gpt-4",
  1153. url: "https://openrouter.ai",
  1154. npm: "@openrouter/ai-sdk-provider",
  1155. },
  1156. })
  1157. const result = ProviderTransform.variants(model)
  1158. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  1159. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  1160. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  1161. })
  1162. test("gemini-3 returns OPENAI_EFFORTS with reasoning", () => {
  1163. const model = createMockModel({
  1164. id: "openrouter/gemini-3-5-pro",
  1165. providerID: "openrouter",
  1166. api: {
  1167. id: "gemini-3-5-pro",
  1168. url: "https://openrouter.ai",
  1169. npm: "@openrouter/ai-sdk-provider",
  1170. },
  1171. })
  1172. const result = ProviderTransform.variants(model)
  1173. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  1174. })
  1175. test("grok-4 returns empty object", () => {
  1176. const model = createMockModel({
  1177. id: "openrouter/grok-4",
  1178. providerID: "openrouter",
  1179. api: {
  1180. id: "grok-4",
  1181. url: "https://openrouter.ai",
  1182. npm: "@openrouter/ai-sdk-provider",
  1183. },
  1184. })
  1185. const result = ProviderTransform.variants(model)
  1186. expect(result).toEqual({})
  1187. })
  1188. test("grok-3-mini returns low and high with reasoning", () => {
  1189. const model = createMockModel({
  1190. id: "openrouter/grok-3-mini",
  1191. providerID: "openrouter",
  1192. api: {
  1193. id: "grok-3-mini",
  1194. url: "https://openrouter.ai",
  1195. npm: "@openrouter/ai-sdk-provider",
  1196. },
  1197. })
  1198. const result = ProviderTransform.variants(model)
  1199. expect(Object.keys(result)).toEqual(["low", "high"])
  1200. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  1201. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  1202. })
  1203. })
  1204. describe("@ai-sdk/gateway", () => {
  1205. test("returns OPENAI_EFFORTS with reasoningEffort", () => {
  1206. const model = createMockModel({
  1207. id: "gateway/gateway-model",
  1208. providerID: "gateway",
  1209. api: {
  1210. id: "gateway-model",
  1211. url: "https://gateway.ai",
  1212. npm: "@ai-sdk/gateway",
  1213. },
  1214. })
  1215. const result = ProviderTransform.variants(model)
  1216. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  1217. expect(result.low).toEqual({ reasoningEffort: "low" })
  1218. expect(result.high).toEqual({ reasoningEffort: "high" })
  1219. })
  1220. })
  1221. describe("@ai-sdk/github-copilot", () => {
  1222. test("standard models return low, medium, high", () => {
  1223. const model = createMockModel({
  1224. id: "gpt-4.5",
  1225. providerID: "github-copilot",
  1226. api: {
  1227. id: "gpt-4.5",
  1228. url: "https://api.githubcopilot.com",
  1229. npm: "@ai-sdk/github-copilot",
  1230. },
  1231. })
  1232. const result = ProviderTransform.variants(model)
  1233. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1234. expect(result.low).toEqual({
  1235. reasoningEffort: "low",
  1236. reasoningSummary: "auto",
  1237. include: ["reasoning.encrypted_content"],
  1238. })
  1239. })
  1240. test("gpt-5.1-codex-max includes xhigh", () => {
  1241. const model = createMockModel({
  1242. id: "gpt-5.1-codex-max",
  1243. providerID: "github-copilot",
  1244. api: {
  1245. id: "gpt-5.1-codex-max",
  1246. url: "https://api.githubcopilot.com",
  1247. npm: "@ai-sdk/github-copilot",
  1248. },
  1249. })
  1250. const result = ProviderTransform.variants(model)
  1251. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  1252. })
  1253. test("gpt-5.1-codex-mini does not include xhigh", () => {
  1254. const model = createMockModel({
  1255. id: "gpt-5.1-codex-mini",
  1256. providerID: "github-copilot",
  1257. api: {
  1258. id: "gpt-5.1-codex-mini",
  1259. url: "https://api.githubcopilot.com",
  1260. npm: "@ai-sdk/github-copilot",
  1261. },
  1262. })
  1263. const result = ProviderTransform.variants(model)
  1264. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1265. })
  1266. test("gpt-5.1-codex does not include xhigh", () => {
  1267. const model = createMockModel({
  1268. id: "gpt-5.1-codex",
  1269. providerID: "github-copilot",
  1270. api: {
  1271. id: "gpt-5.1-codex",
  1272. url: "https://api.githubcopilot.com",
  1273. npm: "@ai-sdk/github-copilot",
  1274. },
  1275. })
  1276. const result = ProviderTransform.variants(model)
  1277. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1278. })
  1279. test("gpt-5.2 includes xhigh", () => {
  1280. const model = createMockModel({
  1281. id: "gpt-5.2",
  1282. providerID: "github-copilot",
  1283. api: {
  1284. id: "gpt-5.2",
  1285. url: "https://api.githubcopilot.com",
  1286. npm: "@ai-sdk/github-copilot",
  1287. },
  1288. })
  1289. const result = ProviderTransform.variants(model)
  1290. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  1291. expect(result.xhigh).toEqual({
  1292. reasoningEffort: "xhigh",
  1293. reasoningSummary: "auto",
  1294. include: ["reasoning.encrypted_content"],
  1295. })
  1296. })
  1297. test("gpt-5.2-codex includes xhigh", () => {
  1298. const model = createMockModel({
  1299. id: "gpt-5.2-codex",
  1300. providerID: "github-copilot",
  1301. api: {
  1302. id: "gpt-5.2-codex",
  1303. url: "https://api.githubcopilot.com",
  1304. npm: "@ai-sdk/github-copilot",
  1305. },
  1306. })
  1307. const result = ProviderTransform.variants(model)
  1308. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  1309. })
  1310. })
  1311. describe("@ai-sdk/cerebras", () => {
  1312. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  1313. const model = createMockModel({
  1314. id: "cerebras/llama-4",
  1315. providerID: "cerebras",
  1316. api: {
  1317. id: "llama-4-sc",
  1318. url: "https://api.cerebras.ai",
  1319. npm: "@ai-sdk/cerebras",
  1320. },
  1321. })
  1322. const result = ProviderTransform.variants(model)
  1323. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1324. expect(result.low).toEqual({ reasoningEffort: "low" })
  1325. expect(result.high).toEqual({ reasoningEffort: "high" })
  1326. })
  1327. })
  1328. describe("@ai-sdk/togetherai", () => {
  1329. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  1330. const model = createMockModel({
  1331. id: "togetherai/llama-4",
  1332. providerID: "togetherai",
  1333. api: {
  1334. id: "llama-4-sc",
  1335. url: "https://api.togetherai.com",
  1336. npm: "@ai-sdk/togetherai",
  1337. },
  1338. })
  1339. const result = ProviderTransform.variants(model)
  1340. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1341. expect(result.low).toEqual({ reasoningEffort: "low" })
  1342. expect(result.high).toEqual({ reasoningEffort: "high" })
  1343. })
  1344. })
  1345. describe("@ai-sdk/xai", () => {
  1346. test("grok-3 returns empty object", () => {
  1347. const model = createMockModel({
  1348. id: "xai/grok-3",
  1349. providerID: "xai",
  1350. api: {
  1351. id: "grok-3",
  1352. url: "https://api.x.ai",
  1353. npm: "@ai-sdk/xai",
  1354. },
  1355. })
  1356. const result = ProviderTransform.variants(model)
  1357. expect(result).toEqual({})
  1358. })
  1359. test("grok-3-mini returns low and high with reasoningEffort", () => {
  1360. const model = createMockModel({
  1361. id: "xai/grok-3-mini",
  1362. providerID: "xai",
  1363. api: {
  1364. id: "grok-3-mini",
  1365. url: "https://api.x.ai",
  1366. npm: "@ai-sdk/xai",
  1367. },
  1368. })
  1369. const result = ProviderTransform.variants(model)
  1370. expect(Object.keys(result)).toEqual(["low", "high"])
  1371. expect(result.low).toEqual({ reasoningEffort: "low" })
  1372. expect(result.high).toEqual({ reasoningEffort: "high" })
  1373. })
  1374. })
  1375. describe("@ai-sdk/deepinfra", () => {
  1376. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  1377. const model = createMockModel({
  1378. id: "deepinfra/llama-4",
  1379. providerID: "deepinfra",
  1380. api: {
  1381. id: "llama-4-sc",
  1382. url: "https://api.deepinfra.com",
  1383. npm: "@ai-sdk/deepinfra",
  1384. },
  1385. })
  1386. const result = ProviderTransform.variants(model)
  1387. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1388. expect(result.low).toEqual({ reasoningEffort: "low" })
  1389. expect(result.high).toEqual({ reasoningEffort: "high" })
  1390. })
  1391. })
  1392. describe("@ai-sdk/openai-compatible", () => {
  1393. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  1394. const model = createMockModel({
  1395. id: "custom-provider/custom-model",
  1396. providerID: "custom-provider",
  1397. api: {
  1398. id: "custom-model",
  1399. url: "https://api.custom.com",
  1400. npm: "@ai-sdk/openai-compatible",
  1401. },
  1402. })
  1403. const result = ProviderTransform.variants(model)
  1404. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1405. expect(result.low).toEqual({ reasoningEffort: "low" })
  1406. expect(result.high).toEqual({ reasoningEffort: "high" })
  1407. })
  1408. test("Claude via LiteLLM returns thinking with snake_case budget_tokens", () => {
  1409. const model = createMockModel({
  1410. id: "anthropic/claude-sonnet-4-5",
  1411. providerID: "anthropic",
  1412. api: {
  1413. id: "claude-sonnet-4-5-20250929",
  1414. url: "http://localhost:4000",
  1415. npm: "@ai-sdk/openai-compatible",
  1416. },
  1417. })
  1418. const result = ProviderTransform.variants(model)
  1419. expect(Object.keys(result)).toEqual(["high", "max"])
  1420. expect(result.high).toEqual({
  1421. thinking: {
  1422. type: "enabled",
  1423. budget_tokens: 16000,
  1424. },
  1425. })
  1426. expect(result.max).toEqual({
  1427. thinking: {
  1428. type: "enabled",
  1429. budget_tokens: 31999,
  1430. },
  1431. })
  1432. })
  1433. test("Claude model (by model.id) via openai-compatible uses snake_case", () => {
  1434. const model = createMockModel({
  1435. id: "litellm/claude-3-opus",
  1436. providerID: "litellm",
  1437. api: {
  1438. id: "claude-3-opus-20240229",
  1439. url: "http://localhost:4000",
  1440. npm: "@ai-sdk/openai-compatible",
  1441. },
  1442. })
  1443. const result = ProviderTransform.variants(model)
  1444. expect(Object.keys(result)).toEqual(["high", "max"])
  1445. expect(result.high).toEqual({
  1446. thinking: {
  1447. type: "enabled",
  1448. budget_tokens: 16000,
  1449. },
  1450. })
  1451. })
  1452. test("Anthropic model (by model.api.id) via openai-compatible uses snake_case", () => {
  1453. const model = createMockModel({
  1454. id: "custom/my-model",
  1455. providerID: "custom",
  1456. api: {
  1457. id: "anthropic.claude-sonnet",
  1458. url: "http://localhost:4000",
  1459. npm: "@ai-sdk/openai-compatible",
  1460. },
  1461. })
  1462. const result = ProviderTransform.variants(model)
  1463. expect(Object.keys(result)).toEqual(["high", "max"])
  1464. expect(result.high.thinking.budget_tokens).toBe(16000)
  1465. })
  1466. })
  1467. describe("@ai-sdk/azure", () => {
  1468. test("o1-mini returns empty object", () => {
  1469. const model = createMockModel({
  1470. id: "o1-mini",
  1471. providerID: "azure",
  1472. api: {
  1473. id: "o1-mini",
  1474. url: "https://azure.com",
  1475. npm: "@ai-sdk/azure",
  1476. },
  1477. })
  1478. const result = ProviderTransform.variants(model)
  1479. expect(result).toEqual({})
  1480. })
  1481. test("standard azure models return custom efforts with reasoningSummary", () => {
  1482. const model = createMockModel({
  1483. id: "o1",
  1484. providerID: "azure",
  1485. api: {
  1486. id: "o1",
  1487. url: "https://azure.com",
  1488. npm: "@ai-sdk/azure",
  1489. },
  1490. })
  1491. const result = ProviderTransform.variants(model)
  1492. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1493. expect(result.low).toEqual({
  1494. reasoningEffort: "low",
  1495. reasoningSummary: "auto",
  1496. include: ["reasoning.encrypted_content"],
  1497. })
  1498. })
  1499. test("gpt-5 adds minimal effort", () => {
  1500. const model = createMockModel({
  1501. id: "gpt-5",
  1502. providerID: "azure",
  1503. api: {
  1504. id: "gpt-5",
  1505. url: "https://azure.com",
  1506. npm: "@ai-sdk/azure",
  1507. },
  1508. })
  1509. const result = ProviderTransform.variants(model)
  1510. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  1511. })
  1512. })
  1513. describe("@ai-sdk/openai", () => {
  1514. test("gpt-5-pro returns empty object", () => {
  1515. const model = createMockModel({
  1516. id: "gpt-5-pro",
  1517. providerID: "openai",
  1518. api: {
  1519. id: "gpt-5-pro",
  1520. url: "https://api.openai.com",
  1521. npm: "@ai-sdk/openai",
  1522. },
  1523. })
  1524. const result = ProviderTransform.variants(model)
  1525. expect(result).toEqual({})
  1526. })
  1527. test("standard openai models return custom efforts with reasoningSummary", () => {
  1528. const model = createMockModel({
  1529. id: "gpt-5",
  1530. providerID: "openai",
  1531. api: {
  1532. id: "gpt-5",
  1533. url: "https://api.openai.com",
  1534. npm: "@ai-sdk/openai",
  1535. },
  1536. release_date: "2024-06-01",
  1537. })
  1538. const result = ProviderTransform.variants(model)
  1539. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  1540. expect(result.low).toEqual({
  1541. reasoningEffort: "low",
  1542. reasoningSummary: "auto",
  1543. include: ["reasoning.encrypted_content"],
  1544. })
  1545. })
  1546. test("models after 2025-11-13 include 'none' effort", () => {
  1547. const model = createMockModel({
  1548. id: "gpt-5-nano",
  1549. providerID: "openai",
  1550. api: {
  1551. id: "gpt-5-nano",
  1552. url: "https://api.openai.com",
  1553. npm: "@ai-sdk/openai",
  1554. },
  1555. release_date: "2025-11-14",
  1556. })
  1557. const result = ProviderTransform.variants(model)
  1558. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high"])
  1559. })
  1560. test("models after 2025-12-04 include 'xhigh' effort", () => {
  1561. const model = createMockModel({
  1562. id: "openai/gpt-5-chat",
  1563. providerID: "openai",
  1564. api: {
  1565. id: "gpt-5-chat",
  1566. url: "https://api.openai.com",
  1567. npm: "@ai-sdk/openai",
  1568. },
  1569. release_date: "2025-12-05",
  1570. })
  1571. const result = ProviderTransform.variants(model)
  1572. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  1573. })
  1574. })
  1575. describe("@ai-sdk/anthropic", () => {
  1576. test("returns high and max with thinking config", () => {
  1577. const model = createMockModel({
  1578. id: "anthropic/claude-4",
  1579. providerID: "anthropic",
  1580. api: {
  1581. id: "claude-4",
  1582. url: "https://api.anthropic.com",
  1583. npm: "@ai-sdk/anthropic",
  1584. },
  1585. })
  1586. const result = ProviderTransform.variants(model)
  1587. expect(Object.keys(result)).toEqual(["high", "max"])
  1588. expect(result.high).toEqual({
  1589. thinking: {
  1590. type: "enabled",
  1591. budgetTokens: 16000,
  1592. },
  1593. })
  1594. expect(result.max).toEqual({
  1595. thinking: {
  1596. type: "enabled",
  1597. budgetTokens: 31999,
  1598. },
  1599. })
  1600. })
  1601. })
  1602. describe("@ai-sdk/amazon-bedrock", () => {
  1603. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningConfig", () => {
  1604. const model = createMockModel({
  1605. id: "bedrock/llama-4",
  1606. providerID: "bedrock",
  1607. api: {
  1608. id: "llama-4-sc",
  1609. url: "https://bedrock.amazonaws.com",
  1610. npm: "@ai-sdk/amazon-bedrock",
  1611. },
  1612. })
  1613. const result = ProviderTransform.variants(model)
  1614. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  1615. expect(result.low).toEqual({
  1616. reasoningConfig: {
  1617. type: "enabled",
  1618. maxReasoningEffort: "low",
  1619. },
  1620. })
  1621. })
  1622. })
  1623. describe("@ai-sdk/google", () => {
  1624. test("gemini-2.5 returns high and max with thinkingConfig and thinkingBudget", () => {
  1625. const model = createMockModel({
  1626. id: "google/gemini-2.5-pro",
  1627. providerID: "google",
  1628. api: {
  1629. id: "gemini-2.5-pro",
  1630. url: "https://generativelanguage.googleapis.com",
  1631. npm: "@ai-sdk/google",
  1632. },
  1633. })
  1634. const result = ProviderTransform.variants(model)
  1635. expect(Object.keys(result)).toEqual(["high", "max"])
  1636. expect(result.high).toEqual({
  1637. thinkingConfig: {
  1638. includeThoughts: true,
  1639. thinkingBudget: 16000,
  1640. },
  1641. })
  1642. expect(result.max).toEqual({
  1643. thinkingConfig: {
  1644. includeThoughts: true,
  1645. thinkingBudget: 24576,
  1646. },
  1647. })
  1648. })
  1649. test("other gemini models return low and high with thinkingLevel", () => {
  1650. const model = createMockModel({
  1651. id: "google/gemini-2.0-pro",
  1652. providerID: "google",
  1653. api: {
  1654. id: "gemini-2.0-pro",
  1655. url: "https://generativelanguage.googleapis.com",
  1656. npm: "@ai-sdk/google",
  1657. },
  1658. })
  1659. const result = ProviderTransform.variants(model)
  1660. expect(Object.keys(result)).toEqual(["low", "high"])
  1661. expect(result.low).toEqual({
  1662. includeThoughts: true,
  1663. thinkingLevel: "low",
  1664. })
  1665. expect(result.high).toEqual({
  1666. includeThoughts: true,
  1667. thinkingLevel: "high",
  1668. })
  1669. })
  1670. })
  1671. describe("@ai-sdk/google-vertex", () => {
  1672. test("gemini-2.5 returns high and max with thinkingConfig and thinkingBudget", () => {
  1673. const model = createMockModel({
  1674. id: "google-vertex/gemini-2.5-pro",
  1675. providerID: "google-vertex",
  1676. api: {
  1677. id: "gemini-2.5-pro",
  1678. url: "https://vertexai.googleapis.com",
  1679. npm: "@ai-sdk/google-vertex",
  1680. },
  1681. })
  1682. const result = ProviderTransform.variants(model)
  1683. expect(Object.keys(result)).toEqual(["high", "max"])
  1684. })
  1685. test("other vertex models return low and high with thinkingLevel", () => {
  1686. const model = createMockModel({
  1687. id: "google-vertex/gemini-2.0-pro",
  1688. providerID: "google-vertex",
  1689. api: {
  1690. id: "gemini-2.0-pro",
  1691. url: "https://vertexai.googleapis.com",
  1692. npm: "@ai-sdk/google-vertex",
  1693. },
  1694. })
  1695. const result = ProviderTransform.variants(model)
  1696. expect(Object.keys(result)).toEqual(["low", "high"])
  1697. })
  1698. })
  1699. describe("@ai-sdk/cohere", () => {
  1700. test("returns empty object", () => {
  1701. const model = createMockModel({
  1702. id: "cohere/command-r",
  1703. providerID: "cohere",
  1704. api: {
  1705. id: "command-r",
  1706. url: "https://api.cohere.com",
  1707. npm: "@ai-sdk/cohere",
  1708. },
  1709. })
  1710. const result = ProviderTransform.variants(model)
  1711. expect(result).toEqual({})
  1712. })
  1713. })
  1714. describe("@ai-sdk/groq", () => {
  1715. test("returns none and WIDELY_SUPPORTED_EFFORTS with thinkingLevel", () => {
  1716. const model = createMockModel({
  1717. id: "groq/llama-4",
  1718. providerID: "groq",
  1719. api: {
  1720. id: "llama-4-sc",
  1721. url: "https://api.groq.com",
  1722. npm: "@ai-sdk/groq",
  1723. },
  1724. })
  1725. const result = ProviderTransform.variants(model)
  1726. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high"])
  1727. expect(result.none).toEqual({
  1728. includeThoughts: true,
  1729. thinkingLevel: "none",
  1730. })
  1731. expect(result.low).toEqual({
  1732. includeThoughts: true,
  1733. thinkingLevel: "low",
  1734. })
  1735. })
  1736. })
  1737. describe("@ai-sdk/perplexity", () => {
  1738. test("returns empty object", () => {
  1739. const model = createMockModel({
  1740. id: "perplexity/sonar-plus",
  1741. providerID: "perplexity",
  1742. api: {
  1743. id: "sonar-plus",
  1744. url: "https://api.perplexity.ai",
  1745. npm: "@ai-sdk/perplexity",
  1746. },
  1747. })
  1748. const result = ProviderTransform.variants(model)
  1749. expect(result).toEqual({})
  1750. })
  1751. })
  1752. })