feat(web): show native context compaction activity in chat

This commit is contained in:
2026-10-20 22:15:03 +01:00
parent aa57fe8a04
commit d8b111913e
9 changed files with 216 additions and 11 deletions
+28 -3
View File
@@ -1209,6 +1209,7 @@ export function mergeHistoryPage(previous, page, { older = false } = {}) {
/** ChatEvent.Kind values pushed on the `chat_event` stream (internal/protocol/chat.go). */
export const EVENT_STEP = "step";
export const EVENT_COMPACTION = "compaction";
export const EVENT_USAGE = "usage";
export const EVENT_USER = "user";
export const EVENT_SHELL = "shell";
@@ -1222,6 +1223,7 @@ export const STEP_ENDED = "ended";
export const TURN_PHASE_THINKING = "thinking";
export const TURN_PHASE_TOOL = "tool";
export const TURN_PHASE_WAITING = "waiting";
export const TURN_PHASE_COMPACTING = "compacting";
/**
* The live-stream badge words, shared verbatim with `liveBadgeState` /
@@ -1396,6 +1398,7 @@ export function newChatThread(sessionId = "") {
error: "",
running: false,
runningSince: 0,
compaction: null,
toolStartedAt: {},
lastDeltaAt: 0,
queued: false,
@@ -1430,6 +1433,7 @@ export function settledThread(thread) {
...(thread ?? {}),
running: false,
runningSince: 0,
compaction: null,
toolStartedAt: {},
lastDeltaAt: 0,
queued: false,
@@ -1555,6 +1559,7 @@ export function turnPhase(tools) {
/** The running indicator's headline for [phase], naming what is happening. */
export function turnPhaseLabel(phase) {
if (phase === TURN_PHASE_COMPACTING) return "Compacting context…";
if (phase === TURN_PHASE_THINKING) return "Thinking…";
if (phase === TURN_PHASE_TOOL) return "Running a tool…";
return "Waiting…";
@@ -1567,6 +1572,7 @@ export function turnPhaseLabel(phase) {
*/
export function threadPhase(thread, nowMillis) {
if (!thread?.running) return null;
if (thread.compaction?.status === "running") return TURN_PHASE_COMPACTING;
if (thread.queued) return TURN_PHASE_WAITING;
return turnPhase(threadRunningTools(thread.messages));
}
@@ -1609,6 +1615,7 @@ export function applyChatForm(thread, form) {
* targets is created on first sight, so a live turn never gains a second bubble.
*/
export function applyChatEvent(thread, frame, now = Date.now()) {
if (frame?.kind === EVENT_COMPACTION) return applyChatCompaction(thread, frame, now);
if (frame?.kind === EVENT_RESYNC) return thread ?? newChatThread(str(frame?.session_id));
if (frame?.kind === EVENT_STEP && frame.step?.status === STEP_ENDED && frame.step?.turn_id && frame.step.turn_id !== thread?.nativeTurnId) return thread ?? newChatThread(str(frame?.session_id));
const current = thread?.queued
@@ -1641,6 +1648,21 @@ export function applyChatEvent(thread, frame, now = Date.now()) {
}
}
/** Native session activity, not a synthetic tool call or assistant message. */
function applyChatCompaction(thread, frame, now) {
const current = thread ?? newChatThread(str(frame?.session_id));
if (current.sessionId && frame.session_id !== current.sessionId) return current;
const incoming = frame.compaction;
if (!incoming || !["running", "completed", "failed"].includes(incoming.status)) return current;
if (incoming.status === "running") {
return setThreadRunning({ ...current, compaction: { id: str(incoming.id), status: "running" } }, true, now);
}
if (!current.compaction || (incoming.id && current.compaction.id && incoming.id !== current.compaction.id)) return current;
// A compacted context can immediately continue the same model turn. Only a
// real final step/interrupt/disconnect settles the turn and its Stop control.
return { ...current, compaction: null };
}
/**
* Folds a user turn another client sent into the transcript, in place, once. The
* server never echoes this client's own prompts, so the sender's own message is
@@ -1740,8 +1762,10 @@ function upsertParts(thread, frame, now, deltaArrivedAt, upsert) {
* dropped too (the reconnect re-fetches `chat_shells`), exactly like the Android
* `streamResumed` path.
*/
export function historyResync(thread, page) {
const settled = resumedThread(thread ?? newChatThread());
export function historyResync(thread, page, { preserveActivity = false } = {}) {
// Reconnect callers clear stale activity BEFORE reattaching. Anything learned
// after that reset is fresh native state and must survive the history read.
const settled = preserveActivity ? (thread ?? newChatThread()) : resumedThread(thread ?? newChatThread());
return {
...settled,
messages: (page?.messages ?? []).slice(),
@@ -1951,7 +1975,7 @@ export function composerSubmit(thread, selected = 0) {
* (mirrors ChatStore.event). A null frame leaves the flag untouched.
*/
export function queuedAfterFrame(queued, frame) {
if (frame == null) return queued === true;
if (frame == null || frame.kind === EVENT_COMPACTION) return queued === true;
return false;
}
@@ -1962,6 +1986,7 @@ export function queuedAfterFrame(queued, frame) {
* running bit, which the composer's Stop control reads.
*/
export function runningAfterFrame(running, frame) {
if (frame?.kind === EVENT_COMPACTION && frame.compaction?.status === "running") return true;
if (frame?.kind === "step" && frame?.step?.status === "ended" && !frame?.step?.intermediate) return false;
return running === true;
}
+17 -5
View File
@@ -158,6 +158,7 @@ import {
threadPhase,
threadToolElapsedMillis,
TURN_PHASE_WAITING,
TURN_PHASE_COMPACTING,
toolCallCount,
toolCallRow,
toolCallsAnnouncement,
@@ -1017,12 +1018,12 @@ function shellOutputFailed(sessionId, shellId, message) {
async function resyncOpenChat() {
const id = state.chatActiveId;
if (!id || state.connection.kind !== "connected") return;
await subscribeChatSession(id);
const previous = state.chatThreads.get(id) ?? newChatThread(id);
state.chatThreads.set(id, resumedThread(previous));
await subscribeChatSession(id);
try {
const page = await repo.chatHistory(id);
state.chatThreads.set(id, historyResync(previous, page));
state.chatThreads.set(id, historyResync(state.chatThreads.get(id), page, { preserveActivity: true }));
} catch {
// The history refresh failed; the settled thread stands and the badge is honest.
}
@@ -3400,6 +3401,8 @@ function renderChatActivity(turn, streaming = false, elapsedMillis = () => 0, th
class: `chat-reasoning chat-activity${streaming ? " chat-reasoning-live" : ""}`,
});
const head = h("div", { class: "chat-activity-head" });
const compacting = streaming && threadPhase(thread, Date.now()) === TURN_PHASE_COMPACTING;
if (compacting) head.classList.add("chat-activity-compacting");
if (streaming) {
const spinner = h("span", {
class: "spinner chat-activity-spinner",
@@ -3409,9 +3412,18 @@ function renderChatActivity(turn, streaming = false, elapsedMillis = () => 0, th
spinner.style.animationDelay = `-${(performance.now() % 800) / 1000}s`;
head.append(spinner);
}
if (compacting) {
head.append(h("span", {
class: "chat-activity-phase chat-compaction-status",
role: "status",
"aria-live": "polite",
text: turnPhaseLabel(TURN_PHASE_COMPACTING),
}));
if (steps.length || parts.length) head.append(h("span", { class: "chat-compaction-break", "aria-hidden": "true" }));
}
if (steps.length > 0) {
head.append(renderChatReasoningToggle(turn, streaming, steps));
} else if (streaming) {
head.append(renderChatReasoningToggle(turn, streaming && !compacting, steps));
} else if (streaming && !compacting) {
head.append(
h("span", {
class: "chat-activity-phase",
@@ -3420,7 +3432,7 @@ function renderChatActivity(turn, streaming = false, elapsedMillis = () => 0, th
);
}
if (parts.length > 0) {
if (head.childElementCount > 0) head.append(h("span", { class: "chat-activity-sep", text: "\u00b7" }));
if (steps.length > 0 || (streaming && !compacting)) head.append(h("span", { class: "chat-activity-sep", text: "\u00b7" }));
head.append(renderChatToolChip(turn, { streaming, elapsedMillis }));
}
const elapsed = streaming