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 194 lines 7.42 KB
Raw
1 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
2 // Import the BunServices submodule directly (as a namespace, matching its own named export
3 // shape); the package's barrel (`@effect/platform-bun`) unconditionally re-exports BunRedis,
4 // which imports the `bun` builtin at module scope and crashes Node's ESM resolver (vitest runs
5 // under Node, not Bun).
6 import * as BunServices from '@effect/platform-bun/BunServices';
7 import { Effect, FileSystem, Layer, Option, References } from 'effect';
8 import { ChildProcess, ChildProcessSpawner } from 'effect/process';
9 import path from 'node:path';
10
11 const __dirname = path.resolve(path.dirname(new URL(import.meta.url).pathname));
12
13 /**
14 * Sets up TypeScript fixtures by simulating `@composio/core` package installation via `pnpm`.
15 * For all fixture folders containing a `package.json` with `@composio/core` in `dependencies` / `devDependencies`,
16 * installs the package by copying the built files from dist to node_modules.
17 */
18 function setupFixturesTypeScript(fixturePaths: string[]) {
19 return Effect.gen(function* () {
20 const fs = yield* FileSystem.FileSystem;
21 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
22
23 yield* Effect.all(
24 fixturePaths.map(fixturePath => {
25 const fixtureDirName = path.basename(fixturePath);
26 return Effect.gen(function* () {
27 // Check if package.json exists and contains @composio/core
28 const packageJsonPath = path.join(fixturePath, 'package.json');
29 const packageJsonExists = yield* fs.exists(packageJsonPath);
30
31 if (!packageJsonExists) {
32 yield* Effect.logDebug(`Skipping ${fixtureDirName}: no package.json found`);
33 return;
34 }
35
36 // Read and parse package.json
37 const packageJsonContent = yield* fs.readFileString(packageJsonPath);
38 const packageJson = JSON.parse(packageJsonContent);
39
40 // Check if @composio/core is in dependencies or devDependencies
41 const hasComposioCore =
42 (packageJson.dependencies && packageJson.dependencies['@composio/core']) ||
43 (packageJson.devDependencies && packageJson.devDependencies['@composio/core']);
44
45 if (!hasComposioCore) {
46 yield* Effect.logDebug(
47 `Skipping ${fixtureDirName}: no @composio/core dependency found`
48 );
49 return;
50 }
51
52 yield* Effect.logDebug(`Setting up @composio/core for fixture: ${fixtureDirName}`);
53
54 // Clean up existing node_modules/@composio/core
55 const nodeModulesDir = path.join(fixturePath, 'node_modules');
56 yield* fs.remove(nodeModulesDir, { recursive: true, force: true });
57
58 const installCmd = ChildProcess.make('pnpm', ['install', '--ignore-workspace'], {
59 cwd: fixturePath,
60 stdout: 'inherit',
61 stderr: 'inherit',
62 });
63 const exitCode = Number(yield* spawner.exitCode(installCmd));
64
65 if (exitCode !== 0) {
66 yield* Effect.logError(
67 `Failed to install @composio/core for fixture: ${fixtureDirName}`
68 );
69 return;
70 }
71
72 yield* Effect.logDebug(
73 `Successfully set up @composio/core for fixture: ${fixtureDirName}`
74 );
75 }).pipe(
76 Effect.catch(error =>
77 Effect.logError(`Failed to setup fixture ${fixtureDirName}: ${error}`)
78 )
79 );
80 }),
81 { concurrency: 4 }
82 );
83 });
84 }
85
86 /**
87 * Sets up Python fixtures by simulating `composio_core` package installation via `uv pip`.
88 * For all fixture folders containing a `requirements.txt` it sets up `uv venv` and installs
89 * the required packages from the Internet.
90 */
91 function setupFixturesPython(fixturePaths: string[]) {
92 return Effect.gen(function* () {
93 const fs = yield* FileSystem.FileSystem;
94 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
95
96 yield* Effect.all(
97 fixturePaths.map(fixturePath => {
98 const fixtureDirName = path.basename(fixturePath);
99 return Effect.gen(function* () {
100 // Check if requirements.txt exists
101 const requirementsPath = path.join(fixturePath, 'requirements.txt');
102 const requirementsExists = yield* fs.exists(requirementsPath);
103
104 if (!requirementsExists) {
105 yield* Effect.logDebug(`Skipping ${fixtureDirName}: no requirements.txt found`);
106 return;
107 }
108
109 // Read and parse requirements.txt
110 const requirementsContent = yield* fs.readFileString(requirementsPath);
111 const requirementsTxt = requirementsContent.split('\n');
112
113 // Check if @composio/core is in dependencies or devDependencies
114 const hasComposioCore = requirementsTxt.includes('composio_core');
115
116 if (!hasComposioCore) {
117 yield* Effect.logDebug(
118 `Skipping ${fixtureDirName}: no \`composio_core\` dependency found`
119 );
120 return;
121 }
122
123 const setupShPath = path.join(fixturePath, 'setup.sh');
124 const setupShExists = yield* fs.exists(setupShPath);
125
126 if (!setupShExists) {
127 yield* Effect.logDebug(`Skipping ${fixtureDirName}: no setup.sh found`);
128 return;
129 }
130
131 yield* Effect.logDebug(`Setting up \`uv\` for fixture: ${fixtureDirName}`);
132
133 const installCmd = ChildProcess.make(setupShPath, [], {
134 cwd: fixturePath,
135 shell: true,
136 stdout: 'inherit',
137 stderr: 'inherit',
138 });
139 const exitCode = Number(yield* spawner.exitCode(installCmd));
140
141 if (exitCode !== 0) {
142 yield* Effect.logError(
143 `Failed to install @composio/core for fixture: ${fixtureDirName}`
144 );
145 return;
146 }
147
148 yield* Effect.logDebug(
149 `Successfully set up @composio/core for fixture: ${fixtureDirName}`
150 );
151 }).pipe(
152 Effect.catch(error =>
153 Effect.logError(`Failed to setup fixture ${fixtureDirName}: ${error}`)
154 )
155 );
156 }),
157 { concurrency: 4 }
158 );
159 });
160 }
161
162 export async function setup() {
163 const program = Effect.gen(function* () {
164 // Path to the fixtures directory.
165 // Note: we're using `__dirname` because `import.meta.resolve` is not yet available in Vitest.
166 // See: https://github.com/vitest-dev/vitest/pull/5188.
167 const fixturesDir = path.resolve(__dirname, '../__fixtures__');
168 yield* Effect.logDebug(`Setting up TypeScript fixtures in ${fixturesDir}`);
169
170 const fs = yield* FileSystem.FileSystem;
171
172 // Get all fixture directories
173 const fixtureEntries = yield* fs.readDirectory(fixturesDir);
174
175 // Filter to only directories by checking each entry
176 const fixtureDirNames: string[] = yield* Effect.all(
177 fixtureEntries.map(entryName =>
178 Effect.gen(function* () {
179 const entryPath = path.join(fixturesDir, entryName);
180 const stat = yield* fs.stat(entryPath);
181 return stat.type === 'Directory' ? Option.some(entryPath) : Option.none<string>();
182 }).pipe(Effect.catch(() => Effect.succeed(Option.none<string>())))
183 )
184 ).pipe(Effect.map(Option.all), Effect.map(Option.getOrElse(() => [] as string[])));
185
186 yield* setupFixturesPython(fixtureDirNames);
187 yield* setupFixturesTypeScript(fixtureDirNames);
188 }).pipe(
189 Effect.provide(BunServices.layer),
190 Effect.provide(Layer.succeed(References.MinimumLogLevel, 'Debug'))
191 );
192
193 await Effect.runPromise(program);
194 }