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 62 lines 2.45 KB
Raw
1 import * as FileSystem from 'effect/FileSystem';
2 import * as Path from 'effect/Path';
3 import { Effect, Option } from 'effect';
4 import { HOST_CONFIG, type AgentHostEnvironment } from 'src/config';
5 import type { AgentHost } from './agent-host';
6 import { NodeOs } from './node-os';
7
8 type HostEnvMarkers = Pick<AgentHostEnvironment, 'claudeCode' | 'codexThreadId' | 'codexSandbox'>;
9
10 // Blank markers already read as `undefined` (see `HOST_CONFIG`).
11 export function detectPluginHost(markers: HostEnvMarkers): AgentHost | undefined {
12 if (markers.claudeCode !== undefined) return 'claude';
13 if (markers.codexThreadId !== undefined || markers.codexSandbox !== undefined) return 'codex';
14 return undefined;
15 }
16
17 export const rawHostEnvironment: Effect.Effect<AgentHostEnvironment> = Effect.orDie(
18 HOST_CONFIG.AGENT_HOST
19 );
20
21 export const hostConfigDirectory = (host: AgentHost) =>
22 Effect.gen(function* () {
23 const path = yield* Path.Path;
24 const os = yield* NodeOs;
25 const env = yield* rawHostEnvironment;
26 // Anchor relative overrides to the home directory so probe results
27 // cannot depend on the process cwd; absolute overrides pass through.
28 if (host === 'claude') return path.resolve(os.homedir, env.claudeConfigDir ?? '.claude');
29 return path.resolve(os.homedir, env.codexHome ?? '.codex');
30 });
31
32 const KNOWN_BINARY_PATHS: Readonly<Record<AgentHost, ReadonlyArray<string>>> = {
33 claude: [
34 '.claude/local/claude',
35 '.local/bin/claude',
36 '.npm-global/bin/claude',
37 '/usr/local/bin/claude',
38 '/opt/homebrew/bin/claude',
39 ],
40 codex: [
41 '.local/bin/codex',
42 '.npm-global/bin/codex',
43 '/usr/local/bin/codex',
44 '/opt/homebrew/bin/codex',
45 ],
46 };
47
48 export const probeHostInstallation = (host: AgentHost) =>
49 Effect.gen(function* () {
50 const fs = yield* FileSystem.FileSystem;
51 const path = yield* Path.Path;
52 const os = yield* NodeOs;
53 const exists = (target: string) => fs.exists(target).pipe(Effect.orElseSucceed(() => false));
54 // Presence means an actual directory: a file at the config path (or an
55 // unreadable one) must not report the host as installed.
56 const dirInfo = yield* Effect.option(fs.stat(yield* hostConfigDirectory(host)));
57 const configDirPresent = Option.exists(dirInfo, info => info.type === 'Directory');
58 const found = yield* Effect.forEach(KNOWN_BINARY_PATHS[host], entry =>
59 exists(path.resolve(os.homedir, entry))
60 );
61 return { configDirPresent, binaryInKnownPaths: found.includes(true) };
62 });