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, expect, test } from 'bun:test'; |
| 2 | import { |
| 3 | ComposioToolInputRequiredError, |
| 4 | type ToolRouterInputRequest, |
| 5 | type ToolRouterInputResponse, |
| 6 | } from '@composio/core'; |
| 7 | import { askForApproval, runWithApprovals } from '../src/approval'; |
| 8 | import { assertBatch } from '../src/verification'; |
| 9 | |
| 10 | const question: ToolRouterInputRequest = { |
| 11 | type: 'elicitation', |
| 12 | mode: 'form', |
| 13 | message: 'Allow this read-only call?', |
| 14 | requestedSchema: { |
| 15 | type: 'object', |
| 16 | properties: { |
| 17 | decision: { |
| 18 | type: 'string', |
| 19 | oneOf: [{ const: 'approve' }, { const: 'always_approve' }, { const: 'deny' }], |
| 20 | }, |
| 21 | }, |
| 22 | required: ['decision'], |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | describe('terminal approvals', () => { |
| 27 | test.each(['approve', 'always_approve', 'deny', 'decline', 'cancel'])( |
| 28 | 'forwards the person’s %s choice', |
| 29 | async choice => { |
| 30 | const response = await askForApproval( |
| 31 | question, |
| 32 | async () => choice, |
| 33 | () => {} |
| 34 | ); |
| 35 | expect(response).toEqual( |
| 36 | choice === 'decline' || choice === 'cancel' |
| 37 | ? { action: choice } |
| 38 | : { action: 'accept', content: { decision: choice } } |
| 39 | ); |
| 40 | } |
| 41 | ); |
| 42 | |
| 43 | test('rejects an invalid choice and asks again', async () => { |
| 44 | const answers = ['yes', 'approve']; |
| 45 | let reads = 0; |
| 46 | expect( |
| 47 | await askForApproval( |
| 48 | question, |
| 49 | async () => answers[reads++], |
| 50 | () => {} |
| 51 | ) |
| 52 | ).toEqual({ action: 'accept', content: { decision: 'approve' } }); |
| 53 | expect(reads).toBe(2); |
| 54 | }); |
| 55 | |
| 56 | test('uses the schema choices instead of adding Always approve', async () => { |
| 57 | const answers = ['always_approve', 'deny']; |
| 58 | const restricted = { |
| 59 | ...question, |
| 60 | requestedSchema: { |
| 61 | type: 'object', |
| 62 | properties: { decision: { type: 'string', enum: ['approve', 'deny'] } }, |
| 63 | }, |
| 64 | }; |
| 65 | let reads = 0; |
| 66 | expect( |
| 67 | await askForApproval( |
| 68 | restricted, |
| 69 | async () => answers[reads++], |
| 70 | () => {} |
| 71 | ) |
| 72 | ).toEqual({ action: 'accept', content: { decision: 'deny' } }); |
| 73 | expect(reads).toBe(2); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | test('waits for consent before resuming the call', async () => { |
| 78 | let release!: (answer: ToolRouterInputResponse) => void; |
| 79 | const consent = new Promise<ToolRouterInputResponse>(resolve => { |
| 80 | release = resolve; |
| 81 | }); |
| 82 | let calls = 0; |
| 83 | const pending = runWithApprovals( |
| 84 | async options => { |
| 85 | calls++; |
| 86 | if (!options.inputResponses) |
| 87 | throw new ComposioToolInputRequiredError('Read', { |
| 88 | inputRequests: { random_id: question }, |
| 89 | requestState: 'opaque-token', |
| 90 | }); |
| 91 | expect(options).toEqual({ |
| 92 | inputResponses: { random_id: { action: 'cancel' } }, |
| 93 | requestState: 'opaque-token', |
| 94 | }); |
| 95 | return 'cancelled'; |
| 96 | }, |
| 97 | () => consent |
| 98 | ); |
| 99 | await Promise.resolve(); |
| 100 | await Promise.resolve(); |
| 101 | expect(calls).toBe(1); |
| 102 | release({ action: 'cancel' }); |
| 103 | expect(await pending).toBe('cancelled'); |
| 104 | expect(calls).toBe(2); |
| 105 | }); |
| 106 | |
| 107 | test('answers all batch questions and replaces continuation state on another round', async () => { |
| 108 | const responses: ToolRouterInputResponse[] = [ |
| 109 | { action: 'accept', content: { decision: 'approve' } }, |
| 110 | { action: 'decline' }, |
| 111 | { action: 'cancel' }, |
| 112 | ]; |
| 113 | let calls = 0; |
| 114 | let answerIndex = 0; |
| 115 | const result = await runWithApprovals( |
| 116 | async options => { |
| 117 | calls++; |
| 118 | if (calls === 1) |
| 119 | throw new ComposioToolInputRequiredError('Batch', { |
| 120 | inputRequests: { first: question, second: question }, |
| 121 | requestState: 'first-state', |
| 122 | }); |
| 123 | if (calls === 2) { |
| 124 | expect(options).toEqual({ |
| 125 | inputResponses: { first: responses[0], second: responses[1] }, |
| 126 | requestState: 'first-state', |
| 127 | }); |
| 128 | throw new ComposioToolInputRequiredError('Batch', { |
| 129 | inputRequests: { next: question }, |
| 130 | requestState: 'next-state', |
| 131 | }); |
| 132 | } |
| 133 | expect(options).toEqual({ |
| 134 | inputResponses: { next: responses[2] }, |
| 135 | requestState: 'next-state', |
| 136 | }); |
| 137 | return 'done'; |
| 138 | }, |
| 139 | async () => responses[answerIndex++] |
| 140 | ); |
| 141 | expect(result).toBe('done'); |
| 142 | }); |
| 143 | |
| 144 | test('propagates execution and UI failures without retrying', async () => { |
| 145 | let calls = 0; |
| 146 | await expect( |
| 147 | runWithApprovals( |
| 148 | async () => { |
| 149 | calls++; |
| 150 | throw new Error('network failure'); |
| 151 | }, |
| 152 | async () => { |
| 153 | throw new Error('must not ask'); |
| 154 | } |
| 155 | ) |
| 156 | ).rejects.toThrow('network failure'); |
| 157 | expect(calls).toBe(1); |
| 158 | await expect( |
| 159 | runWithApprovals( |
| 160 | async () => { |
| 161 | throw new ComposioToolInputRequiredError('Read', { inputRequests: { first: question } }); |
| 162 | }, |
| 163 | async () => { |
| 164 | throw new Error('UI failed'); |
| 165 | } |
| 166 | ) |
| 167 | ).rejects.toThrow('UI failed'); |
| 168 | }); |
| 169 | |
| 170 | test('live verification rejects a completed batch with failed inner calls', () => { |
| 171 | expect(() => |
| 172 | assertBatch( |
| 173 | { |
| 174 | total_count: 2, |
| 175 | success_count: 0, |
| 176 | error_count: 2, |
| 177 | results: [{ error: 'upstream unavailable' }, { error: 'upstream unavailable' }], |
| 178 | }, |
| 179 | 2, |
| 180 | 0 |
| 181 | ) |
| 182 | ).toThrow(); |
| 183 | }); |
| 184 | |
| 185 | test('live verification distinguishes an approval denial from an unrelated tool failure', () => { |
| 186 | const batch = { |
| 187 | total_count: 2, |
| 188 | success_count: 1, |
| 189 | error_count: 1, |
| 190 | results: [ |
| 191 | { error: null, response: { successful: true, error: null } }, |
| 192 | { error: 'User denied tool approval', response: null }, |
| 193 | ], |
| 194 | }; |
| 195 | expect(() => assertBatch(batch, 1, 1)).not.toThrow(); |
| 196 | batch.results[1].error = 'Upstream unavailable'; |
| 197 | expect(() => assertBatch(batch, 1, 1)).toThrow(); |
| 198 | }); |