feat(sessions): implement Agent 1 (Objectif) of the coach pipeline

Add the sessions module (create/fetch session, Agent 1 conversation and
confirmation endpoints, all JWT-protected) and a reusable OpenRouter
client with DeepSeek/Qwen fallback + backoff on rate-limit/timeout.

Agent 1 runs a Socratic dialogue to capture the 3 mandatory pillars
(subject, professional goal, duration) and enforces two guardrail
levels before any explicit user confirmation: level 1 is hard-coded
length/format rules (objective-guardrails.ts), level 2 is an LLM
coherence score across 3 dimensions (reject below 0.6). The objective
is only persisted to session_objective, and the session only moves to
objective_validated, once the user explicitly confirms via
POST /sessions/:id/objective/confirm - never on the LLM's say-so alone.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-10-31 20:16:03 +01:00
co-authored by Claude Sonnet 5
parent b321dff3bd
commit bae5ace70c
19 changed files with 1270 additions and 0 deletions
@@ -0,0 +1,128 @@
import { ConflictException, NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../prisma/prisma.service';
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() },
};
const objectiveAgent = { converse: jest.fn(), confirm: jest.fn() };
beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
providers: [
SessionsService,
{ provide: PrismaService, useValue: prisma },
{ provide: ObjectiveAgent, useValue: objectiveAgent },
],
}).compile();
service = module.get(SessionsService);
});
describe('create()', () => {
it('should create a session in objective_discovery status', async () => {
prisma.session.create.mockResolvedValueOnce({
id: 1,
userId: 7,
status: 'objective_discovery',
createdAt: new Date('2026-01-01'),
updatedAt: new Date('2026-01-01'),
});
const result = await service.create(7);
expect(prisma.session.create).toHaveBeenCalledWith({
data: { userId: 7 },
});
expect(result.status).toBe('objective_discovery');
expect(result.objective).toBeUndefined();
});
});
describe('findOne()', () => {
it('should throw NotFoundException when the session does not belong to the user', async () => {
prisma.session.findUnique.mockResolvedValueOnce({
id: 1,
userId: 2,
objective: null,
});
await expect(service.findOne(1, 99)).rejects.toThrow(NotFoundException);
});
it('should throw NotFoundException when the session does not exist', async () => {
prisma.session.findUnique.mockResolvedValueOnce(null);
await expect(service.findOne(1, 99)).rejects.toThrow(NotFoundException);
});
});
describe('sendObjectiveMessage()', () => {
it('should delegate to the objective agent when the session is still in discovery', async () => {
prisma.session.findUnique.mockResolvedValueOnce({
id: 1,
userId: 7,
status: 'objective_discovery',
objective: null,
});
objectiveAgent.converse.mockResolvedValueOnce({
reply: 'Quel est ton sujet ?',
ready: false,
});
const result = await service.sendObjectiveMessage(
1,
7,
'Je veux apprendre',
);
expect(objectiveAgent.converse).toHaveBeenCalledWith(
1,
7,
'Je veux apprendre',
);
expect(result.ready).toBe(false);
});
it('should reject when the objective is already validated for this session', async () => {
prisma.session.findUnique.mockResolvedValueOnce({
id: 1,
userId: 7,
status: 'objective_validated',
objective: { id: 1 },
});
await expect(
service.sendObjectiveMessage(1, 7, 'Un message'),
).rejects.toThrow(ConflictException);
expect(objectiveAgent.converse).not.toHaveBeenCalled();
});
});
describe('confirmObjective()', () => {
it('should map the confirmed objective to a response DTO', async () => {
prisma.session.findUnique.mockResolvedValueOnce({
id: 1,
userId: 7,
status: 'objective_discovery',
objective: null,
});
objectiveAgent.confirm.mockResolvedValueOnce({
id: 5,
sessionId: 1,
subject: 'Facturation freelance',
professionalGoal: 'Facturer sans erreur',
duration: '2 semaines',
validatedAt: new Date('2026-01-02'),
});
const result = await service.confirmObjective(1, 7);
expect(result.id).toBe(5);
expect(result.validatedAt).toEqual(new Date('2026-01-02'));
});
});
});