|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { Input } from "@/components/ui/input"; |
| 4 | +import { Label } from "@/components/ui/label"; |
| 5 | +import { Separator } from "@/components/ui/separator"; |
| 6 | +import { GetAIConfig, SaveAIConfig } from "../../../wailsjs/go/database/Service"; |
| 7 | +import { database } from "../../../wailsjs/go/models"; |
| 8 | + |
| 9 | +const PROVIDERS = ["gemini", "openai", "claude"]; |
| 10 | + |
| 11 | +export function AIModelSettings() { |
| 12 | + const [configs, setConfigs] = useState<Record<string, database.AIConfig>>({}); |
| 13 | + const [loading, setLoading] = useState(false); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + loadConfigs(); |
| 17 | + }, []); |
| 18 | + |
| 19 | + const loadConfigs = async () => { |
| 20 | + setLoading(true); |
| 21 | + const newConfigs: Record<string, database.AIConfig> = {}; |
| 22 | + try { |
| 23 | + for (const provider of PROVIDERS) { |
| 24 | + const config = await GetAIConfig(provider); |
| 25 | + newConfigs[provider] = config || new database.AIConfig({ provider }); |
| 26 | + } |
| 27 | + setConfigs(newConfigs); |
| 28 | + } catch (err) { |
| 29 | + console.error("Failed to load configs", err); |
| 30 | + } finally { |
| 31 | + setLoading(false); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const handleSave = async (provider: string) => { |
| 36 | + const config = configs[provider]; |
| 37 | + if (!config) return; |
| 38 | + try { |
| 39 | + config.provider = provider; |
| 40 | + await SaveAIConfig(config); |
| 41 | + // Ideally show a toast |
| 42 | + } catch (err) { |
| 43 | + console.error(`Failed to save ${provider}`, err); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const handleChange = (provider: string, field: "apiKey" | "baseUrl", value: string) => { |
| 48 | + setConfigs((prev) => ({ |
| 49 | + ...prev, |
| 50 | + [provider]: new database.AIConfig({ |
| 51 | + ...prev[provider], |
| 52 | + [field]: value, |
| 53 | + }), |
| 54 | + })); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="space-y-8"> |
| 59 | + {PROVIDERS.map((provider, index) => { |
| 60 | + const config = configs[provider] || new database.AIConfig({ provider }); |
| 61 | + |
| 62 | + return ( |
| 63 | + <div key={provider} className="space-y-4"> |
| 64 | + {index > 0 && <Separator className="my-6" />} |
| 65 | + |
| 66 | + <div className="flex items-center justify-between"> |
| 67 | + <div> |
| 68 | + <h4 className="text-base font-medium capitalize">{provider}</h4> |
| 69 | + <p className="text-sm text-muted-foreground"> |
| 70 | + Configure connection details for {provider}. |
| 71 | + </p> |
| 72 | + </div> |
| 73 | + <Button |
| 74 | + variant="outline" |
| 75 | + size="sm" |
| 76 | + onClick={() => handleSave(provider)} |
| 77 | + disabled={loading} |
| 78 | + > |
| 79 | + 保存 |
| 80 | + </Button> |
| 81 | + </div> |
| 82 | + |
| 83 | + <div className="grid gap-4 bg-muted/30 p-4 rounded-lg border"> |
| 84 | + <div className="grid gap-2"> |
| 85 | + <Label htmlFor={`${provider}-key`}>API Key</Label> |
| 86 | + <Input |
| 87 | + id={`${provider}-key`} |
| 88 | + type="password" |
| 89 | + className="bg-background" |
| 90 | + value={config.apiKey || ""} |
| 91 | + onChange={(e) => handleChange(provider, "apiKey", e.target.value)} |
| 92 | + placeholder={`sk-...`} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + <div className="grid gap-2"> |
| 96 | + <Label htmlFor={`${provider}-base`}>Base URL</Label> |
| 97 | + <Input |
| 98 | + id={`${provider}-base`} |
| 99 | + className="bg-background" |
| 100 | + value={config.baseUrl || ""} |
| 101 | + onChange={(e) => handleChange(provider, "baseUrl", e.target.value)} |
| 102 | + placeholder="Default" |
| 103 | + /> |
| 104 | + <p className="text-[0.8rem] text-muted-foreground"> |
| 105 | + Optional. Leave empty to use the default endpoint. |
| 106 | + </p> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | + })} |
| 112 | + </div> |
| 113 | + ); |
| 114 | +} |
0 commit comments