Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeHome.ts

89 lines3.8 KB

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 // Changing HOME hides macOS Keychain credentials; use CLAUDE_CONFIG_DIR to keep login access.
41 CLAUDE_CONFIG_DIR: resolvedHomePath,
42 };
43});
44​
45/** managed-mcp.json excludes other MCP config and forbids --strict-mcp-config; omit the flag when present. */
46export const hasClaudeManagedMcpConfig = Effect.fn("hasClaudeManagedMcpConfig")(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 const platform = yield* HostProcess.Platform;
49 const managedMcpConfigPath =
50 platform === "darwin"
51 ? "/Library/Application Support/ClaudeCode/managed-mcp.json"
52 : platform === "win32"
53 ? "C:\\Program Files\\ClaudeCode\\managed-mcp.json"
54 : "/etc/claude-code/managed-mcp.json";
55 return yield* fileSystem.exists(managedMcpConfigPath).pipe(Effect.orElseSucceed(() => false));
56});
57​
58export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
59 function* (
60 config: Pick<ClaudeSettings, "homePath">,
61 environment?: NodeJS.ProcessEnv,
62 ): Effect.fn.Return<string, never, Path.Path> {
63 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
64 return `claude:home:${resolvedHomePath}`;
65 },
66);
67​
68export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
69 function* (
70 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
71 cwd?: string,
72 environment?: NodeJS.ProcessEnv,
73 ): Effect.fn.Return<string, never, Path.Path> {
74 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
75 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
76 },
77);
78​
79/** Separate spawn env from login commands to preserve literal paths, including relative inherited values. */
80export const claudeSignedOutMessage = (input: {
81 readonly configDir: string | undefined;
82 readonly cwd: string;
83}): string => {
84 const configuration =
85 input.configDir !== undefined
86 ? ` from ${quotePath(input.cwd)}, with CLAUDE_CONFIG_DIR set to ${quotePath(input.configDir)}`
87 : "";
88 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.`;
89};