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
+67
View File
@@ -4,18 +4,26 @@ import {
NotFoundException,
} from '@nestjs/common';
import { Session, SessionObjective } from '@prisma/client';
import { OpenRouterService } from '../llm/openrouter.service';
import { PrismaService } from '../prisma/prisma.service';
import { IntentRouterAgent } from './agents/intent-router.agent';
import { ObjectiveAgent, ObjectiveTurnResult } from './agents/objective.agent';
import { RouteMessageResponseDto } from './dto/route-message-response.dto';
import { buildAgentPrompt, PERSONALITY_PROMPT } from './prompts/base.prompt';
import { SessionObjectiveResponseDto } from './dto/session-objective-response.dto';
import { SessionResponseDto } from './dto/session-response.dto';
type SessionWithObjective = Session & { objective: SessionObjective | null };
const IMPLEMENTED_STATUSES = new Set(['objective_discovery']);
@Injectable()
export class SessionsService {
constructor(
private readonly prisma: PrismaService,
private readonly objectiveAgent: ObjectiveAgent,
private readonly intentRouter: IntentRouterAgent,
private readonly llm: OpenRouterService,
) {}
async create(userId: number): Promise<SessionResponseDto> {
@@ -55,6 +63,65 @@ export class SessionsService {
};
}
/**
* Porte d'entree unique du chat (docs/Max_System_Prompt.md) : route vers l'agent de la session
* active si elle existe, sinon classifie le message (routeur d'intention) pour decider de
* demarrer une formation ou de repondre simplement en conversation. Le statut de session n'est
* jamais choisi par le LLM : seul le code decide ici de creer une session ou non.
*/
async routeIncomingMessage(
userId: number,
message: string,
): Promise<RouteMessageResponseDto> {
const activeSession = await this.prisma.session.findFirst({
where: { userId, status: { not: 'completed' } },
orderBy: { createdAt: 'desc' },
});
if (activeSession) {
if (!IMPLEMENTED_STATUSES.has(activeSession.status)) {
throw new ConflictException(
"Cette etape du pipeline n'est pas encore implementee.",
);
}
const result = await this.sendObjectiveMessage(
activeSession.id,
userId,
message,
);
return {
type: 'learn',
sessionId: activeSession.id,
reply: result.reply,
ready: result.ready,
draft: result.draft,
};
}
const intent = await this.intentRouter.analyze(userId, message);
if (intent.intent === 'chat') {
const reply = await this.llm.chat(userId, [
{ role: 'system', content: buildAgentPrompt(PERSONALITY_PROMPT) },
{ role: 'user', content: message },
]);
return { type: 'chat', reply };
}
const session = await this.prisma.session.create({ data: { userId } });
const result = await this.objectiveAgent.converse(
session.id,
userId,
message,
);
return {
type: 'learn',
sessionId: session.id,
reply: result.reply,
ready: result.ready,
draft: result.draft,
};
}
private async findOwnedSession(
id: number,
userId: number,