* feat: AI-assisted flow builder with 4-stage wizard Implements the complete AI flow builder feature using a guided 4-stage wizard (Foundation → Scaffold → Branch Detail → Review & Assemble). AI assists at bounded points using Claude Haiku for cost-efficient structured JSON generation (~$0.01-0.03/flow). Backend: new models (ai_conversations, ai_usage), Alembic migration, quota enforcement with billing anchor, Anthropic API integration with prompt caching, tree validation, conversation CRUD with 24h TTL, APScheduler cleanup job, 5 API endpoints, Pydantic schemas. Frontend: TypeScript types, API client, Zustand store for wizard state, 7 components (modal, step indicator, foundation form, branch selector, branch detail view, tree preview, quota display), MyTreesPage integration with "Build with AI" button (hidden when AI not configured). Tests: 14 validator unit tests + 11 endpoint integration tests with mocked Anthropic (zero real API spend). All 25 tests passing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: dashboard design doc and implementation plan Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 1 — pinnedFlowsStore, pagination hook, cached quota hook, sidebar refactor - Add pin() to pinnedFlowsApi - Create pinnedFlowsStore (Zustand) — single source of truth for pin state - Add dashboardMyFlowsView preference to userPreferencesStore - Create usePaginationParams hook (URL-synced) - Create useCachedQuota hook (5-min TTL) - Sidebar uses pinnedFlowsStore instead of local state Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 2 — pin/favorite buttons on all library view components - TreeGridView: star in top-right corner of cards - TreeListView: star at end of each row - TreeTableView: dedicated leftmost Favorite column - All with proper a11y (aria-label), event isolation, loading states Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 3 — Library page create dropdown + AI Builder + pin wiring - Replace single Create link with dropdown menu (3 flow types + AI Builder) - Wire pinnedFlowsStore to all view components - AI Builder modal integration via useCachedQuota hook Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 4 — Dashboard refactor with Favorites grid + paginated My Flows - Favorites section: compact grid from pinnedFlowsStore, max 2 rows, expandable - My Flows: author_id filter, URL-synced pagination (10/25/50/All) - View toggle (grid/list/table) with independent preference - Skeleton loaders, empty states with CTAs - Create dropdown with AI Builder option - 500-item ceiling for "Show All" mode Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 5 — Sidebar pinned section dual collapse + show more/less - Header collapse hides entire section, resets to 5 items on re-expand - List truncation: show first 5, "Show more (N)" expands to all - Clicking a flow auto-collapses back to 5 - Smooth max-height CSS transition (250ms ease-out) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: stabilize usePaginationParams to prevent infinite re-render loop allowedPageSizes array was recreated every render as a useMemo dep, causing infinite updates. Use useRef to stabilize the reference. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: remove Set-based Zustand selectors causing infinite re-render loop Zustand selectors returning new Set() on every call fail Object.is equality check, triggering continuous re-renders. Replaced with useMemo-derived Sets in consuming components. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: pin route ordering and star icon overlap in grid view Move GET /pinned and PATCH /pinned/reorder before GET /{tree_id} to prevent FastAPI from matching "pinned" as a UUID path parameter (422). Relocate star button from absolute positioning into the header row to avoid overlapping privacy icons and category badges. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: code review fixes — date calc, input validation, rate limits, shared components - Fix monthly_reset_at crash when billing anchor day exceeds next month's length - Add environment_tags sanitization (max 20 tags, 100 chars each) to prevent prompt injection - Add @limiter.limit("10/minute") rate limiting to all AI endpoints - Use getTreeNavigatePath() routing helper instead of hardcoded paths - Extract shared CreateFlowDropdown component from QuickStartPage and TreeLibraryPage - Clear useCachedQuota on logout to prevent stale data across user sessions - Add useRef guard to scaffold useEffect to prevent potential double-fire - Use node.id as React key instead of array index in BranchDetailView - Remove redundant dead logic in ai_tree_validator Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: correct Anthropic model ID to full dated version claude-haiku-4-5 is not a valid model alias — Anthropic requires the full dated model ID claude-haiku-4-5-20251001. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: strip markdown code fences from AI JSON responses Haiku sometimes wraps its JSON in ```json ... ``` despite the prompt instructing otherwise. Strip fences before parsing to avoid JSONDecodeError at char 0. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: increase branch_detail max_tokens to 8192 and add response logging Truncated output at 4096 tokens produces invalid JSON mid-generation. Also logs stop_reason and output_tokens per attempt to diagnose failures. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: pass explicit status='draft' when creating AI-generated flow Tree model defaults to 'published' in the DB schema, but passing status=None from the constructor overrides that default, causing a nullable=False violation and a 500 on save. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: auto-advance branch detail and pin navigation bar - Auto-advance to next undetailed branch after generation completes, using a useEffect that watches the count of detailed branches - Cap tree preview at max-h-48 with internal scroll so the nav bar is never pushed off screen - Make nav bar sticky bottom-0 with bg-card so it stays visible regardless of content height Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: increase branch retries to 3 and relax cross-reference validation on final attempt next_node_id mismatches are a common model hallucination that the retry prompt doesn't reliably fix. On the final (3rd) attempt, accept the branch with strict=False so only truly fatal errors (missing fields, dead ends, bad JSON) cause a hard failure. Cross-reference issues are minor and fixable in the tree editor. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: strengthen prompt to prevent next_node_id mismatches, keep strict validation Rather than lowering the validation bar, improve the system prompt: - Rule 6 now explicitly states next_node_id must match a direct child's id - Added rule 10: build tree bottom-up to avoid forward-reference errors - Corrective prompt now calls out the ID mismatch constraint specifically Reverts the strict=False fallback — flows must be correct before saving. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: persist branch viewing index in store to survive phase remounts Local useState resets to 0 every time phase transitions from 'generating' back to 'detailing', causing the view to snap back to branch 1. Move viewingIndex to store's currentBranchIndex (already existed) and advance it in generateBranchDetail after success. Component reads from store so remounts no longer lose position. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: correct publish validation to check title instead of action/solution fields The publish validator was checking for an 'action' field on action nodes and a 'solution' field on solution nodes, but the actual node schema (confirmed from seed data and frontend types) uses 'title'/'description'. This caused all AI-generated trees to fail publish validation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: correct action node schema and improve AI flow quality - Fix action nodes to use next_node_id (not children) for continuation, matching how TreeNavigationPage.tsx navigates action nodes - Validator now requires next_node_id on all action nodes and flags missing ones as broken dead ends - Update _check_branch_termination: action nodes are not dead ends since they continue via next_node_id (validated separately) - Improve scaffold prompt: branch names must describe observable symptoms users can self-identify, not internal category names - Update branch_detail prompt with clearer action node schema, corrected few-shot example showing proper next_node_id on action nodes - Improve assemble_tree root question to be more user-facing Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * docs: add AI flow builder gotchas to CLAUDE.md (#23-25) - Action nodes use next_node_id (not children) for navigation - Anthropic model IDs require full dated version string - Claude API may wrap JSON in markdown fences Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: resolve CI lint errors and httpx dependency conflict - Fix httpx version conflict: requirements-dev.txt now uses >=0.27.0 to match requirements.txt - Extract CSAT helper functions to csatUtils.ts to fix react-refresh/only-export-components - Remove default export from admin/EmptyState.tsx shim (same rule) - Fix empty catch block in Modal.tsx (no-empty) - Add eslint-disable comments for intentional setState-in-effect patterns in FlowAnalyticsPanel, QuickLaunch, NodeEditorPanel, useCachedQuota, MyAnalyticsPage, TeamAnalyticsPage - Add eslint-disable comments for intentional _children destructure in NodeEditorPanel - Fix _parentId unused var in useTreeLayout.ts - Rewrite usePaginationParams.ts to avoid reading refs during render Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: update tests to match action node schema (next_node_id, not children) - Update _make_valid_tree() in test_ai_tree_validator to use next_node_id on action nodes (solution is a sibling, not a child) - Fix test_dead_end_action_node → test_dead_end_decision_node (action nodes don't have child-based dead ends; dead ends are decision nodes with no children) - Add test_action_missing_next_node_id for the new validation rule - Update BRANCH_DETAIL_JSON in test_ai_endpoints to use next_node_id pattern - Update test_draft_trees.py to use "title" field for action/solution nodes (tree_validation.py was updated this branch to require "title" not "action"/"solution") Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: update remaining tests and session_to_tree for title field rename - test_tree_validation.py: replace "action"/"solution" content fields with "title" - test_procedural_flows.py: update solution node fixtures to use "title" - test_save_session_as_tree.py: update fixtures and assertions for "title" field - session_to_tree.py: generate "title" instead of "action"/"solution" on converted nodes; fall back to legacy field names when reading from old tree snapshots for compatibility Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
290 lines
10 KiB
Python
290 lines
10 KiB
Python
"""Tree validation helper module for draft/published workflow."""
|
|
from typing import Any
|
|
|
|
PROCEDURAL_TREE_TYPES = {"procedural", "maintenance"}
|
|
|
|
|
|
class TreeValidationError(Exception):
|
|
"""Custom exception for tree validation errors."""
|
|
def __init__(self, field: str, message: str):
|
|
self.field = field
|
|
self.message = message
|
|
super().__init__(f"{field}: {message}")
|
|
|
|
|
|
# --- Troubleshooting Tree Validation ---
|
|
|
|
def validate_tree_structure(tree_structure: dict[str, Any]) -> tuple[bool, list[dict[str, str]]]:
|
|
"""Validate troubleshooting tree structure for publishing.
|
|
|
|
A valid tree for publishing must have:
|
|
- A root node with id, type, and appropriate content fields
|
|
- All decision nodes must have a question field
|
|
- All decision nodes with children must have at least 2 children
|
|
- All action nodes must have an action field
|
|
- All solution nodes must have a solution field
|
|
- No orphaned nodes (all nodes reachable from root)
|
|
|
|
Args:
|
|
tree_structure: The tree structure dict to validate
|
|
|
|
Returns:
|
|
Tuple of (is_valid, list of errors)
|
|
Each error is a dict with 'field' and 'message' keys
|
|
"""
|
|
errors = []
|
|
|
|
# Check root node exists
|
|
if not tree_structure:
|
|
errors.append({"field": "tree_structure", "message": "Tree structure cannot be empty"})
|
|
return False, errors
|
|
|
|
if "id" not in tree_structure:
|
|
errors.append({"field": "tree_structure.id", "message": "Root node must have an id"})
|
|
|
|
if "type" not in tree_structure:
|
|
errors.append({"field": "tree_structure.type", "message": "Root node must have a type"})
|
|
return False, errors
|
|
|
|
# Validate root node based on type
|
|
_validate_node(tree_structure, "root", errors)
|
|
|
|
# Validate all child nodes recursively
|
|
if "children" in tree_structure:
|
|
_validate_children(tree_structure["children"], "root.children", errors)
|
|
|
|
# Block publish if any answer placeholder nodes remain
|
|
if _has_answer_nodes(tree_structure):
|
|
errors.append({
|
|
"field": "tree_structure",
|
|
"message": "Answer placeholders must be resolved to a node type before publishing."
|
|
})
|
|
|
|
return len(errors) == 0, errors
|
|
|
|
|
|
def _validate_node(node: dict[str, Any], path: str, errors: list[dict[str, str]]) -> None:
|
|
"""Validate a single node in the tree structure."""
|
|
node_type = node.get("type")
|
|
|
|
if node_type == "decision":
|
|
if "question" not in node or not node["question"]:
|
|
errors.append({
|
|
"field": f"{path}.question",
|
|
"message": "Decision nodes must have a non-empty question"
|
|
})
|
|
|
|
# If node has children, must have at least 2 (for decision branches)
|
|
children = node.get("children", [])
|
|
if children and len(children) < 2:
|
|
errors.append({
|
|
"field": f"{path}.children",
|
|
"message": "Decision nodes with children must have at least 2 branches"
|
|
})
|
|
|
|
elif node_type == "action":
|
|
if "title" not in node or not node["title"]:
|
|
errors.append({
|
|
"field": f"{path}.title",
|
|
"message": "Action nodes must have a non-empty title"
|
|
})
|
|
|
|
elif node_type == "solution":
|
|
if "title" not in node or not node["title"]:
|
|
errors.append({
|
|
"field": f"{path}.title",
|
|
"message": "Solution nodes must have a non-empty title"
|
|
})
|
|
|
|
elif node_type == "answer":
|
|
# Answer nodes are draft-only placeholders — no structural validation needed
|
|
pass
|
|
|
|
else:
|
|
errors.append({
|
|
"field": f"{path}.type",
|
|
"message": f"Unknown node type: {node_type}"
|
|
})
|
|
|
|
|
|
def _validate_children(children: list[dict[str, Any]], path: str, errors: list[dict[str, str]]) -> None:
|
|
"""Recursively validate child nodes."""
|
|
for i, child in enumerate(children):
|
|
child_path = f"{path}[{i}]"
|
|
|
|
if "id" not in child:
|
|
errors.append({"field": f"{child_path}.id", "message": "Child node must have an id"})
|
|
|
|
if "type" not in child:
|
|
errors.append({"field": f"{child_path}.type", "message": "Child node must have a type"})
|
|
continue
|
|
|
|
_validate_node(child, child_path, errors)
|
|
|
|
# Recursively validate grandchildren
|
|
if "children" in child:
|
|
_validate_children(child["children"], f"{child_path}.children", errors)
|
|
|
|
|
|
def _has_answer_nodes(node: dict[str, Any]) -> bool:
|
|
"""Recursively check if any node in the tree has type 'answer'."""
|
|
if node.get("type") == "answer":
|
|
return True
|
|
for child in node.get("children", []):
|
|
if _has_answer_nodes(child):
|
|
return True
|
|
return False
|
|
|
|
|
|
# --- Procedural Tree Validation ---
|
|
|
|
VALID_STEP_TYPES = {"procedure_step", "procedure_end", "section_header"}
|
|
VALID_CONTENT_TYPES = {"action", "informational", "verification", "warning"}
|
|
|
|
|
|
def validate_procedural_structure(tree_structure: dict[str, Any]) -> tuple[bool, list[dict[str, str]]]:
|
|
"""Validate procedural tree structure for publishing.
|
|
|
|
Procedural trees store steps as a flat ordered array in tree_structure["steps"].
|
|
|
|
Rules:
|
|
- Must have a non-empty "steps" array
|
|
- Each step must have: id, type, title
|
|
- Only procedure_step and procedure_end types allowed
|
|
- Must have exactly one procedure_end (as the last step)
|
|
- All other steps must be procedure_step
|
|
- No duplicate step IDs
|
|
- Steps with content_type must use valid values
|
|
|
|
Args:
|
|
tree_structure: Dict with a "steps" key containing the ordered step array
|
|
|
|
Returns:
|
|
Tuple of (is_valid, list of errors)
|
|
"""
|
|
errors = []
|
|
|
|
if not tree_structure:
|
|
errors.append({"field": "tree_structure", "message": "Tree structure cannot be empty"})
|
|
return False, errors
|
|
|
|
steps = tree_structure.get("steps")
|
|
if not steps or not isinstance(steps, list):
|
|
errors.append({"field": "tree_structure.steps", "message": "Procedural tree must have a non-empty steps array"})
|
|
return False, errors
|
|
|
|
# Track IDs for uniqueness
|
|
seen_ids: set[str] = set()
|
|
end_count = 0
|
|
|
|
for i, step in enumerate(steps):
|
|
path = f"steps[{i}]"
|
|
|
|
# Required fields
|
|
step_id = step.get("id")
|
|
if not step_id:
|
|
errors.append({"field": f"{path}.id", "message": "Step must have an id"})
|
|
elif step_id in seen_ids:
|
|
errors.append({"field": f"{path}.id", "message": f"Duplicate step id: {step_id}"})
|
|
else:
|
|
seen_ids.add(step_id)
|
|
|
|
step_type = step.get("type")
|
|
if not step_type:
|
|
errors.append({"field": f"{path}.type", "message": "Step must have a type"})
|
|
elif step_type not in VALID_STEP_TYPES:
|
|
errors.append({"field": f"{path}.type", "message": f"Invalid step type: {step_type}. Must be one of: {', '.join(VALID_STEP_TYPES)}"})
|
|
elif step_type == "procedure_end":
|
|
end_count += 1
|
|
# procedure_end must be last step
|
|
if i != len(steps) - 1:
|
|
errors.append({"field": f"{path}.type", "message": "procedure_end must be the last step"})
|
|
|
|
if not step.get("title"):
|
|
errors.append({"field": f"{path}.title", "message": "Step must have a non-empty title"})
|
|
|
|
# Validate content_type if present
|
|
content_type = step.get("content_type")
|
|
if content_type and content_type not in VALID_CONTENT_TYPES:
|
|
errors.append({"field": f"{path}.content_type", "message": f"Invalid content_type: {content_type}. Must be one of: {', '.join(VALID_CONTENT_TYPES)}"})
|
|
|
|
# Must have exactly one end step
|
|
if end_count == 0:
|
|
errors.append({"field": "tree_structure.steps", "message": "Procedural tree must have a procedure_end step as the last step"})
|
|
elif end_count > 1:
|
|
errors.append({"field": "tree_structure.steps", "message": "Procedural tree must have exactly one procedure_end step"})
|
|
|
|
return len(errors) == 0, errors
|
|
|
|
|
|
# --- Dispatch ---
|
|
|
|
def can_publish_tree(
|
|
tree_structure: dict[str, Any],
|
|
name: str,
|
|
description: str | None = None,
|
|
tree_type: str = "troubleshooting",
|
|
intake_form: list[dict[str, Any]] | None = None,
|
|
) -> tuple[bool, list[dict[str, str]]]:
|
|
"""Check if a tree can be published.
|
|
|
|
Dispatches to the appropriate validator based on tree_type.
|
|
|
|
Args:
|
|
tree_structure: The tree structure to validate
|
|
name: The tree name
|
|
description: Optional tree description
|
|
tree_type: 'troubleshooting' or 'procedural'
|
|
intake_form: Optional intake form fields (procedural only)
|
|
|
|
Returns:
|
|
Tuple of (can_publish, list of errors)
|
|
"""
|
|
errors = []
|
|
|
|
# Validate name
|
|
if not name or not name.strip():
|
|
errors.append({"field": "name", "message": "Tree must have a name to be published"})
|
|
|
|
# Validate structure based on tree type
|
|
if tree_type in PROCEDURAL_TREE_TYPES:
|
|
structure_valid, structure_errors = validate_procedural_structure(tree_structure)
|
|
else:
|
|
structure_valid, structure_errors = validate_tree_structure(tree_structure)
|
|
errors.extend(structure_errors)
|
|
|
|
# Validate intake form if present (procedural only)
|
|
if intake_form and tree_type in PROCEDURAL_TREE_TYPES:
|
|
form_valid, form_errors = _validate_intake_form(intake_form)
|
|
errors.extend(form_errors)
|
|
|
|
return len(errors) == 0, errors
|
|
|
|
|
|
def _validate_intake_form(intake_form: list[dict[str, Any]]) -> tuple[bool, list[dict[str, str]]]:
|
|
"""Validate intake form field definitions."""
|
|
errors = []
|
|
variable_names: set[str] = set()
|
|
|
|
for i, field in enumerate(intake_form):
|
|
path = f"intake_form[{i}]"
|
|
|
|
var_name = field.get("variable_name")
|
|
if not var_name:
|
|
errors.append({"field": f"{path}.variable_name", "message": "Field must have a variable_name"})
|
|
elif var_name in variable_names:
|
|
errors.append({"field": f"{path}.variable_name", "message": f"Duplicate variable_name: {var_name}"})
|
|
else:
|
|
variable_names.add(var_name)
|
|
|
|
if not field.get("label"):
|
|
errors.append({"field": f"{path}.label", "message": "Field must have a label"})
|
|
|
|
field_type = field.get("field_type")
|
|
if field_type in ("select", "multi_select"):
|
|
options = field.get("options")
|
|
if not options or len(options) == 0:
|
|
errors.append({"field": f"{path}.options", "message": f"{field_type} fields must have at least one option"})
|
|
|
|
return len(errors) == 0, errors
|