feat: add account management, email verification, AI fixes, and user guides
- Profile settings, account transfer, delete/leave account flows - Email verification with JWT tokens and Resend integration - AI assistant/copilot fixes: markdown rendering, shared RAG helpers, token tracking, input refocus, model_validate usage - User guides hub + detail pages with 13 topic guides - Sidebar and top bar navigation for guides Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.models.assistant_chat import AssistantChat
|
||||
from app.services import rag_service
|
||||
from app.services.rag_service import search as rag_search, build_rag_context, extract_suggested_flows
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -33,36 +33,6 @@ When answering:
|
||||
"""
|
||||
|
||||
|
||||
def _build_rag_context(rag_results: list[dict[str, Any]]) -> str:
|
||||
"""Format RAG results into a system prompt section."""
|
||||
if not rag_results:
|
||||
return ""
|
||||
|
||||
parts = ["\n--- RELEVANT FLOWS FROM TEAM LIBRARY ---"]
|
||||
for r in rag_results[:5]:
|
||||
parts.append(f"- [{r['tree_type']}] {r['tree_name']}: {r['chunk_text'][:200]}")
|
||||
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
def _extract_suggested_flows(rag_results: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""Extract unique suggested flows from RAG results."""
|
||||
seen: set[str] = set()
|
||||
suggestions = []
|
||||
for r in rag_results:
|
||||
tid = r["tree_id"]
|
||||
if tid in seen or r["similarity"] < 0.3:
|
||||
continue
|
||||
seen.add(tid)
|
||||
suggestions.append({
|
||||
"tree_id": tid,
|
||||
"tree_name": r["tree_name"],
|
||||
"tree_type": r["tree_type"],
|
||||
"relevance_snippet": r["chunk_text"][:150],
|
||||
})
|
||||
return suggestions[:3]
|
||||
|
||||
|
||||
def _auto_title(message: str) -> str:
|
||||
"""Generate a short title from the first user message."""
|
||||
title = message.strip()[:100]
|
||||
@@ -113,7 +83,7 @@ async def send_message(
|
||||
chat.title = _auto_title(message)
|
||||
|
||||
# RAG search
|
||||
rag_results = await rag_service.search(
|
||||
rag_results = await rag_search(
|
||||
query=message,
|
||||
account_id=account_id,
|
||||
db=db,
|
||||
@@ -121,7 +91,7 @@ async def send_message(
|
||||
)
|
||||
|
||||
# Build system prompt
|
||||
system_prompt = ASSISTANT_SYSTEM_PROMPT + _build_rag_context(rag_results)
|
||||
system_prompt = ASSISTANT_SYSTEM_PROMPT + build_rag_context(rag_results)
|
||||
|
||||
# Build messages for AI
|
||||
ai_messages = []
|
||||
@@ -147,6 +117,6 @@ async def send_message(
|
||||
chat.total_input_tokens += input_tokens
|
||||
chat.total_output_tokens += output_tokens
|
||||
|
||||
suggested_flows = _extract_suggested_flows(rag_results)
|
||||
suggested_flows = extract_suggested_flows(rag_results)
|
||||
|
||||
return ai_content, suggested_flows, chat
|
||||
|
||||
@@ -15,7 +15,7 @@ from sqlalchemy.orm import selectinload
|
||||
from app.core.ai_provider import get_ai_provider
|
||||
from app.models.tree import Tree
|
||||
from app.models.copilot_conversation import CopilotConversation
|
||||
from app.services import rag_service
|
||||
from app.services.rag_service import search as rag_search, build_rag_context, extract_suggested_flows
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -83,45 +83,6 @@ def _find_node(structure: dict, node_id: str) -> Optional[dict]:
|
||||
return None
|
||||
|
||||
|
||||
def _build_rag_context(rag_results: list[dict[str, Any]]) -> str:
|
||||
"""Format RAG results into a system prompt section."""
|
||||
if not rag_results:
|
||||
return ""
|
||||
|
||||
parts = ["\n--- RELEVANT FLOWS FROM TEAM LIBRARY ---"]
|
||||
for r in rag_results[:5]: # Cap at 5 for prompt size
|
||||
parts.append(f"- [{r['tree_type']}] {r['tree_name']}: {r['chunk_text'][:200]}")
|
||||
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
def _extract_suggested_flows(
|
||||
rag_results: list[dict[str, Any]],
|
||||
exclude_tree_id: Optional[UUID] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Extract unique suggested flows from RAG results."""
|
||||
seen_tree_ids: set[str] = set()
|
||||
suggestions = []
|
||||
|
||||
for r in rag_results:
|
||||
tid = r["tree_id"]
|
||||
if exclude_tree_id and tid == str(exclude_tree_id):
|
||||
continue
|
||||
if tid in seen_tree_ids:
|
||||
continue
|
||||
if r["similarity"] < 0.3:
|
||||
continue
|
||||
seen_tree_ids.add(tid)
|
||||
suggestions.append({
|
||||
"tree_id": tid,
|
||||
"tree_name": r["tree_name"],
|
||||
"tree_type": r["tree_type"],
|
||||
"relevance_snippet": r["chunk_text"][:150],
|
||||
})
|
||||
|
||||
return suggestions[:3]
|
||||
|
||||
|
||||
async def start_conversation(
|
||||
user_id: UUID,
|
||||
account_id: UUID,
|
||||
@@ -168,10 +129,10 @@ async def send_message(
|
||||
message: str,
|
||||
current_node_id: Optional[str],
|
||||
db: AsyncSession,
|
||||
) -> tuple[str, list[dict[str, Any]]]:
|
||||
) -> tuple[str, list[dict[str, Any]], CopilotConversation]:
|
||||
"""Send a user message and get AI response.
|
||||
|
||||
Returns (ai_content, suggested_flows).
|
||||
Returns (ai_content, suggested_flows, conversation).
|
||||
"""
|
||||
result = await db.execute(
|
||||
select(CopilotConversation).where(
|
||||
@@ -199,7 +160,7 @@ async def send_message(
|
||||
conversation.current_node_id = current_node_id
|
||||
|
||||
# RAG search
|
||||
rag_results = await rag_service.search(
|
||||
rag_results = await rag_search(
|
||||
query=message,
|
||||
account_id=conversation.account_id,
|
||||
db=db,
|
||||
@@ -209,7 +170,7 @@ async def send_message(
|
||||
# Build system prompt
|
||||
system_prompt = COPILOT_SYSTEM_PROMPT
|
||||
system_prompt += _build_flow_context(tree, conversation.current_node_id)
|
||||
system_prompt += _build_rag_context(rag_results)
|
||||
system_prompt += build_rag_context(rag_results)
|
||||
|
||||
# Build messages for AI
|
||||
ai_messages = []
|
||||
@@ -236,6 +197,6 @@ async def send_message(
|
||||
conversation.total_output_tokens += output_tokens
|
||||
|
||||
# Extract suggested flows
|
||||
suggested_flows = _extract_suggested_flows(rag_results, exclude_tree_id=tree.id)
|
||||
suggested_flows = extract_suggested_flows(rag_results, exclude_tree_id=tree.id)
|
||||
|
||||
return ai_content, suggested_flows
|
||||
return ai_content, suggested_flows, conversation
|
||||
|
||||
@@ -168,3 +168,42 @@ async def search(
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
|
||||
|
||||
def build_rag_context(rag_results: list[dict[str, Any]]) -> str:
|
||||
"""Format RAG results into a system prompt section."""
|
||||
if not rag_results:
|
||||
return ""
|
||||
|
||||
parts = ["\n--- RELEVANT FLOWS FROM TEAM LIBRARY ---"]
|
||||
for r in rag_results[:5]: # Cap at 5 for prompt size
|
||||
parts.append(f"- [{r['tree_type']}] {r['tree_name']}: {r['chunk_text'][:200]}")
|
||||
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
def extract_suggested_flows(
|
||||
rag_results: list[dict[str, Any]],
|
||||
exclude_tree_id: Optional[UUID] = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Extract unique suggested flows from RAG results."""
|
||||
seen_tree_ids: set[str] = set()
|
||||
suggestions = []
|
||||
|
||||
for r in rag_results:
|
||||
tid = r["tree_id"]
|
||||
if exclude_tree_id and tid == str(exclude_tree_id):
|
||||
continue
|
||||
if tid in seen_tree_ids:
|
||||
continue
|
||||
if r["similarity"] < 0.3:
|
||||
continue
|
||||
seen_tree_ids.add(tid)
|
||||
suggestions.append({
|
||||
"tree_id": tid,
|
||||
"tree_name": r["tree_name"],
|
||||
"tree_type": r["tree_type"],
|
||||
"relevance_snippet": r["chunk_text"][:150],
|
||||
})
|
||||
|
||||
return suggestions[:3]
|
||||
|
||||
Reference in New Issue
Block a user