Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4
5import * as HostProcess from "@t3tools/shared/HostProcess";
6import { SpawnExecutableResolution } from "@t3tools/shared/shell";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9
10/** Node 20.12+ cannot spawn Windows launcher scripts without a shell; the SDK cannot use these as executables. */
11const WINDOWS_SHIM_EXTENSIONS: ReadonlySet<string> = new Set([".cmd", ".bat", ".ps1"]);
12
13export const isWindowsClaudeLauncherShimPath = (filePath: string): boolean =>
14 WINDOWS_SHIM_EXTENSIONS.has(NodePath.win32.extname(filePath).toLowerCase());
15
16/**
17 * npm: new packages provide bin/claude.exe; older ones need a JavaScript runtime for cli.js.
18 * WinGet: Links\claude.cmd forwards to the package's native executable under ..\Packages.
19 */
20const PACKAGE_ENTRY_CANDIDATES = [
21 ["node_modules", "@anthropic-ai", "claude-code", "bin", "claude.exe"],
22 ["node_modules", "@anthropic-ai", "claude-code", "cli.js"],
23 ["..", "Packages", "Anthropic.ClaudeCode_Microsoft.Winget.Source_8wekyb3d8bbwe", "claude.exe"],
24] as const;
25
26/**
27 * pnpm installs global packages under a content-hashed directory, so only its launcher
28 * records where the package lives: a line `@"%~dp0\..\global\v11\<hash>\node_modules\...\claude.exe" %*`.
29 */
30const PNPM_LAUNCHER_TARGET = /^@"%~dp0\\([^"%\r\n]+)"[ \t]+%\*[ \t]*\r?$/m;
31const CLAUDE_PACKAGE_EXECUTABLE_SUFFIX =
32 "\\node_modules\\@anthropic-ai\\claude-code\\bin\\claude.exe";
33
34function readLauncherTarget(shimPath: string): string | undefined {
35 let contents: string;
36 try {
37 contents = NodeFS.readFileSync(shimPath, "utf8");
38 } catch {
39 return undefined;
40 }
41 const target = PNPM_LAUNCHER_TARGET.exec(contents)?.[1];
42 if (target === undefined) return undefined;
43 const executable = NodePath.win32.join(NodePath.win32.dirname(shimPath), target);
44 return executable.toLowerCase().endsWith(CLAUDE_PACKAGE_EXECUTABLE_SUFFIX)
45 ? executable
46 : undefined;
47}
48
49export type ExecutableFileCheck = (filePath: string) => boolean;
50
51function isExistingFile(filePath: string): boolean {
52 try {
53 return NodeFS.statSync(filePath).isFile();
54 } catch {
55 return false;
56 }
57}
58
59export const ClaudeExecutableFileCheck = Context.Reference<ExecutableFileCheck>(
60 "server/provider/Drivers/ClaudeExecutableFileCheck",
61 {
62 defaultValue: () => isExistingFile,
63 },
64);
65
66/** On Windows the SDK skips shell and PATH/PATHEXT lookup: bare names and .cmd shims fail. */
67export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecutablePath")(
68 function* (binaryPath: string, environment: NodeJS.ProcessEnv): Effect.fn.Return<string> {
69 const platform = yield* HostProcess.Platform;
70 if (platform !== "win32") {
71 return binaryPath;
72 }
73
74 const resolveExecutable = yield* SpawnExecutableResolution;
75 const isFile = yield* ClaudeExecutableFileCheck;
76 const resolved =
77 (yield* Effect.promise(async () => resolveExecutable(binaryPath, platform, environment))) ??
78 binaryPath;
79 const extension = NodePath.win32.extname(resolved).toLowerCase();
80 if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) {
81 return resolved;
82 }
83
84 const shimDirectory = NodePath.win32.dirname(resolved);
85 for (const entrySegments of PACKAGE_ENTRY_CANDIDATES) {
86 const candidate = NodePath.win32.join(shimDirectory, ...entrySegments);
87 if (isFile(candidate)) {
88 return candidate;
89 }
90 }
91 const launcherTarget = readLauncherTarget(resolved);
92 if (launcherTarget !== undefined && isFile(launcherTarget)) {
93 return launcherTarget;
94 }
95
96 yield* Effect.logWarning(
97 "Claude launcher shim resolved but no known package entry was found next to it; the Claude Agent SDK cannot spawn launcher scripts directly.",
98 { binaryPath, resolvedShimPath: resolved },
99 );
100 return binaryPath;
101 },
102);
103
104/**
105 * Returns an effect reading the real path of the installed Claude binary.
106 * Updates repoint the installed link to a new version file, so comparing this
107 * detects when a live CLI process was started from an older version.
108 */
109export const claudeProgramReader = Effect.fn("claudeProgramReader")(function* (
110 binaryPath: string,
111 environment: NodeJS.ProcessEnv,
112): Effect.fn.Return<Effect.Effect<string | undefined>> {
113 const platform = yield* HostProcess.Platform;
114 const resolveExecutable = yield* SpawnExecutableResolution;
115 return Effect.promise(async () => {
116 try {
117 const resolved = await resolveExecutable(binaryPath, platform, environment);
118 return resolved === undefined ? undefined : await NodeFS.promises.realpath(resolved);
119 } catch {
120 return undefined;
121 }
122 });
123});
124
125/**
126 * Reports whether an update replaced the program a live CLI process started
127 * from. An unreadable path on either side reports no update.
128 */
129export const claudeProgramWasReplaced = (
130 startedFrom: string | undefined,
131 now: string | undefined,
132): boolean => startedFrom !== undefined && now !== undefined && startedFrom !== now;