1
/**
2
* E2E fixture: Custom local tools execution flow.
3
+ * Tests local execution, remote execution, mixed routing, error handling,
4
+ * and SessionContext chaining against the live Composio API.
5
+ *
6
+ * Uses weathermap toolkit (no auth needed, active connection).
7
* Requires COMPOSIO_API_KEY in environment.
8
*/
9
import { Composio } from '@composio/core';
55
},
56
});
57
58
+// Tool with strict numeric schema — for Zod validation failure test
59
+const strictTool = CustomTool({
60
+ slug: 'STRICT_TOOL',
61
+ name: 'Strict tool',
62
+ description: 'Tool with strict numeric input',
63
+ inputParams: z.object({
64
+ count: z.number(),
65
+ }),
66
+ execute: async (input) => {
67
+ return { doubled: input.count * 2 };
68
+ },
69
+});
70
+
71
+// Tool that chains into a remote tool via SessionContext.execute()
72
+const weatherChain = CustomTool({
73
+ slug: 'WEATHER_CHAIN',
74
+ name: 'Weather chain',
75
+ description: 'Fetches weather via remote tool and enriches with local context',
76
+ inputParams: z.object({
77
+ city: z.string(),
78
+ }),
79
+ execute: async (input, session) => {
80
+ // Call remote WEATHERMAP_WEATHER tool through SessionContext
81
+ const weather = await session.execute('WEATHERMAP_WEATHER', {
82
+ location: input.city,
83
+ });
84
+ return {
85
+ city: input.city,
86
+ userId: session.userId,
87
+ weatherData: weather.data,
88
+ weatherError: weather.error,
89
+ };
90
+ },
91
+});
92
+
93
// ── Create session and run tests ─────────────────────────────
94
95
const composio = new Composio({ apiKey });
98
const userId = `e2e-custom-tools-${Date.now()}`;
99
100
const session = await composio.create(userId, {
62
- toolkits: ['hackernews'],
101
+ toolkits: ['weathermap'],
102
manageConnections: false,
64
- customTools: [getUserContext, enrichedSearch, throwingTool],
103
+ customTools: [getUserContext, enrichedSearch, throwingTool, strictTool, weatherChain],
104
});
105
106
// ── Test 1: Single local tool execution ──
124
console.log('ZOD_DEFAULTS_OK');
125
}
126
88
- // ── Test 3: Error handling ──
127
+ // ── Test 3: Error handling — throw wrapped into { data, error } ──
128
{
129
const result = await session.execute('THROWING_TOOL', {});
130
if (!result.error || !result.error.includes('intentional error')) {
131
throw new Error(`ERROR_HANDLING failed: ${JSON.stringify(result)}`);
132
}
133
+ // Verify it didn't crash — we got a structured response
134
+ if (result.data === undefined) {
135
+ throw new Error(`ERROR_HANDLING no data field: ${JSON.stringify(result)}`);
136
+ }
137
console.log('ERROR_HANDLING_OK');
138
}
139
97
- // ── Test 4: Multiple local tools ──
140
+ // ── Test 4: Zod validation failure — wrong type wrapped into error ──
141
+ {
142
+ const result = await session.execute('STRICT_TOOL', { count: 'not-a-number' });
143
+ if (!result.error) {
144
+ throw new Error(`ZOD_VALIDATION_FAIL expected error: ${JSON.stringify(result)}`);
145
+ }
146
+ if (!result.error.toLowerCase().includes('validation')) {
147
+ throw new Error(`ZOD_VALIDATION_FAIL expected validation error: ${result.error}`);
148
+ }
149
+ console.log('ZOD_VALIDATION_FAIL_OK');
150
+ }
151
+
152
+ // ── Test 5: Multiple local tools route correctly ──
153
{
154
const r1 = await session.execute('GET_USER_CONTEXT', { category: 'a' });
155
const r2 = await session.execute('ENRICHED_SEARCH', { query: 'test' });
162
console.log('MULTIPLE_TOOLS_OK');
163
}
164
110
- // ── Test 5: Session context injection ──
165
+ // ── Test 6: Session context injection (userId) ──
166
{
167
const result = await session.execute('ENRICHED_SEARCH', { query: 'context-test' });
168
if (!result.data?.userId) {
174
console.log('SESSION_CONTEXT_OK');
175
}
176
122
- // ── Test 6: Case-insensitive slug ──
177
+ // ── Test 7: Case-insensitive slug ──
178
{
179
const result = await session.execute('get_user_context', { category: 'case-test' });
180
if (result.data?.preferences?.category !== 'case-test') {
183
console.log('CASE_INSENSITIVE_OK');
184
}
185
131
- // ── Test 7: Prefixed slug (LOCAL_) ──
186
+ // ── Test 8: Prefixed slug (LOCAL_) ──
187
{
188
const result = await session.execute('LOCAL_GET_USER_CONTEXT', { category: 'prefix-test' });
189
if (result.data?.preferences?.category !== 'prefix-test') {
192
console.log('PREFIXED_SLUG_OK');
193
}
194
140
- // ── Test 8: localTools() method ──
141
- // localTools() returns a wrapped tool — we just verify it doesn't throw
142
- // and returns something non-empty (can't fully test without LLM)
195
+ // ── Test 9: SessionContext.execute() chaining — local tool calls remote tool ──
196
+ // This is mixed local+remote execution: local tool runs in-process,
197
+ // then calls WEATHERMAP_WEATHER on the backend via session.execute()
198
+ {
199
+ const result = await session.execute('WEATHER_CHAIN', { city: 'London' });
200
+ if (result.error) {
201
+ throw new Error(`CHAINED_EXECUTE failed: ${result.error}`);
202
+ }
203
+ if (result.data?.city !== 'London') {
204
+ throw new Error(`CHAINED_EXECUTE city mismatch: ${JSON.stringify(result)}`);
205
+ }
206
+ if (!result.data?.userId) {
207
+ throw new Error(`CHAINED_EXECUTE missing userId: ${JSON.stringify(result)}`);
208
+ }
209
+ // The chained remote call should have returned weather data
210
+ if (!result.data?.weatherData) {
211
+ throw new Error(`CHAINED_EXECUTE missing weatherData: ${JSON.stringify(result)}`);
212
+ }
213
+ console.log('CHAINED_EXECUTE_OK');
214
+ }
215
+
216
+ // ── Test 10: Non-existent tool returns error gracefully ──
217
+ // Remote tools that don't exist throw from the backend (400).
218
+ // Verify the SDK surfaces this as a catchable error, not a silent failure.
219
+ {
220
+ let caught = false;
221
+ try {
222
+ await session.execute('COMPLETELY_FAKE_TOOL_12345', {});
223
+ } catch (err) {
224
+ caught = true;
225
+ if (!err.message?.includes('not found') && !err.message?.includes('400')) {
226
+ throw new Error(`NONEXISTENT_TOOL unexpected error: ${err.message}`);
227
+ }
228
+ }
229
+ if (!caught) {
230
+ throw new Error('NONEXISTENT_TOOL expected an error but got success');
231
+ }
232
+ console.log('NONEXISTENT_TOOL_OK');
233
+ }
234
+
235
+ // ── Test 11: localTools() method ──
236
{
237
try {
238
const localTool = await session.localTools();
146
- // Verify it returned something (provider-specific format)
239
if (!localTool) {
240
throw new Error('localTools() returned falsy value');
241
}
242
console.log('LOCAL_TOOLS_METHOD_OK');
243
} catch (err) {
152
- // If provider doesn't support it, that's OK for this test
244
+ // Expected — no provider configured in this fixture
245
if (err.message?.includes('provider')) {
154
- console.log('LOCAL_TOOLS_METHOD_OK'); // Expected if no provider
246
+ console.log('LOCAL_TOOLS_METHOD_OK');
247
} else {
248
throw err;
249
}
250
}
251
}
252
161
- // ── Test 9: session.tools() wrapping ──
162
- // Verify session.tools() returns wrapped tools and doesn't crash
253
+ // ── Test 12: session.tools() wrapping ──
254
{
255
try {
256
const tools = await session.tools();
259
}
260
console.log('TOOLS_WRAPPING_OK');
261
} catch (err) {
171
- // If provider doesn't support it, that's OK
262
+ // Expected — no provider configured, standard path used
263
if (err.message?.includes('provider')) {
264
console.log('TOOLS_WRAPPING_OK');
265
} else {