7f61dfb25b
Check Cross-Plugin Imports / check (push) Has been cancelled
Migration 0137: Drop AI chat tables (ai_chat_sessions, ai_chat_messages, ai_chat_attachments, ai_conversations, ai_messages)
Backend:
- Remove AIChatSession, AIChatMessage, AIChatAttachment models from ai_assistant/models.py
- Remove AIConversation, AIMessage from app/models/__init__.py
- Remove session/message/stream/attachment routes from ai_assistant/routes.py
- Add new streaming route POST /ai/conversations/{conversation_id}/stream using comm tables
- Add new messages route GET /ai/conversations/{conversation_id}/messages using comm tables
- Add stream_chat_comm, get_comm_messages, save_comm_message to services.py
- Update external_api.py to use CommConversation/CommMessage instead of AIChatSession/AIChatMessage
- Update unified_search ai_chat_provider to search comm_messages with conversation_type=ai
- Remove ai_copilot router from main.py and routes/__init__.py
- Remove ai_conversation from entity_permissions.py and owner_transfer_service.py
- Update ai_assistant/plugin.py get_entity_models to remove AIChatSession
- Guard ai_copilot_service.py imports with try/except
Frontend:
- Remove AIAssistant.tsx, AIAssistantStandalone.tsx, SessionList.tsx, ChatWindow.tsx
- Remove AI Assistant routes from routes/index.tsx
- Update api/ai.ts: streamChat uses /ai/conversations/{id}/stream, fetchMessages uses /ai/conversations/{id}/messages
- Update Communication.tsx: use convId for AI streaming, remove aiSessionId, use fetchAiMessages for AI conversations
- Update AiChatPanel.tsx: create comm conversation instead of AI session, use new fetchMessages
- Update AISidebar.tsx: remove ChatWindow import, show placeholder
tsc clean, build successful, backend import OK
138 lines
5.0 KiB
Python
138 lines
5.0 KiB
Python
"""AI chat search provider — FTS search on comm_messages for AI conversations."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import uuid
|
|
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 AIChatSearchProvider(BaseSearchProvider):
|
|
"""Search provider for AI chat messages in comm_messages."""
|
|
|
|
entity_type = "ai_chat"
|
|
supports_fts = True
|
|
supports_vector = False
|
|
|
|
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 comm_messages.content for AI conversations."""
|
|
if visible_ids is not None:
|
|
sql = text(
|
|
"""
|
|
SELECT m.id, m.tenant_id, m.conversation_id, m.sender_type AS role, m.content,
|
|
c.title AS session_title,
|
|
ts_rank(to_tsvector('pg_catalog.german', m.content),
|
|
to_tsquery('pg_catalog.german', :q)) AS rank
|
|
FROM comm_messages m
|
|
JOIN comm_conversations c ON c.id = m.conversation_id
|
|
WHERE m.tenant_id = :tid
|
|
AND c.metadata->>'conversation_type' = 'ai'
|
|
AND to_tsvector('pg_catalog.german', m.content) @@ 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.id, m.tenant_id, m.conversation_id, m.sender_type AS role, m.content,
|
|
c.title AS session_title,
|
|
ts_rank(to_tsvector('pg_catalog.german', m.content),
|
|
to_tsquery('pg_catalog.german', :q)) AS rank
|
|
FROM comm_messages m
|
|
JOIN comm_conversations c ON c.id = m.conversation_id
|
|
WHERE m.tenant_id = :tid
|
|
AND c.metadata->>'conversation_type' = 'ai'
|
|
AND to_tsvector('pg_catalog.german', m.content) @@ 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_filtered(
|
|
self,
|
|
db: AsyncSession,
|
|
embedding: list[float],
|
|
tenant_id: uuid.UUID,
|
|
limit: int,
|
|
visible_ids: set[uuid.UUID] | None,
|
|
) -> list[dict[str, Any]]:
|
|
"""No vector support for chat messages — return empty list."""
|
|
return []
|
|
|
|
async def get_embedding_text(
|
|
self, db: AsyncSession, entity_id: uuid.UUID, tenant_id: uuid.UUID
|
|
) -> str:
|
|
"""Get text for embedding generation — returns message content."""
|
|
sql = text(
|
|
"""
|
|
SELECT content
|
|
FROM comm_messages
|
|
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 ""
|
|
return str(row.get("content", ""))
|
|
|
|
def to_search_result(self, entity: object) -> dict[str, Any]:
|
|
"""Convert chat message to search result dict."""
|
|
if isinstance(entity, dict):
|
|
entity_id = str(entity.get("id", ""))
|
|
content = entity.get("content", "")
|
|
role = entity.get("role", "")
|
|
session_title = entity.get("session_title", "")
|
|
conversation_id = str(entity.get("conversation_id", ""))
|
|
score = entity.get("rank", 0.0)
|
|
else:
|
|
entity_id = str(getattr(entity, "id", ""))
|
|
content = getattr(entity, "content", "")
|
|
role = getattr(entity, "role", "")
|
|
session_title = getattr(entity, "session_title", "")
|
|
conversation_id = str(getattr(entity, "conversation_id", ""))
|
|
score = getattr(entity, "rank", 0.0)
|
|
return {
|
|
"entity_type": self.entity_type,
|
|
"entity_id": entity_id,
|
|
"title": session_title or content[:80] if content else "",
|
|
"snippet": content,
|
|
"score": float(score) if score else 0.0,
|
|
"data": {
|
|
"role": role,
|
|
"conversation_id": conversation_id,
|
|
"session_title": session_title,
|
|
},
|
|
}
|