feat(web): recall prompt history from the plugin with a browser fallback
The composer's ↑/↓ history is the web port of pi2.nvim's prompt buffer, so it has to walk the archive nvim walks — not a copy of it. Three tiers: the plugin owns the text, the daemon relays one live-only read, the browser keeps a mirror. - `features/chat/prompt-history.ts`: the readline rules (`appendEntry`: blank ignored, consecutive dedupe, newest 500), the `localStorage` mirror (`pi2-prompt-history`, per workspace, 40 workspaces LRU), the `PromptHistorySource` seam, and `PromptHistoryNav` — the per-composer cursor with a stashed draft, independent positions, and a reset on edit/send - `features/chat/prompt-history-feed.ts`: `usePromptHistory(workspaceId)` reads `GET /workspaces/:id/prompt-history`, appends a send optimistically, refreshes the mirror from every successful read, and falls back to the mirror whenever the relay cannot answer (no plugin, no capability, a 404 from an older daemon, an offline browser). One workspace-scoped read per page, never per keystroke. - composer: ↑ recalls from the first line, ↓ from the last line or while browsing, `<C-p>`/`<C-n>` always recall; an IME composition and a text selection keep their keys; the caret lands at the end of a recalled prompt and a manual edit ends recall - fixes a mirror bug found while testing it: a full list that accepts one more entry and drops the oldest has the same length, so the old "unchanged" check silently stopped recording after 500 prompts - `docs/prompt-history.md` records the tiers, the key map, the storage contract and what is deliberately not shared yet (cross-host, history without nvim); the UI mock server answers the new endpoint so the visual harness stays honest Version skew is boring by design: an old daemon (404) or an old plugin (no capability) keeps recall working from the mirror.
This commit is contained in:
1 parent
32292873f9
commit
b8f1fca5bd
11 files changed
+1324
-6
No files matched your search
@@ -2,6 +2,7 @@ import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { Composer, type ComposerProps } from "@/features/chat/composer";
|
||||
import { readPromptHistory, rememberPrompt } from "@/features/chat/prompt-history";
|
||||
import { setLanguage } from "@/lib/i18n";
|
||||
import { sessionFixture } from "./chat-fixtures";
|
||||
|
||||
@@ -215,3 +216,196 @@ describe("composer", () => {
|
||||
expect(screen.queryByText(/prototype demo only/i)).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Prompt recall (↑/↓), the web port of pi2.nvim's prompt-buffer history. The
|
||||
* store/nav contract itself is covered in `prompt-history.test.ts`; here we
|
||||
* only pin the keyboard rules the composer adds on top of it.
|
||||
*/
|
||||
describe("composer prompt history", () => {
|
||||
/** Place a collapsed caret, so the ArrowUp/ArrowDown line rule is explicit. */
|
||||
function caretAt(element: HTMLTextAreaElement, at: number): void {
|
||||
element.setSelectionRange(at, at);
|
||||
}
|
||||
|
||||
function input(): HTMLTextAreaElement {
|
||||
return screen.getByTestId("composer-input") as HTMLTextAreaElement;
|
||||
}
|
||||
|
||||
it("recalls older prompts on ArrowUp and the draft past the newest", () => {
|
||||
rememberPrompt("w1", "first");
|
||||
rememberPrompt("w1", "second");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.change(textarea, { target: { value: "my draft" } });
|
||||
caretAt(textarea, 0);
|
||||
|
||||
// A recall swallows the key (the caret must not also move).
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false);
|
||||
expect(textarea).toHaveValue("second");
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false);
|
||||
expect(textarea).toHaveValue("first");
|
||||
// At the oldest entry the key is left to the caret again.
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(true);
|
||||
expect(textarea).toHaveValue("first");
|
||||
|
||||
fireEvent.keyDown(textarea, { key: "ArrowDown" });
|
||||
expect(textarea).toHaveValue("second");
|
||||
// Past the newest entry the in-progress draft is restored.
|
||||
fireEvent.keyDown(textarea, { key: "ArrowDown" });
|
||||
expect(textarea).toHaveValue("my draft");
|
||||
// Recall is over: ↓ is a plain caret move again.
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(true);
|
||||
expect(textarea).toHaveValue("my draft");
|
||||
});
|
||||
|
||||
it("leaves ArrowUp to the caret inside a multi-line draft", () => {
|
||||
rememberPrompt("w1", "old prompt");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
const draft = "line1\nline2";
|
||||
|
||||
fireEvent.change(textarea, { target: { value: draft } });
|
||||
// Caret on the second line: ↑ must move the caret, not recall.
|
||||
caretAt(textarea, draft.length);
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(true);
|
||||
expect(textarea).toHaveValue(draft);
|
||||
|
||||
// Caret on the first line: ↑ recalls.
|
||||
caretAt(textarea, 0);
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false);
|
||||
expect(textarea).toHaveValue("old prompt");
|
||||
});
|
||||
|
||||
it("leaves ArrowDown to the caret above the last line", () => {
|
||||
rememberPrompt("w1", "archived");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
const draft = "draft\ntail";
|
||||
|
||||
fireEvent.change(textarea, { target: { value: draft } });
|
||||
caretAt(textarea, 2); // still on the first line
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(true);
|
||||
expect(textarea).toHaveValue(draft);
|
||||
});
|
||||
|
||||
it("walks back out of a recalled multi-line entry from its last line", () => {
|
||||
rememberPrompt("w1", "line1\nline2");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.change(textarea, { target: { value: "draft" } });
|
||||
caretAt(textarea, 0);
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false);
|
||||
expect(textarea).toHaveValue("line1\nline2");
|
||||
// The caret is parked on the entry's last line, so ↓ leaves recall.
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(false);
|
||||
expect(textarea).toHaveValue("draft");
|
||||
});
|
||||
|
||||
it("always recalls on Ctrl+P / Ctrl+N", () => {
|
||||
rememberPrompt("w1", "alpha");
|
||||
rememberPrompt("w1", "beta");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.change(textarea, { target: { value: "draft text" } });
|
||||
caretAt(textarea, 3); // mid-line: the caret rule does not apply
|
||||
|
||||
fireEvent.keyDown(textarea, { key: "p", ctrlKey: true });
|
||||
expect(textarea).toHaveValue("beta");
|
||||
fireEvent.keyDown(textarea, { key: "p", ctrlKey: true });
|
||||
expect(textarea).toHaveValue("alpha");
|
||||
fireEvent.keyDown(textarea, { key: "n", ctrlKey: true });
|
||||
expect(textarea).toHaveValue("beta");
|
||||
fireEvent.keyDown(textarea, { key: "n", ctrlKey: true });
|
||||
expect(textarea).toHaveValue("draft text");
|
||||
});
|
||||
|
||||
it("moves the caret to the end of a recalled prompt", () => {
|
||||
rememberPrompt("w1", "remembered prompt");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.keyDown(textarea, { key: "ArrowUp" });
|
||||
expect(textarea).toHaveValue("remembered prompt");
|
||||
expect(textarea.selectionStart).toBe("remembered prompt".length);
|
||||
expect(textarea.selectionEnd).toBe("remembered prompt".length);
|
||||
});
|
||||
|
||||
it("ends recall when the user edits by hand", () => {
|
||||
rememberPrompt("w1", "older");
|
||||
rememberPrompt("w1", "newer");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.keyDown(textarea, { key: "ArrowUp" });
|
||||
expect(textarea).toHaveValue("newer");
|
||||
fireEvent.change(textarea, { target: { value: "newer, edited" } });
|
||||
caretAt(textarea, "newer, edited".length);
|
||||
|
||||
// No longer browsing: ↓ must not replace the edit with a stale entry.
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(true);
|
||||
expect(textarea).toHaveValue("newer, edited");
|
||||
// ↓ again starts from the newest entry, not from where recall stopped.
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false);
|
||||
expect(textarea).toHaveValue("newer");
|
||||
});
|
||||
|
||||
it("remembers each send in its own workspace, trimmed and deduped", () => {
|
||||
const { onSend, unmount } = renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.change(textarea, { target: { value: " padded " } });
|
||||
fireEvent.keyDown(textarea, { key: "Enter" });
|
||||
expect(onSend).toHaveBeenCalledWith("padded", [], undefined);
|
||||
expect(readPromptHistory("w1")).toEqual(["padded"]);
|
||||
unmount();
|
||||
|
||||
// Another workspace shares no history…
|
||||
const other = renderComposer(sessionFixture({ status: "idle", workspaceId: "w2" }));
|
||||
fireEvent.change(input(), { target: { value: "from w2" } });
|
||||
fireEvent.keyDown(input(), { key: "Enter" });
|
||||
expect(other.onSend).toHaveBeenCalledWith("from w2", [], undefined);
|
||||
expect(readPromptHistory("w2")).toEqual(["from w2"]);
|
||||
expect(readPromptHistory("w1")).toEqual(["padded"]);
|
||||
other.unmount();
|
||||
|
||||
// …and the first one still recalls its own send.
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
expect(fireEvent.keyDown(input(), { key: "ArrowUp" })).toBe(false);
|
||||
expect(input()).toHaveValue("padded");
|
||||
});
|
||||
|
||||
it("does not let an IME own-up compose recall its candidate list", () => {
|
||||
rememberPrompt("w1", "archived");
|
||||
renderComposer(sessionFixture({ status: "idle" }));
|
||||
const textarea = input();
|
||||
|
||||
fireEvent.change(textarea, { target: { value: "nihao" } });
|
||||
caretAt(textarea, 5);
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp", isComposing: true })).toBe(true);
|
||||
expect(textarea).toHaveValue("nihao");
|
||||
});
|
||||
|
||||
it("recalls from an injected feed and records sends into it", () => {
|
||||
// `pages/chat.tsx` injects the plugin-backed feed; the composer must neither
|
||||
// read the mirror nor decide where a send is recorded.
|
||||
const entries: string[] = ["typed in nvim"];
|
||||
const remember = vi.fn((text: string) => entries.push(text));
|
||||
const { onSend } = renderComposer(sessionFixture({ status: "idle" }), {
|
||||
history: { entries: () => entries, remember },
|
||||
});
|
||||
const textarea = input();
|
||||
|
||||
expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false);
|
||||
expect(textarea).toHaveValue("typed in nvim");
|
||||
|
||||
fireEvent.keyDown(textarea, { key: "Enter" });
|
||||
expect(onSend).toHaveBeenCalledWith("typed in nvim", [], undefined);
|
||||
expect(remember).toHaveBeenCalledWith("typed in nvim");
|
||||
// The mirror stays untouched: the injected feed owns the archive now.
|
||||
expect(readPromptHistory("w1")).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user