@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
58 lines • 2.51 kB
JavaScript
import { existsSync } from "node:fs";
import { stat } from "node:fs/promises";
export function getMissingSessionCwdIssue(sessionManager, fallbackCwd) {
const sessionFile = sessionManager.getSessionFile();
if (!sessionFile) {
return undefined;
}
const sessionCwd = sessionManager.getCwd();
if (!sessionCwd || existsSync(sessionCwd)) {
return undefined;
}
return {
sessionFile,
sessionCwd,
fallbackCwd,
};
}
export function formatMissingSessionCwdError(issue) {
const sessionFile = issue.sessionFile ? `\nSession file: ${issue.sessionFile}` : "";
return `Stored session working directory does not exist: ${issue.sessionCwd}${sessionFile}\nCurrent working directory: ${issue.fallbackCwd}`;
}
export function formatMissingSessionCwdPrompt(issue) {
return `cwd from session file does not exist\n${issue.sessionCwd}\n\ncontinue in current cwd\n${issue.fallbackCwd}`;
}
export class MissingSessionCwdError extends Error {
constructor(issue) {
super(formatMissingSessionCwdError(issue));
this.name = "MissingSessionCwdError";
this.issue = issue;
}
}
export class SessionCwdNotDirectoryError extends Error {
constructor(issue) {
const sessionFile = issue.sessionFile ? `\nSession file: ${issue.sessionFile}` : "";
super(`Session working directory is not a directory: ${issue.sessionCwd}${sessionFile}`);
this.name = "SessionCwdNotDirectoryError";
this.issue = issue;
}
}
/**
* Async on purpose: the RPC session path may not reach a synchronous filesystem call
* (test/suite/no-sync-in-session-path.test.ts), so the directory probe is awaited here rather
* than taken with statSync.
*/
export async function assertSessionCwdExists(sessionManager, fallbackCwd) {
const issue = getMissingSessionCwdIssue(sessionManager, fallbackCwd);
if (issue) {
throw new MissingSessionCwdError(issue);
}
const sessionCwd = sessionManager.getCwd();
// A missing cwd is the case above (or a session with no file, which keeps today's behaviour); one that exists must be a folder.
const stats = sessionCwd ? await stat(sessionCwd).catch(() => undefined) : undefined;
if (sessionCwd && stats !== undefined && !stats.isDirectory()) {
const sessionFile = sessionManager.getSessionFile();
throw new SessionCwdNotDirectoryError({ ...(sessionFile ? { sessionFile } : {}), sessionCwd });
}
}
//# sourceMappingURL=session-cwd.js.map