Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 208 lines 6.94 KB
Raw
1 import { describe, it, expect, beforeEach, vi } from 'vitest';
2 import { LangchainProvider } from '../src';
3 import { Tool } from '@composio/core';
4 import { DynamicStructuredTool } from '@langchain/core/tools';
5 import { convertToOpenAITool } from '@langchain/core/utils/function_calling';
6
7 describe('LangchainProvider', () => {
8 let provider: LangchainProvider;
9 let sampleTool: Tool;
10 let executeToolFn: (toolSlug: string, params: Record<string, unknown>) => Promise<unknown>;
11
12 beforeEach(() => {
13 provider = new LangchainProvider();
14
15 // Create a real execute function that simulates tool execution
16 executeToolFn = vi
17 .fn()
18 .mockImplementation(async (toolSlug: string, params: Record<string, unknown>) => {
19 // Simulate actual tool execution with realistic response
20 return {
21 data: { toolSlug, params },
22 error: null,
23 successful: true,
24 };
25 });
26
27 // Create a real sample tool that matches actual tool structure
28 sampleTool = {
29 slug: 'SEARCH_TOOL',
30 name: 'Search Tool',
31 description: 'Search for information in the knowledge base',
32 version: '20250909_00',
33 availableVersions: ['20250909_00', '20250901_00'],
34 inputParameters: {
35 type: 'object',
36 properties: {
37 query: {
38 type: 'string',
39 description: 'The search query',
40 },
41 },
42 required: ['query'],
43 additionalProperties: false,
44 },
45 toolkit: {
46 slug: 'search_toolkit',
47 name: 'Search Toolkit',
48 },
49 tags: ['search', 'knowledge'],
50 };
51 });
52
53 describe('Provider Configuration', () => {
54 it('should be properly initialized with correct name', () => {
55 expect(provider.name).toBe('langchain');
56 });
57
58 it('should be marked as agentic', () => {
59 expect(provider._isAgentic).toBe(true);
60 });
61 });
62
63 describe('Tool Wrapping', () => {
64 it('should successfully wrap a valid tool into LangChain format', () => {
65 const wrappedTool = provider.wrapTool(sampleTool, executeToolFn);
66
67 // Verify the wrapped tool has the correct structure
68 expect(wrappedTool).toBeInstanceOf(DynamicStructuredTool);
69 expect(wrappedTool.name).toBe(sampleTool.slug);
70 expect(wrappedTool.description).toBe(sampleTool.description);
71 expect(typeof wrappedTool.func).toBe('function');
72 });
73
74 it('should wrap a tool that can be executed with parameters', async () => {
75 const wrappedTool = provider.wrapTool(sampleTool, executeToolFn);
76 const searchParams = { query: 'test search' };
77
78 const result = await wrappedTool.func(searchParams);
79 const parsedResult = JSON.parse(result as string);
80
81 expect(parsedResult).toEqual({
82 data: {
83 toolSlug: sampleTool.slug,
84 params: searchParams,
85 },
86 error: null,
87 successful: true,
88 });
89 });
90
91 it('should fail to wrap a tool without toolkit', () => {
92 const invalidTool = { ...sampleTool, toolkit: undefined };
93
94 expect(() => {
95 provider.wrapTool(invalidTool, executeToolFn);
96 }).toThrow('App name is not defined');
97 });
98
99 it('should fail to wrap a tool without input parameters', () => {
100 const invalidTool = { ...sampleTool, inputParameters: undefined };
101
102 expect(() => {
103 provider.wrapTool(invalidTool, executeToolFn);
104 }).toThrow('Tool input parameters are not defined');
105 });
106 });
107
108 describe('Multiple Tools Wrapping', () => {
109 it('should wrap multiple tools correctly', () => {
110 const anotherTool: Tool = {
111 ...sampleTool,
112 slug: 'WEATHER_TOOL',
113 name: 'Weather Tool',
114 description: 'Get weather information',
115 inputParameters: {
116 type: 'object',
117 properties: {
118 location: {
119 type: 'string',
120 description: 'Location to get weather for',
121 },
122 },
123 required: ['location'],
124 additionalProperties: false,
125 },
126 };
127
128 const tools = [sampleTool, anotherTool];
129 const wrappedTools = provider.wrapTools(tools, executeToolFn);
130
131 expect(wrappedTools).toHaveLength(2);
132 expect(wrappedTools[0].name).toBe('SEARCH_TOOL');
133 expect(wrappedTools[1].name).toBe('WEATHER_TOOL');
134 expect(wrappedTools.every(tool => tool instanceof DynamicStructuredTool)).toBe(true);
135 });
136
137 it('should handle empty tools array', () => {
138 const wrappedTools = provider.wrapTools([], executeToolFn);
139 expect(wrappedTools).toEqual([]);
140 });
141 });
142
143 describe('LangChain Integration', () => {
144 it('should produce tools that can be used with LangChain', async () => {
145 const wrappedTool = provider.wrapTool(sampleTool, executeToolFn);
146
147 // Verify the tool has all required LangChain properties
148 expect(wrappedTool).toHaveProperty('name');
149 expect(wrappedTool).toHaveProperty('description');
150 expect(wrappedTool).toHaveProperty('schema');
151 expect(wrappedTool).toHaveProperty('func');
152
153 // Test actual execution through LangChain interface
154 const result = await wrappedTool.func({ query: 'test query' });
155 expect(typeof result).toBe('string');
156 expect(() => JSON.parse(result as string)).not.toThrow();
157 });
158
159 it('should serialize tool parameters with an object root when a parameter uses anyOf', () => {
160 const wrappedTool = provider.wrapTool(
161 {
162 ...sampleTool,
163 inputParameters: {
164 type: 'object',
165 properties: {
166 query: { type: 'string' },
167 limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] },
168 },
169 required: ['query'],
170 },
171 },
172 executeToolFn
173 );
174
175 const parameters = convertToOpenAITool(wrappedTool).function.parameters;
176
177 expect(parameters).toMatchObject({
178 type: 'object',
179 properties: {
180 query: { type: 'string' },
181 limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] },
182 },
183 required: ['query'],
184 });
185 expect(parameters).not.toHaveProperty('allOf');
186 });
187
188 it('should normalize a stringified-JSON input to an object before executing (issue #2406)', async () => {
189 const wrappedTool = provider.wrapTool(sampleTool, executeToolFn);
190
191 const result = await wrappedTool.func(
192 JSON.stringify({ query: 'test query' }) as unknown as Record<string, unknown>
193 );
194
195 // The mock echoes back the params it received; confirm it saw a parsed object.
196 expect(executeToolFn).toHaveBeenCalledWith(sampleTool.slug, { query: 'test query' });
197 expect(JSON.parse(result as string).data.params).toEqual({ query: 'test query' });
198 });
199
200 it('should throw a typed error for a malformed-JSON string input (issue #2406)', async () => {
201 const wrappedTool = provider.wrapTool(sampleTool, executeToolFn);
202
203 await expect(
204 wrappedTool.func('{"query":' as unknown as Record<string, unknown>)
205 ).rejects.toThrow(/not valid JSON/);
206 });
207 });
208 });