transform.test.ts 55 KB

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