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

ClaudeHome.ts

134 lines5.7 KB

1import type { ClaudeSettings, ProviderInstanceEnvironment } 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 { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
8import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10​
11import { readClaudeSettingsEnv } from "./ClaudeSkills.ts";
12​
13const quotePath = Schema.encodeSync(Schema.fromJsonString(Schema.String));
14​
15export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function* (
16 config: Pick<ClaudeSettings, "homePath">,
17 environment?: NodeJS.ProcessEnv,
18): Effect.fn.Return<string, never, Path.Path> {
19 const path = yield* Path.Path;
20 const home = yield* HostProcess.HomeDirectory;
21 const homePath = config.homePath.trim();
22 if (homePath.length > 0) {
23 return path.resolve(expandHomePath(homePath, home));
24 }
25 // Inherited env vars are not shell-expanded, so a literal `~` stays literal.
26 const inherited = environment?.CLAUDE_CONFIG_DIR?.trim() ?? "";
27 if (inherited.length > 0) {
28 return path.resolve(inherited);
29 }
30 return path.resolve(path.join(home, ".claude"));
31});
32​
33export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* (
34 config: Pick<ClaudeSettings, "homePath">,
35 baseEnv?: NodeJS.ProcessEnv,
36): Effect.fn.Return<NodeJS.ProcessEnv, never, Path.Path> {
37 const resolvedBaseEnv = baseEnv ?? process.env;
38 const homePath = config.homePath.trim();
39 if (homePath.length === 0) return resolvedBaseEnv;
40 const resolvedHomePath = yield* resolveClaudeHomePath(config);
41 return {
42 ...resolvedBaseEnv,
43 // Changing HOME hides macOS Keychain credentials; use CLAUDE_CONFIG_DIR to keep login access.
44 CLAUDE_CONFIG_DIR: resolvedHomePath,
45 };
46});
47​
48// A Claude Code session that launched T3 exports these. A claude spawned with them acts as part of
49// that session: its session id, messaging socket and token, and effort.
50const CLAUDE_HOST_SESSION_VARIABLES = [
51 "CLAUDECODE",
52 "CLAUDE_CODE_SESSION_ID",
53 "CLAUDE_CODE_HOST_SESSION_ID",
54 "CLAUDE_CODE_CHILD_SESSION",
55 "CLAUDE_CODE_SESSION_ATTENDED",
56 "CLAUDE_CODE_MESSAGING_SOCKET",
57 "CLAUDE_CODE_MESSAGING_TOKEN",
58 "CLAUDE_CODE_EXECPATH",
59 "CLAUDE_CODE_DESKTOP_APP_VERSION",
60 "CLAUDE_EFFORT",
61 "CLAUDE_PID",
62 "CLAUDE_AGENT_SDK_VERSION",
63];
64​
65/**
66 * Base env for the Claude processes T3 Code runs, before `makeClaudeEnvironment`.
67 * Claude Code reads its entrypoint from the process env, not its settings `env`, so T3 Code applies that override itself.
68 */
69export const mergeClaudeProcessEnvironment = Effect.fn("mergeClaudeProcessEnvironment")(function* (
70 config: Pick<ClaudeSettings, "homePath">,
71 instanceEnvironment: ProviderInstanceEnvironment | undefined,
72 hostEnvironment: NodeJS.ProcessEnv,
73) {
74 const baseEnvironment: NodeJS.ProcessEnv = {
75 ...hostEnvironment,
76 CLAUDE_CODE_ENTRYPOINT: "t3-code",
77 };
78 for (const name of CLAUDE_HOST_SESSION_VARIABLES) delete baseEnvironment[name];
79 // The desktop runs the server as Node through Electron. Commands Claude runs inherit its env, so
80 // an Electron app they start (T3 Code itself, VS Code) would run as Node and exit.
81 delete baseEnvironment.ELECTRON_RUN_AS_NODE;
82 const environment = yield* mergeProviderInstanceEnvironment(instanceEnvironment, baseEnvironment);
83 const settingsEntrypoint = (yield* readClaudeSettingsEnv(config, undefined, environment))
84 .CLAUDE_CODE_ENTRYPOINT;
85 return settingsEntrypoint
86 ? { ...environment, CLAUDE_CODE_ENTRYPOINT: settingsEntrypoint }
87 : environment;
88});
89​
90/** managed-mcp.json excludes other MCP config and forbids --strict-mcp-config; omit the flag when present. */
91export const hasClaudeManagedMcpConfig = Effect.fn("hasClaudeManagedMcpConfig")(function* () {
92 const fileSystem = yield* FileSystem.FileSystem;
93 const platform = yield* HostProcess.Platform;
94 const managedMcpConfigPath =
95 platform === "darwin"
96 ? "/Library/Application Support/ClaudeCode/managed-mcp.json"
97 : platform === "win32"
98 ? "C:\\Program Files\\ClaudeCode\\managed-mcp.json"
99 : "/etc/claude-code/managed-mcp.json";
100 return yield* fileSystem.exists(managedMcpConfigPath).pipe(Effect.orElseSucceed(() => false));
101});
102​
103export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationGroupKey")(
104 function* (
105 config: Pick<ClaudeSettings, "homePath">,
106 environment?: NodeJS.ProcessEnv,
107 ): Effect.fn.Return<string, never, Path.Path> {
108 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
109 return `claude:home:${resolvedHomePath}`;
110 },
111);
112​
113export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")(
114 function* (
115 config: Pick<ClaudeSettings, "binaryPath" | "homePath">,
116 cwd?: string,
117 environment?: NodeJS.ProcessEnv,
118 ): Effect.fn.Return<string, never, Path.Path> {
119 const resolvedHomePath = yield* resolveClaudeHomePath(config, environment);
120 return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`;
121 },
122);
123​
124/** Separate spawn env from login commands to preserve literal paths, including relative inherited values. */
125export const claudeSignedOutMessage = (input: {
126 readonly configDir: string | undefined;
127 readonly cwd: string;
128}): string => {
129 const configuration =
130 input.configDir !== undefined
131 ? ` from ${quotePath(input.cwd)}, with CLAUDE_CONFIG_DIR set to ${quotePath(input.configDir)}`
132 : "";
133 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.`;
134};