web: live chat transcript over chat_event (C5)
This commit is contained in:
@@ -208,6 +208,14 @@ export async function launchChrome({ width = 1440, height = 900, chromePath } =
|
||||
{ what: `load of ${nextUrl}` },
|
||||
);
|
||||
},
|
||||
/**
|
||||
* Install a script that runs in every document created from now on, before
|
||||
* the page's own scripts — the CDP hook a UI check uses to wrap a global
|
||||
* (for example to record the page's WebSocket) before the app boots.
|
||||
*/
|
||||
async addInitScript(source) {
|
||||
await session("Page.addScriptToEvaluateOnNewDocument", { source });
|
||||
},
|
||||
/** Set an input's value and fire input/change so the app sees it. */
|
||||
async fill(selector, value) {
|
||||
const ok = await api.evaluate(`(() => {
|
||||
|
||||
@@ -10,6 +10,11 @@ import {
|
||||
MAX_RENDERED_DIFF_LINES,
|
||||
TOOL_DETAIL_MAX_LENGTH,
|
||||
TOOL_LONG_RUNNING_MILLIS,
|
||||
TURN_PHASE_THINKING,
|
||||
TURN_PHASE_TOOL,
|
||||
TURN_PHASE_WAITING,
|
||||
TURN_THINKING_WINDOW_MILLIS,
|
||||
applyChatEvent,
|
||||
chatBottomDistancePx,
|
||||
chatFirstScroll,
|
||||
chatFollowsAt,
|
||||
@@ -17,17 +22,23 @@ import {
|
||||
chatNoMatchMessage,
|
||||
chatOpensAtNewest,
|
||||
chatShowJumpToLatest,
|
||||
chatStreamState,
|
||||
closeChatConfirmation,
|
||||
deltaPartId,
|
||||
directoryLabel,
|
||||
filterSessions,
|
||||
formatToolDuration,
|
||||
groupSessionsByDirectory,
|
||||
historyResync,
|
||||
lineDiff,
|
||||
mergeHistoryPage,
|
||||
mergeToolPart,
|
||||
newChatThread,
|
||||
partRendering,
|
||||
prettyToolInput,
|
||||
reasoningLabel,
|
||||
runningToolElapsedMillis,
|
||||
runningToolStamps,
|
||||
sessionActivity,
|
||||
sessionActivityBadge,
|
||||
sessionChipLabel,
|
||||
@@ -36,6 +47,11 @@ import {
|
||||
sessionStatusLabel,
|
||||
sessionTitle,
|
||||
sessionWorkLabel,
|
||||
settledThread,
|
||||
threadElapsedMillis,
|
||||
threadPhase,
|
||||
threadRunningTools,
|
||||
threadToolElapsedMillis,
|
||||
toolCallCount,
|
||||
toolCallRow,
|
||||
toolCallsAnnouncement,
|
||||
@@ -51,6 +67,8 @@ import {
|
||||
toolsRunning,
|
||||
transcriptRows,
|
||||
turnElapsedMillis,
|
||||
turnPhase,
|
||||
turnPhaseLabel,
|
||||
} from "../src/chat.js";
|
||||
import { ChangeLineKind } from "../src/changes.js";
|
||||
|
||||
@@ -648,3 +666,271 @@ test("a chat command rejects a server error envelope with the server's own text"
|
||||
message: "unknown command: chat_messages",
|
||||
});
|
||||
});
|
||||
|
||||
// --- C5: live stream folding ------------------------------------------------
|
||||
// Every frame below is the server's `chat_event` shape (internal/protocol/chat.go
|
||||
// ChatEventFrame). The synthetic frames prove the pure fold; the ui-smoke replays
|
||||
// the same shapes against the real page.
|
||||
|
||||
const evt = (kind, extra = {}) => ({ session_id: "s1", kind, ...extra });
|
||||
|
||||
test("a text delta appends to the part the first frame started", () => {
|
||||
let thread = newChatThread("s1");
|
||||
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "a1#text#0" }, delta: "Hel" }), 1000);
|
||||
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "a1#text#0" }, delta: "lo" }), 1001);
|
||||
|
||||
assert.equal(thread.messages.length, 1);
|
||||
assert.equal(thread.messages[0].role, "assistant");
|
||||
assert.equal(thread.messages[0].parts.length, 1);
|
||||
assert.equal(thread.messages[0].parts[0].text, "Hello");
|
||||
assert.equal(thread.running, true);
|
||||
assert.equal(thread.runningSince, 1000);
|
||||
assert.equal(thread.lastDeltaAt, 1001);
|
||||
});
|
||||
|
||||
test("a text delta and a reasoning delta sharing a bare id land in separate parts", () => {
|
||||
let thread = newChatThread("s1");
|
||||
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "x" }, delta: "answer" }), 1000);
|
||||
thread = applyChatEvent(thread, evt("reasoning", { message_id: "a1", part: { id: "x" }, delta: "think" }), 1001);
|
||||
|
||||
const parts = thread.messages[0].parts;
|
||||
assert.equal(parts.length, 2);
|
||||
const text = parts.find((part) => part.type === "text");
|
||||
const reasoning = parts.find((part) => part.type === "reasoning");
|
||||
assert.equal(text.id, "x");
|
||||
assert.equal(text.text, "answer");
|
||||
assert.equal(reasoning.id, "x#reasoning");
|
||||
assert.equal(reasoning.text, "think");
|
||||
// The id derivation is the one the broker uses once the kinds are distinct.
|
||||
assert.equal(deltaPartId(parts, "text", "x"), "x");
|
||||
assert.equal(deltaPartId(parts, "reasoning", "x"), "x#reasoning");
|
||||
});
|
||||
|
||||
test("tool frames merge field by field without blanking what an earlier frame taught", () => {
|
||||
let thread = newChatThread("s1");
|
||||
thread = applyChatEvent(
|
||||
thread,
|
||||
evt("tool", {
|
||||
message_id: "a1",
|
||||
part: { type: "tool", id: "t1", name: "read", state: { status: "running", input: { filePath: "a.js" } } },
|
||||
}),
|
||||
1000,
|
||||
);
|
||||
// A later frame carries the result but no name and no input: both are retained.
|
||||
thread = applyChatEvent(
|
||||
thread,
|
||||
evt("tool", {
|
||||
message_id: "a1",
|
||||
part: { type: "tool", id: "t1", state: { status: "completed", content: [{ type: "text", text: "done" }] } },
|
||||
}),
|
||||
1001,
|
||||
);
|
||||
// A final frame carries the error but no content: the content is retained too.
|
||||
thread = applyChatEvent(
|
||||
thread,
|
||||
evt("tool", {
|
||||
message_id: "a1",
|
||||
part: { type: "tool", id: "t1", state: { status: "error", error: { message: "boom" } } },
|
||||
}),
|
||||
1002,
|
||||
);
|
||||
|
||||
const tool = thread.messages[0].parts.find((part) => part.type === "tool");
|
||||
assert.equal(tool.name, "read");
|
||||
assert.deepEqual(tool.state.input, { filePath: "a.js" });
|
||||
assert.equal(tool.state.content[0].text, "done");
|
||||
assert.equal(tool.state.error.message, "boom");
|
||||
assert.equal(tool.state.status, "error");
|
||||
// And the pure merge keeps a non-empty incoming field while retaining the prior.
|
||||
const merged = mergeToolPart(
|
||||
{ type: "tool", id: "t1", name: "read", executed: true, state: { status: "running", input: { a: 1 } } },
|
||||
{ type: "tool", id: "t1", name: "", state: { status: "", input: { b: 2 } } },
|
||||
);
|
||||
assert.equal(merged.name, "read");
|
||||
assert.equal(merged.executed, true);
|
||||
assert.equal(merged.state.status, "running");
|
||||
assert.deepEqual(merged.state.input, { b: 2 });
|
||||
});
|
||||
|
||||
test("a repeated user message id adds no second turn and keeps a non-blank client", () => {
|
||||
let thread = newChatThread("s1");
|
||||
const frame = evt("user", { message_id: "u9", part: { text: "hi there" }, client: "Pixel" });
|
||||
thread = applyChatEvent(thread, frame, 1000);
|
||||
thread = applyChatEvent(thread, frame, 1001);
|
||||
|
||||
assert.equal(thread.messages.filter((message) => message.role === "user").length, 1);
|
||||
assert.equal(thread.messages[0].client, "Pixel");
|
||||
assert.equal(thread.messages[0].parts[0].text, "hi there");
|
||||
// Another id with no text or no id at all is ignored.
|
||||
thread = applyChatEvent(thread, evt("user", { message_id: "u10", part: { text: "" } }), 1002);
|
||||
thread = applyChatEvent(thread, evt("user", { part: { text: "no id" } }), 1003);
|
||||
assert.equal(thread.messages.length, 1);
|
||||
});
|
||||
|
||||
test("step started runs the turn and step ended settles it", () => {
|
||||
let thread = newChatThread("s1");
|
||||
thread = applyChatEvent(thread, evt("step", { step: { status: "started", index: 1 } }), 1000);
|
||||
assert.equal(thread.running, true);
|
||||
assert.equal(thread.runningSince, 1000);
|
||||
|
||||
// A tool call in flight leaves a stamp; ending the step clears running and the stamp.
|
||||
thread = applyChatEvent(
|
||||
thread,
|
||||
evt("tool", { message_id: "a1", part: { type: "tool", id: "t1", state: { status: "running" } } }),
|
||||
1001,
|
||||
);
|
||||
assert.equal(thread.toolStartedAt.t1, 1001);
|
||||
thread = applyChatEvent(thread, evt("step", { step: { status: "ended" } }), 2000);
|
||||
assert.equal(thread.running, false);
|
||||
assert.equal(thread.runningSince, 0);
|
||||
assert.deepEqual(thread.toolStartedAt, {});
|
||||
assert.equal(thread.lastDeltaAt, 0);
|
||||
});
|
||||
|
||||
test("any frame clears the queued flag", () => {
|
||||
for (const frame of [
|
||||
evt("text", { message_id: "a1", part: { id: "p" }, delta: "x" }),
|
||||
evt("step", { step: { status: "started" } }),
|
||||
evt("usage", { usage: { input_tokens: 1 } }),
|
||||
evt("shell", { shell: { id: "sh1", status: "running" } }),
|
||||
]) {
|
||||
const queued = { ...newChatThread("s1"), queued: true };
|
||||
assert.equal(applyChatEvent(queued, frame, 1000).queued, false);
|
||||
}
|
||||
});
|
||||
|
||||
test("a shell frame never marks the thread running", () => {
|
||||
const thread = applyChatEvent(
|
||||
newChatThread("s1"),
|
||||
evt("shell", { shell: { id: "sh1", status: "running", command: "go test ./..." } }),
|
||||
1000,
|
||||
);
|
||||
assert.equal(thread.running, false);
|
||||
assert.equal(thread.runningSince, 0);
|
||||
});
|
||||
|
||||
test("a usage frame changes nothing", () => {
|
||||
const before = newChatThread("s1");
|
||||
const after = applyChatEvent(before, evt("usage", { usage: { input_tokens: 5, cost: 0.01 } }), 1000);
|
||||
assert.deepEqual(after, before);
|
||||
});
|
||||
|
||||
test("settling a thread (a socket drop) clears running, its stamps and queued", () => {
|
||||
let thread = newChatThread("s1");
|
||||
thread = applyChatEvent(thread, evt("text", { message_id: "a1", part: { id: "p" }, delta: "x" }), 1000);
|
||||
thread = applyChatEvent(
|
||||
thread,
|
||||
evt("tool", { message_id: "a1", part: { type: "tool", id: "t1", state: { status: "running" } } }),
|
||||
1001,
|
||||
);
|
||||
assert.equal(thread.running, true);
|
||||
assert.equal(thread.toolStartedAt.t1, 1001);
|
||||
|
||||
const settled = settledThread(thread);
|
||||
assert.equal(settled.running, false);
|
||||
assert.equal(settled.runningSince, 0);
|
||||
assert.deepEqual(settled.toolStartedAt, {});
|
||||
assert.equal(settled.lastDeltaAt, 0);
|
||||
assert.equal(settled.queued, false);
|
||||
// The conversation itself is untouched by settling.
|
||||
assert.equal(settled.messages.length, 1);
|
||||
});
|
||||
|
||||
test("historyResync replaces the turn wholesale and settles it", () => {
|
||||
const held = {
|
||||
...newChatThread("s1"),
|
||||
messages: [assistant("a1", [{ type: "text", text: "stale" }])],
|
||||
nextCursor: "old",
|
||||
hasMore: true,
|
||||
loaded: true,
|
||||
running: true,
|
||||
runningSince: 555,
|
||||
toolStartedAt: { t1: 555 },
|
||||
lastDeltaAt: 600,
|
||||
};
|
||||
const page = {
|
||||
messages: [user("u1", "hi"), assistant("a2", [{ type: "text", text: "fresh" }])],
|
||||
nextCursor: "",
|
||||
hasMore: false,
|
||||
};
|
||||
|
||||
const resynced = historyResync(held, page);
|
||||
assert.deepEqual(resynced.messages, page.messages);
|
||||
assert.ok(!resynced.messages.some((message) => message.id === "a1"));
|
||||
assert.equal(resynced.nextCursor, "");
|
||||
assert.equal(resynced.hasMore, false);
|
||||
assert.equal(resynced.loaded, true);
|
||||
assert.equal(resynced.running, false);
|
||||
assert.equal(resynced.runningSince, 0);
|
||||
assert.deepEqual(resynced.toolStartedAt, {});
|
||||
assert.equal(resynced.lastDeltaAt, 0);
|
||||
});
|
||||
|
||||
test("the chat stream badge uses the live / reconnecting / not live words", () => {
|
||||
assert.deepEqual(chatStreamState("connected", true), { tone: "live", label: "live" });
|
||||
assert.equal(chatStreamState("connected", false).label, "not live");
|
||||
assert.equal(chatStreamState("connecting", true).label, "reconnecting");
|
||||
assert.equal(chatStreamState("disconnected", true).label, "reconnecting");
|
||||
assert.equal(chatStreamState("idle", false).label, "not live");
|
||||
});
|
||||
|
||||
test("the turn phase names what is happening", () => {
|
||||
assert.equal(turnPhase([], 1000), TURN_PHASE_THINKING);
|
||||
assert.equal(turnPhaseLabel(TURN_PHASE_THINKING), "Thinking…");
|
||||
assert.equal(turnPhase([{ type: "tool", id: "t", state: { status: "running" } }], 100000), TURN_PHASE_TOOL);
|
||||
assert.equal(turnPhaseLabel(TURN_PHASE_TOOL), "Running a tool…");
|
||||
assert.equal(turnPhase([], null), TURN_PHASE_WAITING);
|
||||
assert.equal(turnPhase([], TURN_THINKING_WINDOW_MILLIS), TURN_PHASE_WAITING);
|
||||
assert.equal(turnPhaseLabel(TURN_PHASE_WAITING), "Waiting…");
|
||||
});
|
||||
|
||||
test("only a call at or past 60s reads as long", () => {
|
||||
assert.equal(toolLongRunning(TOOL_LONG_RUNNING_MILLIS - 1), false);
|
||||
assert.equal(toolLongRunning(TOOL_LONG_RUNNING_MILLIS), true);
|
||||
assert.equal(TOOL_LONG_RUNNING_MILLIS, 60000);
|
||||
});
|
||||
|
||||
test("a live turn interleaving reasoning, text and tool frames folds to one agent row", () => {
|
||||
let thread = newChatThread("live");
|
||||
const frames = [
|
||||
evt("reasoning", { message_id: "a1", part: { id: "r0" }, delta: "think" }),
|
||||
evt("text", { message_id: "a1", part: { id: "t0" }, delta: "Hel" }),
|
||||
evt("tool", { message_id: "a1", part: { type: "tool", id: "tool1", name: "read", state: { status: "running" } } }),
|
||||
evt("tool", {
|
||||
message_id: "a1",
|
||||
part: { type: "tool", id: "tool1", state: { status: "completed", content: [{ type: "text", text: "ok" }] } },
|
||||
}),
|
||||
evt("reasoning", { message_id: "a2", part: { id: "r0" }, delta: "more" }),
|
||||
evt("text", { message_id: "a2", part: { id: "t0" }, delta: "lo" }),
|
||||
];
|
||||
for (const frame of frames) thread = applyChatEvent(thread, frame, 1000);
|
||||
|
||||
const rows = transcriptRows(thread.messages);
|
||||
const agentRows = rows.filter((row) => row.kind === "agent");
|
||||
assert.equal(agentRows.length, 1);
|
||||
assert.equal(agentRows[0].key, "a1");
|
||||
assert.deepEqual(agentRows[0].turn.reasoningSteps, ["think", "more"]);
|
||||
assert.equal(agentRows[0].turn.tools.length, 1);
|
||||
assert.equal(agentRows[0].turn.answer, "Hel\n\nlo");
|
||||
assert.equal(threadRunningTools(thread.messages).length, 0);
|
||||
});
|
||||
|
||||
test("the running indicator's elapsed and per-tool timing use the thread's own stamps", () => {
|
||||
let thread = newChatThread("s1");
|
||||
thread = applyChatEvent(thread, evt("step", { step: { status: "started" } }), 1000);
|
||||
thread = applyChatEvent(
|
||||
thread,
|
||||
evt("tool", {
|
||||
message_id: "a1",
|
||||
part: { type: "tool", id: "t1", name: "shell", state: { status: "running", input: { command: "go build" } } },
|
||||
}),
|
||||
1500,
|
||||
);
|
||||
|
||||
assert.equal(threadElapsedMillis(thread, 3000), 2000);
|
||||
assert.equal(formatToolDuration(threadToolElapsedMillis(thread, thread.messages[0].parts[0], 3000)), "1.5s");
|
||||
assert.equal(threadPhase(thread, 3000), TURN_PHASE_TOOL);
|
||||
// runningToolStamps keeps a stamp it already had and drops one that settled.
|
||||
assert.deepEqual(runningToolStamps({ t1: 42 }, thread.messages, 9999), { t1: 42 });
|
||||
assert.deepEqual(runningToolStamps({ t1: 42 }, [], 9999), {});
|
||||
});
|
||||
|
||||
@@ -90,9 +90,11 @@ test("an error reply surfaces the server text as a ProtocolError", async () => {
|
||||
await assert.rejects(() => client.call({ type: "get_run" }), ProtocolError);
|
||||
});
|
||||
|
||||
test("an unsolicited envelope with no matching id is dropped, not misattributed", async () => {
|
||||
test("an unsolicited envelope is surfaced as a frame event and not misattributed", async () => {
|
||||
const { client, ws } = connectedClient();
|
||||
ws.reply = undefined;
|
||||
const frames = [];
|
||||
client.addEventListener("frame", (event) => frames.push(event.detail));
|
||||
|
||||
const pending = client.call({ type: "ping" });
|
||||
let settled = false;
|
||||
@@ -102,10 +104,29 @@ test("an unsolicited envelope with no matching id is dropped, not misattributed"
|
||||
ws.push({ type: "pipelines", pipelines: [] }); // no id
|
||||
await flush();
|
||||
assert.equal(settled, false);
|
||||
assert.deepEqual(frames, [{ type: "pipelines", pipelines: [] }]);
|
||||
|
||||
const frame = ws.sent.at(-1);
|
||||
ws.push({ id: frame.id, type: "pong" });
|
||||
assert.equal((await pending).type, "pong");
|
||||
// A matched reply is not also broadcast as an unsolicited frame.
|
||||
assert.equal(frames.length, 1);
|
||||
});
|
||||
|
||||
test("a server-pushed chat_event frame reaches a frame listener", async () => {
|
||||
const { client, ws } = connectedClient();
|
||||
const frames = [];
|
||||
client.addEventListener("frame", (event) => frames.push(event.detail));
|
||||
|
||||
ws.push({
|
||||
type: "chat_event",
|
||||
chat_event: { session_id: "ses_1", message_id: "a1", kind: "text", delta: "hi" },
|
||||
});
|
||||
await flush();
|
||||
|
||||
assert.equal(frames.length, 1);
|
||||
assert.equal(frames[0].type, "chat_event");
|
||||
assert.equal(frames[0].chat_event.session_id, "ses_1");
|
||||
});
|
||||
|
||||
test("a malformed frame is ignored and does not reject a pending call", async () => {
|
||||
|
||||
@@ -231,3 +231,29 @@ test("a run action throws when the reply has no run detail", async () => {
|
||||
ws.reply = (frame) => ({ id: frame.id, type: "run" });
|
||||
await assert.rejects(() => repo.cancelRun(7), ProtocolError);
|
||||
});
|
||||
|
||||
test("subscribeChat sends exactly {type:'chat_subscribe',chat_subscribe:{session_id}}", async () => {
|
||||
const { repo, ws } = connected();
|
||||
ws.reply = (frame) => ({ id: frame.id, type: "chat_subscribe" });
|
||||
|
||||
await repo.subscribeChat("ses_eae413f50ffeLQDExlg8cYnscN");
|
||||
assert.deepEqual(lastFrame(ws), {
|
||||
type: "chat_subscribe",
|
||||
chat_subscribe: { session_id: "ses_eae413f50ffeLQDExlg8cYnscN" },
|
||||
});
|
||||
});
|
||||
|
||||
test("subscribeChat rejects a blank session id without sending anything", async () => {
|
||||
const { repo, ws } = connected();
|
||||
await assert.rejects(() => repo.subscribeChat(" "), ProtocolError);
|
||||
assert.equal(ws.sent.length, 0);
|
||||
});
|
||||
|
||||
test("subscribeChat surfaces a server refusal as a ProtocolError", async () => {
|
||||
const { repo, ws } = connected();
|
||||
ws.reply = (frame) => ({ id: frame.id, type: "error", error: "unknown command: chat_subscribe" });
|
||||
await assert.rejects(() => repo.subscribeChat("ses_1"), {
|
||||
name: "ProtocolError",
|
||||
message: "unknown command: chat_subscribe",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -266,6 +266,30 @@ try {
|
||||
browser = await launchChrome({ width: 1440, height: 900 });
|
||||
page = await browser.openPage(`http://127.0.0.1:${port}/`);
|
||||
|
||||
// C5 evidence needs to replay exact `chat_event` frames against the real page
|
||||
// (a real live turn is not available on demand without disrupting the operator's
|
||||
// server). A tiny WebSocket shim is installed before the page's own scripts on
|
||||
// every document from now on: it constructs the real socket and records it on
|
||||
// `window`, so the smoke can hand frames to the socket's own `onmessage` and the
|
||||
// app folds them through its real path. Every other frame (the subscribe ack, the
|
||||
// history reply) still comes from the real server.
|
||||
step("installing the WebSocket recorder shim before the page scripts");
|
||||
await page.addInitScript(`(() => {
|
||||
const Native = window.WebSocket;
|
||||
if (!Native || window.__pipelinesSocketShim) return;
|
||||
window.__pipelinesSocketShim = true;
|
||||
window.__pipelinesSockets = [];
|
||||
function Shim(url, protocols) {
|
||||
const ws = protocols === undefined ? new Native(url) : new Native(url, protocols);
|
||||
window.__pipelinesSockets.push(ws);
|
||||
window.__pipelinesSocket = ws;
|
||||
return ws;
|
||||
}
|
||||
Shim.prototype = Native.prototype;
|
||||
for (const key of ["CONNECTING", "OPEN", "CLOSING", "CLOSED"]) Shim[key] = Native[key];
|
||||
window.WebSocket = Shim;
|
||||
})()`);
|
||||
|
||||
// Deterministic dismissals: the attention set is read from localStorage once at
|
||||
// boot, so clear the key and reload before connecting so the surface derives
|
||||
// from an empty set regardless of any stale profile state.
|
||||
@@ -2673,6 +2697,215 @@ try {
|
||||
`${openedId}`,
|
||||
);
|
||||
|
||||
// C5: the shared live stream. A real live turn needs a prompt the operator's
|
||||
// server may not be running, so the real page is subscribed to the real
|
||||
// session and then exact `chat_event` frames are replayed through the socket's
|
||||
// own `onmessage` (see the shim installed before the page's scripts). The
|
||||
// subscribe ack and every history reply are real server traffic; only the
|
||||
// frames replayed below are synthetic, and this log says so explicitly.
|
||||
step("C5: the live stream — subscribe, replayed chat_event frames, stream state");
|
||||
await ensureConnected();
|
||||
await page.evaluate(`document.querySelector("#chatJump")?.click()`);
|
||||
|
||||
const chatBadge = () =>
|
||||
page.evaluate(
|
||||
`document.querySelector(".chat-context .live-badge .live-word")?.textContent ?? ""`,
|
||||
);
|
||||
const chatLive = await chatBadge();
|
||||
if (chatLive !== "live") {
|
||||
throw new Error(`chat stream badge should read "live" while subscribed, got ${JSON.stringify(chatLive)}`);
|
||||
}
|
||||
console.log(` chat stream badge while subscribed: "${chatLive}" (real chat_subscribe ack + real history)`);
|
||||
|
||||
const sessionId = openedId;
|
||||
const replayFrames = (frames) =>
|
||||
page.evaluate(`(() => {
|
||||
const ws = window.__pipelinesSocket;
|
||||
if (!ws || typeof ws.onmessage !== "function") return false;
|
||||
for (const frame of ${JSON.stringify(frames)}) {
|
||||
ws.onmessage({ data: JSON.stringify({ type: "chat_event", chat_event: frame }) });
|
||||
}
|
||||
return true;
|
||||
})()`);
|
||||
const agentCount = () => page.evaluate(`document.querySelectorAll("#chatTranscript .chat-turn-agent").length`);
|
||||
const indicator = () =>
|
||||
page.evaluate(`(() => {
|
||||
const el = document.querySelector("#chatRunning");
|
||||
if (!el) return null;
|
||||
return {
|
||||
spinner: !!el.querySelector(".spinner"),
|
||||
label: el.querySelector(".chat-running-label")?.textContent ?? "",
|
||||
elapsed: el.querySelector(".chat-running-elapsed")?.textContent ?? "",
|
||||
tools: [...el.querySelectorAll(".chat-running-tool")].map((line) => ({
|
||||
text: line.querySelector(".chat-running-tool-text")?.textContent ?? "",
|
||||
long: line.querySelector(".chat-tool-long")?.textContent ?? "",
|
||||
longTinted: line.classList.contains("chat-running-tool-long"),
|
||||
})),
|
||||
};
|
||||
})()`);
|
||||
|
||||
const applyFrames = async (frames, label) => {
|
||||
const ok = await replayFrames(frames);
|
||||
if (!ok) throw new Error(`C5: the socket shim did not expose the live socket to replay ${label}`);
|
||||
await eventually(() => page.evaluate(`!!document.querySelector("#chatTranscript")`), {
|
||||
what: "the transcript to render",
|
||||
});
|
||||
};
|
||||
|
||||
const agentBefore = await agentCount();
|
||||
const sessionFrame = (extra) => ({ session_id: sessionId, ...extra });
|
||||
|
||||
// 1. Reasoning and text deltas on one assistant message id: the answer grows
|
||||
// inside the existing agent bubble.
|
||||
await applyFrames(
|
||||
[
|
||||
sessionFrame({ message_id: "cap-live-1", kind: "reasoning", part: { id: "cap-live-1#reasoning#0" }, delta: "Planning the live capture." }),
|
||||
sessionFrame({ message_id: "cap-live-1", kind: "text", part: { id: "cap-live-1#text#0" }, delta: "The live turn is " }),
|
||||
sessionFrame({ message_id: "cap-live-1", kind: "text", part: { id: "cap-live-1#text#0" }, delta: "growing." }),
|
||||
],
|
||||
"the reasoning/text deltas",
|
||||
);
|
||||
const liveTurn = await page.evaluate(`(() => {
|
||||
const transcript = document.querySelector("#chatTranscript");
|
||||
const bubbles = [...transcript.querySelectorAll(".chat-turn-agent")];
|
||||
return {
|
||||
agents: bubbles.length,
|
||||
answers: bubbles.filter((b) => b.textContent.includes("The live turn is growing.")).length,
|
||||
reasoning: bubbles.some((b) => b.textContent.includes("Planning the live capture.")),
|
||||
};
|
||||
})()`);
|
||||
console.log(
|
||||
` replayed 3 frames (reasoning + 2 text deltas) → ${liveTurn.answers} agent bubble carries ` +
|
||||
`"The live turn is growing." + reasoning (transcript agent bubbles ${agentBefore} -> ${liveTurn.agents})`,
|
||||
);
|
||||
if (liveTurn.answers !== 1 || !liveTurn.reasoning || liveTurn.agents !== agentBefore) {
|
||||
throw new Error(`C5: the replayed deltas did not grow one agent bubble: ${JSON.stringify(liveTurn)}`);
|
||||
}
|
||||
await shot("56-chat-live-turn.png");
|
||||
|
||||
// 2. A running tool: the indicator's headline is `Running a tool…` with a
|
||||
// running-tool line and a live `· {elapsed}`.
|
||||
await applyFrames(
|
||||
[sessionFrame({ message_id: "cap-live-1", kind: "tool", part: { type: "tool", id: "cap-tool-1", name: "shell", state: { status: "running", input: { command: "npm run build" } } } })],
|
||||
"the running tool",
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 1300));
|
||||
const running = await indicator();
|
||||
if (
|
||||
!running ||
|
||||
!running.spinner ||
|
||||
running.label !== "Running a tool…" ||
|
||||
!/^· \d/.test(running.elapsed) ||
|
||||
running.tools.length < 1
|
||||
) {
|
||||
throw new Error(`C5: the running indicator is wrong: ${JSON.stringify(running)}`);
|
||||
}
|
||||
console.log(
|
||||
` running indicator: spinner=${running.spinner}, "${running.label}", "${running.elapsed}", ` +
|
||||
`${running.tools.length} running-tool line(s) — "${running.tools[0].text}"`,
|
||||
);
|
||||
await shot("57-chat-running.png");
|
||||
|
||||
// 3. A second running tool that started 61 s ago (protocol `time.ran`): the
|
||||
// `· long` mark, tinted with the error hue. Two assistant message ids are in
|
||||
// play now and the turn is still one bubble.
|
||||
await applyFrames(
|
||||
[
|
||||
sessionFrame({
|
||||
message_id: "cap-live-2",
|
||||
kind: "reasoning",
|
||||
part: { id: "cap-live-2#reasoning#0" },
|
||||
delta: "Streaming the second step.",
|
||||
}),
|
||||
sessionFrame({
|
||||
message_id: "cap-live-2",
|
||||
kind: "tool",
|
||||
part: { type: "tool", id: "cap-tool-2", name: "shell", state: { status: "running", input: { command: "npm run long" } }, time: { ran: Date.now() - 61000 } },
|
||||
}),
|
||||
],
|
||||
"the long-running tool",
|
||||
);
|
||||
const longCall = await indicator();
|
||||
const longLine = longCall?.tools.find((line) => line.long !== "");
|
||||
if (!longLine || !longLine.longTinted || !longLine.long.includes("long")) {
|
||||
throw new Error(`C5: no tinted long-running tool line: ${JSON.stringify(longCall)}`);
|
||||
}
|
||||
const agentsWithTwoIds = await agentCount();
|
||||
console.log(` long-call line: "${longLine.text}${longLine.long}" tinted=${longLine.longTinted}`);
|
||||
if (agentsWithTwoIds !== agentBefore) {
|
||||
throw new Error(`C5: two assistant message ids fragmented the live turn into ${agentsWithTwoIds} bubbles`);
|
||||
}
|
||||
await shot("58-chat-long-call.png");
|
||||
|
||||
// 4. Finish the tools and end the step: the indicator disappears and the same
|
||||
// single agent bubble stands, settled.
|
||||
await applyFrames(
|
||||
[
|
||||
sessionFrame({ message_id: "cap-live-1", kind: "tool", part: { type: "tool", id: "cap-tool-1", state: { status: "completed", content: [{ type: "text", text: "build ok" }] } } }),
|
||||
sessionFrame({ message_id: "cap-live-2", kind: "tool", part: { type: "tool", id: "cap-tool-2", state: { status: "completed", content: [{ type: "text", text: "long ok" }] } } }),
|
||||
sessionFrame({ message_id: "cap-live-2", kind: "step", step: { status: "ended" } }),
|
||||
],
|
||||
"the completed tools and the ended step",
|
||||
);
|
||||
const settled = await page.evaluate(`(() => {
|
||||
const transcript = document.querySelector("#chatTranscript");
|
||||
return {
|
||||
indicator: !!document.querySelector("#chatRunning"),
|
||||
agents: transcript.querySelectorAll(".chat-turn-agent").length,
|
||||
answers: [...transcript.querySelectorAll(".chat-turn-agent")].filter((b) => b.textContent.includes("The live turn is growing.")).length,
|
||||
};
|
||||
})()`);
|
||||
if (settled.indicator) throw new Error("C5: the running indicator survived the ended step");
|
||||
if (settled.agents !== agentBefore || settled.answers !== 1) {
|
||||
throw new Error(`C5: the settled turn is not the same single agent bubble: ${JSON.stringify(settled)}`);
|
||||
}
|
||||
console.log(
|
||||
` after the tools completed and the step ended: no indicator, still ${settled.agents} agent bubble ` +
|
||||
`carrying the live answer (unchanged from ${agentBefore})`,
|
||||
);
|
||||
await shot("59-chat-settled.png");
|
||||
|
||||
// The stream-state words. Dropping the socket unexpectedly reads `reconnecting`
|
||||
// while the client comes back; the reconnect re-subscribes the open chat and
|
||||
// refreshes its history wholesale, so the replayed turn above is gone — which is
|
||||
// exactly the promised wholesale replacement.
|
||||
await page.evaluate(`window.__pipelinesSocket && window.__pipelinesSocket.close(4000, "smoke drop")`);
|
||||
await eventually(
|
||||
() => page.evaluate(`document.querySelector(".chat-context .live-badge .live-word")?.textContent === "reconnecting"`),
|
||||
{ timeout: 10000, what: "the chat badge to read reconnecting" },
|
||||
);
|
||||
console.log(` chat stream badge after an unexpected drop: "${await chatBadge()}"`);
|
||||
await ensureConnected(30000);
|
||||
await eventually(
|
||||
() => page.evaluate(`document.querySelector(".chat-context .live-badge .live-word")?.textContent === "live"`),
|
||||
{ timeout: 30000, what: "the chat badge to return to live after reconnect" },
|
||||
);
|
||||
// The badge turns live as soon as the subscription is re-attached; the history
|
||||
// refresh that replaces the conversation follows it, so wait for the replayed
|
||||
// marker to actually disappear rather than racing the fetch.
|
||||
await eventually(
|
||||
() =>
|
||||
page.evaluate(
|
||||
`!document.querySelector("#chatTranscript")?.textContent?.includes("The live turn is growing.")`,
|
||||
),
|
||||
{ timeout: 20000, what: "the reconnect to replace the conversation with fresh history" },
|
||||
);
|
||||
console.log(
|
||||
' after reconnect: chat badge "live" again and history replaced the replayed turn wholesale (marker gone)',
|
||||
);
|
||||
|
||||
// The third word: disconnecting entirely reads `not live`.
|
||||
await page.click("#connectBtn");
|
||||
await eventually(
|
||||
() => page.evaluate(`document.querySelector(".chat-context .live-badge .live-word")?.textContent === "not live"`),
|
||||
{ timeout: 15000, what: "the chat badge to read not live while idle" },
|
||||
);
|
||||
console.log(` chat stream badge after Disconnect: "${await chatBadge()}"`);
|
||||
|
||||
console.log(
|
||||
" NOTE: the chat_subscribe ack and every chat_history reply above came from the real server; only the chat_event frames were replayed",
|
||||
);
|
||||
|
||||
console.log("\nUI SMOKE PASS");
|
||||
for (const shot of shots) console.log(` ${shot}`);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user