google-shared-image-tool-result-routing.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { describe, expect, it } from "vitest";
  2. import { convertMessages } from "../src/providers/google-shared.ts";
  3. import type { Context, Model } from "../src/types.ts";
  4. function makeModel<TApi extends "google-generative-ai">(
  5. api: TApi,
  6. provider: Model<TApi>["provider"],
  7. id: string,
  8. ): Model<TApi> {
  9. return {
  10. id,
  11. name: id,
  12. api,
  13. provider,
  14. baseUrl: "https://example.com",
  15. reasoning: true,
  16. input: ["text", "image"],
  17. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  18. contextWindow: 128000,
  19. maxTokens: 8192,
  20. };
  21. }
  22. function makeContext(model: { api: string; provider: string; id: string }): Context {
  23. const now = Date.now();
  24. return {
  25. messages: [
  26. { role: "user", content: "read the files", timestamp: now },
  27. {
  28. role: "assistant",
  29. content: [
  30. { type: "toolCall", id: "call_a", name: "read", arguments: { path: "a.txt" } },
  31. { type: "toolCall", id: "call_img", name: "read", arguments: { path: "image.png" } },
  32. { type: "toolCall", id: "call_b", name: "read", arguments: { path: "b.txt" } },
  33. ],
  34. api: model.api,
  35. provider: model.provider,
  36. model: model.id,
  37. usage: {
  38. input: 0,
  39. output: 0,
  40. cacheRead: 0,
  41. cacheWrite: 0,
  42. totalTokens: 0,
  43. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  44. },
  45. stopReason: "toolUse",
  46. timestamp: now,
  47. },
  48. {
  49. role: "toolResult",
  50. toolCallId: "call_a",
  51. toolName: "read",
  52. content: [{ type: "text", text: "alpha text" }],
  53. isError: false,
  54. timestamp: now,
  55. },
  56. {
  57. role: "toolResult",
  58. toolCallId: "call_img",
  59. toolName: "read",
  60. content: [{ type: "image", data: "abc", mimeType: "image/png" }],
  61. isError: false,
  62. timestamp: now,
  63. },
  64. {
  65. role: "toolResult",
  66. toolCallId: "call_b",
  67. toolName: "read",
  68. content: [{ type: "text", text: "beta text" }],
  69. isError: false,
  70. timestamp: now,
  71. },
  72. ],
  73. };
  74. }
  75. describe("google-shared image tool result routing", () => {
  76. it("keeps separate synthetic image turn for Gemini 2.x Google API models", () => {
  77. const model = makeModel("google-generative-ai", "google", "gemini-2.5-flash");
  78. const contents = convertMessages(model, makeContext(model));
  79. expect(contents).toHaveLength(5);
  80. expect(contents[2].parts?.every((part) => part.functionResponse)).toBe(true);
  81. expect(contents[3].parts?.[0]?.text).toBe("Tool result image:");
  82. expect(contents[3].parts?.[1]?.inlineData).toBeTruthy();
  83. expect(contents[4].parts?.[0]?.functionResponse).toBeTruthy();
  84. });
  85. it("nests image tool results for Gemini 3 Google API models", () => {
  86. const model = makeModel("google-generative-ai", "google", "gemini-3-pro-preview");
  87. const contents = convertMessages(model, makeContext(model));
  88. expect(contents).toHaveLength(3);
  89. const toolResultTurn = contents[2];
  90. expect(toolResultTurn.parts).toHaveLength(3);
  91. const imageResponse = toolResultTurn.parts?.[1]?.functionResponse;
  92. expect(imageResponse).toBeTruthy();
  93. expect(imageResponse?.parts).toHaveLength(1);
  94. expect(imageResponse?.parts?.[0]?.inlineData).toBeTruthy();
  95. });
  96. });