@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 186 lines 6.39 KB
Raw
1 import { Data, Effect, FileSystem, Match, Path } from 'effect';
2 import { ExpectedFailure } from 'src/effects/expected-failure';
3 import { ChildProcess } from 'effect/process';
4 import { ChildProcessSpawner } from 'effect/process/ChildProcessSpawner';
5 import {
6 JsPackageManagerDetector,
7 type PackageManager,
8 } from 'src/services/js-package-manager-detector';
9
10 export class ComposioCorePkgNotFound extends Data.TaggedError('error/ComposioCorePkgNotFound')<{
11 readonly message: string;
12 readonly cause: string;
13 readonly fix?: string;
14 }> {
15 get [ExpectedFailure]() {
16 return true;
17 }
18 }
19
20 const DEFAULT_PACKAGE_MANAGER: PackageManager = 'npm';
21
22 export function pyFindComposioCoreGenerated(cwd: string) {
23 return Effect.gen(function* () {
24 const path = yield* Path.Path;
25
26 /**
27 * Returns a `ComposioCorePkgNotFound` error with the given message and cause.
28 * It lazily identifies the package manager used by the user to tell them how to install `@composio/core`.
29 */
30 const onError = (message: string) => (e: Error | string) =>
31 Effect.gen(function* () {
32 yield* Effect.logDebug('Identifying JS package manager...');
33 const pkgManagerDetector = yield* JsPackageManagerDetector;
34 const pkgManager = yield* pkgManagerDetector.detectJsPackageManager(cwd).pipe(
35 Effect.tapError(e => Effect.logError(e)),
36 Effect.catch(() => Effect.succeed(DEFAULT_PACKAGE_MANAGER))
37 );
38
39 yield* Effect.logDebug({ pkgManager });
40
41 const installCmd = `uv pip install`;
42
43 const fix = `Install \`composio-core\` with \`${installCmd} composio-core\`, or specify an output directory using \`--output-dir\``;
44
45 return yield* Effect.fail(
46 new ComposioCorePkgNotFound({
47 cause: e instanceof Error ? e.message : e,
48 fix,
49 message,
50 })
51 );
52 }) satisfies Effect.Effect<never, ComposioCorePkgNotFound, JsPackageManagerDetector>;
53
54 // Try to find `composio-core` in the current `uv` environment
55 const [cmd, ...args] = [
56 'uv',
57 'run',
58 'python',
59 '-c',
60 'import composio; print(composio.__file__)',
61 ];
62
63 const spawner = yield* ChildProcessSpawner;
64 const stdout = yield* spawner
65 .string(ChildProcess.make(cmd, args, { extendEnv: true }))
66 .pipe(
67 Effect.catch((e: unknown) =>
68 onError('Failed to locate composio-core in uv environment')(
69 e instanceof Error ? e : String(e)
70 )
71 )
72 );
73
74 yield* Effect.logDebug({ stdout, cmd: [cmd, ...args].join(' ') });
75
76 const composioCorePath = `${path.dirname(stdout)}/generated`;
77 yield* Effect.logDebug({ composioCorePath });
78
79 return composioCorePath;
80 }) satisfies Effect.Effect<string, ComposioCorePkgNotFound, unknown>;
81 }
82
83 /**
84 * Returns the path to `@composio/core/generated` inside the user's project, if it exists.
85 * The search is scoped to the given current working directory.
86 */
87 export function jsFindComposioCoreGenerated(cwd: string) {
88 return Effect.gen(function* () {
89 /**
90 * Returns a `ComposioCorePkgNotFound` error with the given message and cause.
91 * It lazily identifies the package manager used by the user to tell them how to install `@composio/core`.
92 */
93 const onError =
94 (message: string, { pkgManager }: { pkgManager: PackageManager }) =>
95 (e: Error | string) =>
96 Effect.gen(function* () {
97 const installCmd = Match.value(pkgManager).pipe(
98 Match.when('pnpm', () => 'pnpm add'),
99 Match.when('bun', () => 'bun add'),
100 Match.when('yarn', () => 'yarn add'),
101 Match.when('npm', () => 'npm install -S'),
102 Match.exhaustive
103 );
104
105 const fix = `Install @composio/core with \`${installCmd} @composio/core\`, or specify an output directory using \`--output-dir\``;
106
107 return yield* Effect.fail(
108 new ComposioCorePkgNotFound({
109 cause: e instanceof Error ? e.message : e,
110 fix,
111 message,
112 })
113 );
114 }) satisfies Effect.Effect<never, ComposioCorePkgNotFound, JsPackageManagerDetector>;
115
116 const fs = yield* FileSystem.FileSystem;
117 const path = yield* Path.Path;
118
119 yield* Effect.logDebug('Detecting package manager...');
120 const pkgManagerDetector = yield* JsPackageManagerDetector;
121 const pkgManager = yield* pkgManagerDetector.detectJsPackageManager(cwd).pipe(
122 Effect.tapError(e => Effect.logError(e)),
123 Effect.catch(() => Effect.succeed(DEFAULT_PACKAGE_MANAGER))
124 );
125 yield* Effect.logDebug({ pkgManager });
126
127 /**
128 * Walk up the directory tree to find node_modules containing @composio/core.
129 * This handles monorepo scenarios where the package may be installed at a parent level.
130 */
131 const findInNodeModules = (
132 searchDir: string
133 ): Effect.Effect<string | null, never, FileSystem.FileSystem> =>
134 Effect.gen(function* () {
135 const nodeModulesPath = path.join(searchDir, 'node_modules', '@composio', 'core');
136
137 yield* Effect.logDebug({ searchingIn: nodeModulesPath });
138
139 const exists = yield* fs
140 .exists(nodeModulesPath)
141 .pipe(Effect.catch(() => Effect.succeed(false)));
142
143 if (exists) {
144 yield* Effect.logDebug({ foundComposioCoreIn: nodeModulesPath });
145 return path.join(nodeModulesPath, 'generated');
146 }
147
148 // For pnpm, also check the virtual store
149 if (pkgManager === 'pnpm') {
150 const pnpmStorePath = path.join(
151 searchDir,
152 'node_modules',
153 '.pnpm',
154 'node_modules',
155 '@composio',
156 'core'
157 );
158 const pnpmExists = yield* fs
159 .exists(pnpmStorePath)
160 .pipe(Effect.catch(() => Effect.succeed(false)));
161
162 if (pnpmExists) {
163 yield* Effect.logDebug({ foundComposioCoreInPnpmStore: pnpmStorePath });
164 return path.join(pnpmStorePath, 'generated');
165 }
166 }
167
168 const parentDir = path.dirname(searchDir);
169 if (parentDir === searchDir) {
170 return null;
171 }
172
173 return yield* findInNodeModules(parentDir);
174 });
175
176 const foundPath = yield* findInNodeModules(cwd);
177
178 if (foundPath) {
179 return foundPath;
180 }
181
182 return yield* onError('@composio/core not found', { pkgManager })(
183 '@composio/core not installed'
184 );
185 }) satisfies Effect.Effect<string, ComposioCorePkgNotFound, unknown>;
186 }