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

ClaudeHome.ts

52 lines2.1 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";
6​
7import { expandHomePath } from "../../pathExpansion.ts";
8​
9export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function* (
10 config: Pick<ClaudeSettings, "homePath">,
11): Effect.fn.Return<string, never, Path.Path> {
12 const path = yield* Path.Path;
13 const homePath = config.homePath.trim();
14 return path.resolve(homePath.length > 0 ? expandHomePath(homePath) : NodeOS.homedir());
15});
16​
17export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* (
18 config: Pick<ClaudeSettings, "homePath">,
19 baseEnv?: NodeJS.ProcessEnv,
20): Effect.fn.Return<NodeJS.ProcessEnv, never, Path.Path> {
21 const resolvedBaseEnv = baseEnv ?? process.env;
22 const homePath = config.homePath.trim();
23 if (homePath.length === 0) return resolvedBaseEnv;
24 const resolvedHomePath = yield* resolveClaudeHomePath(config);
25 return {
26 ...resolvedBaseEnv,
27 // Isolate this instance's config via CLAUDE_CONFIG_DIR rather than HOME.
28 // Overriding HOME also relocates the macOS login keychain lookup
29 // ($HOME/Library/Keychains), so the spawned CLI can't find its stored
30 // OAuth credentials and reports "Not logged in". CLAUDE_CONFIG_DIR points
31 // Claude Code at its config dir directly while leaving HOME (and the
32 // keychain) intact.
33 CLAUDE_CONFIG_DIR: resolvedHomePath,
34 };
35});
36​
37export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
38 function* (config: Pick<ClaudeSettings, "homePath">): Effect.fn.Return<string, never, Path.Path> {
39 const resolvedHomePath = yield* resolveClaudeHomePath(config);
40 return `claude:home:${resolvedHomePath}`;
41 },
42);
43​
44export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
45 function* (
46 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
47 cwd?: string,
48 ): Effect.fn.Return<string, never, Path.Path> {
49 const resolvedHomePath = yield* resolveClaudeHomePath(config);
50 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
51 },
52);