* chore: update Google Fonts to Bricolage Grotesque, IBM Plex Sans, JetBrains Mono Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: update Tailwind config to Slate & Ice theme colors and fonts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: update CSS variables and glass-card utilities for Slate & Ice theme - Replace all color variables with Slate & Ice palette - Add glass system vars (--glass-bg, --glass-blur, --shadow-float) - Replace legacy glass-card with new variable-driven glass classes - Add breatheGlow, bellWobble, slideDown, fadeInRight keyframes - Update font references to IBM Plex Sans and Bricolage Grotesque Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: recolor BrandLogo to cyan gradient, split BrandWordmark for gradient Flow text Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: update TopBar with glassmorphism backdrop and cyan accent styling Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: update Sidebar with glassmorphism backdrop Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add ambient atmosphere gradient orbs behind app shell Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: update QuickStats and SessionsPanel with glass-card styling Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add WeeklyCalendar, QuickActions, OpenSessions, RecentActivity dashboard components Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: redesign dashboard layout with calendar, open sessions, and glass-card panels New layout: greeting → calendar+actions → sessions+stats → activity Replaces old QuickStats and SessionsPanel with new dashboard components Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: replace remaining purple hex references with ice-cyan accent Sweep of hardcoded purple hex values (#818cf8, #6366f1) replaced with new cyan accent (#06b6d4) in QuickActions, RecentActivity, QuickLaunch, and SVG brand assets. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: update CLAUDE.md branding and design system for Slate & Ice Modern Updated Last Updated date, branding section (fonts, colors, glass utilities, atmosphere orbs), component styling rules, and Design System section to reflect the new ice-cyan glassmorphism theme. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: add Slate & Ice Modern design doc and implementation plan Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: redesign login page with Slate & Ice Modern design system Apply glassmorphism styling, atmosphere orbs, branded wordmark, and consistent design tokens to match the updated app shell aesthetic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: raise TopBar z-index so profile dropdown renders above main content Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add AI assistant with in-session copilot and standalone chat with RAG Implements three-phase AI assistant feature: - Phase 0: RAG infrastructure with pgvector embeddings, Voyage AI integration, tree chunking service, and semantic search over team's flow library - Phase 1: In-session copilot panel during flow navigation with contextual AI help, current step awareness, and suggested related flows - Phase 2: Standalone AI chat page with persistent conversation history, pin/delete, and configurable retention policies (account-level) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * 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> * fix: prevent stale chunk errors after deployments - Set Cache-Control no-cache on index.html in nginx so browsers always fetch fresh chunk references after a deploy - Auto-reload on chunk load failures (stale deploy detection) with loop prevention via sessionStorage - Show friendly "App Updated" message if auto-reload doesn't resolve it Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add email verification toggle to admin settings Adds platform-level toggle to enable/disable email verification. When disabled, the verification banner is hidden and the send endpoint returns 403. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
193 lines
6.6 KiB
Python
193 lines
6.6 KiB
Python
"""In-session copilot endpoints.
|
|
|
|
Contextual AI assistant during flow navigation:
|
|
POST /copilot/conversations — Start conversation (requires tree_id)
|
|
POST /copilot/conversations/{id}/messages — Send message, get response + suggestions
|
|
GET /copilot/conversations/{id} — Get conversation history
|
|
"""
|
|
import logging
|
|
from typing import Annotated
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.rate_limit import limiter
|
|
from app.api.deps import get_current_active_user, get_db, require_engineer_or_admin
|
|
from app.core.config import settings
|
|
from app.core.ai_quota_service import check_ai_quota, record_ai_usage, get_user_plan
|
|
from app.models.user import User
|
|
from app.schemas.copilot import (
|
|
CopilotStartRequest,
|
|
CopilotStartResponse,
|
|
CopilotMessageRequest,
|
|
CopilotMessageResponse,
|
|
CopilotConversationResponse,
|
|
SuggestedFlow,
|
|
)
|
|
from app.models.copilot_conversation import CopilotConversation
|
|
from app.services import copilot_service
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(prefix="/copilot", tags=["copilot"])
|
|
|
|
|
|
def _require_ai_enabled() -> None:
|
|
if not settings.ai_enabled:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="AI is not configured. Set GOOGLE_AI_API_KEY or ANTHROPIC_API_KEY.",
|
|
)
|
|
|
|
|
|
@router.post("/conversations", response_model=CopilotStartResponse, status_code=201)
|
|
@limiter.limit("10/minute")
|
|
async def start_conversation(
|
|
request: Request,
|
|
data: CopilotStartRequest,
|
|
current_user: Annotated[User, Depends(get_current_active_user)],
|
|
db: Annotated[AsyncSession, Depends(get_db)],
|
|
_: None = Depends(require_engineer_or_admin),
|
|
):
|
|
"""Start a new copilot conversation for a flow."""
|
|
_require_ai_enabled()
|
|
|
|
allowed, quota_status = await check_ai_quota(
|
|
user_id=current_user.id,
|
|
account_id=current_user.account_id,
|
|
db=db,
|
|
billing_anchor=current_user.ai_billing_cycle_anchor_at,
|
|
is_super_admin=current_user.is_super_admin,
|
|
)
|
|
if not allowed:
|
|
reset_key = "daily_reset_at" if quota_status.get("deny_reason") == "daily" else "monthly_reset_at"
|
|
raise HTTPException(
|
|
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
|
detail={
|
|
"message": f"AI limit exceeded ({quota_status['deny_reason']})",
|
|
"reset_at": quota_status.get(reset_key),
|
|
"quota": quota_status,
|
|
},
|
|
)
|
|
|
|
try:
|
|
conversation, greeting = await copilot_service.start_conversation(
|
|
user_id=current_user.id,
|
|
account_id=current_user.account_id,
|
|
tree_id=data.tree_id,
|
|
session_id=data.session_id,
|
|
current_node_id=data.current_node_id,
|
|
db=db,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
|
|
except Exception as e:
|
|
logger.exception("Copilot conversation start failed: %s", e)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_502_BAD_GATEWAY,
|
|
detail=f"AI provider error ({type(e).__name__}). Please try again.",
|
|
)
|
|
|
|
await db.commit()
|
|
|
|
return CopilotStartResponse(
|
|
conversation_id=conversation.id,
|
|
greeting=greeting,
|
|
)
|
|
|
|
|
|
@router.post("/conversations/{conversation_id}/messages", response_model=CopilotMessageResponse)
|
|
@limiter.limit("10/minute")
|
|
async def post_message(
|
|
request: Request,
|
|
conversation_id: UUID,
|
|
data: CopilotMessageRequest,
|
|
current_user: Annotated[User, Depends(get_current_active_user)],
|
|
db: Annotated[AsyncSession, Depends(get_db)],
|
|
_: None = Depends(require_engineer_or_admin),
|
|
):
|
|
"""Send a message and get AI response with flow suggestions."""
|
|
_require_ai_enabled()
|
|
|
|
plan = await get_user_plan(current_user.account_id, db)
|
|
|
|
try:
|
|
ai_content, suggested_flows, conversation = await copilot_service.send_message(
|
|
conversation_id=conversation_id,
|
|
user_id=current_user.id,
|
|
message=data.message,
|
|
current_node_id=data.current_node_id,
|
|
db=db,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
|
|
except Exception as e:
|
|
logger.exception("Copilot message failed: %s", e)
|
|
await record_ai_usage(
|
|
user_id=current_user.id,
|
|
account_id=current_user.account_id,
|
|
conversation_id=None,
|
|
generation_type="copilot_message",
|
|
tier=plan,
|
|
input_tokens=0,
|
|
output_tokens=0,
|
|
estimated_cost=0,
|
|
succeeded=False,
|
|
counts_toward_quota=False,
|
|
error_code=type(e).__name__,
|
|
extra_data={"copilot_conversation_id": str(conversation_id)},
|
|
db=db,
|
|
)
|
|
await db.commit()
|
|
raise HTTPException(
|
|
status_code=status.HTTP_502_BAD_GATEWAY,
|
|
detail=f"AI provider error ({type(e).__name__}). Please try again.",
|
|
)
|
|
|
|
await record_ai_usage(
|
|
user_id=current_user.id,
|
|
account_id=current_user.account_id,
|
|
conversation_id=None,
|
|
generation_type="copilot_message",
|
|
tier=plan,
|
|
input_tokens=conversation.total_input_tokens,
|
|
output_tokens=conversation.total_output_tokens,
|
|
estimated_cost=(
|
|
conversation.total_input_tokens * 1.0 / 1_000_000
|
|
+ conversation.total_output_tokens * 5.0 / 1_000_000
|
|
),
|
|
succeeded=True,
|
|
counts_toward_quota=False,
|
|
error_code=None,
|
|
extra_data={"copilot_conversation_id": str(conversation_id)},
|
|
db=db,
|
|
)
|
|
await db.commit()
|
|
|
|
return CopilotMessageResponse(
|
|
content=ai_content,
|
|
suggested_flows=[SuggestedFlow.model_validate(sf) for sf in suggested_flows],
|
|
)
|
|
|
|
|
|
@router.get("/conversations/{conversation_id}", response_model=CopilotConversationResponse)
|
|
async def get_conversation(
|
|
conversation_id: UUID,
|
|
current_user: Annotated[User, Depends(get_current_active_user)],
|
|
db: Annotated[AsyncSession, Depends(get_db)],
|
|
):
|
|
"""Get copilot conversation history."""
|
|
result = await db.execute(
|
|
select(CopilotConversation).where(
|
|
CopilotConversation.id == conversation_id,
|
|
CopilotConversation.user_id == current_user.id,
|
|
)
|
|
)
|
|
conversation = result.scalar_one_or_none()
|
|
if not conversation:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Conversation not found")
|
|
|
|
return CopilotConversationResponse.model_validate(conversation)
|