2026-07-18 11:21:51 +02:00
|
|
|
"""Mail search provider."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import uuid
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import text
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
2026-08-14 01:34:58 +02:00
|
|
|
from app.plugins.builtins.unified_search.base_provider import BaseSearchProvider
|
|
|
|
|
|
2026-07-18 11:21:51 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2026-08-14 01:34:58 +02:00
|
|
|
class MailSearchProvider(BaseSearchProvider):
|
2026-07-18 11:21:51 +02:00
|
|
|
"""Search provider for Mail entities."""
|
|
|
|
|
|
|
|
|
|
entity_type = "mail"
|
|
|
|
|
|
2026-08-14 01:34:58 +02:00
|
|
|
async def _search_fts_filtered(
|
2026-07-18 11:21:51 +02:00
|
|
|
self,
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
tsquery: str,
|
|
|
|
|
tenant_id: uuid.UUID,
|
|
|
|
|
limit: int,
|
2026-08-14 01:34:58 +02:00
|
|
|
visible_ids: set[uuid.UUID] | None,
|
2026-07-18 11:21:51 +02:00
|
|
|
) -> list[dict[str, Any]]:
|
2026-08-14 01:34:58 +02:00
|
|
|
"""Full-text search on mails.body_tsv, filtered by visible_ids.
|
|
|
|
|
|
|
|
|
|
If visible_ids is None, no visibility filter is applied (system admin).
|
|
|
|
|
"""
|
|
|
|
|
if visible_ids is not None:
|
|
|
|
|
sql = text(
|
|
|
|
|
"""
|
|
|
|
|
SELECT m.*, ts_rank(m.body_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
|
|
|
|
|
FROM mails m
|
|
|
|
|
WHERE m.tenant_id = :tid
|
|
|
|
|
AND m.deleted_at IS NULL
|
|
|
|
|
AND m.body_tsv @@ to_tsquery('pg_catalog.german', :q)
|
|
|
|
|
AND m.id = ANY(:visible_ids)
|
|
|
|
|
ORDER BY rank DESC
|
|
|
|
|
LIMIT :lim
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
sql,
|
|
|
|
|
{
|
|
|
|
|
"q": tsquery,
|
|
|
|
|
"tid": tenant_id,
|
|
|
|
|
"lim": limit,
|
|
|
|
|
"visible_ids": list(visible_ids),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
sql = text(
|
|
|
|
|
"""
|
|
|
|
|
SELECT m.*, ts_rank(m.body_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
|
|
|
|
|
FROM mails m
|
|
|
|
|
WHERE m.tenant_id = :tid
|
|
|
|
|
AND m.deleted_at IS NULL
|
|
|
|
|
AND m.body_tsv @@ to_tsquery('pg_catalog.german', :q)
|
|
|
|
|
ORDER BY rank DESC
|
|
|
|
|
LIMIT :lim
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
sql,
|
|
|
|
|
{"q": tsquery, "tid": tenant_id, "lim": limit},
|
|
|
|
|
)
|
2026-07-18 11:21:51 +02:00
|
|
|
rows = result.mappings().all()
|
|
|
|
|
return [dict(r) for r in rows]
|
|
|
|
|
|
2026-08-14 01:34:58 +02:00
|
|
|
async def _search_vector_filtered(
|
2026-07-18 11:21:51 +02:00
|
|
|
self,
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
embedding: list[float],
|
|
|
|
|
tenant_id: uuid.UUID,
|
|
|
|
|
limit: int,
|
2026-08-14 01:34:58 +02:00
|
|
|
visible_ids: set[uuid.UUID] | None,
|
2026-07-18 11:21:51 +02:00
|
|
|
) -> list[dict[str, Any]]:
|
2026-08-14 01:34:58 +02:00
|
|
|
"""Semantic search on mails.embedding, filtered by visible_ids.
|
|
|
|
|
|
|
|
|
|
If visible_ids is None, no visibility filter is applied (system admin).
|
|
|
|
|
"""
|
|
|
|
|
if visible_ids is not None:
|
|
|
|
|
sql = text(
|
|
|
|
|
"""
|
|
|
|
|
SELECT m.*, 1 - (m.embedding <=> cast(:emb AS vector)) AS score
|
|
|
|
|
FROM mails m
|
|
|
|
|
WHERE m.tenant_id = :tid
|
|
|
|
|
AND m.deleted_at IS NULL
|
|
|
|
|
AND m.embedding IS NOT NULL
|
|
|
|
|
AND m.id = ANY(:visible_ids)
|
|
|
|
|
ORDER BY m.embedding <=> cast(:emb AS vector)
|
|
|
|
|
LIMIT :lim
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
sql,
|
|
|
|
|
{
|
|
|
|
|
"emb": str(embedding),
|
|
|
|
|
"tid": tenant_id,
|
|
|
|
|
"lim": limit,
|
|
|
|
|
"visible_ids": list(visible_ids),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
sql = text(
|
|
|
|
|
"""
|
|
|
|
|
SELECT m.*, 1 - (m.embedding <=> cast(:emb AS vector)) AS score
|
|
|
|
|
FROM mails m
|
|
|
|
|
WHERE m.tenant_id = :tid
|
|
|
|
|
AND m.deleted_at IS NULL
|
|
|
|
|
AND m.embedding IS NOT NULL
|
|
|
|
|
ORDER BY m.embedding <=> cast(:emb AS vector)
|
|
|
|
|
LIMIT :lim
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
sql,
|
|
|
|
|
{"emb": str(embedding), "tid": tenant_id, "lim": limit},
|
|
|
|
|
)
|
2026-07-18 11:21:51 +02:00
|
|
|
rows = result.mappings().all()
|
|
|
|
|
return [dict(r) for r in rows]
|
|
|
|
|
|
|
|
|
|
async def get_embedding_text(
|
2026-08-14 01:34:58 +02:00
|
|
|
self, db: AsyncSession, entity_id: uuid.UUID, tenant_id: uuid.UUID
|
2026-07-18 11:21:51 +02:00
|
|
|
) -> str:
|
|
|
|
|
"""Get text for embedding generation."""
|
|
|
|
|
sql = text(
|
|
|
|
|
"""
|
|
|
|
|
SELECT subject, body_text
|
|
|
|
|
FROM mails
|
|
|
|
|
WHERE id = :eid AND tenant_id = :tid
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
result = await db.execute(sql, {"eid": entity_id, "tid": tenant_id})
|
|
|
|
|
row = result.mappings().first()
|
|
|
|
|
if not row:
|
|
|
|
|
return ""
|
|
|
|
|
subject = row.get("subject", "") or ""
|
|
|
|
|
body = row.get("body_text", "") or ""
|
|
|
|
|
return f"{subject} {body[:5000]}"
|
|
|
|
|
|
|
|
|
|
def to_search_result(self, entity: object) -> dict[str, Any]:
|
|
|
|
|
"""Convert mail to search result dict."""
|
|
|
|
|
if isinstance(entity, dict):
|
|
|
|
|
subject = entity.get("subject", "")
|
|
|
|
|
body = entity.get("body_text", "") or ""
|
|
|
|
|
entity_id = str(entity.get("id", ""))
|
|
|
|
|
else:
|
|
|
|
|
subject = getattr(entity, "subject", "")
|
|
|
|
|
body = getattr(entity, "body_text", "") or ""
|
|
|
|
|
entity_id = str(getattr(entity, "id", ""))
|
|
|
|
|
return {
|
|
|
|
|
"entity_type": self.entity_type,
|
|
|
|
|
"entity_id": entity_id,
|
|
|
|
|
"title": subject,
|
|
|
|
|
"snippet": body[:200],
|
|
|
|
|
"score": 0.0,
|
|
|
|
|
"data": {},
|
|
|
|
|
}
|