fix(voice): route voice turns through the same backend pipeline as chat

Voice interactions still called OpenRouter directly from the main
process with a leftover pre-redesign system prompt ("coach de
formation vocal"), bypassing Max API's intent router and the new
PERSONALITY_PROMPT entirely - causing Max to misrepresent himself in
spoken conversation. voice-turn now calls routeMessage() (the same
POST /sessions/messages entry point the chat window uses), so voice
and text share one personality and one pipeline.

The leaked-boilerplate/degenerate-text safety checks that used to live
alongside the old direct OpenRouter call move to a focused
speechSafety.ts module, still applied to the reply before it's spoken.
getAssistantReply() and its model-fallback plumbing are deleted - that
logic now lives server-side in Max API's OpenRouterService.

A learn-intent voice command (e.g. saying "apprends-moi React out
loud") now opens the pipeline stepper and syncs session status exactly
like typing it in chat, via a handleRouteMessageResult() helper shared
between voice-turn and sessions:route-message.

Known limitations (unchanged from the text-chat intent router, now
just consistent across both channels): casual chat-intent turns have
no cross-turn memory (no pre-session history storage server-side yet),
and a voice-started session's messages don't appear in the chat
window's own transcript if opened afterwards (per-window state only).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-11-01 09:33:24 +01:00
co-authored by Claude Sonnet 5
parent 78d6547037
commit 73bc2587b8
5 changed files with 117 additions and 231 deletions
-90
View File
@@ -1,90 +0,0 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { setCurrentUserEmail, clearSession } from '../../src/main/auth/session'
vi.mock('@electron-toolkit/utils', () => ({ is: { dev: false } }))
const { getAssistantReply } = await import('../../src/main/ai/openrouter')
function okResponse(body: unknown): Response {
return { ok: true, json: () => Promise.resolve(body) } as Response
}
function textMessage(content: string): unknown {
return { choices: [{ message: { role: 'assistant', content } }] }
}
describe('getAssistantReply()', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubGlobal('fetch', vi.fn())
process.env.OPENROUTER_API_KEY = 'test-key'
setCurrentUserEmail('test@example.com')
})
afterEach(() => {
clearSession()
})
it('should throw when the API key is missing', async () => {
delete process.env.OPENROUTER_API_KEY
await expect(getAssistantReply('Bonjour')).rejects.toThrow(/OPENROUTER_API_KEY/)
})
it('should return the assistant text reply when the API responds successfully', async () => {
vi.mocked(fetch).mockResolvedValue(okResponse(textMessage('Bonjour, comment puis-je aider ?')))
const reply = await getAssistantReply('Salut Max')
expect(reply).toBe('Bonjour, comment puis-je aider ?')
})
it('should transparently fall back to the secondary model when the primary one errors out', async () => {
vi.mocked(fetch)
.mockResolvedValueOnce({
ok: false,
status: 503,
text: () => Promise.resolve('unavailable')
} as Response)
.mockResolvedValueOnce(okResponse(textMessage('Reponse du modele de secours.')))
const reply = await getAssistantReply('question')
expect(reply).toBe('Reponse du modele de secours.')
})
it('should treat a 200 response with no choices as a failure and try the other model', async () => {
vi.mocked(fetch)
.mockResolvedValueOnce(okResponse({}))
.mockResolvedValueOnce(okResponse(textMessage('Ca marche quand meme.')))
const reply = await getAssistantReply('question')
expect(reply).toBe('Ca marche quand meme.')
})
it('should throw a clear error when both models fail', async () => {
vi.mocked(fetch).mockResolvedValue({
ok: false,
status: 500,
text: () => Promise.resolve('panne totale')
} as Response)
await expect(getAssistantReply('question')).rejects.toThrow(/Aucun modele disponible/)
})
it('should replace a leaked English boilerplate reply with the safe fallback message', async () => {
vi.mocked(fetch).mockResolvedValue(
okResponse(
textMessage(
'Use the results below to formulate an answer to the user question with additional information needed'
)
)
)
const reply = await getAssistantReply('question')
expect(reply).toMatch(/bien avance avec toi/)
})
it('should replace a degenerate (non-Latin) reply with the safe fallback message', async () => {
vi.mocked(fetch).mockResolvedValue(
okResponse(textMessage('的问题是什么呢我不知道该怎么办才好呢'))
)
const reply = await getAssistantReply('question')
expect(reply).toMatch(/bien avance avec toi/)
})
})
@@ -0,0 +1,55 @@
import { describe, it, expect } from 'vitest'
import {
containsLeakedBoilerplate,
isDegenerateText,
sanitizeForSpeech,
SPEECH_FALLBACK_REPLY
} from '../../src/main/ai/speechSafety'
describe('containsLeakedBoilerplate()', () => {
it('should detect leaked English model boilerplate with no French words', () => {
expect(
containsLeakedBoilerplate(
'Use the results below to formulate an answer to the user question with additional information needed'
)
).toBe(true)
})
it('should not flag a normal French reply', () => {
expect(containsLeakedBoilerplate('Bonjour, comment puis-je aider ?')).toBe(false)
})
})
describe('isDegenerateText()', () => {
it('should detect a non-Latin corrupted reply', () => {
expect(isDegenerateText('的问题是什么呢我不知道该怎么办才好呢')).toBe(true)
})
it('should not flag a normal French reply', () => {
expect(isDegenerateText('Bonjour, comment puis-je aider ?')).toBe(false)
})
it('should not flag an empty string', () => {
expect(isDegenerateText('')).toBe(false)
})
})
describe('sanitizeForSpeech()', () => {
it('should replace a leaked English boilerplate reply with the safe fallback message', () => {
expect(
sanitizeForSpeech(
'Use the results below to formulate an answer to the user question with additional information needed'
)
).toBe(SPEECH_FALLBACK_REPLY)
})
it('should replace a degenerate (non-Latin) reply with the safe fallback message', () => {
expect(sanitizeForSpeech('的问题是什么呢我不知道该怎么办才好呢')).toBe(SPEECH_FALLBACK_REPLY)
})
it('should return the original text when it is a normal French reply', () => {
expect(sanitizeForSpeech('Bonjour, comment puis-je aider ?')).toBe(
'Bonjour, comment puis-je aider ?'
)
})
})