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:
yuez committed 2026-11-02 11:12:41 +08:00
1 parent 32292873f9
commit b8f1fca5bd
11 files changed
+1324 -6

No files matched your search

+24
View File
@@ -263,6 +263,12 @@ function serveStatic(req, res, pathname) {
return undefined;
}
/** Prompt-history fixture for `workspace.query{kind:"promptHistory"}` (protocol §3.1.1). */
const PROMPT_HISTORY_FIXTURE = [
"Summarise the diff and list the risky parts.",
"Run the full gate: typecheck, both test suites, smoke.",
];
/** Sessions of one workspace, in the shape `/workspaces/:id/sessions` acks. */
function sessionsOf(workspaceId) {
const rows = FIXTURES.sessions.filter((session) => session.workspaceId === workspaceId);
@@ -366,6 +372,24 @@ async function handleApi(req, res, url) {
}
return sendJson(res, 200, sessionsOf(workspaceId));
}
// Composer ↑ recall. Always "live" here: the harness should exercise the
// relayed path, not the browser-mirror fallback (which the unit tests cover).
match = /^\/api\/v1\/workspaces\/([^/]+)\/prompt-history$/.exec(pathname);
if (match && method === "GET") {
const workspaceId = decodeURIComponent(match[1]);
if (!FIXTURES.workspaces.some((workspace) => workspace.workspaceId === workspaceId)) {
return notFound(res, `unknown workspace ${workspaceId}`);
}
const raw = Number(url.searchParams.get("limit"));
const limit = Number.isFinite(raw) && raw > 0 ? Math.min(500, Math.floor(raw)) : 200;
return sendJson(res, 200, {
v: 1,
workspaceId,
live: true,
supported: true,
entries: PROMPT_HISTORY_FIXTURE.slice(-limit),
});
}
match = /^\/api\/v1\/sessions\/([^/]+)\/entries$/.exec(pathname);
if (match && method === "GET") {
const sessionId = decodeURIComponent(match[1]);