transform.test.ts 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130
  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("leaves legacy Anthropic effort options to budget fallback", () => {
  2835. expect(
  2836. ProviderTransform.reasoningVariants(
  2837. model([{ type: "effort", values: ["high"] }]),
  2838. target("@ai-sdk/anthropic", "claude-sonnet-4"),
  2839. ),
  2840. ).toBeUndefined()
  2841. })
  2842. test("uses adaptive reasoning config for Anthropic models on Bedrock", () => {
  2843. expect(
  2844. ProviderTransform.reasoningVariants(
  2845. model([{ type: "effort", values: ["high"] }]),
  2846. target("@ai-sdk/amazon-bedrock", "anthropic.claude-opus-4-7-v1:0"),
  2847. ),
  2848. ).toEqual({
  2849. high: {
  2850. reasoningConfig: {
  2851. type: "adaptive",
  2852. maxReasoningEffort: "high",
  2853. display: "summarized",
  2854. },
  2855. },
  2856. })
  2857. })
  2858. test("leaves legacy Anthropic Bedrock effort options to budget fallback", () => {
  2859. expect(
  2860. ProviderTransform.reasoningVariants(
  2861. model([{ type: "effort", values: ["high"] }]),
  2862. target("@ai-sdk/amazon-bedrock", "anthropic.claude-sonnet-4-v1:0"),
  2863. ),
  2864. ).toBeUndefined()
  2865. })
  2866. test.each([
  2867. ["@openrouter/ai-sdk-provider", { reasoning: { max_tokens: 16_000 } }],
  2868. ["@ai-sdk/anthropic", { thinking: { type: "enabled", budgetTokens: 16_000 } }],
  2869. ["@ai-sdk/google-vertex/anthropic", { thinking: { type: "enabled", budgetTokens: 16_000 } }],
  2870. ["@ai-sdk/google", { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } }],
  2871. ["@ai-sdk/google-vertex", { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } }],
  2872. ["@ai-sdk/amazon-bedrock", { reasoningConfig: { type: "enabled", budgetTokens: 16_000 } }],
  2873. ["@ai-sdk/cohere", { thinking: { type: "enabled", tokenBudget: 16_000 } }],
  2874. ["@ai-sdk/alibaba", { enableThinking: true, thinkingBudget: 16_000 }],
  2875. ])("converts token budgets for %s", (npm, high) => {
  2876. const variants = ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024 }]), target(npm))
  2877. expect(variants?.high).toEqual(high)
  2878. expect(Object.keys(variants ?? {})).toEqual(["high", "max"])
  2879. })
  2880. test("maps null effort to none", () => {
  2881. expect(
  2882. ProviderTransform.reasoningVariants(model([{ type: "effort", values: [null] }]), target("@ai-sdk/openai")),
  2883. ).toEqual({
  2884. none: {
  2885. reasoningEffort: "none",
  2886. reasoningSummary: "auto",
  2887. include: ["reasoning.encrypted_content"],
  2888. },
  2889. })
  2890. })
  2891. test.each([
  2892. ["@ai-sdk/alibaba", { none: { enableThinking: false }, high: { enableThinking: true } }],
  2893. [
  2894. "@ai-sdk/cohere",
  2895. {
  2896. none: { thinking: { type: "disabled" } },
  2897. high: { thinking: { type: "enabled" } },
  2898. },
  2899. ],
  2900. ])("converts toggle options for %s", (npm, expected) => {
  2901. expect(ProviderTransform.reasoningVariants(model([{ type: "toggle" }]), target(npm))).toEqual(expected)
  2902. })
  2903. test("combines Cohere toggle and budget options", () => {
  2904. expect(
  2905. ProviderTransform.reasoningVariants(
  2906. model([{ type: "toggle" }, { type: "budget_tokens", min: 1 }]),
  2907. target("@ai-sdk/cohere"),
  2908. ),
  2909. ).toEqual({
  2910. none: { thinking: { type: "disabled" } },
  2911. high: { thinking: { type: "enabled", tokenBudget: 16_000 } },
  2912. max: { thinking: { type: "enabled", tokenBudget: 31_999 } },
  2913. })
  2914. })
  2915. test("generates bounded high and max token budgets", () => {
  2916. expect(
  2917. ProviderTransform.reasoningVariants(
  2918. model([{ type: "budget_tokens", min: 1_024, max: 64_000 }]),
  2919. target("@ai-sdk/anthropic"),
  2920. ),
  2921. ).toEqual({
  2922. high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
  2923. max: { thinking: { type: "enabled", budgetTokens: 31_999 } },
  2924. })
  2925. })
  2926. test("caps token budgets below the model output limit", () => {
  2927. const anthropic = target("@ai-sdk/anthropic")
  2928. anthropic.limit.output = 5_000
  2929. expect(
  2930. ProviderTransform.reasoningVariants(model([{ type: "budget_tokens", min: 1_024, max: 64_000 }]), anthropic),
  2931. ).toEqual({
  2932. high: { thinking: { type: "enabled", budgetTokens: 2_500 } },
  2933. max: { thinking: { type: "enabled", budgetTokens: 4_999 } },
  2934. })
  2935. })
  2936. test("derives high and max budgets when models.dev omits max", () => {
  2937. expect(
  2938. ProviderTransform.reasoningVariants(
  2939. model([{ type: "budget_tokens", min: 1_024 }]),
  2940. target("@ai-sdk/anthropic", "claude-haiku-4-5"),
  2941. ),
  2942. ).toEqual({
  2943. high: { thinking: { type: "enabled", budgetTokens: 16_000 } },
  2944. max: { thinking: { type: "enabled", budgetTokens: 31_999 } },
  2945. })
  2946. })
  2947. test("preserves explicit inclusive budget maxima", () => {
  2948. expect(
  2949. ProviderTransform.reasoningVariants(
  2950. model([{ type: "budget_tokens", min: 1_024, max: 24_576 }]),
  2951. target("@ai-sdk/google", "gemini-2.5-pro"),
  2952. ),
  2953. ).toEqual({
  2954. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 12_288 } },
  2955. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
  2956. })
  2957. })
  2958. test("prefers effort options over token budgets", () => {
  2959. expect(
  2960. ProviderTransform.reasoningVariants(
  2961. model([
  2962. { type: "budget_tokens", min: 1_024, max: 64_000 },
  2963. { type: "effort", values: ["low"] },
  2964. ]),
  2965. target("@ai-sdk/openai"),
  2966. ),
  2967. ).toEqual({
  2968. low: {
  2969. reasoningEffort: "low",
  2970. reasoningSummary: "auto",
  2971. include: ["reasoning.encrypted_content"],
  2972. },
  2973. })
  2974. })
  2975. test("leaves unsupported options for heuristic fallback", () => {
  2976. expect(
  2977. ProviderTransform.reasoningVariants(model([{ type: "effort", values: ["high"] }]), target("@ai-sdk/perplexity")),
  2978. ).toBeUndefined()
  2979. expect(ProviderTransform.reasoningVariants(model([{ type: "toggle" }]), target("@ai-sdk/openai"))).toBeUndefined()
  2980. })
  2981. test("uses model-family options for gateway and GitHub Copilot", () => {
  2982. const effort = model([{ type: "effort", values: ["high"] }])
  2983. expect(ProviderTransform.reasoningVariants(effort, target("@ai-sdk/gateway", "anthropic/claude-sonnet-4"))).toEqual(
  2984. {
  2985. high: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  2986. },
  2987. )
  2988. expect(ProviderTransform.reasoningVariants(effort, target("@ai-sdk/gateway", "google/gemini-3-pro"))).toEqual({
  2989. high: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  2990. })
  2991. expect(
  2992. ProviderTransform.reasoningVariants(effort, target("@ai-sdk/github-copilot", "gemini-3-pro")),
  2993. ).toBeUndefined()
  2994. })
  2995. test.each(["@ai-sdk/cohere", "@ai-sdk/perplexity", "@ai-sdk/vercel", "@ai-sdk/alibaba", "gitlab-ai-provider"])(
  2996. "does not invent effort controls for %s",
  2997. (npm) => {
  2998. expect(
  2999. ProviderTransform.reasoningVariants(model([{ type: "effort", values: ["high"] }]), target(npm)),
  3000. ).toBeUndefined()
  3001. },
  3002. )
  3003. })
  3004. describe("ProviderTransform.variants", () => {
  3005. const createMockModel = (overrides: Partial<any> = {}): any => ({
  3006. id: "test/test-model",
  3007. providerID: "test",
  3008. api: {
  3009. id: "test-model",
  3010. url: "https://api.test.com",
  3011. npm: "@ai-sdk/openai",
  3012. },
  3013. name: "Test Model",
  3014. capabilities: {
  3015. temperature: true,
  3016. reasoning: true,
  3017. attachment: true,
  3018. toolcall: true,
  3019. input: { text: true, audio: false, image: true, video: false, pdf: false },
  3020. output: { text: true, audio: false, image: false, video: false, pdf: false },
  3021. interleaved: false,
  3022. },
  3023. cost: {
  3024. input: 0.001,
  3025. output: 0.002,
  3026. cache: { read: 0.0001, write: 0.0002 },
  3027. },
  3028. limit: {
  3029. context: 200_000,
  3030. output: 64_000,
  3031. },
  3032. status: "active",
  3033. options: {},
  3034. headers: {},
  3035. release_date: "2024-01-01",
  3036. ...overrides,
  3037. })
  3038. test("returns empty object when model has no reasoning capabilities", () => {
  3039. const model = createMockModel({
  3040. capabilities: { reasoning: false },
  3041. })
  3042. const result = ProviderTransform.variants(model)
  3043. expect(result).toEqual({})
  3044. })
  3045. test("deepseek returns empty object", () => {
  3046. const model = createMockModel({
  3047. id: "deepseek/deepseek-chat",
  3048. providerID: "deepseek",
  3049. api: {
  3050. id: "deepseek-chat",
  3051. url: "https://api.deepseek.com",
  3052. npm: "@ai-sdk/openai-compatible",
  3053. },
  3054. })
  3055. const result = ProviderTransform.variants(model)
  3056. expect(result).toEqual({})
  3057. })
  3058. test("minimax returns empty object", () => {
  3059. const model = createMockModel({
  3060. id: "minimax/minimax-model",
  3061. providerID: "minimax",
  3062. api: {
  3063. id: "minimax-model",
  3064. url: "https://api.minimax.com",
  3065. npm: "@ai-sdk/openai-compatible",
  3066. },
  3067. })
  3068. const result = ProviderTransform.variants(model)
  3069. expect(result).toEqual({})
  3070. })
  3071. test("minimax m3 using anthropic returns thinking toggles", () => {
  3072. const model = createMockModel({
  3073. id: "minimax/minimax-m3",
  3074. providerID: "minimax",
  3075. api: {
  3076. id: "MiniMax-M3",
  3077. url: "https://api.minimax.com/anthropic/v1",
  3078. npm: "@ai-sdk/anthropic",
  3079. },
  3080. })
  3081. const result = ProviderTransform.variants(model)
  3082. expect(result).toEqual({
  3083. none: { thinking: { type: "disabled" } },
  3084. thinking: { thinking: { type: "adaptive" } },
  3085. })
  3086. })
  3087. test("minimax m3 using openai-compatible returns thinking toggles", () => {
  3088. const model = createMockModel({
  3089. id: "minimax/minimax-m3",
  3090. providerID: "minimax",
  3091. api: {
  3092. id: "minimax-m3",
  3093. url: "https://api.minimax.com/v1",
  3094. npm: "@ai-sdk/openai-compatible",
  3095. },
  3096. })
  3097. expect(ProviderTransform.variants(model)).toEqual({
  3098. none: { thinking: { type: "disabled" } },
  3099. thinking: { thinking: { type: "adaptive" } },
  3100. })
  3101. })
  3102. test("glm returns empty object", () => {
  3103. const model = createMockModel({
  3104. id: "glm/glm-4",
  3105. providerID: "glm",
  3106. api: {
  3107. id: "glm-4",
  3108. url: "https://api.glm.com",
  3109. npm: "@ai-sdk/openai-compatible",
  3110. },
  3111. })
  3112. const result = ProviderTransform.variants(model)
  3113. expect(result).toEqual({})
  3114. })
  3115. test("glm-5.2 returns native effort variants for openai-compatible providers", () => {
  3116. const model = createMockModel({
  3117. id: "zhipuai/glm-5.2",
  3118. providerID: "zhipuai",
  3119. api: {
  3120. id: "glm-5.2",
  3121. url: "https://open.bigmodel.cn/api/paas/v4",
  3122. npm: "@ai-sdk/openai-compatible",
  3123. },
  3124. })
  3125. expect(ProviderTransform.variants(model)).toEqual({
  3126. high: { reasoningEffort: "high" },
  3127. max: { reasoningEffort: "max" },
  3128. })
  3129. })
  3130. test("recognizes GLM-5.2 provider model IDs", () => {
  3131. for (const id of ["accounts/fireworks/models/glm-5p2", "zai-org-glm-5-2", "umans-glm-5.2"]) {
  3132. const model = createMockModel({
  3133. id: `test/${id}`,
  3134. api: {
  3135. id,
  3136. url: "https://api.test.com",
  3137. npm: "@ai-sdk/openai-compatible",
  3138. },
  3139. })
  3140. expect(ProviderTransform.variants(model)).toEqual({
  3141. high: { reasoningEffort: "high" },
  3142. max: { reasoningEffort: "max" },
  3143. })
  3144. }
  3145. })
  3146. test("recognizes GLM-5.2 from the API ID when the configured model ID is an alias", () => {
  3147. const model = createMockModel({
  3148. id: "custom/my-glm",
  3149. api: {
  3150. id: "accounts/fireworks/models/glm-5p2",
  3151. url: "https://api.fireworks.ai/inference/v1",
  3152. npm: "@ai-sdk/openai-compatible",
  3153. },
  3154. })
  3155. expect(ProviderTransform.variants(model)).toEqual({
  3156. high: { reasoningEffort: "high" },
  3157. max: { reasoningEffort: "max" },
  3158. })
  3159. })
  3160. test("glm-5.2 returns openrouter effort variants for openrouter", () => {
  3161. const model = createMockModel({
  3162. id: "openrouter/z-ai/glm-5.2",
  3163. providerID: "openrouter",
  3164. api: {
  3165. id: "z-ai/glm-5.2",
  3166. url: "https://openrouter.ai/api/v1",
  3167. npm: "@openrouter/ai-sdk-provider",
  3168. },
  3169. })
  3170. expect(ProviderTransform.variants(model)).toEqual({
  3171. high: { reasoning: { effort: "high" } },
  3172. xhigh: { reasoning: { effort: "xhigh" } },
  3173. })
  3174. })
  3175. test("glm-5.2 returns effort variants for anthropic-compatible providers", () => {
  3176. const model = createMockModel({
  3177. id: "zai-coding-plan/glm-5.2",
  3178. providerID: "zai-coding-plan",
  3179. api: {
  3180. id: "glm-5.2",
  3181. url: "https://api.z.ai/api/anthropic",
  3182. npm: "@ai-sdk/anthropic",
  3183. },
  3184. })
  3185. expect(ProviderTransform.variants(model)).toEqual({
  3186. high: { effort: "high" },
  3187. max: { effort: "max" },
  3188. })
  3189. })
  3190. test("glm-5.2 falls back to provider defaults for other packages", () => {
  3191. const model = createMockModel({
  3192. id: "test/glm-5.2",
  3193. api: {
  3194. id: "glm-5.2",
  3195. url: "https://api.test.com",
  3196. npm: "@ai-sdk/amazon-bedrock",
  3197. },
  3198. })
  3199. expect(ProviderTransform.variants(model)).toEqual({
  3200. low: { reasoningConfig: { type: "enabled", maxReasoningEffort: "low" } },
  3201. medium: { reasoningConfig: { type: "enabled", maxReasoningEffort: "medium" } },
  3202. high: { reasoningConfig: { type: "enabled", maxReasoningEffort: "high" } },
  3203. })
  3204. })
  3205. test("mistral models with reasoning support return variants", () => {
  3206. const model = createMockModel({
  3207. id: "mistral/mistral-small-latest",
  3208. providerID: "mistral",
  3209. api: {
  3210. id: "mistral-small-latest",
  3211. url: "https://api.mistral.com",
  3212. npm: "@ai-sdk/mistral",
  3213. },
  3214. capabilities: { reasoning: true },
  3215. })
  3216. const result = ProviderTransform.variants(model)
  3217. expect(result).toEqual({
  3218. high: { reasoningEffort: "high" },
  3219. })
  3220. })
  3221. test("mistral-medium-3.5 with reasoning returns variants", () => {
  3222. const model = createMockModel({
  3223. id: "mistral/mistral-medium-3.5",
  3224. providerID: "mistral",
  3225. api: {
  3226. id: "mistral-medium-3.5",
  3227. url: "https://api.mistral.com",
  3228. npm: "@ai-sdk/mistral",
  3229. },
  3230. capabilities: { reasoning: true },
  3231. })
  3232. const result = ProviderTransform.variants(model)
  3233. expect(result).toEqual({
  3234. high: { reasoningEffort: "high" },
  3235. })
  3236. })
  3237. test("mistral without reasoning returns empty object", () => {
  3238. const model = createMockModel({
  3239. id: "mistral/mistral-large",
  3240. providerID: "mistral",
  3241. api: {
  3242. id: "mistral-large-latest",
  3243. url: "https://api.mistral.com",
  3244. npm: "@ai-sdk/mistral",
  3245. },
  3246. capabilities: { reasoning: false },
  3247. })
  3248. const result = ProviderTransform.variants(model)
  3249. expect(result).toEqual({})
  3250. })
  3251. test("mistral large with reasoning returns empty object (only small supports reasoning)", () => {
  3252. const model = createMockModel({
  3253. id: "mistral/mistral-large",
  3254. providerID: "mistral",
  3255. api: {
  3256. id: "mistral-large-latest",
  3257. url: "https://api.mistral.com",
  3258. npm: "@ai-sdk/mistral",
  3259. },
  3260. capabilities: { reasoning: true },
  3261. })
  3262. const result = ProviderTransform.variants(model)
  3263. expect(result).toEqual({})
  3264. })
  3265. describe("@openrouter/ai-sdk-provider", () => {
  3266. test("returns widely supported efforts for other reasoning models", () => {
  3267. const model = createMockModel({
  3268. id: "openrouter/test-model",
  3269. providerID: "openrouter",
  3270. api: {
  3271. id: "test-model",
  3272. url: "https://openrouter.ai",
  3273. npm: "@openrouter/ai-sdk-provider",
  3274. },
  3275. })
  3276. const result = ProviderTransform.variants(model)
  3277. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3278. expect(result.medium).toEqual({ reasoning: { effort: "medium" } })
  3279. })
  3280. test("gpt models return OPENAI_EFFORTS with reasoning", () => {
  3281. const model = createMockModel({
  3282. id: "openrouter/gpt-4",
  3283. providerID: "openrouter",
  3284. api: {
  3285. id: "gpt-4",
  3286. url: "https://openrouter.ai",
  3287. npm: "@openrouter/ai-sdk-provider",
  3288. },
  3289. })
  3290. const result = ProviderTransform.variants(model)
  3291. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  3292. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  3293. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  3294. })
  3295. for (const testCase of [
  3296. { id: "openai/o3-mini", efforts: ["none", "minimal", "low", "medium", "high", "xhigh"] },
  3297. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3298. { id: "openai/gpt-5-pro", efforts: ["high"] },
  3299. { id: "openai/gpt-5.5-pro", efforts: ["medium", "high", "xhigh"] },
  3300. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  3301. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3302. { id: "openai/gpt-5.3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3303. { id: "openai/gpt-5-chat-latest", efforts: [] },
  3304. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  3305. ]) {
  3306. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  3307. const result = ProviderTransform.variants(
  3308. createMockModel({
  3309. id: testCase.id,
  3310. providerID: "openrouter",
  3311. api: {
  3312. id: testCase.id,
  3313. url: "https://openrouter.ai",
  3314. npm: "@openrouter/ai-sdk-provider",
  3315. },
  3316. }),
  3317. )
  3318. expect(Object.keys(result)).toEqual(testCase.efforts)
  3319. })
  3320. }
  3321. test("gemini-3 returns widely supported efforts with reasoning", () => {
  3322. const model = createMockModel({
  3323. id: "openrouter/gemini-3-5-pro",
  3324. providerID: "openrouter",
  3325. api: {
  3326. id: "gemini-3-5-pro",
  3327. url: "https://openrouter.ai",
  3328. npm: "@openrouter/ai-sdk-provider",
  3329. },
  3330. })
  3331. const result = ProviderTransform.variants(model)
  3332. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3333. })
  3334. test("grok-4 uses the provider's standard efforts", () => {
  3335. const model = createMockModel({
  3336. id: "openrouter/grok-4",
  3337. providerID: "openrouter",
  3338. api: {
  3339. id: "grok-4",
  3340. url: "https://openrouter.ai",
  3341. npm: "@openrouter/ai-sdk-provider",
  3342. },
  3343. })
  3344. const result = ProviderTransform.variants(model)
  3345. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3346. expect(result.medium).toEqual({ reasoning: { effort: "medium" } })
  3347. })
  3348. test("grok-3-mini returns low and high with reasoning", () => {
  3349. const model = createMockModel({
  3350. id: "openrouter/grok-3-mini",
  3351. providerID: "openrouter",
  3352. api: {
  3353. id: "grok-3-mini",
  3354. url: "https://openrouter.ai",
  3355. npm: "@openrouter/ai-sdk-provider",
  3356. },
  3357. })
  3358. const result = ProviderTransform.variants(model)
  3359. expect(Object.keys(result)).toEqual(["low", "high"])
  3360. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  3361. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  3362. })
  3363. })
  3364. describe("@ai-sdk/gateway", () => {
  3365. test("configured anthropic aliases route by the API ID", () => {
  3366. const model = createMockModel({
  3367. id: "my-claude",
  3368. providerID: "gateway",
  3369. api: {
  3370. id: "anthropic/claude-sonnet-4-6",
  3371. url: "https://gateway.ai",
  3372. npm: "@ai-sdk/gateway",
  3373. },
  3374. })
  3375. const result = ProviderTransform.variants(model)
  3376. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3377. expect(result.high).toEqual({
  3378. thinking: {
  3379. type: "adaptive",
  3380. },
  3381. effort: "high",
  3382. })
  3383. })
  3384. test("configured google aliases route by the API ID", () => {
  3385. const model = createMockModel({
  3386. id: "my-gemini",
  3387. providerID: "gateway",
  3388. api: {
  3389. id: "google/gemini-2.5-pro",
  3390. url: "https://gateway.ai",
  3391. npm: "@ai-sdk/gateway",
  3392. },
  3393. })
  3394. expect(ProviderTransform.variants(model)).toEqual({
  3395. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  3396. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } },
  3397. })
  3398. })
  3399. test("anthropic sonnet 4.6 models return adaptive thinking options", () => {
  3400. const model = createMockModel({
  3401. id: "anthropic/claude-sonnet-4-6",
  3402. providerID: "gateway",
  3403. api: {
  3404. id: "anthropic/claude-sonnet-4-6",
  3405. url: "https://gateway.ai",
  3406. npm: "@ai-sdk/gateway",
  3407. },
  3408. })
  3409. const result = ProviderTransform.variants(model)
  3410. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3411. expect(result.medium).toEqual({
  3412. thinking: {
  3413. type: "adaptive",
  3414. },
  3415. effort: "medium",
  3416. })
  3417. })
  3418. test("anthropic sonnet 4.6 dot-format models return adaptive thinking options", () => {
  3419. const model = createMockModel({
  3420. id: "anthropic/claude-sonnet-4-6",
  3421. providerID: "gateway",
  3422. api: {
  3423. id: "anthropic/claude-sonnet-4.6",
  3424. url: "https://gateway.ai",
  3425. npm: "@ai-sdk/gateway",
  3426. },
  3427. })
  3428. const result = ProviderTransform.variants(model)
  3429. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3430. expect(result.medium).toEqual({
  3431. thinking: {
  3432. type: "adaptive",
  3433. },
  3434. effort: "medium",
  3435. })
  3436. })
  3437. test("anthropic opus 4.6 dot-format models return adaptive thinking options", () => {
  3438. const model = createMockModel({
  3439. id: "anthropic/claude-opus-4-6",
  3440. providerID: "gateway",
  3441. api: {
  3442. id: "anthropic/claude-opus-4.6",
  3443. url: "https://gateway.ai",
  3444. npm: "@ai-sdk/gateway",
  3445. },
  3446. })
  3447. const result = ProviderTransform.variants(model)
  3448. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3449. expect(result.high).toEqual({
  3450. thinking: {
  3451. type: "adaptive",
  3452. },
  3453. effort: "high",
  3454. })
  3455. })
  3456. test("anthropic opus 4.7 models return adaptive thinking options with xhigh", () => {
  3457. const model = createMockModel({
  3458. id: "anthropic/claude-opus-4-7",
  3459. providerID: "gateway",
  3460. api: {
  3461. id: "anthropic/claude-opus-4-7",
  3462. url: "https://gateway.ai",
  3463. npm: "@ai-sdk/gateway",
  3464. },
  3465. })
  3466. const result = ProviderTransform.variants(model)
  3467. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3468. expect(result.xhigh).toEqual({
  3469. thinking: {
  3470. type: "adaptive",
  3471. display: "summarized",
  3472. },
  3473. effort: "xhigh",
  3474. })
  3475. expect(result.max).toEqual({
  3476. thinking: {
  3477. type: "adaptive",
  3478. display: "summarized",
  3479. },
  3480. effort: "max",
  3481. })
  3482. })
  3483. test("anthropic opus 4.7 dot-format models return adaptive thinking options with xhigh", () => {
  3484. const model = createMockModel({
  3485. id: "anthropic/claude-opus-4-7",
  3486. providerID: "gateway",
  3487. api: {
  3488. id: "anthropic/claude-opus-4.7",
  3489. url: "https://gateway.ai",
  3490. npm: "@ai-sdk/gateway",
  3491. },
  3492. })
  3493. const result = ProviderTransform.variants(model)
  3494. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3495. })
  3496. test("anthropic opus 4.8 forces display summarized for adaptive reasoning", () => {
  3497. const model = createMockModel({
  3498. id: "anthropic/claude-opus-4-8",
  3499. providerID: "gateway",
  3500. api: {
  3501. id: "anthropic/claude-opus-4-8",
  3502. url: "https://gateway.ai",
  3503. npm: "@ai-sdk/gateway",
  3504. },
  3505. })
  3506. const result = ProviderTransform.variants(model)
  3507. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3508. expect(result.high).toEqual({
  3509. thinking: {
  3510. type: "adaptive",
  3511. display: "summarized",
  3512. },
  3513. effort: "high",
  3514. })
  3515. })
  3516. test("anthropic sonnet 5 returns adaptive thinking options with xhigh", () => {
  3517. const model = createMockModel({
  3518. id: "anthropic/claude-sonnet-5",
  3519. providerID: "gateway",
  3520. api: {
  3521. id: "anthropic/claude-sonnet-5",
  3522. url: "https://gateway.ai",
  3523. npm: "@ai-sdk/gateway",
  3524. },
  3525. })
  3526. const result = ProviderTransform.variants(model)
  3527. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3528. expect(result.high).toEqual({
  3529. thinking: {
  3530. type: "adaptive",
  3531. display: "summarized",
  3532. },
  3533. effort: "high",
  3534. })
  3535. })
  3536. test("anthropic opus 4.6 omits display so it keeps the summarized default", () => {
  3537. const model = createMockModel({
  3538. id: "anthropic/claude-opus-4-6",
  3539. providerID: "gateway",
  3540. api: {
  3541. id: "anthropic/claude-opus-4-6",
  3542. url: "https://gateway.ai",
  3543. npm: "@ai-sdk/gateway",
  3544. },
  3545. })
  3546. const result = ProviderTransform.variants(model)
  3547. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3548. expect(result.high).toEqual({
  3549. thinking: {
  3550. type: "adaptive",
  3551. },
  3552. effort: "high",
  3553. })
  3554. })
  3555. test("anthropic models return anthropic thinking options", () => {
  3556. const model = createMockModel({
  3557. id: "anthropic/claude-sonnet-4",
  3558. providerID: "gateway",
  3559. api: {
  3560. id: "anthropic/claude-sonnet-4",
  3561. url: "https://gateway.ai",
  3562. npm: "@ai-sdk/gateway",
  3563. },
  3564. })
  3565. const result = ProviderTransform.variants(model)
  3566. expect(Object.keys(result)).toEqual(["high", "max"])
  3567. expect(result.high).toEqual({
  3568. thinking: {
  3569. type: "enabled",
  3570. budgetTokens: 16000,
  3571. },
  3572. })
  3573. expect(result.max).toEqual({
  3574. thinking: {
  3575. type: "enabled",
  3576. budgetTokens: 31999,
  3577. },
  3578. })
  3579. })
  3580. test("returns OPENAI_EFFORTS with reasoningEffort", () => {
  3581. const model = createMockModel({
  3582. id: "gateway/gateway-model",
  3583. providerID: "gateway",
  3584. api: {
  3585. id: "gateway-model",
  3586. url: "https://gateway.ai",
  3587. npm: "@ai-sdk/gateway",
  3588. },
  3589. })
  3590. const result = ProviderTransform.variants(model)
  3591. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  3592. expect(result.low).toEqual({ reasoningEffort: "low" })
  3593. expect(result.high).toEqual({ reasoningEffort: "high" })
  3594. })
  3595. for (const testCase of [
  3596. { id: "openai/gpt-5-5", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3597. { id: "openai/gpt-5-pro", efforts: ["high"] },
  3598. { id: "openai/gpt-5-5-pro", efforts: ["medium", "high", "xhigh"] },
  3599. { id: "openai/gpt-5-2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  3600. { id: "openai/gpt-5-3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3601. { id: "openai/gpt-5-3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3602. { id: "openai/gpt-5-chat-latest", efforts: [] },
  3603. { id: "openai/gpt-5-2-chat-latest", efforts: ["medium"] },
  3604. ]) {
  3605. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  3606. const result = ProviderTransform.variants(
  3607. createMockModel({
  3608. id: testCase.id,
  3609. providerID: "gateway",
  3610. api: {
  3611. id: testCase.id,
  3612. url: "https://gateway.ai",
  3613. npm: "@ai-sdk/gateway",
  3614. },
  3615. }),
  3616. )
  3617. expect(Object.keys(result)).toEqual(testCase.efforts)
  3618. })
  3619. }
  3620. })
  3621. describe("@ai-sdk/github-copilot", () => {
  3622. test("standard models return low, medium, high", () => {
  3623. const model = createMockModel({
  3624. id: "gpt-4.5",
  3625. providerID: "github-copilot",
  3626. api: {
  3627. id: "gpt-4.5",
  3628. url: "https://api.githubcopilot.com",
  3629. npm: "@ai-sdk/github-copilot",
  3630. },
  3631. })
  3632. const result = ProviderTransform.variants(model)
  3633. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3634. expect(result.low).toEqual({
  3635. reasoningEffort: "low",
  3636. reasoningSummary: "auto",
  3637. include: ["reasoning.encrypted_content"],
  3638. })
  3639. })
  3640. test("gpt-5.1-codex-max includes xhigh", () => {
  3641. const model = createMockModel({
  3642. id: "gpt-5.1-codex-max",
  3643. providerID: "github-copilot",
  3644. api: {
  3645. id: "gpt-5.1-codex-max",
  3646. url: "https://api.githubcopilot.com",
  3647. npm: "@ai-sdk/github-copilot",
  3648. },
  3649. })
  3650. const result = ProviderTransform.variants(model)
  3651. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3652. })
  3653. test("gpt-5.1-codex-mini does not include xhigh", () => {
  3654. const model = createMockModel({
  3655. id: "gpt-5.1-codex-mini",
  3656. providerID: "github-copilot",
  3657. api: {
  3658. id: "gpt-5.1-codex-mini",
  3659. url: "https://api.githubcopilot.com",
  3660. npm: "@ai-sdk/github-copilot",
  3661. },
  3662. })
  3663. const result = ProviderTransform.variants(model)
  3664. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3665. })
  3666. test("gpt-5.1-codex does not include xhigh", () => {
  3667. const model = createMockModel({
  3668. id: "gpt-5.1-codex",
  3669. providerID: "github-copilot",
  3670. api: {
  3671. id: "gpt-5.1-codex",
  3672. url: "https://api.githubcopilot.com",
  3673. npm: "@ai-sdk/github-copilot",
  3674. },
  3675. })
  3676. const result = ProviderTransform.variants(model)
  3677. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3678. })
  3679. test("gpt-5.2 includes xhigh", () => {
  3680. const model = createMockModel({
  3681. id: "gpt-5.2",
  3682. providerID: "github-copilot",
  3683. api: {
  3684. id: "gpt-5.2",
  3685. url: "https://api.githubcopilot.com",
  3686. npm: "@ai-sdk/github-copilot",
  3687. },
  3688. })
  3689. const result = ProviderTransform.variants(model)
  3690. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3691. expect(result.xhigh).toEqual({
  3692. reasoningEffort: "xhigh",
  3693. reasoningSummary: "auto",
  3694. include: ["reasoning.encrypted_content"],
  3695. })
  3696. })
  3697. test("gpt-5.2-codex includes xhigh", () => {
  3698. const model = createMockModel({
  3699. id: "gpt-5.2-codex",
  3700. providerID: "github-copilot",
  3701. api: {
  3702. id: "gpt-5.2-codex",
  3703. url: "https://api.githubcopilot.com",
  3704. npm: "@ai-sdk/github-copilot",
  3705. },
  3706. })
  3707. const result = ProviderTransform.variants(model)
  3708. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3709. })
  3710. test("gpt-5.3-codex includes xhigh", () => {
  3711. const model = createMockModel({
  3712. id: "gpt-5.3-codex",
  3713. providerID: "github-copilot",
  3714. api: {
  3715. id: "gpt-5.3-codex",
  3716. url: "https://api.githubcopilot.com",
  3717. npm: "@ai-sdk/github-copilot",
  3718. },
  3719. })
  3720. const result = ProviderTransform.variants(model)
  3721. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3722. })
  3723. test("gpt-5.4 includes xhigh", () => {
  3724. const model = createMockModel({
  3725. id: "gpt-5.4",
  3726. release_date: "2026-03-05",
  3727. providerID: "github-copilot",
  3728. api: {
  3729. id: "gpt-5.4",
  3730. url: "https://api.githubcopilot.com",
  3731. npm: "@ai-sdk/github-copilot",
  3732. },
  3733. })
  3734. const result = ProviderTransform.variants(model)
  3735. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  3736. })
  3737. })
  3738. describe("@ai-sdk/cerebras", () => {
  3739. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3740. const model = createMockModel({
  3741. id: "cerebras/llama-4",
  3742. providerID: "cerebras",
  3743. api: {
  3744. id: "llama-4-sc",
  3745. url: "https://api.cerebras.ai",
  3746. npm: "@ai-sdk/cerebras",
  3747. },
  3748. })
  3749. const result = ProviderTransform.variants(model)
  3750. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3751. expect(result.low).toEqual({ reasoningEffort: "low" })
  3752. expect(result.high).toEqual({ reasoningEffort: "high" })
  3753. })
  3754. })
  3755. describe("@ai-sdk/togetherai", () => {
  3756. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3757. const model = createMockModel({
  3758. id: "togetherai/llama-4",
  3759. providerID: "togetherai",
  3760. api: {
  3761. id: "llama-4-sc",
  3762. url: "https://api.togetherai.com",
  3763. npm: "@ai-sdk/togetherai",
  3764. },
  3765. })
  3766. const result = ProviderTransform.variants(model)
  3767. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3768. expect(result.low).toEqual({ reasoningEffort: "low" })
  3769. expect(result.high).toEqual({ reasoningEffort: "high" })
  3770. })
  3771. })
  3772. describe("@ai-sdk/xai", () => {
  3773. test("grok-4.5 uses standard reasoning efforts", () => {
  3774. const model = createMockModel({
  3775. id: "xai/grok-4.5",
  3776. providerID: "xai",
  3777. api: {
  3778. id: "grok-4.5",
  3779. url: "https://api.x.ai",
  3780. npm: "@ai-sdk/xai",
  3781. },
  3782. })
  3783. const result = ProviderTransform.variants(model)
  3784. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3785. expect(result.medium).toEqual({ reasoningEffort: "medium" })
  3786. })
  3787. test("grok-3-mini returns low and high with reasoningEffort", () => {
  3788. const model = createMockModel({
  3789. id: "xai/grok-3-mini",
  3790. providerID: "xai",
  3791. api: {
  3792. id: "grok-3-mini",
  3793. url: "https://api.x.ai",
  3794. npm: "@ai-sdk/xai",
  3795. },
  3796. })
  3797. const result = ProviderTransform.variants(model)
  3798. expect(Object.keys(result)).toEqual(["low", "high"])
  3799. expect(result.low).toEqual({ reasoningEffort: "low" })
  3800. expect(result.high).toEqual({ reasoningEffort: "high" })
  3801. })
  3802. })
  3803. describe("@ai-sdk/deepinfra", () => {
  3804. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3805. const model = createMockModel({
  3806. id: "deepinfra/llama-4",
  3807. providerID: "deepinfra",
  3808. api: {
  3809. id: "llama-4-sc",
  3810. url: "https://api.deepinfra.com",
  3811. npm: "@ai-sdk/deepinfra",
  3812. },
  3813. })
  3814. const result = ProviderTransform.variants(model)
  3815. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3816. expect(result.low).toEqual({ reasoningEffort: "low" })
  3817. expect(result.high).toEqual({ reasoningEffort: "high" })
  3818. })
  3819. })
  3820. describe("@ai-sdk/openai-compatible", () => {
  3821. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  3822. const model = createMockModel({
  3823. id: "custom-provider/custom-model",
  3824. providerID: "custom-provider",
  3825. api: {
  3826. id: "custom-model",
  3827. url: "https://api.custom.com",
  3828. npm: "@ai-sdk/openai-compatible",
  3829. },
  3830. })
  3831. const result = ProviderTransform.variants(model)
  3832. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3833. expect(result.low).toEqual({ reasoningEffort: "low" })
  3834. expect(result.high).toEqual({ reasoningEffort: "high" })
  3835. })
  3836. test("north-mini-code-1-0 returns only none and high", () => {
  3837. const model = createMockModel({
  3838. id: "cohere/north-mini-code-1-0",
  3839. providerID: "cohere",
  3840. api: {
  3841. id: "North-Mini-Code-1-0-latest",
  3842. url: "https://api.cohere.com/compatibility/v1",
  3843. npm: "@ai-sdk/openai-compatible",
  3844. },
  3845. })
  3846. const result = ProviderTransform.variants(model)
  3847. expect(result).toEqual({
  3848. none: { reasoningEffort: "none" },
  3849. high: { reasoningEffort: "high" },
  3850. })
  3851. })
  3852. })
  3853. describe("@ai-sdk/azure", () => {
  3854. test("o1-mini returns empty object", () => {
  3855. const model = createMockModel({
  3856. id: "o1-mini",
  3857. providerID: "azure",
  3858. api: {
  3859. id: "o1-mini",
  3860. url: "https://azure.com",
  3861. npm: "@ai-sdk/azure",
  3862. },
  3863. })
  3864. const result = ProviderTransform.variants(model)
  3865. expect(result).toEqual({})
  3866. })
  3867. test("standard azure models return custom efforts with reasoningSummary", () => {
  3868. const model = createMockModel({
  3869. id: "o1",
  3870. providerID: "azure",
  3871. api: {
  3872. id: "o1",
  3873. url: "https://azure.com",
  3874. npm: "@ai-sdk/azure",
  3875. },
  3876. })
  3877. const result = ProviderTransform.variants(model)
  3878. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3879. expect(result.low).toEqual({
  3880. reasoningEffort: "low",
  3881. reasoningSummary: "auto",
  3882. include: ["reasoning.encrypted_content"],
  3883. })
  3884. })
  3885. test("gpt-5 adds minimal effort", () => {
  3886. const model = createMockModel({
  3887. id: "gpt-5",
  3888. providerID: "azure",
  3889. api: {
  3890. id: "gpt-5",
  3891. url: "https://azure.com",
  3892. npm: "@ai-sdk/azure",
  3893. },
  3894. })
  3895. const result = ProviderTransform.variants(model)
  3896. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  3897. })
  3898. for (const testCase of [
  3899. { id: "gpt-5-1", efforts: ["none", "low", "medium", "high"] },
  3900. { id: "gpt-5-4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3901. { id: "gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3902. { id: "gpt-5-5", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3903. ]) {
  3904. test(`${testCase.id} returns supported Azure reasoning efforts`, () => {
  3905. const result = ProviderTransform.variants(
  3906. createMockModel({
  3907. id: testCase.id,
  3908. providerID: "azure",
  3909. api: {
  3910. id: testCase.id,
  3911. url: "https://azure.com",
  3912. npm: "@ai-sdk/azure",
  3913. },
  3914. }),
  3915. )
  3916. expect(Object.keys(result)).toEqual(testCase.efforts)
  3917. })
  3918. }
  3919. })
  3920. describe("@ai-sdk/openai", () => {
  3921. test("gpt-5-pro returns only high effort", () => {
  3922. const model = createMockModel({
  3923. id: "gpt-5-pro",
  3924. providerID: "openai",
  3925. api: {
  3926. id: "gpt-5-pro",
  3927. url: "https://api.openai.com",
  3928. npm: "@ai-sdk/openai",
  3929. },
  3930. })
  3931. const result = ProviderTransform.variants(model)
  3932. expect(Object.keys(result)).toEqual(["high"])
  3933. })
  3934. test("standard openai models return custom efforts with reasoningSummary", () => {
  3935. const model = createMockModel({
  3936. id: "gpt-5",
  3937. providerID: "openai",
  3938. api: {
  3939. id: "gpt-5",
  3940. url: "https://api.openai.com",
  3941. npm: "@ai-sdk/openai",
  3942. },
  3943. release_date: "2024-06-01",
  3944. })
  3945. const result = ProviderTransform.variants(model)
  3946. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  3947. expect(result.low).toEqual({
  3948. reasoningEffort: "low",
  3949. reasoningSummary: "auto",
  3950. include: ["reasoning.encrypted_content"],
  3951. })
  3952. })
  3953. test("models after 2025-11-13 include 'none' effort", () => {
  3954. const model = createMockModel({
  3955. id: "gpt-5-nano",
  3956. providerID: "openai",
  3957. api: {
  3958. id: "gpt-5-nano",
  3959. url: "https://api.openai.com",
  3960. npm: "@ai-sdk/openai",
  3961. },
  3962. release_date: "2025-11-14",
  3963. })
  3964. const result = ProviderTransform.variants(model)
  3965. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high"])
  3966. })
  3967. test("models after 2025-12-04 include 'xhigh' effort", () => {
  3968. const model = createMockModel({
  3969. id: "openai/gpt-5-reasoning",
  3970. providerID: "openai",
  3971. api: {
  3972. id: "gpt-5-reasoning",
  3973. url: "https://api.openai.com",
  3974. npm: "@ai-sdk/openai",
  3975. },
  3976. release_date: "2025-12-05",
  3977. })
  3978. const result = ProviderTransform.variants(model)
  3979. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  3980. })
  3981. for (const testCase of [
  3982. { id: "o1", releaseDate: "2024-12-17", efforts: ["low", "medium", "high"] },
  3983. { id: "o1-pro", releaseDate: "2025-03-19", efforts: ["low", "medium", "high"] },
  3984. { id: "o3", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  3985. { id: "o3-mini", releaseDate: "2025-01-31", efforts: ["low", "medium", "high"] },
  3986. { id: "o3-pro", releaseDate: "2025-06-10", efforts: ["low", "medium", "high"] },
  3987. { id: "o4-mini", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  3988. { id: "o3-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  3989. { id: "o4-mini-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  3990. { id: "gpt-5.1", releaseDate: "2025-11-13", efforts: ["none", "low", "medium", "high"] },
  3991. { id: "gpt-5.4", releaseDate: "2026-03-05", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3992. {
  3993. id: "gpt-5.5",
  3994. modelID: "gpt-5-5",
  3995. releaseDate: "2026-04-23",
  3996. efforts: ["none", "low", "medium", "high", "xhigh"],
  3997. },
  3998. { id: "gpt-5.4-pro", releaseDate: "2026-03-05", efforts: ["medium", "high", "xhigh"] },
  3999. { id: "gpt-5.5-pro", releaseDate: "2026-04-23", efforts: ["medium", "high", "xhigh"] },
  4000. { id: "gpt-5-codex", releaseDate: "2025-09-23", efforts: ["low", "medium", "high"] },
  4001. { id: "gpt-5.1-codex", releaseDate: "2025-11-13", efforts: ["low", "medium", "high"] },
  4002. { id: "gpt-5.1-codex-max", releaseDate: "2025-11-13", efforts: ["low", "medium", "high", "xhigh"] },
  4003. { id: "gpt-5.2-codex", releaseDate: "2025-12-11", efforts: ["low", "medium", "high", "xhigh"] },
  4004. { id: "gpt-5.3-codex", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4005. { id: "gpt-5.3-codex-max", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4006. { id: "gpt-5-chat-latest", releaseDate: "2025-08-07", efforts: [] },
  4007. { id: "gpt-5.1-chat-latest", releaseDate: "2025-11-13", efforts: ["medium"] },
  4008. { id: "gpt-5.2-chat-latest", releaseDate: "2025-12-11", efforts: ["medium"] },
  4009. ]) {
  4010. test(`${testCase.id} returns supported reasoning efforts`, () => {
  4011. const result = ProviderTransform.variants(
  4012. createMockModel({
  4013. id: testCase.modelID ?? testCase.id,
  4014. providerID: "openai",
  4015. api: {
  4016. id: testCase.id,
  4017. url: "https://api.openai.com",
  4018. npm: "@ai-sdk/openai",
  4019. },
  4020. release_date: testCase.releaseDate,
  4021. }),
  4022. )
  4023. expect(Object.keys(result)).toEqual(testCase.efforts)
  4024. })
  4025. }
  4026. test("gpt-50 (lookalike) does not get gpt-5 family treatment", () => {
  4027. const model = createMockModel({
  4028. id: "gpt-50",
  4029. providerID: "openai",
  4030. api: {
  4031. id: "gpt-50",
  4032. url: "https://api.openai.com",
  4033. npm: "@ai-sdk/openai",
  4034. },
  4035. release_date: "2024-01-01",
  4036. })
  4037. const result = ProviderTransform.variants(model)
  4038. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  4039. })
  4040. })
  4041. describe("@ai-sdk/amazon-bedrock/mantle", () => {
  4042. test("gpt-5.5 returns OpenAI-style reasoning variants", () => {
  4043. const model = createMockModel({
  4044. id: "openai.gpt-5.5",
  4045. providerID: "amazon-bedrock",
  4046. api: {
  4047. id: "openai.gpt-5.5",
  4048. url: "https://bedrock-mantle.us-east-2.api.aws/openai/v1",
  4049. npm: "@ai-sdk/amazon-bedrock/mantle",
  4050. },
  4051. release_date: "2026-04-23",
  4052. })
  4053. const result = ProviderTransform.variants(model)
  4054. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high", "xhigh"])
  4055. expect(result.medium).toEqual({
  4056. reasoningEffort: "medium",
  4057. reasoningSummary: "auto",
  4058. include: ["reasoning.encrypted_content"],
  4059. })
  4060. })
  4061. })
  4062. describe("@ai-sdk/anthropic", () => {
  4063. for (const testCase of [
  4064. {
  4065. name: "opus 4.5",
  4066. apiIds: ["claude-opus-4-5-20251101", "claude-opus-4.5-20251101"],
  4067. efforts: ["low", "medium", "high"],
  4068. expectedHigh: { effort: "high" },
  4069. },
  4070. {
  4071. name: "sonnet 4.6",
  4072. apiIds: ["claude-sonnet-4-6", "claude-sonnet-4.6"],
  4073. efforts: ["low", "medium", "high", "max"],
  4074. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  4075. },
  4076. {
  4077. name: "opus 4.6",
  4078. apiIds: ["claude-opus-4-6", "claude-opus-4.6"],
  4079. efforts: ["low", "medium", "high", "max"],
  4080. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  4081. },
  4082. {
  4083. name: "opus 4.7",
  4084. apiIds: ["claude-opus-4-7", "claude-opus-4.7"],
  4085. efforts: ["low", "medium", "high", "xhigh", "max"],
  4086. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4087. },
  4088. {
  4089. name: "opus 4.8",
  4090. apiIds: ["claude-opus-4-8", "claude-opus-4.8"],
  4091. efforts: ["low", "medium", "high", "xhigh", "max"],
  4092. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4093. },
  4094. {
  4095. name: "sonnet 5",
  4096. apiIds: ["claude-sonnet-5", "claude-sonnet-5-20260630"],
  4097. efforts: ["low", "medium", "high", "xhigh", "max"],
  4098. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4099. },
  4100. {
  4101. name: "fable 5",
  4102. apiIds: ["claude-fable-5"],
  4103. efforts: ["low", "medium", "high", "xhigh", "max"],
  4104. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  4105. },
  4106. ]) {
  4107. for (const apiId of testCase.apiIds) {
  4108. test(`${testCase.name} ${apiId} returns supported reasoning efforts`, () => {
  4109. const result = ProviderTransform.variants(
  4110. createMockModel({
  4111. id: `anthropic/${apiId}`,
  4112. providerID: "anthropic",
  4113. api: {
  4114. id: apiId,
  4115. url: "https://api.anthropic.com",
  4116. npm: "@ai-sdk/anthropic",
  4117. },
  4118. }),
  4119. )
  4120. expect(Object.keys(result)).toEqual(testCase.efforts)
  4121. expect(result.high).toEqual(testCase.expectedHigh)
  4122. })
  4123. }
  4124. }
  4125. test("github copilot opus 4.7 returns only medium reasoning effort", () => {
  4126. const model = createMockModel({
  4127. id: "claude-opus-4.7",
  4128. providerID: "github-copilot",
  4129. api: {
  4130. id: "claude-opus-4.7",
  4131. url: "https://api.githubcopilot.com/v1",
  4132. npm: "@ai-sdk/anthropic",
  4133. },
  4134. })
  4135. const result = ProviderTransform.variants(model)
  4136. expect(result).toEqual({
  4137. medium: {
  4138. thinking: {
  4139. type: "adaptive",
  4140. display: "summarized",
  4141. },
  4142. effort: "medium",
  4143. },
  4144. })
  4145. })
  4146. test("returns high and max with thinking config", () => {
  4147. const model = createMockModel({
  4148. id: "anthropic/claude-4",
  4149. providerID: "anthropic",
  4150. api: {
  4151. id: "claude-4",
  4152. url: "https://api.anthropic.com",
  4153. npm: "@ai-sdk/anthropic",
  4154. },
  4155. })
  4156. const result = ProviderTransform.variants(model)
  4157. expect(Object.keys(result)).toEqual(["high", "max"])
  4158. expect(result.high).toEqual({
  4159. thinking: {
  4160. type: "enabled",
  4161. budgetTokens: 16000,
  4162. },
  4163. })
  4164. expect(result.max).toEqual({
  4165. thinking: {
  4166. type: "enabled",
  4167. budgetTokens: 31999,
  4168. },
  4169. })
  4170. })
  4171. })
  4172. describe("@ai-sdk/google-vertex/anthropic", () => {
  4173. test("opus 4.8 uses adaptive reasoning for Vertex model IDs", () => {
  4174. const result = ProviderTransform.variants(
  4175. createMockModel({
  4176. id: "google-vertex-anthropic/claude-opus-4-8@default",
  4177. providerID: "google-vertex-anthropic",
  4178. api: {
  4179. id: "claude-opus-4-8@default",
  4180. url: "https://us-central1-aiplatform.googleapis.com",
  4181. npm: "@ai-sdk/google-vertex/anthropic",
  4182. },
  4183. }),
  4184. )
  4185. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4186. expect(result.high).toEqual({
  4187. thinking: {
  4188. type: "adaptive",
  4189. display: "summarized",
  4190. },
  4191. effort: "high",
  4192. })
  4193. })
  4194. test("sonnet 5 uses adaptive reasoning for Vertex model IDs", () => {
  4195. const result = ProviderTransform.variants(
  4196. createMockModel({
  4197. id: "google-vertex-anthropic/claude-sonnet-5@default",
  4198. providerID: "google-vertex-anthropic",
  4199. api: {
  4200. id: "claude-sonnet-5@default",
  4201. url: "https://us-central1-aiplatform.googleapis.com",
  4202. npm: "@ai-sdk/google-vertex/anthropic",
  4203. },
  4204. }),
  4205. )
  4206. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4207. expect(result.high).toEqual({
  4208. thinking: {
  4209. type: "adaptive",
  4210. display: "summarized",
  4211. },
  4212. effort: "high",
  4213. })
  4214. })
  4215. })
  4216. describe("@ai-sdk/amazon-bedrock", () => {
  4217. test("anthropic sonnet 4.6 returns adaptive reasoning options", () => {
  4218. const model = createMockModel({
  4219. id: "bedrock/anthropic-claude-sonnet-4-6",
  4220. providerID: "bedrock",
  4221. api: {
  4222. id: "anthropic.claude-sonnet-4-6",
  4223. url: "https://bedrock.amazonaws.com",
  4224. npm: "@ai-sdk/amazon-bedrock",
  4225. },
  4226. })
  4227. const result = ProviderTransform.variants(model)
  4228. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  4229. expect(result.max).toEqual({
  4230. reasoningConfig: {
  4231. type: "adaptive",
  4232. maxReasoningEffort: "max",
  4233. },
  4234. })
  4235. })
  4236. test("anthropic opus 4.7 returns adaptive reasoning options with xhigh", () => {
  4237. const model = createMockModel({
  4238. id: "bedrock/anthropic-claude-opus-4-7",
  4239. providerID: "bedrock",
  4240. api: {
  4241. id: "anthropic.claude-opus-4-7",
  4242. url: "https://bedrock.amazonaws.com",
  4243. npm: "@ai-sdk/amazon-bedrock",
  4244. },
  4245. })
  4246. const result = ProviderTransform.variants(model)
  4247. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4248. expect(result.xhigh).toEqual({
  4249. reasoningConfig: {
  4250. type: "adaptive",
  4251. maxReasoningEffort: "xhigh",
  4252. display: "summarized",
  4253. },
  4254. })
  4255. expect(result.max).toEqual({
  4256. reasoningConfig: {
  4257. type: "adaptive",
  4258. maxReasoningEffort: "max",
  4259. display: "summarized",
  4260. },
  4261. })
  4262. })
  4263. test("anthropic opus 4.8 returns adaptive reasoning options with xhigh", () => {
  4264. const result = ProviderTransform.variants(
  4265. createMockModel({
  4266. id: "bedrock/anthropic-claude-opus-4.8",
  4267. providerID: "bedrock",
  4268. api: {
  4269. id: "anthropic.claude-opus-4.8",
  4270. url: "https://bedrock.amazonaws.com",
  4271. npm: "@ai-sdk/amazon-bedrock",
  4272. },
  4273. }),
  4274. )
  4275. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4276. expect(result.high).toEqual({
  4277. reasoningConfig: {
  4278. type: "adaptive",
  4279. maxReasoningEffort: "high",
  4280. display: "summarized",
  4281. },
  4282. })
  4283. })
  4284. test("anthropic sonnet 5 returns adaptive reasoning options with xhigh", () => {
  4285. const result = ProviderTransform.variants(
  4286. createMockModel({
  4287. id: "bedrock/anthropic-claude-sonnet-5",
  4288. providerID: "bedrock",
  4289. api: {
  4290. id: "anthropic.claude-sonnet-5",
  4291. url: "https://bedrock.amazonaws.com",
  4292. npm: "@ai-sdk/amazon-bedrock",
  4293. },
  4294. }),
  4295. )
  4296. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  4297. expect(result.high).toEqual({
  4298. reasoningConfig: {
  4299. type: "adaptive",
  4300. maxReasoningEffort: "high",
  4301. display: "summarized",
  4302. },
  4303. })
  4304. })
  4305. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningConfig", () => {
  4306. const model = createMockModel({
  4307. id: "bedrock/llama-4",
  4308. providerID: "bedrock",
  4309. api: {
  4310. id: "llama-4-sc",
  4311. url: "https://bedrock.amazonaws.com",
  4312. npm: "@ai-sdk/amazon-bedrock",
  4313. },
  4314. })
  4315. const result = ProviderTransform.variants(model)
  4316. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  4317. expect(result.low).toEqual({
  4318. reasoningConfig: {
  4319. type: "enabled",
  4320. maxReasoningEffort: "low",
  4321. },
  4322. })
  4323. })
  4324. })
  4325. for (const provider of [
  4326. { name: "@ai-sdk/google", providerID: "google", url: "https://generativelanguage.googleapis.com" },
  4327. { name: "@ai-sdk/google-vertex", providerID: "google-vertex", url: "https://vertexai.googleapis.com" },
  4328. ]) {
  4329. describe(provider.name, () => {
  4330. for (const testCase of [
  4331. {
  4332. apiId: "gemini-2.5-pro",
  4333. efforts: ["high", "max"],
  4334. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  4335. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } },
  4336. },
  4337. {
  4338. apiId: "gemini-2.5-flash",
  4339. efforts: ["high", "max"],
  4340. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  4341. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
  4342. },
  4343. {
  4344. apiId: "gemini-3-pro-preview",
  4345. efforts: ["low", "medium", "high"],
  4346. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4347. },
  4348. {
  4349. apiId: "gemini-3.1-pro-preview",
  4350. efforts: ["low", "medium", "high"],
  4351. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4352. },
  4353. {
  4354. apiId: "gemini-3-flash-preview",
  4355. efforts: ["minimal", "low", "medium", "high"],
  4356. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4357. },
  4358. {
  4359. apiId: "gemini-3.1-flash-lite",
  4360. efforts: ["minimal", "low", "medium", "high"],
  4361. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4362. },
  4363. {
  4364. apiId: "gemini-3.1-flash-image-preview",
  4365. efforts: ["minimal", "high"],
  4366. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4367. },
  4368. {
  4369. apiId: "gemini-3-pro-image-preview",
  4370. efforts: ["high"],
  4371. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  4372. },
  4373. ]) {
  4374. test(`${testCase.apiId} returns supported thinking controls`, () => {
  4375. const result = ProviderTransform.variants(
  4376. createMockModel({
  4377. id: `${provider.providerID}/${testCase.apiId}`,
  4378. providerID: provider.providerID,
  4379. api: {
  4380. id: testCase.apiId,
  4381. url: provider.url,
  4382. npm: provider.name,
  4383. },
  4384. }),
  4385. )
  4386. expect(Object.keys(result)).toEqual(testCase.efforts)
  4387. expect(result.high).toEqual(testCase.expectedHigh)
  4388. if (testCase.expectedMax) expect(result.max).toEqual(testCase.expectedMax)
  4389. })
  4390. }
  4391. })
  4392. }
  4393. describe("@ai-sdk/cohere", () => {
  4394. test("returns empty object", () => {
  4395. const model = createMockModel({
  4396. id: "cohere/command-r",
  4397. providerID: "cohere",
  4398. api: {
  4399. id: "command-r",
  4400. url: "https://api.cohere.com",
  4401. npm: "@ai-sdk/cohere",
  4402. },
  4403. })
  4404. const result = ProviderTransform.variants(model)
  4405. expect(result).toEqual({})
  4406. })
  4407. })
  4408. describe("@ai-sdk/groq", () => {
  4409. test("returns none and WIDELY_SUPPORTED_EFFORTS with thinkingLevel", () => {
  4410. const model = createMockModel({
  4411. id: "groq/llama-4",
  4412. providerID: "groq",
  4413. api: {
  4414. id: "llama-4-sc",
  4415. url: "https://api.groq.com",
  4416. npm: "@ai-sdk/groq",
  4417. },
  4418. })
  4419. const result = ProviderTransform.variants(model)
  4420. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high"])
  4421. expect(result.none).toEqual({
  4422. reasoningEffort: "none",
  4423. })
  4424. expect(result.low).toEqual({
  4425. reasoningEffort: "low",
  4426. })
  4427. })
  4428. })
  4429. describe("@ai-sdk/perplexity", () => {
  4430. test("returns empty object", () => {
  4431. const model = createMockModel({
  4432. id: "perplexity/sonar-plus",
  4433. providerID: "perplexity",
  4434. api: {
  4435. id: "sonar-plus",
  4436. url: "https://api.perplexity.ai",
  4437. npm: "@ai-sdk/perplexity",
  4438. },
  4439. })
  4440. const result = ProviderTransform.variants(model)
  4441. expect(result).toEqual({})
  4442. })
  4443. })
  4444. describe("@jerome-benoit/sap-ai-provider-v2", () => {
  4445. const sapModel = (apiId: string, releaseDate = "2024-01-01") =>
  4446. createMockModel({
  4447. id: `sap-ai-core/${apiId}`,
  4448. providerID: "sap-ai-core",
  4449. api: {
  4450. id: apiId,
  4451. url: "https://api.ai.sap",
  4452. npm: "@jerome-benoit/sap-ai-provider-v2",
  4453. },
  4454. release_date: releaseDate,
  4455. })
  4456. for (const testCase of [
  4457. {
  4458. name: "sonnet 4.6",
  4459. apiIds: ["anthropic--claude-sonnet-4-6"],
  4460. efforts: ["low", "medium", "high", "max"],
  4461. thinking: { type: "adaptive" },
  4462. },
  4463. {
  4464. name: "opus 4.6",
  4465. apiIds: ["anthropic--claude-4.6-opus", "anthropic--claude-4-6-opus"],
  4466. efforts: ["low", "medium", "high", "max"],
  4467. thinking: { type: "adaptive" },
  4468. },
  4469. {
  4470. name: "opus 4.7",
  4471. apiIds: ["anthropic--claude-4.7-opus", "anthropic--claude-4-7-opus"],
  4472. efforts: ["low", "medium", "high", "xhigh", "max"],
  4473. thinking: { type: "adaptive", display: "summarized" },
  4474. },
  4475. {
  4476. name: "opus 4.8",
  4477. apiIds: ["anthropic--claude-4.8-opus", "anthropic--claude-4-8-opus"],
  4478. efforts: ["low", "medium", "high", "xhigh", "max"],
  4479. thinking: { type: "adaptive", display: "summarized" },
  4480. },
  4481. {
  4482. name: "sonnet 5",
  4483. apiIds: ["anthropic--claude-sonnet-5", "anthropic--claude-5-sonnet"],
  4484. efforts: ["low", "medium", "high", "xhigh", "max"],
  4485. thinking: { type: "adaptive", display: "summarized" },
  4486. },
  4487. ]) {
  4488. for (const apiId of testCase.apiIds) {
  4489. test(`${testCase.name} ${apiId} returns adaptive thinking variants under modelParams`, () => {
  4490. const result = ProviderTransform.variants(sapModel(apiId))
  4491. expect(Object.keys(result)).toEqual(testCase.efforts)
  4492. for (const effort of testCase.efforts) {
  4493. expect(result[effort]).toEqual({
  4494. modelParams: {
  4495. thinking: testCase.thinking,
  4496. output_config: { effort },
  4497. },
  4498. })
  4499. }
  4500. })
  4501. }
  4502. }
  4503. for (const apiId of ["anthropic--claude-sonnet-4", "anthropic--claude-4.5-opus"]) {
  4504. test(`${apiId} returns budget_tokens variants under modelParams`, () => {
  4505. const result = ProviderTransform.variants(sapModel(apiId))
  4506. expect(Object.keys(result)).toEqual(["high", "max"])
  4507. expect(result.high).toEqual({
  4508. modelParams: { thinking: { type: "enabled", budget_tokens: 16000 } },
  4509. })
  4510. expect(result.max).toEqual({
  4511. modelParams: { thinking: { type: "enabled", budget_tokens: 31999 } },
  4512. })
  4513. })
  4514. }
  4515. for (const testCase of [
  4516. { apiId: "gemini-2.5-pro", maxBudget: 32768 },
  4517. { apiId: "gemini-2.5-flash", maxBudget: 24576 },
  4518. ]) {
  4519. test(`${testCase.apiId} returns thinkingConfig variants under modelParams`, () => {
  4520. const result = ProviderTransform.variants(sapModel(testCase.apiId))
  4521. expect(Object.keys(result)).toEqual(["high", "max"])
  4522. expect(result.high).toEqual({
  4523. modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } },
  4524. })
  4525. expect(result.max).toEqual({
  4526. modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: testCase.maxBudget } },
  4527. })
  4528. })
  4529. }
  4530. for (const testCase of [
  4531. { apiId: "gpt-5", releaseDate: "2025-08-07", efforts: ["minimal", "low", "medium", "high"] },
  4532. { apiId: "gpt-5-mini", releaseDate: "2025-08-07", efforts: ["minimal", "low", "medium", "high"] },
  4533. { apiId: "gpt-5-nano", releaseDate: "2025-08-07", efforts: ["minimal", "low", "medium", "high"] },
  4534. { apiId: "gpt-5.4", releaseDate: "2026-01-15", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4535. { apiId: "azure-openai--o3-mini", releaseDate: "2024-01-01", efforts: ["low", "medium", "high"] },
  4536. ]) {
  4537. test(`${testCase.apiId} returns reasoning_effort variants under modelParams`, () => {
  4538. const result = ProviderTransform.variants(sapModel(testCase.apiId, testCase.releaseDate))
  4539. expect(Object.keys(result)).toEqual(testCase.efforts)
  4540. for (const effort of testCase.efforts) {
  4541. expect(result[effort]).toEqual({ modelParams: { reasoning_effort: effort } })
  4542. }
  4543. })
  4544. }
  4545. for (const apiId of [
  4546. "gemini-3.1-flash-lite",
  4547. "cohere--command-a-reasoning",
  4548. "sonar-deep-research",
  4549. "aws--llama-opus-4.7-fake",
  4550. ]) {
  4551. test(`${apiId} falls through to harmonized reasoning_effort fallback`, () => {
  4552. const result = ProviderTransform.variants(sapModel(apiId))
  4553. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  4554. for (const effort of ["low", "medium", "high"]) {
  4555. expect(result[effort]).toEqual({ modelParams: { reasoning_effort: effort } })
  4556. }
  4557. })
  4558. }
  4559. })
  4560. describe("ai-gateway-provider (cloudflare-ai-gateway)", () => {
  4561. const cfModel = (apiId: string, releaseDate = "2024-01-01") =>
  4562. createMockModel({
  4563. id: `cloudflare-ai-gateway/${apiId}`,
  4564. providerID: "cloudflare-ai-gateway",
  4565. api: {
  4566. id: apiId,
  4567. url: "https://gateway.ai.cloudflare.com/v1/compat",
  4568. npm: "ai-gateway-provider",
  4569. },
  4570. release_date: releaseDate,
  4571. })
  4572. for (const testCase of [
  4573. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4574. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  4575. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  4576. { id: "openai/gpt-5-pro", efforts: ["high"] },
  4577. { id: "openai/gpt-5.2-pro", efforts: ["medium", "high", "xhigh"] },
  4578. { id: "openai/gpt-5-chat-latest", efforts: [] },
  4579. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  4580. ]) {
  4581. test(`${testCase.id} returns supported reasoning efforts`, () => {
  4582. const result = ProviderTransform.variants(cfModel(testCase.id, "2026-03-05"))
  4583. expect(Object.keys(result)).toEqual(testCase.efforts)
  4584. })
  4585. }
  4586. test("openai gpt-4o (no reasoning) returns empty", () => {
  4587. const model = cfModel("openai/gpt-4o")
  4588. model.capabilities.reasoning = false
  4589. const result = ProviderTransform.variants(model)
  4590. expect(result).toEqual({})
  4591. })
  4592. test("non-openai upstream falls back to widely-supported OAI efforts", () => {
  4593. const result = ProviderTransform.variants(cfModel("anthropic/claude-sonnet-4-6"))
  4594. expect(result).toEqual({
  4595. low: { reasoningEffort: "low" },
  4596. medium: { reasoningEffort: "medium" },
  4597. high: { reasoningEffort: "high" },
  4598. })
  4599. })
  4600. })
  4601. })
  4602. describe("ProviderTransform.smallOptions - gpt-5 chat/search", () => {
  4603. const createModel = (apiId: string) => {
  4604. const model = {
  4605. id: `openai/${apiId}`,
  4606. providerID: "openai",
  4607. api: {
  4608. id: apiId,
  4609. url: "https://api.openai.com",
  4610. npm: "@ai-sdk/openai",
  4611. },
  4612. capabilities: { reasoning: true },
  4613. limit: { output: 64_000 },
  4614. release_date: "2026-01-01",
  4615. } as any
  4616. model.variants = ProviderTransform.variants(model)
  4617. return model
  4618. }
  4619. for (const testCase of [
  4620. { id: "gpt-5-chat-latest", options: { store: false } },
  4621. {
  4622. id: "gpt-5.1-chat-latest",
  4623. options: {
  4624. store: false,
  4625. reasoningEffort: "medium",
  4626. reasoningSummary: "auto",
  4627. include: ["reasoning.encrypted_content"],
  4628. },
  4629. },
  4630. {
  4631. id: "gpt-5.2-chat-latest",
  4632. options: {
  4633. store: false,
  4634. reasoningEffort: "medium",
  4635. reasoningSummary: "auto",
  4636. include: ["reasoning.encrypted_content"],
  4637. },
  4638. },
  4639. {
  4640. id: "gpt-5-search-api",
  4641. options: {
  4642. store: false,
  4643. reasoningEffort: "none",
  4644. reasoningSummary: "auto",
  4645. include: ["reasoning.encrypted_content"],
  4646. },
  4647. },
  4648. ]) {
  4649. test(`${testCase.id} returns only supported small options`, () => {
  4650. expect(ProviderTransform.smallOptions(createModel(testCase.id))).toEqual(testCase.options)
  4651. })
  4652. }
  4653. })
  4654. test("ProviderTransform.smallOptions preserves the weakest OpenRouter reasoning effort", () => {
  4655. expect(
  4656. ProviderTransform.smallOptions({
  4657. providerID: "openrouter",
  4658. api: {
  4659. id: "google/gemini-3.5-flash",
  4660. npm: "@openrouter/ai-sdk-provider",
  4661. },
  4662. variants: {
  4663. low: { reasoning: { effort: "low" } },
  4664. medium: { reasoning: { effort: "medium" } },
  4665. high: { reasoning: { effort: "high" } },
  4666. },
  4667. } as any),
  4668. ).toEqual({ reasoning: { effort: "low" } })
  4669. })
  4670. describe("ProviderTransform.smallOptions - google thinking controls", () => {
  4671. const createGoogleModel = (apiId: string) => {
  4672. const model = {
  4673. id: `google/${apiId}`,
  4674. providerID: "google",
  4675. api: {
  4676. id: apiId,
  4677. url: "https://generativelanguage.googleapis.com",
  4678. npm: "@ai-sdk/google",
  4679. },
  4680. capabilities: { reasoning: true },
  4681. limit: { output: 64_000 },
  4682. } as any
  4683. model.variants = ProviderTransform.variants(model)
  4684. return model
  4685. }
  4686. for (const testCase of [
  4687. { id: "gemini-3-pro-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "low" } } },
  4688. { id: "gemini-3-flash-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } } },
  4689. {
  4690. id: "gemini-3.1-flash-image-preview",
  4691. options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } },
  4692. },
  4693. { id: "gemini-3-pro-image-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } } },
  4694. { id: "gemini-2.5-pro", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  4695. { id: "gemini-2.5-flash", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  4696. ]) {
  4697. test(`${testCase.id} returns supported small thinking options`, () => {
  4698. expect(ProviderTransform.smallOptions(createGoogleModel(testCase.id))).toEqual(testCase.options)
  4699. })
  4700. }
  4701. test("uses the first configured variant when available", () => {
  4702. expect(
  4703. ProviderTransform.smallOptions({
  4704. ...createGoogleModel("gemini-2.5-pro"),
  4705. variants: {
  4706. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } },
  4707. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32768 } },
  4708. },
  4709. }),
  4710. ).toEqual({ thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } })
  4711. })
  4712. test("does not synthesize thinking options when variants are empty", () => {
  4713. expect(ProviderTransform.smallOptions({ ...createGoogleModel("gemini-2.5-pro"), variants: {} })).toEqual({})
  4714. })
  4715. })
  4716. describe("ProviderTransform.providerOptions - ai-gateway-provider", () => {
  4717. const createModel = (overrides: Partial<any> = {}) =>
  4718. ({
  4719. id: "cloudflare-ai-gateway/openai/gpt-5.4",
  4720. providerID: "cloudflare-ai-gateway",
  4721. api: {
  4722. id: "openai/gpt-5.4",
  4723. url: "https://gateway.ai.cloudflare.com/v1/compat",
  4724. npm: "ai-gateway-provider",
  4725. },
  4726. capabilities: {
  4727. temperature: false,
  4728. reasoning: true,
  4729. attachment: true,
  4730. toolcall: true,
  4731. input: { text: true, audio: false, image: true, video: false, pdf: true },
  4732. output: { text: true, audio: false, image: false, video: false, pdf: false },
  4733. interleaved: false,
  4734. },
  4735. cost: { input: 1, output: 1, cache: { read: 0, write: 0 } },
  4736. limit: { context: 1_000_000, output: 128_000 },
  4737. status: "active",
  4738. options: {},
  4739. headers: {},
  4740. release_date: "2026-03-05",
  4741. ...overrides,
  4742. }) as any
  4743. test("routes options under openaiCompatible (the key @ai-sdk/openai-compatible reads)", () => {
  4744. // Regression: previously fell back to providerID="cloudflare-ai-gateway",
  4745. // which @ai-sdk/openai-compatible never reads, silently dropping reasoningEffort.
  4746. const result = ProviderTransform.providerOptions(createModel(), { reasoningEffort: "high" })
  4747. expect(result).toEqual({ openaiCompatible: { reasoningEffort: "high" } })
  4748. })
  4749. })