Skip to content

Commit 607be9d

Browse files
committed
refactor: centralize chat connection ownership
1 parent 22c77a8 commit 607be9d

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

‎ui/src/pages/chat/chat-send-delivery.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { isTerminalFailureChatSendAck } from "./chat-send-ack.ts";
3030
import { cancelChatDelivery, canRestoreComposer, restoreComposer } from "./chat-send-composer.ts";
3131
import type { ChatHost } from "./chat-send-contract.ts";
3232
import {
33+
captureChatConnectionOwner,
3334
deliveryStateWriter,
3435
finishScopedChatSending,
3536
reconnectSafeQueuedSendState,
@@ -279,12 +280,7 @@ async function sendQueuedChatMessage(
279280
return "pending";
280281
}
281282

282-
const requestClient = host.client;
283-
const requestConnectionEpoch = host.connectionEpoch;
284-
const requestConnectionIsCurrent = () =>
285-
host.connected &&
286-
host.client === requestClient &&
287-
host.connectionEpoch === requestConnectionEpoch;
283+
const requestConnectionIsCurrent = captureChatConnectionOwner(host);
288284
const runId = prepared.sendRunId ?? generateUUID();
289285
const startedAt = Date.now();
290286
const requestStartedAtMs = controlUiNowMs();
@@ -593,8 +589,7 @@ export async function deliverChatQueueItem(
593589
const sessionKey = item.sessionKey ?? host.sessionKey;
594590
const storageMode = options.storageMode ?? "durable";
595591
const routingSessionKey = options.routingSessionKey ?? sessionKey;
596-
const deliveryClient = host.client;
597-
const deliveryConnectionEpoch = host.connectionEpoch;
592+
const deliveryConnectionIsCurrent = captureChatConnectionOwner(host, false);
598593
const deliveryAgentId =
599594
item.agentId ?? scopedAgentIdForSession(host, routingSessionKey) ?? undefined;
600595
const sendOptions = { ...options, routingSessionKey, storageMode };
@@ -697,8 +692,7 @@ export async function deliverChatQueueItem(
697692
}
698693
}
699694
if (
700-
host.client === deliveryClient &&
701-
host.connectionEpoch === deliveryConnectionEpoch &&
695+
deliveryConnectionIsCurrent() &&
702696
host.sessionKey === routingSessionKey &&
703697
visibleSessionMatches(host, routingSessionKey, deliveryAgentId)
704698
) {

‎ui/src/pages/chat/chat-send-queue-state.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ export function reconnectSafeQueuedSendState(
7474
return host.connected && host.client ? "waiting-idle" : "waiting-reconnect";
7575
}
7676

77+
export function captureChatConnectionOwner(
78+
host: Pick<ChatHost, "client" | "connected" | "connectionEpoch">,
79+
requireConnected = true,
80+
): () => boolean {
81+
const client = host.client;
82+
const connectionEpoch = host.connectionEpoch;
83+
return () =>
84+
(!requireConnected || host.connected) &&
85+
host.client === client &&
86+
host.connectionEpoch === connectionEpoch;
87+
}
88+
7789
export function updateQueuedSendItem(
7890
host: ChatHost,
7991
storageMode: QueuedChatStorageMode,

0 commit comments

Comments
 (0)