fix(web): make the web console work end-to-end

- Pair through the public POST /api/v1/pair and persist the device scopes;
  keep the allow-listed next deep link. Rename the manual field to pairing code.
- Authenticate the WebSocket with the raw device token (strip Bearer) and
  recover from a 401 by clearing the connection once.
- Fix chat payloads: setModel/setThinking, attention/diff responses, subagent
  dispatch items, queue ids/mode, task output and WS resume since.
- Render exactly one assistant message per turn: one-to-one snapshot/history
  merge plus a stale-delta guard after message_end (fixes PONGONG).
- Fix dialogs (content above the overlay), unknown-session deep links
  (error + retry, no silent fallback), device list/revoke, status identity and
  live pairing code, reconnect banner and Playwright selectors.
This commit is contained in:
yuez committed 2026-10-21 20:42:19 +08:00
1 parent 5572ac0d84
commit 0ef019f58d
57 files changed
+3549 -684

No files matched your search

+95
View File
@@ -121,6 +121,101 @@ describe("history entry projection", () => {
expect(new Set(combined.map((entry) => entry.id)).size).toBe(combined.length);
});
it("dedupes a reload where the snapshot used synthetic ids and the pages raw ids", () => {
const history = projectEntries([
{ type: "message", id: "u1", message: { role: "user", content: "hello", timestamp: TS } },
{
type: "message",
id: "a1",
message: {
role: "assistant",
timestamp: TS + 1,
content: [{ type: "text", text: "on it" }],
},
},
{ type: "message", id: "u2", message: { role: "user", content: "and again", timestamp: TS + 2 } },
]);
// The live snapshot replays the first two messages under `m<N>` ids.
const live = [
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "hello" }] }),
entryFixture({ id: "m2", role: "assistant", ts: TS + 1, blocks: [{ type: "text", text: "on it" }] }),
];
const merged = mergeEntries(history, live);
// One copy each, still in transcript order, with the live entry winning.
expect(merged.map((entry) => entry.id)).toEqual(["m1", "m2", "u2"]);
expect(merged.filter((entry) => entry.role === "user")).toHaveLength(2);
expect(merged[0]?.ts).toBe(TS);
});
it("keeps live-only entries and drops raw duplicates when prepending a page", () => {
const live = [
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "hello" }] }),
entryFixture({ id: "m2", role: "assistant", ts: TS + 1, blocks: [{ type: "text", text: "on it" }] }),
];
const older = projectEntries([
{ type: "message", id: "u0", message: { role: "user", content: "first", timestamp: TS - 10 } },
{ type: "message", id: "u1", message: { role: "user", content: "hello", timestamp: TS } },
]);
const combined = prependUnique(live, older);
expect(combined.map((entry) => entry.id)).toEqual(["u0", "m1", "m2"]);
});
it("renders one assistant entry when a snapshot tail and a raw history page carry the same reply", () => {
// The snapshot tail carries plugin-side synthetic ids and the plugin
// event clock; the page carries pi's raw ids and message clock, so the
// same reply lands in a different second bucket than the live entry.
const historyTs = TS + 1;
const liveTs = historyTs + 1_400;
const history = projectEntries([
{ type: "message", id: "u1", message: { role: "user", content: "ping", timestamp: TS } },
{
type: "message",
id: "a1",
message: { role: "assistant", timestamp: historyTs, content: [{ type: "text", text: "PONG" }] },
},
]);
const live = [
entryFixture({ id: "m1", role: "user", ts: TS, blocks: [{ type: "text", text: "ping" }] }),
entryFixture({ id: "m2", role: "assistant", ts: liveTs, blocks: [{ type: "text", text: "PONG" }] }),
];
const merged = mergeEntries(history, live);
expect(merged.map((entry) => entry.id)).toEqual(["m1", "m2"]);
expect(merged.filter((entry) => entry.role === "assistant")).toHaveLength(1);
// A reconnect replays the snapshot against the same page: still one each.
const replayed = mergeEntries(history, live.map((entry) => ({ ...entry })));
expect(replayed.map((entry) => entry.id)).toEqual(["m1", "m2"]);
expect(replayed.filter((entry) => entry.role === "assistant")).toHaveLength(1);
});
it("keeps genuinely repeated replies instead of collapsing them", () => {
const history = projectEntries([
{ type: "message", id: "u1", message: { role: "user", content: "hi", timestamp: TS } },
{
type: "message",
id: "a1",
message: { role: "assistant", timestamp: TS + 1, content: [{ type: "text", text: "ok" }] },
},
{ type: "message", id: "u2", message: { role: "user", content: "hi", timestamp: TS + 2 } },
{
type: "message",
id: "a2",
message: { role: "assistant", timestamp: TS + 3, content: [{ type: "text", text: "ok" }] },
},
]);
const live = [
entryFixture({ id: "m3", role: "user", ts: TS + 2, blocks: [{ type: "text", text: "hi" }] }),
entryFixture({ id: "m4", role: "assistant", ts: TS + 3, blocks: [{ type: "text", text: "ok" }] }),
];
const merged = mergeEntries(history, live);
expect(merged.map((entry) => entry.id)).toEqual(["u1", "a1", "m3", "m4"]);
expect(merged).toHaveLength(4);
});
it("normalizes the entries page cursor", () => {
expect(
normalizeEntriesPage({