"use client" import { useState } from "react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Settings, Key, AlertCircle, CheckCircle } from "lucide-react" interface APIConfigProps { onConfigUpdate?: () => void } export function APIConfig({ onConfigUpdate }: APIConfigProps) { const [provider, setProvider] = useState("deepseek") const [apiKey, setApiKey] = useState("") const [isTestingAPI, setIsTestingAPI] = useState(false) const [testResult, setTestResult] = useState<{ success: boolean; message: string } | null>(null) const handleTestAPI = async () => { if (!apiKey) { setTestResult({ success: false, message: "請輸入 API 金鑰" }) return } setIsTestingAPI(true) setTestResult(null) try { const response = await fetch("/api/test-api", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ provider, apiKey, }), }) const data = await response.json() if (response.ok) { setTestResult({ success: true, message: "API 連接成功!" }) if (onConfigUpdate) { onConfigUpdate() } } else { setTestResult({ success: false, message: data.error || "API 測試失敗" }) } } catch (error) { setTestResult({ success: false, message: "網路連接錯誤" }) } finally { setIsTestingAPI(false) } } return (

API 配置

setApiKey(e.target.value)} placeholder={provider === "deepseek" ? "sk-xxxxxxxx" : "sk-proj-xxxxxxxx"} className="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-transparent" />
{testResult && (
{testResult.success ? ( ) : ( )} {testResult.message}
)}

如何獲取 API 金鑰:

{provider === "deepseek" ? (

DeepSeek API:

  1. 訪問 platform.deepseek.com
  2. 註冊或登入帳號
  3. 前往 API 金鑰頁面
  4. 創建新的 API 金鑰
  5. 複製金鑰並貼上

💡 DeepSeek 提供更實惠的定價

) : (

OpenAI API:

  1. 訪問 platform.openai.com/api-keys
  2. 登入您的 OpenAI 帳號
  3. 點擊 "Create new secret key"
  4. 複製生成的金鑰
  5. 貼上到上方欄位
)}
) }