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/**
11 * Windows launcher-script extensions that Node cannot spawn without a shell
12 * (`spawn EINVAL` since Node 20.12) and that the Claude Agent SDK therefore
13 * cannot use as `pathToClaudeCodeExecutable`.
14 */
15const WINDOWS_SHIM_EXTENSIONS: ReadonlySet<string> = new Set([".cmd", ".bat", ".ps1"]);
16
17export const isWindowsClaudeLauncherShimPath = (filePath: string): boolean =>
18 WINDOWS_SHIM_EXTENSIONS.has(NodePath.win32.extname(filePath).toLowerCase());
19
20/**
21 * Newer package versions ship a native `bin/claude.exe`; older versions only ship `cli.js`,
22 * which the SDK runs with a JavaScript runtime.
23 */
24const NPM_PACKAGE_ENTRY_CANDIDATES = [
25 ["node_modules", "@anthropic-ai", "claude-code", "bin", "claude.exe"],
26 ["node_modules", "@anthropic-ai", "claude-code", "cli.js"],
27] as const;
28
29export type ExecutableFileCheck = (filePath: string) => boolean;
30
31function isExistingFile(filePath: string): boolean {
32 try {
33 return NodeFS.statSync(filePath).isFile();
34 } catch {
35 return false;
36 }
37}
38
39export const ClaudeExecutableFileCheck = Context.Reference<ExecutableFileCheck>(
40 "server/provider/Drivers/ClaudeExecutableFileCheck",
41 {
42 defaultValue: () => isExistingFile,
43 },
44);
45
46/**
47 * The SDK spawns the given path without a shell and without Windows PATH / PATHEXT resolution,
48 * so a bare command name like `claude` fails with "native binary not found" and an npm
49 * `claude.cmd` shim fails with `spawn EINVAL`. CLI probes avoid this via `resolveSpawnCommand`,
50 * which can fall back to `shell: true`; the SDK offers no such escape hatch.
51 */
52export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecutablePath")(
53 function* (binaryPath: string, environment: NodeJS.ProcessEnv): Effect.fn.Return<string> {
54 const platform = yield* HostProcess.Platform;
55 if (platform !== "win32") {
56 return binaryPath;
57 }
58
59 const resolveExecutable = yield* SpawnExecutableResolution;
60 const isFile = yield* ClaudeExecutableFileCheck;
61 const resolved = resolveExecutable(binaryPath, platform, environment) ?? binaryPath;
62 const extension = NodePath.win32.extname(resolved).toLowerCase();
63 if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) {
64 return resolved;
65 }
66
67 const shimDirectory = NodePath.win32.dirname(resolved);
68 for (const entrySegments of NPM_PACKAGE_ENTRY_CANDIDATES) {
69 const candidate = NodePath.win32.join(shimDirectory, ...entrySegments);
70 if (isFile(candidate)) {
71 return candidate;
72 }
73 }
74
75 yield* Effect.logWarning(
76 "Claude launcher shim resolved but no known package entry was found next to it; the Claude Agent SDK cannot spawn launcher scripts directly.",
77 { binaryPath, resolvedShimPath: resolved },
78 );
79 return binaryPath;
80 },
81);