Progressive Context Building
The system builds understanding progressively through clarifying questions when initial context is vague.
Multi-Turn Clarification Flow
Progressive State
| Turn | Context State | Confidence | Action |
|---|---|---|---|
| 1 | { intent: "unknown" } | 0.3 | Ask: "Kellele?" |
| 2 | { recipient: "kolleeg" } | 0.6 | Ask: "Mis puhuks?" |
| 3 | { recipient: "kolleeg", occasion: "sünnipäev" } | 0.8 | Search |
Context Accumulation Logic
export async function orchestrate(options): Promise<Result> {
// Step 1: Extract from current message
const { giftContext } = await extractContext({
userMessage,
conversationState // Prior state
});
// Step 2: Fetch stored context
const { storedContext } = await fetchStoredContext({
conversationId,
giftContext
});
// Step 3: Merge stored with new
if (!giftContext.budget && storedContext?.budget) {
giftContext.budget = storedContext.budget; // Restore
}
if (!giftContext.occasion && storedContext?.occasion) {
giftContext.occasion = storedContext.occasion; // Restore
}
// ... similar for recipient, productType, category
return { giftContext, storedContext };
}
When to Ask Clarifying Questions
export function shouldAskClarifyingQuestion(
giftContext: GiftContext
): boolean {
// Low confidence OR ambiguous intent
const hasLowConfidence = (giftContext.confidence ?? 0) < 0.7;
const isAmbiguous = ['unknown', 'greeting'].includes(giftContext.intent);
if (!hasLowConfidence && !isAmbiguous) return false;
// Has meaningful signals?
const hasProductSignals = hasMeaningfulProductSignals(giftContext);
const hasGiftContext = !isGiftContextMissing(giftContext);
// Has explicit productType/category?
const hasExplicit = giftContext.productType || giftContext.category;
// ALWAYS search if explicit present
if (hasExplicit) return false;
// Ask if vague AND no signals AND no context
return (hasLowConfidence || isAmbiguous) &&
!hasProductSignals &&
!hasGiftContext;
}
Clarifying Question Generation
System Prompt Enhancement:
prompt += `
CONTEXT ENRICHMENT:
After recommendations, ask up to 2 clarifying questions:
Ask ONLY about missing:
- Budget unknown → "What's your budget?"
- Recipient unknown → "Who is this gift for?"
- Occasion unknown → "What's the occasion?"
- Age unknown → "How old is [recipient]?"
- Interests unknown → "What are [recipient]'s interests?"
Format naturally:
- "To help me give better recommendations:" + questions
Rules:
- Maximum 2 questions
- DON'T ask if already known
- Natural and conversational
`;
Real Example
Turn 1:
User: "Kingitust"
System: "Kellele soovite kingitust valida? Sõbrale, perele, või kolleegile?
Samuti aitaks teada, mis puhuks:
- Sünnipäev
- Jõulud
- Tänuavaldus
Kui teil on eelarve piirang, andke teada!"
Progressive Metrics
| Metric | Value | Notes |
|---|---|---|
| Avg Turns to Specificity | 2.3 | Vague → Specific |
| Clarification Success Rate | 89% | User provides context |
| Abandonment After Clarification | 11% | User gives up |
| Context Accumulation Accuracy | 94% | Fields correctly merged |
Related Documentation
- Refinement Detection - How refinements are detected
- Budget System - Budget handling details
- Overview - Conversational intelligence overview