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 106 lines 3.13 KB
Raw
1 import process from 'node:process';
2 import { Config, ConfigProvider, Console, Effect, Stream, Logger, Layer, References } from 'effect';
3 import { ChildProcess as Command } from 'effect/process';
4 import * as BunServices from '@effect/platform-bun/BunServices';
5 import * as BunRuntime from '@effect/platform-bun/BunRuntime';
6 import { buildCompanionModules, posthogBakeArgs, teardown } from './_shared';
7 import { BinaryBuildError } from './build-error';
8 import { buildCliReleaseVersionDefineArgs } from '../src/utils/cli-release-version';
9
10 /**
11 * Usage: `bun scripts/build-binary.ts`
12 */
13 export function buildBinary() {
14 return Effect.gen(function* () {
15 const cwd = process.cwd();
16 yield* Effect.logDebug(`Building binary in ${cwd}`);
17
18 const args = [
19 'bun',
20 /**
21 * Transpile and bundle the CLI app.
22 */
23 'build',
24 './src/bin.ts',
25
26 /**
27 * Statically inline any environment variable that matches `DEBUG_OVERRIDE_*`.
28 */
29 '--env',
30 'DEBUG_OVERRIDE_*',
31 ...posthogBakeArgs(),
32
33 ...buildCliReleaseVersionDefineArgs(process.env.RELEASE_TAG),
34
35 /**
36 * Generate a standalone Bun executable containing your bundled code.
37 */
38 '--compile',
39 '--production',
40
41 /**
42 * Output file destination.
43 */
44 '--outfile',
45 './dist/composio',
46 ] as const satisfies ReadonlyArray<string>;
47
48 yield* Effect.logDebug('Running build command with', args.join(' '), '');
49
50 const child = yield* Command.make(args[0], args.slice(1));
51
52 const { exitCode } = yield* Effect.all(
53 {
54 exitCode: child.exitCode,
55 output: Stream.merge(
56 Stream.decodeText(child.stdout, { encoding: 'utf-8' }),
57 Stream.decodeText(child.stderr, { encoding: 'utf-8' }),
58 { haltStrategy: 'left' }
59 ).pipe(
60 Stream.tap(chunk => Console.log(chunk)),
61 Stream.runDrain
62 ),
63 },
64 {
65 concurrency: 'unbounded',
66 }
67 );
68
69 process.exitCode = exitCode;
70
71 if (exitCode !== 0) {
72 return yield* new BinaryBuildError({
73 message: 'Failed to build binary',
74 exitCode,
75 });
76 }
77
78 yield* Effect.logDebug('', 'Binary built successfully');
79
80 // Build companion modules that `composio run` imports in the child process.
81 // These cannot live inside the compiled binary because they run in a separate
82 // Bun process via the --preload globals file.
83 yield* buildCompanionModules('./dist');
84
85 yield* Effect.logDebug('', 'Companion modules built successfully');
86 });
87 }
88
89 const ConfigLive = Effect.gen(function* () {
90 const logLevel = yield* Config.LogLevel('COMPOSIO_LOG_LEVEL').pipe(Config.withDefault('Info'));
91
92 return Layer.succeed(References.MinimumLogLevel, logLevel);
93 }).pipe(Layer.unwrap, Layer.merge(ConfigProvider.layer(ConfigProvider.fromEnv())));
94
95 if (require.main === module) {
96 buildBinary().pipe(
97 Effect.provide(ConfigLive),
98 Effect.provide(Logger.layer([Logger.consolePretty()])),
99 Effect.provide(BunServices.layer),
100 Effect.scoped,
101 Effect.map(() => ({ message: 'Process completed successfully.' })),
102 BunRuntime.runMain({
103 teardown,
104 })
105 );
106 }