feat(daemon): serve a live-only prompt-history read

The web composer's ↑/↓ recall needs the plugin's prompt history, so the browser
and nvim walk one list instead of two. The daemon relays it and stores nothing:
it is a router, and the plugin stays the authority (AGENTS.md rule 2).

- plugin `hello` advertises the new `promptHistory` capability; the daemon never
  relays the read to a plugin that did not advertise it
- `workspace.query{kind:"promptHistory",limit}` is answered by the plugin from
  its own `prompt.history` archive (workspace cwd), oldest-first
- `GET /api/v1/workspaces/:id/prompt-history?limit=` → `{v, workspaceId, live,
  supported, entries}`; `limit` defaults to 200 and clamps to 500, an unknown
  workspace is a 404, and malformed entries are dropped
- the read deliberately does **not** call `instances.ensure()`: recall is a
  keystroke, not a lifecycle event, so a workspace with no connected plugin
  answers `{live:false, supported:false, entries:[]}` instead of booting an nvim
- `PluginConnection.capabilities` joins the interface the daemon sees, and the
  fake connection grows a writable one so the gate is testable

No event or reducer change: `fixtures/` are untouched.
This commit is contained in:
yuez committed 2026-11-02 11:12:21 +08:00
1 parent c24f30fe71
commit 32292873f9
6 files changed
+204

No files matched your search

+2
View File
@@ -22,6 +22,8 @@ export interface PluginConnection {
readonly hasUI: boolean;
readonly listenAddr: string | null;
readonly pluginVersion: string;
/** Capability strings the plugin advertised in its `hello` (`snapshot`, `promptHistory`, …). */
readonly capabilities: string[];
/** Send one JSONL record to the plugin. */
send(record: Record<string, unknown>): void;
/** Forward a command; resolves with the plugin `ack` payload. */
+14
View File
@@ -331,6 +331,20 @@ export function createApp(deps: AppDeps): Hono<Env> {
return json(200, ack);
});
// Composer ↑ recall. Live-only by construction: the router reads an already
// connected instance and never calls `instances.ensure()`, so a recall can
// never spawn an nvim. An absent instance answers `{live:false,supported:false}`
// and an uncapable one `{live:true,supported:false}`, both with no entries.
app.get("/api/v1/workspaces/:id/prompt-history", async (c) => {
const workspaceId = c.req.param("id");
if (!workspaces.get(workspaceId)) throw DaemonError.notFound(`unknown workspace ${workspaceId}`);
const raw = c.req.query("limit");
// Absent or empty (a bare `?limit=`) lets the router apply its default.
const limit = raw === undefined || raw === "" ? undefined : Number(raw);
const result = await router.relayPromptHistory(workspaceId, limit);
return json(200, { v: 1, workspaceId, ...result });
});
app.get("/api/v1/models", async (c) => {
const workspaceId = c.req.query("workspaceId");
if (workspaceId) {
+56
View File
@@ -13,6 +13,10 @@ import { DaemonError, toErrorPayload } from "./errors.js";
const DEDUPE_TTL_MS = 5 * 60 * 1000;
/** Prompt-history read sizing: the composer recalls at most this many entries. */
const PROMPT_HISTORY_DEFAULT_LIMIT = 200;
const PROMPT_HISTORY_MAX_LIMIT = 500;
export interface RouterDeps {
auth: AuthStore;
instances: InstanceManager;
@@ -54,6 +58,30 @@ function optionalString(params: Record<string, unknown>, key: string): string |
return typeof value === "string" && value.trim() !== "" ? value : undefined;
}
/** Clamp a caller-supplied prompt-history limit; garbage falls back to the default. */
function clampPromptHistoryLimit(raw: number | undefined): number {
if (typeof raw !== "number" || !Number.isFinite(raw)) return PROMPT_HISTORY_DEFAULT_LIMIT;
return Math.max(1, Math.min(PROMPT_HISTORY_MAX_LIMIT, Math.floor(raw)));
}
/**
* Keep only usable entries and trim to the newest `limit`.
*
* The plugin answers oldest-first, so the tail is the recent end; a missing or
* malformed `entries` payload degrades to an empty list rather than throwing.
*/
function normalizePromptHistoryEntries(raw: unknown, limit: number): string[] {
if (!Array.isArray(raw)) return [];
const entries = raw.filter((value): value is string => typeof value === "string" && value.trim() !== "");
return entries.slice(-limit);
}
export interface PromptHistoryResult {
live: boolean;
supported: boolean;
entries: string[];
}
export class CommandRouter {
private readonly auth: AuthStore;
private readonly instances: InstanceManager;
@@ -148,6 +176,34 @@ export class CommandRouter {
return this.relay(workspaceId, sessionId, method, params);
}
/**
* Live-only workspace read used by `GET /workspaces/:id/prompt-history`.
*
* Recall is a convenience, never a lifecycle event: this looks up an already
* connected instance and deliberately does **not** call `ensure()`, so pressing
* ↑ in the composer cannot boot an nvim. No connection answers
* `{live:false,supported:false}`, an instance that did not advertise
* `promptHistory` in its `hello` gets no command at all, and only a capable
* connection sees a relay (workspace-scoped: no `sessionId` on the envelope).
*/
async relayPromptHistory(workspaceId: string, limit?: number): Promise<PromptHistoryResult> {
if (typeof workspaceId !== "string" || workspaceId === "") {
throw DaemonError.badRequest("workspaceId is required");
}
const effectiveLimit = clampPromptHistoryLimit(limit);
const connection = this.instances.get(workspaceId);
if (!connection) return { live: false, supported: false, entries: [] };
if (!connection.capabilities.includes("promptHistory")) {
return { live: true, supported: false, entries: [] };
}
const ack = await connection.command({
id: `req-${randomUUID()}`,
method: "workspace.query",
params: { kind: "promptHistory", limit: effectiveLimit },
});
return { live: true, supported: true, entries: normalizePromptHistoryEntries(ack.entries, effectiveLimit) };
}
private async relay(
workspaceId: string,
sessionId: string | undefined,