summaryrefslogtreecommitdiffhomepage
path: root/packages/openai-stream/src/convert-tools.test.ts
blob: 907ea158274d4f6215b4fd7399984cf39fb653aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type { ToolContract } from "@dispatch/kernel";
import { describe, expect, it } from "vitest";
import { convertTools } from "./convert-tools.js";

describe("convertTools", () => {
  it("converts a single tool to OpenAI function format", () => {
    const tools: ToolContract[] = [
      {
        name: "read_file",
        description: "Read a file from disk",
        parameters: {
          type: "object",
          properties: {
            path: { type: "string", description: "File path" },
          },
          required: ["path"],
          additionalProperties: false,
        },
        execute: async () => ({ content: "" }),
      },
    ];

    const result = convertTools(tools);
    expect(result).toEqual([
      {
        type: "function",
        function: {
          name: "read_file",
          description: "Read a file from disk",
          parameters: {
            type: "object",
            properties: {
              path: { type: "string", description: "File path" },
            },
            required: ["path"],
            additionalProperties: false,
          },
        },
      },
    ]);
  });

  it("converts multiple tools", () => {
    const tools: ToolContract[] = [
      {
        name: "read_file",
        description: "Read a file",
        parameters: { type: "object" },
        execute: async () => ({ content: "" }),
      },
      {
        name: "run_shell",
        description: "Run a shell command",
        parameters: {
          type: "object",
          properties: {
            command: { type: "string", description: "The command" },
          },
          required: ["command"],
        },
        execute: async () => ({ content: "" }),
      },
    ];

    const result = convertTools(tools);
    expect(result).toHaveLength(2);
    expect(result[0]?.function.name).toBe("read_file");
    expect(result[1]?.function.name).toBe("run_shell");
  });

  it("returns empty array for no tools", () => {
    const result = convertTools([]);
    expect(result).toEqual([]);
  });

  it("preserves nested parameter schema properties", () => {
    const tools: ToolContract[] = [
      {
        name: "search",
        description: "Search code",
        parameters: {
          type: "object",
          properties: {
            query: { type: "string", description: "Search query" },
            options: {
              type: "object",
              properties: {
                limit: { type: "number", description: "Max results", default: 10 },
              },
            },
          },
          required: ["query"],
        },
        execute: async () => ({ content: "" }),
      },
    ];

    const result = convertTools(tools);
    expect(result[0]?.function.parameters.properties?.options).toEqual({
      type: "object",
      properties: {
        limit: { type: "number", description: "Max results", default: 10 },
      },
    });
  });
});