Spaces:
Running
Running
axeronis commited on
Commit ·
e90bf86
1
Parent(s): b4cac97
Fix: Add missing _get_booking_context method to AICore
Browse files- backend/app/services/ai_core.py +50 -0
- backend/check_admin_user.py +47 -0
backend/app/services/ai_core.py
CHANGED
|
@@ -10,6 +10,7 @@ from app.models.knowledge import SalesScript
|
|
| 10 |
from app.models.sales_stage import SalesStage # NEW
|
| 11 |
from app.models.ai_training import AITrainingExample, AILearningRule
|
| 12 |
from app.models.crm_integration import CRMIntegration, SyncStatus
|
|
|
|
| 13 |
from sqlmodel import select
|
| 14 |
|
| 15 |
|
|
@@ -408,6 +409,55 @@ class AICore:
|
|
| 408 |
|
| 409 |
return merged
|
| 410 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
async def _get_relevant_context(
|
| 412 |
self,
|
| 413 |
session: AsyncSession,
|
|
|
|
| 10 |
from app.models.sales_stage import SalesStage # NEW
|
| 11 |
from app.models.ai_training import AITrainingExample, AILearningRule
|
| 12 |
from app.models.crm_integration import CRMIntegration, SyncStatus
|
| 13 |
+
from app.models.booking import Service
|
| 14 |
from sqlmodel import select
|
| 15 |
|
| 16 |
|
|
|
|
| 409 |
|
| 410 |
return merged
|
| 411 |
|
| 412 |
+
async def _get_booking_context(
|
| 413 |
+
self,
|
| 414 |
+
session: AsyncSession,
|
| 415 |
+
tenant_id: UUID,
|
| 416 |
+
lead: Lead,
|
| 417 |
+
user_message: str,
|
| 418 |
+
stage: Optional[SalesStage] = None
|
| 419 |
+
) -> str:
|
| 420 |
+
"""
|
| 421 |
+
Get booking/appointment context if relevant to the current stage or user intent.
|
| 422 |
+
|
| 423 |
+
Returns:
|
| 424 |
+
String with available services/slots, or empty string if not relevant.
|
| 425 |
+
"""
|
| 426 |
+
# 1. Check if relevant (Stage is "Demo"/"Meeting" or user asks "schedule"/"book")
|
| 427 |
+
is_relevant = False
|
| 428 |
+
|
| 429 |
+
# Check explicit intent keywords
|
| 430 |
+
user_lower = user_message.lower()
|
| 431 |
+
if any(w in user_lower for w in ["записаться", "book", "schedule", "назначить", "встреч", "демо", "созвон"]):
|
| 432 |
+
is_relevant = True
|
| 433 |
+
|
| 434 |
+
# Check stage intent (if stage dictates booking)
|
| 435 |
+
if stage and any(w in stage.name.lower() for w in ["demo", "meeting", "встреча", "демо", "созвон"]):
|
| 436 |
+
is_relevant = True
|
| 437 |
+
|
| 438 |
+
if not is_relevant:
|
| 439 |
+
return ""
|
| 440 |
+
|
| 441 |
+
# 2. Get active services for tenant
|
| 442 |
+
stmt = select(Service).where(
|
| 443 |
+
Service.tenant_id == tenant_id,
|
| 444 |
+
Service.is_active == True
|
| 445 |
+
)
|
| 446 |
+
result = await session.execute(stmt)
|
| 447 |
+
services = result.scalars().all()
|
| 448 |
+
|
| 449 |
+
if not services:
|
| 450 |
+
return ""
|
| 451 |
+
|
| 452 |
+
# 3. Format context
|
| 453 |
+
context = "AVAILABLE BOOKING SERVICES:\n"
|
| 454 |
+
for service in services:
|
| 455 |
+
context += f"- {service.name} ({service.duration_minutes} min): {service.description or 'No description'}\n"
|
| 456 |
+
|
| 457 |
+
context += "\n(To book, ask the user for their preferred date/time)"
|
| 458 |
+
|
| 459 |
+
return context
|
| 460 |
+
|
| 461 |
async def _get_relevant_context(
|
| 462 |
self,
|
| 463 |
session: AsyncSession,
|
backend/check_admin_user.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Add backend directory to path to import app modules
|
| 7 |
+
backend_path = os.path.abspath(os.path.join(os.getcwd(), 'backend'))
|
| 8 |
+
sys.path.append(backend_path)
|
| 9 |
+
|
| 10 |
+
# Load .env file BEFORE importing app modules
|
| 11 |
+
load_dotenv(os.path.join(backend_path, '.env'))
|
| 12 |
+
|
| 13 |
+
from app.core.database import async_session_factory
|
| 14 |
+
from app.models.user import User
|
| 15 |
+
from app.core.security import get_password_hash
|
| 16 |
+
from sqlalchemy import select
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
async def check_user(email, new_password):
|
| 20 |
+
async with async_session_factory() as session:
|
| 21 |
+
result = await session.execute(select(User).where(User.email == email))
|
| 22 |
+
user = result.scalar_one_or_none()
|
| 23 |
+
|
| 24 |
+
if user:
|
| 25 |
+
print(f"User found: {user.email}")
|
| 26 |
+
print(f"ID: {user.id}")
|
| 27 |
+
print(f"Role: {user.role}")
|
| 28 |
+
print(f"Is Active: {user.is_active}")
|
| 29 |
+
print(f"Tenant ID: {user.tenant_id}")
|
| 30 |
+
|
| 31 |
+
print(f"Resetting password for {email} to {new_password}...")
|
| 32 |
+
user.hashed_password = get_password_hash(new_password)
|
| 33 |
+
user.is_superuser = True # Set superuser flag as requested
|
| 34 |
+
|
| 35 |
+
await session.commit()
|
| 36 |
+
print(f"User updated: Password reset, is_superuser={user.is_superuser}")
|
| 37 |
+
else:
|
| 38 |
+
print(f"User with email {email} NOT found.")
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
if len(sys.argv) < 3:
|
| 42 |
+
print("Usage: python check_admin_user.py <email> <password>")
|
| 43 |
+
sys.exit(1)
|
| 44 |
+
|
| 45 |
+
email = sys.argv[1]
|
| 46 |
+
password = sys.argv[2]
|
| 47 |
+
asyncio.run(check_user(email, password))
|