feat(web): show native context compaction activity in chat
This commit is contained in:
+28
-3
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user