Take the page AI chat thread from the URL and drop the global current thread

Merged
CH
15h ago
+535-870

Why

After #30582 the side panel and the chat widget own their thread, but the inbox pane and /chat still read one global, currentAiChatThreadState, kept in sync with the URL by an effect and seeded at start-up by picking the latest thread. The URL already says which chat the page shows, so the global, the sync effect, the start-up pick and the hasInitializedAiChatThreadsState flag are a second copy of it. This PR makes the URL the only source for the page chat and deletes them (last of the per-instance AI chat PRs).

How it works

flowchart LR
  URL["URL<br/>/inbox/:threadId, /inbox/new<br/>/chat/:threadId, /chat"] --> PAGE["InboxPage, AiChatPage<br/>useParams"]
  URL --> ID["getAiChatPageThreadId<br/>useAiChatPageThreadId"]
  PAGE -- "thread" --> THREAD["AiChatThreadPageContent<br/>ChatWidget instance"]
  PAGE -- "new chat" --> NEW["AiChatNewThreadPageContent<br/>AiChatInstance on newAiChatThreadIdState"]
  PAGE --> EFFECTS["AiChatPageEffects<br/>AiChatPageThreadLoadEffect<br/>AiChatPageContinueInSidePanelEffect"]
  ID --> DRAWER["Drawer and mobile highlight"]
  ID --> SYNC["InboxRecentThreadListSyncEffect"]
  ID --> LEAVE["useLeaveRemovedInboxThread<br/>useLoadInboxThreadList"]
  NEW -- "onThreadChange" --> SELECT["useSelectAiChatThread<br/>navigates"] --> URL
  • Which chat the page shows comes from the URL: a thread id, or the per-tab new chat on /inbox/new and /chat. Code outside the route reads it with getAiChatPageThreadId / useAiChatPageThreadId.
  • The new-chat page wraps its chat in an AiChatInstance on newAiChatThreadIdState whose onThreadChange navigates. Once a new chat's first message is sent, useAiChat calls the instance's onThreadChange with the created thread if the instance is still on it, which moves the URL to /inbox/:id or /chat/:id.
  • AiChatPageThreadLoadEffect replaces the URL sync effect: it loads a thread opened by URL that is not in the store yet, and falls back to the new chat when it does not exist.
  • Continue in side panel stores the page's thread id (aiChatContinueInSidePanelThreadIdState) instead of a boolean, so the hand-off no longer reads the global.

What

  • Deleted currentAiChatThreadState, hasInitializedAiChatThreadsState, currentAiChatThreadDataSelector, closingAiChatThreadIdState, resetClosingInboxThread, useSwitchAiChatThreadWithDraft, AiChatPageThreadUrlSyncEffect, AiChatPageThreadSelection, AiChatCurrentThreadInstance, useIsCurrentAiChatThreadNew and getDisplayedAiChatThreadId.
  • InboxThreadInitializationEffect only loads the recent list; the start-up pick is gone.
  • /chat without an id opens the new chat instead of the last current thread.
  • The drawer and mobile home highlight the thread in the URL.
  • The empty state and loading selector no longer wait for the start-up flag.
  • Tests that used the global to pick the thread of AiChatTestInstance now set aiChatTestInstanceThreadIdState from modules/ai/testing.

Screenshots

No visual change. Checked on a local stack:

  • Inbox: opening a thread from the list highlights it in the drawer; New chat goes to /inbox/new, and sending its first message moves the URL to the new thread with the message shown.
  • /chat opens the new chat; /chat/<unknown id> falls back to /chat; a cold load of /chat/<thread> shows the thread.
  • Leaving /chat/<thread> for Companies continues that thread in the side panel; leaving /inbox/new with a draft continues the new chat and its draft in the side panel; closing the inbox pane and then leaving opens nothing.
  • No console errors. Streaming not exercised (no AI provider configured locally).