feat: prefill forked session prompt draft

This commit is contained in:
Federico Jaramillo Martinez
2026-05-21 22:56:03 +02:00
parent 2f5293a8c9
commit 9e3d272731
9 changed files with 51 additions and 8 deletions
+1 -1
View File
@@ -103,7 +103,7 @@ export interface PiSessionRuntime {
readonly cwd: string;
readonly session: PiAgentSession;
setRebindSession(rebindSession?: (session: PiAgentSession) => Promise<void>): void;
fork(entryId: string, options?: { position?: "before" | "at" }): Promise<{ cancelled: boolean }>;
fork(entryId: string, options?: { position?: "before" | "at" }): Promise<{ cancelled: boolean; selectedText?: string }>;
dispose(): Promise<void>;
}
@@ -112,13 +112,14 @@ describe("SessionCommandService", () => {
{ entryId: "newest", text: "newest message" },
]),
});
vi.mocked(active.runtime.fork).mockResolvedValueOnce({ cancelled: false, selectedText: "newest message" });
const service = new SessionCommandService(() => getActive(active), vi.fn(), eventPublisher());
const result = await service.run("s1", "/fork");
expect(result).toMatchObject({ type: "select", title: "Fork from message", options: [{ value: "newest" }, { value: "middle" }, { value: "oldest" }] });
if (result.type !== "select") throw new Error("Expected select result");
await expect(service.respond("s1", result.requestId, "newest")).resolves.toMatchObject({ type: "done", message: "Session forked", session: { id: "s1" } });
await expect(service.respond("s1", result.requestId, "newest")).resolves.toMatchObject({ type: "done", message: "Session forked", session: { id: "s1" }, promptDraft: "newest message" });
expect(active.runtime.fork).toHaveBeenCalledWith("newest");
await expect(service.respond("s1", result.requestId, "newest")).resolves.toEqual({ type: "unsupported", message: "Command request expired" });
});
+6 -2
View File
@@ -33,7 +33,7 @@ export interface CommandSession {
export interface CommandRuntime<TSession extends CommandSession = CommandSession> {
cwd: string;
session: TSession;
fork: (entryId: string, options?: { position?: "before" | "at" }) => Promise<{ cancelled: boolean }>;
fork: (entryId: string, options?: { position?: "before" | "at" }) => Promise<{ cancelled: boolean; selectedText?: string }>;
}
export interface CommandActiveSession<TSession extends CommandSession = CommandSession> {
@@ -96,7 +96,7 @@ export class SessionCommandService<TSession extends CommandSession = CommandSess
if (sessionHasActiveWork(active.runtime.session)) return forkActiveUnsupported("fork");
const result = await active.runtime.fork(value);
if (result.cancelled) return { type: "done", message: "Fork cancelled" };
return { type: "done", message: "Session forked", session: clientSessionFromRuntime(active.runtime) };
return { type: "done", message: "Session forked", session: clientSessionFromRuntime(active.runtime), ...promptDraft(result.selectedText) };
}
private nameSession(active: CommandActiveSession<TSession>, name: string): ClientCommandResult {
@@ -179,6 +179,10 @@ function forkActiveUnsupported(command: "fork" | "clone"): ClientCommandResult {
return { type: "unsupported", message: `Cannot ${command} while the session is active. Stop current activity before ${command === "fork" ? "forking" : "cloning"}.` };
}
function promptDraft(text: string | undefined): Partial<Pick<Extract<ClientCommandResult, { type: "done" }>, "promptDraft">> {
return text === undefined ? {} : { promptDraft: text };
}
function formatSessionStats(session: CommandSession): string {
const stats = session.getSessionStats();
return [