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, 04:45 UTC
| 1 | /** |
| 2 | * Regression test: `LangchainProvider.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 { LangchainProvider } from '../src'; |
| 11 | |
| 12 | const refTool: Tool = { |
| 13 | slug: 'TEST_REF_TOOL', |
| 14 | name: 'Ref Tool', |
| 15 | description: 'Tool whose schema carries internal $ref pointers', |
| 16 | toolkit: { slug: 'reftoolkit', name: 'Ref Toolkit' }, |
| 17 | version: '20260828_00', |
| 18 | availableVersions: ['20260828_00'], |
| 19 | tags: [], |
| 20 | inputParameters: { |
| 21 | type: 'object', |
| 22 | properties: { |
| 23 | message: { $ref: '#/$defs/Message' }, |
| 24 | }, |
| 25 | required: ['message'], |
| 26 | $defs: { |
| 27 | Message: { |
| 28 | type: 'object', |
| 29 | properties: { |
| 30 | subject: { type: 'string' }, |
| 31 | body: { type: 'string' }, |
| 32 | }, |
| 33 | required: ['subject', 'body'], |
| 34 | additionalProperties: false, |
| 35 | }, |
| 36 | }, |
| 37 | } as unknown as Tool['inputParameters'], |
| 38 | } as unknown as Tool; |
| 39 | |
| 40 | // Same tool but with the $defs block deleted — GMAIL_FETCH_EMAILS style. |
| 41 | const danglingRefTool: Tool = { |
| 42 | ...refTool, |
| 43 | slug: 'TEST_DANGLING_REF_TOOL', |
| 44 | inputParameters: { |
| 45 | type: 'object', |
| 46 | properties: { |
| 47 | message: { $ref: '#/$defs/Message' }, |
| 48 | }, |
| 49 | required: ['message'], |
| 50 | } as unknown as Tool['inputParameters'], |
| 51 | }; |
| 52 | |
| 53 | // A self-referential schema has no finite inlined form. |
| 54 | const recursiveRefTool: Tool = { |
| 55 | ...refTool, |
| 56 | slug: 'TEST_RECURSIVE_REF_TOOL', |
| 57 | inputParameters: { |
| 58 | type: 'object', |
| 59 | properties: { |
| 60 | node: { $ref: '#/$defs/Node' }, |
| 61 | }, |
| 62 | required: ['node'], |
| 63 | $defs: { |
| 64 | Node: { |
| 65 | type: 'object', |
| 66 | properties: { |
| 67 | value: { type: 'string' }, |
| 68 | child: { $ref: '#/$defs/Node' }, |
| 69 | }, |
| 70 | required: ['value'], |
| 71 | }, |
| 72 | }, |
| 73 | } as unknown as Tool['inputParameters'], |
| 74 | }; |
| 75 | |
| 76 | describe('LangchainProvider regression: $ref in JSON Schema', () => { |
| 77 | let provider: LangchainProvider; |
| 78 | let executeToolFn: (toolSlug: string, params: Record<string, unknown>) => Promise<unknown>; |
| 79 | |
| 80 | beforeEach(() => { |
| 81 | provider = new LangchainProvider(); |
| 82 | executeToolFn = vi.fn().mockResolvedValue({ data: {}, error: null, successful: true }); |
| 83 | }); |
| 84 | |
| 85 | it('produces a Zod schema that validates the $defs-described shape', () => { |
| 86 | const wrapped = provider.wrapTool(refTool, executeToolFn); |
| 87 | const schema = wrapped.schema as { safeParse: (value: unknown) => { success: boolean } }; |
| 88 | |
| 89 | expect(schema.safeParse({ message: { subject: 's', body: 'b' } }).success).toBe(true); |
| 90 | // Before the fix, $ref degraded to z.any() and this would have passed. |
| 91 | expect(schema.safeParse({ message: 42 }).success).toBe(false); |
| 92 | }); |
| 93 | |
| 94 | it('does not throw when $ref points into an undeclared $defs block', () => { |
| 95 | expect(() => provider.wrapTool(danglingRefTool, executeToolFn)).not.toThrow(); |
| 96 | }); |
| 97 | |
| 98 | it('does not throw on a self-referential $defs schema', () => { |
| 99 | expect(() => provider.wrapTool(recursiveRefTool, executeToolFn)).not.toThrow(); |
| 100 | }); |
| 101 | }); |