"use client"; import { useState, useEffect } from "react"; import { Plus, Copy, Check, List } from "lucide-react"; import { useRouter } from "next/navigation"; import DashboardLayout from "@/components/layout/dashboard-layout"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useToast } from "@/components/ui/use-toast"; import { api } from "@/lib/api"; export interface APIKey { id: number; name: string; key: string; is_active: boolean; last_used_at: string | null; created_at: string; updated_at: string; } export interface APIKeyCreate { name: string; is_active?: boolean; } export interface APIKeyUpdate { name?: string; is_active?: boolean; } export default function APIKeysPage() { const [apiKeys, setApiKeys] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isCreating, setIsCreating] = useState(false); const [newKeyName, setNewKeyName] = useState(""); const [isDialogOpen, setIsDialogOpen] = useState(false); const [isAPIListDialogOpen, setIsAPIListDialogOpen] = useState(false); const [copiedId, setCopiedId] = useState(null); const { toast } = useToast(); const router = useRouter(); // 获取 API Keys 列表 const fetchAPIKeys = async () => { try { const data = await api.get("/api/api-keys"); setApiKeys(data); } catch (error) { toast({ title: "错误", description: "获取 API 密钥失败", variant: "destructive", }); } finally { setIsLoading(false); } }; useEffect(() => { fetchAPIKeys(); }, []); // 创建新的 API Key const createAPIKey = async () => { if (!newKeyName.trim()) { toast({ title: "错误", description: "请输入 API 密钥名称", variant: "destructive", }); return; } setIsCreating(true); try { const data = await api.post("/api/api-keys", { name: newKeyName, is_active: true, }); setApiKeys([...apiKeys, data]); setNewKeyName(""); setIsDialogOpen(false); toast({ title: "成功", description: "API 密钥创建成功", }); } catch (error) { toast({ title: "错误", description: "创建 API 密钥失败", variant: "destructive", }); } finally { setIsCreating(false); } }; // 删除 API Key const deleteAPIKey = async (id: number) => { try { const response = await api.delete(`/api/api-keys/${id}`); if (!response.ok) throw new Error("删除 API 密钥失败"); setApiKeys(apiKeys.filter((key) => key.id !== id)); toast({ title: "成功", description: "API 密钥删除成功", }); } catch (error) { toast({ title: "错误", description: "删除 API 密钥失败", variant: "destructive", }); } }; // 更新 API Key 状态 const toggleAPIKeyStatus = async (id: number, currentStatus: boolean) => { try { const response = await api.put(`/api/api-keys/${id}`, { is_active: !currentStatus, }); setApiKeys( apiKeys.map((key) => key.id === id ? { ...key, is_active: !currentStatus } : key ) ); toast({ title: "成功", description: "API 密钥状态更新成功", }); } catch (error) { toast({ title: "错误", description: "更新 API 密钥失败", variant: "destructive", }); } }; // 复制 API Key const copyAPIKey = async (id: number, key: string) => { try { await navigator.clipboard.writeText(key); setCopiedId(id); setTimeout(() => { setCopiedId(null); }, 3000); toast({ title: "成功", description: "API 密钥已复制到剪贴板", }); } catch (error) { toast({ title: "错误", description: "复制 API 密钥失败", variant: "destructive", }); } }; return (

API 密钥

可用 API 端点 查看可用 API 端点及使用方式。

知识库查询

请求方法

GET

接口路径

/openapi/knowledge/{"{id}"}/query

查询参数

query
查询语句
top_k
返回结果数量(可选,默认 3)

请求头

X-API-Key
你的 API 密钥
创建新 API 密钥 创建新的 API 密钥,用于程序化访问接口。
setNewKeyName(e.target.value)} placeholder="请输入 API 密钥名称" />
名称 API 密钥 状态 创建时间 最近使用 操作 {apiKeys.map((apiKey) => ( {apiKey.name} {apiKey.key} toggleAPIKeyStatus(apiKey.id, apiKey.is_active) } /> {new Date(apiKey.created_at).toLocaleDateString("zh-CN")} {apiKey.last_used_at ? new Date(apiKey.last_used_at).toLocaleDateString("zh-CN") : "从未"} ))}
); }