From 4b45c4a644419f4c036098486142458b027df673 Mon Sep 17 00:00:00 2001 From: william Date: Sun, 1 Nov 2026 10:25:48 +0100 Subject: [PATCH] fix(voice): sync voice-originated pipeline turns to the chat window A learn-intent conversation driven entirely by voice never reached ObjectiveValidationCard: the chat window's Zustand store lives in its own isolated renderer process and only ever updated from its own direct sessions:route-message calls, never from voice-turn results handled in the main process. A user doing everything by voice could never see or confirm their objective. The main process now opens the chat window and broadcasts the voice turn (reply/ready/draft) to it whenever intent is 'learn'; the chat window's store applies it the same way it applies its own responses. Co-Authored-By: Claude Sonnet 5 --- Max Avatar/src/main/index.ts | 21 +++++++++++++++ Max Avatar/src/preload/index.d.ts | 1 + Max Avatar/src/preload/index.ts | 6 +++++ .../src/renderer/src/components/ChatPanel.tsx | 8 +++++- .../renderer/src/stores/useSessionStore.ts | 19 +++++++++++++- .../tests/unit/components/ChatPanel.test.tsx | 26 ++++++++++++++++++- 6 files changed, 78 insertions(+), 3 deletions(-) diff --git a/Max Avatar/src/main/index.ts b/Max Avatar/src/main/index.ts index 6e69442..2a49970 100644 --- a/Max Avatar/src/main/index.ts +++ b/Max Avatar/src/main/index.ts @@ -179,6 +179,18 @@ app } } + // Relais inter-fenetres specifique au vocal (voir handler "voice-turn" ci-dessous) : la + // fenetre de chat texte a son propre store Zustand (messages, brouillon d'objectif) qui ne + // recoit jamais les tours de conversation inities depuis la voix, puisque ceux-ci sont geres + // entierement dans le main process sans passer par l'IPC "sessions:route-message" de cette + // fenetre. Sans ce relais, ObjectiveValidationCard ne peut jamais apparaitre pour un + // utilisateur qui fait tout au vocal (constate en test reel). + function broadcastVoiceTurn(result: RouteMessageResponse): void { + for (const window of BrowserWindow.getAllWindows()) { + window.webContents.send('voice-turn-synced', result) + } + } + // Partage entre le chat texte (sessions:route-message) et la voix (voice-turn) : "learn" est // le seul cas ou une session existe reellement (nouvelle ou poursuivie), cote serveur, quel // que soit le canal d'entree (routeur d'intention, voir docs/Max_System_Prompt.md). @@ -213,6 +225,15 @@ app const afterChat = performance.now() const reply = sanitizeForSpeech(routed.reply) await handleRouteMessageResult(routed) + // La fenetre de chat texte est la seule a savoir afficher ObjectiveValidationCard (la + // confirmation finale exige un clic, pas realisable en vocal seul) : si la voix a fait + // avancer une formation, on ouvre cette fenetre et on lui transmet le tour (message + + // etat pret/brouillon) puisque son store Zustand, isole dans son propre process + // renderer, ne recoit sinon jamais rien de ce qui se passe cote vocal. + if (routed.type === 'learn') { + openChatWindow() + broadcastVoiceTurn(routed) + } const speech = await synthesizeSpeech(reply) const afterTts = performance.now() diff --git a/Max Avatar/src/preload/index.d.ts b/Max Avatar/src/preload/index.d.ts index c9aa41a..34faf86 100644 --- a/Max Avatar/src/preload/index.d.ts +++ b/Max Avatar/src/preload/index.d.ts @@ -31,6 +31,7 @@ export interface ForgeAvatarApi { onProactiveSpeech: (callback: (audio: ArrayBuffer) => void) => () => void onChatWindowClosed: (callback: () => void) => () => void onSessionStatusChanged: (callback: (status: SessionStatus) => void) => () => void + onVoiceTurnSynced: (callback: (result: RouteMessageResponse) => void) => () => void onUpdateAvailable: (callback: () => void) => () => void onUpdateReady: (callback: () => void) => () => void login: (email: string, password: string) => Promise diff --git a/Max Avatar/src/preload/index.ts b/Max Avatar/src/preload/index.ts index 0a82c7e..eb85874 100644 --- a/Max Avatar/src/preload/index.ts +++ b/Max Avatar/src/preload/index.ts @@ -58,6 +58,12 @@ const api = { ipcRenderer.on('session-status-changed', handler) return () => ipcRenderer.removeListener('session-status-changed', handler) }, + onVoiceTurnSynced: (callback: (result: RouteMessageResponse) => void): (() => void) => { + const handler = (_event: Electron.IpcRendererEvent, result: RouteMessageResponse): void => + callback(result) + ipcRenderer.on('voice-turn-synced', handler) + return () => ipcRenderer.removeListener('voice-turn-synced', handler) + }, onUpdateAvailable: (callback: () => void): (() => void) => { const handler = (): void => callback() ipcRenderer.on('update:available', handler) diff --git a/Max Avatar/src/renderer/src/components/ChatPanel.tsx b/Max Avatar/src/renderer/src/components/ChatPanel.tsx index 5f088c6..155b758 100644 --- a/Max Avatar/src/renderer/src/components/ChatPanel.tsx +++ b/Max Avatar/src/renderer/src/components/ChatPanel.tsx @@ -22,7 +22,8 @@ export default function ChatPanel(): React.JSX.Element { error, sendMessage, confirmObjective, - restart + restart, + syncVoiceTurn } = useSessionStore() const chatRef = useRef(null) @@ -33,6 +34,11 @@ export default function ChatPanel(): React.JSX.Element { } }, [messages]) + // Une formation demarree ou poursuivie au vocal n'arrive jamais ici via sendMessage() (c'est le + // main process qui appelle Max API directement pour la voix) : sans cet abonnement, la carte de + // validation ne peut jamais apparaitre pour un utilisateur qui fait tout au vocal. + useEffect(() => window.api.onVoiceTurnSynced(syncVoiceTurn), [syncVoiceTurn]) + const showObjectiveCard = objectiveReady && objectiveDraft !== null && !objectiveConfirmed return ( diff --git a/Max Avatar/src/renderer/src/stores/useSessionStore.ts b/Max Avatar/src/renderer/src/stores/useSessionStore.ts index 6c1b278..16345d5 100644 --- a/Max Avatar/src/renderer/src/stores/useSessionStore.ts +++ b/Max Avatar/src/renderer/src/stores/useSessionStore.ts @@ -1,5 +1,5 @@ import { create } from 'zustand' -import type { ObjectiveDraft, SessionStatus } from '../../../shared/session' +import type { ObjectiveDraft, RouteMessageResponse, SessionStatus } from '../../../shared/session' export interface ChatMessage { id: string @@ -21,6 +21,7 @@ interface SessionState { confirmObjective: (objective: ObjectiveDraft) => Promise restart: () => void setStatus: (status: SessionStatus) => void + syncVoiceTurn: (result: RouteMessageResponse) => void } function pushMessage( @@ -123,5 +124,21 @@ export const useSessionStore = create((set, get) => ({ // deriver son statut directement des reponses de ses propres appels API. setStatus: (status: SessionStatus): void => { set({ status }) + }, + + // Utilise uniquement par la fenetre de chat (src/renderer/src/components/ChatPanel.tsx), qui + // sinon ne voit jamais les tours de conversation inities au vocal : son store Zustand, isole + // dans son propre process renderer, ne recoit rien des appels faits par le process main pour le + // handler "voice-turn" (voir src/main/index.ts, broadcastVoiceTurn). Sans ca, + // ObjectiveValidationCard ne peut jamais apparaitre pour un utilisateur qui fait tout au vocal + // (constate en test reel). Ne pousse PAS le message utilisateur original (le main process ne le + // relaie pas ici), seulement la reponse de Max et l'etat pret/brouillon qui en resulte. + syncVoiceTurn: (result: RouteMessageResponse): void => { + set((state) => ({ + sessionId: result.sessionId ?? state.sessionId, + messages: pushMessage(state.messages, 'max', result.reply), + objectiveReady: result.ready ?? state.objectiveReady, + objectiveDraft: result.draft ?? state.objectiveDraft + })) } })) diff --git a/Max Avatar/tests/unit/components/ChatPanel.test.tsx b/Max Avatar/tests/unit/components/ChatPanel.test.tsx index 94e326a..eb6d133 100644 --- a/Max Avatar/tests/unit/components/ChatPanel.test.tsx +++ b/Max Avatar/tests/unit/components/ChatPanel.test.tsx @@ -7,12 +7,14 @@ const get = vi.fn() const routeMessage = vi.fn() const confirmObjective = vi.fn() const notifySessionEnded = vi.fn() +const onVoiceTurnSynced = vi.fn(() => () => {}) beforeEach(() => { vi.clearAllMocks() ;(window as unknown as { api: Record }).api = { sessions: { get, routeMessage, confirmObjective }, - notifySessionEnded + notifySessionEnded, + onVoiceTurnSynced } useSessionStore.getState().restart() }) @@ -59,6 +61,28 @@ describe('', () => { expect(screen.queryByLabelText('Message')).not.toBeInTheDocument() }) + it('should show the ObjectiveValidationCard from a voice-originated turn, not just a typed message', async () => { + let voiceTurnCallback: ((result: unknown) => void) | undefined + onVoiceTurnSynced.mockImplementationOnce((callback: (result: unknown) => void) => { + voiceTurnCallback = callback + return () => {} + }) + + render() + expect(screen.queryByText('Ton objectif de formation')).not.toBeInTheDocument() + + voiceTurnCallback?.({ + type: 'learn', + sessionId: 1, + reply: 'Voici le recap (dit a voix haute)', + ready: true, + draft: { subject: 's', professionalGoal: 'g', duration: '1 semaine' } + }) + + await waitFor(() => expect(screen.getByText('Ton objectif de formation')).toBeInTheDocument()) + expect(screen.getByText('Voici le recap (dit a voix haute)')).toBeInTheDocument() + }) + it('should show the store error message when a call fails', async () => { routeMessage.mockRejectedValueOnce(new Error('Max API injoignable'))