Spaces:
Running
Running
File size: 5,780 Bytes
3193174 81f5c1c 3193174 81f5c1c 3193174 81f5c1c 3193174 81f5c1c 3193174 5cdde73 3193174 81f5c1c 3193174 5cdde73 3193174 81f5c1c 3193174 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import { useCallback, useMemo, useState } from "react";
import {
ReactFlow,
Background,
Controls,
MiniMap,
type Edge,
type NodeTypes,
type EdgeTypes,
} from "@xyflow/react";
import "@xyflow/react/dist/style.css";
import { AgentNode } from "./AgentNode";
import { ConditionalEdge } from "./ConditionalEdge";
import { EdgeConditionDialog } from "./EdgeConditionDialog";
import { AgentForm } from "@/components/agents/AgentForm";
import { useGraphStore, type AgentNodeData, type EdgeData } from "@/stores/graphStore";
import { useAgentStore } from "@/stores/agentStore";
import type { AgentCreateRequest } from "@/types/agent";
export function GraphEditor() {
const {
nodes,
edges,
onNodesChange,
onEdgesChange,
onConnect,
addAgentNode,
removeNode,
setEdgeCondition,
editingNodeId,
setEditingNodeId,
updateNodeData,
} = useGraphStore();
const agents = useAgentStore((s) => s.agents);
const updateAgent = useAgentStore((s) => s.updateAgent);
const [selectedEdge, setSelectedEdge] = useState<Edge<EdgeData> | null>(null);
const [conditionDialogOpen, setConditionDialogOpen] = useState(false);
const nodeTypes: NodeTypes = useMemo(() => ({ agentNode: AgentNode }), []);
const edgeTypes: EdgeTypes = useMemo(() => ({ conditionEdge: ConditionalEdge }), []);
const handleEdgeClick = useCallback((_: React.MouseEvent, edge: Edge) => {
setSelectedEdge(edge as Edge<EdgeData>);
setConditionDialogOpen(true);
}, []);
const handleEdgeConditionSave = useCallback(
(edgeId: string, condition: string, weight: number) => {
setEdgeCondition(edgeId, condition, weight);
},
[setEdgeCondition]
);
const handleDragOver = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.dataTransfer.dropEffect = "move";
}, []);
const handleDrop = useCallback(
(e: React.DragEvent) => {
e.preventDefault();
const agentId = e.dataTransfer.getData("application/agent-id");
if (!agentId) return;
const agent = agents.find((a) => a.agent_id === agentId);
if (!agent) return;
const reactFlowBounds = (e.target as HTMLElement)
.closest(".react-flow")
?.getBoundingClientRect();
if (!reactFlowBounds) return;
const position = {
x: e.clientX - reactFlowBounds.left - 100,
y: e.clientY - reactFlowBounds.top - 60,
};
addAgentNode(agent, position);
},
[agents, addAgentNode]
);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === "Delete" || e.key === "Backspace") {
const selectedNodes = nodes.filter((n) => n.selected);
selectedNodes.forEach((n) => removeNode(n.id));
}
},
[nodes, removeNode]
);
// Build AgentProfile-like object from the node being edited
const editingNode = editingNodeId
? nodes.find((n) => n.id === editingNodeId)
: null;
const editingAgentProfile = editingNode
? {
agent_id: editingNode.data.agentId,
display_name: editingNode.data.displayName,
persona: editingNode.data.persona,
description: editingNode.data.description,
llm_backbone: editingNode.data.llmBackbone,
tools: editingNode.data.tools,
llm_config: null,
input_schema: null,
output_schema: null,
}
: null;
const handleAgentEditSubmit = async (data: AgentCreateRequest) => {
if (!editingNodeId) return;
// Update the node on the canvas
updateNodeData(editingNodeId, {
displayName: data.display_name,
persona: data.persona || "",
description: data.description || "",
llmBackbone: data.llm_backbone || null,
tools: data.tools || [],
});
// Also update the agent on the backend
try {
await updateAgent(editingNodeId, data);
} catch {
// agent may not exist on server yet (template-only)
}
};
return (
<div className="flex-1 h-full" onKeyDown={handleKeyDown} tabIndex={0}>
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
onEdgeClick={handleEdgeClick}
onDragOver={handleDragOver}
onDrop={handleDrop}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
fitView
snapToGrid
snapGrid={[15, 15]}
defaultEdgeOptions={{
type: "conditionEdge",
animated: true,
}}
deleteKeyCode={["Backspace", "Delete"]}
>
<Background gap={15} size={1} />
<Controls />
<MiniMap
nodeColor={(n) => {
const data = n.data as unknown as AgentNodeData;
switch (data.executionStatus) {
case "running":
return "#3b82f6";
case "completed":
return "#22c55e";
case "error":
return "#ef4444";
case "pending":
return "#eab308";
default:
return "#94a3b8";
}
}}
maskColor="rgba(0, 0, 0, 0.1)"
/>
</ReactFlow>
<EdgeConditionDialog
open={conditionDialogOpen}
onOpenChange={setConditionDialogOpen}
edgeId={selectedEdge?.id || ""}
currentCondition={(selectedEdge?.data as EdgeData | undefined)?.condition}
currentWeight={(selectedEdge?.data as EdgeData | undefined)?.weight ?? 1.0}
onSave={handleEdgeConditionSave}
/>
<AgentForm
open={!!editingNodeId}
onOpenChange={(open) => { if (!open) setEditingNodeId(null); }}
agent={editingAgentProfile}
onSubmit={handleAgentEditSubmit}
/>
</div>
);
}
|