Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
8a6f7f5
1
Parent(s): 93d3f09
feat: add chat reset functionality to clear history and AI memory while keeping lead
Browse files- backend/app/api/leads.py +69 -0
- frontend/app/chats/page.tsx +21 -0
- frontend/components/LeadsList.tsx +50 -35
- frontend/lib/api.ts +5 -0
backend/app/api/leads.py
CHANGED
|
@@ -637,3 +637,72 @@ async def delete_lead(
|
|
| 637 |
}
|
| 638 |
|
| 639 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
}
|
| 638 |
|
| 639 |
|
| 640 |
+
@router.post("/{lead_id}/reset")
|
| 641 |
+
async def reset_lead(
|
| 642 |
+
lead_id: UUID,
|
| 643 |
+
current_user: User = Depends(get_current_user),
|
| 644 |
+
session = Depends(get_session)
|
| 645 |
+
):
|
| 646 |
+
"""
|
| 647 |
+
Reset a lead - clears conversation history and AI analysis.
|
| 648 |
+
The lead record is preserved so new messages from the same contact
|
| 649 |
+
will be treated as a fresh conversation.
|
| 650 |
+
|
| 651 |
+
This is useful for testing different prompts with the same account.
|
| 652 |
+
"""
|
| 653 |
+
# Use authenticated user's tenant_id (prevents tenant isolation breach)
|
| 654 |
+
tenant_id = current_user.tenant_id
|
| 655 |
+
|
| 656 |
+
# Get lead
|
| 657 |
+
stmt = select(Lead).where(
|
| 658 |
+
Lead.tenant_id == tenant_id,
|
| 659 |
+
Lead.id == lead_id
|
| 660 |
+
)
|
| 661 |
+
result = await session.execute(stmt)
|
| 662 |
+
lead = result.scalar_one_or_none()
|
| 663 |
+
|
| 664 |
+
if not lead:
|
| 665 |
+
raise HTTPException(status_code=404, detail="Lead not found")
|
| 666 |
+
|
| 667 |
+
lead_name = lead.name or "Unnamed"
|
| 668 |
+
messages_deleted = 0
|
| 669 |
+
conversations_deleted = 0
|
| 670 |
+
|
| 671 |
+
# Delete all messages from conversations
|
| 672 |
+
conv_stmt = select(Conversation).where(Conversation.lead_id == lead_id)
|
| 673 |
+
conv_result = await session.execute(conv_stmt)
|
| 674 |
+
conversations = conv_result.scalars().all()
|
| 675 |
+
|
| 676 |
+
for conv in conversations:
|
| 677 |
+
# Delete messages for this conversation
|
| 678 |
+
msg_delete_stmt = select(Message).where(Message.conversation_id == conv.id)
|
| 679 |
+
msg_result = await session.execute(msg_delete_stmt)
|
| 680 |
+
messages = msg_result.scalars().all()
|
| 681 |
+
for msg in messages:
|
| 682 |
+
await session.delete(msg)
|
| 683 |
+
messages_deleted += 1
|
| 684 |
+
|
| 685 |
+
# Delete conversation
|
| 686 |
+
await session.delete(conv)
|
| 687 |
+
conversations_deleted += 1
|
| 688 |
+
|
| 689 |
+
# Reset lead to initial state
|
| 690 |
+
lead.ai_summary_json = {} # Clear AI analysis
|
| 691 |
+
lead.heat_score = 0
|
| 692 |
+
lead.heat_level = LeadHeat.COLD
|
| 693 |
+
lead.status = LeadStatus.NEW
|
| 694 |
+
lead.psychotype = lead.psychotype # Keep psychotype for now (will be re-analyzed)
|
| 695 |
+
lead.first_message_at = datetime.utcnow()
|
| 696 |
+
lead.last_message_at = datetime.utcnow()
|
| 697 |
+
lead.updated_at = datetime.utcnow()
|
| 698 |
+
|
| 699 |
+
session.add(lead)
|
| 700 |
+
await session.commit()
|
| 701 |
+
|
| 702 |
+
return {
|
| 703 |
+
"ok": True,
|
| 704 |
+
"message": f"Lead '{lead_name}' has been reset. Next message will start fresh.",
|
| 705 |
+
"lead_id": str(lead_id),
|
| 706 |
+
"messages_deleted": messages_deleted,
|
| 707 |
+
"conversations_deleted": conversations_deleted
|
| 708 |
+
}
|
frontend/app/chats/page.tsx
CHANGED
|
@@ -125,6 +125,26 @@ function ChatsContent() {
|
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
if (authLoading || !user) {
|
| 129 |
return (
|
| 130 |
<div className="flex items-center justify-center h-screen bg-background">
|
|
@@ -152,6 +172,7 @@ function ChatsContent() {
|
|
| 152 |
selectedLeadId={selectedLead?.id}
|
| 153 |
onSelectLead={handleSelectLead}
|
| 154 |
onDeleteLead={handleDeleteLead}
|
|
|
|
| 155 |
searchQuery={searchQuery}
|
| 156 |
setSearchQuery={setSearchQuery}
|
| 157 |
activeTab={activeTab}
|
|
|
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
| 128 |
+
// Reset Lead Handler - clears history but keeps the lead
|
| 129 |
+
const handleResetLead = async (leadId: string) => {
|
| 130 |
+
try {
|
| 131 |
+
const result = await leadsApi.resetLead(leadId)
|
| 132 |
+
toast.success('Сброшено', `Чат сброшен. Удалено ${result.messages_deleted} сообщений`)
|
| 133 |
+
|
| 134 |
+
// Clear messages display if this was the selected lead
|
| 135 |
+
if (selectedLead?.id === leadId) {
|
| 136 |
+
setMessages([])
|
| 137 |
+
setConversation(null)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// Refresh leads list
|
| 141 |
+
await refetchLeads()
|
| 142 |
+
} catch (error) {
|
| 143 |
+
console.error('Error resetting lead:', error)
|
| 144 |
+
toast.error('Ошибка', 'Не удалось сбросить чат')
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
if (authLoading || !user) {
|
| 149 |
return (
|
| 150 |
<div className="flex items-center justify-center h-screen bg-background">
|
|
|
|
| 172 |
selectedLeadId={selectedLead?.id}
|
| 173 |
onSelectLead={handleSelectLead}
|
| 174 |
onDeleteLead={handleDeleteLead}
|
| 175 |
+
onResetLead={handleResetLead}
|
| 176 |
searchQuery={searchQuery}
|
| 177 |
setSearchQuery={setSearchQuery}
|
| 178 |
activeTab={activeTab}
|
frontend/components/LeadsList.tsx
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
import { useState } from 'react'
|
| 4 |
import { Lead } from '@/lib/api'
|
| 5 |
import { cn } from '@/lib/utils'
|
| 6 |
-
import { Search, Flame, Inbox, CheckCircle2, MessageCircle, Mail, Linkedin, Trash2, X, Brain } from 'lucide-react'
|
| 7 |
|
| 8 |
interface LeadsListProps {
|
| 9 |
leads: Lead[] | undefined
|
|
@@ -11,6 +11,7 @@ interface LeadsListProps {
|
|
| 11 |
selectedLeadId: string | undefined
|
| 12 |
onSelectLead: (lead: Lead) => void
|
| 13 |
onDeleteLead?: (leadId: string, clearCache: boolean) => void
|
|
|
|
| 14 |
searchQuery: string
|
| 15 |
setSearchQuery: (query: string) => void
|
| 16 |
activeTab: 'all' | 'hot' | 'unread' | 'whatsapp' | 'email' | 'linkedin'
|
|
@@ -23,6 +24,7 @@ export function LeadsList({
|
|
| 23 |
selectedLeadId,
|
| 24 |
onSelectLead,
|
| 25 |
onDeleteLead,
|
|
|
|
| 26 |
searchQuery,
|
| 27 |
setSearchQuery,
|
| 28 |
activeTab,
|
|
@@ -41,6 +43,13 @@ export function LeadsList({
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
// Filter Logic
|
| 45 |
const filteredLeads = leads?.filter(lead => {
|
| 46 |
const matchesSearch = (lead.name || '').toLowerCase().includes(searchQuery.toLowerCase())
|
|
@@ -74,12 +83,12 @@ export function LeadsList({
|
|
| 74 |
|
| 75 |
return (
|
| 76 |
<>
|
| 77 |
-
{/* Delete Confirmation Modal */}
|
| 78 |
{deleteModal && (
|
| 79 |
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
|
| 80 |
<div className="bg-[#1A1B26] rounded-2xl border border-white/10 p-6 max-w-md w-full shadow-2xl">
|
| 81 |
<div className="flex items-center justify-between mb-4">
|
| 82 |
-
<h3 className="text-lg font-bold text-white">У
|
| 83 |
<button
|
| 84 |
onClick={() => { setDeleteModal(null); setClearCacheOnDelete(false) }}
|
| 85 |
className="p-1 hover:bg-white/10 rounded-lg text-slate-400 hover:text-white"
|
|
@@ -88,44 +97,50 @@ export function LeadsList({
|
|
| 88 |
</button>
|
| 89 |
</div>
|
| 90 |
|
| 91 |
-
<p className="text-slate-400 text-sm mb-
|
| 92 |
-
|
| 93 |
-
Все сообщения будут удалены безвозвратно.
|
| 94 |
</p>
|
| 95 |
|
| 96 |
-
{/*
|
| 97 |
-
|
| 98 |
-
<
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
className="
|
| 103 |
-
|
| 104 |
-
<div>
|
| 105 |
-
<div className="flex items-center gap-2 text-sm font-medium text-white">
|
| 106 |
-
<Brain className="w-4 h-4 text-indigo-400" />
|
| 107 |
-
Очистить кэш AI
|
| 108 |
</div>
|
| 109 |
-
<
|
| 110 |
-
Сбросит
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
</p>
|
| 112 |
</div>
|
| 113 |
-
</
|
| 114 |
|
| 115 |
-
<
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
</button>
|
| 122 |
-
<button
|
| 123 |
-
onClick={handleConfirmDelete}
|
| 124 |
-
className="flex-1 px-4 py-2.5 text-sm font-bold bg-red-500/20 text-red-400 rounded-xl hover:bg-red-500/30 transition-colors"
|
| 125 |
-
>
|
| 126 |
-
Удалить
|
| 127 |
-
</button>
|
| 128 |
-
</div>
|
| 129 |
</div>
|
| 130 |
</div>
|
| 131 |
)}
|
|
|
|
| 3 |
import { useState } from 'react'
|
| 4 |
import { Lead } from '@/lib/api'
|
| 5 |
import { cn } from '@/lib/utils'
|
| 6 |
+
import { Search, Flame, Inbox, CheckCircle2, MessageCircle, Mail, Linkedin, Trash2, X, Brain, RotateCcw } from 'lucide-react'
|
| 7 |
|
| 8 |
interface LeadsListProps {
|
| 9 |
leads: Lead[] | undefined
|
|
|
|
| 11 |
selectedLeadId: string | undefined
|
| 12 |
onSelectLead: (lead: Lead) => void
|
| 13 |
onDeleteLead?: (leadId: string, clearCache: boolean) => void
|
| 14 |
+
onResetLead?: (leadId: string) => void
|
| 15 |
searchQuery: string
|
| 16 |
setSearchQuery: (query: string) => void
|
| 17 |
activeTab: 'all' | 'hot' | 'unread' | 'whatsapp' | 'email' | 'linkedin'
|
|
|
|
| 24 |
selectedLeadId,
|
| 25 |
onSelectLead,
|
| 26 |
onDeleteLead,
|
| 27 |
+
onResetLead,
|
| 28 |
searchQuery,
|
| 29 |
setSearchQuery,
|
| 30 |
activeTab,
|
|
|
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
+
const handleResetLead = () => {
|
| 47 |
+
if (deleteModal && onResetLead) {
|
| 48 |
+
onResetLead(deleteModal.leadId)
|
| 49 |
+
setDeleteModal(null)
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
// Filter Logic
|
| 54 |
const filteredLeads = leads?.filter(lead => {
|
| 55 |
const matchesSearch = (lead.name || '').toLowerCase().includes(searchQuery.toLowerCase())
|
|
|
|
| 83 |
|
| 84 |
return (
|
| 85 |
<>
|
| 86 |
+
{/* Delete/Reset Confirmation Modal */}
|
| 87 |
{deleteModal && (
|
| 88 |
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
|
| 89 |
<div className="bg-[#1A1B26] rounded-2xl border border-white/10 p-6 max-w-md w-full shadow-2xl">
|
| 90 |
<div className="flex items-center justify-between mb-4">
|
| 91 |
+
<h3 className="text-lg font-bold text-white">Управление чатом</h3>
|
| 92 |
<button
|
| 93 |
onClick={() => { setDeleteModal(null); setClearCacheOnDelete(false) }}
|
| 94 |
className="p-1 hover:bg-white/10 rounded-lg text-slate-400 hover:text-white"
|
|
|
|
| 97 |
</button>
|
| 98 |
</div>
|
| 99 |
|
| 100 |
+
<p className="text-slate-400 text-sm mb-6">
|
| 101 |
+
Выберите действие для чата с <span className="font-semibold text-white">"{deleteModal.leadName}"</span>
|
|
|
|
| 102 |
</p>
|
| 103 |
|
| 104 |
+
{/* Reset Option */}
|
| 105 |
+
{onResetLead && (
|
| 106 |
+
<button
|
| 107 |
+
onClick={handleResetLead}
|
| 108 |
+
className="w-full flex items-start gap-3 p-4 bg-indigo-500/10 rounded-xl border border-indigo-500/20 hover:border-indigo-500/40 transition-colors mb-3 text-left"
|
| 109 |
+
>
|
| 110 |
+
<div className="p-2 bg-indigo-500/20 rounded-lg">
|
| 111 |
+
<RotateCcw className="w-5 h-5 text-indigo-400" />
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
</div>
|
| 113 |
+
<div>
|
| 114 |
+
<div className="text-sm font-bold text-white mb-1">Сбросить чат</div>
|
| 115 |
+
<p className="text-xs text-slate-400">
|
| 116 |
+
Очистит все сообщения и AI-анализ. Контакт останется, новые сообщения начнутся с чистого листа.
|
| 117 |
+
</p>
|
| 118 |
+
</div>
|
| 119 |
+
</button>
|
| 120 |
+
)}
|
| 121 |
+
|
| 122 |
+
{/* Delete Option */}
|
| 123 |
+
<button
|
| 124 |
+
onClick={handleConfirmDelete}
|
| 125 |
+
className="w-full flex items-start gap-3 p-4 bg-red-500/10 rounded-xl border border-red-500/20 hover:border-red-500/40 transition-colors text-left"
|
| 126 |
+
>
|
| 127 |
+
<div className="p-2 bg-red-500/20 rounded-lg">
|
| 128 |
+
<Trash2 className="w-5 h-5 text-red-400" />
|
| 129 |
+
</div>
|
| 130 |
+
<div>
|
| 131 |
+
<div className="text-sm font-bold text-white mb-1">Удалить полностью</div>
|
| 132 |
+
<p className="text-xs text-slate-400">
|
| 133 |
+
Удалит чат, все сообщения и контакт. При новом сообщении создастся новый лид.
|
| 134 |
</p>
|
| 135 |
</div>
|
| 136 |
+
</button>
|
| 137 |
|
| 138 |
+
<button
|
| 139 |
+
onClick={() => { setDeleteModal(null); setClearCacheOnDelete(false) }}
|
| 140 |
+
className="w-full mt-4 px-4 py-2.5 text-sm font-medium text-slate-500 hover:text-white transition-colors"
|
| 141 |
+
>
|
| 142 |
+
Отмена
|
| 143 |
+
</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
</div>
|
| 145 |
</div>
|
| 146 |
)}
|
frontend/lib/api.ts
CHANGED
|
@@ -131,6 +131,11 @@ export const leadsApi = {
|
|
| 131 |
params: { clear_cache: clearCache }
|
| 132 |
})
|
| 133 |
return response.data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
|
|
|
| 131 |
params: { clear_cache: clearCache }
|
| 132 |
})
|
| 133 |
return response.data
|
| 134 |
+
},
|
| 135 |
+
|
| 136 |
+
resetLead: async (leadId: string) => {
|
| 137 |
+
const response = await api.post(`/api/v1/leads/${leadId}/reset`)
|
| 138 |
+
return response.data
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|