fix: KB Accelerator tree builder, approve all, canvas delete button
- Fix _build_troubleshooting_tree() to handle deep nesting, warning nodes, and DAG deduplication (placed set prevents duplicate IDs) - Fix step_sync VARCHAR(255) overflow on publish by truncating title - Add "Approve All" button to KB review screen - Add delete button (hover-reveal) to flow canvas nodes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -608,53 +608,81 @@ async def delete_import(
|
||||
|
||||
|
||||
def _build_troubleshooting_tree(nodes: list[KBImportNode]) -> dict:
|
||||
"""Build a troubleshooting tree_structure from import nodes."""
|
||||
"""Build a troubleshooting tree_structure from import nodes.
|
||||
|
||||
The tree editor expects a deeply nested structure where each decision
|
||||
node's `children` array contains all reachable descendant nodes.
|
||||
Action/solution nodes use `title`/`description` (not `question`).
|
||||
|
||||
The AI generates a DAG (shared nodes reachable from multiple paths),
|
||||
but the tree editor requires unique IDs — each node can only appear
|
||||
once. We embed each node the first time it's encountered; subsequent
|
||||
references just use next_node_id / options[].next_node_id to point
|
||||
back to the already-embedded node.
|
||||
"""
|
||||
if not nodes:
|
||||
return {"id": "root", "type": "decision", "question": "Empty", "children": []}
|
||||
|
||||
# Map original IDs to proper tree node structure
|
||||
# Map original IDs to import nodes
|
||||
original_id_map: dict[str, KBImportNode] = {}
|
||||
for node in nodes:
|
||||
orig_id = node.content.get("original_id", str(node.id))
|
||||
original_id_map[orig_id] = node
|
||||
|
||||
def _build_node(import_node: KBImportNode) -> dict:
|
||||
# Track which nodes have been placed in the tree to avoid duplicates
|
||||
placed: set[str] = set()
|
||||
|
||||
def _build_node(import_node: KBImportNode) -> dict | None:
|
||||
content = import_node.content
|
||||
node_type = import_node.node_type
|
||||
node_id = content.get("original_id", str(import_node.id))
|
||||
|
||||
# Already placed in the tree — don't create a duplicate.
|
||||
# The reference (next_node_id / options) is sufficient.
|
||||
if node_id in placed:
|
||||
return None
|
||||
placed.add(node_id)
|
||||
|
||||
question_text = content.get("question", "")
|
||||
|
||||
if node_type == "resolution":
|
||||
return {
|
||||
"id": content.get("original_id", str(import_node.id)),
|
||||
"id": node_id,
|
||||
"type": "solution",
|
||||
"question": content.get("question", ""),
|
||||
"children": [],
|
||||
"title": question_text,
|
||||
"description": content.get("description", ""),
|
||||
}
|
||||
|
||||
if node_type == "action":
|
||||
result = {
|
||||
"id": content.get("original_id", str(import_node.id)),
|
||||
if node_type in ("action", "warning"):
|
||||
result: dict = {
|
||||
"id": node_id,
|
||||
"type": "action",
|
||||
"question": content.get("question", ""),
|
||||
"children": [],
|
||||
"title": question_text,
|
||||
"description": content.get("description", ""),
|
||||
}
|
||||
next_id = content.get("next_node_id")
|
||||
if next_id and next_id in original_id_map:
|
||||
result["next_node_id"] = next_id
|
||||
return result
|
||||
|
||||
# question/decision type
|
||||
# question/decision type — recursively build children
|
||||
options = content.get("options", [])
|
||||
children = []
|
||||
for opt in options:
|
||||
next_id = opt.get("next_node_id")
|
||||
if next_id and next_id in original_id_map:
|
||||
child_node = _build_node(original_id_map[next_id])
|
||||
children.append(child_node)
|
||||
if child_node is not None:
|
||||
children.append(child_node)
|
||||
# If the child is an action with a next_node_id, also
|
||||
# build that target as a sibling (the tree editor
|
||||
# expects reachable nodes nested under the decision)
|
||||
_collect_action_chain(child_node, children)
|
||||
|
||||
return {
|
||||
"id": content.get("original_id", str(import_node.id)),
|
||||
"id": node_id,
|
||||
"type": "decision",
|
||||
"question": content.get("question", ""),
|
||||
"question": question_text,
|
||||
"options": [
|
||||
{"label": opt.get("label", ""), "next_node_id": opt.get("next_node_id", "")}
|
||||
for opt in options
|
||||
@@ -662,8 +690,26 @@ def _build_troubleshooting_tree(nodes: list[KBImportNode]) -> dict:
|
||||
"children": children,
|
||||
}
|
||||
|
||||
def _collect_action_chain(node: dict, siblings: list[dict]) -> None:
|
||||
"""Follow action node next_node_id chains and add targets as siblings."""
|
||||
if node.get("type") != "action":
|
||||
return
|
||||
next_id = node.get("next_node_id")
|
||||
if not next_id or next_id not in original_id_map:
|
||||
return
|
||||
# Don't add if already in this siblings list or already placed
|
||||
if any(s["id"] == next_id for s in siblings):
|
||||
return
|
||||
target = _build_node(original_id_map[next_id])
|
||||
if target is None:
|
||||
return
|
||||
siblings.append(target)
|
||||
# Continue chain if the target is also an action
|
||||
_collect_action_chain(target, siblings)
|
||||
|
||||
root_node = nodes[0]
|
||||
return _build_node(root_node)
|
||||
result = _build_node(root_node)
|
||||
return result or {"id": "root", "type": "decision", "question": "Empty", "children": []}
|
||||
|
||||
|
||||
def _build_procedural_tree(nodes: list[KBImportNode]) -> dict:
|
||||
|
||||
@@ -162,7 +162,7 @@ async def sync_steps_from_tree(
|
||||
is_active = true
|
||||
"""),
|
||||
{
|
||||
"title": step_data["title"],
|
||||
"title": step_data["title"][:255],
|
||||
"step_type": step_data["step_type"],
|
||||
"content": json.dumps(step_data["content"]),
|
||||
"created_by": str(resolved_author_id),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState } from 'react'
|
||||
import { CheckCircle2, AlertTriangle, BarChart3 } from 'lucide-react'
|
||||
import { CheckCircle2, AlertTriangle, BarChart3, CheckCheck } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { NodeCard } from './NodeCard'
|
||||
import { SourcePanel } from './SourcePanel'
|
||||
@@ -8,12 +8,13 @@ import type { KBImport, KBNodeEditRequest, KBCommitRequest } from '@/types/kbAcc
|
||||
interface ReviewScreenProps {
|
||||
kbImport: KBImport
|
||||
onEditNode: (nodeId: string, data: KBNodeEditRequest) => Promise<void>
|
||||
onApproveAll: () => Promise<void>
|
||||
onCommit: (data?: KBCommitRequest) => Promise<void>
|
||||
onDiscard: () => Promise<void>
|
||||
loading: boolean
|
||||
}
|
||||
|
||||
export function ReviewScreen({ kbImport, onEditNode, onCommit, onDiscard, loading }: ReviewScreenProps) {
|
||||
export function ReviewScreen({ kbImport, onEditNode, onApproveAll, onCommit, onDiscard, loading }: ReviewScreenProps) {
|
||||
const [highlightExcerpt, setHighlightExcerpt] = useState<string | null>(null)
|
||||
|
||||
const nodes = kbImport.nodes
|
||||
@@ -57,6 +58,21 @@ export function ReviewScreen({ kbImport, onEditNode, onCommit, onDiscard, loadin
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{approvedCount < nodes.length && (
|
||||
<button
|
||||
onClick={onApproveAll}
|
||||
disabled={loading}
|
||||
className={cn(
|
||||
'flex items-center gap-1.5 px-3 py-1.5 rounded-[8px] text-xs font-medium transition-colors',
|
||||
'bg-emerald-400/10 border border-emerald-400/20 text-emerald-400',
|
||||
'hover:bg-emerald-400/20 hover:border-emerald-400/30',
|
||||
'disabled:opacity-50 disabled:cursor-not-allowed'
|
||||
)}
|
||||
>
|
||||
<CheckCheck size={14} />
|
||||
Approve All
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -98,7 +114,7 @@ export function ReviewScreen({ kbImport, onEditNode, onCommit, onDiscard, loadin
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="shrink-0 flex items-center justify-between gap-3 pb-1">
|
||||
<button
|
||||
onClick={onDiscard}
|
||||
disabled={loading}
|
||||
|
||||
@@ -35,10 +35,11 @@ interface FlowCanvasProps {
|
||||
selectedNodeId: string | null
|
||||
onNodeSelect: (nodeId: string | null) => void
|
||||
onSelectAnswerType: (nodeId: string, type: 'decision' | 'action' | 'solution') => void
|
||||
onNodeDelete?: (nodeId: string) => void
|
||||
onNodeContextMenu?: (e: React.MouseEvent, nodeId: string) => void
|
||||
}
|
||||
|
||||
function FlowCanvasInner({ selectedNodeId, onNodeSelect, onSelectAnswerType, onNodeContextMenu }: FlowCanvasProps) {
|
||||
function FlowCanvasInner({ selectedNodeId, onNodeSelect, onSelectAnswerType, onNodeDelete, onNodeContextMenu }: FlowCanvasProps) {
|
||||
const { fitView, setCenter } = useReactFlow()
|
||||
const { nodes: layoutNodes, edges: layoutEdges, collapsedNodeIds, toggleCollapse, onNodesMeasured } = useTreeLayout(selectedNodeId)
|
||||
const [minimapVisible, setMinimapVisible] = useState(true)
|
||||
@@ -51,7 +52,7 @@ function FlowCanvasInner({ selectedNodeId, onNodeSelect, onSelectAnswerType, onN
|
||||
return {
|
||||
...n,
|
||||
selected: n.id === selectedNodeId,
|
||||
data: { ...data, onToggleCollapse: toggleCollapse, onContextMenu: onNodeContextMenu },
|
||||
data: { ...data, onToggleCollapse: toggleCollapse, onDelete: onNodeDelete, onContextMenu: onNodeContextMenu },
|
||||
}
|
||||
}
|
||||
if (n.type === 'answerStub') {
|
||||
@@ -64,7 +65,7 @@ function FlowCanvasInner({ selectedNodeId, onNodeSelect, onSelectAnswerType, onN
|
||||
}
|
||||
return n
|
||||
})
|
||||
}, [layoutNodes, selectedNodeId, toggleCollapse, onSelectAnswerType, onNodeContextMenu])
|
||||
}, [layoutNodes, selectedNodeId, toggleCollapse, onSelectAnswerType, onNodeDelete, onNodeContextMenu])
|
||||
|
||||
const [nodes, setNodes, onNodesChange] = useNodesState(nodesWithCallbacks)
|
||||
const [edges, setEdges, onEdgesChange] = useEdgesState(layoutEdges)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { memo } from 'react'
|
||||
import { Handle, Position, type NodeProps } from '@xyflow/react'
|
||||
import { HelpCircle, Zap, CheckCircle, ChevronDown, ChevronRight, AlertCircle } from 'lucide-react'
|
||||
import { HelpCircle, Zap, CheckCircle, ChevronDown, ChevronRight, AlertCircle, Trash2 } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import type { TreeStructure, NodeType } from '@/types'
|
||||
|
||||
@@ -40,14 +40,16 @@ export interface FlowCanvasNodeData {
|
||||
isCollapsed: boolean
|
||||
hasValidationErrors: boolean
|
||||
isNew: boolean
|
||||
isRoot: boolean
|
||||
onToggleCollapse: (nodeId: string) => void
|
||||
onDelete?: (nodeId: string) => void
|
||||
onContextMenu?: (e: React.MouseEvent, nodeId: string) => void
|
||||
onAcceptSuggestion?: (nodeId: string) => void
|
||||
onDismissSuggestion?: (nodeId: string) => void
|
||||
}
|
||||
|
||||
function FlowCanvasNodeComponent({ data, selected }: NodeProps) {
|
||||
const { node, hasChildren, isCollapsed, hasValidationErrors, isNew, onToggleCollapse, onContextMenu, onAcceptSuggestion, onDismissSuggestion } = data as unknown as FlowCanvasNodeData
|
||||
const { node, hasChildren, isCollapsed, hasValidationErrors, isNew, isRoot, onToggleCollapse, onDelete, onContextMenu, onAcceptSuggestion, onDismissSuggestion } = data as unknown as FlowCanvasNodeData
|
||||
const isGhost = !!(node as unknown as Record<string, unknown>)._suggestion
|
||||
const nodeType = node.type as Exclude<NodeType, 'answer'>
|
||||
const config = NODE_TYPE_CONFIG[nodeType] ?? NODE_TYPE_CONFIG.decision
|
||||
@@ -67,7 +69,7 @@ function FlowCanvasNodeComponent({ data, selected }: NodeProps) {
|
||||
<div
|
||||
onContextMenu={(e) => onContextMenu?.(e, node.id)}
|
||||
className={cn(
|
||||
'w-[280px] rounded-xl border border-border bg-card shadow-xs cursor-pointer transition-all',
|
||||
'group w-[280px] rounded-xl border border-border bg-card shadow-xs cursor-pointer transition-all',
|
||||
config.borderClass,
|
||||
selected && 'ring-1 ring-primary shadow-md',
|
||||
isGhost && 'border-dashed border-primary/40! opacity-60'
|
||||
@@ -94,6 +96,21 @@ function FlowCanvasNodeComponent({ data, selected }: NodeProps) {
|
||||
{hasValidationErrors && (
|
||||
<AlertCircle className="h-3.5 w-3.5 shrink-0 text-red-400" />
|
||||
)}
|
||||
|
||||
{/* Delete button — visible on hover, hidden for root */}
|
||||
{!isRoot && !isGhost && onDelete && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onDelete(node.id)
|
||||
}}
|
||||
className="shrink-0 rounded-md p-1 text-muted-foreground opacity-0 group-hover:opacity-100 hover:bg-red-500/20 hover:text-red-400 transition-all"
|
||||
title="Delete node"
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Decision options preview */}
|
||||
|
||||
@@ -19,6 +19,7 @@ interface TreeEditorLayoutProps {
|
||||
editingNodeId: string | null
|
||||
onNodeSelect: (nodeId: string | null) => void
|
||||
onSelectAnswerType: (nodeId: string, type: 'decision' | 'action' | 'solution') => void
|
||||
onNodeDelete?: (nodeId: string) => void
|
||||
onNodeContextMenu?: (e: React.MouseEvent, nodeId: string) => void
|
||||
}
|
||||
|
||||
@@ -29,6 +30,7 @@ export function TreeEditorLayout({
|
||||
editingNodeId,
|
||||
onNodeSelect,
|
||||
onSelectAnswerType,
|
||||
onNodeDelete,
|
||||
onNodeContextMenu,
|
||||
}: TreeEditorLayoutProps) {
|
||||
const editorMode = useTreeEditorStore(s => s.editorMode)
|
||||
@@ -72,6 +74,7 @@ export function TreeEditorLayout({
|
||||
selectedNodeId={editingNodeId}
|
||||
onNodeSelect={onNodeSelect}
|
||||
onSelectAnswerType={onSelectAnswerType}
|
||||
onNodeDelete={onNodeDelete}
|
||||
onNodeContextMenu={onNodeContextMenu}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -190,6 +190,7 @@ export function useTreeLayout(selectedNodeId?: string | null): UseTreeLayoutResu
|
||||
isCollapsed,
|
||||
hasValidationErrors: hasErrors,
|
||||
isNew: false,
|
||||
isRoot: node.id === treeStructure?.id,
|
||||
onToggleCollapse: () => {}, // placeholder — set by FlowCanvas
|
||||
} satisfies FlowCanvasNodeData,
|
||||
style: { width: NODE_WIDTH },
|
||||
|
||||
@@ -89,6 +89,26 @@ export default function KBAcceleratorPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleApproveAll = async () => {
|
||||
if (!importId || !kbImport) return
|
||||
const unapproved = kbImport.nodes.filter(n => !n.user_approved)
|
||||
if (unapproved.length === 0) return
|
||||
setLoading(true)
|
||||
try {
|
||||
await Promise.all(
|
||||
unapproved.map(n => kbAcceleratorApi.editNode(importId, n.id, { operation: 'approve' }))
|
||||
)
|
||||
setKbImport(prev => {
|
||||
if (!prev) return prev
|
||||
return { ...prev, nodes: prev.nodes.map(n => ({ ...n, user_approved: true })) }
|
||||
})
|
||||
} catch {
|
||||
toast.error('Failed to approve all nodes')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleEditNode = async (nodeId: string, data: KBNodeEditRequest) => {
|
||||
if (!importId) return
|
||||
const updatedNode = await kbAcceleratorApi.editNode(importId, nodeId, data)
|
||||
@@ -149,9 +169,9 @@ export default function KBAcceleratorPage() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full p-6">
|
||||
<div className="flex flex-col h-full min-h-0 p-6">
|
||||
{/* Page title */}
|
||||
<div className="flex items-center gap-3 mb-6">
|
||||
<div className="shrink-0 flex items-center gap-3 mb-6">
|
||||
<Sparkles size={24} className="text-primary" />
|
||||
<h1 className="text-2xl font-heading font-bold text-foreground">KB Accelerator</h1>
|
||||
</div>
|
||||
@@ -181,13 +201,16 @@ export default function KBAcceleratorPage() {
|
||||
)}
|
||||
|
||||
{phase === 'review' && kbImport && (
|
||||
<ReviewScreen
|
||||
kbImport={kbImport}
|
||||
onEditNode={handleEditNode}
|
||||
onCommit={handleCommit}
|
||||
onDiscard={handleDiscard}
|
||||
loading={loading}
|
||||
/>
|
||||
<div className="flex-1 min-h-0">
|
||||
<ReviewScreen
|
||||
kbImport={kbImport}
|
||||
onEditNode={handleEditNode}
|
||||
onApproveAll={handleApproveAll}
|
||||
onCommit={handleCommit}
|
||||
onDiscard={handleDiscard}
|
||||
loading={loading}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{phase === 'success' && commitResult && (
|
||||
|
||||
@@ -815,6 +815,7 @@ export function TreeEditorPage() {
|
||||
editingNodeId={editorAI.isOpen ? null : editingNodeId}
|
||||
onNodeSelect={handleNodeSelect}
|
||||
onSelectAnswerType={handleSelectAnswerType}
|
||||
onNodeDelete={deleteNode}
|
||||
onNodeContextMenu={editorAI.openContextMenu}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user