Spaces:
Build error
Build error
File size: 16,597 Bytes
6a059d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | /**
* AmaniQuery v2 Response Display Component
*
* Displays structured JSON responses with persona-specific theming:
* - wanjiku: Warm orange/green (Kenyan flag vibes)
* - wakili: Navy blue + gold (legal professional)
* - mwanahabari: Neutral grey + data visualization
*/
import React, { useState } from 'react';
import { ChevronDown, ChevronUp, ExternalLink, BookOpen, TrendingUp, Users } from 'lucide-react';
import { cn } from "@/lib/utils";
import { ThinkingProcess } from './ThinkingProcess';
import { FactCheckAlert } from './FactCheckAlert';
// ============================================================================
// TYPES
// ============================================================================
export type QueryType = 'public_interest' | 'legal' | 'research';
export interface AmaniQueryResponse {
query_type: QueryType;
language_detected: string;
response: {
summary_card: {
title: string;
content: string;
};
detailed_breakdown: {
points: string[];
};
kenyan_context: {
impact: string;
related_topic: string | null;
};
citations: Array<{
source: string;
url: string;
quote: string | null;
}>;
};
follow_up_suggestions: string[];
metadata?: {
reasoning_path?: {
query: string;
thoughts: Array<{
step: number;
action: string;
observation: string;
reasoning: string;
duration_ms?: number;
confidence?: number;
}>;
total_duration_ms: number;
final_conclusion: string;
};
quality_issues?: string[];
[key: string]: unknown;
};
}
interface AmaniQueryResponseProps {
data: AmaniQueryResponse;
className?: string;
onFollowUpClick?: (suggestion: string) => void;
}
// ============================================================================
// THEME CONFIGURATION
// ============================================================================
const PERSONA_THEMES = {
public_interest: {
name: 'wanjiku',
classes: {
container: 'bg-orange-50/50 dark:bg-orange-950/20',
card: 'bg-white dark:bg-gray-900 border-orange-100 dark:border-orange-900/50',
summaryGradient: 'from-orange-600 to-green-600',
primaryText: 'text-orange-900 dark:text-orange-100',
secondaryText: 'text-orange-700 dark:text-orange-300',
accentBg: 'bg-orange-500',
accentText: 'text-orange-600 dark:text-orange-400',
border: 'border-orange-200 dark:border-orange-800',
contextBorder: 'border-green-600 dark:border-green-500',
iconColor: 'text-green-600 dark:text-green-500',
buttonHover: 'hover:bg-orange-100 dark:hover:bg-orange-900/50'
},
icon: 'π°πͺ',
summaryTitle: 'Kwa Ufupi',
contextIcon: 'π' // Matatu
},
legal: {
name: 'wakili',
classes: {
container: 'bg-indigo-50/50 dark:bg-indigo-950/20',
card: 'bg-white dark:bg-gray-900 border-indigo-100 dark:border-indigo-900/50',
summaryGradient: 'from-indigo-700 to-blue-900',
primaryText: 'text-indigo-900 dark:text-indigo-100',
secondaryText: 'text-indigo-700 dark:text-indigo-300',
accentBg: 'bg-indigo-500',
accentText: 'text-indigo-600 dark:text-indigo-400',
border: 'border-indigo-200 dark:border-indigo-800',
contextBorder: 'border-amber-400 dark:border-amber-500',
iconColor: 'text-amber-500 dark:text-amber-400',
buttonHover: 'hover:bg-indigo-100 dark:hover:bg-indigo-900/50'
},
icon: 'βοΈ',
summaryTitle: 'Legal Summary',
contextIcon: 'π'
},
research: {
name: 'mwanahabari',
classes: {
container: 'bg-slate-50/50 dark:bg-slate-950/20',
card: 'bg-white dark:bg-gray-900 border-slate-100 dark:border-slate-800',
summaryGradient: 'from-slate-700 to-teal-700',
primaryText: 'text-slate-900 dark:text-slate-100',
secondaryText: 'text-slate-700 dark:text-slate-300',
accentBg: 'bg-teal-500',
accentText: 'text-teal-600 dark:text-teal-400',
border: 'border-slate-200 dark:border-slate-700',
contextBorder: 'border-teal-500 dark:border-teal-400',
iconColor: 'text-teal-600 dark:text-teal-500',
buttonHover: 'hover:bg-slate-100 dark:hover:bg-slate-800'
},
icon: 'π',
summaryTitle: 'The Bottom Line',
contextIcon: 'π'
}
};
// ============================================================================
// KENYAN CONTEXT ICONS
// ============================================================================
const KENYAN_CONTEXT_ICONS: Record<string, string> = {
'transport': 'π', // Matatu
'money': 'π°',
'government': 'ποΈ',
'food': 'π½', // Maize/ugali
'housing': 'ποΈ',
'health': 'π₯',
'education': 'π',
'default': 'π°πͺ'
};
const getContextIcon = (impact: string): string => {
const lowercaseImpact = impact.toLowerCase();
if (lowercaseImpact.includes('transport') || lowercaseImpact.includes('matatu') || lowercaseImpact.includes('parking')) {
return KENYAN_CONTEXT_ICONS.transport;
}
if (lowercaseImpact.includes('money') || lowercaseImpact.includes('fee') || lowercaseImpact.includes('cost')) {
return KENYAN_CONTEXT_ICONS.money;
}
if (lowercaseImpact.includes('food') || lowercaseImpact.includes('mama mboga')) {
return KENYAN_CONTEXT_ICONS.food;
}
if (lowercaseImpact.includes('housing') || lowercaseImpact.includes('rent')) {
return KENYAN_CONTEXT_ICONS.housing;
}
return KENYAN_CONTEXT_ICONS.default;
};
// ============================================================================
// MAIN COMPONENT
// ============================================================================
export const AmaniQueryResponse: React.FC<AmaniQueryResponseProps> = ({ data, className = '', onFollowUpClick }) => {
const [citationsExpanded, setCitationsExpanded] = useState(false);
const [detailsExpanded, setDetailsExpanded] = useState(true);
const theme = PERSONA_THEMES[data.query_type];
const contextIcon = getContextIcon(data.response.kenyan_context.impact);
return (
<div className={cn(
"rounded-2xl p-5 max-w-3xl mx-auto font-sans transition-colors duration-200",
theme.classes.container,
className
)}>
{/* THINKING PROCESS - Display if available */}
{data.metadata?.reasoning_path && (
<div className="mb-6">
<ThinkingProcess
reasoning={data.metadata.reasoning_path}
className="bg-white/50 dark:bg-black/20 backdrop-blur-sm"
/>
</div>
)}
{/* FACT CHECK ALERT - Display if issues found */}
{data.metadata?.quality_issues && Array.isArray(data.metadata.quality_issues) && data.metadata.quality_issues.length > 0 && (
<FactCheckAlert issues={data.metadata.quality_issues as string[]} />
)}
{/* SUMMARY CARD - HUGE AND PROMINENT */}
<SummaryCard
title={data.response.summary_card.title}
content={data.response.summary_card.content}
theme={theme}
/>
{/* DETAILED BREAKDOWN */}
<DetailedBreakdown
points={data.response.detailed_breakdown.points}
theme={theme}
isExpanded={detailsExpanded}
onToggle={() => setDetailsExpanded(!detailsExpanded)}
/>
{/* KENYAN CONTEXT */}
<KenyanContext
impact={data.response.kenyan_context.impact}
relatedTopic={data.response.kenyan_context.related_topic}
icon={contextIcon}
theme={theme}
/>
{/* CITATIONS */}
<Citations
citations={data.response.citations}
theme={theme}
isExpanded={citationsExpanded}
onToggle={() => setCitationsExpanded(!citationsExpanded)}
/>
{/* FOLLOW-UP SUGGESTIONS */}
<FollowUpSuggestions
suggestions={data.follow_up_suggestions}
theme={theme}
onFollowUpClick={onFollowUpClick}
/>
</div>
);
};
// ============================================================================
// SUB-COMPONENTS
// ============================================================================
interface ThemeProps {
theme: typeof PERSONA_THEMES[keyof typeof PERSONA_THEMES];
}
// SUMMARY CARD - TikTok-style prominence
const SummaryCard: React.FC<{ title: string; content: string } & ThemeProps> = ({
title,
content,
theme
}) => {
return (
<div className={cn(
"relative overflow-hidden rounded-2xl p-8 mb-6 shadow-lg text-white",
`bg-gradient-to-br ${theme.classes.summaryGradient}`
)}>
{/* Background pattern */}
<div className="absolute top-0 right-0 text-[120px] opacity-10 leading-none pointer-events-none select-none">
{theme.icon}
</div>
{/* Header */}
<div className="flex items-center gap-3 mb-4 relative z-10">
<span className="text-3xl">{theme.icon}</span>
<h2 className="text-lg font-bold uppercase tracking-wide m-0">
{theme.summaryTitle}
</h2>
</div>
{/* Title - HUGE */}
<h1 className="text-2xl md:text-3xl font-extrabold leading-tight mb-4 drop-shadow-sm relative z-10">
{title}
</h1>
{/* Content */}
<p className="text-lg leading-relaxed font-normal opacity-95 m-0 relative z-10">
{content}
</p>
</div>
);
};
// DETAILED BREAKDOWN
const DetailedBreakdown: React.FC<{
points: string[];
isExpanded: boolean;
onToggle: () => void;
} & ThemeProps> = ({ points, theme, isExpanded, onToggle }) => {
return (
<div className={cn(
"rounded-2xl p-5 mb-4 border shadow-sm transition-all duration-200",
theme.classes.card
)}>
{/* Header */}
<button
onClick={onToggle}
className="w-full flex justify-between items-center bg-transparent border-none cursor-pointer p-0"
>
<h3 className={cn(
"text-lg font-bold flex items-center gap-2 m-0",
theme.classes.secondaryText
)}>
<BookOpen size={20} />
Detailed Breakdown
</h3>
{isExpanded ? (
<ChevronUp className={theme.classes.secondaryText} />
) : (
<ChevronDown className={theme.classes.secondaryText} />
)}
</button>
{/* Points */}
{isExpanded && (
<ul className="list-none p-0 m-0 mt-4 space-y-3">
{points.map((point, idx) => (
<li key={idx} className={cn(
"pl-8 relative text-base leading-relaxed",
theme.classes.primaryText
)}>
<span className={cn(
"absolute left-0 top-0.5 w-5 h-5 rounded-full flex items-center justify-center text-xs font-bold text-white",
theme.classes.accentBg
)}>
{idx + 1}
</span>
{point}
</li>
))}
</ul>
)}
</div>
);
};
// KENYAN CONTEXT
const KenyanContext: React.FC<{
impact: string;
relatedTopic: string | null;
icon: string;
} & ThemeProps> = ({ impact, relatedTopic, icon, theme }) => {
return (
<div className={cn(
"rounded-2xl p-5 mb-4 border-2 border-l-[6px] shadow-sm",
theme.classes.card,
theme.classes.contextBorder
)}>
{/* Header with icon */}
<div className="flex items-start gap-4 mb-3">
<div className="text-5xl leading-none flex-shrink-0">
{icon}
</div>
<div className="flex-1">
<h3 className={cn(
"text-base font-bold uppercase tracking-wide mb-2",
theme.classes.iconColor
)}>
π°πͺ Kenyan Context
</h3>
<p className={cn(
"text-base leading-relaxed m-0",
theme.classes.primaryText
)}>
{impact}
</p>
</div>
</div>
{/* Related Topic */}
{relatedTopic && (
<div className={cn(
"mt-3 p-3 rounded-lg text-sm flex items-center gap-2",
theme.classes.container
)}>
<TrendingUp size={16} className={theme.classes.secondaryText} />
<span className={theme.classes.secondaryText}>
<strong>Related:</strong> {relatedTopic}
</span>
</div>
)}
</div>
);
};
// CITATIONS (Collapsible)
const Citations: React.FC<{
citations: AmaniQueryResponse['response']['citations'];
isExpanded: boolean;
onToggle: () => void;
} & ThemeProps> = ({ citations, theme, isExpanded, onToggle }) => {
return (
<div className={cn(
"rounded-2xl p-5 mb-4 border shadow-sm",
theme.classes.card
)}>
{/* Header */}
<button
onClick={onToggle}
className="w-full flex justify-between items-center bg-transparent border-none cursor-pointer p-0"
>
<h3 className="text-base font-bold text-muted-foreground flex items-center gap-2 m-0">
<ExternalLink size={18} />
Sources ({citations.length})
</h3>
{isExpanded ? (
<ChevronUp className="text-muted-foreground" />
) : (
<ChevronDown className="text-muted-foreground" />
)}
</button>
{/* Citation List */}
{isExpanded && (
<div className="mt-4 space-y-2">
{citations.map((citation, idx) => (
<div
key={idx}
className={cn(
"p-3 rounded-lg border-l-4 [border-left-color:currentColor]",
theme.classes.container,
theme.classes.border
)}
>
<div className={cn(
"text-sm font-semibold mb-1",
theme.classes.primaryText
)}>
{citation.source}
</div>
{citation.quote && (
<blockquote className={cn(
"mt-2 mb-0 pl-3 border-l-2 text-sm italic",
"border-muted-foreground/30 text-muted-foreground"
)}>
"{citation.quote}"
</blockquote>
)}
{citation.url !== 'N/A' && (
<a
href={citation.url}
target="_blank"
rel="noopener noreferrer"
className={cn(
"text-sm underline decoration-2 underline-offset-2 inline-flex items-center gap-1 mt-2",
"text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300",
"transition-colors duration-200 font-medium"
)}
>
View source <ExternalLink size={12} />
</a>
)}
</div>
))}
</div>
)}
</div>
);
};
// FOLLOW-UP SUGGESTIONS
const FollowUpSuggestions: React.FC<{
suggestions: string[];
onFollowUpClick?: (suggestion: string) => void;
} & ThemeProps> = ({ suggestions, theme, onFollowUpClick }) => {
return (
<div className={cn(
"rounded-2xl p-5 border shadow-sm",
theme.classes.card
)}>
<h3 className={cn(
"text-base font-bold mb-3 flex items-center gap-2",
theme.classes.secondaryText
)}>
<Users size={18} />
Want to know more?
</h3>
<div className="flex flex-col gap-2">
{suggestions.map((suggestion, idx) => (
<button
key={idx}
onClick={() => onFollowUpClick?.(suggestion)}
className={cn(
"p-3 md:px-4 rounded-xl text-sm text-left transition-all duration-200 font-medium border-2",
"bg-transparent cursor-pointer",
theme.classes.border,
theme.classes.primaryText,
theme.classes.buttonHover,
"hover:translate-x-1 hover:shadow-md active:scale-[0.98]"
)}
>
{suggestion}
</button>
))}
</div>
</div>
);
};
export default AmaniQueryResponse;
|