Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 211 lines 7.24 KB
Raw
1 import fs from 'node:fs';
2 import path from 'node:path';
3 import { describe, expect, it } from '@effect/vitest';
4 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
5 import * as BunPath from '@effect/platform-bun/BunPath';
6 import { Array as Arr, ConfigProvider, Effect, Layer, Option } from 'effect';
7 import * as tempy from 'tempy';
8 import {
9 getCachedToolInputDefinition,
10 ToolInputSchemaCompileError,
11 ToolInputValidationError,
12 validateToolInputArgumentsWithDefinition,
13 } from 'src/services/tool-input-validation';
14 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
15 import { loadObjectCases } from '../../fixtures/corpus';
16
17 const definition = {
18 schemaPath: '/tmp/GMAIL_SEND_EMAIL.json',
19 schema: {
20 type: 'object',
21 additionalProperties: false,
22 required: ['recipient_email'],
23 properties: {
24 recipient_email: { type: 'string' },
25 subject: { type: 'string' },
26 body: { type: 'string' },
27 },
28 },
29 } as const;
30
31 // Every schema here compiles, so only the argument failure is of interest.
32 const flipValidationError = <A, R>(
33 effect: Effect.Effect<A, ToolInputValidationError | ToolInputSchemaCompileError, R>
34 ) => effect.pipe(Effect.catchTag('ToolInputSchemaCompileError', Effect.die), Effect.flip);
35
36 describe('tool input validation', () => {
37 it.effect('validates tool input through Effect Schema', () =>
38 Effect.gen(function* () {
39 const result = yield* validateToolInputArgumentsWithDefinition(
40 'GMAIL_SEND_EMAIL',
41 { recipient_email: 'karan@composio.dev', subject: 'Hello' },
42 definition
43 );
44
45 expect(result).toEqual(definition);
46 })
47 );
48
49 it.effect('suggests a schema key using the CLI autocorrect distance', () =>
50 Effect.gen(function* () {
51 const error = yield* validateToolInputArgumentsWithDefinition(
52 'GMAIL_SEND_EMAIL',
53 { recipent_email: 'karan@composio.dev' },
54 definition
55 ).pipe(flipValidationError);
56
57 expect(error).toBeInstanceOf(ToolInputValidationError);
58 expect(error.issues).toContain(
59 '<root>: Unknown key "recipent_email". Use "recipient_email" instead. Allowed top-level keys: recipient_email, subject, body'
60 );
61 })
62 );
63
64 it.effect('keeps prefix-based suggestions without guessing unrelated keys', () =>
65 Effect.gen(function* () {
66 const prefixError = yield* validateToolInputArgumentsWithDefinition(
67 'GMAIL_SEND_EMAIL',
68 { recipient: 'karan@composio.dev' },
69 definition
70 ).pipe(flipValidationError);
71 const unrelatedError = yield* validateToolInputArgumentsWithDefinition(
72 'GMAIL_SEND_EMAIL',
73 { account: 'karan@composio.dev' },
74 definition
75 ).pipe(flipValidationError);
76
77 expect(prefixError.issues).toContain(
78 '<root>: Unknown key "recipient". Use "recipient_email" instead. Allowed top-level keys: recipient_email, subject, body'
79 );
80 expect(unrelatedError.issues).toContain(
81 '<root>: Unknown key "account". Allowed top-level keys: recipient_email, subject, body'
82 );
83 })
84 );
85
86 it.effect(
87 'keeps length-aware typo suggestions without letting containment bypass the limit',
88 () =>
89 Effect.gen(function* () {
90 const longTypoError = yield* validateToolInputArgumentsWithDefinition(
91 'GMAIL_SEND_EMAIL',
92 { recpnt_email: 'karan@composio.dev' },
93 definition
94 ).pipe(flipValidationError);
95 const containmentError = yield* validateToolInputArgumentsWithDefinition(
96 'TEST_TOOL',
97 { to: true },
98 {
99 schemaPath: '/tmp/TEST_TOOL.json',
100 schema: {
101 type: 'object',
102 additionalProperties: false,
103 properties: { auto_reply: { type: 'boolean' } },
104 },
105 }
106 ).pipe(flipValidationError);
107
108 expect(longTypoError.issues).toContain(
109 '<root>: Unknown key "recpnt_email". Use "recipient_email" instead. Allowed top-level keys: recipient_email, subject, body'
110 );
111 expect(containmentError.issues).toContain(
112 '<root>: Unknown key "to". Allowed top-level keys: auto_reply'
113 );
114 })
115 );
116
117 it.effect('reads legacy bare-schema cache entries through the Effect decoder', () => {
118 const cacheDir = tempy.temporaryDirectory();
119 const schemaPath = path.join(cacheDir, 'tool_definitions', 'GMAIL_SEND_EMAIL.json');
120 fs.mkdirSync(path.dirname(schemaPath), { recursive: true });
121 fs.writeFileSync(schemaPath, JSON.stringify(definition.schema));
122
123 return Effect.gen(function* () {
124 const cached = yield* getCachedToolInputDefinition('GMAIL_SEND_EMAIL');
125
126 expect(cached).toEqual({
127 schemaPath,
128 schema: definition.schema,
129 version: null,
130 });
131 }).pipe(
132 Effect.provide(
133 Layer.mergeAll(
134 BunFileSystem.layer,
135 BunPath.layer,
136 Layer.succeed(NodeOs, defaultNodeOs({ homedir: cacheDir }))
137 )
138 ),
139 Effect.provideService(
140 ConfigProvider.ConfigProvider,
141 ConfigProvider.fromEnv({ env: { COMPOSIO_CACHE_DIR: cacheDir } })
142 )
143 );
144 });
145
146 it.effect('preserves field paths in validation errors', () =>
147 Effect.gen(function* () {
148 const error = yield* validateToolInputArgumentsWithDefinition(
149 'GMAIL_SEND_EMAIL',
150 { recipient_email: 42 },
151 definition
152 ).pipe(flipValidationError);
153
154 expect(error.issues).toContainEqual(expect.stringMatching(/^recipient_email:/));
155 })
156 );
157
158 it.effect('preserves all field and unknown-key failures in one validation pass', () =>
159 Effect.gen(function* () {
160 const error = yield* validateToolInputArgumentsWithDefinition(
161 'GMAIL_SEND_EMAIL',
162 { recipient_email: 42, subject: false, recipent_email: 'karan@composio.dev' },
163 definition
164 ).pipe(flipValidationError);
165
166 expect(error.issues).toContainEqual(expect.stringMatching(/^recipient_email:/));
167 expect(error.issues).toContainEqual(expect.stringMatching(/^subject:/));
168 expect(error.issues).toContain(
169 '<root>: Unknown key "recipent_email". Use "recipient_email" instead. Allowed top-level keys: recipient_email, subject, body'
170 );
171 })
172 );
173 });
174
175 describe('free-form object tool inputs', () => {
176 const freeFormCase = Arr.findFirst(
177 loadObjectCases(),
178 entry => entry.id === 'nested-free-form-object'
179 ).pipe(
180 Option.getOrThrowWith(() => new Error('Corpus is missing the nested-free-form-object case'))
181 );
182
183 const freeFormDefinition = {
184 schemaPath: '/tmp/METABASE_POST_API_CARD.json',
185 schema: freeFormCase.schema,
186 } as const;
187
188 it.effect('accepts arbitrary content inside a property-less object', () =>
189 Effect.gen(function* () {
190 const result = yield* validateToolInputArgumentsWithDefinition(
191 'METABASE_POST_API_CARD',
192 freeFormCase.instances[0].input,
193 freeFormDefinition
194 );
195
196 expect(result).toEqual(freeFormDefinition);
197 })
198 );
199
200 it.effect('still reports unknown keys for named-property schemas', () =>
201 Effect.gen(function* () {
202 const error = yield* validateToolInputArgumentsWithDefinition(
203 'GMAIL_SEND_EMAIL',
204 { recipient_email: 'karan@composio.dev', nope: 1 },
205 definition
206 ).pipe(flipValidationError);
207
208 expect(error).toBeInstanceOf(ToolInputValidationError);
209 })
210 );
211 });