google-shared-convert-tools.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { describe, expect, it } from "vitest";
  2. import { convertTools } from "../src/providers/google-shared.ts";
  3. import type { Tool } from "../src/types.ts";
  4. function makeTool(parameters: Record<string, unknown>): Tool {
  5. return {
  6. name: "test_tool",
  7. description: "A test tool",
  8. parameters: parameters as Tool["parameters"],
  9. };
  10. }
  11. describe("google-shared convertTools", () => {
  12. it("strips JSON Schema meta keys from parameters when useParameters=true", () => {
  13. const tools = [
  14. makeTool({
  15. $schema: "http://json-schema.org/draft-07/schema#",
  16. $id: "urn:bash-tool",
  17. $comment: "A bash tool for demonstration",
  18. $defs: {
  19. commandDef: { type: "string" },
  20. },
  21. definitions: {
  22. legacyDef: { type: "number" },
  23. },
  24. type: "object",
  25. properties: {
  26. command: { type: "string" },
  27. },
  28. required: ["command"],
  29. }),
  30. ];
  31. const result = convertTools(tools, true);
  32. const decl = result?.[0]?.functionDeclarations?.[0];
  33. expect(decl).toBeDefined();
  34. expect(decl?.parameters).toEqual({
  35. type: "object",
  36. properties: {
  37. command: { type: "string" },
  38. },
  39. required: ["command"],
  40. });
  41. expect(decl?.parameters).not.toHaveProperty("$schema");
  42. expect(decl?.parameters).not.toHaveProperty("$id");
  43. expect(decl?.parameters).not.toHaveProperty("$comment");
  44. expect(decl?.parameters).not.toHaveProperty("$defs");
  45. expect(decl?.parameters).not.toHaveProperty("definitions");
  46. });
  47. it("recursively strips nested JSON Schema meta keys", () => {
  48. const tools = [
  49. makeTool({
  50. $schema: "http://json-schema.org/draft-07/schema#",
  51. type: "object",
  52. properties: {
  53. deep: {
  54. $schema: "http://json-schema.org/draft-07/schema#",
  55. $id: "urn:nested",
  56. type: "string",
  57. },
  58. },
  59. }),
  60. ];
  61. const result = convertTools(tools, true);
  62. const decl = result?.[0]?.functionDeclarations?.[0];
  63. expect(decl).toBeDefined();
  64. expect(decl?.parameters).toEqual({
  65. type: "object",
  66. properties: {
  67. deep: {
  68. type: "string",
  69. },
  70. },
  71. });
  72. });
  73. it("preserves $ref while stripping meta keys", () => {
  74. const tools = [
  75. makeTool({
  76. $schema: "http://json-schema.org/draft-07/schema#",
  77. type: "object",
  78. properties: {
  79. refProp: {
  80. $ref: "#/$defs/someDef",
  81. type: "string",
  82. },
  83. },
  84. }),
  85. ];
  86. const result = convertTools(tools, true);
  87. const decl = result?.[0]?.functionDeclarations?.[0];
  88. expect(decl).toBeDefined();
  89. expect(decl?.parameters).toEqual({
  90. type: "object",
  91. properties: {
  92. refProp: {
  93. $ref: "#/$defs/someDef",
  94. type: "string",
  95. },
  96. },
  97. });
  98. });
  99. it("does not mutate the original Tool.parameters object", () => {
  100. const originalParameters = {
  101. $schema: "http://json-schema.org/draft-07/schema#",
  102. type: "object",
  103. properties: {
  104. command: { type: "string" },
  105. },
  106. required: ["command"],
  107. };
  108. const tools = [makeTool(originalParameters)];
  109. convertTools(tools, true);
  110. expect(originalParameters).toEqual({
  111. $schema: "http://json-schema.org/draft-07/schema#",
  112. type: "object",
  113. properties: {
  114. command: { type: "string" },
  115. },
  116. required: ["command"],
  117. });
  118. });
  119. it("preserves $schema in parametersJsonSchema when useParameters=false", () => {
  120. const tools = [
  121. makeTool({
  122. $schema: "http://json-schema.org/draft-07/schema#",
  123. type: "object",
  124. properties: {
  125. command: { type: "string" },
  126. },
  127. required: ["command"],
  128. }),
  129. ];
  130. const result = convertTools(tools, false);
  131. const decl = result?.[0]?.functionDeclarations?.[0];
  132. expect(decl).toBeDefined();
  133. expect(decl?.parametersJsonSchema).toEqual({
  134. $schema: "http://json-schema.org/draft-07/schema#",
  135. type: "object",
  136. properties: {
  137. command: { type: "string" },
  138. },
  139. required: ["command"],
  140. });
  141. });
  142. it("handles tools without $schema gracefully", () => {
  143. const tools = [
  144. makeTool({
  145. type: "object",
  146. properties: {
  147. path: { type: "string" },
  148. },
  149. required: ["path"],
  150. }),
  151. ];
  152. const result = convertTools(tools, true);
  153. const decl = result?.[0]?.functionDeclarations?.[0];
  154. expect(decl).toBeDefined();
  155. expect(decl?.parameters).toEqual({
  156. type: "object",
  157. properties: {
  158. path: { type: "string" },
  159. },
  160. required: ["path"],
  161. });
  162. });
  163. it("returns undefined for empty tool list", () => {
  164. expect(convertTools([])).toBeUndefined();
  165. expect(convertTools([], true)).toBeUndefined();
  166. });
  167. });