feat: team module, multi-stage Dockerfile, doctor utils, AI config overhaul

- Team module: POST /api/team/members (in-place employee creation with
  temp password + Redis cache), PUT /api/team/members/:id, GET temp
  password endpoint. Uses signUpInWorkspace — no email invites.
- Dockerfile: rewritten as multi-stage build (builder + runtime) so
  native modules compile for target arch. Fixes darwin→linux crash.
- .dockerignore: exclude dist, node_modules, .env, .git, data/
- package-lock.json: regenerated against public npmjs.org (was
  pointing at localhost:4873 Verdaccio — broke docker builds)
- Doctor utils: shared DOCTOR_VISIT_SLOTS_FRAGMENT + normalizeDoctors
  helper for visit-slot-aware queries across 6 consumers
- AI config: full admin CRUD (GET/PUT/POST reset), workspace-scoped
  setup-state with workspace ID isolation, AI prompt defaults overhaul
- Agent config: camelCase field fix for SDK-synced workspaces
- Session service: workspace-scoped Redis key prefixing for setup state
- Recordings/supervisor/widget services: updated to use doctor-utils
  shared fragments instead of inline visitingHours queries

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 08:37:58 +05:30
parent eacfce6970
commit 695f119c2b
25 changed files with 2756 additions and 1936 deletions

View File

@@ -7,6 +7,7 @@ import { z } from 'zod';
import { PlatformGraphqlService } from '../platform/platform-graphql.service';
import { createAiModel, isAiConfigured } from './ai-provider';
import { AiConfigService } from '../config/ai-config.service';
import { DOCTOR_VISIT_SLOTS_FRAGMENT, normalizeDoctors } from '../shared/doctor-utils';
type ChatRequest = {
message: string;
@@ -126,7 +127,13 @@ export class AiChatController {
undefined, auth,
),
platformService.queryWithAuth<any>(
`{ agents(first: 20) { edges { node { id name ozonetelagentid npsscore maxidleminutes minnpsthreshold minconversionpercent } } } }`,
// Field names are label-derived camelCase on the
// current platform schema. The legacy lowercase
// names (ozonetelagentid etc.) only still exist on
// staging workspaces that were synced from an
// older SDK. See agent-config.service.ts for the
// canonical explanation.
`{ agents(first: 20) { edges { node { id name ozonetelAgentId npsScore maxIdleMinutes minNpsThreshold minConversion } } } }`,
undefined, auth,
),
platformService.queryWithAuth<any>(
@@ -143,7 +150,7 @@ export class AiChatController {
const agentMetrics = agents
.filter((a: any) => !agentName || a.name.toLowerCase().includes(agentName.toLowerCase()))
.map((agent: any) => {
const agentCalls = calls.filter((c: any) => c.agentName === agent.name || c.agentName === agent.ozonetelagentid);
const agentCalls = calls.filter((c: any) => c.agentName === agent.name || c.agentName === agent.ozonetelAgentId);
const totalCalls = agentCalls.length;
const missed = agentCalls.filter((c: any) => c.callStatus === 'MISSED').length;
const completed = agentCalls.filter((c: any) => c.callStatus === 'COMPLETED').length;
@@ -162,12 +169,12 @@ export class AiChatController {
conversionRate: `${conversionRate}%`,
assignedLeads: agentLeads.length,
pendingFollowUps,
npsScore: agent.npsscore,
maxIdleMinutes: agent.maxidleminutes,
minNpsThreshold: agent.minnpsthreshold,
minConversionPercent: agent.minconversionpercent,
belowNpsThreshold: agent.minnpsthreshold && (agent.npsscore ?? 100) < agent.minnpsthreshold,
belowConversionThreshold: agent.minconversionpercent && conversionRate < agent.minconversionpercent,
npsScore: agent.npsScore,
maxIdleMinutes: agent.maxIdleMinutes,
minNpsThreshold: agent.minNpsThreshold,
minConversionPercent: agent.minConversion,
belowNpsThreshold: agent.minNpsThreshold && (agent.npsScore ?? 100) < agent.minNpsThreshold,
belowConversionThreshold: agent.minConversion && conversionRate < agent.minConversion,
};
});
@@ -350,13 +357,13 @@ export class AiChatController {
const data = await platformService.queryWithAuth<any>(
`{ doctors(first: 10) { edges { node {
id fullName { firstName lastName }
department specialty visitingHours
department specialty
consultationFeeNew { amountMicros currencyCode }
clinic { clinicName }
${DOCTOR_VISIT_SLOTS_FRAGMENT}
} } } }`,
undefined, auth,
);
const doctors = data.doctors.edges.map((e: any) => e.node);
const doctors = normalizeDoctors(data.doctors.edges.map((e: any) => e.node));
// Strip "Dr." prefix and search flexibly
const search = doctorName.toLowerCase().replace(/^dr\.?\s*/i, '').trim();
const searchWords = search.split(/\s+/);
@@ -562,25 +569,28 @@ export class AiChatController {
try {
const docData = await this.platform.queryWithAuth<any>(
`{ doctors(first: 20) { edges { node {
fullName { firstName lastName } department specialty visitingHours
id fullName { firstName lastName } department specialty
consultationFeeNew { amountMicros currencyCode }
clinic { clinicName }
${DOCTOR_VISIT_SLOTS_FRAGMENT}
} } } }`,
undefined, auth,
);
const doctors = docData.doctors.edges.map((e: any) => e.node);
const doctors = normalizeDoctors(docData.doctors.edges.map((e: any) => e.node));
if (doctors.length) {
sections.push('\n## DOCTORS');
for (const d of doctors) {
const name = `Dr. ${d.fullName?.firstName ?? ''} ${d.fullName?.lastName ?? ''}`.trim();
const fee = d.consultationFeeNew ? `${d.consultationFeeNew.amountMicros / 1_000_000}` : '';
const clinic = d.clinic?.clinicName ?? '';
// List ALL clinics this doctor visits in the KB so
// the AI can answer questions like "where can I see
// Dr. X" without needing a follow-up tool call.
const clinics = d.clinics.map((c) => c.clinicName).join(', ');
sections.push(`### ${name}`);
sections.push(` Department: ${d.department ?? 'N/A'}`);
sections.push(` Specialty: ${d.specialty ?? 'N/A'}`);
if (d.visitingHours) sections.push(` Hours: ${d.visitingHours}`);
if (fee) sections.push(` Consultation fee: ${fee}`);
if (clinic) sections.push(` Clinic: ${clinic}`);
if (clinics) sections.push(` Clinics: ${clinics}`);
}
}
} catch (err) {
@@ -651,24 +661,15 @@ export class AiChatController {
}
private buildSupervisorSystemPrompt(): string {
return `You are an AI assistant for supervisors at Global Hospital's call center (Helix Engage).
You help supervisors monitor team performance, identify issues, and make data-driven decisions.
return this.aiConfig.renderPrompt('supervisorChat', {
hospitalName: this.getHospitalName(),
});
}
## YOUR CAPABILITIES
You have access to tools that query real-time data:
- **Agent performance**: call counts, conversion rates, NPS scores, idle time, pending follow-ups
- **Campaign stats**: lead counts, conversion rates per campaign, platform breakdown
- **Call summary**: total calls, inbound/outbound split, missed call rate, disposition breakdown
- **SLA breaches**: missed calls that haven't been called back within the SLA threshold
## RULES
1. ALWAYS use tools to fetch data before answering. NEVER guess or fabricate performance numbers.
2. Be specific — include actual numbers from the tool response, not vague qualifiers.
3. When comparing agents, use their configured thresholds (minConversionPercent, minNpsThreshold, maxIdleMinutes) and team averages. Let the data determine who is underperforming — do not assume.
4. Be concise — supervisors want quick answers. Use bullet points.
5. When recommending actions, ground them in the data returned by tools.
6. If asked about trends, use the call summary tool with different periods.
7. Do not use any agent name in a negative context unless the data explicitly supports it.`;
// Best-effort hospital name lookup for the AI prompts. Falls back
// to a generic label so prompt rendering never throws.
private getHospitalName(): string {
return process.env.HOSPITAL_NAME ?? 'the hospital';
}
private buildRulesSystemPrompt(currentConfig: any): string {
@@ -718,25 +719,10 @@ ${configJson}
}
private buildSystemPrompt(kb: string): string {
return `You are an AI assistant for call center agents at Global Hospital, Bangalore.
You help agents answer questions about patients, doctors, appointments, clinics, and hospital services during live calls.
IMPORTANT — ANSWER FROM KNOWLEDGE BASE FIRST:
The knowledge base below contains REAL clinic locations, timings, doctor details, health packages, and insurance partners.
When asked about clinic timings, locations, doctor availability, packages, or insurance — ALWAYS check the knowledge base FIRST before saying you don't know.
Example: "What are the Koramangala timings?" → Look for "Koramangala" in the Clinics section below.
RULES:
1. For patient-specific questions (history, appointments, calls), use the lookup tools. NEVER guess patient data.
2. For doctor details beyond what's in the KB, use the lookup_doctor tool.
3. For clinic info, timings, packages, insurance → answer directly from the knowledge base below.
4. If you truly cannot find the answer in the KB or via tools, say "I couldn't find that in our system."
5. Be concise — agents are on live calls. Under 100 words unless asked for detail.
6. NEVER give medical advice, diagnosis, or treatment recommendations.
7. Format with bullet points for easy scanning.
KNOWLEDGE BASE (this is real data from our system):
${kb}`;
return this.aiConfig.renderPrompt('ccAgentHelper', {
hospitalName: this.getHospitalName(),
knowledgeBase: kb,
});
}
private async chatWithTools(userMessage: string, auth: string) {
@@ -850,16 +836,15 @@ ${kb}`;
`{ doctors(first: 10) { edges { node {
id name fullName { firstName lastName }
department specialty qualifications yearsOfExperience
visitingHours
consultationFeeNew { amountMicros currencyCode }
consultationFeeFollowUp { amountMicros currencyCode }
active registrationNumber
clinic { id name clinicName }
${DOCTOR_VISIT_SLOTS_FRAGMENT}
} } } }`,
undefined, auth,
);
const doctors = data.doctors.edges.map((e: any) => e.node);
const doctors = normalizeDoctors(data.doctors.edges.map((e: any) => e.node));
const search = doctorName.toLowerCase();
const matched = doctors.filter((d: any) => {
const full = `${d.fullName?.firstName ?? ''} ${d.fullName?.lastName ?? ''} ${d.name ?? ''}`.toLowerCase();
@@ -872,7 +857,13 @@ ${kb}`;
found: true,
doctors: matched.map((d: any) => ({
...d,
clinicName: d.clinic?.clinicName ?? d.clinic?.name ?? 'N/A',
// Multi-clinic doctors show as
// "Koramangala / Indiranagar" so the
// model has the full picture without
// a follow-up tool call.
clinicName: d.clinics.length > 0
? d.clinics.map((c: { clinicName: string }) => c.clinicName).join(' / ')
: 'N/A',
feeNewFormatted: d.consultationFeeNew ? `${d.consultationFeeNew.amountMicros / 1_000_000}` : 'N/A',
feeFollowUpFormatted: d.consultationFeeFollowUp ? `${d.consultationFeeFollowUp.amountMicros / 1_000_000}` : 'N/A',
})),
@@ -896,13 +887,13 @@ ${kb}`;
try {
const doctors = await this.platform.queryWithAuth<any>(
`{ doctors(first: 10) { edges { node {
name fullName { firstName lastName } department specialty visitingHours
id name fullName { firstName lastName } department specialty
consultationFeeNew { amountMicros currencyCode }
clinic { name clinicName }
${DOCTOR_VISIT_SLOTS_FRAGMENT}
} } } }`,
undefined, auth,
);
const docs = doctors.doctors.edges.map((e: any) => e.node);
const docs = normalizeDoctors(doctors.doctors.edges.map((e: any) => e.node));
const l = msg.toLowerCase();
const matchedDoc = docs.find((d: any) => {
@@ -912,7 +903,7 @@ ${kb}`;
if (matchedDoc) {
const fee = matchedDoc.consultationFeeNew ? `${matchedDoc.consultationFeeNew.amountMicros / 1_000_000}` : '';
const clinic = matchedDoc.clinic?.clinicName ?? '';
return `Dr. ${matchedDoc.fullName?.lastName ?? matchedDoc.name} (${matchedDoc.department ?? matchedDoc.specialty}): ${matchedDoc.visitingHours ?? 'hours not set'}${clinic ? ` at ${clinic}` : ''}${fee ? `. Fee: ${fee}` : ''}.`;
return `Dr. ${matchedDoc.fullName?.lastName ?? matchedDoc.name} (${matchedDoc.department ?? matchedDoc.specialty}): ${matchedDoc.visitingHours || 'hours not set'}${clinic ? ` at ${clinic}` : ''}${fee ? `. Fee: ${fee}` : ''}.`;
}
if (l.includes('doctor') || l.includes('available')) {