web: steady chat composer and running indicator
Keep caret focus, value and selection across live-frame re-renders, and show a single in-place running-tool line so tool calls no longer grow and shrink the transcript for every call. Right-align the pipeline-list miniature.
This commit is contained in:
+77
-11
@@ -331,7 +331,45 @@ function transcriptDistance(el) {
|
||||
* re-render replaces it, so `afterChatRender` can put the operator back exactly
|
||||
* where they were. A no-op when no transcript is mounted.
|
||||
*/
|
||||
let pendingComposerFocus = null;
|
||||
|
||||
/**
|
||||
* Remember the composer's focus and selection before a re-render replaces the
|
||||
* textarea. Live frames arrive continuously, so without this every tool/text
|
||||
* frame would steal focus and the operator could not type a queued message.
|
||||
*/
|
||||
function captureChatFocus() {
|
||||
const input = document.getElementById("chatInput");
|
||||
if (!input || document.activeElement !== input) {
|
||||
pendingComposerFocus = null;
|
||||
return;
|
||||
}
|
||||
pendingComposerFocus = {
|
||||
sessionId: state.chatActiveId ?? "",
|
||||
start: input.selectionStart,
|
||||
end: input.selectionEnd,
|
||||
direction: input.selectionDirection,
|
||||
};
|
||||
}
|
||||
|
||||
/** Put the caret back after the transcript is rebuilt, without scrolling. */
|
||||
function restoreChatFocus() {
|
||||
const pending = pendingComposerFocus;
|
||||
pendingComposerFocus = null;
|
||||
if (!pending) return;
|
||||
if (state.chatActiveId !== pending.sessionId) return;
|
||||
const input = document.getElementById("chatInput");
|
||||
if (!input) return;
|
||||
input.focus({ preventScroll: true });
|
||||
try {
|
||||
input.setSelectionRange(pending.start, pending.end, pending.direction);
|
||||
} catch {
|
||||
// A non-text input or an unsupported selection range is harmless.
|
||||
}
|
||||
}
|
||||
|
||||
function captureChatPosition() {
|
||||
captureChatFocus();
|
||||
const el = document.getElementById("chatTranscript");
|
||||
if (!el || !el.dataset.session) return;
|
||||
state.chatPending = {
|
||||
@@ -2570,17 +2608,38 @@ function renderChatRunningIndicator(thread, now) {
|
||||
}),
|
||||
);
|
||||
wrap.append(head);
|
||||
for (const part of threadRunningTools(thread.messages)) {
|
||||
wrap.append(renderChatRunningTool(thread, part, now));
|
||||
// One stable detail line for the whole turn. Rendering one line per in-flight
|
||||
// tool made the transcript grow and shrink for every call, which bounced the
|
||||
// text (and the composer) while a turn streamed. The line updates in place to
|
||||
// the longest-running call; the tool chip in the turn still lists every call.
|
||||
const running = threadRunningTools(thread.messages);
|
||||
const chosen =
|
||||
running.find((part) => toolLongRunning(threadToolElapsedMillis(thread, part, now))) ??
|
||||
running[0] ??
|
||||
null;
|
||||
if (chosen) {
|
||||
wrap.append(renderChatRunningTool(thread, chosen, now));
|
||||
} else {
|
||||
wrap.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "chat-running-tool" },
|
||||
h("span", { class: "chat-running-tool-text", text: "Preparing the next step…" }),
|
||||
),
|
||||
);
|
||||
}
|
||||
return wrap;
|
||||
}
|
||||
|
||||
function renderChatRunningTool(thread, part, now) {
|
||||
const line = h("div", { class: "chat-running-tool", "data-part-id": String(part?.id ?? "") });
|
||||
function runningToolText(part) {
|
||||
const name = toolLabel(part);
|
||||
const detail = toolDetail(part);
|
||||
line.append(h("span", { class: "chat-running-tool-text", text: detail == null ? name : `${name} ${detail}` }));
|
||||
return detail == null ? name : `${name} ${detail}`;
|
||||
}
|
||||
|
||||
function renderChatRunningTool(thread, part, now) {
|
||||
const line = h("div", { class: "chat-running-tool", "data-part-id": String(part?.id ?? "") });
|
||||
line.append(h("span", { class: "chat-running-tool-text", text: runningToolText(part) }));
|
||||
line.append(
|
||||
h("span", {
|
||||
class: "chat-running-tool-elapsed muted",
|
||||
@@ -3418,6 +3477,7 @@ function afterChatRender() {
|
||||
});
|
||||
|
||||
updateChatJump(el);
|
||||
restoreChatFocus();
|
||||
}
|
||||
|
||||
/** Offer "Jump to latest" only when not following and something is below the fold. */
|
||||
@@ -3472,14 +3532,20 @@ function updateChatIndicator(now) {
|
||||
if (label) label.textContent = turnPhaseLabel(threadPhase(thread, now) ?? TURN_PHASE_WAITING);
|
||||
const elapsed = el.querySelector(".chat-running-elapsed");
|
||||
if (elapsed) elapsed.textContent = `\u00b7 ${formatDuration(Math.trunc(threadElapsedMillis(thread, now) / 1000))}`;
|
||||
for (const part of threadRunningTools(thread.messages)) {
|
||||
const line = el.querySelector(`.chat-running-tool[data-part-id="${String(part?.id ?? "")}"]`);
|
||||
if (!line) continue;
|
||||
const age = threadToolElapsedMillis(thread, part, now);
|
||||
const running = threadRunningTools(thread.messages);
|
||||
const chosen =
|
||||
running.find((part) => toolLongRunning(threadToolElapsedMillis(thread, part, now))) ??
|
||||
running[0] ??
|
||||
null;
|
||||
const line = el.querySelector(".chat-running-tool");
|
||||
if (line) {
|
||||
const text = line.querySelector(".chat-running-tool-text");
|
||||
if (text && chosen) text.textContent = runningToolText(chosen);
|
||||
const age = chosen ? threadToolElapsedMillis(thread, chosen, now) : 0;
|
||||
const toolElapsed = line.querySelector(".chat-running-tool-elapsed");
|
||||
if (toolElapsed) toolElapsed.textContent = formatToolDuration(age);
|
||||
if (toolElapsed) toolElapsed.textContent = chosen ? formatToolDuration(age) : "";
|
||||
const mark = line.querySelector(".chat-tool-long");
|
||||
if (toolLongRunning(age) && !mark) {
|
||||
if (chosen && toolLongRunning(age) && !mark) {
|
||||
line.classList.add("chat-running-tool-long");
|
||||
line.append(h("span", { class: "chat-tool-long", text: "\u00b7 long" }));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user