refactor: extract Claude binary lookup into separate function

This commit is contained in:
Mohamed Boudra
2026-03-09 16:49:00 +07:00
parent a15b52efc8
commit 7b22fc5c3f
@@ -363,14 +363,18 @@ type ClaudeAgentSessionOptions = {
logger: Logger; logger: Logger;
}; };
function resolveClaudeBinary(): string { function whichClaude(): string | null {
try { try {
const claudePath = execSync("which claude", { encoding: "utf8" }).trim(); return execSync("which claude", { encoding: "utf8", env: process.env }).trim() || null;
if (claudePath) {
return claudePath;
}
} catch { } catch {
// fall through return null;
}
}
function resolveClaudeBinary(): string {
const claudePath = whichClaude();
if (claudePath) {
return claudePath;
} }
throw new Error( throw new Error(
"Claude CLI not found. Install claude or configure agents.providers.claude.command.mode='replace'." "Claude CLI not found. Install claude or configure agents.providers.claude.command.mode='replace'."
@@ -1410,11 +1414,7 @@ export class ClaudeAgentClient implements AgentClient {
this.defaults = options.defaults; this.defaults = options.defaults;
this.logger = options.logger.child({ module: "agent", provider: "claude" }); this.logger = options.logger.child({ module: "agent", provider: "claude" });
this.runtimeSettings = options.runtimeSettings; this.runtimeSettings = options.runtimeSettings;
try { this.claudePath = whichClaude();
this.claudePath = execSync("which claude", { encoding: "utf8" }).trim() || null;
} catch {
this.claudePath = null;
}
if (this.claudePath) { if (this.claudePath) {
try { try {
const version = execSync(`${this.claudePath} --version`, { encoding: "utf8" }).trim(); const version = execSync(`${this.claudePath} --version`, { encoding: "utf8" }).trim();
@@ -1423,7 +1423,7 @@ export class ClaudeAgentClient implements AgentClient {
this.logger.info({ claudePath: this.claudePath }, "Resolved Claude binary (version unknown)"); this.logger.info({ claudePath: this.claudePath }, "Resolved Claude binary (version unknown)");
} }
} else { } else {
this.logger.warn("Claude binary not found via 'which claude'; SDK will use bundled binary"); this.logger.warn("Claude binary not found in PATH; SDK will use bundled binary");
} }
} }