"use client"; import { useState } from "react"; import { Sparkles, Loader2, Check, Copy, Shrink, Zap, Wand2, ListFilter, FileText } from "lucide-react"; interface AIPanelProps { // For text operations (polish/expand/shorten/fix) selectedText: string; onApplyText: (text: string) => void; // For summary/tags generation title: string; content: string; onApplyMeta?: (data: { summary?: string; tags?: string }) => void; } export default function AIPanel({ selectedText, onApplyText, title, content, onApplyMeta }: AIPanelProps) { const [loading, setLoading] = useState(null); const [result, setResult] = useState(null); const [error, setError] = useState(null); const runAction = async (action: string) => { setLoading(action); setError(null); setResult(null); try { const body: Record = { action }; if (["polish", "expand", "shorten", "fix"].includes(action) && selectedText) { body.text = selectedText; } else if (["summary", "tags"].includes(action)) { body.title = title; body.content = content; } const res = await fetch("/api/ai", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!res.ok) throw new Error((await res.json()).error || "AI error"); const data = await res.json(); setResult(data.result); } catch (err: unknown) { setError(err instanceof Error ? err.message : "请求失败"); } finally { setLoading(null); } }; const apply = () => { if (!result) return; if (loading && ["summary", "tags"].includes(loading)) { // For tags: parse and apply if (loading === "tags" && onApplyMeta) { onApplyMeta({ tags: result }); } else if (loading === "summary" && onApplyMeta) { onApplyMeta({ summary: result }); } } else { onApplyText(result); } setResult(null); }; const textActions = [ { id: "polish", label: "润色", icon: Wand2, desc: "让文字更流畅" }, { id: "expand", label: "扩写", icon: Zap, desc: "增加细节论述" }, { id: "shorten", label: "缩写", icon: Shrink, desc: "精简核心信息" }, { id: "fix", label: "纠错", icon: Check, desc: "修正错别字语法" }, ]; const metaActions = [ { id: "summary", label: "生成摘要", icon: FileText }, { id: "tags", label: "提取标签", icon: ListFilter }, ]; return (

AI 助手

{/* Text operations */}
文本处理
{textActions.map((a) => ( ))}
{/* Meta generation */}
元信息生成
{metaActions.map((a) => ( ))}
{/* Result panel */} {(result || error) && (
{error ? (
{error}
) : ( <>
{result}
)}
)} {/* API key tip */} {!selectedText && !title && (

选中文字后可使用润色/扩写/缩写/纠错。填写标题后可生成摘要和标签。

)}
); }