feat(coding-agent): add --fork session flag closes #2290
This commit is contained in:
@@ -23,6 +23,7 @@ export interface Args {
|
||||
mode?: Mode;
|
||||
noSession?: boolean;
|
||||
session?: string;
|
||||
fork?: string;
|
||||
sessionDir?: string;
|
||||
models?: string[];
|
||||
tools?: ToolName[];
|
||||
@@ -89,6 +90,8 @@ export function parseArgs(args: string[], extensionFlags?: Map<string, { type: "
|
||||
result.noSession = true;
|
||||
} else if (arg === "--session" && i + 1 < args.length) {
|
||||
result.session = args[++i];
|
||||
} else if (arg === "--fork" && i + 1 < args.length) {
|
||||
result.fork = args[++i];
|
||||
} else if (arg === "--session-dir" && i + 1 < args.length) {
|
||||
result.sessionDir = args[++i];
|
||||
} else if (arg === "--models" && i + 1 < args.length) {
|
||||
@@ -202,6 +205,7 @@ ${chalk.bold("Options:")}
|
||||
--continue, -c Continue previous session
|
||||
--resume, -r Select a session to resume
|
||||
--session <path> Use specific session file
|
||||
--fork <path> Fork specific session file or partial UUID into a new session
|
||||
--session-dir <dir> Directory for session storage and lookup
|
||||
--no-session Don't save session (ephemeral)
|
||||
--models <patterns> Comma-separated model patterns for Ctrl+P cycling
|
||||
|
||||
@@ -408,6 +408,32 @@ async function callSessionDirectoryHook(extensions: LoadExtensionsResult, cwd: s
|
||||
return customSessionDir;
|
||||
}
|
||||
|
||||
function validateForkFlags(parsed: Args): void {
|
||||
if (!parsed.fork) return;
|
||||
|
||||
const conflictingFlags = [
|
||||
parsed.session ? "--session" : undefined,
|
||||
parsed.continue ? "--continue" : undefined,
|
||||
parsed.resume ? "--resume" : undefined,
|
||||
parsed.noSession ? "--no-session" : undefined,
|
||||
].filter((flag): flag is string => flag !== undefined);
|
||||
|
||||
if (conflictingFlags.length > 0) {
|
||||
console.error(chalk.red(`Error: --fork cannot be combined with ${conflictingFlags.join(", ")}`));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function forkSessionOrExit(sourcePath: string, cwd: string, sessionDir?: string): SessionManager {
|
||||
try {
|
||||
return SessionManager.forkFrom(sourcePath, cwd, sessionDir);
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(chalk.red(`Error: ${message}`));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
async function createSessionManager(
|
||||
parsed: Args,
|
||||
cwd: string,
|
||||
@@ -423,6 +449,21 @@ async function createSessionManager(
|
||||
effectiveSessionDir = await callSessionDirectoryHook(extensions, cwd);
|
||||
}
|
||||
|
||||
if (parsed.fork) {
|
||||
const resolved = await resolveSessionPath(parsed.fork, cwd, effectiveSessionDir);
|
||||
|
||||
switch (resolved.type) {
|
||||
case "path":
|
||||
case "local":
|
||||
case "global":
|
||||
return forkSessionOrExit(resolved.path, cwd, effectiveSessionDir);
|
||||
|
||||
case "not_found":
|
||||
console.error(chalk.red(`No session found matching '${resolved.arg}'`));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.session) {
|
||||
const resolved = await resolveSessionPath(parsed.session, cwd, effectiveSessionDir);
|
||||
|
||||
@@ -439,7 +480,7 @@ async function createSessionManager(
|
||||
console.log(chalk.dim("Aborted."));
|
||||
process.exit(0);
|
||||
}
|
||||
return SessionManager.forkFrom(resolved.path, cwd, effectiveSessionDir);
|
||||
return forkSessionOrExit(resolved.path, cwd, effectiveSessionDir);
|
||||
}
|
||||
|
||||
case "not_found":
|
||||
@@ -698,6 +739,8 @@ export async function main(args: string[]) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
validateForkFlags(parsed);
|
||||
|
||||
const { initialMessage, initialImages } = await prepareInitialMessage(
|
||||
parsed,
|
||||
settingsManager.getImageAutoResize(),
|
||||
|
||||
Reference in New Issue
Block a user