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 | * Regression test: `LlamaindexProvider.wrapTool` must dereference internal |
| 3 | * `$ref`/`$defs` before handing the schema to `jsonSchemaToZodSchema`. |
| 4 | * Without dereferencing, a `$ref`-typed property degrades to `z.any()` and |
| 5 | * silently accepts any value. |
| 6 | */ |
| 7 | |
| 8 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 9 | import type { Tool } from '@composio/core'; |
| 10 | import { LlamaindexProvider } from '../src'; |
| 11 | |
| 12 | // Mock the llamaindex module (per-file scoping keeps this independent of llamaindex.test.ts). |
| 13 | vi.mock('llamaindex', () => { |
| 14 | return { |
| 15 | FunctionTool: class { |
| 16 | constructor( |
| 17 | readonly call: (input: unknown) => unknown, |
| 18 | readonly metadata: { name: string; description: string; parameters: unknown }, |
| 19 | readonly zodType: unknown |
| 20 | ) {} |
| 21 | }, |
| 22 | }; |
| 23 | }); |
| 24 | |
| 25 | const refTool: Tool = { |
| 26 | slug: 'TEST_REF_TOOL', |
| 27 | name: 'Ref Tool', |
| 28 | description: 'Tool whose schema carries internal $ref pointers', |
| 29 | toolkit: { slug: 'reftoolkit', name: 'Ref Toolkit' }, |
| 30 | version: '20260828_00', |
| 31 | availableVersions: ['20260828_00'], |
| 32 | tags: [], |
| 33 | inputParameters: { |
| 34 | type: 'object', |
| 35 | properties: { |
| 36 | message: { $ref: '#/$defs/Message' }, |
| 37 | }, |
| 38 | required: ['message'], |
| 39 | $defs: { |
| 40 | Message: { |
| 41 | type: 'object', |
| 42 | properties: { |
| 43 | subject: { type: 'string' }, |
| 44 | body: { type: 'string' }, |
| 45 | }, |
| 46 | required: ['subject', 'body'], |
| 47 | additionalProperties: false, |
| 48 | }, |
| 49 | }, |
| 50 | } as unknown as Tool['inputParameters'], |
| 51 | } as unknown as Tool; |
| 52 | |
| 53 | const danglingRefTool: Tool = { |
| 54 | ...refTool, |
| 55 | slug: 'TEST_DANGLING_REF_TOOL', |
| 56 | inputParameters: { |
| 57 | type: 'object', |
| 58 | properties: { |
| 59 | message: { $ref: '#/$defs/Message' }, |
| 60 | }, |
| 61 | required: ['message'], |
| 62 | } as unknown as Tool['inputParameters'], |
| 63 | }; |
| 64 | |
| 65 | const recursiveRefTool: Tool = { |
| 66 | ...refTool, |
| 67 | slug: 'TEST_RECURSIVE_REF_TOOL', |
| 68 | inputParameters: { |
| 69 | type: 'object', |
| 70 | properties: { |
| 71 | node: { $ref: '#/$defs/Node' }, |
| 72 | }, |
| 73 | required: ['node'], |
| 74 | $defs: { |
| 75 | Node: { |
| 76 | type: 'object', |
| 77 | properties: { |
| 78 | value: { type: 'string' }, |
| 79 | child: { $ref: '#/$defs/Node' }, |
| 80 | }, |
| 81 | required: ['value'], |
| 82 | }, |
| 83 | }, |
| 84 | } as unknown as Tool['inputParameters'], |
| 85 | }; |
| 86 | |
| 87 | describe('LlamaindexProvider regression: $ref in JSON Schema', () => { |
| 88 | let provider: LlamaindexProvider; |
| 89 | let executeToolFn: (toolSlug: string, params: Record<string, unknown>) => Promise<unknown>; |
| 90 | |
| 91 | beforeEach(() => { |
| 92 | provider = new LlamaindexProvider(); |
| 93 | executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true }); |
| 94 | }); |
| 95 | |
| 96 | it('validates arguments against the $defs-described shape', () => { |
| 97 | const wrapped = provider.wrapTool(refTool, executeToolFn); |
| 98 | const schema = (wrapped as unknown as { zodType: unknown }).zodType as { |
| 99 | safeParse: (value: unknown) => { success: boolean }; |
| 100 | }; |
| 101 | |
| 102 | expect(schema.safeParse({ message: { subject: 's', body: 'b' } }).success).toBe(true); |
| 103 | expect(schema.safeParse({ message: 42 }).success).toBe(false); |
| 104 | }); |
| 105 | |
| 106 | it('shows the model the schema with its definitions inlined', () => { |
| 107 | const wrapped = provider.wrapTool(refTool, executeToolFn); |
| 108 | |
| 109 | expect(wrapped.metadata.parameters).toEqual({ |
| 110 | type: 'object', |
| 111 | properties: { |
| 112 | message: { |
| 113 | type: 'object', |
| 114 | properties: { subject: { type: 'string' }, body: { type: 'string' } }, |
| 115 | required: ['subject', 'body'], |
| 116 | additionalProperties: false, |
| 117 | }, |
| 118 | }, |
| 119 | required: ['message'], |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | it('does not throw when $ref points into an undeclared $defs block', () => { |
| 124 | expect(() => provider.wrapTool(danglingRefTool, executeToolFn)).not.toThrow(); |
| 125 | }); |
| 126 | |
| 127 | it('does not throw on a self-referential $defs schema', () => { |
| 128 | expect(() => provider.wrapTool(recursiveRefTool, executeToolFn)).not.toThrow(); |
| 129 | }); |
| 130 | }); |