Let the side panel and chat widget own the AI chat thread they show
Why
Every AI chat surface shows one global open thread, currentAiChatThreadState. So the side panel and a page chat can never show different threads, the chat widget has to copy its record into the global, and guards close the side panel whenever a page chat mounts. This PR makes the side panel and the chat widget own their thread, so two chats can be open at once (first of two PRs from the per-instance AI chat design doc).
How it works
The inbox pane and /chat have no host of their own: on a new chat they render AiChatNewThreadPageContent, and on a thread they render AiChatThreadPageContent, a record page layout whose chat widget is ChatWidget. Each host decides which thread its chat shows and wraps the chat in an AiChatInstance. The chat tree reads its thread from that instance. Each instance registers its thread, and the runtime mounted once in AiChatProvider runs one fetch and one stream per distinct open thread, writing to per-thread states.
flowchart TB
subgraph surfaces["Where a chat appears"]
INBOX["Inbox pane<br/>InboxPage"]
CHATPAGE["/chat page<br/>AiChatPage"]
ASKAI["Side panel Ask AI"]
RECORD["Chat thread record<br/>on a record page or in the side panel"]
end
subgraph hosts["Hosts: who decides the thread"]
SP["SidePanelAskAiPage<br/>askAiSidePanelThreadIdComponentState<br/>(side panel page instance)<br/>useAskAiSidePanelThreadId"]
CW["ChatWidget<br/>threadId = targetRecord.id<br/>onThreadChange = useAiChatThreadClick"]
PG["AiChatNewThreadPageContent<br/>AiChatCurrentThreadInstance<br/>threadId = currentAiChatThreadState ?? newAiChatThreadIdState<br/>onThreadChange = useSelectAiChatThread"]
end
subgraph instance["AiChatInstance (one per mounted chat)"]
CTX["AiChatInstanceContext<br/>{ instanceId, threadId, onThreadChange }<br/>read with useAiChatInstanceOrThrow"]
REG["AiChatInstanceRegistrationEffect"]
PRE["AiChatPrepromptEffect<br/>(when useIsAiChatRuntimeEnabled)"]
SEND["AiChatSendMessageEffect<br/>listens to send and stop, calls useAiChat<br/>(only under AiChatRuntimeContext)"]
TAB["AiChatTab and the chat tree<br/>composer, message list, usage,<br/>AiChatEditorDraftSyncEffect,<br/>AiChatDictationEffect, AiChatThreadMarkAsReadEffect"]
end
subgraph instanceState["Per-instance component states"]
DICT["dictationEngineComponentState<br/>isDictationRecordingComponentState"]
end
subgraph runtime["AiChatProvider runtime (mounted once)"]
OPEN["aiChatOpenThreadIdByInstanceIdState<br/>{ instanceId: threadId }"]
RT["AiChatRuntimeEffects<br/>one pair per distinct thread"]
FETCH["AiChatMessagesFetchEffect"]
STREAM["AiChatStreamSubscriptionEffect<br/>useAiChatSubscription"]
end
subgraph threadState["Per-thread family states (shared by chats on the same thread)"]
SEL["aiChat*FamilySelector by threadId<br/>has message, message ids, last message,<br/>first unread, reply quote"]
TS["aiChatMessagesFamilyState, aiChatFetchedMessagesFamilyState,<br/>aiChatMessagesLoadingFamilyState, aiChatIsStreamingFamilyState,<br/>aiChatUploadedFilesFamilyState, aiChatSelectedFilesFamilyState,<br/>aiChatThreadVisitFamilyState, aiChatThreadOpenViewCountFamilyState,<br/>aiChatUsageFamilyState, aiChatDraftsByThreadIdState"]
end
INBOX -- "/inbox/new" --> PG
INBOX -- "/inbox/:threadId<br/>AiChatThreadPageContent" --> CW
CHATPAGE -- "/chat" --> PG
CHATPAGE -- "/chat/:threadId<br/>AiChatThreadPageContent" --> CW
ASKAI --> SP
RECORD --> CW
SP --> CTX
CW --> CTX
PG --> CTX
CTX --> REG
CTX --> PRE
CTX --> SEND
CTX --> TAB
REG -- "registers its thread" --> OPEN
TAB -- "reads with its threadId" --> SEL
TAB -- "reads" --> DICT
TAB -- "reads and writes" --> TS
OPEN --> RT
RT --> FETCH
RT --> STREAM
FETCH -- "writes" --> TS
STREAM -- "writes" --> TS
SEL -- "derived from" --> TS
SEND -- "writes" --> TS
SEND -- "onThreadChange: new chat sent, DM opened" --> CTX
Opening a thread from outside a chat goes through the host instead of a global:
flowchart LR
CLICK["Thread click, new chat,<br/>deep link, hand-off, onboarding"] --> DECIDE{"On the inbox<br/>or /chat?"}
DECIDE -- yes --> SELECT["useSelectAiChatThread<br/>currentAiChatThreadState + URL"] --> PG2["Page chat"]
DECIDE -- no --> OPENASK["openAskAiPage({ threadId })<br/>writes askAiSidePanelThreadIdComponentState<br/>for the new side panel page"] --> SP2["Side panel chat"]
Browser events now say which chat or thread they are for:
| Event | Detail | Listened by |
|---|---|---|
| Send, stop | { instanceId } | useAiChat, dictation and sent-message hand-off of that chat |
| Refetch messages | { threadId } | AiChatMessagesFetchEffect of that thread |
| Restore editor content | { threadId, content } | every editor showing that thread |
What
AiChatInstancewraps every rendered chat and providesAiChatInstanceContext. The chat tree reads the thread fromuseAiChatInstanceOrThrow()instead of the global (mechanical, most of the diff).- The global displayed thread is gone: a chat shows its own thread, and the selectors that read the displayed thread are family selectors keyed by thread id.
- The composer's reply quote now hides while the chat's thread has no messages loaded, instead of comparing the displayed thread with the current one.
- One runtime per open thread: instances register in
aiChatOpenThreadIdByInstanceIdState, andAiChatRuntimeEffectsmounts one fetch and one stream per distinct thread.useAiChatis a plain action hook;AiChatSendMessageEffectlistens for a chat's send and stop events and only mounts underAiChatProvider(AiChatRuntimeContext). - State that two chats would otherwise fight over is keyed by thread (messages loading, uploads, visits, open view count) or by instance (dictation). Editors on the same thread stay in sync with the shared draft.
- Hosts:
- Side panel Ask AI keeps its thread in
askAiSidePanelThreadIdComponentState.openAskAiPage({ threadId })opens it on a thread, or on the new chat when none is given. - The chat widget shows its record's thread.
ChatWidgetThreadSyncEffect,ChatWidgetOpenInChatPageEffectandAiChatPageCloseSidePanelChatEffectare deleted. - The new-chat page in the inbox and
/chatstill reads the global throughAiChatCurrentThreadInstance; moving it to the URL is the next PR.
- Side panel Ask AI keeps its thread in
- Thread clicks, new chat, deep links, the continue-in-side-panel hand-off, the onboarding setup chat and preprompts pass a thread id instead of setting the global.
Screenshots
Checked on a local stack: Ask AI open in the side panel with a draft on a new chat, then navigating to an inbox thread. The side panel stays open on its own new chat while the inbox pane loads and shows "Qualify the lead". Before, the side panel closed. Streaming was not exercised locally (no AI provider configured).