feat(performance-alerts): rules-engine-driven alerts, persisted as PerformanceAlert

Phase A+B of the alerts overhaul:

- New PerformanceFactsProvider exposes agent.idleMinutes (from
  AgentSession), agent.busyMinutes, agent.totalCallsToday,
  agent.bookedCallsToday, agent.conversionPercent
- Implement EscalateActionHandler (was a stub): persists a
  PerformanceAlert row, dedupes per agent+type+IST date so a 5-min
  cron can't spam, updates value if it changes
- New PerformanceConsumer: setInterval every 5 min, reads on_schedule
  rules referencing agent.* facts, evaluates per agent, dispatches
  escalate actions
- Two starter rules in hospital-starter.json: excessive-idle (>60min)
  and low-conversion (<15% with >10 calls today). NPS deferred — no
  source signal exists yet
- New PerformanceAlertsController: GET /api/supervisor/performance-alerts
  (active list), POST /:id/dismiss, POST /dismiss-all
- Rules engine now injects EscalateActionHandler via DI so the action
  has access to PlatformGraphqlService for persistence

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-15 09:02:02 +05:30
parent 5b40f49b65
commit 8dcfa5a72f
8 changed files with 451 additions and 10 deletions

View File

@@ -20,11 +20,14 @@ export class RulesEngineService {
private readonly agentFacts = new AgentFactsProvider();
private readonly actionHandlers: Map<string, ActionHandler>;
constructor(private readonly storage: RulesStorageService) {
constructor(
private readonly storage: RulesStorageService,
private readonly escalateHandler: EscalateActionHandler,
) {
this.actionHandlers = new Map([
['score', new ScoreActionHandler()],
['assign', new AssignActionHandler()],
['escalate', new EscalateActionHandler()],
['escalate', this.escalateHandler],
]);
}