845
*
846
* Resolution is the same as for the `run` companions: the `.ts` source next to
847
* this file when running from a checkout (tests, `bun run src/bin.ts`), the
848
- * `.mjs` bundle next to the executable in a packaged install. A packaged install
849
- * that lost the file goes through the same release-archive repair `composio run`
850
- * uses before failing.
848
+ * `.mjs` bundle next to the executable in a packaged install.
849
+ *
850
+ * The module is imported before anything else is checked, so a companion that
851
+ * loads is used even when an unrelated one is missing and the repair download
852
+ * would fail offline. Only a module that fails to load, or that lacks one of
853
+ * `requiredExports` (a file left behind by a different release), sends a
854
+ * packaged install through the release-archive repair `composio run` uses. If
855
+ * that repair restored missing files the module is imported once more;
856
+ * otherwise the load error stands.
857
*
858
* The specifier is computed at runtime on purpose: a literal `import('./x')`
859
* would make the bundler fold the module back into the executable, which is the
863
* is type-only and leaves no import behind.
864
*/
865
export const loadInstalledCompanionModule = <M>(
860
- baseName: string
866
+ baseName: string,
867
+ requiredExports: ReadonlyArray<keyof M & string>
868
): Effect.Effect<
869
M,
870
RunCompanionRepairError | PlatformError.PlatformError,
875
const callerImportMetaUrl = import.meta.url;
876
const execPath = process.execPath;
877
871
- yield* repairMissingInstalledRunCompanionModules({
872
- callerImportMetaUrl,
873
- execPath,
874
- appVersion: APP_VERSION,
875
- });
878
+ const importModule = Effect.gen(function* () {
879
+ const modulePath = yield* resolveRunCompanionModulePath({
880
+ callerImportMetaUrl,
881
+ execPath,
882
+ relativeNoExtensionFromCaller: `./${baseName}`,
883
+ });
884
+ const fileName = path.basename(modulePath);
885
+ const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath));
886
+
887
+ // A rejected import means the file resolved above is missing or unloadable:
888
+ // a broken install. It stays a typed failure so callers can report it, or
889
+ // fall back, instead of crashing with a stack trace.
890
+ const exports = yield* Effect.tryPromise({
891
+ try: () => import(moduleUrl.href) as Promise<Record<string, unknown>>,
892
+ catch: cause =>
893
+ new RunCompanionRepairError({
894
+ message: `Unable to load the CLI's bundled support file ${fileName}. Reinstall the CLI and try again.`,
895
+ cause,
896
+ }),
897
+ });
898
877
- const modulePath = yield* resolveRunCompanionModulePath({
878
- callerImportMetaUrl,
879
- execPath,
880
- relativeNoExtensionFromCaller: `./${baseName}`,
881
- });
882
- const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath));
883
-
884
- // A rejected import means the file resolved above is missing or unloadable:
885
- // a broken install. It stays a typed failure so callers can report it, or
886
- // fall back, instead of crashing with a stack trace.
887
- return yield* Effect.tryPromise({
888
- try: () => import(moduleUrl.href) as Promise<M>,
889
- catch: cause =>
890
- new RunCompanionRepairError({
891
- message: `Unable to load the CLI's bundled support file ${path.basename(modulePath)}. Reinstall the CLI and try again.`,
892
- cause,
893
- }),
899
+ const missingExports = requiredExports.filter(name => !(name in exports));
900
+ if (missingExports.length > 0) {
901
+ return yield* new RunCompanionRepairError({
902
+ message: `The CLI's bundled support file ${fileName} does not match this CLI (missing ${missingExports.join(', ')}). Reinstall the CLI and try again.`,
903
+ });
904
+ }
905
+ return exports as M;
906
});
907
+
908
+ return yield* importModule.pipe(
909
+ Effect.catchTag('services/RunCompanionRepairError', loadError =>
910
+ repairMissingInstalledRunCompanionModules({
911
+ callerImportMetaUrl,
912
+ execPath,
913
+ appVersion: APP_VERSION,
914
+ }).pipe(
915
+ Effect.flatMap(({ repaired }) => (repaired ? importModule : Effect.fail(loadError)))
916
+ )
917
+ )
918
+ );
919
});