Skip to content

The agent that grows with you

fix(desktop): keep optional action handlers optional through the latest-actions adapters

teknium178c06525e8e9
diff --git a/apps/desktop/src/app/contrib/latest-actions.test.ts b/apps/desktop/src/app/contrib/latest-actions.test.ts
index b0fecc28efe..9ff335054f3 100644
--- a/apps/desktop/src/app/contrib/latest-actions.test.ts
+++ b/apps/desktop/src/app/contrib/latest-actions.test.ts
@@ -78,4 +78,48 @@ describe('latestActions adapters', () => {
expect(staleNavigate).not.toHaveBeenCalled()
expect(latestNavigate).toHaveBeenCalledWith(item)
})
+
+ // An absent optional handler must stay absent through the adapter. Children
+ // gate on PRESENCE, not just invocation: onDismissError renders the dismiss
+ // button only when defined, onRestoreToMessage gates the restore-confirm
+ // flow, and onTranscribeAudio gates voice recording. Wrapping an undefined
+ // field in an arrow function makes it unconditionally truthy, which would
+ // paint a dead dismiss button and let voice recording run with no
+ // transcription backend.
+ it('leaves absent optional handlers undefined instead of always-truthy wrappers', () => {
+ const chat = makeChatActions()
+ chat.onDismissError = undefined
+ chat.onRestoreToMessage = undefined
+ chat.onTranscribeAudio = undefined
+
+ const adaptedChat = latestChatActions(chat)
+
+ expect(adaptedChat.onDismissError).toBeUndefined()
+ expect(adaptedChat.onRestoreToMessage).toBeUndefined()
+ expect(adaptedChat.onTranscribeAudio).toBeUndefined()
+
+ const sidebar = makeSidebarActions()
+ sidebar.onLoadMoreMessaging = undefined
+ sidebar.onLoadMoreProfileSessions = undefined
+
+ const adaptedSidebar = latestSidebarActions(sidebar)
+
+ expect(adaptedSidebar.onLoadMoreMessaging).toBeUndefined()
+ expect(adaptedSidebar.onLoadMoreProfileSessions).toBeUndefined()
+ })
+
+ it('still late-binds a PRESENT optional handler to the latest closure', async () => {
+ const staleTranscribe = vi.fn(async () => 'stale')
+ const latestTranscribe = vi.fn(async () => 'latest')
+ const actions = makeChatActions()
+ actions.onTranscribeAudio = staleTranscribe
+
+ const adapted = latestChatActions(actions)
+
+ actions.onTranscribeAudio = latestTranscribe
+
+ expect(adapted.onTranscribeAudio).toBeTypeOf('function')
+ await expect(adapted.onTranscribeAudio!(new Blob())).resolves.toBe('latest')
+ expect(staleTranscribe).not.toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/src/app/contrib/latest-actions.ts b/apps/desktop/src/app/contrib/latest-actions.ts
index 91750243a12..4407739398a 100644
--- a/apps/desktop/src/app/contrib/latest-actions.ts
+++ b/apps/desktop/src/app/contrib/latest-actions.ts
@@ -6,7 +6,28 @@ import type { ChatActions, SidebarActions } from './types'
* directly, the child keeps the function from the surface's last render and can
* submit/click against a stale session closure. These adapters keep a stable
* wrapper but dereference the latest field at call time.
+ *
+ * OPTIONAL handlers must stay optional. Several children gate on a handler's
+ * *presence*, not just call it — `onDismissError` renders the dismiss button
+ * only when it exists (assistant-message.tsx), `onRestoreToMessage` gates the
+ * restore-confirm flow (thread/index.tsx), and `onTranscribeAudio` gates voice
+ * recording/conversation (use-voice-recorder, use-voice-conversation). An
+ * unconditional arrow wrapper is always truthy, which would render a dead
+ * dismiss button and let voice recording proceed with no transcription backend.
+ * So wrap an optional field only when it is currently present, and re-read the
+ * latest value inside the wrapper for the stale-closure fix.
*/
+function latestOptional<A extends unknown[], R>(
+ read: () => ((...args: A) => R) | undefined
+): ((...args: A) => R) | undefined {
+ // Presence is sampled from the object identity the surface currently holds.
+ // The controller mutates fields in place rather than swapping a handler
+ // between defined and undefined, so presence is stable for a given actions
+ // object while the *closure* is what churns — which is exactly what the
+ // indirection below re-reads.
+ return read() ? (...args: A) => read()!(...args) : undefined
+}
+
export function latestChatActions(actions: ChatActions): ChatActions {
return {
onAddContextRef: (...args) => actions.onAddContextRef(...args),
@@ -16,7 +37,7 @@ export function latestChatActions(actions: ChatActions): ChatActions {
onBranchInNewChat: (...args) => actions.onBranchInNewChat(...args),
onCancel: (...args) => actions.onCancel(...args),
onDeleteSelectedSession: (...args) => actions.onDeleteSelectedSession(...args),
- onDismissError: (...args) => actions.onDismissError?.(...args),
+ onDismissError: latestOptional(() => actions.onDismissError),
onEdit: (...args) => actions.onEdit(...args),
onPasteClipboardImage: (...args) => actions.onPasteClipboardImage(...args),
onPickFiles: (...args) => actions.onPickFiles(...args),
@@ -24,13 +45,13 @@ export function latestChatActions(actions: ChatActions): ChatActions {
onPickImages: (...args) => actions.onPickImages(...args),
onReload: (...args) => actions.onReload(...args),
onRemoveAttachment: (...args) => actions.onRemoveAttachment(...args),
- onRestoreToMessage: (...args) => actions.onRestoreToMessage?.(...args) ?? Promise.resolve(),
+ onRestoreToMessage: latestOptional(() => actions.onRestoreToMessage),
onRetryResume: (...args) => actions.onRetryResume(...args),
onSteer: (...args) => actions.onSteer(...args),
onSubmit: (...args) => actions.onSubmit(...args),
onThreadMessagesChange: (...args) => actions.onThreadMessagesChange(...args),
onToggleSelectedPin: (...args) => actions.onToggleSelectedPin(...args),
- onTranscribeAudio: (...args) => actions.onTranscribeAudio?.(...args) ?? Promise.resolve('')
+ onTranscribeAudio: latestOptional(() => actions.onTranscribeAudio)
}
}
@@ -39,8 +60,8 @@ export function latestSidebarActions(actions: SidebarActions): SidebarActions {
onArchiveSession: (...args) => actions.onArchiveSession(...args),
onBranchSession: (...args) => actions.onBranchSession(...args),
onDeleteSession: (...args) => actions.onDeleteSession(...args),
- onLoadMoreMessaging: (...args) => actions.onLoadMoreMessaging?.(...args),
- onLoadMoreProfileSessions: (...args) => actions.onLoadMoreProfileSessions?.(...args),
+ onLoadMoreMessaging: latestOptional(() => actions.onLoadMoreMessaging),
+ onLoadMoreProfileSessions: latestOptional(() => actions.onLoadMoreProfileSessions),
onLoadMoreSessions: (...args) => actions.onLoadMoreSessions(...args),
onManageCronJob: (...args) => actions.onManageCronJob(...args),
onNavigate: (...args) => actions.onNavigate(...args),
diff --git a/apps/desktop/src/app/contrib/surfaces.tsx b/apps/desktop/src/app/contrib/surfaces.tsx
index 0d95bdf6e78..852c5311a29 100644
--- a/apps/desktop/src/app/contrib/surfaces.tsx
+++ b/apps/desktop/src/app/contrib/surfaces.tsx
@@ -141,6 +141,7 @@ export const ChatRoutesSurface = memo(function ChatRoutesSurface({
)
const chatActions = useMemo(() => latestChatActions(actions), [actions])
+
const chatView = (
<ChatView
gateway={gateway}
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..5e3ba0a4569
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+eagle-nyp
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..5e3ba0a4569
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+eagle-nyp