Editable analysis prompts on the admin page
The system prompts of the free summary and the paid detailed analysis can be edited per language under /admin/promptok. Every save is a new row in prompt_revisions (NULL = built-in text); new analyses record the revision in prompt_version (v2-r<id>) and the worker uses exactly that text. Existing analyses stay; the cross-user cache only copies results of the same revision. The prompt code moves to the shared analysis-prompt crate so the backend can show the built-in text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { Tabs } from "@/components/Tabs";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { formatDate } from "@/lib/astro";
|
||||
import type { AdminPrompt, PromptKind } from "@/lib/types";
|
||||
|
||||
const KINDS: { kind: PromptKind; id: string; label: string; about: string }[] = [
|
||||
{
|
||||
kind: "summary",
|
||||
id: "osszefoglalo",
|
||||
label: "Tömör összefoglaló",
|
||||
about: "Ingyenes, a képlet oldalán a „Röviden” blokk: egy cím és 3–4 mondat. Minden új képlethez magától elkészül.",
|
||||
},
|
||||
{
|
||||
kind: "natal",
|
||||
id: "reszletes",
|
||||
label: "Részletes elemzés",
|
||||
about: "Fizetős, hat fejezet, erősségek, kihívások és útravaló. Kérésre készül.",
|
||||
},
|
||||
];
|
||||
|
||||
const LANGUAGES: Record<string, string> = { hu: "Magyar", en: "Angol" };
|
||||
|
||||
export function AdminPrompts() {
|
||||
const [prompts, setPrompts] = useState<AdminPrompt[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
api<{ prompts: AdminPrompt[] }>("/admin/prompts")
|
||||
.then((d) => setPrompts(d.prompts))
|
||||
.catch((e) => setError(errorText(e)));
|
||||
}, []);
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
if (!prompts) return <p className="muted">Betöltés…</p>;
|
||||
|
||||
const replace = (next: AdminPrompt) =>
|
||||
setPrompts((all) => all?.map((p) => (p.kind === next.kind && p.language === next.language ? next : p)) ?? null);
|
||||
|
||||
return (
|
||||
<section className="card">
|
||||
<Tabs
|
||||
label="Elemzésfajták"
|
||||
tabs={KINDS.map((k) => ({
|
||||
id: k.id,
|
||||
label: k.label,
|
||||
content: (
|
||||
<KindPrompts about={k.about} prompts={prompts.filter((p) => p.kind === k.kind)} onSaved={replace} />
|
||||
),
|
||||
}))}
|
||||
/>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function KindPrompts({
|
||||
about,
|
||||
prompts,
|
||||
onSaved,
|
||||
}: {
|
||||
about: string;
|
||||
prompts: AdminPrompt[];
|
||||
onSaved: (p: AdminPrompt) => void;
|
||||
}) {
|
||||
const [language, setLanguage] = useState(prompts[0]?.language ?? "hu");
|
||||
const prompt = prompts.find((p) => p.language === language) ?? prompts[0];
|
||||
if (!prompt) return null;
|
||||
return (
|
||||
<div className="prompt-kind">
|
||||
<div className="prompt-kind-head">
|
||||
<p className="muted">{about}</p>
|
||||
{prompts.length > 1 && (
|
||||
<div className="field prompt-lang">
|
||||
<label htmlFor={`lang-${prompt.kind}`}>Nyelv</label>
|
||||
<select
|
||||
id={`lang-${prompt.kind}`}
|
||||
className="inp"
|
||||
value={language}
|
||||
onChange={(e) => setLanguage(e.target.value)}
|
||||
>
|
||||
{prompts.map((p) => (
|
||||
<option key={p.language} value={p.language}>
|
||||
{LANGUAGES[p.language] ?? p.language}
|
||||
{p.custom ? " (szerkesztett)" : ""}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<PromptEditor key={`${prompt.kind}-${prompt.language}`} prompt={prompt} onSaved={onSaved} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function PromptEditor({ prompt, onSaved }: { prompt: AdminPrompt; onSaved: (p: AdminPrompt) => void }) {
|
||||
const [draft, setDraft] = useState(prompt.system);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [saved, setSaved] = useState(false);
|
||||
const dirty = draft.trim() !== prompt.system.trim();
|
||||
const id = `prompt-${prompt.kind}-${prompt.language}`;
|
||||
|
||||
useEffect(() => {
|
||||
if (!dirty) return;
|
||||
const warn = (e: BeforeUnloadEvent) => e.preventDefault();
|
||||
window.addEventListener("beforeunload", warn);
|
||||
return () => window.removeEventListener("beforeunload", warn);
|
||||
}, [dirty]);
|
||||
|
||||
async function save(system: string | null) {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setSaved(false);
|
||||
try {
|
||||
const next = await api<AdminPrompt>(`/admin/prompts/${prompt.kind}/${prompt.language}`, {
|
||||
method: "PUT",
|
||||
body: { system },
|
||||
});
|
||||
onSaved(next);
|
||||
setDraft(next.system);
|
||||
setSaved(true);
|
||||
} catch (e) {
|
||||
setError(errorText(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="prompt-editor">
|
||||
<div className="prompt-status">
|
||||
{prompt.custom ? (
|
||||
<span className="badge badge-gold">Szerkesztett</span>
|
||||
) : (
|
||||
<span className="badge">Alapértelmezett</span>
|
||||
)}
|
||||
{prompt.updated_at && (
|
||||
<span className="hint">
|
||||
Utoljára mentve: {formatDate(prompt.updated_at, true)}
|
||||
{prompt.updated_by ? ` · ${prompt.updated_by}` : ""}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="field">
|
||||
<label htmlFor={id}>Utasítások a modellnek (system prompt)</label>
|
||||
<textarea
|
||||
id={id}
|
||||
className="inp prompt-text"
|
||||
value={draft}
|
||||
onChange={(e) => {
|
||||
setDraft(e.target.value);
|
||||
setSaved(false);
|
||||
}}
|
||||
spellCheck={false}
|
||||
rows={24}
|
||||
/>
|
||||
<span className="hint">
|
||||
{draft.length.toLocaleString("hu-HU")} karakter
|
||||
{dirty ? " · mentetlen változás" : ""}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{saved && !dirty && (
|
||||
<p className="msg msg-ok" role="status">
|
||||
Elmentve. Az ezután kért elemzések már ezzel készülnek.
|
||||
</p>
|
||||
)}
|
||||
|
||||
<div className="form-actions">
|
||||
<button type="button" className="btn btn-primary" onClick={() => save(draft)} disabled={busy || !dirty}>
|
||||
{busy ? "Mentés…" : "Mentés"}
|
||||
</button>
|
||||
<button type="button" className="btn" onClick={() => setDraft(prompt.system)} disabled={busy || !dirty}>
|
||||
Változások elvetése
|
||||
</button>
|
||||
{(prompt.custom || draft.trim() !== prompt.default.trim()) && (
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-ghost"
|
||||
onClick={() => setDraft(prompt.default)}
|
||||
disabled={busy || draft.trim() === prompt.default.trim()}
|
||||
>
|
||||
Alapértelmezett szöveg betöltése
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<details className="details">
|
||||
<summary>Amit a modell ezen felül kap</summary>
|
||||
<div className="prompt-extra">
|
||||
<p className="hint">
|
||||
Ezt a kód állítja össze, nem szerkeszthető. A <code>{"{chart data}"}</code> helyére a képlet pozíciói kerülnek
|
||||
(név, dátum és hely nélkül). A válasz formáját (mezők, fejezetek) egy rögzített JSON-séma írja elő.
|
||||
</p>
|
||||
<pre className="prompt-pre">{prompt.user_template}</pre>
|
||||
</div>
|
||||
</details>
|
||||
|
||||
{prompt.history.length > 0 && (
|
||||
<details className="details">
|
||||
<summary>Korábbi változatok ({prompt.history.length})</summary>
|
||||
<ul className="prompt-history">
|
||||
{prompt.history.map((h, i) => (
|
||||
<li key={h.id}>
|
||||
<span>
|
||||
{formatDate(h.created_at, true)}
|
||||
{h.created_by ? ` · ${h.created_by}` : ""}
|
||||
</span>
|
||||
<span className="hint">
|
||||
{i === 0 ? "érvényben · " : ""}
|
||||
{h.custom ? `${(h.system ?? "").length.toLocaleString("hu-HU")} karakter` : "alapértelmezett"}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-sm"
|
||||
onClick={() => setDraft(h.system ?? prompt.default)}
|
||||
disabled={busy}
|
||||
>
|
||||
Betöltés
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<p className="hint">A betöltött változat a szerkesztőbe kerül; a mentéssel lesz érvényes.</p>
|
||||
</details>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user