human handoffs

This commit is contained in:
debu-2 2026-10-06 01:51:23 +05:30
parent 79188e0f59
commit 36a55930f9
35 changed files with 4039 additions and 139 deletions

View file

@ -8,6 +8,7 @@ from app.config import get_settings
from app.database import SessionFactory, engine, mongo_client, mongo_db
from app.models import Base
from app.routers import (
agent_handoff,
aliases,
auth,
dns_setup,
@ -18,7 +19,9 @@ from app.routers import (
people,
sections,
sticky_notes,
threads,
)
from app.services.thread_service import create_thread, message_event_at
from app.services.domain_provisioning import ensure_platform_domain
from app.services.inbound_smtp import InboundSMTPServer
@ -26,6 +29,25 @@ settings = get_settings()
inbound_server = InboundSMTPServer()
async def backfill_missing_thread_ids(database) -> None:
cursor = database.mailbox_entries.find({"thread_id": None})
async for entry in cursor:
message = await database.messages.find_one({"_id": entry["message_id"]})
if message is None:
continue
thread_id = await create_thread(
database,
account_id=entry["account_id"],
subject=entry.get("subject") or message.get("subject", ""),
message_id=entry["message_id"],
event_at=entry.get("event_at") or message_event_at(message),
)
await database.mailbox_entries.update_one(
{"_id": entry["_id"]},
{"$set": {"thread_id": thread_id}},
)
async def migrate_mail_group_columns(connection) -> None:
await connection.execute(
text(
@ -168,6 +190,39 @@ async def lifespan(_: FastAPI):
],
name="mailbox_search",
)
await mongo_db.messages.create_index(
[("message_id", 1)],
sparse=True,
)
await mongo_db.mailbox_entries.create_index(
[("account_id", 1), ("thread_id", 1), ("event_at", -1), ("_id", -1)]
)
await mongo_db.email_threads.create_index(
[("account_id", 1), ("last_message_at", -1), ("_id", -1)]
)
await mongo_db.human_handoff_rules.create_index(
[("account_id", 1), ("updated_at", -1)]
)
await mongo_db.human_handoffs.create_index(
[("account_id", 1), ("updated_at", -1)]
)
await mongo_db.human_handoffs.create_index(
[("account_id", 1), ("external_thread_id", 1)]
)
await mongo_db.human_handoffs.create_index(
[("account_id", 1), ("handoff_thread_id", 1)]
)
await mongo_db.handoff_message_links.create_index(
[("handoff_id", 1), ("created_at", -1)]
)
await mongo_db.mailbox_entries.create_index(
[("account_id", 1), ("message_scope", 1), ("mailbox", 1), ("event_at", -1)]
)
await mongo_db.messages.create_index(
[("message_id", 1), ("message_scope", 1)],
sparse=True,
)
await backfill_missing_thread_ids(mongo_db)
if settings.inbound_smtp_enabled:
await inbound_server.start()
try:
@ -192,6 +247,8 @@ app.add_middleware(
allow_headers=["*"],
)
app.include_router(emails.router, prefix=settings.api_prefix)
app.include_router(threads.router, prefix=settings.api_prefix)
app.include_router(agent_handoff.router, prefix=settings.api_prefix)
app.include_router(sticky_notes.router, prefix=settings.api_prefix)
app.include_router(auth.router, prefix=settings.api_prefix)
app.include_router(domains.router, prefix=settings.api_prefix)