Files
pi2d/docs/prompt-history.md
yuez b8f1fca5bd 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.
2026-11-02 11:12:41 +08:00

6.6 KiB

Prompt input history (web composer)

The Chat composer recalls previously sent prompts with the arrow keys. The text comes from the plugin (pi2.nvim owns the archive); the daemon relays a live-only read; the browser keeps a mirror so recall still works when the relay cannot answer. This document is the web-side map of that design — the wire contract lives in docs/protocol.md §3.1.1.

Key map

Key Behaviour
↑ (<C-p>) Recall the previous (older) prompt.
↓ (<C-n>) Walk back toward the newest entry; past it, restore the draft.
Enter / Shift+Enter Unchanged: send / newline.

↑ and ↓ only recall when the caret has nowhere left to go — ↑ on the first line, ↓ on the last line — or, for ↓, while already browsing history. A collapsed caret is required (a selection means "editing a fragment"), and an IME composition (isComposing) keeps the keys to itself, because arrows navigate its candidate window. <C-p> / <C-n> are the readline aliases and always recall.

Recalling stashes the in-progress draft, which comes back when you walk past the newest entry; the caret is parked at the end of the recalled text. Any manual edit ends browsing, so a later ↓ can never overwrite what you typed. Sending also ends browsing and records the prompt (trimmed).

Who owns the text

Three tiers, in this precedence order:

Tier Owns Lifetime
pi2.nvim (prompt.history, one archive per workspace cwd) the entries durable, per workspace, shared with every surface
daemon nothing — (relay only)
browser mirror (localStorage, pi2-prompt-history) a fallback copy per browser profile, bounded
  1. The plugin is the authority. Every prompt is recorded by pi2.nvim when it is sent — Store:add inside Chat:_send_message, whichever surface sent it — so nvim's ↑ and the console's ↑ walk the same list, and so does every device paired to this daemon. The console writes nothing back: sending through the composer already lands in that store through prompt.send.
  2. The daemon relays one read and stores nothing. GET /api/v1/workspaces/:id/prompt-history?limit= → {live, supported, entries}. It is live-only: with no connected plugin it answers live: false instead of instances.ensure(), because pressing ↑ must never boot an nvim. It is also capability-gated: a plugin that did not advertise promptHistory gets no command at all (supported: false). Unknown workspace → 404.
  3. The browser mirror is the fallback. Written on every send and refreshed from every successful relay read, so losing the plugin (instance restart, an older daemon that 404s the endpoint, an older plugin without the capability, an offline browser) still recalls the last known history instead of only what this browser typed. It is never authoritative: when the relay answers supported, the plugin's entries win.

Version skew is therefore boring: a new console against an old daemon (404) or an old plugin (no capability) simply keeps using the mirror.

What is remembered

Mirrors Store:add in lua/pi/prompt_history.lua, on both the plugin and the mirror:

  • blank / whitespace-only prompts are not recorded;
  • a prompt identical to the newest entry is skipped (readline consecutive dedupe — a non-consecutive repeat is kept);
  • entries are multi-line safe and stored verbatim;
  • the newest 500 are kept (HISTORY_LIMIT; the plugin's prompt.history.max defaults to the same).

Scope and storage

The mirror is one localStorage key, pi2-prompt-history, holding { [workspaceId]: string[] }, entries oldest-first.

  • Scope = workspace id, so every session in a workspace shares one archive (like nvim's per-cwd store) while another workspace starts clean. A session not yet bound to a workspace uses the default bucket rather than losing the send.
  • Bounded: the 40 least-recently-used workspaces are kept (WORKSPACE_LIMIT), so a long-lived profile cannot grow without bound.
  • Every read/write tolerates a missing, corrupt or unwritable storage (private mode, quota): the feature degrades to "no mirror", never to a broken composer.
  • The cursor (position + stashed draft, PromptHistoryNav) never leaves the composer. No server sees it, and two tabs never share it.

Where it lives

Path Contents
web/src/features/chat/prompt-history.ts the mirror (readPromptHistory, rememberPrompt, replacePromptHistory, clearPromptHistory), the readline rules (appendEntry), the PromptHistorySource seam and PromptHistoryNav
web/src/features/chat/prompt-history-feed.ts usePromptHistory(workspaceId, enabled) — the relay read, the optimistic append after a send, the mirror refresh, and the fallback decision (remote / live)
web/src/features/chat/composer.tsx key handling, draft stash, caret placement; recalls from and records into the injected source
web/src/pages/chat.tsx builds the feed and injects it
src/daemon/router.ts relayPromptHistory (live-only, capability-gated, no ensure())
src/daemon/http.ts GET /api/v1/workspaces/:id/prompt-history
pi2.nvim lua/pi/web/bridge.lua advertises the promptHistory capability in hello
pi2.nvim lua/pi/web/commands.lua answers workspace.query{kind="promptHistory"} from the workspace archive, and records every prompt.send into it (the TUI records inside Chat:_send_message, which a web send never reaches)
web/tests/prompt-history.test.ts mirror + readline rules + cursor contract (the port of tests/prompt_history_spec.lua)
web/tests/chat.prompt-history-feed.test.tsx relay, fallback, mirror refresh, version skew
web/tests/chat.composer.test.tsx the keyboard rules

Not done (deliberately)

  • Cross-host sharing. One daemon sees one host's plugins. Two machines running their own pi2d still have separate archives; sharing them means a shared store directory (Syncthing/NFS) and switching the plugin's store from whole-file rewrite to append-only JSONL first, or two nvim processes in the same cwd clobber each other.
  • History without nvim. Because the plugin is the authority, a workspace with no live plugin recalls only the mirror. Making the daemon hold the durable store would lift that, at the cost of a second writer (the plugin must still work standalone), conflict resolution and a daemon-owned user-data lifecycle — an ADR decision, not a patch.
  • A "forget this workspace" action. clearPromptHistory(scope) exists and is tested; nothing in the UI calls it yet.