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 260 lines 8.26 KB
Raw
1 import { describe, it, expect } from 'vitest';
2 import {
3 transformSearchResponse,
4 transformExecuteResponse,
5 } from '../../src/utils/transformers/toolRouterResponseTransform';
6 import { ToolRouterSessionSearchResponseSchema } from '../../src/types/toolRouter.types';
7
8 describe('toolRouterResponseTransform', () => {
9 describe('transformSearchResponse', () => {
10 it('should transform snake_case search response to camelCase', () => {
11 const raw = {
12 success: true,
13 error: null,
14 results: [
15 {
16 index: 1,
17 use_case: 'send emails',
18 primary_tool_slugs: ['GMAIL_SEND_EMAIL'],
19 related_tool_slugs: ['GMAIL_CREATE_DRAFT'],
20 toolkits: ['gmail'],
21 difficulty: 'easy',
22 recommended_plan_steps: ['Step 1', 'Step 2'],
23 },
24 ],
25 tool_schemas: {
26 GMAIL_SEND_EMAIL: {
27 tool_slug: 'GMAIL_SEND_EMAIL',
28 toolkit: 'gmail',
29 description: 'Send an email',
30 hasFullSchema: true,
31 input_schema: { to: { type: 'string' } },
32 output_schema: { id: { type: 'string' } },
33 },
34 },
35 toolkit_connection_statuses: [
36 {
37 toolkit: 'gmail',
38 description: 'Gmail toolkit',
39 has_active_connection: true,
40 status_message: 'Connected',
41 },
42 ],
43 next_steps_guidance: ['Connect Gmail if needed'],
44 session: {
45 id: 'trs_123',
46 generate_id: false,
47 instructions: 'Reuse this session id',
48 },
49 time_info: {
50 current_time_utc: '2025-03-09T12:00:00.000Z',
51 current_time_utc_epoch_seconds: 1741521600,
52 message: 'UTC time',
53 },
54 };
55
56 const result = transformSearchResponse(raw);
57
58 expect(result.success).toBe(true);
59 expect(result.error).toBeNull();
60 expect(result.results).toHaveLength(1);
61 expect(result.results[0]).toEqual({
62 index: 1,
63 useCase: 'send emails',
64 primaryToolSlugs: ['GMAIL_SEND_EMAIL'],
65 relatedToolSlugs: ['GMAIL_CREATE_DRAFT'],
66 toolkits: ['gmail'],
67 difficulty: 'easy',
68 recommendedPlanSteps: ['Step 1', 'Step 2'],
69 });
70 expect(result.toolSchemas).toHaveProperty('GMAIL_SEND_EMAIL');
71 expect(result.toolSchemas.GMAIL_SEND_EMAIL).toEqual({
72 toolSlug: 'GMAIL_SEND_EMAIL',
73 toolkit: 'gmail',
74 description: 'Send an email',
75 hasFullSchema: true,
76 inputSchema: { to: { type: 'string' } },
77 outputSchema: { id: { type: 'string' } },
78 });
79 expect(result.toolkitConnectionStatuses[0]).toEqual({
80 toolkit: 'gmail',
81 description: 'Gmail toolkit',
82 hasActiveConnection: true,
83 statusMessage: 'Connected',
84 });
85 expect(result.nextStepsGuidance).toEqual(['Connect Gmail if needed']);
86 expect(result.session).toEqual({
87 id: 'trs_123',
88 generateId: false,
89 instructions: 'Reuse this session id',
90 });
91 expect(result.timeInfo).toEqual({
92 currentTimeUtc: '2025-03-09T12:00:00.000Z',
93 currentTimeUtcEpochSeconds: 1741521600,
94 message: 'UTC time',
95 });
96 });
97
98 it('should transform schemaRef with tool_slugs to toolSlugs', () => {
99 const raw = {
100 success: true,
101 error: null,
102 results: [],
103 tool_schemas: {
104 TOOL_X: {
105 tool_slug: 'TOOL_X',
106 toolkit: 'x',
107 schemaRef: {
108 args: { tool_slugs: ['TOOL_X'] },
109 message: 'Fetch schema',
110 tool: 'COMPOSIO_GET_TOOL_SCHEMAS',
111 },
112 },
113 },
114 toolkit_connection_statuses: [],
115 next_steps_guidance: [],
116 session: {
117 id: 'trs_1',
118 generate_id: true,
119 instructions: 'Use session',
120 },
121 time_info: {
122 current_time_utc: '2025-03-09T12:00:00.000Z',
123 current_time_utc_epoch_seconds: 1741521600,
124 message: 'UTC',
125 },
126 };
127
128 const result = transformSearchResponse(raw);
129 expect(result.toolSchemas.TOOL_X.schemaRef).toEqual({
130 args: { toolSlugs: ['TOOL_X'] },
131 message: 'Fetch schema',
132 tool: 'COMPOSIO_GET_TOOL_SCHEMAS',
133 });
134 });
135
136 it('maps the Instant account allowlist only when the API sends one', () => {
137 const raw = {
138 success: true,
139 error: null,
140 results: [],
141 tool_schemas: {},
142 toolkit_connection_statuses: [
143 {
144 toolkit: 'exa',
145 description: 'Exa',
146 has_active_connection: true,
147 status_message: 'Connected via the Composio Instant account.',
148 instant_account: { allowed_tool_slugs: ['EXA_SEARCH'] },
149 },
150 {
151 toolkit: 'gmail',
152 description: 'Gmail',
153 has_active_connection: false,
154 status_message: 'No connection',
155 },
156 ],
157 next_steps_guidance: [],
158 session: {
159 id: 'trs_1',
160 generate_id: false,
161 instructions: 'Use session',
162 },
163 time_info: {
164 current_time_utc: '2025-03-09T12:00:00.000Z',
165 current_time_utc_epoch_seconds: 1741521600,
166 message: 'UTC',
167 },
168 };
169
170 const [instantAccount, notInstantAccount] =
171 transformSearchResponse(raw).toolkitConnectionStatuses;
172 expect(instantAccount.instantAccount).toEqual({ allowedToolSlugs: ['EXA_SEARCH'] });
173 expect(notInstantAccount).not.toHaveProperty('instantAccount');
174 });
175
176 it('keeps Instant tool prices keyed by the original tool slug', () => {
177 const prices = { EXA_SEARCH: { description: '$0.01 per call', discount: null } };
178 const raw = {
179 success: true,
180 error: null,
181 results: [],
182 tool_schemas: {},
183 toolkit_connection_statuses: [
184 {
185 toolkit: 'exa',
186 description: 'Exa',
187 has_active_connection: true,
188 status_message: 'Connected via the Composio Instant account.',
189 instant_account: { allowed_tool_slugs: ['EXA_SEARCH'], prices },
190 },
191 ],
192 next_steps_guidance: [],
193 session: { id: 'trs_1', generate_id: false, instructions: 'Use session' },
194 time_info: {
195 current_time_utc: '2025-03-09T12:00:00.000Z',
196 current_time_utc_epoch_seconds: 1741521600,
197 message: 'UTC',
198 },
199 };
200
201 const parsed = ToolRouterSessionSearchResponseSchema.parse(transformSearchResponse(raw));
202 expect(parsed.toolkitConnectionStatuses[0].instantAccount).toEqual({
203 allowedToolSlugs: ['EXA_SEARCH'],
204 prices,
205 });
206 });
207 });
208
209 describe('transformExecuteResponse', () => {
210 it('should transform snake_case execute response to camelCase', () => {
211 const raw = {
212 result_type: 'completed' as const,
213 data: { tool_slug: 'GMAIL_SEND_EMAIL', id: 'msg_123' },
214 error: null,
215 log_id: 'log_abc',
216 };
217
218 const result = transformExecuteResponse(raw, 'GMAIL_SEND_EMAIL');
219
220 expect(result.data).toEqual({ tool_slug: 'GMAIL_SEND_EMAIL', id: 'msg_123' });
221 expect(result.error).toBeNull();
222 expect(result.logId).toBe('log_abc');
223 });
224
225 it('should preserve error in execute response', () => {
226 const raw = {
227 result_type: 'failed' as const,
228 data: {},
229 error: 'Connection not found',
230 log_id: 'log_err',
231 };
232
233 const result = transformExecuteResponse(raw, 'GMAIL_SEND_EMAIL');
234
235 expect(result.error).toBe('Connection not found');
236 expect(result.logId).toBe('log_err');
237 });
238
239 it('preserves the optional Instant charge without inventing one', () => {
240 expect(
241 transformExecuteResponse(
242 {
243 result_type: 'completed',
244 data: {},
245 error: null,
246 log_id: 'log_paid',
247 instant_charge: { amount: '0.01', currency: 'USD', charged_by: 'composio' },
248 },
249 'EXA_SEARCH'
250 ).instantCharge
251 ).toEqual({ amount: '0.01', currency: 'USD', charged_by: 'composio' });
252 expect(
253 transformExecuteResponse(
254 { result_type: 'completed', data: {}, error: null, log_id: 'log_free' },
255 'EXA_SEARCH'
256 )
257 ).not.toHaveProperty('instantCharge');
258 });
259 });
260 });