digital-tools 2.1.1 → 2.3.0
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/README.md +2 -0
- package/dist/client.d.ts +109 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +69 -0
- package/dist/client.js.map +1 -0
- package/dist/define.d.ts +2 -2
- package/dist/define.d.ts.map +1 -1
- package/dist/define.js +22 -20
- package/dist/define.js.map +1 -1
- package/dist/function-ref.d.ts +229 -0
- package/dist/function-ref.d.ts.map +1 -0
- package/dist/function-ref.js +28 -0
- package/dist/function-ref.js.map +1 -0
- package/dist/function-sugar.d.ts +57 -0
- package/dist/function-sugar.d.ts.map +1 -0
- package/dist/function-sugar.js +79 -0
- package/dist/function-sugar.js.map +1 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24 -4
- package/dist/index.js.map +1 -1
- package/dist/providers/analytics/mixpanel.d.ts.map +1 -1
- package/dist/providers/analytics/mixpanel.js +21 -18
- package/dist/providers/analytics/mixpanel.js.map +1 -1
- package/dist/providers/calendar/cal-com.d.ts.map +1 -1
- package/dist/providers/calendar/cal-com.js +10 -10
- package/dist/providers/calendar/cal-com.js.map +1 -1
- package/dist/providers/calendar/google-calendar.d.ts.map +1 -1
- package/dist/providers/calendar/google-calendar.js +4 -4
- package/dist/providers/calendar/google-calendar.js.map +1 -1
- package/dist/providers/crm/hubspot.d.ts.map +1 -1
- package/dist/providers/crm/hubspot.js +107 -85
- package/dist/providers/crm/hubspot.js.map +1 -1
- package/dist/providers/development/github.d.ts.map +1 -1
- package/dist/providers/development/github.js +40 -43
- package/dist/providers/development/github.js.map +1 -1
- package/dist/providers/ecommerce/shopify.d.ts.map +1 -1
- package/dist/providers/ecommerce/shopify.js +79 -62
- package/dist/providers/ecommerce/shopify.js.map +1 -1
- package/dist/providers/email/resend.d.ts.map +1 -1
- package/dist/providers/email/resend.js +20 -16
- package/dist/providers/email/resend.js.map +1 -1
- package/dist/providers/email/sendgrid.d.ts.map +1 -1
- package/dist/providers/email/sendgrid.js +12 -9
- package/dist/providers/email/sendgrid.js.map +1 -1
- package/dist/providers/finance/stripe.d.ts.map +1 -1
- package/dist/providers/finance/stripe.js +44 -42
- package/dist/providers/finance/stripe.js.map +1 -1
- package/dist/providers/forms/typeform.d.ts.map +1 -1
- package/dist/providers/forms/typeform.js +68 -58
- package/dist/providers/forms/typeform.js.map +1 -1
- package/dist/providers/knowledge/notion.d.ts.map +1 -1
- package/dist/providers/knowledge/notion.js +75 -41
- package/dist/providers/knowledge/notion.js.map +1 -1
- package/dist/providers/marketing/mailchimp.d.ts.map +1 -1
- package/dist/providers/marketing/mailchimp.js +74 -61
- package/dist/providers/marketing/mailchimp.js.map +1 -1
- package/dist/providers/media/cloudinary.d.ts.map +1 -1
- package/dist/providers/media/cloudinary.js +30 -28
- package/dist/providers/media/cloudinary.js.map +1 -1
- package/dist/providers/messaging/slack.d.ts.map +1 -1
- package/dist/providers/messaging/slack.js +75 -58
- package/dist/providers/messaging/slack.js.map +1 -1
- package/dist/providers/messaging/twilio-sms.d.ts.map +1 -1
- package/dist/providers/messaging/twilio-sms.js +33 -15
- package/dist/providers/messaging/twilio-sms.js.map +1 -1
- package/dist/providers/project-management/linear.d.ts.map +1 -1
- package/dist/providers/project-management/linear.js +31 -27
- package/dist/providers/project-management/linear.js.map +1 -1
- package/dist/providers/spreadsheet/google-sheets.d.ts.map +1 -1
- package/dist/providers/spreadsheet/google-sheets.js +21 -18
- package/dist/providers/spreadsheet/google-sheets.js.map +1 -1
- package/dist/providers/spreadsheet/xlsx.d.ts.map +1 -1
- package/dist/providers/spreadsheet/xlsx.js +4 -4
- package/dist/providers/spreadsheet/xlsx.js.map +1 -1
- package/dist/providers/storage/index.js +1 -0
- package/dist/providers/storage/index.js.map +1 -1
- package/dist/providers/storage/s3.d.ts.map +1 -1
- package/dist/providers/storage/s3.js +36 -27
- package/dist/providers/storage/s3.js.map +1 -1
- package/dist/providers/support/zendesk.d.ts.map +1 -1
- package/dist/providers/support/zendesk.js +24 -25
- package/dist/providers/support/zendesk.js.map +1 -1
- package/dist/providers/tasks/todoist.d.ts.map +1 -1
- package/dist/providers/tasks/todoist.js +18 -18
- package/dist/providers/tasks/todoist.js.map +1 -1
- package/dist/providers/video-conferencing/google-meet.d.ts.map +1 -1
- package/dist/providers/video-conferencing/google-meet.js +11 -11
- package/dist/providers/video-conferencing/google-meet.js.map +1 -1
- package/dist/providers/video-conferencing/jitsi.js +14 -14
- package/dist/providers/video-conferencing/jitsi.js.map +1 -1
- package/dist/providers/video-conferencing/teams.d.ts.map +1 -1
- package/dist/providers/video-conferencing/teams.js +9 -7
- package/dist/providers/video-conferencing/teams.js.map +1 -1
- package/dist/providers/video-conferencing/zoom.d.ts.map +1 -1
- package/dist/providers/video-conferencing/zoom.js +26 -24
- package/dist/providers/video-conferencing/zoom.js.map +1 -1
- package/dist/tools/data.d.ts.map +1 -1
- package/dist/tools/data.js +5 -12
- package/dist/tools/data.js.map +1 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +1 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/system.d.ts +289 -0
- package/dist/tools/system.d.ts.map +1 -0
- package/dist/tools/system.js +752 -0
- package/dist/tools/system.js.map +1 -0
- package/dist/tools/web.d.ts.map +1 -1
- package/dist/tools/web.js +22 -10
- package/dist/tools/web.js.map +1 -1
- package/dist/track-record.d.ts +101 -0
- package/dist/track-record.d.ts.map +1 -0
- package/dist/track-record.js +17 -0
- package/dist/track-record.js.map +1 -0
- package/dist/types.d.ts +210 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/verb-registration.d.ts +122 -0
- package/dist/verb-registration.d.ts.map +1 -0
- package/dist/verb-registration.js +176 -0
- package/dist/verb-registration.js.map +1 -0
- package/dist/worker.d.ts +93 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +315 -0
- package/dist/worker.js.map +1 -0
- package/dist/wrap.d.ts +89 -0
- package/dist/wrap.d.ts.map +1 -0
- package/dist/wrap.js +225 -0
- package/dist/wrap.js.map +1 -0
- package/package.json +21 -4
- package/src/client.ts +136 -0
- package/src/define.ts +31 -37
- package/src/function-ref.ts +264 -0
- package/src/function-sugar.ts +134 -0
- package/src/index.ts +132 -10
- package/src/providers/analytics/mixpanel.ts +19 -18
- package/src/providers/calendar/cal-com.ts +29 -18
- package/src/providers/calendar/google-calendar.ts +20 -14
- package/src/providers/crm/hubspot.ts +225 -99
- package/src/providers/development/github.ts +206 -135
- package/src/providers/ecommerce/shopify.ts +250 -89
- package/src/providers/email/resend.ts +101 -28
- package/src/providers/email/sendgrid.ts +12 -9
- package/src/providers/finance/stripe.ts +128 -49
- package/src/providers/forms/typeform.ts +74 -58
- package/src/providers/knowledge/notion.ts +340 -88
- package/src/providers/marketing/mailchimp.ts +86 -70
- package/src/providers/media/cloudinary.ts +99 -41
- package/src/providers/messaging/slack.ts +283 -85
- package/src/providers/messaging/twilio-sms.ts +35 -15
- package/src/providers/project-management/linear.ts +143 -55
- package/src/providers/spreadsheet/google-sheets.ts +222 -56
- package/src/providers/spreadsheet/xlsx.ts +47 -16
- package/src/providers/storage/s3.ts +119 -47
- package/src/providers/support/zendesk.ts +196 -46
- package/src/providers/tasks/todoist.ts +20 -26
- package/src/providers/video-conferencing/google-meet.ts +17 -20
- package/src/providers/video-conferencing/jitsi.ts +14 -14
- package/src/providers/video-conferencing/teams.ts +14 -13
- package/src/providers/video-conferencing/zoom.ts +54 -49
- package/src/tools/data.ts +6 -16
- package/src/tools/index.ts +1 -0
- package/src/tools/system.ts +887 -0
- package/src/tools/web.ts +22 -10
- package/src/track-record.ts +106 -0
- package/src/types.ts +241 -13
- package/src/verb-registration.ts +197 -0
- package/src/worker.ts +370 -0
- package/src/wrap.ts +260 -0
- package/test/client.test.ts +146 -0
- package/test/communication-tools-extended.test.ts +734 -0
- package/test/data-tools-extended.test.ts +743 -0
- package/test/define-extended.test.ts +819 -0
- package/test/define.test.ts +150 -41
- package/test/entities.test.ts +623 -0
- package/test/extended-entities.test.ts +1228 -0
- package/test/provider-implementations.test.ts +725 -0
- package/test/provider-registry-extended.test.ts +583 -0
- package/test/providers/google-sheets.test.ts +851 -0
- package/test/providers/helpers.ts +554 -0
- package/test/providers/hubspot.test.ts +576 -0
- package/test/providers/slack.test.ts +932 -0
- package/test/providers/stripe.test.ts +701 -0
- package/test/providers.test.ts +578 -0
- package/test/system-tools-extended.test.ts +632 -0
- package/test/system.test.ts +673 -0
- package/test/tools.test.ts +15 -11
- package/test/types.test.ts +402 -0
- package/test/verb-registration.test.ts +395 -0
- package/test/web-tools.test.ts +553 -0
- package/test/worker-extended.test.ts +699 -0
- package/test/worker.test.ts +576 -0
- package/test/wrap.test.ts +366 -0
- package/tsconfig.json +3 -13
- package/vitest.config.ts +37 -0
- package/wrangler.jsonc +9 -0
- package/.turbo/turbo-build.log +0 -5
- package/dist/providers/voice/vapi.d.ts +0 -27
- package/dist/providers/voice/vapi.d.ts.map +0 -1
- package/dist/providers/voice/vapi.js +0 -440
- package/dist/providers/voice/vapi.js.map +0 -1
- package/src/define.js +0 -267
- package/src/entities/advertising.js +0 -999
- package/src/entities/ai.js +0 -756
- package/src/entities/analytics.js +0 -1588
- package/src/entities/automation.js +0 -601
- package/src/entities/communication.js +0 -1150
- package/src/entities/crm.js +0 -1386
- package/src/entities/design.js +0 -546
- package/src/entities/development.js +0 -2212
- package/src/entities/document.js +0 -874
- package/src/entities/ecommerce.js +0 -1429
- package/src/entities/experiment.js +0 -1039
- package/src/entities/finance.js +0 -3478
- package/src/entities/forms.js +0 -1892
- package/src/entities/hr.js +0 -661
- package/src/entities/identity.js +0 -997
- package/src/entities/index.js +0 -282
- package/src/entities/infrastructure.js +0 -1153
- package/src/entities/knowledge.js +0 -1438
- package/src/entities/marketing.js +0 -1610
- package/src/entities/media.js +0 -1634
- package/src/entities/notification.js +0 -1199
- package/src/entities/presentation.js +0 -1274
- package/src/entities/productivity.js +0 -1317
- package/src/entities/project-management.js +0 -1136
- package/src/entities/recruiting.js +0 -736
- package/src/entities/shipping.js +0 -509
- package/src/entities/signature.js +0 -1102
- package/src/entities/site.js +0 -222
- package/src/entities/spreadsheet.js +0 -1341
- package/src/entities/storage.js +0 -1198
- package/src/entities/support.js +0 -1166
- package/src/entities/video-conferencing.js +0 -1750
- package/src/entities/video.js +0 -950
- package/src/entities.js +0 -1663
- package/src/index.js +0 -74
- package/src/providers/analytics/index.js +0 -17
- package/src/providers/analytics/mixpanel.js +0 -255
- package/src/providers/calendar/cal-com.js +0 -303
- package/src/providers/calendar/google-calendar.js +0 -335
- package/src/providers/calendar/index.js +0 -20
- package/src/providers/crm/hubspot.js +0 -566
- package/src/providers/crm/index.js +0 -17
- package/src/providers/development/github.js +0 -472
- package/src/providers/development/index.js +0 -17
- package/src/providers/ecommerce/index.js +0 -17
- package/src/providers/ecommerce/shopify.js +0 -378
- package/src/providers/email/index.js +0 -20
- package/src/providers/email/resend.js +0 -258
- package/src/providers/email/sendgrid.js +0 -161
- package/src/providers/finance/index.js +0 -17
- package/src/providers/finance/stripe.js +0 -549
- package/src/providers/forms/index.js +0 -17
- package/src/providers/forms/typeform.js +0 -500
- package/src/providers/index.js +0 -123
- package/src/providers/knowledge/index.js +0 -17
- package/src/providers/knowledge/notion.js +0 -389
- package/src/providers/marketing/index.js +0 -17
- package/src/providers/marketing/mailchimp.js +0 -443
- package/src/providers/media/cloudinary.js +0 -318
- package/src/providers/media/index.js +0 -17
- package/src/providers/messaging/index.js +0 -20
- package/src/providers/messaging/slack.js +0 -393
- package/src/providers/messaging/twilio-sms.js +0 -249
- package/src/providers/project-management/index.js +0 -17
- package/src/providers/project-management/linear.js +0 -575
- package/src/providers/registry.js +0 -86
- package/src/providers/spreadsheet/google-sheets.js +0 -375
- package/src/providers/spreadsheet/index.js +0 -20
- package/src/providers/spreadsheet/xlsx.js +0 -423
- package/src/providers/storage/index.js +0 -24
- package/src/providers/storage/s3.js +0 -419
- package/src/providers/support/index.js +0 -17
- package/src/providers/support/zendesk.js +0 -373
- package/src/providers/tasks/index.js +0 -17
- package/src/providers/tasks/todoist.js +0 -286
- package/src/providers/types.js +0 -9
- package/src/providers/video-conferencing/google-meet.js +0 -286
- package/src/providers/video-conferencing/index.js +0 -31
- package/src/providers/video-conferencing/jitsi.js +0 -254
- package/src/providers/video-conferencing/teams.js +0 -270
- package/src/providers/video-conferencing/zoom.js +0 -332
- package/src/registry.js +0 -128
- package/src/tools/communication.js +0 -184
- package/src/tools/data.js +0 -205
- package/src/tools/index.js +0 -11
- package/src/tools/web.js +0 -137
- package/src/types.js +0 -10
- package/test/define.test.js +0 -306
- package/test/registry.test.js +0 -357
- package/test/tools.test.js +0 -363
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extended Tests for Tool Definition Utilities
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive tests for defineTool, defineAndRegister, createToolExecutor, and toolBuilder.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, beforeEach } from 'vitest'
|
|
8
|
+
import {
|
|
9
|
+
defineTool,
|
|
10
|
+
defineAndRegister,
|
|
11
|
+
createToolExecutor,
|
|
12
|
+
toolBuilder,
|
|
13
|
+
registry,
|
|
14
|
+
} from '../src/index.js'
|
|
15
|
+
|
|
16
|
+
describe('defineTool - Extended Cases', () => {
|
|
17
|
+
describe('input schema handling', () => {
|
|
18
|
+
it('handles non-object schema', () => {
|
|
19
|
+
const tool = defineTool({
|
|
20
|
+
id: 'single.param',
|
|
21
|
+
name: 'Single Param',
|
|
22
|
+
description: 'Tool with single non-object input',
|
|
23
|
+
category: 'data',
|
|
24
|
+
input: { type: 'string', description: 'A string input' },
|
|
25
|
+
handler: async (input) => input,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
// Should wrap in 'input' parameter
|
|
29
|
+
expect(tool.parameters).toHaveLength(1)
|
|
30
|
+
expect(tool.parameters[0].name).toBe('input')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('handles schema without properties', () => {
|
|
34
|
+
const tool = defineTool({
|
|
35
|
+
id: 'no.props',
|
|
36
|
+
name: 'No Props',
|
|
37
|
+
description: 'Schema without properties',
|
|
38
|
+
category: 'data',
|
|
39
|
+
input: { type: 'object' },
|
|
40
|
+
handler: async () => ({}),
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// Should create 'input' parameter
|
|
44
|
+
expect(tool.parameters).toHaveLength(1)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('handles empty properties object', () => {
|
|
48
|
+
const tool = defineTool({
|
|
49
|
+
id: 'empty.props',
|
|
50
|
+
name: 'Empty Props',
|
|
51
|
+
description: 'Empty properties',
|
|
52
|
+
category: 'data',
|
|
53
|
+
input: { type: 'object', properties: {} },
|
|
54
|
+
handler: async () => ({}),
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
expect(tool.parameters).toHaveLength(0)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('extracts default values from schema', () => {
|
|
61
|
+
const tool = defineTool({
|
|
62
|
+
id: 'defaults.test',
|
|
63
|
+
name: 'Defaults Test',
|
|
64
|
+
description: 'Test default values',
|
|
65
|
+
category: 'data',
|
|
66
|
+
input: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
properties: {
|
|
69
|
+
name: { type: 'string', default: 'Anonymous' },
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
handler: async (input) => input,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
expect(tool.parameters[0].default).toBe('Anonymous')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('handles required array in schema', () => {
|
|
79
|
+
const tool = defineTool({
|
|
80
|
+
id: 'required.test',
|
|
81
|
+
name: 'Required Test',
|
|
82
|
+
description: 'Test required fields',
|
|
83
|
+
category: 'data',
|
|
84
|
+
input: {
|
|
85
|
+
type: 'object',
|
|
86
|
+
properties: {
|
|
87
|
+
requiredField: { type: 'string' },
|
|
88
|
+
optionalField: { type: 'string' },
|
|
89
|
+
},
|
|
90
|
+
required: ['requiredField'],
|
|
91
|
+
},
|
|
92
|
+
handler: async (input) => input,
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const required = tool.parameters.find((p) => p.name === 'requiredField')
|
|
96
|
+
const optional = tool.parameters.find((p) => p.name === 'optionalField')
|
|
97
|
+
|
|
98
|
+
expect(required?.required).toBe(true)
|
|
99
|
+
expect(optional?.required).toBe(false)
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
describe('output schema', () => {
|
|
104
|
+
it('sets output with description', () => {
|
|
105
|
+
const tool = defineTool({
|
|
106
|
+
id: 'output.test',
|
|
107
|
+
name: 'Output Test',
|
|
108
|
+
description: 'Test output schema',
|
|
109
|
+
category: 'data',
|
|
110
|
+
input: { type: 'object', properties: {} },
|
|
111
|
+
output: {
|
|
112
|
+
type: 'object',
|
|
113
|
+
properties: {
|
|
114
|
+
result: { type: 'string' },
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
handler: async () => ({ result: 'done' }),
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
expect(tool.output).toBeDefined()
|
|
121
|
+
expect(tool.output?.description).toBe('Tool output')
|
|
122
|
+
expect(tool.output?.schema).toHaveProperty('properties')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('handles tool without output schema', () => {
|
|
126
|
+
const tool = defineTool({
|
|
127
|
+
id: 'no.output',
|
|
128
|
+
name: 'No Output',
|
|
129
|
+
description: 'No output schema',
|
|
130
|
+
category: 'data',
|
|
131
|
+
input: { type: 'object', properties: {} },
|
|
132
|
+
handler: async () => ({}),
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
expect(tool.output).toBeUndefined()
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
describe('optional options', () => {
|
|
140
|
+
it('sets version', () => {
|
|
141
|
+
const tool = defineTool({
|
|
142
|
+
id: 'version.test',
|
|
143
|
+
name: 'Version Test',
|
|
144
|
+
description: 'Test version',
|
|
145
|
+
category: 'data',
|
|
146
|
+
input: { type: 'object', properties: {} },
|
|
147
|
+
handler: async () => ({}),
|
|
148
|
+
options: { version: '1.2.3' },
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
expect(tool.version).toBe('1.2.3')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('sets author', () => {
|
|
155
|
+
const tool = defineTool({
|
|
156
|
+
id: 'author.test',
|
|
157
|
+
name: 'Author Test',
|
|
158
|
+
description: 'Test author',
|
|
159
|
+
category: 'data',
|
|
160
|
+
input: { type: 'object', properties: {} },
|
|
161
|
+
handler: async () => ({}),
|
|
162
|
+
options: { author: 'Test Author' },
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
expect(tool.author).toBe('Test Author')
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it('sets docsUrl', () => {
|
|
169
|
+
const tool = defineTool({
|
|
170
|
+
id: 'docs.test',
|
|
171
|
+
name: 'Docs Test',
|
|
172
|
+
description: 'Test docs URL',
|
|
173
|
+
category: 'data',
|
|
174
|
+
input: { type: 'object', properties: {} },
|
|
175
|
+
handler: async () => ({}),
|
|
176
|
+
options: { docsUrl: 'https://docs.example.com' },
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
expect(tool.docsUrl).toBe('https://docs.example.com')
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('sets requiresConfirmation', () => {
|
|
183
|
+
const tool = defineTool({
|
|
184
|
+
id: 'confirm.test',
|
|
185
|
+
name: 'Confirm Test',
|
|
186
|
+
description: 'Test confirmation',
|
|
187
|
+
category: 'communication',
|
|
188
|
+
input: { type: 'object', properties: {} },
|
|
189
|
+
handler: async () => ({}),
|
|
190
|
+
options: { requiresConfirmation: true },
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
expect(tool.requiresConfirmation).toBe(true)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
it('sets idempotent', () => {
|
|
197
|
+
const tool = defineTool({
|
|
198
|
+
id: 'idempotent.test',
|
|
199
|
+
name: 'Idempotent Test',
|
|
200
|
+
description: 'Test idempotency',
|
|
201
|
+
category: 'data',
|
|
202
|
+
input: { type: 'object', properties: {} },
|
|
203
|
+
handler: async () => ({}),
|
|
204
|
+
options: { idempotent: true },
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
expect(tool.idempotent).toBe(true)
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('sets estimatedDuration', () => {
|
|
211
|
+
const tool = defineTool({
|
|
212
|
+
id: 'duration.test',
|
|
213
|
+
name: 'Duration Test',
|
|
214
|
+
description: 'Test duration',
|
|
215
|
+
category: 'web',
|
|
216
|
+
input: { type: 'object', properties: {} },
|
|
217
|
+
handler: async () => ({}),
|
|
218
|
+
options: { estimatedDuration: 5000 },
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
expect(tool.estimatedDuration).toBe(5000)
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
it('sets costPerExecution', () => {
|
|
225
|
+
const tool = defineTool({
|
|
226
|
+
id: 'cost.test',
|
|
227
|
+
name: 'Cost Test',
|
|
228
|
+
description: 'Test cost',
|
|
229
|
+
category: 'data',
|
|
230
|
+
input: { type: 'object', properties: {} },
|
|
231
|
+
handler: async () => ({}),
|
|
232
|
+
options: { costPerExecution: 0.01 },
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
expect(tool.costPerExecution).toBe(0.01)
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it('sets rateLimit', () => {
|
|
239
|
+
const tool = defineTool({
|
|
240
|
+
id: 'rate.test',
|
|
241
|
+
name: 'Rate Test',
|
|
242
|
+
description: 'Test rate limiting',
|
|
243
|
+
category: 'web',
|
|
244
|
+
input: { type: 'object', properties: {} },
|
|
245
|
+
handler: async () => ({}),
|
|
246
|
+
options: {
|
|
247
|
+
rateLimit: {
|
|
248
|
+
maxCalls: 100,
|
|
249
|
+
periodSeconds: 60,
|
|
250
|
+
scope: 'user',
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
expect(tool.rateLimit?.maxCalls).toBe(100)
|
|
256
|
+
expect(tool.rateLimit?.periodSeconds).toBe(60)
|
|
257
|
+
expect(tool.rateLimit?.scope).toBe('user')
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
it('sets securityLevel', () => {
|
|
261
|
+
const tool = defineTool({
|
|
262
|
+
id: 'security.test',
|
|
263
|
+
name: 'Security Test',
|
|
264
|
+
description: 'Test security level',
|
|
265
|
+
category: 'security',
|
|
266
|
+
input: { type: 'object', properties: {} },
|
|
267
|
+
handler: async () => ({}),
|
|
268
|
+
options: { securityLevel: 'restricted' },
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
expect(tool.securityLevel).toBe('restricted')
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('sets permissions array', () => {
|
|
275
|
+
const tool = defineTool({
|
|
276
|
+
id: 'perms.test',
|
|
277
|
+
name: 'Permissions Test',
|
|
278
|
+
description: 'Test permissions',
|
|
279
|
+
category: 'system',
|
|
280
|
+
input: { type: 'object', properties: {} },
|
|
281
|
+
handler: async () => ({}),
|
|
282
|
+
options: {
|
|
283
|
+
permissions: [
|
|
284
|
+
{ type: 'read', resource: 'filesystem' },
|
|
285
|
+
{ type: 'write', resource: 'filesystem' },
|
|
286
|
+
],
|
|
287
|
+
},
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
expect(tool.permissions).toHaveLength(2)
|
|
291
|
+
expect(tool.permissions?.[0]?.type).toBe('read')
|
|
292
|
+
expect(tool.permissions?.[1]?.type).toBe('write')
|
|
293
|
+
})
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
describe('all categories', () => {
|
|
297
|
+
const categories = [
|
|
298
|
+
'communication',
|
|
299
|
+
'data',
|
|
300
|
+
'development',
|
|
301
|
+
'documents',
|
|
302
|
+
'finance',
|
|
303
|
+
'integration',
|
|
304
|
+
'knowledge',
|
|
305
|
+
'media',
|
|
306
|
+
'productivity',
|
|
307
|
+
'security',
|
|
308
|
+
'system',
|
|
309
|
+
'web',
|
|
310
|
+
] as const
|
|
311
|
+
|
|
312
|
+
for (const category of categories) {
|
|
313
|
+
it(`supports ${category} category`, () => {
|
|
314
|
+
const tool = defineTool({
|
|
315
|
+
id: `${category}.test`,
|
|
316
|
+
name: `${category} Test`,
|
|
317
|
+
description: `Test ${category} category`,
|
|
318
|
+
category,
|
|
319
|
+
input: { type: 'object', properties: {} },
|
|
320
|
+
handler: async () => ({}),
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
expect(tool.category).toBe(category)
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
describe('defineAndRegister - Extended', () => {
|
|
330
|
+
beforeEach(() => {
|
|
331
|
+
registry.clear()
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
it('returns the created tool', () => {
|
|
335
|
+
const tool = defineAndRegister({
|
|
336
|
+
id: 'return.test',
|
|
337
|
+
name: 'Return Test',
|
|
338
|
+
description: 'Test return value',
|
|
339
|
+
category: 'data',
|
|
340
|
+
input: { type: 'object', properties: {} },
|
|
341
|
+
handler: async () => ({}),
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
expect(tool.id).toBe('return.test')
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
it('tool is immediately available in registry', () => {
|
|
348
|
+
defineAndRegister({
|
|
349
|
+
id: 'immediate.test',
|
|
350
|
+
name: 'Immediate Test',
|
|
351
|
+
description: 'Test immediate registration',
|
|
352
|
+
category: 'data',
|
|
353
|
+
input: { type: 'object', properties: {} },
|
|
354
|
+
handler: async () => ({}),
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
expect(registry.has('immediate.test')).toBe(true)
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
it('handler is executable from registry', async () => {
|
|
361
|
+
defineAndRegister({
|
|
362
|
+
id: 'exec.from.registry',
|
|
363
|
+
name: 'Exec From Registry',
|
|
364
|
+
description: 'Test execution',
|
|
365
|
+
category: 'data',
|
|
366
|
+
input: {
|
|
367
|
+
type: 'object',
|
|
368
|
+
properties: { value: { type: 'number' } },
|
|
369
|
+
},
|
|
370
|
+
handler: async (input: { value: number }) => ({ doubled: input.value * 2 }),
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
const tool = registry.get('exec.from.registry')
|
|
374
|
+
const result = await tool?.handler({ value: 5 })
|
|
375
|
+
|
|
376
|
+
expect(result).toEqual({ doubled: 10 })
|
|
377
|
+
})
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
describe('createToolExecutor - Extended', () => {
|
|
381
|
+
beforeEach(() => {
|
|
382
|
+
registry.clear()
|
|
383
|
+
|
|
384
|
+
registry.register(
|
|
385
|
+
defineTool({
|
|
386
|
+
id: 'exec.test.tool',
|
|
387
|
+
name: 'Exec Test Tool',
|
|
388
|
+
description: 'Tool for testing',
|
|
389
|
+
category: 'data',
|
|
390
|
+
input: {
|
|
391
|
+
type: 'object',
|
|
392
|
+
properties: { value: { type: 'string' } },
|
|
393
|
+
},
|
|
394
|
+
handler: async (input: { value: string }) => ({
|
|
395
|
+
result: input.value.toUpperCase(),
|
|
396
|
+
}),
|
|
397
|
+
})
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
registry.register(
|
|
401
|
+
defineTool({
|
|
402
|
+
id: 'exec.slow.tool',
|
|
403
|
+
name: 'Slow Tool',
|
|
404
|
+
description: 'Tool that takes time',
|
|
405
|
+
category: 'data',
|
|
406
|
+
input: { type: 'object', properties: {} },
|
|
407
|
+
handler: async () => {
|
|
408
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
409
|
+
return { done: true }
|
|
410
|
+
},
|
|
411
|
+
})
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
registry.register(
|
|
415
|
+
defineTool({
|
|
416
|
+
id: 'exec.throwing.tool',
|
|
417
|
+
name: 'Throwing Tool',
|
|
418
|
+
description: 'Tool that throws',
|
|
419
|
+
category: 'data',
|
|
420
|
+
input: { type: 'object', properties: {} },
|
|
421
|
+
handler: async () => {
|
|
422
|
+
throw new Error('Intentional test error')
|
|
423
|
+
},
|
|
424
|
+
})
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
registry.register(
|
|
428
|
+
defineTool({
|
|
429
|
+
id: 'exec.human.only',
|
|
430
|
+
name: 'Human Only Tool',
|
|
431
|
+
description: 'Only for humans',
|
|
432
|
+
category: 'data',
|
|
433
|
+
input: { type: 'object', properties: {} },
|
|
434
|
+
handler: async () => ({ human: true }),
|
|
435
|
+
options: { audience: 'human' },
|
|
436
|
+
})
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
registry.register(
|
|
440
|
+
defineTool({
|
|
441
|
+
id: 'exec.both.audiences',
|
|
442
|
+
name: 'Both Audiences',
|
|
443
|
+
description: 'For both',
|
|
444
|
+
category: 'data',
|
|
445
|
+
input: { type: 'object', properties: {} },
|
|
446
|
+
handler: async () => ({ both: true }),
|
|
447
|
+
options: { audience: 'both' },
|
|
448
|
+
})
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
registry.register(
|
|
452
|
+
defineTool({
|
|
453
|
+
id: 'exec.no.audience',
|
|
454
|
+
name: 'No Audience',
|
|
455
|
+
description: 'No audience specified',
|
|
456
|
+
category: 'data',
|
|
457
|
+
input: { type: 'object', properties: {} },
|
|
458
|
+
handler: async () => ({ noAudience: true }),
|
|
459
|
+
})
|
|
460
|
+
)
|
|
461
|
+
})
|
|
462
|
+
|
|
463
|
+
describe('execute method', () => {
|
|
464
|
+
it('executes tool and returns success', async () => {
|
|
465
|
+
const executor = createToolExecutor({
|
|
466
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
const result = await executor.execute('exec.test.tool', { value: 'hello' })
|
|
470
|
+
|
|
471
|
+
expect(result.success).toBe(true)
|
|
472
|
+
expect(result.data).toEqual({ result: 'HELLO' })
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
it('returns error for missing tool', async () => {
|
|
476
|
+
const executor = createToolExecutor({
|
|
477
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
478
|
+
})
|
|
479
|
+
|
|
480
|
+
const result = await executor.execute('nonexistent.tool', {})
|
|
481
|
+
|
|
482
|
+
expect(result.success).toBe(false)
|
|
483
|
+
expect(result.error?.code).toBe('TOOL_NOT_FOUND')
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
it('tracks execution duration', async () => {
|
|
487
|
+
const executor = createToolExecutor({
|
|
488
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
const result = await executor.execute('exec.slow.tool', {})
|
|
492
|
+
|
|
493
|
+
expect(result.success).toBe(true)
|
|
494
|
+
expect(result.metadata?.duration).toBeGreaterThanOrEqual(40)
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
it('handles handler errors', async () => {
|
|
498
|
+
const executor = createToolExecutor({
|
|
499
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
500
|
+
})
|
|
501
|
+
|
|
502
|
+
const result = await executor.execute('exec.throwing.tool', {})
|
|
503
|
+
|
|
504
|
+
expect(result.success).toBe(false)
|
|
505
|
+
expect(result.error?.code).toBe('EXECUTION_ERROR')
|
|
506
|
+
expect(result.error?.message).toBe('Intentional test error')
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
it('includes error duration in metadata', async () => {
|
|
510
|
+
const executor = createToolExecutor({
|
|
511
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
512
|
+
})
|
|
513
|
+
|
|
514
|
+
const result = await executor.execute('exec.throwing.tool', {})
|
|
515
|
+
|
|
516
|
+
expect(result.metadata?.duration).toBeDefined()
|
|
517
|
+
expect(result.metadata?.duration).toBeGreaterThanOrEqual(0)
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
it('includes requestId in success metadata', async () => {
|
|
521
|
+
const executor = createToolExecutor({
|
|
522
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
523
|
+
requestId: 'req-abc-123',
|
|
524
|
+
})
|
|
525
|
+
|
|
526
|
+
const result = await executor.execute('exec.test.tool', { value: 'test' })
|
|
527
|
+
|
|
528
|
+
expect(result.metadata?.requestId).toBe('req-abc-123')
|
|
529
|
+
})
|
|
530
|
+
|
|
531
|
+
it('includes requestId in error metadata', async () => {
|
|
532
|
+
const executor = createToolExecutor({
|
|
533
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
534
|
+
requestId: 'req-error-456',
|
|
535
|
+
})
|
|
536
|
+
|
|
537
|
+
const result = await executor.execute('exec.throwing.tool', {})
|
|
538
|
+
|
|
539
|
+
expect(result.metadata?.requestId).toBe('req-error-456')
|
|
540
|
+
})
|
|
541
|
+
})
|
|
542
|
+
|
|
543
|
+
describe('audience restrictions', () => {
|
|
544
|
+
it('agent cannot access human-only tools', async () => {
|
|
545
|
+
const executor = createToolExecutor({
|
|
546
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
const result = await executor.execute('exec.human.only', {})
|
|
550
|
+
|
|
551
|
+
expect(result.success).toBe(false)
|
|
552
|
+
expect(result.error?.code).toBe('ACCESS_DENIED')
|
|
553
|
+
})
|
|
554
|
+
|
|
555
|
+
it('human cannot access agent-only tools', async () => {
|
|
556
|
+
// Register an agent-only tool
|
|
557
|
+
registry.register(
|
|
558
|
+
defineTool({
|
|
559
|
+
id: 'exec.agent.only',
|
|
560
|
+
name: 'Agent Only',
|
|
561
|
+
description: 'Only for agents',
|
|
562
|
+
category: 'data',
|
|
563
|
+
input: { type: 'object', properties: {} },
|
|
564
|
+
handler: async () => ({ agent: true }),
|
|
565
|
+
options: { audience: 'agent' },
|
|
566
|
+
})
|
|
567
|
+
)
|
|
568
|
+
|
|
569
|
+
const executor = createToolExecutor({
|
|
570
|
+
executor: { type: 'human', id: 'user-1' },
|
|
571
|
+
})
|
|
572
|
+
|
|
573
|
+
const result = await executor.execute('exec.agent.only', {})
|
|
574
|
+
|
|
575
|
+
expect(result.success).toBe(false)
|
|
576
|
+
expect(result.error?.code).toBe('ACCESS_DENIED')
|
|
577
|
+
})
|
|
578
|
+
|
|
579
|
+
it('both audiences can access both-audience tools', async () => {
|
|
580
|
+
const agentExecutor = createToolExecutor({
|
|
581
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
582
|
+
})
|
|
583
|
+
const humanExecutor = createToolExecutor({
|
|
584
|
+
executor: { type: 'human', id: 'user-1' },
|
|
585
|
+
})
|
|
586
|
+
|
|
587
|
+
const agentResult = await agentExecutor.execute('exec.both.audiences', {})
|
|
588
|
+
const humanResult = await humanExecutor.execute('exec.both.audiences', {})
|
|
589
|
+
|
|
590
|
+
expect(agentResult.success).toBe(true)
|
|
591
|
+
expect(humanResult.success).toBe(true)
|
|
592
|
+
})
|
|
593
|
+
|
|
594
|
+
it('tools without audience are accessible to all', async () => {
|
|
595
|
+
const agentExecutor = createToolExecutor({
|
|
596
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
597
|
+
})
|
|
598
|
+
const humanExecutor = createToolExecutor({
|
|
599
|
+
executor: { type: 'human', id: 'user-1' },
|
|
600
|
+
})
|
|
601
|
+
|
|
602
|
+
const agentResult = await agentExecutor.execute('exec.no.audience', {})
|
|
603
|
+
const humanResult = await humanExecutor.execute('exec.no.audience', {})
|
|
604
|
+
|
|
605
|
+
expect(agentResult.success).toBe(true)
|
|
606
|
+
expect(humanResult.success).toBe(true)
|
|
607
|
+
})
|
|
608
|
+
})
|
|
609
|
+
|
|
610
|
+
describe('listAvailable method', () => {
|
|
611
|
+
it('lists tools available to agents', () => {
|
|
612
|
+
const executor = createToolExecutor({
|
|
613
|
+
executor: { type: 'agent', id: 'agent-1' },
|
|
614
|
+
})
|
|
615
|
+
|
|
616
|
+
const available = executor.listAvailable()
|
|
617
|
+
|
|
618
|
+
// Should not include human-only tools
|
|
619
|
+
expect(available.some((t) => t.id === 'exec.human.only')).toBe(false)
|
|
620
|
+
// Should include both-audience and no-audience tools
|
|
621
|
+
expect(available.some((t) => t.id === 'exec.both.audiences')).toBe(true)
|
|
622
|
+
expect(available.some((t) => t.id === 'exec.no.audience')).toBe(true)
|
|
623
|
+
})
|
|
624
|
+
|
|
625
|
+
it('lists tools available to humans', () => {
|
|
626
|
+
const executor = createToolExecutor({
|
|
627
|
+
executor: { type: 'human', id: 'user-1' },
|
|
628
|
+
})
|
|
629
|
+
|
|
630
|
+
const available = executor.listAvailable()
|
|
631
|
+
|
|
632
|
+
// Should include human-only tools
|
|
633
|
+
expect(available.some((t) => t.id === 'exec.human.only')).toBe(true)
|
|
634
|
+
// Should include both-audience and no-audience tools
|
|
635
|
+
expect(available.some((t) => t.id === 'exec.both.audiences')).toBe(true)
|
|
636
|
+
})
|
|
637
|
+
})
|
|
638
|
+
})
|
|
639
|
+
|
|
640
|
+
describe('toolBuilder - Extended', () => {
|
|
641
|
+
beforeEach(() => {
|
|
642
|
+
registry.clear()
|
|
643
|
+
})
|
|
644
|
+
|
|
645
|
+
describe('fluent API', () => {
|
|
646
|
+
it('chains all methods', () => {
|
|
647
|
+
const tool = toolBuilder('chain.test')
|
|
648
|
+
.name('Chain Test')
|
|
649
|
+
.description('Test chaining')
|
|
650
|
+
.category('data')
|
|
651
|
+
.subcategory('transform')
|
|
652
|
+
.input({ type: 'object', properties: {} })
|
|
653
|
+
.output({ type: 'object', properties: { result: { type: 'string' } } })
|
|
654
|
+
.options({ audience: 'both', tags: ['test'] })
|
|
655
|
+
.handler(async () => ({ result: 'done' }))
|
|
656
|
+
.build()
|
|
657
|
+
|
|
658
|
+
expect(tool.id).toBe('chain.test')
|
|
659
|
+
expect(tool.name).toBe('Chain Test')
|
|
660
|
+
expect(tool.description).toBe('Test chaining')
|
|
661
|
+
expect(tool.category).toBe('data')
|
|
662
|
+
expect(tool.subcategory).toBe('transform')
|
|
663
|
+
expect(tool.output).toBeDefined()
|
|
664
|
+
expect(tool.audience).toBe('both')
|
|
665
|
+
expect(tool.tags).toContain('test')
|
|
666
|
+
})
|
|
667
|
+
|
|
668
|
+
it('allows methods in any order', () => {
|
|
669
|
+
const tool = toolBuilder('order.test')
|
|
670
|
+
.handler(async () => ({}))
|
|
671
|
+
.category('web')
|
|
672
|
+
.name('Order Test')
|
|
673
|
+
.input({ type: 'object', properties: {} })
|
|
674
|
+
.description('Test order')
|
|
675
|
+
.build()
|
|
676
|
+
|
|
677
|
+
expect(tool.id).toBe('order.test')
|
|
678
|
+
expect(tool.name).toBe('Order Test')
|
|
679
|
+
expect(tool.category).toBe('web')
|
|
680
|
+
})
|
|
681
|
+
})
|
|
682
|
+
|
|
683
|
+
describe('build validation', () => {
|
|
684
|
+
it('throws when name is missing', () => {
|
|
685
|
+
expect(() => {
|
|
686
|
+
toolBuilder('missing.name')
|
|
687
|
+
.description('Description')
|
|
688
|
+
.category('data')
|
|
689
|
+
.input({ type: 'object', properties: {} })
|
|
690
|
+
.handler(async () => ({}))
|
|
691
|
+
.build()
|
|
692
|
+
}).toThrow()
|
|
693
|
+
})
|
|
694
|
+
|
|
695
|
+
it('throws when description is missing', () => {
|
|
696
|
+
expect(() => {
|
|
697
|
+
toolBuilder('missing.desc')
|
|
698
|
+
.name('Name')
|
|
699
|
+
.category('data')
|
|
700
|
+
.input({ type: 'object', properties: {} })
|
|
701
|
+
.handler(async () => ({}))
|
|
702
|
+
.build()
|
|
703
|
+
}).toThrow()
|
|
704
|
+
})
|
|
705
|
+
|
|
706
|
+
it('throws when category is missing', () => {
|
|
707
|
+
expect(() => {
|
|
708
|
+
toolBuilder('missing.category')
|
|
709
|
+
.name('Name')
|
|
710
|
+
.description('Description')
|
|
711
|
+
.input({ type: 'object', properties: {} })
|
|
712
|
+
.handler(async () => ({}))
|
|
713
|
+
.build()
|
|
714
|
+
}).toThrow()
|
|
715
|
+
})
|
|
716
|
+
|
|
717
|
+
it('throws when input is missing', () => {
|
|
718
|
+
expect(() => {
|
|
719
|
+
toolBuilder('missing.input')
|
|
720
|
+
.name('Name')
|
|
721
|
+
.description('Description')
|
|
722
|
+
.category('data')
|
|
723
|
+
.handler(async () => ({}))
|
|
724
|
+
.build()
|
|
725
|
+
}).toThrow()
|
|
726
|
+
})
|
|
727
|
+
|
|
728
|
+
it('throws when handler is missing', () => {
|
|
729
|
+
expect(() => {
|
|
730
|
+
toolBuilder('missing.handler')
|
|
731
|
+
.name('Name')
|
|
732
|
+
.description('Description')
|
|
733
|
+
.category('data')
|
|
734
|
+
.input({ type: 'object', properties: {} })
|
|
735
|
+
.build()
|
|
736
|
+
}).toThrow()
|
|
737
|
+
})
|
|
738
|
+
})
|
|
739
|
+
|
|
740
|
+
describe('register method', () => {
|
|
741
|
+
it('builds and registers tool', () => {
|
|
742
|
+
const tool = toolBuilder('builder.register')
|
|
743
|
+
.name('Builder Register')
|
|
744
|
+
.description('Test register')
|
|
745
|
+
.category('data')
|
|
746
|
+
.input({ type: 'object', properties: {} })
|
|
747
|
+
.handler(async () => ({}))
|
|
748
|
+
.register()
|
|
749
|
+
|
|
750
|
+
expect(tool.id).toBe('builder.register')
|
|
751
|
+
expect(registry.has('builder.register')).toBe(true)
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
it('returns the registered tool', () => {
|
|
755
|
+
const tool = toolBuilder('return.registered')
|
|
756
|
+
.name('Return Registered')
|
|
757
|
+
.description('Test return')
|
|
758
|
+
.category('data')
|
|
759
|
+
.input({ type: 'object', properties: {} })
|
|
760
|
+
.handler(async () => ({}))
|
|
761
|
+
.register()
|
|
762
|
+
|
|
763
|
+
const fromRegistry = registry.get('return.registered')
|
|
764
|
+
expect(tool.id).toBe(fromRegistry?.id)
|
|
765
|
+
})
|
|
766
|
+
})
|
|
767
|
+
|
|
768
|
+
describe('handler execution', () => {
|
|
769
|
+
it('handler receives input correctly', async () => {
|
|
770
|
+
const tool = toolBuilder('input.receive')
|
|
771
|
+
.name('Input Receive')
|
|
772
|
+
.description('Test input')
|
|
773
|
+
.category('data')
|
|
774
|
+
.input({
|
|
775
|
+
type: 'object',
|
|
776
|
+
properties: { x: { type: 'number' }, y: { type: 'number' } },
|
|
777
|
+
})
|
|
778
|
+
.handler(async (input: { x: number; y: number }) => ({
|
|
779
|
+
sum: input.x + input.y,
|
|
780
|
+
}))
|
|
781
|
+
.build()
|
|
782
|
+
|
|
783
|
+
const result = await tool.handler({ x: 3, y: 4 })
|
|
784
|
+
|
|
785
|
+
expect(result).toEqual({ sum: 7 })
|
|
786
|
+
})
|
|
787
|
+
|
|
788
|
+
it('handler can be async', async () => {
|
|
789
|
+
const tool = toolBuilder('async.handler')
|
|
790
|
+
.name('Async Handler')
|
|
791
|
+
.description('Test async')
|
|
792
|
+
.category('data')
|
|
793
|
+
.input({ type: 'object', properties: {} })
|
|
794
|
+
.handler(async () => {
|
|
795
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
796
|
+
return { delayed: true }
|
|
797
|
+
})
|
|
798
|
+
.build()
|
|
799
|
+
|
|
800
|
+
const result = await tool.handler({})
|
|
801
|
+
|
|
802
|
+
expect(result).toEqual({ delayed: true })
|
|
803
|
+
})
|
|
804
|
+
|
|
805
|
+
it('handler can throw errors', async () => {
|
|
806
|
+
const tool = toolBuilder('error.handler')
|
|
807
|
+
.name('Error Handler')
|
|
808
|
+
.description('Test errors')
|
|
809
|
+
.category('data')
|
|
810
|
+
.input({ type: 'object', properties: {} })
|
|
811
|
+
.handler(async () => {
|
|
812
|
+
throw new Error('Handler error')
|
|
813
|
+
})
|
|
814
|
+
.build()
|
|
815
|
+
|
|
816
|
+
await expect(tool.handler({})).rejects.toThrow('Handler error')
|
|
817
|
+
})
|
|
818
|
+
})
|
|
819
|
+
})
|