Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 06:00 UTC
| 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 | await wrapped.execute( |
| 54 | { query: 'options input' }, |
| 55 | { |
| 56 | toolCallId: 'call_1', |
| 57 | messages: [], |
| 58 | context: {}, |
| 59 | } |
| 60 | ); |
| 61 | if (calls.at(-1)?.slug !== 'TEST_TOOL' || calls.at(-1)?.params?.query !== 'options input') { |
| 62 | throw new Error('AI SDK v7 execution options were not accepted'); |
| 63 | } |
| 64 | console.log('V7_EXECUTION_OPTIONS_OK'); |
| 65 | |
| 66 | // The model is shown the tool's JSON Schema as is, and the AI SDK validates |
| 67 | // tool-call arguments with the Zod schema built from it. |
| 68 | const annotatedTool = { |
| 69 | ...composioTool, |
| 70 | slug: 'ANNOTATED_TOOL', |
| 71 | inputParameters: { |
| 72 | type: 'object', |
| 73 | properties: { |
| 74 | file: { type: 'string', format: 'binary' }, |
| 75 | hex: { |
| 76 | type: 'string', |
| 77 | contentEncoding: 'base16', |
| 78 | contentMediaType: 'application/octet-stream', |
| 79 | }, |
| 80 | }, |
| 81 | required: ['hex'], |
| 82 | }, |
| 83 | }; |
| 84 | const annotatedSchema = asSchema(provider.wrapTool(annotatedTool, executeTool).inputSchema); |
| 85 | const annotatedJson = await annotatedSchema.jsonSchema; |
| 86 | if ( |
| 87 | annotatedJson.properties?.file?.format !== 'binary' || |
| 88 | annotatedJson.properties?.hex?.contentEncoding !== 'base16' || |
| 89 | annotatedJson.properties?.hex?.contentMediaType !== 'application/octet-stream' |
| 90 | ) { |
| 91 | throw new Error(`The source keywords did not reach the schema: ${JSON.stringify(annotatedJson)}`); |
| 92 | } |
| 93 | const accepted = await annotatedSchema.validate({ hex: 'ff' }); |
| 94 | const rejected = await annotatedSchema.validate({ hex: 1 }); |
| 95 | if (!accepted.success || rejected.success) { |
| 96 | throw new Error('Tool-call arguments were not validated with the Zod schema'); |
| 97 | } |
| 98 | console.log('SOURCE_SCHEMA_OK'); |
| 99 | |
| 100 | const toolSet = provider.wrapTools([composioTool], executeTool); |
| 101 | if (!toolSet.TEST_TOOL) { |
| 102 | throw new Error('Wrapped tools collection is missing TEST_TOOL'); |
| 103 | } |
| 104 | console.log('TOOL_SET_OK'); |