tools-schema.spec.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { describe, expect, it } from "vitest"
  2. import { toOpenAITools } from "./tools-schema"
  3. import type { Tool } from "./types"
  4. describe("toOpenAITools", () => {
  5. it("converts a simple tool to OpenAI format", () => {
  6. const tool: Tool = {
  7. name: "read_chapter",
  8. description: "读取章节全文",
  9. category: "read",
  10. parameters: {
  11. name: { type: "string", description: "章节名称" },
  12. },
  13. execute: async () => "",
  14. }
  15. const result = toOpenAITools([tool])
  16. expect(result).toEqual([
  17. {
  18. type: "function",
  19. function: {
  20. name: "read_chapter",
  21. description: "读取章节全文",
  22. parameters: {
  23. type: "object",
  24. properties: {
  25. name: { type: "string", description: "章节名称" },
  26. },
  27. required: [],
  28. },
  29. },
  30. },
  31. ])
  32. })
  33. it("marks required parameters", () => {
  34. const tool: Tool = {
  35. name: "foo",
  36. description: "",
  37. category: "read",
  38. parameters: {
  39. a: { type: "string", description: "a", required: true },
  40. b: { type: "number", description: "b" },
  41. },
  42. execute: async () => "",
  43. }
  44. const result = toOpenAITools([tool])
  45. expect(result[0].function.parameters.required).toEqual(["a"])
  46. })
  47. it("includes enum when present", () => {
  48. const tool: Tool = {
  49. name: "foo",
  50. description: "",
  51. category: "action",
  52. parameters: {
  53. mode: { type: "string", description: "mode", enum: ["a", "b"], required: true },
  54. },
  55. execute: async () => "",
  56. }
  57. const result = toOpenAITools([tool])
  58. const modeProp = result[0].function.parameters.properties.mode as { enum?: string[] }
  59. expect(modeProp.enum).toEqual(["a", "b"])
  60. })
  61. })