import { Field, type WorkflowNodeRegistry } from "@flowgram.ai/free-layout-editor" import { PlusIcon } from "lucide-react" import { Button } from "@/components/ui/button" import type { AIWorkflowNodeSpec } from "@/lib/api/admin" import { createConditionBranchID, normalizeNodeConfig, type WorkflowConditionBranch, } from "./workflow-utils" export function buildFlowgramNodeRegistries(nodeSpecs: AIWorkflowNodeSpec[]): WorkflowNodeRegistry[] { const seen = new Set() const specs = nodeSpecs.length > 0 ? nodeSpecs : [ { type: "start", title: "开始" }, { type: "end", title: "结束" }, ] return specs .filter((spec) => { if (!spec.type || seen.has(spec.type)) { return false } seen.add(spec.type) return true }) .map((spec) => ({ type: spec.type, meta: { defaultExpanded: true, isStart: spec.type === "start", deleteDisable: spec.type === "start" || spec.type === "end", copyDisable: spec.type === "start" || spec.type === "end", defaultPorts: defaultPortsForNodeType(spec.type), }, formMeta: { render: () => , }, })) } function FlowgramNodeForm({ nodeType, fallbackTitle, }: { nodeType: string fallbackTitle: string }) { if (nodeType === "condition") { return } return (
name="title"> {({ field }) => (
{field.value || fallbackTitle}
)}
{nodeType}
) } function defaultPortsForNodeType(type: string) { if (type === "start") { return [{ type: "output" as const }] } if (type === "end") { return [{ type: "input" as const }] } if (type === "condition") { return [{ type: "input" as const }] } return [{ type: "input" as const }, { type: "output" as const }] } function ConditionNodeForm({ fallbackTitle }: { fallbackTitle: string }) { return (
name="title"> {({ field }) => (
{field.value || fallbackTitle}
)} > name="config"> {({ field }) => { const config = normalizeNodeConfig(field.value) const branches = ensureConditionBranches(config.branches ?? []) const updateBranches = (nextBranches: WorkflowConditionBranch[]) => { field.onChange({ ...config, branches: ensureConditionBranches(nextBranches), }) } return (
{branches.map((branch) => (
{branch.name || (branch.default ? "默认分支" : branch.id)} {branch.default ? ( else ) : null}
))}
) }}
) } function ensureConditionBranches(branches: WorkflowConditionBranch[]) { const normalized = branches.some((branch) => branch.default) ? branches : [...branches, { id: "default", name: "默认分支", targetNodeId: "", default: true }] return [ ...normalized.filter((branch) => !branch.default), ...normalized.filter((branch) => branch.default).slice(0, 1), ] }