Open History to see the last commit
1import type { ClaudeSettings } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Path from "effect/Path";
5import * as Schema from "effect/Schema";
6
7import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9
10const quotePath = Schema.encodeSync(Schema.fromJsonString(Schema.String));
11
12export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function* (
13 config: Pick<ClaudeSettings, "homePath">,
14 environment?: NodeJS.ProcessEnv,
15): Effect.fn.Return<string, never, Path.Path> {
16 const path = yield* Path.Path;
17 const home = yield* HostProcess.HomeDirectory;
18 const homePath = config.homePath.trim();
19 if (homePath.length > 0) {
20 return path.resolve(expandHomePath(homePath, home));
21 }
22 // Inherited env vars are not shell-expanded, so a literal `~` stays literal.
23 const inherited = environment?.CLAUDE_CONFIG_DIR?.trim() ?? "";
24 if (inherited.length > 0) {
25 return path.resolve(inherited);
26 }
27 return path.resolve(path.join(home, ".claude"));
28});
29
30export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* (
31 config: Pick<ClaudeSettings, "homePath">,
32 baseEnv?: NodeJS.ProcessEnv,
33): Effect.fn.Return<NodeJS.ProcessEnv, never, Path.Path> {
34 const resolvedBaseEnv = baseEnv ?? process.env;
35 const homePath = config.homePath.trim();
36 if (homePath.length === 0) return resolvedBaseEnv;
37 const resolvedHomePath = yield* resolveClaudeHomePath(config);
38 return {
39 ...resolvedBaseEnv,
40 // Overriding HOME also relocates the macOS login keychain lookup
41 // ($HOME/Library/Keychains), so the spawned CLI can't find its stored
42 // OAuth credentials and reports "Not logged in". CLAUDE_CONFIG_DIR points
43 // Claude Code at its config dir directly while leaving HOME (and the
44 // keychain) intact.
45 CLAUDE_CONFIG_DIR: resolvedHomePath,
46 };
47});
48
49/**
50 * While it exists Claude refuses `--strict-mcp-config` and exits before
51 * initializing, but it already ignores user, project and `--mcp-config` servers
52 * on its own, so callers that pass the flag for isolation can drop it. Paths
53 * match the ones Claude Code checks.
54 */
55export const hasClaudeManagedMcpConfig = Effect.fn("hasClaudeManagedMcpConfig")(function* () {
56 const fileSystem = yield* FileSystem.FileSystem;
57 const platform = yield* HostProcess.Platform;
58 const managedMcpConfigPath =
59 platform === "darwin"
60 ? "/Library/Application Support/ClaudeCode/managed-mcp.json"
61 : platform === "win32"
62 ? "C:\\Program Files\\ClaudeCode\\managed-mcp.json"
63 : "/etc/claude-code/managed-mcp.json";
64 return yield* fileSystem.exists(managedMcpConfigPath).pipe(Effect.orElseSucceed(() => false));
65});
66
67export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
68 function* (
69 config: Pick<ClaudeSettings, "homePath">,
70 environment?: NodeJS.ProcessEnv,
71 ): Effect.fn.Return<string, never, Path.Path> {
72 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
73 return `claude:home:${resolvedHomePath}`;
74 },
75);
76
77export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
78 function* (
79 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
80 cwd?: string,
81 environment?: NodeJS.ProcessEnv,
82 ): Effect.fn.Return<string, never, Path.Path> {
83 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
84 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
85 },
86);
87
88/**
89 * Describe the spawned CLI's environment separately from the login command so
90 * paths remain literal on every shell, including relative inherited values.
91 */
92export const claudeSignedOutMessage = (input: {
93 readonly configDir: string | undefined;
94 readonly cwd: string;
95}): string => {
96 const configuration =
97 input.configDir !== undefined
98 ? ` from ${quotePath(input.cwd)}, with CLAUDE_CONFIG_DIR set to ${quotePath(input.configDir)}`
99 : "";
100 return `Claude could not authenticate. For subscription login, run \`claude auth login\` on this environment's machine${configuration}, then start a new thread. For API-key authentication, check this instance's configured credentials.`;
101};