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
mjs 91 lines 2.85 KB
Raw
1 import { VercelProvider } from '@composio/vercel';
2 import { asSchema } from 'ai';
3
4 const composioTool = {
5 slug: 'TEST_TOOL',
6 name: 'Test Tool',
7 description: 'A tool used by the AI SDK compatibility fixture',
8 version: '20260625_00',
9 availableVersions: ['20260625_00'],
10 inputParameters: {
11 type: 'object',
12 properties: {
13 query: {
14 type: 'string',
15 description: 'Query text',
16 },
17 },
18 required: ['query'],
19 },
20 tags: [],
21 };
22
23 const calls = [];
24 const executeTool = async (slug, params) => {
25 calls.push({ slug, params });
26 return {
27 data: { slug, params },
28 error: null,
29 successful: true,
30 };
31 };
32
33 const provider = new VercelProvider();
34 const wrapped = provider.wrapTool(composioTool, executeTool);
35
36 if (!wrapped || !wrapped.inputSchema || typeof wrapped.execute !== 'function') {
37 throw new Error('Wrapped tool does not match the AI SDK tool shape');
38 }
39 console.log('WRAPPED_TOOL_INPUT_SCHEMA_OK');
40
41 await wrapped.execute({ query: 'object input' });
42 if (calls.at(-1)?.slug !== 'TEST_TOOL' || calls.at(-1)?.params?.query !== 'object input') {
43 throw new Error('Object input was not forwarded to executeTool');
44 }
45 console.log('OBJECT_INPUT_EXECUTION_OK');
46
47 await wrapped.execute(JSON.stringify({ query: 'string input' }));
48 if (calls.at(-1)?.slug !== 'TEST_TOOL' || calls.at(-1)?.params?.query !== 'string input') {
49 throw new Error('String input was not normalized before executeTool');
50 }
51 console.log('STRING_INPUT_EXECUTION_OK');
52
53 // The model is shown the tool's JSON Schema as is, and the AI SDK validates
54 // tool-call arguments with the Zod schema built from it.
55 const annotatedTool = {
56 ...composioTool,
57 slug: 'ANNOTATED_TOOL',
58 inputParameters: {
59 type: 'object',
60 properties: {
61 file: { type: 'string', format: 'binary' },
62 hex: {
63 type: 'string',
64 contentEncoding: 'base16',
65 contentMediaType: 'application/octet-stream',
66 },
67 },
68 required: ['hex'],
69 },
70 };
71 const annotatedSchema = asSchema(provider.wrapTool(annotatedTool, executeTool).inputSchema);
72 const annotatedJson = await annotatedSchema.jsonSchema;
73 if (
74 annotatedJson.properties?.file?.format !== 'binary' ||
75 annotatedJson.properties?.hex?.contentEncoding !== 'base16' ||
76 annotatedJson.properties?.hex?.contentMediaType !== 'application/octet-stream'
77 ) {
78 throw new Error(`The source keywords did not reach the schema: ${JSON.stringify(annotatedJson)}`);
79 }
80 const accepted = await annotatedSchema.validate({ hex: 'ff' });
81 const rejected = await annotatedSchema.validate({ hex: 1 });
82 if (!accepted.success || rejected.success) {
83 throw new Error('Tool-call arguments were not validated with the Zod schema');
84 }
85 console.log('SOURCE_SCHEMA_OK');
86
87 const toolSet = provider.wrapTools([composioTool], executeTool);
88 if (!toolSet.TEST_TOOL) {
89 throw new Error('Wrapped tools collection is missing TEST_TOOL');
90 }
91 console.log('TOOL_SET_OK');