Files
DataClaw/frontend/src/pages/Skills.tsx
T

358 lines
16 KiB
TypeScript
Raw Normal View History

2026-03-14 15:52:27 +08:00
import { useState, useEffect } from 'react';
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";
2026-03-16 17:26:02 +08:00
import { Trash2, Edit2, Plus, Terminal, Loader2, FolderOpen, Share2, Download, Eye, ShieldCheck, AlertCircle, Wand2, Upload } from "lucide-react";
2026-03-14 15:52:27 +08:00
import { ScrollArea } from "@/components/ui/scroll-area";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogFooter } from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
2026-03-16 17:26:02 +08:00
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
2026-03-14 15:52:27 +08:00
import { api } from "@/lib/api";
2026-03-16 16:12:35 +08:00
import { useProjectStore } from "@/store/projectStore";
2026-03-16 17:26:02 +08:00
import { useRef } from 'react';
2026-03-14 15:52:27 +08:00
interface Skill {
id: string;
name: string;
description: string;
content: string;
2026-03-16 17:26:02 +08:00
type: string;
2026-03-16 16:12:35 +08:00
project_id?: number;
2026-03-16 17:26:02 +08:00
source: string;
installation_time: string;
status: string;
file_path?: string;
2026-03-14 15:52:27 +08:00
}
export function Skills() {
const [skills, setSkills] = useState<Skill[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isDialogOpen, setIsDialogOpen] = useState(false);
2026-03-16 16:12:35 +08:00
const [editingSkill, setEditingSkill] = useState<Skill | null>(null);
2026-03-16 17:26:02 +08:00
const [newSkill, setNewSkill] = useState<Partial<Skill>>({ type: 'python', content: '', source: '本地导入', status: '安全' });
2026-03-16 16:12:35 +08:00
const { currentProject } = useProjectStore();
2026-03-16 17:26:02 +08:00
const fileInputRef = useRef<HTMLInputElement>(null);
2026-03-14 15:52:27 +08:00
useEffect(() => {
2026-03-16 16:12:35 +08:00
if (currentProject) {
fetchSkills();
}
}, [currentProject]);
2026-03-14 15:52:27 +08:00
const fetchSkills = async () => {
2026-03-16 16:12:35 +08:00
if (!currentProject) return;
2026-03-14 15:52:27 +08:00
setIsLoading(true);
try {
2026-03-16 16:12:35 +08:00
const data = await api.get<Skill[]>(`/api/v1/skills?project_id=${currentProject.id}`);
2026-03-14 15:52:27 +08:00
setSkills(data);
} catch (error) {
console.error("Failed to fetch skills", error);
} finally {
setIsLoading(false);
}
};
2026-03-16 17:26:02 +08:00
const handleFileUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file || !currentProject) return;
const formData = new FormData();
formData.append('file', file);
formData.append('project_id', currentProject.id.toString());
setIsLoading(true);
try {
await api.post('/api/v1/skills/upload', formData);
await fetchSkills();
} catch (error: any) {
console.error("Failed to upload skill", error);
const errorMessage = error.response?.data?.detail || error.message || "未知错误";
alert("上传失败: " + errorMessage);
} finally {
setIsLoading(false);
if (fileInputRef.current) fileInputRef.current.value = '';
}
};
2026-03-14 15:52:27 +08:00
const handleAddSkill = async () => {
2026-03-16 16:12:35 +08:00
if (!currentProject) return;
2026-03-14 15:52:27 +08:00
if (newSkill.name && newSkill.description && newSkill.content) {
try {
2026-03-16 16:12:35 +08:00
if (editingSkill) {
2026-03-16 17:26:02 +08:00
await api.put<Skill>(`/api/v1/skills/${editingSkill.id}?project_id=${currentProject.id}`, {
2026-03-16 16:12:35 +08:00
...newSkill,
project_id: currentProject.id
});
} else {
const skillToCreate = {
...newSkill,
id: Date.now().toString(),
project_id: currentProject.id
};
2026-03-16 17:26:02 +08:00
await api.post<Skill>('/api/v1/skills', skillToCreate);
2026-03-16 16:12:35 +08:00
}
2026-03-16 17:26:02 +08:00
await fetchSkills();
setNewSkill({ type: 'python', content: '', source: '本地导入', status: '安全' });
2026-03-16 16:12:35 +08:00
setEditingSkill(null);
2026-03-14 15:52:27 +08:00
setIsDialogOpen(false);
} catch (error) {
2026-03-16 16:12:35 +08:00
console.error("Failed to save skill", error);
2026-03-14 15:52:27 +08:00
}
}
};
2026-03-16 16:12:35 +08:00
const handleEditSkill = (skill: Skill) => {
setEditingSkill(skill);
setNewSkill(skill);
setIsDialogOpen(true);
};
2026-03-14 15:52:27 +08:00
const handleDeleteSkill = async (id: string) => {
2026-03-16 16:12:35 +08:00
if (!currentProject) return;
if (!window.confirm("确定要删除这个技能吗?")) return;
2026-03-14 15:52:27 +08:00
try {
2026-03-16 16:12:35 +08:00
await api.delete(`/api/v1/skills/${id}?project_id=${currentProject.id}`);
2026-03-14 15:52:27 +08:00
setSkills(skills.filter(s => s.id !== id));
} catch (error) {
console.error("Failed to delete skill", error);
}
};
2026-03-16 16:12:35 +08:00
if (!currentProject) {
return (
<div className="h-full flex flex-col items-center justify-center text-zinc-500 gap-4">
<FolderOpen className="h-12 w-12 text-zinc-200" />
<p></p>
</div>
);
}
2026-03-14 15:52:27 +08:00
return (
2026-03-16 17:26:02 +08:00
<div className="h-full flex flex-col bg-white overflow-hidden">
<div className="border-b border-zinc-100 px-8 py-5 flex items-center justify-between bg-white shrink-0">
2026-03-14 15:52:27 +08:00
<div>
2026-03-16 17:26:02 +08:00
<h1 className="text-2xl font-bold text-zinc-900 flex items-center gap-2">
< Wand2 className="h-6 w-6 text-indigo-500" />
Skills - {currentProject.name}
</h1>
<p className="text-sm text-zinc-500 mt-1"> AI agentskills.io </p>
2026-03-14 15:52:27 +08:00
</div>
2026-03-16 17:26:02 +08:00
<div className="flex gap-3">
<input
type="file"
ref={fileInputRef}
onChange={handleFileUpload}
className="hidden"
accept=".md,.zip,.tar.gz,.tgz"
/>
<Button
className="bg-indigo-600 hover:bg-indigo-700 text-white gap-2"
onClick={() => fileInputRef.current?.click()}
>
<Upload className="h-4 w-4" />
Skill
</Button>
</div>
</div>
<div className="flex-1 overflow-auto p-4 md:p-8 bg-zinc-50/30">
<div className="bg-white rounded-xl border border-zinc-200 shadow-sm overflow-hidden min-w-[800px] lg:min-w-0">
<Table className="table-fixed w-full">
<TableHeader className="bg-zinc-50/50">
<TableRow className="hover:bg-transparent">
<TableHead className="w-[40%] font-semibold text-zinc-700 py-3 px-4 text-sm"></TableHead>
<TableHead className="w-[15%] font-semibold text-zinc-700 py-3 px-4 text-sm"></TableHead>
<TableHead className="w-[15%] font-semibold text-zinc-700 py-3 px-4 text-sm text-center"></TableHead>
<TableHead className="w-[15%] font-semibold text-zinc-700 py-3 px-4 text-sm text-center"></TableHead>
<TableHead className="w-[15%] font-semibold text-zinc-700 py-3 px-4 text-sm text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{isLoading ? (
<TableRow>
<TableCell colSpan={5} className="py-24 text-center">
<div className="flex items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-indigo-500" />
</div>
</TableCell>
</TableRow>
) : (
<>
{skills.map((skill) => (
<TableRow key={skill.id} className="group hover:bg-zinc-50/50 transition-colors border-zinc-100">
<TableCell className="py-4 px-4 overflow-hidden">
<div className="flex items-start gap-3 min-w-0">
<div className="p-2 bg-indigo-50 rounded-lg text-indigo-600 mt-0.5 shrink-0">
<Terminal className="h-4 w-4" />
</div>
<div className="flex-1 min-w-0 space-y-1">
<div className="flex items-center gap-2">
<h3 className="font-bold text-zinc-900 text-sm md:text-base truncate flex-1" title={skill.name}>{skill.name}</h3>
{skill.type === 'agentskill' && (
<span className="px-1.5 py-0.5 bg-indigo-100 text-indigo-700 text-[10px] font-bold rounded uppercase tracking-wider shrink-0">
Agent
</span>
)}
</div>
<p
className="text-zinc-500 text-xs leading-relaxed truncate cursor-help"
title={skill.description}
>
{skill.description}
</p>
</div>
</div>
</TableCell>
<TableCell className="py-4 px-4 text-zinc-600 text-sm">
<div className="truncate" title={skill.source}>{skill.source}</div>
</TableCell>
<TableCell className="py-4 px-4 text-zinc-400 text-center text-xs">
<div className="truncate">{skill.installation_time}</div>
</TableCell>
<TableCell className="py-4 px-4 text-center">
<div className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] md:text-xs font-medium whitespace-nowrap ${
skill.status === '安全'
? 'bg-green-50 text-green-700 border border-green-100'
: 'bg-amber-50 text-amber-700 border border-amber-100'
}`}>
{skill.status === '安全' ? (
<ShieldCheck className="h-3 w-3" />
) : (
<AlertCircle className="h-3 w-3" />
)}
{skill.status}
</div>
</TableCell>
<TableCell className="py-4 px-4 text-right">
<div className="flex justify-end gap-1">
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-zinc-400 hover:text-indigo-600 hover:bg-indigo-50 rounded-md transition-all"
onClick={() => handleEditSkill(skill)}
>
<Eye className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-zinc-400 hover:text-rose-600 hover:bg-rose-50 rounded-md transition-all"
onClick={() => handleDeleteSkill(skill.id)}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</TableCell>
</TableRow>
))}
{skills.length === 0 && (
<TableRow>
<TableCell colSpan={5} className="py-24 text-center">
<div className="flex flex-col items-center gap-3 text-zinc-400">
<div className="p-4 bg-zinc-50 rounded-2xl">
<Terminal className="h-10 w-10 opacity-20" />
</div>
<p className="text-sm"> Skill</p>
</div>
</TableCell>
</TableRow>
)}
</>
)}
</TableBody>
</Table>
</div>
</div>
<Dialog open={isDialogOpen} onOpenChange={(open) => {
setIsDialogOpen(open);
if (!open) {
setEditingSkill(null);
setNewSkill({ type: 'python', content: '', source: '本地导入', status: '安全' });
}
}}>
<DialogContent className="sm:max-w-[600px] max-h-[90vh] flex flex-col rounded-2xl p-0 overflow-hidden">
<DialogHeader className="p-6 pb-2">
<DialogTitle className="text-xl font-bold text-zinc-900">{editingSkill ? '编辑技能' : '添加新技能'}</DialogTitle>
</DialogHeader>
<div className="flex-1 overflow-y-auto px-6 py-2">
<div className="grid gap-5">
<div className="grid gap-1.5">
<Label htmlFor="name" className="text-zinc-600 font-medium text-sm"></Label>
2026-03-14 15:52:27 +08:00
<Input
id="name"
2026-03-16 17:26:02 +08:00
placeholder="技能名称"
2026-03-14 15:52:27 +08:00
value={newSkill.name || ''}
onChange={(e) => setNewSkill({...newSkill, name: e.target.value})}
2026-03-16 17:26:02 +08:00
className="rounded-lg border-zinc-200 h-10"
2026-03-14 15:52:27 +08:00
/>
</div>
2026-03-16 17:26:02 +08:00
<div className="grid grid-cols-2 gap-4">
<div className="grid gap-1.5">
<Label htmlFor="type" className="text-zinc-600 font-medium text-sm"></Label>
<Select
value={newSkill.type}
onValueChange={(val: any) => setNewSkill({...newSkill, type: val})}
>
<SelectTrigger className="rounded-lg border-zinc-200 h-10">
<SelectValue placeholder="选择类型" />
</SelectTrigger>
<SelectContent className="rounded-lg">
<SelectItem value="python">Python</SelectItem>
<SelectItem value="sql">SQL</SelectItem>
<SelectItem value="api">API</SelectItem>
</SelectContent>
</Select>
</div>
<div className="grid gap-1.5">
<Label htmlFor="status" className="text-zinc-600 font-medium text-sm"></Label>
<Select
value={newSkill.status}
onValueChange={(val: any) => setNewSkill({...newSkill, status: val})}
>
<SelectTrigger className="rounded-lg border-zinc-200 h-10">
<SelectValue placeholder="选择状态" />
</SelectTrigger>
<SelectContent className="rounded-lg">
<SelectItem value="安全"></SelectItem>
<SelectItem value="低风险"></SelectItem>
</SelectContent>
</Select>
</div>
2026-03-14 15:52:27 +08:00
</div>
2026-03-16 17:26:02 +08:00
<div className="grid gap-1.5">
<Label htmlFor="description" className="text-zinc-600 font-medium text-sm"></Label>
2026-03-14 15:52:27 +08:00
<Textarea
id="description"
2026-03-16 17:26:02 +08:00
placeholder="简要描述技能的功能..."
2026-03-14 15:52:27 +08:00
value={newSkill.description || ''}
onChange={(e) => setNewSkill({...newSkill, description: e.target.value})}
2026-03-16 17:26:02 +08:00
className="rounded-lg border-zinc-200 min-h-[80px] py-2 text-sm"
2026-03-14 15:52:27 +08:00
/>
</div>
2026-03-16 17:26:02 +08:00
<div className="grid gap-1.5">
<Label htmlFor="content" className="text-zinc-600 font-medium text-sm"></Label>
2026-03-14 15:52:27 +08:00
<Textarea
id="content"
value={newSkill.content || ''}
onChange={(e) => setNewSkill({...newSkill, content: e.target.value})}
2026-03-16 17:26:02 +08:00
className="rounded-lg border-zinc-200 font-mono text-xs min-h-[160px] py-3 bg-zinc-50"
2026-03-16 16:12:35 +08:00
placeholder="Python 代码、SQL 查询模板或 API 规范..."
2026-03-14 15:52:27 +08:00
/>
</div>
</div>
2026-03-16 17:26:02 +08:00
</div>
<DialogFooter className="p-6 pt-2">
<Button onClick={handleAddSkill} className="bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg px-6 h-10 w-full">
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
2026-03-14 15:52:27 +08:00
</div>
);
}
2026-03-16 17:26:02 +08:00