|
| 1 | +import { 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 { toast } from "sonner"; |
| 6 | +import { useProviderStore } from "@/stores/useProviderStore"; |
| 7 | +import { ConfigTip } from "./config-tip"; |
| 8 | + |
| 9 | +export function AddProviderForm() { |
| 10 | + const [name, setName] = useState(""); |
| 11 | + const [models, setModels] = useState(""); |
| 12 | + const [baseUrl, setBaseUrl] = useState(""); |
| 13 | + const [envKey, setEnvKey] = useState(""); |
| 14 | + const { addProvider, providers } = useProviderStore(); |
| 15 | + |
| 16 | + const handleSubmit = (e: React.FormEvent) => { |
| 17 | + e.preventDefault(); |
| 18 | + if (!name || !models) return; |
| 19 | + |
| 20 | + if (providers.some((p) => p.name === name)) { |
| 21 | + toast.error("Provider with this name already exists."); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + addProvider({ |
| 26 | + name, |
| 27 | + models: models |
| 28 | + .split(",") |
| 29 | + .map((m) => m.trim()) |
| 30 | + .filter(Boolean), |
| 31 | + baseUrl: baseUrl || undefined, |
| 32 | + envKey: envKey || undefined, |
| 33 | + }); |
| 34 | + toast.success("Provider added successfully!"); |
| 35 | + setName(""); |
| 36 | + setModels(""); |
| 37 | + setBaseUrl(""); |
| 38 | + setEnvKey(""); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <div> |
| 43 | + <form onSubmit={handleSubmit} className="p-4 space-y-4"> |
| 44 | + <div className="space-y-2"> |
| 45 | + <Label>Provider Name</Label> |
| 46 | + <Input |
| 47 | + value={name} |
| 48 | + onChange={(e) => setName(e.target.value)} |
| 49 | + placeholder="e.g., My Custom AI" |
| 50 | + /> |
| 51 | + </div> |
| 52 | + <div className="space-y-2"> |
| 53 | + <Label>Models (comma-separated)</Label> |
| 54 | + <Input |
| 55 | + value={models} |
| 56 | + onChange={(e) => setModels(e.target.value)} |
| 57 | + placeholder="model-1, model-2" |
| 58 | + /> |
| 59 | + </div> |
| 60 | + <div className="space-y-2"> |
| 61 | + <Label>Base URL (Optional)</Label> |
| 62 | + <Input |
| 63 | + value={baseUrl} |
| 64 | + onChange={(e) => setBaseUrl(e.target.value)} |
| 65 | + placeholder="e.g., https://api.example.com/v1" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + <div className="space-y-2"> |
| 69 | + <Label>Environment Key (Optional)</Label> |
| 70 | + <Input |
| 71 | + value={envKey} |
| 72 | + onChange={(e) => setEnvKey(e.target.value)} |
| 73 | + placeholder="e.g., MY_API_KEY" |
| 74 | + /> |
| 75 | + </div> |
| 76 | + <Button type="submit" size="sm" className="w-full"> |
| 77 | + Add Provider |
| 78 | + </Button> |
| 79 | + </form> |
| 80 | + <div className="text-center"> |
| 81 | + <ConfigTip /> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +export function AddModelForm({ |
| 88 | + providerId, |
| 89 | + onAdd, |
| 90 | +}: { |
| 91 | + providerId: string; |
| 92 | + onAdd: () => void; |
| 93 | +}) { |
| 94 | + const [modelName, setModelName] = useState(""); |
| 95 | + const { addModel } = useProviderStore(); |
| 96 | + |
| 97 | + const handleSubmit = (e: React.FormEvent) => { |
| 98 | + e.preventDefault(); |
| 99 | + if (!modelName) return; |
| 100 | + addModel(providerId, modelName.trim()); |
| 101 | + setModelName(""); |
| 102 | + onAdd(); |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <form onSubmit={handleSubmit} className="p-4 space-y-4"> |
| 107 | + <div className="space-y-2"> |
| 108 | + <Label>Model Name</Label> |
| 109 | + <Input |
| 110 | + value={modelName} |
| 111 | + onChange={(e) => setModelName(e.target.value)} |
| 112 | + placeholder="e.g., new-model-v1" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + <Button type="submit" size="sm" className="w-full"> |
| 116 | + Add Model |
| 117 | + </Button> |
| 118 | + </form> |
| 119 | + ); |
| 120 | +} |
0 commit comments