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 | /** |
| 2 | * A CLI rig: one fake Composio deployment and one journey container whose |
| 3 | * `composio` binary is pointed at it for every host it knows. Other outbound |
| 4 | * traffic from the container is not blocked. |
| 5 | */ |
| 6 | |
| 7 | import { startJourneyContainer, type E2ETestResult, type JourneyContainer } from '@e2e-tests/utils'; |
| 8 | import type { JourneyExecOptions, JourneyOptions } from '@e2e-tests/utils'; |
| 9 | import type { JournalEntry } from './journal'; |
| 10 | import { startFakeComposio, type FakeComposio } from './server'; |
| 11 | import { USER_API_KEY } from './world'; |
| 12 | |
| 13 | /** Where the fake Dashboard's certificate lives inside the container. */ |
| 14 | export const CA_CERT_PATH = '/etc/composio-e2e/fake-ca.pem'; |
| 15 | |
| 16 | export interface RigOptions { |
| 17 | /** Short name for the container, shown in `docker ps`. */ |
| 18 | label?: string; |
| 19 | /** |
| 20 | * `false` leaves `COMPOSIO_WEB_URL` unset, which keeps consumer executions on |
| 21 | * the backend instead of the Dashboard. Defaults to `true`, the production path. |
| 22 | */ |
| 23 | dashboard?: boolean; |
| 24 | /** Extra environment for every command, applied over the rig's own. */ |
| 25 | env?: Record<string, string>; |
| 26 | /** Host files mounted read-only into the container. */ |
| 27 | readOnlyMounts?: JourneyOptions['readOnlyMounts']; |
| 28 | } |
| 29 | |
| 30 | export interface CommandResult extends E2ETestResult { |
| 31 | /** What the fake received while the command ran, in arrival order. */ |
| 32 | readonly requests: ReadonlyArray<JournalEntry>; |
| 33 | } |
| 34 | |
| 35 | export interface CliRig { |
| 36 | readonly fake: FakeComposio; |
| 37 | readonly journey: JourneyContainer; |
| 38 | /** Runs a command line in the container and collects the requests it caused. */ |
| 39 | run(command: string, options?: JourneyExecOptions): Promise<CommandResult>; |
| 40 | /** Stores the seeded user API key the way `composio login --user-api-key` does. */ |
| 41 | login(): Promise<CommandResult>; |
| 42 | stop(): Promise<void>; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * The environment that keeps the binary hermetic: every host it knows is the |
| 47 | * fake, telemetry is off, and the fake Dashboard's certificate is trusted. |
| 48 | */ |
| 49 | export const hermeticEnv = ( |
| 50 | fake: FakeComposio, |
| 51 | options: Pick<RigOptions, 'dashboard'> = {} |
| 52 | ): Record<string, string> => ({ |
| 53 | ...fake.cliEnv({ dashboard: options.dashboard }), |
| 54 | COMPOSIO_CLI_TELEMETRY_DISABLED: '1', |
| 55 | NODE_EXTRA_CA_CERTS: CA_CERT_PATH, |
| 56 | }); |
| 57 | |
| 58 | export const startCliRig = async (options: RigOptions = {}): Promise<CliRig> => { |
| 59 | const fake = await startFakeComposio(); |
| 60 | let journey: JourneyContainer; |
| 61 | try { |
| 62 | journey = await startJourneyContainer({ |
| 63 | label: options.label, |
| 64 | env: { ...hermeticEnv(fake, options), ...options.env }, |
| 65 | readOnlyMounts: options.readOnlyMounts, |
| 66 | }); |
| 67 | await journey.writeFile(CA_CERT_PATH, fake.caCert); |
| 68 | } catch (error) { |
| 69 | await fake.close(); |
| 70 | throw error; |
| 71 | } |
| 72 | |
| 73 | const run: CliRig['run'] = async (command, execOptions) => { |
| 74 | const mark = fake.journal.mark(); |
| 75 | const result = await journey.exec(command, execOptions); |
| 76 | return { ...result, requests: fake.journal.since(mark) }; |
| 77 | }; |
| 78 | |
| 79 | return { |
| 80 | fake, |
| 81 | journey, |
| 82 | run, |
| 83 | login: async () => { |
| 84 | const result = await run(`composio login --user-api-key ${USER_API_KEY} --no-skill-install`); |
| 85 | if (result.exitCode !== 0) { |
| 86 | throw new Error( |
| 87 | `Rig login failed (exit ${result.exitCode})\nstdout: ${result.stdout}\nstderr: ${result.stderr}` |
| 88 | ); |
| 89 | } |
| 90 | return result; |
| 91 | }, |
| 92 | stop: async () => { |
| 93 | await journey.stop(); |
| 94 | await fake.close(); |
| 95 | }, |
| 96 | }; |
| 97 | }; |