Skip to content

Commit b104a33

Browse files
committed
refactor(ui): centralize chat outbox projections
1 parent 41051fe commit b104a33

15 files changed

Lines changed: 538 additions & 544 deletions

‎ui/src/e2e/chat-flow.navigation-presentation.e2e.test.ts‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -736,20 +736,35 @@ suite.define(() => {
736736
const state = (
737737
pane as HTMLElement & {
738738
state: {
739-
chatQueueByScope: Record<string, unknown[]>;
739+
settings?: { gatewayUrl?: string };
740740
};
741741
}
742742
).state;
743-
state.chatQueueByScope[`${targetKey}\u0000agent:main`] = [
744-
{
745-
id: "queued-before-switch",
746-
text: "flush after idle reconciliation",
747-
createdAt: Date.now(),
748-
sendState: "waiting-idle",
749-
sessionKey: targetKey,
750-
agentId: "main",
751-
},
752-
];
743+
const gatewayOwner = state.settings?.gatewayUrl?.trim() || "default";
744+
const key = `openclaw.control.chatComposer.v2:${encodeURIComponent(gatewayOwner)}`;
745+
sessionStorage.setItem(
746+
key,
747+
JSON.stringify({
748+
version: 2,
749+
gatewayOwner,
750+
sessions: {
751+
[`${targetKey}\u0000agent:main`]: {
752+
updatedAt: Date.now(),
753+
queue: [
754+
{
755+
id: "queued-before-switch",
756+
text: "flush after idle reconciliation",
757+
createdAt: Date.now(),
758+
sendState: "waiting-idle",
759+
sessionKey: targetKey,
760+
agentId: "main",
761+
},
762+
],
763+
},
764+
},
765+
}),
766+
);
767+
window.dispatchEvent(new StorageEvent("storage", { key }));
753768
}, firstKey);
754769
await page
755770
.locator(

‎ui/src/lib/chat/outbox-store.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ afterEach(() => {
1919
});
2020

2121
describe("stored outbox summaries", () => {
22-
it("bridges matching cross-tab storage changes until the last subscriber leaves", () => {
22+
it("bridges matching storage events until the last subscriber leaves", () => {
2323
const addEventListener = vi.spyOn(window, "addEventListener");
2424
const removeEventListener = vi.spyOn(window, "removeEventListener");
2525
const firstListener = vi.fn();

‎ui/src/pages/chat/chat-outbox-drain.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { loadChatHistory, type ChatHistoryResult, type ChatState } from "./chat-
1313
import {
1414
excludeComposerAttachments,
1515
removeQueuedMessageWithoutReleasing,
16-
syncChatQueueFromStoredOutbox,
16+
syncVisibleChatQueueProjection,
1717
updateQueuedMessageForSession,
1818
} from "./chat-queue.ts";
1919
import type { ChatHost } from "./chat-send-contract.ts";
@@ -184,7 +184,7 @@ async function readCurrentStoredChatHistory(
184184
if (!currentOutbox || !currentItem || !sameQueuedDeliveryVersion(currentItem, item)) {
185185
return "continue";
186186
}
187-
syncChatQueueFromStoredOutbox(host, currentOutbox);
187+
syncVisibleChatQueueProjection(host);
188188
if (chatMessagesContainQueuedSend(history.messages, item)) {
189189
// Server history owns the turn, but the visible transcript may not have
190190
// reloaded yet; materialize the turn locally before dropping the queue row
@@ -293,7 +293,7 @@ async function drainStoredChatOutbox(
293293
return "empty";
294294
}
295295
if (item.sendState === "unconfirmed" || item.sendState === "waiting-model") {
296-
syncChatQueueFromStoredOutbox(host, outbox);
296+
syncVisibleChatQueueProjection(host);
297297
return "blocked";
298298
}
299299
const visible = visibleSessionMatches(host, outbox.sessionKey, outbox.agentId);
@@ -303,7 +303,7 @@ async function drainStoredChatOutbox(
303303
lane.pendingOptions.delete(item.id);
304304
return "blocked";
305305
}
306-
syncChatQueueFromStoredOutbox(host, outbox);
306+
syncVisibleChatQueueProjection(host);
307307
if (item.localCommandName === "reset") {
308308
const resetText = item.localCommandArgs ? `/reset ${item.localCommandArgs}` : "/reset";
309309
const convertResetToMessage = (sendState?: ChatQueueItem["sendState"]) =>
@@ -494,7 +494,7 @@ async function drainStoredChatOutbox(
494494
}
495495
}
496496
if (visible && isChatBusy(host)) {
497-
syncChatQueueFromStoredOutbox(host, outbox);
497+
syncVisibleChatQueueProjection(host);
498498
updateQueuedMessageForSession(host, outbox.sessionKey, item.id, (entry) => ({
499499
...entry,
500500
sendState: host.connected && host.client ? "waiting-idle" : "waiting-reconnect",
@@ -506,7 +506,7 @@ async function drainStoredChatOutbox(
506506
if (!currentOutbox || !currentItem || !sameQueuedDeliveryVersion(currentItem, item)) {
507507
continue;
508508
}
509-
syncChatQueueFromStoredOutbox(host, currentOutbox);
509+
syncVisibleChatQueueProjection(host);
510510
const result = await dependencies.sendQueuedChatMessage(
511511
host,
512512
item.id,

0 commit comments

Comments
 (0)