* feat: add session sharing types, API client, and utilities Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add SessionTimeline and ActionMenu reusable components SessionTimeline extracts timeline/checklist rendering from SessionDetailPage into a reusable component for both authenticated and public session views. ActionMenu provides a dropdown action menu with keyboard/click-outside dismiss. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add ShareSessionModal and integrate into SessionDetailPage Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add Share Progress popover to TreeNavigationPage Replace the single "Copy for Ticket" button with a "Share Progress" popover that offers three actions: Copy Progress Summary (existing PSA export flow), Copy Share Link (auto-creates account-only share if needed), and Manage Share Links (opens ShareSessionModal). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add public SharedSessionPage with tree preview Add the public-facing shared session page at /share/:shareToken that renders shared sessions without authentication. Includes error handling for 401 (redirect to login), 403 (access denied), 404 (not found), and 410 (expired). The page features a minimal header, session metadata, SessionTimeline component, and a new SharedSessionTreePreview component that renders the decision tree structure with the path taken highlighted. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add My Shares management page with nav link Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: address code review issues in session sharing - Add useCallback for loadShares in ShareSessionModal (React hook deps) - Use TreeStructure type instead of Record<string, unknown> for type safety - Fix login redirect format to match LoginPage's expected state shape Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add focused tests for session sharing utilities and API Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: resolve tree_structure type compatibility for shared session views - Use TreeStructure & Record<string, unknown> intersection for JSONB flexibility - Add explicit cast in SharedSessionTreePreview for recursive node rendering Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: add session sharing learnings to CLAUDE.md Add gotchas #12 (TreeStructure vs Tree types) and #13 (login redirect state format), note about npm run build strictness, and public route pattern to Common Tasks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: procedural editor UX improvements Add URL intake field type, fix variable name editing collapsing fields (index-based keys/updates), auto-generate variable names by field type, add section header as first-class step type, and simplify step editor with "More Options" collapsible for advanced fields. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: allow section_header step type in validation, improve tag input - Add 'section_header' to VALID_STEP_TYPES in backend validation so procedural flows with section headers can be published - Replace procedural editor's inline tag input with TagInput component (supports autocomplete, Tab, comma, semicolon, and paste splitting) - Add semicolon delimiter support to TagInput component Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: add type-aware routing for procedural flows Centralizes tree navigation routing via getTreeNavigatePath helper. Fixes all pages to route procedural sessions to /flows/:id/navigate instead of /trees/:id/navigate. Adds safety redirect in troubleshooting navigator and resume support in procedural navigator. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: remove unused index prop from IntakeFieldEditor Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
248 lines
8.1 KiB
Python
248 lines
8.1 KiB
Python
from datetime import datetime
|
|
from typing import Optional, Any, Literal
|
|
from uuid import UUID
|
|
from pydantic import BaseModel, Field, field_validator, model_validator
|
|
import re
|
|
|
|
|
|
# --- Tree Type ---
|
|
|
|
TreeType = Literal['troubleshooting', 'procedural']
|
|
|
|
# --- Intake Form Schemas ---
|
|
|
|
FIELD_TYPES = Literal[
|
|
'text', 'textarea', 'number', 'ip_address', 'email', 'url',
|
|
'select', 'multi_select', 'checkbox', 'password'
|
|
]
|
|
|
|
VARIABLE_NAME_PATTERN = re.compile(r'^[a-z][a-z0-9_]*$')
|
|
|
|
|
|
class IntakeFieldValidation(BaseModel):
|
|
"""Validation config for an intake form field."""
|
|
min_length: Optional[int] = None
|
|
max_length: Optional[int] = None
|
|
pattern: Optional[str] = None
|
|
pattern_message: Optional[str] = None
|
|
format: Optional[Literal['ipv4', 'email']] = None
|
|
min_value: Optional[float] = None
|
|
max_value: Optional[float] = None
|
|
min_selections: Optional[int] = None
|
|
max_selections: Optional[int] = None
|
|
|
|
|
|
class IntakeFormField(BaseModel):
|
|
"""Schema for a single intake form field definition."""
|
|
variable_name: str = Field(..., min_length=1, max_length=50)
|
|
label: str = Field(..., min_length=1, max_length=100)
|
|
field_type: FIELD_TYPES
|
|
required: bool = True
|
|
options: Optional[list[str]] = None
|
|
placeholder: Optional[str] = Field(None, max_length=200)
|
|
help_text: Optional[str] = Field(None, max_length=500)
|
|
default_value: Optional[str] = None
|
|
group_name: Optional[str] = Field(None, max_length=100)
|
|
display_order: int = Field(..., ge=1)
|
|
validation: Optional[IntakeFieldValidation] = None
|
|
|
|
@field_validator('variable_name')
|
|
@classmethod
|
|
def validate_variable_name(cls, v: str) -> str:
|
|
if not VARIABLE_NAME_PATTERN.match(v):
|
|
raise ValueError(
|
|
'Variable name must start with a lowercase letter and contain only '
|
|
'lowercase letters, numbers, and underscores'
|
|
)
|
|
return v
|
|
|
|
@model_validator(mode='after')
|
|
def validate_options_for_select(self):
|
|
if self.field_type in ('select', 'multi_select'):
|
|
if not self.options or len(self.options) == 0:
|
|
raise ValueError(
|
|
f'{self.field_type} fields must have at least one option'
|
|
)
|
|
return self
|
|
|
|
|
|
class CategoryInfo(BaseModel):
|
|
"""Embedded category info for tree responses."""
|
|
id: UUID
|
|
name: str
|
|
slug: str
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class TreeBase(BaseModel):
|
|
name: str = Field(..., min_length=1, max_length=255)
|
|
description: Optional[str] = None
|
|
# Legacy category field - kept for backward compatibility
|
|
category: Optional[str] = Field(None, max_length=100)
|
|
|
|
|
|
class TreeCreate(TreeBase):
|
|
tree_type: TreeType = Field('troubleshooting', description="Tree type: troubleshooting or procedural")
|
|
tree_structure: dict[str, Any] = Field(..., description="The decision tree structure in JSON format")
|
|
intake_form: Optional[list[IntakeFormField]] = Field(None, description="Intake form field definitions (procedural flows only)")
|
|
is_public: bool = Field(False, description="Make tree visible to all users")
|
|
is_default: bool = Field(False, description="Mark as a default/system tree (admin only)")
|
|
status: Literal['draft', 'published'] = Field('published', description="Status: draft or published")
|
|
category_id: Optional[UUID] = Field(None, description="Category ID from tree_categories table")
|
|
tags: Optional[list[str]] = Field(None, max_length=10, description="List of tag names to assign")
|
|
|
|
@model_validator(mode='after')
|
|
def validate_intake_form_unique_variables(self):
|
|
if self.intake_form:
|
|
names = [f.variable_name for f in self.intake_form]
|
|
if len(names) != len(set(names)):
|
|
raise ValueError('Intake form variable names must be unique')
|
|
return self
|
|
|
|
|
|
class TreeUpdate(BaseModel):
|
|
name: Optional[str] = Field(None, min_length=1, max_length=255)
|
|
description: Optional[str] = None
|
|
category: Optional[str] = Field(None, max_length=100)
|
|
category_id: Optional[UUID] = None
|
|
tree_type: Optional[TreeType] = None
|
|
tree_structure: Optional[dict[str, Any]] = None
|
|
intake_form: Optional[list[IntakeFormField]] = None
|
|
is_public: Optional[bool] = None
|
|
is_active: Optional[bool] = None
|
|
status: Optional[Literal['draft', 'published']] = None
|
|
tags: Optional[list[str]] = Field(None, max_length=10, description="List of tag names to assign (replaces existing)")
|
|
|
|
@model_validator(mode='after')
|
|
def validate_intake_form_unique_variables(self):
|
|
if self.intake_form:
|
|
names = [f.variable_name for f in self.intake_form]
|
|
if len(names) != len(set(names)):
|
|
raise ValueError('Intake form variable names must be unique')
|
|
return self
|
|
|
|
|
|
class ForkCreate(BaseModel):
|
|
fork_reason: Optional[str] = Field(None, max_length=255, description="Brief reason for forking")
|
|
name: Optional[str] = Field(None, min_length=1, max_length=255, description="Name for the fork (defaults to 'Fork of {original name}')")
|
|
|
|
|
|
class ForkInfo(BaseModel):
|
|
"""Fork metadata included in tree responses."""
|
|
parent_tree_id: Optional[UUID] = None
|
|
root_tree_id: Optional[UUID] = None
|
|
fork_reason: Optional[str] = None
|
|
fork_depth: int = 0
|
|
parent_updated_at: Optional[datetime] = None
|
|
has_parent_updates: bool = False
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class TreeResponse(TreeBase):
|
|
id: UUID
|
|
tree_type: str = 'troubleshooting'
|
|
tree_structure: dict[str, Any]
|
|
intake_form: Optional[list[dict[str, Any]]] = None
|
|
author_id: Optional[UUID] = None
|
|
account_id: Optional[UUID] = None
|
|
category_id: Optional[UUID] = None
|
|
category_info: Optional[CategoryInfo] = None
|
|
tags: list[str] = [] # List of tag names
|
|
fork_info: Optional[ForkInfo] = None
|
|
is_active: bool
|
|
is_public: bool
|
|
is_default: bool
|
|
status: str # draft or published
|
|
version: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
usage_count: int
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class TreeListResponse(BaseModel):
|
|
id: UUID
|
|
name: str
|
|
description: Optional[str] = None
|
|
tree_type: str = 'troubleshooting'
|
|
category: Optional[str] = None
|
|
category_id: Optional[UUID] = None
|
|
category_info: Optional[CategoryInfo] = None
|
|
tags: list[str] = [] # List of tag names
|
|
author_id: Optional[UUID] = None
|
|
account_id: Optional[UUID] = None
|
|
is_active: bool
|
|
is_public: bool
|
|
is_default: bool
|
|
status: str # draft or published
|
|
version: int
|
|
usage_count: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
# --- Tree Sharing Schemas ---
|
|
|
|
class TreeShareCreate(BaseModel):
|
|
"""Request to create a share token for a tree."""
|
|
allow_forking: bool = Field(True, description="Whether recipients can fork this tree")
|
|
expires_at: Optional[datetime] = Field(None, description="Optional expiration time for the share")
|
|
|
|
|
|
class TreeShareResponse(BaseModel):
|
|
"""Response containing share token and URL."""
|
|
id: UUID
|
|
tree_id: UUID
|
|
share_token: str
|
|
share_url: str
|
|
allow_forking: bool
|
|
created_by: UUID
|
|
created_at: datetime
|
|
expires_at: Optional[datetime] = None
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class TreeVisibilityUpdate(BaseModel):
|
|
"""Request to update tree visibility."""
|
|
visibility: Literal['private', 'team', 'link', 'public'] = Field(..., description="Visibility level")
|
|
|
|
|
|
class SharedTreeResponse(TreeBase):
|
|
"""Public response for shared trees (minimal info)."""
|
|
id: UUID
|
|
tree_structure: dict[str, Any]
|
|
category: Optional[str] = None
|
|
tags: list[str] = []
|
|
version: int
|
|
allow_forking: bool # From share token
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
# --- Tree Validation Schemas ---
|
|
|
|
class ValidationError(BaseModel):
|
|
"""Individual validation error."""
|
|
field: str
|
|
message: str
|
|
|
|
|
|
class TreeValidationResponse(BaseModel):
|
|
"""Response for tree validation endpoint."""
|
|
can_publish: bool
|
|
errors: list[ValidationError] = []
|