feat(sessions): add unified /sessions/messages entry point

Add SessionsService#routeIncomingMessage: if the user has an active
non-completed session, route to its agent by status (only
objective_discovery is implemented so far - any other status rejects
with a clear 409 rather than failing silently); otherwise run the
intent router first and only create a session + start Agent 1 when it
detects a learning intent, replying as plain conversational Max
(PERSONALITY_PROMPT only, no session) otherwise.

Exposed as POST /sessions/messages (JWT-protected), the new single
entry point the chat window should call instead of unconditionally
creating a session on every message.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-11-01 08:57:29 +01:00
co-authored by Claude Sonnet 5
parent dc368a32f0
commit ce25d3f290
6 changed files with 237 additions and 2 deletions
+108 -1
View File
@@ -1,15 +1,19 @@
import { ConflictException, NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { OpenRouterService } from '../llm/openrouter.service';
import { PrismaService } from '../prisma/prisma.service';
import { IntentRouterAgent } from './agents/intent-router.agent';
import { ObjectiveAgent } from './agents/objective.agent';
import { SessionsService } from './sessions.service';
describe('SessionsService', () => {
let service: SessionsService;
const prisma = {
session: { create: jest.fn(), findUnique: jest.fn() },
session: { create: jest.fn(), findUnique: jest.fn(), findFirst: jest.fn() },
};
const objectiveAgent = { converse: jest.fn(), confirm: jest.fn() };
const intentRouter = { analyze: jest.fn() };
const llm = { chat: jest.fn() };
beforeEach(async () => {
jest.clearAllMocks();
@@ -18,6 +22,8 @@ describe('SessionsService', () => {
SessionsService,
{ provide: PrismaService, useValue: prisma },
{ provide: ObjectiveAgent, useValue: objectiveAgent },
{ provide: IntentRouterAgent, useValue: intentRouter },
{ provide: OpenRouterService, useValue: llm },
],
}).compile();
@@ -125,4 +131,105 @@ describe('SessionsService', () => {
expect(result.validatedAt).toEqual(new Date('2026-01-02'));
});
});
describe('routeIncomingMessage()', () => {
it('should route to the objective agent when an active objective_discovery session exists', async () => {
const activeSession = {
id: 1,
userId: 7,
status: 'objective_discovery',
objective: null,
};
prisma.session.findFirst.mockResolvedValueOnce(activeSession);
prisma.session.findUnique.mockResolvedValueOnce(activeSession);
objectiveAgent.converse.mockResolvedValueOnce({
reply: 'Quel est ton sujet ?',
ready: false,
});
const result = await service.routeIncomingMessage(7, 'Je veux apprendre');
expect(objectiveAgent.converse).toHaveBeenCalledWith(
1,
7,
'Je veux apprendre',
);
expect(result).toEqual({
type: 'learn',
sessionId: 1,
reply: 'Quel est ton sujet ?',
ready: false,
draft: undefined,
});
expect(intentRouter.analyze).not.toHaveBeenCalled();
});
it('should reject when the active session is in a status with no agent built yet', async () => {
prisma.session.findFirst.mockResolvedValueOnce({
id: 1,
userId: 7,
status: 'mapping',
});
await expect(
service.routeIncomingMessage(7, 'un message'),
).rejects.toThrow(ConflictException);
expect(objectiveAgent.converse).not.toHaveBeenCalled();
});
it('should reply conversationally without creating a session when intent is chat', async () => {
prisma.session.findFirst.mockResolvedValueOnce(null);
intentRouter.analyze.mockResolvedValueOnce({
intent: 'chat',
subject: null,
});
llm.chat.mockResolvedValueOnce('Salut, comment puis-je aider ?');
const result = await service.routeIncomingMessage(7, 'Salut ça va ?');
expect(result).toEqual({
type: 'chat',
reply: 'Salut, comment puis-je aider ?',
});
expect(prisma.session.create).not.toHaveBeenCalled();
});
it('should create a session and start Agent 1 when intent is learn', async () => {
prisma.session.findFirst.mockResolvedValueOnce(null);
intentRouter.analyze.mockResolvedValueOnce({
intent: 'learn',
subject: 'React',
});
prisma.session.create.mockResolvedValueOnce({
id: 9,
userId: 7,
status: 'objective_discovery',
});
objectiveAgent.converse.mockResolvedValueOnce({
reply: 'Quel est ton objectif avec React ?',
ready: false,
});
const result = await service.routeIncomingMessage(
7,
'Apprends-moi React',
);
expect(prisma.session.create).toHaveBeenCalledWith({
data: { userId: 7 },
});
expect(objectiveAgent.converse).toHaveBeenCalledWith(
9,
7,
'Apprends-moi React',
);
expect(result).toEqual({
type: 'learn',
sessionId: 9,
reply: 'Quel est ton objectif avec React ?',
ready: false,
draft: undefined,
});
});
});
});