sprint2+3: remaining services visibility filter + search provider permission-aware + dashboard route
Check Cross-Plugin Imports / check (push) Has been cancelled
Check Cross-Plugin Imports / check (push) Has been cancelled
This commit is contained in:
@@ -9,71 +9,124 @@ from typing import Any
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.plugins.builtins.unified_search.base_provider import BaseSearchProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ContactSearchProvider:
|
||||
class ContactSearchProvider(BaseSearchProvider):
|
||||
"""Search provider for Contact entities (all types)."""
|
||||
|
||||
entity_type = "contact"
|
||||
|
||||
async def search_fts(
|
||||
async def _search_fts_filtered(
|
||||
self,
|
||||
db: AsyncSession,
|
||||
tsquery: str,
|
||||
tenant_id: uuid.UUID,
|
||||
limit: int,
|
||||
visible_ids: set[uuid.UUID] | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Full-text search on contacts.search_tsv."""
|
||||
sql = text(
|
||||
"""
|
||||
SELECT c.*, ts_rank(c.search_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
|
||||
FROM contacts c
|
||||
WHERE c.tenant_id = :tid
|
||||
AND c.deleted_at IS NULL
|
||||
AND c.search_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},
|
||||
)
|
||||
"""Full-text search on contacts.search_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 c.*, ts_rank(c.search_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
|
||||
FROM contacts c
|
||||
WHERE c.tenant_id = :tid
|
||||
AND c.deleted_at IS NULL
|
||||
AND c.search_tsv @@ to_tsquery('pg_catalog.german', :q)
|
||||
AND c.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 c.*, ts_rank(c.search_tsv, to_tsquery('pg_catalog.german', :q)) AS rank
|
||||
FROM contacts c
|
||||
WHERE c.tenant_id = :tid
|
||||
AND c.deleted_at IS NULL
|
||||
AND c.search_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},
|
||||
)
|
||||
rows = result.mappings().all()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
async def search_vector(
|
||||
async def _search_vector_filtered(
|
||||
self,
|
||||
db: AsyncSession,
|
||||
embedding: list[float],
|
||||
tenant_id: uuid.UUID,
|
||||
limit: int,
|
||||
visible_ids: set[uuid.UUID] | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Semantic search on contacts.embedding."""
|
||||
sql = text(
|
||||
"""
|
||||
SELECT c.*, 1 - (c.embedding <=> cast(:emb AS vector)) AS score
|
||||
FROM contacts c
|
||||
WHERE c.tenant_id = :tid
|
||||
AND c.deleted_at IS NULL
|
||||
AND c.embedding IS NOT NULL
|
||||
ORDER BY c.embedding <=> cast(:emb AS vector)
|
||||
LIMIT :lim
|
||||
"""
|
||||
)
|
||||
result = await db.execute(
|
||||
sql,
|
||||
{"emb": str(embedding), "tid": tenant_id, "lim": limit},
|
||||
)
|
||||
"""Semantic search on contacts.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 c.*, 1 - (c.embedding <=> cast(:emb AS vector)) AS score
|
||||
FROM contacts c
|
||||
WHERE c.tenant_id = :tid
|
||||
AND c.deleted_at IS NULL
|
||||
AND c.embedding IS NOT NULL
|
||||
AND c.id = ANY(:visible_ids)
|
||||
ORDER BY c.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 c.*, 1 - (c.embedding <=> cast(:emb AS vector)) AS score
|
||||
FROM contacts c
|
||||
WHERE c.tenant_id = :tid
|
||||
AND c.deleted_at IS NULL
|
||||
AND c.embedding IS NOT NULL
|
||||
ORDER BY c.embedding <=> cast(:emb AS vector)
|
||||
LIMIT :lim
|
||||
"""
|
||||
)
|
||||
result = await db.execute(
|
||||
sql,
|
||||
{"emb": str(embedding), "tid": tenant_id, "lim": limit},
|
||||
)
|
||||
rows = result.mappings().all()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
async def get_embedding_text(
|
||||
self,
|
||||
db: AsyncSession,
|
||||
entity_id: uuid.UUID,
|
||||
tenant_id: uuid.UUID,
|
||||
self, db: AsyncSession, entity_id: uuid.UUID, tenant_id: uuid.UUID
|
||||
) -> str:
|
||||
"""Get text for embedding generation."""
|
||||
sql = text(
|
||||
|
||||
@@ -51,17 +51,20 @@ async def search(
|
||||
"""Perform hybrid search with KI query understanding."""
|
||||
tenant_id = uuid.UUID(current_user["tenant_id"])
|
||||
user_id = uuid.UUID(current_user["user_id"])
|
||||
is_system_admin = current_user.get("is_system_admin", False)
|
||||
|
||||
# KI query understanding
|
||||
query_analysis = await llm_analyze_query(req.query, db=db, tenant_id=tenant_id)
|
||||
|
||||
# Hybrid search
|
||||
# Hybrid search with visibility filtering
|
||||
results = await hybrid_search(
|
||||
db=db,
|
||||
query_analysis=query_analysis,
|
||||
tenant_id=tenant_id,
|
||||
entity_types=req.entity_types,
|
||||
limit=req.limit,
|
||||
user_id=user_id,
|
||||
is_system_admin=is_system_admin,
|
||||
)
|
||||
|
||||
# Resolve user permissions for field-level RBAC
|
||||
|
||||
@@ -68,11 +68,15 @@ async def hybrid_search(
|
||||
tenant_id: uuid.UUID,
|
||||
entity_types: list[str] | None = None,
|
||||
limit: int = 20,
|
||||
user_id: uuid.UUID | None = None,
|
||||
is_system_admin: bool = False,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Perform hybrid search across all entity types.
|
||||
|
||||
For each entity: FTS search + vector search + RRF fusion.
|
||||
Merge all results, sort by fused score, return top limit.
|
||||
|
||||
Passes user_id and is_system_admin to each provider for visibility filtering.
|
||||
"""
|
||||
registry = get_search_registry()
|
||||
all_entity_types = entity_types or registry.get_entity_types()
|
||||
@@ -108,13 +112,13 @@ async def hybrid_search(
|
||||
vec_results: list[dict[str, Any]] = []
|
||||
|
||||
try:
|
||||
fts_results = await provider.search_fts(db, tsquery, tenant_id, fetch_limit)
|
||||
fts_results = await provider.search_fts(db, tsquery, tenant_id, fetch_limit, user_id=user_id, is_system_admin=is_system_admin)
|
||||
except Exception:
|
||||
logger.exception("FTS search failed for %s", entity_type)
|
||||
|
||||
if query_embedding:
|
||||
try:
|
||||
vec_results = await provider.search_vector(db, query_embedding, tenant_id, fetch_limit)
|
||||
vec_results = await provider.search_vector(db, query_embedding, tenant_id, fetch_limit, user_id=user_id, is_system_admin=is_system_admin)
|
||||
except Exception:
|
||||
logger.exception("Vector search failed for %s", entity_type)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user