chore: update appcast and TUI streaming handling

This commit is contained in:
Peter Steinberger
2026-01-03 11:06:43 +01:00
parent 45c555a4bd
commit 1bebcf8033
3 changed files with 37 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ export class ChatLog extends Container {
private toolById = new Map<string, ToolExecutionComponent>();
private streamingAssistant: AssistantMessageComponent | null = null;
private streamingRunId: string | null = null;
private streamingText: string | null = null;
private toolsExpanded = false;
clearAll() {
@@ -15,6 +16,7 @@ export class ChatLog extends Container {
this.toolById.clear();
this.streamingAssistant = null;
this.streamingRunId = null;
this.streamingText = null;
}
addSystem(text: string) {
@@ -30,6 +32,7 @@ export class ChatLog extends Container {
const component = new AssistantMessageComponent(text);
this.streamingAssistant = component;
this.streamingRunId = runId ?? null;
this.streamingText = text;
this.addChild(component);
return component;
}
@@ -42,17 +45,23 @@ export class ChatLog extends Container {
this.startAssistant(text, runId);
return;
}
this.streamingText = text;
this.streamingAssistant.setText(text);
}
finalizeAssistant(text: string, runId?: string) {
if (this.streamingAssistant && (!runId || runId === this.streamingRunId)) {
if (
this.streamingAssistant &&
(!runId || runId === this.streamingRunId || text === this.streamingText)
) {
this.streamingText = text;
this.streamingAssistant.setText(text);
} else {
this.startAssistant(text, runId);
}
this.streamingAssistant = null;
this.streamingRunId = null;
this.streamingText = null;
}
startTool(toolCallId: string, toolName: string, args: unknown) {

View File

@@ -110,6 +110,7 @@ export async function runTui(opts: TuiOptions) {
let currentSessionKey = defaultSession;
let currentSessionId: string | null = null;
let activeChatRunId: string | null = null;
const finalizedRuns = new Map<string, number>();
let historyLoaded = false;
let isConnected = false;
let toolsExpanded = false;
@@ -296,10 +297,30 @@ export async function runTui(opts: TuiOptions) {
tui.requestRender();
};
const noteFinalizedRun = (runId: string) => {
finalizedRuns.set(runId, Date.now());
if (finalizedRuns.size <= 200) return;
const keepUntil = Date.now() - 10 * 60 * 1000;
for (const [key, ts] of finalizedRuns) {
if (finalizedRuns.size <= 150) break;
if (ts < keepUntil) finalizedRuns.delete(key);
}
if (finalizedRuns.size > 200) {
for (const key of finalizedRuns.keys()) {
finalizedRuns.delete(key);
if (finalizedRuns.size <= 150) break;
}
}
};
const handleChatEvent = (payload: unknown) => {
if (!payload || typeof payload !== "object") return;
const evt = payload as ChatEvent;
if (evt.sessionKey !== currentSessionKey) return;
if (finalizedRuns.has(evt.runId)) {
if (evt.state === "delta") return;
if (evt.state === "final") return;
}
if (evt.state === "delta") {
const text = extractTextFromMessage(evt.message, {
includeThinking: showThinking,
@@ -313,6 +334,7 @@ export async function runTui(opts: TuiOptions) {
includeThinking: showThinking,
});
chatLog.finalizeAssistant(text || "(no output)", evt.runId);
noteFinalizedRun(evt.runId);
activeChatRunId = null;
setStatus("idle");
}