transform.test.ts 13 KB

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