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

ClaudeHome.ts

70 lines2.9 KB

1import * as NodeOS from "node:os";
2​
3import type { ClaudeSettings } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7​
8import { expandHomePath } from "../../pathExpansion.ts";
9​
10const quotePath = Schema.encodeSync(Schema.fromJsonString(Schema.String));
11​
12export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function* (
13 config: Pick<ClaudeSettings, "homePath">,
14): Effect.fn.Return<string, never, Path.Path> {
15 const path = yield* Path.Path;
16 const homePath = config.homePath.trim();
17 return path.resolve(homePath.length > 0 ? expandHomePath(homePath) : NodeOS.homedir());
18});
19​
20export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* (
21 config: Pick<ClaudeSettings, "homePath">,
22 baseEnv?: NodeJS.ProcessEnv,
23): Effect.fn.Return<NodeJS.ProcessEnv, never, Path.Path> {
24 const resolvedBaseEnv = baseEnv ?? process.env;
25 const homePath = config.homePath.trim();
26 if (homePath.length === 0) return resolvedBaseEnv;
27 const resolvedHomePath = yield* resolveClaudeHomePath(config);
28 return {
29 ...resolvedBaseEnv,
30 // Isolate this instance's config via CLAUDE_CONFIG_DIR rather than HOME.
31 // Overriding HOME also relocates the macOS login keychain lookup
32 // ($HOME/Library/Keychains), so the spawned CLI can't find its stored
33 // OAuth credentials and reports "Not logged in". CLAUDE_CONFIG_DIR points
34 // Claude Code at its config dir directly while leaving HOME (and the
35 // keychain) intact.
36 CLAUDE_CONFIG_DIR: resolvedHomePath,
37 };
38});
39​
40export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
41 function* (config: Pick<ClaudeSettings, "homePath">): Effect.fn.Return<string, never, Path.Path> {
42 const resolvedHomePath = yield* resolveClaudeHomePath(config);
43 return `claude:home:${resolvedHomePath}`;
44 },
45);
46​
47export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
48 function* (
49 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
50 cwd?: string,
51 ): Effect.fn.Return<string, never, Path.Path> {
52 const resolvedHomePath = yield* resolveClaudeHomePath(config);
53 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
54 },
55);
56​
57/**
58 * Describe the spawned CLI's environment separately from the login command so
59 * paths remain literal on every shell, including relative inherited values.
60 */
61export const claudeSignedOutMessage = (input: {
62 readonly configDir: string | undefined;
63 readonly cwd: string;
64}): string => {
65 const configuration =
66 input.configDir !== undefined
67 ? ` from ${quotePath(input.cwd)}, with CLAUDE_CONFIG_DIR set to ${quotePath(input.configDir)}`
68 : "";
69 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.`;
70};