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 | // Companion-module asset resolution and self-repair helpers shared by the CLI runtime, |
| 2 | // the bundled `composio run` companion modules, and the binary build scripts. Every |
| 3 | // helper is an Effect over the @effect/platform FileSystem/Path services; consumers |
| 4 | // outside the CLI runtime (companion runtimes, scripts) provide their own platform layers. |
| 5 | import * as FileSystem from 'effect/FileSystem'; |
| 6 | import * as Path from 'effect/Path'; |
| 7 | import { Data, Effect, Option, PlatformError, Schema } from 'effect'; |
| 8 | import semver from 'semver'; |
| 9 | import { extractZipSafely } from 'src/utils/extract-zip-safely'; |
| 10 | import { APP_VERSION, IS_RELEASE_BUILD } from 'src/constants'; |
| 11 | import { GitHubRelease } from 'src/effects/resolve-cli-release'; |
| 12 | import { GITHUB_REPAIR_CONFIG } from 'src/config'; |
| 13 | import { atomicReplaceFile } from 'src/utils/atomic-replace'; |
| 14 | import { parseChecksumsText, sha256Hex } from 'src/utils/checksums'; |
| 15 | import { CLI_RELEASE_TAG_PREFIX } from 'src/utils/cli-release-version'; |
| 16 | |
| 17 | // Modules the binary build bundles separately (`dist/<name>.mjs` next to the |
| 18 | // executable) instead of into the executable itself. The first one is what |
| 19 | // `composio run` preloads into the script it spawns. The last one is loaded into |
| 20 | // the CLI's own process, on demand, through `loadInstalledCompanionModule`: it |
| 21 | // carries the TypeScript compiler, which was 44% of the executable's JavaScript |
| 22 | // and cost every command parse time even though only `generate` and `run` |
| 23 | // reach it. |
| 24 | export const RUN_COMPANION_MODULE_BASENAMES: ReadonlyArray<string> = [ |
| 25 | 'run-helpers-runtime', |
| 26 | 'generation-runtime', |
| 27 | ]; |
| 28 | |
| 29 | export const RUN_COMPANION_MODULE_FILENAMES = RUN_COMPANION_MODULE_BASENAMES.map( |
| 30 | name => `${name}.mjs` |
| 31 | ); |
| 32 | |
| 33 | export const RUN_COMPANION_RELEASE_TAG_FILENAME = 'release-tag.txt'; |
| 34 | |
| 35 | export const RELEASE_MANIFEST_FILENAME = 'release-manifest.json'; |
| 36 | |
| 37 | export const RELEASE_MANIFEST_SCHEMA_VERSION = 1; |
| 38 | |
| 39 | // Paths the removed `experimental_subAgent` helper used to ship. Release archives |
| 40 | // keep them as zero-byte files because every stable CLI from 0.2.12 to 0.4.2 |
| 41 | // verifies a downloaded upgrade package against its own copy of this list, by |
| 42 | // existence only, and refuses a package that lacks any of them. |
| 43 | // |
| 44 | // Frozen: nothing derives it from live code, so it must not change while those |
| 45 | // clients can still upgrade. It stays out of `RUN_COMPANION_MODULE_BASENAMES`, |
| 46 | // which drives the bundler; packaging is the only writer, so installs and source |
| 47 | // checkouts never require these files. |
| 48 | // `.github/scripts/cli-release/verify-archive-companions.sh` carries the same list. |
| 49 | export const RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS: ReadonlyArray<string> = [ |
| 50 | 'run-subagent-shared.mjs', |
| 51 | 'run-subagent-acp.mjs', |
| 52 | 'run-subagent-legacy.mjs', |
| 53 | 'run-subagent-output-mcp.mjs', |
| 54 | 'acp-adapters/claude-code-acp.mjs', |
| 55 | 'acp-adapters/cli.js', |
| 56 | 'acp-adapters/codex/darwin-arm64/codex-acp', |
| 57 | 'acp-adapters/codex/darwin-x64/codex-acp', |
| 58 | 'acp-adapters/codex/linux-arm64/codex-acp', |
| 59 | 'acp-adapters/codex/linux-x64/codex-acp', |
| 60 | ]; |
| 61 | |
| 62 | const isArchiveRelativePath = (value: string): boolean => |
| 63 | !value.startsWith('/') && |
| 64 | !value.includes('\\') && |
| 65 | !/^[A-Za-z]:/.test(value) && |
| 66 | value.split('/').every(segment => segment !== '' && segment !== '.' && segment !== '..'); |
| 67 | |
| 68 | const ArchiveRelativePath = Schema.String.check( |
| 69 | Schema.makeFilter(isArchiveRelativePath, { |
| 70 | expected: 'a relative POSIX path with no empty, "." or ".." segments', |
| 71 | }) |
| 72 | ); |
| 73 | |
| 74 | const isSortedAndUnique = (values: ReadonlyArray<string>): boolean => |
| 75 | values.every((value, index) => index === 0 || values[index - 1]! < value); |
| 76 | |
| 77 | /** |
| 78 | * What a release archive says it contains, written by packaging as |
| 79 | * `release-manifest.json` next to the executable. A client that reads it checks |
| 80 | * the download against `companions` instead of the companion names compiled |
| 81 | * into itself, so a later archive can drop a file without breaking upgrades. |
| 82 | * |
| 83 | * Adding a field keeps schema version 1, since decoding ignores unknown keys. A |
| 84 | * change an older reader would misread bumps the version, and a client refuses a |
| 85 | * version it does not know. |
| 86 | */ |
| 87 | export const ReleaseManifest = Schema.Struct({ |
| 88 | schemaVersion: Schema.Literal(RELEASE_MANIFEST_SCHEMA_VERSION), |
| 89 | /** The earliest CLI version that may upgrade to this archive. */ |
| 90 | minimumUpgradeFrom: Schema.String.check( |
| 91 | Schema.makeFilter(value => semver.valid(value) === value, { expected: 'a semver version' }) |
| 92 | ), |
| 93 | companions: Schema.NonEmptyArray(ArchiveRelativePath).check( |
| 94 | Schema.makeFilter(isSortedAndUnique, { expected: 'sorted, unique paths' }) |
| 95 | ), |
| 96 | }); |
| 97 | export type ReleaseManifest = typeof ReleaseManifest.Type; |
| 98 | |
| 99 | const ReleaseManifestVersion = Schema.fromJsonString( |
| 100 | Schema.Struct({ schemaVersion: Schema.Unknown }) |
| 101 | ); |
| 102 | const ReleaseManifestFromJson = Schema.fromJsonString(ReleaseManifest); |
| 103 | |
| 104 | export class ReleaseManifestError extends Data.TaggedError('services/ReleaseManifestError')<{ |
| 105 | readonly message: string; |
| 106 | readonly cause?: unknown; |
| 107 | }> {} |
| 108 | |
| 109 | export type ArchiveCompanions = Data.TaggedEnum<{ |
| 110 | Manifest: { readonly manifest: ReleaseManifest; readonly relativePaths: ReadonlyArray<string> }; |
| 111 | CompiledNames: { readonly relativePaths: ReadonlyArray<string> }; |
| 112 | }>; |
| 113 | export const ArchiveCompanions = Data.taggedEnum<ArchiveCompanions>(); |
| 114 | |
| 115 | const manifestError = (message: string) => (cause: unknown) => |
| 116 | new ReleaseManifestError({ message, cause }); |
| 117 | |
| 118 | const decodeReleaseManifest = ( |
| 119 | text: string |
| 120 | ): Effect.Effect<ReleaseManifest, ReleaseManifestError> => |
| 121 | Effect.gen(function* () { |
| 122 | const { schemaVersion } = yield* Schema.decodeUnknownEffect(ReleaseManifestVersion)(text).pipe( |
| 123 | Effect.mapError(manifestError(`${RELEASE_MANIFEST_FILENAME} is not valid JSON`)) |
| 124 | ); |
| 125 | if (schemaVersion !== RELEASE_MANIFEST_SCHEMA_VERSION) { |
| 126 | return yield* new ReleaseManifestError({ |
| 127 | message: `${RELEASE_MANIFEST_FILENAME} has schema version ${String(schemaVersion)}, which this CLI cannot read`, |
| 128 | }); |
| 129 | } |
| 130 | return yield* Schema.decodeUnknownEffect(ReleaseManifestFromJson)(text).pipe( |
| 131 | Effect.mapError(manifestError(`${RELEASE_MANIFEST_FILENAME} is invalid`)) |
| 132 | ); |
| 133 | }); |
| 134 | |
| 135 | /** |
| 136 | * Resolves what the release package extracted at `packageDir` provides. A |
| 137 | * manifest that is present but unreadable or invalid fails: the archive is |
| 138 | * broken, and guessing from compiled names could install a partial release. |
| 139 | */ |
| 140 | export const resolveArchiveCompanions = ( |
| 141 | packageDir: string |
| 142 | ): Effect.Effect<ArchiveCompanions, ReleaseManifestError, FileSystem.FileSystem | Path.Path> => |
| 143 | Effect.gen(function* () { |
| 144 | const fs = yield* FileSystem.FileSystem; |
| 145 | const path = yield* Path.Path; |
| 146 | const manifestText = yield* fs |
| 147 | .readFileString(path.join(packageDir, RELEASE_MANIFEST_FILENAME), 'utf8') |
| 148 | .pipe( |
| 149 | Effect.map(Option.some), |
| 150 | Effect.catchReason('PlatformError', 'NotFound', () => Effect.succeed(Option.none())), |
| 151 | Effect.mapError(manifestError(`Cannot read ${RELEASE_MANIFEST_FILENAME}`)) |
| 152 | ); |
| 153 | if (Option.isNone(manifestText)) { |
| 154 | return ArchiveCompanions.CompiledNames({ |
| 155 | relativePaths: yield* collectExpectedRunCompanionAssetRelativePaths(packageDir), |
| 156 | }); |
| 157 | } |
| 158 | const manifest = yield* decodeReleaseManifest(manifestText.value); |
| 159 | return ArchiveCompanions.Manifest({ manifest, relativePaths: manifest.companions }); |
| 160 | }); |
| 161 | |
| 162 | export class RunCompanionRepairError extends Data.TaggedError('services/RunCompanionRepairError')<{ |
| 163 | readonly message: string; |
| 164 | readonly cause?: unknown; |
| 165 | }> {} |
| 166 | |
| 167 | const relativeImportPattern = |
| 168 | /(?:import\s+(?:[^'"]+?\s+from\s+)?|export\s+(?:\*\s+from\s+|\{[^}]+\}\s+from\s+)|import\s*\()\s*["'](\.{1,2}\/[^"']+?\.mjs)["']/g; |
| 169 | |
| 170 | const isImportGraphFile = (relativePath: string) => /\.(?:m?js|ts)$/.test(relativePath); |
| 171 | |
| 172 | const fileExists = (fs: FileSystem.FileSystem, filePath: string) => |
| 173 | fs.exists(filePath).pipe(Effect.orElseSucceed(() => false)); |
| 174 | |
| 175 | export const isBunVirtualPath = (filePath: string): boolean => |
| 176 | /^(?:\/?[A-Za-z]:)?[/\\]\$bunfs[/\\]/.test(filePath) || |
| 177 | /^\/?[A-Za-z]:[/\\]~BUN[/\\]/.test(filePath); |
| 178 | |
| 179 | const filePathFromUrl = (path: Path.Path, url: string): Effect.Effect<string> => |
| 180 | Schema.decodeUnknownEffect(Schema.URLFromString)(url).pipe( |
| 181 | Effect.flatMap(path.fromFileUrl), |
| 182 | Effect.orDie |
| 183 | ); |
| 184 | |
| 185 | const collectRelativeImportPaths = ({ |
| 186 | fs, |
| 187 | path, |
| 188 | rootDir, |
| 189 | relativePath, |
| 190 | collected, |
| 191 | recordMissingPaths = false, |
| 192 | }: { |
| 193 | fs: FileSystem.FileSystem; |
| 194 | path: Path.Path; |
| 195 | rootDir: string; |
| 196 | relativePath: string; |
| 197 | collected: Set<string>; |
| 198 | recordMissingPaths?: boolean; |
| 199 | }): Effect.Effect<void> => |
| 200 | Effect.gen(function* () { |
| 201 | const normalizedRelativePath = relativePath.replaceAll(path.sep, '/'); |
| 202 | if (collected.has(normalizedRelativePath)) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | const absolutePath = path.join(rootDir, normalizedRelativePath); |
| 207 | const exists = yield* fileExists(fs, absolutePath); |
| 208 | if (!exists && !recordMissingPaths) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | collected.add(normalizedRelativePath); |
| 213 | if (!exists) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (!isImportGraphFile(normalizedRelativePath)) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | const source = yield* Effect.orDie(fs.readFileString(absolutePath, 'utf8')); |
| 222 | for (const match of source.matchAll(relativeImportPattern)) { |
| 223 | const specifier = match[1]; |
| 224 | if (!specifier) { |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | const dependencyRelativePath = path |
| 229 | .relative(rootDir, path.resolve(path.dirname(absolutePath), specifier)) |
| 230 | .replaceAll(path.sep, '/'); |
| 231 | |
| 232 | yield* collectRelativeImportPaths({ |
| 233 | fs, |
| 234 | path, |
| 235 | rootDir, |
| 236 | relativePath: dependencyRelativePath, |
| 237 | collected, |
| 238 | recordMissingPaths, |
| 239 | }); |
| 240 | } |
| 241 | }); |
| 242 | |
| 243 | /** |
| 244 | * Relative paths an install rooted at `rootDir` is expected to contain: the |
| 245 | * companion wrappers and every relative import reachable from them. |
| 246 | */ |
| 247 | export const collectExpectedRunCompanionAssetRelativePaths = ( |
| 248 | rootDir: string |
| 249 | ): Effect.Effect<ReadonlyArray<string>, never, FileSystem.FileSystem | Path.Path> => |
| 250 | Effect.gen(function* () { |
| 251 | const fs = yield* FileSystem.FileSystem; |
| 252 | const path = yield* Path.Path; |
| 253 | const collected = new Set<string>(); |
| 254 | |
| 255 | for (const fileName of RUN_COMPANION_MODULE_FILENAMES) { |
| 256 | yield* collectRelativeImportPaths({ |
| 257 | fs, |
| 258 | path, |
| 259 | rootDir, |
| 260 | relativePath: fileName, |
| 261 | collected, |
| 262 | recordMissingPaths: true, |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | return [...collected].sort(); |
| 267 | }); |
| 268 | |
| 269 | export const resolveBinaryAssetName = ({ |
| 270 | platform = process.platform, |
| 271 | arch = process.arch, |
| 272 | }: { |
| 273 | platform?: NodeJS.Platform; |
| 274 | arch?: string; |
| 275 | }) => { |
| 276 | switch (`${platform}-${arch}`) { |
| 277 | case 'darwin-arm64': |
| 278 | return 'composio-darwin-aarch64.zip'; |
| 279 | case 'darwin-x64': |
| 280 | return 'composio-darwin-x64.zip'; |
| 281 | case 'linux-x64': |
| 282 | return 'composio-linux-x64.zip'; |
| 283 | case 'linux-arm64': |
| 284 | return 'composio-linux-aarch64.zip'; |
| 285 | case 'win32-x64': |
| 286 | return 'composio-windows-x64.zip'; |
| 287 | default: |
| 288 | return undefined; |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | const readTextFileIfPresent = (fs: FileSystem.FileSystem, filePath: string) => |
| 293 | Effect.gen(function* () { |
| 294 | const exists = yield* fileExists(fs, filePath); |
| 295 | if (!exists) { |
| 296 | return undefined; |
| 297 | } |
| 298 | |
| 299 | const value = (yield* Effect.orDie(fs.readFileString(filePath, 'utf8'))).trim(); |
| 300 | return value.length > 0 ? value : undefined; |
| 301 | }); |
| 302 | |
| 303 | export const readInstalledReleaseTag = ( |
| 304 | execPath: string |
| 305 | ): Effect.Effect<string | undefined, never, FileSystem.FileSystem | Path.Path> => |
| 306 | Effect.gen(function* () { |
| 307 | const fs = yield* FileSystem.FileSystem; |
| 308 | const path = yield* Path.Path; |
| 309 | return yield* readTextFileIfPresent( |
| 310 | fs, |
| 311 | path.join(path.dirname(execPath), RUN_COMPANION_RELEASE_TAG_FILENAME) |
| 312 | ); |
| 313 | }); |
| 314 | |
| 315 | export const normalizeCliReleaseVersion = (releaseIdentifier: string): string => { |
| 316 | const trimmed = releaseIdentifier.trim(); |
| 317 | if (trimmed.startsWith(CLI_RELEASE_TAG_PREFIX)) { |
| 318 | return trimmed.slice(CLI_RELEASE_TAG_PREFIX.length); |
| 319 | } |
| 320 | if (/^v\d+\.\d+\.\d+(?:[-+].*)?$/.test(trimmed)) { |
| 321 | return trimmed.slice(1); |
| 322 | } |
| 323 | return trimmed; |
| 324 | }; |
| 325 | |
| 326 | export const normalizeCliReleaseTag = (releaseIdentifier: string): string => |
| 327 | `${CLI_RELEASE_TAG_PREFIX}${normalizeCliReleaseVersion(releaseIdentifier)}`; |
| 328 | |
| 329 | export const resolveInstalledCliVersion = ( |
| 330 | execPath: string, |
| 331 | fallbackVersion: string |
| 332 | ): Effect.Effect<string, never, FileSystem.FileSystem | Path.Path> => |
| 333 | Effect.map(readInstalledReleaseTag(execPath), releaseTag => |
| 334 | normalizeCliReleaseVersion(releaseTag ?? fallbackVersion) |
| 335 | ); |
| 336 | |
| 337 | export const resolveInstalledCliReleaseTag = ( |
| 338 | execPath: string, |
| 339 | fallbackVersion: string |
| 340 | ): Effect.Effect<string, never, FileSystem.FileSystem | Path.Path> => |
| 341 | Effect.map(readInstalledReleaseTag(execPath), releaseTag => |
| 342 | normalizeCliReleaseTag(releaseTag ?? fallbackVersion) |
| 343 | ); |
| 344 | |
| 345 | export const resolveRunningCliVersion = ( |
| 346 | execPath: string, |
| 347 | appVersion: string, |
| 348 | isReleaseBuild = IS_RELEASE_BUILD |
| 349 | ): Effect.Effect<string, never, FileSystem.FileSystem | Path.Path> => |
| 350 | isReleaseBuild |
| 351 | ? Effect.succeed(normalizeCliReleaseVersion(appVersion)) |
| 352 | : resolveInstalledCliVersion(execPath, appVersion); |
| 353 | |
| 354 | export const resolveRecordedRunningCliVersion = ( |
| 355 | execPath: string, |
| 356 | appVersion: string, |
| 357 | isReleaseBuild = IS_RELEASE_BUILD |
| 358 | ): Effect.Effect<Option.Option<string>, never, FileSystem.FileSystem | Path.Path> => |
| 359 | isReleaseBuild |
| 360 | ? Effect.succeed(Option.some(normalizeCliReleaseVersion(appVersion))) |
| 361 | : Effect.map(readInstalledReleaseTag(execPath), releaseTag => |
| 362 | Option.map(Option.fromNullishOr(releaseTag), normalizeCliReleaseVersion) |
| 363 | ); |
| 364 | |
| 365 | export const resolveRunningCliReleaseTag = ( |
| 366 | execPath: string, |
| 367 | appVersion: string, |
| 368 | isReleaseBuild = IS_RELEASE_BUILD |
| 369 | ): Effect.Effect<string, never, FileSystem.FileSystem | Path.Path> => |
| 370 | Effect.map( |
| 371 | resolveRunningCliVersion(execPath, appVersion, isReleaseBuild), |
| 372 | normalizeCliReleaseTag |
| 373 | ); |
| 374 | |
| 375 | export const writeInstalledReleaseTag = ( |
| 376 | installDir: string, |
| 377 | releaseTag: string |
| 378 | ): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> => |
| 379 | Effect.gen(function* () { |
| 380 | const fs = yield* FileSystem.FileSystem; |
| 381 | const path = yield* Path.Path; |
| 382 | yield* fs.writeFileString( |
| 383 | path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME), |
| 384 | `${releaseTag}\n` |
| 385 | ); |
| 386 | }); |
| 387 | |
| 388 | /** |
| 389 | * The companion wrappers and their import graph, listed when missing. |
| 390 | * |
| 391 | * `composio run` preloads `run-helpers-runtime` into the spawned child, and the |
| 392 | * CLI loads the in-process ones on demand, so a missing one really is a broken |
| 393 | * install and justifies the self-repair download. |
| 394 | */ |
| 395 | export const listMissingInstalledRunCompanionModules = ( |
| 396 | execPath: string |
| 397 | ): Effect.Effect<ReadonlyArray<string>, never, FileSystem.FileSystem | Path.Path> => |
| 398 | Effect.gen(function* () { |
| 399 | const fs = yield* FileSystem.FileSystem; |
| 400 | const path = yield* Path.Path; |
| 401 | const installDirectory = path.dirname(execPath); |
| 402 | const expectedRelativePaths = |
| 403 | yield* collectExpectedRunCompanionAssetRelativePaths(installDirectory); |
| 404 | return yield* Effect.filter(expectedRelativePaths, relativePath => |
| 405 | Effect.map(fileExists(fs, path.join(installDirectory, relativePath)), exists => !exists) |
| 406 | ); |
| 407 | }); |
| 408 | |
| 409 | /** |
| 410 | * The files one companion needs next to the executable: its wrapper and every |
| 411 | * relative import reachable from it, listed when missing. |
| 412 | */ |
| 413 | const listMissingInstalledCompanionModuleFiles = ( |
| 414 | execPath: string, |
| 415 | baseName: string |
| 416 | ): Effect.Effect<ReadonlyArray<string>, never, FileSystem.FileSystem | Path.Path> => |
| 417 | Effect.gen(function* () { |
| 418 | const fs = yield* FileSystem.FileSystem; |
| 419 | const path = yield* Path.Path; |
| 420 | const installDirectory = path.dirname(execPath); |
| 421 | const collected = new Set<string>(); |
| 422 | yield* collectRelativeImportPaths({ |
| 423 | fs, |
| 424 | path, |
| 425 | rootDir: installDirectory, |
| 426 | relativePath: `${baseName}.mjs`, |
| 427 | collected, |
| 428 | recordMissingPaths: true, |
| 429 | }); |
| 430 | return yield* Effect.filter([...collected], relativePath => |
| 431 | Effect.map(fileExists(fs, path.join(installDirectory, relativePath)), exists => !exists) |
| 432 | ); |
| 433 | }); |
| 434 | |
| 435 | const fetchGitHubJson = async <A, I>( |
| 436 | schema: Schema.Codec<A, I>, |
| 437 | { |
| 438 | url, |
| 439 | accessToken, |
| 440 | fetchErrorMessage, |
| 441 | }: { |
| 442 | url: string; |
| 443 | accessToken?: string; |
| 444 | fetchErrorMessage: string; |
| 445 | } |
| 446 | ): Promise<A> => { |
| 447 | const response = await fetch(url, { |
| 448 | headers: { |
| 449 | Accept: 'application/vnd.github+json', |
| 450 | 'User-Agent': 'composio-cli-run-repair', |
| 451 | ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}), |
| 452 | }, |
| 453 | }); |
| 454 | |
| 455 | if (!response.ok) { |
| 456 | const body = await response.text().catch(() => ''); |
| 457 | throw new Error(`${fetchErrorMessage} (HTTP ${response.status}${body ? `: ${body}` : ''})`); |
| 458 | } |
| 459 | |
| 460 | return Schema.decodeUnknownPromise(schema)(await response.json()); |
| 461 | }; |
| 462 | |
| 463 | const fetchChecksums = async ({ |
| 464 | release, |
| 465 | accessToken, |
| 466 | }: { |
| 467 | release: GitHubRelease; |
| 468 | accessToken?: string; |
| 469 | }) => { |
| 470 | const checksumsAsset = release.assets.find(asset => asset.name === 'checksums.txt'); |
| 471 | if (!checksumsAsset) { |
| 472 | return undefined; |
| 473 | } |
| 474 | |
| 475 | const response = await fetch(checksumsAsset.browser_download_url, { |
| 476 | headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined, |
| 477 | }); |
| 478 | |
| 479 | if (!response.ok) { |
| 480 | return undefined; |
| 481 | } |
| 482 | |
| 483 | return parseChecksumsText(await response.text()); |
| 484 | }; |
| 485 | |
| 486 | const verifyChecksum = async ({ |
| 487 | data, |
| 488 | expectedHash, |
| 489 | fileName, |
| 490 | }: { |
| 491 | data: Uint8Array; |
| 492 | expectedHash: string; |
| 493 | fileName: string; |
| 494 | }) => { |
| 495 | const actualHash = await sha256Hex(data); |
| 496 | |
| 497 | if (actualHash !== expectedHash) { |
| 498 | throw new Error( |
| 499 | `Checksum mismatch while repairing ${fileName}\n Expected: ${expectedHash}\n Actual: ${actualHash}` |
| 500 | ); |
| 501 | } |
| 502 | }; |
| 503 | |
| 504 | const toRepairError = (error: unknown) => |
| 505 | new RunCompanionRepairError({ |
| 506 | message: error instanceof Error ? error.message : String(error), |
| 507 | cause: error, |
| 508 | }); |
| 509 | |
| 510 | // GITHUB_TAG pins the release used for self-repair (set by the binary build workflow). |
| 511 | const resolveRepairReleaseTag = ({ |
| 512 | pinnedTag, |
| 513 | execPath, |
| 514 | appVersion, |
| 515 | }: { |
| 516 | pinnedTag: string | undefined; |
| 517 | execPath: string; |
| 518 | appVersion: string; |
| 519 | }) => |
| 520 | pinnedTag === undefined |
| 521 | ? resolveRunningCliReleaseTag(execPath, appVersion) |
| 522 | : Effect.succeed(pinnedTag); |
| 523 | |
| 524 | /** |
| 525 | * Restores a packaged install whose companion wrappers went missing, from the |
| 526 | * release archive of the running version. The archive's legacy placeholders are |
| 527 | * not companions and are left behind. |
| 528 | */ |
| 529 | export const repairMissingInstalledRunCompanionModules = ({ |
| 530 | callerImportMetaUrl, |
| 531 | execPath, |
| 532 | appVersion, |
| 533 | companionBaseName, |
| 534 | }: { |
| 535 | callerImportMetaUrl: string; |
| 536 | execPath: string; |
| 537 | appVersion: string; |
| 538 | /** |
| 539 | * Repair only when this companion's own files are missing, rather than when |
| 540 | * any companion file is. The repair still restores the full set. |
| 541 | */ |
| 542 | companionBaseName?: string; |
| 543 | }): Effect.Effect< |
| 544 | { readonly repaired: false } | { readonly repaired: true; readonly releaseTag: string }, |
| 545 | RunCompanionRepairError | PlatformError.PlatformError, |
| 546 | FileSystem.FileSystem | Path.Path |
| 547 | > => |
| 548 | Effect.gen(function* () { |
| 549 | const fs = yield* FileSystem.FileSystem; |
| 550 | const path = yield* Path.Path; |
| 551 | |
| 552 | const currentFilePath = yield* filePathFromUrl(path, callerImportMetaUrl); |
| 553 | if (!isBunVirtualPath(currentFilePath)) { |
| 554 | return { repaired: false as const }; |
| 555 | } |
| 556 | |
| 557 | const missingModules = |
| 558 | companionBaseName === undefined |
| 559 | ? yield* listMissingInstalledRunCompanionModules(execPath) |
| 560 | : yield* listMissingInstalledCompanionModuleFiles(execPath, companionBaseName); |
| 561 | if (missingModules.length === 0) { |
| 562 | return { repaired: false as const }; |
| 563 | } |
| 564 | |
| 565 | const githubConfig = yield* Effect.orDie(GITHUB_REPAIR_CONFIG); |
| 566 | const releaseTag = yield* resolveRepairReleaseTag({ |
| 567 | pinnedTag: githubConfig.tag, |
| 568 | execPath, |
| 569 | appVersion, |
| 570 | }); |
| 571 | |
| 572 | const encodedTag = encodeURIComponent(releaseTag); |
| 573 | const release = yield* Effect.tryPromise({ |
| 574 | try: () => |
| 575 | fetchGitHubJson(GitHubRelease, { |
| 576 | url: `${githubConfig.apiBaseUrl}/repos/${githubConfig.owner}/${githubConfig.repo}/releases/tags/${encodedTag}`, |
| 577 | accessToken: githubConfig.accessToken, |
| 578 | fetchErrorMessage: `Failed to fetch release metadata for ${releaseTag} while repairing run companion modules`, |
| 579 | }), |
| 580 | catch: error => |
| 581 | new RunCompanionRepairError({ |
| 582 | message: [ |
| 583 | `Unable to restore the CLI's bundled support files for ${releaseTag}.`, |
| 584 | error instanceof Error ? error.message : String(error), |
| 585 | `Reinstall the CLI, or set GITHUB_TAG to the exact release tag for this build and try again.`, |
| 586 | ].join('\n'), |
| 587 | cause: error, |
| 588 | }), |
| 589 | }); |
| 590 | |
| 591 | const assetName = resolveBinaryAssetName({}); |
| 592 | if (!assetName) { |
| 593 | return yield* Effect.fail( |
| 594 | new RunCompanionRepairError({ |
| 595 | message: `Unsupported platform for run companion repair: ${process.platform}-${process.arch}`, |
| 596 | }) |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | const asset = release.assets.find(candidate => candidate.name === assetName); |
| 601 | if (!asset) { |
| 602 | return yield* Effect.fail( |
| 603 | new RunCompanionRepairError({ |
| 604 | message: `Release ${release.tag_name} does not contain ${assetName}; cannot restore run companion modules.`, |
| 605 | }) |
| 606 | ); |
| 607 | } |
| 608 | |
| 609 | const archiveData = yield* Effect.tryPromise({ |
| 610 | try: async () => { |
| 611 | const archiveResponse = await fetch(asset.browser_download_url, { |
| 612 | headers: githubConfig.accessToken |
| 613 | ? { Authorization: `Bearer ${githubConfig.accessToken}` } |
| 614 | : undefined, |
| 615 | }); |
| 616 | if (!archiveResponse.ok) { |
| 617 | throw new Error( |
| 618 | `Failed to download ${asset.name} from ${release.tag_name} while repairing run companion modules (HTTP ${archiveResponse.status}).` |
| 619 | ); |
| 620 | } |
| 621 | return new Uint8Array(await archiveResponse.arrayBuffer()); |
| 622 | }, |
| 623 | catch: toRepairError, |
| 624 | }); |
| 625 | |
| 626 | const checksums = yield* Effect.tryPromise({ |
| 627 | try: () => |
| 628 | fetchChecksums({ |
| 629 | release, |
| 630 | accessToken: githubConfig.accessToken, |
| 631 | }), |
| 632 | catch: toRepairError, |
| 633 | }); |
| 634 | const expectedChecksum = checksums?.get(asset.name); |
| 635 | if (expectedChecksum) { |
| 636 | yield* Effect.tryPromise({ |
| 637 | try: () => |
| 638 | verifyChecksum({ |
| 639 | data: archiveData, |
| 640 | expectedHash: expectedChecksum, |
| 641 | fileName: asset.name, |
| 642 | }), |
| 643 | catch: toRepairError, |
| 644 | }); |
| 645 | } |
| 646 | |
| 647 | return yield* Effect.scoped( |
| 648 | Effect.gen(function* () { |
| 649 | const tempDirectory = yield* fs.makeTempDirectoryScoped({ |
| 650 | prefix: 'composio-run-repair-', |
| 651 | }); |
| 652 | const archivePath = path.join(tempDirectory, asset.name); |
| 653 | const extractDirectory = path.join(tempDirectory, 'extract'); |
| 654 | const packageDirectory = path.join(extractDirectory, path.parse(asset.name).name); |
| 655 | yield* fs.writeFile(archivePath, archiveData); |
| 656 | yield* fs.makeDirectory(extractDirectory, { recursive: true }); |
| 657 | yield* Effect.tryPromise({ |
| 658 | try: () => extractZipSafely(archivePath, extractDirectory), |
| 659 | catch: toRepairError, |
| 660 | }); |
| 661 | |
| 662 | const installDirectory = path.dirname(execPath); |
| 663 | const archiveCompanions = yield* resolveArchiveCompanions(packageDirectory).pipe( |
| 664 | Effect.mapError( |
| 665 | error => |
| 666 | new RunCompanionRepairError({ |
| 667 | message: `Release ${release.tag_name} has a broken ${RELEASE_MANIFEST_FILENAME} (${error.message}); cannot restore the CLI's bundled support files. Reinstall the CLI and try again.`, |
| 668 | cause: error, |
| 669 | }) |
| 670 | ) |
| 671 | ); |
| 672 | const restoredRelativePaths = ArchiveCompanions.$is('Manifest')(archiveCompanions) |
| 673 | ? [...archiveCompanions.relativePaths, RELEASE_MANIFEST_FILENAME] |
| 674 | : archiveCompanions.relativePaths; |
| 675 | |
| 676 | for (const relativePath of restoredRelativePaths) { |
| 677 | const sourcePath = path.join(packageDirectory, relativePath); |
| 678 | const sourceExists = yield* fileExists(fs, sourcePath); |
| 679 | if (!sourceExists) { |
| 680 | return yield* Effect.fail( |
| 681 | new RunCompanionRepairError({ |
| 682 | message: `Release ${release.tag_name} is missing ${relativePath}; cannot restore the CLI's bundled support files.`, |
| 683 | }) |
| 684 | ); |
| 685 | } |
| 686 | |
| 687 | const targetPath = path.join(installDirectory, relativePath); |
| 688 | yield* fs.makeDirectory(path.dirname(targetPath), { recursive: true }); |
| 689 | yield* atomicReplaceFile({ sourcePath, targetPath }).pipe( |
| 690 | Effect.mapError( |
| 691 | error => |
| 692 | new RunCompanionRepairError({ |
| 693 | message: [ |
| 694 | `Unable to restore the CLI's bundled support files for ${releaseTag}.`, |
| 695 | error.message, |
| 696 | `Reinstall the CLI, or set GITHUB_TAG to the exact release tag for this build and try again.`, |
| 697 | ].join('\n'), |
| 698 | cause: error.cause, |
| 699 | }) |
| 700 | ) |
| 701 | ); |
| 702 | } |
| 703 | |
| 704 | yield* writeInstalledReleaseTag(installDirectory, release.tag_name); |
| 705 | return { |
| 706 | repaired: true as const, |
| 707 | releaseTag: release.tag_name, |
| 708 | }; |
| 709 | }) |
| 710 | ); |
| 711 | }); |
| 712 | |
| 713 | export const resolveRunCompanionModulePath = ({ |
| 714 | callerImportMetaUrl, |
| 715 | execPath, |
| 716 | relativeNoExtensionFromCaller, |
| 717 | }: { |
| 718 | callerImportMetaUrl: string; |
| 719 | execPath: string; |
| 720 | relativeNoExtensionFromCaller: string; |
| 721 | }): Effect.Effect<string, never, FileSystem.FileSystem | Path.Path> => |
| 722 | Effect.gen(function* () { |
| 723 | const fs = yield* FileSystem.FileSystem; |
| 724 | const path = yield* Path.Path; |
| 725 | const currentFilePath = yield* filePathFromUrl(path, callerImportMetaUrl); |
| 726 | const currentDirectory = path.dirname(currentFilePath); |
| 727 | const executableDirectory = path.dirname(execPath); |
| 728 | const baseName = path.basename(relativeNoExtensionFromCaller); |
| 729 | |
| 730 | const candidates = [ |
| 731 | path.resolve(currentDirectory, `${relativeNoExtensionFromCaller}.ts`), |
| 732 | path.resolve(currentDirectory, `${relativeNoExtensionFromCaller}.js`), |
| 733 | path.resolve(currentDirectory, 'services', `${baseName}.mjs`), |
| 734 | path.resolve(currentDirectory, 'services', `${baseName}.js`), |
| 735 | path.resolve(currentDirectory, `${baseName}.mjs`), |
| 736 | path.resolve(currentDirectory, `${baseName}.js`), |
| 737 | path.resolve(executableDirectory, `${baseName}.mjs`), |
| 738 | path.resolve(executableDirectory, `${baseName}.js`), |
| 739 | ]; |
| 740 | |
| 741 | const found = yield* Effect.findFirst(candidates, candidate => fileExists(fs, candidate)); |
| 742 | return Option.getOrElse(found, () => |
| 743 | isBunVirtualPath(currentFilePath) |
| 744 | ? path.resolve(executableDirectory, `${baseName}.mjs`) |
| 745 | : path.resolve(currentDirectory, `${baseName}.mjs`) |
| 746 | ); |
| 747 | }); |
| 748 | |
| 749 | /** |
| 750 | * Loads one of the in-process companion modules (see the note on |
| 751 | * `RUN_COMPANION_MODULE_BASENAMES`) and returns its exports. |
| 752 | * |
| 753 | * Resolution is the same as for the `run` companions: the `.ts` source next to |
| 754 | * this file when running from a checkout (tests, `bun run src/bin.ts`), the |
| 755 | * `.mjs` bundle next to the executable in a packaged install. |
| 756 | * |
| 757 | * A packaged install missing this module's own files goes through the |
| 758 | * release-archive repair `composio run` uses before the import. Files of other |
| 759 | * companions do not trigger it, so an offline repair failure cannot block a |
| 760 | * module that is already in place. The repair has to come first: Bun keeps a |
| 761 | * failed or already-loaded import in its module registry, so importing again |
| 762 | * after a repair can still see the old result. |
| 763 | * |
| 764 | * A module that loads but lacks one of `requiredExports` is a file left behind |
| 765 | * by a different release; it fails with a typed error asking for a reinstall. |
| 766 | * |
| 767 | * The specifier is computed at runtime on purpose: a literal `import('./x')` |
| 768 | * would make the bundler fold the module back into the executable, which is the |
| 769 | * exact thing these modules exist to avoid. |
| 770 | * |
| 771 | * `M` is the module's type; pass `typeof import('src/services/<name>')`, which |
| 772 | * is type-only and leaves no import behind. |
| 773 | */ |
| 774 | export const loadInstalledCompanionModule = <M>( |
| 775 | baseName: string, |
| 776 | requiredExports: ReadonlyArray<keyof M & string> |
| 777 | ): Effect.Effect< |
| 778 | M, |
| 779 | RunCompanionRepairError | PlatformError.PlatformError, |
| 780 | FileSystem.FileSystem | Path.Path |
| 781 | > => |
| 782 | Effect.gen(function* () { |
| 783 | const path = yield* Path.Path; |
| 784 | const callerImportMetaUrl = import.meta.url; |
| 785 | const execPath = process.execPath; |
| 786 | |
| 787 | yield* repairMissingInstalledRunCompanionModules({ |
| 788 | callerImportMetaUrl, |
| 789 | execPath, |
| 790 | appVersion: APP_VERSION, |
| 791 | companionBaseName: baseName, |
| 792 | }); |
| 793 | |
| 794 | const modulePath = yield* resolveRunCompanionModulePath({ |
| 795 | callerImportMetaUrl, |
| 796 | execPath, |
| 797 | relativeNoExtensionFromCaller: `./${baseName}`, |
| 798 | }); |
| 799 | const fileName = path.basename(modulePath); |
| 800 | const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath)); |
| 801 | |
| 802 | // A rejected import means the file resolved above is missing or unloadable: |
| 803 | // a broken install. It stays a typed failure so callers can report it, or |
| 804 | // fall back, instead of crashing with a stack trace. |
| 805 | const exports = yield* Effect.tryPromise({ |
| 806 | try: () => import(moduleUrl.href) as Promise<Record<string, unknown>>, |
| 807 | catch: cause => |
| 808 | new RunCompanionRepairError({ |
| 809 | message: `Unable to load the CLI's bundled support file ${fileName}. Reinstall the CLI and try again.`, |
| 810 | cause, |
| 811 | }), |
| 812 | }); |
| 813 | |
| 814 | const missingExports = requiredExports.filter(name => !(name in exports)); |
| 815 | if (missingExports.length > 0) { |
| 816 | return yield* new RunCompanionRepairError({ |
| 817 | message: `The CLI's bundled support file ${fileName} does not match this CLI (missing ${missingExports.join(', ')}). Reinstall the CLI and try again.`, |
| 818 | }); |
| 819 | } |
| 820 | return exports as M; |
| 821 | }); |