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 | /** |
| 2 | * The schema the provider shows the model through LlamaIndex, and the |
| 3 | * validation LlamaIndex runs on tool-call arguments. |
| 4 | */ |
| 5 | import { describe, expect, it, vi } from 'vitest'; |
| 6 | import type { Tool } from '@composio/core'; |
| 7 | import { LlamaindexProvider } from '../src'; |
| 8 | import { |
| 9 | acceptedFor, |
| 10 | loadObjectCases, |
| 11 | } from '../../../core/test/fixtures/json-schema-conversion/corpus'; |
| 12 | |
| 13 | type Json = Record<string, unknown>; |
| 14 | type Validator = { safeParse: (value: unknown) => { success: boolean } }; |
| 15 | |
| 16 | // The real `FunctionTool`, which also keeps the validator it was given so the |
| 17 | // tests can run it: LlamaIndex itself only logs a failed validation. |
| 18 | vi.mock('llamaindex', async importOriginal => { |
| 19 | const actual = await importOriginal<typeof import('llamaindex')>(); |
| 20 | class InspectableFunctionTool extends actual.FunctionTool<unknown, string> { |
| 21 | readonly validator: unknown; |
| 22 | constructor(...args: ConstructorParameters<typeof actual.FunctionTool<unknown, string>>) { |
| 23 | super(...args); |
| 24 | this.validator = args[2]; |
| 25 | } |
| 26 | } |
| 27 | return { ...actual, FunctionTool: InspectableFunctionTool }; |
| 28 | }); |
| 29 | |
| 30 | const buildTool = (inputParameters: Json): Tool => |
| 31 | ({ |
| 32 | slug: 'TEST_TOOL', |
| 33 | name: 'Test Tool', |
| 34 | description: 'A tool under test', |
| 35 | version: '20260901_00', |
| 36 | availableVersions: ['20260901_00'], |
| 37 | tags: [], |
| 38 | inputParameters, |
| 39 | }) as unknown as Tool; |
| 40 | |
| 41 | const wrap = (tool: Tool) => |
| 42 | new LlamaindexProvider().wrapTool(tool, async () => ({ |
| 43 | data: {}, |
| 44 | error: null, |
| 45 | successful: true, |
| 46 | })); |
| 47 | |
| 48 | describe('LlamaindexProvider schema sent to the model', () => { |
| 49 | it('sends the source schema, including keywords a Zod schema cannot carry', () => { |
| 50 | const inputParameters = { |
| 51 | type: 'object', |
| 52 | properties: { |
| 53 | file: { type: 'string', format: 'binary' }, |
| 54 | hex: { |
| 55 | type: 'string', |
| 56 | contentEncoding: 'base16', |
| 57 | contentMediaType: 'application/octet-stream', |
| 58 | }, |
| 59 | page: { type: 'integer', default: 1 }, |
| 60 | }, |
| 61 | required: ['file'], |
| 62 | }; |
| 63 | |
| 64 | expect(wrap(buildTool(inputParameters)).metadata.parameters).toEqual(inputParameters); |
| 65 | }); |
| 66 | |
| 67 | it('removes duplicate required entries, including in inlined definitions', () => { |
| 68 | const parameters = wrap( |
| 69 | buildTool({ |
| 70 | type: 'object', |
| 71 | properties: { query: { type: 'string' }, filter: { $ref: '#/$defs/Filter' } }, |
| 72 | required: ['query', 'query'], |
| 73 | $defs: { |
| 74 | Filter: { |
| 75 | type: 'object', |
| 76 | properties: { field: { type: 'string' } }, |
| 77 | required: ['field', 'field'], |
| 78 | }, |
| 79 | }, |
| 80 | }) |
| 81 | ).metadata.parameters as Json; |
| 82 | |
| 83 | expect(parameters.required).toEqual(['query']); |
| 84 | expect((parameters.properties as Record<string, Json>).filter.required).toEqual(['field']); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe('LlamaindexProvider tool-call validation', () => { |
| 89 | for (const testCase of loadObjectCases()) { |
| 90 | it(`matches the shared corpus on ${testCase.id}`, () => { |
| 91 | const { validator } = wrap(buildTool(testCase.schema)) as unknown as { |
| 92 | validator: Validator; |
| 93 | }; |
| 94 | |
| 95 | for (const instance of testCase.instances) { |
| 96 | expect(validator.safeParse(instance.input).success, JSON.stringify(instance.input)).toBe( |
| 97 | acceptedFor(instance, 'zod') |
| 98 | ); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | it('passes the parsed arguments to the tool', async () => { |
| 104 | const executeTool = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true }); |
| 105 | const wrapped = new LlamaindexProvider().wrapTool( |
| 106 | buildTool({ |
| 107 | type: 'object', |
| 108 | properties: { query: { type: 'string' }, page: { type: 'integer', default: 1 } }, |
| 109 | required: ['query'], |
| 110 | }), |
| 111 | executeTool |
| 112 | ); |
| 113 | |
| 114 | await wrapped.call({ query: 'composio' }); |
| 115 | |
| 116 | expect(executeTool).toHaveBeenCalledWith('TEST_TOOL', { query: 'composio', page: 1 }); |
| 117 | }); |
| 118 | }); |