Skip to content

Commit eeb61e9

Browse files
committed
feat(gui): add custom models from provider workspace
Reason: - Provider workspace users could not add a model when live discovery was incomplete. Changes: - Add provider-scoped custom model entry with validation, failure handling, and immediate list updates. - Preserve configured fallback models and cover duplicate, loading, refresh, and network error paths.
1 parent 235f25f commit eeb61e9

6 files changed

Lines changed: 388 additions & 7 deletions

File tree

‎gui/src/components/provider-workspace/ProviderDetails.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ export default function ProviderDetails({
227227
)}
228228
{tab === "models" && (
229229
<ProviderModels
230+
key={item.name}
230231
item={item}
232+
apiBase={apiBase}
231233
availableModels={availableModels}
232234
selectedModels={selectedModels}
233235
modelsLoading={modelsLoading}

‎gui/src/components/provider-workspace/ProviderModels.tsx‎

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { filterModels } from "../../provider-workspace/report";
1010

1111
export default function ProviderModels({
1212
item,
13+
apiBase,
1314
availableModels,
1415
selectedModels,
1516
modelsLoading = false,
@@ -19,6 +20,7 @@ export default function ProviderModels({
1920
onOpenAccounts,
2021
}: {
2122
item: WorkspaceItem;
23+
apiBase: string;
2224
availableModels: string[];
2325
selectedModels: string[];
2426
modelsLoading?: boolean;
@@ -30,15 +32,54 @@ export default function ProviderModels({
3032
}) {
3133
const t = useT();
3234
const [query, setQuery] = useState("");
35+
const [customModelId, setCustomModelId] = useState("");
36+
const [customSaving, setCustomSaving] = useState(false);
37+
const [customError, setCustomError] = useState("");
38+
const [customSuccess, setCustomSuccess] = useState("");
39+
const [customModelIds, setCustomModelIds] = useState<string[]>([]);
40+
const [customModelsReady, setCustomModelsReady] = useState(false);
3341
const [copiedId, setCopiedId] = useState<string | null>(null);
3442
const copyResetRef = useRef<number | null>(null);
3543
const selectedSet = useMemo(() => new Set(selectedModels), [selectedModels]);
3644
const configuredModels = useMemo(() => item.models ?? [], [item.models]);
45+
const trimmedCustomModelId = customModelId.trim();
46+
const customModelInvalid = !customModelsReady
47+
|| !trimmedCustomModelId
48+
|| trimmedCustomModelId.includes("/")
49+
|| availableModels.includes(trimmedCustomModelId)
50+
|| customModelIds.includes(trimmedCustomModelId)
51+
|| configuredModels.includes(trimmedCustomModelId)
52+
|| item.defaultModel === trimmedCustomModelId;
3753
const models = useMemo(
38-
() => filterModels(availableModels, item.defaultModel, query, configuredModels),
39-
[availableModels, item.defaultModel, query, configuredModels],
54+
() => filterModels(availableModels, item.defaultModel, query, configuredModels, customModelIds),
55+
[availableModels, item.defaultModel, query, configuredModels, customModelIds],
4056
);
4157

58+
useEffect(() => {
59+
let active = true;
60+
void fetch(`${apiBase}/api/custom-models`)
61+
.then(response => {
62+
if (!response.ok) throw new Error();
63+
return response.json();
64+
})
65+
.then((rows: unknown) => {
66+
if (!Array.isArray(rows)) throw new Error("Invalid custom model list");
67+
if (!active) return;
68+
setCustomModelIds(rows.flatMap(row => {
69+
if (!row || typeof row !== "object") return [];
70+
const model = row as { provider?: unknown; modelId?: unknown };
71+
return model.provider === item.name && typeof model.modelId === "string" ? [model.modelId] : [];
72+
}));
73+
setCustomModelsReady(true);
74+
})
75+
.catch(() => {
76+
if (!active) return;
77+
setCustomModelIds([]);
78+
setCustomError(t("models.networkError"));
79+
});
80+
return () => { active = false; };
81+
}, [apiBase, item.name, t]);
82+
4283
useEffect(() => () => {
4384
if (copyResetRef.current != null) window.clearTimeout(copyResetRef.current);
4485
}, []);
@@ -57,7 +98,36 @@ export default function ProviderModels({
5798
}
5899
};
59100

60-
const emptyBase = availableModels.length === 0 && configuredModels.length === 0 && !item.defaultModel;
101+
const addCustomModel = async () => {
102+
if (customModelInvalid || customSaving) return;
103+
setCustomSaving(true);
104+
setCustomError("");
105+
setCustomSuccess("");
106+
try {
107+
const response = await fetch(`${apiBase}/api/custom-models`, {
108+
method: "POST",
109+
headers: { "Content-Type": "application/json" },
110+
body: JSON.stringify({ provider: item.name, modelId: trimmedCustomModelId }),
111+
});
112+
if (response.ok) {
113+
setCustomModelIds(ids => ids.includes(trimmedCustomModelId) ? ids : [...ids, trimmedCustomModelId]);
114+
setCustomModelId("");
115+
setCustomSuccess(t("models.customAdded"));
116+
onRetryModels?.();
117+
} else {
118+
setCustomError(t("models.customSaveFailed"));
119+
}
120+
} catch {
121+
setCustomError(t("models.networkError"));
122+
} finally {
123+
setCustomSaving(false);
124+
}
125+
};
126+
127+
const emptyBase = availableModels.length === 0
128+
&& configuredModels.length === 0
129+
&& customModelIds.length === 0
130+
&& !item.defaultModel;
61131
const showingConfiguredFallback = availableModels.length === 0 && configuredModels.length > 0;
62132
// Aggregators (OpenRouter etc.) can return thousands of ids; capping the mounted
63133
// chips keeps the tab responsive. Filtering narrows the list, so the cap only
@@ -87,6 +157,31 @@ export default function ProviderModels({
87157
{showingConfiguredFallback && !needsReauth && (
88158
<p className="muted text-label" style={{ marginBottom: 10 }}>{t("pws.modelsConfiguredFallback")}</p>
89159
)}
160+
<label className="text-label pws-custom-model-label" htmlFor={`pws-custom-model-${item.name}`}>
161+
{t("models.customAdd")}
162+
</label>
163+
<div className="row pws-custom-model-row">
164+
<input
165+
id={`pws-custom-model-${item.name}`}
166+
className="input"
167+
value={customModelId}
168+
onChange={event => setCustomModelId(event.target.value)}
169+
onKeyDown={event => { if (event.key === "Enter") void addCustomModel(); }}
170+
placeholder={t("models.customFieldModelIdPlaceholder")}
171+
aria-label={t("models.customAdd")}
172+
disabled={customSaving}
173+
/>
174+
<button
175+
type="button"
176+
className="btn btn-primary btn-sm"
177+
onClick={() => { void addCustomModel(); }}
178+
disabled={customSaving || customModelInvalid}
179+
>
180+
{customSaving ? t("models.customSaving") : t("models.customAddBtn")}
181+
</button>
182+
</div>
183+
{customSuccess && <p className="muted text-label" role="status">{customSuccess}</p>}
184+
{customError && <p className="pws-inline-error" role="alert">{customError}</p>}
90185
{!emptyBase && (
91186
<input
92187
type="search"

‎gui/src/provider-workspace/report.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ export function filterModels(
6565
defaultModel: string | undefined,
6666
query: string,
6767
configuredModels?: string[],
68+
customModels: string[] = [],
6869
): string[] {
69-
const list = base.length > 0
70+
const customSet = new Set(customModels);
71+
const hasLiveModels = base.some(modelId => !customSet.has(modelId));
72+
const fallback = configuredModels && configuredModels.length > 0
73+
? configuredModels
74+
: defaultModel ? [defaultModel] : [];
75+
const primary = base.length > 0 && (hasLiveModels || customSet.size === 0)
7076
? base
71-
: (configuredModels && configuredModels.length > 0)
72-
? configuredModels
73-
: defaultModel ? [defaultModel] : [];
77+
: fallback;
78+
const list = [...new Set([...primary, ...customModels])];
7479
const q = query.trim().toLowerCase();
7580
if (!q) return list;
7681
return list.filter(id => id.toLowerCase().includes(q));

‎gui/src/styles/provider-workspace-shell.css‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,21 @@
823823
margin-bottom: 18px;
824824
}
825825

826+
.pws-custom-model-label {
827+
display: block;
828+
margin-bottom: 6px;
829+
}
830+
831+
.pws-custom-model-row {
832+
gap: 8px;
833+
margin-bottom: 12px;
834+
}
835+
836+
.pws-custom-model-row .input {
837+
flex: 1;
838+
min-width: 0;
839+
}
840+
826841
.pws-model-list {
827842
display: flex;
828843
flex-wrap: wrap;

0 commit comments

Comments
 (0)