Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(desktop-app): seed the chat drawer from conversationOrigin, drop the destroy round-trip (Fase 4)

Diego Zacarias7828e0fb4b0d
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index d6c48bc4e..10e884595 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -282,6 +282,10 @@ export function App() {
// not mounted yet, so we open the drawer and defer the focus bump until its
// column commits.
const pendingChatSwitcherFocusRef = React.useRef(false)
+ // Mirror of the app's originating conversation so the drawer can be seeded
+ // from it inside stable callbacks.
+ const sandboxUiConversationOriginRef = React.useRef<SandboxUiConversationOrigin | null>(null)
+ sandboxUiConversationOriginRef.current = sandboxUiConversationOrigin
const [composerFocusRequestId, setComposerFocusRequestId] = React.useState(0)
const [globalSearchFocusRequestId, setGlobalSearchFocusRequestId] = React.useState(0)
const [notificationOpenRequestId, setNotificationOpenRequestId] = React.useState(0)
@@ -383,11 +387,25 @@ export function App() {
const openChatDrawer = React.useCallback(() => {
setChatDrawerOpen(true)
+ // Seed the drawer from the conversation the app was opened from, if any, so
+ // "open the drawer" brings that chat back in place — no destroy-and-
+ // reconstitute round-trip. Otherwise reveal whatever tab is already active.
+ const origin = sandboxUiConversationOriginRef.current
+ let next = chatViewTabsRef.current
+ if (origin) {
+ next = openPersistedChatViewTab(next, {
+ id: nextChatTabId(),
+ agentRef: origin.agentName,
+ chatId: origin.chatId,
+ title: origin.title,
+ })
+ setChatViewTabs(next)
+ }
// The ref still reads `false` until the next render commits the open state,
// so reveal explicitly in-drawer to swap the shared <ChatPage> in place.
- revealChatViewTab(activeChatViewTab(chatViewTabsRef.current), true)
+ revealChatViewTab(activeChatViewTab(next), true)
setComposerFocusRequestId(value => value + 1)
- }, [revealChatViewTab])
+ }, [nextChatTabId, revealChatViewTab])
const closeChatDrawer = React.useCallback(() => {
setChatDrawerOpen(false)
@@ -733,9 +751,24 @@ export function App() {
setActiveSandboxUiApp(app)
vm.handleNavSelect(DESKTOP_ROUTES.apps)
setSandboxUiShortcutOpenRequestId(requestId)
+ if (conversationOrigin) {
+ // Opened from a chat: bring that conversation straight into the drawer
+ // instead of the destroy-and-reconstitute round-trip. The embed stays
+ // live; `keepNavItem` (via revealChatViewTab in-drawer) records a pending
+ // selection that survives the `apps` route change and loads the chat.
+ setChatDrawerOpen(true)
+ const next = openPersistedChatViewTab(chatViewTabsRef.current, {
+ id: nextChatTabId(),
+ agentRef: conversationOrigin.agentName,
+ chatId: conversationOrigin.chatId,
+ title: conversationOrigin.title,
+ })
+ setChatViewTabs(next)
+ revealChatViewTab(activeChatViewTab(next), true)
+ }
return requestId
},
- [vm.handleNavSelect]
+ [nextChatTabId, revealChatViewTab, vm.handleNavSelect]
)
const handleSidebarNavSelect = React.useCallback(
diff --git a/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx b/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx
index b1503aa16..a9205ea48 100644
--- a/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx
+++ b/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx
@@ -60,6 +60,39 @@ describe('useAppController — chat drawer keepNavItem', () => {
expect(app.result.current.navItem).toBe(DESKTOP_ROUTES.apps)
})
+ it('bypasses the same-route fast path with keepNavItem so a concurrent route change survives', async () => {
+ installAppControllerClerum({ agentNames: ['agent-x'] })
+ const app = renderAppController()
+ unmount = app.unmount
+
+ await waitFor(() => expect(app.result.current.isAuthenticated).toBe(true))
+ await waitFor(() => expect(app.result.current.initialExperienceLoading).toBe(false))
+
+ // Land on the chat route with the agent selected — this is exactly the state
+ // where the fast path (same agent + same-route + chatId) would fire.
+ act(() => {
+ app.result.current.handleSelectChatAgent('agent-x', { chatId: 'chat-1', selectLatest: false })
+ })
+ await waitFor(() => expect(app.result.current.navItem).toBe(DESKTOP_ROUTES.chat))
+ await waitFor(() => expect(app.result.current.selectedAgent).toBe('agent-x'))
+
+ // Launch-from-chat batches a route change to `apps` with a keepNavItem chat
+ // selection. `nav.navItem` still reads `chat` when the selection runs, so the
+ // fast path would set navItem back to `chat` and leave no pending selection to
+ // survive the route change. keepNavItem must force the pending-selection path.
+ act(() => {
+ app.result.current.handleNavSelect(DESKTOP_ROUTES.apps)
+ app.result.current.handleSelectChatAgent('agent-x', {
+ chatId: 'chat-2',
+ title: 'Seeded from conversation',
+ selectLatest: false,
+ keepNavItem: true,
+ })
+ })
+
+ expect(app.result.current.navItem).toBe(DESKTOP_ROUTES.apps)
+ })
+
it('navigates to the full-screen chat route without keepNavItem', async () => {
installAppControllerClerum({ agentNames: ['agent-x'] })
const app = renderAppController()
diff --git a/desktop-app/ui/src/hooks/useAppController.ts b/desktop-app/ui/src/hooks/useAppController.ts
index 87b65f81e..3fb2e3be2 100644
--- a/desktop-app/ui/src/hooks/useAppController.ts
+++ b/desktop-app/ui/src/hooks/useAppController.ts
@@ -809,7 +809,17 @@ export function useAppController() {
// the pending-selection path below, which is designed to survive a route
// change: it sets the active chat synchronously AND records the selection
// the effect replays.
- if (targetChatId && nav.selectedAgent === agentName && nav.navItem === DESKTOP_ROUTES.chat) {
+ // `keepNavItem` always takes the pending-selection path below: it is used
+ // when the route is changing to (or staying on) a non-chat route (the app
+ // drawer), where the fast path's imperative switch would leave no pending
+ // selection for the agent-selection effect to replay after `navItem`
+ // changes — the effect would then reset `activeChatId` to null.
+ if (
+ targetChatId &&
+ nav.selectedAgent === agentName &&
+ nav.navItem === DESKTOP_ROUTES.chat &&
+ !options.keepNavItem
+ ) {
// D.4: switchToChat is now a single unified path (no isRemote) — the
// server is the source of truth and hydrates server-only chats itself.
void chat.switchToChat(agentName, targetChatId)
diff --git a/desktop-app/ui/src/pages/SandboxUiPage.tsx b/desktop-app/ui/src/pages/SandboxUiPage.tsx
index 05dedb8b9..31c81e4c9 100644
--- a/desktop-app/ui/src/pages/SandboxUiPage.tsx
+++ b/desktop-app/ui/src/pages/SandboxUiPage.tsx
@@ -550,15 +550,24 @@ export function SandboxUiPage({
return (
<section className="page">
<div className="sandbox-ui-mounted-header">
- {conversationOrigin && onBackToConversation ? (
+ {conversationOrigin && (onToggleChatDrawer || onBackToConversation) ? (
+ // The originating conversation lives in the drawer beside the live
+ // embed now, so "Back to {title}" toggles the drawer instead of
+ // destroying the embed and reconstructing the chat full-screen. When
+ // no drawer toggle is wired it falls back to the destroy-and-
+ // reconstitute path (also reachable via the app.backToConversation
+ // command).
<Button
color="neutral"
- variant="soft"
+ variant={onToggleChatDrawer && chatDrawerOpen ? 'solid' : 'soft'}
size="sm"
className="sandbox-ui-conversation-btn"
aria-label={`Back to ${conversationOrigin.title}`}
+ aria-pressed={onToggleChatDrawer ? chatDrawerOpen : undefined}
title={`Back to ${conversationOrigin.title}`}
- onClick={() => void handleBackToConversation()}
+ onClick={
+ onToggleChatDrawer ? onToggleChatDrawer : () => void handleBackToConversation()
+ }
>
<IconChat />
<span>Back to {conversationOrigin.title}</span>
@@ -576,7 +585,8 @@ export function SandboxUiPage({
<IconClose />
<span>Back to apps</span>
</Button>
- {launch.kind === 'mounted' && onToggleChatDrawer && (
+ {launch.kind === 'mounted' && onToggleChatDrawer && !conversationOrigin && (
+ // No originating conversation to return to — a plain drawer toggle.
<Button
color="neutral"
variant={chatDrawerOpen ? 'solid' : 'soft'}