transform.test.ts 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812
  1. import { describe, expect, test } from "bun:test"
  2. import { ProviderTransform } from "@/provider/transform"
  3. import { ModelID, ProviderID } from "../../src/provider/schema"
  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: ModelID.make("deepseek/deepseek-chat"),
  988. providerID: ProviderID.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: ModelID.make("openai/gpt-4"),
  1047. providerID: ProviderID.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("splits anthropic assistant messages when text trails tool calls", () => {
  1467. const msgs = [
  1468. {
  1469. role: "user",
  1470. content: [{ type: "text", text: "Check my home directory for PDFs" }],
  1471. },
  1472. {
  1473. role: "assistant",
  1474. content: [
  1475. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1476. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1477. { type: "text", text: "I checked your home directory and looked for PDF files." },
  1478. ],
  1479. },
  1480. {
  1481. role: "tool",
  1482. content: [
  1483. { type: "tool-result", toolCallId: "toolu_1", toolName: "read", output: { type: "text", value: "ok" } },
  1484. {
  1485. type: "tool-result",
  1486. toolCallId: "toolu_2",
  1487. toolName: "glob",
  1488. output: { type: "text", value: "No files found" },
  1489. },
  1490. ],
  1491. },
  1492. ] as any[]
  1493. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  1494. expect(result).toHaveLength(4)
  1495. expect(result[1]).toMatchObject({
  1496. role: "assistant",
  1497. content: [{ type: "text", text: "I checked your home directory and looked for PDF files." }],
  1498. })
  1499. expect(result[2]).toMatchObject({
  1500. role: "assistant",
  1501. content: [
  1502. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1503. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1504. ],
  1505. })
  1506. })
  1507. test("leaves valid anthropic assistant tool ordering unchanged", () => {
  1508. const msgs = [
  1509. {
  1510. role: "assistant",
  1511. content: [
  1512. { type: "text", text: "I checked your home directory and looked for PDF files." },
  1513. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1514. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1515. ],
  1516. },
  1517. ] as any[]
  1518. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  1519. expect(result).toHaveLength(1)
  1520. expect(result[0].content).toMatchObject([
  1521. { type: "text", text: "I checked your home directory and looked for PDF files." },
  1522. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1523. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1524. ])
  1525. })
  1526. test("splits vertex anthropic assistant messages when text trails tool calls", () => {
  1527. const model = {
  1528. ...anthropicModel,
  1529. providerID: "google-vertex-anthropic",
  1530. api: {
  1531. id: "claude-sonnet-4@20250514",
  1532. url: "https://us-central1-aiplatform.googleapis.com",
  1533. npm: "@ai-sdk/google-vertex/anthropic",
  1534. },
  1535. }
  1536. const msgs = [
  1537. {
  1538. role: "assistant",
  1539. content: [
  1540. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1541. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1542. { type: "text", text: "I checked your home directory and looked for PDF files." },
  1543. ],
  1544. },
  1545. ] as any[]
  1546. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1547. expect(result).toHaveLength(2)
  1548. expect(result[0]).toMatchObject({
  1549. role: "assistant",
  1550. content: [{ type: "text", text: "I checked your home directory and looked for PDF files." }],
  1551. })
  1552. expect(result[1]).toMatchObject({
  1553. role: "assistant",
  1554. content: [
  1555. { type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
  1556. { type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
  1557. ],
  1558. })
  1559. })
  1560. })
  1561. describe("ProviderTransform.message - strip openai metadata when store=false", () => {
  1562. const openaiModel = {
  1563. id: "openai/gpt-5",
  1564. providerID: "openai",
  1565. api: {
  1566. id: "gpt-5",
  1567. url: "https://api.openai.com",
  1568. npm: "@ai-sdk/openai",
  1569. },
  1570. name: "GPT-5",
  1571. capabilities: {
  1572. temperature: true,
  1573. reasoning: true,
  1574. attachment: true,
  1575. toolcall: true,
  1576. input: { text: true, audio: false, image: true, video: false, pdf: false },
  1577. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1578. interleaved: false,
  1579. },
  1580. cost: { input: 0.03, output: 0.06, cache: { read: 0.001, write: 0.002 } },
  1581. limit: { context: 128000, output: 4096 },
  1582. status: "active",
  1583. options: {},
  1584. headers: {},
  1585. } as any
  1586. test("preserves itemId and reasoningEncryptedContent when store=false", () => {
  1587. const msgs = [
  1588. {
  1589. role: "assistant",
  1590. content: [
  1591. {
  1592. type: "reasoning",
  1593. text: "thinking...",
  1594. providerOptions: {
  1595. openai: {
  1596. itemId: "rs_123",
  1597. reasoningEncryptedContent: "encrypted",
  1598. },
  1599. },
  1600. },
  1601. {
  1602. type: "text",
  1603. text: "Hello",
  1604. providerOptions: {
  1605. openai: {
  1606. itemId: "msg_456",
  1607. },
  1608. },
  1609. },
  1610. ],
  1611. },
  1612. ] as any[]
  1613. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  1614. expect(result).toHaveLength(1)
  1615. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("rs_123")
  1616. expect(result[0].content[1].providerOptions?.openai?.itemId).toBe("msg_456")
  1617. })
  1618. test("preserves itemId and reasoningEncryptedContent when store=false even when not openai", () => {
  1619. const zenModel = {
  1620. ...openaiModel,
  1621. providerID: "zen",
  1622. }
  1623. const msgs = [
  1624. {
  1625. role: "assistant",
  1626. content: [
  1627. {
  1628. type: "reasoning",
  1629. text: "thinking...",
  1630. providerOptions: {
  1631. openai: {
  1632. itemId: "rs_123",
  1633. reasoningEncryptedContent: "encrypted",
  1634. },
  1635. },
  1636. },
  1637. {
  1638. type: "text",
  1639. text: "Hello",
  1640. providerOptions: {
  1641. openai: {
  1642. itemId: "msg_456",
  1643. },
  1644. },
  1645. },
  1646. ],
  1647. },
  1648. ] as any[]
  1649. const result = ProviderTransform.message(msgs, zenModel, { store: false }) as any[]
  1650. expect(result).toHaveLength(1)
  1651. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("rs_123")
  1652. expect(result[0].content[1].providerOptions?.openai?.itemId).toBe("msg_456")
  1653. })
  1654. test("preserves other openai options including itemId", () => {
  1655. const msgs = [
  1656. {
  1657. role: "assistant",
  1658. content: [
  1659. {
  1660. type: "text",
  1661. text: "Hello",
  1662. providerOptions: {
  1663. openai: {
  1664. itemId: "msg_123",
  1665. otherOption: "value",
  1666. },
  1667. },
  1668. },
  1669. ],
  1670. },
  1671. ] as any[]
  1672. const result = ProviderTransform.message(msgs, openaiModel, { store: false }) as any[]
  1673. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1674. expect(result[0].content[0].providerOptions?.openai?.otherOption).toBe("value")
  1675. })
  1676. test("preserves metadata for openai package when store is true", () => {
  1677. const msgs = [
  1678. {
  1679. role: "assistant",
  1680. content: [
  1681. {
  1682. type: "text",
  1683. text: "Hello",
  1684. providerOptions: {
  1685. openai: {
  1686. itemId: "msg_123",
  1687. },
  1688. },
  1689. },
  1690. ],
  1691. },
  1692. ] as any[]
  1693. // openai package preserves itemId regardless of store value
  1694. const result = ProviderTransform.message(msgs, openaiModel, { store: true }) as any[]
  1695. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1696. })
  1697. test("preserves metadata for non-openai packages when store is false", () => {
  1698. const anthropicModel = {
  1699. ...openaiModel,
  1700. providerID: "anthropic",
  1701. api: {
  1702. id: "claude-3",
  1703. url: "https://api.anthropic.com",
  1704. npm: "@ai-sdk/anthropic",
  1705. },
  1706. }
  1707. const msgs = [
  1708. {
  1709. role: "assistant",
  1710. content: [
  1711. {
  1712. type: "text",
  1713. text: "Hello",
  1714. providerOptions: {
  1715. openai: {
  1716. itemId: "msg_123",
  1717. },
  1718. },
  1719. },
  1720. ],
  1721. },
  1722. ] as any[]
  1723. // store=false preserves metadata for non-openai packages
  1724. const result = ProviderTransform.message(msgs, anthropicModel, { store: false }) as any[]
  1725. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1726. })
  1727. test("preserves metadata using providerID key when store is false", () => {
  1728. const opencodeModel = {
  1729. ...openaiModel,
  1730. providerID: "opencode",
  1731. api: {
  1732. id: "opencode-test",
  1733. url: "https://api.opencode.ai",
  1734. npm: "@ai-sdk/openai-compatible",
  1735. },
  1736. }
  1737. const msgs = [
  1738. {
  1739. role: "assistant",
  1740. content: [
  1741. {
  1742. type: "text",
  1743. text: "Hello",
  1744. providerOptions: {
  1745. opencode: {
  1746. itemId: "msg_123",
  1747. otherOption: "value",
  1748. },
  1749. },
  1750. },
  1751. ],
  1752. },
  1753. ] as any[]
  1754. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  1755. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_123")
  1756. expect(result[0].content[0].providerOptions?.opencode?.otherOption).toBe("value")
  1757. })
  1758. test("preserves itemId across all providerOptions keys", () => {
  1759. const opencodeModel = {
  1760. ...openaiModel,
  1761. providerID: "opencode",
  1762. api: {
  1763. id: "opencode-test",
  1764. url: "https://api.opencode.ai",
  1765. npm: "@ai-sdk/openai-compatible",
  1766. },
  1767. }
  1768. const msgs = [
  1769. {
  1770. role: "assistant",
  1771. providerOptions: {
  1772. openai: { itemId: "msg_root" },
  1773. opencode: { itemId: "msg_opencode" },
  1774. extra: { itemId: "msg_extra" },
  1775. },
  1776. content: [
  1777. {
  1778. type: "text",
  1779. text: "Hello",
  1780. providerOptions: {
  1781. openai: { itemId: "msg_openai_part" },
  1782. opencode: { itemId: "msg_opencode_part" },
  1783. extra: { itemId: "msg_extra_part" },
  1784. },
  1785. },
  1786. ],
  1787. },
  1788. ] as any[]
  1789. const result = ProviderTransform.message(msgs, opencodeModel, { store: false }) as any[]
  1790. expect(result[0].providerOptions?.openai?.itemId).toBe("msg_root")
  1791. expect(result[0].providerOptions?.opencode?.itemId).toBe("msg_opencode")
  1792. expect(result[0].providerOptions?.extra?.itemId).toBe("msg_extra")
  1793. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_openai_part")
  1794. expect(result[0].content[0].providerOptions?.opencode?.itemId).toBe("msg_opencode_part")
  1795. expect(result[0].content[0].providerOptions?.extra?.itemId).toBe("msg_extra_part")
  1796. })
  1797. test("does not strip metadata for non-openai packages when store is not false", () => {
  1798. const anthropicModel = {
  1799. ...openaiModel,
  1800. providerID: "anthropic",
  1801. api: {
  1802. id: "claude-3",
  1803. url: "https://api.anthropic.com",
  1804. npm: "@ai-sdk/anthropic",
  1805. },
  1806. }
  1807. const msgs = [
  1808. {
  1809. role: "assistant",
  1810. content: [
  1811. {
  1812. type: "text",
  1813. text: "Hello",
  1814. providerOptions: {
  1815. openai: {
  1816. itemId: "msg_123",
  1817. },
  1818. },
  1819. },
  1820. ],
  1821. },
  1822. ] as any[]
  1823. const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
  1824. expect(result[0].content[0].providerOptions?.openai?.itemId).toBe("msg_123")
  1825. })
  1826. })
  1827. describe("ProviderTransform.message - providerOptions key remapping", () => {
  1828. const createModel = (providerID: string, npm: string) =>
  1829. ({
  1830. id: `${providerID}/test-model`,
  1831. providerID,
  1832. api: {
  1833. id: "test-model",
  1834. url: "https://api.test.com",
  1835. npm,
  1836. },
  1837. name: "Test Model",
  1838. capabilities: {
  1839. temperature: true,
  1840. reasoning: false,
  1841. attachment: true,
  1842. toolcall: true,
  1843. input: { text: true, audio: false, image: true, video: false, pdf: true },
  1844. output: { text: true, audio: false, image: false, video: false, pdf: false },
  1845. interleaved: false,
  1846. },
  1847. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  1848. limit: { context: 128000, output: 8192 },
  1849. status: "active",
  1850. options: {},
  1851. headers: {},
  1852. }) as any
  1853. test("azure keeps 'azure' key and does not remap to 'openai'", () => {
  1854. const model = createModel("azure", "@ai-sdk/azure")
  1855. const msgs = [
  1856. {
  1857. role: "user",
  1858. content: "Hello",
  1859. providerOptions: {
  1860. azure: { someOption: "value" },
  1861. },
  1862. },
  1863. ] as any[]
  1864. const result = ProviderTransform.message(msgs, model, {})
  1865. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  1866. expect(result[0].providerOptions?.openai).toBeUndefined()
  1867. })
  1868. test("azure cognitive services remaps providerID to 'azure' key", () => {
  1869. const model = createModel("azure-cognitive-services", "@ai-sdk/azure")
  1870. const msgs = [
  1871. {
  1872. role: "user",
  1873. content: [
  1874. {
  1875. type: "text",
  1876. text: "Hello",
  1877. providerOptions: {
  1878. "azure-cognitive-services": { part: true },
  1879. },
  1880. },
  1881. ],
  1882. providerOptions: {
  1883. "azure-cognitive-services": { someOption: "value" },
  1884. },
  1885. },
  1886. ] as any[]
  1887. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1888. const part = result[0].content[0] as any
  1889. expect(result[0].providerOptions?.azure).toEqual({ someOption: "value" })
  1890. expect(result[0].providerOptions?.["azure-cognitive-services"]).toBeUndefined()
  1891. expect(part.providerOptions?.azure).toEqual({ part: true })
  1892. expect(part.providerOptions?.["azure-cognitive-services"]).toBeUndefined()
  1893. })
  1894. test("copilot remaps providerID to 'copilot' key", () => {
  1895. const model = createModel("github-copilot", "@ai-sdk/github-copilot")
  1896. const msgs = [
  1897. {
  1898. role: "user",
  1899. content: "Hello",
  1900. providerOptions: {
  1901. copilot: { someOption: "value" },
  1902. },
  1903. },
  1904. ] as any[]
  1905. const result = ProviderTransform.message(msgs, model, {})
  1906. expect(result[0].providerOptions?.copilot).toEqual({ someOption: "value" })
  1907. expect(result[0].providerOptions?.["github-copilot"]).toBeUndefined()
  1908. })
  1909. test("bedrock remaps providerID to 'bedrock' key", () => {
  1910. const model = createModel("my-bedrock", "@ai-sdk/amazon-bedrock")
  1911. const msgs = [
  1912. {
  1913. role: "user",
  1914. content: "Hello",
  1915. providerOptions: {
  1916. "my-bedrock": { someOption: "value" },
  1917. },
  1918. },
  1919. ] as any[]
  1920. const result = ProviderTransform.message(msgs, model, {})
  1921. expect(result[0].providerOptions?.bedrock).toEqual({ someOption: "value" })
  1922. expect(result[0].providerOptions?.["my-bedrock"]).toBeUndefined()
  1923. })
  1924. })
  1925. describe("ProviderTransform.message - claude w/bedrock custom inference profile", () => {
  1926. test("adds cachePoint", () => {
  1927. const model = {
  1928. id: "amazon-bedrock/custom-claude-sonnet-4.5",
  1929. providerID: "amazon-bedrock",
  1930. api: {
  1931. id: "arn:aws:bedrock:xxx:yyy:application-inference-profile/zzz",
  1932. url: "https://api.test.com",
  1933. npm: "@ai-sdk/amazon-bedrock",
  1934. },
  1935. name: "Custom inference profile",
  1936. capabilities: {},
  1937. options: {},
  1938. headers: {},
  1939. } as any
  1940. const msgs = [
  1941. {
  1942. role: "user",
  1943. content: "Hello",
  1944. },
  1945. ] as any[]
  1946. const result = ProviderTransform.message(msgs, model, {})
  1947. expect(result[0].providerOptions?.bedrock).toEqual(
  1948. expect.objectContaining({
  1949. cachePoint: {
  1950. type: "default",
  1951. },
  1952. }),
  1953. )
  1954. })
  1955. })
  1956. describe("ProviderTransform.message - bedrock caching with non-bedrock providerID", () => {
  1957. test("applies cache options at message level when npm package is amazon-bedrock", () => {
  1958. const model = {
  1959. id: "aws/us.anthropic.claude-opus-4-6-v1",
  1960. providerID: "aws",
  1961. api: {
  1962. id: "us.anthropic.claude-opus-4-6-v1",
  1963. url: "https://bedrock-runtime.us-east-1.amazonaws.com",
  1964. npm: "@ai-sdk/amazon-bedrock",
  1965. },
  1966. name: "Claude Opus 4.6",
  1967. capabilities: {},
  1968. options: {},
  1969. headers: {},
  1970. } as any
  1971. const msgs = [
  1972. {
  1973. role: "system",
  1974. content: "You are a helpful assistant",
  1975. },
  1976. {
  1977. role: "user",
  1978. content: [{ type: "text", text: "Hello" }],
  1979. },
  1980. ] as any[]
  1981. const result = ProviderTransform.message(msgs, model, {}) as any[]
  1982. // Cache should be at the message level and not the content-part level
  1983. expect(result[0].providerOptions?.bedrock).toEqual({
  1984. cachePoint: { type: "default" },
  1985. })
  1986. expect(result[0].content).toBe("You are a helpful assistant")
  1987. })
  1988. })
  1989. describe("ProviderTransform.message - cache control on gateway", () => {
  1990. const createModel = (overrides: Partial<any> = {}) =>
  1991. ({
  1992. id: "anthropic/claude-sonnet-4",
  1993. providerID: "vercel",
  1994. api: {
  1995. id: "anthropic/claude-sonnet-4",
  1996. url: "https://ai-gateway.vercel.sh/v3/ai",
  1997. npm: "@ai-sdk/gateway",
  1998. },
  1999. name: "Claude Sonnet 4",
  2000. capabilities: {
  2001. temperature: true,
  2002. reasoning: true,
  2003. attachment: true,
  2004. toolcall: true,
  2005. input: { text: true, audio: false, image: true, video: false, pdf: true },
  2006. output: { text: true, audio: false, image: false, video: false, pdf: false },
  2007. interleaved: false,
  2008. },
  2009. cost: { input: 0.001, output: 0.002, cache: { read: 0.0001, write: 0.0002 } },
  2010. limit: { context: 200_000, output: 8192 },
  2011. status: "active",
  2012. options: {},
  2013. headers: {},
  2014. ...overrides,
  2015. }) as any
  2016. test("gateway does not set cache control for anthropic models", () => {
  2017. const model = createModel()
  2018. const msgs = [
  2019. {
  2020. role: "system",
  2021. content: "You are a helpful assistant",
  2022. },
  2023. {
  2024. role: "user",
  2025. content: "Hello",
  2026. },
  2027. ] as any[]
  2028. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2029. expect(result[0].content).toBe("You are a helpful assistant")
  2030. expect(result[0].providerOptions).toBeUndefined()
  2031. })
  2032. test("non-gateway anthropic keeps existing cache control behavior", () => {
  2033. const model = createModel({
  2034. providerID: "anthropic",
  2035. api: {
  2036. id: "claude-sonnet-4",
  2037. url: "https://api.anthropic.com",
  2038. npm: "@ai-sdk/anthropic",
  2039. },
  2040. })
  2041. const msgs = [
  2042. {
  2043. role: "system",
  2044. content: "You are a helpful assistant",
  2045. },
  2046. {
  2047. role: "user",
  2048. content: "Hello",
  2049. },
  2050. ] as any[]
  2051. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2052. expect(result[0].providerOptions).toEqual({
  2053. anthropic: {
  2054. cacheControl: {
  2055. type: "ephemeral",
  2056. },
  2057. },
  2058. openrouter: {
  2059. cacheControl: {
  2060. type: "ephemeral",
  2061. },
  2062. },
  2063. bedrock: {
  2064. cachePoint: {
  2065. type: "default",
  2066. },
  2067. },
  2068. openaiCompatible: {
  2069. cache_control: {
  2070. type: "ephemeral",
  2071. },
  2072. },
  2073. copilot: {
  2074. copilot_cache_control: {
  2075. type: "ephemeral",
  2076. },
  2077. },
  2078. alibaba: {
  2079. cacheControl: {
  2080. type: "ephemeral",
  2081. },
  2082. },
  2083. })
  2084. })
  2085. test("google-vertex-anthropic applies cache control", () => {
  2086. const model = createModel({
  2087. providerID: "google-vertex-anthropic",
  2088. api: {
  2089. id: "google-vertex-anthropic",
  2090. url: "https://us-central1-aiplatform.googleapis.com",
  2091. npm: "@ai-sdk/google-vertex/anthropic",
  2092. },
  2093. id: "claude-sonnet-4@20250514",
  2094. })
  2095. const msgs = [
  2096. {
  2097. role: "system",
  2098. content: "You are a helpful assistant",
  2099. },
  2100. {
  2101. role: "user",
  2102. content: "Hello",
  2103. },
  2104. ] as any[]
  2105. const result = ProviderTransform.message(msgs, model, {}) as any[]
  2106. expect(result[0].providerOptions).toEqual({
  2107. anthropic: {
  2108. cacheControl: {
  2109. type: "ephemeral",
  2110. },
  2111. },
  2112. openrouter: {
  2113. cacheControl: {
  2114. type: "ephemeral",
  2115. },
  2116. },
  2117. bedrock: {
  2118. cachePoint: {
  2119. type: "default",
  2120. },
  2121. },
  2122. openaiCompatible: {
  2123. cache_control: {
  2124. type: "ephemeral",
  2125. },
  2126. },
  2127. copilot: {
  2128. copilot_cache_control: {
  2129. type: "ephemeral",
  2130. },
  2131. },
  2132. alibaba: {
  2133. cacheControl: {
  2134. type: "ephemeral",
  2135. },
  2136. },
  2137. })
  2138. })
  2139. })
  2140. describe("ProviderTransform.variants", () => {
  2141. const createMockModel = (overrides: Partial<any> = {}): any => ({
  2142. id: "test/test-model",
  2143. providerID: "test",
  2144. api: {
  2145. id: "test-model",
  2146. url: "https://api.test.com",
  2147. npm: "@ai-sdk/openai",
  2148. },
  2149. name: "Test Model",
  2150. capabilities: {
  2151. temperature: true,
  2152. reasoning: true,
  2153. attachment: true,
  2154. toolcall: true,
  2155. input: { text: true, audio: false, image: true, video: false, pdf: false },
  2156. output: { text: true, audio: false, image: false, video: false, pdf: false },
  2157. interleaved: false,
  2158. },
  2159. cost: {
  2160. input: 0.001,
  2161. output: 0.002,
  2162. cache: { read: 0.0001, write: 0.0002 },
  2163. },
  2164. limit: {
  2165. context: 200_000,
  2166. output: 64_000,
  2167. },
  2168. status: "active",
  2169. options: {},
  2170. headers: {},
  2171. release_date: "2024-01-01",
  2172. ...overrides,
  2173. })
  2174. test("returns empty object when model has no reasoning capabilities", () => {
  2175. const model = createMockModel({
  2176. capabilities: { reasoning: false },
  2177. })
  2178. const result = ProviderTransform.variants(model)
  2179. expect(result).toEqual({})
  2180. })
  2181. test("deepseek returns empty object", () => {
  2182. const model = createMockModel({
  2183. id: "deepseek/deepseek-chat",
  2184. providerID: "deepseek",
  2185. api: {
  2186. id: "deepseek-chat",
  2187. url: "https://api.deepseek.com",
  2188. npm: "@ai-sdk/openai-compatible",
  2189. },
  2190. })
  2191. const result = ProviderTransform.variants(model)
  2192. expect(result).toEqual({})
  2193. })
  2194. test("minimax returns empty object", () => {
  2195. const model = createMockModel({
  2196. id: "minimax/minimax-model",
  2197. providerID: "minimax",
  2198. api: {
  2199. id: "minimax-model",
  2200. url: "https://api.minimax.com",
  2201. npm: "@ai-sdk/openai-compatible",
  2202. },
  2203. })
  2204. const result = ProviderTransform.variants(model)
  2205. expect(result).toEqual({})
  2206. })
  2207. test("glm returns empty object", () => {
  2208. const model = createMockModel({
  2209. id: "glm/glm-4",
  2210. providerID: "glm",
  2211. api: {
  2212. id: "glm-4",
  2213. url: "https://api.glm.com",
  2214. npm: "@ai-sdk/openai-compatible",
  2215. },
  2216. })
  2217. const result = ProviderTransform.variants(model)
  2218. expect(result).toEqual({})
  2219. })
  2220. test("mistral models with reasoning support return variants", () => {
  2221. const model = createMockModel({
  2222. id: "mistral/mistral-small-latest",
  2223. providerID: "mistral",
  2224. api: {
  2225. id: "mistral-small-latest",
  2226. url: "https://api.mistral.com",
  2227. npm: "@ai-sdk/mistral",
  2228. },
  2229. capabilities: { reasoning: true },
  2230. })
  2231. const result = ProviderTransform.variants(model)
  2232. expect(result).toEqual({
  2233. high: { reasoningEffort: "high" },
  2234. })
  2235. })
  2236. test("mistral-medium-3.5 with reasoning returns variants", () => {
  2237. const model = createMockModel({
  2238. id: "mistral/mistral-medium-3.5",
  2239. providerID: "mistral",
  2240. api: {
  2241. id: "mistral-medium-3.5",
  2242. url: "https://api.mistral.com",
  2243. npm: "@ai-sdk/mistral",
  2244. },
  2245. capabilities: { reasoning: true },
  2246. })
  2247. const result = ProviderTransform.variants(model)
  2248. expect(result).toEqual({
  2249. high: { reasoningEffort: "high" },
  2250. })
  2251. })
  2252. test("mistral without reasoning returns empty object", () => {
  2253. const model = createMockModel({
  2254. id: "mistral/mistral-large",
  2255. providerID: "mistral",
  2256. api: {
  2257. id: "mistral-large-latest",
  2258. url: "https://api.mistral.com",
  2259. npm: "@ai-sdk/mistral",
  2260. },
  2261. capabilities: { reasoning: false },
  2262. })
  2263. const result = ProviderTransform.variants(model)
  2264. expect(result).toEqual({})
  2265. })
  2266. test("mistral large with reasoning returns empty object (only small supports reasoning)", () => {
  2267. const model = createMockModel({
  2268. id: "mistral/mistral-large",
  2269. providerID: "mistral",
  2270. api: {
  2271. id: "mistral-large-latest",
  2272. url: "https://api.mistral.com",
  2273. npm: "@ai-sdk/mistral",
  2274. },
  2275. capabilities: { reasoning: true },
  2276. })
  2277. const result = ProviderTransform.variants(model)
  2278. expect(result).toEqual({})
  2279. })
  2280. describe("@openrouter/ai-sdk-provider", () => {
  2281. test("returns empty object for non-qualifying models", () => {
  2282. const model = createMockModel({
  2283. id: "openrouter/test-model",
  2284. providerID: "openrouter",
  2285. api: {
  2286. id: "test-model",
  2287. url: "https://openrouter.ai",
  2288. npm: "@openrouter/ai-sdk-provider",
  2289. },
  2290. })
  2291. const result = ProviderTransform.variants(model)
  2292. expect(result).toEqual({})
  2293. })
  2294. test("gpt models return OPENAI_EFFORTS with reasoning", () => {
  2295. const model = createMockModel({
  2296. id: "openrouter/gpt-4",
  2297. providerID: "openrouter",
  2298. api: {
  2299. id: "gpt-4",
  2300. url: "https://openrouter.ai",
  2301. npm: "@openrouter/ai-sdk-provider",
  2302. },
  2303. })
  2304. const result = ProviderTransform.variants(model)
  2305. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2306. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  2307. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  2308. })
  2309. for (const testCase of [
  2310. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2311. { id: "openai/gpt-5-pro", efforts: ["high"] },
  2312. { id: "openai/gpt-5.5-pro", efforts: ["medium", "high", "xhigh"] },
  2313. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  2314. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2315. { id: "openai/gpt-5.3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2316. { id: "openai/gpt-5-chat-latest", efforts: [] },
  2317. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  2318. ]) {
  2319. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  2320. const result = ProviderTransform.variants(
  2321. createMockModel({
  2322. id: testCase.id,
  2323. providerID: "openrouter",
  2324. api: {
  2325. id: testCase.id,
  2326. url: "https://openrouter.ai",
  2327. npm: "@openrouter/ai-sdk-provider",
  2328. },
  2329. }),
  2330. )
  2331. expect(Object.keys(result)).toEqual(testCase.efforts)
  2332. })
  2333. }
  2334. test("gemini-3 returns OPENAI_EFFORTS with reasoning", () => {
  2335. const model = createMockModel({
  2336. id: "openrouter/gemini-3-5-pro",
  2337. providerID: "openrouter",
  2338. api: {
  2339. id: "gemini-3-5-pro",
  2340. url: "https://openrouter.ai",
  2341. npm: "@openrouter/ai-sdk-provider",
  2342. },
  2343. })
  2344. const result = ProviderTransform.variants(model)
  2345. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2346. })
  2347. test("grok-4 returns empty object", () => {
  2348. const model = createMockModel({
  2349. id: "openrouter/grok-4",
  2350. providerID: "openrouter",
  2351. api: {
  2352. id: "grok-4",
  2353. url: "https://openrouter.ai",
  2354. npm: "@openrouter/ai-sdk-provider",
  2355. },
  2356. })
  2357. const result = ProviderTransform.variants(model)
  2358. expect(result).toEqual({})
  2359. })
  2360. test("grok-3-mini returns low and high with reasoning", () => {
  2361. const model = createMockModel({
  2362. id: "openrouter/grok-3-mini",
  2363. providerID: "openrouter",
  2364. api: {
  2365. id: "grok-3-mini",
  2366. url: "https://openrouter.ai",
  2367. npm: "@openrouter/ai-sdk-provider",
  2368. },
  2369. })
  2370. const result = ProviderTransform.variants(model)
  2371. expect(Object.keys(result)).toEqual(["low", "high"])
  2372. expect(result.low).toEqual({ reasoning: { effort: "low" } })
  2373. expect(result.high).toEqual({ reasoning: { effort: "high" } })
  2374. })
  2375. })
  2376. describe("@ai-sdk/gateway", () => {
  2377. test("anthropic sonnet 4.6 models return adaptive thinking options", () => {
  2378. const model = createMockModel({
  2379. id: "anthropic/claude-sonnet-4-6",
  2380. providerID: "gateway",
  2381. api: {
  2382. id: "anthropic/claude-sonnet-4-6",
  2383. url: "https://gateway.ai",
  2384. npm: "@ai-sdk/gateway",
  2385. },
  2386. })
  2387. const result = ProviderTransform.variants(model)
  2388. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2389. expect(result.medium).toEqual({
  2390. thinking: {
  2391. type: "adaptive",
  2392. },
  2393. effort: "medium",
  2394. })
  2395. })
  2396. test("anthropic sonnet 4.6 dot-format models return adaptive thinking options", () => {
  2397. const model = createMockModel({
  2398. id: "anthropic/claude-sonnet-4-6",
  2399. providerID: "gateway",
  2400. api: {
  2401. id: "anthropic/claude-sonnet-4.6",
  2402. url: "https://gateway.ai",
  2403. npm: "@ai-sdk/gateway",
  2404. },
  2405. })
  2406. const result = ProviderTransform.variants(model)
  2407. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2408. expect(result.medium).toEqual({
  2409. thinking: {
  2410. type: "adaptive",
  2411. },
  2412. effort: "medium",
  2413. })
  2414. })
  2415. test("anthropic opus 4.6 dot-format models return adaptive thinking options", () => {
  2416. const model = createMockModel({
  2417. id: "anthropic/claude-opus-4-6",
  2418. providerID: "gateway",
  2419. api: {
  2420. id: "anthropic/claude-opus-4.6",
  2421. url: "https://gateway.ai",
  2422. npm: "@ai-sdk/gateway",
  2423. },
  2424. })
  2425. const result = ProviderTransform.variants(model)
  2426. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  2427. expect(result.high).toEqual({
  2428. thinking: {
  2429. type: "adaptive",
  2430. },
  2431. effort: "high",
  2432. })
  2433. })
  2434. test("anthropic opus 4.7 models return adaptive thinking options with xhigh", () => {
  2435. const model = createMockModel({
  2436. id: "anthropic/claude-opus-4-7",
  2437. providerID: "gateway",
  2438. api: {
  2439. id: "anthropic/claude-opus-4-7",
  2440. url: "https://gateway.ai",
  2441. npm: "@ai-sdk/gateway",
  2442. },
  2443. })
  2444. const result = ProviderTransform.variants(model)
  2445. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  2446. expect(result.xhigh).toEqual({
  2447. thinking: {
  2448. type: "adaptive",
  2449. },
  2450. effort: "xhigh",
  2451. })
  2452. expect(result.max).toEqual({
  2453. thinking: {
  2454. type: "adaptive",
  2455. },
  2456. effort: "max",
  2457. })
  2458. })
  2459. test("anthropic opus 4.7 dot-format models return adaptive thinking options with xhigh", () => {
  2460. const model = createMockModel({
  2461. id: "anthropic/claude-opus-4-7",
  2462. providerID: "gateway",
  2463. api: {
  2464. id: "anthropic/claude-opus-4.7",
  2465. url: "https://gateway.ai",
  2466. npm: "@ai-sdk/gateway",
  2467. },
  2468. })
  2469. const result = ProviderTransform.variants(model)
  2470. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  2471. })
  2472. test("anthropic models return anthropic thinking options", () => {
  2473. const model = createMockModel({
  2474. id: "anthropic/claude-sonnet-4",
  2475. providerID: "gateway",
  2476. api: {
  2477. id: "anthropic/claude-sonnet-4",
  2478. url: "https://gateway.ai",
  2479. npm: "@ai-sdk/gateway",
  2480. },
  2481. })
  2482. const result = ProviderTransform.variants(model)
  2483. expect(Object.keys(result)).toEqual(["high", "max"])
  2484. expect(result.high).toEqual({
  2485. thinking: {
  2486. type: "enabled",
  2487. budgetTokens: 16000,
  2488. },
  2489. })
  2490. expect(result.max).toEqual({
  2491. thinking: {
  2492. type: "enabled",
  2493. budgetTokens: 31999,
  2494. },
  2495. })
  2496. })
  2497. test("returns OPENAI_EFFORTS with reasoningEffort", () => {
  2498. const model = createMockModel({
  2499. id: "gateway/gateway-model",
  2500. providerID: "gateway",
  2501. api: {
  2502. id: "gateway-model",
  2503. url: "https://gateway.ai",
  2504. npm: "@ai-sdk/gateway",
  2505. },
  2506. })
  2507. const result = ProviderTransform.variants(model)
  2508. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2509. expect(result.low).toEqual({ reasoningEffort: "low" })
  2510. expect(result.high).toEqual({ reasoningEffort: "high" })
  2511. })
  2512. for (const testCase of [
  2513. { id: "openai/gpt-5-5", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2514. { id: "openai/gpt-5-pro", efforts: ["high"] },
  2515. { id: "openai/gpt-5-5-pro", efforts: ["medium", "high", "xhigh"] },
  2516. { id: "openai/gpt-5-2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  2517. { id: "openai/gpt-5-3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2518. { id: "openai/gpt-5-3-codex-max", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2519. { id: "openai/gpt-5-chat-latest", efforts: [] },
  2520. { id: "openai/gpt-5-2-chat-latest", efforts: ["medium"] },
  2521. ]) {
  2522. test(`${testCase.id} returns supported OpenAI reasoning efforts`, () => {
  2523. const result = ProviderTransform.variants(
  2524. createMockModel({
  2525. id: testCase.id,
  2526. providerID: "gateway",
  2527. api: {
  2528. id: testCase.id,
  2529. url: "https://gateway.ai",
  2530. npm: "@ai-sdk/gateway",
  2531. },
  2532. }),
  2533. )
  2534. expect(Object.keys(result)).toEqual(testCase.efforts)
  2535. })
  2536. }
  2537. })
  2538. describe("@ai-sdk/github-copilot", () => {
  2539. test("standard models return low, medium, high", () => {
  2540. const model = createMockModel({
  2541. id: "gpt-4.5",
  2542. providerID: "github-copilot",
  2543. api: {
  2544. id: "gpt-4.5",
  2545. url: "https://api.githubcopilot.com",
  2546. npm: "@ai-sdk/github-copilot",
  2547. },
  2548. })
  2549. const result = ProviderTransform.variants(model)
  2550. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2551. expect(result.low).toEqual({
  2552. reasoningEffort: "low",
  2553. reasoningSummary: "auto",
  2554. include: ["reasoning.encrypted_content"],
  2555. })
  2556. })
  2557. test("gpt-5.1-codex-max includes xhigh", () => {
  2558. const model = createMockModel({
  2559. id: "gpt-5.1-codex-max",
  2560. providerID: "github-copilot",
  2561. api: {
  2562. id: "gpt-5.1-codex-max",
  2563. url: "https://api.githubcopilot.com",
  2564. npm: "@ai-sdk/github-copilot",
  2565. },
  2566. })
  2567. const result = ProviderTransform.variants(model)
  2568. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2569. })
  2570. test("gpt-5.1-codex-mini does not include xhigh", () => {
  2571. const model = createMockModel({
  2572. id: "gpt-5.1-codex-mini",
  2573. providerID: "github-copilot",
  2574. api: {
  2575. id: "gpt-5.1-codex-mini",
  2576. url: "https://api.githubcopilot.com",
  2577. npm: "@ai-sdk/github-copilot",
  2578. },
  2579. })
  2580. const result = ProviderTransform.variants(model)
  2581. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2582. })
  2583. test("gpt-5.1-codex does not include xhigh", () => {
  2584. const model = createMockModel({
  2585. id: "gpt-5.1-codex",
  2586. providerID: "github-copilot",
  2587. api: {
  2588. id: "gpt-5.1-codex",
  2589. url: "https://api.githubcopilot.com",
  2590. npm: "@ai-sdk/github-copilot",
  2591. },
  2592. })
  2593. const result = ProviderTransform.variants(model)
  2594. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2595. })
  2596. test("gpt-5.2 includes xhigh", () => {
  2597. const model = createMockModel({
  2598. id: "gpt-5.2",
  2599. providerID: "github-copilot",
  2600. api: {
  2601. id: "gpt-5.2",
  2602. url: "https://api.githubcopilot.com",
  2603. npm: "@ai-sdk/github-copilot",
  2604. },
  2605. })
  2606. const result = ProviderTransform.variants(model)
  2607. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2608. expect(result.xhigh).toEqual({
  2609. reasoningEffort: "xhigh",
  2610. reasoningSummary: "auto",
  2611. include: ["reasoning.encrypted_content"],
  2612. })
  2613. })
  2614. test("gpt-5.2-codex includes xhigh", () => {
  2615. const model = createMockModel({
  2616. id: "gpt-5.2-codex",
  2617. providerID: "github-copilot",
  2618. api: {
  2619. id: "gpt-5.2-codex",
  2620. url: "https://api.githubcopilot.com",
  2621. npm: "@ai-sdk/github-copilot",
  2622. },
  2623. })
  2624. const result = ProviderTransform.variants(model)
  2625. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2626. })
  2627. test("gpt-5.3-codex includes xhigh", () => {
  2628. const model = createMockModel({
  2629. id: "gpt-5.3-codex",
  2630. providerID: "github-copilot",
  2631. api: {
  2632. id: "gpt-5.3-codex",
  2633. url: "https://api.githubcopilot.com",
  2634. npm: "@ai-sdk/github-copilot",
  2635. },
  2636. })
  2637. const result = ProviderTransform.variants(model)
  2638. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2639. })
  2640. test("gpt-5.4 includes xhigh", () => {
  2641. const model = createMockModel({
  2642. id: "gpt-5.4",
  2643. release_date: "2026-03-05",
  2644. providerID: "github-copilot",
  2645. api: {
  2646. id: "gpt-5.4",
  2647. url: "https://api.githubcopilot.com",
  2648. npm: "@ai-sdk/github-copilot",
  2649. },
  2650. })
  2651. const result = ProviderTransform.variants(model)
  2652. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh"])
  2653. })
  2654. })
  2655. describe("@ai-sdk/cerebras", () => {
  2656. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2657. const model = createMockModel({
  2658. id: "cerebras/llama-4",
  2659. providerID: "cerebras",
  2660. api: {
  2661. id: "llama-4-sc",
  2662. url: "https://api.cerebras.ai",
  2663. npm: "@ai-sdk/cerebras",
  2664. },
  2665. })
  2666. const result = ProviderTransform.variants(model)
  2667. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2668. expect(result.low).toEqual({ reasoningEffort: "low" })
  2669. expect(result.high).toEqual({ reasoningEffort: "high" })
  2670. })
  2671. })
  2672. describe("@ai-sdk/togetherai", () => {
  2673. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2674. const model = createMockModel({
  2675. id: "togetherai/llama-4",
  2676. providerID: "togetherai",
  2677. api: {
  2678. id: "llama-4-sc",
  2679. url: "https://api.togetherai.com",
  2680. npm: "@ai-sdk/togetherai",
  2681. },
  2682. })
  2683. const result = ProviderTransform.variants(model)
  2684. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2685. expect(result.low).toEqual({ reasoningEffort: "low" })
  2686. expect(result.high).toEqual({ reasoningEffort: "high" })
  2687. })
  2688. })
  2689. describe("@ai-sdk/xai", () => {
  2690. test("grok-3 returns empty object", () => {
  2691. const model = createMockModel({
  2692. id: "xai/grok-3",
  2693. providerID: "xai",
  2694. api: {
  2695. id: "grok-3",
  2696. url: "https://api.x.ai",
  2697. npm: "@ai-sdk/xai",
  2698. },
  2699. })
  2700. const result = ProviderTransform.variants(model)
  2701. expect(result).toEqual({})
  2702. })
  2703. test("grok-3-mini returns low and high with reasoningEffort", () => {
  2704. const model = createMockModel({
  2705. id: "xai/grok-3-mini",
  2706. providerID: "xai",
  2707. api: {
  2708. id: "grok-3-mini",
  2709. url: "https://api.x.ai",
  2710. npm: "@ai-sdk/xai",
  2711. },
  2712. })
  2713. const result = ProviderTransform.variants(model)
  2714. expect(Object.keys(result)).toEqual(["low", "high"])
  2715. expect(result.low).toEqual({ reasoningEffort: "low" })
  2716. expect(result.high).toEqual({ reasoningEffort: "high" })
  2717. })
  2718. })
  2719. describe("@ai-sdk/deepinfra", () => {
  2720. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2721. const model = createMockModel({
  2722. id: "deepinfra/llama-4",
  2723. providerID: "deepinfra",
  2724. api: {
  2725. id: "llama-4-sc",
  2726. url: "https://api.deepinfra.com",
  2727. npm: "@ai-sdk/deepinfra",
  2728. },
  2729. })
  2730. const result = ProviderTransform.variants(model)
  2731. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2732. expect(result.low).toEqual({ reasoningEffort: "low" })
  2733. expect(result.high).toEqual({ reasoningEffort: "high" })
  2734. })
  2735. })
  2736. describe("@ai-sdk/openai-compatible", () => {
  2737. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningEffort", () => {
  2738. const model = createMockModel({
  2739. id: "custom-provider/custom-model",
  2740. providerID: "custom-provider",
  2741. api: {
  2742. id: "custom-model",
  2743. url: "https://api.custom.com",
  2744. npm: "@ai-sdk/openai-compatible",
  2745. },
  2746. })
  2747. const result = ProviderTransform.variants(model)
  2748. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2749. expect(result.low).toEqual({ reasoningEffort: "low" })
  2750. expect(result.high).toEqual({ reasoningEffort: "high" })
  2751. })
  2752. })
  2753. describe("@ai-sdk/azure", () => {
  2754. test("o1-mini returns empty object", () => {
  2755. const model = createMockModel({
  2756. id: "o1-mini",
  2757. providerID: "azure",
  2758. api: {
  2759. id: "o1-mini",
  2760. url: "https://azure.com",
  2761. npm: "@ai-sdk/azure",
  2762. },
  2763. })
  2764. const result = ProviderTransform.variants(model)
  2765. expect(result).toEqual({})
  2766. })
  2767. test("standard azure models return custom efforts with reasoningSummary", () => {
  2768. const model = createMockModel({
  2769. id: "o1",
  2770. providerID: "azure",
  2771. api: {
  2772. id: "o1",
  2773. url: "https://azure.com",
  2774. npm: "@ai-sdk/azure",
  2775. },
  2776. })
  2777. const result = ProviderTransform.variants(model)
  2778. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2779. expect(result.low).toEqual({
  2780. reasoningEffort: "low",
  2781. reasoningSummary: "auto",
  2782. include: ["reasoning.encrypted_content"],
  2783. })
  2784. })
  2785. test("gpt-5 adds minimal effort", () => {
  2786. const model = createMockModel({
  2787. id: "gpt-5",
  2788. providerID: "azure",
  2789. api: {
  2790. id: "gpt-5",
  2791. url: "https://azure.com",
  2792. npm: "@ai-sdk/azure",
  2793. },
  2794. })
  2795. const result = ProviderTransform.variants(model)
  2796. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  2797. })
  2798. for (const id of ["gpt-5-4", "gpt-5-5"]) {
  2799. test(`${id} does not add minimal effort`, () => {
  2800. const result = ProviderTransform.variants(
  2801. createMockModel({
  2802. id,
  2803. providerID: "azure",
  2804. api: {
  2805. id,
  2806. url: "https://azure.com",
  2807. npm: "@ai-sdk/azure",
  2808. },
  2809. }),
  2810. )
  2811. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2812. })
  2813. }
  2814. })
  2815. describe("@ai-sdk/openai", () => {
  2816. test("gpt-5-pro returns only high effort", () => {
  2817. const model = createMockModel({
  2818. id: "gpt-5-pro",
  2819. providerID: "openai",
  2820. api: {
  2821. id: "gpt-5-pro",
  2822. url: "https://api.openai.com",
  2823. npm: "@ai-sdk/openai",
  2824. },
  2825. })
  2826. const result = ProviderTransform.variants(model)
  2827. expect(Object.keys(result)).toEqual(["high"])
  2828. })
  2829. test("standard openai models return custom efforts with reasoningSummary", () => {
  2830. const model = createMockModel({
  2831. id: "gpt-5",
  2832. providerID: "openai",
  2833. api: {
  2834. id: "gpt-5",
  2835. url: "https://api.openai.com",
  2836. npm: "@ai-sdk/openai",
  2837. },
  2838. release_date: "2024-06-01",
  2839. })
  2840. const result = ProviderTransform.variants(model)
  2841. expect(Object.keys(result)).toEqual(["minimal", "low", "medium", "high"])
  2842. expect(result.low).toEqual({
  2843. reasoningEffort: "low",
  2844. reasoningSummary: "auto",
  2845. include: ["reasoning.encrypted_content"],
  2846. })
  2847. })
  2848. test("models after 2025-11-13 include 'none' effort", () => {
  2849. const model = createMockModel({
  2850. id: "gpt-5-nano",
  2851. providerID: "openai",
  2852. api: {
  2853. id: "gpt-5-nano",
  2854. url: "https://api.openai.com",
  2855. npm: "@ai-sdk/openai",
  2856. },
  2857. release_date: "2025-11-14",
  2858. })
  2859. const result = ProviderTransform.variants(model)
  2860. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high"])
  2861. })
  2862. test("models after 2025-12-04 include 'xhigh' effort", () => {
  2863. const model = createMockModel({
  2864. id: "openai/gpt-5-reasoning",
  2865. providerID: "openai",
  2866. api: {
  2867. id: "gpt-5-reasoning",
  2868. url: "https://api.openai.com",
  2869. npm: "@ai-sdk/openai",
  2870. },
  2871. release_date: "2025-12-05",
  2872. })
  2873. const result = ProviderTransform.variants(model)
  2874. expect(Object.keys(result)).toEqual(["none", "minimal", "low", "medium", "high", "xhigh"])
  2875. })
  2876. for (const testCase of [
  2877. { id: "o1", releaseDate: "2024-12-17", efforts: ["low", "medium", "high"] },
  2878. { id: "o1-pro", releaseDate: "2025-03-19", efforts: ["low", "medium", "high"] },
  2879. { id: "o3", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  2880. { id: "o3-mini", releaseDate: "2025-01-31", efforts: ["low", "medium", "high"] },
  2881. { id: "o3-pro", releaseDate: "2025-06-10", efforts: ["low", "medium", "high"] },
  2882. { id: "o4-mini", releaseDate: "2025-04-16", efforts: ["low", "medium", "high"] },
  2883. { id: "o3-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  2884. { id: "o4-mini-deep-research", releaseDate: "2025-06-26", efforts: ["medium"] },
  2885. { id: "gpt-5.1", releaseDate: "2025-11-13", efforts: ["none", "low", "medium", "high"] },
  2886. { id: "gpt-5.4", releaseDate: "2026-03-05", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2887. {
  2888. id: "gpt-5.5",
  2889. modelID: "gpt-5-5",
  2890. releaseDate: "2026-04-23",
  2891. efforts: ["none", "low", "medium", "high", "xhigh"],
  2892. },
  2893. { id: "gpt-5.4-pro", releaseDate: "2026-03-05", efforts: ["medium", "high", "xhigh"] },
  2894. { id: "gpt-5.5-pro", releaseDate: "2026-04-23", efforts: ["medium", "high", "xhigh"] },
  2895. { id: "gpt-5-codex", releaseDate: "2025-09-23", efforts: ["low", "medium", "high"] },
  2896. { id: "gpt-5.1-codex", releaseDate: "2025-11-13", efforts: ["low", "medium", "high"] },
  2897. { id: "gpt-5.1-codex-max", releaseDate: "2025-11-13", efforts: ["low", "medium", "high", "xhigh"] },
  2898. { id: "gpt-5.2-codex", releaseDate: "2025-12-11", efforts: ["low", "medium", "high", "xhigh"] },
  2899. { id: "gpt-5.3-codex", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2900. { id: "gpt-5.3-codex-max", releaseDate: "2026-01-22", efforts: ["none", "low", "medium", "high", "xhigh"] },
  2901. { id: "gpt-5-chat-latest", releaseDate: "2025-08-07", efforts: [] },
  2902. { id: "gpt-5.1-chat-latest", releaseDate: "2025-11-13", efforts: ["medium"] },
  2903. { id: "gpt-5.2-chat-latest", releaseDate: "2025-12-11", efforts: ["medium"] },
  2904. ]) {
  2905. test(`${testCase.id} returns supported reasoning efforts`, () => {
  2906. const result = ProviderTransform.variants(
  2907. createMockModel({
  2908. id: testCase.modelID ?? testCase.id,
  2909. providerID: "openai",
  2910. api: {
  2911. id: testCase.id,
  2912. url: "https://api.openai.com",
  2913. npm: "@ai-sdk/openai",
  2914. },
  2915. release_date: testCase.releaseDate,
  2916. }),
  2917. )
  2918. expect(Object.keys(result)).toEqual(testCase.efforts)
  2919. })
  2920. }
  2921. test("gpt-50 (lookalike) does not get gpt-5 family treatment", () => {
  2922. const model = createMockModel({
  2923. id: "gpt-50",
  2924. providerID: "openai",
  2925. api: {
  2926. id: "gpt-50",
  2927. url: "https://api.openai.com",
  2928. npm: "@ai-sdk/openai",
  2929. },
  2930. release_date: "2024-01-01",
  2931. })
  2932. const result = ProviderTransform.variants(model)
  2933. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  2934. })
  2935. })
  2936. describe("@ai-sdk/anthropic", () => {
  2937. for (const testCase of [
  2938. {
  2939. name: "opus 4.5",
  2940. apiIds: ["claude-opus-4-5-20251101", "claude-opus-4.5-20251101"],
  2941. efforts: ["low", "medium", "high"],
  2942. expectedHigh: { effort: "high" },
  2943. },
  2944. {
  2945. name: "sonnet 4.6",
  2946. apiIds: ["claude-sonnet-4-6", "claude-sonnet-4.6"],
  2947. efforts: ["low", "medium", "high", "max"],
  2948. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  2949. },
  2950. {
  2951. name: "opus 4.6",
  2952. apiIds: ["claude-opus-4-6", "claude-opus-4.6"],
  2953. efforts: ["low", "medium", "high", "max"],
  2954. expectedHigh: { thinking: { type: "adaptive" }, effort: "high" },
  2955. },
  2956. {
  2957. name: "opus 4.7",
  2958. apiIds: ["claude-opus-4-7", "claude-opus-4.7"],
  2959. efforts: ["low", "medium", "high", "xhigh", "max"],
  2960. expectedHigh: { thinking: { type: "adaptive", display: "summarized" }, effort: "high" },
  2961. },
  2962. ]) {
  2963. for (const apiId of testCase.apiIds) {
  2964. test(`${testCase.name} ${apiId} returns supported reasoning efforts`, () => {
  2965. const result = ProviderTransform.variants(
  2966. createMockModel({
  2967. id: `anthropic/${apiId}`,
  2968. providerID: "anthropic",
  2969. api: {
  2970. id: apiId,
  2971. url: "https://api.anthropic.com",
  2972. npm: "@ai-sdk/anthropic",
  2973. },
  2974. }),
  2975. )
  2976. expect(Object.keys(result)).toEqual(testCase.efforts)
  2977. expect(result.high).toEqual(testCase.expectedHigh)
  2978. })
  2979. }
  2980. }
  2981. test("github copilot opus 4.7 returns only medium reasoning effort", () => {
  2982. const model = createMockModel({
  2983. id: "claude-opus-4.7",
  2984. providerID: "github-copilot",
  2985. api: {
  2986. id: "claude-opus-4.7",
  2987. url: "https://api.githubcopilot.com/v1",
  2988. npm: "@ai-sdk/anthropic",
  2989. },
  2990. })
  2991. const result = ProviderTransform.variants(model)
  2992. expect(result).toEqual({
  2993. medium: {
  2994. thinking: {
  2995. type: "adaptive",
  2996. display: "summarized",
  2997. },
  2998. effort: "medium",
  2999. },
  3000. })
  3001. })
  3002. test("returns high and max with thinking config", () => {
  3003. const model = createMockModel({
  3004. id: "anthropic/claude-4",
  3005. providerID: "anthropic",
  3006. api: {
  3007. id: "claude-4",
  3008. url: "https://api.anthropic.com",
  3009. npm: "@ai-sdk/anthropic",
  3010. },
  3011. })
  3012. const result = ProviderTransform.variants(model)
  3013. expect(Object.keys(result)).toEqual(["high", "max"])
  3014. expect(result.high).toEqual({
  3015. thinking: {
  3016. type: "enabled",
  3017. budgetTokens: 16000,
  3018. },
  3019. })
  3020. expect(result.max).toEqual({
  3021. thinking: {
  3022. type: "enabled",
  3023. budgetTokens: 31999,
  3024. },
  3025. })
  3026. })
  3027. })
  3028. describe("@ai-sdk/amazon-bedrock", () => {
  3029. test("anthropic sonnet 4.6 returns adaptive reasoning options", () => {
  3030. const model = createMockModel({
  3031. id: "bedrock/anthropic-claude-sonnet-4-6",
  3032. providerID: "bedrock",
  3033. api: {
  3034. id: "anthropic.claude-sonnet-4-6",
  3035. url: "https://bedrock.amazonaws.com",
  3036. npm: "@ai-sdk/amazon-bedrock",
  3037. },
  3038. })
  3039. const result = ProviderTransform.variants(model)
  3040. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3041. expect(result.max).toEqual({
  3042. reasoningConfig: {
  3043. type: "adaptive",
  3044. maxReasoningEffort: "max",
  3045. },
  3046. })
  3047. })
  3048. test("anthropic opus 4.7 returns adaptive reasoning options with xhigh", () => {
  3049. const model = createMockModel({
  3050. id: "bedrock/anthropic-claude-opus-4-7",
  3051. providerID: "bedrock",
  3052. api: {
  3053. id: "anthropic.claude-opus-4-7",
  3054. url: "https://bedrock.amazonaws.com",
  3055. npm: "@ai-sdk/amazon-bedrock",
  3056. },
  3057. })
  3058. const result = ProviderTransform.variants(model)
  3059. expect(Object.keys(result)).toEqual(["low", "medium", "high", "xhigh", "max"])
  3060. expect(result.xhigh).toEqual({
  3061. reasoningConfig: {
  3062. type: "adaptive",
  3063. maxReasoningEffort: "xhigh",
  3064. display: "summarized",
  3065. },
  3066. })
  3067. expect(result.max).toEqual({
  3068. reasoningConfig: {
  3069. type: "adaptive",
  3070. maxReasoningEffort: "max",
  3071. display: "summarized",
  3072. },
  3073. })
  3074. })
  3075. test("returns WIDELY_SUPPORTED_EFFORTS with reasoningConfig", () => {
  3076. const model = createMockModel({
  3077. id: "bedrock/llama-4",
  3078. providerID: "bedrock",
  3079. api: {
  3080. id: "llama-4-sc",
  3081. url: "https://bedrock.amazonaws.com",
  3082. npm: "@ai-sdk/amazon-bedrock",
  3083. },
  3084. })
  3085. const result = ProviderTransform.variants(model)
  3086. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3087. expect(result.low).toEqual({
  3088. reasoningConfig: {
  3089. type: "enabled",
  3090. maxReasoningEffort: "low",
  3091. },
  3092. })
  3093. })
  3094. })
  3095. for (const provider of [
  3096. { name: "@ai-sdk/google", providerID: "google", url: "https://generativelanguage.googleapis.com" },
  3097. { name: "@ai-sdk/google-vertex", providerID: "google-vertex", url: "https://vertexai.googleapis.com" },
  3098. ]) {
  3099. describe(provider.name, () => {
  3100. for (const testCase of [
  3101. {
  3102. apiId: "gemini-2.5-pro",
  3103. efforts: ["high", "max"],
  3104. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  3105. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } },
  3106. },
  3107. {
  3108. apiId: "gemini-2.5-flash",
  3109. efforts: ["high", "max"],
  3110. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } },
  3111. expectedMax: { thinkingConfig: { includeThoughts: true, thinkingBudget: 24_576 } },
  3112. },
  3113. {
  3114. apiId: "gemini-3-pro-preview",
  3115. efforts: ["low", "medium", "high"],
  3116. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3117. },
  3118. {
  3119. apiId: "gemini-3.1-pro-preview",
  3120. efforts: ["low", "medium", "high"],
  3121. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3122. },
  3123. {
  3124. apiId: "gemini-3-flash-preview",
  3125. efforts: ["minimal", "low", "medium", "high"],
  3126. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3127. },
  3128. {
  3129. apiId: "gemini-3.1-flash-lite",
  3130. efforts: ["minimal", "low", "medium", "high"],
  3131. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3132. },
  3133. {
  3134. apiId: "gemini-3.1-flash-image-preview",
  3135. efforts: ["minimal", "high"],
  3136. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3137. },
  3138. {
  3139. apiId: "gemini-3-pro-image-preview",
  3140. efforts: ["high"],
  3141. expectedHigh: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
  3142. },
  3143. ]) {
  3144. test(`${testCase.apiId} returns supported thinking controls`, () => {
  3145. const result = ProviderTransform.variants(
  3146. createMockModel({
  3147. id: `${provider.providerID}/${testCase.apiId}`,
  3148. providerID: provider.providerID,
  3149. api: {
  3150. id: testCase.apiId,
  3151. url: provider.url,
  3152. npm: provider.name,
  3153. },
  3154. }),
  3155. )
  3156. expect(Object.keys(result)).toEqual(testCase.efforts)
  3157. expect(result.high).toEqual(testCase.expectedHigh)
  3158. if (testCase.expectedMax) expect(result.max).toEqual(testCase.expectedMax)
  3159. })
  3160. }
  3161. })
  3162. }
  3163. describe("@ai-sdk/cohere", () => {
  3164. test("returns empty object", () => {
  3165. const model = createMockModel({
  3166. id: "cohere/command-r",
  3167. providerID: "cohere",
  3168. api: {
  3169. id: "command-r",
  3170. url: "https://api.cohere.com",
  3171. npm: "@ai-sdk/cohere",
  3172. },
  3173. })
  3174. const result = ProviderTransform.variants(model)
  3175. expect(result).toEqual({})
  3176. })
  3177. })
  3178. describe("@ai-sdk/groq", () => {
  3179. test("returns none and WIDELY_SUPPORTED_EFFORTS with thinkingLevel", () => {
  3180. const model = createMockModel({
  3181. id: "groq/llama-4",
  3182. providerID: "groq",
  3183. api: {
  3184. id: "llama-4-sc",
  3185. url: "https://api.groq.com",
  3186. npm: "@ai-sdk/groq",
  3187. },
  3188. })
  3189. const result = ProviderTransform.variants(model)
  3190. expect(Object.keys(result)).toEqual(["none", "low", "medium", "high"])
  3191. expect(result.none).toEqual({
  3192. reasoningEffort: "none",
  3193. })
  3194. expect(result.low).toEqual({
  3195. reasoningEffort: "low",
  3196. })
  3197. })
  3198. })
  3199. describe("@ai-sdk/perplexity", () => {
  3200. test("returns empty object", () => {
  3201. const model = createMockModel({
  3202. id: "perplexity/sonar-plus",
  3203. providerID: "perplexity",
  3204. api: {
  3205. id: "sonar-plus",
  3206. url: "https://api.perplexity.ai",
  3207. npm: "@ai-sdk/perplexity",
  3208. },
  3209. })
  3210. const result = ProviderTransform.variants(model)
  3211. expect(result).toEqual({})
  3212. })
  3213. })
  3214. describe("@jerome-benoit/sap-ai-provider-v2", () => {
  3215. test("anthropic models return thinking variants", () => {
  3216. const model = createMockModel({
  3217. id: "sap-ai-core/anthropic--claude-sonnet-4",
  3218. providerID: "sap-ai-core",
  3219. api: {
  3220. id: "anthropic--claude-sonnet-4",
  3221. url: "https://api.ai.sap",
  3222. npm: "@jerome-benoit/sap-ai-provider-v2",
  3223. },
  3224. })
  3225. const result = ProviderTransform.variants(model)
  3226. expect(Object.keys(result)).toEqual(["high", "max"])
  3227. expect(result.high).toEqual({
  3228. thinking: {
  3229. type: "enabled",
  3230. budgetTokens: 16000,
  3231. },
  3232. })
  3233. expect(result.max).toEqual({
  3234. thinking: {
  3235. type: "enabled",
  3236. budgetTokens: 31999,
  3237. },
  3238. })
  3239. })
  3240. test("anthropic 4.6 models return adaptive thinking variants", () => {
  3241. const model = createMockModel({
  3242. id: "sap-ai-core/anthropic--claude-sonnet-4-6",
  3243. providerID: "sap-ai-core",
  3244. api: {
  3245. id: "anthropic--claude-sonnet-4-6",
  3246. url: "https://api.ai.sap",
  3247. npm: "@jerome-benoit/sap-ai-provider-v2",
  3248. },
  3249. })
  3250. const result = ProviderTransform.variants(model)
  3251. expect(Object.keys(result)).toEqual(["low", "medium", "high", "max"])
  3252. expect(result.low).toEqual({
  3253. thinking: {
  3254. type: "adaptive",
  3255. },
  3256. effort: "low",
  3257. })
  3258. expect(result.max).toEqual({
  3259. thinking: {
  3260. type: "adaptive",
  3261. },
  3262. effort: "max",
  3263. })
  3264. })
  3265. test("gemini 2.5 models return thinkingConfig variants", () => {
  3266. const model = createMockModel({
  3267. id: "sap-ai-core/gcp--gemini-2.5-pro",
  3268. providerID: "sap-ai-core",
  3269. api: {
  3270. id: "gcp--gemini-2.5-pro",
  3271. url: "https://api.ai.sap",
  3272. npm: "@jerome-benoit/sap-ai-provider-v2",
  3273. },
  3274. })
  3275. const result = ProviderTransform.variants(model)
  3276. expect(Object.keys(result)).toEqual(["high", "max"])
  3277. expect(result.high).toEqual({
  3278. thinkingConfig: {
  3279. includeThoughts: true,
  3280. thinkingBudget: 16000,
  3281. },
  3282. })
  3283. expect(result.max).toEqual({
  3284. thinkingConfig: {
  3285. includeThoughts: true,
  3286. thinkingBudget: 24576,
  3287. },
  3288. })
  3289. })
  3290. test("gpt models return reasoningEffort variants", () => {
  3291. const model = createMockModel({
  3292. id: "sap-ai-core/azure-openai--gpt-4o",
  3293. providerID: "sap-ai-core",
  3294. api: {
  3295. id: "azure-openai--gpt-4o",
  3296. url: "https://api.ai.sap",
  3297. npm: "@jerome-benoit/sap-ai-provider-v2",
  3298. },
  3299. })
  3300. const result = ProviderTransform.variants(model)
  3301. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3302. expect(result.low).toEqual({ reasoningEffort: "low" })
  3303. expect(result.high).toEqual({ reasoningEffort: "high" })
  3304. })
  3305. test("o-series models return reasoningEffort variants", () => {
  3306. const model = createMockModel({
  3307. id: "sap-ai-core/azure-openai--o3-mini",
  3308. providerID: "sap-ai-core",
  3309. api: {
  3310. id: "azure-openai--o3-mini",
  3311. url: "https://api.ai.sap",
  3312. npm: "@jerome-benoit/sap-ai-provider-v2",
  3313. },
  3314. })
  3315. const result = ProviderTransform.variants(model)
  3316. expect(Object.keys(result)).toEqual(["low", "medium", "high"])
  3317. expect(result.low).toEqual({ reasoningEffort: "low" })
  3318. expect(result.high).toEqual({ reasoningEffort: "high" })
  3319. })
  3320. test("sonar models return empty object", () => {
  3321. const model = createMockModel({
  3322. id: "sap-ai-core/perplexity--sonar-pro",
  3323. providerID: "sap-ai-core",
  3324. api: {
  3325. id: "perplexity--sonar-pro",
  3326. url: "https://api.ai.sap",
  3327. npm: "@jerome-benoit/sap-ai-provider-v2",
  3328. },
  3329. })
  3330. const result = ProviderTransform.variants(model)
  3331. expect(result).toEqual({})
  3332. })
  3333. test("mistral models return empty object", () => {
  3334. const model = createMockModel({
  3335. id: "sap-ai-core/mistral--mistral-large",
  3336. providerID: "sap-ai-core",
  3337. api: {
  3338. id: "mistral--mistral-large",
  3339. url: "https://api.ai.sap",
  3340. npm: "@jerome-benoit/sap-ai-provider-v2",
  3341. },
  3342. })
  3343. const result = ProviderTransform.variants(model)
  3344. expect(result).toEqual({})
  3345. })
  3346. })
  3347. describe("ai-gateway-provider (cloudflare-ai-gateway)", () => {
  3348. const cfModel = (apiId: string, releaseDate = "2024-01-01") =>
  3349. createMockModel({
  3350. id: `cloudflare-ai-gateway/${apiId}`,
  3351. providerID: "cloudflare-ai-gateway",
  3352. api: {
  3353. id: apiId,
  3354. url: "https://gateway.ai.cloudflare.com/v1/compat",
  3355. npm: "ai-gateway-provider",
  3356. },
  3357. release_date: releaseDate,
  3358. })
  3359. for (const testCase of [
  3360. { id: "openai/gpt-5.4", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3361. { id: "openai/gpt-5.2-codex", efforts: ["low", "medium", "high", "xhigh"] },
  3362. { id: "openai/gpt-5.3-codex", efforts: ["none", "low", "medium", "high", "xhigh"] },
  3363. { id: "openai/gpt-5-pro", efforts: ["high"] },
  3364. { id: "openai/gpt-5.2-pro", efforts: ["medium", "high", "xhigh"] },
  3365. { id: "openai/gpt-5-chat-latest", efforts: [] },
  3366. { id: "openai/gpt-5.2-chat-latest", efforts: ["medium"] },
  3367. ]) {
  3368. test(`${testCase.id} returns supported reasoning efforts`, () => {
  3369. const result = ProviderTransform.variants(cfModel(testCase.id, "2026-03-05"))
  3370. expect(Object.keys(result)).toEqual(testCase.efforts)
  3371. })
  3372. }
  3373. test("openai gpt-4o (no reasoning) returns empty", () => {
  3374. const model = cfModel("openai/gpt-4o")
  3375. model.capabilities.reasoning = false
  3376. const result = ProviderTransform.variants(model)
  3377. expect(result).toEqual({})
  3378. })
  3379. test("non-openai upstream falls back to widely-supported OAI efforts", () => {
  3380. const result = ProviderTransform.variants(cfModel("anthropic/claude-sonnet-4-6"))
  3381. expect(result).toEqual({
  3382. low: { reasoningEffort: "low" },
  3383. medium: { reasoningEffort: "medium" },
  3384. high: { reasoningEffort: "high" },
  3385. })
  3386. })
  3387. })
  3388. })
  3389. describe("ProviderTransform.smallOptions - gpt-5 chat/search", () => {
  3390. const createModel = (apiId: string) => {
  3391. const model = {
  3392. id: `openai/${apiId}`,
  3393. providerID: "openai",
  3394. api: {
  3395. id: apiId,
  3396. url: "https://api.openai.com",
  3397. npm: "@ai-sdk/openai",
  3398. },
  3399. capabilities: { reasoning: true },
  3400. limit: { output: 64_000 },
  3401. release_date: "2026-01-01",
  3402. } as any
  3403. model.variants = ProviderTransform.variants(model)
  3404. return model
  3405. }
  3406. for (const testCase of [
  3407. { id: "gpt-5-chat-latest", options: { store: false } },
  3408. {
  3409. id: "gpt-5.1-chat-latest",
  3410. options: {
  3411. store: false,
  3412. reasoningEffort: "medium",
  3413. reasoningSummary: "auto",
  3414. include: ["reasoning.encrypted_content"],
  3415. },
  3416. },
  3417. {
  3418. id: "gpt-5.2-chat-latest",
  3419. options: {
  3420. store: false,
  3421. reasoningEffort: "medium",
  3422. reasoningSummary: "auto",
  3423. include: ["reasoning.encrypted_content"],
  3424. },
  3425. },
  3426. {
  3427. id: "gpt-5-search-api",
  3428. options: {
  3429. store: false,
  3430. reasoningEffort: "none",
  3431. reasoningSummary: "auto",
  3432. include: ["reasoning.encrypted_content"],
  3433. },
  3434. },
  3435. ]) {
  3436. test(`${testCase.id} returns only supported small options`, () => {
  3437. expect(ProviderTransform.smallOptions(createModel(testCase.id))).toEqual(testCase.options)
  3438. })
  3439. }
  3440. })
  3441. describe("ProviderTransform.smallOptions - google thinking controls", () => {
  3442. const createGoogleModel = (apiId: string) => {
  3443. const model = {
  3444. id: `google/${apiId}`,
  3445. providerID: "google",
  3446. api: {
  3447. id: apiId,
  3448. url: "https://generativelanguage.googleapis.com",
  3449. npm: "@ai-sdk/google",
  3450. },
  3451. capabilities: { reasoning: true },
  3452. limit: { output: 64_000 },
  3453. } as any
  3454. model.variants = ProviderTransform.variants(model)
  3455. return model
  3456. }
  3457. for (const testCase of [
  3458. { id: "gemini-3-pro-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "low" } } },
  3459. { id: "gemini-3-flash-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } } },
  3460. {
  3461. id: "gemini-3.1-flash-image-preview",
  3462. options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "minimal" } },
  3463. },
  3464. { id: "gemini-3-pro-image-preview", options: { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } } },
  3465. { id: "gemini-2.5-pro", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  3466. { id: "gemini-2.5-flash", options: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } } },
  3467. ]) {
  3468. test(`${testCase.id} returns supported small thinking options`, () => {
  3469. expect(ProviderTransform.smallOptions(createGoogleModel(testCase.id))).toEqual(testCase.options)
  3470. })
  3471. }
  3472. test("uses the first configured variant when available", () => {
  3473. expect(
  3474. ProviderTransform.smallOptions({
  3475. ...createGoogleModel("gemini-2.5-pro"),
  3476. variants: {
  3477. high: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } },
  3478. max: { thinkingConfig: { includeThoughts: true, thinkingBudget: 32768 } },
  3479. },
  3480. }),
  3481. ).toEqual({ thinkingConfig: { includeThoughts: true, thinkingBudget: 16000 } })
  3482. })
  3483. test("does not synthesize thinking options when variants are empty", () => {
  3484. expect(ProviderTransform.smallOptions({ ...createGoogleModel("gemini-2.5-pro"), variants: {} })).toEqual({})
  3485. })
  3486. })
  3487. describe("ProviderTransform.providerOptions - ai-gateway-provider", () => {
  3488. const createModel = (overrides: Partial<any> = {}) =>
  3489. ({
  3490. id: "cloudflare-ai-gateway/openai/gpt-5.4",
  3491. providerID: "cloudflare-ai-gateway",
  3492. api: {
  3493. id: "openai/gpt-5.4",
  3494. url: "https://gateway.ai.cloudflare.com/v1/compat",
  3495. npm: "ai-gateway-provider",
  3496. },
  3497. capabilities: {
  3498. temperature: false,
  3499. reasoning: true,
  3500. attachment: true,
  3501. toolcall: true,
  3502. input: { text: true, audio: false, image: true, video: false, pdf: true },
  3503. output: { text: true, audio: false, image: false, video: false, pdf: false },
  3504. interleaved: false,
  3505. },
  3506. cost: { input: 1, output: 1, cache: { read: 0, write: 0 } },
  3507. limit: { context: 1_000_000, output: 128_000 },
  3508. status: "active",
  3509. options: {},
  3510. headers: {},
  3511. release_date: "2026-03-05",
  3512. ...overrides,
  3513. }) as any
  3514. test("routes options under openaiCompatible (the key @ai-sdk/openai-compatible reads)", () => {
  3515. // Regression: previously fell back to providerID="cloudflare-ai-gateway",
  3516. // which @ai-sdk/openai-compatible never reads, silently dropping reasoningEffort.
  3517. const result = ProviderTransform.providerOptions(createModel(), { reasoningEffort: "high" })
  3518. expect(result).toEqual({ openaiCompatible: { reasoningEffort: "high" } })
  3519. })
  3520. })