// ============================================================ // Agent 模板管理 // ============================================================ import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { Button, message, Tag, Modal, Form, Input, Select, InputNumber, Space, Popconfirm, Descriptions } from 'antd' import { PlusOutlined, MinusCircleOutlined } from '@ant-design/icons' import type { ProColumns } from '@ant-design/pro-components' import { ProTable } from '@ant-design/pro-components' import { agentTemplateService } from '@/services/agent-templates' import type { AgentTemplate } from '@/types' const { TextArea } = Input const sourceLabels: Record = { builtin: '内置', custom: '自定义' } const visibilityLabels: Record = { public: '公开', team: '团队', private: '私有' } const statusLabels: Record = { active: '活跃', archived: '已归档' } const statusColors: Record = { active: 'green', archived: 'default' } export default function AgentTemplates() { const queryClient = useQueryClient() const [form] = Form.useForm() const [modalOpen, setModalOpen] = useState(false) const [detailRecord, setDetailRecord] = useState(null) const { data, isLoading } = useQuery({ queryKey: ['agent-templates'], queryFn: ({ signal }) => agentTemplateService.list(signal), }) const createMutation = useMutation({ mutationFn: (data: Parameters[0]) => agentTemplateService.create(data), onSuccess: () => { message.success('创建成功') queryClient.invalidateQueries({ queryKey: ['agent-templates'] }) setModalOpen(false) form.resetFields() }, onError: (err: Error) => message.error(err.message || '创建失败'), }) const archiveMutation = useMutation({ mutationFn: (id: string) => agentTemplateService.archive(id), onSuccess: () => { message.success('已归档') queryClient.invalidateQueries({ queryKey: ['agent-templates'] }) }, onError: (err: Error) => message.error(err.message || '归档失败'), }) const columns: ProColumns[] = [ { title: '图标', dataIndex: 'emoji', width: 60 }, { title: '名称', dataIndex: 'name', width: 160 }, { title: '分类', dataIndex: 'category', width: 100 }, { title: '模型', dataIndex: 'model', width: 140, render: (_, r) => r.model || '-' }, { title: '来源', dataIndex: 'source', width: 80, render: (_, r) => {sourceLabels[r.source] || r.source}, }, { title: '可见性', dataIndex: 'visibility', width: 80, render: (_, r) => {visibilityLabels[r.visibility] || r.visibility}, }, { title: '状态', dataIndex: 'status', width: 80, render: (_, r) => {statusLabels[r.status] || r.status}, }, { title: '版本', dataIndex: 'current_version', width: 70 }, { title: '操作', width: 180, render: (_, record) => ( {record.status === 'active' && ( archiveMutation.mutate(record.id)}> )} ), }, ] const handleCreate = async () => { const values = await form.validateFields() createMutation.mutate(values) } return (
columns={columns} dataSource={data?.items ?? []} loading={isLoading} rowKey="id" search={false} toolBarRender={() => [ , ]} pagination={{ total: data?.total ?? 0, pageSize: data?.page_size ?? 20, current: data?.page ?? 1, showSizeChanger: false, }} /> { setModalOpen(false); form.resetFields() }} confirmLoading={createMutation.isPending} width={640} >