Spaces:
Paused
Paused
CBV01 commited on
Commit ·
ff1aa72
1
Parent(s): e9969e0
Fix JSON decode error, speed up AI suggestion generation, add live message reading scan tracker, and rename sidebar to DM Inbox
Browse files- backend/main.py +69 -20
- frontend/src/app/ai-assistant/page.tsx +183 -62
- frontend/src/components/Sidebar.tsx +3 -2
backend/main.py
CHANGED
|
@@ -5591,27 +5591,28 @@ async def analyze_dm_conversation(req: AIAnalyzeDmRequest, user_id: str = Depend
|
|
| 5591 |
f" - Option 2 (Helpful & Informative): Provides clear, practical insight or addresses their specific query.\n"
|
| 5592 |
f" - Option 3 (Smooth Next Step): Politely guides the conversation toward a clear next step or asks a natural follow-up question.\n"
|
| 5593 |
f"4. 100% READY TO SEND: Do NOT use any placeholders like '[insert link]' or '[name]'. Write full, ready-to-copy text.\n\n"
|
| 5594 |
-
f"Return ONLY valid JSON
|
| 5595 |
f"{{\n"
|
| 5596 |
-
f' "conversation_summary":
|
| 5597 |
f' "suggested_replies": [\n'
|
| 5598 |
-
f' {{"angle": "Direct & Friendly", "text":
|
| 5599 |
-
f' {{"angle": "Helpful & Informative", "text":
|
| 5600 |
-
f' {{"angle": "Smooth Next Step", "text":
|
| 5601 |
f' ]\n'
|
| 5602 |
f"}}"
|
| 5603 |
)
|
| 5604 |
|
| 5605 |
-
async with httpx.AsyncClient(timeout=
|
| 5606 |
try:
|
| 5607 |
payload: Dict[str, Any] = {
|
| 5608 |
"model": selected_model,
|
| 5609 |
"messages": [{"role": "user", "content": prompt}],
|
| 5610 |
-
"max_tokens":
|
| 5611 |
"include_reasoning": False
|
| 5612 |
}
|
| 5613 |
if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
|
| 5614 |
-
payload["temperature"] = 0.
|
|
|
|
| 5615 |
|
| 5616 |
res = await client.post(
|
| 5617 |
"https://openrouter.ai/api/v1/chat/completions",
|
|
@@ -5642,23 +5643,71 @@ async def analyze_dm_conversation(req: AIAnalyzeDmRequest, user_id: str = Depend
|
|
| 5642 |
content = re.sub(r'```(?:json)?', '', content).strip('`').strip()
|
| 5643 |
s = content.find('{')
|
| 5644 |
e = content.rfind('}')
|
| 5645 |
-
if s != -1 and e != -1
|
| 5646 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5647 |
replies = parsed.get("suggested_replies") or []
|
| 5648 |
-
|
| 5649 |
-
|
| 5650 |
-
|
| 5651 |
-
|
| 5652 |
-
|
| 5653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5654 |
|
| 5655 |
-
# If
|
| 5656 |
return {
|
| 5657 |
"conversation_summary": f"Discussion with {contact}",
|
| 5658 |
"suggested_replies": [
|
| 5659 |
-
{"angle": "Direct
|
| 5660 |
-
{"angle": "Helpful
|
| 5661 |
-
{"angle": "Next
|
| 5662 |
],
|
| 5663 |
"model": selected_model
|
| 5664 |
}
|
|
|
|
| 5591 |
f" - Option 2 (Helpful & Informative): Provides clear, practical insight or addresses their specific query.\n"
|
| 5592 |
f" - Option 3 (Smooth Next Step): Politely guides the conversation toward a clear next step or asks a natural follow-up question.\n"
|
| 5593 |
f"4. 100% READY TO SEND: Do NOT use any placeholders like '[insert link]' or '[name]'. Write full, ready-to-copy text.\n\n"
|
| 5594 |
+
f"Return ONLY valid JSON matching this exact structure with double-quoted strings:\n"
|
| 5595 |
f"{{\n"
|
| 5596 |
+
f' "conversation_summary": "1 sentence summarizing the discussion",\n'
|
| 5597 |
f' "suggested_replies": [\n'
|
| 5598 |
+
f' {{"angle": "Direct & Friendly", "text": "Natural Telegram reply answering their exact message"}},\n'
|
| 5599 |
+
f' {{"angle": "Helpful & Informative", "text": "Clear and informative Telegram reply offering assistance"}},\n'
|
| 5600 |
+
f' {{"angle": "Smooth Next Step", "text": "Polite Telegram reply advancing to the next step"}}\n'
|
| 5601 |
f' ]\n'
|
| 5602 |
f"}}"
|
| 5603 |
)
|
| 5604 |
|
| 5605 |
+
async with httpx.AsyncClient(timeout=35.0) as client:
|
| 5606 |
try:
|
| 5607 |
payload: Dict[str, Any] = {
|
| 5608 |
"model": selected_model,
|
| 5609 |
"messages": [{"role": "user", "content": prompt}],
|
| 5610 |
+
"max_tokens": 380,
|
| 5611 |
"include_reasoning": False
|
| 5612 |
}
|
| 5613 |
if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
|
| 5614 |
+
payload["temperature"] = 0.5
|
| 5615 |
+
payload["response_format"] = {"type": "json_object"}
|
| 5616 |
|
| 5617 |
res = await client.post(
|
| 5618 |
"https://openrouter.ai/api/v1/chat/completions",
|
|
|
|
| 5643 |
content = re.sub(r'```(?:json)?', '', content).strip('`').strip()
|
| 5644 |
s = content.find('{')
|
| 5645 |
e = content.rfind('}')
|
| 5646 |
+
json_str = content[s:e+1] if (s != -1 and e != -1) else content
|
| 5647 |
+
|
| 5648 |
+
replies = []
|
| 5649 |
+
summary = f"Conversation with {contact}"
|
| 5650 |
+
|
| 5651 |
+
# Strategy 1: Standard JSON parse
|
| 5652 |
+
try:
|
| 5653 |
+
parsed = json.loads(json_str)
|
| 5654 |
replies = parsed.get("suggested_replies") or []
|
| 5655 |
+
summary = parsed.get("conversation_summary") or summary
|
| 5656 |
+
except Exception:
|
| 5657 |
+
pass
|
| 5658 |
+
|
| 5659 |
+
# Strategy 2: Remove malformed/unquoted conversation_summary if that broke JSON
|
| 5660 |
+
if not replies:
|
| 5661 |
+
try:
|
| 5662 |
+
cleaned = re.sub(r'"conversation_summary"\s*:\s*([^",\n][^\n]*?),', '', json_str)
|
| 5663 |
+
cleaned = re.sub(r',\s*([}\]])', r'\1', cleaned)
|
| 5664 |
+
parsed = json.loads(cleaned)
|
| 5665 |
+
replies = parsed.get("suggested_replies") or []
|
| 5666 |
+
summary = parsed.get("conversation_summary") or summary
|
| 5667 |
+
except Exception:
|
| 5668 |
+
pass
|
| 5669 |
+
|
| 5670 |
+
# Strategy 3: ast.literal_eval for single-quoted Python dict syntax
|
| 5671 |
+
if not replies:
|
| 5672 |
+
try:
|
| 5673 |
+
parsed = ast.literal_eval(json_str)
|
| 5674 |
+
if isinstance(parsed, dict):
|
| 5675 |
+
replies = parsed.get("suggested_replies") or []
|
| 5676 |
+
summary = parsed.get("conversation_summary") or summary
|
| 5677 |
+
except Exception:
|
| 5678 |
+
pass
|
| 5679 |
+
|
| 5680 |
+
# Strategy 4: Regex extraction for angle/text pairs
|
| 5681 |
+
if not replies:
|
| 5682 |
+
pairs = re.findall(r'["\']angle["\']\s*:\s*["\']([^"\']+)["\']\s*,\s*["\']text["\']\s*:\s*["\']([^"\']+)["\']', content)
|
| 5683 |
+
if not pairs:
|
| 5684 |
+
pairs = re.findall(r'["\']text["\']\s*:\s*["\']([^"\']+)["\']\s*,\s*["\']angle["\']\s*:\s*["\']([^"\']+)["\']', content)
|
| 5685 |
+
if pairs:
|
| 5686 |
+
pairs = [(p[1], p[0]) for p in pairs]
|
| 5687 |
+
if pairs:
|
| 5688 |
+
replies = [{"angle": a.strip(), "text": t.strip()} for a, t in pairs]
|
| 5689 |
+
|
| 5690 |
+
# Strategy 5: Regex extraction for numbered or bullet lines
|
| 5691 |
+
if not replies:
|
| 5692 |
+
bullets = re.findall(r'(?:^|\n)\s*(?:\d+[\.\)]|\-|\*)\s*(?:([A-Za-z\s&]+):)?\s*(.+)', content)
|
| 5693 |
+
if bullets:
|
| 5694 |
+
for b_angle, b_text in bullets[:3]:
|
| 5695 |
+
replies.append({"angle": b_angle.strip() if b_angle else "Suggested Reply", "text": b_text.strip()})
|
| 5696 |
+
|
| 5697 |
+
if replies:
|
| 5698 |
+
return {
|
| 5699 |
+
"conversation_summary": summary,
|
| 5700 |
+
"suggested_replies": replies[:3],
|
| 5701 |
+
"model": selected_model
|
| 5702 |
+
}
|
| 5703 |
|
| 5704 |
+
# If all parsing and choices were empty, provide graceful fallback
|
| 5705 |
return {
|
| 5706 |
"conversation_summary": f"Discussion with {contact}",
|
| 5707 |
"suggested_replies": [
|
| 5708 |
+
{"angle": "Direct & Friendly", "text": f"Hey {contact}, thanks for reaching out! Let me know if you'd like to get this set up."},
|
| 5709 |
+
{"angle": "Helpful & Informative", "text": f"Hi {contact}, happy to assist with that. Let me know what timing suits you best."},
|
| 5710 |
+
{"angle": "Smooth Next Step", "text": f"Sounds good {contact}! Let's coordinate the next step so we can get this moving."}
|
| 5711 |
],
|
| 5712 |
"model": selected_model
|
| 5713 |
}
|
frontend/src/app/ai-assistant/page.tsx
CHANGED
|
@@ -20,7 +20,8 @@ import {
|
|
| 20 |
User,
|
| 21 |
Star,
|
| 22 |
File,
|
| 23 |
-
MessageSquare
|
|
|
|
| 24 |
} from 'lucide-react';
|
| 25 |
import { apiFetch } from '@/lib/auth';
|
| 26 |
|
|
@@ -225,6 +226,10 @@ export default function AiAssistantPage() {
|
|
| 225 |
const [dmAnalysisError, setDmAnalysisError] = useState('');
|
| 226 |
const [copiedIdx, setCopiedIdx] = useState<number | null>(null);
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
const messagesEndRef = useRef<HTMLDivElement | null>(null);
|
| 229 |
|
| 230 |
// Load initial settings and accounts
|
|
@@ -419,6 +424,25 @@ export default function AiAssistantPage() {
|
|
| 419 |
|
| 420 |
setIsAnalyzingDm(true);
|
| 421 |
setDmAnalysisError('');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
const currentKey = aiKey || (typeof window !== 'undefined' ? localStorage.getItem('arkitel_ai_key') || '' : '');
|
| 424 |
const currentModel = aiModel || (typeof window !== 'undefined' ? localStorage.getItem('arkitel_ai_model') || 'meta-llama/llama-3.3-70b-instruct' : 'meta-llama/llama-3.3-70b-instruct');
|
|
@@ -426,13 +450,21 @@ export default function AiAssistantPage() {
|
|
| 426 |
if (!currentKey) {
|
| 427 |
if (selectedContact?.is_demo && DEMO_ANALYSIS[selectedContact.id]) {
|
| 428 |
setTimeout(() => {
|
|
|
|
| 429 |
setDmAnalysis(DEMO_ANALYSIS[selectedContact.id]);
|
| 430 |
setIsAnalyzingDm(false);
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
return;
|
| 433 |
} else {
|
|
|
|
| 434 |
setDmAnalysisError('OpenRouter API key is not connected. Please click "Connect AI" in the left panel to configure your key.');
|
| 435 |
setIsAnalyzingDm(false);
|
|
|
|
|
|
|
| 436 |
return;
|
| 437 |
}
|
| 438 |
}
|
|
@@ -456,7 +488,12 @@ export default function AiAssistantPage() {
|
|
| 456 |
} catch (e: any) {
|
| 457 |
setDmAnalysisError(e.message || 'Error communicating with AI service.');
|
| 458 |
} finally {
|
|
|
|
| 459 |
setIsAnalyzingDm(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
}
|
| 461 |
};
|
| 462 |
|
|
@@ -729,6 +766,33 @@ export default function AiAssistantPage() {
|
|
| 729 |
</div>
|
| 730 |
</div>
|
| 731 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
{/* CONVERSATION STREAM (INDEPENDENT SCROLL) */}
|
| 733 |
<div className="flex-1 p-4 sm:p-5 overflow-y-auto space-y-4 custom-scrollbar bg-radial from-background via-background to-card/20">
|
| 734 |
{fetchingMessages && (
|
|
@@ -744,69 +808,110 @@ export default function AiAssistantPage() {
|
|
| 744 |
</div>
|
| 745 |
)}
|
| 746 |
|
| 747 |
-
{messages.map(msg =>
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
</span>
|
| 758 |
</div>
|
| 759 |
-
<span className="text-[10px] font-mono text-foreground/40">
|
| 760 |
-
{msg.date || "Sent"}
|
| 761 |
-
</span>
|
| 762 |
-
</div>
|
| 763 |
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
|
|
|
| 780 |
</div>
|
| 781 |
-
|
| 782 |
-
)}
|
| 783 |
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
|
|
|
| 787 |
</div>
|
| 788 |
</div>
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
|
|
|
|
|
|
|
|
|
| 797 |
</div>
|
| 798 |
-
<span className="font-bold text-foreground text-xs">{msg.sender_name || selectedContact.name}</span>
|
| 799 |
-
<span className="opacity-40">•</span>
|
| 800 |
-
<span className="text-[10px] text-foreground/40 font-mono">{msg.date || "Recent"}</span>
|
| 801 |
-
</div>
|
| 802 |
|
| 803 |
-
|
| 804 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
</div>
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
)
|
| 810 |
<div ref={messagesEndRef} />
|
| 811 |
</div>
|
| 812 |
|
|
@@ -886,14 +991,30 @@ export default function AiAssistantPage() {
|
|
| 886 |
{/* Slide-Up Content Stream */}
|
| 887 |
<div className="flex-1 overflow-y-auto p-4 sm:p-5 space-y-4 custom-scrollbar">
|
| 888 |
{isAnalyzingDm && (
|
| 889 |
-
<div className="py-
|
| 890 |
-
<
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 897 |
</div>
|
| 898 |
)}
|
| 899 |
|
|
|
|
| 20 |
User,
|
| 21 |
Star,
|
| 22 |
File,
|
| 23 |
+
MessageSquare,
|
| 24 |
+
Target
|
| 25 |
} from 'lucide-react';
|
| 26 |
import { apiFetch } from '@/lib/auth';
|
| 27 |
|
|
|
|
| 226 |
const [dmAnalysisError, setDmAnalysisError] = useState('');
|
| 227 |
const [copiedIdx, setCopiedIdx] = useState<number | null>(null);
|
| 228 |
|
| 229 |
+
// Live Visual Scanning / Reading Tracker State
|
| 230 |
+
const [scannedMsgIndex, setScannedMsgIndex] = useState<number>(-1);
|
| 231 |
+
const [scanPhaseText, setScanPhaseText] = useState<string>('');
|
| 232 |
+
|
| 233 |
const messagesEndRef = useRef<HTMLDivElement | null>(null);
|
| 234 |
|
| 235 |
// Load initial settings and accounts
|
|
|
|
| 424 |
|
| 425 |
setIsAnalyzingDm(true);
|
| 426 |
setDmAnalysisError('');
|
| 427 |
+
setScannedMsgIndex(0);
|
| 428 |
+
setScanPhaseText(`Reading message 1 of ${msgs.length}...`);
|
| 429 |
+
|
| 430 |
+
// Real-time visual tracking scan that jumps across messages
|
| 431 |
+
let currentIdx = 0;
|
| 432 |
+
const scanInterval = setInterval(() => {
|
| 433 |
+
currentIdx++;
|
| 434 |
+
if (currentIdx < msgs.length) {
|
| 435 |
+
setScannedMsgIndex(currentIdx);
|
| 436 |
+
const isLast = currentIdx === msgs.length - 1;
|
| 437 |
+
if (isLast) {
|
| 438 |
+
setScanPhaseText(`Analyzing ${contactName}'s latest inquiry...`);
|
| 439 |
+
} else {
|
| 440 |
+
setScanPhaseText(`Reading message ${currentIdx + 1} of ${msgs.length}...`);
|
| 441 |
+
}
|
| 442 |
+
} else {
|
| 443 |
+
setScanPhaseText(`Formulating 3 natural Telegram reply options...`);
|
| 444 |
+
}
|
| 445 |
+
}, 380);
|
| 446 |
|
| 447 |
const currentKey = aiKey || (typeof window !== 'undefined' ? localStorage.getItem('arkitel_ai_key') || '' : '');
|
| 448 |
const currentModel = aiModel || (typeof window !== 'undefined' ? localStorage.getItem('arkitel_ai_model') || 'meta-llama/llama-3.3-70b-instruct' : 'meta-llama/llama-3.3-70b-instruct');
|
|
|
|
| 450 |
if (!currentKey) {
|
| 451 |
if (selectedContact?.is_demo && DEMO_ANALYSIS[selectedContact.id]) {
|
| 452 |
setTimeout(() => {
|
| 453 |
+
clearInterval(scanInterval);
|
| 454 |
setDmAnalysis(DEMO_ANALYSIS[selectedContact.id]);
|
| 455 |
setIsAnalyzingDm(false);
|
| 456 |
+
setTimeout(() => {
|
| 457 |
+
setScannedMsgIndex(-1);
|
| 458 |
+
setScanPhaseText('');
|
| 459 |
+
}, 1200);
|
| 460 |
+
}, 400);
|
| 461 |
return;
|
| 462 |
} else {
|
| 463 |
+
clearInterval(scanInterval);
|
| 464 |
setDmAnalysisError('OpenRouter API key is not connected. Please click "Connect AI" in the left panel to configure your key.');
|
| 465 |
setIsAnalyzingDm(false);
|
| 466 |
+
setScannedMsgIndex(-1);
|
| 467 |
+
setScanPhaseText('');
|
| 468 |
return;
|
| 469 |
}
|
| 470 |
}
|
|
|
|
| 488 |
} catch (e: any) {
|
| 489 |
setDmAnalysisError(e.message || 'Error communicating with AI service.');
|
| 490 |
} finally {
|
| 491 |
+
clearInterval(scanInterval);
|
| 492 |
setIsAnalyzingDm(false);
|
| 493 |
+
setTimeout(() => {
|
| 494 |
+
setScannedMsgIndex(-1);
|
| 495 |
+
setScanPhaseText('');
|
| 496 |
+
}, 1200);
|
| 497 |
}
|
| 498 |
};
|
| 499 |
|
|
|
|
| 766 |
</div>
|
| 767 |
</div>
|
| 768 |
|
| 769 |
+
{/* LIVE AI CONVERSATION READING HUD BANNER */}
|
| 770 |
+
{isAnalyzingDm && (
|
| 771 |
+
<div className="bg-gradient-to-r from-cyan-500/15 via-purple-500/15 to-blue-500/15 border-b border-cyan-500/30 px-4 sm:px-5 py-2.5 flex items-center justify-between text-xs animate-in slide-in-from-top duration-300 shrink-0">
|
| 772 |
+
<div className="flex items-center gap-2.5 min-w-0">
|
| 773 |
+
<span className="w-2.5 h-2.5 rounded-full bg-cyan-400 animate-ping shrink-0" />
|
| 774 |
+
<span className="font-extrabold text-foreground tracking-tight text-xs flex items-center gap-1.5 shrink-0">
|
| 775 |
+
<Sparkles size={13} className="text-cyan-400 animate-spin" />
|
| 776 |
+
Live Conversation Tracker:
|
| 777 |
+
</span>
|
| 778 |
+
<span className="text-cyan-400 font-bold text-xs truncate">
|
| 779 |
+
{scanPhaseText || `Reading conversation thread with ${selectedContact.name}...`}
|
| 780 |
+
</span>
|
| 781 |
+
</div>
|
| 782 |
+
<div className="flex items-center gap-2 shrink-0">
|
| 783 |
+
<span className="text-[10px] font-mono font-bold text-foreground/60 hidden sm:inline">
|
| 784 |
+
{scannedMsgIndex >= 0 ? `Message ${Math.min(scannedMsgIndex + 1, messages.length)}/${messages.length}` : 'Scanning'}
|
| 785 |
+
</span>
|
| 786 |
+
<div className="w-20 sm:w-28 bg-foreground/10 rounded-full h-1.5 overflow-hidden">
|
| 787 |
+
<div
|
| 788 |
+
className="h-full bg-gradient-to-r from-cyan-400 to-purple-500 transition-all duration-300"
|
| 789 |
+
style={{ width: `${Math.min(100, Math.round(((scannedMsgIndex + 1) / Math.max(1, messages.length)) * 100))}%` }}
|
| 790 |
+
/>
|
| 791 |
+
</div>
|
| 792 |
+
</div>
|
| 793 |
+
</div>
|
| 794 |
+
)}
|
| 795 |
+
|
| 796 |
{/* CONVERSATION STREAM (INDEPENDENT SCROLL) */}
|
| 797 |
<div className="flex-1 p-4 sm:p-5 overflow-y-auto space-y-4 custom-scrollbar bg-radial from-background via-background to-card/20">
|
| 798 |
{fetchingMessages && (
|
|
|
|
| 808 |
</div>
|
| 809 |
)}
|
| 810 |
|
| 811 |
+
{messages.map((msg, idx) => {
|
| 812 |
+
const isCurrentlyScanning = isAnalyzingDm && (scannedMsgIndex === idx || (scannedMsgIndex >= messages.length && idx === messages.length - 1));
|
| 813 |
+
const isAlreadyScanned = isAnalyzingDm && scannedMsgIndex > idx;
|
| 814 |
+
const isKeyLastMsg = idx === messages.length - 1;
|
| 815 |
+
|
| 816 |
+
return (
|
| 817 |
+
<div key={msg.id} className="space-y-1.5 transition-all duration-300">
|
| 818 |
+
{/* Dynamic Tracker Badge Above Message */}
|
| 819 |
+
{isCurrentlyScanning && (
|
| 820 |
+
<div className={`flex items-center gap-1.5 px-2.5 py-1 rounded-xl text-[10px] font-black tracking-wide w-fit animate-pulse shadow-md transition-all ${
|
| 821 |
+
msg.is_out ? 'ml-auto bg-gradient-to-r from-blue-600 to-cyan-500 text-white' : 'mr-auto bg-gradient-to-r from-cyan-500 to-purple-600 text-white'
|
| 822 |
+
}`}>
|
| 823 |
+
<Sparkles size={11} className="animate-spin" />
|
| 824 |
+
<span>
|
| 825 |
+
{isKeyLastMsg
|
| 826 |
+
? `🎯 Key Context: Reading ${selectedContact.name}'s latest statement...`
|
| 827 |
+
: `⚡ AI Reading Message ${idx + 1} of ${messages.length}...`}
|
| 828 |
+
</span>
|
| 829 |
+
</div>
|
| 830 |
+
)}
|
| 831 |
+
|
| 832 |
+
{!isCurrentlyScanning && isAlreadyScanned && (
|
| 833 |
+
<div className={`flex items-center gap-1 text-[10px] font-bold text-emerald-400 opacity-80 transition-opacity ${
|
| 834 |
+
msg.is_out ? 'justify-end' : 'justify-start'
|
| 835 |
+
}`}>
|
| 836 |
+
<CheckCircle2 size={11} />
|
| 837 |
+
<span>Context Read</span>
|
| 838 |
+
</div>
|
| 839 |
+
)}
|
| 840 |
+
|
| 841 |
+
{msg.is_out ? (
|
| 842 |
+
<div className="max-w-xl ml-auto">
|
| 843 |
+
<div className={`rounded-2xl bg-card border shadow-md p-4 space-y-3 transition-all duration-300 ${
|
| 844 |
+
isCurrentlyScanning
|
| 845 |
+
? 'ring-2 ring-cyan-400 border-cyan-400 shadow-xl shadow-cyan-500/20 bg-cyan-500/[0.08] scale-[1.01]'
|
| 846 |
+
: isAlreadyScanned
|
| 847 |
+
? 'border-emerald-500/30 bg-emerald-500/[0.02]'
|
| 848 |
+
: 'border-border/80'
|
| 849 |
+
}`}>
|
| 850 |
+
<div className="flex items-center justify-between border-b border-border/50 pb-2">
|
| 851 |
+
<div className="flex items-center gap-2">
|
| 852 |
+
<span className="w-2 h-2 rounded-full bg-cyan-400" />
|
| 853 |
+
<span className="text-xs font-black text-foreground">
|
| 854 |
+
{msg.sender_name || "ArkiTel Agent"}
|
| 855 |
+
</span>
|
| 856 |
+
</div>
|
| 857 |
+
<span className="text-[10px] font-mono text-foreground/40">
|
| 858 |
+
{msg.date || "Sent"}
|
| 859 |
</span>
|
| 860 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
|
| 862 |
+
<div className="text-xs text-foreground/90 leading-relaxed font-sans whitespace-pre-wrap">
|
| 863 |
+
{msg.text}
|
| 864 |
+
</div>
|
| 865 |
|
| 866 |
+
{msg.attachment_name && (
|
| 867 |
+
<div className="flex items-center gap-2.5 p-2.5 rounded-xl bg-background/80 border border-border/80 hover:border-cyan-500/40 transition-colors w-fit">
|
| 868 |
+
<div className="w-7 h-7 rounded-lg bg-cyan-500/15 border border-cyan-500/30 text-cyan-400 flex items-center justify-center">
|
| 869 |
+
<File size={14} />
|
| 870 |
+
</div>
|
| 871 |
+
<div>
|
| 872 |
+
<span className="text-xs font-bold text-foreground block">
|
| 873 |
+
{msg.attachment_name}
|
| 874 |
+
</span>
|
| 875 |
+
<span className="text-[10px] text-foreground/40 font-mono">
|
| 876 |
+
{msg.attachment_size || "2.4 MB"}
|
| 877 |
+
</span>
|
| 878 |
+
</div>
|
| 879 |
</div>
|
| 880 |
+
)}
|
|
|
|
| 881 |
|
| 882 |
+
<div className="flex items-center justify-end gap-1.5 pt-0.5 text-[10px] text-emerald-400 font-bold">
|
| 883 |
+
<CheckCircle2 size={12} />
|
| 884 |
+
<span>Delivered</span>
|
| 885 |
+
</div>
|
| 886 |
</div>
|
| 887 |
</div>
|
| 888 |
+
) : (
|
| 889 |
+
<div className="max-w-xl mr-auto space-y-1.5">
|
| 890 |
+
<div className="flex items-center gap-2 text-xs text-foreground/60">
|
| 891 |
+
<div className={`w-5 h-5 rounded-full flex items-center justify-center font-bold text-[9px] text-white ${
|
| 892 |
+
selectedContact.avatar_bg || 'bg-blue-600'
|
| 893 |
+
}`}>
|
| 894 |
+
{selectedContact.avatar_initials || 'AW'}
|
| 895 |
+
</div>
|
| 896 |
+
<span className="font-bold text-foreground text-xs">{msg.sender_name || selectedContact.name}</span>
|
| 897 |
+
<span className="opacity-40">•</span>
|
| 898 |
+
<span className="text-[10px] text-foreground/40 font-mono">{msg.date || "Recent"}</span>
|
| 899 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 900 |
|
| 901 |
+
<div className={`rounded-2xl bg-card border shadow-sm p-3.5 text-xs text-foreground/90 leading-relaxed whitespace-pre-wrap transition-all duration-300 ${
|
| 902 |
+
isCurrentlyScanning
|
| 903 |
+
? 'ring-2 ring-cyan-400 border-cyan-400 shadow-xl shadow-cyan-500/20 bg-cyan-500/[0.08] scale-[1.01]'
|
| 904 |
+
: isAlreadyScanned
|
| 905 |
+
? 'border-emerald-500/30 bg-emerald-500/[0.02]'
|
| 906 |
+
: 'border-border/80'
|
| 907 |
+
}`}>
|
| 908 |
+
{msg.text}
|
| 909 |
+
</div>
|
| 910 |
</div>
|
| 911 |
+
)}
|
| 912 |
+
</div>
|
| 913 |
+
);
|
| 914 |
+
})}
|
| 915 |
<div ref={messagesEndRef} />
|
| 916 |
</div>
|
| 917 |
|
|
|
|
| 991 |
{/* Slide-Up Content Stream */}
|
| 992 |
<div className="flex-1 overflow-y-auto p-4 sm:p-5 space-y-4 custom-scrollbar">
|
| 993 |
{isAnalyzingDm && (
|
| 994 |
+
<div className="py-8 px-4 text-center space-y-3.5 max-w-md mx-auto">
|
| 995 |
+
<div className="relative w-14 h-14 mx-auto flex items-center justify-center">
|
| 996 |
+
<div className="absolute inset-0 rounded-2xl bg-gradient-to-tr from-cyan-500 to-purple-600 animate-pulse opacity-30 blur-sm" />
|
| 997 |
+
<div className="relative w-12 h-12 rounded-2xl bg-card border border-cyan-500/40 text-cyan-400 flex items-center justify-center shadow-md shadow-cyan-500/20">
|
| 998 |
+
<Sparkles size={22} className="animate-spin" />
|
| 999 |
+
</div>
|
| 1000 |
+
</div>
|
| 1001 |
+
<div>
|
| 1002 |
+
<h4 className="text-sm font-extrabold text-foreground">
|
| 1003 |
+
{scanPhaseText || "Reading conversation stream..."}
|
| 1004 |
+
</h4>
|
| 1005 |
+
<p className="text-xs text-foreground/50 mt-1">
|
| 1006 |
+
Analyzing chat thread & targeting {selectedContact.name}'s last statement
|
| 1007 |
+
</p>
|
| 1008 |
+
</div>
|
| 1009 |
+
<div className="w-full bg-foreground/10 rounded-full h-2 overflow-hidden">
|
| 1010 |
+
<div
|
| 1011 |
+
className="h-full bg-gradient-to-r from-cyan-400 via-purple-500 to-blue-500 transition-all duration-300"
|
| 1012 |
+
style={{ width: `${Math.min(100, Math.max(25, Math.round(((scannedMsgIndex + 1) / Math.max(1, messages.length)) * 100)))}%` }}
|
| 1013 |
+
/>
|
| 1014 |
+
</div>
|
| 1015 |
+
<span className="text-[10px] text-cyan-400 font-mono font-bold block animate-pulse">
|
| 1016 |
+
Inspecting message {Math.min(scannedMsgIndex + 1, messages.length)} of {messages.length}...
|
| 1017 |
+
</span>
|
| 1018 |
</div>
|
| 1019 |
)}
|
| 1020 |
|
frontend/src/components/Sidebar.tsx
CHANGED
|
@@ -22,7 +22,8 @@ import {
|
|
| 22 |
Shield,
|
| 23 |
Bookmark,
|
| 24 |
FileText,
|
| 25 |
-
Sparkles
|
|
|
|
| 26 |
} from "lucide-react";
|
| 27 |
import { getToken, removeToken, apiFetch } from "@/lib/auth";
|
| 28 |
import { BroadcastBanner } from "./BroadcastBanner";
|
|
@@ -189,7 +190,7 @@ export function Sidebar({ children }: { children: React.ReactNode }) {
|
|
| 189 |
{ name: "Scraper", href: "/scraper", icon: Search },
|
| 190 |
{ name: "Leads", href: "/leads", icon: Bookmark },
|
| 191 |
{ name: "Templates", href: "/templates", icon: FileText },
|
| 192 |
-
{ name: "
|
| 193 |
];
|
| 194 |
|
| 195 |
if (user?.role === 'admin') {
|
|
|
|
| 22 |
Shield,
|
| 23 |
Bookmark,
|
| 24 |
FileText,
|
| 25 |
+
Sparkles,
|
| 26 |
+
Inbox
|
| 27 |
} from "lucide-react";
|
| 28 |
import { getToken, removeToken, apiFetch } from "@/lib/auth";
|
| 29 |
import { BroadcastBanner } from "./BroadcastBanner";
|
|
|
|
| 190 |
{ name: "Scraper", href: "/scraper", icon: Search },
|
| 191 |
{ name: "Leads", href: "/leads", icon: Bookmark },
|
| 192 |
{ name: "Templates", href: "/templates", icon: FileText },
|
| 193 |
+
{ name: "DM Inbox", href: "/ai-assistant", icon: Inbox },
|
| 194 |
];
|
| 195 |
|
| 196 |
if (user?.role === 'admin') {
|