| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { Tool } from "../.."; |
| 3 | import { |
| 4 | TOOL_INSTRUCTIONS_TAG, |
| 5 | addSystemMessageToolsToSystemMessage, |
| 6 | createSystemMessageExampleCall, |
| 7 | generateToolsSystemMessage, |
| 8 | } from "./buildToolsSystemMessage"; |
| 9 | import { closeTag } from "./systemToolUtils"; |
| 10 | |
| 11 | const SHARED_TOOL_FIELDS = { |
| 12 | displayTitle: "DUD", |
| 13 | group: "DUD", |
| 14 | readonly: true, |
| 15 | type: "function" as const, |
| 16 | }; |
| 17 | |
| 18 | describe("createSystemMessageExampleCall", () => { |
| 19 | it("creates a system message example call with no args", () => { |
| 20 | const result = createSystemMessageExampleCall( |
| 21 | "test_tool", |
| 22 | "Use this tool to test things", |
| 23 | ); |
| 24 | expect(result).includes("Use this tool to test things"); |
| 25 | expect(result).includes("```tool"); |
| 26 | expect(result).includes("TOOL_NAME: test_tool"); |
| 27 | expect(result).includes("```"); |
| 28 | }); |
| 29 | |
| 30 | it("creates a system message example call with args", () => { |
| 31 | const result = createSystemMessageExampleCall( |
| 32 | "test_tool", |
| 33 | "Use this tool to test things", |
| 34 | [ |
| 35 | ["arg1", "value1"], |
| 36 | ["arg2", "value2"], |
| 37 | ], |
| 38 | ); |
| 39 | |
| 40 | expect(result).includes("Use this tool to test things"); |
| 41 | expect(result).includes("```tool"); |
| 42 | expect(result).includes("TOOL_NAME: test_tool"); |
| 43 | expect(result).includes("BEGIN_ARG: arg1"); |
| 44 | expect(result).includes("value1"); |
| 45 | expect(result).includes("END_ARG"); |
| 46 | expect(result).includes("BEGIN_ARG: arg2"); |
| 47 | expect(result).includes("value2"); |
| 48 | }); |
| 49 | |
| 50 | it("handles multiline arg values correctly", () => { |
| 51 | const multilineValue = "value\nwith\nmultiple\nlines"; |
| 52 | const result = createSystemMessageExampleCall( |
| 53 | "test_tool", |
| 54 | "Use this tool to test things", |
| 55 | [["arg1", multilineValue]], |
| 56 | ); |
| 57 | |
| 58 | expect(result).includes("BEGIN_ARG: arg1"); |
| 59 | expect(result).includes(multilineValue); |
| 60 | expect(result).includes("END_ARG"); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("generateToolsSystemMessage", () => { |
| 65 | it("returns empty string for empty tools array", () => { |
| 66 | const result = generateToolsSystemMessage([]); |
| 67 | expect(result).toBe(""); |
| 68 | }); |
| 69 | |
| 70 | it("generates system message for tools with system message descriptions", () => { |
| 71 | const customDescription = |
| 72 | "This is test tool 1's system message description"; |
| 73 | const tools: Tool[] = [ |
| 74 | { |
| 75 | function: { |
| 76 | name: "test_tool1", |
| 77 | description: "Test tool 1 description", |
| 78 | parameters: { |
| 79 | type: "object", |
| 80 | properties: {}, |
| 81 | required: [], |
| 82 | }, |
| 83 | }, |
| 84 | systemMessageDescription: customDescription, |
| 85 | ...SHARED_TOOL_FIELDS, |
| 86 | }, |
| 87 | ]; |
| 88 | |
| 89 | const result = generateToolsSystemMessage(tools); |
| 90 | |
| 91 | // Check structure rather than exact text |
| 92 | expect(result).includes(TOOL_INSTRUCTIONS_TAG); |
| 93 | expect(result).includes(customDescription); |
| 94 | expect(result).includes(closeTag(TOOL_INSTRUCTIONS_TAG)); |
| 95 | |
| 96 | // Check for general section about available tools without requiring exact wording |
| 97 | const hasToolsAvailableSection = /tools are available to you/i.test(result); |
| 98 | expect(hasToolsAvailableSection).toBe(true); |
| 99 | }); |
| 100 | |
| 101 | it("generates system message for tools without system message descriptions", () => { |
| 102 | const toolName = "test_tool2"; |
| 103 | const toolDesc = "Test tool 2 description"; |
| 104 | const paramDesc = "Parameter 1 description"; |
| 105 | |
| 106 | const tools: Tool[] = [ |
| 107 | { |
| 108 | function: { |
| 109 | name: toolName, |
| 110 | description: toolDesc, |
| 111 | parameters: { |
| 112 | type: "object", |
| 113 | properties: { |
| 114 | param1: { |
| 115 | type: "string", |
| 116 | description: paramDesc, |
| 117 | }, |
| 118 | }, |
| 119 | required: ["param1"], |
| 120 | }, |
| 121 | }, |
| 122 | ...SHARED_TOOL_FIELDS, |
| 123 | }, |
| 124 | ]; |
| 125 | |
| 126 | const result = generateToolsSystemMessage(tools); |
| 127 | |
| 128 | // Check for key elements without requiring exact wording |
| 129 | expect(result).includes(`TOOL_NAME: ${toolName}`); |
| 130 | expect(result).includes("TOOL_DESCRIPTION:"); |
| 131 | expect(result).includes(toolDesc); |
| 132 | expect(result).includes("TOOL_ARG: param1 (string, required)"); |
| 133 | expect(result).includes(paramDesc); |
| 134 | |
| 135 | // Check for dynamic tool section without exact text matching |
| 136 | const hasDynamicToolsSection = /additional tool definitions/i.test(result); |
| 137 | expect(hasDynamicToolsSection).toBe(true); |
| 138 | }); |
| 139 | |
| 140 | it("handles tools with and without system message descriptions", () => { |
| 141 | const customMsg = "Custom system message"; |
| 142 | const tools: Tool[] = [ |
| 143 | { |
| 144 | function: { |
| 145 | name: "tool_with_description", |
| 146 | description: "Tool description", |
| 147 | parameters: { |
| 148 | type: "object", |
| 149 | properties: {}, |
| 150 | required: [], |
| 151 | }, |
| 152 | }, |
| 153 | systemMessageDescription: customMsg, |
| 154 | ...SHARED_TOOL_FIELDS, |
| 155 | }, |
| 156 | { |
| 157 | function: { |
| 158 | name: "tool_without_description", |
| 159 | description: "Another tool description", |
| 160 | parameters: { |
| 161 | type: "object", |
| 162 | properties: { |
| 163 | param1: { |
| 164 | type: "string", |
| 165 | description: "Parameter description", |
| 166 | }, |
| 167 | }, |
| 168 | required: ["param1"], |
| 169 | }, |
| 170 | }, |
| 171 | ...SHARED_TOOL_FIELDS, |
| 172 | }, |
| 173 | ]; |
| 174 | |
| 175 | const result = generateToolsSystemMessage(tools); |
| 176 | |
| 177 | // Check for both types of tools |
| 178 | expect(result).includes(customMsg); |
| 179 | expect(result).includes("TOOL_NAME: tool_without_description"); |
| 180 | |
| 181 | // Verify structure without exact text matching |
| 182 | const hasAvailableToolsSection = /tools are available to you/i.test(result); |
| 183 | expect(hasAvailableToolsSection).toBe(true); |
| 184 | |
| 185 | const hasDynamicToolsSection = /additional tool definitions/i.test(result); |
| 186 | expect(hasDynamicToolsSection).toBe(true); |
| 187 | }); |
| 188 | |
| 189 | it("includes example tool definition and call", () => { |
| 190 | const tools: Tool[] = [ |
| 191 | { |
| 192 | function: { |
| 193 | name: "test_tool", |
| 194 | description: "Test description", |
| 195 | parameters: { |
| 196 | type: "object", |
| 197 | properties: {}, |
| 198 | required: [], |
| 199 | }, |
| 200 | }, |
| 201 | ...SHARED_TOOL_FIELDS, |
| 202 | }, |
| 203 | ]; |
| 204 | |
| 205 | const result = generateToolsSystemMessage(tools); |
| 206 | |
| 207 | // Check for example sections without exact text |
| 208 | expect(result).includes("```tool_definition"); |
| 209 | expect(result).includes("TOOL_NAME: example_tool"); |
| 210 | expect(result).includes("```tool"); |
| 211 | |
| 212 | // Check for example format structure |
| 213 | const hasExampleDefinition = /example.*tool definition/i.test(result); |
| 214 | const hasExampleCall = /called like this/i.test(result); |
| 215 | |
| 216 | expect(hasExampleDefinition).toBe(true); |
| 217 | expect(hasExampleCall).toBe(true); |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | describe("addSystemMessageToolsToSystemMessage", () => { |
| 222 | it("returns original system message when no tools are provided", () => { |
| 223 | const baseMessage = "This is the base system message"; |
| 224 | const result = addSystemMessageToolsToSystemMessage(baseMessage, []); |
| 225 | expect(result).toBe(baseMessage); |
| 226 | }); |
| 227 | |
| 228 | it("appends tools system message to base message", () => { |
| 229 | const baseMessage = "This is the base system message"; |
| 230 | const toolName = "test_tool"; |
| 231 | const tools: Tool[] = [ |
| 232 | { |
| 233 | function: { |
| 234 | name: toolName, |
| 235 | description: "Test description", |
| 236 | parameters: { |
| 237 | type: "object", |
| 238 | properties: {}, |
| 239 | required: [], |
| 240 | }, |
| 241 | }, |
| 242 | ...SHARED_TOOL_FIELDS, |
| 243 | }, |
| 244 | ]; |
| 245 | |
| 246 | const result = addSystemMessageToolsToSystemMessage(baseMessage, tools); |
| 247 | |
| 248 | expect(result.startsWith(baseMessage)).toBe(true); |
| 249 | expect(result).includes(TOOL_INSTRUCTIONS_TAG); |
| 250 | expect(result).includes(`TOOL_NAME: ${toolName}`); |
| 251 | }); |
| 252 | }); |