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
| 1 | #!/usr/bin/env bun |
| 2 | |
| 3 | /** |
| 4 | * Package each raw binary in `dist/binaries/` into a `.zip` archive. |
| 5 | * |
| 6 | * Usage: `bun scripts/package-binaries.ts` |
| 7 | * |
| 8 | * Creates a nested directory structure inside each zip: |
| 9 | * composio-<target>/composio (composio.exe on Windows) |
| 10 | * |
| 11 | * This matches the structure expected by `install.sh` and `install.ps1`. |
| 12 | * |
| 13 | * Input: `dist/binaries/composio-{platform-arch}` (raw binaries) |
| 14 | * Output: `dist/binaries/composio-{platform-arch}.zip` |
| 15 | */ |
| 16 | |
| 17 | import process from 'node:process'; |
| 18 | import { Config, ConfigProvider, Console, Effect, Logger, Layer, References } from 'effect'; |
| 19 | import * as BunServices from '@effect/platform-bun/BunServices'; |
| 20 | import * as BunRuntime from '@effect/platform-bun/BunRuntime'; |
| 21 | import { teardown } from './_shared'; |
| 22 | import { $ } from 'bun'; |
| 23 | import { readdir, stat, writeFile } from 'node:fs/promises'; |
| 24 | import path from 'node:path'; |
| 25 | import { |
| 26 | collectExpectedRunCompanionAssetRelativePaths, |
| 27 | RELEASE_MANIFEST_FILENAME, |
| 28 | } from '../src/services/run-companion-modules'; |
| 29 | import { |
| 30 | archiveCompanionEntries, |
| 31 | archiveExecutableName, |
| 32 | buildReleaseManifest, |
| 33 | compiledBinaryFileName, |
| 34 | RELEASE_TARGETS, |
| 35 | serializeReleaseManifest, |
| 36 | } from './_release-artifacts'; |
| 37 | |
| 38 | const BINARIES_DIR = './dist/binaries'; |
| 39 | const COMPANIONS_DIR = path.join(BINARIES_DIR, 'companions'); |
| 40 | const RELEASE_TAG = process.env.RELEASE_TAG?.trim(); |
| 41 | |
| 42 | export function packageBinaries() { |
| 43 | return Effect.gen(function* () { |
| 44 | const entries = yield* Effect.tryPromise(() => readdir(BINARIES_DIR)); |
| 45 | |
| 46 | const binaries = RELEASE_TARGETS.map(({ artifact }) => artifact).filter(artifact => |
| 47 | entries.includes(compiledBinaryFileName(artifact)) |
| 48 | ); |
| 49 | |
| 50 | if (binaries.length === 0) { |
| 51 | yield* Console.error('No binaries found in dist/binaries/. Run build:binary:all first.'); |
| 52 | process.exitCode = 1; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Only the live companions come from `COMPANIONS_DIR`. The legacy placeholders |
| 57 | // are written straight into each archive and exist nowhere else. |
| 58 | const liveCompanionRelativePaths = |
| 59 | yield* collectExpectedRunCompanionAssetRelativePaths(COMPANIONS_DIR); |
| 60 | for (const relativePath of liveCompanionRelativePaths) { |
| 61 | const companionPath = path.join(COMPANIONS_DIR, relativePath); |
| 62 | const exists = yield* Effect.tryPromise(() => Bun.file(companionPath).exists()); |
| 63 | if (!exists) { |
| 64 | yield* Console.error( |
| 65 | `Missing companion module ${companionPath}. Run build:binary:all before packaging.` |
| 66 | ); |
| 67 | process.exitCode = 1; |
| 68 | return; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | yield* Console.log(`Packaging ${binaries.length} binaries...`); |
| 73 | |
| 74 | // See `archiveCompanionEntries` for why the legacy paths are present but empty. |
| 75 | const companionEntries = archiveCompanionEntries(liveCompanionRelativePaths); |
| 76 | const releaseManifest = serializeReleaseManifest( |
| 77 | buildReleaseManifest(liveCompanionRelativePaths) |
| 78 | ); |
| 79 | |
| 80 | for (const binary of binaries) { |
| 81 | const binaryPath = path.join(BINARIES_DIR, compiledBinaryFileName(binary)); |
| 82 | const zipPath = path.join(BINARIES_DIR, `${binary}.zip`); |
| 83 | const absoluteZipPath = path.resolve(zipPath); |
| 84 | |
| 85 | // Create nested directory structure: <artifact>/<binary-name> |
| 86 | const tempDir = path.join(BINARIES_DIR, `_pkg_${binary}`); |
| 87 | const nestedDir = path.join(tempDir, binary); |
| 88 | |
| 89 | yield* Effect.tryPromise(async () => { |
| 90 | await $`rm -f ${zipPath}`.quiet(); |
| 91 | await $`rm -rf ${tempDir}`.quiet(); |
| 92 | await $`mkdir -p ${nestedDir}`.quiet(); |
| 93 | await $`cp ${binaryPath} ${path.join(nestedDir, archiveExecutableName(binary))}`.quiet(); |
| 94 | for (const { relativePath, kind } of companionEntries) { |
| 95 | const destinationPath = path.join(nestedDir, relativePath); |
| 96 | await $`mkdir -p ${path.dirname(destinationPath)}`.quiet(); |
| 97 | if (kind === 'placeholder') { |
| 98 | await writeFile(destinationPath, ''); |
| 99 | continue; |
| 100 | } |
| 101 | await $`cp ${path.join(COMPANIONS_DIR, relativePath)} ${destinationPath}`.quiet(); |
| 102 | } |
| 103 | await writeFile(path.join(nestedDir, RELEASE_MANIFEST_FILENAME), releaseManifest, 'utf8'); |
| 104 | if (RELEASE_TAG) { |
| 105 | await writeFile(path.join(nestedDir, 'release-tag.txt'), `${RELEASE_TAG}\n`, 'utf8'); |
| 106 | } |
| 107 | const previousCwd = process.cwd(); |
| 108 | process.chdir(tempDir); |
| 109 | try { |
| 110 | await $`zip -r ${absoluteZipPath} ${binary}`.quiet(); |
| 111 | } finally { |
| 112 | process.chdir(previousCwd); |
| 113 | } |
| 114 | await $`rm -rf ${tempDir}`.quiet(); |
| 115 | }); |
| 116 | |
| 117 | const zipStat = yield* Effect.tryPromise(() => stat(zipPath)); |
| 118 | const sizeMB = (zipStat.size / (1024 * 1024)).toFixed(1); |
| 119 | yield* Console.log(` ${binary}.zip (${sizeMB} MB)`); |
| 120 | } |
| 121 | |
| 122 | yield* Console.log(`\nAll ${binaries.length} archives created.`); |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | const ConfigLive = Effect.gen(function* () { |
| 127 | const logLevel = yield* Config.LogLevel('COMPOSIO_LOG_LEVEL').pipe(Config.withDefault('Info')); |
| 128 | |
| 129 | return Layer.succeed(References.MinimumLogLevel, logLevel); |
| 130 | }).pipe(Layer.unwrap, Layer.merge(ConfigProvider.layer(ConfigProvider.fromEnv()))); |
| 131 | |
| 132 | if (require.main === module) { |
| 133 | packageBinaries().pipe( |
| 134 | Effect.provide(ConfigLive), |
| 135 | Effect.provide(Logger.layer([Logger.consolePretty()])), |
| 136 | Effect.provide(BunServices.layer), |
| 137 | Effect.scoped, |
| 138 | Effect.map(() => ({ message: 'Process completed successfully.' })), |
| 139 | BunRuntime.runMain({ |
| 140 | teardown, |
| 141 | }) |
| 142 | ); |
| 143 | } |