tool-json-schema.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // 只有 default、没有 optional 的字段在输出里一定有值,算必填。
  46. expect(JSON.stringify(schema)).toBe(
  47. '{"$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","count"],"additionalProperties":false}',
  48. );
  49. });
  50. it("leaves optional fields out of required, including default-then-optional and preprocess", () => {
  51. const schema = toToolJsonSchema(
  52. z.object({
  53. flag: z.boolean().default(false).optional(),
  54. limit: z
  55. .preprocess((value) => value, z.number().int().min(1).max(50))
  56. .default(20)
  57. .optional(),
  58. offset: z.preprocess((value) => value, z.number().optional()),
  59. name: z.string(),
  60. }),
  61. );
  62. expect(schema.required).toEqual(["name"]);
  63. });
  64. it("writes unions and nullable fields as anyOf branches", () => {
  65. const schema = toToolJsonSchema(
  66. z.object({
  67. delay: z.number().int().positive().nullable().optional(),
  68. target: z.discriminatedUnion("kind", [
  69. z.object({ kind: z.literal("a") }).strict(),
  70. z.object({ kind: z.literal("b") }).strict(),
  71. ]),
  72. }),
  73. );
  74. expect(JSON.stringify(schema.properties)).toBe(
  75. '{"delay":{"anyOf":[{"type":"integer","exclusiveMinimum":0,"maximum":9007199254740991},{"type":"null"}]},"target":{"anyOf":[{"type":"object","properties":{"kind":{"type":"string","const":"a"}},"required":["kind"],"additionalProperties":false},{"type":"object","properties":{"kind":{"type":"string","const":"b"}},"required":["kind"],"additionalProperties":false}]}}',
  76. );
  77. });
  78. it("rejects zod types that JSON Schema cannot represent", () => {
  79. expect(() => toToolJsonSchema(z.object({ when: z.date() }))).toThrow(
  80. "Date cannot be represented in JSON Schema",
  81. );
  82. // 自定义清单曾漏掉 undefined:它会静默变成 `{}`,模型看到一个什么都接受的参数。
  83. expect(() => toToolJsonSchema(z.object({ gone: z.undefined() }))).toThrow(
  84. "Undefined cannot be represented in JSON Schema",
  85. );
  86. });
  87. it("keeps property declaration order even when a property is named like a schema keyword", () => {
  88. const schema = toToolJsonSchema(
  89. z.object({
  90. label: z.string(),
  91. description: z.string(),
  92. type: z.string(),
  93. }),
  94. );
  95. expect(Object.keys(schema.properties as Record<string, unknown>)).toEqual([
  96. "label",
  97. "description",
  98. "type",
  99. ]);
  100. });
  101. });