File size: 9,232 Bytes
4e23b01 | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | import type { ToolCall } from '#/message';
import type { JsonValue } from './fixtures/args-validator';
import { SimpleToolset, toolOk } from './fixtures/simple-toolset';
import type { ToolReturnValue } from './fixtures/simple-toolset';
import { describe, expect, it } from 'vitest';
function makeToolCall(id: string, name: string, args: string | null): ToolCall {
return {
type: 'function',
id,
name, arguments: args,
};
}
describe('SimpleToolset', () => {
it('handle() invokes the registered handler and returns ToolResult', async () => {
const toolset = new SimpleToolset();
toolset.add(
{ name: 'plus', description: 'Add two numbers', parameters: {} },
async (args: JsonValue): Promise<ToolReturnValue> => {
const obj = args as Record<string, JsonValue>;
const a = obj['a'] as number;
const b = obj['b'] as number;
return toolOk({ output: String(a + b) });
},
);
const tc = makeToolCall('1', 'plus', '{"a": 1, "b": 2}');
const result = await toolset.handle(tc);
expect(result.toolCallId).toBe('1');
expect(result.returnValue.isError).toBe(false);
expect(result.returnValue.output).toBe('3');
});
it('handle() returns toolNotFoundError for unknown tool', async () => {
const toolset = new SimpleToolset();
const tc = makeToolCall('1', 'not_found', null);
const result = await toolset.handle(tc);
expect(result.toolCallId).toBe('1');
expect(result.returnValue.isError).toBe(true);
expect(result.returnValue.message).toContain('not_found');
});
it('handle() returns toolParseError on invalid JSON', async () => {
const toolset = new SimpleToolset();
toolset.add(
{ name: 'test', description: 'test', parameters: {} },
async (): Promise<ToolReturnValue> => toolOk({ output: 'ok' }),
);
const tc = makeToolCall('1', 'test', '{invalid json}');
const result = await toolset.handle(tc);
expect(result.toolCallId).toBe('1');
expect(result.returnValue.isError).toBe(true);
expect(result.returnValue.message).toContain('Error parsing JSON arguments:');
expect(result.returnValue.display).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'brief', text: 'Invalid arguments' }),
]),
);
});
it('handle() returns toolRuntimeError when handler throws', async () => {
const toolset = new SimpleToolset();
toolset.add(
{ name: 'explode', description: 'Throws', parameters: {} },
async (): Promise<ToolReturnValue> => {
throw new Error('boom');
},
);
const tc = makeToolCall('1', 'explode', '{}');
const result = await toolset.handle(tc);
expect(result.toolCallId).toBe('1');
expect(result.returnValue.isError).toBe(true);
expect(result.returnValue.message).toBe('Error running tool: boom');
expect(result.returnValue.display).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'brief', text: 'Tool runtime error' }),
]),
);
});
it('handle() resolves to toolRuntimeError when the handler throws synchronously', async () => {
const toolset = new SimpleToolset();
toolset.add(
{ name: 'explode_sync', description: 'Throws synchronously', parameters: {} },
(): Promise<ToolReturnValue> => {
throw new Error('boom');
},
);
await expect(toolset.handle(makeToolCall('1', 'explode_sync', '{}'))).resolves.toMatchObject({
toolCallId: '1',
returnValue: expect.objectContaining({
isError: true,
message: expect.stringContaining('boom'),
}),
});
});
it('handle() resolves to toolRuntimeError when the handler returns Promise.reject()', async () => {
const toolset = new SimpleToolset();
toolset.add(
{ name: 'explode_reject', description: 'Rejects', parameters: {} },
async (): Promise<ToolReturnValue> => {
throw new Error('boom reject');
},
);
await expect(toolset.handle(makeToolCall('1', 'explode_reject', '{}'))).resolves.toMatchObject({
toolCallId: '1',
returnValue: expect.objectContaining({
isError: true,
message: expect.stringContaining('boom reject'),
}),
});
});
it('handle() returns toolValidateError when arguments do not satisfy parameters schema', async () => {
const toolset = new SimpleToolset();
let handlerCalled = false;
toolset.add(
{
name: 'needs_x',
description: 'Requires x',
parameters: {
type: 'object',
properties: {
x: { type: 'string' },
},
required: ['x'],
},
},
async (): Promise<ToolReturnValue> => {
handlerCalled = true;
return toolOk({ output: 'ok' });
},
);
const result = await toolset.handle(makeToolCall('1', 'needs_x', '{}'));
expect(result.toolCallId).toBe('1');
expect(result.returnValue.isError).toBe(true);
expect(result.returnValue.message).toContain('Error validating JSON arguments:');
expect(result.returnValue.message).toContain('x');
expect(result.returnValue.display).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'brief', text: 'Invalid arguments' }),
]),
);
expect(handlerCalled).toBe(false);
});
it('add() and remove() manage tools', () => {
const toolset = new SimpleToolset();
expect(toolset.tools).toHaveLength(0);
toolset.add(
{ name: 'a', description: 'tool a', parameters: {} },
async (): Promise<ToolReturnValue> => toolOk({ output: '' }),
);
toolset.add(
{ name: 'b', description: 'tool b', parameters: {} },
async (): Promise<ToolReturnValue> => toolOk({ output: '' }),
);
expect(toolset.tools).toHaveLength(2);
expect(toolset.tools.map((t) => t.name)).toEqual(['a', 'b']);
toolset.remove('a');
expect(toolset.tools).toHaveLength(1);
expect(toolset.tools[0]!.name).toBe('b');
});
it('add() overwrites existing tool with same name', async () => {
const toolset = new SimpleToolset();
toolset.add(
{ name: 'tool', description: 'v1', parameters: {} },
async (): Promise<ToolReturnValue> => toolOk({ output: 'v1' }),
);
toolset.add(
{ name: 'tool', description: 'v2', parameters: {} },
async (): Promise<ToolReturnValue> => toolOk({ output: 'v2' }),
);
expect(toolset.tools).toHaveLength(1);
expect(toolset.tools[0]!.description).toBe('v2');
const tc = makeToolCall('1', 'tool', '{}');
const result = await toolset.handle(tc);
expect(result.returnValue.output).toBe('v2');
});
it('handle() uses empty object when arguments is null', async () => {
const toolset = new SimpleToolset();
let receivedArgs: JsonValue = null;
toolset.add(
{ name: 'tool', description: 'test', parameters: {} },
async (args: JsonValue): Promise<ToolReturnValue> => {
receivedArgs = args;
return toolOk({ output: 'ok' });
},
);
const tc = makeToolCall('1', 'tool', null);
await toolset.handle(tc);
expect(receivedArgs).toEqual({});
});
it('remove() throws for non-existent tool', () => {
const toolset = new SimpleToolset();
expect(() => {
toolset.remove('does-not-exist');
}).toThrow('Tool `does-not-exist` not found in the toolset.');
});
it('validation error message uses "must NOT have additional property" for additionalProperties: false', async () => {
const toolset = new SimpleToolset();
toolset.add(
{
name: 'strict_tool',
description: 'rejects unknown keys',
parameters: {
type: 'object',
properties: {
x: { type: 'string' },
},
required: ['x'],
additionalProperties: false,
},
},
async (): Promise<ToolReturnValue> => toolOk({ output: 'ok' }),
);
const result = await toolset.handle(makeToolCall('1', 'strict_tool', '{"x":"a","extra":1}'));
expect(result.returnValue.isError).toBe(true);
expect(result.returnValue.message).toContain("must NOT have additional property 'extra'");
});
it('validation error message includes instancePath for nested field errors', async () => {
const toolset = new SimpleToolset();
toolset.add(
{
name: 'nested_tool',
description: 'expects nested object',
parameters: {
type: 'object',
properties: {
user: {
type: 'object',
properties: {
age: { type: 'integer' },
},
required: ['age'],
},
},
required: ['user'],
},
},
async (): Promise<ToolReturnValue> => toolOk({ output: 'ok' }),
);
// Pass age as string, triggering a type error on the nested path.
const result = await toolset.handle(
makeToolCall('1', 'nested_tool', '{"user":{"age":"not a number"}}'),
);
expect(result.returnValue.isError).toBe(true);
// Error message should mention the instance path `/user/age`.
expect(result.returnValue.message).toContain('/user/age');
});
});
|