Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

13.9 KB
Raw Blame
"use client";
import { useMemo, useState } from "react";
import { Bot, Braces, GitBranch, Plus, Wrench, Zap } from "lucide-react";
import {
  Handle, MarkerType, Position,
  type Connection, type Edge, type Node, type NodeProps,
} from "@xyflow/react";
import type { WorkflowGraph, WorkflowGraphNode, WorkflowGraphNodeState, WorkflowGraphRunEdgeState } from "@/lib/contracts/workflow-graph";
import { useGraphProjection } from "@/components/shared/use-graph-projection";
import { GraphCanvas } from "@/components/shared/graph-canvas";
import { GraphCustomControls } from "@/components/shared/graph-custom-controls";
import { GraphCustomCard } from "@/components/shared/graph-custom-card";
import { projectCustomNodes, moveGraphNodesWithCustomGroups, type CustomCanvasNode } from "@/components/shared/graph-custom-projection";
import { graphRoutedEdgeTypes } from "@/components/shared/graph-routed-edge";
import { graphFocusClusterIds } from "@/components/shared/graph-focus";
import { GraphNodeShell } from "@/components/shared/graph-node-shell";
import type { GraphCustomNode } from "@/lib/contracts/graph-custom-nodes";
import { compactWorkflowFocusIds } from "../lib/compact-focus";
import { cn } from "@/lib/utils";

interface WorkflowNodeData extends Record<string, unknown> {
  node: WorkflowGraphNode;
  state?: WorkflowGraphNodeState;
  quickAdd?: (sourceId: string, sourceHandle?: string) => void;
}
type FlowNode = Node<WorkflowNodeData, "workflow">;
type FlowEdge = Edge;

const flowTypes = new Set(["condition", "switch", "merge", "batch", "loop", "repeat", "wait", "subflow", "output"]);
const contextTypes = new Set(["cache", "memory", "session", "directory", "project", "folder", "skill", "knowledge"]);
function nodeFamily(node: WorkflowGraphNode) {
  if (["manual", "schedule", "webhook"].includes(node.type)) return { label: "Trigger", Icon: Zap };
  if (node.type === "agent") return { label: "AI", Icon: Bot };
  if (flowTypes.has(node.type)) return { label: "Flow", Icon: GitBranch };
  if (contextTypes.has(node.type)) return { label: "Context", Icon: Braces };
  return { label: "Action", Icon: Wrench };
}
function sourceHandles(node: WorkflowGraphNode): Array<string | undefined> {
  if (node.type === "condition") return ["true", "false"];
  if (node.type === "repeat") return ["done", "exhausted"];
  if (node.type === "switch") {
    const cases = Array.isArray(node.config.cases) ? node.config.cases : [];
    return [...cases.map((row, index) => row && typeof row === "object" && typeof (row as {handle?: unknown}).handle === "string" ? String((row as {handle: string}).handle) : `case-${index + 1}`), "default"];
  }
  return [undefined];
}
const canError = (node: WorkflowGraphNode) => !["manual", "schedule", "webhook", "output"].includes(node.type);
const canInput = (node: WorkflowGraphNode) => !["manual", "schedule", "webhook"].includes(node.type);
const canOutput = (node: WorkflowGraphNode) => node.type !== "output";
function nodeMetrics(node: WorkflowGraphNode) {
  const semantic = node.config.sessionStep === true || node.config.sessionRoot === true, handles = sourceHandles(node);
  return { width: semantic ? 250 : 210, height: semantic ? 104 : Math.max(84, 54 + Math.max(handles.length, canError(node) ? 2 : 1) * 20) };
}
function WorkflowNodeCard({ data, selected }: NodeProps<FlowNode>) {
  const { node, state, quickAdd } = data, handles = sourceHandles(node), metrics = nodeMetrics(node), semantic = node.config.sessionStep === true || node.config.sessionRoot === true;
  const actionCount = typeof node.config.actionCount === "number" ? node.config.actionCount : undefined, summary = typeof node.config.summary === "string" ? node.config.summary : undefined;
  const { label, Icon } = nodeFamily(node);
  return <GraphNodeShell selected={selected} className={cn(semantic ? "w-[250px]" : "w-[210px]", state === "failed" && "border-destructive ring-1 ring-destructive", state === "running" && "border-primary ring-1 ring-primary", ["skipped", "blocked"].includes(state ?? "") && "opacity-55", node.disabled && "opacity-50")} style={{ minHeight: metrics.height }}>
    {canInput(node) ? <Handle type="target" position={Position.Left} id="input" className="!size-3.5 !border-2 !border-background !bg-muted-foreground"/> : null}
    <div className="flex items-start gap-2">
      <span className="grid size-7 shrink-0 place-items-center rounded-lg border bg-background"><Icon className="size-3.5 text-muted-foreground"/></span>
      <div className="min-w-0 flex-1">
        <div className={cn("text-xs font-semibold", semantic ? "line-clamp-2 leading-4" : "truncate")}>{node.name}</div>
        <div className="mt-1 flex flex-wrap items-center gap-1.5 text-[9px] uppercase tracking-wide text-muted-foreground">
          <span>{label}</span><span>·</span><span>{node.type.replaceAll("_", " ")}</span>
          {state ? <><span>·</span><span className={cn(state === "failed" && "text-destructive", state === "completed" && "text-success", state === "running" && "text-primary")}>{state}</span></> : null}
        </div>
      </div>
    </div>
    {actionCount !== undefined ? <span className="mt-2 inline-flex rounded bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground">{actionCount} actions</span> : null}
    {summary ? <p className="mt-1.5 line-clamp-2 text-[10px] leading-4 text-muted-foreground">{summary}</p> : null}
    {canOutput(node) ? handles.map((handle, index) => <div key={handle ?? "output"} className="absolute right-0 flex translate-x-1/2 items-center" style={{ top: 42 + index * 20 }}>
      {handle ? <span className="pointer-events-none absolute right-4 whitespace-nowrap pr-1 text-[8px] text-muted-foreground">{handle}</span> : null}
      <Handle type="source" position={Position.Right} id={handle ?? "output"} className="!relative !right-auto !top-auto !size-3.5 !translate-x-0 !translate-y-0 !border-2 !border-background !bg-muted-foreground"/>
      {quickAdd ? <button type="button" aria-label={`Add after ${node.name}${handle ? ` (${handle})` : ""}`} title="Add and connect next node" className="nodrag nopan absolute left-5 grid size-5 place-items-center rounded-full border bg-background text-muted-foreground opacity-0 shadow-sm transition-opacity hover:text-foreground focus-visible:opacity-100 [.react-flow__node:hover_&]:opacity-100" onPointerDown={(event) => event.stopPropagation()} onClick={(event) => { event.stopPropagation(); quickAdd(node.id, handle); }}><Plus className="size-3"/></button> : null}
    </div>) : null}
    {canError(node) ? <Handle type="source" position={Position.Bottom} id="error" className="!size-3 !border-2 !border-background !bg-destructive"/> : null}
  </GraphNodeShell>;
}
const nodeTypes = { workflow: WorkflowNodeCard, customGroup: GraphCustomCard };
type CanvasNode = FlowNode | CustomCanvasNode;
function toNodes(graph: WorkflowGraph, selectedId: string | null, nodeStates?: ReadonlyMap<string, WorkflowGraphNodeState>, quickAdd?: (sourceId: string, sourceHandle?: string) => void): FlowNode[] {
  return graph.nodes.map((node) => {
    const metrics = nodeMetrics(node);
    return { id: node.id, type: "workflow", position: node.position, width: metrics.width, height: metrics.height, selected: node.id === selectedId, data: { node, state: nodeStates?.get(node.id), quickAdd } };
  });
}
function edgeVisual(edge: WorkflowGraph["edges"][number], state: WorkflowGraphRunEdgeState | undefined, nodeStates?: ReadonlyMap<string, WorkflowGraphNodeState>) {
  const running = nodeStates?.get(edge.source) === "running" || nodeStates?.get(edge.target) === "running", error = edge.sourceHandle === "error", branch = Boolean(edge.sourceHandle && edge.sourceHandle !== "output"), disabled = edge.disabled || state === "disabled";
  const stroke = error ? "var(--destructive)" : running ? "var(--accent)" : state === "enabled" ? "var(--success)" : "var(--muted-foreground)";
  const strokeDasharray = disabled ? "2 6" : edge.style === "dashed" ? "7 5" : edge.style === "solid" ? undefined : branch ? "7 5" : undefined;
  const opacity = disabled ? 0.24 : state === "pending" ? 0.55 : 1;
  return { animated: running && !disabled, style: { stroke, strokeWidth: running ? 2.5 : state === "enabled" ? 2 : 1.5, opacity, strokeDasharray }, markerEnd: { type: MarkerType.ArrowClosed, width: 18, height: 18, color: stroke } };
}
function toEdges(graph: WorkflowGraph, nodeStates?: ReadonlyMap<string, WorkflowGraphNodeState>, edgeStates?: ReadonlyMap<string, WorkflowGraphRunEdgeState>): FlowEdge[] {
  return graph.edges.map((edge) => ({ id: edge.id, source: edge.source, target: edge.target, sourceHandle: edge.sourceHandle ?? "output", targetHandle: edge.targetHandle ?? "input", type: "routed", ...edgeVisual(edge, edgeStates?.get(edge.id), nodeStates) }));
}
function minimapColor(node: FlowNode) {
  const state = node.data.state;
  if (state === "running") return "var(--accent)";
  if (state === "failed") return "var(--destructive)";
  if (state === "completed") return "var(--success)";
  if (state === "skipped" || state === "blocked") return "var(--text-dim)";
  return "var(--info)";
}

export function WorkflowCanvas({ graph, selectedId, onSelect, onMove, onMoveMany, onCustomNodes, onConnect, onQuickAdd, nodeStates, edgeStates, onOpen, onDeleteNodes, onDeleteEdges, onTidy, readOnly = false }: {
  graph: WorkflowGraph;
  selectedId: string | null;
  onSelect: (id: string | null) => void;
  onMove: (id: string, position: {x: number; y: number}) => void;
  onMoveMany?: (nodes: WorkflowGraphNode[]) => void;
  onCustomNodes?: (groups: GraphCustomNode[]) => void;
  onConnect: (source: string, target: string, sourceHandle?: string) => void;
  onQuickAdd?: (sourceId: string, sourceHandle?: string) => void;
  nodeStates?: ReadonlyMap<string, WorkflowGraphNodeState>;
  edgeStates?: ReadonlyMap<string, WorkflowGraphRunEdgeState>;
  onOpen?: (id: string) => void;
  onDeleteNodes?: (ids: string[]) => void;
  onDeleteEdges?: (ids: string[]) => void;
  onTidy?: () => void;
  readOnly?: boolean;
}) {
  const [activeGroupId, setActiveGroupId] = useState<string | null>(null);
  const groups = useMemo(() => graph.metadata.customNodes ?? [], [graph.metadata.customNodes]);
  const selection = useMemo(() => activeGroupId ? [activeGroupId] : selectedId ? [selectedId] : [], [activeGroupId, selectedId]);
  const mapped = useMemo(() => projectCustomNodes(toNodes(graph, null, nodeStates, readOnly ? undefined : onQuickAdd), toEdges(graph, nodeStates, edgeStates), groups, selection), [edgeStates, graph, groups, nodeStates, onQuickAdd, readOnly, selection]);
  const { nodes, edges, onNodesChange, onEdgesChange } = useGraphProjection<CanvasNode, FlowEdge>(mapped);
  const selectedIds = nodes.filter((node) => node.selected).map((node) => node.id);
  const changeGroups = (next: GraphCustomNode[]) => {
    onCustomNodes?.(next);
    if (activeGroupId && !next.some((group) => group.id === activeGroupId)) setActiveGroupId(null);
    onSelect(null);
  };
  const move = (items: CanvasNode[]) => {
    const next = moveGraphNodesWithCustomGroups(graph.nodes, groups, items);
    if (onMoveMany) onMoveMany(next); else for (const item of items) onMove(item.id, item.position);
  };
  const connect = (connection: Connection) => {
    if (!graph.nodes.some((n) => n.id === connection.source) || !graph.nodes.some((n) => n.id === connection.target) || connection.source === connection.target) return;
    const handle = connection.sourceHandle && connection.sourceHandle !== "output" ? connection.sourceHandle : undefined;
    onConnect(connection.source!, connection.target!, handle);
  };
  const visibleIds = useMemo(() => new Set(nodes.map((node) => node.id)), [nodes]);
  const rootFocusIds = useMemo(() => compactWorkflowFocusIds(graph.nodes, groups, visibleIds, readOnly ? 2 : 1), [graph.nodes, groups, readOnly, visibleIds]);
  const compactIds = graphFocusClusterIds(nodes.map((node) => node.id), edges, selectedId ? [selectedId] : rootFocusIds, { depth: 1, maxNodes: readOnly ? 12 : 8, fallbackLimit: readOnly ? 2 : 1 });
  return <div className="flex h-full min-h-0 flex-col">
    {!readOnly && onCustomNodes ? <GraphCustomControls groups={groups} selectedIds={selectedIds} nodeIds={graph.nodes.map((n) => n.id)} onChange={changeGroups}/> : null}
    <div className="min-h-0 flex-1"><GraphCanvas<CanvasNode, FlowEdge> ariaLabel="Workflow canvas" showMinimap={nodes.length > 3} nodes={nodes} edges={edges} nodeTypes={nodeTypes} edgeTypes={graphRoutedEdgeTypes}
      onNodesChange={onNodesChange} onEdgesChange={onEdgesChange} onConnect={readOnly ? undefined : connect}
      onPaneClick={() => { setActiveGroupId(null); onSelect(null); }}
      onNodeClick={(event, item) => {
        if (event.ctrlKey || event.metaKey) return;
        if (item.type === "customGroup") { setActiveGroupId(item.id); onSelect(null); return; }
        setActiveGroupId(null); onSelect(item.id);
      }}
      onEdgeClick={readOnly ? undefined : (_, edge) => { if (graph.nodes.some((n) => n.id === edge.source)) { setActiveGroupId(null); onSelect(edge.source); } }}
      onNodeDoubleClick={(_, item) => {
        if (item.type === "customGroup") { if (!readOnly) changeGroups(groups.map((g) => g.id === item.id ? { ...g, collapsed: false } : g)); return; }
        const node = (item as FlowNode).data.node;
        if ((readOnly || node.type === "project" || node.type === "folder") && onOpen) onOpen(node.id);
      }}
      onNodeDragStop={(_, item, items) => move(items.length ? items : [item])} onSelectionDragStop={(_, items) => move(items)}
      onNodesDelete={readOnly ? undefined : (items) => onDeleteNodes?.(items.filter((n) => n.type !== "customGroup").map((n) => n.id))}
      onEdgesDelete={readOnly ? undefined : (items) => onDeleteEdges?.(items.map((item) => item.id))}
      nodesDraggable={!readOnly} nodesConnectable={!readOnly} edgesReconnectable={false} deleteKeyCode={readOnly ? null : ["Backspace", "Delete"]} onTidy={onTidy} snapToGrid snapGrid={[12, 12]}
      miniMapNodeColor={(node) => node.type === "customGroup" ? "var(--primary)" : minimapColor(node as FlowNode)} compactFitNodeIds={compactIds} compactFitMaxZoom={readOnly ? 0.72 : 0.9} initialFitNodeIds={compactIds} initialFitMaxZoom={readOnly ? 0.78 : 0.94}/></div>
  </div>;
}