Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Make one Welcome chat when switch-buddy's chat is opened twice at once

Simon Flack817b76e87c14
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.test.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.test.ts
index c4a67dea4..2a2447355 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.test.ts
@@ -98,6 +98,31 @@ describe('openBuddyChat', () => {
);
});
+ it('makes one Welcome chat when the chat is opened twice at once', async () => {
+ // The setup dialog's success and the buddy page noticing the new buddy
+ // both open its chat in the same moment.
+ listChats.mockResolvedValue([]);
+ createChat.mockResolvedValue(chat('r-new', BUDDY_CHAT_NAME));
+
+ await Promise.all([openBuddyChat('srv-1', buddy), openBuddyChat('srv-1', buddy)]);
+
+ expect(createChat).toHaveBeenCalledTimes(1);
+ expect(navigate).toHaveBeenCalledTimes(2);
+ for (const call of navigate.mock.calls) expect(call[1]).toMatchObject({ roomId: 'r-new' });
+ });
+
+ it('looks again for a later open, once the first has finished', async () => {
+ listChats.mockResolvedValue([]);
+ createChat.mockResolvedValue(chat('r-new', BUDDY_CHAT_NAME));
+ await openBuddyChat('srv-1', buddy);
+
+ listChats.mockResolvedValue([chat('r-new', BUDDY_CHAT_NAME)]);
+ await openBuddyChat('srv-1', buddy);
+
+ expect(listChats).toHaveBeenCalledTimes(2);
+ expect(createChat).toHaveBeenCalledTimes(1);
+ });
+
it("still opens the buddy's page, and says why, when its chat cannot be had", async () => {
listChats.mockRejectedValue(new Error('Switch is down.'));
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.ts
index 7dfbc548c..aec46e272 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-buddy/open-buddy.ts
@@ -13,13 +13,32 @@ import { buddyWelcomeMessage } from './buddy-welcome-message';
/** What the buddy's first chat is called, as on the board; it keeps it. */
export const BUDDY_CHAT_NAME = 'Welcome';
+/** The buddy chats being found or made, by server and agent: one at a time each. */
+const finding = new Map<string, Promise<ChatSummary>>();
+
/**
* The buddy's chat to open: the one named Welcome, else its newest direct
* chat; a new Welcome chat only when it has none. Opening the buddy again —
* replaying onboarding, a second Start — lands in the same chat rather than
* adding one each time.
+ *
+ * Two opens at once share one look, so they cannot each find no chat and
+ * each make one: the setup dialog's success and the buddy's page noticing it
+ * now exists both open it in the same moment.
*/
-async function buddyChat(
+function buddyChat(
+ serverId: string,
+ agent: { agentId: string; name: string }
+): Promise<ChatSummary> {
+ const key = `${serverId}\u0000${agent.agentId}`;
+ const running = finding.get(key);
+ if (running) return running;
+ const found = findOrMakeBuddyChat(serverId, agent).finally(() => finding.delete(key));
+ finding.set(key, found);
+ return found;
+}
+
+async function findOrMakeBuddyChat(
serverId: string,
agent: { agentId: string; name: string }
): Promise<ChatSummary> {