transform.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import { describe, expect, test } from "bun:test"
  2. import { ProviderTransform } from "../../src/provider/transform"
  3. const OUTPUT_TOKEN_MAX = 32000
  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(mockModel, sessionID, { setCacheKey: true })
  39. expect(result.promptCacheKey).toBe(sessionID)
  40. })
  41. test("should not set promptCacheKey when providerOptions.setCacheKey is false", () => {
  42. const result = ProviderTransform.options(mockModel, sessionID, { setCacheKey: false })
  43. expect(result.promptCacheKey).toBeUndefined()
  44. })
  45. test("should not set promptCacheKey when providerOptions is undefined", () => {
  46. const result = ProviderTransform.options(mockModel, sessionID, undefined)
  47. expect(result.promptCacheKey).toBeUndefined()
  48. })
  49. test("should not set promptCacheKey when providerOptions does not have setCacheKey", () => {
  50. const result = ProviderTransform.options(mockModel, sessionID, {})
  51. expect(result.promptCacheKey).toBeUndefined()
  52. })
  53. test("should set promptCacheKey for openai provider regardless of setCacheKey", () => {
  54. const openaiModel = {
  55. ...mockModel,
  56. providerID: "openai",
  57. api: {
  58. id: "gpt-4",
  59. url: "https://api.openai.com",
  60. npm: "@ai-sdk/openai",
  61. },
  62. }
  63. const result = ProviderTransform.options(openaiModel, sessionID, {})
  64. expect(result.promptCacheKey).toBe(sessionID)
  65. })
  66. })
  67. describe("ProviderTransform.maxOutputTokens", () => {
  68. test("returns 32k when modelLimit > 32k", () => {
  69. const modelLimit = 100000
  70. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai", {}, modelLimit, OUTPUT_TOKEN_MAX)
  71. expect(result).toBe(OUTPUT_TOKEN_MAX)
  72. })
  73. test("returns modelLimit when modelLimit < 32k", () => {
  74. const modelLimit = 16000
  75. const result = ProviderTransform.maxOutputTokens("@ai-sdk/openai", {}, modelLimit, OUTPUT_TOKEN_MAX)
  76. expect(result).toBe(16000)
  77. })
  78. describe("azure", () => {
  79. test("returns 32k when modelLimit > 32k", () => {
  80. const modelLimit = 100000
  81. const result = ProviderTransform.maxOutputTokens("@ai-sdk/azure", {}, modelLimit, OUTPUT_TOKEN_MAX)
  82. expect(result).toBe(OUTPUT_TOKEN_MAX)
  83. })
  84. test("returns modelLimit when modelLimit < 32k", () => {
  85. const modelLimit = 16000
  86. const result = ProviderTransform.maxOutputTokens("@ai-sdk/azure", {}, modelLimit, OUTPUT_TOKEN_MAX)
  87. expect(result).toBe(16000)
  88. })
  89. })
  90. describe("bedrock", () => {
  91. test("returns 32k when modelLimit > 32k", () => {
  92. const modelLimit = 100000
  93. const result = ProviderTransform.maxOutputTokens("@ai-sdk/amazon-bedrock", {}, modelLimit, OUTPUT_TOKEN_MAX)
  94. expect(result).toBe(OUTPUT_TOKEN_MAX)
  95. })
  96. test("returns modelLimit when modelLimit < 32k", () => {
  97. const modelLimit = 16000
  98. const result = ProviderTransform.maxOutputTokens("@ai-sdk/amazon-bedrock", {}, modelLimit, OUTPUT_TOKEN_MAX)
  99. expect(result).toBe(16000)
  100. })
  101. })
  102. describe("anthropic without thinking options", () => {
  103. test("returns 32k when modelLimit > 32k", () => {
  104. const modelLimit = 100000
  105. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", {}, modelLimit, OUTPUT_TOKEN_MAX)
  106. expect(result).toBe(OUTPUT_TOKEN_MAX)
  107. })
  108. test("returns modelLimit when modelLimit < 32k", () => {
  109. const modelLimit = 16000
  110. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", {}, modelLimit, OUTPUT_TOKEN_MAX)
  111. expect(result).toBe(16000)
  112. })
  113. })
  114. describe("anthropic with thinking options", () => {
  115. test("returns 32k when budgetTokens + 32k <= modelLimit", () => {
  116. const modelLimit = 100000
  117. const options = {
  118. thinking: {
  119. type: "enabled",
  120. budgetTokens: 10000,
  121. },
  122. }
  123. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", options, modelLimit, OUTPUT_TOKEN_MAX)
  124. expect(result).toBe(OUTPUT_TOKEN_MAX)
  125. })
  126. test("returns modelLimit - budgetTokens when budgetTokens + 32k > modelLimit", () => {
  127. const modelLimit = 50000
  128. const options = {
  129. thinking: {
  130. type: "enabled",
  131. budgetTokens: 30000,
  132. },
  133. }
  134. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", options, modelLimit, OUTPUT_TOKEN_MAX)
  135. expect(result).toBe(20000)
  136. })
  137. test("returns 32k when thinking type is not enabled", () => {
  138. const modelLimit = 100000
  139. const options = {
  140. thinking: {
  141. type: "disabled",
  142. budgetTokens: 10000,
  143. },
  144. }
  145. const result = ProviderTransform.maxOutputTokens("@ai-sdk/anthropic", options, modelLimit, OUTPUT_TOKEN_MAX)
  146. expect(result).toBe(OUTPUT_TOKEN_MAX)
  147. })
  148. })
  149. })
  150. describe("ProviderTransform.message - DeepSeek reasoning content", () => {
  151. test("DeepSeek with tool calls includes reasoning_content in providerOptions", () => {
  152. const msgs = [
  153. {
  154. role: "assistant",
  155. content: [
  156. { type: "reasoning", text: "Let me think about this..." },
  157. {
  158. type: "tool-call",
  159. toolCallId: "test",
  160. toolName: "bash",
  161. input: { command: "echo hello" },
  162. },
  163. ],
  164. },
  165. ] as any[]
  166. const result = ProviderTransform.message(msgs, {
  167. id: "deepseek/deepseek-chat",
  168. providerID: "deepseek",
  169. api: {
  170. id: "deepseek-chat",
  171. url: "https://api.deepseek.com",
  172. npm: "@ai-sdk/openai-compatible",
  173. },
  174. name: "DeepSeek Chat",
  175. capabilities: {
  176. temperature: true,
  177. reasoning: true,
  178. attachment: false,
  179. toolcall: true,
  180. input: { text: true, audio: false, image: false, video: false, pdf: false },
  181. output: { text: true, audio: false, image: false, video: false, pdf: false },
  182. interleaved: false,
  183. },
  184. cost: {
  185. input: 0.001,
  186. output: 0.002,
  187. cache: { read: 0.0001, write: 0.0002 },
  188. },
  189. limit: {
  190. context: 128000,
  191. output: 8192,
  192. },
  193. status: "active",
  194. options: {},
  195. headers: {},
  196. release_date: "2023-04-01",
  197. })
  198. expect(result).toHaveLength(1)
  199. expect(result[0].content).toEqual([
  200. {
  201. type: "tool-call",
  202. toolCallId: "test",
  203. toolName: "bash",
  204. input: { command: "echo hello" },
  205. },
  206. ])
  207. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBe("Let me think about this...")
  208. })
  209. test("DeepSeek model ID containing 'deepseek' matches (case insensitive)", () => {
  210. const msgs = [
  211. {
  212. role: "assistant",
  213. content: [
  214. { type: "reasoning", text: "Thinking..." },
  215. {
  216. type: "tool-call",
  217. toolCallId: "test",
  218. toolName: "get_weather",
  219. input: { location: "Hangzhou" },
  220. },
  221. ],
  222. },
  223. ] as any[]
  224. const result = ProviderTransform.message(msgs, {
  225. id: "someprovider/deepseek-reasoner",
  226. providerID: "someprovider",
  227. api: {
  228. id: "deepseek-reasoner",
  229. url: "https://api.someprovider.com",
  230. npm: "@ai-sdk/openai-compatible",
  231. },
  232. name: "SomeProvider DeepSeek Reasoner",
  233. capabilities: {
  234. temperature: true,
  235. reasoning: true,
  236. attachment: false,
  237. toolcall: true,
  238. input: { text: true, audio: false, image: false, video: false, pdf: false },
  239. output: { text: true, audio: false, image: false, video: false, pdf: false },
  240. interleaved: false,
  241. },
  242. cost: {
  243. input: 0.001,
  244. output: 0.002,
  245. cache: { read: 0.0001, write: 0.0002 },
  246. },
  247. limit: {
  248. context: 128000,
  249. output: 8192,
  250. },
  251. status: "active",
  252. options: {},
  253. headers: {},
  254. release_date: "2023-04-01",
  255. })
  256. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBe("Thinking...")
  257. })
  258. test("Non-DeepSeek providers leave reasoning content unchanged", () => {
  259. const msgs = [
  260. {
  261. role: "assistant",
  262. content: [
  263. { type: "reasoning", text: "Should not be processed" },
  264. { type: "text", text: "Answer" },
  265. ],
  266. },
  267. ] as any[]
  268. const result = ProviderTransform.message(msgs, {
  269. id: "openai/gpt-4",
  270. providerID: "openai",
  271. api: {
  272. id: "gpt-4",
  273. url: "https://api.openai.com",
  274. npm: "@ai-sdk/openai",
  275. },
  276. name: "GPT-4",
  277. capabilities: {
  278. temperature: true,
  279. reasoning: false,
  280. attachment: true,
  281. toolcall: true,
  282. input: { text: true, audio: false, image: true, video: false, pdf: false },
  283. output: { text: true, audio: false, image: false, video: false, pdf: false },
  284. interleaved: false,
  285. },
  286. cost: {
  287. input: 0.03,
  288. output: 0.06,
  289. cache: { read: 0.001, write: 0.002 },
  290. },
  291. limit: {
  292. context: 128000,
  293. output: 4096,
  294. },
  295. status: "active",
  296. options: {},
  297. headers: {},
  298. release_date: "2023-04-01",
  299. })
  300. expect(result[0].content).toEqual([
  301. { type: "reasoning", text: "Should not be processed" },
  302. { type: "text", text: "Answer" },
  303. ])
  304. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBeUndefined()
  305. })
  306. })
  307. describe("ProviderTransform.message - empty image handling", () => {
  308. const mockModel = {
  309. id: "anthropic/claude-3-5-sonnet",
  310. providerID: "anthropic",
  311. api: {
  312. id: "claude-3-5-sonnet-20241022",
  313. url: "https://api.anthropic.com",
  314. npm: "@ai-sdk/anthropic",
  315. },
  316. name: "Claude 3.5 Sonnet",
  317. capabilities: {
  318. temperature: true,
  319. reasoning: false,
  320. attachment: true,
  321. toolcall: true,
  322. input: { text: true, audio: false, image: true, video: false, pdf: true },
  323. output: { text: true, audio: false, image: false, video: false, pdf: false },
  324. interleaved: false,
  325. },
  326. cost: {
  327. input: 0.003,
  328. output: 0.015,
  329. cache: { read: 0.0003, write: 0.00375 },
  330. },
  331. limit: {
  332. context: 200000,
  333. output: 8192,
  334. },
  335. status: "active",
  336. options: {},
  337. headers: {},
  338. } as any
  339. test("should replace empty base64 image with error text", () => {
  340. const msgs = [
  341. {
  342. role: "user",
  343. content: [
  344. { type: "text", text: "What is in this image?" },
  345. { type: "image", image: "data:image/png;base64," },
  346. ],
  347. },
  348. ] as any[]
  349. const result = ProviderTransform.message(msgs, mockModel)
  350. expect(result).toHaveLength(1)
  351. expect(result[0].content).toHaveLength(2)
  352. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  353. expect(result[0].content[1]).toEqual({
  354. type: "text",
  355. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  356. })
  357. })
  358. test("should keep valid base64 images unchanged", () => {
  359. const validBase64 =
  360. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  361. const msgs = [
  362. {
  363. role: "user",
  364. content: [
  365. { type: "text", text: "What is in this image?" },
  366. { type: "image", image: `data:image/png;base64,${validBase64}` },
  367. ],
  368. },
  369. ] as any[]
  370. const result = ProviderTransform.message(msgs, mockModel)
  371. expect(result).toHaveLength(1)
  372. expect(result[0].content).toHaveLength(2)
  373. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  374. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  375. })
  376. test("should handle mixed valid and empty images", () => {
  377. const validBase64 =
  378. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  379. const msgs = [
  380. {
  381. role: "user",
  382. content: [
  383. { type: "text", text: "Compare these images" },
  384. { type: "image", image: `data:image/png;base64,${validBase64}` },
  385. { type: "image", image: "data:image/jpeg;base64," },
  386. ],
  387. },
  388. ] as any[]
  389. const result = ProviderTransform.message(msgs, mockModel)
  390. expect(result).toHaveLength(1)
  391. expect(result[0].content).toHaveLength(3)
  392. expect(result[0].content[0]).toEqual({ type: "text", text: "Compare these images" })
  393. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  394. expect(result[0].content[2]).toEqual({
  395. type: "text",
  396. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  397. })
  398. })
  399. })