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/** New packages provide bin/claude.exe; older packages need a JavaScript runtime for cli.js. */
17const NPM_PACKAGE_ENTRY_CANDIDATES = [
18 ["node_modules", "@anthropic-ai", "claude-code", "bin", "claude.exe"],
19 ["node_modules", "@anthropic-ai", "claude-code", "cli.js"],
20] as const;
21
22export type ExecutableFileCheck = (filePath: string) => boolean;
23
24function isExistingFile(filePath: string): boolean {
25 try {
26 return NodeFS.statSync(filePath).isFile();
27 } catch {
28 return false;
29 }
30}
31
32export const ClaudeExecutableFileCheck = Context.Reference<ExecutableFileCheck>(
33 "server/provider/Drivers/ClaudeExecutableFileCheck",
34 {
35 defaultValue: () => isExistingFile,
36 },
37);
38
39/** On Windows the SDK skips shell and PATH/PATHEXT lookup: bare names and .cmd shims fail. */
40export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecutablePath")(
41 function* (binaryPath: string, environment: NodeJS.ProcessEnv): Effect.fn.Return<string> {
42 const platform = yield* HostProcess.Platform;
43 if (platform !== "win32") {
44 return binaryPath;
45 }
46
47 const resolveExecutable = yield* SpawnExecutableResolution;
48 const isFile = yield* ClaudeExecutableFileCheck;
49 const resolved = resolveExecutable(binaryPath, platform, environment) ?? binaryPath;
50 const extension = NodePath.win32.extname(resolved).toLowerCase();
51 if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) {
52 return resolved;
53 }
54
55 const shimDirectory = NodePath.win32.dirname(resolved);
56 for (const entrySegments of NPM_PACKAGE_ENTRY_CANDIDATES) {
57 const candidate = NodePath.win32.join(shimDirectory, ...entrySegments);
58 if (isFile(candidate)) {
59 return candidate;
60 }
61 }
62
63 yield* Effect.logWarning(
64 "Claude launcher shim resolved but no known package entry was found next to it; the Claude Agent SDK cannot spawn launcher scripts directly.",
65 { binaryPath, resolvedShimPath: resolved },
66 );
67 return binaryPath;
68 },
69);