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
+101
View File
@@ -0,0 +1,101 @@
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Session, SessionObjective } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { ObjectiveAgent, ObjectiveTurnResult } from './agents/objective.agent';
import { SessionObjectiveResponseDto } from './dto/session-objective-response.dto';
import { SessionResponseDto } from './dto/session-response.dto';
type SessionWithObjective = Session & { objective: SessionObjective | null };
@Injectable()
export class SessionsService {
constructor(
private readonly prisma: PrismaService,
private readonly objectiveAgent: ObjectiveAgent,
) {}
async create(userId: number): Promise<SessionResponseDto> {
const session = await this.prisma.session.create({ data: { userId } });
return this.toResponse({ ...session, objective: null });
}
async findOne(id: number, userId: number): Promise<SessionResponseDto> {
const session = await this.findOwnedSession(id, userId);
return this.toResponse(session);
}
async sendObjectiveMessage(
sessionId: number,
userId: number,
message: string,
): Promise<ObjectiveTurnResult> {
const session = await this.findOwnedSession(sessionId, userId);
this.assertObjectiveDiscoveryInProgress(session);
return this.objectiveAgent.converse(sessionId, userId, message);
}
async confirmObjective(
sessionId: number,
userId: number,
): Promise<SessionObjectiveResponseDto> {
const session = await this.findOwnedSession(sessionId, userId);
this.assertObjectiveDiscoveryInProgress(session);
const objective = await this.objectiveAgent.confirm(sessionId, userId);
return {
id: objective.id,
sessionId: objective.sessionId,
subject: objective.subject,
professionalGoal: objective.professionalGoal,
duration: objective.duration,
validatedAt: objective.validatedAt ?? new Date(),
};
}
private async findOwnedSession(
id: number,
userId: number,
): Promise<SessionWithObjective> {
const session = await this.prisma.session.findUnique({
where: { id },
include: { objective: true },
});
if (!session || session.userId !== userId) {
throw new NotFoundException(`Session ${id} introuvable`);
}
return session;
}
private assertObjectiveDiscoveryInProgress(
session: SessionWithObjective,
): void {
if (session.status !== 'objective_discovery') {
throw new ConflictException(
"L'objectif de cette session est deja valide ou cette session n'en est plus a cette etape.",
);
}
}
private toResponse(session: SessionWithObjective): SessionResponseDto {
return {
id: session.id,
status: session.status,
createdAt: session.createdAt,
updatedAt: session.updatedAt,
objective: session.objective
? {
id: session.objective.id,
sessionId: session.objective.sessionId,
subject: session.objective.subject,
professionalGoal: session.objective.professionalGoal,
duration: session.objective.duration,
validatedAt:
session.objective.validatedAt ?? session.objective.createdAt,
}
: undefined,
};
}
}