The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | |
| 5 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 6 | import { SpawnExecutableResolution } from "@t3tools/shared/shell"; |
| 7 | import * as Context from "effect/Context"; |
| 8 | import * 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. */ |
| 11 | const WINDOWS_SHIM_EXTENSIONS: ReadonlySet<string> = new Set([".cmd", ".bat", ".ps1"]); |
| 12 | |
| 13 | export 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 | */ |
| 20 | const 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 | export type ExecutableFileCheck = (filePath: string) => boolean; |
| 27 | |
| 28 | function isExistingFile(filePath: string): boolean { |
| 29 | try { |
| 30 | return NodeFS.statSync(filePath).isFile(); |
| 31 | } catch { |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export 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. */ |
| 44 | export 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 = resolveExecutable(binaryPath, platform, environment) ?? binaryPath; |
| 54 | const extension = NodePath.win32.extname(resolved).toLowerCase(); |
| 55 | if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) { |
| 56 | return resolved; |
| 57 | } |
| 58 | |
| 59 | const shimDirectory = NodePath.win32.dirname(resolved); |
| 60 | for (const entrySegments of PACKAGE_ENTRY_CANDIDATES) { |
| 61 | const candidate = NodePath.win32.join(shimDirectory, ...entrySegments); |
| 62 | if (isFile(candidate)) { |
| 63 | return candidate; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | yield* Effect.logWarning( |
| 68 | "Claude launcher shim resolved but no known package entry was found next to it; the Claude Agent SDK cannot spawn launcher scripts directly.", |
| 69 | { binaryPath, resolvedShimPath: resolved }, |
| 70 | ); |
| 71 | return binaryPath; |
| 72 | }, |
| 73 | ); |