mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-01 04:36:26 +00:00
fix(app): preserve existing drafts during rewind
Restore the rewound prompt as one draft so its text or attachments cannot mix with a prompt already in the composer.
This commit is contained in:
@@ -469,6 +469,59 @@ test.describe("Rewind sheet", () => {
|
||||
}
|
||||
});
|
||||
|
||||
const existingDrafts = [
|
||||
{
|
||||
name: "typed text",
|
||||
text: "Keep this draft.",
|
||||
fileCount: 0,
|
||||
prepare: (page: Page) => fillComposerDraft(page, "Keep this draft."),
|
||||
},
|
||||
{
|
||||
name: "an uploaded file",
|
||||
text: "",
|
||||
fileCount: 1,
|
||||
prepare: async (page: Page) => {
|
||||
await attachFileFromMenu(page, {
|
||||
name: "draft.txt",
|
||||
mimeType: "text/plain",
|
||||
buffer: Buffer.from("Keep this draft file"),
|
||||
});
|
||||
await expectAttachmentPill(page, "composer-file-attachment-pill");
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
for (const draft of existingDrafts) {
|
||||
test(`preserves a draft containing ${draft.name} when rewinding a sent image`, async ({
|
||||
page,
|
||||
}) => {
|
||||
const prompt = "Review this image before rewind.";
|
||||
const session = await seedMockAgentWorkspace({
|
||||
repoPrefix: "rewind-keeps-draft-e2e-",
|
||||
title: "Rewind with an existing draft",
|
||||
});
|
||||
try {
|
||||
await openAgentRoute(page, session);
|
||||
await expectComposerVisible(page);
|
||||
await attachComposerTestImage(page);
|
||||
await expectAttachmentPill(page, "composer-image-attachment-pill");
|
||||
await submitMessageWithButton(page, prompt);
|
||||
await session.client.waitForFinish(session.agentId);
|
||||
await draft.prepare(page);
|
||||
|
||||
await rewindMessageWithImage(page, prompt);
|
||||
await expectUserMessageCount(page, 0);
|
||||
await expectComposerDraft(page, draft.text);
|
||||
await expect(page.getByTestId("composer-image-attachment-pill")).toHaveCount(0);
|
||||
await expect(page.getByTestId("composer-file-attachment-pill")).toHaveCount(
|
||||
draft.fileCount,
|
||||
);
|
||||
} finally {
|
||||
await session.cleanup();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test("surfaces rewind failures without crashing the page", async ({ page }) => {
|
||||
const firstPrompt = "emit 1 coalesced agent stream updates for failed rewind turn.";
|
||||
const rewindError = "No file checkpoint found for message rewind-failure-e2e.";
|
||||
|
||||
@@ -1,30 +1,10 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import type { AttachmentMetadata, UserComposerAttachment } from "@/attachments/types";
|
||||
import type { AttachmentMetadata } from "@/attachments/types";
|
||||
import type { UploadedFileAttachment } from "@getpaseo/protocol/messages";
|
||||
import { splitComposerAttachmentsForSubmit } from "@/composer/attachments/submit";
|
||||
import { restoreComposerAttachmentsIfEmpty, restoreComposerTextIfEmpty } from "./composer-restore";
|
||||
import { composeRewoundAttachments } from "./composer-restore";
|
||||
import { shouldRestoreComposerForRewindMode } from "./rewind-mode";
|
||||
|
||||
describe("restoreComposerTextIfEmpty", () => {
|
||||
test("restores the rewound message when the composer is empty", () => {
|
||||
expect(
|
||||
restoreComposerTextIfEmpty({
|
||||
currentText: "",
|
||||
rewoundText: "message before rewind",
|
||||
}),
|
||||
).toBe("message before rewind");
|
||||
});
|
||||
|
||||
test("preserves an existing composer draft", () => {
|
||||
expect(
|
||||
restoreComposerTextIfEmpty({
|
||||
currentText: "keep this draft",
|
||||
rewoundText: "message before rewind",
|
||||
}),
|
||||
).toBe("keep this draft");
|
||||
});
|
||||
});
|
||||
|
||||
describe("shouldRestoreComposerForRewindMode", () => {
|
||||
test("restores only conversation-mutating rewind modes", () => {
|
||||
expect(shouldRestoreComposerForRewindMode("conversation")).toBe(true);
|
||||
@@ -50,11 +30,12 @@ const file: UploadedFileAttachment = {
|
||||
path: "/uploads/notes.txt",
|
||||
};
|
||||
|
||||
describe("restoreComposerAttachmentsIfEmpty", () => {
|
||||
describe("composeRewoundAttachments", () => {
|
||||
test("restores images and uploaded files ready for resubmission", () => {
|
||||
const restored = restoreComposerAttachmentsIfEmpty({
|
||||
currentAttachments: [],
|
||||
rewoundMessage: { text: "Review these", images: [image], attachments: [file] },
|
||||
const restored = composeRewoundAttachments({
|
||||
text: "Review these",
|
||||
images: [image],
|
||||
attachments: [file],
|
||||
});
|
||||
expect(restored).toEqual([
|
||||
{ kind: "image", metadata: image },
|
||||
@@ -66,30 +47,19 @@ describe("restoreComposerAttachmentsIfEmpty", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("preserves attachments added to an existing draft", () => {
|
||||
const draft: UserComposerAttachment[] = [{ kind: "file", attachment: file }];
|
||||
expect(
|
||||
restoreComposerAttachmentsIfEmpty({
|
||||
currentAttachments: draft,
|
||||
rewoundMessage: { text: "", images: [image] },
|
||||
}),
|
||||
).toBe(draft);
|
||||
});
|
||||
|
||||
test("restores an image-only message", () => {
|
||||
expect(
|
||||
restoreComposerAttachmentsIfEmpty({
|
||||
currentAttachments: [],
|
||||
rewoundMessage: { text: "", images: [image] },
|
||||
composeRewoundAttachments({
|
||||
text: "",
|
||||
images: [image],
|
||||
}),
|
||||
).toEqual([{ kind: "image", metadata: image }]);
|
||||
});
|
||||
|
||||
test("keeps text-only messages free of attachments", () => {
|
||||
expect(
|
||||
restoreComposerAttachmentsIfEmpty({
|
||||
currentAttachments: [],
|
||||
rewoundMessage: { text: "hello" },
|
||||
composeRewoundAttachments({
|
||||
text: "hello",
|
||||
}),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { AttachmentMetadata, UserComposerAttachment } from "@/attachments/types";
|
||||
import type { AgentInputDraft } from "@/composer/draft/input-draft";
|
||||
import type { UserMessageItem } from "@/types/stream";
|
||||
import type { ComposerTextSource } from "@/composer/text-source";
|
||||
import { createContext, useCallback, useContext, useMemo, type ReactNode } from "react";
|
||||
|
||||
type RewoundMessage = Pick<UserMessageItem, "text" | "attachments"> & {
|
||||
@@ -13,36 +12,19 @@ interface RewindComposerRestoreContextValue {
|
||||
}
|
||||
|
||||
interface RewindComposerRestoreProviderProps {
|
||||
textSource: ComposerTextSource;
|
||||
setText: (text: string) => void;
|
||||
setAttachments: AgentInputDraft["setAttachments"];
|
||||
restoreIfEmpty: AgentInputDraft["restoreIfEmpty"];
|
||||
onRewindComplete: (restoredComposerText: boolean) => void;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
const RewindComposerRestoreContext = createContext<RewindComposerRestoreContextValue | null>(null);
|
||||
|
||||
export function restoreComposerTextIfEmpty(input: {
|
||||
currentText: string;
|
||||
rewoundText: string;
|
||||
}): string {
|
||||
if (input.currentText.length > 0) {
|
||||
return input.currentText;
|
||||
}
|
||||
return input.rewoundText;
|
||||
}
|
||||
|
||||
export function restoreComposerAttachmentsIfEmpty(input: {
|
||||
currentAttachments: UserComposerAttachment[];
|
||||
rewoundMessage: RewoundMessage;
|
||||
}): UserComposerAttachment[] {
|
||||
if (input.currentAttachments.length > 0) {
|
||||
return input.currentAttachments;
|
||||
}
|
||||
const attachments: UserComposerAttachment[] = (input.rewoundMessage.images ?? []).map(
|
||||
(metadata) => ({ kind: "image", metadata }),
|
||||
);
|
||||
for (const attachment of input.rewoundMessage.attachments ?? []) {
|
||||
export function composeRewoundAttachments(message: RewoundMessage): UserComposerAttachment[] {
|
||||
const attachments: UserComposerAttachment[] = (message.images ?? []).map((metadata) => ({
|
||||
kind: "image",
|
||||
metadata,
|
||||
}));
|
||||
for (const attachment of message.attachments ?? []) {
|
||||
if (attachment.type === "uploaded_file") {
|
||||
attachments.push({ kind: "file", attachment });
|
||||
}
|
||||
@@ -51,29 +33,19 @@ export function restoreComposerAttachmentsIfEmpty(input: {
|
||||
}
|
||||
|
||||
export function RewindComposerRestoreProvider({
|
||||
textSource,
|
||||
setText,
|
||||
setAttachments,
|
||||
restoreIfEmpty,
|
||||
onRewindComplete,
|
||||
children,
|
||||
}: RewindComposerRestoreProviderProps) {
|
||||
const completeRewind = useCallback(
|
||||
(message: RewoundMessage) => {
|
||||
const currentText = textSource.getSnapshot();
|
||||
const nextText = restoreComposerTextIfEmpty({
|
||||
currentText: currentText,
|
||||
rewoundText: message.text,
|
||||
const restored = restoreIfEmpty({
|
||||
text: message.text,
|
||||
attachments: composeRewoundAttachments(message),
|
||||
});
|
||||
const restoredComposerText = nextText !== currentText;
|
||||
if (restoredComposerText) {
|
||||
setText(nextText);
|
||||
}
|
||||
setAttachments((currentAttachments) =>
|
||||
restoreComposerAttachmentsIfEmpty({ currentAttachments, rewoundMessage: message }),
|
||||
);
|
||||
onRewindComplete(restoredComposerText);
|
||||
onRewindComplete(restored && message.text.length > 0);
|
||||
},
|
||||
[onRewindComplete, setAttachments, setText, textSource],
|
||||
[onRewindComplete, restoreIfEmpty],
|
||||
);
|
||||
|
||||
const value = useMemo(() => ({ completeRewind }), [completeRewind]);
|
||||
|
||||
@@ -66,6 +66,7 @@ export interface AgentInputDraft {
|
||||
textSource: ComposerTextSource;
|
||||
editText: (text: string) => void;
|
||||
replaceText: (text: string) => void;
|
||||
restoreIfEmpty: (draft: DraftInput) => boolean;
|
||||
textReplacement: TextReplacement;
|
||||
attachments: UserComposerAttachment[];
|
||||
setAttachments: (updater: AttachmentUpdater) => void;
|
||||
@@ -189,6 +190,16 @@ export function useAgentInputDraft(input: UseAgentInputDraftInput): AgentInputDr
|
||||
[draftKey, publishTextReplacement, textPublication],
|
||||
);
|
||||
|
||||
const restoreIfEmpty = useCallback(
|
||||
(draft: DraftInput) => {
|
||||
textPublication.flush();
|
||||
if (!useDraftStore.getState().restoreDraftInputIfEmpty({ draftKey, draft })) return false;
|
||||
if (draft.text.length > 0) publishTextReplacement("replace", draft.text);
|
||||
return true;
|
||||
},
|
||||
[draftKey, publishTextReplacement, textPublication],
|
||||
);
|
||||
|
||||
const setAttachments = useCallback(
|
||||
(updater: AttachmentUpdater) => {
|
||||
saveDraft((current) => ({
|
||||
@@ -390,6 +401,7 @@ export function useAgentInputDraft(input: UseAgentInputDraftInput): AgentInputDr
|
||||
textSource,
|
||||
editText,
|
||||
replaceText,
|
||||
restoreIfEmpty,
|
||||
textReplacement,
|
||||
attachments,
|
||||
setAttachments,
|
||||
|
||||
@@ -1189,6 +1189,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({
|
||||
textSource,
|
||||
editText,
|
||||
replaceText,
|
||||
restoreIfEmpty,
|
||||
textReplacement,
|
||||
attachments,
|
||||
setAttachments,
|
||||
@@ -1202,6 +1203,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({
|
||||
textSource,
|
||||
editText,
|
||||
replaceText,
|
||||
restoreIfEmpty,
|
||||
textReplacement,
|
||||
attachments,
|
||||
setAttachments,
|
||||
@@ -1214,6 +1216,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({
|
||||
textSource,
|
||||
editText,
|
||||
replaceText,
|
||||
restoreIfEmpty,
|
||||
textReplacement,
|
||||
attachments,
|
||||
setAttachments,
|
||||
@@ -1335,9 +1338,7 @@ const ChatAgentReadyContent = memo(function ChatAgentReadyContent({
|
||||
|
||||
const content = (
|
||||
<RewindComposerRestoreProvider
|
||||
textSource={agentInputDraft.textSource}
|
||||
setText={agentInputDraft.replaceText}
|
||||
setAttachments={agentInputDraft.setAttachments}
|
||||
restoreIfEmpty={agentInputDraft.restoreIfEmpty}
|
||||
onRewindComplete={handleRewindComplete}
|
||||
>
|
||||
{hasAgentComposer ? (
|
||||
|
||||
@@ -46,6 +46,7 @@ interface DraftStoreActions {
|
||||
getDraftInput: (draftKey: string) => DraftInput | undefined;
|
||||
hydrateDraftInput: (input: { draftKey: string }) => Promise<DraftInput | undefined>;
|
||||
saveDraftInput: (input: { draftKey: string; draft: DraftInput }) => void;
|
||||
restoreDraftInputIfEmpty: (input: { draftKey: string; draft: DraftInput }) => boolean;
|
||||
/** Saves what `update` makes of the current input; an emptied input abandons the draft. */
|
||||
updateDraftInput: (input: {
|
||||
draftKey: string;
|
||||
@@ -343,6 +344,18 @@ export const useDraftStore = create<DraftStore>()(
|
||||
scheduleAttachmentGc();
|
||||
},
|
||||
|
||||
restoreDraftInputIfEmpty: ({ draftKey, draft }) => {
|
||||
const store = get();
|
||||
const record = store.drafts[draftKey];
|
||||
if (record?.lifecycle === "active") {
|
||||
const current = toDraftInputIfReady(record);
|
||||
if (!current || hasDraftContent(current)) return false;
|
||||
}
|
||||
if (!hasDraftContent(draft)) return false;
|
||||
store.saveDraftInput({ draftKey, draft });
|
||||
return true;
|
||||
},
|
||||
|
||||
updateDraftInput: ({ draftKey, update }) => {
|
||||
const next = update(get().getDraftInput(draftKey) ?? { text: "", attachments: [] });
|
||||
if (hasDraftContent(next)) get().saveDraftInput({ draftKey, draft: next });
|
||||
|
||||
Reference in New Issue
Block a user