Add Playerbot Characters model setup

This commit is contained in:
Nightwalker743 committed 2026-11-20 16:14:27 -06:00
1 parent 10cfdcf5a2
commit 206d4844c9
16 files changed
+564 -4

No files matched your search

+7 -3
View File
@@ -9,6 +9,7 @@ import { useHuman, useI18n, useSchemaText, type Key } from "@/i18n";
import { auctionWeightKey, canonicalModuleValue, moduleEnableKey, moduleField, retiredModuleKeys } from "@/lib/moduleSettings";
import { AuctionTypes } from "@/screens/AuctionTypes";
import { RaceAvailabilitySettings } from "@/screens/RaceAvailabilitySettings";
import { PbcConnectionSettings } from "@/screens/PbcConnectionSettings";
import type { UnsavedChangesGuard } from "@/lib/unsavedGuard";
/** The GitHub mark (lucide dropped its brand icons). */
@@ -111,16 +112,18 @@ export function ModulesPage({ serverId, onChanged, onRegisterGuard }: { serverId
}, [serverId]);
const [guard, setGuard] = useState<UnsavedChangesGuard | null>(null);
const [racesGuard, setRacesGuard] = useState<UnsavedChangesGuard | null>(null);
const [pbcGuard, setPbcGuard] = useState<UnsavedChangesGuard | null>(null);
const markChanged = useCallback(() => setChanged(true), []);
useEffect(() => {
const guards = [guard, racesGuard].filter((item): item is UnsavedChangesGuard => item !== null);
const guards = [guard, racesGuard, pbcGuard].filter((item): item is UnsavedChangesGuard => item !== null);
onRegisterGuard?.(guards.length ? {
isDirty: () => guards.some(item => item.isDirty()),
save: async () => { for (const item of guards) if (item.isDirty() && !await item.save()) return false; return true; },
discard: () => guards.forEach(item => item.discard()),
} : null);
return () => onRegisterGuard?.(null);
}, [guard, racesGuard, onRegisterGuard]);
}, [guard, racesGuard, pbcGuard, onRegisterGuard]);
const load = useCallback(async () => {
try {
@@ -343,6 +346,7 @@ export function ModulesPage({ serverId, onChanged, onRegisterGuard }: { serverId
{selected?.has_settings && !selected.page && selectedAt >= 0 && Math.floor(selectedAt / 2) === Math.floor(i / 2) && (i % 2 === 1 || i === modules.length - 1) && (
<SettingsPanel key={selected.id} className="sm:col-span-2">
<h2 className="font-semibold">{t("mod.settingsOf", { name: selected.name })}</h2>
{selected.id === "mod-pbc" && <PbcConnectionSettings serverId={serverId} onSaved={markChanged} onRegisterGuard={setPbcGuard} />}
<ModuleSettings serverId={serverId} module={selected} onSaved={() => setChanged(true)} onRegisterGuard={setGuard} />
{selected.id === "custom-races" && <RaceAvailabilitySettings serverId={serverId}
blocked={racesInstalling || toggling !== null} onSaved={() => setChanged(true)}
@@ -461,7 +465,7 @@ function ModuleSettings({ serverId, module, onSaved, onRegisterGuard }: { server
else next[key] = value;
return next;
});
const available = items?.filter(s => s.key !== moduleEnableKey(module.id) && !["CoACustomRaces.DisabledRaceIds", "CoACustomRaces.DisabledRaceCatalogSha256"].includes(s.key) && !retiredModuleKeys.has(s.key)) ?? [];
const available = items?.filter(s => s.key !== moduleEnableKey(module.id) && !["CoACustomRaces.DisabledRaceIds", "CoACustomRaces.DisabledRaceCatalogSha256", "PBC.DefaultConnection"].includes(s.key) && !retiredModuleKeys.has(s.key)) ?? [];
const advancedCount = available.filter(s => !auctionWeightKey(s.key) && (!moduleField(s.key, locale, s.field) || moduleField(s.key, locale, s.field)?.advanced)).length;
const visible = available.filter(s => {
if (module.id === "ah-bot" && auctionWeightKey(s.key)) return false;
+141
View File
@@ -0,0 +1,141 @@
import { useCallback, useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { useHuman, useI18n } from "@/i18n";
import { api, asUiError, type PbcConnection, type PbcConnectionSetup, type UiError } from "@/lib/api";
import type { UnsavedChangesGuard } from "@/lib/unsavedGuard";
type Provider = "openai" | "ollama" | "anthropic" | "compatible";
const presets: Record<Provider, PbcConnection> = {
openai: { apiType: "openai", baseUrl: "https://api.openai.com/v1", model: "gpt-5.4-mini" },
ollama: { apiType: "ollama", baseUrl: "http://127.0.0.1:11434", model: "" },
anthropic: { apiType: "anthropic", baseUrl: "https://api.anthropic.com/v1", model: "" },
compatible: { apiType: "openai", baseUrl: "", model: "" },
};
const providerOf = (connection: PbcConnection): Provider => connection.apiType !== "openai" ? connection.apiType
: connection.baseUrl === presets.openai.baseUrl ? "openai" : "compatible";
const inputClass = "selectable w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none focus:border-gold";
export function PbcConnectionSettings({ serverId, onSaved, onRegisterGuard }: {
serverId: string;
onSaved: () => void;
onRegisterGuard: (guard: UnsavedChangesGuard | null) => void;
}) {
const { t } = useI18n();
const human = useHuman();
const [setup, setSetup] = useState<PbcConnectionSetup | null>(null);
const [connection, setConnection] = useState<PbcConnection>(presets.openai);
const [provider, setProvider] = useState<Provider>("openai");
const [apiKey, setApiKey] = useState("");
const [removeKey, setRemoveKey] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<UiError | null>(null);
const [saved, setSaved] = useState(false);
const reset = useCallback((value: PbcConnectionSetup) => {
const current = value.connection ?? presets.openai;
setConnection(current);
setProvider(providerOf(current));
setApiKey("");
setRemoveKey(false);
}, []);
useEffect(() => {
let cancelled = false;
setSetup(null);
setSaved(false);
setError(null);
void api.pbcConnection(serverId).then(value => {
if (cancelled) return;
setSetup(value);
reset(value);
}).catch(e => { if (!cancelled) setError(asUiError(e)); });
return () => { cancelled = true; };
}, [serverId, reset]);
const previous = setup?.connection ?? presets.openai;
const dirty = !!setup && (connection.apiType !== previous.apiType || connection.baseUrl !== previous.baseUrl
|| connection.model !== previous.model || !!apiKey || removeKey);
const sameEndpoint = connection.apiType === previous.apiType
&& connection.baseUrl.trim().replace(/\/+$/, "") === previous.baseUrl;
const hasKey = !!setup?.connection?.hasKey && sameEndpoint;
const valid = !!connection.baseUrl.trim() && !!connection.model.trim();
const save = useCallback(async () => {
if (!setup || !valid || busy) return false;
setBusy(true);
setSaved(false);
setError(null);
try {
const next = await api.pbcSaveConnection(serverId, { ...connection, apiKey: apiKey || undefined, removeKey });
setSetup(next);
reset(next);
setSaved(true);
onSaved();
return true;
} catch (e) {
setError(asUiError(e));
return false;
} finally {
setBusy(false);
}
}, [setup, valid, busy, serverId, connection, apiKey, removeKey, reset, onSaved]);
useEffect(() => {
if (!setup || !dirty) return;
onRegisterGuard({ isDirty: () => true, save, discard: () => reset(setup) });
return () => onRegisterGuard(null);
}, [setup, dirty, save, reset, onRegisterGuard]);
function changeProvider(value: Provider) {
setProvider(value);
setConnection(presets[value]);
setApiKey("");
setRemoveKey(false);
setSaved(false);
}
return (
<section className="mt-4 space-y-3 border-t border-line pt-4" aria-labelledby="pbc-connection-heading">
<h3 id="pbc-connection-heading" className="font-semibold">{t("mod.pbc.connection")}</h3>
{error && <p className="text-sm text-warn" role="alert">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>}
{setup && (
<form className="space-y-3" onSubmit={event => { event.preventDefault(); void save(); }}>
{!setup.active && <p className="text-sm text-muted">{t("mod.pbc.useConnection")}</p>}
<fieldset disabled={busy} className="grid gap-3 sm:grid-cols-2">
<label className="space-y-1 text-sm">
<span>{t("mod.pbc.provider")}</span>
<select className={inputClass} value={provider} onChange={event => changeProvider(event.target.value as Provider)}>
<option value="openai">OpenAI</option>
<option value="ollama">Ollama</option>
<option value="anthropic">Anthropic</option>
<option value="compatible">{t("mod.pbc.compatible")}</option>
</select>
</label>
<label className="space-y-1 text-sm">
<span>{t("mod.pbc.model")}</span>
<input className={inputClass} value={connection.model} required onChange={event => { setConnection({ ...connection, model: event.target.value }); setSaved(false); }} />
</label>
<label className="space-y-1 text-sm sm:col-span-2">
<span>{t("mod.pbc.endpoint")}</span>
<input className={inputClass} value={connection.baseUrl} type="url" required readOnly={provider === "openai"}
onChange={event => { setConnection({ ...connection, baseUrl: event.target.value }); setApiKey(""); setRemoveKey(false); setSaved(false); }} />
</label>
<label className="space-y-1 text-sm sm:col-span-2">
<span>{t("mod.pbc.key")}</span>
<input className={inputClass} value={apiKey} type="password" autoComplete="off" disabled={removeKey}
placeholder={hasKey ? t("mod.pbc.savedKey") : undefined}
onChange={event => { setApiKey(event.target.value); setSaved(false); }} />
</label>
{hasKey && <label className="flex items-center gap-2 text-sm sm:col-span-2">
<input type="checkbox" checked={removeKey} onChange={event => { setRemoveKey(event.target.checked); setApiKey(""); setSaved(false); }} />
{t("mod.pbc.removeKey")}
</label>}
</fieldset>
<p className="text-sm text-muted">{t("mod.pbc.keyHint")}</p>
<Button size="sm" type="submit" disabled={busy || !valid || (setup.active && !dirty)}>{t("mod.pbc.save")}</Button>
{saved && <p className="text-sm text-ok" role="status">{t("mod.pbc.saved")}</p>}
</form>
)}
</section>
);
}