digital-tools 2.0.2 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/package.json +3 -4
- package/src/define.js +267 -0
- package/src/entities/advertising.js +999 -0
- package/src/entities/ai.js +756 -0
- package/src/entities/analytics.js +1588 -0
- package/src/entities/automation.js +601 -0
- package/src/entities/communication.js +1150 -0
- package/src/entities/crm.js +1386 -0
- package/src/entities/design.js +546 -0
- package/src/entities/development.js +2212 -0
- package/src/entities/document.js +874 -0
- package/src/entities/ecommerce.js +1429 -0
- package/src/entities/experiment.js +1039 -0
- package/src/entities/finance.js +3478 -0
- package/src/entities/forms.js +1892 -0
- package/src/entities/hr.js +661 -0
- package/src/entities/identity.js +997 -0
- package/src/entities/index.js +282 -0
- package/src/entities/infrastructure.js +1153 -0
- package/src/entities/knowledge.js +1438 -0
- package/src/entities/marketing.js +1610 -0
- package/src/entities/media.js +1634 -0
- package/src/entities/notification.js +1199 -0
- package/src/entities/presentation.js +1274 -0
- package/src/entities/productivity.js +1317 -0
- package/src/entities/project-management.js +1136 -0
- package/src/entities/recruiting.js +736 -0
- package/src/entities/shipping.js +509 -0
- package/src/entities/signature.js +1102 -0
- package/src/entities/site.js +222 -0
- package/src/entities/spreadsheet.js +1341 -0
- package/src/entities/storage.js +1198 -0
- package/src/entities/support.js +1166 -0
- package/src/entities/video-conferencing.js +1750 -0
- package/src/entities/video.js +950 -0
- package/src/entities.js +1663 -0
- package/src/index.js +74 -0
- package/src/providers/analytics/index.js +17 -0
- package/src/providers/analytics/mixpanel.js +255 -0
- package/src/providers/calendar/cal-com.js +303 -0
- package/src/providers/calendar/google-calendar.js +335 -0
- package/src/providers/calendar/index.js +20 -0
- package/src/providers/crm/hubspot.js +566 -0
- package/src/providers/crm/index.js +17 -0
- package/src/providers/development/github.js +472 -0
- package/src/providers/development/index.js +17 -0
- package/src/providers/ecommerce/index.js +17 -0
- package/src/providers/ecommerce/shopify.js +378 -0
- package/src/providers/email/index.js +20 -0
- package/src/providers/email/resend.js +258 -0
- package/src/providers/email/sendgrid.js +161 -0
- package/src/providers/finance/index.js +17 -0
- package/src/providers/finance/stripe.js +549 -0
- package/src/providers/forms/index.js +17 -0
- package/src/providers/forms/typeform.js +500 -0
- package/src/providers/index.js +123 -0
- package/src/providers/knowledge/index.js +17 -0
- package/src/providers/knowledge/notion.js +389 -0
- package/src/providers/marketing/index.js +17 -0
- package/src/providers/marketing/mailchimp.js +443 -0
- package/src/providers/media/cloudinary.js +318 -0
- package/src/providers/media/index.js +17 -0
- package/src/providers/messaging/index.js +20 -0
- package/src/providers/messaging/slack.js +393 -0
- package/src/providers/messaging/twilio-sms.js +249 -0
- package/src/providers/project-management/index.js +17 -0
- package/src/providers/project-management/linear.js +575 -0
- package/src/providers/registry.js +86 -0
- package/src/providers/spreadsheet/google-sheets.js +375 -0
- package/src/providers/spreadsheet/index.js +20 -0
- package/src/providers/spreadsheet/xlsx.js +423 -0
- package/src/providers/storage/index.js +24 -0
- package/src/providers/storage/s3.js +419 -0
- package/src/providers/support/index.js +17 -0
- package/src/providers/support/zendesk.js +373 -0
- package/src/providers/tasks/index.js +17 -0
- package/src/providers/tasks/todoist.js +286 -0
- package/src/providers/types.js +9 -0
- package/src/providers/video-conferencing/google-meet.js +286 -0
- package/src/providers/video-conferencing/index.js +31 -0
- package/src/providers/video-conferencing/jitsi.js +254 -0
- package/src/providers/video-conferencing/teams.js +270 -0
- package/src/providers/video-conferencing/zoom.js +332 -0
- package/src/registry.js +128 -0
- package/src/tools/communication.js +184 -0
- package/src/tools/data.js +205 -0
- package/src/tools/index.js +11 -0
- package/src/tools/web.js +137 -0
- package/src/types.js +10 -0
- package/test/define.test.js +306 -0
- package/test/registry.test.js +357 -0
- package/test/tools.test.js +363 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for Tool Definition functionality
|
|
3
|
+
*
|
|
4
|
+
* Covers defineTool, defineAndRegister, createToolExecutor, and toolBuilder.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
7
|
+
import { defineTool, defineAndRegister, createToolExecutor, toolBuilder, registry, } from '../src/index.js';
|
|
8
|
+
describe('defineTool', () => {
|
|
9
|
+
it('creates a tool with basic config', () => {
|
|
10
|
+
const tool = defineTool({
|
|
11
|
+
id: 'basic.tool',
|
|
12
|
+
name: 'Basic Tool',
|
|
13
|
+
description: 'A basic tool',
|
|
14
|
+
category: 'data',
|
|
15
|
+
input: {
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
value: { type: 'string', description: 'Input value' },
|
|
19
|
+
},
|
|
20
|
+
required: ['value'],
|
|
21
|
+
},
|
|
22
|
+
handler: async (input) => ({ result: input }),
|
|
23
|
+
});
|
|
24
|
+
expect(tool.id).toBe('basic.tool');
|
|
25
|
+
expect(tool.name).toBe('Basic Tool');
|
|
26
|
+
expect(tool.description).toBe('A basic tool');
|
|
27
|
+
expect(tool.category).toBe('data');
|
|
28
|
+
});
|
|
29
|
+
it('converts input schema to parameters', () => {
|
|
30
|
+
const tool = defineTool({
|
|
31
|
+
id: 'param.tool',
|
|
32
|
+
name: 'Param Tool',
|
|
33
|
+
description: 'Tool with parameters',
|
|
34
|
+
category: 'data',
|
|
35
|
+
input: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
name: { type: 'string', description: 'Name input' },
|
|
39
|
+
age: { type: 'number', description: 'Age input' },
|
|
40
|
+
},
|
|
41
|
+
required: ['name'],
|
|
42
|
+
},
|
|
43
|
+
handler: async (input) => input,
|
|
44
|
+
});
|
|
45
|
+
expect(tool.parameters).toHaveLength(2);
|
|
46
|
+
expect(tool.parameters[0].name).toBe('name');
|
|
47
|
+
expect(tool.parameters[0].required).toBe(true);
|
|
48
|
+
expect(tool.parameters[1].name).toBe('age');
|
|
49
|
+
expect(tool.parameters[1].required).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
it('sets subcategory', () => {
|
|
52
|
+
const tool = defineTool({
|
|
53
|
+
id: 'subcat.tool',
|
|
54
|
+
name: 'Subcategory Tool',
|
|
55
|
+
description: 'Tool with subcategory',
|
|
56
|
+
category: 'data',
|
|
57
|
+
subcategory: 'transform',
|
|
58
|
+
input: { type: 'object', properties: {} },
|
|
59
|
+
handler: async () => ({}),
|
|
60
|
+
});
|
|
61
|
+
expect(tool.subcategory).toBe('transform');
|
|
62
|
+
});
|
|
63
|
+
it('sets output schema', () => {
|
|
64
|
+
const tool = defineTool({
|
|
65
|
+
id: 'output.tool',
|
|
66
|
+
name: 'Output Tool',
|
|
67
|
+
description: 'Tool with output',
|
|
68
|
+
category: 'data',
|
|
69
|
+
input: { type: 'object', properties: {} },
|
|
70
|
+
output: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
result: { type: 'string' },
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
handler: async () => ({ result: 'done' }),
|
|
77
|
+
});
|
|
78
|
+
expect(tool.output).toBeDefined();
|
|
79
|
+
expect(tool.output?.schema).toHaveProperty('properties');
|
|
80
|
+
});
|
|
81
|
+
it('applies options', () => {
|
|
82
|
+
const tool = defineTool({
|
|
83
|
+
id: 'options.tool',
|
|
84
|
+
name: 'Options Tool',
|
|
85
|
+
description: 'Tool with options',
|
|
86
|
+
category: 'data',
|
|
87
|
+
input: { type: 'object', properties: {} },
|
|
88
|
+
handler: async () => ({}),
|
|
89
|
+
options: {
|
|
90
|
+
audience: 'agent',
|
|
91
|
+
tags: ['test', 'example'],
|
|
92
|
+
idempotent: true,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
expect(tool.audience).toBe('agent');
|
|
96
|
+
expect(tool.tags).toContain('test');
|
|
97
|
+
expect(tool.idempotent).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
it('handler is callable', async () => {
|
|
100
|
+
const tool = defineTool({
|
|
101
|
+
id: 'callable.tool',
|
|
102
|
+
name: 'Callable Tool',
|
|
103
|
+
description: 'Tool with callable handler',
|
|
104
|
+
category: 'data',
|
|
105
|
+
input: {
|
|
106
|
+
type: 'object',
|
|
107
|
+
properties: { x: { type: 'number' } },
|
|
108
|
+
required: ['x'],
|
|
109
|
+
},
|
|
110
|
+
handler: async (input) => ({ doubled: input.x * 2 }),
|
|
111
|
+
});
|
|
112
|
+
const result = await tool.handler({ x: 5 });
|
|
113
|
+
expect(result).toEqual({ doubled: 10 });
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('defineAndRegister', () => {
|
|
117
|
+
beforeEach(() => {
|
|
118
|
+
registry.clear();
|
|
119
|
+
});
|
|
120
|
+
it('creates and registers a tool', () => {
|
|
121
|
+
const tool = defineAndRegister({
|
|
122
|
+
id: 'register.tool',
|
|
123
|
+
name: 'Register Tool',
|
|
124
|
+
description: 'Auto-registered tool',
|
|
125
|
+
category: 'data',
|
|
126
|
+
input: { type: 'object', properties: {} },
|
|
127
|
+
handler: async () => ({}),
|
|
128
|
+
});
|
|
129
|
+
expect(tool.id).toBe('register.tool');
|
|
130
|
+
expect(registry.has('register.tool')).toBe(true);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe('createToolExecutor', () => {
|
|
134
|
+
beforeEach(() => {
|
|
135
|
+
registry.clear();
|
|
136
|
+
registry.register(defineTool({
|
|
137
|
+
id: 'exec.test',
|
|
138
|
+
name: 'Exec Test',
|
|
139
|
+
description: 'Test execution',
|
|
140
|
+
category: 'data',
|
|
141
|
+
input: {
|
|
142
|
+
type: 'object',
|
|
143
|
+
properties: { value: { type: 'string' } },
|
|
144
|
+
required: ['value'],
|
|
145
|
+
},
|
|
146
|
+
handler: async (input) => ({
|
|
147
|
+
upper: input.value.toUpperCase(),
|
|
148
|
+
}),
|
|
149
|
+
}));
|
|
150
|
+
registry.register(defineTool({
|
|
151
|
+
id: 'exec.agent-only',
|
|
152
|
+
name: 'Agent Only',
|
|
153
|
+
description: 'Agent only tool',
|
|
154
|
+
category: 'data',
|
|
155
|
+
input: { type: 'object', properties: {} },
|
|
156
|
+
handler: async () => ({ result: 'agent' }),
|
|
157
|
+
options: { audience: 'agent' },
|
|
158
|
+
}));
|
|
159
|
+
registry.register(defineTool({
|
|
160
|
+
id: 'exec.error',
|
|
161
|
+
name: 'Error Tool',
|
|
162
|
+
description: 'Tool that throws',
|
|
163
|
+
category: 'data',
|
|
164
|
+
input: { type: 'object', properties: {} },
|
|
165
|
+
handler: async () => {
|
|
166
|
+
throw new Error('Tool error');
|
|
167
|
+
},
|
|
168
|
+
}));
|
|
169
|
+
});
|
|
170
|
+
it('executes a tool with context', async () => {
|
|
171
|
+
const executor = createToolExecutor({
|
|
172
|
+
executor: { type: 'agent', id: 'agent_1', name: 'Test Agent' },
|
|
173
|
+
environment: 'test',
|
|
174
|
+
});
|
|
175
|
+
const result = await executor.execute('exec.test', { value: 'hello' });
|
|
176
|
+
expect(result.success).toBe(true);
|
|
177
|
+
expect(result.data?.upper).toBe('HELLO');
|
|
178
|
+
});
|
|
179
|
+
it('returns error for non-existent tool', async () => {
|
|
180
|
+
const executor = createToolExecutor({
|
|
181
|
+
executor: { type: 'agent', id: 'agent_1', name: 'Test Agent' },
|
|
182
|
+
});
|
|
183
|
+
const result = await executor.execute('non.existent', {});
|
|
184
|
+
expect(result.success).toBe(false);
|
|
185
|
+
expect(result.error?.code).toBe('TOOL_NOT_FOUND');
|
|
186
|
+
});
|
|
187
|
+
it('checks audience restrictions', async () => {
|
|
188
|
+
const executor = createToolExecutor({
|
|
189
|
+
executor: { type: 'human', id: 'user_1', name: 'Test User' },
|
|
190
|
+
});
|
|
191
|
+
const result = await executor.execute('exec.agent-only', {});
|
|
192
|
+
expect(result.success).toBe(false);
|
|
193
|
+
expect(result.error?.code).toBe('ACCESS_DENIED');
|
|
194
|
+
});
|
|
195
|
+
it('handles execution errors', async () => {
|
|
196
|
+
const executor = createToolExecutor({
|
|
197
|
+
executor: { type: 'agent', id: 'agent_1', name: 'Test Agent' },
|
|
198
|
+
});
|
|
199
|
+
const result = await executor.execute('exec.error', {});
|
|
200
|
+
expect(result.success).toBe(false);
|
|
201
|
+
expect(result.error?.code).toBe('EXECUTION_ERROR');
|
|
202
|
+
expect(result.error?.message).toBe('Tool error');
|
|
203
|
+
});
|
|
204
|
+
it('tracks execution metadata', async () => {
|
|
205
|
+
const executor = createToolExecutor({
|
|
206
|
+
executor: { type: 'agent', id: 'agent_1', name: 'Test Agent' },
|
|
207
|
+
requestId: 'req_123',
|
|
208
|
+
});
|
|
209
|
+
const result = await executor.execute('exec.test', { value: 'test' });
|
|
210
|
+
expect(result.metadata?.duration).toBeGreaterThanOrEqual(0);
|
|
211
|
+
expect(result.metadata?.requestId).toBe('req_123');
|
|
212
|
+
});
|
|
213
|
+
it('lists available tools', () => {
|
|
214
|
+
const executor = createToolExecutor({
|
|
215
|
+
executor: { type: 'human', id: 'user_1', name: 'Test User' },
|
|
216
|
+
});
|
|
217
|
+
const available = executor.listAvailable();
|
|
218
|
+
// Should not include agent-only tool
|
|
219
|
+
expect(available.some(t => t.id === 'exec.test')).toBe(true);
|
|
220
|
+
expect(available.some(t => t.id === 'exec.agent-only')).toBe(false);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
describe('toolBuilder', () => {
|
|
224
|
+
beforeEach(() => {
|
|
225
|
+
registry.clear();
|
|
226
|
+
});
|
|
227
|
+
it('builds a tool with fluent API', () => {
|
|
228
|
+
const tool = toolBuilder('builder.test')
|
|
229
|
+
.name('Builder Test')
|
|
230
|
+
.description('Built with builder')
|
|
231
|
+
.category('data')
|
|
232
|
+
.subcategory('transform')
|
|
233
|
+
.input({
|
|
234
|
+
type: 'object',
|
|
235
|
+
properties: {
|
|
236
|
+
value: { type: 'string' },
|
|
237
|
+
},
|
|
238
|
+
required: ['value'],
|
|
239
|
+
})
|
|
240
|
+
.handler(async (input) => ({ result: input.value }))
|
|
241
|
+
.build();
|
|
242
|
+
expect(tool.id).toBe('builder.test');
|
|
243
|
+
expect(tool.name).toBe('Builder Test');
|
|
244
|
+
expect(tool.category).toBe('data');
|
|
245
|
+
expect(tool.subcategory).toBe('transform');
|
|
246
|
+
});
|
|
247
|
+
it('sets output schema', () => {
|
|
248
|
+
const tool = toolBuilder('output.test')
|
|
249
|
+
.name('Output Test')
|
|
250
|
+
.description('With output')
|
|
251
|
+
.category('data')
|
|
252
|
+
.input({ type: 'object', properties: {} })
|
|
253
|
+
.output({ type: 'object', properties: { result: { type: 'string' } } })
|
|
254
|
+
.handler(async () => ({ result: 'done' }))
|
|
255
|
+
.build();
|
|
256
|
+
expect(tool.output).toBeDefined();
|
|
257
|
+
});
|
|
258
|
+
it('sets options', () => {
|
|
259
|
+
const tool = toolBuilder('options.test')
|
|
260
|
+
.name('Options Test')
|
|
261
|
+
.description('With options')
|
|
262
|
+
.category('data')
|
|
263
|
+
.input({ type: 'object', properties: {} })
|
|
264
|
+
.options({
|
|
265
|
+
audience: 'both',
|
|
266
|
+
tags: ['builder'],
|
|
267
|
+
})
|
|
268
|
+
.handler(async () => ({}))
|
|
269
|
+
.build();
|
|
270
|
+
expect(tool.audience).toBe('both');
|
|
271
|
+
expect(tool.tags).toContain('builder');
|
|
272
|
+
});
|
|
273
|
+
it('registers tool with register()', () => {
|
|
274
|
+
const tool = toolBuilder('register.test')
|
|
275
|
+
.name('Register Test')
|
|
276
|
+
.description('Auto register')
|
|
277
|
+
.category('data')
|
|
278
|
+
.input({ type: 'object', properties: {} })
|
|
279
|
+
.handler(async () => ({}))
|
|
280
|
+
.register();
|
|
281
|
+
expect(registry.has('register.test')).toBe(true);
|
|
282
|
+
});
|
|
283
|
+
it('throws if required fields are missing', () => {
|
|
284
|
+
expect(() => {
|
|
285
|
+
toolBuilder('incomplete.test')
|
|
286
|
+
.name('Incomplete')
|
|
287
|
+
// Missing description, category, input, handler
|
|
288
|
+
.build();
|
|
289
|
+
}).toThrow();
|
|
290
|
+
});
|
|
291
|
+
it('handler is callable after build', async () => {
|
|
292
|
+
const tool = toolBuilder('callable.test')
|
|
293
|
+
.name('Callable')
|
|
294
|
+
.description('Callable handler')
|
|
295
|
+
.category('data')
|
|
296
|
+
.input({
|
|
297
|
+
type: 'object',
|
|
298
|
+
properties: { n: { type: 'number' } },
|
|
299
|
+
required: ['n'],
|
|
300
|
+
})
|
|
301
|
+
.handler(async (input) => ({ squared: input.n * input.n }))
|
|
302
|
+
.build();
|
|
303
|
+
const result = await tool.handler({ n: 4 });
|
|
304
|
+
expect(result).toEqual({ squared: 16 });
|
|
305
|
+
});
|
|
306
|
+
});
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for Tool Registry functionality
|
|
3
|
+
*
|
|
4
|
+
* Covers tool registration, querying, and MCP conversion.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
7
|
+
import { registry, createRegistry, registerTool, getTool, executeTool, toMCP, listMCPTools, defineTool, } from '../src/index.js';
|
|
8
|
+
describe('Registry', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
registry.clear();
|
|
11
|
+
});
|
|
12
|
+
describe('register', () => {
|
|
13
|
+
it('registers a tool', () => {
|
|
14
|
+
const tool = defineTool({
|
|
15
|
+
id: 'test.tool',
|
|
16
|
+
name: 'Test Tool',
|
|
17
|
+
description: 'A test tool',
|
|
18
|
+
category: 'data',
|
|
19
|
+
input: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: { value: { type: 'string' } },
|
|
22
|
+
required: ['value'],
|
|
23
|
+
},
|
|
24
|
+
handler: async (input) => ({ result: input }),
|
|
25
|
+
});
|
|
26
|
+
registry.register(tool);
|
|
27
|
+
expect(registry.has('test.tool')).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
it('overwrites existing tool with same id', () => {
|
|
30
|
+
const tool1 = defineTool({
|
|
31
|
+
id: 'test.tool',
|
|
32
|
+
name: 'Tool V1',
|
|
33
|
+
description: 'Version 1',
|
|
34
|
+
category: 'data',
|
|
35
|
+
input: { type: 'object', properties: {} },
|
|
36
|
+
handler: async () => ({ v: 1 }),
|
|
37
|
+
});
|
|
38
|
+
const tool2 = defineTool({
|
|
39
|
+
id: 'test.tool',
|
|
40
|
+
name: 'Tool V2',
|
|
41
|
+
description: 'Version 2',
|
|
42
|
+
category: 'data',
|
|
43
|
+
input: { type: 'object', properties: {} },
|
|
44
|
+
handler: async () => ({ v: 2 }),
|
|
45
|
+
});
|
|
46
|
+
registry.register(tool1);
|
|
47
|
+
registry.register(tool2);
|
|
48
|
+
expect(registry.get('test.tool')?.name).toBe('Tool V2');
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
describe('get', () => {
|
|
52
|
+
it('returns tool by id', () => {
|
|
53
|
+
const tool = defineTool({
|
|
54
|
+
id: 'get.test',
|
|
55
|
+
name: 'Get Test',
|
|
56
|
+
description: 'Test get',
|
|
57
|
+
category: 'data',
|
|
58
|
+
input: { type: 'object', properties: {} },
|
|
59
|
+
handler: async () => ({}),
|
|
60
|
+
});
|
|
61
|
+
registry.register(tool);
|
|
62
|
+
expect(registry.get('get.test')?.name).toBe('Get Test');
|
|
63
|
+
});
|
|
64
|
+
it('returns undefined for non-existent tool', () => {
|
|
65
|
+
expect(registry.get('non.existent')).toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
describe('has', () => {
|
|
69
|
+
it('returns true for registered tool', () => {
|
|
70
|
+
const tool = defineTool({
|
|
71
|
+
id: 'has.test',
|
|
72
|
+
name: 'Has Test',
|
|
73
|
+
description: 'Test has',
|
|
74
|
+
category: 'data',
|
|
75
|
+
input: { type: 'object', properties: {} },
|
|
76
|
+
handler: async () => ({}),
|
|
77
|
+
});
|
|
78
|
+
registry.register(tool);
|
|
79
|
+
expect(registry.has('has.test')).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
it('returns false for non-existent tool', () => {
|
|
82
|
+
expect(registry.has('non.existent')).toBe(false);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe('unregister', () => {
|
|
86
|
+
it('removes a tool from registry', () => {
|
|
87
|
+
const tool = defineTool({
|
|
88
|
+
id: 'unregister.test',
|
|
89
|
+
name: 'Unregister Test',
|
|
90
|
+
description: 'Test unregister',
|
|
91
|
+
category: 'data',
|
|
92
|
+
input: { type: 'object', properties: {} },
|
|
93
|
+
handler: async () => ({}),
|
|
94
|
+
});
|
|
95
|
+
registry.register(tool);
|
|
96
|
+
const result = registry.unregister('unregister.test');
|
|
97
|
+
expect(result).toBe(true);
|
|
98
|
+
expect(registry.has('unregister.test')).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
it('returns false for non-existent tool', () => {
|
|
101
|
+
expect(registry.unregister('non.existent')).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
describe('list', () => {
|
|
105
|
+
it('returns all tool ids', () => {
|
|
106
|
+
registry.register(defineTool({
|
|
107
|
+
id: 'list.a',
|
|
108
|
+
name: 'A',
|
|
109
|
+
description: 'A',
|
|
110
|
+
category: 'data',
|
|
111
|
+
input: { type: 'object', properties: {} },
|
|
112
|
+
handler: async () => ({}),
|
|
113
|
+
}));
|
|
114
|
+
registry.register(defineTool({
|
|
115
|
+
id: 'list.b',
|
|
116
|
+
name: 'B',
|
|
117
|
+
description: 'B',
|
|
118
|
+
category: 'web',
|
|
119
|
+
input: { type: 'object', properties: {} },
|
|
120
|
+
handler: async () => ({}),
|
|
121
|
+
}));
|
|
122
|
+
const ids = registry.list();
|
|
123
|
+
expect(ids).toContain('list.a');
|
|
124
|
+
expect(ids).toContain('list.b');
|
|
125
|
+
});
|
|
126
|
+
it('returns empty array when no tools', () => {
|
|
127
|
+
expect(registry.list()).toHaveLength(0);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe('query', () => {
|
|
131
|
+
beforeEach(() => {
|
|
132
|
+
registry.register(defineTool({
|
|
133
|
+
id: 'query.data.1',
|
|
134
|
+
name: 'Data Tool 1',
|
|
135
|
+
description: 'Data tool',
|
|
136
|
+
category: 'data',
|
|
137
|
+
subcategory: 'transform',
|
|
138
|
+
input: { type: 'object', properties: {} },
|
|
139
|
+
handler: async () => ({}),
|
|
140
|
+
options: { audience: 'agent', tags: ['json', 'parse'] },
|
|
141
|
+
}));
|
|
142
|
+
registry.register(defineTool({
|
|
143
|
+
id: 'query.web.1',
|
|
144
|
+
name: 'Web Tool 1',
|
|
145
|
+
description: 'Web tool',
|
|
146
|
+
category: 'web',
|
|
147
|
+
subcategory: 'fetch',
|
|
148
|
+
input: { type: 'object', properties: {} },
|
|
149
|
+
handler: async () => ({}),
|
|
150
|
+
options: { audience: 'human', tags: ['http'] },
|
|
151
|
+
}));
|
|
152
|
+
registry.register(defineTool({
|
|
153
|
+
id: 'query.data.2',
|
|
154
|
+
name: 'Data Tool 2',
|
|
155
|
+
description: 'Another data tool',
|
|
156
|
+
category: 'data',
|
|
157
|
+
subcategory: 'validate',
|
|
158
|
+
input: { type: 'object', properties: {} },
|
|
159
|
+
handler: async () => ({}),
|
|
160
|
+
options: { audience: 'both', tags: ['validate'] },
|
|
161
|
+
}));
|
|
162
|
+
});
|
|
163
|
+
it('filters by category', () => {
|
|
164
|
+
const results = registry.query({ category: 'data' });
|
|
165
|
+
expect(results).toHaveLength(2);
|
|
166
|
+
expect(results.every(t => t.category === 'data')).toBe(true);
|
|
167
|
+
});
|
|
168
|
+
it('filters by subcategory', () => {
|
|
169
|
+
const results = registry.query({ subcategory: 'transform' });
|
|
170
|
+
expect(results).toHaveLength(1);
|
|
171
|
+
expect(results[0].id).toBe('query.data.1');
|
|
172
|
+
});
|
|
173
|
+
it('filters by tags', () => {
|
|
174
|
+
const results = registry.query({ tags: ['json'] });
|
|
175
|
+
expect(results).toHaveLength(1);
|
|
176
|
+
expect(results[0].id).toBe('query.data.1');
|
|
177
|
+
});
|
|
178
|
+
it('filters by audience', () => {
|
|
179
|
+
const agentResults = registry.query({ audience: 'agent' });
|
|
180
|
+
// agent can use tools with audience: agent, both, or undefined
|
|
181
|
+
expect(agentResults.length).toBeGreaterThanOrEqual(1);
|
|
182
|
+
});
|
|
183
|
+
it('searches by text', () => {
|
|
184
|
+
const results = registry.query({ search: 'another' });
|
|
185
|
+
expect(results).toHaveLength(1);
|
|
186
|
+
expect(results[0].id).toBe('query.data.2');
|
|
187
|
+
});
|
|
188
|
+
it('applies pagination', () => {
|
|
189
|
+
const results = registry.query({ limit: 2 });
|
|
190
|
+
expect(results).toHaveLength(2);
|
|
191
|
+
});
|
|
192
|
+
it('applies offset', () => {
|
|
193
|
+
const all = registry.query({});
|
|
194
|
+
const offset = registry.query({ offset: 1 });
|
|
195
|
+
expect(offset).toHaveLength(all.length - 1);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
describe('byCategory', () => {
|
|
199
|
+
it('returns tools by category', () => {
|
|
200
|
+
registry.register(defineTool({
|
|
201
|
+
id: 'cat.web.1',
|
|
202
|
+
name: 'Web',
|
|
203
|
+
description: 'Web',
|
|
204
|
+
category: 'web',
|
|
205
|
+
input: { type: 'object', properties: {} },
|
|
206
|
+
handler: async () => ({}),
|
|
207
|
+
}));
|
|
208
|
+
registry.register(defineTool({
|
|
209
|
+
id: 'cat.data.1',
|
|
210
|
+
name: 'Data',
|
|
211
|
+
description: 'Data',
|
|
212
|
+
category: 'data',
|
|
213
|
+
input: { type: 'object', properties: {} },
|
|
214
|
+
handler: async () => ({}),
|
|
215
|
+
}));
|
|
216
|
+
const webTools = registry.byCategory('web');
|
|
217
|
+
expect(webTools).toHaveLength(1);
|
|
218
|
+
expect(webTools[0].id).toBe('cat.web.1');
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
describe('clear', () => {
|
|
222
|
+
it('removes all tools', () => {
|
|
223
|
+
registry.register(defineTool({
|
|
224
|
+
id: 'clear.1',
|
|
225
|
+
name: 'Clear',
|
|
226
|
+
description: 'Clear',
|
|
227
|
+
category: 'data',
|
|
228
|
+
input: { type: 'object', properties: {} },
|
|
229
|
+
handler: async () => ({}),
|
|
230
|
+
}));
|
|
231
|
+
registry.clear();
|
|
232
|
+
expect(registry.list()).toHaveLength(0);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
describe('createRegistry', () => {
|
|
237
|
+
it('creates a new isolated registry', () => {
|
|
238
|
+
const reg1 = createRegistry();
|
|
239
|
+
const reg2 = createRegistry();
|
|
240
|
+
reg1.register(defineTool({
|
|
241
|
+
id: 'isolated.1',
|
|
242
|
+
name: 'Isolated',
|
|
243
|
+
description: 'Isolated',
|
|
244
|
+
category: 'data',
|
|
245
|
+
input: { type: 'object', properties: {} },
|
|
246
|
+
handler: async () => ({}),
|
|
247
|
+
}));
|
|
248
|
+
expect(reg1.has('isolated.1')).toBe(true);
|
|
249
|
+
expect(reg2.has('isolated.1')).toBe(false);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
describe('registerTool', () => {
|
|
253
|
+
beforeEach(() => {
|
|
254
|
+
registry.clear();
|
|
255
|
+
});
|
|
256
|
+
it('registers tool in global registry', () => {
|
|
257
|
+
const tool = defineTool({
|
|
258
|
+
id: 'global.test',
|
|
259
|
+
name: 'Global',
|
|
260
|
+
description: 'Global',
|
|
261
|
+
category: 'data',
|
|
262
|
+
input: { type: 'object', properties: {} },
|
|
263
|
+
handler: async () => ({}),
|
|
264
|
+
});
|
|
265
|
+
registerTool(tool);
|
|
266
|
+
expect(registry.has('global.test')).toBe(true);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
describe('getTool', () => {
|
|
270
|
+
beforeEach(() => {
|
|
271
|
+
registry.clear();
|
|
272
|
+
});
|
|
273
|
+
it('gets tool from global registry', () => {
|
|
274
|
+
const tool = defineTool({
|
|
275
|
+
id: 'get.global',
|
|
276
|
+
name: 'Get Global',
|
|
277
|
+
description: 'Get global',
|
|
278
|
+
category: 'data',
|
|
279
|
+
input: { type: 'object', properties: {} },
|
|
280
|
+
handler: async () => ({}),
|
|
281
|
+
});
|
|
282
|
+
registry.register(tool);
|
|
283
|
+
expect(getTool('get.global')?.name).toBe('Get Global');
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
describe('executeTool', () => {
|
|
287
|
+
beforeEach(() => {
|
|
288
|
+
registry.clear();
|
|
289
|
+
});
|
|
290
|
+
it('executes a registered tool', async () => {
|
|
291
|
+
const tool = defineTool({
|
|
292
|
+
id: 'execute.test',
|
|
293
|
+
name: 'Execute',
|
|
294
|
+
description: 'Execute',
|
|
295
|
+
category: 'data',
|
|
296
|
+
input: { type: 'object', properties: { value: { type: 'string' } } },
|
|
297
|
+
handler: async (input) => ({ doubled: input.value + input.value }),
|
|
298
|
+
});
|
|
299
|
+
registry.register(tool);
|
|
300
|
+
const result = await executeTool('execute.test', { value: 'test' });
|
|
301
|
+
expect(result.doubled).toBe('testtest');
|
|
302
|
+
});
|
|
303
|
+
it('throws for non-existent tool', async () => {
|
|
304
|
+
await expect(executeTool('non.existent', {})).rejects.toThrow('not found');
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
describe('toMCP', () => {
|
|
308
|
+
it('converts tool to MCP format', () => {
|
|
309
|
+
const tool = defineTool({
|
|
310
|
+
id: 'mcp.test',
|
|
311
|
+
name: 'MCP Test',
|
|
312
|
+
description: 'Test MCP conversion',
|
|
313
|
+
category: 'data',
|
|
314
|
+
input: {
|
|
315
|
+
type: 'object',
|
|
316
|
+
properties: {
|
|
317
|
+
url: { type: 'string', description: 'URL to fetch' },
|
|
318
|
+
timeout: { type: 'number', description: 'Timeout in ms' },
|
|
319
|
+
},
|
|
320
|
+
required: ['url'],
|
|
321
|
+
},
|
|
322
|
+
handler: async () => ({}),
|
|
323
|
+
});
|
|
324
|
+
const mcp = toMCP(tool);
|
|
325
|
+
expect(mcp.name).toBe('mcp.test');
|
|
326
|
+
expect(mcp.description).toBe('Test MCP conversion');
|
|
327
|
+
expect(mcp.inputSchema.type).toBe('object');
|
|
328
|
+
expect(mcp.inputSchema.properties).toHaveProperty('url');
|
|
329
|
+
expect(mcp.inputSchema.required).toContain('url');
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
describe('listMCPTools', () => {
|
|
333
|
+
beforeEach(() => {
|
|
334
|
+
registry.clear();
|
|
335
|
+
});
|
|
336
|
+
it('lists all tools in MCP format', () => {
|
|
337
|
+
registry.register(defineTool({
|
|
338
|
+
id: 'mcp.list.1',
|
|
339
|
+
name: 'Tool 1',
|
|
340
|
+
description: 'Tool 1',
|
|
341
|
+
category: 'data',
|
|
342
|
+
input: { type: 'object', properties: {} },
|
|
343
|
+
handler: async () => ({}),
|
|
344
|
+
}));
|
|
345
|
+
registry.register(defineTool({
|
|
346
|
+
id: 'mcp.list.2',
|
|
347
|
+
name: 'Tool 2',
|
|
348
|
+
description: 'Tool 2',
|
|
349
|
+
category: 'web',
|
|
350
|
+
input: { type: 'object', properties: {} },
|
|
351
|
+
handler: async () => ({}),
|
|
352
|
+
}));
|
|
353
|
+
const mcpTools = listMCPTools();
|
|
354
|
+
expect(mcpTools).toHaveLength(2);
|
|
355
|
+
expect(mcpTools.every(t => 'inputSchema' in t)).toBe(true);
|
|
356
|
+
});
|
|
357
|
+
});
|