fix(gateway): unify chat display projection

This commit is contained in:
Peter Steinberger
2026-04-26 05:33:46 +01:00
parent dc9ce2a1bf
commit 5f2273e81e
18 changed files with 987 additions and 743 deletions

View File

@@ -407,10 +407,14 @@ function handleTerminalChatEvent(
host: GatewayHost,
payload: ChatEventPayload | undefined,
state: ReturnType<typeof handleChatEvent>,
activeRunIdBeforeEvent: string | null,
): boolean {
if (state !== "final" && state !== "error" && state !== "aborted") {
return false;
}
if (isEventForDifferentActiveRun(payload, activeRunIdBeforeEvent)) {
return false;
}
// Check if tool events were seen before resetting (resetToolStream clears toolStreamOrder).
const toolHost = host as unknown as Parameters<typeof resetToolStream>[0];
const hadToolEvents = toolHost.toolStreamOrder.length > 0;
@@ -447,6 +451,13 @@ function handleTerminalChatEvent(
return false;
}
function isEventForDifferentActiveRun(
payload: ChatEventPayload | undefined,
activeRunId: string | null,
): boolean {
return Boolean(activeRunId && payload?.runId && payload.runId !== activeRunId);
}
function handleChatGatewayEvent(host: GatewayHost, payload: ChatEventPayload | undefined) {
if (payload?.sessionKey) {
setLastActiveSessionKey(
@@ -463,8 +474,13 @@ function handleChatGatewayEvent(host: GatewayHost, payload: ChatEventPayload | u
sideResultHost.chatSideResultTerminalRuns?.delete(payload.runId);
return;
}
const activeRunIdBeforeEvent = host.chatRunId;
const state = handleChatEvent(host as unknown as ChatState, payload);
const historyReloaded = handleTerminalChatEvent(host, payload, state);
const terminalEventIsForDifferentActiveRun = isEventForDifferentActiveRun(
payload,
activeRunIdBeforeEvent,
);
const historyReloaded = handleTerminalChatEvent(host, payload, state, activeRunIdBeforeEvent);
const deferredReloadHost = host as GatewayHostWithDeferredSessionMessageReload;
const deferredSessionKey = deferredReloadHost.pendingSessionMessageReloadSessionKey?.trim();
const payloadSessionKey = payload?.sessionKey?.trim();
@@ -479,7 +495,12 @@ function handleChatGatewayEvent(host: GatewayHost, payload: ChatEventPayload | u
if (deferredSessionKey && payloadSessionKey && deferredSessionKey === payloadSessionKey) {
deferredReloadHost.pendingSessionMessageReloadSessionKey = null;
}
if (state === "final" && !historyReloaded && shouldReloadHistoryForFinalEvent(payload)) {
if (
state === "final" &&
!historyReloaded &&
!terminalEventIsForDifferentActiveRun &&
shouldReloadHistoryForFinalEvent(payload)
) {
void loadChatHistory(host as unknown as ChatState);
return;
}

View File

@@ -501,7 +501,12 @@ export function handleAgentEvent(host: ToolStreamHost, payload?: AgentEventPaylo
if (!entry) {
// Commit any in-progress streaming text as a segment so it renders
// above the tool card instead of below it.
if (host.chatStream && host.chatStream.trim().length > 0) {
if (
host.chatRunId &&
payload.runId === host.chatRunId &&
host.chatStream &&
host.chatStream.trim().length > 0
) {
host.chatStreamSegments = [...host.chatStreamSegments, { text: host.chatStream, ts: now }];
host.chatStream = null;
host.chatStreamStartedAt = null;