From e03b1e6235a22c1fac3e545b9b12e56b7f080ff8 Mon Sep 17 00:00:00 2001 From: saridsa2 Date: Fri, 17 Apr 2026 11:11:13 +0530 Subject: [PATCH] feat: structured JSON output + suggestion rules in AI system prompt Co-Authored-By: Claude Opus 4.6 (1M context) --- src/ai/ai-chat.controller.ts | 3 +++ src/config/ai.defaults.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/ai/ai-chat.controller.ts b/src/ai/ai-chat.controller.ts index d50acdf..de65de4 100644 --- a/src/ai/ai-chat.controller.ts +++ b/src/ai/ai-chat.controller.ts @@ -105,6 +105,9 @@ export class AiChatController { const callerCtx = await this.callerContext.getOrBuild(ctx.leadId, '', auth); if (callerCtx) { systemPrompt += `\n\n${this.callerContext.renderForPrompt(callerCtx)}`; + if (callerCtx.suggestionTriggers?.length) { + systemPrompt += this.callerContext.renderSuggestionsForPrompt(callerCtx.suggestionTriggers); + } } } else if (ctx?.callerPhone) { systemPrompt += `\n\nCURRENT CONTEXT:\nCaller phone: ${ctx.callerPhone}\nNew caller — no prior records.`; diff --git a/src/config/ai.defaults.ts b/src/config/ai.defaults.ts index 69f5f36..d34a948 100644 --- a/src/config/ai.defaults.ts +++ b/src/config/ai.defaults.ts @@ -125,6 +125,20 @@ RULES: 7. NEVER give medical advice, diagnosis, or treatment recommendations. 8. Format with bullet points for easy scanning. +RESPONSE FORMAT (STRICT): +You MUST respond with valid JSON in this exact format — no markdown fences, no extra text, just raw JSON: +{"message": "your response text here", "suggestions": [{"id": "s1", "type": "upsell", "title": "short title", "script": "2-3 sentence script the agent reads aloud", "priority": "high"}]} + +Response format rules: +- "message" contains your conversational response to the agent. Use plain text, no markdown. +- "suggestions" contains 0-4 contextual suggestions based on the SUGGESTION RULES section below (if present). +- Each suggestion needs a personalized "script" using the caller's name, doctor, department from the context. +- type must be one of: upsell, crosssell, retention, operational +- priority must be one of: high, medium, low +- On the first response (patient summary), always include suggestions from the rules. +- On subsequent responses, update suggestions based on conversation — remove acted-on ones, add new if relevant. +- If no suggestion rules are provided, return an empty suggestions array. + KNOWLEDGE BASE (this is real data from our system): {{knowledgeBase}}`;