"use client"
import type { ReactNode } from "react"
import { Trash2Icon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { OptionCombobox } from "@/components/option-combobox"
import type { AIWorkflowDefinition, AIWorkflowNodeSpec } from "@/lib/api/admin"
import { cn } from "@/lib/utils"
import { VariableSelector } from "./variable-selector"
import {
buildVariableSpecDisplay,
createConditionBranchID,
isRefValue,
normalizeNodeConfig,
refField,
refNodeId,
type WorkflowConditionBranch,
type WorkflowVariableRef,
} from "./workflow-utils"
export type WorkflowBranchSummary = {
branchId: string
targetNodeId?: string
targetName?: string
}
const CONDITION_OPERATOR_OPTIONS = [
{ value: "eq", label: "等于" },
{ value: "neq", label: "不等于" },
{ value: "contains", label: "包含" },
{ value: "not_contains", label: "不包含" },
{ value: "gt", label: "大于" },
{ value: "gte", label: "大于等于" },
{ value: "lt", label: "小于" },
{ value: "lte", label: "小于等于" },
{ value: "exists", label: "存在" },
{ value: "empty", label: "为空" },
]
const cardInputClassName = "h-8 rounded-md border-slate-200 bg-white text-xs shadow-none"
const cardComboboxClassName = "h-8 rounded-md border-slate-200 bg-white text-xs shadow-none"
export function NodeConfigPanel({
node,
nodeSpec,
nodes,
availableVariables,
showHeader = true,
showConditionBranches = true,
onChange,
onDelete,
}: {
node: AIWorkflowDefinition["nodes"][number] | null
nodeSpec?: AIWorkflowNodeSpec
nodes: AIWorkflowDefinition["nodes"]
availableVariables?: WorkflowVariableRef[]
branchSummaries?: WorkflowBranchSummary[]
showHeader?: boolean
showConditionBranches?: boolean
onChange: (nodeId: string, data: AIWorkflowDefinition["nodes"][number]["data"]) => void
onDelete?: (nodeId: string) => void
}) {
if (!node) {
return (
未选择节点
)
}
const inputsValues = node.data?.inputsValues ?? {}
const inputSchema = nodeSpec?.inputSchema ?? []
const outputSchema = nodeSpec?.outputSchema ?? []
const canDelete = node.type !== "start" && node.type !== "end"
const config = normalizeNodeConfig(node.data?.config)
const branches = config.branches ?? []
const updateData = (data: Partial) => {
onChange(node.id, {
...(node.data ?? {}),
...data,
})
}
const updateConfig = (nextConfig: Record) => updateData({ config: nextConfig })
const updateBranch = (branch: WorkflowConditionBranch) => {
const nextBranches = branches.some((item) => item.id === branch.id)
? branches.map((item) => (item.id === branch.id ? branch : item))
: [...branches, branch]
updateConfig({ ...config, branches: nextBranches })
}
const deleteBranch = (branchId: string) => {
updateConfig({ ...config, branches: branches.filter((branch) => branch.id !== branchId) })
}
const addBranch = () => {
const targetNodeId = nodes.find((item) => item.id !== node.id && item.type !== "start")?.id ?? ""
updateBranch({
id: createConditionBranchID(branches),
name: "新分支",
targetNodeId,
condition: {
operator: "eq",
},
})
}
return (
{showHeader ? (
{node.data?.title || nodeSpec?.title || node.type}
{node.id}
{canDelete ? (
) : null}
) : null}
{inputSchema.length > 0 ? (
{inputSchema.map((input) => {
const value = inputsValues[input.name]
return (
{
updateData({
inputsValues: {
...inputsValues,
[input.name]: next,
},
})
}}
/>
)
})}
) : null}
{showConditionBranches && (node.type === "condition" || branches.length > 0) ? (
) : null}
{outputSchema.length > 0 ? (
{outputSchema.map((output) => {
const item = buildVariableSpecDisplay(output)
return (
{item.label}
{item.subtitle}
{item.description ? (
{item.description}
) : null}
)
})}
) : null}
)
}
export function ConditionBranchConfigPanel({
node,
nodes,
branchId,
variables,
onChange,
onDelete,
}: {
node: AIWorkflowDefinition["nodes"][number]
nodes: AIWorkflowDefinition["nodes"]
branchId: string
variables: WorkflowVariableRef[]
onChange: (nodeId: string, data: AIWorkflowDefinition["nodes"][number]["data"]) => void
onDelete: (branchId: string) => void
}) {
const config = normalizeNodeConfig(node.data?.config)
const branches = config.branches ?? []
const branch = branches.find((item) => item.id === branchId)
const targetOptions = buildTargetOptions(nodes, node.id)
if (!branch) {
return (
条件分支不存在
)
}
const updateBranch = (nextBranch: WorkflowConditionBranch) => {
onChange(node.id, {
...(node.data ?? {}),
config: {
...config,
branches: branches.map((item) => (item.id === nextBranch.id ? nextBranch : item)),
},
})
}
return (
{branch.default ? (
默认分支不需要条件表达式,会在其他条件不匹配时执行。
) : (
)}
{branch.default ? null : (
)}
)
}
function ConditionBranchesEditor({
branches,
nodes,
currentNodeId,
variables,
onAdd,
onChange,
onDelete,
}: {
branches: WorkflowConditionBranch[]
nodes: AIWorkflowDefinition["nodes"]
currentNodeId: string
variables: WorkflowVariableRef[]
onAdd: () => void
onChange: (branch: WorkflowConditionBranch) => void
onDelete: (branchId: string) => void
}) {
const targetOptions = buildTargetOptions(nodes, currentNodeId)
return (
添加
}
>
{branches.length === 0 ? (
暂无分支。条件节点需要至少一个默认分支或条件分支。
) : null}
{branches.map((branch) => {
return (
)
})}
)
}
function ConditionFields({
branch,
variables,
onChange,
compact = false,
}: {
branch: WorkflowConditionBranch
variables: WorkflowVariableRef[]
onChange: (branch: WorkflowConditionBranch) => void
compact?: boolean
}) {
const condition = branch.condition ?? {}
const selectedVariable = isRefValue(condition.left)
? variables.find((item) => item.nodeId === refNodeId(condition.left) && item.field === refField(condition.left))
: undefined
const valueOptions = selectedVariable?.valueOptions ?? []
const rightDisabled = ["exists", "empty"].includes(condition.operator ?? "")
return (
onChange({
...branch,
condition: { ...condition, left },
})}
/>
{compact ?
: null}
{compact ? null : }
onChange({
...branch,
condition: { ...condition, operator },
})}
/>
{compact ? null : }
{valueOptions.length > 0 && !rightDisabled ? (
({
value: stringifyConditionRight(option.value),
label: option.label || stringifyConditionRight(option.value),
description: option.description,
}))}
placeholder="选择取值"
triggerClassName={cardComboboxClassName}
onChange={(nextValue) => {
const selectedOption = valueOptions.find((option) => stringifyConditionRight(option.value) === nextValue)
onChange({
...branch,
condition: { ...condition, right: selectedOption?.value ?? nextValue },
})
}}
/>
) : (
onChange({
...branch,
condition: { ...condition, right: event.target.value },
})}
/>
)}
)
}
function ConfigCard({
title,
meta,
action,
children,
}: {
title: string
meta?: string
action?: ReactNode
children: ReactNode
}) {
return (
{meta ? {meta} : null}
{action}
{children}
)
}
function buildTargetOptions(nodes: AIWorkflowDefinition["nodes"], currentNodeId: string) {
return nodes
.filter((node) => node.id !== currentNodeId && node.type !== "start")
.map((node) => ({
value: node.id,
label: node.data?.title || node.type || node.id,
}))
}
function stringifyConditionRight(value: unknown) {
if (value === undefined || value === null) {
return ""
}
if (typeof value === "string") {
return value
}
return JSON.stringify(value)
}