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

ClaudeExecutable.ts

105 lines4.0 KB

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​
26export type ExecutableFileCheck = (filePath: string) => boolean;
27​
28function isExistingFile(filePath: string): boolean {
29 try {
30 return NodeFS.statSync(filePath).isFile();
31 } catch {
32 return false;
33 }
35​
36export const ClaudeExecutableFileCheck = Context.Reference<ExecutableFileCheck>(
37 "server/provider/Drivers/ClaudeExecutableFileCheck",
38 {
39 defaultValue: () => isExistingFile,
40 },
41);
42​
43/** On Windows the SDK skips shell and PATH/PATHEXT lookup: bare names and .cmd shims fail. */
44export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecutablePath")(
45 function* (binaryPath: string, environment: NodeJS.ProcessEnv): Effect.fn.Return<string> {
46 const platform = yield* HostProcess.Platform;
47 if (platform !== "win32") {
48 return binaryPath;
49 }
50​
51 const resolveExecutable = yield* SpawnExecutableResolution;
52 const isFile = yield* ClaudeExecutableFileCheck;
53 const resolved =
54 (yield* Effect.promise(async () => resolveExecutable(binaryPath, platform, environment))) ??
55 binaryPath;
56 const extension = NodePath.win32.extname(resolved).toLowerCase();
57 if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) {
58 return resolved;
59 }
60​
61 const shimDirectory = NodePath.win32.dirname(resolved);
62 for (const entrySegments of PACKAGE_ENTRY_CANDIDATES) {
63 const candidate = NodePath.win32.join(shimDirectory, ...entrySegments);
64 if (isFile(candidate)) {
65 return candidate;
66 }
67 }
68​
69 yield* Effect.logWarning(
70 "Claude launcher shim resolved but no known package entry was found next to it; the Claude Agent SDK cannot spawn launcher scripts directly.",
71 { binaryPath, resolvedShimPath: resolved },
72 );
73 return binaryPath;
74 },
75);
76​
77/**
78 * Returns an effect reading the real path of the installed Claude binary.
79 * Updates repoint the installed link to a new version file, so comparing this
80 * detects when a live CLI process was started from an older version.
81 */
82export const claudeProgramReader = Effect.fn("claudeProgramReader")(function* (
83 binaryPath: string,
84 environment: NodeJS.ProcessEnv,
85): Effect.fn.Return<Effect.Effect<string | undefined>> {
86 const platform = yield* HostProcess.Platform;
87 const resolveExecutable = yield* SpawnExecutableResolution;
88 return Effect.promise(async () => {
89 try {
90 const resolved = await resolveExecutable(binaryPath, platform, environment);
91 return resolved === undefined ? undefined : await NodeFS.promises.realpath(resolved);
92 } catch {
93 return undefined;
94 }
95 });
96});
97​
98/**
99 * Reports whether an update replaced the program a live CLI process started
100 * from. An unreadable path on either side reports no update.
101 */
102export const claudeProgramWasReplaced = (
103 startedFrom: string | undefined,
104 now: string | undefined,
105): boolean => startedFrom !== undefined && now !== undefined && startedFrom !== now;