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