Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
/**
* Resolves the Claude Code CLI binary the Claude Agent provider drives.
*
* The provider runs in "bring-your-own-binary" mode: the ~250 MB native binary
* that the SDK would otherwise bundle is stripped at install time (see
* `ignoredOptionalDependencies` in the root pnpm-workspace.yaml), so we point
* the SDK at the user's own installed CLI via
* `pathToClaudeCodeExecutable`. This keeps the server install lean and lets each
* platform provide a binary that actually runs there (e.g. the nixpkgs wrapper
* on NixOS, which needs no glibc/nix-ld shim — unlike the SDK's bundled ELF).
*
* Resolution order: the TRILIUM_CLAUDE_CODE_PATH override, then `claude` on
* PATH (see `findOnPath`, which also asks the login shell). The resolved binary
* is probed with `--version` once so a broken/absent install surfaces as a
* clear, actionable error instead of an opaque spawn failure mid-chat.
*/
import { getLog } from "@triliumnext/core";
import { execFile } from "child_process";
import { existsSync } from "fs";
import { promisify } from "util";
import { cachedProbe, describeFlatpakSandbox, findOnPath } from "./binary_lookup.js";
const execFileAsync = promisify(execFile);
/** The probed binary, shared by concurrent first calls (see {@link cachedProbe}). */
const probed = cachedProbe(probeBinary);
export function resolveClaudeBinaryPath(): Promise<string> {
return probed.resolve();
}
/** For tests: forget the probed binary so the next call re-resolves. */
export function resetClaudeBinaryCache(): void {
probed.reset();
}
async function probeBinary(): Promise<string> {
const binary = await locateBinary();
// Probe once: confirms the binary actually runs on this host (catches a
// wrong-arch/broken install) and records the version for diagnostics.
// Async on purpose — this runs on the first chat request, and a sync probe
// would freeze the whole server for up to the 15 s timeout.
let version: string;
try {
version = (await execFileAsync(binary, ["--version"], {
timeout: 15000,
encoding: "utf8",
// On Windows the resolved binary is typically a .cmd batch file
// (npm shim); Node's execFile cannot run .cmd files directly —
// it must delegate to cmd.exe via `shell: true`.
shell: process.platform === "win32"
})).stdout.trim();
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
throw new Error(`Found Claude Code at "${binary}" but it failed to run (${detail}). Ensure it is installed correctly and that you've run \`claude /login\` on the machine running the Trilium server.`);
}
getLog().info(`Claude Agent provider: using Claude Code at ${binary} (${version})`);
return binary;
}
async function locateBinary(): Promise<string> {
const override = process.env.TRILIUM_CLAUDE_CODE_PATH?.trim();
if (override) {
if (!existsSync(override)) {
throw new Error(`TRILIUM_CLAUDE_CODE_PATH is set to "${override}", but no file exists there.`);
}
return override;
}
const onPath = await findOnPath("claude");
if (onPath) {
return onPath;
}
throw new Error(await describeFlatpakSandbox("Claude Code") ?? "Claude Code CLI not found. Install it (`npm install -g @anthropic-ai/claude-code`) and run `claude /login` on the machine running the Trilium server, or set the TRILIUM_CLAUDE_CODE_PATH environment variable to its location.");
}