feat(sessions): route chat messages through the intent-aware backend entry point

Replace the chat window's unconditional create-session-then-send flow
with a single call to the new POST /sessions/messages endpoint: a
plain "salut ça va ?" no longer starts a fake training session, since
the backend's intent router decides that now.

Add sessions.routeMessage to the IPC/preload surface and the sessions
HTTP client. useSessionStore.sendMessage only sets sessionId/status
(fetched fresh via sessions:get) when the backend actually reports
type: "learn". The stepper-window-open + status-broadcast trigger in
main/index.ts moves from sessions:create's handler to
sessions:route-message's, since session creation now happens inside
the backend's routing logic rather than via an explicit prior call.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-11-01 09:02:39 +01:00
co-authored by Claude Sonnet 5
parent ce25d3f290
commit 7179743d4e
9 changed files with 160 additions and 44 deletions
@@ -1,56 +1,89 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { useSessionStore } from '../../../../src/renderer/src/stores/useSessionStore'
const create = vi.fn()
const get = vi.fn()
const sendObjectiveMessage = vi.fn()
const routeMessage = vi.fn()
const confirmObjective = vi.fn()
const notifySessionEnded = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
;(window as unknown as { api: Record<string, unknown> }).api = {
sessions: { create, get, sendObjectiveMessage, confirmObjective },
sessions: { get, routeMessage, confirmObjective },
notifySessionEnded
}
useSessionStore.getState().restart()
})
describe('useSessionStore', () => {
it('should create a session on the first message and send it there', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'Quel est ton sujet ?', ready: false })
it('should not create a session when the backend classifies the message as chat', async () => {
routeMessage.mockResolvedValueOnce({ type: 'chat', reply: 'Salut, comment puis-je aider ?' })
await useSessionStore.getState().sendMessage('Je veux apprendre la facturation')
await useSessionStore.getState().sendMessage('Salut ça va ?')
expect(create).toHaveBeenCalledOnce()
expect(sendObjectiveMessage).toHaveBeenCalledWith(1, 'Je veux apprendre la facturation')
expect(routeMessage).toHaveBeenCalledWith('Salut ça va ?')
expect(get).not.toHaveBeenCalled()
const state = useSessionStore.getState()
expect(state.sessionId).toBe(1)
expect(state.status).toBe('objective_discovery')
expect(state.sessionId).toBeNull()
expect(state.messages.map((m) => m.role)).toEqual(['user', 'max'])
expect(state.loading).toBe(false)
})
it('should reuse the existing session id on subsequent messages', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValue({ reply: 'ok', ready: false })
it('should set the session id and fetch the fresh status when the backend starts a training session', async () => {
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Quel est ton sujet ?',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('Je veux apprendre la facturation')
expect(get).toHaveBeenCalledWith(1)
const state = useSessionStore.getState()
expect(state.sessionId).toBe(1)
expect(state.status).toBe('objective_discovery')
expect(state.messages.map((m) => m.role)).toEqual(['user', 'max'])
})
it('should reuse the same session id on subsequent messages of an ongoing training', async () => {
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Quel est ton sujet ?',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('premier message')
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Et ton but professionnel ?',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('second message')
expect(create).toHaveBeenCalledOnce()
expect(sendObjectiveMessage).toHaveBeenCalledTimes(2)
expect(routeMessage).toHaveBeenCalledTimes(2)
expect(useSessionStore.getState().sessionId).toBe(1)
})
it('should store the draft and ready flag once the agent is ready to confirm', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
const draft = {
subject: 'Facturation freelance',
professionalGoal: 'Facturer sans erreur',
duration: '2 semaines'
}
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'Voici le recap', ready: true, draft })
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Voici le recap',
ready: true,
draft
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('message')
@@ -60,7 +93,7 @@ describe('useSessionStore', () => {
})
it('should set an error and stop loading when sending a message fails', async () => {
create.mockRejectedValueOnce(new Error('Max API injoignable'))
routeMessage.mockRejectedValueOnce(new Error('Max API injoignable'))
await useSessionStore.getState().sendMessage('message')
@@ -70,19 +103,18 @@ describe('useSessionStore', () => {
})
it('should confirm the objective and refresh the session status', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Recap',
ready: true,
draft: { subject: 's', professionalGoal: 'g', duration: '1 semaine' }
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('message')
confirmObjective.mockResolvedValueOnce({})
get.mockResolvedValueOnce({ id: 1, status: 'objective_validated' })
;(window as unknown as { api: Record<string, unknown> }).api = {
sessions: { create, get, sendObjectiveMessage, confirmObjective }
}
await useSessionStore
.getState()
@@ -104,8 +136,13 @@ describe('useSessionStore', () => {
})
it('should reset all state on restart and notify the main process', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'ok', ready: false })
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'ok',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('message')
notifySessionEnded.mockClear()