206 lines
7.5 KiB
TypeScript
206 lines
7.5 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useEffect, useMemo, useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
import { formatDate } from "@/lib/astro";
|
||
|
|
import type { CatalogModel, ModelFunction, PromptKind } from "@/lib/types";
|
||
|
|
|
||
|
|
const LABELS: Record<PromptKind, { title: string; about: string }> = {
|
||
|
|
summary: { title: "Tömör összefoglaló", about: "Ingyenes, minden új képlethez magától elkészül. Olcsó, gyors modell elég." },
|
||
|
|
natal: { title: "Részletes képletelemzés", about: "Fizetős, hat fejezet." },
|
||
|
|
synastry: { title: "Kapcsolati elemzés", about: "Fizetős, öt fejezet két képletről." },
|
||
|
|
forecast: { title: "Éves előrejelzés", about: "Fizetős, öt fejezet, a leghosszabb adatsorral." },
|
||
|
|
};
|
||
|
|
|
||
|
|
/** How many models the picker lists at once (narrow it with the search). */
|
||
|
|
const SHOWN = 60;
|
||
|
|
|
||
|
|
const usd = (x: number) => (x < 0.01 ? `$${x.toFixed(4)}` : `$${x.toFixed(3)}`);
|
||
|
|
const perMillion = (x: number) => `$${x < 1 ? x.toFixed(2) : x.toFixed(x % 1 === 0 ? 0 : 2)}`;
|
||
|
|
|
||
|
|
/** Estimated cost of one analysis of this kind with this model, from the average size of the latest ones. */
|
||
|
|
function estimate(f: ModelFunction, m: CatalogModel | undefined): number | null {
|
||
|
|
if (!m || f.avg_input_tokens === null || f.avg_output_tokens === null) return null;
|
||
|
|
return (f.avg_input_tokens * m.input_per_million + f.avg_output_tokens * m.output_per_million) / 1e6;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function AdminModels() {
|
||
|
|
const [functions, setFunctions] = useState<ModelFunction[] | null>(null);
|
||
|
|
const [catalog, setCatalog] = useState<CatalogModel[] | null>(null);
|
||
|
|
const [catalogError, setCatalogError] = useState<string | null>(null);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
api<{ functions: ModelFunction[] }>("/admin/models")
|
||
|
|
.then((d) => setFunctions(d.functions))
|
||
|
|
.catch((e) => setError(errorText(e)));
|
||
|
|
api<{ models: CatalogModel[] }>("/admin/models/catalog")
|
||
|
|
.then((d) => setCatalog(d.models))
|
||
|
|
.catch((e) => setCatalogError(errorText(e)));
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
if (error) {
|
||
|
|
return (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
if (!functions) return <p className="muted">Betöltés…</p>;
|
||
|
|
const replace = (next: ModelFunction) => setFunctions((all) => all?.map((f) => (f.kind === next.kind ? next : f)) ?? null);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="model-list">
|
||
|
|
{catalogError && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{catalogError}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
{functions.map((f) => (
|
||
|
|
<FunctionModel key={f.kind} fn={f} catalog={catalog} onSaved={replace} />
|
||
|
|
))}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
function FunctionModel({
|
||
|
|
fn,
|
||
|
|
catalog,
|
||
|
|
onSaved,
|
||
|
|
}: {
|
||
|
|
fn: ModelFunction;
|
||
|
|
catalog: CatalogModel[] | null;
|
||
|
|
onSaved: (f: ModelFunction) => void;
|
||
|
|
}) {
|
||
|
|
const [open, setOpen] = useState(false);
|
||
|
|
const [query, setQuery] = useState("");
|
||
|
|
const [busy, setBusy] = useState(false);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
const current = catalog?.find((m) => m.id === fn.model);
|
||
|
|
const cost = estimate(fn, current);
|
||
|
|
const label = LABELS[fn.kind];
|
||
|
|
const searchId = `model-search-${fn.kind}`;
|
||
|
|
|
||
|
|
const matches = useMemo(() => {
|
||
|
|
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
|
||
|
|
return (catalog ?? []).filter((m) => {
|
||
|
|
const text = `${m.name} ${m.id}`.toLowerCase();
|
||
|
|
return words.every((w) => text.includes(w));
|
||
|
|
});
|
||
|
|
}, [catalog, query]);
|
||
|
|
|
||
|
|
async function save(model: string | null) {
|
||
|
|
setBusy(true);
|
||
|
|
setError(null);
|
||
|
|
try {
|
||
|
|
onSaved(await api<ModelFunction>(`/admin/models/${fn.kind}`, { method: "PUT", body: { model } }));
|
||
|
|
setOpen(false);
|
||
|
|
setQuery("");
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
} finally {
|
||
|
|
setBusy(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<section className="card model-card" aria-labelledby={`model-${fn.kind}`}>
|
||
|
|
<div className="model-head">
|
||
|
|
<div>
|
||
|
|
<h2 id={`model-${fn.kind}`}>{label.title}</h2>
|
||
|
|
<p className="hint">{label.about}</p>
|
||
|
|
</div>
|
||
|
|
<div className="model-current">
|
||
|
|
<strong>{current?.name ?? fn.model}</strong>
|
||
|
|
<code>{fn.model}</code>
|
||
|
|
<span className="hint">
|
||
|
|
{current
|
||
|
|
? `${perMillion(current.input_per_million)} be / ${perMillion(current.output_per_million)} ki, millió tokenenként`
|
||
|
|
: catalog
|
||
|
|
? "nincs az OpenRouter listájában"
|
||
|
|
: ""}
|
||
|
|
</span>
|
||
|
|
<span className="hint">
|
||
|
|
{cost !== null
|
||
|
|
? `Becsült költség: kb. ${usd(cost)} elemzésenként (az utolsó ${fn.samples} alapján)`
|
||
|
|
: "Költségbecslés az első elkészült elemzések után."}
|
||
|
|
</span>
|
||
|
|
<span className="hint">
|
||
|
|
{fn.custom
|
||
|
|
? `Módosítva: ${fn.updated_at ? formatDate(fn.updated_at, true) : ""}${fn.updated_by ? `, ${fn.updated_by}` : ""}`
|
||
|
|
: "Alapértelmezett"}
|
||
|
|
</span>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
<div className="form-actions">
|
||
|
|
<button type="button" className="btn btn-sm" onClick={() => setOpen((o) => !o)} aria-expanded={open}>
|
||
|
|
{open ? "Mégse" : "Másik modell"}
|
||
|
|
</button>
|
||
|
|
{fn.custom && (
|
||
|
|
<button type="button" className="btn btn-sm btn-ghost" onClick={() => save(null)} disabled={busy}>
|
||
|
|
Vissza az alapértelmezettre ({fn.default})
|
||
|
|
</button>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
{open && (
|
||
|
|
<div className="model-picker">
|
||
|
|
<div className="field">
|
||
|
|
<label htmlFor={searchId}>Keresés név vagy azonosító szerint</label>
|
||
|
|
<input
|
||
|
|
id={searchId}
|
||
|
|
className="inp"
|
||
|
|
type="search"
|
||
|
|
value={query}
|
||
|
|
autoFocus
|
||
|
|
placeholder="pl. claude, gpt, gemini"
|
||
|
|
onChange={(e) => setQuery(e.target.value)}
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
{!catalog ? (
|
||
|
|
<p className="muted">A lista betöltése…</p>
|
||
|
|
) : (
|
||
|
|
<>
|
||
|
|
<p className="hint">
|
||
|
|
{matches.length} modell (csak azok, amelyek kötött JSON-választ tudnak adni). Ár: dollár millió tokenenként,
|
||
|
|
bemenet / kimenet{fn.avg_input_tokens !== null ? "; jobb oldalt a becsült költség elemzésenként" : ""}.
|
||
|
|
</p>
|
||
|
|
<ul className="model-options">
|
||
|
|
{matches.slice(0, SHOWN).map((m) => {
|
||
|
|
const each = estimate(fn, m);
|
||
|
|
return (
|
||
|
|
<li key={m.id}>
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
className="model-option"
|
||
|
|
aria-current={m.id === fn.model ? "true" : undefined}
|
||
|
|
onClick={() => save(m.id)}
|
||
|
|
disabled={busy}
|
||
|
|
>
|
||
|
|
<span>
|
||
|
|
<strong>{m.name}</strong>
|
||
|
|
<code>{m.id}</code>
|
||
|
|
</span>
|
||
|
|
<span className="model-price">
|
||
|
|
{perMillion(m.input_per_million)} / {perMillion(m.output_per_million)}
|
||
|
|
{each !== null && <strong>{usd(each)}</strong>}
|
||
|
|
</span>
|
||
|
|
</button>
|
||
|
|
</li>
|
||
|
|
);
|
||
|
|
})}
|
||
|
|
</ul>
|
||
|
|
{matches.length > SHOWN && <p className="hint">Szűkítsd a keresést a többi találathoz.</p>}
|
||
|
|
</>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
</section>
|
||
|
|
);
|
||
|
|
}
|