google-shared-gemini3-unsigned-tool-call.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 makeGemini3Model<TApi extends "google-generative-ai" | "google-vertex">(
  5. api: TApi,
  6. provider: Model<TApi>["provider"],
  7. id = "gemini-3-pro-preview",
  8. ): Model<TApi> {
  9. return {
  10. id,
  11. name: "Gemini 3 Pro Preview",
  12. api,
  13. provider,
  14. baseUrl: "https://example.com",
  15. reasoning: true,
  16. input: ["text"],
  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 }, thoughtSignature?: string): Context {
  23. const now = Date.now();
  24. return {
  25. messages: [
  26. { role: "user", content: "Hi", timestamp: now },
  27. {
  28. role: "assistant",
  29. content: [
  30. {
  31. type: "toolCall",
  32. id: "call_1",
  33. name: "bash",
  34. arguments: { command: "echo hi" },
  35. ...(thoughtSignature && { thoughtSignature }),
  36. },
  37. {
  38. type: "toolCall",
  39. id: "call_2",
  40. name: "bash",
  41. arguments: { command: "ls -la" },
  42. },
  43. ],
  44. api: model.api,
  45. provider: model.provider,
  46. model: model.id,
  47. usage: {
  48. input: 0,
  49. output: 0,
  50. cacheRead: 0,
  51. cacheWrite: 0,
  52. totalTokens: 0,
  53. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  54. },
  55. stopReason: "toolUse",
  56. timestamp: now,
  57. },
  58. ],
  59. };
  60. }
  61. describe("google-shared convertMessages — Gemini 3 unsigned tool calls", () => {
  62. it("does not add skip_thought_signature_validator for unsigned Google Gen AI tool calls", () => {
  63. const model = makeGemini3Model("google-generative-ai", "google");
  64. const contents = convertMessages(model, makeContext({ ...model, id: "other-model" }));
  65. const modelTurn = contents.find((c) => c.role === "model");
  66. expect(modelTurn).toBeTruthy();
  67. const functionCallParts = modelTurn?.parts?.filter((p) => p.functionCall !== undefined) ?? [];
  68. expect(functionCallParts).toHaveLength(2);
  69. expect(functionCallParts[0]?.thoughtSignature).toBeUndefined();
  70. expect(functionCallParts[1]?.thoughtSignature).toBeUndefined();
  71. expect(JSON.stringify(modelTurn)).not.toContain("skip_thought_signature_validator");
  72. const textParts = modelTurn?.parts?.filter((p) => p.text !== undefined) ?? [];
  73. const historicalText = textParts.filter((p) => p.text?.includes("Historical context"));
  74. expect(historicalText).toHaveLength(0);
  75. });
  76. it("does not add skip_thought_signature_validator for unsigned Vertex tool calls", () => {
  77. const model = makeGemini3Model("google-vertex", "google-vertex");
  78. const contents = convertMessages(model, makeContext(model));
  79. const modelTurn = contents.find((c) => c.role === "model");
  80. const functionCallParts = modelTurn?.parts?.filter((p) => p.functionCall !== undefined) ?? [];
  81. expect(functionCallParts).toHaveLength(2);
  82. expect(functionCallParts[0]?.thoughtSignature).toBeUndefined();
  83. expect(functionCallParts[1]?.thoughtSignature).toBeUndefined();
  84. expect(JSON.stringify(modelTurn)).not.toContain("skip_thought_signature_validator");
  85. });
  86. it("preserves valid thoughtSignature when present for the same provider and model", () => {
  87. const model = makeGemini3Model("google-generative-ai", "google");
  88. const validSig = "AAAAAAAAAAAAAAAAAAAAAA==";
  89. const contents = convertMessages(model, makeContext(model, validSig));
  90. const modelTurn = contents.find((c) => c.role === "model");
  91. const functionCallParts = modelTurn?.parts?.filter((p) => p.functionCall !== undefined) ?? [];
  92. expect(functionCallParts).toHaveLength(2);
  93. expect(functionCallParts[0]?.thoughtSignature).toBe(validSig);
  94. expect(functionCallParts[1]?.thoughtSignature).toBeUndefined();
  95. });
  96. it("does not add a thoughtSignature for non-Gemini-3 models", () => {
  97. const model = makeGemini3Model("google-generative-ai", "google", "gemini-2.5-flash");
  98. const contents = convertMessages(model, makeContext({ ...model, id: "other-model" }));
  99. const modelTurn = contents.find((c) => c.role === "model");
  100. const fcPart = modelTurn?.parts?.find((p) => p.functionCall !== undefined);
  101. expect(fcPart).toBeTruthy();
  102. expect(fcPart?.thoughtSignature).toBeUndefined();
  103. });
  104. });