Skip to content

Keep several chats live beside one conversation list

Let a reply go on streaming in one conversation while the person reads or writes in another — one chat per conversation, one conversation manager, one list.

On this page

A chat answers one conversation at a time: open another one in it, and the reply on its way stops. For a reply that goes on streaming in conversation A while the person reads or writes in B — and is complete when they come back — keep one chat mounted per conversation, show one of them, and let the list choose which.

It takes four things:

  • One conversation manager for every chat, on the config they share (Save conversations).
  • One chat per conversation, each with its own id and a client scoped to it (scopeToTargetId), so each turn is answered in its own chat.
  • Each chat bound to its conversation for its whole life: the conversationId it mounts with, or null for a new one. A new chat’s first message creates the conversation and hands you its id through onConversationCreated; give it back to that chat as its conversationId if your state keeps it — the chat already shows it, so nothing reloads.
  • A list that opens nothing by itself: target set to the id of the element that holds the chats, which is no chat’s id. A click on a row then reaches your code alone, and you show the chat that has that conversation, or mount one for it.
ts
import { AparteClient, AparteConversationController, type AparteChat } from '@aparte/core';

const pool = document.getElementById('chats')!;   // <div id="chats">
const list = document.querySelector('aparte-conversation-list')!;
list.setAttribute('target', 'chats');
list.setAttribute('manage', '');

const live: Array<{ chat: AparteChat; controller: AparteConversationController }> = [];
let next = 1;

function show(chat: AparteChat): void {
  for (const entry of live) entry.chat.hidden = entry.chat !== chat;
  const shown = live.find((entry) => entry.chat === chat);
  list.setAttribute('active-id', shown?.controller.activeId ?? '');
}

function mount(conversationId: string | null): AparteChat {
  const chat = document.createElement('aparte-chat') as AparteChat;
  chat.id = `chat-${next++}`;
  pool.append(chat);
  new AparteClient({ scopeToTargetId: chat.id }).start();
  const viewport = chat.viewport!;
  const controller = new AparteConversationController({
    hostId: chat.id,
    host: chat,
    getMessages: () => viewport.getMessages(),
    setMessages: (messages) => viewport.setMessages(messages),
    appendMessage: (message) => viewport.appendMessage(message),
    clearMessages: (options) => viewport.clearAll(options),
  }, { onConversationCreated: () => { if (!chat.hidden) show(chat); } });
  controller.bind();
  void controller.setConversationId(conversationId);
  live.push({ chat, controller });
  return chat;
}

window.addEventListener('aparte-conversation-select', (e) => {
  const { id, targetId } = e.detail;
  if (targetId !== 'chats') return;
  const open = id ? live.find((entry) => entry.controller.activeId === id) : undefined;
  show(open?.chat ?? mount(id));
});

show(mount(null));

A New chat button of yours sends aparte-conversation-select with { id: null, targetId: 'chats' } (AparteConversationSelectDetail types id as string | null), and the same code mounts a fresh chat for it. Unmount the chats you no longer need — a few recent ones are enough — and a chat that goes saves what it holds, a reply still on its way included.

The manager has one activeId. Every chat writes it when it opens or creates a conversation, so with several chats it names whichever moved last — not the one on screen, and not a given chat’s. Each chat answers for itself: getConversationId() on its handle returns the conversation it shows, or the one its first message is creating, and null before either. It is on every wrapper’s <AparteChat> (React’s ref, Vue’s and Svelte’s instance, Angular’s component) and on useAparteChat(). A chat’s first message only ever creates a conversation of its own, even while another chat is creating one at the same moment.

Calling manager.select() or manager.createNew() yourself moves no chat. A chat opens a conversation through its conversationId, its setConversationId(), or aparte-conversation-select carrying its id as targetId.

A chat hidden with hidden or display: none is still mounted, so it goes on working: its client answers the turn, the reply is written into its transcript and saved when the turn ends, and aparte-conversation-loaded is sent once a conversation’s messages are in it, shown or not. What a hidden chat cannot do is measure itself: call scrollToBottom() on it when you show it, if it should open at the end of the reply.