Try draft prompts on own charts before saving
CI / test (push) Successful in 46s
CI / build (push) Successful in 4m16s
CI / deploy (push) Skipped

Under each prompt on /admin/promptok, "Kipróbálás" writes the summary or
detailed analysis with the text in the editor (saved or not) on one of the
admin's own charts. Test runs live in prompt_previews: never shown with the
chart, not cached, no credits, one attempt, at most three open per admin,
removed after 30 days. The worker writes them when no analysis is due. The
result shows the model, tokens, cost and time, and the draft can be loaded back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-17 14:41:12 +00:00
co-authored by Claude Opus 5.5
parent 86d5dc5503
commit b77dafdb26
13 changed files with 697 additions and 24 deletions
@@ -5,7 +5,9 @@ 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";
import type { AdminPrompt, ChartListItem, PromptKind } from "@/lib/types";
import { PromptTrial } from "./PromptTrial";
const KINDS: { kind: PromptKind; id: string; label: string; about: string }[] = [
{
@@ -26,12 +28,16 @@ const LANGUAGES: Record<string, string> = { hu: "Magyar", en: "Angol" };
export function AdminPrompts() {
const [prompts, setPrompts] = useState<AdminPrompt[] | null>(null);
const [charts, setCharts] = useState<ChartListItem[]>([]);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
api<{ prompts: AdminPrompt[] }>("/admin/prompts")
.then((d) => setPrompts(d.prompts))
.catch((e) => setError(errorText(e)));
api<{ charts: ChartListItem[] }>("/charts")
.then((d) => setCharts(d.charts))
.catch(() => setCharts([]));
}, []);
if (error) {
@@ -54,7 +60,12 @@ export function AdminPrompts() {
id: k.id,
label: k.label,
content: (
<KindPrompts about={k.about} prompts={prompts.filter((p) => p.kind === k.kind)} onSaved={replace} />
<KindPrompts
about={k.about}
prompts={prompts.filter((p) => p.kind === k.kind)}
charts={charts}
onSaved={replace}
/>
),
}))}
/>
@@ -65,10 +76,12 @@ export function AdminPrompts() {
function KindPrompts({
about,
prompts,
charts,
onSaved,
}: {
about: string;
prompts: AdminPrompt[];
charts: ChartListItem[];
onSaved: (p: AdminPrompt) => void;
}) {
const [language, setLanguage] = useState(prompts[0]?.language ?? "hu");
@@ -97,12 +110,20 @@ function KindPrompts({
</div>
)}
</div>
<PromptEditor key={`${prompt.kind}-${prompt.language}`} prompt={prompt} onSaved={onSaved} />
<PromptEditor key={`${prompt.kind}-${prompt.language}`} prompt={prompt} charts={charts} onSaved={onSaved} />
</div>
);
}
function PromptEditor({ prompt, onSaved }: { prompt: AdminPrompt; onSaved: (p: AdminPrompt) => void }) {
function PromptEditor({
prompt,
charts,
onSaved,
}: {
prompt: AdminPrompt;
charts: ChartListItem[];
onSaved: (p: AdminPrompt) => void;
}) {
const [draft, setDraft] = useState(prompt.system);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
@@ -201,6 +222,17 @@ function PromptEditor({ prompt, onSaved }: { prompt: AdminPrompt; onSaved: (p: A
)}
</div>
<PromptTrial
kind={prompt.kind}
language={prompt.language}
draft={draft}
charts={charts}
onLoad={(system) => {
setDraft(system);
setSaved(false);
}}
/>
<details className="details">
<summary>Amit a modell ezen felül kap</summary>
<div className="prompt-extra">
@@ -0,0 +1,195 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { AnalysisLoading } from "@/components/AnalysisLoading";
import { AnalysisView } from "@/components/AnalysisView";
import { api, errorText } from "@/lib/api";
import { formatDate } from "@/lib/astro";
import type { ChartListItem, PromptKind, PromptPreview } from "@/lib/types";
const POLL_MS = 4000;
const MAX_POLLS = 150; // 10 minutes
interface Trial {
preview: PromptPreview;
/** The draft it was written with, to load it back. */
system: string;
}
/**
* Writes an analysis with the draft in the editor (saved or not) on one of
* the admin's charts. Nothing is stored with the chart.
*/
export function PromptTrial({
kind,
language,
draft,
charts,
onLoad,
}: {
kind: PromptKind;
language: string;
draft: string;
charts: ChartListItem[];
onLoad: (system: string) => void;
}) {
const [chartId, setChartId] = useState(charts.find((c) => c.is_primary)?.id ?? charts[0]?.id ?? "");
const [trials, setTrials] = useState<Trial[]>([]);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const selectId = `trial-chart-${kind}-${language}`;
async function start() {
setBusy(true);
setError(null);
try {
const preview = await api<PromptPreview>(`/admin/prompts/${kind}/${language}/preview`, {
body: { system: draft, chart_id: chartId },
});
setTrials((all) => [{ preview, system: draft }, ...all]);
} catch (e) {
setError(errorText(e));
} finally {
setBusy(false);
}
}
const update = useCallback(
(next: PromptPreview) =>
setTrials((all) => all.map((t) => (t.preview.id === next.id ? { ...t, preview: next } : t))),
[],
);
return (
<section className="prompt-trial" aria-labelledby={`${selectId}-title`}>
<div className="prompt-trial-head">
<h2 id={`${selectId}-title`}>Kipróbálás</h2>
<p className="hint">
A szerkesztőben lévő szöveggel (mentés nélkül is) megírja az elemzést egy saját képletedre. Az eredmény nem
kerül a képlethez, csak itt látod. Valódi modellhívás, ezért költsége van.
</p>
</div>
{charts.length === 0 ? (
<p className="muted">Kipróbáláshoz előbb legyen legalább egy képleted.</p>
) : (
<div className="prompt-trial-form">
<div className="field">
<label htmlFor={selectId}>Képlet</label>
<select id={selectId} className="inp" value={chartId} onChange={(e) => setChartId(e.target.value)}>
{charts.map((c) => (
<option key={c.id} value={c.id}>
{c.is_primary ? `Én (${c.label})` : c.label}
{c.birth.time ? "" : " – idő nélkül"}
</option>
))}
</select>
</div>
<button type="button" className="btn btn-primary" onClick={start} disabled={busy || !chartId || !draft.trim()}>
{busy ? "Indítás…" : "Kipróbálás"}
</button>
</div>
)}
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{trials.map((t) => (
<TrialResult
key={t.preview.id}
trial={t}
chartLabel={charts.find((c) => c.id === t.preview.chart_id)?.label ?? "Képlet"}
changed={t.system.trim() !== draft.trim()}
onUpdate={update}
onLoad={() => onLoad(t.system)}
/>
))}
</section>
);
}
function TrialResult({
trial,
chartLabel,
changed,
onUpdate,
onLoad,
}: {
trial: Trial;
chartLabel: string;
changed: boolean;
onUpdate: (p: PromptPreview) => void;
onLoad: () => void;
}) {
const p = trial.preview;
const waiting = p.status === "pending" || p.status === "running";
useEffect(() => {
if (!waiting) return;
let polls = 0;
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const tick = async () => {
try {
const next = await api<PromptPreview>(`/admin/previews/${p.id}`);
if (stopped) return;
onUpdate(next);
if (next.status === "done" || next.status === "failed") return;
} catch {
// try again on the next tick
}
if (++polls < MAX_POLLS) timer = setTimeout(tick, POLL_MS);
};
timer = setTimeout(tick, POLL_MS);
return () => {
stopped = true;
clearTimeout(timer);
};
}, [waiting, p.id, onUpdate]);
const meta = [
p.model_used ?? p.model,
p.seconds !== null ? `${p.seconds} mp` : null,
p.input_tokens !== null && p.output_tokens !== null
? `${p.input_tokens.toLocaleString("hu-HU")} + ${p.output_tokens.toLocaleString("hu-HU")} token`
: null,
p.cost_usd !== null ? `$${p.cost_usd.toFixed(4)}` : null,
].filter(Boolean);
return (
<article className="trial" aria-busy={waiting}>
<header className="trial-head">
<div>
<strong>{chartLabel}</strong>
<span className="hint"> · {formatDate(p.created_at, true)}</span>
</div>
{changed && (
<button type="button" className="btn btn-sm" onClick={onLoad}>
Ennek a szövegnek a betöltése
</button>
)}
</header>
{waiting ? (
<AnalysisLoading
compact
text={p.kind === "summary" ? "Készül az összefoglaló, kb. 10–30 másodperc." : "Készül az elemzés, kb. 1–3 perc."}
/>
) : p.status === "failed" ? (
<p className="msg msg-error" role="alert">
Nem sikerült: {p.error ?? "ismeretlen hiba"}
</p>
) : p.content ? (
p.kind === "summary" ? (
<div className="trial-summary">
<h3 className="summary-title">{p.content.title}</h3>
<p className="summary-text">{p.content.summary}</p>
</div>
) : (
<AnalysisView content={p.content} />
)
) : null}
{meta.length > 0 && !waiting && <p className="hint">{meta.join(" · ")}</p>}
</article>
);
}
+18
View File
@@ -441,3 +441,21 @@ td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.prompt-history li .hint { grid-column: 1; }
.prompt-history li .btn { grid-column: 2; grid-row: 1 / span 2; }
}
.prompt-trial {
display: flex; flex-direction: column; gap: 16px; padding: 20px; border: 1px solid var(--line);
border-radius: var(--radius); background: rgba(255, 255, 255, 0.015);
}
.prompt-trial-head { display: flex; flex-direction: column; gap: 6px; }
.prompt-trial-head h2 { font-size: 18px; margin: 0; }
.prompt-trial-head .hint { margin: 0; }
.prompt-trial-form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.prompt-trial-form .field { flex: 0 1 360px; }
.trial { display: flex; flex-direction: column; gap: 14px; padding-top: 16px; border-top: 1px solid var(--line); }
.trial-head { display: flex; gap: 8px 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.trial-summary { display: flex; flex-direction: column; gap: 8px; }
.trial-summary .summary-title, .trial-summary .summary-text { margin: 0; }
@media (max-width: 640px) {
.prompt-trial { padding: 16px; }
.prompt-trial-form .field { flex-basis: 100%; }
.prompt-trial-form .btn { width: 100%; }
}