tool-json-schema.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it } from "vitest";
  2. import { z } from "zod";
  3. import { EditInputJsonSchema } from "../src/tools/edit.js";
  4. import { toToolJsonSchema } from "../src/tools/json-schema.js";
  5. import { EnterPlanModeInputJsonSchema } from "../src/tools/plan-mode.js";
  6. import { SkillInputJsonSchema } from "../src/tools/skill.js";
  7. import { ToolSearchInputJsonSchema } from "../src/tools/tool-search.js";
  8. import { WebSearchInputJsonSchema } from "../src/tools/websearch.js";
  9. import { WriteInputJsonSchema } from "../src/tools/write.js";
  10. // input_schema 的固定字节:key 顺序也计入 tools 缓存前缀。
  11. const EXPECTED_INPUT_SCHEMAS = {
  12. Edit: '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"file_path":{"description":"The absolute path to the file to modify","type":"string"},"old_string":{"description":"The text to replace","type":"string"},"new_string":{"description":"The text to replace it with (must be different from old_string)","type":"string"},"replace_all":{"description":"Replace all occurrences of old_string (default false)","default":false,"type":"boolean"}},"required":["file_path","old_string","new_string"],"additionalProperties":false}',
  13. Write:
  14. '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"file_path":{"description":"The absolute path to the file to write (must be absolute, not relative)","type":"string"},"content":{"description":"The content to write to the file","type":"string"}},"required":["file_path","content"],"additionalProperties":false}',
  15. EnterPlanMode:
  16. '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{},"additionalProperties":false}',
  17. Skill:
  18. '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"skill":{"description":"The name of a skill from the available-skills list. Do not guess names.","type":"string"},"args":{"description":"Optional arguments for the skill","type":"string"}},"required":["skill"],"additionalProperties":false}',
  19. WebSearch:
  20. '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"query":{"description":"The search query to use","type":"string","minLength":2},"allowed_domains":{"description":"Only include search results from these domains","type":"array","items":{"type":"string"}},"blocked_domains":{"description":"Never include search results from these domains","type":"array","items":{"type":"string"}}},"required":["query"],"additionalProperties":false}',
  21. ToolSearch:
  22. '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"query":{"description":"Query to find deferred tools. Use \\"select:<tool_name>\\" for direct selection, or keywords to search. Regular expressions are not supported.","type":"string"},"max_results":{"description":"Maximum number of results to return (default: 5)","default":5,"type":"number"}},"required":["query","max_results"],"additionalProperties":false}',
  23. };
  24. describe("tool JSON schema key order", () => {
  25. it("serializes shared tool input schemas byte-for-byte", () => {
  26. const actual = {
  27. Edit: EditInputJsonSchema,
  28. Write: WriteInputJsonSchema,
  29. EnterPlanMode: EnterPlanModeInputJsonSchema,
  30. Skill: SkillInputJsonSchema,
  31. WebSearch: WebSearchInputJsonSchema,
  32. ToolSearch: ToolSearchInputJsonSchema,
  33. };
  34. for (const [name, expected] of Object.entries(EXPECTED_INPUT_SCHEMAS)) {
  35. expect(JSON.stringify(actual[name as keyof typeof actual]), name).toBe(expected);
  36. }
  37. });
  38. it("puts $schema first, description and default before type, and array bounds before type", () => {
  39. const schema = toToolJsonSchema(
  40. z.object({
  41. tags: z.array(z.string().max(12)).min(1).max(4).describe("Tags"),
  42. count: z.number().int().min(0).max(9).default(3).describe("Count"),
  43. }),
  44. );
  45. expect(JSON.stringify(schema)).toBe(
  46. '{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"tags":{"description":"Tags","minItems":1,"maxItems":4,"type":"array","items":{"type":"string","maxLength":12}},"count":{"description":"Count","default":3,"type":"integer","minimum":0,"maximum":9}},"required":["tags"],"additionalProperties":false}',
  47. );
  48. });
  49. it("keeps property declaration order even when a property is named like a schema keyword", () => {
  50. const schema = toToolJsonSchema(
  51. z.object({
  52. label: z.string(),
  53. description: z.string(),
  54. type: z.string(),
  55. }),
  56. );
  57. expect(Object.keys(schema.properties as Record<string, unknown>)).toEqual([
  58. "label",
  59. "description",
  60. "type",
  61. ]);
  62. });
  63. });