Skip to content

The agent that grows with you

fix(desktop): route reactions through stored session fallback

UniversePeak872b82b71f8d
diff --git a/apps/desktop/src/components/assistant-ui/thread/use-message-reactions.ts b/apps/desktop/src/components/assistant-ui/thread/use-message-reactions.ts
index 75b011c5555..6fe52bb23df 100644
--- a/apps/desktop/src/components/assistant-ui/thread/use-message-reactions.ts
+++ b/apps/desktop/src/components/assistant-ui/thread/use-message-reactions.ts
@@ -2,6 +2,7 @@ import { useAuiState, useMessageRuntime } from '@assistant-ui/react'
import { useStore } from '@nanostores/react'
import { type MouseEvent, useCallback } from 'react'
+import { useSessionView } from '@/app/chat/session-view'
import type { ChatMessage } from '@/lib/chat-messages'
import { triggerHaptic } from '@/lib/haptics'
import { QUICK_REACTIONS, toggleMessageReaction } from '@/store/reactions'
@@ -41,12 +42,13 @@ function commitReaction(
role: ChatMessage['role'],
rowId: number | undefined,
reactions: MessageReaction[],
- emoji: null | string
+ emoji: null | string,
+ sessionId: null | string
): void {
// Flip the UI immediately — a tapback is direct manipulation and must never
// wait on a round-trip. Persistence follows in the background.
setLocalReaction(messageId, emoji)
- void toggleMessageReaction({ id: messageId, role, rowId, reactions } as ChatMessage, emoji)
+ void toggleMessageReaction({ id: messageId, role, rowId, reactions } as ChatMessage, emoji, 'user', sessionId)
}
/**
@@ -81,12 +83,16 @@ export function useMessageReactions(
const enabled = useStore($reactionsEnabled)
const localAll = useStore($localReactions)
const agentLive = useStore($agentReactions)
+ const sessionView = useSessionView()
+ const runtimeSessionId = useStore(sessionView.$runtimeId)
+ const storedSessionId = useStore(sessionView.$storedId)
+ const sessionId = runtimeSessionId ?? storedSessionId
return {
enabled,
react: useCallback(
- (emoji: null | string) => commitReaction(messageId, role, rowId, reactions, emoji),
- [messageId, reactions, role, rowId]
+ (emoji: null | string) => commitReaction(messageId, role, rowId, reactions, emoji, sessionId),
+ [messageId, reactions, role, rowId, sessionId]
),
reactions: mergeReactions(reactions, localAll[messageId], rowId === undefined ? undefined : agentLive[rowId])
}
@@ -107,6 +113,10 @@ export function useTapbackDoubleClick(
): ((event: MouseEvent<HTMLElement>) => void) | undefined {
const enabled = useStore($reactionsEnabled)
const messageRuntime = useMessageRuntime()
+ const sessionView = useSessionView()
+ const runtimeSessionId = useStore(sessionView.$runtimeId)
+ const storedSessionId = useStore(sessionView.$storedId)
+ const sessionId = runtimeSessionId ?? storedSessionId
const onDoubleClick = useCallback(
(event: MouseEvent<HTMLElement>) => {
@@ -136,10 +146,11 @@ export function useTapbackDoubleClick(
role,
custom.rowId,
reactions,
- mine?.emoji === DOUBLE_CLICK_REACTION ? null : DOUBLE_CLICK_REACTION
+ mine?.emoji === DOUBLE_CLICK_REACTION ? null : DOUBLE_CLICK_REACTION,
+ sessionId
)
},
- [messageId, messageRuntime, role]
+ [messageId, messageRuntime, role, sessionId]
)
return enabled ? onDoubleClick : undefined
diff --git a/apps/desktop/src/store/reactions.test.ts b/apps/desktop/src/store/reactions.test.ts
index 357d563a2c6..32e37c936c0 100644
--- a/apps/desktop/src/store/reactions.test.ts
+++ b/apps/desktop/src/store/reactions.test.ts
@@ -1,10 +1,59 @@
-import { describe, expect, it } from 'vitest'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
-import { applyReaction } from '@/store/reactions'
import type { MessageReaction } from '@/types/hermes'
+const reactionTestState = vi.hoisted(() => {
+ const store = <T>(initial: T) => {
+ let value = initial
+
+ return {
+ get: () => value,
+ set: (next: T | ((current: T) => T)) => {
+ value = typeof next === 'function' ? (next as (current: T) => T)(value) : next
+ }
+ }
+ }
+ const gateway = { request: vi.fn(async (..._args: any[]) => ({ row_id: 7, reactions: [] })) }
+
+ return {
+ activeSessionId: store<string | null>(null),
+ gateway,
+ gatewayStore: store(gateway),
+ messages: store<any[]>([]),
+ requestForOwnedSession: vi.fn(
+ async <T>(
+ _sessionId: string | null,
+ request: typeof gateway.request,
+ method: string,
+ params: Record<string, unknown>
+ ) => request(method, params) as Promise<T>
+ ),
+ selectedStoredSessionId: store<string | null>(null)
+ }
+})
+
+vi.mock('@/store/gateway', () => ({ $gateway: reactionTestState.gatewayStore }))
+vi.mock('@/store/notifications', () => ({ notifyError: vi.fn() }))
+vi.mock('@/store/session-states', () => ({ requestForOwnedSession: reactionTestState.requestForOwnedSession }))
+vi.mock('@/store/session', () => ({
+ $activeSessionId: reactionTestState.activeSessionId,
+ $messages: reactionTestState.messages,
+ $selectedStoredSessionId: reactionTestState.selectedStoredSessionId,
+ setMessages: (update: (messages: any[]) => any[]) =>
+ reactionTestState.messages.set(update(reactionTestState.messages.get()))
+}))
+
+import { applyReaction, QUICK_REACTIONS, toggleMessageReaction } from '@/store/reactions'
const at = 1_700_000_000
+beforeEach(() => {
+ reactionTestState.activeSessionId.set(null)
+ reactionTestState.selectedStoredSessionId.set(null)
+ reactionTestState.messages.set([])
+ reactionTestState.gateway.request.mockClear()
+ reactionTestState.requestForOwnedSession.mockClear()
+})
+
function reaction(emoji: string, author: MessageReaction['author']): MessageReaction {
return { emoji, author, at }
}
@@ -50,3 +99,33 @@ describe('applyReaction', () => {
expect(before).toEqual(snapshot)
})
})
+
+describe('QUICK_REACTIONS', () => {
+ it('is the six iOS Tapback defaults, each distinct', () => {
+ expect(QUICK_REACTIONS).toHaveLength(6)
+ expect(new Set(QUICK_REACTIONS).size).toBe(6)
+ })
+})
+
+describe('toggleMessageReaction session binding', () => {
+ it('uses the selected stored session while its runtime id is unset', async () => {
+ reactionTestState.selectedStoredSessionId.set('stored-session')
+
+ await toggleMessageReaction({ id: 'message-1', role: 'assistant', parts: [] }, '❤️')
+
+ expect(reactionTestState.requestForOwnedSession).toHaveBeenCalledWith(
+ 'stored-session',
+ expect.any(Function),
+ 'message.react',
+ expect.objectContaining({ session_id: 'stored-session', newest_role: 'assistant', emoji: '❤️' })
+ )
+ expect(reactionTestState.gateway.request).toHaveBeenCalledTimes(1)
+ })
+
+ it('still rejects a genuinely new draft with no session identity', async () => {
+ await toggleMessageReaction({ id: 'message-1', role: 'assistant', parts: [] }, '❤️')
+
+ expect(reactionTestState.requestForOwnedSession).not.toHaveBeenCalled()
+ expect(reactionTestState.gateway.request).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/store/reactions.ts b/apps/desktop/src/store/reactions.ts
index 7ba8103054d..c63c1f76756 100644
--- a/apps/desktop/src/store/reactions.ts
+++ b/apps/desktop/src/store/reactions.ts
@@ -1,7 +1,7 @@
import type { ChatMessage } from '@/lib/chat-messages'
import { $gateway } from '@/store/gateway'
import { notifyError } from '@/store/notifications'
-import { $activeSessionId, $messages, setMessages } from '@/store/session'
+import { $activeSessionId, $messages, $selectedStoredSessionId, setMessages } from '@/store/session'
import { requestForOwnedSession } from '@/store/session-states'
import type { MessageReaction } from '@/types/hermes'
@@ -52,14 +52,21 @@ function writeReactions(messageId: string, reactions: MessageReaction[], rowId?:
export async function toggleMessageReaction(
message: ChatMessage,
emoji: null | string,
- author: MessageReaction['author'] = 'user'
+ author: MessageReaction['author'] = 'user',
+ sessionIdOverride?: null | string
): Promise<void> {
// A live message hasn't round-tripped through a resume yet, so it carries no
// rowId. Rather than disable the affordance (which made reactions invisible
// in any active conversation), let the backend resolve the newest row of
// this role — which is exactly the message being reacted to.
const rowId = message.rowId
- const sessionId = $activeSessionId.get()
+ // The runtime id is unavailable while a stored conversation is being
+ // resumed (and can briefly be cleared during a profile/context switch), but
+ // the mounted transcript is still owned by the selected stored session.
+ // Use that durable identity as the routing fallback instead of treating a
+ // visible conversation as a draft. A genuinely new draft has neither id.
+ const sessionId =
+ sessionIdOverride === undefined ? ($activeSessionId.get() ?? $selectedStoredSessionId.get()) : sessionIdOverride
const gateway = $gateway.get()
if (!sessionId || !gateway) {
@@ -85,7 +92,9 @@ export async function toggleMessageReaction(
// even though the row exists in the owning profile's state DB (#80670).
// requestForOwnedSession resolves the exact owner route and fails closed;
// the ambient request stays the fallback for legacy single-profile
- // setups where the owner cannot be named.
+ // setups where the owner cannot be named. The selected stored-id fallback
+ // above matters here too: owner lookup can resolve the stored id even
+ // while the runtime binding is still being rebuilt.
const result = await requestForOwnedSession<MessageReactResponse>(sessionId, ambientRequest, 'message.react', {
session_id: sessionId,
...(rowId === undefined ? { newest_role: message.role } : { row_id: rowId }),