transform.test.ts 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770
  1. import { describe, expect, test } from "bun:test"
  2. import { ProviderTransform } from "@/provider/transform"
  3. import { ProviderV2 } from "@opencode-ai/core/provider"
  4. describe("ProviderTransform.options - setCacheKey", () => {
  5. const sessionID = "test-session-123"
  6. const mockModel = {
  7. id: "anthropic/claude-3-5-sonnet",
  8. providerID: "anthropic",
  9. api: {
  10. id: "claude-3-5-sonnet-20241022",
  11. url: "https://api.anthropic.com",
  12. npm: "@ai-sdk/anthropic",
  13. },
  14. name: "Claude 3.5 Sonnet",
  15. capabilities: {
  16. temperature: true,
  17. reasoning: false,
  18. attachment: true,
  19. toolcall: true,
  20. input: { text: true, audio: false, image: true, video: false, pdf: true },
  21. output: { text: true, audio: false, image: false, video: false, pdf: false },
  22. interleaved: false,
  23. },
  24. cost: {
  25. input: 0.003,
  26. output: 0.015,
  27. cache: { read: 0.0003, write: 0.00375 },
  28. },
  29. limit: {
  30. context: 200000,
  31. output: 8192,
  32. },
  33. status: "active",
  34. options: {},
  35. headers: {},
  36. } as any
  37. test("should set promptCacheKey when providerOptions.setCacheKey is true", () => {
  38. const result = ProviderTransform.options({
  39. model: mockModel,
  40. sessionID,
  41. providerOptions: { setCacheKey: true },
  42. })
  43. expect(result.promptCacheKey).toBe(sessionID)
  44. })
  45. test("should not set promptCacheKey when providerOptions.setCacheKey is false", () => {
  46. const result = ProviderTransform.options({
  47. model: mockModel,
  48. sessionID,
  49. providerOptions: { setCacheKey: false },
  50. })
  51. expect(result.promptCacheKey).toBeUndefined()
  52. })
  53. test("should not set promptCacheKey when providerOptions is undefined", () => {
  54. const result = ProviderTransform.options({
  55. model: mockModel,
  56. sessionID,
  57. providerOptions: undefined,
  58. })
  59. expect(result.promptCacheKey).toBeUndefined()
  60. })
  61. test("should not set promptCacheKey when providerOptions does not have setCacheKey", () => {
  62. const result = ProviderTransform.options({ model: mockModel, sessionID, providerOptions: {} })
  63. expect(result.promptCacheKey).toBeUndefined()
  64. })
  65. test("should set promptCacheKey for openai provider regardless of setCacheKey", () => {
  66. const openaiModel = {
  67. ...mockModel,
  68. providerID: "openai",
  69. api: {
  70. id: "gpt-4",
  71. url: "https://api.openai.com",
  72. npm: "@ai-sdk/openai",
  73. },
  74. }
  75. const result = ProviderTransform.options({ model: openaiModel, sessionID, providerOptions: {} })
  76. expect(result.promptCacheKey).toBe(sessionID)
  77. })
  78. test("should set store=false for openai provider", () => {
  79. const openaiModel = {
  80. ...mockModel,
  81. providerID: "openai",
  82. api: {
  83. id: "gpt-4",
  84. url: "https://api.openai.com",
  85. npm: "@ai-sdk/openai",
  86. },
  87. }
  88. const result = ProviderTransform.options({
  89. model: openaiModel,
  90. sessionID,
  91. providerOptions: {},
  92. })
  93. expect(result.store).toBe(false)
  94. })
  95. test("should set store=false for azure provider by default", () => {
  96. const azureModel = {
  97. ...mockModel,
  98. providerID: "azure",
  99. api: {
  100. id: "gpt-4",
  101. url: "https://azure.com",
  102. npm: "@ai-sdk/azure",
  103. },
  104. }
  105. const result = ProviderTransform.options({
  106. model: azureModel,
  107. sessionID,
  108. providerOptions: {},
  109. })
  110. expect(result.store).toBe(false)
  111. })
  112. })
  113. describe("ProviderTransform.options - zai/zhipuai thinking", () => {
  114. const sessionID = "test-session-123"
  115. const createModel = (providerID: string) =>
  116. ({
  117. id: `${providerID}/glm-4.6`,
  118. providerID,
  119. api: {
  120. id: "glm-4.6",
  121. url: "https://open.bigmodel.cn/api/paas/v4",
  122. npm: "@ai-sdk/openai-compatible",
  123. },
  124. name: "GLM 4.6",
  125. capabilities: {
  126. temperature: true,
  127. reasoning: true,
  128. attachment: true,
  129. toolcall: true,
  130. input: { text: true, audio: false, image: true, video: false, pdf: true },
  131. output: { text: true, audio: false, image: false, video: false, pdf: false },
  132. interleaved: false,
  133. },
  134. cost: {
  135. input: 0.001,
  136. output: 0.002,
  137. cache: { read: 0.0001, write: 0.0002 },
  138. },
  139. limit: {
  140. context: 128000,
  141. output: 8192,
  142. },
  143. status: "active",
  144. options: {},
  145. headers: {},
  146. }) as any
  147. for (const providerID of ["zai-coding-plan", "zai", "zhipuai-coding-plan", "zhipuai"]) {
  148. test(`${providerID} should set thinking cfg`, () => {
  149. const result = ProviderTransform.options({
  150. model: createModel(providerID),
  151. sessionID,
  152. providerOptions: {},
  153. })
  154. expect(result.thinking).toEqual({
  155. type: "enabled",
  156. clear_thinking: false,
  157. })
  158. })
  159. }
  160. })
  161. describe("ProviderTransform.options - google thinkingConfig gating", () => {
  162. const sessionID = "test-session-123"
  163. const createGoogleModel = (reasoning: boolean, npm: "@ai-sdk/google" | "@ai-sdk/google-vertex") =>
  164. ({
  165. id: `${npm === "@ai-sdk/google" ? "google" : "google-vertex"}/gemini-2.0-flash`,
  166. providerID: npm === "@ai-sdk/google" ? "google" : "google-vertex",
  167. api: {
  168. id: "gemini-2.0-flash",
  169. url: npm === "@ai-sdk/google" ? "https://generativelanguage.googleapis.com" : "https://vertexai.googleapis.com",
  170. npm,
  171. },
  172. name: "Gemini 2.0 Flash",
  173. capabilities: {
  174. temperature: true,
  175. reasoning,
  176. attachment: true,
  177. toolcall: true,
  178. input: { text: true, audio: false, image: true, video: false, pdf: true },
  179. output: { text: true, audio: false, image: false, video: false, pdf: false },
  180. interleaved: false,
  181. },
  182. cost: {
  183. input: 0.001,
  184. output: 0.002,
  185. cache: { read: 0.0001, write: 0.0002 },
  186. },
  187. limit: {
  188. context: 1_000_000,
  189. output: 8192,
  190. },
  191. status: "active",
  192. options: {},
  193. headers: {},
  194. }) as any
  195. test("does not set thinkingConfig for google models without reasoning capability", () => {
  196. const result = ProviderTransform.options({
  197. model: createGoogleModel(false, "@ai-sdk/google"),
  198. sessionID,
  199. providerOptions: {},
  200. })
  201. expect(result.thinkingConfig).toBeUndefined()
  202. })
  203. test("sets thinkingConfig for google models with reasoning capability", () => {
  204. const result = ProviderTransform.options({
  205. model: createGoogleModel(true, "@ai-sdk/google"),
  206. sessionID,
  207. providerOptions: {},
  208. })
  209. expect(result.thinkingConfig).toEqual({
  210. includeThoughts: true,
  211. })
  212. })
  213. test("does not set thinkingConfig for vertex models without reasoning capability", () => {
  214. const result = ProviderTransform.options({
  215. model: createGoogleModel(false, "@ai-sdk/google-vertex"),
  216. sessionID,
  217. providerOptions: {},
  218. })
  219. expect(result.thinkingConfig).toBeUndefined()
  220. })
  221. })
  222. describe("ProviderTransform.options - gpt-5 textVerbosity", () => {
  223. const sessionID = "test-session-123"
  224. const createGpt5Model = (apiId: string) =>
  225. ({
  226. id: `openai/${apiId}`,
  227. providerID: "openai",
  228. api: {
  229. id: apiId,
  230. url: "https://api.openai.com",
  231. npm: "@ai-sdk/openai",
  232. },
  233. name: apiId,
  234. capabilities: {
  235. temperature: true,
  236. reasoning: true,
  237. attachment: true,
  238. toolcall: true,
  239. input: { text: true, audio: false, image: true, video: false, pdf: false },
  240. output: { text: true, audio: false, image: false, video: false, pdf: false },
  241. interleaved: false,
  242. },
  243. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  244. limit: { context: 128000, output: 4096 },
  245. status: "active",
  246. options: {},
  247. headers: {},
  248. }) as any
  249. test("gpt-5.2 should have textVerbosity set to low", () => {
  250. const model = createGpt5Model("gpt-5.2")
  251. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  252. expect(result.textVerbosity).toBe("low")
  253. expect(result.include).toEqual(["reasoning.encrypted_content"])
  254. })
  255. test("gpt-5.1 should have textVerbosity set to low", () => {
  256. const model = createGpt5Model("gpt-5.1")
  257. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  258. expect(result.textVerbosity).toBe("low")
  259. })
  260. test("gpt-5.2-chat-latest should NOT have textVerbosity set (only supports medium)", () => {
  261. const model = createGpt5Model("gpt-5.2-chat-latest")
  262. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  263. expect(result.textVerbosity).toBeUndefined()
  264. })
  265. test("gpt-5.1-chat-latest should NOT have textVerbosity set (only supports medium)", () => {
  266. const model = createGpt5Model("gpt-5.1-chat-latest")
  267. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  268. expect(result.textVerbosity).toBeUndefined()
  269. })
  270. test("gpt-5.2-chat should NOT have textVerbosity set", () => {
  271. const model = createGpt5Model("gpt-5.2-chat")
  272. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  273. expect(result.textVerbosity).toBeUndefined()
  274. })
  275. test("gpt-5-chat should NOT have textVerbosity set", () => {
  276. const model = createGpt5Model("gpt-5-chat")
  277. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  278. expect(result.textVerbosity).toBeUndefined()
  279. })
  280. test("gpt-5.2-codex should NOT have textVerbosity set (codex models excluded)", () => {
  281. const model = createGpt5Model("gpt-5.2-codex")
  282. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  283. expect(result.textVerbosity).toBeUndefined()
  284. })
  285. })
  286. describe("ProviderTransform.options - gpt-5 reasoningEffort", () => {
  287. const sessionID = "test-session-123"
  288. const createModel = (apiId: string) =>
  289. ({
  290. id: `azure/${apiId}`,
  291. providerID: "azure",
  292. api: {
  293. id: apiId,
  294. url: "https://azure.com",
  295. npm: "@ai-sdk/azure",
  296. },
  297. name: apiId,
  298. capabilities: {
  299. temperature: true,
  300. reasoning: true,
  301. attachment: true,
  302. toolcall: true,
  303. input: {
  304. text: true,
  305. audio: false,
  306. image: true,
  307. video: false,
  308. pdf: false,
  309. },
  310. output: {
  311. text: true,
  312. audio: false,
  313. image: false,
  314. video: false,
  315. pdf: false,
  316. },
  317. interleaved: false,
  318. },
  319. cost: {
  320. input: 0.03,
  321. output: 0.06,
  322. cache: { read: 0.001, write: 0.002 },
  323. },
  324. limit: {
  325. context: 128000,
  326. output: 4096,
  327. },
  328. status: "active",
  329. options: {},
  330. headers: {},
  331. }) as any
  332. test("gpt-5-chat should NOT set reasoningEffort", () => {
  333. const result = ProviderTransform.options({
  334. model: createModel("gpt-5-chat"),
  335. sessionID,
  336. providerOptions: {},
  337. })
  338. expect(result.reasoningEffort).toBeUndefined()
  339. })
  340. test("gpt-5.5 should NOT set reasoningEffort", () => {
  341. const result = ProviderTransform.options({
  342. model: createModel("gpt-5.5"),
  343. sessionID,
  344. providerOptions: {},
  345. })
  346. expect(result.reasoningEffort).toBeUndefined()
  347. })
  348. })
  349. describe("ProviderTransform.options - gateway", () => {
  350. const sessionID = "test-session-123"
  351. const createModel = (id: string) =>
  352. ({
  353. id,
  354. providerID: "vercel",
  355. api: {
  356. id,
  357. url: "https://ai-gateway.vercel.sh/v3/ai",
  358. npm: "@ai-sdk/gateway",
  359. },
  360. name: id,
  361. capabilities: {
  362. temperature: true,
  363. reasoning: true,
  364. attachment: true,
  365. toolcall: true,
  366. input: { text: true, audio: false, image: true, video: false, pdf: true },
  367. output: { text: true, audio: false, image: false, video: false, pdf: false },
  368. interleaved: false,
  369. },
  370. cost: {
  371. input: 0.001,
  372. output: 0.002,
  373. cache: { read: 0.0001, write: 0.0002 },
  374. },
  375. limit: {
  376. context: 200_000,
  377. output: 8192,
  378. },
  379. status: "active",
  380. options: {},
  381. headers: {},
  382. release_date: "2024-01-01",
  383. }) as any
  384. test("puts gateway defaults under gateway key", () => {
  385. const model = createModel("anthropic/claude-sonnet-4")
  386. const result = ProviderTransform.options({ model, sessionID, providerOptions: {} })
  387. expect(result).toEqual({
  388. gateway: {
  389. caching: "auto",
  390. },
  391. })
  392. })
  393. })
  394. describe("ProviderTransform.providerOptions", () => {
  395. const createModel = (overrides: Partial<any> = {}) =>
  396. ({
  397. id: "test/test-model",
  398. providerID: "test",
  399. api: {
  400. id: "test-model",
  401. url: "https://api.test.com",
  402. npm: "@ai-sdk/openai",
  403. },
  404. name: "Test Model",
  405. capabilities: {
  406. temperature: true,
  407. reasoning: true,
  408. attachment: true,
  409. toolcall: true,
  410. input: { text: true, audio: false, image: true, video: false, pdf: false },
  411. output: { text: true, audio: false, image: false, video: false, pdf: false },
  412. interleaved: false,
  413. },
  414. cost: {
  415. input: 0.001,
  416. output: 0.002,
  417. cache: { read: 0.0001, write: 0.0002 },
  418. },
  419. limit: {
  420. context: 200_000,
  421. output: 64_000,
  422. },
  423. status: "active",
  424. options: {},
  425. headers: {},
  426. release_date: "2024-01-01",
  427. ...overrides,
  428. }) as any
  429. test("uses sdk key for non-gateway models", () => {
  430. const model = createModel({
  431. providerID: "my-bedrock",
  432. api: {
  433. id: "anthropic.claude-sonnet-4",
  434. url: "https://bedrock.aws",
  435. npm: "@ai-sdk/amazon-bedrock",
  436. },
  437. })
  438. expect(ProviderTransform.providerOptions(model, { cachePoint: { type: "default" } })).toEqual({
  439. bedrock: { cachePoint: { type: "default" } },
  440. })
  441. })
  442. test("uses gateway model provider slug for gateway models", () => {
  443. const model = createModel({
  444. providerID: "vercel",
  445. api: {
  446. id: "anthropic/claude-sonnet-4",
  447. url: "https://ai-gateway.vercel.sh/v3/ai",
  448. npm: "@ai-sdk/gateway",
  449. },
  450. })
  451. expect(ProviderTransform.providerOptions(model, { thinking: { type: "enabled", budgetTokens: 12_000 } })).toEqual({
  452. anthropic: { thinking: { type: "enabled", budgetTokens: 12_000 } },
  453. })
  454. })
  455. test("falls back to gateway key when gateway api id is unscoped", () => {
  456. const model = createModel({
  457. id: "anthropic/claude-sonnet-4",
  458. providerID: "vercel",
  459. api: {
  460. id: "claude-sonnet-4",
  461. url: "https://ai-gateway.vercel.sh/v3/ai",
  462. npm: "@ai-sdk/gateway",
  463. },
  464. })
  465. expect(ProviderTransform.providerOptions(model, { thinking: { type: "enabled", budgetTokens: 12_000 } })).toEqual({
  466. gateway: { thinking: { type: "enabled", budgetTokens: 12_000 } },
  467. })
  468. })
  469. test("splits gateway routing options from provider-specific options", () => {
  470. const model = createModel({
  471. providerID: "vercel",
  472. api: {
  473. id: "anthropic/claude-sonnet-4",
  474. url: "https://ai-gateway.vercel.sh/v3/ai",
  475. npm: "@ai-sdk/gateway",
  476. },
  477. })
  478. expect(
  479. ProviderTransform.providerOptions(model, {
  480. gateway: { order: ["vertex", "anthropic"] },
  481. thinking: { type: "enabled", budgetTokens: 12_000 },
  482. }),
  483. ).toEqual({
  484. gateway: { order: ["vertex", "anthropic"] },
  485. anthropic: { thinking: { type: "enabled", budgetTokens: 12_000 } },
  486. } as any)
  487. })
  488. test("falls back to gateway key when model id has no provider slug", () => {
  489. const model = createModel({
  490. id: "claude-sonnet-4",
  491. providerID: "vercel",
  492. api: {
  493. id: "claude-sonnet-4",
  494. url: "https://ai-gateway.vercel.sh/v3/ai",
  495. npm: "@ai-sdk/gateway",
  496. },
  497. })
  498. expect(ProviderTransform.providerOptions(model, { reasoningEffort: "high" })).toEqual({
  499. gateway: { reasoningEffort: "high" },
  500. })
  501. })
  502. test("maps amazon slug to bedrock for provider options", () => {
  503. const model = createModel({
  504. providerID: "vercel",
  505. api: {
  506. id: "amazon/nova-2-lite",
  507. url: "https://ai-gateway.vercel.sh/v3/ai",
  508. npm: "@ai-sdk/gateway",
  509. },
  510. })
  511. expect(ProviderTransform.providerOptions(model, { reasoningConfig: { type: "enabled" } })).toEqual({
  512. bedrock: { reasoningConfig: { type: "enabled" } },
  513. })
  514. })
  515. test("uses groq slug for groq models", () => {
  516. const model = createModel({
  517. providerID: "vercel",
  518. api: {
  519. id: "groq/llama-3.3-70b-versatile",
  520. url: "https://ai-gateway.vercel.sh/v3/ai",
  521. npm: "@ai-sdk/gateway",
  522. },
  523. })
  524. expect(ProviderTransform.providerOptions(model, { reasoningFormat: "parsed" })).toEqual({
  525. groq: { reasoningFormat: "parsed" },
  526. })
  527. })
  528. })
  529. describe("ProviderTransform.schema - gemini array items", () => {
  530. test("adds missing items for array properties", () => {
  531. const geminiModel = {
  532. providerID: "google",
  533. api: {
  534. id: "gemini-3-pro",
  535. },
  536. } as any
  537. const schema = {
  538. type: "object",
  539. properties: {
  540. nodes: { type: "array" },
  541. edges: { type: "array", items: { type: "string" } },
  542. },
  543. } as any
  544. const result = ProviderTransform.schema(geminiModel, schema) as any
  545. expect(result.properties.nodes.items).toBeDefined()
  546. expect(result.properties.edges.items.type).toBe("string")
  547. })
  548. })
  549. describe("ProviderTransform.schema - gemini nested array items", () => {
  550. const geminiModel = {
  551. providerID: "google",
  552. api: {
  553. id: "gemini-3-pro",
  554. },
  555. } as any
  556. test("adds type to 2D array with empty inner items", () => {
  557. const schema = {
  558. type: "object",
  559. properties: {
  560. values: {
  561. type: "array",
  562. items: {
  563. type: "array",
  564. items: {}, // Empty items object
  565. },
  566. },
  567. },
  568. } as any
  569. const result = ProviderTransform.schema(geminiModel, schema) as any
  570. // Inner items should have a default type
  571. expect(result.properties.values.items.items.type).toBe("string")
  572. })
  573. test("adds items and type to 2D array with missing inner items", () => {
  574. const schema = {
  575. type: "object",
  576. properties: {
  577. data: {
  578. type: "array",
  579. items: { type: "array" }, // No items at all
  580. },
  581. },
  582. } as any
  583. const result = ProviderTransform.schema(geminiModel, schema) as any
  584. expect(result.properties.data.items.items).toBeDefined()
  585. expect(result.properties.data.items.items.type).toBe("string")
  586. })
  587. test("handles deeply nested arrays (3D)", () => {
  588. const schema = {
  589. type: "object",
  590. properties: {
  591. matrix: {
  592. type: "array",
  593. items: {
  594. type: "array",
  595. items: {
  596. type: "array",
  597. // No items
  598. },
  599. },
  600. },
  601. },
  602. } as any
  603. const result = ProviderTransform.schema(geminiModel, schema) as any
  604. expect(result.properties.matrix.items.items.items).toBeDefined()
  605. expect(result.properties.matrix.items.items.items.type).toBe("string")
  606. })
  607. test("preserves existing item types in nested arrays", () => {
  608. const schema = {
  609. type: "object",
  610. properties: {
  611. numbers: {
  612. type: "array",
  613. items: {
  614. type: "array",
  615. items: { type: "number" }, // Has explicit type
  616. },
  617. },
  618. },
  619. } as any
  620. const result = ProviderTransform.schema(geminiModel, schema) as any
  621. // Should preserve the explicit type
  622. expect(result.properties.numbers.items.items.type).toBe("number")
  623. })
  624. test("handles mixed nested structures with objects and arrays", () => {
  625. const schema = {
  626. type: "object",
  627. properties: {
  628. spreadsheetData: {
  629. type: "object",
  630. properties: {
  631. rows: {
  632. type: "array",
  633. items: {
  634. type: "array",
  635. items: {}, // Empty items
  636. },
  637. },
  638. },
  639. },
  640. },
  641. } as any
  642. const result = ProviderTransform.schema(geminiModel, schema) as any
  643. expect(result.properties.spreadsheetData.properties.rows.items.items.type).toBe("string")
  644. })
  645. })
  646. describe("ProviderTransform.schema - gemini combiner nodes", () => {
  647. const geminiModel = {
  648. providerID: "google",
  649. api: {
  650. id: "gemini-3-pro",
  651. },
  652. } as any
  653. const walk = (node: any, cb: (node: any, path: (string | number)[]) => void, path: (string | number)[] = []) => {
  654. if (node === null || typeof node !== "object") {
  655. return
  656. }
  657. if (Array.isArray(node)) {
  658. node.forEach((item, i) => walk(item, cb, [...path, i]))
  659. return
  660. }
  661. cb(node, path)
  662. Object.entries(node).forEach(([key, value]) => walk(value, cb, [...path, key]))
  663. }
  664. test("keeps edits.items.anyOf without adding type", () => {
  665. const schema = {
  666. type: "object",
  667. properties: {
  668. edits: {
  669. type: "array",
  670. items: {
  671. anyOf: [
  672. {
  673. type: "object",
  674. properties: {
  675. old_string: { type: "string" },
  676. new_string: { type: "string" },
  677. },
  678. required: ["old_string", "new_string"],
  679. },
  680. {
  681. type: "object",
  682. properties: {
  683. old_string: { type: "string" },
  684. new_string: { type: "string" },
  685. replace_all: { type: "boolean" },
  686. },
  687. required: ["old_string", "new_string"],
  688. },
  689. ],
  690. },
  691. },
  692. },
  693. required: ["edits"],
  694. } as any
  695. const result = ProviderTransform.schema(geminiModel, schema) as any
  696. expect(Array.isArray(result.properties.edits.items.anyOf)).toBe(true)
  697. expect(result.properties.edits.items.type).toBeUndefined()
  698. })
  699. test("does not add sibling keys to combiner nodes during sanitize", () => {
  700. const schema = {
  701. type: "object",
  702. properties: {
  703. edits: {
  704. type: "array",
  705. items: {
  706. anyOf: [{ type: "string" }, { type: "number" }],
  707. },
  708. },
  709. value: {
  710. oneOf: [{ type: "string" }, { type: "boolean" }],
  711. },
  712. meta: {
  713. allOf: [
  714. {
  715. type: "object",
  716. properties: { a: { type: "string" } },
  717. },
  718. {
  719. type: "object",
  720. properties: { b: { type: "string" } },
  721. },
  722. ],
  723. },
  724. },
  725. } as any
  726. const input = JSON.parse(JSON.stringify(schema))
  727. const result = ProviderTransform.schema(geminiModel, schema) as any
  728. walk(result, (node, path) => {
  729. const hasCombiner = Array.isArray(node.anyOf) || Array.isArray(node.oneOf) || Array.isArray(node.allOf)
  730. if (!hasCombiner) {
  731. return
  732. }
  733. const before = path.reduce((acc: any, key) => acc?.[key], input)
  734. const added = Object.keys(node).filter((key) => !(key in before))
  735. expect(added).toEqual([])
  736. })
  737. })
  738. })
  739. describe("ProviderTransform.schema - gemini non-object properties removal", () => {
  740. const geminiModel = {
  741. providerID: "google",
  742. api: {
  743. id: "gemini-3-pro",
  744. },
  745. } as any
  746. test("removes properties from non-object types", () => {
  747. const schema = {
  748. type: "object",
  749. properties: {
  750. data: {
  751. type: "string",
  752. properties: { invalid: { type: "string" } },
  753. },
  754. },
  755. } as any
  756. const result = ProviderTransform.schema(geminiModel, schema) as any
  757. expect(result.properties.data.type).toBe("string")
  758. expect(result.properties.data.properties).toBeUndefined()
  759. })
  760. test("removes required from non-object types", () => {
  761. const schema = {
  762. type: "object",
  763. properties: {
  764. data: {
  765. type: "array",
  766. items: { type: "string" },
  767. required: ["invalid"],
  768. },
  769. },
  770. } as any
  771. const result = ProviderTransform.schema(geminiModel, schema) as any
  772. expect(result.properties.data.type).toBe("array")
  773. expect(result.properties.data.required).toBeUndefined()
  774. })
  775. test("removes properties and required from nested non-object types", () => {
  776. const schema = {
  777. type: "object",
  778. properties: {
  779. outer: {
  780. type: "object",
  781. properties: {
  782. inner: {
  783. type: "number",
  784. properties: { bad: { type: "string" } },
  785. required: ["bad"],
  786. },
  787. },
  788. },
  789. },
  790. } as any
  791. const result = ProviderTransform.schema(geminiModel, schema) as any
  792. expect(result.properties.outer.properties.inner.type).toBe("number")
  793. expect(result.properties.outer.properties.inner.properties).toBeUndefined()
  794. expect(result.properties.outer.properties.inner.required).toBeUndefined()
  795. })
  796. test("keeps properties and required on object types", () => {
  797. const schema = {
  798. type: "object",
  799. properties: {
  800. data: {
  801. type: "object",
  802. properties: { name: { type: "string" } },
  803. required: ["name"],
  804. },
  805. },
  806. } as any
  807. const result = ProviderTransform.schema(geminiModel, schema) as any
  808. expect(result.properties.data.type).toBe("object")
  809. expect(result.properties.data.properties).toBeDefined()
  810. expect(result.properties.data.required).toEqual(["name"])
  811. })
  812. test("does not affect non-gemini providers", () => {
  813. const openaiModel = {
  814. providerID: "openai",
  815. api: {
  816. id: "gpt-4",
  817. },
  818. } as any
  819. const schema = {
  820. type: "object",
  821. properties: {
  822. data: {
  823. type: "string",
  824. properties: { invalid: { type: "string" } },
  825. },
  826. },
  827. } as any
  828. const result = ProviderTransform.schema(openaiModel, schema) as any
  829. expect(result.properties.data.properties).toBeDefined()
  830. })
  831. })
  832. describe("ProviderTransform.schema - moonshot $ref siblings", () => {
  833. const moonshotModel = {
  834. providerID: "moonshotai",
  835. api: {
  836. id: "kimi-k2",
  837. },
  838. } as any
  839. test("removes sibling descriptions from referenced tool parameter schemas", () => {
  840. const schema = {
  841. type: "object",
  842. properties: {
  843. deviceType: {
  844. description: "Optional. The type of device that captured the screenshot, e.g. mobile or desktop.",
  845. enum: ["DEVICE_TYPE_UNSPECIFIED", "MOBILE", "DESKTOP", "TABLET", "AGNOSTIC"],
  846. type: "string",
  847. },
  848. modelId: {
  849. description: "Optional. The model to use for generation.",
  850. enum: ["MODEL_ID_UNSPECIFIED", "GEMINI_3_PRO", "GEMINI_3_FLASH", "GEMINI_3_1_PRO"],
  851. type: "string",
  852. },
  853. projectId: {
  854. description: "Required. The project ID of screens to generate variants for.",
  855. type: "string",
  856. },
  857. prompt: {
  858. description: "Required. The input text used to generate the variants.",
  859. type: "string",
  860. },
  861. selectedScreenIds: {
  862. description: "Required. The screen ids of screen to generate variants for.",
  863. items: {
  864. type: "string",
  865. },
  866. type: "array",
  867. },
  868. variantOptions: {
  869. $ref: "#/$defs/VariantOptions",
  870. description:
  871. "Required. The variant options for generation, including the number of variants, creative range, and aspects to focus on.",
  872. },
  873. },
  874. required: ["projectId", "selectedScreenIds", "prompt", "variantOptions"],
  875. $defs: {
  876. VariantOptions: {
  877. description:
  878. "Configuration options for design variant generation. This message captures all parameters used to generate variants, allowing the configuration to be stored, replayed, or analyzed.",
  879. properties: {
  880. aspects: {
  881. description: "Optional. Specific aspects to focus on. If empty, all aspects may be varied.",
  882. items: {
  883. enum: ["VARIANT_ASPECT_UNSPECIFIED", "LAYOUT", "COLOR_SCHEME", "IMAGES", "TEXT_FONT", "TEXT_CONTENT"],
  884. type: "string",
  885. },
  886. type: "array",
  887. },
  888. creativeRange: {
  889. description: "Optional. Creative range for variations. Default: EXPLORE",
  890. enum: ["CREATIVE_RANGE_UNSPECIFIED", "REFINE", "EXPLORE", "REIMAGINE"],
  891. type: "string",
  892. },
  893. variantCount: {
  894. description: "Optional. Number of variants to generate (1-5). Default: 3",
  895. format: "int32",
  896. type: "integer",
  897. },
  898. },
  899. type: "object",
  900. },
  901. },
  902. description: "Request message for GenerateVariants.",
  903. additionalProperties: false,
  904. } as any
  905. const result = ProviderTransform.schema(moonshotModel, schema) as any
  906. expect(result.properties.variantOptions).toEqual({
  907. $ref: "#/$defs/VariantOptions",
  908. })
  909. expect(result.$defs.VariantOptions.description).toBe(schema.$defs.VariantOptions.description)
  910. })
  911. test("also runs for kimi models outside the moonshot provider", () => {
  912. const result = ProviderTransform.schema(
  913. {
  914. providerID: "openrouter",
  915. name: "Kimi K2",
  916. api: {
  917. id: "moonshotai/kimi-k2",
  918. },
  919. } as any,
  920. {
  921. type: "object",
  922. properties: {
  923. value: {
  924. $ref: "#/$defs/Value",
  925. description: "Moonshot rejects this sibling after ref expansion.",
  926. },
  927. },
  928. $defs: {
  929. Value: {
  930. description: "Referenced schema description stays here.",
  931. type: "object",
  932. },
  933. },
  934. } as any,
  935. ) as any
  936. expect(result.properties.value).toEqual({
  937. $ref: "#/$defs/Value",
  938. })
  939. })
  940. test("converts tuple-style array items to a single item schema", () => {
  941. const result = ProviderTransform.schema(moonshotModel, {
  942. type: "object",
  943. properties: {
  944. codeSpec: {
  945. type: "object",
  946. properties: {
  947. accessibility: {
  948. type: "object",
  949. properties: {
  950. renderedSize: {
  951. description: "Rendered size [width, height] in px",
  952. type: "array",
  953. items: [{ type: "number" }, { type: "number" }],
  954. minItems: 2,
  955. maxItems: 2,
  956. },
  957. },
  958. },
  959. },
  960. },
  961. },
  962. } as any) as any
  963. expect(result.properties.codeSpec.properties.accessibility.properties.renderedSize.items).toEqual({
  964. type: "number",
  965. })
  966. })
  967. })
  968. describe("ProviderTransform.message - DeepSeek reasoning content", () => {
  969. test("DeepSeek with tool calls includes reasoning_content in providerOptions", () => {
  970. const msgs = [
  971. {
  972. role: "assistant",
  973. content: [
  974. { type: "reasoning", text: "Let me think about this..." },
  975. {
  976. type: "tool-call",
  977. toolCallId: "test",
  978. toolName: "bash",
  979. input: { command: "echo hello" },
  980. },
  981. ],
  982. },
  983. ] as any[]
  984. const result = ProviderTransform.message(
  985. msgs,
  986. {
  987. id: ProviderV2.ModelID.make("deepseek/deepseek-chat"),
  988. providerID: ProviderV2.ID.make("deepseek"),
  989. api: {
  990. id: "deepseek-chat",
  991. url: "https://api.deepseek.com",
  992. npm: "@ai-sdk/openai-compatible",
  993. },
  994. name: "DeepSeek Chat",
  995. capabilities: {
  996. temperature: true,
  997. reasoning: true,
  998. attachment: false,
  999. toolcall: true,
  1000. input: { text: true, audio: false, image: false, video: false, pdf: false },
  1001. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1002. interleaved: {
  1003. field: "reasoning_content",
  1004. },
  1005. },
  1006. cost: {
  1007. input: 0.001,
  1008. output: 0.002,
  1009. cache: { read: 0.0001, write: 0.0002 },
  1010. },
  1011. limit: {
  1012. context: 128000,
  1013. output: 8192,
  1014. },
  1015. status: "active",
  1016. options: {},
  1017. headers: {},
  1018. release_date: "2023-04-01",
  1019. },
  1020. {},
  1021. )
  1022. expect(result).toHaveLength(1)
  1023. expect(result[0].content).toEqual([
  1024. {
  1025. type: "tool-call",
  1026. toolCallId: "test",
  1027. toolName: "bash",
  1028. input: { command: "echo hello" },
  1029. },
  1030. ])
  1031. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBe("Let me think about this...")
  1032. })
  1033. test("Non-DeepSeek providers leave reasoning content unchanged", () => {
  1034. const msgs = [
  1035. {
  1036. role: "assistant",
  1037. content: [
  1038. { type: "reasoning", text: "Should not be processed" },
  1039. { type: "text", text: "Answer" },
  1040. ],
  1041. },
  1042. ] as any[]
  1043. const result = ProviderTransform.message(
  1044. msgs,
  1045. {
  1046. id: ProviderV2.ModelID.make("openai/gpt-4"),
  1047. providerID: ProviderV2.ID.make("openai"),
  1048. api: {
  1049. id: "gpt-4",
  1050. url: "https://api.openai.com",
  1051. npm: "@ai-sdk/openai",
  1052. },
  1053. name: "GPT-4",
  1054. capabilities: {
  1055. temperature: true,
  1056. reasoning: false,
  1057. attachment: true,
  1058. toolcall: true,
  1059. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1060. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1061. interleaved: false,
  1062. },
  1063. cost: {
  1064. input: 0.03,
  1065. output: 0.06,
  1066. cache: { read: 0.001, write: 0.002 },
  1067. },
  1068. limit: {
  1069. context: 128000,
  1070. output: 4096,
  1071. },
  1072. status: "active",
  1073. options: {},
  1074. headers: {},
  1075. release_date: "2023-04-01",
  1076. },
  1077. {},
  1078. )
  1079. expect(result[0].content).toEqual([
  1080. { type: "reasoning", text: "Should not be processed" },
  1081. { type: "text", text: "Answer" },
  1082. ])
  1083. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBeUndefined()
  1084. })
  1085. })
  1086. describe("ProviderTransform.message - surrogate sanitization", () => {
  1087. const model = {
  1088. id: "test/test-model",
  1089. providerID: "test",
  1090. api: {
  1091. id: "test-model",
  1092. url: "https://api.test.com",
  1093. npm: "@ai-sdk/openai-compatible",
  1094. },
  1095. name: "Test Model",
  1096. capabilities: {
  1097. temperature: true,
  1098. reasoning: true,
  1099. attachment: true,
  1100. toolcall: true,
  1101. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1102. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1103. interleaved: false,
  1104. },
  1105. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  1106. limit: { context: 128000, output: 8192 },
  1107. status: "active",
  1108. options: {},
  1109. headers: {},
  1110. } as any
  1111. test("replaces lone surrogates in model-visible text", () => {
  1112. const lone = "\uD83D"
  1113. const valid = "🚀"
  1114. const sanitized = "�"
  1115. const text = (label: string) => `${label} ${lone} and ${valid}`
  1116. const expected = (label: string) => `${label} ${sanitized} and ${valid}`
  1117. const msgs = [
  1118. { role: "system", content: text("system") },
  1119. { role: "user", content: text("user string") },
  1120. {
  1121. role: "user",
  1122. content: [
  1123. { type: "text", text: text("user text") },
  1124. { type: "image", image: "data:image/png;base64,abcd" },
  1125. ],
  1126. },
  1127. { role: "assistant", content: text("assistant string") },
  1128. {
  1129. role: "assistant",
  1130. content: [
  1131. { type: "text", text: text("assistant text") },
  1132. { type: "reasoning", text: text("assistant reasoning") },
  1133. { type: "tool-call", toolCallId: "call-1", toolName: "Read", input: { filePath: ".opencode/tool/emoji.ts" } },
  1134. {
  1135. type: "tool-result",
  1136. toolCallId: "call-2",
  1137. toolName: "Read",
  1138. output: { type: "text", value: text("assistant tool text") },
  1139. },
  1140. {
  1141. type: "tool-result",
  1142. toolCallId: "call-3",
  1143. toolName: "Read",
  1144. output: { type: "error-text", value: text("assistant tool error") },
  1145. },
  1146. {
  1147. type: "tool-result",
  1148. toolCallId: "call-4",
  1149. toolName: "Read",
  1150. output: { type: "content", value: [{ type: "text", text: text("assistant tool content") }] },
  1151. },
  1152. ],
  1153. },
  1154. {
  1155. role: "tool",
  1156. content: [
  1157. {
  1158. type: "tool-result",
  1159. toolCallId: "call-5",
  1160. toolName: "Read",
  1161. output: { type: "text", value: text("tool text") },
  1162. },
  1163. {
  1164. type: "tool-result",
  1165. toolCallId: "call-6",
  1166. toolName: "Read",
  1167. output: { type: "error-text", value: text("tool error") },
  1168. },
  1169. {
  1170. type: "tool-result",
  1171. toolCallId: "call-7",
  1172. toolName: "Read",
  1173. output: { type: "content", value: [{ type: "text", text: text("tool content") }] },
  1174. },
  1175. ],
  1176. },
  1177. ] as any[]
  1178. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1179. expect(result[0].content).toBe(expected("system"))
  1180. expect(result[1].content).toBe(expected("user string"))
  1181. expect(result[2].content[0].text).toBe(expected("user text"))
  1182. expect(result[3].content).toBe(expected("assistant string"))
  1183. expect(result[4].content[0].text).toBe(expected("assistant text"))
  1184. expect(result[4].content[1].text).toBe(expected("assistant reasoning"))
  1185. expect(result[4].content[3].output.value).toBe(expected("assistant tool text"))
  1186. expect(result[4].content[4].output.value).toBe(expected("assistant tool error"))
  1187. expect(result[4].content[5].output.value[0].text).toBe(expected("assistant tool content"))
  1188. expect(result[5].content[0].output.value).toBe(expected("tool text"))
  1189. expect(result[5].content[1].output.value).toBe(expected("tool error"))
  1190. expect(result[5].content[2].output.value[0].text).toBe(expected("tool content"))
  1191. expect(result[2].content[1]).toEqual({ type: "image", image: "data:image/png;base64,abcd" })
  1192. })
  1193. })
  1194. describe("ProviderTransform.message - empty image handling", () => {
  1195. const mockModel = {
  1196. id: "anthropic/claude-3-5-sonnet",
  1197. providerID: "anthropic",
  1198. api: {
  1199. id: "claude-3-5-sonnet-20241022",
  1200. url: "https://api.anthropic.com",
  1201. npm: "@ai-sdk/anthropic",
  1202. },
  1203. name: "Claude 3.5 Sonnet",
  1204. capabilities: {
  1205. temperature: true,
  1206. reasoning: false,
  1207. attachment: true,
  1208. toolcall: true,
  1209. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1210. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1211. interleaved: false,
  1212. },
  1213. cost: {
  1214. input: 0.003,
  1215. output: 0.015,
  1216. cache: { read: 0.0003, write: 0.00375 },
  1217. },
  1218. limit: {
  1219. context: 200000,
  1220. output: 8192,
  1221. },
  1222. status: "active",
  1223. options: {},
  1224. headers: {},
  1225. } as any
  1226. test("should replace empty base64 image with error text", () => {
  1227. const msgs = [
  1228. {
  1229. role: "user",
  1230. content: [
  1231. { type: "text", text: "What is in this image?" },
  1232. { type: "image", image: "data:image/png;base64," },
  1233. ],
  1234. },
  1235. ] as any[]
  1236. const result = ProviderTransform.message(msgs, mockModel, {})
  1237. expect(result).toHaveLength(1)
  1238. expect(result[0].content).toHaveLength(2)
  1239. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  1240. expect(result[0].content[1]).toEqual({
  1241. type: "text",
  1242. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  1243. })
  1244. })
  1245. test("should keep valid base64 images unchanged", () => {
  1246. const validBase64 =
  1247. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  1248. const msgs = [
  1249. {
  1250. role: "user",
  1251. content: [
  1252. { type: "text", text: "What is in this image?" },
  1253. { type: "image", image: `data:image/png;base64,${validBase64}` },
  1254. ],
  1255. },
  1256. ] as any[]
  1257. const result = ProviderTransform.message(msgs, mockModel, {})
  1258. expect(result).toHaveLength(1)
  1259. expect(result[0].content).toHaveLength(2)
  1260. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  1261. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  1262. })
  1263. test("should handle mixed valid and empty images", () => {
  1264. const validBase64 =
  1265. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  1266. const msgs = [
  1267. {
  1268. role: "user",
  1269. content: [
  1270. { type: "text", text: "Compare these images" },
  1271. { type: "image", image: `data:image/png;base64,${validBase64}` },
  1272. { type: "image", image: "data:image/jpeg;base64," },
  1273. ],
  1274. },
  1275. ] as any[]
  1276. const result = ProviderTransform.message(msgs, mockModel, {})
  1277. expect(result).toHaveLength(1)
  1278. expect(result[0].content).toHaveLength(3)
  1279. expect(result[0].content[0]).toEqual({ type: "text", text: "Compare these images" })
  1280. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  1281. expect(result[0].content[2]).toEqual({
  1282. type: "text",
  1283. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  1284. })
  1285. })
  1286. })
  1287. describe("ProviderTransform.message - anthropic empty content filtering", () => {
  1288. const anthropicModel = {
  1289. id: "anthropic/claude-3-5-sonnet",
  1290. providerID: "anthropic",
  1291. api: {
  1292. id: "claude-3-5-sonnet-20241022",
  1293. url: "https://api.anthropic.com",
  1294. npm: "@ai-sdk/anthropic",
  1295. },
  1296. name: "Claude 3.5 Sonnet",
  1297. capabilities: {
  1298. temperature: true,
  1299. reasoning: false,
  1300. attachment: true,
  1301. toolcall: true,
  1302. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1303. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1304. interleaved: false,
  1305. },
  1306. cost: {
  1307. input: 0.003,
  1308. output: 0.015,
  1309. cache: { read: 0.0003, write: 0.00375 },
  1310. },
  1311. limit: {
  1312. context: 200000,
  1313. output: 8192,
  1314. },
  1315. status: "active",
  1316. options: {},
  1317. headers: {},
  1318. } as any
  1319. test("filters out messages with empty string content", () => {
  1320. const msgs = [
  1321. { role: "user", content: "Hello" },
  1322. { role: "assistant", content: "" },
  1323. { role: "user", content: "World" },
  1324. ] as any[]
  1325. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1326. expect(result).toHaveLength(2)
  1327. expect(result[0].content).toBe("Hello")
  1328. expect(result[1].content).toBe("World")
  1329. })
  1330. test("filters out empty text parts from array content", () => {
  1331. const msgs = [
  1332. {
  1333. role: "assistant",
  1334. content: [
  1335. { type: "text", text: "" },
  1336. { type: "text", text: "Hello" },
  1337. { type: "text", text: "" },
  1338. ],
  1339. },
  1340. ] as any[]
  1341. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1342. expect(result).toHaveLength(1)
  1343. expect(result[0].content).toHaveLength(1)
  1344. expect(result[0].content[0]).toEqual({ type: "text", text: "Hello" })
  1345. })
  1346. test("filters out empty reasoning parts from array content", () => {
  1347. const msgs = [
  1348. {
  1349. role: "assistant",
  1350. content: [
  1351. { type: "reasoning", text: "" },
  1352. { type: "text", text: "Answer" },
  1353. { type: "reasoning", text: "" },
  1354. ],
  1355. },
  1356. ] as any[]
  1357. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1358. expect(result).toHaveLength(1)
  1359. expect(result[0].content).toHaveLength(1)
  1360. expect(result[0].content[0]).toEqual({ type: "text", text: "Answer" })
  1361. })
  1362. test("removes entire message when all parts are empty", () => {
  1363. const msgs = [
  1364. { role: "user", content: "Hello" },
  1365. {
  1366. role: "assistant",
  1367. content: [
  1368. { type: "text", text: "" },
  1369. { type: "reasoning", text: "" },
  1370. ],
  1371. },
  1372. { role: "user", content: "World" },
  1373. ] as any[]
  1374. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1375. expect(result).toHaveLength(2)
  1376. expect(result[0].content).toBe("Hello")
  1377. expect(result[1].content).toBe("World")
  1378. })
  1379. test("keeps non-text/reasoning parts even if text parts are empty", () => {
  1380. const msgs = [
  1381. {
  1382. role: "assistant",
  1383. content: [
  1384. { type: "text", text: "" },
  1385. { type: "tool-call", toolCallId: "123", toolName: "bash", input: { command: "ls" } },
  1386. ],
  1387. },
  1388. ] as any[]
  1389. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1390. expect(result).toHaveLength(1)
  1391. expect(result[0].content).toHaveLength(1)
  1392. expect(result[0].content[0]).toEqual({
  1393. type: "tool-call",
  1394. toolCallId: "123",
  1395. toolName: "bash",
  1396. input: { command: "ls" },
  1397. })
  1398. })
  1399. test("keeps messages with valid text alongside empty parts", () => {
  1400. const msgs = [
  1401. {
  1402. role: "assistant",
  1403. content: [
  1404. { type: "reasoning", text: "Thinking..." },
  1405. { type: "text", text: "" },
  1406. { type: "text", text: "Result" },
  1407. ],
  1408. },
  1409. ] as any[]
  1410. const result = ProviderTransform.message(msgs, anthropicModel, {})
  1411. expect(result).toHaveLength(1)
  1412. expect(result[0].content).toHaveLength(2)
  1413. expect(result[0].content[0]).toEqual({ type: "reasoning", text: "Thinking..." })
  1414. expect(result[0].content[1]).toEqual({ type: "text", text: "Result" })
  1415. })
  1416. test("filters empty content for bedrock provider", () => {
  1417. const bedrockModel = {
  1418. ...anthropicModel,
  1419. id: "amazon-bedrock/anthropic.claude-opus-4-6",
  1420. providerID: "amazon-bedrock",
  1421. api: {
  1422. id: "anthropic.claude-opus-4-6",
  1423. url: "https://bedrock-runtime.us-east-1.amazonaws.com",
  1424. npm: "@ai-sdk/amazon-bedrock",
  1425. },
  1426. }
  1427. const msgs = [
  1428. { role: "user", content: "Hello" },
  1429. { role: "assistant", content: "" },
  1430. {
  1431. role: "assistant",
  1432. content: [
  1433. { type: "text", text: "" },
  1434. { type: "text", text: "Answer" },
  1435. ],
  1436. },
  1437. ] as any[]
  1438. const result = ProviderTransform.message(msgs, bedrockModel, {})
  1439. expect(result).toHaveLength(2)
  1440. expect(result[0].content).toBe("Hello")
  1441. expect(result[1].content).toHaveLength(1)
  1442. expect(result[1].content[0]).toEqual({ type: "text", text: "Answer" })
  1443. })
  1444. test("does not filter for non-anthropic providers", () => {
  1445. const openaiModel = {
  1446. ...anthropicModel,
  1447. providerID: "openai",
  1448. api: {
  1449. id: "gpt-4",
  1450. url: "https://api.openai.com",
  1451. npm: "@ai-sdk/openai",
  1452. },
  1453. }
  1454. const msgs = [
  1455. { role: "assistant", content: "" },
  1456. {
  1457. role: "assistant",
  1458. content: [{ type: "text", text: "" }],
  1459. },
  1460. ] as any[]
  1461. const result = ProviderTransform.message(msgs, openaiModel, {})
  1462. expect(result).toHaveLength(2)
  1463. expect(result[0].content).toBe("")
  1464. expect(result[1].content).toHaveLength(1)
  1465. })
  1466. test("leaves valid anthropic assistant tool ordering unchanged", () => {
  1467. const msgs = [
  1468. {
  1469. role: "assistant",
  1470. content: [
  1471. { type: "text", text: "I checked your home directory and looked for PDF files." },
  1472. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1473. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1474. ],
  1475. },
  1476. ] as any[]
  1477. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  1478. expect(result).toHaveLength(1)
  1479. expect(result[0].content).toMatchObject([
  1480. { type: "text", text: "I checked your home directory and looked for PDF files." },
  1481. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1482. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1483. ])
  1484. })
  1485. })
  1486. describe("ProviderTransform.message - strip openai metadata when store=false", () => {
  1487. const openaiModel = {
  1488. id: "openai/gpt-5",
  1489. providerID: "openai",
  1490. api: {
  1491. id: "gpt-5",
  1492. url: "https://api.openai.com",
  1493. npm: "@ai-sdk/openai",
  1494. },
  1495. name: "GPT-5",
  1496. capabilities: {
  1497. temperature: true,
  1498. reasoning: true,
  1499. attachment: true,
  1500. toolcall: true,
  1501. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1502. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1503. interleaved: false,
  1504. },
  1505. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  1506. limit: { context: 128000, output: 4096 },
  1507. status: "active",
  1508. options: {},
  1509. headers: {},
  1510. } as any
  1511. test("preserves itemId and reasoningEncryptedContent when store=false", () => {
  1512. const msgs = [
  1513. {
  1514. role: "assistant",
  1515. content: [
  1516. {
  1517. type: "reasoning",
  1518. text: "thinking...",
  1519. providerOptions: {
  1520. openai: {
  1521. itemId: "rs_123",
  1522. reasoningEncryptedContent: "encrypted",
  1523. },
  1524. },
  1525. },
  1526. {
  1527. type: "text",
  1528. text: "Hello",
  1529. providerOptions: {
  1530. openai: {
  1531. itemId: "msg_456",
  1532. },
  1533. },
  1534. },
  1535. ],
  1536. },
  1537. ] as any[]
  1538. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  1539. expect(result).toHaveLength(1)
  1540. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("rs_123")
  1541. expect(result[0].content[1].providerOptions?.openai?.itemId).toBe("msg_456")
  1542. })
  1543. test("preserves itemId and reasoningEncryptedContent when store=false even when not openai", () => {
  1544. const zenModel = {
  1545. ...openaiModel,
  1546. providerID: "zen",
  1547. }
  1548. const msgs = [
  1549. {
  1550. role: "assistant",
  1551. content: [
  1552. {
  1553. type: "reasoning",
  1554. text: "thinking...",
  1555. providerOptions: {
  1556. openai: {
  1557. itemId: "rs_123",
  1558. reasoningEncryptedContent: "encrypted",
  1559. },
  1560. },
  1561. },
  1562. {
  1563. type: "text",
  1564. text: "Hello",
  1565. providerOptions: {
  1566. openai: {
  1567. itemId: "msg_456",
  1568. },
  1569. },
  1570. },
  1571. ],
  1572. },
  1573. ] as any[]
  1574. const result = ProviderTransform.message(msgs, zenModel, { store: false }) as any[]
  1575. expect(result).toHaveLength(1)
  1576. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("rs_123")
  1577. expect(result[0].content[1].providerOptions?.openai?.itemId).toBe("msg_456")
  1578. })
  1579. test("preserves other openai options including itemId", () => {
  1580. const msgs = [
  1581. {
  1582. role: "assistant",
  1583. content: [
  1584. {
  1585. type: "text",
  1586. text: "Hello",
  1587. providerOptions: {
  1588. openai: {
  1589. itemId: "msg_123",
  1590. otherOption: "value",
  1591. },
  1592. },
  1593. },
  1594. ],
  1595. },
  1596. ] as any[]
  1597. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  1598. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1599. expect(result[0].content[0].providerOptions?.openai?.otherOption).toBe("value")
  1600. })
  1601. test("preserves metadata for openai package when store is true", () => {
  1602. const msgs = [
  1603. {
  1604. role: "assistant",
  1605. content: [
  1606. {
  1607. type: "text",
  1608. text: "Hello",
  1609. providerOptions: {
  1610. openai: {
  1611. itemId: "msg_123",
  1612. },
  1613. },
  1614. },
  1615. ],
  1616. },
  1617. ] as any[]
  1618. // openai package preserves itemId regardless of store value
  1619. const result = ProviderTransform.message(msgs, openaiModel, { store: true }) as any[]
  1620. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1621. })
  1622. test("preserves metadata for non-openai packages when store is false", () => {
  1623. const anthropicModel = {
  1624. ...openaiModel,
  1625. providerID: "anthropic",
  1626. api: {
  1627. id: "claude-3",
  1628. url: "https://api.anthropic.com",
  1629. npm: "@ai-sdk/anthropic",
  1630. },
  1631. }
  1632. const msgs = [
  1633. {
  1634. role: "assistant",
  1635. content: [
  1636. {
  1637. type: "text",
  1638. text: "Hello",
  1639. providerOptions: {
  1640. openai: {
  1641. itemId: "msg_123",
  1642. },
  1643. },
  1644. },
  1645. ],
  1646. },
  1647. ] as any[]
  1648. // store=false preserves metadata for non-openai packages
  1649. const result = ProviderTransform.message(msgs, anthropicModel, { store: false }) as any[]
  1650. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1651. })
  1652. test("preserves metadata using providerID key when store is false", () => {
  1653. const opencodeModel = {
  1654. ...openaiModel,
  1655. providerID: "opencode",
  1656. api: {
  1657. id: "opencode-test",
  1658. url: "https://api.opencode.ai",
  1659. npm: "@ai-sdk/openai-compatible",
  1660. },
  1661. }
  1662. const msgs = [
  1663. {
  1664. role: "assistant",
  1665. content: [
  1666. {
  1667. type: "text",
  1668. text: "Hello",
  1669. providerOptions: {
  1670. opencode: {
  1671. itemId: "msg_123",
  1672. otherOption: "value",
  1673. },
  1674. },
  1675. },
  1676. ],
  1677. },
  1678. ] as any[]
  1679. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  1680. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_123")
  1681. expect(result[0].content[0].providerOptions?.opencode?.otherOption).toBe("value")
  1682. })
  1683. test("preserves itemId across all providerOptions keys", () => {
  1684. const opencodeModel = {
  1685. ...openaiModel,
  1686. providerID: "opencode",
  1687. api: {
  1688. id: "opencode-test",
  1689. url: "https://api.opencode.ai",
  1690. npm: "@ai-sdk/openai-compatible",
  1691. },
  1692. }
  1693. const msgs = [
  1694. {
  1695. role: "assistant",
  1696. providerOptions: {
  1697. openai: { itemId: "msg_root" },
  1698. opencode: { itemId: "msg_opencode" },
  1699. extra: { itemId: "msg_extra" },
  1700. },
  1701. content: [
  1702. {
  1703. type: "text",
  1704. text: "Hello",
  1705. providerOptions: {
  1706. openai: { itemId: "msg_openai_part" },
  1707. opencode: { itemId: "msg_opencode_part" },
  1708. extra: { itemId: "msg_extra_part" },
  1709. },
  1710. },
  1711. ],
  1712. },
  1713. ] as any[]
  1714. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  1715. expect(result[0].providerOptions?.openai?.itemId).toBe("msg_root")
  1716. expect(result[0].providerOptions?.opencode?.itemId).toBe("msg_opencode")
  1717. expect(result[0].providerOptions?.extra?.itemId).toBe("msg_extra")
  1718. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_openai_part")
  1719. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_opencode_part")
  1720. expect(result[0].content[0].providerOptions?.extra?.itemId).toBe("msg_extra_part")
  1721. })
  1722. test("does not strip metadata for non-openai packages when store is not false", () => {
  1723. const anthropicModel = {
  1724. ...openaiModel,
  1725. providerID: "anthropic",
  1726. api: {
  1727. id: "claude-3",
  1728. url: "https://api.anthropic.com",
  1729. npm: "@ai-sdk/anthropic",
  1730. },
  1731. }
  1732. const msgs = [
  1733. {
  1734. role: "assistant",
  1735. content: [
  1736. {
  1737. type: "text",
  1738. text: "Hello",
  1739. providerOptions: {
  1740. openai: {
  1741. itemId: "msg_123",
  1742. },
  1743. },
  1744. },
  1745. ],
  1746. },
  1747. ] as any[]
  1748. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  1749. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1750. })
  1751. })
  1752. describe("ProviderTransform.message - providerOptions key remapping", () => {
  1753. const createModel = (providerID: string, npm: string) =>
  1754. ({
  1755. id: `${providerID}/test-model`,
  1756. providerID,
  1757. api: {
  1758. id: "test-model",
  1759. url: "https://api.test.com",
  1760. npm,
  1761. },
  1762. name: "Test Model",
  1763. capabilities: {
  1764. temperature: true,
  1765. reasoning: false,
  1766. attachment: true,
  1767. toolcall: true,
  1768. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1769. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1770. interleaved: false,
  1771. },
  1772. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  1773. limit: { context: 128000, output: 8192 },
  1774. status: "active",
  1775. options: {},
  1776. headers: {},
  1777. }) as any
  1778. test("azure keeps 'azure' key and does not remap to 'openai'", () => {
  1779. const model = createModel("azure", "@ai-sdk/azure")
  1780. const msgs = [
  1781. {
  1782. role: "user",
  1783. content: "Hello",
  1784. providerOptions: {
  1785. azure: { someOption: "value" },
  1786. },
  1787. },
  1788. ] as any[]
  1789. const result = ProviderTransform.message(msgs, model, {})
  1790. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  1791. expect(result[0].providerOptions?.openai).toBeUndefined()
  1792. })
  1793. test("azure cognitive services remaps providerID to 'azure' key", () => {
  1794. const model = createModel("azure-cognitive-services", "@ai-sdk/azure")
  1795. const msgs = [
  1796. {
  1797. role: "user",
  1798. content: [
  1799. {
  1800. type: "text",
  1801. text: "Hello",
  1802. providerOptions: {
  1803. "azure-cognitive-services": { part: true },
  1804. },
  1805. },
  1806. ],
  1807. providerOptions: {
  1808. "azure-cognitive-services": { someOption: "value" },
  1809. },
  1810. },
  1811. ] as any[]
  1812. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1813. const part = result[0].content[0] as any
  1814. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  1815. expect(result[0].providerOptions?.["azure-cognitive-services"]).toBeUndefined()
  1816. expect(part.providerOptions?.azure).toEqual({ part: true })
  1817. expect(part.providerOptions?.["azure-cognitive-services"]).toBeUndefined()
  1818. })
  1819. test("copilot remaps providerID to 'copilot' key", () => {
  1820. const model = createModel("github-copilot", "@ai-sdk/github-copilot")
  1821. const msgs = [
  1822. {
  1823. role: "user",
  1824. content: "Hello",
  1825. providerOptions: {
  1826. copilot: { someOption: "value" },
  1827. },
  1828. },
  1829. ] as any[]
  1830. const result = ProviderTransform.message(msgs, model, {})
  1831. expect(result[0].providerOptions?.copilot).toEqual({ someOption: "value" })
  1832. expect(result[0].providerOptions?.["github-copilot"]).toBeUndefined()
  1833. })
  1834. test("bedrock remaps providerID to 'bedrock' key", () => {
  1835. const model = createModel("my-bedrock", "@ai-sdk/amazon-bedrock")
  1836. const msgs = [
  1837. {
  1838. role: "user",
  1839. content: "Hello",
  1840. providerOptions: {
  1841. "my-bedrock": { someOption: "value" },
  1842. },
  1843. },
  1844. ] as any[]
  1845. const result = ProviderTransform.message(msgs, model, {})
  1846. expect(result[0].providerOptions?.bedrock).toEqual({ someOption: "value" })
  1847. expect(result[0].providerOptions?.["my-bedrock"]).toBeUndefined()
  1848. })
  1849. })
  1850. describe("ProviderTransform.message - claude w/bedrock custom inference profile", () => {
  1851. test("adds cachePoint", () => {
  1852. const model = {
  1853. id: "amazon-bedrock/custom-claude-sonnet-4.5",
  1854. providerID: "amazon-bedrock",
  1855. api: {
  1856. id: "arn:aws:bedrock:xxx:yyy:application-inference-profile/zzz",
  1857. url: "https://api.test.com",
  1858. npm: "@ai-sdk/amazon-bedrock",
  1859. },
  1860. name: "Custom inference profile",
  1861. capabilities: {},
  1862. options: {},
  1863. headers: {},
  1864. } as any
  1865. const msgs = [
  1866. {
  1867. role: "user",
  1868. content: "Hello",
  1869. },
  1870. ] as any[]
  1871. const result = ProviderTransform.message(msgs, model, {})
  1872. expect(result[0].providerOptions?.bedrock).toEqual(
  1873. expect.objectContaining({
  1874. cachePoint: {
  1875. type: "default",
  1876. },
  1877. }),
  1878. )
  1879. })
  1880. })
  1881. describe("ProviderTransform.message - bedrock caching with non-bedrock providerID", () => {
  1882. test("applies cache options at message level when npm package is amazon-bedrock", () => {
  1883. const model = {
  1884. id: "aws/us.anthropic.claude-opus-4-6-v1",
  1885. providerID: "aws",
  1886. api: {
  1887. id: "us.anthropic.claude-opus-4-6-v1",
  1888. url: "https://bedrock-runtime.us-east-1.amazonaws.com",
  1889. npm: "@ai-sdk/amazon-bedrock",
  1890. },
  1891. name: "Claude Opus 4.6",
  1892. capabilities: {},
  1893. options: {},
  1894. headers: {},
  1895. } as any
  1896. const msgs = [
  1897. {
  1898. role: "system",
  1899. content: "You are a helpful assistant",
  1900. },
  1901. {
  1902. role: "user",
  1903. content: [{ type: "text", text: "Hello" }],
  1904. },
  1905. ] as any[]
  1906. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1907. // Cache should be at the message level and not the content-part level
  1908. expect(result[0].providerOptions?.bedrock).toEqual({
  1909. cachePoint: { type: "default" },
  1910. })
  1911. expect(result[0].content).toBe("You are a helpful assistant")
  1912. })
  1913. })
  1914. describe("ProviderTransform.message - cache control on gateway", () => {
  1915. const createModel = (overrides: Partial<any> = {}) =>
  1916. ({
  1917. id: "anthropic/claude-sonnet-4",
  1918. providerID: "vercel",
  1919. api: {
  1920. id: "anthropic/claude-sonnet-4",
  1921. url: "https://ai-gateway.vercel.sh/v3/ai",
  1922. npm: "@ai-sdk/gateway",
  1923. },
  1924. name: "Claude Sonnet 4",
  1925. capabilities: {
  1926. temperature: true,
  1927. reasoning: true,
  1928. attachment: true,
  1929. toolcall: true,
  1930. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1931. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1932. interleaved: false,
  1933. },
  1934. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  1935. limit: { context: 200_000, output: 8192 },
  1936. status: "active",
  1937. options: {},
  1938. headers: {},
  1939. ...overrides,
  1940. }) as any
  1941. test("gateway does not set cache control for anthropic models", () => {
  1942. const model = createModel()
  1943. const msgs = [
  1944. {
  1945. role: "system",
  1946. content: "You are a helpful assistant",
  1947. },
  1948. {
  1949. role: "user",
  1950. content: "Hello",
  1951. },
  1952. ] as any[]
  1953. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1954. expect(result[0].content).toBe("You are a helpful assistant")
  1955. expect(result[0].providerOptions).toBeUndefined()
  1956. })
  1957. test("non-gateway anthropic keeps existing cache control behavior", () => {
  1958. const model = createModel({
  1959. providerID: "anthropic",
  1960. api: {
  1961. id: "claude-sonnet-4",
  1962. url: "https://api.anthropic.com",
  1963. npm: "@ai-sdk/anthropic",
  1964. },
  1965. })
  1966. const msgs = [
  1967. {
  1968. role: "system",
  1969. content: "You are a helpful assistant",
  1970. },
  1971. {
  1972. role: "user",
  1973. content: "Hello",
  1974. },
  1975. ] as any[]
  1976. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1977. expect(result[0].providerOptions).toEqual({
  1978. anthropic: {
  1979. cacheControl: {
  1980. type: "ephemeral",
  1981. },
  1982. },
  1983. openrouter: {
  1984. cacheControl: {
  1985. type: "ephemeral",
  1986. },
  1987. },
  1988. bedrock: {
  1989. cachePoint: {
  1990. type: "default",
  1991. },
  1992. },
  1993. openaiCompatible: {
  1994. cache_control: {
  1995. type: "ephemeral",
  1996. },
  1997. },
  1998. copilot: {
  1999. copilot_cache_control: {
  2000. type: "ephemeral",
  2001. },
  2002. },
  2003. alibaba: {
  2004. cacheControl: {
  2005. type: "ephemeral",
  2006. },
  2007. },
  2008. })
  2009. })
  2010. test("google-vertex-anthropic applies cache control", () => {
  2011. const model = createModel({
  2012. providerID: "google-vertex-anthropic",
  2013. api: {
  2014. id: "google-vertex-anthropic",
  2015. url: "https://us-central1-aiplatform.googleapis.com",
  2016. npm: "@ai-sdk/google-vertex/anthropic",
  2017. },
  2018. id: "claude-sonnet-4@20250514",
  2019. })
  2020. const msgs = [
  2021. {
  2022. role: "system",
  2023. content: "You are a helpful assistant",
  2024. },
  2025. {
  2026. role: "user",
  2027. content: "Hello",
  2028. },
  2029. ] as any[]
  2030. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2031. expect(result[0].providerOptions).toEqual({
  2032. anthropic: {
  2033. cacheControl: {
  2034. type: "ephemeral",
  2035. },
  2036. },
  2037. openrouter: {
  2038. cacheControl: {
  2039. type: "ephemeral",
  2040. },
  2041. },
  2042. bedrock: {
  2043. cachePoint: {
  2044. type: "default",
  2045. },
  2046. },
  2047. openaiCompatible: {
  2048. cache_control: {
  2049. type: "ephemeral",
  2050. },
  2051. },
  2052. copilot: {
  2053. copilot_cache_control: {
  2054. type: "ephemeral",
  2055. },
  2056. },
  2057. alibaba: {
  2058. cacheControl: {
  2059. type: "ephemeral",
  2060. },
  2061. },
  2062. })
  2063. })
  2064. })
  2065. describe("ProviderTransform.variants", () => {
  2066. const createMockModel = (overrides: Partial<any> = {}): any => ({
  2067. id: "test/test-model",
  2068. providerID: "test",
  2069. api: {
  2070. id: "test-model",
  2071. url: "https://api.test.com",
  2072. npm: "@ai-sdk/openai",
  2073. },
  2074. name: "Test Model",
  2075. capabilities: {
  2076. temperature: true,
  2077. reasoning: true,
  2078. attachment: true,
  2079. toolcall: true,
  2080. input: { text: true, audio: false, image: true, video: false, pdf: false },
  2081. output: { text: true, audio: false, image: false, video: false, pdf: false },
  2082. interleaved: false,
  2083. },
  2084. cost: {
  2085. input: 0.001,
  2086. output: 0.002,
  2087. cache: { read: 0.0001, write: 0.0002 },
  2088. },
  2089. limit: {
  2090. context: 200_000,
  2091. output: 64_000,
  2092. },
  2093. status: "active",
  2094. options: {},
  2095. headers: {},
  2096. release_date: "2024-01-01",
  2097. ...overrides,
  2098. })
  2099. test("returns empty object when model has no reasoning capabilities", () => {
  2100. const model = createMockModel({
  2101. capabilities: { reasoning: false },
  2102. })
  2103. const result = ProviderTransform.variants(model)
  2104. expect(result).toEqual({})
  2105. })
  2106. test("deepseek returns empty object", () => {
  2107. const model = createMockModel({
  2108. id: "deepseek/deepseek-chat",
  2109. providerID: "deepseek",
  2110. api: {
  2111. id: "deepseek-chat",
  2112. url: "https://api.deepseek.com",
  2113. npm: "@ai-sdk/openai-compatible",
  2114. },
  2115. })
  2116. const result = ProviderTransform.variants(model)
  2117. expect(result).toEqual({})
  2118. })
  2119. test("minimax returns empty object", () => {
  2120. const model = createMockModel({
  2121. id: "minimax/minimax-model",
  2122. providerID: "minimax",
  2123. api: {
  2124. id: "minimax-model",
  2125. url: "https://api.minimax.com",
  2126. npm: "@ai-sdk/openai-compatible",
  2127. },
  2128. })
  2129. const result = ProviderTransform.variants(model)
  2130. expect(result).toEqual({})
  2131. })
  2132. test("glm returns empty object", () => {
  2133. const model = createMockModel({
  2134. id: "glm/glm-4",
  2135. providerID: "glm",
  2136. api: {
  2137. id: "glm-4",
  2138. url: "https://api.glm.com",
  2139. npm: "@ai-sdk/openai-compatible",
  2140. },
  2141. })
  2142. const result = ProviderTransform.variants(model)
  2143. expect(result).toEqual({})
  2144. })
  2145. test("mistral models with reasoning support return variants", () => {
  2146. const model = createMockModel({
  2147. id: "mistral/mistral-small-latest",
  2148. providerID: "mistral",
  2149. api: {
  2150. id: "mistral-small-latest",
  2151. url: "https://api.mistral.com",
  2152. npm: "@ai-sdk/mistral",
  2153. },
  2154. capabilities: { reasoning: true },
  2155. })
  2156. const result = ProviderTransform.variants(model)
  2157. expect(result).toEqual({
  2158. high: { reasoningEffort: "high" },
  2159. })
  2160. })
  2161. test("mistral-medium-3.5 with reasoning returns variants", () => {
  2162. const model = createMockModel({
  2163. id: "mistral/mistral-medium-3.5",
  2164. providerID: "mistral",
  2165. api: {
  2166. id: "mistral-medium-3.5",
  2167. url: "https://api.mistral.com",
  2168. npm: "@ai-sdk/mistral",
  2169. },
  2170. capabilities: { reasoning: true },
  2171. })
  2172. const result = ProviderTransform.variants(model)
  2173. expect(result).toEqual({
  2174. high: { reasoningEffort: "high" },
  2175. })
  2176. })
  2177. test("mistral without reasoning returns empty object", () => {
  2178. const model = createMockModel({
  2179. id: "mistral/mistral-large",
  2180. providerID: "mistral",
  2181. api: {
  2182. id: "mistral-large-latest",
  2183. url: "https://api.mistral.com",
  2184. npm: "@ai-sdk/mistral",
  2185. },
  2186. capabilities: { reasoning: false },
  2187. })
  2188. const result = ProviderTransform.variants(model)
  2189. expect(result).toEqual({})
  2190. })
  2191. test("mistral large with reasoning returns empty object (only small supports reasoning)", () => {
  2192. const model = createMockModel({
  2193. id: "mistral/mistral-large",
  2194. providerID: "mistral",
  2195. api: {
  2196. id: "mistral-large-latest",
  2197. url: "https://api.mistral.com",
  2198. npm: "@ai-sdk/mistral",
  2199. },
  2200. capabilities: { reasoning: true },
  2201. })
  2202. const result = ProviderTransform.variants(model)
  2203. expect(result).toEqual({})
  2204. })
  2205. describe("@openrouter/ai-sdk-provider", () => {
  2206. test("returns empty object for non-qualifying models", () => {
  2207. const model = createMockModel({
  2208. id: "openrouter/test-model",
  2209. providerID: "openrouter",
  2210. api: {
  2211. id: "test-model",
  2212. url: "https://openrouter.ai",
  2213. npm: "@openrouter/ai-sdk-provider",
  2214. },
  2215. })
  2216. const result = ProviderTransform.variants(model)
  2217. expect(result).toEqual({})
  2218. })
  2219. test("gpt models return OPENAI_EFFORTS with reasoning", () => {
  2220. const model = createMockModel({
  2221. id: "openrouter/gpt-4",
  2222. providerID: "openrouter",
  2223. api: {
  2224. id: "gpt-4",
  2225. url: "https://openrouter.ai",
  2226. npm: "@openrouter/ai-sdk-provider",
  2227. },
  2228. })
  2229. const result = ProviderTransform.variants(model)
  2230. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2231. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  2232. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  2233. })
  2234. for (const testCase of [
  2235. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2236. { id: "openai/gpt-5-pro", efforts: ["high"] },
  2237. { id: "openai/gpt-5.5-pro", efforts: ["medium", "high", "xhigh"] },
  2238. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  2239. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2240. { id: "openai/gpt-5.3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2241. { id: "openai/gpt-5-chat-latest", efforts: [] },
  2242. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  2243. ]) {
  2244. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  2245. const result = ProviderTransform.variants(
  2246. createMockModel({
  2247. id: testCase.id,
  2248. providerID: "openrouter",
  2249. api: {
  2250. id: testCase.id,
  2251. url: "https://openrouter.ai",
  2252. npm: "@openrouter/ai-sdk-provider",
  2253. },
  2254. }),
  2255. )
  2256. expect(Object.keys(result)).toEqual(testCase.efforts)
  2257. })
  2258. }
  2259. test("gemini-3 returns OPENAI_EFFORTS with reasoning", () => {
  2260. const model = createMockModel({
  2261. id: "openrouter/gemini-3-5-pro",
  2262. providerID: "openrouter",
  2263. api: {
  2264. id: "gemini-3-5-pro",
  2265. url: "https://openrouter.ai",
  2266. npm: "@openrouter/ai-sdk-provider",
  2267. },
  2268. })
  2269. const result = ProviderTransform.variants(model)
  2270. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2271. })
  2272. test("grok-4 returns empty object", () => {
  2273. const model = createMockModel({
  2274. id: "openrouter/grok-4",
  2275. providerID: "openrouter",
  2276. api: {
  2277. id: "grok-4",
  2278. url: "https://openrouter.ai",
  2279. npm: "@openrouter/ai-sdk-provider",
  2280. },
  2281. })
  2282. const result = ProviderTransform.variants(model)
  2283. expect(result).toEqual({})
  2284. })
  2285. test("grok-3-mini returns low and high with reasoning", () => {
  2286. const model = createMockModel({
  2287. id: "openrouter/grok-3-mini",
  2288. providerID: "openrouter",
  2289. api: {
  2290. id: "grok-3-mini",
  2291. url: "https://openrouter.ai",
  2292. npm: "@openrouter/ai-sdk-provider",
  2293. },
  2294. })
  2295. const result = ProviderTransform.variants(model)
  2296. expect(Object.keys(result)).toEqual(["low", "high"])
  2297. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  2298. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  2299. })
  2300. })
  2301. describe("@ai-sdk/gateway", () => {
  2302. test("anthropic sonnet 4.6 models return adaptive thinking options", () => {
  2303. const model = createMockModel({
  2304. id: "anthropic/claude-sonnet-4-6",
  2305. providerID: "gateway",
  2306. api: {
  2307. id: "anthropic/claude-sonnet-4-6",
  2308. url: "https://gateway.ai",
  2309. npm: "@ai-sdk/gateway",
  2310. },
  2311. })
  2312. const result = ProviderTransform.variants(model)
  2313. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2314. expect(result.medium).toEqual({
  2315. thinking: {
  2316. type: "adaptive",
  2317. },
  2318. effort: "medium",
  2319. })
  2320. })
  2321. test("anthropic sonnet 4.6 dot-format models return adaptive thinking options", () => {
  2322. const model = createMockModel({
  2323. id: "anthropic/claude-sonnet-4-6",
  2324. providerID: "gateway",
  2325. api: {
  2326. id: "anthropic/claude-sonnet-4.6",
  2327. url: "https://gateway.ai",
  2328. npm: "@ai-sdk/gateway",
  2329. },
  2330. })
  2331. const result = ProviderTransform.variants(model)
  2332. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2333. expect(result.medium).toEqual({
  2334. thinking: {
  2335. type: "adaptive",
  2336. },
  2337. effort: "medium",
  2338. })
  2339. })
  2340. test("anthropic opus 4.6 dot-format models return adaptive thinking options", () => {
  2341. const model = createMockModel({
  2342. id: "anthropic/claude-opus-4-6",
  2343. providerID: "gateway",
  2344. api: {
  2345. id: "anthropic/claude-opus-4.6",
  2346. url: "https://gateway.ai",
  2347. npm: "@ai-sdk/gateway",
  2348. },
  2349. })
  2350. const result = ProviderTransform.variants(model)
  2351. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2352. expect(result.high).toEqual({
  2353. thinking: {
  2354. type: "adaptive",
  2355. },
  2356. effort: "high",
  2357. })
  2358. })
  2359. test("anthropic opus 4.7 models return adaptive thinking options with xhigh", () => {
  2360. const model = createMockModel({
  2361. id: "anthropic/claude-opus-4-7",
  2362. providerID: "gateway",
  2363. api: {
  2364. id: "anthropic/claude-opus-4-7",
  2365. url: "https://gateway.ai",
  2366. npm: "@ai-sdk/gateway",
  2367. },
  2368. })
  2369. const result = ProviderTransform.variants(model)
  2370. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  2371. expect(result.xhigh).toEqual({
  2372. thinking: {
  2373. type: "adaptive",
  2374. display: "summarized",
  2375. },
  2376. effort: "xhigh",
  2377. })
  2378. expect(result.max).toEqual({
  2379. thinking: {
  2380. type: "adaptive",
  2381. display: "summarized",
  2382. },
  2383. effort: "max",
  2384. })
  2385. })
  2386. test("anthropic opus 4.7 dot-format models return adaptive thinking options with xhigh", () => {
  2387. const model = createMockModel({
  2388. id: "anthropic/claude-opus-4-7",
  2389. providerID: "gateway",
  2390. api: {
  2391. id: "anthropic/claude-opus-4.7",
  2392. url: "https://gateway.ai",
  2393. npm: "@ai-sdk/gateway",
  2394. },
  2395. })
  2396. const result = ProviderTransform.variants(model)
  2397. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  2398. })
  2399. test("anthropic opus 4.8 forces display summarized for adaptive reasoning", () => {
  2400. const model = createMockModel({
  2401. id: "anthropic/claude-opus-4-8",
  2402. providerID: "gateway",
  2403. api: {
  2404. id: "anthropic/claude-opus-4-8",
  2405. url: "https://gateway.ai",
  2406. npm: "@ai-sdk/gateway",
  2407. },
  2408. })
  2409. const result = ProviderTransform.variants(model)
  2410. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  2411. expect(result.high).toEqual({
  2412. thinking: {
  2413. type: "adaptive",
  2414. display: "summarized",
  2415. },
  2416. effort: "high",
  2417. })
  2418. })
  2419. test("anthropic opus 4.6 omits display so it keeps the summarized default", () => {
  2420. const model = createMockModel({
  2421. id: "anthropic/claude-opus-4-6",
  2422. providerID: "gateway",
  2423. api: {
  2424. id: "anthropic/claude-opus-4-6",
  2425. url: "https://gateway.ai",
  2426. npm: "@ai-sdk/gateway",
  2427. },
  2428. })
  2429. const result = ProviderTransform.variants(model)
  2430. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2431. expect(result.high).toEqual({
  2432. thinking: {
  2433. type: "adaptive",
  2434. },
  2435. effort: "high",
  2436. })
  2437. })
  2438. test("anthropic models return anthropic thinking options", () => {
  2439. const model = createMockModel({
  2440. id: "anthropic/claude-sonnet-4",
  2441. providerID: "gateway",
  2442. api: {
  2443. id: "anthropic/claude-sonnet-4",
  2444. url: "https://gateway.ai",
  2445. npm: "@ai-sdk/gateway",
  2446. },
  2447. })
  2448. const result = ProviderTransform.variants(model)
  2449. expect(Object.keys(result)).toEqual(["high", "max"])
  2450. expect(result.high).toEqual({
  2451. thinking: {
  2452. type: "enabled",
  2453. budgetTokens: 16000,
  2454. },
  2455. })
  2456. expect(result.max).toEqual({
  2457. thinking: {
  2458. type: "enabled",
  2459. budgetTokens: 31999,
  2460. },
  2461. })
  2462. })
  2463. test("returns OPENAI_EFFORTS with reasoningEffort", () => {
  2464. const model = createMockModel({
  2465. id: "gateway/gateway-model",
  2466. providerID: "gateway",
  2467. api: {
  2468. id: "gateway-model",
  2469. url: "https://gateway.ai",
  2470. npm: "@ai-sdk/gateway",
  2471. },
  2472. })
  2473. const result = ProviderTransform.variants(model)
  2474. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2475. expect(result.low).toEqual({ reasoningEffort: "low" })
  2476. expect(result.high).toEqual({ reasoningEffort: "high" })
  2477. })
  2478. for (const testCase of [
  2479. { id: "openai/gpt-5-5", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2480. { id: "openai/gpt-5-pro", efforts: ["high"] },
  2481. { id: "openai/gpt-5-5-pro", efforts: ["medium", "high", "xhigh"] },
  2482. { id: "openai/gpt-5-2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  2483. { id: "openai/gpt-5-3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2484. { id: "openai/gpt-5-3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2485. { id: "openai/gpt-5-chat-latest", efforts: [] },
  2486. { id: "openai/gpt-5-2-chat-latest", efforts: ["medium"] },
  2487. ]) {
  2488. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  2489. const result = ProviderTransform.variants(
  2490. createMockModel({
  2491. id: testCase.id,
  2492. providerID: "gateway",
  2493. api: {
  2494. id: testCase.id,
  2495. url: "https://gateway.ai",
  2496. npm: "@ai-sdk/gateway",
  2497. },
  2498. }),
  2499. )
  2500. expect(Object.keys(result)).toEqual(testCase.efforts)
  2501. })
  2502. }
  2503. })
  2504. describe("@ai-sdk/github-copilot", () => {
  2505. test("standard models return low, medium, high", () => {
  2506. const model = createMockModel({
  2507. id: "gpt-4.5",
  2508. providerID: "github-copilot",
  2509. api: {
  2510. id: "gpt-4.5",
  2511. url: "https://api.githubcopilot.com",
  2512. npm: "@ai-sdk/github-copilot",
  2513. },
  2514. })
  2515. const result = ProviderTransform.variants(model)
  2516. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2517. expect(result.low).toEqual({
  2518. reasoningEffort: "low",
  2519. reasoningSummary: "auto",
  2520. include: ["reasoning.encrypted_content"],
  2521. })
  2522. })
  2523. test("gpt-5.1-codex-max includes xhigh", () => {
  2524. const model = createMockModel({
  2525. id: "gpt-5.1-codex-max",
  2526. providerID: "github-copilot",
  2527. api: {
  2528. id: "gpt-5.1-codex-max",
  2529. url: "https://api.githubcopilot.com",
  2530. npm: "@ai-sdk/github-copilot",
  2531. },
  2532. })
  2533. const result = ProviderTransform.variants(model)
  2534. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2535. })
  2536. test("gpt-5.1-codex-mini does not include xhigh", () => {
  2537. const model = createMockModel({
  2538. id: "gpt-5.1-codex-mini",
  2539. providerID: "github-copilot",
  2540. api: {
  2541. id: "gpt-5.1-codex-mini",
  2542. url: "https://api.githubcopilot.com",
  2543. npm: "@ai-sdk/github-copilot",
  2544. },
  2545. })
  2546. const result = ProviderTransform.variants(model)
  2547. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2548. })
  2549. test("gpt-5.1-codex does not include xhigh", () => {
  2550. const model = createMockModel({
  2551. id: "gpt-5.1-codex",
  2552. providerID: "github-copilot",
  2553. api: {
  2554. id: "gpt-5.1-codex",
  2555. url: "https://api.githubcopilot.com",
  2556. npm: "@ai-sdk/github-copilot",
  2557. },
  2558. })
  2559. const result = ProviderTransform.variants(model)
  2560. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2561. })
  2562. test("gpt-5.2 includes xhigh", () => {
  2563. const model = createMockModel({
  2564. id: "gpt-5.2",
  2565. providerID: "github-copilot",
  2566. api: {
  2567. id: "gpt-5.2",
  2568. url: "https://api.githubcopilot.com",
  2569. npm: "@ai-sdk/github-copilot",
  2570. },
  2571. })
  2572. const result = ProviderTransform.variants(model)
  2573. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2574. expect(result.xhigh).toEqual({
  2575. reasoningEffort: "xhigh",
  2576. reasoningSummary: "auto",
  2577. include: ["reasoning.encrypted_content"],
  2578. })
  2579. })
  2580. test("gpt-5.2-codex includes xhigh", () => {
  2581. const model = createMockModel({
  2582. id: "gpt-5.2-codex",
  2583. providerID: "github-copilot",
  2584. api: {
  2585. id: "gpt-5.2-codex",
  2586. url: "https://api.githubcopilot.com",
  2587. npm: "@ai-sdk/github-copilot",
  2588. },
  2589. })
  2590. const result = ProviderTransform.variants(model)
  2591. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2592. })
  2593. test("gpt-5.3-codex includes xhigh", () => {
  2594. const model = createMockModel({
  2595. id: "gpt-5.3-codex",
  2596. providerID: "github-copilot",
  2597. api: {
  2598. id: "gpt-5.3-codex",
  2599. url: "https://api.githubcopilot.com",
  2600. npm: "@ai-sdk/github-copilot",
  2601. },
  2602. })
  2603. const result = ProviderTransform.variants(model)
  2604. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2605. })
  2606. test("gpt-5.4 includes xhigh", () => {
  2607. const model = createMockModel({
  2608. id: "gpt-5.4",
  2609. release_date: "2026-03-05",
  2610. providerID: "github-copilot",
  2611. api: {
  2612. id: "gpt-5.4",
  2613. url: "https://api.githubcopilot.com",
  2614. npm: "@ai-sdk/github-copilot",
  2615. },
  2616. })
  2617. const result = ProviderTransform.variants(model)
  2618. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2619. })
  2620. })
  2621. describe("@ai-sdk/cerebras", () => {
  2622. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2623. const model = createMockModel({
  2624. id: "cerebras/llama-4",
  2625. providerID: "cerebras",
  2626. api: {
  2627. id: "llama-4-sc",
  2628. url: "https://api.cerebras.ai",
  2629. npm: "@ai-sdk/cerebras",
  2630. },
  2631. })
  2632. const result = ProviderTransform.variants(model)
  2633. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2634. expect(result.low).toEqual({ reasoningEffort: "low" })
  2635. expect(result.high).toEqual({ reasoningEffort: "high" })
  2636. })
  2637. })
  2638. describe("@ai-sdk/togetherai", () => {
  2639. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2640. const model = createMockModel({
  2641. id: "togetherai/llama-4",
  2642. providerID: "togetherai",
  2643. api: {
  2644. id: "llama-4-sc",
  2645. url: "https://api.togetherai.com",
  2646. npm: "@ai-sdk/togetherai",
  2647. },
  2648. })
  2649. const result = ProviderTransform.variants(model)
  2650. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2651. expect(result.low).toEqual({ reasoningEffort: "low" })
  2652. expect(result.high).toEqual({ reasoningEffort: "high" })
  2653. })
  2654. })
  2655. describe("@ai-sdk/xai", () => {
  2656. test("grok-3 returns empty object", () => {
  2657. const model = createMockModel({
  2658. id: "xai/grok-3",
  2659. providerID: "xai",
  2660. api: {
  2661. id: "grok-3",
  2662. url: "https://api.x.ai",
  2663. npm: "@ai-sdk/xai",
  2664. },
  2665. })
  2666. const result = ProviderTransform.variants(model)
  2667. expect(result).toEqual({})
  2668. })
  2669. test("grok-3-mini returns low and high with reasoningEffort", () => {
  2670. const model = createMockModel({
  2671. id: "xai/grok-3-mini",
  2672. providerID: "xai",
  2673. api: {
  2674. id: "grok-3-mini",
  2675. url: "https://api.x.ai",
  2676. npm: "@ai-sdk/xai",
  2677. },
  2678. })
  2679. const result = ProviderTransform.variants(model)
  2680. expect(Object.keys(result)).toEqual(["low", "high"])
  2681. expect(result.low).toEqual({ reasoningEffort: "low" })
  2682. expect(result.high).toEqual({ reasoningEffort: "high" })
  2683. })
  2684. })
  2685. describe("@ai-sdk/deepinfra", () => {
  2686. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2687. const model = createMockModel({
  2688. id: "deepinfra/llama-4",
  2689. providerID: "deepinfra",
  2690. api: {
  2691. id: "llama-4-sc",
  2692. url: "https://api.deepinfra.com",
  2693. npm: "@ai-sdk/deepinfra",
  2694. },
  2695. })
  2696. const result = ProviderTransform.variants(model)
  2697. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2698. expect(result.low).toEqual({ reasoningEffort: "low" })
  2699. expect(result.high).toEqual({ reasoningEffort: "high" })
  2700. })
  2701. })
  2702. describe("@ai-sdk/openai-compatible", () => {
  2703. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2704. const model = createMockModel({
  2705. id: "custom-provider/custom-model",
  2706. providerID: "custom-provider",
  2707. api: {
  2708. id: "custom-model",
  2709. url: "https://api.custom.com",
  2710. npm: "@ai-sdk/openai-compatible",
  2711. },
  2712. })
  2713. const result = ProviderTransform.variants(model)
  2714. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2715. expect(result.low).toEqual({ reasoningEffort: "low" })
  2716. expect(result.high).toEqual({ reasoningEffort: "high" })
  2717. })
  2718. })
  2719. describe("@ai-sdk/azure", () => {
  2720. test("o1-mini returns empty object", () => {
  2721. const model = createMockModel({
  2722. id: "o1-mini",
  2723. providerID: "azure",
  2724. api: {
  2725. id: "o1-mini",
  2726. url: "https://azure.com",
  2727. npm: "@ai-sdk/azure",
  2728. },
  2729. })
  2730. const result = ProviderTransform.variants(model)
  2731. expect(result).toEqual({})
  2732. })
  2733. test("standard azure models return custom efforts with reasoningSummary", () => {
  2734. const model = createMockModel({
  2735. id: "o1",
  2736. providerID: "azure",
  2737. api: {
  2738. id: "o1",
  2739. url: "https://azure.com",
  2740. npm: "@ai-sdk/azure",
  2741. },
  2742. })
  2743. const result = ProviderTransform.variants(model)
  2744. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2745. expect(result.low).toEqual({
  2746. reasoningEffort: "low",
  2747. reasoningSummary: "auto",
  2748. include: ["reasoning.encrypted_content"],
  2749. })
  2750. })
  2751. test("gpt-5 adds minimal effort", () => {
  2752. const model = createMockModel({
  2753. id: "gpt-5",
  2754. providerID: "azure",
  2755. api: {
  2756. id: "gpt-5",
  2757. url: "https://azure.com",
  2758. npm: "@ai-sdk/azure",
  2759. },
  2760. })
  2761. const result = ProviderTransform.variants(model)
  2762. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  2763. })
  2764. for (const id of ["gpt-5-4", "gpt-5-5"]) {
  2765. test(`${id} does not add minimal effort`, () => {
  2766. const result = ProviderTransform.variants(
  2767. createMockModel({
  2768. id,
  2769. providerID: "azure",
  2770. api: {
  2771. id,
  2772. url: "https://azure.com",
  2773. npm: "@ai-sdk/azure",
  2774. },
  2775. }),
  2776. )
  2777. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2778. })
  2779. }
  2780. })
  2781. describe("@ai-sdk/openai", () => {
  2782. test("gpt-5-pro returns only high effort", () => {
  2783. const model = createMockModel({
  2784. id: "gpt-5-pro",
  2785. providerID: "openai",
  2786. api: {
  2787. id: "gpt-5-pro",
  2788. url: "https://api.openai.com",
  2789. npm: "@ai-sdk/openai",
  2790. },
  2791. })
  2792. const result = ProviderTransform.variants(model)
  2793. expect(Object.keys(result)).toEqual(["high"])
  2794. })
  2795. test("standard openai models return custom efforts with reasoningSummary", () => {
  2796. const model = createMockModel({
  2797. id: "gpt-5",
  2798. providerID: "openai",
  2799. api: {
  2800. id: "gpt-5",
  2801. url: "https://api.openai.com",
  2802. npm: "@ai-sdk/openai",
  2803. },
  2804. release_date: "2024-06-01",
  2805. })
  2806. const result = ProviderTransform.variants(model)
  2807. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  2808. expect(result.low).toEqual({
  2809. reasoningEffort: "low",
  2810. reasoningSummary: "auto",
  2811. include: ["reasoning.encrypted_content"],
  2812. })
  2813. })
  2814. test("models after 2025-11-13 include 'none' effort", () => {
  2815. const model = createMockModel({
  2816. id: "gpt-5-nano",
  2817. providerID: "openai",
  2818. api: {
  2819. id: "gpt-5-nano",
  2820. url: "https://api.openai.com",
  2821. npm: "@ai-sdk/openai",
  2822. },
  2823. release_date: "2025-11-14",
  2824. })
  2825. const result = ProviderTransform.variants(model)
  2826. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high"])
  2827. })
  2828. test("models after 2025-12-04 include 'xhigh' effort", () => {
  2829. const model = createMockModel({
  2830. id: "openai/gpt-5-reasoning",
  2831. providerID: "openai",
  2832. api: {
  2833. id: "gpt-5-reasoning",
  2834. url: "https://api.openai.com",
  2835. npm: "@ai-sdk/openai",
  2836. },
  2837. release_date: "2025-12-05",
  2838. })
  2839. const result = ProviderTransform.variants(model)
  2840. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2841. })
  2842. for (const testCase of [
  2843. { id: "o1", releaseDate: "2024-12-17", efforts: ["low", "medium", "high"] },
  2844. { id: "o1-pro", releaseDate: "2025-03-19", efforts: ["low", "medium", "high"] },
  2845. { id: "o3", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  2846. { id: "o3-mini", releaseDate: "2025-01-31", efforts: ["low", "medium", "high"] },
  2847. { id: "o3-pro", releaseDate: "2025-06-10", efforts: ["low", "medium", "high"] },
  2848. { id: "o4-mini", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  2849. { id: "o3-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  2850. { id: "o4-mini-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  2851. { id: "gpt-5.1", releaseDate: "2025-11-13", efforts: ["none", "low", "medium", "high"] },
  2852. { id: "gpt-5.4", releaseDate: "2026-03-05", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2853. {
  2854. id: "gpt-5.5",
  2855. modelID: "gpt-5-5",
  2856. releaseDate: "2026-04-23",
  2857. efforts: ["none", "low", "medium", "high", "xhigh"],
  2858. },
  2859. { id: "gpt-5.4-pro", releaseDate: "2026-03-05", efforts: ["medium", "high", "xhigh"] },
  2860. { id: "gpt-5.5-pro", releaseDate: "2026-04-23", efforts: ["medium", "high", "xhigh"] },
  2861. { id: "gpt-5-codex", releaseDate: "2025-09-23", efforts: ["low", "medium", "high"] },
  2862. { id: "gpt-5.1-codex", releaseDate: "2025-11-13", efforts: ["low", "medium", "high"] },
  2863. { id: "gpt-5.1-codex-max", releaseDate: "2025-11-13", efforts: ["low", "medium", "high", "xhigh"] },
  2864. { id: "gpt-5.2-codex", releaseDate: "2025-12-11", efforts: ["low", "medium", "high", "xhigh"] },
  2865. { id: "gpt-5.3-codex", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2866. { id: "gpt-5.3-codex-max", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2867. { id: "gpt-5-chat-latest", releaseDate: "2025-08-07", efforts: [] },
  2868. { id: "gpt-5.1-chat-latest", releaseDate: "2025-11-13", efforts: ["medium"] },
  2869. { id: "gpt-5.2-chat-latest", releaseDate: "2025-12-11", efforts: ["medium"] },
  2870. ]) {
  2871. test(`${testCase.id} returns supported reasoning efforts`, () => {
  2872. const result = ProviderTransform.variants(
  2873. createMockModel({
  2874. id: testCase.modelID ?? testCase.id,
  2875. providerID: "openai",
  2876. api: {
  2877. id: testCase.id,
  2878. url: "https://api.openai.com",
  2879. npm: "@ai-sdk/openai",
  2880. },
  2881. release_date: testCase.releaseDate,
  2882. }),
  2883. )
  2884. expect(Object.keys(result)).toEqual(testCase.efforts)
  2885. })
  2886. }
  2887. test("gpt-50 (lookalike) does not get gpt-5 family treatment", () => {
  2888. const model = createMockModel({
  2889. id: "gpt-50",
  2890. providerID: "openai",
  2891. api: {
  2892. id: "gpt-50",
  2893. url: "https://api.openai.com",
  2894. npm: "@ai-sdk/openai",
  2895. },
  2896. release_date: "2024-01-01",
  2897. })
  2898. const result = ProviderTransform.variants(model)
  2899. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2900. })
  2901. })
  2902. describe("@ai-sdk/anthropic", () => {
  2903. for (const testCase of [
  2904. {
  2905. name: "opus 4.5",
  2906. apiIds: ["claude-opus-4-5-20251101", "claude-opus-4.5-20251101"],
  2907. efforts: ["low", "medium", "high"],
  2908. expectedHigh: { effort: "high" },
  2909. },
  2910. {
  2911. name: "sonnet 4.6",
  2912. apiIds: ["claude-sonnet-4-6", "claude-sonnet-4.6"],
  2913. efforts: ["low", "medium", "high", "max"],
  2914. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  2915. },
  2916. {
  2917. name: "opus 4.6",
  2918. apiIds: ["claude-opus-4-6", "claude-opus-4.6"],
  2919. efforts: ["low", "medium", "high", "max"],
  2920. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  2921. },
  2922. {
  2923. name: "opus 4.7",
  2924. apiIds: ["claude-opus-4-7", "claude-opus-4.7"],
  2925. efforts: ["low", "medium", "high", "xhigh", "max"],
  2926. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  2927. },
  2928. {
  2929. name: "opus 4.8",
  2930. apiIds: ["claude-opus-4-8", "claude-opus-4.8"],
  2931. efforts: ["low", "medium", "high", "xhigh", "max"],
  2932. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  2933. },
  2934. ]) {
  2935. for (const apiId of testCase.apiIds) {
  2936. test(`${testCase.name} ${apiId} returns supported reasoning efforts`, () => {
  2937. const result = ProviderTransform.variants(
  2938. createMockModel({
  2939. id: `anthropic/${apiId}`,
  2940. providerID: "anthropic",
  2941. api: {
  2942. id: apiId,
  2943. url: "https://api.anthropic.com",
  2944. npm: "@ai-sdk/anthropic",
  2945. },
  2946. }),
  2947. )
  2948. expect(Object.keys(result)).toEqual(testCase.efforts)
  2949. expect(result.high).toEqual(testCase.expectedHigh)
  2950. })
  2951. }
  2952. }
  2953. test("github copilot opus 4.7 returns only medium reasoning effort", () => {
  2954. const model = createMockModel({
  2955. id: "claude-opus-4.7",
  2956. providerID: "github-copilot",
  2957. api: {
  2958. id: "claude-opus-4.7",
  2959. url: "https://api.githubcopilot.com/v1",
  2960. npm: "@ai-sdk/anthropic",
  2961. },
  2962. })
  2963. const result = ProviderTransform.variants(model)
  2964. expect(result).toEqual({
  2965. medium: {
  2966. thinking: {
  2967. type: "adaptive",
  2968. display: "summarized",
  2969. },
  2970. effort: "medium",
  2971. },
  2972. })
  2973. })
  2974. test("returns high and max with thinking config", () => {
  2975. const model = createMockModel({
  2976. id: "anthropic/claude-4",
  2977. providerID: "anthropic",
  2978. api: {
  2979. id: "claude-4",
  2980. url: "https://api.anthropic.com",
  2981. npm: "@ai-sdk/anthropic",
  2982. },
  2983. })
  2984. const result = ProviderTransform.variants(model)
  2985. expect(Object.keys(result)).toEqual(["high", "max"])
  2986. expect(result.high).toEqual({
  2987. thinking: {
  2988. type: "enabled",
  2989. budgetTokens: 16000,
  2990. },
  2991. })
  2992. expect(result.max).toEqual({
  2993. thinking: {
  2994. type: "enabled",
  2995. budgetTokens: 31999,
  2996. },
  2997. })
  2998. })
  2999. })
  3000. describe("@ai-sdk/google-vertex/anthropic", () => {
  3001. test("opus 4.8 uses adaptive reasoning for Vertex model IDs", () => {
  3002. const result = ProviderTransform.variants(
  3003. createMockModel({
  3004. id: "google-vertex-anthropic/claude-opus-4-8@default",
  3005. providerID: "google-vertex-anthropic",
  3006. api: {
  3007. id: "claude-opus-4-8@default",
  3008. url: "https://us-central1-aiplatform.googleapis.com",
  3009. npm: "@ai-sdk/google-vertex/anthropic",
  3010. },
  3011. }),
  3012. )
  3013. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3014. expect(result.high).toEqual({
  3015. thinking: {
  3016. type: "adaptive",
  3017. display: "summarized",
  3018. },
  3019. effort: "high",
  3020. })
  3021. })
  3022. })
  3023. describe("@ai-sdk/amazon-bedrock", () => {
  3024. test("anthropic sonnet 4.6 returns adaptive reasoning options", () => {
  3025. const model = createMockModel({
  3026. id: "bedrock/anthropic-claude-sonnet-4-6",
  3027. providerID: "bedrock",
  3028. api: {
  3029. id: "anthropic.claude-sonnet-4-6",
  3030. url: "https://bedrock.amazonaws.com",
  3031. npm: "@ai-sdk/amazon-bedrock",
  3032. },
  3033. })
  3034. const result = ProviderTransform.variants(model)
  3035. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3036. expect(result.max).toEqual({
  3037. reasoningConfig: {
  3038. type: "adaptive",
  3039. maxReasoningEffort: "max",
  3040. },
  3041. })
  3042. })
  3043. test("anthropic opus 4.7 returns adaptive reasoning options with xhigh", () => {
  3044. const model = createMockModel({
  3045. id: "bedrock/anthropic-claude-opus-4-7",
  3046. providerID: "bedrock",
  3047. api: {
  3048. id: "anthropic.claude-opus-4-7",
  3049. url: "https://bedrock.amazonaws.com",
  3050. npm: "@ai-sdk/amazon-bedrock",
  3051. },
  3052. })
  3053. const result = ProviderTransform.variants(model)
  3054. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3055. expect(result.xhigh).toEqual({
  3056. reasoningConfig: {
  3057. type: "adaptive",
  3058. maxReasoningEffort: "xhigh",
  3059. display: "summarized",
  3060. },
  3061. })
  3062. expect(result.max).toEqual({
  3063. reasoningConfig: {
  3064. type: "adaptive",
  3065. maxReasoningEffort: "max",
  3066. display: "summarized",
  3067. },
  3068. })
  3069. })
  3070. test("anthropic opus 4.8 returns adaptive reasoning options with xhigh", () => {
  3071. const result = ProviderTransform.variants(
  3072. createMockModel({
  3073. id: "bedrock/anthropic-claude-opus-4.8",
  3074. providerID: "bedrock",
  3075. api: {
  3076. id: "anthropic.claude-opus-4.8",
  3077. url: "https://bedrock.amazonaws.com",
  3078. npm: "@ai-sdk/amazon-bedrock",
  3079. },
  3080. }),
  3081. )
  3082. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3083. expect(result.high).toEqual({
  3084. reasoningConfig: {
  3085. type: "adaptive",
  3086. maxReasoningEffort: "high",
  3087. display: "summarized",
  3088. },
  3089. })
  3090. })
  3091. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningConfig", () => {
  3092. const model = createMockModel({
  3093. id: "bedrock/llama-4",
  3094. providerID: "bedrock",
  3095. api: {
  3096. id: "llama-4-sc",
  3097. url: "https://bedrock.amazonaws.com",
  3098. npm: "@ai-sdk/amazon-bedrock",
  3099. },
  3100. })
  3101. const result = ProviderTransform.variants(model)
  3102. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3103. expect(result.low).toEqual({
  3104. reasoningConfig: {
  3105. type: "enabled",
  3106. maxReasoningEffort: "low",
  3107. },
  3108. })
  3109. })
  3110. })
  3111. for (const provider of [
  3112. { name: "@ai-sdk/google", providerID: "google", url: "https://generativelanguage.googleapis.com" },
  3113. { name: "@ai-sdk/google-vertex", providerID: "google-vertex", url: "https://vertexai.googleapis.com" },
  3114. ]) {
  3115. describe(provider.name, () => {
  3116. for (const testCase of [
  3117. {
  3118. apiId: "gemini-2.5-pro",
  3119. efforts: ["high", "max"],
  3120. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  3121. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } },
  3122. },
  3123. {
  3124. apiId: "gemini-2.5-flash",
  3125. efforts: ["high", "max"],
  3126. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  3127. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
  3128. },
  3129. {
  3130. apiId: "gemini-3-pro-preview",
  3131. efforts: ["low", "medium", "high"],
  3132. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3133. },
  3134. {
  3135. apiId: "gemini-3.1-pro-preview",
  3136. efforts: ["low", "medium", "high"],
  3137. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3138. },
  3139. {
  3140. apiId: "gemini-3-flash-preview",
  3141. efforts: ["minimal", "low", "medium", "high"],
  3142. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3143. },
  3144. {
  3145. apiId: "gemini-3.1-flash-lite",
  3146. efforts: ["minimal", "low", "medium", "high"],
  3147. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3148. },
  3149. {
  3150. apiId: "gemini-3.1-flash-image-preview",
  3151. efforts: ["minimal", "high"],
  3152. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3153. },
  3154. {
  3155. apiId: "gemini-3-pro-image-preview",
  3156. efforts: ["high"],
  3157. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3158. },
  3159. ]) {
  3160. test(`${testCase.apiId} returns supported thinking controls`, () => {
  3161. const result = ProviderTransform.variants(
  3162. createMockModel({
  3163. id: `${provider.providerID}/${testCase.apiId}`,
  3164. providerID: provider.providerID,
  3165. api: {
  3166. id: testCase.apiId,
  3167. url: provider.url,
  3168. npm: provider.name,
  3169. },
  3170. }),
  3171. )
  3172. expect(Object.keys(result)).toEqual(testCase.efforts)
  3173. expect(result.high).toEqual(testCase.expectedHigh)
  3174. if (testCase.expectedMax) expect(result.max).toEqual(testCase.expectedMax)
  3175. })
  3176. }
  3177. })
  3178. }
  3179. describe("@ai-sdk/cohere", () => {
  3180. test("returns empty object", () => {
  3181. const model = createMockModel({
  3182. id: "cohere/command-r",
  3183. providerID: "cohere",
  3184. api: {
  3185. id: "command-r",
  3186. url: "https://api.cohere.com",
  3187. npm: "@ai-sdk/cohere",
  3188. },
  3189. })
  3190. const result = ProviderTransform.variants(model)
  3191. expect(result).toEqual({})
  3192. })
  3193. })
  3194. describe("@ai-sdk/groq", () => {
  3195. test("returns none and WIDELY_SUPPORTED_EFFORTS with thinkingLevel", () => {
  3196. const model = createMockModel({
  3197. id: "groq/llama-4",
  3198. providerID: "groq",
  3199. api: {
  3200. id: "llama-4-sc",
  3201. url: "https://api.groq.com",
  3202. npm: "@ai-sdk/groq",
  3203. },
  3204. })
  3205. const result = ProviderTransform.variants(model)
  3206. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high"])
  3207. expect(result.none).toEqual({
  3208. reasoningEffort: "none",
  3209. })
  3210. expect(result.low).toEqual({
  3211. reasoningEffort: "low",
  3212. })
  3213. })
  3214. })
  3215. describe("@ai-sdk/perplexity", () => {
  3216. test("returns empty object", () => {
  3217. const model = createMockModel({
  3218. id: "perplexity/sonar-plus",
  3219. providerID: "perplexity",
  3220. api: {
  3221. id: "sonar-plus",
  3222. url: "https://api.perplexity.ai",
  3223. npm: "@ai-sdk/perplexity",
  3224. },
  3225. })
  3226. const result = ProviderTransform.variants(model)
  3227. expect(result).toEqual({})
  3228. })
  3229. })
  3230. describe("@jerome-benoit/sap-ai-provider-v2", () => {
  3231. const sapModel = (apiId: string) =>
  3232. createMockModel({
  3233. id: `sap-ai-core/${apiId}`,
  3234. providerID: "sap-ai-core",
  3235. api: {
  3236. id: apiId,
  3237. url: "https://api.ai.sap",
  3238. npm: "@jerome-benoit/sap-ai-provider-v2",
  3239. },
  3240. })
  3241. for (const testCase of [
  3242. {
  3243. name: "sonnet 4.6",
  3244. apiIds: ["anthropic--claude-sonnet-4-6"],
  3245. efforts: ["low", "medium", "high", "max"],
  3246. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  3247. },
  3248. {
  3249. name: "opus 4.6",
  3250. apiIds: ["anthropic--claude-4.6-opus", "anthropic--claude-4-6-opus"],
  3251. efforts: ["low", "medium", "high", "max"],
  3252. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  3253. },
  3254. {
  3255. name: "opus 4.7",
  3256. apiIds: ["anthropic--claude-4.7-opus", "anthropic--claude-4-7-opus"],
  3257. efforts: ["low", "medium", "high", "xhigh", "max"],
  3258. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  3259. },
  3260. {
  3261. name: "opus 4.8",
  3262. apiIds: ["anthropic--claude-4.8-opus", "anthropic--claude-4-8-opus"],
  3263. efforts: ["low", "medium", "high", "xhigh", "max"],
  3264. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  3265. },
  3266. ]) {
  3267. for (const apiId of testCase.apiIds) {
  3268. test(`${testCase.name} ${apiId} returns adaptive thinking variants`, () => {
  3269. const result = ProviderTransform.variants(sapModel(apiId))
  3270. expect(Object.keys(result)).toEqual(testCase.efforts)
  3271. expect(result.high).toEqual(testCase.expectedHigh)
  3272. if (testCase.efforts.includes("xhigh")) {
  3273. expect(result.xhigh).toEqual({ ...testCase.expectedHigh, effort: "xhigh" })
  3274. }
  3275. })
  3276. }
  3277. }
  3278. test("anthropic sonnet 4 returns budget-tokens variants", () => {
  3279. const result = ProviderTransform.variants(sapModel("anthropic--claude-sonnet-4"))
  3280. expect(Object.keys(result)).toEqual(["high", "max"])
  3281. expect(result.high).toEqual({ thinking: { type: "enabled", budgetTokens: 16000 } })
  3282. expect(result.max).toEqual({ thinking: { type: "enabled", budgetTokens: 31999 } })
  3283. })
  3284. test("gemini 2.5 returns thinkingConfig variants", () => {
  3285. const result = ProviderTransform.variants(sapModel("gcp--gemini-2.5-pro"))
  3286. expect(Object.keys(result)).toEqual(["high", "max"])
  3287. expect(result.high).toEqual({ thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } })
  3288. expect(result.max).toEqual({ thinkingConfig: { includeThoughts: true, thinkingBudget: 24576 } })
  3289. })
  3290. for (const apiId of ["azure-openai--gpt-4o", "azure-openai--o3-mini"]) {
  3291. test(`${apiId} returns reasoningEffort variants`, () => {
  3292. const result = ProviderTransform.variants(sapModel(apiId))
  3293. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3294. expect(result.low).toEqual({ reasoningEffort: "low" })
  3295. expect(result.high).toEqual({ reasoningEffort: "high" })
  3296. })
  3297. }
  3298. for (const apiId of ["perplexity--sonar-pro", "mistral--mistral-large"]) {
  3299. test(`${apiId} returns empty object`, () => {
  3300. expect(ProviderTransform.variants(sapModel(apiId))).toEqual({})
  3301. })
  3302. }
  3303. test("non-anthropic models with opus-like substrings do not get adaptive thinking", () => {
  3304. expect(ProviderTransform.variants(sapModel("aws--llama-opus-4.7-fake"))).toEqual({})
  3305. })
  3306. })
  3307. describe("ai-gateway-provider (cloudflare-ai-gateway)", () => {
  3308. const cfModel = (apiId: string, releaseDate = "2024-01-01") =>
  3309. createMockModel({
  3310. id: `cloudflare-ai-gateway/${apiId}`,
  3311. providerID: "cloudflare-ai-gateway",
  3312. api: {
  3313. id: apiId,
  3314. url: "https://gateway.ai.cloudflare.com/v1/compat",
  3315. npm: "ai-gateway-provider",
  3316. },
  3317. release_date: releaseDate,
  3318. })
  3319. for (const testCase of [
  3320. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3321. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  3322. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3323. { id: "openai/gpt-5-pro", efforts: ["high"] },
  3324. { id: "openai/gpt-5.2-pro", efforts: ["medium", "high", "xhigh"] },
  3325. { id: "openai/gpt-5-chat-latest", efforts: [] },
  3326. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  3327. ]) {
  3328. test(`${testCase.id} returns supported reasoning efforts`, () => {
  3329. const result = ProviderTransform.variants(cfModel(testCase.id, "2026-03-05"))
  3330. expect(Object.keys(result)).toEqual(testCase.efforts)
  3331. })
  3332. }
  3333. test("openai gpt-4o (no reasoning) returns empty", () => {
  3334. const model = cfModel("openai/gpt-4o")
  3335. model.capabilities.reasoning = false
  3336. const result = ProviderTransform.variants(model)
  3337. expect(result).toEqual({})
  3338. })
  3339. test("non-openai upstream falls back to widely-supported OAI efforts", () => {
  3340. const result = ProviderTransform.variants(cfModel("anthropic/claude-sonnet-4-6"))
  3341. expect(result).toEqual({
  3342. low: { reasoningEffort: "low" },
  3343. medium: { reasoningEffort: "medium" },
  3344. high: { reasoningEffort: "high" },
  3345. })
  3346. })
  3347. })
  3348. })
  3349. describe("ProviderTransform.smallOptions - gpt-5 chat/search", () => {
  3350. const createModel = (apiId: string) => {
  3351. const model = {
  3352. id: `openai/${apiId}`,
  3353. providerID: "openai",
  3354. api: {
  3355. id: apiId,
  3356. url: "https://api.openai.com",
  3357. npm: "@ai-sdk/openai",
  3358. },
  3359. capabilities: { reasoning: true },
  3360. limit: { output: 64_000 },
  3361. release_date: "2026-01-01",
  3362. } as any
  3363. model.variants = ProviderTransform.variants(model)
  3364. return model
  3365. }
  3366. for (const testCase of [
  3367. { id: "gpt-5-chat-latest", options: { store: false } },
  3368. {
  3369. id: "gpt-5.1-chat-latest",
  3370. options: {
  3371. store: false,
  3372. reasoningEffort: "medium",
  3373. reasoningSummary: "auto",
  3374. include: ["reasoning.encrypted_content"],
  3375. },
  3376. },
  3377. {
  3378. id: "gpt-5.2-chat-latest",
  3379. options: {
  3380. store: false,
  3381. reasoningEffort: "medium",
  3382. reasoningSummary: "auto",
  3383. include: ["reasoning.encrypted_content"],
  3384. },
  3385. },
  3386. {
  3387. id: "gpt-5-search-api",
  3388. options: {
  3389. store: false,
  3390. reasoningEffort: "none",
  3391. reasoningSummary: "auto",
  3392. include: ["reasoning.encrypted_content"],
  3393. },
  3394. },
  3395. ]) {
  3396. test(`${testCase.id} returns only supported small options`, () => {
  3397. expect(ProviderTransform.smallOptions(createModel(testCase.id))).toEqual(testCase.options)
  3398. })
  3399. }
  3400. })
  3401. describe("ProviderTransform.smallOptions - google thinking controls", () => {
  3402. const createGoogleModel = (apiId: string) => {
  3403. const model = {
  3404. id: `google/${apiId}`,
  3405. providerID: "google",
  3406. api: {
  3407. id: apiId,
  3408. url: "https://generativelanguage.googleapis.com",
  3409. npm: "@ai-sdk/google",
  3410. },
  3411. capabilities: { reasoning: true },
  3412. limit: { output: 64_000 },
  3413. } as any
  3414. model.variants = ProviderTransform.variants(model)
  3415. return model
  3416. }
  3417. for (const testCase of [
  3418. { id: "gemini-3-pro-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "low" } } },
  3419. { id: "gemini-3-flash-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } } },
  3420. {
  3421. id: "gemini-3.1-flash-image-preview",
  3422. options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } },
  3423. },
  3424. { id: "gemini-3-pro-image-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } } },
  3425. { id: "gemini-2.5-pro", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  3426. { id: "gemini-2.5-flash", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  3427. ]) {
  3428. test(`${testCase.id} returns supported small thinking options`, () => {
  3429. expect(ProviderTransform.smallOptions(createGoogleModel(testCase.id))).toEqual(testCase.options)
  3430. })
  3431. }
  3432. test("uses the first configured variant when available", () => {
  3433. expect(
  3434. ProviderTransform.smallOptions({
  3435. ...createGoogleModel("gemini-2.5-pro"),
  3436. variants: {
  3437. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } },
  3438. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32768 } },
  3439. },
  3440. }),
  3441. ).toEqual({ thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } })
  3442. })
  3443. test("does not synthesize thinking options when variants are empty", () => {
  3444. expect(ProviderTransform.smallOptions({ ...createGoogleModel("gemini-2.5-pro"), variants: {} })).toEqual({})
  3445. })
  3446. })
  3447. describe("ProviderTransform.providerOptions - ai-gateway-provider", () => {
  3448. const createModel = (overrides: Partial<any> = {}) =>
  3449. ({
  3450. id: "cloudflare-ai-gateway/openai/gpt-5.4",
  3451. providerID: "cloudflare-ai-gateway",
  3452. api: {
  3453. id: "openai/gpt-5.4",
  3454. url: "https://gateway.ai.cloudflare.com/v1/compat",
  3455. npm: "ai-gateway-provider",
  3456. },
  3457. capabilities: {
  3458. temperature: false,
  3459. reasoning: true,
  3460. attachment: true,
  3461. toolcall: true,
  3462. input: { text: true, audio: false, image: true, video: false, pdf: true },
  3463. output: { text: true, audio: false, image: false, video: false, pdf: false },
  3464. interleaved: false,
  3465. },
  3466. cost: { input: 1, output: 1, cache: { read: 0, write: 0 } },
  3467. limit: { context: 1_000_000, output: 128_000 },
  3468. status: "active",
  3469. options: {},
  3470. headers: {},
  3471. release_date: "2026-03-05",
  3472. ...overrides,
  3473. }) as any
  3474. test("routes options under openaiCompatible (the key @ai-sdk/openai-compatible reads)", () => {
  3475. // Regression: previously fell back to providerID="cloudflare-ai-gateway",
  3476. // which @ai-sdk/openai-compatible never reads, silently dropping reasoningEffort.
  3477. const result = ProviderTransform.providerOptions(createModel(), { reasoningEffort: "high" })
  3478. expect(result).toEqual({ openaiCompatible: { reasoningEffort: "high" } })
  3479. })
  3480. })