Skip to content

Commit 6a684fd

Browse files
committed
perf(ui): window chat + lazy tool output
1 parent 55c8b81 commit 6a684fd

5 files changed

Lines changed: 148 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
- Web UI: add Logs tab for gateway file logs with filtering, auto-follow, and export.
5555
- Web UI: cap tool output + large markdown rendering to avoid UI freezes on huge tool results.
5656
- Web UI: keep config form edits synced to raw JSON so form saves persist.
57+
- Web UI: window chat history rendering and throttle tool stream updates to reduce UI churn.
58+
- Web UI: collapse tool output by default and lazy-render tool markdown on expand.
5759
- ClawdbotKit: fix SwiftPM resource bundling path for `tool-display.json`. Thanks @fcatuhe for PR #398.
5860
- Tools: add Telegram/WhatsApp reaction tools (with per-provider gating). Thanks @zats for PR #353.
5961
- Tools: flatten literal-union schemas for Claude on Vertex AI. Thanks @carlulsoe for PR #409.

‎ui/src/styles/components.css‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,34 @@
883883
color: var(--muted);
884884
}
885885

886+
.chat-tool-card__details {
887+
margin-top: 6px;
888+
}
889+
890+
.chat-tool-card__summary {
891+
font-family: var(--font-mono);
892+
font-size: 11px;
893+
color: var(--muted);
894+
cursor: pointer;
895+
list-style: none;
896+
display: inline-flex;
897+
align-items: center;
898+
gap: 6px;
899+
}
900+
901+
.chat-tool-card__summary::-webkit-details-marker {
902+
display: none;
903+
}
904+
905+
.chat-tool-card__summary-meta {
906+
color: var(--muted);
907+
opacity: 0.8;
908+
}
909+
910+
.chat-tool-card__details[open] .chat-tool-card__summary {
911+
color: var(--chat-text);
912+
}
913+
886914
.chat-tool-card__output {
887915
margin-top: 6px;
888916
font-family: var(--font-mono);

‎ui/src/ui/app-render.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ export function renderApp(state: AppViewState) {
440440
disabledReason: chatDisabledReason,
441441
error: state.lastError,
442442
sessions: state.sessionsResult,
443+
isToolOutputExpanded: (id) => state.toolOutputExpanded.has(id),
444+
onToolOutputToggle: (id, expanded) =>
445+
state.toggleToolOutput(id, expanded),
443446
onRefresh: () => {
444447
state.resetToolStream();
445448
return loadChatHistory(state);

‎ui/src/ui/app.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type EventLogEntry = {
8989
};
9090

9191
const TOOL_STREAM_LIMIT = 50;
92+
const TOOL_STREAM_THROTTLE_MS = 80;
9293
const TOOL_OUTPUT_CHAR_LIMIT = 120_000;
9394
const DEFAULT_LOG_LEVEL_FILTERS: Record<LogLevel, boolean> = {
9495
trace: true,
@@ -200,6 +201,7 @@ export class ClawdbotApp extends LitElement {
200201
@state() lastError: string | null = null;
201202
@state() eventLog: EventLogEntry[] = [];
202203
private eventLogBuffer: EventLogEntry[] = [];
204+
private toolStreamSyncTimer: number | null = null;
203205

204206
@state() sessionKey = this.settings.sessionKey;
205207
@state() chatLoading = false;
@@ -211,6 +213,7 @@ export class ClawdbotApp extends LitElement {
211213
@state() chatStreamStartedAt: number | null = null;
212214
@state() chatRunId: string | null = null;
213215
@state() chatThinkingLevel: string | null = null;
216+
@state() toolOutputExpanded = new Set<string>();
214217

215218
@state() nodesLoading = false;
216219
@state() nodes: Array<Record<string, unknown>> = [];
@@ -608,12 +611,24 @@ export class ClawdbotApp extends LitElement {
608611
this.toolStreamById.clear();
609612
this.toolStreamOrder = [];
610613
this.chatToolMessages = [];
614+
this.toolOutputExpanded = new Set();
615+
this.flushToolStreamSync();
611616
}
612617

613618
resetChatScroll() {
614619
this.chatHasAutoScrolled = false;
615620
}
616621

622+
toggleToolOutput(id: string, expanded: boolean) {
623+
const next = new Set(this.toolOutputExpanded);
624+
if (expanded) {
625+
next.add(id);
626+
} else {
627+
next.delete(id);
628+
}
629+
this.toolOutputExpanded = next;
630+
}
631+
617632
private trimToolStream() {
618633
if (this.toolStreamOrder.length <= TOOL_STREAM_LIMIT) return;
619634
const overflow = this.toolStreamOrder.length - TOOL_STREAM_LIMIT;
@@ -627,6 +642,26 @@ export class ClawdbotApp extends LitElement {
627642
.filter((msg): msg is Record<string, unknown> => Boolean(msg));
628643
}
629644

645+
private scheduleToolStreamSync(force = false) {
646+
if (force) {
647+
this.flushToolStreamSync();
648+
return;
649+
}
650+
if (this.toolStreamSyncTimer != null) return;
651+
this.toolStreamSyncTimer = window.setTimeout(
652+
() => this.flushToolStreamSync(),
653+
TOOL_STREAM_THROTTLE_MS,
654+
);
655+
}
656+
657+
private flushToolStreamSync() {
658+
if (this.toolStreamSyncTimer != null) {
659+
clearTimeout(this.toolStreamSyncTimer);
660+
this.toolStreamSyncTimer = null;
661+
}
662+
this.syncToolStreamMessages();
663+
}
664+
630665
private buildToolStreamMessage(entry: ToolStreamEntry): Record<string, unknown> {
631666
const content: Array<Record<string, unknown>> = [];
632667
content.push({
@@ -699,7 +734,7 @@ export class ClawdbotApp extends LitElement {
699734

700735
entry.message = this.buildToolStreamMessage(entry);
701736
this.trimToolStream();
702-
this.syncToolStreamMessages();
737+
this.scheduleToolStreamSync(phase === "result");
703738
}
704739

705740
private onEvent(evt: GatewayEventFrame) {

‎ui/src/ui/views/chat.ts‎

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export type ChatProps = {
2222
disabledReason: string | null;
2323
error: string | null;
2424
sessions: SessionsListResult | null;
25+
isToolOutputExpanded: (id: string) => boolean;
26+
onToolOutputToggle: (id: string, expanded: boolean) => void;
2527
onRefresh: () => void;
2628
onDraftChange: (next: string) => void;
2729
onSend: () => void;
@@ -88,10 +90,11 @@ export function renderChat(props: ChatProps) {
8890
content: [{ type: "text", text: item.text }],
8991
timestamp: item.startedAt,
9092
},
93+
props,
9194
{ streaming: true },
9295
);
9396
}
94-
return renderMessage(item.message);
97+
return renderMessage(item.message, props);
9598
})}
9699
</div>
97100
@@ -131,12 +134,30 @@ type ChatItem =
131134
| { kind: "stream"; key: string; text: string; startedAt: number }
132135
| { kind: "reading-indicator"; key: string };
133136

137+
const CHAT_HISTORY_RENDER_LIMIT = 200;
138+
134139
function buildChatItems(props: ChatProps): ChatItem[] {
135140
const items: ChatItem[] = [];
136141
const history = Array.isArray(props.messages) ? props.messages : [];
137142
const tools = Array.isArray(props.toolMessages) ? props.toolMessages : [];
138-
for (let i = 0; i < history.length; i++) {
139-
items.push({ kind: "message", key: messageKey(history[i], i), message: history[i] });
143+
const historyStart = Math.max(0, history.length - CHAT_HISTORY_RENDER_LIMIT);
144+
if (historyStart > 0) {
145+
items.push({
146+
kind: "message",
147+
key: "chat:history:notice",
148+
message: {
149+
role: "system",
150+
content: `Showing last ${CHAT_HISTORY_RENDER_LIMIT} messages (${historyStart} hidden).`,
151+
timestamp: Date.now(),
152+
},
153+
});
154+
}
155+
for (let i = historyStart; i < history.length; i++) {
156+
items.push({
157+
kind: "message",
158+
key: messageKey(history[i], i),
159+
message: history[i],
160+
});
140161
}
141162
for (let i = 0; i < tools.length; i++) {
142163
items.push({
@@ -260,7 +281,11 @@ function renderReadingIndicator() {
260281
`;
261282
}
262283

263-
function renderMessage(message: unknown, opts?: { streaming?: boolean }) {
284+
function renderMessage(
285+
message: unknown,
286+
props?: Pick<ChatProps, "isToolOutputExpanded" | "onToolOutputToggle">,
287+
opts?: { streaming?: boolean },
288+
) {
264289
const m = message as Record<string, unknown>;
265290
const role = typeof m.role === "string" ? m.role : "unknown";
266291
const toolCards = extractToolCards(message);
@@ -287,14 +312,28 @@ function renderMessage(message: unknown, opts?: { streaming?: boolean }) {
287312
typeof m.timestamp === "number" ? new Date(m.timestamp).toLocaleTimeString() : "";
288313
const klass = role === "assistant" ? "assistant" : role === "user" ? "user" : "other";
289314
const who = role === "assistant" ? "Assistant" : role === "user" ? "You" : role;
315+
const toolCallId = typeof m.toolCallId === "string" ? m.toolCallId : "";
316+
const toolCardBase =
317+
toolCallId ||
318+
(typeof m.id === "string" ? m.id : "") ||
319+
(typeof m.messageId === "string" ? m.messageId : "") ||
320+
(typeof m.timestamp === "number" ? String(m.timestamp) : "tool-card");
290321
return html`
291322
<div class="chat-line ${klass}">
292323
<div class="chat-msg">
293324
<div class="chat-bubble ${opts?.streaming ? "streaming" : ""}">
294325
${markdown
295326
? html`<div class="chat-text">${unsafeHTML(toSanitizedMarkdownHtml(markdown))}</div>`
296327
: nothing}
297-
${toolCards.map((card) => renderToolCard(card))}
328+
${toolCards.map((card, index) =>
329+
renderToolCard(card, {
330+
id: `${toolCardBase}:${index}`,
331+
expanded: props?.isToolOutputExpanded
332+
? props.isToolOutputExpanded(`${toolCardBase}:${index}`)
333+
: false,
334+
onToggle: props?.onToolOutputToggle,
335+
}),
336+
)}
298337
</div>
299338
<div class="chat-stamp mono">
300339
${who}${timestamp ? html` · ${timestamp}` : nothing}
@@ -368,19 +407,49 @@ function extractToolCards(message: unknown): ToolCard[] {
368407
return cards;
369408
}
370409

371-
function renderToolCard(card: ToolCard) {
410+
function renderToolCard(
411+
card: ToolCard,
412+
opts?: {
413+
id: string;
414+
expanded: boolean;
415+
onToggle?: (id: string, expanded: boolean) => void;
416+
},
417+
) {
372418
const display = resolveToolDisplay({ name: card.name, args: card.args });
373419
const detail = formatToolDetail(display);
420+
const hasOutput = typeof card.text === "string" && card.text.length > 0;
421+
const expanded = opts?.expanded ?? false;
422+
const id = opts?.id ?? `${card.name}-${Math.random()}`;
374423
return html`
375424
<div class="chat-tool-card">
376425
<div class="chat-tool-card__title">${display.emoji} ${display.label}</div>
377426
${detail
378427
? html`<div class="chat-tool-card__detail">${detail}</div>`
379428
: nothing}
380-
${card.text
381-
? html`<div class="chat-tool-card__output chat-text">
382-
${unsafeHTML(toSanitizedMarkdownHtml(card.text))}
383-
</div>`
429+
${hasOutput
430+
? html`
431+
<details
432+
class="chat-tool-card__details"
433+
?open=${expanded}
434+
@toggle=${(e: Event) => {
435+
if (!opts?.onToggle) return;
436+
const target = e.currentTarget as HTMLDetailsElement;
437+
opts.onToggle(id, target.open);
438+
}}
439+
>
440+
<summary class="chat-tool-card__summary">
441+
${expanded ? "Hide output" : "Show output"}
442+
<span class="chat-tool-card__summary-meta">
443+
(${card.text?.length ?? 0} chars)
444+
</span>
445+
</summary>
446+
${expanded
447+
? html`<div class="chat-tool-card__output chat-text">
448+
${unsafeHTML(toSanitizedMarkdownHtml(card.text ?? ""))}
449+
</div>`
450+
: nothing}
451+
</details>
452+
`
384453
: nothing}
385454
</div>
386455
`;

0 commit comments

Comments
 (0)