feat: flexible intake — deferred variables + prepared sessions
Remove blocking intake form modal. Variables are now filled inline during
flow execution or pre-filled via prepared sessions. Adds PATCH /sessions/{id}/variables
endpoint, POST /sessions/prepare for session pre-staging, inline variable prompts
in StepDetail, editable Session Variables panel, and "Prepared for You" dashboard section.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,8 @@ from app.schemas.session import (
|
||||
SaveAsTreeRequest,
|
||||
SaveAsTreeResponse,
|
||||
SessionComplete,
|
||||
SessionVariablesUpdate,
|
||||
PrepareSessionRequest,
|
||||
)
|
||||
from app.api.deps import get_current_active_user
|
||||
from app.core.permissions import can_access_tree
|
||||
@@ -39,6 +41,8 @@ async def list_sessions(
|
||||
tree_name: Optional[str] = Query(None, description="Filter by tree name from snapshot"),
|
||||
tree_id: Optional[UUID] = Query(None, description="Filter by tree ID"),
|
||||
batch_id: Optional[UUID] = Query(None, description="Filter by batch ID (maintenance batch runs)"),
|
||||
assigned_to_id: Optional[UUID] = Query(None, description="Filter by assigned user ID (prepared sessions)"),
|
||||
status: Optional[str] = Query(None, description="Filter by status: prepared, active, completed"),
|
||||
started_after: Optional[datetime] = Query(None, description="Filter sessions started after this datetime"),
|
||||
started_before: Optional[datetime] = Query(None, description="Filter sessions started before this datetime"),
|
||||
completed_after: Optional[datetime] = Query(None, description="Filter sessions completed after this datetime"),
|
||||
@@ -49,7 +53,11 @@ async def list_sessions(
|
||||
limit: int = Query(50, ge=1, le=100)
|
||||
):
|
||||
"""List user's troubleshooting sessions with comprehensive filtering."""
|
||||
query = select(Session).where(Session.user_id == current_user.id)
|
||||
from sqlalchemy import or_
|
||||
# Show sessions owned by user OR assigned to user (prepared sessions)
|
||||
query = select(Session).where(
|
||||
or_(Session.user_id == current_user.id, Session.assigned_to_id == current_user.id)
|
||||
)
|
||||
|
||||
# Completion status filter
|
||||
if completed is not None:
|
||||
@@ -78,6 +86,18 @@ async def list_sessions(
|
||||
if batch_id:
|
||||
query = query.where(Session.batch_id == batch_id)
|
||||
|
||||
# Assigned user filter (for prepared sessions)
|
||||
if assigned_to_id:
|
||||
query = query.where(Session.assigned_to_id == assigned_to_id)
|
||||
|
||||
# Status filter: prepared (started_at IS NULL), active, completed
|
||||
if status == "prepared":
|
||||
query = query.where(Session.started_at.is_(None))
|
||||
elif status == "active":
|
||||
query = query.where(Session.started_at.isnot(None), Session.completed_at.is_(None))
|
||||
elif status == "completed":
|
||||
query = query.where(Session.completed_at.isnot(None))
|
||||
|
||||
# Date range filters
|
||||
if started_after:
|
||||
query = query.where(Session.started_at >= started_after)
|
||||
@@ -93,7 +113,7 @@ async def list_sessions(
|
||||
if page is not None:
|
||||
effective_skip = (page - 1) * effective_limit
|
||||
|
||||
query = query.order_by(Session.started_at.desc())
|
||||
query = query.order_by(Session.started_at.desc().nullslast())
|
||||
query = query.offset(effective_skip).limit(effective_limit)
|
||||
|
||||
result = await db.execute(query)
|
||||
@@ -117,7 +137,7 @@ async def get_session(
|
||||
detail="Session not found"
|
||||
)
|
||||
|
||||
if session.user_id != current_user.id:
|
||||
if session.user_id != current_user.id and session.assigned_to_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this session"
|
||||
@@ -155,18 +175,9 @@ async def start_session(
|
||||
detail="You don't have access to this tree"
|
||||
)
|
||||
|
||||
# For procedural trees with intake forms, validate required fields
|
||||
# Deferred variables: sessions can start with empty/partial variables
|
||||
# Variables are filled inline during execution via PATCH /sessions/{id}/variables
|
||||
session_variables = session_data.session_variables or {}
|
||||
if tree.tree_type == 'procedural' and tree.intake_form:
|
||||
missing_fields = []
|
||||
for field in tree.intake_form:
|
||||
if field.get("required") and not session_variables.get(field["variable_name"]):
|
||||
missing_fields.append(field["label"])
|
||||
if missing_fields:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail=f"Missing required intake form fields: {', '.join(missing_fields)}"
|
||||
)
|
||||
|
||||
# Create session with tree snapshot (includes tree metadata for filtering/export)
|
||||
tree_snapshot = {
|
||||
@@ -217,7 +228,7 @@ async def update_session(
|
||||
detail="Session not found"
|
||||
)
|
||||
|
||||
if session.user_id != current_user.id:
|
||||
if session.user_id != current_user.id and session.assigned_to_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this session"
|
||||
@@ -229,6 +240,13 @@ async def update_session(
|
||||
detail="Cannot update a completed session"
|
||||
)
|
||||
|
||||
# Start a prepared session on first update (set started_at)
|
||||
if session.started_at is None:
|
||||
session.started_at = datetime.now(timezone.utc)
|
||||
# Transfer ownership to the executing user if they're the assignee
|
||||
if session.assigned_to_id == current_user.id and session.user_id != current_user.id:
|
||||
session.user_id = current_user.id
|
||||
|
||||
# Use mode='json' to ensure datetime fields are serialized as ISO strings for JSONB storage
|
||||
update_data = session_data.model_dump(exclude_unset=True, mode='json')
|
||||
|
||||
@@ -257,7 +275,7 @@ async def complete_session(
|
||||
detail="Session not found"
|
||||
)
|
||||
|
||||
if session.user_id != current_user.id:
|
||||
if session.user_id != current_user.id and session.assigned_to_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this session"
|
||||
@@ -295,7 +313,7 @@ async def update_scratchpad(
|
||||
detail="Session not found"
|
||||
)
|
||||
|
||||
if session.user_id != current_user.id:
|
||||
if session.user_id != current_user.id and session.assigned_to_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this session"
|
||||
@@ -307,6 +325,45 @@ async def update_scratchpad(
|
||||
return session
|
||||
|
||||
|
||||
@router.patch("/{session_id}/variables", response_model=SessionResponse)
|
||||
async def update_session_variables(
|
||||
session_id: UUID,
|
||||
data: SessionVariablesUpdate,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)]
|
||||
):
|
||||
"""Update session variables (partial dict merge). Used for deferred variable input."""
|
||||
result = await db.execute(select(Session).where(Session.id == session_id))
|
||||
session = result.scalar_one_or_none()
|
||||
|
||||
if not session:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Session not found"
|
||||
)
|
||||
|
||||
if session.user_id != current_user.id and session.assigned_to_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this session"
|
||||
)
|
||||
|
||||
if session.completed_at:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot update variables on a completed session"
|
||||
)
|
||||
|
||||
# Merge new variables into existing ones
|
||||
existing = session.session_variables or {}
|
||||
merged = {**existing, **data.variables}
|
||||
session.session_variables = merged
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(session)
|
||||
return session
|
||||
|
||||
|
||||
@router.post("/{session_id}/export")
|
||||
async def export_session(
|
||||
session_id: UUID,
|
||||
@@ -324,7 +381,7 @@ async def export_session(
|
||||
detail="Session not found"
|
||||
)
|
||||
|
||||
if session.user_id != current_user.id:
|
||||
if session.user_id != current_user.id and session.assigned_to_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this session"
|
||||
@@ -508,6 +565,89 @@ async def save_session_as_tree(
|
||||
)
|
||||
|
||||
|
||||
# ── Prepare Session (Flexible Intake) ─────────────────────────────────────
|
||||
|
||||
|
||||
@router.post("/prepare", response_model=SessionResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def prepare_session(
|
||||
data: PrepareSessionRequest,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
current_user: Annotated[User, Depends(get_current_active_user)]
|
||||
):
|
||||
"""Create a prepared session with pre-filled variables and optional assignee.
|
||||
|
||||
Prepared sessions have started_at = NULL. They appear in the assigned
|
||||
engineer's queue and can be started later with variables already filled.
|
||||
"""
|
||||
# Get the tree
|
||||
result = await db.execute(select(Tree).where(Tree.id == data.tree_id))
|
||||
tree = result.scalar_one_or_none()
|
||||
|
||||
if not tree:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Tree not found"
|
||||
)
|
||||
|
||||
if not tree.is_active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot prepare session with inactive tree"
|
||||
)
|
||||
|
||||
if not can_access_tree(current_user, tree):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have access to this tree"
|
||||
)
|
||||
|
||||
# Validate assignee exists and is on the same team (if specified)
|
||||
if data.assigned_to_id:
|
||||
assignee_result = await db.execute(select(User).where(User.id == data.assigned_to_id))
|
||||
assignee = assignee_result.scalar_one_or_none()
|
||||
if not assignee:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Assigned user not found"
|
||||
)
|
||||
if current_user.team_id and assignee.team_id != current_user.team_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Can only assign to users on your team"
|
||||
)
|
||||
|
||||
# Create tree snapshot
|
||||
tree_snapshot = {
|
||||
**tree.tree_structure,
|
||||
"name": tree.name,
|
||||
"description": tree.description,
|
||||
"category": tree.category,
|
||||
"version": tree.version,
|
||||
"tree_type": tree.tree_type,
|
||||
}
|
||||
|
||||
session_variables = data.session_variables or {}
|
||||
|
||||
new_session = Session(
|
||||
tree_id=tree.id,
|
||||
user_id=data.assigned_to_id or current_user.id,
|
||||
tree_snapshot=tree_snapshot,
|
||||
path_taken=[],
|
||||
decisions=[],
|
||||
session_variables=session_variables,
|
||||
ticket_number=data.ticket_number,
|
||||
client_name=data.client_name,
|
||||
started_at=None, # NULL = prepared state
|
||||
prepared_by_id=current_user.id,
|
||||
assigned_to_id=data.assigned_to_id,
|
||||
)
|
||||
|
||||
db.add(new_session)
|
||||
await db.commit()
|
||||
await db.refresh(new_session)
|
||||
return new_session
|
||||
|
||||
|
||||
# ── Batch Launch (Maintenance Flows) ──────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user