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 70 lines 2.58 KB
Raw
1 import process from 'node:process';
2 import { Config, ConfigProvider, Console, Effect, Logger, Layer, References } from 'effect';
3 import * as BunServices from '@effect/platform-bun/BunServices';
4 import * as BunRuntime from '@effect/platform-bun/BunRuntime';
5 import { teardown } from './_shared';
6 import path from 'node:path';
7 import os from 'node:os';
8 import { mkdir } from 'node:fs/promises';
9 import { $ } from 'bun';
10 import { collectExpectedRunCompanionAssetRelativePaths } from '../src/services/run-companion-modules';
11
12 /**
13 * Usage: `COMPOSIO_INSTALL_DIR=<INSTALL_DIR> bun scripts/build-binary.ts <BINARY_PATH>`
14 */
15 export function installBinary() {
16 return Effect.gen(function* () {
17 const binaryPath = process.argv[2];
18
19 if (!binaryPath) {
20 return yield* Effect.logError('Missing <BINARY_PATH> argument');
21 }
22
23 const defaultInstallDir = path.join(os.homedir(), '.composio');
24 const installDir = yield* Config.String('COMPOSIO_INSTALL_DIR').pipe(
25 Config.withDefault(defaultInstallDir)
26 );
27
28 yield* Effect.logDebug(`Installing binary in ${installDir}`);
29
30 yield* Effect.tryPromise(() => mkdir(installDir, { recursive: true }));
31 yield* Effect.tryPromise(() => $`cp ${binaryPath} ${installDir}/composio`.quiet());
32
33 const sourceDirectory = path.dirname(path.resolve(binaryPath));
34 const companionRelativePaths =
35 yield* collectExpectedRunCompanionAssetRelativePaths(sourceDirectory);
36
37 for (const relativePath of companionRelativePaths) {
38 const sourcePath = path.join(sourceDirectory, relativePath);
39 const targetPath = path.join(installDir, relativePath);
40 yield* Effect.tryPromise(async () => {
41 if (!(await Bun.file(sourcePath).exists())) {
42 throw new Error(`Missing companion module: ${sourcePath}`);
43 }
44 await $`mkdir -p ${path.dirname(targetPath)}`.quiet();
45 await $`cp ${sourcePath} ${targetPath}`.quiet();
46 });
47 }
48
49 yield* Console.log('Binary successfully installed in', installDir);
50 });
51 }
52
53 const ConfigLive = Effect.gen(function* () {
54 const logLevel = yield* Config.LogLevel('COMPOSIO_LOG_LEVEL').pipe(Config.withDefault('Info'));
55
56 return Layer.succeed(References.MinimumLogLevel, logLevel);
57 }).pipe(Layer.unwrap, Layer.merge(ConfigProvider.layer(ConfigProvider.fromEnv())));
58
59 if (require.main === module) {
60 installBinary().pipe(
61 Effect.provide(ConfigLive),
62 Effect.provide(Logger.layer([Logger.consolePretty()])),
63 Effect.provide(BunServices.layer),
64 Effect.scoped,
65 Effect.map(() => ({ message: 'Process completed successfully.' })),
66 BunRuntime.runMain({
67 teardown,
68 })
69 );
70 }