/** * 插件 API 文档组件 * * 展示 HTTP API 和 MCP 工具文档。 * 使用 console 语义 token 保持视觉一致。 */ import { useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; import { Skeleton } from '@components/ui/skeleton'; import { Cpu, ArrowLeftRight, Globe } from 'lucide-react'; import { Badge } from '@components/ui/badge'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@components/ui/tabs'; import { StatePanel } from '@components/console'; interface HttpApiEndpoint { path: string; methods: string[]; name: string; description?: string; } interface MCPTool { name: string; full_name: string; title: string; description: string; input: { type: string; properties: Record; required: string[]; }; output: { type: string; properties: Record; required: string[]; }; } interface PluginDocs { plugin_name: string; http_api: HttpApiEndpoint[]; mcp_tools: MCPTool[]; } // HTTP 方法对应的 console 状态色 class const methodColorMap: Record = { GET: 'bg-console-status-enabled text-white', POST: 'bg-console-status-info text-white', DELETE: 'bg-console-status-error text-white', PUT: 'bg-console-status-warning text-white', }; export function PluginApiDocs() { const { pluginName } = useParams<{ pluginName: string }>(); const [docs, setDocs] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!pluginName) { setLoading(false); return; } const loadDocs = async () => { setLoading(true); setError(null); try { const response = await fetch(`/api/plugin-docs/${pluginName}`); if (!response.ok) { setError('加载文档失败'); return; } const data: PluginDocs = await response.json(); setDocs(data); } catch (err) { console.error('Failed to load docs:', err); setError('加载文档失败'); } setLoading(false); }; loadDocs(); }, [pluginName]); if (loading) { return (
); } if (error) { return ( ); } if (!docs || (!docs.http_api.length && !docs.mcp_tools.length)) { return ( ); } const hasHttpApi = docs.http_api.length > 0; const hasMcpTools = docs.mcp_tools.length > 0; // 如果只有一种,直接显示 if (!hasHttpApi) { return ; } if (!hasMcpTools) { return ; } // 两种都有,显示 Tabs return ( HTTP API MCP 工具 ); } function HttpApiDisplay({ endpoints, pluginName }: { endpoints: HttpApiEndpoint[]; pluginName: string }) { return (

HTTP API 文档

HTTP API 可通过 /plugins/{pluginName}/api 路径调用。

{endpoints.map((endpoint, index) => (
{endpoint.methods.map((method) => ( {method} ))} {endpoint.path}

{endpoint.description || '无描述'}

))}
); } function MCPToolsDisplay({ tools }: { tools: MCPTool[] }) { return (

MCP 工具文档

MCP (Model Context Protocol) 工具可通过 /mcp 端点调用, 用于 AI 模型与插件交互。

{tools.map((tool) => (
{tool.title || tool.name} {tool.full_name}

{tool.description}

{tool.input.properties && Object.keys(tool.input.properties).length > 0 && (

输入参数

{Object.entries(tool.input.properties).map(([key, prop]) => (
{key} {tool.input.required.includes(key) && ( 必需 )} {(prop as any).description || (prop as any).type}
))}
)} {tool.output.properties && Object.keys(tool.output.properties).length > 0 && (

输出结果

{Object.entries(tool.output.properties).map(([key, prop]) => (
{key} {(prop as any).description || (prop as any).type}
))}
)}
))}
); }