transform.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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: {
  183. field: "reasoning_content",
  184. },
  185. },
  186. cost: {
  187. input: 0.001,
  188. output: 0.002,
  189. cache: { read: 0.0001, write: 0.0002 },
  190. },
  191. limit: {
  192. context: 128000,
  193. output: 8192,
  194. },
  195. status: "active",
  196. options: {},
  197. headers: {},
  198. release_date: "2023-04-01",
  199. })
  200. expect(result).toHaveLength(1)
  201. expect(result[0].content).toEqual([
  202. {
  203. type: "tool-call",
  204. toolCallId: "test",
  205. toolName: "bash",
  206. input: { command: "echo hello" },
  207. },
  208. ])
  209. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBe("Let me think about this...")
  210. })
  211. test("Non-DeepSeek providers leave reasoning content unchanged", () => {
  212. const msgs = [
  213. {
  214. role: "assistant",
  215. content: [
  216. { type: "reasoning", text: "Should not be processed" },
  217. { type: "text", text: "Answer" },
  218. ],
  219. },
  220. ] as any[]
  221. const result = ProviderTransform.message(msgs, {
  222. id: "openai/gpt-4",
  223. providerID: "openai",
  224. api: {
  225. id: "gpt-4",
  226. url: "https://api.openai.com",
  227. npm: "@ai-sdk/openai",
  228. },
  229. name: "GPT-4",
  230. capabilities: {
  231. temperature: true,
  232. reasoning: false,
  233. attachment: true,
  234. toolcall: true,
  235. input: { text: true, audio: false, image: true, video: false, pdf: false },
  236. output: { text: true, audio: false, image: false, video: false, pdf: false },
  237. interleaved: false,
  238. },
  239. cost: {
  240. input: 0.03,
  241. output: 0.06,
  242. cache: { read: 0.001, write: 0.002 },
  243. },
  244. limit: {
  245. context: 128000,
  246. output: 4096,
  247. },
  248. status: "active",
  249. options: {},
  250. headers: {},
  251. release_date: "2023-04-01",
  252. })
  253. expect(result[0].content).toEqual([
  254. { type: "reasoning", text: "Should not be processed" },
  255. { type: "text", text: "Answer" },
  256. ])
  257. expect(result[0].providerOptions?.openaiCompatible?.reasoning_content).toBeUndefined()
  258. })
  259. })
  260. describe("ProviderTransform.message - empty image handling", () => {
  261. const mockModel = {
  262. id: "anthropic/claude-3-5-sonnet",
  263. providerID: "anthropic",
  264. api: {
  265. id: "claude-3-5-sonnet-20241022",
  266. url: "https://api.anthropic.com",
  267. npm: "@ai-sdk/anthropic",
  268. },
  269. name: "Claude 3.5 Sonnet",
  270. capabilities: {
  271. temperature: true,
  272. reasoning: false,
  273. attachment: true,
  274. toolcall: true,
  275. input: { text: true, audio: false, image: true, video: false, pdf: true },
  276. output: { text: true, audio: false, image: false, video: false, pdf: false },
  277. interleaved: false,
  278. },
  279. cost: {
  280. input: 0.003,
  281. output: 0.015,
  282. cache: { read: 0.0003, write: 0.00375 },
  283. },
  284. limit: {
  285. context: 200000,
  286. output: 8192,
  287. },
  288. status: "active",
  289. options: {},
  290. headers: {},
  291. } as any
  292. test("should replace empty base64 image with error text", () => {
  293. const msgs = [
  294. {
  295. role: "user",
  296. content: [
  297. { type: "text", text: "What is in this image?" },
  298. { type: "image", image: "data:image/png;base64," },
  299. ],
  300. },
  301. ] as any[]
  302. const result = ProviderTransform.message(msgs, mockModel)
  303. expect(result).toHaveLength(1)
  304. expect(result[0].content).toHaveLength(2)
  305. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  306. expect(result[0].content[1]).toEqual({
  307. type: "text",
  308. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  309. })
  310. })
  311. test("should keep valid base64 images unchanged", () => {
  312. const validBase64 =
  313. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  314. const msgs = [
  315. {
  316. role: "user",
  317. content: [
  318. { type: "text", text: "What is in this image?" },
  319. { type: "image", image: `data:image/png;base64,${validBase64}` },
  320. ],
  321. },
  322. ] as any[]
  323. const result = ProviderTransform.message(msgs, mockModel)
  324. expect(result).toHaveLength(1)
  325. expect(result[0].content).toHaveLength(2)
  326. expect(result[0].content[0]).toEqual({ type: "text", text: "What is in this image?" })
  327. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  328. })
  329. test("should handle mixed valid and empty images", () => {
  330. const validBase64 =
  331. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
  332. const msgs = [
  333. {
  334. role: "user",
  335. content: [
  336. { type: "text", text: "Compare these images" },
  337. { type: "image", image: `data:image/png;base64,${validBase64}` },
  338. { type: "image", image: "data:image/jpeg;base64," },
  339. ],
  340. },
  341. ] as any[]
  342. const result = ProviderTransform.message(msgs, mockModel)
  343. expect(result).toHaveLength(1)
  344. expect(result[0].content).toHaveLength(3)
  345. expect(result[0].content[0]).toEqual({ type: "text", text: "Compare these images" })
  346. expect(result[0].content[1]).toEqual({ type: "image", image: `data:image/png;base64,${validBase64}` })
  347. expect(result[0].content[2]).toEqual({
  348. type: "text",
  349. text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
  350. })
  351. })
  352. })