Skip to main content

Progressive Context Building

The system builds understanding progressively through clarifying questions when initial context is vague.

Multi-Turn Clarification Flow

Progressive State

TurnContext StateConfidenceAction
1{ intent: "unknown" }0.3Ask: "Kellele?"
2{ recipient: "kolleeg" }0.6Ask: "Mis puhuks?"
3{ recipient: "kolleeg", occasion: "sünnipäev" }0.8Search

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) &lt; 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

MetricValueNotes
Avg Turns to Specificity2.3Vague → Specific
Clarification Success Rate89%User provides context
Abandonment After Clarification11%User gives up
Context Accumulation Accuracy94%Fields correctly merged