Questions about an analysis or forecast, answered by the model that wrote it
- 3 questions in every package, +5 questions add-on (1 000 Ft); question units, refund on failure - The answer sees the chart data, the analysis and earlier questions; at most 250 words - Editable question prompt (Promptok → Kérdés-válasz); admins regenerate answers - Package texts and FAQ updated 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,198 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { api, ApiError, errorText } from "@/lib/api";
|
||||
import type { Question, UnitBalance } from "@/lib/types";
|
||||
|
||||
import { AnalysisLoading } from "./AnalysisLoading";
|
||||
import { PackageChooser } from "./PackageCards";
|
||||
import { RegenerateButton } from "./RegenerateButton";
|
||||
import { Paragraphs } from "./Rich";
|
||||
|
||||
const MAX_CHARS = 600;
|
||||
const POLL_MS = 3000;
|
||||
const MAX_POLLS = 80; // 4 minutes
|
||||
|
||||
/**
|
||||
* Questions about a finished analysis or forecast, and their answers. Each
|
||||
* question uses one from the user's questions (admins ask for free); the
|
||||
* answer comes from the model that wrote the analysis.
|
||||
*/
|
||||
export function AnalysisQuestions({
|
||||
analysisId,
|
||||
left: initialLeft,
|
||||
isAdmin,
|
||||
canPay,
|
||||
forecast = false,
|
||||
}: {
|
||||
analysisId: string;
|
||||
left: number;
|
||||
isAdmin: boolean;
|
||||
canPay: boolean;
|
||||
forecast?: boolean;
|
||||
}) {
|
||||
const [questions, setQuestions] = useState<Question[] | null>(null);
|
||||
const [left, setLeft] = useState(initialLeft);
|
||||
const [text, setText] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [buying, setBuying] = useState(false);
|
||||
const waiting = questions?.some((q) => q.status === "pending" || q.status === "running") ?? false;
|
||||
const fieldId = `question-${analysisId}`;
|
||||
|
||||
useEffect(() => {
|
||||
api<{ questions: Question[] }>(`/analyses/${analysisId}/questions`)
|
||||
.then((d) => setQuestions(d.questions))
|
||||
.catch(() => setQuestions([]));
|
||||
}, [analysisId]);
|
||||
|
||||
const replace = useCallback(
|
||||
(next: Question) => setQuestions((all) => all?.map((q) => (q.id === next.id ? next : q)) ?? [next]),
|
||||
[],
|
||||
);
|
||||
|
||||
// Polls the open question until it is answered.
|
||||
const openId = questions?.find((q) => q.status === "pending" || q.status === "running")?.id;
|
||||
useEffect(() => {
|
||||
if (!openId) return;
|
||||
let polls = 0;
|
||||
let stopped = false;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const tick = async () => {
|
||||
try {
|
||||
const next = await api<Question>(`/questions/${openId}`);
|
||||
if (stopped) return;
|
||||
replace(next);
|
||||
if (next.status === "done" || next.status === "failed") {
|
||||
if (next.status === "failed" && next.credits_charged > 0) setLeft((n) => n + next.credits_charged);
|
||||
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);
|
||||
};
|
||||
}, [openId, replace]);
|
||||
|
||||
async function ask(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const q = await api<Question>(`/analyses/${analysisId}/questions`, { body: { question: text } });
|
||||
setQuestions((all) => [...(all ?? []), q]);
|
||||
setLeft((n) => n - q.credits_charged);
|
||||
setText("");
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status === 402) {
|
||||
setLeft(0);
|
||||
setBuying(true);
|
||||
} else {
|
||||
setError(errorText(err));
|
||||
}
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function bought() {
|
||||
try {
|
||||
const me = await api<{ analyses_left: UnitBalance }>("/me");
|
||||
setLeft(me.analyses_left.question);
|
||||
} catch {
|
||||
setLeft((n) => n + 5);
|
||||
}
|
||||
setBuying(false);
|
||||
}
|
||||
|
||||
const canAsk = isAdmin || left > 0;
|
||||
return (
|
||||
<section className="questions" aria-labelledby={`${fieldId}-title`}>
|
||||
<h3 id={`${fieldId}-title`}>Kérdezz {forecast ? "az előrejelzésedről" : "az elemzésedről"}</h3>
|
||||
<p className="hint">
|
||||
Mit jelent ez a te életedben, mire figyelj egy időszakban, hogyan kapcsolódik két rész egymáshoz? A válasz a képleted
|
||||
és az elemzés alapján készül, legfeljebb 250 szóban.
|
||||
</p>
|
||||
|
||||
{questions?.map((q) => (
|
||||
<article key={q.id} className="qa" aria-busy={q.status === "pending" || q.status === "running"}>
|
||||
<p className="qa-question">
|
||||
<span className="eyebrow">Kérdésed</span>
|
||||
{q.question}
|
||||
</p>
|
||||
{q.status === "done" && q.answer ? (
|
||||
<div className="qa-answer">
|
||||
{isAdmin && (
|
||||
<RegenerateButton<Question> url={`/admin/questions/${q.id}/regenerate`} onStarted={replace} />
|
||||
)}
|
||||
<Paragraphs text={q.answer} />
|
||||
</div>
|
||||
) : q.status === "failed" ? (
|
||||
<div className="qa-answer">
|
||||
{isAdmin && (
|
||||
<RegenerateButton<Question> url={`/admin/questions/${q.id}/regenerate`} onStarted={replace} />
|
||||
)}
|
||||
<p className="msg msg-error" role="alert">
|
||||
Erre most nem sikerült válaszolni{q.credits_charged > 0 ? ", a kérdést visszaadtuk" : ""}. Kérdezz újra
|
||||
egy kicsit később.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<AnalysisLoading compact text="Készül a válasz, ez általában fél perc…" />
|
||||
)}
|
||||
</article>
|
||||
))}
|
||||
|
||||
{canAsk && !buying ? (
|
||||
<form className="qa-form" onSubmit={ask}>
|
||||
<label htmlFor={fieldId} className="sr-only">
|
||||
A kérdésed
|
||||
</label>
|
||||
<textarea
|
||||
id={fieldId}
|
||||
className="inp"
|
||||
rows={3}
|
||||
maxLength={MAX_CHARS}
|
||||
value={text}
|
||||
placeholder={
|
||||
forecast ? "Pl. Mire figyeljek a nyári időszakban a munkámban?" : "Pl. Hogyan hat a Hold helyzete a kapcsolataimra?"
|
||||
}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
disabled={busy || waiting}
|
||||
/>
|
||||
<div className="form-actions">
|
||||
<button type="submit" className="btn btn-primary" disabled={busy || waiting || text.trim().length < 3}>
|
||||
{busy ? "Küldés…" : "Kérdés küldése"}
|
||||
</button>
|
||||
<span className="hint">
|
||||
{waiting
|
||||
? "Várd meg az előző válaszát."
|
||||
: isAdmin
|
||||
? "Adminként ingyenes."
|
||||
: `Még ${left} kérdésed van.`}{" "}
|
||||
{text.length > MAX_CHARS - 100 && `${text.length}/${MAX_CHARS}`}
|
||||
</span>
|
||||
</div>
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
) : canPay ? (
|
||||
<div className="pay-box">
|
||||
<p className="pay-lead">Elfogytak a kérdéseid. Vegyél még ötöt, és kérdezz tovább.</p>
|
||||
<PackageChooser ids={["addon_questions"]} canPay={canPay} onBought={bought} />
|
||||
</div>
|
||||
) : (
|
||||
<p className="hint">Elfogytak a kérdéseid. Újabbakat hamarosan vásárolhatsz.</p>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user