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 | import { describe, it, expect, vi } from 'vitest'; |
| 2 | import ComposioClient, { Auth, createFetchTransport } from '@composio/client'; |
| 3 | import { Tools } from '../../src/models/Tools'; |
| 4 | import { ToolRouterSession } from '../../src/models/ToolRouterSession'; |
| 5 | import { SessionContextImpl } from '../../src/models/SessionContext'; |
| 6 | import { MockProvider } from '../utils/mocks/provider.mock'; |
| 7 | |
| 8 | vi.mock('../../src/telemetry/Telemetry', () => ({ |
| 9 | telemetry: { instrument: vi.fn() }, |
| 10 | })); |
| 11 | |
| 12 | // A session hook can block a call before it runs, or block its result before |
| 13 | // the agent sees it. The API answers with `result_type: "blocked"`, which has |
| 14 | // no data and no HTTP response. These tests run a real client against a |
| 15 | // stubbed `fetch` so they cover the wire shape the SDK reads. |
| 16 | |
| 17 | const SESSION_ID = 'sess_123'; |
| 18 | |
| 19 | const blockedCall = { |
| 20 | result_type: 'blocked', |
| 21 | reason: { type: 'request_blocked', hook_id: 'no_external_email' }, |
| 22 | message: 'Blocked by session policy: No external email', |
| 23 | log_id: '', |
| 24 | hook_results: [{ id: 'no_external_email', status: 'evaluated', outcome: 'block' }], |
| 25 | }; |
| 26 | |
| 27 | const blockedResponse = { |
| 28 | result_type: 'blocked', |
| 29 | reason: { type: 'response_blocked', hook_id: 'no_outside_data' }, |
| 30 | message: 'Blocked by session policy', |
| 31 | log_id: 'log_456', |
| 32 | instant_charge: { amount: '0.01', currency: 'USD', charged_by: 'composio' }, |
| 33 | hook_results: [{ id: 'no_outside_data', status: 'evaluated', outcome: 'block' }], |
| 34 | }; |
| 35 | |
| 36 | const { log_id: _logId, ...blockedProxy } = blockedCall; |
| 37 | |
| 38 | const createClient = (body: unknown) => { |
| 39 | const fetch = vi.fn<typeof globalThis.fetch>( |
| 40 | async () => |
| 41 | new Response(JSON.stringify(body), { |
| 42 | status: 200, |
| 43 | headers: { 'content-type': 'application/json' }, |
| 44 | }) |
| 45 | ); |
| 46 | const client = new ComposioClient({ |
| 47 | auth: Auth.project('test-api-key'), |
| 48 | baseURL: 'https://backend.invalid', |
| 49 | transport: createFetchTransport({ fetch }), |
| 50 | }); |
| 51 | return { client, fetch }; |
| 52 | }; |
| 53 | |
| 54 | const createSession = (client: ComposioClient) => |
| 55 | new ToolRouterSession(client, { apiKey: 'key', provider: new MockProvider() }, SESSION_ID, { |
| 56 | type: 'http' as const, |
| 57 | url: `https://backend.invalid/api/v3/tool_router/session/${SESSION_ID}`, |
| 58 | }); |
| 59 | |
| 60 | const proxyParams = { |
| 61 | toolkit: 'gmail', |
| 62 | endpoint: '/gmail/v1/users/me/messages/send', |
| 63 | method: 'POST' as const, |
| 64 | body: { raw: 'VG86IGNlb0ByaXZhbC5jb20' }, |
| 65 | }; |
| 66 | |
| 67 | describe('session execute when a hook blocks the call', () => { |
| 68 | it.each([ |
| 69 | { |
| 70 | name: 'session.execute', |
| 71 | run: (client: ComposioClient) => |
| 72 | createSession(client).execute('GMAIL_SEND_EMAIL', { to: 'ceo@rival.com' }), |
| 73 | }, |
| 74 | { |
| 75 | name: 'custom tool context execute', |
| 76 | run: (client: ComposioClient) => |
| 77 | new SessionContextImpl(client, 'test-user', SESSION_ID).execute('GMAIL_SEND_EMAIL', { |
| 78 | to: 'ceo@rival.com', |
| 79 | }), |
| 80 | }, |
| 81 | ])('$name returns resultType blocked with the message in error', async ({ run }) => { |
| 82 | const { client } = createClient(blockedCall); |
| 83 | |
| 84 | await expect(run(client)).resolves.toEqual({ |
| 85 | data: {}, |
| 86 | error: 'Blocked by session policy: No external email', |
| 87 | logId: '', |
| 88 | resultType: 'blocked', |
| 89 | blockReason: { type: 'request_blocked', hookId: 'no_external_email' }, |
| 90 | hookResults: [{ id: 'no_external_email', status: 'evaluated', outcome: 'block' }], |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | it('keeps the log id and the charge when the tool ran and its result was blocked', async () => { |
| 95 | const { client } = createClient(blockedResponse); |
| 96 | |
| 97 | await expect(createSession(client).execute('GMAIL_FETCH_EMAILS', {})).resolves.toEqual({ |
| 98 | data: {}, |
| 99 | error: 'Blocked by session policy', |
| 100 | logId: 'log_456', |
| 101 | resultType: 'blocked', |
| 102 | blockReason: { type: 'response_blocked', hookId: 'no_outside_data' }, |
| 103 | hookResults: [{ id: 'no_outside_data', status: 'evaluated', outcome: 'block' }], |
| 104 | instantCharge: { amount: '0.01', currency: 'USD', charged_by: 'composio' }, |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | // Every provider hands the agent `{ data, error, successful }`, so this is |
| 109 | // what the agent sees in any framework. |
| 110 | it('reaches the agent as a failed call with the hook message', async () => { |
| 111 | const { client } = createClient(blockedCall); |
| 112 | |
| 113 | const result = await new Tools(client, { provider: new MockProvider() }).executeSessionTool( |
| 114 | 'GMAIL_SEND_EMAIL', |
| 115 | { sessionId: SESSION_ID, arguments: { to: 'ceo@rival.com' } } |
| 116 | ); |
| 117 | |
| 118 | expect(result).toEqual({ |
| 119 | data: {}, |
| 120 | error: 'Blocked by session policy: No external email', |
| 121 | successful: false, |
| 122 | logId: '', |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | it('returns the hook results of a call that ran', async () => { |
| 127 | const { client } = createClient({ |
| 128 | result_type: 'completed', |
| 129 | data: { id: 'msg_1' }, |
| 130 | error: null, |
| 131 | log_id: 'log_1', |
| 132 | hook_results: [ |
| 133 | { id: 'no_external_email', status: 'evaluated', outcome: 'continue' }, |
| 134 | { id: 'block_injection', status: 'unavailable' }, |
| 135 | ], |
| 136 | }); |
| 137 | |
| 138 | // A hook that could not be evaluated does not block: the call still ran. |
| 139 | await expect(createSession(client).execute('GMAIL_SEND_EMAIL', {})).resolves.toEqual({ |
| 140 | data: { id: 'msg_1' }, |
| 141 | error: null, |
| 142 | logId: 'log_1', |
| 143 | resultType: 'completed', |
| 144 | hookResults: [ |
| 145 | { id: 'no_external_email', status: 'evaluated', outcome: 'continue' }, |
| 146 | { id: 'block_injection', status: 'unavailable' }, |
| 147 | ], |
| 148 | }); |
| 149 | }); |
| 150 | |
| 151 | it('leaves hookResults out of a blocked call that reports none', async () => { |
| 152 | const { hook_results: _hookResults, ...withoutHookResults } = blockedCall; |
| 153 | const { client } = createClient(withoutHookResults); |
| 154 | |
| 155 | await expect(createSession(client).execute('GMAIL_SEND_EMAIL', {})).resolves.not.toHaveProperty( |
| 156 | 'hookResults' |
| 157 | ); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | describe('session proxy execute when a hook blocks the request', () => { |
| 162 | it.each([ |
| 163 | { |
| 164 | name: 'session.proxyExecute', |
| 165 | run: (client: ComposioClient) => createSession(client).proxyExecute(proxyParams), |
| 166 | }, |
| 167 | { |
| 168 | name: 'custom tool context proxyExecute', |
| 169 | run: (client: ComposioClient) => |
| 170 | new SessionContextImpl(client, 'test-user', SESSION_ID).proxyExecute(proxyParams), |
| 171 | }, |
| 172 | ])('$name returns resultType blocked with no HTTP response', async ({ run }) => { |
| 173 | const { client } = createClient(blockedProxy); |
| 174 | |
| 175 | await expect(run(client)).resolves.toEqual({ |
| 176 | resultType: 'blocked', |
| 177 | blockReason: { type: 'request_blocked', hookId: 'no_external_email' }, |
| 178 | message: 'Blocked by session policy: No external email', |
| 179 | hookResults: [{ id: 'no_external_email', status: 'evaluated', outcome: 'block' }], |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | it('marks a request that was sent as completed', async () => { |
| 184 | const { client } = createClient({ |
| 185 | result_type: 'completed', |
| 186 | status: 200, |
| 187 | data: { id: 'msg_1' }, |
| 188 | headers: { 'content-type': 'application/json' }, |
| 189 | }); |
| 190 | |
| 191 | await expect(createSession(client).proxyExecute(proxyParams)).resolves.toEqual({ |
| 192 | resultType: 'completed', |
| 193 | status: 200, |
| 194 | data: { id: 'msg_1' }, |
| 195 | headers: { 'content-type': 'application/json' }, |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | it('returns the hook results of a request that was sent', async () => { |
| 200 | const hookResults = [{ id: 'no_external_email', status: 'evaluated', outcome: 'continue' }]; |
| 201 | const { client } = createClient({ |
| 202 | result_type: 'completed', |
| 203 | status: 200, |
| 204 | data: { id: 'msg_1' }, |
| 205 | hook_results: hookResults, |
| 206 | }); |
| 207 | |
| 208 | await expect(createSession(client).proxyExecute(proxyParams)).resolves.toMatchObject({ |
| 209 | resultType: 'completed', |
| 210 | hookResults, |
| 211 | }); |
| 212 | }); |
| 213 | }); |