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 { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | import { LlamaindexProvider, LlamaindexTool } from '../src'; |
| 3 | import { Tool } from '@composio/core'; |
| 4 | |
| 5 | // Mock the llamaindex module |
| 6 | vi.mock('llamaindex', () => { |
| 7 | return { |
| 8 | FunctionTool: class { |
| 9 | constructor( |
| 10 | readonly call: (input: unknown) => unknown, |
| 11 | readonly metadata: { name: string; description: string; parameters: unknown }, |
| 12 | readonly zodType: unknown |
| 13 | ) {} |
| 14 | }, |
| 15 | }; |
| 16 | }); |
| 17 | |
| 18 | describe('LlamaindexProvider', () => { |
| 19 | let provider: LlamaindexProvider; |
| 20 | let sampleTool: Tool; |
| 21 | let executeToolFn: (toolSlug: string, params: Record<string, unknown>) => Promise<unknown>; |
| 22 | |
| 23 | beforeEach(() => { |
| 24 | provider = new LlamaindexProvider(); |
| 25 | |
| 26 | // Create a real execute function that simulates tool execution |
| 27 | executeToolFn = vi |
| 28 | .fn() |
| 29 | .mockImplementation(async (toolSlug: string, params: Record<string, unknown>) => { |
| 30 | // Simulate actual tool execution with realistic response |
| 31 | return { |
| 32 | data: { toolSlug, params }, |
| 33 | error: null, |
| 34 | successful: true, |
| 35 | }; |
| 36 | }); |
| 37 | |
| 38 | // Create a real sample tool that matches actual tool structure |
| 39 | sampleTool = { |
| 40 | slug: 'SEARCH_TOOL', |
| 41 | name: 'Search Tool', |
| 42 | description: 'Search for information in the knowledge base', |
| 43 | version: '20250909_00', |
| 44 | availableVersions: ['20250909_00', '20250901_00'], |
| 45 | inputParameters: { |
| 46 | type: 'object', |
| 47 | properties: { |
| 48 | query: { |
| 49 | type: 'string', |
| 50 | description: 'The search query', |
| 51 | }, |
| 52 | }, |
| 53 | required: ['query'], |
| 54 | additionalProperties: false, |
| 55 | }, |
| 56 | toolkit: { |
| 57 | slug: 'search_toolkit', |
| 58 | name: 'Search Toolkit', |
| 59 | }, |
| 60 | tags: ['search', 'knowledge'], |
| 61 | }; |
| 62 | }); |
| 63 | |
| 64 | describe('Provider Configuration', () => { |
| 65 | it('should be properly initialized with correct name', () => { |
| 66 | expect(provider.name).toBe('llamaindex'); |
| 67 | }); |
| 68 | |
| 69 | it('should be marked as agentic', () => { |
| 70 | expect(provider._isAgentic).toBe(true); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | describe('Tool Wrapping', () => { |
| 75 | it('should successfully wrap a valid tool into LlamaIndex format', () => { |
| 76 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 77 | |
| 78 | // Verify the wrapped tool has the correct structure |
| 79 | expect(wrappedTool.metadata.name).toBe(sampleTool.slug); |
| 80 | expect(wrappedTool.metadata.description).toBe(sampleTool.description); |
| 81 | expect(wrappedTool).toHaveProperty('metadata'); |
| 82 | expect(wrappedTool).toHaveProperty('call'); |
| 83 | expect(typeof wrappedTool.call).toBe('function'); |
| 84 | }); |
| 85 | |
| 86 | it('should wrap a tool that can be executed with parameters', async () => { |
| 87 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 88 | const searchParams = { query: 'test search' }; |
| 89 | |
| 90 | const result = await wrappedTool.call(searchParams); |
| 91 | const parsedResult = JSON.parse(result as string); |
| 92 | |
| 93 | expect(parsedResult).toEqual({ |
| 94 | data: { |
| 95 | toolSlug: sampleTool.slug, |
| 96 | params: searchParams, |
| 97 | }, |
| 98 | error: null, |
| 99 | successful: true, |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | it('should handle tools without input parameters', () => { |
| 104 | const toolWithoutParams = { ...sampleTool, inputParameters: undefined }; |
| 105 | |
| 106 | const wrappedTool = provider.wrapTool(toolWithoutParams, executeToolFn); |
| 107 | |
| 108 | expect(wrappedTool.metadata.name).toBe(toolWithoutParams.slug); |
| 109 | expect(wrappedTool.metadata.description).toBe(toolWithoutParams.description); |
| 110 | expect(wrappedTool.metadata).toHaveProperty('parameters'); |
| 111 | }); |
| 112 | |
| 113 | it('should handle tools without description', () => { |
| 114 | const toolWithoutDescription = { ...sampleTool, description: undefined }; |
| 115 | |
| 116 | const wrappedTool = provider.wrapTool(toolWithoutDescription, executeToolFn); |
| 117 | |
| 118 | expect(wrappedTool.metadata.name).toBe(toolWithoutDescription.slug); |
| 119 | expect(wrappedTool.metadata.description).toBe(toolWithoutDescription.name); // Falls back to tool name |
| 120 | expect(wrappedTool.metadata).toHaveProperty('parameters'); |
| 121 | }); |
| 122 | |
| 123 | it('should handle tools without description and name', () => { |
| 124 | const toolWithoutDescriptionAndName = { |
| 125 | ...sampleTool, |
| 126 | description: undefined, |
| 127 | name: '', // Empty name to test final fallback |
| 128 | }; |
| 129 | |
| 130 | const wrappedTool = provider.wrapTool(toolWithoutDescriptionAndName, executeToolFn); |
| 131 | |
| 132 | expect(wrappedTool.metadata.name).toBe(toolWithoutDescriptionAndName.slug); |
| 133 | expect(wrappedTool.metadata.description).toBe(''); // Final fallback to empty string |
| 134 | expect(wrappedTool.metadata).toHaveProperty('parameters'); |
| 135 | }); |
| 136 | }); |
| 137 | |
| 138 | describe('Multiple Tools Wrapping', () => { |
| 139 | it('should wrap multiple tools correctly', () => { |
| 140 | const anotherTool: Tool = { |
| 141 | ...sampleTool, |
| 142 | slug: 'WEATHER_TOOL', |
| 143 | name: 'Weather Tool', |
| 144 | description: 'Get weather information', |
| 145 | inputParameters: { |
| 146 | type: 'object', |
| 147 | properties: { |
| 148 | location: { |
| 149 | type: 'string', |
| 150 | description: 'Location to get weather for', |
| 151 | }, |
| 152 | }, |
| 153 | required: ['location'], |
| 154 | additionalProperties: false, |
| 155 | }, |
| 156 | }; |
| 157 | |
| 158 | const tools = [sampleTool, anotherTool]; |
| 159 | const wrappedTools = provider.wrapTools(tools, executeToolFn); |
| 160 | |
| 161 | expect(wrappedTools).toHaveLength(2); |
| 162 | expect(wrappedTools[0].metadata.name).toBe('SEARCH_TOOL'); |
| 163 | expect(wrappedTools[1].metadata.name).toBe('WEATHER_TOOL'); |
| 164 | expect(wrappedTools.every(tool => typeof tool.call === 'function')).toBe(true); |
| 165 | }); |
| 166 | |
| 167 | it('should handle empty tools array', () => { |
| 168 | const wrappedTools = provider.wrapTools([], executeToolFn); |
| 169 | expect(wrappedTools).toEqual([]); |
| 170 | }); |
| 171 | }); |
| 172 | |
| 173 | describe('LlamaIndex Integration', () => { |
| 174 | it('should produce tools that can be used with LlamaIndex', async () => { |
| 175 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 176 | |
| 177 | // Verify the tool has all required LlamaIndex properties |
| 178 | expect(wrappedTool).toHaveProperty('metadata'); |
| 179 | expect(wrappedTool.metadata).toHaveProperty('name'); |
| 180 | expect(wrappedTool.metadata).toHaveProperty('description'); |
| 181 | expect(wrappedTool.metadata).toHaveProperty('parameters'); |
| 182 | expect(wrappedTool).toHaveProperty('call'); |
| 183 | |
| 184 | // Test actual execution through LlamaIndex interface |
| 185 | const result = await wrappedTool.call({ query: 'test query' }); |
| 186 | expect(typeof result).toBe('string'); |
| 187 | expect(() => JSON.parse(result as string)).not.toThrow(); |
| 188 | }); |
| 189 | |
| 190 | it('should return JSONValue from call function', async () => { |
| 191 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 192 | const result = await wrappedTool.call({ query: 'test' }); |
| 193 | |
| 194 | // Should be a JSON string (JSONValue) |
| 195 | expect(typeof result).toBe('string'); |
| 196 | const parsed = JSON.parse(result as string); |
| 197 | expect(parsed).toHaveProperty('data'); |
| 198 | expect(parsed).toHaveProperty('successful'); |
| 199 | }); |
| 200 | |
| 201 | it('should handle execution errors gracefully', async () => { |
| 202 | const errorExecuteFn = vi.fn().mockRejectedValue(new Error('Tool execution failed')); |
| 203 | const wrappedTool = provider.wrapTool(sampleTool, errorExecuteFn); |
| 204 | |
| 205 | await expect(wrappedTool.call({ query: 'test' })).rejects.toThrow('Tool execution failed'); |
| 206 | }); |
| 207 | |
| 208 | it('should normalize a stringified-JSON input to an object before executing (issue #2406)', async () => { |
| 209 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 210 | |
| 211 | const result = await wrappedTool.call( |
| 212 | JSON.stringify({ query: 'test query' }) as unknown as Record<string, unknown> |
| 213 | ); |
| 214 | |
| 215 | expect(executeToolFn).toHaveBeenCalledWith(sampleTool.slug, { query: 'test query' }); |
| 216 | expect(JSON.parse(result as string).data.params).toEqual({ query: 'test query' }); |
| 217 | }); |
| 218 | |
| 219 | it('should throw a typed error for a malformed-JSON string input (issue #2406)', async () => { |
| 220 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 221 | |
| 222 | await expect( |
| 223 | wrappedTool.call('{"query":' as unknown as Record<string, unknown>) |
| 224 | ).rejects.toThrow(/not valid JSON/); |
| 225 | }); |
| 226 | }); |
| 227 | |
| 228 | describe('MCP Server Response Transformation', () => { |
| 229 | it('should transform McpUrlResponse to standard format with URL objects', () => { |
| 230 | const mcpResponse = [ |
| 231 | { name: 'server-1', url: 'https://mcp1.example.com' }, |
| 232 | { name: 'server-2', url: 'https://mcp2.example.com' }, |
| 233 | { name: 'server-3', url: 'https://mcp3.example.com' }, |
| 234 | ]; |
| 235 | |
| 236 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 237 | |
| 238 | expect(Array.isArray(result)).toBe(true); |
| 239 | expect(result).toHaveLength(3); |
| 240 | expect(result[0]).toEqual({ |
| 241 | url: new URL('https://mcp1.example.com'), |
| 242 | name: 'server-1', |
| 243 | }); |
| 244 | expect(result[1]).toEqual({ |
| 245 | url: new URL('https://mcp2.example.com'), |
| 246 | name: 'server-2', |
| 247 | }); |
| 248 | expect(result[2]).toEqual({ |
| 249 | url: new URL('https://mcp3.example.com'), |
| 250 | name: 'server-3', |
| 251 | }); |
| 252 | }); |
| 253 | |
| 254 | it('should handle empty array', () => { |
| 255 | const mcpResponse: Array<{ name: string; url: string }> = []; |
| 256 | |
| 257 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 258 | |
| 259 | expect(Array.isArray(result)).toBe(true); |
| 260 | expect(result).toHaveLength(0); |
| 261 | }); |
| 262 | |
| 263 | it('should create proper URL objects from string URLs', () => { |
| 264 | const mcpResponse = [ |
| 265 | { name: 'http-server', url: 'http://insecure.example.com' }, |
| 266 | { name: 'https-server', url: 'https://secure.example.com' }, |
| 267 | { name: 'port-server', url: 'https://example.com:8080/path' }, |
| 268 | { name: 'query-server', url: 'https://example.com?param=value' }, |
| 269 | { name: 'fragment-server', url: 'https://example.com#section' }, |
| 270 | ]; |
| 271 | |
| 272 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 273 | |
| 274 | expect(result).toHaveLength(5); |
| 275 | |
| 276 | // Verify URLs are proper URL objects |
| 277 | expect(result[0].url).toBeInstanceOf(URL); |
| 278 | expect(result[1].url).toBeInstanceOf(URL); |
| 279 | expect(result[2].url).toBeInstanceOf(URL); |
| 280 | expect(result[3].url).toBeInstanceOf(URL); |
| 281 | expect(result[4].url).toBeInstanceOf(URL); |
| 282 | |
| 283 | // Verify URL values |
| 284 | expect(result[0].url.href).toBe('http://insecure.example.com/'); |
| 285 | expect(result[1].url.href).toBe('https://secure.example.com/'); |
| 286 | expect(result[2].url.href).toBe('https://example.com:8080/path'); |
| 287 | expect(result[3].url.href).toBe('https://example.com/?param=value'); |
| 288 | expect(result[4].url.href).toBe('https://example.com/#section'); |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | describe('Edge Cases and Error Handling', () => { |
| 293 | it('should handle tools with complex input parameters', () => { |
| 294 | const complexTool: Tool = { |
| 295 | ...sampleTool, |
| 296 | slug: 'COMPLEX_TOOL', |
| 297 | inputParameters: { |
| 298 | type: 'object', |
| 299 | properties: { |
| 300 | stringParam: { type: 'string' }, |
| 301 | numberParam: { type: 'number' }, |
| 302 | booleanParam: { type: 'boolean' }, |
| 303 | arrayParam: { type: 'array', items: { type: 'string' } }, |
| 304 | objectParam: { |
| 305 | type: 'object', |
| 306 | properties: { |
| 307 | nestedString: { type: 'string' }, |
| 308 | nestedNumber: { type: 'number' }, |
| 309 | }, |
| 310 | }, |
| 311 | }, |
| 312 | required: ['stringParam', 'numberParam'], |
| 313 | }, |
| 314 | }; |
| 315 | |
| 316 | const wrappedTool = provider.wrapTool(complexTool, executeToolFn); |
| 317 | |
| 318 | expect(wrappedTool.metadata.name).toBe('COMPLEX_TOOL'); |
| 319 | expect(wrappedTool.metadata).toHaveProperty('parameters'); |
| 320 | }); |
| 321 | |
| 322 | it('should handle execution with various parameter types', async () => { |
| 323 | const wrappedTool = provider.wrapTool(sampleTool, executeToolFn); |
| 324 | const complexParams = { |
| 325 | query: 'test search', |
| 326 | options: { |
| 327 | limit: 10, |
| 328 | includeMetadata: true, |
| 329 | }, |
| 330 | tags: ['search', 'test'], |
| 331 | }; |
| 332 | |
| 333 | const result = await wrappedTool.call(complexParams); |
| 334 | const parsedResult = JSON.parse(result as string); |
| 335 | |
| 336 | expect(parsedResult.data.params).toEqual(complexParams); |
| 337 | }); |
| 338 | }); |
| 339 | }); |