diff --git a/CHANGELOG.md b/CHANGELOG.md
index c047ce623..686f87cd3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f
- On a phone, the first tap on an option, **Submit** or **Cancel** in a question panel now acts on it. It used to only close the keyboard or resize the panel, which moved the target out from under the tap. The message box's keyboard now closes as soon as the question appears (the message box is hidden then anyway), the panel no longer resizes when an option or button takes focus, and while you type in **Other** a touch or pen tap elsewhere in the panel no longer moves focus first. Choosing an option then closes the keyboard; tapping back into the kept **Other** text chooses it again. **Submit** closes the keyboard at once instead of after the server confirms the answer, and collapsing the keyboard while typing in **Other** no longer makes the next tap raise it again for a moment. A mouse, including one on a touchscreen laptop, keeps its usual behavior.
- On a phone, opening the keyboard at the end of a conversation no longer hides the newest messages under it: the chat stays scrolled to the bottom. The same holds when the message box or a question panel grows. A conversation you scrolled up in stays where it is.
+- When the agent finishes a reply, the chat no longer jumps up to the start of the reply and back down. The finished reply used to disappear for a moment until its saved copy was loaded; it now stays on screen and the saved copy replaces it in place.
---
diff --git a/components/ChatWindow.tsx b/components/ChatWindow.tsx
index e31cbd10c..d8f07f0a1 100644
--- a/components/ChatWindow.tsx
+++ b/components/ChatWindow.tsx
@@ -526,6 +526,9 @@ export function ChatWindow({ session, newSessionCwd, newSessionWorkspace, toolCa
});
useEffect(() => {
if (!autoplayPendingRef.current) return;
+ // A held reply is about to become its saved row: speak that one, so the
+ // row's own speaker button shows it playing and can stop it.
+ if (!streamState.isStreaming && streamState.streamingMessage) return;
autoplayPendingRef.current = false;
const speech = assistantSpeech(messages, entryIds, streamState.streamingMessage);
if (speech) ttsRef.current.speak(speech.id, speech.text);
@@ -1309,7 +1312,9 @@ export function ChatWindow({ session, newSessionCwd, newSessionWorkspace, toolCa
sentinelRef={sentinelRef}
handleLoadMoreClick={handleLoadMoreClick}
/>
- {streamState.isStreaming && streamState.streamingMessage && (
+ {/* Also while held: a finished reply stays until its saved row
+ replaces it in the same render (useAgentSession-stream). */}
+ {streamState.streamingMessage && (
)}
diff --git a/hooks/useAgentSession-stream.ts b/hooks/useAgentSession-stream.ts
index c34c4ee7d..8ace5fb94 100644
--- a/hooks/useAgentSession-stream.ts
+++ b/hooks/useAgentSession-stream.ts
@@ -42,6 +42,14 @@ export interface StreamingState {
export type StreamAction =
| { type: "start" }
| { type: "update"; message: Partial }
+ /** A finished reply stays on screen until the saved transcript shows it:
+ * dropping it first empties that space for a moment, so the chat jumps up
+ * and back down. `release`, a new stream (`start`, `update`) or `reset`
+ * takes it away; `end` and `idle` leave it. */
+ | { type: "hold"; message: Partial }
+ | { type: "release"; committed: readonly AgentMessage[] }
+ /** No message is streaming now (a message ended, a live snapshot has none). */
+ | { type: "idle"; streaming: boolean }
| { type: "end" }
| { type: "reset" };
@@ -51,7 +59,22 @@ export function streamReducer(state: StreamingState, action: StreamAction): Stre
return { isStreaming: true, streamingMessage: null };
case "update":
return { isStreaming: true, streamingMessage: action.message };
- case "end":
+ case "hold":
+ return { isStreaming: false, streamingMessage: action.message };
+ case "release": {
+ const held = state.isStreaming ? null : state.streamingMessage;
+ if (!held) return state;
+ // No timestamp to match: drop it now, as before holding existed.
+ const saved = held.timestamp === undefined
+ || action.committed.some((message) => message.role === "assistant" && message.timestamp === held.timestamp);
+ return saved ? { isStreaming: false, streamingMessage: null } : state;
+ }
+ case "idle":
+ case "end": {
+ const held = !state.isStreaming && state.streamingMessage;
+ if (held) return state;
+ return { isStreaming: action.type === "idle" && action.streaming, streamingMessage: null };
+ }
case "reset":
return { isStreaming: false, streamingMessage: null };
default:
diff --git a/hooks/useAgentSession.rpc.test.mjs b/hooks/useAgentSession.rpc.test.mjs
index ae75bd9bb..ee7e74518 100644
--- a/hooks/useAgentSession.rpc.test.mjs
+++ b/hooks/useAgentSession.rpc.test.mjs
@@ -1050,6 +1050,41 @@ test("full run over fake SSE: optimistic bubble, coalesced streaming, terminal r
);
});
+test("a finished reply stays on screen until its saved copy replaces it", async () => {
+ resetWorld();
+ primeSession("s1", [userMsg("u0", "loaded question")]);
+ const { w, es } = await startRun("s1", "hello agent");
+ world.agents.set("s1", { running: true, state: { isStreaming: true } });
+ const reply = { ...assistantMsg("a1", "hello world"), timestamp: 42 };
+ await act(async () => { es.emit({ type: "agent_start" }); await Promise.resolve(); });
+ await act(async () => { es.emit({ type: "message_update", message: reply }); await Promise.resolve(); });
+ await settle(90);
+ const shown = () => w.latest.streamState.streamingMessage?.content?.[0]?.text === "hello world";
+ const saved = () => w.latest.messages.filter((m) => m.timestamp === 42).length;
+ // The first read after message_end races omp's write: the reply is not
+ // saved yet, and the server's live state has no message any more.
+ saveSession("s1", [userMsg("u0", "loaded question"), userMsg("u1", "hello agent")]);
+ const releaseSync = holdNextSync("s1", syncSnapshot("s1", liveSnapshot("s1", 99, null)));
+ await act(async () => { es.emit({ type: "message_end", message: reply }); await Promise.resolve(); });
+ // Dropping the reply before its saved copy arrives empties that space: the
+ // chat jumps up to where the reply started, then back down.
+ assert.ok(shown(), "the reply stays while the save is read");
+ await act(async () => { releaseSync(); });
+ await settle();
+ assert.ok(shown(), "a read without the saved reply keeps it on screen");
+ assert.equal(saved(), 0);
+ world.agents.set("s1", { running: false, state: { isStreaming: false } });
+ await act(async () => { es.emit({ type: "agent_end", isTerminal: true }); });
+ await settle();
+ assert.ok(shown(), "the end of the run keeps it too");
+ // The save lands; the next read hands the reply over to its saved copy.
+ saveSession("s1", [userMsg("u0", "loaded question"), userMsg("u1", "hello agent"), reply]);
+ await act(async () => { es.emit({ type: "message_end", message: userMsg("u2", "unrelated") }); });
+ await settle();
+ assert.equal(saved(), 1, "the saved copy is shown once");
+ assert.equal(w.latest.streamState.streamingMessage, null, "the held reply gives way to its saved copy");
+});
+
test("provider error on the assistant message is shown instead of ending silently", async () => {
resetWorld();
primeSession("s1", [userMsg("u0", "q")]);
diff --git a/hooks/useAgentSession.ts b/hooks/useAgentSession.ts
index 2575f1ea8..21d3c5c14 100644
--- a/hooks/useAgentSession.ts
+++ b/hooks/useAgentSession.ts
@@ -474,6 +474,8 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
setData((current) => current ? { ...current, context } : current);
if (metadata) syncActionsRef.current?.metadata(context, metadata.version, metadata.hasLive);
if (!agentRunningRef.current) setTodoPhases(context.todoPhases ?? []);
+ // Hand a held reply over to its saved copy in the same render.
+ dispatch({ type: "release", committed: context.messages });
setActiveLeafId(leafId);
const previous = runPreviousEntryIdsRef.current && new Set(runPreviousEntryIdsRef.current);
if (previous && context.messages.some((message, i) => message.role === "user" && !previous.has(context.entryIds[i]))) {
@@ -497,8 +499,10 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
* inline chat error banner (MessageView) instead of toast-only. No-op when
* nothing is streaming. */
const surfaceQuotaOnStream = useCallback((errorMessage: string) => {
- const current = streamStateRef.current.streamingMessage;
- if (!current || current.role !== "assistant") return;
+ const { isStreaming, streamingMessage: current } = streamStateRef.current;
+ // A held (finished) reply is no live bubble: stamping it would start a
+ // stream again and keep it on screen beside its saved copy.
+ if (!isStreaming || !current || current.role !== "assistant") return;
if (typeof current.errorMessage === "string" && current.errorMessage.trim()) return;
dispatch({
type: "update",
@@ -2263,7 +2267,15 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
}, 1000);
}
}
- dispatch({ type: "reset" });
+ // A finished reply stays until the catch-up above commits its saved
+ // copy (history callback). Tool-call turns still clear at once: their
+ // rows and status line come from the live tool state instead. A
+ // past-branch view never receives the saved copy, so it never holds.
+ const finishedReply = completed?.role === "assistant"
+ && !catchUp.view().leafId
+ && hasVisibleAssistantContent(completed)
+ && !(Array.isArray(completed.content) && completed.content.some((block) => block?.type === "toolCall"));
+ dispatch(finishedReply ? { type: "hold", message: normalizeToolCalls(completed) } : { type: "idle", streaming: false });
setAgentPhase({ kind: "waiting_model" });
break;
}
@@ -2569,7 +2581,9 @@ export function useAgentSession(opts: UseAgentSessionOptions) {
if (fields.message) {
dispatch(snapshot.streamingMessage
? { type: "update", message: normalizeToolCalls(snapshot.streamingMessage as AgentMessage) }
- : snapshot.isStreaming ? { type: "start" } : { type: "reset" });
+ // Between message_end and its saved copy the server has no message:
+ // a held reply stays.
+ : { type: "idle", streaming: snapshot.isStreaming });
if (!promptDispatchPendingRef.current && hasVisibleAssistantContent(snapshot.streamingMessage)) runHadContentRef.current = true;
const messageError = readAgentError(snapshot.streamingMessage);
if (messageError) lastRunErrorRef.current = messageError;