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>
89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
export interface ObjectiveDraft {
|
|
subject: string;
|
|
professionalGoal: string;
|
|
duration: string;
|
|
}
|
|
|
|
export interface GuardrailFailure {
|
|
field: 'subject' | 'professionalGoal' | 'duration';
|
|
reason: string;
|
|
}
|
|
|
|
const MIN_SUBJECT_LENGTH = 10;
|
|
const MIN_GOAL_LENGTH = 20;
|
|
const MIN_DURATION_HOURS = 1;
|
|
const MAX_DURATION_HOURS = 12 * 7 * 24;
|
|
|
|
const DURATION_UNIT_HOURS = new Map<string, number>([
|
|
['heure', 1],
|
|
['heures', 1],
|
|
['h', 1],
|
|
['jour', 24],
|
|
['jours', 24],
|
|
['j', 24],
|
|
['semaine', 7 * 24],
|
|
['semaines', 7 * 24],
|
|
['mois', 30 * 24],
|
|
]);
|
|
|
|
/**
|
|
* Parse une duree en langage libre ("2 semaines", "10h", "1 mois") en heures. Retourne null si
|
|
* le format n'est pas reconnu, pour que l'appelant puisse redemander une reformulation plutot
|
|
* que de faire confiance a un texte non structure.
|
|
*/
|
|
export function parseDurationHours(duration: string): number | null {
|
|
const normalized = duration.trim().toLowerCase();
|
|
const separatorIndex = normalized.search(/[a-zéû]/);
|
|
if (separatorIndex <= 0) return null;
|
|
|
|
const amount = Number.parseFloat(
|
|
normalized.slice(0, separatorIndex).trim().replace(',', '.'),
|
|
);
|
|
const unit = normalized.slice(separatorIndex).trim();
|
|
const hoursPerUnit = DURATION_UNIT_HOURS.get(unit);
|
|
if (hoursPerUnit === undefined || Number.isNaN(amount)) return null;
|
|
return amount * hoursPerUnit;
|
|
}
|
|
|
|
/**
|
|
* Garde-fous de niveau 1 (regles codees, voir CLAUDE.md section 2 / docs/Max Refonte Coach
|
|
* Freelance.md) : longueur/format uniquement, aucun appel LLM. Renvoie la liste des champs en
|
|
* echec, vide si l'objectif est recevable.
|
|
*/
|
|
export function checkLevel1Guardrails(
|
|
draft: ObjectiveDraft,
|
|
): GuardrailFailure[] {
|
|
const failures: GuardrailFailure[] = [];
|
|
|
|
const subject = draft.subject.trim();
|
|
if (subject.length < MIN_SUBJECT_LENGTH || !subject.includes(' ')) {
|
|
failures.push({
|
|
field: 'subject',
|
|
reason: `Le sujet doit faire au moins ${MIN_SUBJECT_LENGTH} caracteres et ne pas etre un terme generique isole.`,
|
|
});
|
|
}
|
|
|
|
const professionalGoal = draft.professionalGoal.trim();
|
|
if (professionalGoal.length < MIN_GOAL_LENGTH) {
|
|
failures.push({
|
|
field: 'professionalGoal',
|
|
reason: `Le but professionnel doit faire au moins ${MIN_GOAL_LENGTH} caracteres.`,
|
|
});
|
|
}
|
|
|
|
const durationHours = parseDurationHours(draft.duration);
|
|
if (
|
|
durationHours === null ||
|
|
durationHours < MIN_DURATION_HOURS ||
|
|
durationHours > MAX_DURATION_HOURS
|
|
) {
|
|
failures.push({
|
|
field: 'duration',
|
|
reason:
|
|
'La duree doit etre comprise entre 1 heure et 12 semaines (ex: "2 semaines", "10 heures").',
|
|
});
|
|
}
|
|
|
|
return failures;
|
|
}
|