Extract duplicate normalizeLocalScheme regex pattern into a shared function in path-utils.ts. Updated interactive-mode.ts, approved-plan.ts, agent-session.ts, bash-skill-urls.ts, and plan-mode-guard.ts to use the shared utility. Also fixed error message formatting (removed extra backslashes).
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { resolveLocalUrlToPath } from "../internal-urls";
|
|
import type { ToolSession } from ".";
|
|
import { normalizeLocalScheme, resolveToCwd } from "./path-utils";
|
|
import { ToolError } from "./tool-errors";
|
|
|
|
const LOCAL_SCHEME_PREFIX = "local:";
|
|
|
|
export function resolvePlanPath(session: ToolSession, targetPath: string): string {
|
|
const normalized = normalizeLocalScheme(targetPath);
|
|
if (normalized.startsWith(LOCAL_SCHEME_PREFIX)) {
|
|
return resolveLocalUrlToPath(normalized, {
|
|
getArtifactsDir: session.getArtifactsDir,
|
|
getSessionId: session.getSessionId,
|
|
});
|
|
}
|
|
|
|
return resolveToCwd(normalized, session.cwd);
|
|
}
|
|
|
|
export function enforcePlanModeWrite(
|
|
session: ToolSession,
|
|
targetPath: string,
|
|
options?: { move?: string; op?: "create" | "update" | "delete" },
|
|
): void {
|
|
const state = session.getPlanModeState?.();
|
|
if (!state?.enabled) return;
|
|
|
|
const resolvedTarget = resolvePlanPath(session, targetPath);
|
|
const resolvedPlan = resolvePlanPath(session, state.planFilePath);
|
|
|
|
if (options?.move) {
|
|
throw new ToolError("Plan mode: renaming files is not allowed.");
|
|
}
|
|
|
|
if (options?.op === "delete") {
|
|
throw new ToolError("Plan mode: deleting files is not allowed.");
|
|
}
|
|
|
|
if (resolvedTarget !== resolvedPlan) {
|
|
throw new ToolError(`Plan mode: only the plan file may be modified (${state.planFilePath}).`);
|
|
}
|
|
}
|