droboticflow/src/pages/Operators.tsx

772 lines
38 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useEffect, useState } from "react";
import { Cpu, Plus, Search, Code, Box, X, Info, Layers, Edit3, Trash2, Sparkles, Loader2, AlertCircle } from "lucide-react";
import { useUser } from "../context/UserContext";
import { cn } from "../lib/utils";
import { Operator, PortDefinition } from "../types";
import { generateOperatorFromDescription } from "../services/aiService";
import { operatorsApi } from "../services/api/operators";
import { useAIStatus } from "../hooks/useAIStatus";
function createPortID() {
if (typeof globalThis.crypto?.randomUUID === "function") {
return globalThis.crypto.randomUUID();
}
if (typeof globalThis.crypto?.getRandomValues === "function") {
const bytes = new Uint8Array(16);
globalThis.crypto.getRandomValues(bytes);
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
}
return `port-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
}
export default function Operators() {
const { role } = useUser();
const aiStatus = useAIStatus();
const [operators, setOperators] = useState<Operator[]>([]);
const [searchQuery, setSearchQuery] = useState("");
const [isModalOpen, setIsModalOpen] = useState(false);
const [isAIModalOpen, setIsAIModalOpen] = useState(false);
const [aiDescription, setAiDescription] = useState("");
const [isGenerating, setIsGenerating] = useState(false);
const [aiError, setAiError] = useState<string | null>(null);
const [editingOpId, setEditingOpId] = useState<string | null>(null);
const [activeTab, setActiveTab] = useState<"basic" | "ports" | "hyperparams" | "skill">("basic");
const [newOp, setNewOp] = useState<Partial<Operator>>({
type: "custom",
category: "CV",
inputs: [],
outputs: [],
hyperparams: [],
skillDefinition: ""
});
useEffect(() => {
operatorsApi.list().then(setOperators).catch((error) => {
console.error("Failed to load operators", error);
});
}, []);
const handleAddHyperParam = () => {
const hp = {
key: `PARAM_${(newOp.hyperparams?.length || 0) + 1}`,
value: "",
description: ""
};
setNewOp({ ...newOp, hyperparams: [...(newOp.hyperparams || []), hp] });
};
const handleAddPort = (type: 'inputs' | 'outputs') => {
const nextIndex = (newOp[type]?.length ?? 0) + 1;
const port: PortDefinition = {
id: createPortID(),
name: `端口 ${nextIndex}`,
type: "json",
transmission: "bus"
};
setNewOp({ ...newOp, [type]: [...(newOp[type] || []), port] });
};
const handleRegister = () => {
if (newOp.name && newOp.description) {
if (editingOpId) {
operatorsApi
.update(editingOpId, newOp)
.then((updated) =>
setOperators(operators.map((op) => (op.id === editingOpId ? updated : op))),
)
.catch((error) => console.error("Failed to update operator", error));
} else {
operatorsApi
.create({
name: newOp.name,
type: "custom",
category: newOp.category || "CV",
description: newOp.description,
inputs: newOp.inputs || [],
outputs: newOp.outputs || [],
hyperparams: newOp.hyperparams || [],
image: newOp.image,
skillDefinition: newOp.skillDefinition,
})
.then((created) => setOperators([created, ...operators]))
.catch((error) => console.error("Failed to create operator", error));
}
setIsModalOpen(false);
setEditingOpId(null);
setActiveTab("basic");
setNewOp({ type: "custom", category: "CV", inputs: [], outputs: [], hyperparams: [], skillDefinition: "" });
}
};
const handleEdit = (op: Operator) => {
setEditingOpId(op.id);
setNewOp({ ...op });
setActiveTab("basic");
setIsModalOpen(true);
};
const handleAIGenerate = async () => {
if (!aiDescription.trim()) return;
setIsGenerating(true);
setAiError(null);
try {
const generated = await generateOperatorFromDescription(aiDescription);
setNewOp({
...newOp,
name: generated.name,
category: generated.category as any,
description: generated.description,
inputs: generated.inputs.map(i => ({ id: createPortID(), ...i })) as any,
outputs: generated.outputs.map(o => ({ id: createPortID(), ...o })) as any,
hyperparams: generated.hyperparams || [],
skillDefinition: generated.skillDefinition
});
setIsAIModalOpen(false);
setIsModalOpen(true);
setActiveTab("basic");
} catch (err) {
setAiError(err instanceof Error ? err.message : "生成失败,请重试。");
} finally {
setIsGenerating(false);
}
};
const handleDelete = (id: string) => {
operatorsApi
.remove(id)
.then(() => setOperators(operators.filter((op) => op.id !== id)))
.catch((error) => console.error("Failed to delete operator", error));
};
const canManage = role === "admin" || role === "operator";
const normalizedSearch = searchQuery.trim().toLowerCase();
const filteredOperators = operators.filter((op) => {
if (!normalizedSearch) {
return true;
}
return [op.name, op.description, op.category, op.type]
.filter(Boolean)
.some((value) => value.toLowerCase().includes(normalizedSearch));
});
return (
<div className="space-y-8">
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold tracking-tight text-white">算子管理</h1>
<p className="text-gray-500 mt-1">工作流的基础功能模块,运行在 K8s 集群中</p>
</div>
{canManage && (
<div className="flex gap-3">
<button
onClick={() => {
setAiDescription("");
setAiError(null);
setIsAIModalOpen(true);
}}
disabled={!aiStatus.enabled}
title={aiStatus.enabled ? `使用 ${aiStatus.model || "glm-5"} 辅助生成算子定义` : (aiStatus.reason || "当前服务端未配置 AI 服务")}
className={cn(
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20",
aiStatus.enabled
? "bg-white/5 hover:bg-white/10 text-blue-400"
: "bg-white/5 text-gray-500 cursor-not-allowed opacity-60",
)}
>
<Sparkles className="w-4 h-4" />
AI 辅助注册
</button>
<button
onClick={() => {
setEditingOpId(null);
setNewOp({ type: "custom", category: "CV", inputs: [], outputs: [], skillDefinition: "" });
setIsModalOpen(true);
}}
className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md text-sm font-medium transition-colors"
>
<Plus className="w-4 h-4" />
注册算子
</button>
</div>
)}
</div>
<div className="flex items-center gap-4 bg-[#151619] p-4 rounded-lg border border-[#2a2b2f]">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-500" />
<input
type="text"
placeholder="搜索算子..."
value={searchQuery}
onChange={(event) => setSearchQuery(event.target.value)}
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md pl-10 pr-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors"
/>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{filteredOperators.map((op) => (
<div
key={op.id}
data-testid="operator-card"
onDoubleClick={() => canManage && handleEdit(op)}
className={cn(
"bg-[#151619] border border-[#2a2b2f] rounded-xl p-6 hover:bg-[#1a1b1e] transition-all group relative",
canManage && "cursor-pointer"
)}
>
<div className="flex items-center justify-between mb-4">
<div className={cn(
"w-12 h-12 rounded-lg flex items-center justify-center",
op.type === "builtin" ? "bg-blue-500/10 text-blue-500" : "bg-purple-500/10 text-purple-500"
)}>
{op.category === "IO" ? <Box className="w-6 h-6" /> : <Code className="w-6 h-6" />}
</div>
<div className="flex items-center gap-2">
{op.type === "custom" && canManage && (
<>
<button onClick={() => handleEdit(op)} className="p-1.5 hover:bg-white/5 rounded text-gray-500 hover:text-blue-400 transition-colors">
<Edit3 className="w-3.5 h-3.5" />
</button>
<button onClick={() => handleDelete(op.id)} className="p-1.5 hover:bg-white/5 rounded text-gray-500 hover:text-red-400 transition-colors">
<Trash2 className="w-3.5 h-3.5" />
</button>
</>
)}
<span className={cn(
"text-[10px] px-2 py-0.5 rounded-full font-mono uppercase tracking-wider",
op.type === "builtin" ? "bg-blue-500/10 text-blue-500 border border-blue-500/20" : "bg-purple-500/10 text-purple-500 border border-purple-500/20"
)}>
{op.type === "builtin" ? "Built-in" : "Custom"}
</span>
{op.skillDefinition && (
<span className="text-[10px] px-2 py-0.5 rounded-full font-mono uppercase tracking-wider bg-green-500/10 text-green-500 border border-green-500/20 flex items-center gap-1">
<Layers className="w-2.5 h-2.5" />
Skill
</span>
)}
</div>
</div>
<h3 className="text-lg font-bold text-white mb-1">{op.name}</h3>
<p className="text-sm text-gray-500 mb-6 h-10 line-clamp-2">{op.description}</p>
{/* Contract / Ports Display */}
<div className="space-y-3 mb-6">
<div className="flex items-center gap-2 text-[10px] text-gray-600 uppercase font-mono tracking-widest">
<Layers className="w-3 h-3" />
契约定义 (Ports)
</div>
<div className="flex flex-wrap gap-2">
{op.inputs?.map(port => (
<span key={port.id} className="px-2 py-1 bg-blue-500/5 border border-blue-500/20 rounded text-[10px] text-blue-400">
IN: {port.name} ({port.type})
</span>
))}
{op.outputs?.map(port => (
<span key={port.id} className="px-2 py-1 bg-green-500/5 border border-green-500/20 rounded text-[10px] text-green-400">
OUT: {port.name} ({port.type})
</span>
))}
{(!op.inputs?.length && !op.outputs?.length) && <span className="text-[10px] text-gray-700 italic">无端口定义</span>}
</div>
</div>
{/* Hyperparams Summary */}
{op.hyperparams && op.hyperparams.length > 0 && (
<div className="space-y-3 mb-6">
<div className="flex items-center gap-2 text-[10px] text-gray-600 uppercase font-mono tracking-widest">
<Cpu className="w-3 h-3" />
超参配置 (Hyperparams)
</div>
<div className="flex flex-wrap gap-2">
{op.hyperparams.map(hp => (
<span key={hp.key} className="px-2 py-1 bg-orange-500/5 border border-orange-500/20 rounded text-[10px] text-orange-400 font-mono">
{hp.key}={hp.value}
</span>
))}
</div>
</div>
)}
<div className="flex items-center justify-between pt-4 border-t border-[#2a2b2f]">
<span className="text-[10px] text-gray-600 font-mono uppercase tracking-widest">{op.category}</span>
<button
onClick={() => handleEdit(op)}
className="text-xs text-blue-500 hover:underline flex items-center gap-1"
>
{canManage ? "配置详情" : "查看详情"}
<Info className="w-3 h-3" />
</button>
</div>
</div>
))}
{filteredOperators.length === 0 && (
<div className="col-span-full bg-[#151619] border border-[#2a2b2f] rounded-xl p-12 text-center text-gray-500">
{operators.length === 0 ? "当前还没有算子,请先注册或同步内置算子。" : "没有匹配的算子,请调整搜索条件。"}
</div>
)}
</div>
{/* Registration/Edit Modal */}
{isModalOpen && (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-2xl max-h-[90vh] overflow-hidden flex flex-col shadow-2xl">
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between">
<h2 className="text-xl font-bold text-white">{editingOpId ? "编辑算子" : "注册新算子"}</h2>
<button onClick={() => setIsModalOpen(false)} className="text-gray-500 hover:text-white">
<X className="w-6 h-6" />
</button>
</div>
{/* Tabs */}
<div className="flex px-6 border-b border-[#2a2b2f]">
<button
onClick={() => setActiveTab("basic")}
className={cn(
"px-4 py-3 text-xs font-bold uppercase tracking-widest transition-all border-b-2",
activeTab === "basic" ? "text-blue-500 border-blue-500" : "text-gray-500 border-transparent hover:text-gray-300"
)}
>
基础配置
</button>
<button
onClick={() => setActiveTab("ports")}
className={cn(
"px-4 py-3 text-xs font-bold uppercase tracking-widest transition-all border-b-2",
activeTab === "ports" ? "text-blue-500 border-blue-500" : "text-gray-500 border-transparent hover:text-gray-300"
)}
>
端口定义
</button>
<button
onClick={() => setActiveTab("hyperparams")}
className={cn(
"px-4 py-3 text-xs font-bold uppercase tracking-widest transition-all border-b-2",
activeTab === "hyperparams" ? "text-blue-500 border-blue-500" : "text-gray-500 border-transparent hover:text-gray-300"
)}
>
超参配置
</button>
<button
onClick={() => setActiveTab("skill")}
className={cn(
"px-4 py-3 text-xs font-bold uppercase tracking-widest transition-all border-b-2",
activeTab === "skill" ? "text-blue-500 border-blue-500" : "text-gray-500 border-transparent hover:text-gray-300"
)}
>
AI Skill 描述
</button>
</div>
<div className="p-6 overflow-y-auto flex-1 space-y-6">
{activeTab === "basic" && (
<div className="space-y-6">
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">算子名称</label>
<input
type="text"
placeholder="例如: 图像锐化"
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
value={newOp.name || ""}
onChange={e => setNewOp({...newOp, name: e.target.value})}
/>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">分类</label>
<select
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
value={newOp.category || "CV"}
onChange={e => setNewOp({...newOp, category: e.target.value})}
>
<option value="CV">计算机视觉 (CV)</option>
<option value="AI">人工智能 (AI)</option>
<option value="IO">输入输出 (IO)</option>
<option value="MATH">数学运算 (MATH)</option>
</select>
</div>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">描述</label>
<textarea
placeholder="描述该算子的功能与用途..."
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm h-20 focus:outline-none focus:border-blue-500"
value={newOp.description || ""}
onChange={e => setNewOp({...newOp, description: e.target.value})}
/>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">Docker 镜像地址</label>
<input
type="text"
placeholder="registry.flowai.io/operators/sharpen:v1.0"
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
value={newOp.image || ""}
onChange={e => setNewOp({...newOp, image: e.target.value})}
/>
</div>
</div>
)}
{activeTab === "ports" && (
<div className="grid grid-cols-2 gap-6">
<div className="space-y-3">
<div className="flex items-center justify-between">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">输入端口 (Inputs)</label>
<button onClick={() => handleAddPort('inputs')} className="text-blue-500 hover:text-blue-400 text-xs flex items-center gap-1">
<Plus className="w-3 h-3" /> 添加
</button>
</div>
<div className="space-y-2">
{newOp.inputs?.map((port, idx) => (
<div key={port.id} className="flex gap-2">
<input
type="text"
placeholder="名称"
className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded px-2 py-1 text-xs"
value={port.name}
onChange={e => {
const inputs = [...(newOp.inputs || [])];
inputs[idx].name = e.target.value;
setNewOp({...newOp, inputs});
}}
/>
<select
className="bg-[#0a0a0a] border border-[#2a2b2f] rounded px-2 py-1 text-xs"
value={port.type}
onChange={e => {
const inputs = [...(newOp.inputs || [])];
const type = e.target.value as any;
inputs[idx].type = type;
// Set default transmission based on type
if (type === "video" || type === "file") inputs[idx].transmission = "oss";
else if (type === "tensor") inputs[idx].transmission = "stream";
else inputs[idx].transmission = "bus";
setNewOp({...newOp, inputs});
}}
>
<option value="json">JSON</option>
<option value="image">Image</option>
<option value="video">Video</option>
<option value="tensor">Tensor</option>
<option value="file">File</option>
</select>
<select
className="bg-[#0a0a0a] border border-[#2a2b2f] rounded px-2 py-1 text-[10px] text-blue-400"
value={port.transmission}
onChange={e => {
const inputs = [...(newOp.inputs || [])];
inputs[idx].transmission = e.target.value as any;
setNewOp({...newOp, inputs});
}}
>
<option value="bus">Bus</option>
<option value="oss">OSS</option>
<option value="shm">SHM</option>
<option value="stream">Stream</option>
</select>
<button
onClick={() => {
const inputs = newOp.inputs?.filter((_, i) => i !== idx);
setNewOp({...newOp, inputs});
}}
className="text-gray-600 hover:text-red-500"
>
<Trash2 className="w-3 h-3" />
</button>
</div>
))}
</div>
</div>
<div className="space-y-3">
<div className="flex items-center justify-between">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">输出端口 (Outputs)</label>
<button onClick={() => handleAddPort('outputs')} className="text-green-500 hover:text-green-400 text-xs flex items-center gap-1">
<Plus className="w-3 h-3" /> 添加
</button>
</div>
<div className="space-y-2">
{newOp.outputs?.map((port, idx) => (
<div key={port.id} className="flex gap-2">
<input
type="text"
placeholder="名称"
className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded px-2 py-1 text-xs"
value={port.name}
onChange={e => {
const outputs = [...(newOp.outputs || [])];
outputs[idx].name = e.target.value;
setNewOp({...newOp, outputs});
}}
/>
<select
className="bg-[#0a0a0a] border border-[#2a2b2f] rounded px-2 py-1 text-xs"
value={port.type}
onChange={e => {
const outputs = [...(newOp.outputs || [])];
const type = e.target.value as any;
outputs[idx].type = type;
// Set default transmission based on type
if (type === "video" || type === "file") outputs[idx].transmission = "oss";
else if (type === "tensor") outputs[idx].transmission = "stream";
else outputs[idx].transmission = "bus";
setNewOp({...newOp, outputs});
}}
>
<option value="json">JSON</option>
<option value="image">Image</option>
<option value="video">Video</option>
<option value="tensor">Tensor</option>
<option value="file">File</option>
</select>
<select
className="bg-[#0a0a0a] border border-[#2a2b2f] rounded px-2 py-1 text-[10px] text-blue-400"
value={port.transmission}
onChange={e => {
const outputs = [...(newOp.outputs || [])];
outputs[idx].transmission = e.target.value as any;
setNewOp({...newOp, outputs});
}}
>
<option value="bus">Bus</option>
<option value="oss">OSS</option>
<option value="shm">SHM</option>
<option value="stream">Stream</option>
</select>
<button
onClick={() => {
const outputs = newOp.outputs?.filter((_, i) => i !== idx);
setNewOp({...newOp, outputs});
}}
className="text-gray-600 hover:text-red-500"
>
<Trash2 className="w-3 h-3" />
</button>
</div>
))}
</div>
</div>
</div>
)}
{activeTab === "hyperparams" && (
<div className="space-y-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2 text-blue-500">
<Cpu className="w-4 h-4" />
<h3 className="text-sm font-bold uppercase tracking-wider">超参定义 (K8s Env Vars)</h3>
</div>
<button
onClick={handleAddHyperParam}
className="text-blue-500 hover:text-blue-400 text-xs flex items-center gap-1"
>
<Plus className="w-3 h-3" /> 添加参数
</button>
</div>
<div className="p-4 bg-orange-500/5 border border-orange-500/20 rounded-lg flex items-start gap-3 mb-4">
<Info className="w-4 h-4 text-orange-500 mt-0.5" />
<p className="text-[10px] text-gray-500 leading-relaxed">
超参将作为环境变量注入到算子的 K8s Job 中。
例如:设置 <code>CONFIDENCE_THRESHOLD</code> 为 <code>0.5</code>,
算子代码中可以通过 <code>os.environ.get('CONFIDENCE_THRESHOLD')</code> 获取。
</p>
</div>
<div className="space-y-3">
{newOp.hyperparams?.map((hp, idx) => (
<div key={idx} className="bg-[#0a0a0a] border border-[#2a2b2f] rounded-lg p-4 space-y-3 relative group">
<button
onClick={() => {
const hyperparams = newOp.hyperparams?.filter((_, i) => i !== idx);
setNewOp({...newOp, hyperparams});
}}
className="absolute top-2 right-2 text-gray-600 hover:text-red-500 opacity-0 group-hover:opacity-100 transition-opacity"
>
<Trash2 className="w-4 h-4" />
</button>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1">
<label className="text-[10px] text-gray-500 uppercase font-mono">变量名 (Key)</label>
<input
type="text"
className="w-full bg-[#151619] border border-[#2a2b2f] rounded px-3 py-1.5 text-xs focus:outline-none focus:border-blue-500 font-mono"
value={hp.key}
onChange={e => {
const hyperparams = [...(newOp.hyperparams || [])];
hyperparams[idx].key = e.target.value.toUpperCase().replace(/\s+/g, '_');
setNewOp({...newOp, hyperparams});
}}
/>
</div>
<div className="space-y-1">
<label className="text-[10px] text-gray-500 uppercase font-mono">默认值 (Default Value)</label>
<input
type="text"
className="w-full bg-[#151619] border border-[#2a2b2f] rounded px-3 py-1.5 text-xs focus:outline-none focus:border-blue-500"
value={hp.value}
onChange={e => {
const hyperparams = [...(newOp.hyperparams || [])];
hyperparams[idx].value = e.target.value;
setNewOp({...newOp, hyperparams});
}}
/>
</div>
</div>
<div className="space-y-1">
<label className="text-[10px] text-gray-500 uppercase font-mono">描述 (Description)</label>
<input
type="text"
placeholder="说明该参数的作用..."
className="w-full bg-[#151619] border border-[#2a2b2f] rounded px-3 py-1.5 text-xs focus:outline-none focus:border-blue-500"
value={hp.description}
onChange={e => {
const hyperparams = [...(newOp.hyperparams || [])];
hyperparams[idx].description = e.target.value;
setNewOp({...newOp, hyperparams});
}}
/>
</div>
</div>
))}
{(!newOp.hyperparams || newOp.hyperparams.length === 0) && (
<div className="text-center py-10 border-2 border-dashed border-[#2a2b2f] rounded-xl text-gray-600 text-xs">
暂无超参配置,点击上方“添加参数”开始
</div>
)}
</div>
</div>
)}
{activeTab === "skill" && (
<div className="space-y-4">
<div className="p-4 bg-blue-500/5 border border-blue-500/20 rounded-lg flex items-start gap-3">
<Info className="w-4 h-4 text-blue-500 mt-0.5" />
<p className="text-[10px] text-gray-500 leading-relaxed">
AI Skill 描述用于向 AI 助手描述该算子的功能、输入输出要求以及使用场景。
良好的描述能帮助 AI 更准确地在自动化工作流生成中使用该算子。
</p>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">Skill 定义 (Markdown)</label>
<textarea
placeholder="### 算子名称\n描述算子的核心功能...\n\n#### 使用场景\n在什么情况下应该使用该算子...\n\n#### 限制条件\n输入数据的特殊要求..."
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm h-64 font-mono focus:outline-none focus:border-blue-500"
value={newOp.skillDefinition || ""}
onChange={e => setNewOp({...newOp, skillDefinition: e.target.value})}
/>
</div>
</div>
)}
</div>
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3">
<button
onClick={() => setIsModalOpen(false)}
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
>
取消
</button>
<button
onClick={handleRegister}
className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-colors"
>
{editingOpId ? "保存修改" : "确认注册"}
</button>
</div>
</div>
</div>
)}
{/* AI Generate Modal */}
{isAIModalOpen && (
<div className="fixed inset-0 z-[110] flex items-center justify-center p-4 bg-black/80 backdrop-blur-md">
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-lg shadow-2xl overflow-hidden">
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between bg-gradient-to-r from-blue-600/10 to-purple-600/10">
<div className="flex items-center gap-2">
<Sparkles className="w-5 h-5 text-blue-500" />
<h2 className="text-xl font-bold text-white">AI 辅助注册算子</h2>
</div>
<button onClick={() => setIsAIModalOpen(false)} className="text-gray-500 hover:text-white transition-colors">
<X className="w-6 h-6" />
</button>
</div>
<div className="p-6 space-y-6">
<div className="space-y-3">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">描述您的算子</label>
<textarea
placeholder="例如:帮我注册一个算子,用于对输入的视频进行锐化处理。它应该接受一个视频输入,输出一个处理后的视频。它属于 CV 类别。"
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-xl px-4 py-3 text-sm h-40 focus:outline-none focus:border-blue-500 transition-all resize-none leading-relaxed"
value={aiDescription}
onChange={e => setAiDescription(e.target.value)}
disabled={isGenerating}
/>
<p className="text-[10px] text-gray-500 italic">
提示:AI 会自动为您生成算子名称、描述、端口定义(包含传输协议)以及详细的 Skill 自述文件。
</p>
</div>
{aiError && (
<div className="p-3 bg-red-500/10 border border-red-500/20 rounded-lg flex items-center gap-3 text-red-500 text-xs">
<AlertCircle className="w-4 h-4 shrink-0" />
{aiError}
</div>
)}
{!aiStatus.enabled && (
<div className="p-3 bg-yellow-500/10 border border-yellow-500/20 rounded-lg flex items-center gap-3 text-yellow-300 text-xs">
<AlertCircle className="w-4 h-4 shrink-0" />
{aiStatus.reason || "当前服务端未配置 AI 服务,无法生成算子。"}
</div>
)}
{aiStatus.enabled && (
<div className="p-3 bg-blue-500/10 border border-blue-500/20 rounded-lg flex items-center gap-3 text-blue-200 text-xs">
<Sparkles className="w-4 h-4 shrink-0" />
当前由服务端模型 `{aiStatus.model || "glm-5"}` 生成算子定义,API Key 不会暴露到浏览器。
</div>
)}
</div>
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3 bg-[#0a0a0a]/50">
<button
onClick={() => setIsAIModalOpen(false)}
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
disabled={isGenerating}
>
取消
</button>
<button
onClick={handleAIGenerate}
disabled={!aiStatus.enabled || !aiDescription.trim() || isGenerating}
className={cn(
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all flex items-center gap-2",
(!aiStatus.enabled || !aiDescription.trim() || isGenerating) && "opacity-50 cursor-not-allowed"
)}
>
{isGenerating ? (
<>
<Loader2 className="w-4 h-4 animate-spin" />
正在生成中...
</>
) : (
<>
<Sparkles className="w-4 h-4" />
开始生成
</>
)}
</button>
</div>
</div>
</div>
)}
</div>
);
}