@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
125 lines • 4.22 kB
JavaScript
import { execFile as nodeExecFile } from "node:child_process";
import { accessSync, constants, readdirSync, statSync } from "node:fs";
import { homedir } from "node:os";
import { delimiter, join } from "node:path";
import { cursorAgentEnvironment } from "./environment.js";
export class CursorAgentNotInstalledError extends Error {
constructor() {
super("Cursor CLI is not installed. Install it with `curl https://cursor.com/install -fsS | bash`, " +
"then ensure ~/.local/bin is on your PATH.");
this.kind = "binary_missing";
this.name = "CursorAgentNotInstalledError";
}
}
function executableCandidate(candidate, deps) {
if (!candidate)
return undefined;
try {
return deps.isExecutableFile(candidate) ? candidate : undefined;
}
catch {
return undefined;
}
}
function resolveFromPath(deps) {
const pathValue = deps.env("PATH");
if (!pathValue)
return undefined;
for (const directory of pathValue.split(deps.pathDelimiter)) {
if (!directory)
continue;
const candidate = executableCandidate(join(directory, "cursor-agent"), deps);
if (candidate)
return candidate;
}
return undefined;
}
function resolveFromInstalledVersions(deps) {
const versionsDirectory = join(deps.homeDirectory, ".local", "share", "cursor-agent", "versions");
let entries;
try {
entries = deps.readDirectory(versionsDirectory);
}
catch {
return undefined;
}
const directoryNames = entries
.filter((entry) => entry.isDirectory)
.map((entry) => entry.name)
.sort()
.reverse();
for (const directoryName of directoryNames) {
const candidate = executableCandidate(join(versionsDirectory, directoryName, "cursor-agent"), deps);
if (candidate)
return candidate;
}
return undefined;
}
export function resolveCursorAgentExecutable(deps) {
const orderedCandidates = [
deps.env("SENPI_CURSOR_CLI_OAUTH_EXECUTABLE"),
deps.env("CURSOR_AGENT_EXECUTABLE"),
deps.settings.executablePath,
];
for (const candidate of orderedCandidates) {
const executable = executableCandidate(candidate, deps);
if (executable)
return executable;
}
const pathExecutable = resolveFromPath(deps);
if (pathExecutable)
return pathExecutable;
const installedExecutable = resolveFromInstalledVersions(deps);
if (installedExecutable)
return installedExecutable;
throw new CursorAgentNotInstalledError();
}
const defaultVersionProbeDeps = {
execFile: (file, args, options, callback) => {
nodeExecFile(file, args, options, (error, stdout, stderr) => {
callback(error, stdout, stderr);
});
},
};
export function probeCursorAgentVersion(executable, deps = defaultVersionProbeDeps) {
return new Promise((resolve, reject) => {
const options = {
encoding: "utf8",
timeout: 10_000,
env: cursorAgentEnvironment(homedir()),
// senpi#3965: cursor-agent is a console-subsystem exe on Windows.
windowsHide: true,
};
deps.execFile(executable, ["--version"], options, (error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(stdout.trim());
});
});
}
export function defaultCursorAgentExecutableDeps() {
return {
env: (name) => process.env[name],
settings: {},
homeDirectory: homedir(),
pathDelimiter: delimiter,
isExecutableFile: (candidate) => {
try {
if (!statSync(candidate).isFile())
return false;
accessSync(candidate, constants.X_OK);
return true;
}
catch {
return false;
}
},
readDirectory: (directory) => readdirSync(directory, { withFileTypes: true }).map((entry) => ({
name: entry.name,
isDirectory: entry.isDirectory(),
})),
};
}
//# sourceMappingURL=executable.js.map