File size: 13,004 Bytes
88c4c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | "use client";
import { useState, useCallback, useEffect } from "react";
import PropTypes from "prop-types";
import { Card, Button, Modal } from "@/shared/components";
import { getModelsByProviderId } from "@/shared/constants/models";
import { getProviderAlias } from "@/shared/constants/providers";
import { useCopyToClipboard } from "@/shared/hooks/useCopyToClipboard";
// ββ ModelRow βββββββββββββββββββββββββββββββββββββββββββββββββββ
export function ModelRow({ model, fullModel, copied, onCopy, testStatus, isCustom, isFree, onDeleteAlias, onTest, isTesting }) {
const borderColor = testStatus === "ok" ? "border-green-500/40" : testStatus === "error" ? "border-red-500/40" : "border-border";
const iconColor = testStatus === "ok" ? "#22c55e" : testStatus === "error" ? "#ef4444" : undefined;
return (
<div className={`group px-3 py-2 rounded-lg border ${borderColor} hover:bg-sidebar/50`}>
<div className="flex items-center gap-2">
<span className="material-symbols-outlined text-base" style={iconColor ? { color: iconColor } : undefined}>
{testStatus === "ok" ? "check_circle" : testStatus === "error" ? "cancel" : "smart_toy"}
</span>
<div className="flex flex-col gap-1">
<code className="text-xs text-text-muted font-mono bg-sidebar px-1.5 py-0.5 rounded">{fullModel}</code>
{model.name && <span className="text-[9px] text-text-muted/70 italic pl-1">{model.name}</span>}
</div>
{onTest && (
<div className="relative group/btn">
<button onClick={onTest} disabled={isTesting} className={`p-0.5 hover:bg-sidebar rounded text-text-muted hover:text-primary transition-opacity ${isTesting ? "opacity-100" : "opacity-0 group-hover:opacity-100"}`}>
<span className="material-symbols-outlined text-sm" style={isTesting ? { animation: "spin 1s linear infinite" } : undefined}>
{isTesting ? "progress_activity" : "science"}
</span>
</button>
<span className="pointer-events-none absolute mt-1 top-5 left-1/2 -translate-x-1/2 text-[10px] text-text-muted whitespace-nowrap opacity-0 group-hover/btn:opacity-100 transition-opacity">
{isTesting ? "Testing..." : "Test"}
</span>
</div>
)}
<div className="relative group/btn">
<button onClick={() => onCopy(fullModel, `model-${model.id}`)} className="p-0.5 hover:bg-sidebar rounded text-text-muted hover:text-primary">
<span className="material-symbols-outlined text-sm">{copied === `model-${model.id}` ? "check" : "content_copy"}</span>
</button>
<span className="pointer-events-none absolute mt-1 top-5 left-1/2 -translate-x-1/2 text-[10px] text-text-muted whitespace-nowrap opacity-0 group-hover/btn:opacity-100 transition-opacity">
{copied === `model-${model.id}` ? "Copied!" : "Copy"}
</span>
</div>
{isFree && <span className="text-[10px] font-bold text-green-500 bg-green-500/10 px-1.5 py-0.5 rounded">FREE</span>}
{isCustom && (
<button onClick={onDeleteAlias} className="p-0.5 hover:bg-red-500/10 rounded text-text-muted hover:text-red-500 opacity-0 group-hover:opacity-100 transition-opacity ml-auto" title="Remove custom model">
<span className="material-symbols-outlined text-sm">close</span>
</button>
)}
</div>
</div>
);
}
ModelRow.propTypes = {
model: PropTypes.shape({ id: PropTypes.string.isRequired }).isRequired,
fullModel: PropTypes.string.isRequired,
copied: PropTypes.string,
onCopy: PropTypes.func.isRequired,
testStatus: PropTypes.oneOf(["ok", "error"]),
isCustom: PropTypes.bool,
isFree: PropTypes.bool,
onDeleteAlias: PropTypes.func,
onTest: PropTypes.func,
isTesting: PropTypes.bool,
};
// ββ AddCustomModelModal ββββββββββββββββββββββββββββββββββββββββ
function AddCustomModelModal({ isOpen, onSave, onClose }) {
const [modelId, setModelId] = useState("");
const handleSave = () => {
if (!modelId.trim()) return;
onSave(modelId.trim());
setModelId("");
};
return (
<Modal isOpen={isOpen} title="Add Custom Model" onClose={onClose}>
<div className="flex flex-col gap-4">
<div>
<label className="text-xs text-text-muted mb-1 block">Model ID</label>
<input
className="w-full px-3 py-2 text-sm border border-border rounded-lg bg-background focus:outline-none focus:border-primary"
value={modelId}
onChange={(e) => setModelId(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleSave()}
placeholder="e.g. tts-1-hd"
autoFocus
/>
</div>
<div className="flex gap-2">
<Button onClick={handleSave} fullWidth disabled={!modelId.trim()}>Add</Button>
<Button onClick={onClose} variant="ghost" fullWidth>Cancel</Button>
</div>
</div>
</Modal>
);
}
AddCustomModelModal.propTypes = {
isOpen: PropTypes.bool.isRequired,
onSave: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
};
// ββ ModelsCard βββββββββββββββββββββββββββββββββββββββββββββββββ
// Self-contained card: shows models for a provider, filtered by optional `kindFilter`.
// kindFilter: if provided, only shows models with matching type/kinds field.
export default function ModelsCard({ providerId, kindFilter, providerAliasOverride }) {
const { copied, copy } = useCopyToClipboard();
const [modelAliases, setModelAliases] = useState({});
const [customModels, setCustomModels] = useState([]);
const [modelTestResults, setModelTestResults] = useState({});
const [testingModelId, setTestingModelId] = useState(null);
const [testError, setTestError] = useState("");
const [showAddCustomModel, setShowAddCustomModel] = useState(false);
const [connections, setConnections] = useState([]);
const providerAlias = providerAliasOverride || getProviderAlias(providerId);
const effectiveType = kindFilter || "llm";
const fetchData = useCallback(async () => {
try {
const [aliasRes, connRes, customRes] = await Promise.all([
fetch("/api/models/alias"),
fetch("/api/providers", { cache: "no-store" }),
fetch("/api/models/custom", { cache: "no-store" }),
]);
const aliasData = await aliasRes.json();
const connData = await connRes.json();
const customData = await customRes.json();
if (aliasRes.ok) setModelAliases(aliasData.aliases || {});
if (connRes.ok) setConnections((connData.connections || []).filter((c) => c.provider === providerId));
if (customRes.ok) setCustomModels(customData.models || []);
} catch (e) { console.log("ModelsCard fetch error:", e); }
}, [providerId]);
useEffect(() => { fetchData(); }, [fetchData]);
const handleSetAlias = async (modelId, alias) => {
const fullModel = `${providerAlias}/${modelId}`;
try {
const res = await fetch("/api/models/alias", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ model: fullModel, alias }),
});
if (res.ok) await fetchData();
} catch (e) { console.log("set alias error:", e); }
};
const handleDeleteAlias = async (alias) => {
try {
const res = await fetch(`/api/models/alias?alias=${encodeURIComponent(alias)}`, { method: "DELETE" });
if (res.ok) await fetchData();
} catch (e) { console.log("delete alias error:", e); }
};
const handleAddCustomModel = async (modelId) => {
try {
const res = await fetch("/api/models/custom", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ providerAlias, id: modelId, type: effectiveType }),
});
if (res.ok) {
await fetchData();
window.dispatchEvent(new CustomEvent("customModelChanged"));
}
} catch (e) { console.log("add custom model error:", e); }
};
const handleDeleteCustomModel = async (modelId) => {
try {
const params = new URLSearchParams({ providerAlias, id: modelId, type: effectiveType });
const res = await fetch(`/api/models/custom?${params}`, { method: "DELETE" });
if (res.ok) {
await fetchData();
window.dispatchEvent(new CustomEvent("customModelChanged"));
}
} catch (e) { console.log("delete custom model error:", e); }
};
const handleTestModel = async (modelId) => {
if (testingModelId) return;
setTestingModelId(modelId);
try {
const res = await fetch("/api/models/test", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ model: `${providerAlias}/${modelId}`, kind: kindFilter }),
});
const data = await res.json();
setModelTestResults((prev) => ({ ...prev, [modelId]: data.ok ? "ok" : "error" }));
setTestError(data.ok ? "" : (data.error || "Model not reachable"));
} catch {
setModelTestResults((prev) => ({ ...prev, [modelId]: "error" }));
setTestError("Network error");
} finally { setTestingModelId(null); }
};
// Built-in models β filter by kindFilter if provided
const allBuiltIn = getModelsByProviderId(providerId);
const builtInModels = kindFilter
? allBuiltIn.filter((m) => {
if (m.kinds) return m.kinds.includes(kindFilter);
return (m.type || "llm") === kindFilter;
})
: allBuiltIn;
// Custom models for this provider + kind, dedupe vs built-in
const myCustomModels = customModels.filter(
(m) => m.providerAlias === providerAlias
&& (m.type || "llm") === effectiveType
&& !builtInModels.some((b) => b.id === m.id)
);
const displayModels = builtInModels;
return (
<>
<Card>
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-semibold">Models{kindFilter ? ` β ${kindFilter.toUpperCase()}` : ""}</h2>
</div>
{testError && <p className="text-xs text-red-500 mb-3 break-words">{testError}</p>}
<div className="flex flex-wrap gap-3">
{displayModels.map((model) => {
const fullModel = `${providerAlias}/${model.id}`;
const existingAlias = Object.entries(modelAliases).find(([, m]) => m === fullModel)?.[0];
return (
<ModelRow
key={model.id}
model={model}
fullModel={`${providerAlias}/${model.id}`}
alias={existingAlias}
copied={copied}
onCopy={copy}
onSetAlias={(alias) => handleSetAlias(model.id, alias)}
onDeleteAlias={() => handleDeleteAlias(existingAlias)}
testStatus={modelTestResults[model.id]}
onTest={connections.length > 0 ? () => handleTestModel(model.id) : undefined}
isTesting={testingModelId === model.id}
isFree={model.isFree}
/>
);
})}
{myCustomModels.map((model) => (
<ModelRow
key={`${model.id}-${model.type}`}
model={{ id: model.id, name: model.name }}
fullModel={`${providerAlias}/${model.id}`}
copied={copied}
onCopy={copy}
onSetAlias={() => {}}
onDeleteAlias={() => handleDeleteCustomModel(model.id)}
testStatus={modelTestResults[model.id]}
onTest={connections.length > 0 ? () => handleTestModel(model.id) : undefined}
isTesting={testingModelId === model.id}
isCustom
/>
))}
<button
onClick={() => setShowAddCustomModel(true)}
className="flex items-center gap-1.5 px-3 py-2 rounded-lg border border-dashed border-black/15 dark:border-white/15 text-xs text-text-muted hover:text-primary hover:border-primary/40 transition-colors"
>
<span className="material-symbols-outlined text-sm">add</span>
Add Model
</button>
</div>
</Card>
<AddCustomModelModal
isOpen={showAddCustomModel}
onSave={async (modelId) => {
await handleAddCustomModel(modelId);
setShowAddCustomModel(false);
}}
onClose={() => setShowAddCustomModel(false)}
/>
</>
);
}
ModelsCard.propTypes = {
providerId: PropTypes.string.isRequired,
kindFilter: PropTypes.string, // e.g. "tts", "embedding" β filters models shown
providerAliasOverride: PropTypes.string, // override alias (e.g. for custom-embedding nodes using prefix)
};
|