transform.test.ts 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140
  1. import { describe, expect, test } from "bun:test"
  2. import { Effect } from "effect"
  3. import { ProviderTransform } from "@/provider/transform"
  4. import { LLMRequestPrep } from "@/session/llm/request"
  5. import { ProviderV2 } from "@opencode-ai/core/provider"
  6. import { ModelV2 } from "@opencode-ai/core/model"
  7. import { ModelsDev } from "@opencode-ai/core/models-dev"
  8. import { jsonSchema } from "ai"
  9. describe("ProviderTransform.options - setCacheKey", () => {
  10. const sessionID = "test-session-123"
  11. const mockModel = {
  12. id: "anthropic/claude-3-5-sonnet",
  13. providerID: "anthropic",
  14. api: {
  15. id: "claude-3-5-sonnet-20241022",
  16. url: "https://api.anthropic.com",
  17. npm: "@ai-sdk/anthropic",
  18. },
  19. name: "Claude 3.5 Sonnet",
  20. capabilities: {
  21. temperature: true,
  22. reasoning: false,
  23. attachment: true,
  24. toolcall: true,
  25. input: { text: true, audio: false, image: true, video: false, pdf: true },
  26. output: { text: true, audio: false, image: false, video: false, pdf: false },
  27. interleaved: false,
  28. },
  29. cost: {
  30. input: 0.003,
  31. output: 0.015,
  32. cache: { read: 0.0003, write: 0.00375 },
  33. },
  34. limit: {
  35. context: 200000,
  36. output: 8192,
  37. },
  38. status: "active",
  39. options: {},
  40. headers: {},
  41. } as any
  42. test("should set promptCacheKey when providerOptions.setCacheKey is true", () => {
  43. const result = ProviderTransform.options({
  44. model: mockModel,
  45. sessionID,
  46. providerOptions: { setCacheKey: true },
  47. })
  48. expect(result.promptCacheKey).toBe(sessionID)
  49. })
  50. test("should not set promptCacheKey when providerOptions.setCacheKey is false", () => {
  51. const result = ProviderTransform.options({
  52. model: mockModel,
  53. sessionID,
  54. providerOptions: { setCacheKey: false },
  55. })
  56. expect(result.promptCacheKey).toBeUndefined()
  57. })
  58. test("should not set promptCacheKey when providerOptions is undefined", () => {
  59. const result = ProviderTransform.options({
  60. model: mockModel,
  61. sessionID,
  62. providerOptions: undefined,
  63. })
  64. expect(result.promptCacheKey).toBeUndefined()
  65. })
  66. test("should not set promptCacheKey when providerOptions does not have setCacheKey", () => {
  67. const result = ProviderTransform.options({ model: mockModel, sessionID, providerOptions: {} })
  68. expect(result.promptCacheKey).toBeUndefined()
  69. })
  70. test("should set promptCacheKey for openai provider by default", () => {
  71. const openaiModel = {
  72. ...mockModel,
  73. providerID: "openai",
  74. api: {
  75. id: "gpt-4",
  76. url: "https://api.openai.com",
  77. npm: "@ai-sdk/openai",
  78. },
  79. }
  80. const result = ProviderTransform.options({ model: openaiModel, sessionID, providerOptions: {} })
  81. expect(result.promptCacheKey).toBe(sessionID)
  82. })
  83. test("should not set promptCacheKey for openai when explicitly disabled", () => {
  84. const openaiModel = {
  85. ...mockModel,
  86. providerID: "openai",
  87. api: {
  88. id: "gpt-4",
  89. url: "https://api.openai.com",
  90. npm: "@ai-sdk/openai",
  91. },
  92. }
  93. const result = ProviderTransform.options({
  94. model: openaiModel,
  95. sessionID,
  96. providerOptions: { setCacheKey: false },
  97. })
  98. expect(result.promptCacheKey).toBeUndefined()
  99. })
  100. test("should set promptCacheKey for the xAI SDK by default regardless of provider ID", () => {
  101. const xaiModel = {
  102. ...mockModel,
  103. providerID: "custom-xai",
  104. api: {
  105. id: "grok-4",
  106. url: "https://api.x.ai",
  107. npm: "@ai-sdk/xai",
  108. },
  109. }
  110. const result = ProviderTransform.options({ model: xaiModel, sessionID, providerOptions: {} })
  111. expect(result.promptCacheKey).toBe(sessionID)
  112. })
  113. test("should not set promptCacheKey for the xAI SDK when explicitly disabled", () => {
  114. const xaiModel = {
  115. ...mockModel,
  116. providerID: "xai",
  117. api: {
  118. id: "grok-4",
  119. url: "https://api.x.ai",
  120. npm: "@ai-sdk/xai",
  121. },
  122. }
  123. const result = ProviderTransform.options({
  124. model: xaiModel,
  125. sessionID,
  126. providerOptions: { setCacheKey: false },
  127. })
  128. expect(result.promptCacheKey).toBeUndefined()
  129. })
  130. test("should set store=false for openai provider", () => {
  131. const openaiModel = {
  132. ...mockModel,
  133. providerID: "openai",
  134. api: {
  135. id: "gpt-4",
  136. url: "https://api.openai.com",
  137. npm: "@ai-sdk/openai",
  138. },
  139. }
  140. const result = ProviderTransform.options({
  141. model: openaiModel,
  142. sessionID,
  143. providerOptions: {},
  144. })
  145. expect(result.store).toBe(false)
  146. })
  147. test("should set store=false for xAI provider by default", () => {
  148. const xaiModel = {
  149. ...mockModel,
  150. providerID: "xai",
  151. api: {
  152. id: "grok-4",
  153. url: "https://api.x.ai",
  154. npm: "@ai-sdk/xai",
  155. },
  156. }
  157. const result = ProviderTransform.options({
  158. model: xaiModel,
  159. sessionID,
  160. providerOptions: {},
  161. })
  162. expect(result.store).toBe(false)
  163. expect(result.promptCacheKey).toBe(sessionID)
  164. })
  165. test("should set store=false for xAI SDK regardless of provider ID", () => {
  166. const xaiModel = {
  167. ...mockModel,
  168. providerID: "custom-xai",
  169. api: {
  170. id: "grok-4",
  171. url: "https://api.x.ai",
  172. npm: "@ai-sdk/xai",
  173. },
  174. }
  175. const result = ProviderTransform.options({
  176. model: xaiModel,
  177. sessionID,
  178. providerOptions: {},
  179. })
  180. expect(result.store).toBe(false)
  181. })
  182. test("should set store=false for azure provider by default", () => {
  183. const azureModel = {
  184. ...mockModel,
  185. providerID: "azure",
  186. api: {
  187. id: "gpt-4",
  188. url: "https://azure.com",
  189. npm: "@ai-sdk/azure",
  190. },
  191. }
  192. const result = ProviderTransform.options({
  193. model: azureModel,
  194. sessionID,
  195. providerOptions: {},
  196. })
  197. expect(result.store).toBe(false)
  198. })
  199. })
  200. describe("ProviderTransform.options - zai/zhipuai thinking", () => {
  201. const sessionID = "test-session-123"
  202. const createModel = (providerID: string) =>
  203. ({
  204. id: `${providerID}/glm-4.6`,
  205. providerID,
  206. api: {
  207. id: "glm-4.6",
  208. url: "https://open.bigmodel.cn/api/paas/v4",
  209. npm: "@ai-sdk/openai-compatible",
  210. },
  211. name: "GLM 4.6",
  212. capabilities: {
  213. temperature: true,
  214. reasoning: true,
  215. attachment: true,
  216. toolcall: true,
  217. input: { text: true, audio: false, image: true, video: false, pdf: true },
  218. output: { text: true, audio: false, image: false, video: false, pdf: false },
  219. interleaved: false,
  220. },
  221. cost: {
  222. input: 0.001,
  223. output: 0.002,
  224. cache: { read: 0.0001, write: 0.0002 },
  225. },
  226. limit: {
  227. context: 128000,
  228. output: 8192,
  229. },
  230. status: "active",
  231. options: {},
  232. headers: {},
  233. }) as any
  234. for (const providerID of ["zai-coding-plan", "zai", "zhipuai-coding-plan", "zhipuai"]) {
  235. test(`${providerID} should set thinking cfg`, () => {
  236. const result = ProviderTransform.options({
  237. model: createModel(providerID),
  238. sessionID,
  239. providerOptions: {},
  240. })
  241. expect(result.thinking).toEqual({
  242. type: "enabled",
  243. clear_thinking: false,
  244. })
  245. })
  246. }
  247. })
  248. describe("ProviderTransform.options - minimax m3 thinking", () => {
  249. const createModel = (npm: string) =>
  250. ({
  251. id: "minimax/minimax-m3",
  252. providerID: "minimax",
  253. api: {
  254. id: "minimax-m3",
  255. url: "https://api.minimax.com",
  256. npm,
  257. },
  258. capabilities: { reasoning: true },
  259. limit: { output: 64_000 },
  260. }) as any
  261. test("explicitly enables adaptive thinking with the anthropic SDK", () => {
  262. expect(
  263. ProviderTransform.options({
  264. model: createModel("@ai-sdk/anthropic"),
  265. sessionID: "test-session-123",
  266. }).thinking,
  267. ).toEqual({ type: "adaptive" })
  268. })
  269. test("uses the native default with the openai-compatible SDK", () => {
  270. expect(
  271. ProviderTransform.options({
  272. model: createModel("@ai-sdk/openai-compatible"),
  273. sessionID: "test-session-123",
  274. }).thinking,
  275. ).toBeUndefined()
  276. })
  277. })
  278. describe("ProviderTransform.options - google thinkingConfig gating", () => {
  279. const sessionID = "test-session-123"
  280. const createGoogleModel = (reasoning: boolean, npm: "@ai-sdk/google" | "@ai-sdk/google-vertex") =>
  281. ({
  282. id: `${npm === "@ai-sdk/google" ? "google" : "google-vertex"}/gemini-2.0-flash`,
  283. providerID: npm === "@ai-sdk/google" ? "google" : "google-vertex",
  284. api: {
  285. id: "gemini-2.0-flash",
  286. url: npm === "@ai-sdk/google" ? "https://generativelanguage.googleapis.com" : "https://vertexai.googleapis.com",
  287. npm,
  288. },
  289. name: "Gemini 2.0 Flash",
  290. capabilities: {
  291. temperature: true,
  292. reasoning,
  293. attachment: true,
  294. toolcall: true,
  295. input: { text: true, audio: false, image: true, video: false, pdf: true },
  296. output: { text: true, audio: false, image: false, video: false, pdf: false },
  297. interleaved: false,
  298. },
  299. cost: {
  300. input: 0.001,
  301. output: 0.002,
  302. cache: { read: 0.0001, write: 0.0002 },
  303. },
  304. limit: {
  305. context: 1_000_000,
  306. output: 8192,
  307. },
  308. status: "active",
  309. options: {},
  310. headers: {},
  311. }) as any
  312. test("does not set thinkingConfig for google models without reasoning capability", () => {
  313. const result = ProviderTransform.options({
  314. model: createGoogleModel(false, "@ai-sdk/google"),
  315. sessionID,
  316. providerOptions: {},
  317. })
  318. expect(result.thinkingConfig).toBeUndefined()
  319. })
  320. test("sets thinkingConfig for google models with reasoning capability", () => {
  321. const result = ProviderTransform.options({
  322. model: createGoogleModel(true, "@ai-sdk/google"),
  323. sessionID,
  324. providerOptions: {},
  325. })
  326. expect(result.thinkingConfig).toEqual({
  327. includeThoughts: true,
  328. })
  329. })
  330. test("does not set thinkingConfig for vertex models without reasoning capability", () => {
  331. const result = ProviderTransform.options({
  332. model: createGoogleModel(false, "@ai-sdk/google-vertex"),
  333. sessionID,
  334. providerOptions: {},
  335. })
  336. expect(result.thinkingConfig).toBeUndefined()
  337. })
  338. })
  339. describe("ProviderTransform.options - gpt-5 textVerbosity", () => {
  340. const sessionID = "test-session-123"
  341. const createGpt5Model = (apiId: string) =>
  342. ({
  343. id: `openai/${apiId}`,
  344. providerID: "openai",
  345. api: {
  346. id: apiId,
  347. url: "https://api.openai.com",
  348. npm: "@ai-sdk/openai",
  349. },
  350. name: apiId,
  351. capabilities: {
  352. temperature: true,
  353. reasoning: true,
  354. attachment: true,
  355. toolcall: true,
  356. input: { text: true, audio: false, image: true, video: false, pdf: false },
  357. output: { text: true, audio: false, image: false, video: false, pdf: false },
  358. interleaved: false,
  359. },
  360. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  361. limit: { context: 128000, output: 4096 },
  362. status: "active",
  363. options: {},
  364. headers: {},
  365. }) as any
  366. test("gpt-5.2 should have textVerbosity set to low", () => {
  367. const model = createGpt5Model("gpt-5.2")
  368. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  369. expect(result.textVerbosity).toBe("low")
  370. expect(result.include).toEqual(["reasoning.encrypted_content"])
  371. })
  372. test("Bedrock Mantle gpt-5.5 uses OpenAI Responses defaults", () => {
  373. const model = {
  374. ...createGpt5Model("openai.gpt-5.5"),
  375. id: "amazon-bedrock/openai.gpt-5.5",
  376. providerID: "amazon-bedrock",
  377. api: {
  378. id: "openai.gpt-5.5",
  379. url: "https://bedrock-mantle.us-east-2.api.aws/openai/v1",
  380. npm: "@ai-sdk/amazon-bedrock/mantle",
  381. },
  382. }
  383. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  384. expect(result.store).toBe(false)
  385. expect(result.reasoningEffort).toBe("medium")
  386. expect(result.reasoningSummary).toBe("auto")
  387. expect(result.include).toEqual(["reasoning.encrypted_content"])
  388. expect(result.textVerbosity).toBe("low")
  389. })
  390. test("openai-compatible gpt-5 models omit Responses-only reasoningSummary", () => {
  391. const model = {
  392. ...createGpt5Model("gpt-5.4"),
  393. id: "cortecs/gpt-5.4",
  394. providerID: "cortecs",
  395. api: {
  396. id: "gpt-5.4",
  397. url: "https://api.cortecs.ai/v1",
  398. npm: "@ai-sdk/openai-compatible",
  399. },
  400. }
  401. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  402. expect(result.reasoningEffort).toBe("medium")
  403. expect(result.reasoningSummary).toBeUndefined()
  404. expect(result.include).toBeUndefined()
  405. })
  406. test("azure chat completions omit Responses-only reasoning options after variants merge", async () => {
  407. const model = {
  408. ...createGpt5Model("gpt-5.4"),
  409. id: "azure/gpt-5.4",
  410. providerID: "azure",
  411. api: {
  412. id: "gpt-5.4",
  413. url: "https://azure.com",
  414. npm: "@ai-sdk/azure",
  415. },
  416. variants: {
  417. high: {
  418. reasoningEffort: "high",
  419. reasoningSummary: "auto",
  420. include: ["reasoning.encrypted_content"],
  421. },
  422. },
  423. }
  424. const result = await Effect.runPromise(
  425. LLMRequestPrep.prepare({
  426. user: {
  427. id: "msg_user-test",
  428. sessionID,
  429. role: "user",
  430. time: { created: Date.now() },
  431. agent: "test",
  432. model: { providerID: "azure", modelID: "gpt-5.4", variant: "high" },
  433. } as any,
  434. sessionID,
  435. model,
  436. agent: {
  437. name: "test",
  438. mode: "primary",
  439. options: {},
  440. permission: [],
  441. } as any,
  442. system: [],
  443. messages: [{ role: "user", content: "Hello" }],
  444. tools: {
  445. lookup: {
  446. description: "Look up a value",
  447. inputSchema: jsonSchema({ type: "object", properties: {} }),
  448. },
  449. },
  450. provider: { id: "azure", options: { useCompletionUrls: true } } as any,
  451. auth: undefined,
  452. plugin: {
  453. trigger: (_name: string, _input: unknown, output: unknown) => Effect.succeed(output),
  454. list: () => Effect.succeed([]),
  455. init: () => Effect.void,
  456. } as any,
  457. flags: { outputTokenMax: 32_000, client: "test" } as any,
  458. isWorkflow: false,
  459. }),
  460. )
  461. expect(result.params.options.reasoningEffort).toBe("high")
  462. expect(result.params.options.reasoningSummary).toBeUndefined()
  463. expect(result.params.options.include).toBeUndefined()
  464. expect(result.tools.lookup.strict).toBe(false)
  465. })
  466. test("gpt-5.1 should have textVerbosity set to low", () => {
  467. const model = createGpt5Model("gpt-5.1")
  468. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  469. expect(result.textVerbosity).toBe("low")
  470. })
  471. test("gpt-5.2-chat-latest should NOT have textVerbosity set (only supports medium)", () => {
  472. const model = createGpt5Model("gpt-5.2-chat-latest")
  473. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  474. expect(result.textVerbosity).toBeUndefined()
  475. })
  476. test("gpt-5.1-chat-latest should NOT have textVerbosity set (only supports medium)", () => {
  477. const model = createGpt5Model("gpt-5.1-chat-latest")
  478. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  479. expect(result.textVerbosity).toBeUndefined()
  480. })
  481. test("gpt-5.2-chat should NOT have textVerbosity set", () => {
  482. const model = createGpt5Model("gpt-5.2-chat")
  483. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  484. expect(result.textVerbosity).toBeUndefined()
  485. })
  486. test("gpt-5-chat should NOT have textVerbosity set", () => {
  487. const model = createGpt5Model("gpt-5-chat")
  488. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  489. expect(result.textVerbosity).toBeUndefined()
  490. })
  491. test("gpt-5.2-codex should NOT have textVerbosity set (codex models excluded)", () => {
  492. const model = createGpt5Model("gpt-5.2-codex")
  493. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  494. expect(result.textVerbosity).toBeUndefined()
  495. })
  496. })
  497. describe("ProviderTransform.options - gpt-5 reasoningEffort", () => {
  498. const sessionID = "test-session-123"
  499. const createModel = (apiId: string) =>
  500. ({
  501. id: `azure/${apiId}`,
  502. providerID: "azure",
  503. api: {
  504. id: apiId,
  505. url: "https://azure.com",
  506. npm: "@ai-sdk/azure",
  507. },
  508. name: apiId,
  509. capabilities: {
  510. temperature: true,
  511. reasoning: true,
  512. attachment: true,
  513. toolcall: true,
  514. input: {
  515. text: true,
  516. audio: false,
  517. image: true,
  518. video: false,
  519. pdf: false,
  520. },
  521. output: {
  522. text: true,
  523. audio: false,
  524. image: false,
  525. video: false,
  526. pdf: false,
  527. },
  528. interleaved: false,
  529. },
  530. cost: {
  531. input: 0.03,
  532. output: 0.06,
  533. cache: { read: 0.001, write: 0.002 },
  534. },
  535. limit: {
  536. context: 128000,
  537. output: 4096,
  538. },
  539. status: "active",
  540. options: {},
  541. headers: {},
  542. }) as any
  543. test("gpt-5-chat should NOT set reasoningEffort", () => {
  544. const result = ProviderTransform.options({
  545. model: createModel("gpt-5-chat"),
  546. sessionID,
  547. providerOptions: {},
  548. })
  549. expect(result.reasoningEffort).toBeUndefined()
  550. })
  551. test("gpt-5.5 should NOT set reasoningEffort", () => {
  552. const result = ProviderTransform.options({
  553. model: createModel("gpt-5.5"),
  554. sessionID,
  555. providerOptions: {},
  556. })
  557. expect(result.reasoningEffort).toBeUndefined()
  558. })
  559. })
  560. describe("ProviderTransform.options - gateway", () => {
  561. const sessionID = "test-session-123"
  562. const createModel = (id: string) =>
  563. ({
  564. id,
  565. providerID: "vercel",
  566. api: {
  567. id,
  568. url: "https://ai-gateway.vercel.sh/v3/ai",
  569. npm: "@ai-sdk/gateway",
  570. },
  571. name: id,
  572. capabilities: {
  573. temperature: true,
  574. reasoning: true,
  575. attachment: true,
  576. toolcall: true,
  577. input: { text: true, audio: false, image: true, video: false, pdf: true },
  578. output: { text: true, audio: false, image: false, video: false, pdf: false },
  579. interleaved: false,
  580. },
  581. cost: {
  582. input: 0.001,
  583. output: 0.002,
  584. cache: { read: 0.0001, write: 0.0002 },
  585. },
  586. limit: {
  587. context: 200_000,
  588. output: 8192,
  589. },
  590. status: "active",
  591. options: {},
  592. headers: {},
  593. release_date: "2024-01-01",
  594. }) as any
  595. test("puts gateway defaults under gateway key", () => {
  596. const model = createModel("anthropic/claude-sonnet-4")
  597. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  598. expect(result).toEqual({
  599. gateway: {
  600. caching: "auto",
  601. },
  602. })
  603. })
  604. })
  605. describe("ProviderTransform.providerOptions", () => {
  606. const createModel = (overrides: Partial<any> = {}) =>
  607. ({
  608. id: "test/test-model",
  609. providerID: "test",
  610. api: {
  611. id: "test-model",
  612. url: "https://api.test.com",
  613. npm: "@ai-sdk/openai",
  614. },
  615. name: "Test Model",
  616. capabilities: {
  617. temperature: true,
  618. reasoning: true,
  619. attachment: true,
  620. toolcall: true,
  621. input: { text: true, audio: false, image: true, video: false, pdf: false },
  622. output: { text: true, audio: false, image: false, video: false, pdf: false },
  623. interleaved: false,
  624. },
  625. cost: {
  626. input: 0.001,
  627. output: 0.002,
  628. cache: { read: 0.0001, write: 0.0002 },
  629. },
  630. limit: {
  631. context: 200_000,
  632. output: 64_000,
  633. },
  634. status: "active",
  635. options: {},
  636. headers: {},
  637. release_date: "2024-01-01",
  638. ...overrides,
  639. }) as any
  640. test("uses sdk key for non-gateway models", () => {
  641. const model = createModel({
  642. providerID: "my-bedrock",
  643. api: {
  644. id: "anthropic.claude-sonnet-4",
  645. url: "https://bedrock.aws",
  646. npm: "@ai-sdk/amazon-bedrock",
  647. },
  648. })
  649. expect(ProviderTransform.providerOptions(model, { cachePoint: { type: "default" } })).toEqual({
  650. bedrock: { cachePoint: { type: "default" } },
  651. })
  652. })
  653. test("forces reasoning for custom OpenAI package models with explicit effort", () => {
  654. const model = createModel({
  655. providerID: "meta",
  656. api: {
  657. id: "muse-spark",
  658. url: "https://api.ai.meta.com/v1",
  659. npm: "@ai-sdk/openai",
  660. },
  661. })
  662. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "xhigh", reasoningSummary: "auto" })).toEqual({
  663. openai: { forceReasoning: true, reasoningEffort: "xhigh", reasoningSummary: "auto" },
  664. })
  665. })
  666. test("forces reasoning for OpenAI package models marked reasoning-capable", () => {
  667. expect(ProviderTransform.providerOptions(createModel(), { store: false })).toEqual({
  668. openai: { forceReasoning: true, store: false },
  669. })
  670. })
  671. test("forces reasoning for explicit effort even when model is not marked reasoning-capable", () => {
  672. const model = createModel({
  673. capabilities: {
  674. temperature: true,
  675. reasoning: false,
  676. attachment: true,
  677. toolcall: true,
  678. input: { text: true, audio: false, image: true, video: false, pdf: false },
  679. output: { text: true, audio: false, image: false, video: false, pdf: false },
  680. interleaved: false,
  681. },
  682. })
  683. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "xhigh" })).toEqual({
  684. openai: { forceReasoning: true, reasoningEffort: "xhigh" },
  685. })
  686. })
  687. test("forces reasoning for Azure OpenAI models with explicit effort", () => {
  688. const model = createModel({
  689. providerID: "azure",
  690. api: {
  691. id: "custom-gpt-5-deployment",
  692. url: "https://azure.openai.example.com/openai/v1",
  693. npm: "@ai-sdk/azure",
  694. },
  695. })
  696. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "xhigh" })).toEqual({
  697. openai: { forceReasoning: true, reasoningEffort: "xhigh" },
  698. azure: { forceReasoning: true, reasoningEffort: "xhigh" },
  699. })
  700. })
  701. test("forces reasoning for Bedrock Mantle OpenAI models with explicit effort", () => {
  702. const model = createModel({
  703. providerID: "amazon-bedrock",
  704. api: {
  705. id: "openai.gpt-5-custom",
  706. url: "https://bedrock-mantle.us-east-2.api.aws/openai/v1",
  707. npm: "@ai-sdk/amazon-bedrock/mantle",
  708. },
  709. })
  710. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "xhigh" })).toEqual({
  711. openai: { forceReasoning: true, reasoningEffort: "xhigh" },
  712. })
  713. })
  714. test("overrides forceReasoning false when reasoning should be forced", () => {
  715. expect(
  716. ProviderTransform.providerOptions(createModel(), { forceReasoning: false, reasoningEffort: "xhigh" }),
  717. ).toEqual({
  718. openai: { forceReasoning: true, reasoningEffort: "xhigh" },
  719. })
  720. })
  721. test("uses gateway model provider slug for gateway models", () => {
  722. const model = createModel({
  723. providerID: "vercel",
  724. api: {
  725. id: "anthropic/claude-sonnet-4",
  726. url: "https://ai-gateway.vercel.sh/v3/ai",
  727. npm: "@ai-sdk/gateway",
  728. },
  729. })
  730. expect(ProviderTransform.providerOptions(model, { thinking: { type: "enabled", budgetTokens: 12_000 } })).toEqual({
  731. anthropic: { thinking: { type: "enabled", budgetTokens: 12_000 } },
  732. })
  733. })
  734. test("falls back to gateway key when gateway api id is unscoped", () => {
  735. const model = createModel({
  736. id: "anthropic/claude-sonnet-4",
  737. providerID: "vercel",
  738. api: {
  739. id: "claude-sonnet-4",
  740. url: "https://ai-gateway.vercel.sh/v3/ai",
  741. npm: "@ai-sdk/gateway",
  742. },
  743. })
  744. expect(ProviderTransform.providerOptions(model, { thinking: { type: "enabled", budgetTokens: 12_000 } })).toEqual({
  745. gateway: { thinking: { type: "enabled", budgetTokens: 12_000 } },
  746. })
  747. })
  748. test("splits gateway routing options from provider-specific options", () => {
  749. const model = createModel({
  750. providerID: "vercel",
  751. api: {
  752. id: "anthropic/claude-sonnet-4",
  753. url: "https://ai-gateway.vercel.sh/v3/ai",
  754. npm: "@ai-sdk/gateway",
  755. },
  756. })
  757. expect(
  758. ProviderTransform.providerOptions(model, {
  759. gateway: { order: ["vertex", "anthropic"] },
  760. thinking: { type: "enabled", budgetTokens: 12_000 },
  761. }),
  762. ).toEqual({
  763. gateway: { order: ["vertex", "anthropic"] },
  764. anthropic: { thinking: { type: "enabled", budgetTokens: 12_000 } },
  765. } as any)
  766. })
  767. test("falls back to gateway key when model id has no provider slug", () => {
  768. const model = createModel({
  769. id: "claude-sonnet-4",
  770. providerID: "vercel",
  771. api: {
  772. id: "claude-sonnet-4",
  773. url: "https://ai-gateway.vercel.sh/v3/ai",
  774. npm: "@ai-sdk/gateway",
  775. },
  776. })
  777. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "high" })).toEqual({
  778. gateway: { reasoningEffort: "high" },
  779. })
  780. })
  781. test("maps amazon slug to bedrock for provider options", () => {
  782. const model = createModel({
  783. providerID: "vercel",
  784. api: {
  785. id: "amazon/nova-2-lite",
  786. url: "https://ai-gateway.vercel.sh/v3/ai",
  787. npm: "@ai-sdk/gateway",
  788. },
  789. })
  790. expect(ProviderTransform.providerOptions(model, { reasoningConfig: { type: "enabled" } })).toEqual({
  791. bedrock: { reasoningConfig: { type: "enabled" } },
  792. })
  793. })
  794. test("maps Bedrock Mantle provider options to OpenAI namespace", () => {
  795. const model = createModel({
  796. providerID: "amazon-bedrock",
  797. api: {
  798. id: "openai.gpt-5.5",
  799. url: "https://bedrock-mantle.us-east-2.api.aws/openai/v1",
  800. npm: "@ai-sdk/amazon-bedrock/mantle",
  801. },
  802. })
  803. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "medium" })).toEqual({
  804. openai: { forceReasoning: true, reasoningEffort: "medium" },
  805. })
  806. })
  807. test("uses groq slug for groq models", () => {
  808. const model = createModel({
  809. providerID: "vercel",
  810. api: {
  811. id: "groq/llama-3.3-70b-versatile",
  812. url: "https://ai-gateway.vercel.sh/v3/ai",
  813. npm: "@ai-sdk/gateway",
  814. },
  815. })
  816. expect(ProviderTransform.providerOptions(model, { reasoningFormat: "parsed" })).toEqual({
  817. groq: { reasoningFormat: "parsed" },
  818. })
  819. })
  820. })
  821. describe("ProviderTransform.schema - gemini array items", () => {
  822. test("adds missing items for array properties", () => {
  823. const geminiModel = {
  824. providerID: "google",
  825. api: {
  826. id: "gemini-3-pro",
  827. },
  828. } as any
  829. const schema = {
  830. type: "object",
  831. properties: {
  832. nodes: { type: "array" },
  833. edges: { type: "array", items: { type: "string" } },
  834. },
  835. } as any
  836. const result = ProviderTransform.schema(geminiModel, schema) as any
  837. expect(result.properties.nodes.items).toBeDefined()
  838. expect(result.properties.edges.items.type).toBe("string")
  839. })
  840. })
  841. describe("ProviderTransform.schema - gemini nested array items", () => {
  842. const geminiModel = {
  843. providerID: "google",
  844. api: {
  845. id: "gemini-3-pro",
  846. },
  847. } as any
  848. test("adds type to 2D array with empty inner items", () => {
  849. const schema = {
  850. type: "object",
  851. properties: {
  852. values: {
  853. type: "array",
  854. items: {
  855. type: "array",
  856. items: {}, // Empty items object
  857. },
  858. },
  859. },
  860. } as any
  861. const result = ProviderTransform.schema(geminiModel, schema) as any
  862. // Inner items should have a default type
  863. expect(result.properties.values.items.items.type).toBe("string")
  864. })
  865. test("adds items and type to 2D array with missing inner items", () => {
  866. const schema = {
  867. type: "object",
  868. properties: {
  869. data: {
  870. type: "array",
  871. items: { type: "array" }, // No items at all
  872. },
  873. },
  874. } as any
  875. const result = ProviderTransform.schema(geminiModel, schema) as any
  876. expect(result.properties.data.items.items).toBeDefined()
  877. expect(result.properties.data.items.items.type).toBe("string")
  878. })
  879. test("handles deeply nested arrays (3D)", () => {
  880. const schema = {
  881. type: "object",
  882. properties: {
  883. matrix: {
  884. type: "array",
  885. items: {
  886. type: "array",
  887. items: {
  888. type: "array",
  889. // No items
  890. },
  891. },
  892. },
  893. },
  894. } as any
  895. const result = ProviderTransform.schema(geminiModel, schema) as any
  896. expect(result.properties.matrix.items.items.items).toBeDefined()
  897. expect(result.properties.matrix.items.items.items.type).toBe("string")
  898. })
  899. test("preserves existing item types in nested arrays", () => {
  900. const schema = {
  901. type: "object",
  902. properties: {
  903. numbers: {
  904. type: "array",
  905. items: {
  906. type: "array",
  907. items: { type: "number" }, // Has explicit type
  908. },
  909. },
  910. },
  911. } as any
  912. const result = ProviderTransform.schema(geminiModel, schema) as any
  913. // Should preserve the explicit type
  914. expect(result.properties.numbers.items.items.type).toBe("number")
  915. })
  916. test("handles mixed nested structures with objects and arrays", () => {
  917. const schema = {
  918. type: "object",
  919. properties: {
  920. spreadsheetData: {
  921. type: "object",
  922. properties: {
  923. rows: {
  924. type: "array",
  925. items: {
  926. type: "array",
  927. items: {}, // Empty items
  928. },
  929. },
  930. },
  931. },
  932. },
  933. } as any
  934. const result = ProviderTransform.schema(geminiModel, schema) as any
  935. expect(result.properties.spreadsheetData.properties.rows.items.items.type).toBe("string")
  936. })
  937. })
  938. describe("ProviderTransform.schema - gemini type arrays", () => {
  939. // Mirrors @ai-sdk/google's convertJSONSchemaToOpenAPISchema: JSON Schema type
  940. // arrays (e.g. `["number","string"]`, common in MCP tool schemas) become an
  941. // `anyOf` of single-type schemas, with `null` lifted into `nullable`. Plain
  942. // @ai-sdk/google rewrites these, but OpenAI-compatible transports such as
  943. // GitHub Copilot (proxying to Gemini) forward them verbatim and the backend
  944. // rejects the array form.
  945. const geminiModel = {
  946. providerID: "google",
  947. api: {
  948. id: "gemini-3-pro",
  949. },
  950. } as any
  951. test("splits a multi-type array into anyOf and drops the type array", () => {
  952. const schema = {
  953. type: "object",
  954. properties: {
  955. status: { type: ["number", "string"], description: "status filter" },
  956. },
  957. } as any
  958. const result = ProviderTransform.schema(geminiModel, schema) as any
  959. expect(result.properties.status.type).toBeUndefined()
  960. expect(result.properties.status.anyOf).toEqual([{ type: "number" }, { type: "string" }])
  961. expect(result.properties.status.nullable).toBeUndefined()
  962. // Sibling keywords stay alongside the generated anyOf.
  963. expect(result.properties.status.description).toBe("status filter")
  964. })
  965. test("lifts null into nullable for a nullable type array", () => {
  966. const schema = {
  967. type: "object",
  968. properties: {
  969. maybe: { type: ["string", "null"], description: "nullable string" },
  970. },
  971. } as any
  972. const result = ProviderTransform.schema(geminiModel, schema) as any
  973. expect(result.properties.maybe.type).toBeUndefined()
  974. expect(result.properties.maybe.anyOf).toEqual([{ type: "string" }])
  975. expect(result.properties.maybe.nullable).toBe(true)
  976. })
  977. test("collapses an all-null type array to type null", () => {
  978. const schema = {
  979. type: "object",
  980. properties: {
  981. nothing: { type: ["null"] },
  982. },
  983. } as any
  984. const result = ProviderTransform.schema(geminiModel, schema) as any
  985. expect(result.properties.nothing.type).toBe("null")
  986. expect(result.properties.nothing.anyOf).toBeUndefined()
  987. })
  988. test("rewrites type arrays for gemini served through github-copilot", () => {
  989. const copilotGeminiModel = {
  990. providerID: "github-copilot",
  991. api: {
  992. id: "gemini-3.5-flash",
  993. npm: "@ai-sdk/github-copilot",
  994. },
  995. } as any
  996. const schema = {
  997. type: "object",
  998. properties: {
  999. hook_id: { type: "number", description: "ID of the webhook" },
  1000. status: { type: ["number", "string"], description: "Filter by response status code" },
  1001. },
  1002. required: ["hook_id"],
  1003. additionalProperties: false,
  1004. } as any
  1005. const result = ProviderTransform.schema(copilotGeminiModel, schema) as any
  1006. expect(result.properties.status.anyOf).toEqual([{ type: "number" }, { type: "string" }])
  1007. expect(result.properties.status.type).toBeUndefined()
  1008. expect(result.properties.hook_id.type).toBe("number")
  1009. })
  1010. })
  1011. describe("ProviderTransform.schema - gemini combiner nodes", () => {
  1012. const geminiModel = {
  1013. providerID: "google",
  1014. api: {
  1015. id: "gemini-3-pro",
  1016. },
  1017. } as any
  1018. const walk = (node: any, cb: (node: any, path: (string | number)[]) => void, path: (string | number)[] = []) => {
  1019. if (node === null || typeof node !== "object") {
  1020. return
  1021. }
  1022. if (Array.isArray(node)) {
  1023. node.forEach((item, i) => walk(item, cb, [...path, i]))
  1024. return
  1025. }
  1026. cb(node, path)
  1027. Object.entries(node).forEach(([key, value]) => walk(value, cb, [...path, key]))
  1028. }
  1029. test("keeps edits.items.anyOf without adding type", () => {
  1030. const schema = {
  1031. type: "object",
  1032. properties: {
  1033. edits: {
  1034. type: "array",
  1035. items: {
  1036. anyOf: [
  1037. {
  1038. type: "object",
  1039. properties: {
  1040. old_string: { type: "string" },
  1041. new_string: { type: "string" },
  1042. },
  1043. required: ["old_string", "new_string"],
  1044. },
  1045. {
  1046. type: "object",
  1047. properties: {
  1048. old_string: { type: "string" },
  1049. new_string: { type: "string" },
  1050. replace_all: { type: "boolean" },
  1051. },
  1052. required: ["old_string", "new_string"],
  1053. },
  1054. ],
  1055. },
  1056. },
  1057. },
  1058. required: ["edits"],
  1059. } as any
  1060. const result = ProviderTransform.schema(geminiModel, schema) as any
  1061. expect(Array.isArray(result.properties.edits.items.anyOf)).toBe(true)
  1062. expect(result.properties.edits.items.type).toBeUndefined()
  1063. })
  1064. test("does not add sibling keys to combiner nodes during sanitize", () => {
  1065. const schema = {
  1066. type: "object",
  1067. properties: {
  1068. edits: {
  1069. type: "array",
  1070. items: {
  1071. anyOf: [{ type: "string" }, { type: "number" }],
  1072. },
  1073. },
  1074. value: {
  1075. oneOf: [{ type: "string" }, { type: "boolean" }],
  1076. },
  1077. meta: {
  1078. allOf: [
  1079. {
  1080. type: "object",
  1081. properties: { a: { type: "string" } },
  1082. },
  1083. {
  1084. type: "object",
  1085. properties: { b: { type: "string" } },
  1086. },
  1087. ],
  1088. },
  1089. },
  1090. } as any
  1091. const input = JSON.parse(JSON.stringify(schema))
  1092. const result = ProviderTransform.schema(geminiModel, schema) as any
  1093. walk(result, (node, path) => {
  1094. const hasCombiner = Array.isArray(node.anyOf) || Array.isArray(node.oneOf) || Array.isArray(node.allOf)
  1095. if (!hasCombiner) {
  1096. return
  1097. }
  1098. const before = path.reduce((acc: any, key) => acc?.[key], input)
  1099. const added = Object.keys(node).filter((key) => !(key in before))
  1100. expect(added).toEqual([])
  1101. })
  1102. })
  1103. })
  1104. describe("ProviderTransform.schema - gemini non-object properties removal", () => {
  1105. const geminiModel = {
  1106. providerID: "google",
  1107. api: {
  1108. id: "gemini-3-pro",
  1109. },
  1110. } as any
  1111. test("removes properties from non-object types", () => {
  1112. const schema = {
  1113. type: "object",
  1114. properties: {
  1115. data: {
  1116. type: "string",
  1117. properties: { invalid: { type: "string" } },
  1118. },
  1119. },
  1120. } as any
  1121. const result = ProviderTransform.schema(geminiModel, schema) as any
  1122. expect(result.properties.data.type).toBe("string")
  1123. expect(result.properties.data.properties).toBeUndefined()
  1124. })
  1125. test("removes required from non-object types", () => {
  1126. const schema = {
  1127. type: "object",
  1128. properties: {
  1129. data: {
  1130. type: "array",
  1131. items: { type: "string" },
  1132. required: ["invalid"],
  1133. },
  1134. },
  1135. } as any
  1136. const result = ProviderTransform.schema(geminiModel, schema) as any
  1137. expect(result.properties.data.type).toBe("array")
  1138. expect(result.properties.data.required).toBeUndefined()
  1139. })
  1140. test("removes properties and required from nested non-object types", () => {
  1141. const schema = {
  1142. type: "object",
  1143. properties: {
  1144. outer: {
  1145. type: "object",
  1146. properties: {
  1147. inner: {
  1148. type: "number",
  1149. properties: { bad: { type: "string" } },
  1150. required: ["bad"],
  1151. },
  1152. },
  1153. },
  1154. },
  1155. } as any
  1156. const result = ProviderTransform.schema(geminiModel, schema) as any
  1157. expect(result.properties.outer.properties.inner.type).toBe("number")
  1158. expect(result.properties.outer.properties.inner.properties).toBeUndefined()
  1159. expect(result.properties.outer.properties.inner.required).toBeUndefined()
  1160. })
  1161. test("keeps properties and required on object types", () => {
  1162. const schema = {
  1163. type: "object",
  1164. properties: {
  1165. data: {
  1166. type: "object",
  1167. properties: { name: { type: "string" } },
  1168. required: ["name"],
  1169. },
  1170. },
  1171. } as any
  1172. const result = ProviderTransform.schema(geminiModel, schema) as any
  1173. expect(result.properties.data.type).toBe("object")
  1174. expect(result.properties.data.properties).toBeDefined()
  1175. expect(result.properties.data.required).toEqual(["name"])
  1176. })
  1177. test("does not affect non-gemini providers", () => {
  1178. const openaiModel = {
  1179. providerID: "openai",
  1180. api: {
  1181. id: "gpt-4",
  1182. },
  1183. } as any
  1184. const schema = {
  1185. type: "object",
  1186. properties: {
  1187. data: {
  1188. type: "string",
  1189. properties: { invalid: { type: "string" } },
  1190. },
  1191. },
  1192. } as any
  1193. const result = ProviderTransform.schema(openaiModel, schema) as any
  1194. expect(result.properties.data.properties).toBeDefined()
  1195. })
  1196. })
  1197. describe("ProviderTransform.schema - openai supported schema subset", () => {
  1198. const openaiModel = {
  1199. providerID: "openai",
  1200. api: {
  1201. id: "gpt-4.1",
  1202. npm: "@ai-sdk/openai",
  1203. },
  1204. } as any
  1205. test("removes unsupported JSON Schema keywords recursively", () => {
  1206. const result = ProviderTransform.schema(openaiModel, {
  1207. $schema: "https://json-schema.org/draft/2020-12/schema",
  1208. title: "Search",
  1209. type: "object",
  1210. properties: {
  1211. query: {
  1212. type: "string",
  1213. description: "Search query",
  1214. format: "uri",
  1215. pattern: "^https://",
  1216. minLength: 1,
  1217. maxLength: 100,
  1218. default: "https://example.com",
  1219. },
  1220. count: {
  1221. type: "integer",
  1222. minimum: 1,
  1223. maximum: 10,
  1224. multipleOf: 1,
  1225. },
  1226. createdAt: {
  1227. format: "date-time",
  1228. },
  1229. mode: {
  1230. const: "fast",
  1231. },
  1232. tags: {
  1233. type: "array",
  1234. minItems: 1,
  1235. maxItems: 3,
  1236. uniqueItems: true,
  1237. },
  1238. tuple: {
  1239. type: "array",
  1240. items: [
  1241. { type: "number", minimum: 0 },
  1242. { type: "string", pattern: "^ok$" },
  1243. ],
  1244. },
  1245. metadata: {
  1246. type: "object",
  1247. patternProperties: {
  1248. "^x-": { type: "string" },
  1249. },
  1250. additionalProperties: {
  1251. type: "string",
  1252. pattern: "^safe$",
  1253. },
  1254. },
  1255. },
  1256. patternProperties: {
  1257. "^extra": { type: "string" },
  1258. },
  1259. required: ["query"],
  1260. additionalProperties: false,
  1261. } as any) as any
  1262. expect(result).toEqual({
  1263. type: "object",
  1264. properties: {
  1265. query: {
  1266. type: "string",
  1267. description: "Search query",
  1268. },
  1269. count: {
  1270. type: "integer",
  1271. },
  1272. createdAt: {
  1273. type: "string",
  1274. },
  1275. mode: {
  1276. enum: ["fast"],
  1277. type: "string",
  1278. },
  1279. tags: {
  1280. type: "array",
  1281. items: { type: "string" },
  1282. },
  1283. tuple: {
  1284. type: "array",
  1285. items: [{ type: "number" }, { type: "string" }],
  1286. },
  1287. metadata: {
  1288. type: "object",
  1289. properties: {},
  1290. additionalProperties: {
  1291. type: "string",
  1292. },
  1293. },
  1294. },
  1295. required: ["query"],
  1296. additionalProperties: false,
  1297. })
  1298. })
  1299. test("keeps local references and sanitizes definitions", () => {
  1300. const result = ProviderTransform.schema(openaiModel, {
  1301. type: "object",
  1302. properties: {
  1303. value: {
  1304. $ref: "#/$defs/Value",
  1305. description: "Referenced value",
  1306. examples: ["ignored"],
  1307. },
  1308. },
  1309. $defs: {
  1310. Value: {
  1311. type: "string",
  1312. pattern: "^value$",
  1313. description: "Definition description",
  1314. },
  1315. Unused: {
  1316. type: "number",
  1317. minimum: 0,
  1318. },
  1319. },
  1320. } as any) as any
  1321. expect(result.properties.value).toEqual({
  1322. $ref: "#/$defs/Value",
  1323. description: "Referenced value",
  1324. })
  1325. expect(result.$defs).toEqual({
  1326. Value: {
  1327. type: "string",
  1328. description: "Definition description",
  1329. },
  1330. Unused: {
  1331. type: "number",
  1332. },
  1333. })
  1334. })
  1335. test("does not sanitize non-openai providers", () => {
  1336. const result = ProviderTransform.schema(
  1337. {
  1338. providerID: "anthropic",
  1339. api: {
  1340. id: "claude-sonnet-4",
  1341. npm: "@ai-sdk/anthropic",
  1342. },
  1343. } as any,
  1344. {
  1345. type: "object",
  1346. properties: {
  1347. query: {
  1348. type: "string",
  1349. pattern: "^https://",
  1350. },
  1351. },
  1352. } as any,
  1353. ) as any
  1354. expect(result.properties.query.pattern).toBe("^https://")
  1355. })
  1356. test.each([
  1357. ["opencode", "@ai-sdk/openai"],
  1358. ["custom-openai-compatible", "@ai-sdk/openai"],
  1359. ["azure", "@ai-sdk/azure"],
  1360. ])("sanitizes %s models using %s", (providerID, npm) => {
  1361. expect(
  1362. ProviderTransform.schema(
  1363. {
  1364. providerID,
  1365. api: {
  1366. id: "custom-model",
  1367. npm,
  1368. },
  1369. } as any,
  1370. {
  1371. type: "object",
  1372. properties: {
  1373. query: {
  1374. type: "string",
  1375. pattern: "^https://",
  1376. },
  1377. },
  1378. } as any,
  1379. ),
  1380. ).toEqual({
  1381. type: "object",
  1382. properties: {
  1383. query: {
  1384. type: "string",
  1385. },
  1386. },
  1387. })
  1388. })
  1389. })
  1390. describe("ProviderTransform.schema - moonshot $ref siblings", () => {
  1391. const moonshotModel = {
  1392. providerID: "moonshotai",
  1393. api: {
  1394. id: "kimi-k2",
  1395. },
  1396. } as any
  1397. test("removes sibling descriptions from referenced tool parameter schemas", () => {
  1398. const schema = {
  1399. type: "object",
  1400. properties: {
  1401. deviceType: {
  1402. description: "Optional. The type of device that captured the screenshot, e.g. mobile or desktop.",
  1403. enum: ["DEVICE_TYPE_UNSPECIFIED", "MOBILE", "DESKTOP", "TABLET", "AGNOSTIC"],
  1404. type: "string",
  1405. },
  1406. modelId: {
  1407. description: "Optional. The model to use for generation.",
  1408. enum: ["MODEL_ID_UNSPECIFIED", "GEMINI_3_PRO", "GEMINI_3_FLASH", "GEMINI_3_1_PRO"],
  1409. type: "string",
  1410. },
  1411. projectId: {
  1412. description: "Required. The project ID of screens to generate variants for.",
  1413. type: "string",
  1414. },
  1415. prompt: {
  1416. description: "Required. The input text used to generate the variants.",
  1417. type: "string",
  1418. },
  1419. selectedScreenIds: {
  1420. description: "Required. The screen ids of screen to generate variants for.",
  1421. items: {
  1422. type: "string",
  1423. },
  1424. type: "array",
  1425. },
  1426. variantOptions: {
  1427. $ref: "#/$defs/VariantOptions",
  1428. description:
  1429. "Required. The variant options for generation, including the number of variants, creative range, and aspects to focus on.",
  1430. },
  1431. },
  1432. required: ["projectId", "selectedScreenIds", "prompt", "variantOptions"],
  1433. $defs: {
  1434. VariantOptions: {
  1435. description:
  1436. "Configuration options for design variant generation. This message captures all parameters used to generate variants, allowing the configuration to be stored, replayed, or analyzed.",
  1437. properties: {
  1438. aspects: {
  1439. description: "Optional. Specific aspects to focus on. If empty, all aspects may be varied.",
  1440. items: {
  1441. enum: ["VARIANT_ASPECT_UNSPECIFIED", "LAYOUT", "COLOR_SCHEME", "IMAGES", "TEXT_FONT", "TEXT_CONTENT"],
  1442. type: "string",
  1443. },
  1444. type: "array",
  1445. },
  1446. creativeRange: {
  1447. description: "Optional. Creative range for variations. Default: EXPLORE",
  1448. enum: ["CREATIVE_RANGE_UNSPECIFIED", "REFINE", "EXPLORE", "REIMAGINE"],
  1449. type: "string",
  1450. },
  1451. variantCount: {
  1452. description: "Optional. Number of variants to generate (1-5). Default: 3",
  1453. format: "int32",
  1454. type: "integer",
  1455. },
  1456. },
  1457. type: "object",
  1458. },
  1459. },
  1460. description: "Request message for GenerateVariants.",
  1461. additionalProperties: false,
  1462. } as any
  1463. const result = ProviderTransform.schema(moonshotModel, schema) as any
  1464. expect(result.properties.variantOptions).toEqual({
  1465. $ref: "#/$defs/VariantOptions",
  1466. })
  1467. expect(result.$defs.VariantOptions.description).toBe(schema.$defs.VariantOptions.description)
  1468. })
  1469. test("also runs for kimi models outside the moonshot provider", () => {
  1470. const result = ProviderTransform.schema(
  1471. {
  1472. providerID: "openrouter",
  1473. name: "Kimi K2",
  1474. api: {
  1475. id: "moonshotai/kimi-k2",
  1476. },
  1477. } as any,
  1478. {
  1479. type: "object",
  1480. properties: {
  1481. value: {
  1482. $ref: "#/$defs/Value",
  1483. description: "Moonshot rejects this sibling after ref expansion.",
  1484. },
  1485. },
  1486. $defs: {
  1487. Value: {
  1488. description: "Referenced schema description stays here.",
  1489. type: "object",
  1490. },
  1491. },
  1492. } as any,
  1493. ) as any
  1494. expect(result.properties.value).toEqual({
  1495. $ref: "#/$defs/Value",
  1496. })
  1497. })
  1498. test("converts tuple-style array items to a single item schema", () => {
  1499. const result = ProviderTransform.schema(moonshotModel, {
  1500. type: "object",
  1501. properties: {
  1502. codeSpec: {
  1503. type: "object",
  1504. properties: {
  1505. accessibility: {
  1506. type: "object",
  1507. properties: {
  1508. renderedSize: {
  1509. description: "Rendered size [width, height] in px",
  1510. type: "array",
  1511. items: [{ type: "number" }, { type: "number" }],
  1512. minItems: 2,
  1513. maxItems: 2,
  1514. },
  1515. },
  1516. },
  1517. },
  1518. },
  1519. },
  1520. } as any) as any
  1521. expect(result.properties.codeSpec.properties.accessibility.properties.renderedSize.items).toEqual({
  1522. type: "number",
  1523. })
  1524. })
  1525. })
  1526. describe("ProviderTransform.message - DeepSeek reasoning content", () => {
  1527. test("DeepSeek with tool calls includes reasoning_content in providerOptions", () => {
  1528. const msgs = [
  1529. {
  1530. role: "assistant",
  1531. content: [
  1532. { type: "reasoning", text: "Let me think about this..." },
  1533. {
  1534. type: "tool-call",
  1535. toolCallId: "test",
  1536. toolName: "bash",
  1537. input: { command: "echo hello" },
  1538. },
  1539. ],
  1540. },
  1541. ] as any[]
  1542. const result = ProviderTransform.message(
  1543. msgs,
  1544. {
  1545. id: ModelV2.ID.make("deepseek/deepseek-chat"),
  1546. providerID: ProviderV2.ID.make("deepseek"),
  1547. api: {
  1548. id: "deepseek-chat",
  1549. url: "https://api.deepseek.com",
  1550. npm: "@ai-sdk/openai-compatible",
  1551. },
  1552. name: "DeepSeek Chat",
  1553. capabilities: {
  1554. temperature: true,
  1555. reasoning: true,
  1556. attachment: false,
  1557. toolcall: true,
  1558. input: { text: true, audio: false, image: false, video: false, pdf: false },
  1559. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1560. interleaved: {
  1561. field: "reasoning_content",
  1562. },
  1563. },
  1564. cost: {
  1565. input: 0.001,
  1566. output: 0.002,
  1567. cache: { read: 0.0001, write: 0.0002 },
  1568. },
  1569. limit: {
  1570. context: 128000,
  1571. output: 8192,
  1572. },
  1573. status: "active",
  1574. options: {},
  1575. headers: {},
  1576. release_date: "2023-04-01",
  1577. },
  1578. {},
  1579. )
  1580. expect(result).toHaveLength(1)
  1581. expect(result[0].content).toEqual([
  1582. {
  1583. type: "tool-call",
  1584. toolCallId: "test",
  1585. toolName: "bash",
  1586. input: { command: "echo hello" },
  1587. },
  1588. ])
  1589. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBe("Let me think about this...")
  1590. })
  1591. test("Non-DeepSeek providers leave reasoning content unchanged", () => {
  1592. const msgs = [
  1593. {
  1594. role: "assistant",
  1595. content: [
  1596. { type: "reasoning", text: "Should not be processed" },
  1597. { type: "text", text: "Answer" },
  1598. ],
  1599. },
  1600. ] as any[]
  1601. const result = ProviderTransform.message(
  1602. msgs,
  1603. {
  1604. id: ModelV2.ID.make("openai/gpt-4"),
  1605. providerID: ProviderV2.ID.make("openai"),
  1606. api: {
  1607. id: "gpt-4",
  1608. url: "https://api.openai.com",
  1609. npm: "@ai-sdk/openai",
  1610. },
  1611. name: "GPT-4",
  1612. capabilities: {
  1613. temperature: true,
  1614. reasoning: false,
  1615. attachment: true,
  1616. toolcall: true,
  1617. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1618. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1619. interleaved: false,
  1620. },
  1621. cost: {
  1622. input: 0.03,
  1623. output: 0.06,
  1624. cache: { read: 0.001, write: 0.002 },
  1625. },
  1626. limit: {
  1627. context: 128000,
  1628. output: 4096,
  1629. },
  1630. status: "active",
  1631. options: {},
  1632. headers: {},
  1633. release_date: "2023-04-01",
  1634. },
  1635. {},
  1636. )
  1637. expect(result[0].content).toEqual([
  1638. { type: "reasoning", text: "Should not be processed" },
  1639. { type: "text", text: "Answer" },
  1640. ])
  1641. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBeUndefined()
  1642. })
  1643. })
  1644. describe("ProviderTransform.message - surrogate sanitization", () => {
  1645. const model = {
  1646. id: "test/test-model",
  1647. providerID: "test",
  1648. api: {
  1649. id: "test-model",
  1650. url: "https://api.test.com",
  1651. npm: "@ai-sdk/openai-compatible",
  1652. },
  1653. name: "Test Model",
  1654. capabilities: {
  1655. temperature: true,
  1656. reasoning: true,
  1657. attachment: true,
  1658. toolcall: true,
  1659. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1660. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1661. interleaved: false,
  1662. },
  1663. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  1664. limit: { context: 128000, output: 8192 },
  1665. status: "active",
  1666. options: {},
  1667. headers: {},
  1668. } as any
  1669. test("replaces lone surrogates in model-visible text", () => {
  1670. const lone = "\uD83D"
  1671. const valid = "🚀"
  1672. const sanitized = "�"
  1673. const text = (label: string) => `${label} ${lone} and ${valid}`
  1674. const expected = (label: string) => `${label} ${sanitized} and ${valid}`
  1675. const msgs = [
  1676. { role: "system", content: text("system") },
  1677. { role: "user", content: text("user string") },
  1678. {
  1679. role: "user",
  1680. content: [
  1681. { type: "text", text: text("user text") },
  1682. { type: "image", image: "data:image/png;base64,abcd" },
  1683. ],
  1684. },
  1685. { role: "assistant", content: text("assistant string") },
  1686. {
  1687. role: "assistant",
  1688. content: [
  1689. { type: "text", text: text("assistant text") },
  1690. { type: "reasoning", text: text("assistant reasoning") },
  1691. { type: "tool-call", toolCallId: "call-1", toolName: "Read", input: { filePath: ".opencode/tool/emoji.ts" } },
  1692. {
  1693. type: "tool-result",
  1694. toolCallId: "call-2",
  1695. toolName: "Read",
  1696. output: { type: "text", value: text("assistant tool text") },
  1697. },
  1698. {
  1699. type: "tool-result",
  1700. toolCallId: "call-3",
  1701. toolName: "Read",
  1702. output: { type: "error-text", value: text("assistant tool error") },
  1703. },
  1704. {
  1705. type: "tool-result",
  1706. toolCallId: "call-4",
  1707. toolName: "Read",
  1708. output: { type: "content", value: [{ type: "text", text: text("assistant tool content") }] },
  1709. },
  1710. ],
  1711. },
  1712. {
  1713. role: "tool",
  1714. content: [
  1715. {
  1716. type: "tool-result",
  1717. toolCallId: "call-5",
  1718. toolName: "Read",
  1719. output: { type: "text", value: text("tool text") },
  1720. },
  1721. {
  1722. type: "tool-result",
  1723. toolCallId: "call-6",
  1724. toolName: "Read",
  1725. output: { type: "error-text", value: text("tool error") },
  1726. },
  1727. {
  1728. type: "tool-result",
  1729. toolCallId: "call-7",
  1730. toolName: "Read",
  1731. output: { type: "content", value: [{ type: "text", text: text("tool content") }] },
  1732. },
  1733. ],
  1734. },
  1735. ] as any[]
  1736. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1737. expect(result[0].content).toBe(expected("system"))
  1738. expect(result[1].content).toBe(expected("user string"))
  1739. expect(result[2].content[0].text).toBe(expected("user text"))
  1740. expect(result[3].content).toBe(expected("assistant string"))
  1741. expect(result[4].content[0].text).toBe(expected("assistant text"))
  1742. expect(result[4].content[1].text).toBe(expected("assistant reasoning"))
  1743. expect(result[4].content[3].output.value).toBe(expected("assistant tool text"))
  1744. expect(result[4].content[4].output.value).toBe(expected("assistant tool error"))
  1745. expect(result[4].content[5].output.value[0].text).toBe(expected("assistant tool content"))
  1746. expect(result[5].content[0].output.value).toBe(expected("tool text"))
  1747. expect(result[5].content[1].output.value).toBe(expected("tool error"))
  1748. expect(result[5].content[2].output.value[0].text).toBe(expected("tool content"))
  1749. expect(result[2].content[1]).toEqual({ type: "image", image: "data:image/png;base64,abcd" })
  1750. })
  1751. })
  1752. describe("ProviderTransform.message - empty image handling", () => {
  1753. const mockModel = {
  1754. id: "anthropic/claude-3-5-sonnet",
  1755. providerID: "anthropic",
  1756. api: {
  1757. id: "claude-3-5-sonnet-20241022",
  1758. url: "https://api.anthropic.com",
  1759. npm: "@ai-sdk/anthropic",
  1760. },
  1761. name: "Claude 3.5 Sonnet",
  1762. capabilities: {
  1763. temperature: true,
  1764. reasoning: false,
  1765. attachment: true,
  1766. toolcall: true,
  1767. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1768. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1769. interleaved: false,
  1770. },
  1771. cost: {
  1772. input: 0.003,
  1773. output: 0.015,
  1774. cache: { read: 0.0003, write: 0.00375 },
  1775. },
  1776. limit: {
  1777. context: 200000,
  1778. output: 8192,
  1779. },
  1780. status: "active",
  1781. options: {},
  1782. headers: {},
  1783. } as any
  1784. test("should replace empty base64 image with error text", () => {
  1785. const msgs = [
  1786. {
  1787. role: "user",
  1788. content: [
  1789. { type: "text", text: "What is in this image?" },
  1790. { type: "image", image: "data:image/png;base64," },
  1791. ],
  1792. },
  1793. ] as any[]
  1794. const result = ProviderTransform.message(msgs, mockModel, {})
  1795. expect(result).toHaveLength(1)
  1796. expect(result[0].content).toHaveLength(2)
  1797. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  1798. expect(result[0].content[1]).toEqual({
  1799. type: "text",
  1800. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  1801. })
  1802. })
  1803. test("should keep valid base64 images unchanged", () => {
  1804. const validBase64 =
  1805. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  1806. const msgs = [
  1807. {
  1808. role: "user",
  1809. content: [
  1810. { type: "text", text: "What is in this image?" },
  1811. { type: "image", image: `data:image/png;base64,${validBase64}` },
  1812. ],
  1813. },
  1814. ] as any[]
  1815. const result = ProviderTransform.message(msgs, mockModel, {})
  1816. expect(result).toHaveLength(1)
  1817. expect(result[0].content).toHaveLength(2)
  1818. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  1819. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  1820. })
  1821. test("should handle mixed valid and empty images", () => {
  1822. const validBase64 =
  1823. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  1824. const msgs = [
  1825. {
  1826. role: "user",
  1827. content: [
  1828. { type: "text", text: "Compare these images" },
  1829. { type: "image", image: `data:image/png;base64,${validBase64}` },
  1830. { type: "image", image: "data:image/jpeg;base64," },
  1831. ],
  1832. },
  1833. ] as any[]
  1834. const result = ProviderTransform.message(msgs, mockModel, {})
  1835. expect(result).toHaveLength(1)
  1836. expect(result[0].content).toHaveLength(3)
  1837. expect(result[0].content[0]).toEqual({ type: "text", text: "Compare these images" })
  1838. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  1839. expect(result[0].content[2]).toEqual({
  1840. type: "text",
  1841. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  1842. })
  1843. })
  1844. })
  1845. describe("ProviderTransform.message - anthropic empty content filtering", () => {
  1846. const anthropicModel = {
  1847. id: "anthropic/claude-3-5-sonnet",
  1848. providerID: "anthropic",
  1849. api: {
  1850. id: "claude-3-5-sonnet-20241022",
  1851. url: "https://api.anthropic.com",
  1852. npm: "@ai-sdk/anthropic",
  1853. },
  1854. name: "Claude 3.5 Sonnet",
  1855. capabilities: {
  1856. temperature: true,
  1857. reasoning: false,
  1858. attachment: true,
  1859. toolcall: true,
  1860. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1861. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1862. interleaved: false,
  1863. },
  1864. cost: {
  1865. input: 0.003,
  1866. output: 0.015,
  1867. cache: { read: 0.0003, write: 0.00375 },
  1868. },
  1869. limit: {
  1870. context: 200000,
  1871. output: 8192,
  1872. },
  1873. status: "active",
  1874. options: {},
  1875. headers: {},
  1876. } as any
  1877. test("filters out messages with empty string content", () => {
  1878. const msgs = [
  1879. { role: "user", content: "Hello" },
  1880. { role: "assistant", content: "" },
  1881. { role: "user", content: "World" },
  1882. ] as any[]
  1883. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1884. expect(result).toHaveLength(2)
  1885. expect(result[0].content).toBe("Hello")
  1886. expect(result[1].content).toBe("World")
  1887. })
  1888. test("filters out empty text parts from array content", () => {
  1889. const msgs = [
  1890. {
  1891. role: "assistant",
  1892. content: [
  1893. { type: "text", text: "" },
  1894. { type: "text", text: "Hello" },
  1895. { type: "text", text: "" },
  1896. ],
  1897. },
  1898. ] as any[]
  1899. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1900. expect(result).toHaveLength(1)
  1901. expect(result[0].content).toHaveLength(1)
  1902. expect(result[0].content[0]).toEqual({ type: "text", text: "Hello" })
  1903. })
  1904. test("filters out empty reasoning parts from array content", () => {
  1905. const msgs = [
  1906. {
  1907. role: "assistant",
  1908. content: [
  1909. { type: "reasoning", text: "" },
  1910. { type: "text", text: "Answer" },
  1911. { type: "reasoning", text: "" },
  1912. ],
  1913. },
  1914. ] as any[]
  1915. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1916. expect(result).toHaveLength(1)
  1917. expect(result[0].content).toHaveLength(1)
  1918. expect(result[0].content[0]).toEqual({ type: "text", text: "Answer" })
  1919. })
  1920. test("removes entire message when all parts are empty", () => {
  1921. const msgs = [
  1922. { role: "user", content: "Hello" },
  1923. {
  1924. role: "assistant",
  1925. content: [
  1926. { type: "text", text: "" },
  1927. { type: "reasoning", text: "" },
  1928. ],
  1929. },
  1930. { role: "user", content: "World" },
  1931. ] as any[]
  1932. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1933. expect(result).toHaveLength(2)
  1934. expect(result[0].content).toBe("Hello")
  1935. expect(result[1].content).toBe("World")
  1936. })
  1937. test("keeps non-text/reasoning parts even if text parts are empty", () => {
  1938. const msgs = [
  1939. {
  1940. role: "assistant",
  1941. content: [
  1942. { type: "text", text: "" },
  1943. { type: "tool-call", toolCallId: "123", toolName: "bash", input: { command: "ls" } },
  1944. ],
  1945. },
  1946. ] as any[]
  1947. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1948. expect(result).toHaveLength(1)
  1949. expect(result[0].content).toHaveLength(1)
  1950. expect(result[0].content[0]).toEqual({
  1951. type: "tool-call",
  1952. toolCallId: "123",
  1953. toolName: "bash",
  1954. input: { command: "ls" },
  1955. })
  1956. })
  1957. test("keeps messages with valid text alongside empty parts", () => {
  1958. const msgs = [
  1959. {
  1960. role: "assistant",
  1961. content: [
  1962. { type: "reasoning", text: "Thinking..." },
  1963. { type: "text", text: "" },
  1964. { type: "text", text: "Result" },
  1965. ],
  1966. },
  1967. ] as any[]
  1968. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1969. expect(result).toHaveLength(1)
  1970. expect(result[0].content).toHaveLength(2)
  1971. expect(result[0].content[0]).toEqual({ type: "reasoning", text: "Thinking..." })
  1972. expect(result[0].content[1]).toEqual({ type: "text", text: "Result" })
  1973. })
  1974. test("filters empty content for bedrock provider", () => {
  1975. const bedrockModel = {
  1976. ...anthropicModel,
  1977. id: "amazon-bedrock/anthropic.claude-opus-4-6",
  1978. providerID: "amazon-bedrock",
  1979. api: {
  1980. id: "anthropic.claude-opus-4-6",
  1981. url: "https://bedrock-runtime.us-east-1.amazonaws.com",
  1982. npm: "@ai-sdk/amazon-bedrock",
  1983. },
  1984. }
  1985. const msgs = [
  1986. { role: "user", content: "Hello" },
  1987. { role: "assistant", content: "" },
  1988. {
  1989. role: "assistant",
  1990. content: [
  1991. { type: "text", text: "" },
  1992. { type: "text", text: "Answer" },
  1993. ],
  1994. },
  1995. ] as any[]
  1996. const result = ProviderTransform.message(msgs, bedrockModel, {})
  1997. expect(result).toHaveLength(2)
  1998. expect(result[0].content).toBe("Hello")
  1999. expect(result[1].content).toHaveLength(1)
  2000. expect(result[1].content[0]).toEqual({ type: "text", text: "Answer" })
  2001. })
  2002. test("does not filter for non-anthropic providers", () => {
  2003. const openaiModel = {
  2004. ...anthropicModel,
  2005. providerID: "openai",
  2006. api: {
  2007. id: "gpt-4",
  2008. url: "https://api.openai.com",
  2009. npm: "@ai-sdk/openai",
  2010. },
  2011. }
  2012. const msgs = [
  2013. { role: "assistant", content: "" },
  2014. {
  2015. role: "assistant",
  2016. content: [{ type: "text", text: "" }],
  2017. },
  2018. ] as any[]
  2019. const result = ProviderTransform.message(msgs, openaiModel, {})
  2020. expect(result).toHaveLength(2)
  2021. expect(result[0].content).toBe("")
  2022. expect(result[1].content).toHaveLength(1)
  2023. })
  2024. test("leaves valid anthropic assistant tool ordering unchanged", () => {
  2025. const msgs = [
  2026. {
  2027. role: "assistant",
  2028. content: [
  2029. { type: "text", text: "I checked your home directory and looked for PDF files." },
  2030. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  2031. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  2032. ],
  2033. },
  2034. ] as any[]
  2035. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  2036. expect(result).toHaveLength(1)
  2037. expect(result[0].content).toMatchObject([
  2038. { type: "text", text: "I checked your home directory and looked for PDF files." },
  2039. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  2040. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  2041. ])
  2042. })
  2043. })
  2044. describe("ProviderTransform.message - strip openai metadata when store=false", () => {
  2045. const openaiModel = {
  2046. id: "openai/gpt-5",
  2047. providerID: "openai",
  2048. api: {
  2049. id: "gpt-5",
  2050. url: "https://api.openai.com",
  2051. npm: "@ai-sdk/openai",
  2052. },
  2053. name: "GPT-5",
  2054. capabilities: {
  2055. temperature: true,
  2056. reasoning: true,
  2057. attachment: true,
  2058. toolcall: true,
  2059. input: { text: true, audio: false, image: true, video: false, pdf: false },
  2060. output: { text: true, audio: false, image: false, video: false, pdf: false },
  2061. interleaved: false,
  2062. },
  2063. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  2064. limit: { context: 128000, output: 4096 },
  2065. status: "active",
  2066. options: {},
  2067. headers: {},
  2068. } as any
  2069. test("strips OpenAI itemId and preserves reasoningEncryptedContent when store=false", () => {
  2070. const msgs = [
  2071. {
  2072. role: "assistant",
  2073. content: [
  2074. {
  2075. type: "reasoning",
  2076. text: "thinking...",
  2077. providerOptions: {
  2078. openai: {
  2079. itemId: "rs_123",
  2080. reasoningEncryptedContent: "encrypted",
  2081. },
  2082. },
  2083. },
  2084. {
  2085. type: "text",
  2086. text: "Hello",
  2087. providerOptions: {
  2088. openai: {
  2089. itemId: "msg_456",
  2090. },
  2091. },
  2092. },
  2093. ],
  2094. },
  2095. ] as any[]
  2096. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  2097. expect(result).toHaveLength(1)
  2098. expect(result[0].content[0].providerOptions?.openai?.itemId).toBeUndefined()
  2099. expect(result[0].content[0].providerOptions?.openai?.reasoningEncryptedContent).toBe("encrypted")
  2100. expect(result[0].content[1].providerOptions?.openai?.itemId).toBeUndefined()
  2101. })
  2102. test("uses the SDK package namespace rather than provider ID", () => {
  2103. const zenModel = {
  2104. ...openaiModel,
  2105. providerID: "zen",
  2106. }
  2107. const msgs = [
  2108. {
  2109. role: "assistant",
  2110. content: [
  2111. {
  2112. type: "reasoning",
  2113. text: "thinking...",
  2114. providerOptions: {
  2115. openai: {
  2116. itemId: "rs_123",
  2117. reasoningEncryptedContent: "encrypted",
  2118. },
  2119. },
  2120. },
  2121. {
  2122. type: "text",
  2123. text: "Hello",
  2124. providerOptions: {
  2125. openai: {
  2126. itemId: "msg_456",
  2127. },
  2128. },
  2129. },
  2130. ],
  2131. },
  2132. ] as any[]
  2133. const result = ProviderTransform.message(msgs, zenModel, { store: false }) as any[]
  2134. expect(result).toHaveLength(1)
  2135. expect(result[0].content[0].providerOptions?.openai?.itemId).toBeUndefined()
  2136. expect(result[0].content[0].providerOptions?.openai?.reasoningEncryptedContent).toBe("encrypted")
  2137. expect(result[0].content[1].providerOptions?.openai?.itemId).toBeUndefined()
  2138. })
  2139. test("preserves other OpenAI options", () => {
  2140. const msgs = [
  2141. {
  2142. role: "assistant",
  2143. content: [
  2144. {
  2145. type: "text",
  2146. text: "Hello",
  2147. providerOptions: {
  2148. openai: {
  2149. itemId: "msg_123",
  2150. otherOption: "value",
  2151. },
  2152. },
  2153. },
  2154. ],
  2155. },
  2156. ] as any[]
  2157. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  2158. expect(result[0].content[0].providerOptions?.openai?.itemId).toBeUndefined()
  2159. expect(result[0].content[0].providerOptions?.openai?.otherOption).toBe("value")
  2160. })
  2161. test("strips Azure itemId from the Azure namespace", () => {
  2162. const azureModel = {
  2163. ...openaiModel,
  2164. providerID: "azure",
  2165. api: {
  2166. id: "gpt-5",
  2167. url: "https://example.openai.azure.com",
  2168. npm: "@ai-sdk/azure",
  2169. },
  2170. }
  2171. const msgs = [
  2172. {
  2173. role: "assistant",
  2174. content: [
  2175. {
  2176. type: "text",
  2177. text: "Hello",
  2178. providerOptions: {
  2179. azure: { itemId: "msg_123", otherOption: "value" },
  2180. openai: { itemId: "msg_openai" },
  2181. },
  2182. },
  2183. ],
  2184. },
  2185. ] as any[]
  2186. const result = ProviderTransform.message(msgs, azureModel, { store: false }) as any[]
  2187. expect(result[0].content[0].providerOptions?.azure?.itemId).toBeUndefined()
  2188. expect(result[0].content[0].providerOptions?.azure?.otherOption).toBe("value")
  2189. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_openai")
  2190. })
  2191. test("strips Bedrock Mantle itemId from the OpenAI namespace", () => {
  2192. const mantleModel = {
  2193. ...openaiModel,
  2194. providerID: "amazon-bedrock",
  2195. api: {
  2196. id: "openai.gpt-5.5",
  2197. url: "https://bedrock-mantle.us-east-2.api.aws/openai/v1",
  2198. npm: "@ai-sdk/amazon-bedrock/mantle",
  2199. },
  2200. }
  2201. const msgs = [
  2202. {
  2203. role: "assistant",
  2204. providerOptions: { openai: { itemId: "msg_root", otherOption: "root-value" } },
  2205. content: [
  2206. {
  2207. type: "reasoning",
  2208. text: "thinking...",
  2209. providerOptions: {
  2210. openai: { itemId: "rs_123", reasoningEncryptedContent: "encrypted" },
  2211. },
  2212. },
  2213. ],
  2214. },
  2215. ] as any[]
  2216. const result = ProviderTransform.message(msgs, mantleModel, { store: false }) as any[]
  2217. expect(result[0].providerOptions?.openai?.itemId).toBeUndefined()
  2218. expect(result[0].providerOptions?.openai?.otherOption).toBe("root-value")
  2219. expect(result[0].content[0].providerOptions?.openai?.itemId).toBeUndefined()
  2220. expect(result[0].content[0].providerOptions?.openai?.reasoningEncryptedContent).toBe("encrypted")
  2221. })
  2222. test("strips GitHub Copilot itemId from the copilot namespace, preserving other copilot options", () => {
  2223. const copilotModel = {
  2224. ...openaiModel,
  2225. id: "github-copilot/gpt-5.5",
  2226. providerID: "github-copilot",
  2227. api: {
  2228. id: "gpt-5.5",
  2229. url: "https://api.githubcopilot.com",
  2230. npm: "@ai-sdk/github-copilot",
  2231. },
  2232. }
  2233. const msgs = [
  2234. {
  2235. role: "assistant",
  2236. content: [
  2237. {
  2238. type: "reasoning",
  2239. text: "thinking...",
  2240. providerOptions: {
  2241. copilot: { itemId: "rs_123", reasoningEncryptedContent: "encrypted" },
  2242. },
  2243. },
  2244. {
  2245. // The stale itemId on tool-call parts is what Copilot echoes back as the
  2246. // `function_call` item `id`, which is what the upstream connection rejects.
  2247. type: "tool-call",
  2248. toolCallId: "call_1",
  2249. toolName: "bash",
  2250. input: { command: "ls" },
  2251. providerOptions: {
  2252. copilot: { itemId: "fc_456", reasoningEffort: "medium" },
  2253. },
  2254. },
  2255. ],
  2256. },
  2257. ] as any[]
  2258. const result = ProviderTransform.message(msgs, copilotModel, { store: false }) as any[]
  2259. expect(result[0].content[0].providerOptions?.copilot?.itemId).toBeUndefined()
  2260. expect(result[0].content[0].providerOptions?.copilot?.reasoningEncryptedContent).toBe("encrypted")
  2261. expect(result[0].content[1].providerOptions?.copilot?.itemId).toBeUndefined()
  2262. expect(result[0].content[1].providerOptions?.copilot?.reasoningEffort).toBe("medium")
  2263. })
  2264. test("leaves a stray openai namespace on a Copilot model untouched, since Copilot's Responses model only reads the copilot namespace", () => {
  2265. const copilotModel = {
  2266. ...openaiModel,
  2267. id: "github-copilot/gpt-5.5",
  2268. providerID: "github-copilot",
  2269. api: {
  2270. id: "gpt-5.5",
  2271. url: "https://api.githubcopilot.com",
  2272. npm: "@ai-sdk/github-copilot",
  2273. },
  2274. }
  2275. const msgs = [
  2276. {
  2277. role: "assistant",
  2278. content: [
  2279. {
  2280. type: "text",
  2281. text: "Hello",
  2282. providerOptions: {
  2283. openai: { itemId: "msg_456" },
  2284. },
  2285. },
  2286. ],
  2287. },
  2288. ] as any[]
  2289. const result = ProviderTransform.message(msgs, copilotModel, { store: false }) as any[]
  2290. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_456")
  2291. })
  2292. test("preserves metadata for openai package when store is true", () => {
  2293. const msgs = [
  2294. {
  2295. role: "assistant",
  2296. content: [
  2297. {
  2298. type: "text",
  2299. text: "Hello",
  2300. providerOptions: {
  2301. openai: {
  2302. itemId: "msg_123",
  2303. },
  2304. },
  2305. },
  2306. ],
  2307. },
  2308. ] as any[]
  2309. // openai package preserves itemId regardless of store value
  2310. const result = ProviderTransform.message(msgs, openaiModel, { store: true }) as any[]
  2311. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  2312. })
  2313. test("preserves metadata for non-openai packages when store is false", () => {
  2314. const anthropicModel = {
  2315. ...openaiModel,
  2316. providerID: "anthropic",
  2317. api: {
  2318. id: "claude-3",
  2319. url: "https://api.anthropic.com",
  2320. npm: "@ai-sdk/anthropic",
  2321. },
  2322. }
  2323. const msgs = [
  2324. {
  2325. role: "assistant",
  2326. content: [
  2327. {
  2328. type: "text",
  2329. text: "Hello",
  2330. providerOptions: {
  2331. openai: {
  2332. itemId: "msg_123",
  2333. },
  2334. },
  2335. },
  2336. ],
  2337. },
  2338. ] as any[]
  2339. // store=false preserves metadata for non-openai packages
  2340. const result = ProviderTransform.message(msgs, anthropicModel, { store: false }) as any[]
  2341. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  2342. })
  2343. test("preserves metadata using providerID key when store is false", () => {
  2344. const opencodeModel = {
  2345. ...openaiModel,
  2346. providerID: "opencode",
  2347. api: {
  2348. id: "opencode-test",
  2349. url: "https://api.opencode.ai",
  2350. npm: "@ai-sdk/openai-compatible",
  2351. },
  2352. }
  2353. const msgs = [
  2354. {
  2355. role: "assistant",
  2356. content: [
  2357. {
  2358. type: "text",
  2359. text: "Hello",
  2360. providerOptions: {
  2361. opencode: {
  2362. itemId: "msg_123",
  2363. otherOption: "value",
  2364. },
  2365. },
  2366. },
  2367. ],
  2368. },
  2369. ] as any[]
  2370. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  2371. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_123")
  2372. expect(result[0].content[0].providerOptions?.opencode?.otherOption).toBe("value")
  2373. })
  2374. test("preserves itemId across all providerOptions keys", () => {
  2375. const opencodeModel = {
  2376. ...openaiModel,
  2377. providerID: "opencode",
  2378. api: {
  2379. id: "opencode-test",
  2380. url: "https://api.opencode.ai",
  2381. npm: "@ai-sdk/openai-compatible",
  2382. },
  2383. }
  2384. const msgs = [
  2385. {
  2386. role: "assistant",
  2387. providerOptions: {
  2388. openai: { itemId: "msg_root" },
  2389. opencode: { itemId: "msg_opencode" },
  2390. extra: { itemId: "msg_extra" },
  2391. },
  2392. content: [
  2393. {
  2394. type: "text",
  2395. text: "Hello",
  2396. providerOptions: {
  2397. openai: { itemId: "msg_openai_part" },
  2398. opencode: { itemId: "msg_opencode_part" },
  2399. extra: { itemId: "msg_extra_part" },
  2400. },
  2401. },
  2402. ],
  2403. },
  2404. ] as any[]
  2405. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  2406. expect(result[0].providerOptions?.openai?.itemId).toBe("msg_root")
  2407. expect(result[0].providerOptions?.opencode?.itemId).toBe("msg_opencode")
  2408. expect(result[0].providerOptions?.extra?.itemId).toBe("msg_extra")
  2409. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_openai_part")
  2410. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_opencode_part")
  2411. expect(result[0].content[0].providerOptions?.extra?.itemId).toBe("msg_extra_part")
  2412. })
  2413. test("does not strip metadata for non-openai packages when store is not false", () => {
  2414. const anthropicModel = {
  2415. ...openaiModel,
  2416. providerID: "anthropic",
  2417. api: {
  2418. id: "claude-3",
  2419. url: "https://api.anthropic.com",
  2420. npm: "@ai-sdk/anthropic",
  2421. },
  2422. }
  2423. const msgs = [
  2424. {
  2425. role: "assistant",
  2426. content: [
  2427. {
  2428. type: "text",
  2429. text: "Hello",
  2430. providerOptions: {
  2431. openai: {
  2432. itemId: "msg_123",
  2433. },
  2434. },
  2435. },
  2436. ],
  2437. },
  2438. ] as any[]
  2439. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  2440. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  2441. })
  2442. })
  2443. describe("ProviderTransform.message - providerOptions key remapping", () => {
  2444. const createModel = (providerID: string, npm: string) =>
  2445. ({
  2446. id: `${providerID}/test-model`,
  2447. providerID,
  2448. api: {
  2449. id: "test-model",
  2450. url: "https://api.test.com",
  2451. npm,
  2452. },
  2453. name: "Test Model",
  2454. capabilities: {
  2455. temperature: true,
  2456. reasoning: false,
  2457. attachment: true,
  2458. toolcall: true,
  2459. input: { text: true, audio: false, image: true, video: false, pdf: true },
  2460. output: { text: true, audio: false, image: false, video: false, pdf: false },
  2461. interleaved: false,
  2462. },
  2463. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  2464. limit: { context: 128000, output: 8192 },
  2465. status: "active",
  2466. options: {},
  2467. headers: {},
  2468. }) as any
  2469. test("azure keeps 'azure' key and does not remap to 'openai'", () => {
  2470. const model = createModel("azure", "@ai-sdk/azure")
  2471. const msgs = [
  2472. {
  2473. role: "user",
  2474. content: "Hello",
  2475. providerOptions: {
  2476. azure: { someOption: "value" },
  2477. },
  2478. },
  2479. ] as any[]
  2480. const result = ProviderTransform.message(msgs, model, {})
  2481. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  2482. expect(result[0].providerOptions?.openai).toBeUndefined()
  2483. })
  2484. test("azure cognitive services remaps providerID to 'azure' key", () => {
  2485. const model = createModel("azure-cognitive-services", "@ai-sdk/azure")
  2486. const msgs = [
  2487. {
  2488. role: "user",
  2489. content: [
  2490. {
  2491. type: "text",
  2492. text: "Hello",
  2493. providerOptions: {
  2494. "azure-cognitive-services": { part: true },
  2495. },
  2496. },
  2497. ],
  2498. providerOptions: {
  2499. "azure-cognitive-services": { someOption: "value" },
  2500. },
  2501. },
  2502. ] as any[]
  2503. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2504. const part = result[0].content[0] as any
  2505. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  2506. expect(result[0].providerOptions?.["azure-cognitive-services"]).toBeUndefined()
  2507. expect(part.providerOptions?.azure).toEqual({ part: true })
  2508. expect(part.providerOptions?.["azure-cognitive-services"]).toBeUndefined()
  2509. })
  2510. test("copilot remaps providerID to 'copilot' key", () => {
  2511. const model = createModel("github-copilot", "@ai-sdk/github-copilot")
  2512. const msgs = [
  2513. {
  2514. role: "user",
  2515. content: "Hello",
  2516. providerOptions: {
  2517. copilot: { someOption: "value" },
  2518. },
  2519. },
  2520. ] as any[]
  2521. const result = ProviderTransform.message(msgs, model, {})
  2522. expect(result[0].providerOptions?.copilot).toEqual({ someOption: "value" })
  2523. expect(result[0].providerOptions?.["github-copilot"]).toBeUndefined()
  2524. })
  2525. test("bedrock remaps providerID to 'bedrock' key", () => {
  2526. const model = createModel("my-bedrock", "@ai-sdk/amazon-bedrock")
  2527. const msgs = [
  2528. {
  2529. role: "user",
  2530. content: "Hello",
  2531. providerOptions: {
  2532. "my-bedrock": { someOption: "value" },
  2533. },
  2534. },
  2535. ] as any[]
  2536. const result = ProviderTransform.message(msgs, model, {})
  2537. expect(result[0].providerOptions?.bedrock).toEqual({ someOption: "value" })
  2538. expect(result[0].providerOptions?.["my-bedrock"]).toBeUndefined()
  2539. })
  2540. })
  2541. describe("ProviderTransform.message - claude w/bedrock custom inference profile", () => {
  2542. test("adds cachePoint", () => {
  2543. const model = {
  2544. id: "amazon-bedrock/custom-claude-sonnet-4.5",
  2545. providerID: "amazon-bedrock",
  2546. api: {
  2547. id: "arn:aws:bedrock:xxx:yyy:application-inference-profile/zzz",
  2548. url: "https://api.test.com",
  2549. npm: "@ai-sdk/amazon-bedrock",
  2550. },
  2551. name: "Custom inference profile",
  2552. capabilities: {},
  2553. options: {},
  2554. headers: {},
  2555. } as any
  2556. const msgs = [
  2557. {
  2558. role: "user",
  2559. content: "Hello",
  2560. },
  2561. ] as any[]
  2562. const result = ProviderTransform.message(msgs, model, {})
  2563. expect(result[0].providerOptions?.bedrock).toEqual(
  2564. expect.objectContaining({
  2565. cachePoint: {
  2566. type: "default",
  2567. },
  2568. }),
  2569. )
  2570. })
  2571. })
  2572. describe("ProviderTransform.message - bedrock caching with non-bedrock providerID", () => {
  2573. test("applies cache options at message level when npm package is amazon-bedrock", () => {
  2574. const model = {
  2575. id: "aws/us.anthropic.claude-opus-4-6-v1",
  2576. providerID: "aws",
  2577. api: {
  2578. id: "us.anthropic.claude-opus-4-6-v1",
  2579. url: "https://bedrock-runtime.us-east-1.amazonaws.com",
  2580. npm: "@ai-sdk/amazon-bedrock",
  2581. },
  2582. name: "Claude Opus 4.6",
  2583. capabilities: {},
  2584. options: {},
  2585. headers: {},
  2586. } as any
  2587. const msgs = [
  2588. {
  2589. role: "system",
  2590. content: "You are a helpful assistant",
  2591. },
  2592. {
  2593. role: "user",
  2594. content: [{ type: "text", text: "Hello" }],
  2595. },
  2596. ] as any[]
  2597. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2598. // Cache should be at the message level and not the content-part level
  2599. expect(result[0].providerOptions?.bedrock).toEqual({
  2600. cachePoint: { type: "default" },
  2601. })
  2602. expect(result[0].content).toBe("You are a helpful assistant")
  2603. })
  2604. })
  2605. describe("ProviderTransform.message - cache control on gateway", () => {
  2606. const createModel = (overrides: Partial<any> = {}) =>
  2607. ({
  2608. id: "anthropic/claude-sonnet-4",
  2609. providerID: "vercel",
  2610. api: {
  2611. id: "anthropic/claude-sonnet-4",
  2612. url: "https://ai-gateway.vercel.sh/v3/ai",
  2613. npm: "@ai-sdk/gateway",
  2614. },
  2615. name: "Claude Sonnet 4",
  2616. capabilities: {
  2617. temperature: true,
  2618. reasoning: true,
  2619. attachment: true,
  2620. toolcall: true,
  2621. input: { text: true, audio: false, image: true, video: false, pdf: true },
  2622. output: { text: true, audio: false, image: false, video: false, pdf: false },
  2623. interleaved: false,
  2624. },
  2625. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  2626. limit: { context: 200_000, output: 8192 },
  2627. status: "active",
  2628. options: {},
  2629. headers: {},
  2630. ...overrides,
  2631. }) as any
  2632. test("gateway does not set cache control for anthropic models", () => {
  2633. const model = createModel()
  2634. const msgs = [
  2635. {
  2636. role: "system",
  2637. content: "You are a helpful assistant",
  2638. },
  2639. {
  2640. role: "user",
  2641. content: "Hello",
  2642. },
  2643. ] as any[]
  2644. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2645. expect(result[0].content).toBe("You are a helpful assistant")
  2646. expect(result[0].providerOptions).toBeUndefined()
  2647. })
  2648. test("non-gateway anthropic keeps existing cache control behavior", () => {
  2649. const model = createModel({
  2650. providerID: "anthropic",
  2651. api: {
  2652. id: "claude-sonnet-4",
  2653. url: "https://api.anthropic.com",
  2654. npm: "@ai-sdk/anthropic",
  2655. },
  2656. })
  2657. const msgs = [
  2658. {
  2659. role: "system",
  2660. content: "You are a helpful assistant",
  2661. },
  2662. {
  2663. role: "user",
  2664. content: "Hello",
  2665. },
  2666. ] as any[]
  2667. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2668. expect(result[0].providerOptions).toEqual({
  2669. anthropic: {
  2670. cacheControl: {
  2671. type: "ephemeral",
  2672. },
  2673. },
  2674. openrouter: {
  2675. cacheControl: {
  2676. type: "ephemeral",
  2677. },
  2678. },
  2679. bedrock: {
  2680. cachePoint: {
  2681. type: "default",
  2682. },
  2683. },
  2684. openaiCompatible: {
  2685. cache_control: {
  2686. type: "ephemeral",
  2687. },
  2688. },
  2689. copilot: {
  2690. copilot_cache_control: {
  2691. type: "ephemeral",
  2692. },
  2693. },
  2694. alibaba: {
  2695. cacheControl: {
  2696. type: "ephemeral",
  2697. },
  2698. },
  2699. })
  2700. })
  2701. test("google-vertex-anthropic applies cache control", () => {
  2702. const model = createModel({
  2703. providerID: "google-vertex-anthropic",
  2704. api: {
  2705. id: "google-vertex-anthropic",
  2706. url: "https://us-central1-aiplatform.googleapis.com",
  2707. npm: "@ai-sdk/google-vertex/anthropic",
  2708. },
  2709. id: "claude-sonnet-4@20250514",
  2710. })
  2711. const msgs = [
  2712. {
  2713. role: "system",
  2714. content: "You are a helpful assistant",
  2715. },
  2716. {
  2717. role: "user",
  2718. content: "Hello",
  2719. },
  2720. ] as any[]
  2721. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2722. expect(result[0].providerOptions).toEqual({
  2723. anthropic: {
  2724. cacheControl: {
  2725. type: "ephemeral",
  2726. },
  2727. },
  2728. openrouter: {
  2729. cacheControl: {
  2730. type: "ephemeral",
  2731. },
  2732. },
  2733. bedrock: {
  2734. cachePoint: {
  2735. type: "default",
  2736. },
  2737. },
  2738. openaiCompatible: {
  2739. cache_control: {
  2740. type: "ephemeral",
  2741. },
  2742. },
  2743. copilot: {
  2744. copilot_cache_control: {
  2745. type: "ephemeral",
  2746. },
  2747. },
  2748. alibaba: {
  2749. cacheControl: {
  2750. type: "ephemeral",
  2751. },
  2752. },
  2753. })
  2754. })
  2755. })
  2756. describe("ProviderTransform.temperature - Cohere North", () => {
  2757. test("defaults north-mini-code models to 1.0", () => {
  2758. expect(ProviderTransform.temperature({ id: "cohere/North-Mini-Code-1-0-latest" } as any)).toBe(1.0)
  2759. })
  2760. })
  2761. describe("ProviderTransform.reasoningVariants", () => {
  2762. const model = (reasoning_options: ModelsDev.Model["reasoning_options"]) => ({ reasoning_options }) as ModelsDev.Model
  2763. const target = (npm: string, id = "test-model") =>
  2764. ({ id, api: { id, npm, url: "" }, capabilities: { reasoning: true }, limit: { output: 64_000 } }) as any
  2765. test("respects explicitly empty reasoning options", () => {
  2766. expect(ProviderTransform.reasoningVariants(model([]), target("@ai-sdk/openai"))).toEqual({})
  2767. })
  2768. test.each([
  2769. ["@openrouter/ai-sdk-provider", { reasoning: { effort: "high" } }],
  2770. ["@ai-sdk/anthropic", { thinking: { type: "adaptive" }, effort: "high" }, "claude-opus-4-6"],
  2771. [
  2772. "@ai-sdk/google-vertex/anthropic",
  2773. { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  2774. "claude-opus-4-7",
  2775. ],
  2776. ["@ai-sdk/google", { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } }],
  2777. ["@ai-sdk/google-vertex", { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } }],
  2778. [
  2779. "@ai-sdk/azure",
  2780. {
  2781. reasoningEffort: "high",
  2782. reasoningSummary: "auto",
  2783. include: ["reasoning.encrypted_content"],
  2784. },
  2785. ],
  2786. [
  2787. "@ai-sdk/openai",
  2788. {
  2789. reasoningEffort: "high",
  2790. reasoningSummary: "auto",
  2791. include: ["reasoning.encrypted_content"],
  2792. },
  2793. ],
  2794. [
  2795. "@ai-sdk/amazon-bedrock/mantle",
  2796. {
  2797. reasoningEffort: "high",
  2798. reasoningSummary: "auto",
  2799. include: ["reasoning.encrypted_content"],
  2800. },
  2801. ],
  2802. [
  2803. "@ai-sdk/github-copilot",
  2804. {
  2805. reasoningEffort: "high",
  2806. reasoningSummary: "auto",
  2807. include: ["reasoning.encrypted_content"],
  2808. },
  2809. ],
  2810. ["@ai-sdk/openai-compatible", { reasoningEffort: "high" }],
  2811. ["@ai-sdk/xai", { reasoningEffort: "high" }],
  2812. ["@ai-sdk/mistral", { reasoningEffort: "high" }],
  2813. ["@ai-sdk/groq", { reasoningEffort: "high" }],
  2814. ["@ai-sdk/cerebras", { reasoningEffort: "high" }],
  2815. ["@ai-sdk/deepinfra", { reasoningEffort: "high" }],
  2816. ["@ai-sdk/togetherai", { reasoningEffort: "high" }],
  2817. ["venice-ai-sdk-provider", { reasoningEffort: "high" }],
  2818. ["ai-gateway-provider", { reasoningEffort: "high" }],
  2819. ["@ai-sdk/amazon-bedrock", { reasoningConfig: { type: "enabled", maxReasoningEffort: "high" } }],
  2820. ])("converts effort for %s", (npm, expected, ...args) => {
  2821. const id = args[0] as string | undefined
  2822. expect(ProviderTransform.reasoningVariants(model([{ type: "effort", values: ["high"] }]), target(npm, id))).toEqual(
  2823. { high: expected },
  2824. )
  2825. })
  2826. test("uses bare effort for Claude Opus 4.5", () => {
  2827. expect(
  2828. ProviderTransform.reasoningVariants(
  2829. model([{ type: "effort", values: ["high"] }]),
  2830. target("@ai-sdk/anthropic", "claude-opus-4-5"),
  2831. ),
  2832. ).toEqual({ high: { effort: "high" } })
  2833. })
  2834. test("uses explicit effort metadata for Anthropic-compatible models", () => {
  2835. expect(
  2836. ProviderTransform.reasoningVariants(
  2837. model([{ type: "effort", values: ["high"] }]),
  2838. target("@ai-sdk/anthropic", "claude-sonnet-4"),
  2839. ),
  2840. ).toEqual({ high: { effort: "high" } })
  2841. expect(
  2842. ProviderTransform.reasoningVariants(
  2843. model([{ type: "effort", values: ["max"] }]),
  2844. target("@ai-sdk/anthropic", "k3"),
  2845. ),
  2846. ).toEqual({ max: { effort: "max" } })
  2847. })
  2848. test("uses adaptive reasoning config for Anthropic models on Bedrock", () => {
  2849. expect(
  2850. ProviderTransform.reasoningVariants(
  2851. model([{ type: "effort", values: ["high"] }]),
  2852. target("@ai-sdk/amazon-bedrock", "anthropic.claude-opus-4-7-v1:0"),
  2853. ),
  2854. ).toEqual({
  2855. high: {
  2856. reasoningConfig: {
  2857. type: "adaptive",
  2858. maxReasoningEffort: "high",
  2859. display: "summarized",
  2860. },
  2861. },
  2862. })
  2863. })
  2864. test("does not replace unsupported Anthropic Bedrock effort options with token budgets", () => {
  2865. expect(
  2866. ProviderTransform.reasoningVariants(
  2867. model([{ type: "effort", values: ["high"] }]),
  2868. target("@ai-sdk/amazon-bedrock", "anthropic.claude-sonnet-4-v1:0"),
  2869. ),
  2870. ).toEqual({})
  2871. })
  2872. test.each([
  2873. ["@openrouter/ai-sdk-provider", { reasoning: { max_tokens: 16_000 } }],
  2874. ["@ai-sdk/anthropic", { thinking: { type: "enabled", budgetTokens: 16_000 } }],
  2875. ["@ai-sdk/google-vertex/anthropic", { thinking: { type: "enabled", budgetTokens: 16_000 } }],
  2876. ["@ai-sdk/google", { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } }],
  2877. ["@ai-sdk/google-vertex", { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } }],
  2878. ["@ai-sdk/amazon-bedrock", { reasoningConfig: { type: "enabled", budgetTokens: 16_000 } }],
  2879. ["@ai-sdk/cohere", { thinking: { type: "enabled", tokenBudget: 16_000 } }],
  2880. ["@ai-sdk/alibaba", { enableThinking: true, thinkingBudget: 16_000 }],
  2881. ])("converts token budgets for %s", (npm, high) => {
  2882. const variants = ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024 }]), target(npm))
  2883. expect(variants?.high).toEqual(high)
  2884. expect(Object.keys(variants ?? {})).toEqual(["high", "max"])
  2885. })
  2886. test("maps null effort to none", () => {
  2887. expect(
  2888. ProviderTransform.reasoningVariants(model([{ type: "effort", values: [null] }]), target("@ai-sdk/openai")),
  2889. ).toEqual({
  2890. none: {
  2891. reasoningEffort: "none",
  2892. reasoningSummary: "auto",
  2893. include: ["reasoning.encrypted_content"],
  2894. },
  2895. })
  2896. })
  2897. test.each([
  2898. ["@ai-sdk/alibaba", { none: { enableThinking: false }, high: { enableThinking: true } }],
  2899. [
  2900. "@ai-sdk/cohere",
  2901. {
  2902. none: { thinking: { type: "disabled" } },
  2903. high: { thinking: { type: "enabled" } },
  2904. },
  2905. ],
  2906. ])("converts toggle options for %s", (npm, expected) => {
  2907. expect(ProviderTransform.reasoningVariants(model([{ type: "toggle" }]), target(npm))).toEqual(expected)
  2908. })
  2909. test("combines Cohere toggle and budget options", () => {
  2910. expect(
  2911. ProviderTransform.reasoningVariants(
  2912. model([{ type: "toggle" }, { type: "budget_tokens", min: 1 }]),
  2913. target("@ai-sdk/cohere"),
  2914. ),
  2915. ).toEqual({
  2916. none: { thinking: { type: "disabled" } },
  2917. high: { thinking: { type: "enabled", tokenBudget: 16_000 } },
  2918. max: { thinking: { type: "enabled", tokenBudget: 31_999 } },
  2919. })
  2920. })
  2921. test("generates bounded high and max token budgets", () => {
  2922. expect(
  2923. ProviderTransform.reasoningVariants(
  2924. model([{ type: "budget_tokens", min: 1_024, max: 64_000 }]),
  2925. target("@ai-sdk/anthropic"),
  2926. ),
  2927. ).toEqual({
  2928. high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
  2929. max: { thinking: { type: "enabled", budgetTokens: 31_999 } },
  2930. })
  2931. })
  2932. test("caps token budgets below the model output limit", () => {
  2933. const anthropic = target("@ai-sdk/anthropic")
  2934. anthropic.limit.output = 5_000
  2935. expect(
  2936. ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024, max: 64_000 }]), anthropic),
  2937. ).toEqual({
  2938. high: { thinking: { type: "enabled", budgetTokens: 2_500 } },
  2939. max: { thinking: { type: "enabled", budgetTokens: 4_999 } },
  2940. })
  2941. })
  2942. test("derives high and max budgets when models.dev omits max", () => {
  2943. expect(
  2944. ProviderTransform.reasoningVariants(
  2945. model([{ type: "budget_tokens", min: 1_024 }]),
  2946. target("@ai-sdk/anthropic", "claude-haiku-4-5"),
  2947. ),
  2948. ).toEqual({
  2949. high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
  2950. max: { thinking: { type: "enabled", budgetTokens: 31_999 } },
  2951. })
  2952. })
  2953. test("preserves explicit inclusive budget maxima", () => {
  2954. expect(
  2955. ProviderTransform.reasoningVariants(
  2956. model([{ type: "budget_tokens", min: 1_024, max: 24_576 }]),
  2957. target("@ai-sdk/google", "gemini-2.5-pro"),
  2958. ),
  2959. ).toEqual({
  2960. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 12_288 } },
  2961. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
  2962. })
  2963. })
  2964. test("prefers effort options over token budgets", () => {
  2965. expect(
  2966. ProviderTransform.reasoningVariants(
  2967. model([
  2968. { type: "budget_tokens", min: 1_024, max: 64_000 },
  2969. { type: "effort", values: ["low"] },
  2970. ]),
  2971. target("@ai-sdk/openai"),
  2972. ),
  2973. ).toEqual({
  2974. low: {
  2975. reasoningEffort: "low",
  2976. reasoningSummary: "auto",
  2977. include: ["reasoning.encrypted_content"],
  2978. },
  2979. })
  2980. })
  2981. test("does not replace unsupported effort options with heuristic variants", () => {
  2982. expect(
  2983. ProviderTransform.reasoningVariants(model([{ type: "effort", values: ["high"] }]), target("@ai-sdk/perplexity")),
  2984. ).toEqual({})
  2985. })
  2986. test("leaves unsupported toggle options for heuristic fallback", () => {
  2987. expect(ProviderTransform.reasoningVariants(model([{ type: "toggle" }]), target("@ai-sdk/openai"))).toBeUndefined()
  2988. })
  2989. test("uses model-family options for gateway and GitHub Copilot", () => {
  2990. const effort = model([{ type: "effort", values: ["high"] }])
  2991. expect(ProviderTransform.reasoningVariants(effort, target("@ai-sdk/gateway", "anthropic/claude-sonnet-4"))).toEqual(
  2992. {
  2993. high: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  2994. },
  2995. )
  2996. expect(ProviderTransform.reasoningVariants(effort, target("@ai-sdk/gateway", "google/gemini-3-pro"))).toEqual({
  2997. high: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  2998. })
  2999. expect(
  3000. ProviderTransform.reasoningVariants(effort, target("@ai-sdk/github-copilot", "gemini-3-pro")),
  3001. ).toEqual({})
  3002. })
  3003. test.each(["@ai-sdk/cohere", "@ai-sdk/perplexity", "@ai-sdk/vercel", "@ai-sdk/alibaba", "gitlab-ai-provider"])(
  3004. "does not invent effort controls for %s",
  3005. (npm) => {
  3006. expect(
  3007. ProviderTransform.reasoningVariants(model([{ type: "effort", values: ["high"] }]), target(npm)),
  3008. ).toEqual({})
  3009. },
  3010. )
  3011. })
  3012. describe("ProviderTransform.variants", () => {
  3013. const createMockModel = (overrides: Partial<any> = {}): any => ({
  3014. id: "test/test-model",
  3015. providerID: "test",
  3016. api: {
  3017. id: "test-model",
  3018. url: "https://api.test.com",
  3019. npm: "@ai-sdk/openai",
  3020. },
  3021. name: "Test Model",
  3022. capabilities: {
  3023. temperature: true,
  3024. reasoning: true,
  3025. attachment: true,
  3026. toolcall: true,
  3027. input: { text: true, audio: false, image: true, video: false, pdf: false },
  3028. output: { text: true, audio: false, image: false, video: false, pdf: false },
  3029. interleaved: false,
  3030. },
  3031. cost: {
  3032. input: 0.001,
  3033. output: 0.002,
  3034. cache: { read: 0.0001, write: 0.0002 },
  3035. },
  3036. limit: {
  3037. context: 200_000,
  3038. output: 64_000,
  3039. },
  3040. status: "active",
  3041. options: {},
  3042. headers: {},
  3043. release_date: "2024-01-01",
  3044. ...overrides,
  3045. })
  3046. test("returns empty object when model has no reasoning capabilities", () => {
  3047. const model = createMockModel({
  3048. capabilities: { reasoning: false },
  3049. })
  3050. const result = ProviderTransform.variants(model)
  3051. expect(result).toEqual({})
  3052. })
  3053. test("deepseek returns empty object", () => {
  3054. const model = createMockModel({
  3055. id: "deepseek/deepseek-chat",
  3056. providerID: "deepseek",
  3057. api: {
  3058. id: "deepseek-chat",
  3059. url: "https://api.deepseek.com",
  3060. npm: "@ai-sdk/openai-compatible",
  3061. },
  3062. })
  3063. const result = ProviderTransform.variants(model)
  3064. expect(result).toEqual({})
  3065. })
  3066. test("minimax returns empty object", () => {
  3067. const model = createMockModel({
  3068. id: "minimax/minimax-model",
  3069. providerID: "minimax",
  3070. api: {
  3071. id: "minimax-model",
  3072. url: "https://api.minimax.com",
  3073. npm: "@ai-sdk/openai-compatible",
  3074. },
  3075. })
  3076. const result = ProviderTransform.variants(model)
  3077. expect(result).toEqual({})
  3078. })
  3079. test("minimax m3 using anthropic returns thinking toggles", () => {
  3080. const model = createMockModel({
  3081. id: "minimax/minimax-m3",
  3082. providerID: "minimax",
  3083. api: {
  3084. id: "MiniMax-M3",
  3085. url: "https://api.minimax.com/anthropic/v1",
  3086. npm: "@ai-sdk/anthropic",
  3087. },
  3088. })
  3089. const result = ProviderTransform.variants(model)
  3090. expect(result).toEqual({
  3091. none: { thinking: { type: "disabled" } },
  3092. thinking: { thinking: { type: "adaptive" } },
  3093. })
  3094. })
  3095. test("minimax m3 using openai-compatible returns thinking toggles", () => {
  3096. const model = createMockModel({
  3097. id: "minimax/minimax-m3",
  3098. providerID: "minimax",
  3099. api: {
  3100. id: "minimax-m3",
  3101. url: "https://api.minimax.com/v1",
  3102. npm: "@ai-sdk/openai-compatible",
  3103. },
  3104. })
  3105. expect(ProviderTransform.variants(model)).toEqual({
  3106. none: { thinking: { type: "disabled" } },
  3107. thinking: { thinking: { type: "adaptive" } },
  3108. })
  3109. })
  3110. test("glm returns empty object", () => {
  3111. const model = createMockModel({
  3112. id: "glm/glm-4",
  3113. providerID: "glm",
  3114. api: {
  3115. id: "glm-4",
  3116. url: "https://api.glm.com",
  3117. npm: "@ai-sdk/openai-compatible",
  3118. },
  3119. })
  3120. const result = ProviderTransform.variants(model)
  3121. expect(result).toEqual({})
  3122. })
  3123. test("glm-5.2 returns native effort variants for openai-compatible providers", () => {
  3124. const model = createMockModel({
  3125. id: "zhipuai/glm-5.2",
  3126. providerID: "zhipuai",
  3127. api: {
  3128. id: "glm-5.2",
  3129. url: "https://open.bigmodel.cn/api/paas/v4",
  3130. npm: "@ai-sdk/openai-compatible",
  3131. },
  3132. })
  3133. expect(ProviderTransform.variants(model)).toEqual({
  3134. high: { reasoningEffort: "high" },
  3135. max: { reasoningEffort: "max" },
  3136. })
  3137. })
  3138. test("recognizes GLM-5.2 provider model IDs", () => {
  3139. for (const id of ["accounts/fireworks/models/glm-5p2", "zai-org-glm-5-2", "umans-glm-5.2"]) {
  3140. const model = createMockModel({
  3141. id: `test/${id}`,
  3142. api: {
  3143. id,
  3144. url: "https://api.test.com",
  3145. npm: "@ai-sdk/openai-compatible",
  3146. },
  3147. })
  3148. expect(ProviderTransform.variants(model)).toEqual({
  3149. high: { reasoningEffort: "high" },
  3150. max: { reasoningEffort: "max" },
  3151. })
  3152. }
  3153. })
  3154. test("recognizes GLM-5.2 from the API ID when the configured model ID is an alias", () => {
  3155. const model = createMockModel({
  3156. id: "custom/my-glm",
  3157. api: {
  3158. id: "accounts/fireworks/models/glm-5p2",
  3159. url: "https://api.fireworks.ai/inference/v1",
  3160. npm: "@ai-sdk/openai-compatible",
  3161. },
  3162. })
  3163. expect(ProviderTransform.variants(model)).toEqual({
  3164. high: { reasoningEffort: "high" },
  3165. max: { reasoningEffort: "max" },
  3166. })
  3167. })
  3168. test("glm-5.2 returns openrouter effort variants for openrouter", () => {
  3169. const model = createMockModel({
  3170. id: "openrouter/z-ai/glm-5.2",
  3171. providerID: "openrouter",
  3172. api: {
  3173. id: "z-ai/glm-5.2",
  3174. url: "https://openrouter.ai/api/v1",
  3175. npm: "@openrouter/ai-sdk-provider",
  3176. },
  3177. })
  3178. expect(ProviderTransform.variants(model)).toEqual({
  3179. high: { reasoning: { effort: "high" } },
  3180. xhigh: { reasoning: { effort: "xhigh" } },
  3181. })
  3182. })
  3183. test("glm-5.2 returns effort variants for anthropic-compatible providers", () => {
  3184. const model = createMockModel({
  3185. id: "zai-coding-plan/glm-5.2",
  3186. providerID: "zai-coding-plan",
  3187. api: {
  3188. id: "glm-5.2",
  3189. url: "https://api.z.ai/api/anthropic",
  3190. npm: "@ai-sdk/anthropic",
  3191. },
  3192. })
  3193. expect(ProviderTransform.variants(model)).toEqual({
  3194. high: { effort: "high" },
  3195. max: { effort: "max" },
  3196. })
  3197. })
  3198. test("glm-5.2 falls back to provider defaults for other packages", () => {
  3199. const model = createMockModel({
  3200. id: "test/glm-5.2",
  3201. api: {
  3202. id: "glm-5.2",
  3203. url: "https://api.test.com",
  3204. npm: "@ai-sdk/amazon-bedrock",
  3205. },
  3206. })
  3207. expect(ProviderTransform.variants(model)).toEqual({
  3208. low: { reasoningConfig: { type: "enabled", maxReasoningEffort: "low" } },
  3209. medium: { reasoningConfig: { type: "enabled", maxReasoningEffort: "medium" } },
  3210. high: { reasoningConfig: { type: "enabled", maxReasoningEffort: "high" } },
  3211. })
  3212. })
  3213. test("mistral models with reasoning support return variants", () => {
  3214. const model = createMockModel({
  3215. id: "mistral/mistral-small-latest",
  3216. providerID: "mistral",
  3217. api: {
  3218. id: "mistral-small-latest",
  3219. url: "https://api.mistral.com",
  3220. npm: "@ai-sdk/mistral",
  3221. },
  3222. capabilities: { reasoning: true },
  3223. })
  3224. const result = ProviderTransform.variants(model)
  3225. expect(result).toEqual({
  3226. high: { reasoningEffort: "high" },
  3227. })
  3228. })
  3229. test("mistral-medium-3.5 with reasoning returns variants", () => {
  3230. const model = createMockModel({
  3231. id: "mistral/mistral-medium-3.5",
  3232. providerID: "mistral",
  3233. api: {
  3234. id: "mistral-medium-3.5",
  3235. url: "https://api.mistral.com",
  3236. npm: "@ai-sdk/mistral",
  3237. },
  3238. capabilities: { reasoning: true },
  3239. })
  3240. const result = ProviderTransform.variants(model)
  3241. expect(result).toEqual({
  3242. high: { reasoningEffort: "high" },
  3243. })
  3244. })
  3245. test("mistral without reasoning returns empty object", () => {
  3246. const model = createMockModel({
  3247. id: "mistral/mistral-large",
  3248. providerID: "mistral",
  3249. api: {
  3250. id: "mistral-large-latest",
  3251. url: "https://api.mistral.com",
  3252. npm: "@ai-sdk/mistral",
  3253. },
  3254. capabilities: { reasoning: false },
  3255. })
  3256. const result = ProviderTransform.variants(model)
  3257. expect(result).toEqual({})
  3258. })
  3259. test("mistral large with reasoning returns empty object (only small supports reasoning)", () => {
  3260. const model = createMockModel({
  3261. id: "mistral/mistral-large",
  3262. providerID: "mistral",
  3263. api: {
  3264. id: "mistral-large-latest",
  3265. url: "https://api.mistral.com",
  3266. npm: "@ai-sdk/mistral",
  3267. },
  3268. capabilities: { reasoning: true },
  3269. })
  3270. const result = ProviderTransform.variants(model)
  3271. expect(result).toEqual({})
  3272. })
  3273. describe("@openrouter/ai-sdk-provider", () => {
  3274. test("returns widely supported efforts for other reasoning models", () => {
  3275. const model = createMockModel({
  3276. id: "openrouter/test-model",
  3277. providerID: "openrouter",
  3278. api: {
  3279. id: "test-model",
  3280. url: "https://openrouter.ai",
  3281. npm: "@openrouter/ai-sdk-provider",
  3282. },
  3283. })
  3284. const result = ProviderTransform.variants(model)
  3285. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3286. expect(result.medium).toEqual({ reasoning: { effort: "medium" } })
  3287. })
  3288. test("gpt models return OPENAI_EFFORTS with reasoning", () => {
  3289. const model = createMockModel({
  3290. id: "openrouter/gpt-4",
  3291. providerID: "openrouter",
  3292. api: {
  3293. id: "gpt-4",
  3294. url: "https://openrouter.ai",
  3295. npm: "@openrouter/ai-sdk-provider",
  3296. },
  3297. })
  3298. const result = ProviderTransform.variants(model)
  3299. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  3300. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  3301. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  3302. })
  3303. for (const testCase of [
  3304. { id: "openai/o3-mini", efforts: ["none", "minimal", "low", "medium", "high", "xhigh"] },
  3305. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3306. { id: "openai/gpt-5-pro", efforts: ["high"] },
  3307. { id: "openai/gpt-5.5-pro", efforts: ["medium", "high", "xhigh"] },
  3308. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  3309. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3310. { id: "openai/gpt-5.3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3311. { id: "openai/gpt-5-chat-latest", efforts: [] },
  3312. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  3313. ]) {
  3314. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  3315. const result = ProviderTransform.variants(
  3316. createMockModel({
  3317. id: testCase.id,
  3318. providerID: "openrouter",
  3319. api: {
  3320. id: testCase.id,
  3321. url: "https://openrouter.ai",
  3322. npm: "@openrouter/ai-sdk-provider",
  3323. },
  3324. }),
  3325. )
  3326. expect(Object.keys(result)).toEqual(testCase.efforts)
  3327. })
  3328. }
  3329. test("gemini-3 returns widely supported efforts with reasoning", () => {
  3330. const model = createMockModel({
  3331. id: "openrouter/gemini-3-5-pro",
  3332. providerID: "openrouter",
  3333. api: {
  3334. id: "gemini-3-5-pro",
  3335. url: "https://openrouter.ai",
  3336. npm: "@openrouter/ai-sdk-provider",
  3337. },
  3338. })
  3339. const result = ProviderTransform.variants(model)
  3340. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3341. })
  3342. test("grok-4 uses the provider's standard efforts", () => {
  3343. const model = createMockModel({
  3344. id: "openrouter/grok-4",
  3345. providerID: "openrouter",
  3346. api: {
  3347. id: "grok-4",
  3348. url: "https://openrouter.ai",
  3349. npm: "@openrouter/ai-sdk-provider",
  3350. },
  3351. })
  3352. const result = ProviderTransform.variants(model)
  3353. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3354. expect(result.medium).toEqual({ reasoning: { effort: "medium" } })
  3355. })
  3356. test("grok-3-mini returns low and high with reasoning", () => {
  3357. const model = createMockModel({
  3358. id: "openrouter/grok-3-mini",
  3359. providerID: "openrouter",
  3360. api: {
  3361. id: "grok-3-mini",
  3362. url: "https://openrouter.ai",
  3363. npm: "@openrouter/ai-sdk-provider",
  3364. },
  3365. })
  3366. const result = ProviderTransform.variants(model)
  3367. expect(Object.keys(result)).toEqual(["low", "high"])
  3368. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  3369. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  3370. })
  3371. })
  3372. describe("@ai-sdk/gateway", () => {
  3373. test("configured anthropic aliases route by the API ID", () => {
  3374. const model = createMockModel({
  3375. id: "my-claude",
  3376. providerID: "gateway",
  3377. api: {
  3378. id: "anthropic/claude-sonnet-4-6",
  3379. url: "https://gateway.ai",
  3380. npm: "@ai-sdk/gateway",
  3381. },
  3382. })
  3383. const result = ProviderTransform.variants(model)
  3384. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3385. expect(result.high).toEqual({
  3386. thinking: {
  3387. type: "adaptive",
  3388. },
  3389. effort: "high",
  3390. })
  3391. })
  3392. test("configured google aliases route by the API ID", () => {
  3393. const model = createMockModel({
  3394. id: "my-gemini",
  3395. providerID: "gateway",
  3396. api: {
  3397. id: "google/gemini-2.5-pro",
  3398. url: "https://gateway.ai",
  3399. npm: "@ai-sdk/gateway",
  3400. },
  3401. })
  3402. expect(ProviderTransform.variants(model)).toEqual({
  3403. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  3404. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } },
  3405. })
  3406. })
  3407. test("anthropic sonnet 4.6 models return adaptive thinking options", () => {
  3408. const model = createMockModel({
  3409. id: "anthropic/claude-sonnet-4-6",
  3410. providerID: "gateway",
  3411. api: {
  3412. id: "anthropic/claude-sonnet-4-6",
  3413. url: "https://gateway.ai",
  3414. npm: "@ai-sdk/gateway",
  3415. },
  3416. })
  3417. const result = ProviderTransform.variants(model)
  3418. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3419. expect(result.medium).toEqual({
  3420. thinking: {
  3421. type: "adaptive",
  3422. },
  3423. effort: "medium",
  3424. })
  3425. })
  3426. test("anthropic sonnet 4.6 dot-format models return adaptive thinking options", () => {
  3427. const model = createMockModel({
  3428. id: "anthropic/claude-sonnet-4-6",
  3429. providerID: "gateway",
  3430. api: {
  3431. id: "anthropic/claude-sonnet-4.6",
  3432. url: "https://gateway.ai",
  3433. npm: "@ai-sdk/gateway",
  3434. },
  3435. })
  3436. const result = ProviderTransform.variants(model)
  3437. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3438. expect(result.medium).toEqual({
  3439. thinking: {
  3440. type: "adaptive",
  3441. },
  3442. effort: "medium",
  3443. })
  3444. })
  3445. test("anthropic opus 4.6 dot-format models return adaptive thinking options", () => {
  3446. const model = createMockModel({
  3447. id: "anthropic/claude-opus-4-6",
  3448. providerID: "gateway",
  3449. api: {
  3450. id: "anthropic/claude-opus-4.6",
  3451. url: "https://gateway.ai",
  3452. npm: "@ai-sdk/gateway",
  3453. },
  3454. })
  3455. const result = ProviderTransform.variants(model)
  3456. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3457. expect(result.high).toEqual({
  3458. thinking: {
  3459. type: "adaptive",
  3460. },
  3461. effort: "high",
  3462. })
  3463. })
  3464. test("anthropic opus 4.7 models return adaptive thinking options with xhigh", () => {
  3465. const model = createMockModel({
  3466. id: "anthropic/claude-opus-4-7",
  3467. providerID: "gateway",
  3468. api: {
  3469. id: "anthropic/claude-opus-4-7",
  3470. url: "https://gateway.ai",
  3471. npm: "@ai-sdk/gateway",
  3472. },
  3473. })
  3474. const result = ProviderTransform.variants(model)
  3475. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3476. expect(result.xhigh).toEqual({
  3477. thinking: {
  3478. type: "adaptive",
  3479. display: "summarized",
  3480. },
  3481. effort: "xhigh",
  3482. })
  3483. expect(result.max).toEqual({
  3484. thinking: {
  3485. type: "adaptive",
  3486. display: "summarized",
  3487. },
  3488. effort: "max",
  3489. })
  3490. })
  3491. test("anthropic opus 4.7 dot-format models return adaptive thinking options with xhigh", () => {
  3492. const model = createMockModel({
  3493. id: "anthropic/claude-opus-4-7",
  3494. providerID: "gateway",
  3495. api: {
  3496. id: "anthropic/claude-opus-4.7",
  3497. url: "https://gateway.ai",
  3498. npm: "@ai-sdk/gateway",
  3499. },
  3500. })
  3501. const result = ProviderTransform.variants(model)
  3502. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3503. })
  3504. test("anthropic opus 4.8 forces display summarized for adaptive reasoning", () => {
  3505. const model = createMockModel({
  3506. id: "anthropic/claude-opus-4-8",
  3507. providerID: "gateway",
  3508. api: {
  3509. id: "anthropic/claude-opus-4-8",
  3510. url: "https://gateway.ai",
  3511. npm: "@ai-sdk/gateway",
  3512. },
  3513. })
  3514. const result = ProviderTransform.variants(model)
  3515. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3516. expect(result.high).toEqual({
  3517. thinking: {
  3518. type: "adaptive",
  3519. display: "summarized",
  3520. },
  3521. effort: "high",
  3522. })
  3523. })
  3524. test("anthropic sonnet 5 returns adaptive thinking options with xhigh", () => {
  3525. const model = createMockModel({
  3526. id: "anthropic/claude-sonnet-5",
  3527. providerID: "gateway",
  3528. api: {
  3529. id: "anthropic/claude-sonnet-5",
  3530. url: "https://gateway.ai",
  3531. npm: "@ai-sdk/gateway",
  3532. },
  3533. })
  3534. const result = ProviderTransform.variants(model)
  3535. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3536. expect(result.high).toEqual({
  3537. thinking: {
  3538. type: "adaptive",
  3539. display: "summarized",
  3540. },
  3541. effort: "high",
  3542. })
  3543. })
  3544. test("anthropic opus 4.6 omits display so it keeps the summarized default", () => {
  3545. const model = createMockModel({
  3546. id: "anthropic/claude-opus-4-6",
  3547. providerID: "gateway",
  3548. api: {
  3549. id: "anthropic/claude-opus-4-6",
  3550. url: "https://gateway.ai",
  3551. npm: "@ai-sdk/gateway",
  3552. },
  3553. })
  3554. const result = ProviderTransform.variants(model)
  3555. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3556. expect(result.high).toEqual({
  3557. thinking: {
  3558. type: "adaptive",
  3559. },
  3560. effort: "high",
  3561. })
  3562. })
  3563. test("anthropic models return anthropic thinking options", () => {
  3564. const model = createMockModel({
  3565. id: "anthropic/claude-sonnet-4",
  3566. providerID: "gateway",
  3567. api: {
  3568. id: "anthropic/claude-sonnet-4",
  3569. url: "https://gateway.ai",
  3570. npm: "@ai-sdk/gateway",
  3571. },
  3572. })
  3573. const result = ProviderTransform.variants(model)
  3574. expect(Object.keys(result)).toEqual(["high", "max"])
  3575. expect(result.high).toEqual({
  3576. thinking: {
  3577. type: "enabled",
  3578. budgetTokens: 16000,
  3579. },
  3580. })
  3581. expect(result.max).toEqual({
  3582. thinking: {
  3583. type: "enabled",
  3584. budgetTokens: 31999,
  3585. },
  3586. })
  3587. })
  3588. test("returns OPENAI_EFFORTS with reasoningEffort", () => {
  3589. const model = createMockModel({
  3590. id: "gateway/gateway-model",
  3591. providerID: "gateway",
  3592. api: {
  3593. id: "gateway-model",
  3594. url: "https://gateway.ai",
  3595. npm: "@ai-sdk/gateway",
  3596. },
  3597. })
  3598. const result = ProviderTransform.variants(model)
  3599. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  3600. expect(result.low).toEqual({ reasoningEffort: "low" })
  3601. expect(result.high).toEqual({ reasoningEffort: "high" })
  3602. })
  3603. for (const testCase of [
  3604. { id: "openai/gpt-5-5", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3605. { id: "openai/gpt-5-pro", efforts: ["high"] },
  3606. { id: "openai/gpt-5-5-pro", efforts: ["medium", "high", "xhigh"] },
  3607. { id: "openai/gpt-5-2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  3608. { id: "openai/gpt-5-3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3609. { id: "openai/gpt-5-3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3610. { id: "openai/gpt-5-chat-latest", efforts: [] },
  3611. { id: "openai/gpt-5-2-chat-latest", efforts: ["medium"] },
  3612. ]) {
  3613. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  3614. const result = ProviderTransform.variants(
  3615. createMockModel({
  3616. id: testCase.id,
  3617. providerID: "gateway",
  3618. api: {
  3619. id: testCase.id,
  3620. url: "https://gateway.ai",
  3621. npm: "@ai-sdk/gateway",
  3622. },
  3623. }),
  3624. )
  3625. expect(Object.keys(result)).toEqual(testCase.efforts)
  3626. })
  3627. }
  3628. })
  3629. describe("@ai-sdk/github-copilot", () => {
  3630. test("standard models return low, medium, high", () => {
  3631. const model = createMockModel({
  3632. id: "gpt-4.5",
  3633. providerID: "github-copilot",
  3634. api: {
  3635. id: "gpt-4.5",
  3636. url: "https://api.githubcopilot.com",
  3637. npm: "@ai-sdk/github-copilot",
  3638. },
  3639. })
  3640. const result = ProviderTransform.variants(model)
  3641. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3642. expect(result.low).toEqual({
  3643. reasoningEffort: "low",
  3644. reasoningSummary: "auto",
  3645. include: ["reasoning.encrypted_content"],
  3646. })
  3647. })
  3648. test("gpt-5.1-codex-max includes xhigh", () => {
  3649. const model = createMockModel({
  3650. id: "gpt-5.1-codex-max",
  3651. providerID: "github-copilot",
  3652. api: {
  3653. id: "gpt-5.1-codex-max",
  3654. url: "https://api.githubcopilot.com",
  3655. npm: "@ai-sdk/github-copilot",
  3656. },
  3657. })
  3658. const result = ProviderTransform.variants(model)
  3659. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3660. })
  3661. test("gpt-5.1-codex-mini does not include xhigh", () => {
  3662. const model = createMockModel({
  3663. id: "gpt-5.1-codex-mini",
  3664. providerID: "github-copilot",
  3665. api: {
  3666. id: "gpt-5.1-codex-mini",
  3667. url: "https://api.githubcopilot.com",
  3668. npm: "@ai-sdk/github-copilot",
  3669. },
  3670. })
  3671. const result = ProviderTransform.variants(model)
  3672. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3673. })
  3674. test("gpt-5.1-codex does not include xhigh", () => {
  3675. const model = createMockModel({
  3676. id: "gpt-5.1-codex",
  3677. providerID: "github-copilot",
  3678. api: {
  3679. id: "gpt-5.1-codex",
  3680. url: "https://api.githubcopilot.com",
  3681. npm: "@ai-sdk/github-copilot",
  3682. },
  3683. })
  3684. const result = ProviderTransform.variants(model)
  3685. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3686. })
  3687. test("gpt-5.2 includes xhigh", () => {
  3688. const model = createMockModel({
  3689. id: "gpt-5.2",
  3690. providerID: "github-copilot",
  3691. api: {
  3692. id: "gpt-5.2",
  3693. url: "https://api.githubcopilot.com",
  3694. npm: "@ai-sdk/github-copilot",
  3695. },
  3696. })
  3697. const result = ProviderTransform.variants(model)
  3698. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3699. expect(result.xhigh).toEqual({
  3700. reasoningEffort: "xhigh",
  3701. reasoningSummary: "auto",
  3702. include: ["reasoning.encrypted_content"],
  3703. })
  3704. })
  3705. test("gpt-5.2-codex includes xhigh", () => {
  3706. const model = createMockModel({
  3707. id: "gpt-5.2-codex",
  3708. providerID: "github-copilot",
  3709. api: {
  3710. id: "gpt-5.2-codex",
  3711. url: "https://api.githubcopilot.com",
  3712. npm: "@ai-sdk/github-copilot",
  3713. },
  3714. })
  3715. const result = ProviderTransform.variants(model)
  3716. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3717. })
  3718. test("gpt-5.3-codex includes xhigh", () => {
  3719. const model = createMockModel({
  3720. id: "gpt-5.3-codex",
  3721. providerID: "github-copilot",
  3722. api: {
  3723. id: "gpt-5.3-codex",
  3724. url: "https://api.githubcopilot.com",
  3725. npm: "@ai-sdk/github-copilot",
  3726. },
  3727. })
  3728. const result = ProviderTransform.variants(model)
  3729. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3730. })
  3731. test("gpt-5.4 includes xhigh", () => {
  3732. const model = createMockModel({
  3733. id: "gpt-5.4",
  3734. release_date: "2026-03-05",
  3735. providerID: "github-copilot",
  3736. api: {
  3737. id: "gpt-5.4",
  3738. url: "https://api.githubcopilot.com",
  3739. npm: "@ai-sdk/github-copilot",
  3740. },
  3741. })
  3742. const result = ProviderTransform.variants(model)
  3743. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3744. })
  3745. })
  3746. describe("@ai-sdk/cerebras", () => {
  3747. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3748. const model = createMockModel({
  3749. id: "cerebras/llama-4",
  3750. providerID: "cerebras",
  3751. api: {
  3752. id: "llama-4-sc",
  3753. url: "https://api.cerebras.ai",
  3754. npm: "@ai-sdk/cerebras",
  3755. },
  3756. })
  3757. const result = ProviderTransform.variants(model)
  3758. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3759. expect(result.low).toEqual({ reasoningEffort: "low" })
  3760. expect(result.high).toEqual({ reasoningEffort: "high" })
  3761. })
  3762. })
  3763. describe("@ai-sdk/togetherai", () => {
  3764. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3765. const model = createMockModel({
  3766. id: "togetherai/llama-4",
  3767. providerID: "togetherai",
  3768. api: {
  3769. id: "llama-4-sc",
  3770. url: "https://api.togetherai.com",
  3771. npm: "@ai-sdk/togetherai",
  3772. },
  3773. })
  3774. const result = ProviderTransform.variants(model)
  3775. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3776. expect(result.low).toEqual({ reasoningEffort: "low" })
  3777. expect(result.high).toEqual({ reasoningEffort: "high" })
  3778. })
  3779. })
  3780. describe("@ai-sdk/xai", () => {
  3781. test("grok-4.5 uses standard reasoning efforts", () => {
  3782. const model = createMockModel({
  3783. id: "xai/grok-4.5",
  3784. providerID: "xai",
  3785. api: {
  3786. id: "grok-4.5",
  3787. url: "https://api.x.ai",
  3788. npm: "@ai-sdk/xai",
  3789. },
  3790. })
  3791. const result = ProviderTransform.variants(model)
  3792. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3793. expect(result.medium).toEqual({ reasoningEffort: "medium" })
  3794. })
  3795. test("grok-3-mini returns low and high with reasoningEffort", () => {
  3796. const model = createMockModel({
  3797. id: "xai/grok-3-mini",
  3798. providerID: "xai",
  3799. api: {
  3800. id: "grok-3-mini",
  3801. url: "https://api.x.ai",
  3802. npm: "@ai-sdk/xai",
  3803. },
  3804. })
  3805. const result = ProviderTransform.variants(model)
  3806. expect(Object.keys(result)).toEqual(["low", "high"])
  3807. expect(result.low).toEqual({ reasoningEffort: "low" })
  3808. expect(result.high).toEqual({ reasoningEffort: "high" })
  3809. })
  3810. })
  3811. describe("@ai-sdk/deepinfra", () => {
  3812. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3813. const model = createMockModel({
  3814. id: "deepinfra/llama-4",
  3815. providerID: "deepinfra",
  3816. api: {
  3817. id: "llama-4-sc",
  3818. url: "https://api.deepinfra.com",
  3819. npm: "@ai-sdk/deepinfra",
  3820. },
  3821. })
  3822. const result = ProviderTransform.variants(model)
  3823. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3824. expect(result.low).toEqual({ reasoningEffort: "low" })
  3825. expect(result.high).toEqual({ reasoningEffort: "high" })
  3826. })
  3827. })
  3828. describe("@ai-sdk/openai-compatible", () => {
  3829. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3830. const model = createMockModel({
  3831. id: "custom-provider/custom-model",
  3832. providerID: "custom-provider",
  3833. api: {
  3834. id: "custom-model",
  3835. url: "https://api.custom.com",
  3836. npm: "@ai-sdk/openai-compatible",
  3837. },
  3838. })
  3839. const result = ProviderTransform.variants(model)
  3840. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3841. expect(result.low).toEqual({ reasoningEffort: "low" })
  3842. expect(result.high).toEqual({ reasoningEffort: "high" })
  3843. })
  3844. test("north-mini-code-1-0 returns only none and high", () => {
  3845. const model = createMockModel({
  3846. id: "cohere/north-mini-code-1-0",
  3847. providerID: "cohere",
  3848. api: {
  3849. id: "North-Mini-Code-1-0-latest",
  3850. url: "https://api.cohere.com/compatibility/v1",
  3851. npm: "@ai-sdk/openai-compatible",
  3852. },
  3853. })
  3854. const result = ProviderTransform.variants(model)
  3855. expect(result).toEqual({
  3856. none: { reasoningEffort: "none" },
  3857. high: { reasoningEffort: "high" },
  3858. })
  3859. })
  3860. })
  3861. describe("@ai-sdk/azure", () => {
  3862. test("o1-mini returns empty object", () => {
  3863. const model = createMockModel({
  3864. id: "o1-mini",
  3865. providerID: "azure",
  3866. api: {
  3867. id: "o1-mini",
  3868. url: "https://azure.com",
  3869. npm: "@ai-sdk/azure",
  3870. },
  3871. })
  3872. const result = ProviderTransform.variants(model)
  3873. expect(result).toEqual({})
  3874. })
  3875. test("standard azure models return custom efforts with reasoningSummary", () => {
  3876. const model = createMockModel({
  3877. id: "o1",
  3878. providerID: "azure",
  3879. api: {
  3880. id: "o1",
  3881. url: "https://azure.com",
  3882. npm: "@ai-sdk/azure",
  3883. },
  3884. })
  3885. const result = ProviderTransform.variants(model)
  3886. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3887. expect(result.low).toEqual({
  3888. reasoningEffort: "low",
  3889. reasoningSummary: "auto",
  3890. include: ["reasoning.encrypted_content"],
  3891. })
  3892. })
  3893. test("gpt-5 adds minimal effort", () => {
  3894. const model = createMockModel({
  3895. id: "gpt-5",
  3896. providerID: "azure",
  3897. api: {
  3898. id: "gpt-5",
  3899. url: "https://azure.com",
  3900. npm: "@ai-sdk/azure",
  3901. },
  3902. })
  3903. const result = ProviderTransform.variants(model)
  3904. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  3905. })
  3906. for (const testCase of [
  3907. { id: "gpt-5-1", efforts: ["none", "low", "medium", "high"] },
  3908. { id: "gpt-5-4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3909. { id: "gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3910. { id: "gpt-5-5", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3911. ]) {
  3912. test(`${testCase.id} returns supported Azure reasoning efforts`, () => {
  3913. const result = ProviderTransform.variants(
  3914. createMockModel({
  3915. id: testCase.id,
  3916. providerID: "azure",
  3917. api: {
  3918. id: testCase.id,
  3919. url: "https://azure.com",
  3920. npm: "@ai-sdk/azure",
  3921. },
  3922. }),
  3923. )
  3924. expect(Object.keys(result)).toEqual(testCase.efforts)
  3925. })
  3926. }
  3927. })
  3928. describe("@ai-sdk/openai", () => {
  3929. test("gpt-5-pro returns only high effort", () => {
  3930. const model = createMockModel({
  3931. id: "gpt-5-pro",
  3932. providerID: "openai",
  3933. api: {
  3934. id: "gpt-5-pro",
  3935. url: "https://api.openai.com",
  3936. npm: "@ai-sdk/openai",
  3937. },
  3938. })
  3939. const result = ProviderTransform.variants(model)
  3940. expect(Object.keys(result)).toEqual(["high"])
  3941. })
  3942. test("standard openai models return custom efforts with reasoningSummary", () => {
  3943. const model = createMockModel({
  3944. id: "gpt-5",
  3945. providerID: "openai",
  3946. api: {
  3947. id: "gpt-5",
  3948. url: "https://api.openai.com",
  3949. npm: "@ai-sdk/openai",
  3950. },
  3951. release_date: "2024-06-01",
  3952. })
  3953. const result = ProviderTransform.variants(model)
  3954. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  3955. expect(result.low).toEqual({
  3956. reasoningEffort: "low",
  3957. reasoningSummary: "auto",
  3958. include: ["reasoning.encrypted_content"],
  3959. })
  3960. })
  3961. test("models after 2025-11-13 include 'none' effort", () => {
  3962. const model = createMockModel({
  3963. id: "gpt-5-nano",
  3964. providerID: "openai",
  3965. api: {
  3966. id: "gpt-5-nano",
  3967. url: "https://api.openai.com",
  3968. npm: "@ai-sdk/openai",
  3969. },
  3970. release_date: "2025-11-14",
  3971. })
  3972. const result = ProviderTransform.variants(model)
  3973. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high"])
  3974. })
  3975. test("models after 2025-12-04 include 'xhigh' effort", () => {
  3976. const model = createMockModel({
  3977. id: "openai/gpt-5-reasoning",
  3978. providerID: "openai",
  3979. api: {
  3980. id: "gpt-5-reasoning",
  3981. url: "https://api.openai.com",
  3982. npm: "@ai-sdk/openai",
  3983. },
  3984. release_date: "2025-12-05",
  3985. })
  3986. const result = ProviderTransform.variants(model)
  3987. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  3988. })
  3989. for (const testCase of [
  3990. { id: "o1", releaseDate: "2024-12-17", efforts: ["low", "medium", "high"] },
  3991. { id: "o1-pro", releaseDate: "2025-03-19", efforts: ["low", "medium", "high"] },
  3992. { id: "o3", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  3993. { id: "o3-mini", releaseDate: "2025-01-31", efforts: ["low", "medium", "high"] },
  3994. { id: "o3-pro", releaseDate: "2025-06-10", efforts: ["low", "medium", "high"] },
  3995. { id: "o4-mini", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  3996. { id: "o3-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  3997. { id: "o4-mini-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  3998. { id: "gpt-5.1", releaseDate: "2025-11-13", efforts: ["none", "low", "medium", "high"] },
  3999. { id: "gpt-5.4", releaseDate: "2026-03-05", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4000. {
  4001. id: "gpt-5.5",
  4002. modelID: "gpt-5-5",
  4003. releaseDate: "2026-04-23",
  4004. efforts: ["none", "low", "medium", "high", "xhigh"],
  4005. },
  4006. { id: "gpt-5.4-pro", releaseDate: "2026-03-05", efforts: ["medium", "high", "xhigh"] },
  4007. { id: "gpt-5.5-pro", releaseDate: "2026-04-23", efforts: ["medium", "high", "xhigh"] },
  4008. { id: "gpt-5-codex", releaseDate: "2025-09-23", efforts: ["low", "medium", "high"] },
  4009. { id: "gpt-5.1-codex", releaseDate: "2025-11-13", efforts: ["low", "medium", "high"] },
  4010. { id: "gpt-5.1-codex-max", releaseDate: "2025-11-13", efforts: ["low", "medium", "high", "xhigh"] },
  4011. { id: "gpt-5.2-codex", releaseDate: "2025-12-11", efforts: ["low", "medium", "high", "xhigh"] },
  4012. { id: "gpt-5.3-codex", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4013. { id: "gpt-5.3-codex-max", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4014. { id: "gpt-5-chat-latest", releaseDate: "2025-08-07", efforts: [] },
  4015. { id: "gpt-5.1-chat-latest", releaseDate: "2025-11-13", efforts: ["medium"] },
  4016. { id: "gpt-5.2-chat-latest", releaseDate: "2025-12-11", efforts: ["medium"] },
  4017. ]) {
  4018. test(`${testCase.id} returns supported reasoning efforts`, () => {
  4019. const result = ProviderTransform.variants(
  4020. createMockModel({
  4021. id: testCase.modelID ?? testCase.id,
  4022. providerID: "openai",
  4023. api: {
  4024. id: testCase.id,
  4025. url: "https://api.openai.com",
  4026. npm: "@ai-sdk/openai",
  4027. },
  4028. release_date: testCase.releaseDate,
  4029. }),
  4030. )
  4031. expect(Object.keys(result)).toEqual(testCase.efforts)
  4032. })
  4033. }
  4034. test("gpt-50 (lookalike) does not get gpt-5 family treatment", () => {
  4035. const model = createMockModel({
  4036. id: "gpt-50",
  4037. providerID: "openai",
  4038. api: {
  4039. id: "gpt-50",
  4040. url: "https://api.openai.com",
  4041. npm: "@ai-sdk/openai",
  4042. },
  4043. release_date: "2024-01-01",
  4044. })
  4045. const result = ProviderTransform.variants(model)
  4046. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  4047. })
  4048. })
  4049. describe("@ai-sdk/amazon-bedrock/mantle", () => {
  4050. test("gpt-5.5 returns OpenAI-style reasoning variants", () => {
  4051. const model = createMockModel({
  4052. id: "openai.gpt-5.5",
  4053. providerID: "amazon-bedrock",
  4054. api: {
  4055. id: "openai.gpt-5.5",
  4056. url: "https://bedrock-mantle.us-east-2.api.aws/openai/v1",
  4057. npm: "@ai-sdk/amazon-bedrock/mantle",
  4058. },
  4059. release_date: "2026-04-23",
  4060. })
  4061. const result = ProviderTransform.variants(model)
  4062. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high", "xhigh"])
  4063. expect(result.medium).toEqual({
  4064. reasoningEffort: "medium",
  4065. reasoningSummary: "auto",
  4066. include: ["reasoning.encrypted_content"],
  4067. })
  4068. })
  4069. })
  4070. describe("@ai-sdk/anthropic", () => {
  4071. for (const testCase of [
  4072. {
  4073. name: "opus 4.5",
  4074. apiIds: ["claude-opus-4-5-20251101", "claude-opus-4.5-20251101"],
  4075. efforts: ["low", "medium", "high"],
  4076. expectedHigh: { effort: "high" },
  4077. },
  4078. {
  4079. name: "sonnet 4.6",
  4080. apiIds: ["claude-sonnet-4-6", "claude-sonnet-4.6"],
  4081. efforts: ["low", "medium", "high", "max"],
  4082. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  4083. },
  4084. {
  4085. name: "opus 4.6",
  4086. apiIds: ["claude-opus-4-6", "claude-opus-4.6"],
  4087. efforts: ["low", "medium", "high", "max"],
  4088. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  4089. },
  4090. {
  4091. name: "opus 4.7",
  4092. apiIds: ["claude-opus-4-7", "claude-opus-4.7"],
  4093. efforts: ["low", "medium", "high", "xhigh", "max"],
  4094. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4095. },
  4096. {
  4097. name: "opus 4.8",
  4098. apiIds: ["claude-opus-4-8", "claude-opus-4.8"],
  4099. efforts: ["low", "medium", "high", "xhigh", "max"],
  4100. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4101. },
  4102. {
  4103. name: "sonnet 5",
  4104. apiIds: ["claude-sonnet-5", "claude-sonnet-5-20260630"],
  4105. efforts: ["low", "medium", "high", "xhigh", "max"],
  4106. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4107. },
  4108. {
  4109. name: "fable 5",
  4110. apiIds: ["claude-fable-5"],
  4111. efforts: ["low", "medium", "high", "xhigh", "max"],
  4112. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4113. },
  4114. ]) {
  4115. for (const apiId of testCase.apiIds) {
  4116. test(`${testCase.name} ${apiId} returns supported reasoning efforts`, () => {
  4117. const result = ProviderTransform.variants(
  4118. createMockModel({
  4119. id: `anthropic/${apiId}`,
  4120. providerID: "anthropic",
  4121. api: {
  4122. id: apiId,
  4123. url: "https://api.anthropic.com",
  4124. npm: "@ai-sdk/anthropic",
  4125. },
  4126. }),
  4127. )
  4128. expect(Object.keys(result)).toEqual(testCase.efforts)
  4129. expect(result.high).toEqual(testCase.expectedHigh)
  4130. })
  4131. }
  4132. }
  4133. test("github copilot opus 4.7 returns only medium reasoning effort", () => {
  4134. const model = createMockModel({
  4135. id: "claude-opus-4.7",
  4136. providerID: "github-copilot",
  4137. api: {
  4138. id: "claude-opus-4.7",
  4139. url: "https://api.githubcopilot.com/v1",
  4140. npm: "@ai-sdk/anthropic",
  4141. },
  4142. })
  4143. const result = ProviderTransform.variants(model)
  4144. expect(result).toEqual({
  4145. medium: {
  4146. thinking: {
  4147. type: "adaptive",
  4148. display: "summarized",
  4149. },
  4150. effort: "medium",
  4151. },
  4152. })
  4153. })
  4154. test("returns high and max with thinking config", () => {
  4155. const model = createMockModel({
  4156. id: "anthropic/claude-4",
  4157. providerID: "anthropic",
  4158. api: {
  4159. id: "claude-4",
  4160. url: "https://api.anthropic.com",
  4161. npm: "@ai-sdk/anthropic",
  4162. },
  4163. })
  4164. const result = ProviderTransform.variants(model)
  4165. expect(Object.keys(result)).toEqual(["high", "max"])
  4166. expect(result.high).toEqual({
  4167. thinking: {
  4168. type: "enabled",
  4169. budgetTokens: 16000,
  4170. },
  4171. })
  4172. expect(result.max).toEqual({
  4173. thinking: {
  4174. type: "enabled",
  4175. budgetTokens: 31999,
  4176. },
  4177. })
  4178. })
  4179. })
  4180. describe("@ai-sdk/google-vertex/anthropic", () => {
  4181. test("opus 4.8 uses adaptive reasoning for Vertex model IDs", () => {
  4182. const result = ProviderTransform.variants(
  4183. createMockModel({
  4184. id: "google-vertex-anthropic/claude-opus-4-8@default",
  4185. providerID: "google-vertex-anthropic",
  4186. api: {
  4187. id: "claude-opus-4-8@default",
  4188. url: "https://us-central1-aiplatform.googleapis.com",
  4189. npm: "@ai-sdk/google-vertex/anthropic",
  4190. },
  4191. }),
  4192. )
  4193. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4194. expect(result.high).toEqual({
  4195. thinking: {
  4196. type: "adaptive",
  4197. display: "summarized",
  4198. },
  4199. effort: "high",
  4200. })
  4201. })
  4202. test("sonnet 5 uses adaptive reasoning for Vertex model IDs", () => {
  4203. const result = ProviderTransform.variants(
  4204. createMockModel({
  4205. id: "google-vertex-anthropic/claude-sonnet-5@default",
  4206. providerID: "google-vertex-anthropic",
  4207. api: {
  4208. id: "claude-sonnet-5@default",
  4209. url: "https://us-central1-aiplatform.googleapis.com",
  4210. npm: "@ai-sdk/google-vertex/anthropic",
  4211. },
  4212. }),
  4213. )
  4214. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4215. expect(result.high).toEqual({
  4216. thinking: {
  4217. type: "adaptive",
  4218. display: "summarized",
  4219. },
  4220. effort: "high",
  4221. })
  4222. })
  4223. })
  4224. describe("@ai-sdk/amazon-bedrock", () => {
  4225. test("anthropic sonnet 4.6 returns adaptive reasoning options", () => {
  4226. const model = createMockModel({
  4227. id: "bedrock/anthropic-claude-sonnet-4-6",
  4228. providerID: "bedrock",
  4229. api: {
  4230. id: "anthropic.claude-sonnet-4-6",
  4231. url: "https://bedrock.amazonaws.com",
  4232. npm: "@ai-sdk/amazon-bedrock",
  4233. },
  4234. })
  4235. const result = ProviderTransform.variants(model)
  4236. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  4237. expect(result.max).toEqual({
  4238. reasoningConfig: {
  4239. type: "adaptive",
  4240. maxReasoningEffort: "max",
  4241. },
  4242. })
  4243. })
  4244. test("anthropic opus 4.7 returns adaptive reasoning options with xhigh", () => {
  4245. const model = createMockModel({
  4246. id: "bedrock/anthropic-claude-opus-4-7",
  4247. providerID: "bedrock",
  4248. api: {
  4249. id: "anthropic.claude-opus-4-7",
  4250. url: "https://bedrock.amazonaws.com",
  4251. npm: "@ai-sdk/amazon-bedrock",
  4252. },
  4253. })
  4254. const result = ProviderTransform.variants(model)
  4255. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4256. expect(result.xhigh).toEqual({
  4257. reasoningConfig: {
  4258. type: "adaptive",
  4259. maxReasoningEffort: "xhigh",
  4260. display: "summarized",
  4261. },
  4262. })
  4263. expect(result.max).toEqual({
  4264. reasoningConfig: {
  4265. type: "adaptive",
  4266. maxReasoningEffort: "max",
  4267. display: "summarized",
  4268. },
  4269. })
  4270. })
  4271. test("anthropic opus 4.8 returns adaptive reasoning options with xhigh", () => {
  4272. const result = ProviderTransform.variants(
  4273. createMockModel({
  4274. id: "bedrock/anthropic-claude-opus-4.8",
  4275. providerID: "bedrock",
  4276. api: {
  4277. id: "anthropic.claude-opus-4.8",
  4278. url: "https://bedrock.amazonaws.com",
  4279. npm: "@ai-sdk/amazon-bedrock",
  4280. },
  4281. }),
  4282. )
  4283. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4284. expect(result.high).toEqual({
  4285. reasoningConfig: {
  4286. type: "adaptive",
  4287. maxReasoningEffort: "high",
  4288. display: "summarized",
  4289. },
  4290. })
  4291. })
  4292. test("anthropic sonnet 5 returns adaptive reasoning options with xhigh", () => {
  4293. const result = ProviderTransform.variants(
  4294. createMockModel({
  4295. id: "bedrock/anthropic-claude-sonnet-5",
  4296. providerID: "bedrock",
  4297. api: {
  4298. id: "anthropic.claude-sonnet-5",
  4299. url: "https://bedrock.amazonaws.com",
  4300. npm: "@ai-sdk/amazon-bedrock",
  4301. },
  4302. }),
  4303. )
  4304. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4305. expect(result.high).toEqual({
  4306. reasoningConfig: {
  4307. type: "adaptive",
  4308. maxReasoningEffort: "high",
  4309. display: "summarized",
  4310. },
  4311. })
  4312. })
  4313. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningConfig", () => {
  4314. const model = createMockModel({
  4315. id: "bedrock/llama-4",
  4316. providerID: "bedrock",
  4317. api: {
  4318. id: "llama-4-sc",
  4319. url: "https://bedrock.amazonaws.com",
  4320. npm: "@ai-sdk/amazon-bedrock",
  4321. },
  4322. })
  4323. const result = ProviderTransform.variants(model)
  4324. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  4325. expect(result.low).toEqual({
  4326. reasoningConfig: {
  4327. type: "enabled",
  4328. maxReasoningEffort: "low",
  4329. },
  4330. })
  4331. })
  4332. })
  4333. for (const provider of [
  4334. { name: "@ai-sdk/google", providerID: "google", url: "https://generativelanguage.googleapis.com" },
  4335. { name: "@ai-sdk/google-vertex", providerID: "google-vertex", url: "https://vertexai.googleapis.com" },
  4336. ]) {
  4337. describe(provider.name, () => {
  4338. for (const testCase of [
  4339. {
  4340. apiId: "gemini-2.5-pro",
  4341. efforts: ["high", "max"],
  4342. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  4343. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } },
  4344. },
  4345. {
  4346. apiId: "gemini-2.5-flash",
  4347. efforts: ["high", "max"],
  4348. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  4349. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
  4350. },
  4351. {
  4352. apiId: "gemini-3-pro-preview",
  4353. efforts: ["low", "medium", "high"],
  4354. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4355. },
  4356. {
  4357. apiId: "gemini-3.1-pro-preview",
  4358. efforts: ["low", "medium", "high"],
  4359. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4360. },
  4361. {
  4362. apiId: "gemini-3-flash-preview",
  4363. efforts: ["minimal", "low", "medium", "high"],
  4364. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4365. },
  4366. {
  4367. apiId: "gemini-3.1-flash-lite",
  4368. efforts: ["minimal", "low", "medium", "high"],
  4369. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4370. },
  4371. {
  4372. apiId: "gemini-3.1-flash-image-preview",
  4373. efforts: ["minimal", "high"],
  4374. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4375. },
  4376. {
  4377. apiId: "gemini-3-pro-image-preview",
  4378. efforts: ["high"],
  4379. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4380. },
  4381. ]) {
  4382. test(`${testCase.apiId} returns supported thinking controls`, () => {
  4383. const result = ProviderTransform.variants(
  4384. createMockModel({
  4385. id: `${provider.providerID}/${testCase.apiId}`,
  4386. providerID: provider.providerID,
  4387. api: {
  4388. id: testCase.apiId,
  4389. url: provider.url,
  4390. npm: provider.name,
  4391. },
  4392. }),
  4393. )
  4394. expect(Object.keys(result)).toEqual(testCase.efforts)
  4395. expect(result.high).toEqual(testCase.expectedHigh)
  4396. if (testCase.expectedMax) expect(result.max).toEqual(testCase.expectedMax)
  4397. })
  4398. }
  4399. })
  4400. }
  4401. describe("@ai-sdk/cohere", () => {
  4402. test("returns empty object", () => {
  4403. const model = createMockModel({
  4404. id: "cohere/command-r",
  4405. providerID: "cohere",
  4406. api: {
  4407. id: "command-r",
  4408. url: "https://api.cohere.com",
  4409. npm: "@ai-sdk/cohere",
  4410. },
  4411. })
  4412. const result = ProviderTransform.variants(model)
  4413. expect(result).toEqual({})
  4414. })
  4415. })
  4416. describe("@ai-sdk/groq", () => {
  4417. test("returns none and WIDELY_SUPPORTED_EFFORTS with thinkingLevel", () => {
  4418. const model = createMockModel({
  4419. id: "groq/llama-4",
  4420. providerID: "groq",
  4421. api: {
  4422. id: "llama-4-sc",
  4423. url: "https://api.groq.com",
  4424. npm: "@ai-sdk/groq",
  4425. },
  4426. })
  4427. const result = ProviderTransform.variants(model)
  4428. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high"])
  4429. expect(result.none).toEqual({
  4430. reasoningEffort: "none",
  4431. })
  4432. expect(result.low).toEqual({
  4433. reasoningEffort: "low",
  4434. })
  4435. })
  4436. })
  4437. describe("@ai-sdk/perplexity", () => {
  4438. test("returns empty object", () => {
  4439. const model = createMockModel({
  4440. id: "perplexity/sonar-plus",
  4441. providerID: "perplexity",
  4442. api: {
  4443. id: "sonar-plus",
  4444. url: "https://api.perplexity.ai",
  4445. npm: "@ai-sdk/perplexity",
  4446. },
  4447. })
  4448. const result = ProviderTransform.variants(model)
  4449. expect(result).toEqual({})
  4450. })
  4451. })
  4452. describe("@jerome-benoit/sap-ai-provider-v2", () => {
  4453. const sapModel = (apiId: string, releaseDate = "2024-01-01") =>
  4454. createMockModel({
  4455. id: `sap-ai-core/${apiId}`,
  4456. providerID: "sap-ai-core",
  4457. api: {
  4458. id: apiId,
  4459. url: "https://api.ai.sap",
  4460. npm: "@jerome-benoit/sap-ai-provider-v2",
  4461. },
  4462. release_date: releaseDate,
  4463. })
  4464. for (const testCase of [
  4465. {
  4466. name: "sonnet 4.6",
  4467. apiIds: ["anthropic--claude-sonnet-4-6"],
  4468. efforts: ["low", "medium", "high", "max"],
  4469. thinking: { type: "adaptive" },
  4470. },
  4471. {
  4472. name: "opus 4.6",
  4473. apiIds: ["anthropic--claude-4.6-opus", "anthropic--claude-4-6-opus"],
  4474. efforts: ["low", "medium", "high", "max"],
  4475. thinking: { type: "adaptive" },
  4476. },
  4477. {
  4478. name: "opus 4.7",
  4479. apiIds: ["anthropic--claude-4.7-opus", "anthropic--claude-4-7-opus"],
  4480. efforts: ["low", "medium", "high", "xhigh", "max"],
  4481. thinking: { type: "adaptive", display: "summarized" },
  4482. },
  4483. {
  4484. name: "opus 4.8",
  4485. apiIds: ["anthropic--claude-4.8-opus", "anthropic--claude-4-8-opus"],
  4486. efforts: ["low", "medium", "high", "xhigh", "max"],
  4487. thinking: { type: "adaptive", display: "summarized" },
  4488. },
  4489. {
  4490. name: "sonnet 5",
  4491. apiIds: ["anthropic--claude-sonnet-5", "anthropic--claude-5-sonnet"],
  4492. efforts: ["low", "medium", "high", "xhigh", "max"],
  4493. thinking: { type: "adaptive", display: "summarized" },
  4494. },
  4495. ]) {
  4496. for (const apiId of testCase.apiIds) {
  4497. test(`${testCase.name} ${apiId} returns adaptive thinking variants under modelParams`, () => {
  4498. const result = ProviderTransform.variants(sapModel(apiId))
  4499. expect(Object.keys(result)).toEqual(testCase.efforts)
  4500. for (const effort of testCase.efforts) {
  4501. expect(result[effort]).toEqual({
  4502. modelParams: {
  4503. thinking: testCase.thinking,
  4504. output_config: { effort },
  4505. },
  4506. })
  4507. }
  4508. })
  4509. }
  4510. }
  4511. for (const apiId of ["anthropic--claude-sonnet-4", "anthropic--claude-4.5-opus"]) {
  4512. test(`${apiId} returns budget_tokens variants under modelParams`, () => {
  4513. const result = ProviderTransform.variants(sapModel(apiId))
  4514. expect(Object.keys(result)).toEqual(["high", "max"])
  4515. expect(result.high).toEqual({
  4516. modelParams: { thinking: { type: "enabled", budget_tokens: 16000 } },
  4517. })
  4518. expect(result.max).toEqual({
  4519. modelParams: { thinking: { type: "enabled", budget_tokens: 31999 } },
  4520. })
  4521. })
  4522. }
  4523. for (const testCase of [
  4524. { apiId: "gemini-2.5-pro", maxBudget: 32768 },
  4525. { apiId: "gemini-2.5-flash", maxBudget: 24576 },
  4526. ]) {
  4527. test(`${testCase.apiId} returns thinkingConfig variants under modelParams`, () => {
  4528. const result = ProviderTransform.variants(sapModel(testCase.apiId))
  4529. expect(Object.keys(result)).toEqual(["high", "max"])
  4530. expect(result.high).toEqual({
  4531. modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } },
  4532. })
  4533. expect(result.max).toEqual({
  4534. modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: testCase.maxBudget } },
  4535. })
  4536. })
  4537. }
  4538. for (const testCase of [
  4539. { apiId: "gpt-5", releaseDate: "2025-08-07", efforts: ["minimal", "low", "medium", "high"] },
  4540. { apiId: "gpt-5-mini", releaseDate: "2025-08-07", efforts: ["minimal", "low", "medium", "high"] },
  4541. { apiId: "gpt-5-nano", releaseDate: "2025-08-07", efforts: ["minimal", "low", "medium", "high"] },
  4542. { apiId: "gpt-5.4", releaseDate: "2026-01-15", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4543. { apiId: "azure-openai--o3-mini", releaseDate: "2024-01-01", efforts: ["low", "medium", "high"] },
  4544. ]) {
  4545. test(`${testCase.apiId} returns reasoning_effort variants under modelParams`, () => {
  4546. const result = ProviderTransform.variants(sapModel(testCase.apiId, testCase.releaseDate))
  4547. expect(Object.keys(result)).toEqual(testCase.efforts)
  4548. for (const effort of testCase.efforts) {
  4549. expect(result[effort]).toEqual({ modelParams: { reasoning_effort: effort } })
  4550. }
  4551. })
  4552. }
  4553. for (const apiId of [
  4554. "gemini-3.1-flash-lite",
  4555. "cohere--command-a-reasoning",
  4556. "sonar-deep-research",
  4557. "aws--llama-opus-4.7-fake",
  4558. ]) {
  4559. test(`${apiId} falls through to harmonized reasoning_effort fallback`, () => {
  4560. const result = ProviderTransform.variants(sapModel(apiId))
  4561. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  4562. for (const effort of ["low", "medium", "high"]) {
  4563. expect(result[effort]).toEqual({ modelParams: { reasoning_effort: effort } })
  4564. }
  4565. })
  4566. }
  4567. })
  4568. describe("ai-gateway-provider (cloudflare-ai-gateway)", () => {
  4569. const cfModel = (apiId: string, releaseDate = "2024-01-01") =>
  4570. createMockModel({
  4571. id: `cloudflare-ai-gateway/${apiId}`,
  4572. providerID: "cloudflare-ai-gateway",
  4573. api: {
  4574. id: apiId,
  4575. url: "https://gateway.ai.cloudflare.com/v1/compat",
  4576. npm: "ai-gateway-provider",
  4577. },
  4578. release_date: releaseDate,
  4579. })
  4580. for (const testCase of [
  4581. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4582. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  4583. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4584. { id: "openai/gpt-5-pro", efforts: ["high"] },
  4585. { id: "openai/gpt-5.2-pro", efforts: ["medium", "high", "xhigh"] },
  4586. { id: "openai/gpt-5-chat-latest", efforts: [] },
  4587. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  4588. ]) {
  4589. test(`${testCase.id} returns supported reasoning efforts`, () => {
  4590. const result = ProviderTransform.variants(cfModel(testCase.id, "2026-03-05"))
  4591. expect(Object.keys(result)).toEqual(testCase.efforts)
  4592. })
  4593. }
  4594. test("openai gpt-4o (no reasoning) returns empty", () => {
  4595. const model = cfModel("openai/gpt-4o")
  4596. model.capabilities.reasoning = false
  4597. const result = ProviderTransform.variants(model)
  4598. expect(result).toEqual({})
  4599. })
  4600. test("non-openai upstream falls back to widely-supported OAI efforts", () => {
  4601. const result = ProviderTransform.variants(cfModel("anthropic/claude-sonnet-4-6"))
  4602. expect(result).toEqual({
  4603. low: { reasoningEffort: "low" },
  4604. medium: { reasoningEffort: "medium" },
  4605. high: { reasoningEffort: "high" },
  4606. })
  4607. })
  4608. })
  4609. })
  4610. describe("ProviderTransform.smallOptions - gpt-5 chat/search", () => {
  4611. const createModel = (apiId: string) => {
  4612. const model = {
  4613. id: `openai/${apiId}`,
  4614. providerID: "openai",
  4615. api: {
  4616. id: apiId,
  4617. url: "https://api.openai.com",
  4618. npm: "@ai-sdk/openai",
  4619. },
  4620. capabilities: { reasoning: true },
  4621. limit: { output: 64_000 },
  4622. release_date: "2026-01-01",
  4623. } as any
  4624. model.variants = ProviderTransform.variants(model)
  4625. return model
  4626. }
  4627. for (const testCase of [
  4628. { id: "gpt-5-chat-latest", options: { store: false } },
  4629. {
  4630. id: "gpt-5.1-chat-latest",
  4631. options: {
  4632. store: false,
  4633. reasoningEffort: "medium",
  4634. reasoningSummary: "auto",
  4635. include: ["reasoning.encrypted_content"],
  4636. },
  4637. },
  4638. {
  4639. id: "gpt-5.2-chat-latest",
  4640. options: {
  4641. store: false,
  4642. reasoningEffort: "medium",
  4643. reasoningSummary: "auto",
  4644. include: ["reasoning.encrypted_content"],
  4645. },
  4646. },
  4647. {
  4648. id: "gpt-5-search-api",
  4649. options: {
  4650. store: false,
  4651. reasoningEffort: "none",
  4652. reasoningSummary: "auto",
  4653. include: ["reasoning.encrypted_content"],
  4654. },
  4655. },
  4656. ]) {
  4657. test(`${testCase.id} returns only supported small options`, () => {
  4658. expect(ProviderTransform.smallOptions(createModel(testCase.id))).toEqual(testCase.options)
  4659. })
  4660. }
  4661. })
  4662. test("ProviderTransform.smallOptions preserves the weakest OpenRouter reasoning effort", () => {
  4663. expect(
  4664. ProviderTransform.smallOptions({
  4665. providerID: "openrouter",
  4666. api: {
  4667. id: "google/gemini-3.5-flash",
  4668. npm: "@openrouter/ai-sdk-provider",
  4669. },
  4670. variants: {
  4671. low: { reasoning: { effort: "low" } },
  4672. medium: { reasoning: { effort: "medium" } },
  4673. high: { reasoning: { effort: "high" } },
  4674. },
  4675. } as any),
  4676. ).toEqual({ reasoning: { effort: "low" } })
  4677. })
  4678. describe("ProviderTransform.smallOptions - google thinking controls", () => {
  4679. const createGoogleModel = (apiId: string) => {
  4680. const model = {
  4681. id: `google/${apiId}`,
  4682. providerID: "google",
  4683. api: {
  4684. id: apiId,
  4685. url: "https://generativelanguage.googleapis.com",
  4686. npm: "@ai-sdk/google",
  4687. },
  4688. capabilities: { reasoning: true },
  4689. limit: { output: 64_000 },
  4690. } as any
  4691. model.variants = ProviderTransform.variants(model)
  4692. return model
  4693. }
  4694. for (const testCase of [
  4695. { id: "gemini-3-pro-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "low" } } },
  4696. { id: "gemini-3-flash-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } } },
  4697. {
  4698. id: "gemini-3.1-flash-image-preview",
  4699. options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } },
  4700. },
  4701. { id: "gemini-3-pro-image-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } } },
  4702. { id: "gemini-2.5-pro", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  4703. { id: "gemini-2.5-flash", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  4704. ]) {
  4705. test(`${testCase.id} returns supported small thinking options`, () => {
  4706. expect(ProviderTransform.smallOptions(createGoogleModel(testCase.id))).toEqual(testCase.options)
  4707. })
  4708. }
  4709. test("uses the first configured variant when available", () => {
  4710. expect(
  4711. ProviderTransform.smallOptions({
  4712. ...createGoogleModel("gemini-2.5-pro"),
  4713. variants: {
  4714. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } },
  4715. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32768 } },
  4716. },
  4717. }),
  4718. ).toEqual({ thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } })
  4719. })
  4720. test("does not synthesize thinking options when variants are empty", () => {
  4721. expect(ProviderTransform.smallOptions({ ...createGoogleModel("gemini-2.5-pro"), variants: {} })).toEqual({})
  4722. })
  4723. })
  4724. describe("ProviderTransform.providerOptions - ai-gateway-provider", () => {
  4725. const createModel = (overrides: Partial<any> = {}) =>
  4726. ({
  4727. id: "cloudflare-ai-gateway/openai/gpt-5.4",
  4728. providerID: "cloudflare-ai-gateway",
  4729. api: {
  4730. id: "openai/gpt-5.4",
  4731. url: "https://gateway.ai.cloudflare.com/v1/compat",
  4732. npm: "ai-gateway-provider",
  4733. },
  4734. capabilities: {
  4735. temperature: false,
  4736. reasoning: true,
  4737. attachment: true,
  4738. toolcall: true,
  4739. input: { text: true, audio: false, image: true, video: false, pdf: true },
  4740. output: { text: true, audio: false, image: false, video: false, pdf: false },
  4741. interleaved: false,
  4742. },
  4743. cost: { input: 1, output: 1, cache: { read: 0, write: 0 } },
  4744. limit: { context: 1_000_000, output: 128_000 },
  4745. status: "active",
  4746. options: {},
  4747. headers: {},
  4748. release_date: "2026-03-05",
  4749. ...overrides,
  4750. }) as any
  4751. test("routes options under openaiCompatible (the key @ai-sdk/openai-compatible reads)", () => {
  4752. // Regression: previously fell back to providerID="cloudflare-ai-gateway",
  4753. // which @ai-sdk/openai-compatible never reads, silently dropping reasoningEffort.
  4754. const result = ProviderTransform.providerOptions(createModel(), { reasoningEffort: "high" })
  4755. expect(result).toEqual({ openaiCompatible: { reasoningEffort: "high" } })
  4756. })
  4757. })