feat: add next_steps column and update session schemas
- Add next_steps TEXT column to sessions table via migration 034 - Add include_outcome_notes, include_next_steps, max_step_index to SessionExport - Add next_steps to SessionUpdate, SessionResponse, SessionComplete Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -54,6 +54,116 @@ ADMIN_EMAIL = None
|
||||
ADMIN_PASSWORD = None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TREE STRUCTURE NORMALIZATION
|
||||
# =============================================================================
|
||||
|
||||
def normalize_node(node: dict[str, Any]) -> None:
|
||||
"""Recursively fix node fields to match the backend validation schema.
|
||||
|
||||
- Action nodes: copies 'description' to 'action' if 'action' is missing
|
||||
- Solution nodes: copies 'description' to 'solution' if 'solution' is missing
|
||||
- Decision nodes with only 1 child: duplicates the child with an 'Other' option
|
||||
"""
|
||||
node_type = node.get("type")
|
||||
|
||||
if node_type == "action":
|
||||
if "action" not in node and "description" in node:
|
||||
node["action"] = node["description"]
|
||||
elif node_type == "solution":
|
||||
if "solution" not in node and "description" in node:
|
||||
node["solution"] = node["description"]
|
||||
elif node_type == "decision":
|
||||
children = node.get("children", [])
|
||||
if len(children) == 1:
|
||||
# Add a generic second branch so validation passes
|
||||
fallback = {
|
||||
"id": children[0]["id"] + "_alt",
|
||||
"type": "solution",
|
||||
"title": "Escalate for Further Investigation",
|
||||
"solution": "The issue does not match the expected scenario. Escalate to a senior engineer or gather additional information before proceeding."
|
||||
}
|
||||
children.append(fallback)
|
||||
# Also add an option for the new branch if options exist
|
||||
options = node.get("options", [])
|
||||
if options and len(options) == 1:
|
||||
options.append({
|
||||
"id": options[0]["id"] + "_alt",
|
||||
"label": "None of the above / Not sure",
|
||||
"next_node_id": fallback["id"]
|
||||
})
|
||||
|
||||
# Recurse into children
|
||||
for child in node.get("children", []):
|
||||
normalize_node(child)
|
||||
|
||||
|
||||
def normalize_tree_structure(tree_data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Normalize an entire tree's structure before sending to the API."""
|
||||
if "tree_structure" in tree_data:
|
||||
normalize_node(tree_data["tree_structure"])
|
||||
return tree_data
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# GLOBAL CATEGORY MANAGEMENT
|
||||
# =============================================================================
|
||||
|
||||
def slugify(name: str) -> str:
|
||||
"""Convert a category name to a URL-safe slug."""
|
||||
import re
|
||||
slug = re.sub(r'[^a-zA-Z0-9 ]', '', name.lower())
|
||||
slug = re.sub(r' +', '-', slug.strip())
|
||||
return slug
|
||||
|
||||
|
||||
async def ensure_global_categories(
|
||||
client: httpx.AsyncClient, token: str, category_names: list[str]
|
||||
) -> dict[str, str]:
|
||||
"""Ensure global categories exist and return a name -> UUID mapping.
|
||||
|
||||
Creates any categories that don't already exist.
|
||||
Returns dict like {"Networking": "uuid-here", "Microsoft 365": "uuid-here"}
|
||||
"""
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
category_map: dict[str, str] = {}
|
||||
|
||||
# Fetch existing global categories
|
||||
resp = await client.get(f"{API_BASE_URL}/admin/categories/global", headers=headers)
|
||||
if resp.status_code == 200:
|
||||
for cat in resp.json():
|
||||
category_map[cat["name"]] = cat["id"]
|
||||
|
||||
# Create any missing categories
|
||||
for name in category_names:
|
||||
if name not in category_map:
|
||||
slug = slugify(name)
|
||||
create_resp = await client.post(
|
||||
f"{API_BASE_URL}/admin/categories/global",
|
||||
json={"name": name, "slug": slug, "description": f"Troubleshooting trees for {name}"},
|
||||
headers=headers
|
||||
)
|
||||
if create_resp.status_code == 201:
|
||||
cat_data = create_resp.json()
|
||||
category_map[name] = cat_data["id"]
|
||||
print(f" [NEW] Created global category: {name}")
|
||||
elif create_resp.status_code == 409:
|
||||
# Slug conflict — already exists, re-fetch
|
||||
resp2 = await client.get(f"{API_BASE_URL}/admin/categories/global", headers=headers)
|
||||
if resp2.status_code == 200:
|
||||
for cat in resp2.json():
|
||||
if cat["name"] == name:
|
||||
category_map[name] = cat["id"]
|
||||
break
|
||||
print(f" [OK] Category already exists: {name}")
|
||||
else:
|
||||
print(f" [WARN] Failed to create category '{name}': {create_resp.text}")
|
||||
else:
|
||||
print(f" [OK] Category exists: {name}")
|
||||
|
||||
return category_map
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# NETWORKING TREES
|
||||
# =============================================================================
|
||||
@@ -957,50 +1067,6 @@ def get_site_to_site_vpn_tree() -> dict[str, Any]:
|
||||
# SEEDING INFRASTRUCTURE
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def _fix_node_fields(node: dict[str, Any]) -> None:
|
||||
"""Recursively add required 'action'/'solution' fields from title/description.
|
||||
|
||||
The tree validator requires:
|
||||
- action nodes to have a non-empty 'action' field
|
||||
- solution nodes to have a non-empty 'solution' field
|
||||
- decision nodes with children to have at least 2 children
|
||||
|
||||
Seed data uses 'title' and 'description' but not the required fields.
|
||||
This patches them in-place before sending to the API.
|
||||
"""
|
||||
node_type = node.get("type")
|
||||
|
||||
if node_type == "action" and not node.get("action"):
|
||||
node["action"] = node.get("title") or node.get("description") or "Action"
|
||||
|
||||
elif node_type == "solution" and not node.get("solution"):
|
||||
node["solution"] = node.get("title") or node.get("description") or "Solution"
|
||||
|
||||
elif node_type == "decision":
|
||||
children = node.get("children", [])
|
||||
# If decision node has exactly 1 child, duplicate it with a fallback label
|
||||
if len(children) == 1:
|
||||
fallback = {
|
||||
"id": children[0]["id"] + "_fallback",
|
||||
"type": "solution",
|
||||
"title": "Escalate: No Other Options",
|
||||
"solution": "If the above path does not apply, escalate to senior support.",
|
||||
}
|
||||
children.append(fallback)
|
||||
# Add a matching option if options exist
|
||||
options = node.get("options", [])
|
||||
if options and len(options) < 2:
|
||||
options.append({
|
||||
"id": "fallback",
|
||||
"label": "None of the above / Escalate",
|
||||
"next_node_id": fallback["id"],
|
||||
})
|
||||
|
||||
for child in node.get("children", []):
|
||||
_fix_node_fields(child)
|
||||
|
||||
|
||||
async def get_admin_token(client: httpx.AsyncClient) -> str:
|
||||
"""Authenticate with admin credentials."""
|
||||
if not ADMIN_EMAIL or not ADMIN_PASSWORD:
|
||||
@@ -1017,16 +1083,19 @@ async def get_admin_token(client: httpx.AsyncClient) -> str:
|
||||
return login_response.json()["access_token"]
|
||||
|
||||
|
||||
async def create_tree(client: httpx.AsyncClient, token: str, tree_data: dict) -> dict | None:
|
||||
async def create_tree(client: httpx.AsyncClient, token: str, tree_data: dict, category_id: str | None = None) -> dict | None:
|
||||
"""Create a tree via the API. Returns None if tree already exists."""
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
tree_data["is_default"] = True
|
||||
tree_data["is_public"] = True
|
||||
|
||||
# Fix missing action/solution fields in tree structure nodes
|
||||
if "tree_structure" in tree_data:
|
||||
_fix_node_fields(tree_data["tree_structure"])
|
||||
# Normalize description -> action/solution fields
|
||||
normalize_tree_structure(tree_data)
|
||||
|
||||
# Set category_id if available (future-proof global categories)
|
||||
if category_id:
|
||||
tree_data["category_id"] = category_id
|
||||
|
||||
list_response = await client.get(f"{API_BASE_URL}/trees", headers=headers)
|
||||
if list_response.status_code == 200:
|
||||
@@ -1084,7 +1153,17 @@ async def seed_database():
|
||||
print(f" [ERROR] {e}")
|
||||
return False
|
||||
|
||||
print("\n[2/3] Preparing decision trees...")
|
||||
print("\n[2/5] Setting up global categories...")
|
||||
all_category_names = ["Networking", "Active Directory / Entra ID", "Microsoft 365"]
|
||||
try:
|
||||
category_map = await ensure_global_categories(client, token, all_category_names)
|
||||
print(f" {len(category_map)} categories ready")
|
||||
except Exception as e:
|
||||
print(f" [WARN] Category setup failed: {e}")
|
||||
print(f" Falling back to legacy text categories")
|
||||
category_map = {}
|
||||
|
||||
print("\n[3/5] Preparing decision trees...")
|
||||
trees_to_create = [
|
||||
("Networking", get_dns_resolution_tree()),
|
||||
("Networking", get_dhcp_issues_tree()),
|
||||
@@ -1111,7 +1190,7 @@ async def seed_database():
|
||||
|
||||
print(f" Found {len(trees_to_create)} trees to seed\n")
|
||||
|
||||
print("[3/3] Creating decision trees...")
|
||||
print("[4/5] Creating decision trees...")
|
||||
created_count = 0
|
||||
skipped_count = 0
|
||||
current_category = None
|
||||
@@ -1121,7 +1200,8 @@ async def seed_database():
|
||||
print(f"\n {category}:")
|
||||
current_category = category
|
||||
try:
|
||||
result = await create_tree(client, token, tree_data)
|
||||
cat_id = category_map.get(category) if category_map else None
|
||||
result = await create_tree(client, token, tree_data, category_id=cat_id)
|
||||
if result:
|
||||
created_count += 1
|
||||
else:
|
||||
@@ -1129,9 +1209,11 @@ async def seed_database():
|
||||
except Exception as e:
|
||||
print(f" [FAIL] '{tree_data['name']}': {e}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("\n[5/5] Summary")
|
||||
print("=" * 60)
|
||||
print(" SEEDING COMPLETE")
|
||||
print("=" * 60)
|
||||
print(f" Global categories: {len(category_map)}")
|
||||
print(f" Trees created: {created_count}")
|
||||
print(f" Trees skipped: {skipped_count}")
|
||||
print(f" Total: {created_count + skipped_count}")
|
||||
|
||||
Reference in New Issue
Block a user