@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 208 lines 7.26 KB
Raw
1 /**
2 * Journey containers: one CLI container that stays up while a test runs
3 * several commands in it, so state written by one command (a login, a linked
4 * account, a cache file) is there for the next.
5 *
6 * `runCmd` gives every command a fresh container, which suits single commands.
7 * A journey also differs in what it lets in: nothing from the host. No host
8 * `~/.composio` mount and no API key passthrough, so a journey can only reach
9 * what its test points it at.
10 */
11
12 import { $ } from 'bun';
13 import { writeFile } from 'node:fs/promises';
14 import { getRepoRoot, resolveCliVersionMetaList } from './config';
15 import { ensureCliImage } from './image-lifecycle';
16 import type { E2ETestResult } from './types';
17
18 export interface JourneyOptions {
19 /** Environment of every command in the journey. */
20 env?: Record<string, string>;
21 /**
22 * Home directory inside the container. It does not exist until the journey
23 * starts, so the CLI meets it the way a first run does.
24 */
25 home?: string;
26 /** Short name used in the container name, to tell journeys apart in `docker ps`. */
27 label?: string;
28 /** Host files or directories mounted read-only, such as a tool the journey runs. */
29 readOnlyMounts?: ReadonlyArray<{ readonly source: string; readonly target: string }>;
30 }
31
32 export interface JourneyExecOptions {
33 /** Extra environment for this command only. */
34 env?: Record<string, string>;
35 /** Text piped to the command's stdin. Without it stdin is `/dev/null`. */
36 stdin?: string;
37 /** Working directory; defaults to the journey's home. */
38 cwd?: string;
39 /** Kills the command after this long and reports exit code 124. Defaults to 60 seconds. */
40 timeoutSeconds?: number;
41 /** Runs the command as this `uid:gid` instead of root, so file permissions apply to it. */
42 user?: string;
43 }
44
45 export interface JourneyStep {
46 readonly command: string;
47 readonly result: E2ETestResult;
48 readonly durationMs: number;
49 }
50
51 export interface JourneyContainer {
52 readonly name: string;
53 readonly home: string;
54 /** Runs a POSIX `sh` command line in the container. */
55 exec(command: string, options?: JourneyExecOptions): Promise<E2ETestResult>;
56 /** Writes a file inside the container, creating its directory. */
57 writeFile(path: string, content: string): Promise<void>;
58 /** Reads a file from the container; rejects when it does not exist. */
59 readFile(path: string): Promise<string>;
60 /** Every command run so far, in order. */
61 steps(): ReadonlyArray<JourneyStep>;
62 /** Writes the steps to a log file on the host, for reading after a failure. */
63 writeDebugLog(path: string): Promise<void>;
64 /** Removes the container. */
65 stop(): Promise<void>;
66 }
67
68 const DEFAULT_HOME = '/home/journey';
69 const DEFAULT_TIMEOUT_SECONDS = 60;
70
71 const shellQuote = (value: string): string => `'${value.replace(/'/g, `'\\''`)}'`;
72
73 const envArgs = (env: Record<string, string> = {}): string[] =>
74 Object.entries(env).flatMap(([name, value]) => ['-e', `${name}=${value}`]);
75
76 const formatStep = (step: JourneyStep, index: number): string =>
77 [
78 `--- Step ${index + 1} ---`,
79 `Command: ${step.command}`,
80 `Duration: ${(step.durationMs / 1000).toFixed(2)}s`,
81 `Exit Code: ${step.result.exitCode}`,
82 '',
83 '[stdout]',
84 step.result.stdout.trim() || '(empty)',
85 '',
86 '[stderr]',
87 step.result.stderr.trim() || '(empty)',
88 '',
89 ].join('\n');
90
91 let imageTagPromise: Promise<string> | undefined;
92
93 /** The CLI image of the current checkout, ensured once per test process. */
94 const currentCliImage = (): Promise<string> => {
95 imageTagPromise ??= (() => {
96 const [cliVersion] = resolveCliVersionMetaList(['current']);
97 return ensureCliImage(cliVersion.value, { repoRoot: getRepoRoot() });
98 })();
99 return imageTagPromise;
100 };
101
102 /**
103 * Starts a container from the CLI image of the current checkout and keeps it
104 * running until `stop()`.
105 */
106 export async function startJourneyContainer(
107 options: JourneyOptions = {}
108 ): Promise<JourneyContainer> {
109 const imageTag = await currentCliImage();
110 const home = options.home ?? DEFAULT_HOME;
111 const name = `e2e-journey-${options.label ?? 'cli'}-${Date.now()}-${Math.floor(Math.random() * 1e6)}`;
112 const steps: JourneyStep[] = [];
113 const mountArgs = (options.readOnlyMounts ?? []).flatMap(mount => [
114 '--mount',
115 `type=bind,source=${mount.source},target=${mount.target},readonly`,
116 ]);
117
118 const started =
119 await $`docker run -d --rm --name ${name} --label composio.e2e=true --label composio.runtime=cli --add-host host.docker.internal:host-gateway -e HOME=${home} ${envArgs(options.env)} ${mountArgs} --workdir / ${imageTag} sleep infinity`
120 .nothrow()
121 .quiet();
122 if (started.exitCode !== 0) {
123 throw new Error(`Failed to start journey container: ${started.stderr.toString().trim()}`);
124 }
125
126 const run = async (
127 command: string,
128 execOptions: JourneyExecOptions = {}
129 ): Promise<E2ETestResult> => {
130 const timeout = execOptions.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS;
131 // `timeout` lives in the container so the limit also stops the processes
132 // the command started; a host-side kill would only end `docker exec`.
133 const bounded = `timeout --kill-after=5 ${timeout} sh -c ${shellQuote(command)}`;
134 const process = Bun.spawn(
135 [
136 'docker',
137 'exec',
138 '-i',
139 ...envArgs(execOptions.env),
140 ...(execOptions.user ? ['--user', execOptions.user] : []),
141 '--workdir',
142 execOptions.cwd ?? home,
143 name,
144 'sh',
145 '-c',
146 bounded,
147 ],
148 {
149 stdin: new TextEncoder().encode(execOptions.stdin ?? ''),
150 stdout: 'pipe',
151 stderr: 'pipe',
152 }
153 );
154 const [stdout, stderr, exitCode] = await Promise.all([
155 new Response(process.stdout).text(),
156 new Response(process.stderr).text(),
157 process.exited,
158 ]);
159 return { exitCode, stdout, stderr };
160 };
161
162 const prepared = await run(`mkdir -p ${shellQuote(home)}`, { cwd: '/' });
163 if (prepared.exitCode !== 0) {
164 await $`docker rm -f ${name}`.nothrow().quiet();
165 throw new Error(`Failed to create journey home ${home}: ${prepared.stderr.trim()}`);
166 }
167
168 return {
169 name,
170 home,
171 exec: async (command, execOptions) => {
172 const startedAt = Date.now();
173 const result = await run(command, execOptions);
174 steps.push({ command, result, durationMs: Date.now() - startedAt });
175 return result;
176 },
177 writeFile: async (path, content) => {
178 const directory = path.slice(0, Math.max(1, path.lastIndexOf('/')));
179 const written = await run(`mkdir -p ${shellQuote(directory)} && cat > ${shellQuote(path)}`, {
180 stdin: content,
181 cwd: '/',
182 });
183 if (written.exitCode !== 0) {
184 throw new Error(`Failed to write ${path} in ${name}: ${written.stderr.trim()}`);
185 }
186 },
187 readFile: async path => {
188 const read = await run(`cat ${shellQuote(path)}`, { cwd: '/' });
189 if (read.exitCode !== 0) {
190 throw new Error(`Failed to read ${path} in ${name}: ${read.stderr.trim()}`);
191 }
192 return read.stdout;
193 },
194 steps: () => [...steps],
195 writeDebugLog: async path => {
196 await writeFile(
197 path,
198 [`Journey container: ${name}`, `Image: ${imageTag}`, '', ...steps.map(formatStep)].join(
199 '\n'
200 ),
201 'utf-8'
202 );
203 },
204 stop: async () => {
205 await $`docker rm -f ${name}`.nothrow().quiet();
206 },
207 };
208 }