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 228 lines 6.95 KB
Raw
1 import { Effect, FileSystem, Match, Path } from 'effect';
2 import { ChildProcess } from 'effect/process';
3 import { ChildProcessSpawner } from 'effect/process/ChildProcessSpawner';
4 import {
5 ProjectEnvironmentDetector,
6 type JsLanguage,
7 type JsPackageManager,
8 type PythonPackageManager,
9 } from 'src/services/project-environment-detector';
10 import { getAncestors } from 'src/utils/get-ancestors';
11
12 export type CoreDependencyPlan =
13 | {
14 kind: 'js';
15 language: JsLanguage;
16 packageManager: JsPackageManager;
17 rootDir: string;
18 dependency: '@composio/core';
19 installCommand: string;
20 }
21 | {
22 kind: 'python';
23 language: 'python';
24 packageManager: PythonPackageManager;
25 rootDir: string;
26 dependency: 'composio';
27 installCommand: string;
28 };
29
30 export type CoreDependencyVersion = {
31 version: string;
32 source: 'package.json' | 'node_modules' | 'python';
33 };
34
35 export const getJsInstallCommand = (pm: JsPackageManager, dependency: string) =>
36 Match.value(pm).pipe(
37 Match.when('pnpm', () => `pnpm add ${dependency}`),
38 Match.when('bun', () => `bun add ${dependency}`),
39 Match.when('yarn', () => `yarn add ${dependency}`),
40 Match.when('npm', () => `npm install -S ${dependency}`),
41 Match.when('deno', () => `deno add npm:${dependency}`),
42 Match.exhaustive
43 );
44
45 export const getPythonInstallCommand = (pm: PythonPackageManager, dependency: string) =>
46 pm === 'uv' ? `uv pip install ${dependency}` : `pip install ${dependency}`;
47
48 export const detectCoreDependencyPlan = (cwd: string) =>
49 Effect.gen(function* () {
50 const envDetector = yield* ProjectEnvironmentDetector;
51 const env = yield* envDetector.detectProjectEnvironment(cwd);
52
53 if (env.kind === 'python') {
54 return {
55 kind: 'python',
56 language: 'python',
57 packageManager: env.packageManager,
58 rootDir: env.rootDir,
59 dependency: 'composio',
60 installCommand: getPythonInstallCommand(env.packageManager, 'composio'),
61 } satisfies CoreDependencyPlan;
62 }
63
64 return {
65 kind: 'js',
66 language: env.language,
67 packageManager: env.packageManager,
68 rootDir: env.rootDir,
69 dependency: '@composio/core',
70 installCommand: getJsInstallCommand(env.packageManager, '@composio/core'),
71 } satisfies CoreDependencyPlan;
72 });
73
74 const readPackageJson = (fs: FileSystem.FileSystem, path: Path.Path, dir: string) =>
75 fs.readFileString(path.join(dir, 'package.json')).pipe(
76 Effect.andThen(content =>
77 Effect.try({
78 try: () => JSON.parse(content) as Record<string, unknown>,
79 catch: () => null,
80 })
81 ),
82 Effect.catch(() => Effect.succeed(null))
83 );
84
85 const findDependencySpec = (pkg: Record<string, unknown>, name: string) => {
86 const deps = (pkg.dependencies ?? {}) as Record<string, string>;
87 const devDeps = (pkg.devDependencies ?? {}) as Record<string, string>;
88 const peerDeps = (pkg.peerDependencies ?? {}) as Record<string, string>;
89 const optionalDeps = (pkg.optionalDependencies ?? {}) as Record<string, string>;
90
91 return deps[name] ?? devDeps[name] ?? peerDeps[name] ?? optionalDeps[name] ?? null;
92 };
93
94 const detectJsDependencyVersion = (
95 fs: FileSystem.FileSystem,
96 plan: Extract<CoreDependencyPlan, { kind: 'js' }>
97 ) =>
98 Effect.gen(function* () {
99 const path = yield* Path.Path;
100 const dirs = yield* getAncestors(plan.rootDir);
101
102 for (const dir of dirs) {
103 const packageJsonPath = path.join(dir, 'node_modules', '@composio', 'core', 'package.json');
104 const content = yield* fs
105 .readFileString(packageJsonPath)
106 .pipe(Effect.catch(() => Effect.succeed<string | null>(null)));
107
108 if (content) {
109 const parsed = yield* Effect.try({
110 try: () => JSON.parse(content) as { version?: string },
111 catch: () => null,
112 }).pipe(Effect.catch(() => Effect.succeed(null)));
113
114 if (parsed?.version) {
115 return {
116 version: parsed.version,
117 source: 'node_modules',
118 } satisfies CoreDependencyVersion;
119 }
120 }
121
122 if (plan.packageManager === 'pnpm') {
123 const pnpmStore = path.join(dir, 'node_modules', '.pnpm');
124 const entries = yield* fs
125 .readDirectory(pnpmStore)
126 .pipe(Effect.catch(() => Effect.succeed<string[]>([])));
127 const match = entries.find(entry => entry.startsWith('@composio+core@'));
128 if (!match) {
129 continue;
130 }
131 const pnpmPkgPath = path.join(
132 pnpmStore,
133 match,
134 'node_modules',
135 '@composio',
136 'core',
137 'package.json'
138 );
139 const pnpmContent = yield* fs
140 .readFileString(pnpmPkgPath)
141 .pipe(Effect.catch(() => Effect.succeed<string | null>(null)));
142
143 if (pnpmContent) {
144 const pnpmParsed = yield* Effect.try({
145 try: () => JSON.parse(pnpmContent) as { version?: string },
146 catch: () => null,
147 }).pipe(Effect.catch(() => Effect.succeed(null)));
148
149 if (pnpmParsed?.version) {
150 return {
151 version: pnpmParsed.version,
152 source: 'node_modules',
153 } satisfies CoreDependencyVersion;
154 }
155 }
156 }
157 }
158
159 for (const dir of dirs) {
160 const pkg = yield* readPackageJson(fs, path, dir);
161 if (!pkg) {
162 continue;
163 }
164 const spec = findDependencySpec(pkg, plan.dependency);
165 if (spec) {
166 return {
167 version: spec,
168 source: 'package.json',
169 } satisfies CoreDependencyVersion;
170 }
171 }
172
173 return null;
174 });
175
176 const detectPythonDependencyVersion = (plan: Extract<CoreDependencyPlan, { kind: 'python' }>) =>
177 Effect.gen(function* () {
178 const [cmd, ...args] =
179 plan.packageManager === 'uv'
180 ? [
181 'uv',
182 'run',
183 'python',
184 '-c',
185 "import importlib.metadata as m; print(m.version('composio'))",
186 ]
187 : ['python', '-c', "import importlib.metadata as m; print(m.version('composio'))"];
188
189 const spawner = yield* ChildProcessSpawner;
190 const stdout = yield* spawner
191 .string(ChildProcess.make(cmd, args, { extendEnv: true }))
192 .pipe(Effect.catch(() => Effect.succeed<string | null>(null)));
193
194 if (!stdout) {
195 return null;
196 }
197
198 const version = stdout.trim();
199 if (!version) {
200 return null;
201 }
202
203 return {
204 version,
205 source: 'python',
206 } satisfies CoreDependencyVersion;
207 });
208
209 export const detectCoreDependencyVersion = (plan: CoreDependencyPlan) =>
210 Effect.gen(function* () {
211 if (plan.kind === 'python') {
212 return yield* detectPythonDependencyVersion(plan);
213 }
214
215 const fs = yield* FileSystem.FileSystem;
216 return yield* detectJsDependencyVersion(fs, plan);
217 });
218
219 export const resolveCoreDependencyState = (cwd: string) =>
220 Effect.gen(function* () {
221 const plan = yield* detectCoreDependencyPlan(cwd);
222 const installedVersion = yield* detectCoreDependencyVersion(plan);
223
224 return {
225 plan,
226 installedVersion,
227 } as const;
228 });