transform.test.ts 145 KB

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