Merge branch 'main' into web-template-create

This commit is contained in:
2026-10-19 06:16:16 +01:00
6 changed files with 1200 additions and 17 deletions
+272 -10
View File
@@ -238,6 +238,7 @@ import {
groupSessionsByDirectory,
mergeHistoryPage,
reasoningLabel,
runningToolElapsedMillis,
sessionActivity,
sessionActivityBadge,
sessionChipLabel,
@@ -245,8 +246,16 @@ import {
sessionStatusLabel,
sessionTitle,
sessionWorkLabel,
toolCallCount,
toolCallRow,
toolCallsAnnouncement,
toolCallsLabel,
toolFailed,
toolLongRunning,
toolRunning,
toolsRunning,
transcriptRows,
turnElapsedMillis,
} from "./chat.js";
import { codeBlockLabel, parseInline, parseMarkdown } from "./markdown.js";
@@ -393,6 +402,11 @@ const state = {
chatPlaced: null,
chatFollowing: true,
chatReasoning: new Set(),
// The tool-call panel (C6): the turns whose panel is open and the individual
// rows the operator has expanded, both keyed the same way as chatReasoning so
// a re-render keeps them.
chatToolPanels: new Set(),
chatToolRows: new Set(),
chatScroll: readChatScrollStore(),
// The scroll restore across a prepended older page, the position captured
// from the live transcript just before a re-render (so a dialog, a refresh or
@@ -3476,6 +3490,8 @@ async function openChatSession(id) {
state.chatPlaced = null;
state.chatFollowing = true;
state.chatReasoning = new Set();
state.chatToolPanels = new Set();
state.chatToolRows = new Set();
const existing = state.chatThreads.get(id);
state.chatThreads.set(id, {
messages: existing?.messages ?? [],
@@ -3907,15 +3923,14 @@ function renderChatUserMessage(message) {
}
/** One folded agent reply, left-aligned: reasoning, tool-call chip, answer. */
function renderChatAgentTurn(turn) {
function renderChatAgentTurn(turn, streaming = false) {
const wrap = h("div", { class: "chat-turn chat-turn-agent" });
const bubble = h("div", { class: "chat-bubble agent-bubble" });
const steps = turn.reasoningSteps;
if (steps.length > 0) bubble.append(renderChatReasoning(turn));
if (steps.length > 0) bubble.append(renderChatReasoning(turn, streaming));
if (turn.tools.length > 0) {
bubble.append(
h("div", { class: "chat-tools" }, h("span", { class: "chip chat-tool-chip", text: toolCallsLabel(turn.tools.length) })),
);
const tools = renderChatToolCalls(turn, { streaming });
if (tools) bubble.append(tools);
}
if (turn.answer.trim() !== "") bubble.append(renderMarkdown(turn.answer));
wrap.append(bubble);
@@ -3925,9 +3940,13 @@ function renderChatAgentTurn(turn) {
/**
* The turn's one collapsible reasoning block. The open/closed state lives on the
* conversation keyed by the turn, so it survives a re-render; toggling it never
* rebuilds the transcript (and so never moves the operator's scroll).
* rebuilds the transcript (and so never moves the operator's scroll). The label is
* the app's own (see `reasoningLabel`) with the turn's server-reported duration
* appended when it has one; a settled history turn reads `Reasoning` (or
* `Reasoning · N steps`), a live one `Thinking…`/`Thinking · N steps`. Live
* elapsed time is C5's; this renders the parts' own timing.
*/
function renderChatReasoning(turn) {
function renderChatReasoning(turn, streaming = false) {
const expanded = state.chatReasoning.has(turn.key);
const block = h("div", { class: "chat-reasoning" });
const toggle = h("button", {
@@ -3936,16 +3955,31 @@ function renderChatReasoning(turn) {
"aria-expanded": String(expanded),
});
toggle.append(
h("span", { class: "chat-reasoning-icon", "aria-hidden": "true", text: expanded ? "\u25be" : "\u25b8" }),
h("span", {
class: "chat-reasoning-icon",
role: "img",
"aria-label": expanded ? "Hide reasoning" : "Show reasoning",
text: expanded ? "\u25be" : "\u25b8",
}),
);
const label = reasoningLabel(turn.reasoningSteps.length, streaming);
const elapsedMillis = turnElapsedMillis(turn.parts);
const duration = elapsedMillis == null ? null : formatDuration(Math.trunc(elapsedMillis / 1000));
toggle.append(
h("span", {
class: "chat-reasoning-label",
text: duration == null ? label : `${label} · ${duration}`,
}),
);
toggle.append(h("span", { class: "chat-reasoning-label", text: reasoningLabel(turn.reasoningSteps.length) }));
const body = h("div", { class: "chat-reasoning-body", text: turn.reasoning });
body.hidden = !expanded;
toggle.addEventListener("click", () => {
const next = body.hidden;
body.hidden = !next;
toggle.setAttribute("aria-expanded", String(next));
toggle.querySelector(".chat-reasoning-icon").textContent = next ? "\u25be" : "\u25b8";
const icon = toggle.querySelector(".chat-reasoning-icon");
icon.textContent = next ? "\u25be" : "\u25b8";
icon.setAttribute("aria-label", next ? "Hide reasoning" : "Show reasoning");
if (next) state.chatReasoning.add(turn.key);
else state.chatReasoning.delete(turn.key);
});
@@ -3953,6 +3987,234 @@ function renderChatReasoning(turn) {
return block;
}
/**
* One turn's tool calls behind a single summary chip (C6), the browser's port of
* `ui/chat/ChatToolCalls.kt`: `N tools called`, a spinner while any call is in
* flight, a `· long` mark (error tint) once a running call has passed
* [TOOL_LONG_RUNNING_MILLIS], error styling once a call failed, and a chevron
* naming the panel it opens. The panel is a fixed overlay anchored below the chip
* (flipped above when there is no room), with a header count, a close control, an
* internally scrolling list, and per-row expand state; because it never joins the
* transcript's flow, opening or closing it cannot re-flow the conversation nor
* move its scroll position (C14).
*
* [streaming] is whether the turn is still live, and [elapsedMillis] the live age
* of a running call (the conversation supplies it from its own clock, so the
* `long` mark is derived from real time, never guessed). A settled history turn
* passes neither, so a stored `running` status cannot spin or wear the mark.
*/
function renderChatToolCalls(turn, { streaming = false, elapsedMillis = () => 0 } = {}) {
const parts = turn.tools;
if (parts.length === 0) return null;
const count = toolCallCount(turn.parts);
const running = streaming && toolsRunning(turn.parts);
const failed = parts.some((part) => toolFailed(part.state));
const long = running && parts.some((part) => toolRunning(part) && toolLongRunning(elapsedMillis(part)));
const turnKey = turn.key;
const wrap = h("div", { class: "chat-tools" });
const open = state.chatToolPanels.has(turnKey);
const chip = h("button", {
class: `chip chat-tool-chip${failed ? " chat-tool-chip-failed" : ""}`,
type: "button",
"aria-label": toolCallsAnnouncement(count),
"aria-expanded": String(open),
});
if (running) {
chip.append(h("span", { class: "spinner chat-tool-spinner", "aria-hidden": "true" }));
} else {
chip.append(
h("span", {
class: `chat-tool-icon${failed ? " chat-tool-icon-failed" : ""}`,
"aria-hidden": "true",
text: failed ? "\u26a0" : "\u29c9",
}),
);
}
chip.append(h("span", { class: "chat-tool-label", text: toolCallsLabel(count) }));
if (long) chip.append(h("span", { class: "chat-tool-long", text: "\u00b7 long" }));
const chevron = h("span", {
class: "chat-tool-chevron",
role: "img",
"aria-label": open ? "Hide tool calls" : "Show tool calls",
text: open ? "\u25b4" : "\u25be",
});
chip.append(chevron);
wrap.append(chip);
const panel = h("div", { class: "chat-tool-panel", role: "dialog", "aria-label": toolCallsLabel(count) });
panel.hidden = !open;
const head = h("div", { class: "chat-tool-panel-head" });
head.append(h("span", { class: "chat-tool-panel-count", text: toolCallsLabel(count) }));
const close = h("button", {
class: "ghost chat-tool-close",
type: "button",
"aria-label": "Close tool calls",
title: "Close tool calls",
text: "\u00d7",
});
head.append(close);
panel.append(head);
const list = h("div", { class: "chat-tool-list" });
for (const part of parts) {
const id = String(part?.id ?? "");
const holder = h("div", { class: "chat-tool-row-holder" });
const build = () =>
renderToolCallRow(part, {
expanded: state.chatToolRows.has(id),
onToggle: () => {
if (state.chatToolRows.has(id)) state.chatToolRows.delete(id);
else state.chatToolRows.add(id);
holder.replaceChild(build(), holder.firstChild);
},
});
holder.append(build());
list.append(holder);
}
panel.append(list);
wrap.append(panel);
const setOpen = (next) => {
if (next) state.chatToolPanels.add(turnKey);
else state.chatToolPanels.delete(turnKey);
panel.hidden = !next;
chip.setAttribute("aria-expanded", String(next));
chevron.setAttribute("aria-label", next ? "Hide tool calls" : "Show tool calls");
chevron.textContent = next ? "\u25b4" : "\u25be";
if (next) positionChatToolPanel(panel, chip);
};
chip.addEventListener("click", (event) => {
event.stopPropagation();
setOpen(panel.hidden);
});
close.addEventListener("click", () => setOpen(false));
if (open) {
// The panel is built before its turn is attached to the document, so its
// rect is not measurable yet; place it on the next frame, when it is.
const place = () => positionChatToolPanel(panel, chip);
if (typeof requestAnimationFrame === "function") requestAnimationFrame(place);
else setTimeout(place, 0);
}
return wrap;
}
/**
* Places the tool panel below its chip, or above it when there is no room, keeping
* it inside the viewport margins — the browser's port of
* `ToolPanelPositionProvider`. Resolved in px from the live rects, so it is
* independent of any layout guess.
*/
function positionChatToolPanel(panel, chip) {
const gap = 4;
const margin = 8;
const chipRect = chip.getBoundingClientRect();
const rect = panel.getBoundingClientRect();
let top = chipRect.bottom + gap;
if (top + rect.height > window.innerHeight - margin) top = chipRect.top - rect.height - gap;
top = Math.max(margin, Math.min(top, window.innerHeight - rect.height - margin));
const left = Math.max(margin, Math.min(chipRect.left, window.innerWidth - rect.width - margin));
panel.style.top = `${Math.round(top)}px`;
panel.style.left = `${Math.round(left)}px`;
}
/**
* One call inside the panel: a header naming the call, its `+N -M` stat for an
* edit and its duration, and — expanded in place — the pretty-printed INPUT, the
* synthesised edit diff, then the ERROR block for a failed call or the OUTPUT for
* a settled one. Rows expand and collapse independently. Exported so the smoke can
* render a representative row through this same builder.
*/
export function renderToolCallRow(part, { expanded = false, onToggle = null } = {}) {
const row = toolCallRow(part);
const node = h("div", { class: `chat-tool-row${row.error != null ? " chat-tool-row-failed" : ""}` });
const head = h("button", {
class: "chat-tool-row-head",
type: "button",
"aria-expanded": String(expanded),
});
head.append(
h("span", {
class: `chat-tool-icon${row.error != null ? " chat-tool-icon-failed" : ""}`,
role: "img",
"aria-label": row.error != null ? "Failed tool call" : "",
"aria-hidden": row.error != null ? null : "true",
text: row.error != null ? "\u26a0" : row.diff != null ? "\u270e" : "\u29c9",
}),
);
const titles = h("span", { class: "chat-tool-row-titles" });
titles.append(h("span", { class: "chat-tool-name", text: row.name }));
if (row.detail != null) titles.append(h("span", { class: "chat-tool-detail", text: row.detail }));
head.append(titles);
if (row.diff != null) {
head.append(h("span", { class: "chat-tool-stat", text: changeStat(row.diff.added, row.diff.removed) }));
}
if (row.duration != null) head.append(h("span", { class: "chat-tool-duration", text: row.duration }));
head.append(
h("span", {
class: "chat-tool-row-chevron",
role: "img",
"aria-label": expanded ? `Collapse ${row.name}` : `Expand ${row.name}`,
text: expanded ? "\u25b4" : "\u25be",
}),
);
node.append(head);
if (expanded) {
const body = h("div", { class: "chat-tool-body" });
const input = toolSection("INPUT", row.input);
if (input) body.append(input);
if (row.diff != null) body.append(renderToolDiff(row.diff));
if (row.error != null) {
const error = toolSection("ERROR", row.error);
if (error) body.append(error);
} else {
const output = toolSection("OUTPUT", row.output);
if (output) body.append(output);
}
node.append(body);
}
if (typeof onToggle === "function") {
head.addEventListener("click", () => onToggle());
}
return node;
}
/** One monospace section card (INPUT/OUTPUT/ERROR); blank content renders none. */
function toolSection(label, text) {
if (text == null || String(text).trim() === "") return null;
const section = h("div", { class: "chat-tool-section" });
section.append(h("div", { class: "chat-tool-section-label", text: label }));
section.append(h("pre", { class: "chat-tool-section-body", text }));
return section;
}
/**
* The unified diff body of an edit call: line-by-line monospace with `+`/`-`
* gutter symbols and the app's success/failure hues, the shared line language of
* the run Changes viewer. A diff longer than the rendered cap is summarised with
* an omitted note. Exported so the smoke can render a representative sample.
*/
export function renderToolDiff(diff) {
const card = h("div", { class: "chat-tool-diff" });
if (diff.omitted > 0) {
card.append(
h("div", {
class: "chat-tool-diff-note",
text: `\u2026 ${diff.omitted} more changed ${diff.omitted === 1 ? "line" : "lines"} not shown`,
}),
);
}
for (const line of diff.lines) {
card.append(
h("div", {
class: `chat-tool-diff-line ${diffLineClass(line.kind)}`,
text: `${diffLineSymbol(line.kind)} ${line.text}`,
}),
);
}
return card;
}
/**
* Render assistant markdown by building DOM nodes, never by assigning innerHTML.
* Exported so the live UI smoke can exercise the page's own renderer for the