Compare commits

...
Author SHA1 Message Date
fecoandClaude Opus 5.5 8b2c4310ba Merge main into prod: restart running generations, error log, retry notice, questions on relationship analyses
CI / test (push) Successful in 1m7s
CI / build (push) Successful in 19s
CI / deploy (push) Successful in 52s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:51:39 +00:00
fecoandClaude Opus 5.5 029a001ca6 Questions about relationship analyses too
CI / test (push) Successful in 1m6s
CI / build (push) Successful in 5m1s
CI / deploy (push) Skipped
- The answer sees both charts and the comparison; both people see the questions
- Heading, hint and example question per analysis kind

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:51:38 +00:00
fecoandClaude Opus 5.5 02e4e225f0 Error log, retry notice, a waiting counter that keeps counting
CI / test (push) Successful in 1m8s
CI / build (push) Successful in 5m13s
CI / deploy (push) Skipped
- generation_errors: every failed attempt (model, attempt, outcome, duration); admin
  Hibanapló page and GET /v1/admin/errors; kept 90 days
- The waiting page counts from when the text was asked for (requested_at), not from
  the page load, so a reload no longer restarts it
- It says when an attempt failed and is retried, and when a failed text's unit was
  given back; admins see the error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:47:37 +00:00
fecoandClaude Opus 5.5 cf2fb9d454 jobs: plain query string
CI / test (push) Successful in 1m2s
CI / build (push) Successful in 7m53s
CI / deploy (push) Skipped
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:32:02 +00:00
fecoandClaude Opus 5.5 b5743ec7d1 Admin: list and restart running generations with the models set now
CI / test (push) Successful in 1m17s
CI / build (push) Successful in 5m43s
CI / deploy (push) Skipped
- Modellek page: analyses and answers being written or waiting, with their model
  and the one a restart would use; restart one or all
- Each attempt's started_at is its token: a restarted attempt still running in the
  worker cannot write its result and is dropped within 10 seconds

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:31:09 +00:00
fecoandClaude Opus 5.5 1c24b9a23f Merge main into prod: model functions (analyses, answers, summary), Opus 5.5 default and fallback
CI / test (push) Successful in 1m0s
CI / build (push) Successful in 19s
CI / deploy (push) Successful in 53s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:14:13 +00:00
fecoandClaude Opus 5.5 769b4791ae Models: one for analyses and forecasts, one for answers; Opus 5.5 default and fallback
CI / test (push) Successful in 1m1s
CI / build (push) Successful in 4m56s
CI / deploy (push) Skipped
- Admin Modellek: summary, analyses + forecast (shared), question-answer (own model)
- Every default is Opus 5.5; when the chosen model fails twice (permanent: once),
  the default model takes over with fresh attempts before the unit is refunded
- The page shows how often the fallback was needed in the last 30 days

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 21:11:08 +00:00
34 changed files with 1523 additions and 220 deletions
+11 -6
View File
@@ -462,8 +462,8 @@ fn question_system(language: &str) -> String {
if language == "hu" { if language == "hu" {
let mut text = format!( let mut text = format!(
"Tapasztalt, empatikus, lélektani szemléletű asztrológus vagy a Sorsfonalak alkalmazásban. Az olvasó már \ "Tapasztalt, empatikus, lélektani szemléletű asztrológus vagy a Sorsfonalak alkalmazásban. Az olvasó már \
megkapta a részletes elemzését (születési képletelemzést vagy éves előrejelzést), és most kérdez róla. Válaszolj a \ megkapta a részletes elemzését (születési képletelemzést, kapcsolati elemzést vagy éves előrejelzést), és most kérdez \
kérdésére. róla. Válaszolj a kérdésére.
Szabályok: Szabályok:
- A válasz a megadott képletadatokon és az elemzésen alapuljon. Ne találj ki pozíciót, fényszöget, házat vagy dátumot, \ - A válasz a megadott képletadatokon és az elemzésen alapuljon. Ne találj ki pozíciót, fényszöget, házat vagy dátumot, \
@@ -471,8 +471,10 @@ amely nincs az adatok között. Ha a kérdésre az adatok alapján nem lehet fel
amit a képlet erről mutat. amit a képlet erről mutat.
- Maradj összhangban az elemzéssel. Ha a kérdés olyan részletre vonatkozik, amelyet az elemzés nem tárgyalt, a \ - Maradj összhangban az elemzéssel. Ha a kérdés olyan részletre vonatkozik, amelyet az elemzés nem tárgyalt, a \
képletadatokból egészítsd ki. képletadatokból egészítsd ki.
- Közvetlenül a kérdezőhöz szólj, tegező formában. Ha a kérdés két ember kapcsolatáról szól, csak abból beszélj, amit \ - Közvetlenül a kérdezőhöz szólj, tegező formában. Kapcsolati elemzésnél (két képlet összevetése) a kérdező az egyik \
az adatok mutatnak. fél: ha a kérdésből nem derül ki, melyik, beszélj kettejükhöz („ti”, „kettőtök”). A feleket az adatokban Person 1 és \
Person 2 jelöli, a szövegben „az első fél” és „a második fél”. Ne feltételezd a nemüket és a kapcsolat jellegét, és \
csak abból beszélj, amit az adatok mutatnak.
- Hangnem: meleg, őszinte, megerősítő és konkrét. Ne légy fatalista: ne jósolj betegséget, halált, balesetet, \ - Hangnem: meleg, őszinte, megerősítő és konkrét. Ne légy fatalista: ne jósolj betegséget, halált, balesetet, \
terhességet vagy biztos eseményeket, és ne adj orvosi, jogi vagy pénzügyi tanácsot. Ha ilyet kérdeznek, kedvesen \ terhességet vagy biztos eseményeket, és ne adj orvosi, jogi vagy pénzügyi tanácsot. Ha ilyet kérdeznek, kedvesen \
tereld a választ arra, amit az asztrológia valóban mutat: hajlamokra, időszakokra, lehetőségekre. tereld a választ arra, amit az asztrológia valóban mutat: hajlamokra, időszakokra, lehetőségekre.
@@ -490,7 +492,8 @@ használhatsz.
} }
format!( format!(
"You are an experienced, warm and psychologically minded astrologer in the Sorsfonalak app. The reader has \ "You are an experienced, warm and psychologically minded astrologer in the Sorsfonalak app. The reader has \
received their detailed analysis (a natal chart interpretation or a yearly forecast) and now asks a question about it. \ received their detailed analysis (a natal chart interpretation, a relationship analysis or a yearly forecast) and now \
asks a question about it. \
Answer it. Answer it.
Write in {lang}. Write in {lang}.
@@ -499,7 +502,9 @@ Rules:
- Base the answer on the chart data and the analysis given. Never invent positions, aspects, houses or dates that are \ - Base the answer on the chart data and the analysis given. Never invent positions, aspects, houses or dates that are \
not listed. If the data cannot answer the question, say so honestly and describe what the chart does show about it. not listed. If the data cannot answer the question, say so honestly and describe what the chart does show about it.
- Stay consistent with the analysis; fill in details it did not cover from the chart data. - Stay consistent with the analysis; fill in details it did not cover from the chart data.
- Speak to the reader directly, in the informal second person. - Speak to the reader directly, in the informal second person. On a relationship analysis the reader is one of the \
two people (Person 1 and Person 2 in the data); when it is unclear which, speak to both. Do not assume their genders \
or the type of relationship.
- Tone: warm, honest, empowering and concrete. Not fatalistic: no predictions of illness, death, accidents, pregnancy \ - Tone: warm, honest, empowering and concrete. Not fatalistic: no predictions of illness, death, accidents, pregnancy \
or certain events, and no medical, legal or financial advice; if asked, turn gently to what astrology does show \ or certain events, and no medical, legal or financial advice; if asked, turn gently to what astrology does show \
(tendencies, periods, opportunities). (tendencies, periods, opportunities).
@@ -0,0 +1,18 @@
-- One model for all analyses (natal, synastry, forecast) and a separate one
-- for answering questions; the short summary keeps its own.
ALTER TABLE model_settings DROP CONSTRAINT model_settings_kind_check;
INSERT INTO model_settings (kind, model, updated_by, updated_at)
SELECT 'analysis', model, updated_by, updated_at FROM model_settings
WHERE kind IN ('natal', 'synastry', 'forecast') ORDER BY updated_at DESC LIMIT 1;
DELETE FROM model_settings WHERE kind IN ('natal', 'synastry', 'forecast');
ALTER TABLE model_settings ADD CONSTRAINT model_settings_kind_check
CHECK (kind IN ('summary', 'analysis', 'question'));
-- When the chosen model fails, the default model takes over (`on_fallback`).
-- NULL: the chosen model is the default, there is nothing to fall back to.
ALTER TABLE analyses
ADD COLUMN fallback_model text,
ADD COLUMN on_fallback boolean NOT NULL DEFAULT false;
ALTER TABLE questions
ADD COLUMN fallback_model text,
ADD COLUMN on_fallback boolean NOT NULL DEFAULT false;
@@ -0,0 +1,26 @@
-- Every failed attempt of an analysis, an answer or a prompt test run, for the
-- admin error log. No foreign keys: the log outlives deleted items. Kept for
-- 90 days (worker cleanup).
CREATE TABLE generation_errors (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
created_at timestamptz NOT NULL DEFAULT now(),
source text NOT NULL CHECK (source IN ('analysis', 'question', 'preview')),
item_id uuid NOT NULL,
kind text NOT NULL,
model text NOT NULL,
attempt integer NOT NULL,
-- What happened next: tried again, the default model took over, or gave up.
outcome text NOT NULL CHECK (outcome IN ('retry', 'fallback', 'failed')),
error text NOT NULL,
seconds integer
);
CREATE INDEX generation_errors_created_idx ON generation_errors (created_at DESC);
CREATE INDEX generation_errors_item_idx ON generation_errors (item_id);
-- When the current text was asked for (a regeneration or an admin restart
-- sets it again): the waiting page counts from here.
ALTER TABLE analyses ADD COLUMN requested_at timestamptz NOT NULL DEFAULT now();
UPDATE analyses SET requested_at = created_at;
ALTER TABLE questions ADD COLUMN requested_at timestamptz NOT NULL DEFAULT now();
UPDATE questions SET requested_at = created_at;
+4 -3
View File
@@ -1,13 +1,14 @@
# Kérdések az elemzésekről # Kérdések az elemzésekről
A kész részletes képletelemzés és éves előrejelzés alatt a felhasználó kérdezhet róla („Kérdezz az elemzésedről”). Minden kérdés egy `question` egységet használ; minden csomagban 3 jár, és külön is vehető (+5 kérdés, 1 000 Ft). Admin ingyen kérdez. A kész részletes képletelemzés, kapcsolati elemzés és éves előrejelzés alatt a felhasználó kérdezhet róla („Kérdezz az elemzésedről”). Minden kérdés egy `question` egységet használ; minden csomagban 3 jár, és külön is vehető (+5 kérdés, 1 000 Ft). Admin ingyen kérdez.
## Hogyan készül a válasz ## Hogyan készül a válasz
- **Modell:** ugyanaz, amelyik az elemzést írta (`analyses.model`), nem a Modellek oldalon beállított. - **Modell:** a Modellek oldalon külön beállítható kérdés-válasz modell (alapból Opus 5.5). Ha a választott modell hibára fut, az alapmodell válaszol (tartalék).
- **Amit a modell kap:** a képlet tömör adatsorát (előrejelzésnél az év adatait is), magát az elemzést, az ugyanerre az elemzésre feltett korábbi legfeljebb 6 kérdést a válasszal, és az új kérdést. Születési dátum, hely, idő és név nem megy ki. - **Amit a modell kap:** a képlet tömör adatsorát (kapcsolati elemzésnél mindkét képletet és az összevetést, előrejelzésnél az év adatait is), magát az elemzést, az ugyanerre az elemzésre feltett korábbi legfeljebb 6 kérdést a válasszal, és az új kérdést. Születési dátum, hely, idő és név nem megy ki.
- **Prompt:** az admin felületen szerkeszthető (Promptok → Kérdés-válasz). A szóhatár (`QUESTION_MAX_WORDS` = 250) a felhasználói üzenetben is benne van, így a prompt átírásától sem vész el. A kérdés legfeljebb 600 karakter. - **Prompt:** az admin felületen szerkeszthető (Promptok → Kérdés-válasz). A szóhatár (`QUESTION_MAX_WORDS` = 250) a felhasználói üzenetben is benne van, így a prompt átírásától sem vész el. A kérdés legfeljebb 600 karakter.
- **Szabályok:** csak a képletből és az elemzésből válaszol, nem jósol betegséget, halált, balesetet, terhességet vagy biztos eseményt, nem ad orvosi, jogi, pénzügyi tanácsot; a nem asztrológiai kérdést udvariasan elhárítja; a kérdésben lévő utasításokat nem követi. - **Szabályok:** csak a képletből és az elemzésből válaszol, nem jósol betegséget, halált, balesetet, terhességet vagy biztos eseményt, nem ad orvosi, jogi, pénzügyi tanácsot; a nem asztrológiai kérdést udvariasan elhárítja; a kérdésben lévő utasításokat nem követi.
- **Kapcsolati elemzés:** az összevetés mindkét tagja kérdezhet (a saját kérdéseiből), és mindketten látják a kérdéseket és a válaszokat.
- **Egyszerre egy:** amíg egy kérdésre készül a válasz, újabb nem tehető fel ugyanarra az elemzésre (így a válaszok egymásra épülnek). - **Egyszerre egy:** amíg egy kérdésre készül a válasz, újabb nem tehető fel ugyanarra az elemzésre (így a válaszok egymásra épülnek).
- **Hiba:** a worker újrapróbálja, mint az elemzéseket; végleges hibánál a kérdést visszaadja (egyszer). - **Hiba:** a worker újrapróbálja, mint az elemzéseket; végleges hibánál a kérdést visszaadja (egyszer).
- **Újragenerálás:** admin a válasz melletti ↻ gombbal újraírathatja (díjmentesen). - **Újragenerálás:** admin a válasz melletti ↻ gombbal újraírathatja (díjmentesen).
+6 -3
View File
@@ -20,8 +20,8 @@ Felhasználók, belépés, mentett születési képletek, szinasztriák és megh
| `GOOGLE_CLIENT_IDS` | – | Google OAuth kliens-azonosító(k); üresen a Google-belépés ki van kapcsolva | | `GOOGLE_CLIENT_IDS` | – | Google OAuth kliens-azonosító(k); üresen a Google-belépés ki van kapcsolva |
| `BOOTSTRAP_ADMIN_EMAILS` | – | ezek a címek regisztrációkor vagy belépéskor admin szerepet kapnak (így lesz meg az első admin) | | `BOOTSTRAP_ADMIN_EMAILS` | – | ezek a címek regisztrációkor vagy belépéskor admin szerepet kapnak (így lesz meg az első admin) |
| `SUPPORTED_LOCALES` | `hu,en` | felület- és elemzésnyelvek; az első az alapértelmezett | | `SUPPORTED_LOCALES` | `hu,en` | felület- és elemzésnyelvek; az első az alapértelmezett |
| `ANALYSIS_MODEL` | `anthropic/claude-opus-5.5` | alapból ez írja a fizetős részletes elemzéseket (az admin felületen funkciónként felülírható) | | `ANALYSIS_MODEL` | `anthropic/claude-opus-5.5` | az elemzések, előrejelzések és válaszok alapmodellje, és a tartalék, ha a választott modell hibára fut |
| `SUMMARY_MODEL` | `anthropic/claude-sonnet-5.5` | alapból ez írja minden mentett képlet ingyenes rövid összefoglalóját (kb. 1 cent darabonként) | | `SUMMARY_MODEL` | `anthropic/claude-opus-5.5` | a rövid összefoglaló alapmodellje (az admin felületen olcsóbbra állítható) |
| `OPENROUTER_MODELS_URL` | `https://openrouter.ai/api/v1/models` | az admin modellválasztó listája árakkal (óránként egyszer kérdezi le) | | `OPENROUTER_MODELS_URL` | `https://openrouter.ai/api/v1/models` | az admin modellválasztó listája árakkal (óránként egyszer kérdezi le) |
| `ANALYSIS_PROMPT_VERSION` | `v3` | a prompt verziója (az elemzés-gyorsítótár kulcsának része) | | `ANALYSIS_PROMPT_VERSION` | `v3` | a prompt verziója (az elemzés-gyorsítótár kulcsának része) |
| `MAX_CHARTS_PER_USER` | `1000` | biztonsági felső korlát a képletek számára | | `MAX_CHARTS_PER_USER` | `1000` | biztonsági felső korlát a képletek számára |
@@ -125,7 +125,10 @@ Hibák formátuma: `{"error": {"code", "message"}}`. A születési adatok hibái
- **Ingyenes rész:** minden mentett képlethez elkészül egy rövid összefoglaló (`summary`, `SUMMARY_MODEL`), ezért nem kell fizetni. - **Ingyenes rész:** minden mentett képlethez elkészül egy rövid összefoglaló (`summary`, `SUMMARY_MODEL`), ezért nem kell fizetni.
- **Fizetős rész:** a részletes elemzés darabra megy. A képletelemzés egy `natal`, a szinasztria-elemzés egy `synastry` egységet von le (`credit_ledger.unit`). Az egységek csomagvásárlásból (Stripe, később) vagy admin jóváírásból jönnek. Havi ingyenes keret nincs. - **Fizetős rész:** a részletes elemzés darabra megy. A képletelemzés egy `natal`, a szinasztria-elemzés egy `synastry` egységet von le (`credit_ledger.unit`). Az egységek csomagvásárlásból (Stripe, később) vagy admin jóváírásból jönnek. Havi ingyenes keret nincs.
- **Modell:** funkciónként (összefoglaló, képletelemzés, kapcsolati elemzés, előrejelzés) az admin felületen választható az OpenRouter listájából (`model_settings` tábla); ha nincs beállítva, az `ANALYSIS_MODEL` / `SUMMARY_MODEL` (alapból Opus 5.5, a 2026-11-es összevetés legjobbja, lásd `services/worker/README.md`). Csak kötött JSON-választ adó modell választható. A kliens nem választ modellt, és a modell neve felhasználónak sehol nem jelenik meg. - **Modell:** három beállítás az admin felületen, az OpenRouter listájából (`model_settings` tábla): az elemzések és az éves előrejelzés (képlet-, kapcsolati elemzés, előrejelzés) közös modellje, a kérdés-válasz modellje és a rövid összefoglalóé. Ha nincs beállítva, az `ANALYSIS_MODEL` / `SUMMARY_MODEL` (alapból mindkettő Opus 5.5, a 2026-11-es összevetés legjobbja, lásd `services/worker/README.md`). Csak kötött JSON-választ adó modell választható. A kliens nem választ modellt, és a modell neve felhasználónak sehol nem jelenik meg.
- **Tartalék:** ha a választott modell kétszer hibára fut (végleges hibánál egyszer), a worker az alapmodellel próbálja tovább (`fallback_model`, `on_fallback`); csak ha az is elbukik, jár vissza az egység. A Modellek oldal mutatja, hány szövegnél kellett az elmúlt 30 napban.
- **Hibanapló:** a worker minden sikertelen próbálkozást a `generation_errors` táblába ír (mi, melyik modell, hányadik kísérlet, mi lett: újrapróbálás, tartalék vagy végleges hiba, mennyi ideig tartott); az admin Hibanapló oldala mutatja (`GET /v1/admin/errors?source=&outcome=`), 90 napig őrizzük. A felhasználó a várakozó oldalon látja, ha az előző próbálkozás nem sikerült és újrapróbáljuk; a számláló a kérés idejétől (`requested_at`) mér.
- **Futó generálások:** a Modellek oldalon látszik minden készülő és sorban álló elemzés és válasz, a modelljével és azzal, amivel most indulna; újraindításkor a jelenlegi beállítással indul újra (`GET /v1/admin/jobs`, `POST /v1/admin/jobs/restart`). Minden kísérlet `started_at` értéke a „jegye”: az újraindítás törli, így a workerben még futó régi kísérlet nem írhatja felül az újat, és a worker 10 másodpercen belül eldobja.
- **Újragenerálás:** admin minden elemzést egyenként újraírathat (`POST /v1/admin/analyses/{id}/regenerate`): ugyanaz a sor (az azonosító és a megosztások maradnak), a jelenlegi prompttal és modellel, egységlevonás nélkül. - **Újragenerálás:** admin minden elemzést egyenként újraírathat (`POST /v1/admin/analyses/{id}/regenerate`): ugyanaz a sor (az azonosító és a megosztások maradnak), a jelenlegi prompttal és modellel, egységlevonás nélkül.
- **Újraszámolás:** számítási változás után a `backend recalculate` parancs minden mentett képletet, összevetést és előrejelzést újraszámol, majd minden elemzést újra sorba állít (egységlevonás nélkül). - **Újraszámolás:** számítási változás után a `backend recalculate` parancs minden mentett képletet, összevetést és előrejelzést újraszámol, majd minden elemzést újra sorba állít (egységlevonás nélkül).
- **Kérés:** a `POST .../analyses` (`{language?}`) létrehoz egy `pending` elemzést, és levon egy egységet; a szöveget a worker írja meg, általában 1–2 perc alatt. A kliens a `GET /v1/analyses/{id}` végponton kérdezi le az állapotát. Ha nincs egység, 402 `insufficient_credits` a válasz (`unit`, `balance` részletekkel). Hiba esetén a worker visszaadja az egységet. - **Kérés:** a `POST .../analyses` (`{language?}`) létrehoz egy `pending` elemzést, és levon egy egységet; a szöveget a worker írja meg, általában 1–2 perc alatt. A kliens a `GET /v1/analyses/{id}` végponton kérdezi le az állapotát. Ha nincs egység, 402 `insufficient_credits` a válasz (`unit`, `balance` részletekkel). Hiba esetén a worker visszaadja az egységet.
+6 -4
View File
@@ -55,11 +55,13 @@ pub struct Config {
pub bootstrap_admin_emails: Vec<String>, pub bootstrap_admin_emails: Vec<String>,
/// `SUPPORTED_LOCALES` (default `hu,en`); the first is the default. /// `SUPPORTED_LOCALES` (default `hu,en`); the first is the default.
pub supported_locales: Vec<String>, pub supported_locales: Vec<String>,
/// `ANALYSIS_MODEL`: writes the paid detailed analyses (default /// `ANALYSIS_MODEL`: the default model of the analyses, forecasts and
/// answers, and the fallback when a chosen model fails (default
/// `anthropic/claude-opus-5.5`, the best of the 2026-11 comparison). /// `anthropic/claude-opus-5.5`, the best of the 2026-11 comparison).
pub analysis_model: String, pub analysis_model: String,
/// `SUMMARY_MODEL`: writes the free short summary of every saved chart /// `SUMMARY_MODEL`: the default model of the free short summary of every
/// (default `anthropic/claude-sonnet-5.5`, about 1 US cent each). /// saved chart (default `anthropic/claude-opus-5.5`; the admin page can
/// choose a cheaper one).
pub summary_model: String, pub summary_model: String,
/// `OPENROUTER_MODELS_URL` (default `https://openrouter.ai/api/v1/models`): /// `OPENROUTER_MODELS_URL` (default `https://openrouter.ai/api/v1/models`):
/// the model list with prices for the admin page. /// the model list with prices for the admin page.
@@ -123,7 +125,7 @@ impl Config {
bootstrap_admin_emails: list("BOOTSTRAP_ADMIN_EMAILS").into_iter().map(|e| e.to_lowercase()).collect(), bootstrap_admin_emails: list("BOOTSTRAP_ADMIN_EMAILS").into_iter().map(|e| e.to_lowercase()).collect(),
supported_locales, supported_locales,
analysis_model: var("ANALYSIS_MODEL").unwrap_or_else(|| "anthropic/claude-opus-5.5".into()), analysis_model: var("ANALYSIS_MODEL").unwrap_or_else(|| "anthropic/claude-opus-5.5".into()),
summary_model: var("SUMMARY_MODEL").unwrap_or_else(|| "anthropic/claude-sonnet-5.5".into()), summary_model: var("SUMMARY_MODEL").unwrap_or_else(|| "anthropic/claude-opus-5.5".into()),
openrouter_models_url: var("OPENROUTER_MODELS_URL") openrouter_models_url: var("OPENROUTER_MODELS_URL")
.unwrap_or_else(|| "https://openrouter.ai/api/v1/models".into()), .unwrap_or_else(|| "https://openrouter.ai/api/v1/models".into()),
prompt_version: var("ANALYSIS_PROMPT_VERSION").unwrap_or_else(|| "v3".into()), prompt_version: var("ANALYSIS_PROMPT_VERSION").unwrap_or_else(|| "v3".into()),
+57 -11
View File
@@ -2,8 +2,9 @@
//! offers (with prices) for the admin page. //! offers (with prices) for the admin page.
//! //!
//! The choice is in `model_settings`; without a row the configured default //! The choice is in `model_settings`; without a row the configured default
//! is used (`SUMMARY_MODEL` for the short summary, `ANALYSIS_MODEL` else). //! is used (`SUMMARY_MODEL` for the short summary, `ANALYSIS_MODEL` else,
//! The model is never shown to users (a business secret). //! both Opus 5.5 by default). When the chosen model fails, the worker falls
//! back to the default. The model is never shown to users (a business secret).
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@@ -18,21 +19,66 @@ use crate::AppState;
/// The OpenRouter list is fetched at most this often. /// The OpenRouter list is fetched at most this often.
const CATALOG_TTL: Duration = Duration::from_secs(3600); const CATALOG_TTL: Duration = Duration::from_secs(3600);
/// The configured default model of a kind. /// The model settings of the admin page: one model writes all analyses and
pub fn default_model(state: &AppState, kind: Kind) -> &str { /// forecasts, another the answers to questions, a third the short summary.
match kind { #[derive(Debug, Clone, Copy, PartialEq, Eq)]
Kind::Summary => &state.config.summary_model, pub enum Function {
_ => &state.config.analysis_model, Summary,
Analysis,
Question,
}
impl Function {
pub const ALL: [Function; 3] = [Function::Summary, Function::Analysis, Function::Question];
pub fn key(self) -> &'static str {
match self {
Function::Summary => "summary",
Function::Analysis => "analysis",
Function::Question => "question",
}
}
pub fn parse(key: &str) -> Option<Function> {
Function::ALL.into_iter().find(|f| f.key() == key)
}
pub fn of(kind: Kind) -> Function {
match kind {
Kind::Summary => Function::Summary,
Kind::Question => Function::Question,
Kind::Natal | Kind::Synastry | Kind::Forecast => Function::Analysis,
}
} }
} }
/// The model new analyses of this kind are written with. /// The configured default model of a function; also the fallback when the
pub async fn model_for(state: &AppState, kind: Kind) -> Result<String, sqlx::Error> { /// chosen model fails.
pub fn default_model(state: &AppState, function: Function) -> &str {
match function {
Function::Summary => &state.config.summary_model,
Function::Analysis | Function::Question => &state.config.analysis_model,
}
}
/// The model new texts of this kind are written with, and the fallback model
/// (`None` when the chosen model is the default).
pub async fn models_for(state: &AppState, kind: Kind) -> Result<(String, Option<String>), sqlx::Error> {
let function = Function::of(kind);
let chosen: Option<String> = sqlx::query_scalar("SELECT model FROM model_settings WHERE kind = $1") let chosen: Option<String> = sqlx::query_scalar("SELECT model FROM model_settings WHERE kind = $1")
.bind(kind.as_str()) .bind(function.key())
.fetch_optional(&state.db) .fetch_optional(&state.db)
.await?; .await?;
Ok(chosen.unwrap_or_else(|| default_model(state, kind).to_owned())) let default = default_model(state, function);
Ok(match chosen {
Some(model) if model != default => (model, Some(default.to_owned())),
_ => (default.to_owned(), None),
})
}
/// The model new texts of this kind are written with.
pub async fn model_for(state: &AppState, kind: Kind) -> Result<String, sqlx::Error> {
Ok(models_for(state, kind).await?.0)
} }
/// One model OpenRouter offers, with its price in USD per million tokens. /// One model OpenRouter offers, with its price in USD per million tokens.
+31 -28
View File
@@ -49,11 +49,16 @@ pub struct AnalysisRow {
pub credits_charged: i32, pub credits_charged: i32,
pub created_at: OffsetDateTime, pub created_at: OffsetDateTime,
pub completed_at: Option<OffsetDateTime>, pub completed_at: Option<OffsetDateTime>,
/// Attempts so far; above 1 the page says it is being tried again.
pub attempts: i32,
pub next_attempt_at: OffsetDateTime,
/// When the current text was asked for (set again by a regeneration).
pub requested_at: OffsetDateTime,
} }
const ANALYSIS_COLUMNS: &str = "id, kind, chart_id, synastry_id, forecast_id, language, prompt_version, status, \ const ANALYSIS_COLUMNS: &str = "id, kind, chart_id, synastry_id, forecast_id, language, prompt_version, status, \
content, error, \ content, error, \
credits_charged, created_at, completed_at"; credits_charged, created_at, completed_at, attempts, next_attempt_at, requested_at";
impl AnalysisRow { impl AnalysisRow {
pub fn view(&self) -> Value { pub fn view(&self) -> Value {
@@ -71,6 +76,9 @@ impl AnalysisRow {
"credits_charged": self.credits_charged, "credits_charged": self.credits_charged,
"created_at": rfc3339(self.created_at), "created_at": rfc3339(self.created_at),
"completed_at": rfc3339_opt(self.completed_at), "completed_at": rfc3339_opt(self.completed_at),
"attempts": self.attempts,
"next_attempt_at": rfc3339(self.next_attempt_at),
"requested_at": rfc3339(self.requested_at),
}) })
} }
} }
@@ -146,7 +154,7 @@ async fn request(
/// The cache key and the prompt version of a new analysis. The cache key leaves /// The cache key and the prompt version of a new analysis. The cache key leaves
/// out the admin-edited prompt revision, so a chart keeps its analysis when the /// out the admin-edited prompt revision, so a chart keeps its analysis when the
/// prompt changes; copying from another chart needs the same revision. /// prompt changes; copying from another chart needs the same revision.
async fn versions( pub(super) async fn versions(
state: &AppState, state: &AppState,
kind: &str, kind: &str,
hash: &str, hash: &str,
@@ -175,7 +183,7 @@ pub(super) async fn queue(
Target::Synastry { synastry_id, hash } => ("synastry", hash, None, Some(*synastry_id), None), Target::Synastry { synastry_id, hash } => ("synastry", hash, None, Some(*synastry_id), None),
Target::Forecast { forecast_id, hash } => ("forecast", hash, None, None, Some(*forecast_id)), Target::Forecast { forecast_id, hash } => ("forecast", hash, None, None, Some(*forecast_id)),
}; };
let model = &crate::models::model_for(state, Kind::parse(kind).expect("known kind")).await?; let (model, fallback) = &crate::models::models_for(state, Kind::parse(kind).expect("known kind")).await?;
let (cache_key, prompt_version) = &versions(state, kind, hash, model, language).await?; let (cache_key, prompt_version) = &versions(state, kind, hash, model, language).await?;
// Already requested for this chart/synastry: return it. // Already requested for this chart/synastry: return it.
@@ -197,8 +205,8 @@ pub(super) async fn queue(
let insert = format!( let insert = format!(
"INSERT INTO analyses (id, kind, chart_id, synastry_id, requested_by, language, model, prompt_version, \ "INSERT INTO analyses (id, kind, chart_id, synastry_id, requested_by, language, model, prompt_version, \
cache_key, status, content, input_tokens, output_tokens, credits_charged, completed_at, \ cache_key, status, content, input_tokens, output_tokens, credits_charged, completed_at, \
forecast_id) \ forecast_id, fallback_model) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING {ANALYSIS_COLUMNS}" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING {ANALYSIS_COLUMNS}"
); );
let id = Uuid::now_v7(); let id = Uuid::now_v7();
@@ -229,6 +237,7 @@ pub(super) async fn queue(
.bind(0) .bind(0)
.bind(OffsetDateTime::now_utc()) .bind(OffsetDateTime::now_utc())
.bind(forecast_id) .bind(forecast_id)
.bind(fallback)
.fetch_one(&state.db) .fetch_one(&state.db)
.await?; .await?;
return Ok((StatusCode::CREATED, row)); return Ok((StatusCode::CREATED, row));
@@ -253,6 +262,7 @@ pub(super) async fn queue(
.bind(i32::from(unit.is_some())) .bind(i32::from(unit.is_some()))
.bind(None::<OffsetDateTime>) .bind(None::<OffsetDateTime>)
.bind(forecast_id) .bind(forecast_id)
.bind(fallback)
.fetch_one(&mut *tx) .fetch_one(&mut *tx)
.await?; .await?;
if let Some(unit) = unit { if let Some(unit) = unit {
@@ -357,6 +367,17 @@ pub async fn request_for_forecast(
request(&state, &auth, Target::Forecast { forecast_id: forecast.id, hash: forecast.forecast_hash }, req).await request(&state, &auth, Target::Forecast { forecast_id: forecast.id, hash: forecast.forecast_hash }, req).await
} }
/// The hash of what the analysis is about (chart, comparison or forecast),
/// part of its cache key; `None` when that was deleted.
pub(super) async fn target_hash(db: &PgPool, row: &AnalysisRow) -> Result<Option<String>, sqlx::Error> {
let (query, id) = match row.kind.as_str() {
"synastry" => ("SELECT synastry_hash FROM synastries WHERE id = $1", row.synastry_id),
"forecast" => ("SELECT forecast_hash FROM forecasts WHERE id = $1", row.forecast_id),
_ => ("SELECT chart_hash FROM charts WHERE id = $1", row.chart_id),
};
sqlx::query_scalar(query).bind(id).fetch_optional(db).await
}
/// Admin: writes an analysis again in place (same id, so links stay), with the /// Admin: writes an analysis again in place (same id, so links stay), with the
/// current prompt and model. Nobody is charged. /// current prompt and model. Nobody is charged.
pub async fn regenerate( pub async fn regenerate(
@@ -369,41 +390,23 @@ pub async fn regenerate(
return Err(ApiError::conflict("analysis_in_progress", "Ez az elemzés éppen készül.")); return Err(ApiError::conflict("analysis_in_progress", "Ez az elemzés éppen készül."));
} }
let kind = Kind::parse(&row.kind).ok_or_else(|| ApiError::Internal(format!("unknown kind {}", row.kind)))?; let kind = Kind::parse(&row.kind).ok_or_else(|| ApiError::Internal(format!("unknown kind {}", row.kind)))?;
let hash: Option<String> = match kind { let hash = target_hash(&state.db, &row).await?;
Kind::Question => None,
Kind::Natal | Kind::Summary => {
sqlx::query_scalar("SELECT chart_hash FROM charts WHERE id = $1")
.bind(row.chart_id)
.fetch_optional(&state.db)
.await?
}
Kind::Synastry => {
sqlx::query_scalar("SELECT synastry_hash FROM synastries WHERE id = $1")
.bind(row.synastry_id)
.fetch_optional(&state.db)
.await?
}
Kind::Forecast => {
sqlx::query_scalar("SELECT forecast_hash FROM forecasts WHERE id = $1")
.bind(row.forecast_id)
.fetch_optional(&state.db)
.await?
}
};
let hash = hash.ok_or(ApiError::NotFound("analysis"))?; let hash = hash.ok_or(ApiError::NotFound("analysis"))?;
let model = crate::models::model_for(&state, kind).await?; let (model, fallback) = crate::models::models_for(&state, kind).await?;
let (cache_key, prompt_version) = versions(&state, kind.as_str(), &hash, &model, &row.language).await?; let (cache_key, prompt_version) = versions(&state, kind.as_str(), &hash, &model, &row.language).await?;
let mut tx = state.db.begin().await?; let mut tx = state.db.begin().await?;
let row: AnalysisRow = sqlx::query_as(&format!( let row: AnalysisRow = sqlx::query_as(&format!(
"UPDATE analyses SET status = 'pending', content = NULL, error = NULL, model_used = NULL, \ "UPDATE analyses SET status = 'pending', content = NULL, error = NULL, model_used = NULL, \
input_tokens = NULL, output_tokens = NULL, cost_usd = NULL, attempts = 0, next_attempt_at = now(), \ input_tokens = NULL, output_tokens = NULL, cost_usd = NULL, attempts = 0, next_attempt_at = now(), \
started_at = NULL, completed_at = NULL, model = $2, prompt_version = $3, cache_key = $4 \ started_at = NULL, completed_at = NULL, model = $2, prompt_version = $3, cache_key = $4, \
fallback_model = $5, on_fallback = false, requested_at = now() \
WHERE id = $1 AND status IN ('done', 'failed') RETURNING {ANALYSIS_COLUMNS}" WHERE id = $1 AND status IN ('done', 'failed') RETURNING {ANALYSIS_COLUMNS}"
)) ))
.bind(id) .bind(id)
.bind(&model) .bind(&model)
.bind(&prompt_version) .bind(&prompt_version)
.bind(&cache_key) .bind(&cache_key)
.bind(&fallback)
.fetch_optional(&mut *tx) .fetch_optional(&mut *tx)
.await? .await?
.ok_or_else(|| ApiError::conflict("analysis_in_progress", "Ez az elemzés éppen készül."))?; .ok_or_else(|| ApiError::conflict("analysis_in_progress", "Ez az elemzés éppen készül."))?;
+73
View File
@@ -0,0 +1,73 @@
//! Admin: the error log of the analyses, answers and prompt test runs (every
//! failed attempt, written by the worker), newest first.
use axum::Json;
use axum::extract::{Query, State};
use axum_extra::extract::WithRejection;
use serde::Deserialize;
use serde_json::{Value, json};
use time::OffsetDateTime;
use uuid::Uuid;
use super::St;
use crate::auth::AdminUser;
use crate::error::{ApiError, ApiResult};
use crate::views::rfc3339;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ErrorQuery {
limit: Option<i64>,
offset: Option<i64>,
/// `analysis`, `question` or `preview`.
source: Option<String>,
/// `retry`, `fallback` or `failed`.
outcome: Option<String>,
}
type Row = (i64, OffsetDateTime, String, Uuid, String, String, i32, String, String, Option<i32>, Option<String>);
pub async fn list(
State(state): St,
_admin: AdminUser,
WithRejection(Query(query), _): WithRejection<Query<ErrorQuery>, ApiError>,
) -> ApiResult<Json<Value>> {
let limit = query.limit.unwrap_or(100).clamp(1, 500);
let offset = query.offset.unwrap_or(0).max(0);
// Whose text it was: the requester of the analysis or the asker.
let rows: Vec<Row> = sqlx::query_as(
"SELECT e.id, e.created_at, e.source, e.item_id, e.kind, e.model, e.attempt, e.outcome, e.error, e.seconds, \
u.email \
FROM generation_errors e \
LEFT JOIN analyses a ON e.source = 'analysis' AND a.id = e.item_id \
LEFT JOIN questions q ON e.source = 'question' AND q.id = e.item_id \
LEFT JOIN prompt_previews p ON e.source = 'preview' AND p.id = e.item_id \
LEFT JOIN users u ON u.id = coalesce(a.requested_by, q.asked_by, p.created_by) \
WHERE ($1::text IS NULL OR e.source = $1) AND ($2::text IS NULL OR e.outcome = $2) \
ORDER BY e.id DESC LIMIT $3 OFFSET $4",
)
.bind(&query.source)
.bind(&query.outcome)
.bind(limit)
.bind(offset)
.fetch_all(&state.db)
.await?;
let (total, last_day): (i64, i64) = sqlx::query_as(
"SELECT count(*), count(*) FILTER (WHERE created_at > now() - interval '1 day') FROM generation_errors \
WHERE ($1::text IS NULL OR source = $1) AND ($2::text IS NULL OR outcome = $2)",
)
.bind(&query.source)
.bind(&query.outcome)
.fetch_one(&state.db)
.await?;
let errors: Vec<Value> = rows
.into_iter()
.map(|(id, at, source, item, kind, model, attempt, outcome, error, seconds, email)| {
json!({
"id": id, "created_at": rfc3339(at), "source": source, "item_id": item, "kind": kind, "model": model,
"attempt": attempt, "outcome": outcome, "error": error, "seconds": seconds, "user": email,
})
})
.collect();
Ok(Json(json!({ "errors": errors, "total": total, "last_day": last_day, "limit": limit, "offset": offset })))
}
+150
View File
@@ -0,0 +1,150 @@
//! Admin: the analyses and answers being written or waiting in the queue,
//! and restarting them with the models set now (e.g. after switching away
//! from a slow model). A restarted attempt that is still running in the
//! worker is dropped there and cannot overwrite the new one.
use std::collections::HashMap;
use analysis_prompt::Kind;
use axum::Json;
use axum::extract::State;
use axum_extra::extract::WithRejection;
use serde::Deserialize;
use serde_json::{Value, json};
use time::OffsetDateTime;
use uuid::Uuid;
use super::analyses::{find_analysis, target_hash, versions};
use super::{Body, St};
use crate::AppState;
use crate::auth::AdminUser;
use crate::error::ApiResult;
use crate::models::{Function, models_for};
use crate::views::{rfc3339, rfc3339_opt};
type JobRow = (
Uuid,
String,
String,
String,
Option<String>,
bool,
i32,
Option<OffsetDateTime>,
OffsetDateTime,
OffsetDateTime,
Option<String>,
);
const COLUMNS: &str =
"id, kind, status, model, fallback_model, on_fallback, attempts, started_at, created_at, next_attempt_at, error";
async fn open_jobs(state: &AppState) -> Result<Vec<(&'static str, JobRow)>, sqlx::Error> {
let analyses: Vec<JobRow> = sqlx::query_as(&format!(
"SELECT {COLUMNS} FROM analyses WHERE status IN ('pending', 'running') ORDER BY created_at"
))
.fetch_all(&state.db)
.await?;
let questions: Vec<JobRow> = sqlx::query_as(
"SELECT id, 'question', status, model, fallback_model, on_fallback, attempts, started_at, created_at, \
next_attempt_at, error \
FROM questions WHERE status IN ('pending', 'running') ORDER BY created_at",
)
.fetch_all(&state.db)
.await?;
Ok(analyses.into_iter().map(|r| ("analysis", r)).chain(questions.into_iter().map(|r| ("question", r))).collect())
}
pub async fn list(State(state): St, _admin: AdminUser) -> ApiResult<Json<Value>> {
Ok(Json(json!({ "jobs": views(&state).await? })))
}
async fn views(state: &AppState) -> Result<Vec<Value>, sqlx::Error> {
// The models a restart would use, per function.
let mut next: HashMap<&'static str, (String, Option<String>)> = HashMap::new();
for (function, kind) in
[(Function::Summary, Kind::Summary), (Function::Analysis, Kind::Natal), (Function::Question, Kind::Question)]
{
next.insert(function.key(), models_for(state, kind).await?);
}
Ok(open_jobs(state)
.await?
.into_iter()
.map(|(table, (id, kind, status, model, fallback, on_fallback, attempts, started, created, due, error))| {
let function = Function::of(Kind::parse(&kind).unwrap_or(Kind::Natal)).key();
let (next_model, next_fallback) = &next[function];
let active = if on_fallback { fallback.clone().unwrap_or_else(|| model.clone()) } else { model.clone() };
json!({
"id": id, "type": table, "kind": kind, "status": status, "model": active, "on_fallback": on_fallback,
"next_model": next_model, "next_fallback": next_fallback, "attempts": attempts,
"started_at": rfc3339_opt(started), "created_at": rfc3339(created), "next_attempt_at": rfc3339(due),
"error": error,
})
})
.collect())
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Restart {
/// The jobs to restart; all open ones when missing.
ids: Option<Vec<Uuid>>,
}
pub async fn restart(
State(state): St,
AdminUser(admin): AdminUser,
WithRejection(Json(req), _): Body<Restart>,
) -> ApiResult<Json<Value>> {
let mut restarted = 0;
for (table, (id, kind, ..)) in open_jobs(&state).await? {
if req.ids.as_ref().is_some_and(|ids| !ids.contains(&id)) {
continue;
}
let done = if table == "question" {
let language: String = sqlx::query_scalar("SELECT language FROM questions WHERE id = $1")
.bind(id)
.fetch_one(&state.db)
.await?;
let (model, fallback, prompt_version) = super::questions::model_and_version(&state, &language).await?;
sqlx::query(
"UPDATE questions SET status = 'pending', model = $2, fallback_model = $3, on_fallback = false, \
prompt_version = $4, attempts = 0, next_attempt_at = now(), started_at = NULL, error = NULL, \
requested_at = now() \
WHERE id = $1 AND status IN ('pending', 'running')",
)
.bind(id)
.bind(&model)
.bind(&fallback)
.bind(&prompt_version)
.execute(&state.db)
.await?
.rows_affected()
} else {
let Some(row) = find_analysis(&state.db, id).await? else { continue };
let Some(hash) = target_hash(&state.db, &row).await? else { continue };
let (model, fallback) = models_for(&state, Kind::parse(&kind).unwrap_or(Kind::Natal)).await?;
let (cache_key, prompt_version) = versions(&state, &kind, &hash, &model, &row.language).await?;
sqlx::query(
"UPDATE analyses SET status = 'pending', model = $2, fallback_model = $3, on_fallback = false, \
cache_key = $4, prompt_version = $5, attempts = 0, next_attempt_at = now(), started_at = NULL, \
error = NULL, requested_at = now() \
WHERE id = $1 AND status IN ('pending', 'running')",
)
.bind(id)
.bind(&model)
.bind(&fallback)
.bind(&cache_key)
.bind(&prompt_version)
.execute(&state.db)
.await?
.rows_affected()
};
restarted += done;
}
if restarted > 0 {
sqlx::query("SELECT pg_notify('analyses', '')").execute(&state.db).await?;
}
tracing::info!(admin = %admin.id(), restarted, "generations restarted");
Ok(Json(json!({ "restarted": restarted, "jobs": views(&state).await? })))
}
+5
View File
@@ -10,8 +10,10 @@ mod admin;
mod analyses; mod analyses;
mod auth; mod auth;
mod charts; mod charts;
mod errors;
mod forecasts; mod forecasts;
mod invitations; mod invitations;
mod jobs;
mod models; mod models;
mod payments; mod payments;
mod prompts; mod prompts;
@@ -116,6 +118,9 @@ pub fn router(state: Arc<AppState>) -> Router {
.route("/v1/admin/prompts/{kind}/{language}", axum::routing::put(prompts::save)) .route("/v1/admin/prompts/{kind}/{language}", axum::routing::put(prompts::save))
.route("/v1/admin/prompts/{kind}/{language}/preview", post(prompts::start_preview)) .route("/v1/admin/prompts/{kind}/{language}/preview", post(prompts::start_preview))
.route("/v1/admin/previews/{id}", get(prompts::get_preview)) .route("/v1/admin/previews/{id}", get(prompts::get_preview))
.route("/v1/admin/errors", get(errors::list))
.route("/v1/admin/jobs", get(jobs::list))
.route("/v1/admin/jobs/restart", post(jobs::restart))
.route("/v1/admin/models", get(models::list)) .route("/v1/admin/models", get(models::list))
.route("/v1/admin/models/catalog", get(models::catalog)) .route("/v1/admin/models/catalog", get(models::catalog))
.route("/v1/admin/models/{kind}", axum::routing::put(models::save)) .route("/v1/admin/models/{kind}", axum::routing::put(models::save))
+36 -23
View File
@@ -1,7 +1,7 @@
//! Admin: which model writes which kind of analysis, chosen from the models //! Admin: which model writes the analyses and forecasts, the answers to
//! OpenRouter offers. A change applies to analyses requested after it. //! questions and the short summary, chosen from the models OpenRouter offers.
//! A change applies to texts requested after it.
use analysis_prompt::Kind;
use axum::Json; use axum::Json;
use axum::extract::{Path, State}; use axum::extract::{Path, State};
use axum_extra::extract::WithRejection; use axum_extra::extract::WithRejection;
@@ -13,7 +13,7 @@ use super::{Body, St};
use crate::AppState; use crate::AppState;
use crate::auth::AdminUser; use crate::auth::AdminUser;
use crate::error::{ApiError, ApiResult}; use crate::error::{ApiError, ApiResult};
use crate::models::default_model; use crate::models::{Function, default_model};
use crate::views::rfc3339; use crate::views::rfc3339;
fn catalog_error(e: String) -> ApiError { fn catalog_error(e: String) -> ApiError {
@@ -23,27 +23,38 @@ fn catalog_error(e: String) -> ApiError {
} }
} }
async fn entry(state: &AppState, kind: Kind) -> Result<Value, sqlx::Error> { async fn entry(state: &AppState, function: Function) -> Result<Value, sqlx::Error> {
let chosen: Option<(String, OffsetDateTime, Option<String>)> = sqlx::query_as( let chosen: Option<(String, OffsetDateTime, Option<String>)> = sqlx::query_as(
"SELECT m.model, m.updated_at, u.email FROM model_settings m LEFT JOIN users u ON u.id = m.updated_by \ "SELECT m.model, m.updated_at, u.email FROM model_settings m LEFT JOIN users u ON u.id = m.updated_by \
WHERE m.kind = $1", WHERE m.kind = $1",
) )
.bind(kind.as_str()) .bind(function.key())
.fetch_optional(&state.db) .fetch_optional(&state.db)
.await?; .await?;
// Typical size of this kind of analysis, from the latest ones: for the cost estimate. // Typical size of the latest texts, for the cost estimate, and how often
let (input, output, samples): (Option<f64>, Option<f64>, i64) = sqlx::query_as( // the default model had to take over in the last 30 days.
let (table, kinds) = match function {
Function::Summary => ("analyses", "kind = 'summary'"),
Function::Analysis => ("analyses", "kind IN ('natal', 'synastry', 'forecast')"),
Function::Question => ("questions", "true"),
};
let (input, output, samples): (Option<f64>, Option<f64>, i64) = sqlx::query_as(&format!(
"SELECT avg(input_tokens)::float8, avg(output_tokens)::float8, count(*) FROM ( \ "SELECT avg(input_tokens)::float8, avg(output_tokens)::float8, count(*) FROM ( \
SELECT input_tokens, output_tokens FROM analyses \ SELECT input_tokens, output_tokens FROM {table} \
WHERE kind = $1 AND status = 'done' AND input_tokens IS NOT NULL AND output_tokens IS NOT NULL \ WHERE {kinds} AND status = 'done' AND input_tokens IS NOT NULL AND output_tokens IS NOT NULL \
ORDER BY completed_at DESC LIMIT 50) recent", ORDER BY completed_at DESC LIMIT 50) recent"
) ))
.bind(kind.as_str())
.fetch_one(&state.db) .fetch_one(&state.db)
.await?; .await?;
let default = default_model(state, kind); let (fallbacks, finished): (i64, i64) = sqlx::query_as(&format!(
"SELECT count(*) FILTER (WHERE on_fallback), count(*) FROM {table} \
WHERE {kinds} AND status IN ('done', 'failed') AND completed_at > now() - interval '30 days'"
))
.fetch_one(&state.db)
.await?;
let default = default_model(state, function);
Ok(json!({ Ok(json!({
"kind": kind.as_str(), "kind": function.key(),
"model": chosen.as_ref().map_or(default, |c| c.0.as_str()), "model": chosen.as_ref().map_or(default, |c| c.0.as_str()),
"default": default, "default": default,
"custom": chosen.is_some(), "custom": chosen.is_some(),
@@ -52,13 +63,15 @@ async fn entry(state: &AppState, kind: Kind) -> Result<Value, sqlx::Error> {
"avg_input_tokens": input, "avg_input_tokens": input,
"avg_output_tokens": output, "avg_output_tokens": output,
"samples": samples, "samples": samples,
"fallbacks_30d": fallbacks,
"finished_30d": finished,
})) }))
} }
pub async fn list(State(state): St, _admin: AdminUser) -> ApiResult<Json<Value>> { pub async fn list(State(state): St, _admin: AdminUser) -> ApiResult<Json<Value>> {
let mut functions = Vec::new(); let mut functions = Vec::new();
for kind in Kind::ANALYSES { for function in Function::ALL {
functions.push(entry(&state, kind).await?); functions.push(entry(&state, function).await?);
} }
Ok(Json(json!({ "functions": functions }))) Ok(Json(json!({ "functions": functions })))
} }
@@ -81,9 +94,9 @@ pub async fn save(
WithRejection(Path(kind), _): WithRejection<Path<String>, ApiError>, WithRejection(Path(kind), _): WithRejection<Path<String>, ApiError>,
WithRejection(Json(req), _): Body<SaveModel>, WithRejection(Json(req), _): Body<SaveModel>,
) -> ApiResult<Json<Value>> { ) -> ApiResult<Json<Value>> {
let kind = Kind::parse(&kind).ok_or(ApiError::NotFound("function"))?; let function = Function::parse(&kind).ok_or(ApiError::NotFound("function"))?;
match req.model.as_deref().map(str::trim) { match req.model.as_deref().map(str::trim) {
Some(model) if model != default_model(&state, kind) => { Some(model) if model != default_model(&state, function) => {
let models = state.models.models().await.map_err(catalog_error)?; let models = state.models.models().await.map_err(catalog_error)?;
if !models.iter().any(|m| m.id == model) { if !models.iter().any(|m| m.id == model) {
return Err(ApiError::BadRequest(format!( return Err(ApiError::BadRequest(format!(
@@ -95,16 +108,16 @@ pub async fn save(
ON CONFLICT (kind) DO UPDATE SET model = EXCLUDED.model, updated_by = EXCLUDED.updated_by, \ ON CONFLICT (kind) DO UPDATE SET model = EXCLUDED.model, updated_by = EXCLUDED.updated_by, \
updated_at = now()", updated_at = now()",
) )
.bind(kind.as_str()) .bind(function.key())
.bind(model) .bind(model)
.bind(admin.id()) .bind(admin.id())
.execute(&state.db) .execute(&state.db)
.await?; .await?;
} }
_ => { _ => {
sqlx::query("DELETE FROM model_settings WHERE kind = $1").bind(kind.as_str()).execute(&state.db).await?; sqlx::query("DELETE FROM model_settings WHERE kind = $1").bind(function.key()).execute(&state.db).await?;
} }
} }
tracing::info!(admin = %admin.id(), kind = kind.as_str(), "analysis model changed"); tracing::info!(admin = %admin.id(), function = function.key(), "model changed");
Ok(Json(entry(&state, kind).await?)) Ok(Json(entry(&state, function).await?))
} }
+36 -25
View File
@@ -1,7 +1,8 @@
//! Questions about a finished detailed analysis: a natal chart analysis or a //! Questions about a finished detailed analysis: a natal chart analysis, a
//! yearly forecast. Each question uses a `question` unit (admins ask for //! relationship (synastry) analysis or a yearly forecast. On a comparison both
//! free). The worker answers with the model that wrote the analysis, seeing //! people see the questions and answers. Each question uses a `question` unit (admins ask for
//! the chart data, the analysis and the earlier questions about it. //! free). The worker answers with the question model of the admin page,
//! seeing the chart data, the analysis and the earlier questions about it.
use analysis_prompt::{Kind, QUESTION_MAX_CHARS}; use analysis_prompt::{Kind, QUESTION_MAX_CHARS};
use axum::Json; use axum::Json;
@@ -23,7 +24,7 @@ use crate::error::{ApiError, ApiResult};
use crate::views::{rfc3339, rfc3339_opt}; use crate::views::{rfc3339, rfc3339_opt};
/// The analyses that can be asked about. /// The analyses that can be asked about.
const ASKABLE: [&str; 2] = ["natal", "forecast"]; const ASKABLE: [&str; 3] = ["natal", "synastry", "forecast"];
/// A safety cap per analysis. /// A safety cap per analysis.
const MAX_PER_ANALYSIS: i64 = 100; const MAX_PER_ANALYSIS: i64 = 100;
@@ -39,10 +40,13 @@ pub struct QuestionRow {
pub credits_charged: i32, pub credits_charged: i32,
pub created_at: OffsetDateTime, pub created_at: OffsetDateTime,
pub completed_at: Option<OffsetDateTime>, pub completed_at: Option<OffsetDateTime>,
pub attempts: i32,
pub next_attempt_at: OffsetDateTime,
pub requested_at: OffsetDateTime,
} }
const QUESTION_COLUMNS: &str = const QUESTION_COLUMNS: &str = "id, analysis_id, question, status, answer, error, credits_charged, created_at, \
"id, analysis_id, question, status, answer, error, credits_charged, created_at, completed_at"; completed_at, attempts, next_attempt_at, requested_at";
impl QuestionRow { impl QuestionRow {
pub fn view(&self) -> Value { pub fn view(&self) -> Value {
@@ -56,6 +60,9 @@ impl QuestionRow {
"credits_charged": self.credits_charged, "credits_charged": self.credits_charged,
"created_at": rfc3339(self.created_at), "created_at": rfc3339(self.created_at),
"completed_at": rfc3339_opt(self.completed_at), "completed_at": rfc3339_opt(self.completed_at),
"attempts": self.attempts,
"next_attempt_at": rfc3339(self.next_attempt_at),
"requested_at": rfc3339(self.requested_at),
}) })
} }
} }
@@ -64,21 +71,19 @@ async fn find_question(db: &PgPool, id: Uuid) -> Result<Option<QuestionRow>, sql
sqlx::query_as(&format!("SELECT {QUESTION_COLUMNS} FROM questions WHERE id = $1")).bind(id).fetch_optional(db).await sqlx::query_as(&format!("SELECT {QUESTION_COLUMNS} FROM questions WHERE id = $1")).bind(id).fetch_optional(db).await
} }
/// The model of the analysis (questions are answered by the same model) and /// The question model (and its fallback) and the version of the question
/// the prompt version of the question prompt in force. /// prompt in force.
async fn model_and_version( pub(super) async fn model_and_version(
state: &crate::AppState, state: &crate::AppState,
analysis_id: Uuid,
language: &str, language: &str,
) -> Result<(String, String), sqlx::Error> { ) -> Result<(String, Option<String>, String), sqlx::Error> {
let model: String = let (model, fallback) = crate::models::models_for(state, Kind::Question).await?;
sqlx::query_scalar("SELECT model FROM analyses WHERE id = $1").bind(analysis_id).fetch_one(&state.db).await?;
let base = &state.config.prompt_version; let base = &state.config.prompt_version;
let version = match super::prompts::current_revision(&state.db, Kind::Question.as_str(), language).await? { let version = match super::prompts::current_revision(&state.db, Kind::Question.as_str(), language).await? {
Some(id) => format!("{base}-r{id}"), Some(id) => format!("{base}-r{id}"),
None => base.clone(), None => base.clone(),
}; };
Ok((model, version)) Ok((model, fallback, version))
} }
pub async fn list(State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath) -> ApiResult<Json<Value>> { pub async fn list(State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath) -> ApiResult<Json<Value>> {
@@ -111,7 +116,7 @@ pub async fn ask(
return Err(ApiError::NotFound("analysis")); return Err(ApiError::NotFound("analysis"));
} }
if !ASKABLE.contains(&analysis.kind.as_str()) { if !ASKABLE.contains(&analysis.kind.as_str()) {
return Err(ApiError::BadRequest("questions can be asked about a natal analysis or a forecast".into())); return Err(ApiError::BadRequest("questions can be asked about a detailed analysis or a forecast".into()));
} }
if analysis.status != "done" { if analysis.status != "done" {
return Err(ApiError::conflict("analysis_not_ready", "Az elemzés még nem készült el.")); return Err(ApiError::conflict("analysis_not_ready", "Az elemzés még nem készült el."));
@@ -141,12 +146,13 @@ pub async fn ask(
} }
let admin = auth.user.has_role(Role::Admin); let admin = auth.user.has_role(Role::Admin);
let (model, prompt_version) = model_and_version(&state, id, &analysis.language).await?; let (model, fallback, prompt_version) = model_and_version(&state, &analysis.language).await?;
let question_id = Uuid::now_v7(); let question_id = Uuid::now_v7();
let mut tx = state.db.begin().await?; let mut tx = state.db.begin().await?;
let row: QuestionRow = sqlx::query_as(&format!( let row: QuestionRow = sqlx::query_as(&format!(
"INSERT INTO questions (id, analysis_id, asked_by, question, language, model, prompt_version, credits_charged) \ "INSERT INTO questions (id, analysis_id, asked_by, question, language, model, prompt_version, credits_charged, \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING {QUESTION_COLUMNS}" fallback_model) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING {QUESTION_COLUMNS}"
)) ))
.bind(question_id) .bind(question_id)
.bind(id) .bind(id)
@@ -156,6 +162,7 @@ pub async fn ask(
.bind(&model) .bind(&model)
.bind(&prompt_version) .bind(&prompt_version)
.bind(i32::from(!admin)) .bind(i32::from(!admin))
.bind(&fallback)
.fetch_one(&mut *tx) .fetch_one(&mut *tx)
.await?; .await?;
if !admin { if !admin {
@@ -175,26 +182,30 @@ pub async fn get_one(State(state): St, auth: AuthUser, WithRejection(Path(id), _
} }
/// Admin: answers a question again in place, with the current question prompt /// Admin: answers a question again in place, with the current question prompt
/// and the analysis's model. Nobody is charged. /// and model. Nobody is charged.
pub async fn regenerate( pub async fn regenerate(
State(state): St, State(state): St,
_admin: AdminUser, _admin: AdminUser,
WithRejection(Path(id), _): IdPath, WithRejection(Path(id), _): IdPath,
) -> ApiResult<Json<Value>> { ) -> ApiResult<Json<Value>> {
let row = find_question(&state.db, id).await?.ok_or(ApiError::NotFound("question"))?; let language: String = sqlx::query_scalar("SELECT language FROM questions WHERE id = $1")
let language: String = .bind(id)
sqlx::query_scalar("SELECT language FROM questions WHERE id = $1").bind(id).fetch_one(&state.db).await?; .fetch_optional(&state.db)
let (model, prompt_version) = model_and_version(&state, row.analysis_id, &language).await?; .await?
.ok_or(ApiError::NotFound("question"))?;
let (model, fallback, prompt_version) = model_and_version(&state, &language).await?;
let mut tx = state.db.begin().await?; let mut tx = state.db.begin().await?;
let row: QuestionRow = sqlx::query_as(&format!( let row: QuestionRow = sqlx::query_as(&format!(
"UPDATE questions SET status = 'pending', answer = NULL, error = NULL, model_used = NULL, input_tokens = NULL, \ "UPDATE questions SET status = 'pending', answer = NULL, error = NULL, model_used = NULL, input_tokens = NULL, \
output_tokens = NULL, cost_usd = NULL, attempts = 0, next_attempt_at = now(), started_at = NULL, \ output_tokens = NULL, cost_usd = NULL, attempts = 0, next_attempt_at = now(), started_at = NULL, \
completed_at = NULL, model = $2, prompt_version = $3 \ completed_at = NULL, model = $2, prompt_version = $3, fallback_model = $4, on_fallback = false, \
requested_at = now() \
WHERE id = $1 AND status IN ('done', 'failed') RETURNING {QUESTION_COLUMNS}" WHERE id = $1 AND status IN ('done', 'failed') RETURNING {QUESTION_COLUMNS}"
)) ))
.bind(id) .bind(id)
.bind(&model) .bind(&model)
.bind(&prompt_version) .bind(&prompt_version)
.bind(&fallback)
.fetch_optional(&mut *tx) .fetch_optional(&mut *tx)
.await? .await?
.ok_or_else(|| ApiError::conflict("question_in_progress", "Erre a kérdésre éppen készül a válasz."))?; .ok_or_else(|| ApiError::conflict("question_in_progress", "Erre a kérdésre éppen készül a válasz."))?;
+131 -20
View File
@@ -761,6 +761,18 @@ async fn synastry_analyses_use_synastry_units(pool: PgPool) {
app.get("/v1/credits", Some(&anna)).await.body["balance"], app.get("/v1/credits", Some(&anna)).await.body["balance"],
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0}) json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0})
); );
// questions can be asked about the relationship analysis too
let analysis = r.body["id"].as_str().unwrap().to_string();
sqlx::query("UPDATE analyses SET status = 'done', content = '{\"title\": \"Kettőtök\"}' WHERE kind = 'synastry'")
.execute(&app.pool)
.await
.unwrap();
grant_units(&app, &anna, "question", 1).await;
let r = app
.post(&format!("/v1/analyses/{analysis}/questions"), Some(&anna), json!({"question": "Mire figyeljünk?"}))
.await;
assert_eq!((r.status, r.body["status"].as_str()), (StatusCode::ACCEPTED, Some("pending")), "{}", r.body);
} }
#[sqlx::test(migrator = "db::MIGRATOR")] #[sqlx::test(migrator = "db::MIGRATOR")]
@@ -1499,9 +1511,8 @@ async fn admins_choose_the_model_of_each_function(pool: PgPool) {
models, models,
[ [
("summary", "anthropic/claude-summary", false), ("summary", "anthropic/claude-summary", false),
("natal", "anthropic/claude-test", false), ("analysis", "anthropic/claude-test", false),
("synastry", "anthropic/claude-test", false), ("question", "anthropic/claude-test", false),
("forecast", "anthropic/claude-test", false),
] ]
); );
@@ -1516,9 +1527,9 @@ async fn admins_choose_the_model_of_each_function(pool: PgPool) {
let (app, admin) = (&app, &admin); let (app, admin) = (&app, &admin);
async move { app.call("PUT", &uri, Some(admin), Some(json!({ "model": model }))).await } async move { app.call("PUT", &uri, Some(admin), Some(json!({ "model": model }))).await }
}; };
assert_eq!(put("natal", json!("vendor/plain")).await.status, StatusCode::BAD_REQUEST); assert_eq!(put("analysis", json!("vendor/plain")).await.status, StatusCode::BAD_REQUEST);
assert_eq!(put("composite", json!("openai/gpt-x")).await.status, StatusCode::NOT_FOUND); assert_eq!(put("natal", json!("openai/gpt-x")).await.status, StatusCode::NOT_FOUND);
let r = put("natal", json!("openai/gpt-x")).await; let r = put("analysis", json!("openai/gpt-x")).await;
assert_eq!((r.status, &r.body["model"], &r.body["custom"]), (StatusCode::OK, &json!("openai/gpt-x"), &json!(true))); assert_eq!((r.status, &r.body["model"], &r.body["custom"]), (StatusCode::OK, &json!("openai/gpt-x"), &json!(true)));
// new analyses use it; regenerating writes it again in place with the current model // new analyses use it; regenerating writes it again in place with the current model
@@ -1527,9 +1538,13 @@ async fn admins_choose_the_model_of_each_function(pool: PgPool) {
let r = app.post(&format!("/v1/charts/{chart_id}/analyses"), Some(&admin), json!({})).await; let r = app.post(&format!("/v1/charts/{chart_id}/analyses"), Some(&admin), json!({})).await;
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body); assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
let id = r.body["id"].as_str().unwrap().to_string(); let id = r.body["id"].as_str().unwrap().to_string();
let model = let model = || {
|| sqlx::query_scalar::<_, String>("SELECT model FROM analyses WHERE id = $1::uuid").bind(&id).fetch_one(&pool); sqlx::query_as::<_, (String, Option<String>)>("SELECT model, fallback_model FROM analyses WHERE id = $1::uuid")
assert_eq!(model().await.unwrap(), "openai/gpt-x"); .bind(&id)
.fetch_one(&pool)
};
// the default model is kept as the fallback
assert_eq!(model().await.unwrap(), ("openai/gpt-x".into(), Some("anthropic/claude-test".into())));
let regenerate_uri = format!("/v1/admin/analyses/{id}/regenerate"); let regenerate_uri = format!("/v1/admin/analyses/{id}/regenerate");
let regenerate = || app.post(&regenerate_uri, Some(&admin), json!({})); let regenerate = || app.post(&regenerate_uri, Some(&admin), json!({}));
assert_eq!(regenerate().await.status, StatusCode::CONFLICT); // still being written assert_eq!(regenerate().await.status, StatusCode::CONFLICT); // still being written
@@ -1537,7 +1552,7 @@ async fn admins_choose_the_model_of_each_function(pool: PgPool) {
.execute(&pool) .execute(&pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(put("natal", Value::Null).await.body["model"], "anthropic/claude-test"); assert_eq!(put("analysis", Value::Null).await.body["model"], "anthropic/claude-test");
assert_eq!( assert_eq!(
app.post(&format!("/v1/admin/analyses/{id}/regenerate"), Some(&anna), json!({})).await.status, app.post(&format!("/v1/admin/analyses/{id}/regenerate"), Some(&anna), json!({})).await.status,
StatusCode::FORBIDDEN StatusCode::FORBIDDEN
@@ -1548,7 +1563,7 @@ async fn admins_choose_the_model_of_each_function(pool: PgPool) {
(StatusCode::OK, Some("pending"), &Value::Null) (StatusCode::OK, Some("pending"), &Value::Null)
); );
assert_eq!(r.body["id"].as_str(), Some(id.as_str())); assert_eq!(r.body["id"].as_str(), Some(id.as_str()));
assert_eq!(model().await.unwrap(), "anthropic/claude-test"); assert_eq!(model().await.unwrap(), ("anthropic/claude-test".into(), None));
let r = app.get(&format!("/v1/charts/{chart_id}/analyses"), Some(&admin)).await; let r = app.get(&format!("/v1/charts/{chart_id}/analyses"), Some(&admin)).await;
assert_eq!(r.body["analyses"].as_array().unwrap().iter().filter(|a| a["kind"] == "natal").count(), 1); assert_eq!(r.body["analyses"].as_array().unwrap().iter().filter(|a| a["kind"] == "natal").count(), 1);
} }
@@ -1587,15 +1602,14 @@ async fn questions_about_an_analysis_use_question_units(pool: PgPool) {
assert!(r.body.get("model").is_none(), "the model is a secret"); assert!(r.body.get("model").is_none(), "the model is a secret");
assert_eq!(left().await, json!(2)); assert_eq!(left().await, json!(2));
let question = r.body["id"].as_str().unwrap().to_string(); let question = r.body["id"].as_str().unwrap().to_string();
// the same model as the analysis answers // the question model of the admin page answers (here the default)
let (q_model, a_model): (String, String) = sqlx::query_as( let (q_model, fallback): (String, Option<String>) =
"SELECT q.model, a.model FROM questions q JOIN analyses a ON a.id = q.analysis_id WHERE q.id = $1::uuid", sqlx::query_as("SELECT model, fallback_model FROM questions WHERE id = $1::uuid")
) .bind(&question)
.bind(&question) .fetch_one(&pool)
.fetch_one(&pool) .await
.await .unwrap();
.unwrap(); assert_eq!((q_model.as_str(), fallback), ("anthropic/claude-test", None));
assert_eq!(q_model, a_model);
// one at a time // one at a time
let r = ask(&anna, "És a munkában?").await; let r = ask(&anna, "És a munkában?").await;
assert_eq!((r.status, r.body["error"]["code"].as_str()), (StatusCode::CONFLICT, Some("question_in_progress"))); assert_eq!((r.status, r.body["error"]["code"].as_str()), (StatusCode::CONFLICT, Some("question_in_progress")));
@@ -1637,3 +1651,100 @@ async fn questions_about_an_analysis_use_question_units(pool: PgPool) {
); );
assert_eq!(left().await, json!(5), "regenerating charges nobody"); assert_eq!(left().await, json!(5), "regenerating charges nobody");
} }
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn admins_restart_open_generations_with_the_current_models(pool: PgPool) {
let app = TestApp::new(pool.clone()).await;
let admin = app.register("admin@example.com", "Admin").await;
let anna = app.register("anna@example.com", "Anna").await;
assert_eq!(app.get("/v1/admin/jobs", Some(&anna)).await.status, StatusCode::FORBIDDEN);
let put = |model: &str| {
let (app, admin, body) = (&app, &admin, json!({ "model": model }));
async move { app.call("PUT", "/v1/admin/models/analysis", Some(admin), Some(body)).await }
};
assert_eq!(put("openai/gpt-x").await.status, StatusCode::OK);
let chart = app.chart(&admin, "Saját", true, "1990-03-15").await;
let chart_id = chart["id"].as_str().unwrap();
let r = app.post(&format!("/v1/charts/{chart_id}/analyses"), Some(&admin), json!({})).await;
let id = r.body["id"].as_str().unwrap().to_string();
// the worker is writing it with the chosen model
sqlx::query("UPDATE analyses SET status = 'running', started_at = now(), attempts = 1 WHERE id = $1::uuid")
.bind(&id)
.execute(&pool)
.await
.unwrap();
// after switching back to the default, the list shows what a restart would use
assert_eq!(
app.call("PUT", "/v1/admin/models/analysis", Some(&admin), Some(json!({"model": null}))).await.status,
StatusCode::OK
);
let jobs = app.get("/v1/admin/jobs", Some(&admin)).await.body["jobs"].clone();
let job = jobs.as_array().unwrap().iter().find(|j| j["id"] == json!(id)).expect("the analysis is listed");
assert_eq!(
(&job["status"], &job["model"], &job["next_model"]),
(&json!("running"), &json!("openai/gpt-x"), &json!("anthropic/claude-test"))
);
let r = app.post("/v1/admin/jobs/restart", Some(&admin), json!({ "ids": [id] })).await;
assert_eq!((r.status, &r.body["restarted"]), (StatusCode::OK, &json!(1)), "{}", r.body);
let (status, model, fallback, started, attempts): (
String,
String,
Option<String>,
Option<time::OffsetDateTime>,
i32,
) = sqlx::query_as("SELECT status, model, fallback_model, started_at, attempts FROM analyses WHERE id = $1::uuid")
.bind(&id)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(
(status.as_str(), model.as_str(), fallback, started, attempts),
("pending", "anthropic/claude-test", None, None, 0)
);
// finished ones are left alone
sqlx::query("UPDATE analyses SET status = 'done', content = '{}'").execute(&pool).await.unwrap();
assert_eq!(app.post("/v1/admin/jobs/restart", Some(&admin), json!({})).await.body["restarted"], 0);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn admins_read_the_error_log(pool: PgPool) {
let app = TestApp::new(pool.clone()).await;
let admin = app.register("admin@example.com", "Admin").await;
let anna = app.register("anna@example.com", "Anna").await;
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
let chart_id = chart["id"].as_str().unwrap();
let analysis: uuid::Uuid = sqlx::query_scalar("SELECT id FROM analyses WHERE chart_id = $1::uuid")
.bind(chart_id)
.fetch_one(&pool)
.await
.unwrap();
for (attempt, outcome) in [(1, "retry"), (2, "failed")] {
sqlx::query(
"INSERT INTO generation_errors (source, item_id, kind, model, attempt, outcome, error, seconds) \
VALUES ('analysis', $1, 'summary', 'cheap/model', $2, $3, 'answer cut off at the token limit', 301)",
)
.bind(analysis)
.bind(attempt)
.bind(outcome)
.execute(&pool)
.await
.unwrap();
}
assert_eq!(app.get("/v1/admin/errors", Some(&anna)).await.status, StatusCode::FORBIDDEN);
let r = app.get("/v1/admin/errors", Some(&admin)).await;
assert_eq!((r.status, &r.body["total"], &r.body["last_day"]), (StatusCode::OK, &json!(2), &json!(2)), "{}", r.body);
let first = &r.body["errors"][0];
assert_eq!(
(&first["outcome"], &first["attempt"], &first["user"], &first["seconds"]),
(&json!("failed"), &json!(2), &json!("anna@example.com"), &json!(301))
);
let r = app.get("/v1/admin/errors?outcome=retry", Some(&admin)).await;
assert_eq!(r.body["errors"].as_array().unwrap().len(), 1);
assert_eq!(app.get("/v1/admin/errors?source=question", Some(&admin)).await.body["total"], 0);
// users see the attempts of their analysis (the page says it is tried again)
let r = app.get(&format!("/v1/analyses/{analysis}"), Some(&anna)).await;
assert_eq!(r.body["attempts"], 0);
assert!(r.body["next_attempt_at"].is_string());
}
@@ -0,0 +1,155 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { api, errorText } from "@/lib/api";
import { formatDate } from "@/lib/astro";
import type { GenerationError } from "@/lib/types";
const PAGE = 100;
const SOURCES: Record<string, string> = { analysis: "Elemzés", question: "Kérdés-válasz", preview: "Prompt-próba" };
const KINDS: Record<string, string> = {
summary: "összefoglaló",
natal: "képletelemzés",
synastry: "kapcsolati",
forecast: "előrejelzés",
question: "válasz",
};
const OUTCOMES: Record<GenerationError["outcome"], { label: string; tone: string }> = {
retry: { label: "újrapróbálva", tone: "neutral" },
fallback: { label: "tartalék modell vette át", tone: "neutral" },
failed: { label: "végleg sikertelen", tone: "tense" },
};
interface Page {
errors: GenerationError[];
total: number;
last_day: number;
}
/** The admin error log with filters and paging. */
export function ErrorLog() {
const [source, setSource] = useState("");
const [outcome, setOutcome] = useState("");
const [errors, setErrors] = useState<GenerationError[] | null>(null);
const [total, setTotal] = useState(0);
const [lastDay, setLastDay] = useState(0);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const load = useCallback(
async (offset: number) => {
setBusy(true);
setError(null);
const params = new URLSearchParams({ limit: String(PAGE), offset: String(offset) });
if (source) params.set("source", source);
if (outcome) params.set("outcome", outcome);
try {
const page = await api<Page>(`/admin/errors?${params}`);
setErrors((all) => (offset === 0 ? page.errors : [...(all ?? []), ...page.errors]));
setTotal(page.total);
setLastDay(page.last_day);
} catch (e) {
setError(errorText(e));
} finally {
setBusy(false);
}
},
[source, outcome],
);
useEffect(() => {
load(0);
}, [load]);
return (
<section className="card" style={{ gap: 12 }}>
<div className="row" style={{ gap: 12, flexWrap: "wrap", alignItems: "flex-end" }}>
<div className="field">
<label htmlFor="error-source">Mi</label>
<select id="error-source" className="inp" value={source} onChange={(e) => setSource(e.target.value)}>
<option value="">mind</option>
{Object.entries(SOURCES).map(([v, l]) => (
<option key={v} value={v}>
{l}
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="error-outcome">Következmény</label>
<select id="error-outcome" className="inp" value={outcome} onChange={(e) => setOutcome(e.target.value)}>
<option value="">mind</option>
{Object.entries(OUTCOMES).map(([v, o]) => (
<option key={v} value={v}>
{o.label}
</option>
))}
</select>
</div>
<p className="hint" style={{ margin: 0 }}>
{total} bejegyzés, ebből {lastDay} az elmúlt 24 órában
</p>
<button type="button" className="btn btn-sm btn-ghost" onClick={() => load(0)} disabled={busy}>
Frissítés
</button>
</div>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{errors === null ? (
<p className="muted">Betöltés…</p>
) : errors.length === 0 ? (
<p className="muted">Nincs hiba. 🎉</p>
) : (
<div className="table-wrap">
<table className="error-log">
<thead>
<tr>
<th>Időpont</th>
<th>Mi</th>
<th>Modell</th>
<th>Kísérlet</th>
<th>Következmény</th>
<th>Hiba</th>
</tr>
</thead>
<tbody>
{errors.map((e) => (
<tr key={e.id}>
<td className="num">{formatDate(e.created_at, true)}</td>
<td>
{SOURCES[e.source] ?? e.source}
{e.source !== "question" && ` (${KINDS[e.kind] ?? e.kind})`}
{e.user && <span className="hint">{e.user}</span>}
<code className="hint" title={e.item_id}>
{e.item_id.slice(0, 8)}
</code>
</td>
<td>
<code>{e.model}</code>
</td>
<td className="num">
{e.attempt}.{e.seconds !== null && <span className="hint">{e.seconds} mp</span>}
</td>
<td>
<span className={`outcome tone-${OUTCOMES[e.outcome].tone}`}>{OUTCOMES[e.outcome].label}</span>
</td>
<td className="error-text">{e.error}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{errors && errors.length < total && (
<button type="button" className="btn btn-sm" onClick={() => load(errors.length)} disabled={busy}>
{busy ? "Betöltés…" : "Régebbiek"}
</button>
)}
</section>
);
}
@@ -0,0 +1,27 @@
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { AdminNav } from "@/components/AdminNav";
import { requireMe } from "@/lib/server";
import { ErrorLog } from "./ErrorLog";
export const metadata: Metadata = { title: "Hibanapló" };
export default async function AdminErrorsPage() {
const me = await requireMe("/admin/hibak");
if (!me.user.roles.includes("admin")) notFound();
return (
<main className="page" style={{ maxWidth: 1180 }}>
<AdminNav current="/admin/hibak" />
<div className="page-head">
<h1>Hibanapló</h1>
<p className="muted">
Az elemzések, válaszok és prompt-próbák minden sikertelen próbálkozása, a legújabb elöl. A worker írja; 90 napig
őrizzük.
</p>
</div>
<ErrorLog />
</main>
);
}
@@ -4,13 +4,17 @@ import { useEffect, useMemo, useState } from "react";
import { api, errorText } from "@/lib/api"; import { api, errorText } from "@/lib/api";
import { formatDate } from "@/lib/astro"; import { formatDate } from "@/lib/astro";
import type { CatalogModel, ModelFunction, PromptKind } from "@/lib/types"; import type { CatalogModel, ModelFunction } from "@/lib/types";
const LABELS: Record<Exclude<PromptKind, "question">, { title: string; about: string }> = { import { RunningJobs } from "./RunningJobs";
const LABELS: Record<ModelFunction["kind"], { 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." }, 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." }, analysis: {
synastry: { title: "Kapcsolati elemzés", about: "Fizetős, öt fejezet két képletről." }, title: "Elemzések és éves előrejelzés",
forecast: { title: "Éves előrejelzés", about: "Fizetős, öt fejezet, a leghosszabb adatsorral." }, about: "Fizetős szövegek: részletes képletelemzés, kapcsolati elemzés és éves előrejelzés.",
},
question: { title: "Kérdés-válasz", about: "A kész elemzésekre és előrejelzésekre feltett kérdések válasza (legfeljebb 250 szó)." },
}; };
/** How many models the picker lists at once (narrow it with the search). */ /** How many models the picker lists at once (narrow it with the search). */
@@ -58,11 +62,13 @@ export function AdminModels() {
</p> </p>
)} )}
<p className="hint"> <p className="hint">
Az elemzésekre feltett kérdésekre mindig ugyanaz a modell válaszol, amelyik az adott elemzést írta. Tartalék: ha a választott modell kétszer hibára fut (vagy egyszer végleges hibával), az alapértelmezett modell írja
meg a szöveget. Csak ha az is elbukik, kapja vissza a felhasználó az egységét.
</p> </p>
{functions.map((f) => ( {functions.map((f) => (
<FunctionModel key={f.kind} fn={f} catalog={catalog} onSaved={replace} /> <FunctionModel key={f.kind} fn={f} catalog={catalog} onSaved={replace} />
))} ))}
<RunningJobs />
</div> </div>
); );
} }
@@ -82,7 +88,7 @@ function FunctionModel({
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const current = catalog?.find((m) => m.id === fn.model); const current = catalog?.find((m) => m.id === fn.model);
const cost = estimate(fn, current); const cost = estimate(fn, current);
const label = LABELS[fn.kind as keyof typeof LABELS]; const label = LABELS[fn.kind];
const searchId = `model-search-${fn.kind}`; const searchId = `model-search-${fn.kind}`;
const matches = useMemo(() => { const matches = useMemo(() => {
@@ -134,6 +140,11 @@ function FunctionModel({
? `Módosítva: ${fn.updated_at ? formatDate(fn.updated_at, true) : ""}${fn.updated_by ? `, ${fn.updated_by}` : ""}` ? `Módosítva: ${fn.updated_at ? formatDate(fn.updated_at, true) : ""}${fn.updated_by ? `, ${fn.updated_by}` : ""}`
: "Alapértelmezett"} : "Alapértelmezett"}
</span> </span>
{fn.custom && (
<span className={fn.fallbacks_30d > 0 ? "hint warn" : "hint"}>
Tartalék: {fn.default} · az elmúlt 30 napban {fn.fallbacks_30d} / {fn.finished_30d} szövegnél kellett
</span>
)}
</div> </div>
</div> </div>
<div className="form-actions"> <div className="form-actions">
@@ -0,0 +1,138 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { api, errorText } from "@/lib/api";
import type { AdminJob } from "@/lib/types";
const KINDS: Record<AdminJob["kind"], string> = {
summary: "Összefoglaló",
natal: "Képletelemzés",
synastry: "Kapcsolati elemzés",
forecast: "Éves előrejelzés",
question: "Kérdés-válasz",
};
const REFRESH_MS = 10_000;
function since(iso: string): string {
const minutes = Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 60_000));
return minutes < 1 ? "most" : `${minutes} perce`;
}
/**
* Analyses and answers being written or waiting. A restart starts them again
* with the models set now; an attempt still running is dropped by the worker.
*/
export function RunningJobs() {
const [jobs, setJobs] = useState<AdminJob[] | null>(null);
const [busy, setBusy] = useState(false);
const [asking, setAsking] = useState(false);
const [message, setMessage] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const load = useCallback(() => {
api<{ jobs: AdminJob[] }>("/admin/jobs")
.then((d) => setJobs(d.jobs))
.catch((e) => setError(errorText(e)));
}, []);
useEffect(() => {
load();
const timer = setInterval(load, REFRESH_MS);
return () => clearInterval(timer);
}, [load]);
async function restart(ids?: string[]) {
setBusy(true);
setError(null);
setMessage(null);
try {
const r = await api<{ restarted: number; jobs: AdminJob[] }>("/admin/jobs/restart", { body: ids ? { ids } : {} });
setJobs(r.jobs);
setMessage(r.restarted > 0 ? `${r.restarted} generálás újraindítva.` : "Nem volt mit újraindítani.");
setAsking(false);
} catch (e) {
setError(errorText(e));
} finally {
setBusy(false);
}
}
const changed = (jobs ?? []).filter((j) => j.model !== j.next_model).length;
return (
<section className="card model-card" aria-labelledby="jobs-title">
<div className="model-head">
<div>
<h2 id="jobs-title">Futó generálások</h2>
<p className="hint">
Az éppen készülő és a sorban álló elemzések és válaszok. Újraindításkor mindegyik azzal a modellel indul újra,
ami most be van állítva; a még futó régi kérést a worker eldobja. Senkitől nem von le újabb egységet.
</p>
</div>
{jobs && jobs.length > 0 && (
<div className="regenerate">
{asking ? (
<>
<span className="hint">
{jobs.length} generálás indul újra{changed > 0 ? `, ebből ${changed} másik modellel` : ""}.
</span>
<button type="button" className="btn btn-sm" onClick={() => restart()} disabled={busy}>
{busy ? "Újraindítás…" : "Igen, mind"}
</button>
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setAsking(false)} disabled={busy}>
Mégse
</button>
</>
) : (
<button type="button" className="btn btn-sm" onClick={() => setAsking(true)}>
↻ Mind újraindítása ({jobs.length})
</button>
)}
</div>
)}
</div>
{message && (
<p className="msg" role="status">
{message}
</p>
)}
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{jobs === null ? (
<p className="muted">Betöltés…</p>
) : jobs.length === 0 ? (
<p className="muted">Most semmi nem készül és semmi nem vár a sorban.</p>
) : (
<ul className="model-options">
{jobs.map((j) => (
<li key={j.id} className="job-row">
<span>
<strong>{KINDS[j.kind]}</strong>
<span className="hint">
{j.status === "running" && j.started_at
? `fut, ${since(j.started_at)} indult`
: `vár (kérve ${since(j.created_at)})`}
{j.attempts > 0 ? ` · ${j.attempts}. kísérlet` : ""}
{j.on_fallback ? " · tartalék modellel" : ""}
</span>
{j.error && <span className="hint">utolsó hiba: {j.error}</span>}
</span>
<span className="model-price">
<code>{j.model}</code>
{j.model !== j.next_model ? <strong>→ {j.next_model}</strong> : <span>újraindítva is ez</span>}
</span>
<button type="button" className="btn btn-sm btn-ghost" onClick={() => restart([j.id])} disabled={busy}>
↻
<span className="sr-only"> Újraindítás</span>
</button>
</li>
))}
</ul>
)}
</section>
);
}
@@ -40,7 +40,7 @@ const KINDS: { kind: PromptKind; id: string; label: string; about: string }[] =
id: "kerdesek", id: "kerdesek",
label: "Kérdés-válasz", label: "Kérdés-válasz",
about: about:
"A kész képletelemzésre vagy előrejelzésre feltett kérdések válasza (legfeljebb 250 szó). A modell kapja a képlet adatait, az elemzést és a korábbi kérdéseket; ugyanaz a modell válaszol, amelyik az elemzést írta.", "A kész képletelemzésre, kapcsolati elemzésre vagy előrejelzésre feltett kérdések válasza (legfeljebb 250 szó). A modell kapja a képlet adatait, az elemzést és a korábbi kérdéseket; a Modellek oldalon beállított kérdés-válasz modell válaszol.",
}, },
]; ];
+1 -1
View File
@@ -22,7 +22,7 @@ const FAQ: [string, string][] = [
], ],
[ [
"Mit jelent a kérdés?", "Mit jelent a kérdés?",
"A kész részletes képletelemzésed vagy éves előrejelzésed alatt kérdezhetsz róla, például hogy mit jelent egy fényszög a munkádra nézve, vagy mire figyelj egy időszakban. A választ ugyanaz a modell írja, amelyik az elemzést, a képleted és az elemzés alapján, legfeljebb 250 szóban. Minden csomagban 3 kérdés van, és külön is vehetsz még.", "A kész képletelemzésed, kapcsolati elemzésed vagy éves előrejelzésed alatt kérdezhetsz róla, például hogy mit jelent egy fényszög a munkádra nézve, mire figyeljetek egy kapcsolatban, vagy mire figyelj egy időszakban. A válasz a képleted és az elemzés alapján készül, legfeljebb 250 szóban. Minden csomagban 3 kérdés van, és külön is vehetsz még.",
], ],
[ [
"Mire kell a személyhely?", "Mire kell a személyhely?",
+14
View File
@@ -643,3 +643,17 @@ a.pair-row:hover strong { color: var(--gold); }
.qa-answer p { margin: 0 0 8px; } .qa-answer p { margin: 0 0 8px; }
.qa-form { display: flex; flex-direction: column; gap: 8px; } .qa-form { display: flex; flex-direction: column; gap: 8px; }
.qa-form textarea { resize: vertical; min-height: 84px; } .qa-form textarea { resize: vertical; min-height: 84px; }
.hint.warn { color: var(--gold); }
.job-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 12px; align-items: center; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; }
.job-row > span:first-child { display: flex; flex-direction: column; min-width: 0; }
@media (max-width: 640px) { .job-row { grid-template-columns: minmax(0, 1fr) auto; } .job-row .model-price { grid-column: 1 / -1; align-items: flex-start; } }
.retry-notice { display: flex; flex-direction: column; gap: 4px; }
.retry-notice p { margin: 0; }
/* admin: error log */
.error-log { width: 100%; border-collapse: collapse; font-size: 14px; }
.error-log th, .error-log td { text-align: left; vertical-align: top; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.error-log td > .hint, .error-log td > code { display: block; }
.error-log .error-text { overflow-wrap: anywhere; max-width: 460px; }
.error-log .outcome { white-space: nowrap; }
.error-log .outcome.tone-tense { color: var(--tense); font-weight: 600; }
@@ -129,6 +129,7 @@ export default async function ComparisonPage({ params }: { params: Promise<{ id:
isAdmin={me.user.roles.includes("admin")} isAdmin={me.user.roles.includes("admin")}
canPay={config?.mock_payments ?? false} canPay={config?.mock_payments ?? false}
note={`Az elemzésben az első fél: ${name1}, a második fél: ${name2}.`} note={`Az elemzésben az első fél: ${name1}, a második fél: ${name2}.`}
questionsLeft={me.analyses_left.question ?? 0}
/> />
), ),
}, },
@@ -5,6 +5,7 @@ const PAGES = [
{ href: "/admin/meghivok", label: "Meghívók" }, { href: "/admin/meghivok", label: "Meghívók" },
{ href: "/admin/promptok", label: "Promptok" }, { href: "/admin/promptok", label: "Promptok" },
{ href: "/admin/modellek", label: "Modellek" }, { href: "/admin/modellek", label: "Modellek" },
{ href: "/admin/hibak", label: "Hibanapló" },
]; ];
/** Switches between the admin pages. */ /** Switches between the admin pages. */
@@ -4,13 +4,31 @@ import { useEffect, useState } from "react";
import { ZodiacWheel } from "./ZodiacWheel"; import { ZodiacWheel } from "./ZodiacWheel";
/** Slowly turning wheel with the elapsed time, while the LLM writes. */ /**
export function AnalysisLoading({ title, text, compact = false }: { title?: string; text: string; compact?: boolean }) { * Slowly turning wheel with the elapsed time, while the LLM writes. The time
const [seconds, setSeconds] = useState(0); * counts from `since` (when the text was asked for), so it does not start
* again from zero after a reload; without it, from when the wheel appeared.
*/
export function AnalysisLoading({
title,
text,
compact = false,
since,
}: {
title?: string;
text: string;
compact?: boolean;
since?: string;
}) {
const [start] = useState(() => {
const t = since ? Date.parse(since) : NaN;
return Number.isNaN(t) ? Date.now() : Math.min(t, Date.now());
});
const [seconds, setSeconds] = useState(() => Math.floor((Date.now() - start) / 1000));
useEffect(() => { useEffect(() => {
const t = setInterval(() => setSeconds((s) => s + 1), 1000); const t = setInterval(() => setSeconds(Math.floor((Date.now() - start) / 1000)), 1000);
return () => clearInterval(t); return () => clearInterval(t);
}, []); }, [start]);
const elapsed = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; const elapsed = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
return ( return (
@@ -21,7 +39,7 @@ export function AnalysisLoading({ title, text, compact = false }: { title?: stri
<p className="muted" role="status"> <p className="muted" role="status">
{text} {text}
</p> </p>
<p className="hint" aria-hidden="true"> <p className="hint" aria-hidden="true" suppressHydrationWarning>
{elapsed} {elapsed}
</p> </p>
</div> </div>
@@ -8,9 +8,28 @@ import type { Question, UnitBalance } from "@/lib/types";
import { AnalysisLoading } from "./AnalysisLoading"; import { AnalysisLoading } from "./AnalysisLoading";
import { PackageChooser } from "./PackageCards"; import { PackageChooser } from "./PackageCards";
import { RegenerateButton } from "./RegenerateButton"; import { RegenerateButton } from "./RegenerateButton";
import { RetryNotice } from "./RetryNotice";
import { Paragraphs } from "./Rich"; import { Paragraphs } from "./Rich";
const MAX_CHARS = 600; const MAX_CHARS = 600;
const TEXTS = {
natal: {
title: "Kérdezz az elemzésedről",
hint: "Mit jelent ez a te életedben, 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.",
example: "Pl. Hogyan hat a Hold helyzete a kapcsolataimra?",
},
synastry: {
title: "Kérdezz a kapcsolatotokról",
hint: "Hol erősítitek egymást, mire figyeljetek egy vitában, mit ad nektek a kapcsolat? A válasz a két képlet és az elemzés alapján készül, legfeljebb 250 szóban. A kérdéseket és a válaszokat mindketten látjátok.",
example: "Pl. Hogyan kezeljük jobban a nézeteltéréseinket?",
},
forecast: {
title: "Kérdezz az előrejelzésedről",
hint: "Mire figyelj egy időszakban, mit hozhat egy tranzit a munkádban vagy a kapcsolataidban? A válasz az év adatai és az előrejelzés alapján készül, legfeljebb 250 szóban.",
example: "Pl. Mire figyeljek a nyári időszakban a munkámban?",
},
};
const POLL_MS = 3000; const POLL_MS = 3000;
const MAX_POLLS = 80; // 4 minutes const MAX_POLLS = 80; // 4 minutes
@@ -24,13 +43,14 @@ export function AnalysisQuestions({
left: initialLeft, left: initialLeft,
isAdmin, isAdmin,
canPay, canPay,
forecast = false, about,
}: { }: {
analysisId: string; analysisId: string;
left: number; left: number;
isAdmin: boolean; isAdmin: boolean;
canPay: boolean; canPay: boolean;
forecast?: boolean; /** What the analysis is about: the heading and the example question follow it. */
about: "natal" | "synastry" | "forecast";
}) { }) {
const [questions, setQuestions] = useState<Question[] | null>(null); const [questions, setQuestions] = useState<Question[] | null>(null);
const [left, setLeft] = useState(initialLeft); const [left, setLeft] = useState(initialLeft);
@@ -114,11 +134,8 @@ export function AnalysisQuestions({
const canAsk = isAdmin || left > 0; const canAsk = isAdmin || left > 0;
return ( return (
<section className="questions" aria-labelledby={`${fieldId}-title`}> <section className="questions" aria-labelledby={`${fieldId}-title`}>
<h3 id={`${fieldId}-title`}>Kérdezz {forecast ? "az előrejelzésedről" : "az elemzésedről"}</h3> <h3 id={`${fieldId}-title`}>{TEXTS[about].title}</h3>
<p className="hint"> <p className="hint">{TEXTS[about].hint}</p>
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) => ( {questions?.map((q) => (
<article key={q.id} className="qa" aria-busy={q.status === "pending" || q.status === "running"}> <article key={q.id} className="qa" aria-busy={q.status === "pending" || q.status === "running"}>
@@ -139,12 +156,21 @@ export function AnalysisQuestions({
<RegenerateButton<Question> url={`/admin/questions/${q.id}/regenerate`} onStarted={replace} /> <RegenerateButton<Question> url={`/admin/questions/${q.id}/regenerate`} onStarted={replace} />
)} )}
<p className="msg msg-error" role="alert"> <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 Erre többszöri próbálkozás után sem sikerült válaszolni
egy kicsit később. {q.credits_charged > 0 ? ". A kérdést visszaadtuk, így ingyen újra felteheted" : ""}. Kérdezz újra egy
kicsit később.
</p> </p>
{isAdmin && q.error && <p className="hint">Hiba: {q.error}</p>}
</div> </div>
) : ( ) : (
<AnalysisLoading compact text="Készül a válasz, ez általában fél perc…" /> <>
<AnalysisLoading
compact
text="Készül a válasz, ez általában fél perc…"
since={q.requested_at ?? q.created_at}
/>
<RetryNotice status={q.status} error={q.error} nextAttemptAt={q.next_attempt_at} isAdmin={isAdmin} />
</>
)} )}
</article> </article>
))} ))}
@@ -160,9 +186,7 @@ export function AnalysisQuestions({
rows={3} rows={3}
maxLength={MAX_CHARS} maxLength={MAX_CHARS}
value={text} value={text}
placeholder={ placeholder={TEXTS[about].example}
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)} onChange={(e) => setText(e.target.value)}
disabled={busy || waiting} disabled={busy || waiting}
/> />
@@ -7,6 +7,7 @@ import type { Analysis } from "@/lib/types";
import { AnalysisLoading } from "./AnalysisLoading"; import { AnalysisLoading } from "./AnalysisLoading";
import { RegenerateButton } from "./RegenerateButton"; import { RegenerateButton } from "./RegenerateButton";
import { RetryNotice } from "./RetryNotice";
import { Rich } from "./Rich"; import { Rich } from "./Rich";
import { openTab } from "./Tabs"; import { openTab } from "./Tabs";
@@ -102,7 +103,21 @@ export function ChartSummary({
Az összefoglaló most nem készült el. Töltsd újra az oldalt egy kicsit később. Az összefoglaló most nem készült el. Töltsd újra az oldalt egy kicsit később.
</p> </p>
) : ( ) : (
<AnalysisLoading compact text="Az összefoglalód készül, ez néhány másodperc…" /> <>
<AnalysisLoading
compact
text="Az összefoglalód készül, ez néhány másodperc…"
since={summary?.requested_at ?? summary?.created_at}
/>
{summary && (
<RetryNotice
status={summary.status}
error={summary.error}
nextAttemptAt={summary.next_attempt_at}
isAdmin={isAdmin}
/>
)}
</>
)} )}
</> </>
)} )}
@@ -11,6 +11,7 @@ import { AnalysisLoading } from "./AnalysisLoading";
import { AnalysisQuestions } from "./AnalysisQuestions"; import { AnalysisQuestions } from "./AnalysisQuestions";
import { PackageChooser } from "./PackageCards"; import { PackageChooser } from "./PackageCards";
import { RegenerateButton } from "./RegenerateButton"; import { RegenerateButton } from "./RegenerateButton";
import { RetryNotice } from "./RetryNotice";
import { AnalysisView } from "./AnalysisView"; import { AnalysisView } from "./AnalysisView";
const POLL_MS = 5000; const POLL_MS = 5000;
@@ -99,7 +100,7 @@ export function DetailedAnalysis({
canPay: boolean; canPay: boolean;
/** Shown above a finished analysis (e.g. who is the first and the second person). */ /** Shown above a finished analysis (e.g. who is the first and the second person). */
note?: string; note?: string;
/** Questions left; given where questions can be asked (natal analysis, forecast). */ /** Questions left; given where questions can be asked. */
questionsLeft?: number; questionsLeft?: number;
}) { }) {
const [analysis, setAnalysis] = useState<Analysis | null>(initial); const [analysis, setAnalysis] = useState<Analysis | null>(initial);
@@ -155,13 +156,13 @@ export function DetailedAnalysis({
)} )}
{note && <p className="hint">{note}</p>} {note && <p className="hint">{note}</p>}
<AnalysisView content={analysis.content} kind={target.kind} /> <AnalysisView content={analysis.content} kind={target.kind} />
{questionsLeft !== undefined && target.kind !== "synastry" && ( {questionsLeft !== undefined && (
<AnalysisQuestions <AnalysisQuestions
analysisId={analysis.id} analysisId={analysis.id}
left={questionsLeft} left={questionsLeft}
isAdmin={isAdmin} isAdmin={isAdmin}
canPay={canPay} canPay={canPay}
forecast={target.kind === "forecast"} about={target.kind}
/> />
)} )}
</section> </section>
@@ -172,7 +173,15 @@ export function DetailedAnalysis({
return ( return (
<section className="analysis-card" aria-label={t.eyebrow} aria-busy="true"> <section className="analysis-card" aria-label={t.eyebrow} aria-busy="true">
<span className="eyebrow">{t.eyebrow}</span> <span className="eyebrow">{t.eyebrow}</span>
<AnalysisLoading title={t.loadingTitle} text={t.loading} /> <AnalysisLoading title={t.loadingTitle} text={t.loading} since={analysis?.requested_at ?? analysis?.created_at} />
{analysis && (
<RetryNotice
status={analysis.status}
error={analysis.error}
nextAttemptAt={analysis.next_attempt_at}
isAdmin={isAdmin}
/>
)}
</section> </section>
); );
} }
@@ -186,9 +195,15 @@ export function DetailedAnalysis({
<p className="muted">{t.about}</p> <p className="muted">{t.about}</p>
<p className="ai-line">{AI_SHORT}</p> <p className="ai-line">{AI_SHORT}</p>
{analysis?.status === "failed" && ( {analysis?.status === "failed" && (
<p className="msg msg-error" role="alert"> <div className="msg msg-error" role="alert">
Az előző kérés nem sikerült{isAdmin ? "" : ", az elemzést visszaadtuk a keretedbe"}. Próbáld újra. <p style={{ margin: 0 }}>
</p> Az elemzés többszöri próbálkozás után sem készült el.
{isAdmin || analysis.credits_charged === 0
? " Próbáld újra."
: " A felhasznált egységet visszaadtuk, így ingyen újra kérheted."}
</p>
{isAdmin && analysis.error && <p className="hint">Hiba: {analysis.error}</p>}
</div>
)} )}
{error && ( {error && (
<p className="msg msg-error" role="alert"> <p className="msg msg-error" role="alert">
@@ -0,0 +1,28 @@
/**
* Shown while a text is being written again after a failed attempt: it is
* retried on its own, nothing to do. Admins also see the error.
*/
export function RetryNotice({
status,
error,
nextAttemptAt,
isAdmin = false,
}: {
status: string;
error?: string | null;
nextAttemptAt?: string;
isAdmin?: boolean;
}) {
if (!error || (status !== "pending" && status !== "running")) return null;
const wait = nextAttemptAt ? Math.round((Date.parse(nextAttemptAt) - Date.now()) / 60_000) : 0;
return (
<div className="msg retry-notice" role="status">
<p>
Az előző próbálkozás nem sikerült, ezért automatikusan újrapróbáljuk. Nem kell újra kérned, és nem kerül
többe.
{status === "pending" && wait >= 1 ? ` A következő próbálkozás kb. ${wait} perc múlva indul.` : ""}
</p>
{isAdmin && <p className="hint">Hiba: {error}</p>}
</div>
);
}
+47 -2
View File
@@ -77,6 +77,9 @@ export interface Question {
credits_charged: number; credits_charged: number;
created_at: string; created_at: string;
completed_at: string | null; completed_at: string | null;
attempts?: number;
next_attempt_at?: string;
requested_at?: string;
} }
export interface PublicConfig { export interface PublicConfig {
@@ -232,6 +235,26 @@ export interface Analysis {
credits_charged: number; credits_charged: number;
created_at: string; created_at: string;
completed_at: string | null; completed_at: string | null;
/** Attempts so far, and when the next one starts (while it is retried). */
attempts?: number;
next_attempt_at?: string;
/** When the current text was asked for: the waiting page counts from here. */
requested_at?: string;
}
/** Admin: one failed attempt in the error log. */
export interface GenerationError {
id: number;
created_at: string;
source: "analysis" | "question" | "preview";
item_id: string;
kind: string;
model: string;
attempt: number;
outcome: "retry" | "fallback" | "failed";
error: string;
seconds: number | null;
user: string | null;
} }
export interface AdminUser { export interface AdminUser {
@@ -248,9 +271,9 @@ export interface AdminUser {
export type PromptKind = "summary" | "natal" | "synastry" | "forecast" | "question"; export type PromptKind = "summary" | "natal" | "synastry" | "forecast" | "question";
/** Admin: the model writing one kind of analysis. */ /** Admin: the model of one function (all analyses and forecasts, answers, the short summary). */
export interface ModelFunction { export interface ModelFunction {
kind: PromptKind; kind: "summary" | "analysis" | "question";
model: string; model: string;
/** The configured default (environment). */ /** The configured default (environment). */
default: string; default: string;
@@ -261,6 +284,28 @@ export interface ModelFunction {
avg_input_tokens: number | null; avg_input_tokens: number | null;
avg_output_tokens: number | null; avg_output_tokens: number | null;
samples: number; samples: number;
/** In the last 30 days: texts the default model had to write, of all finished. */
fallbacks_30d: number;
finished_30d: number;
}
/** Admin: an analysis or answer being written or waiting in the queue. */
export interface AdminJob {
id: string;
type: "analysis" | "question";
kind: "summary" | "natal" | "synastry" | "forecast" | "question";
status: "pending" | "running";
/** The model of the current attempt. */
model: string;
on_fallback: boolean;
/** What a restart would use now. */
next_model: string;
next_fallback: string | null;
attempts: number;
started_at: string | null;
created_at: string;
next_attempt_at: string;
error: string | null;
} }
/** A model OpenRouter offers; prices in USD per million tokens. */ /** A model OpenRouter offers; prices in USD per million tokens. */
+1 -1
View File
@@ -45,7 +45,7 @@ A 2026-11-15-i összevetésen két képletet hét modell írt meg; az egyiket a
A MiniMax M3 olcsó, de az asztrológus szerint többször felcserélt dolgokat. A MiniMax M3 olcsó, de az asztrológus szerint többször felcserélt dolgokat.
Az ingyenes rövid összefoglalót (2026-11-16) a Haiku és a Sonnet négy képleten írta meg. Mindkettő tényszerűen helyes volt, de a Sonnet jobban válogat és fogalmaz, ezért ez az alapértelmezett (`SUMMARY_MODEL`, kb. 0,009 dollár, a Haiku kb. 0,001). A fizetős részletes elemzés az Opus 5.5-tel készül. Az ingyenes rövid összefoglalót (2026-11-16) a Haiku és a Sonnet négy képleten írta meg. Mindkettő tényszerűen helyes volt, de a Sonnet jobban válogat és fogalmaz (kb. 0,009 dollár, a Haiku kb. 0,001). 2026-11-18 óta minden funkció alapmodellje az Opus 5.5 (`SUMMARY_MODEL`, `ANALYSIS_MODEL`); olcsóbb modell az admin Modellek oldalán választható, hiba esetén az Opus 5.5 a tartalék.
```bash ```bash
OPENROUTER_API_KEY=... worker compare-models --chart natal.json \ OPENROUTER_API_KEY=... worker compare-models --chart natal.json \
+170 -25
View File
@@ -8,6 +8,10 @@
//! //!
//! Admin test runs of an edited prompt (`prompt_previews`) go through the //! Admin test runs of an edited prompt (`prompt_previews`) go through the
//! same loop when no analysis is due: one attempt, no credits, no retries. //! same loop when no analysis is due: one attempt, no credits, no retries.
//!
//! Fallback: when the model chosen on the admin page fails twice (or once with
//! a permanent error), the default model (`fallback_model`) takes over with a
//! fresh set of attempts; only when that fails too is the unit refunded.
pub mod compact; pub mod compact;
pub mod openrouter; pub mod openrouter;
@@ -40,6 +44,48 @@ pub struct Job {
pub model: String, pub model: String,
pub prompt_version: String, pub prompt_version: String,
pub attempts: i32, pub attempts: i32,
/// The default model, when the chosen one is another (see the module docs).
pub fallback_model: Option<String>,
pub on_fallback: bool,
/// When this attempt started; also its token: an admin restart clears it,
/// so a superseded attempt neither writes its result nor keeps running.
pub started_at: time::OffsetDateTime,
}
impl Job {
/// The model of this attempt.
pub fn active_model(&self) -> &str {
active_model(&self.model, self.fallback_model.as_deref(), self.on_fallback)
}
}
pub fn active_model<'a>(model: &'a str, fallback: Option<&'a str>, on_fallback: bool) -> &'a str {
match fallback {
Some(f) if on_fallback => f,
_ => model,
}
}
/// What happens after a failed attempt.
#[derive(Debug, PartialEq, Eq)]
pub enum Next {
Retry(Duration),
/// Switch to the fallback model, with a fresh set of attempts, at once.
Fallback,
/// Give up (and refund).
Fail,
}
/// The chosen model gets two attempts (one after a permanent error), then the
/// fallback model takes over with its own `max_attempts`.
pub fn next_step(error: &LlmError, attempts: i32, max_attempts: i32, can_fall_back: bool) -> Next {
if can_fall_back && (matches!(error, LlmError::Permanent(_)) || attempts >= 2) {
return Next::Fallback;
}
match error {
LlmError::Transient(_) if attempts < max_attempts => Next::Retry(backoff(attempts)),
_ => Next::Fail,
}
} }
/// A test run of an edited system prompt on a chart. /// A test run of an edited system prompt on a chart.
@@ -81,7 +127,8 @@ impl Analyzer {
"UPDATE analyses SET status = 'running', started_at = now(), attempts = attempts + 1 \ "UPDATE analyses SET status = 'running', started_at = now(), attempts = attempts + 1 \
WHERE id = (SELECT id FROM analyses WHERE status = 'pending' AND next_attempt_at <= now() \ WHERE id = (SELECT id FROM analyses WHERE status = 'pending' AND next_attempt_at <= now() \
ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \ ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \
RETURNING id, kind, chart_id, synastry_id, forecast_id, language, model, prompt_version, attempts", RETURNING id, kind, chart_id, synastry_id, forecast_id, language, model, prompt_version, attempts, \
fallback_model, on_fallback, started_at",
) )
.fetch_optional(&self.db) .fetch_optional(&self.db)
.await?; .await?;
@@ -206,6 +253,8 @@ impl Analyzer {
.await .await
} }
Err(e) => { Err(e) => {
self.log_error("preview", job.id, &job.kind, &job.model, 1, "failed", e.message(), started.elapsed())
.await;
sqlx::query( sqlx::query(
"UPDATE prompt_previews SET status = 'failed', error = $2, completed_at = now() \ "UPDATE prompt_previews SET status = 'failed', error = $2, completed_at = now() \
WHERE id = $1 AND status = 'running'", WHERE id = $1 AND status = 'running'",
@@ -229,12 +278,21 @@ impl Analyzer {
/// Writes one claimed analysis and records the outcome. /// Writes one claimed analysis and records the outcome.
pub async fn handle(&self, job: Job) { pub async fn handle(&self, job: Job) {
let started = Instant::now(); let started = Instant::now();
let result = match self.prompt_for(&job).await { let work = async {
Ok(prompt) => self.llm.complete(&job.model, &prompt).await.map(|mut c| { match self.prompt_for(&job).await {
prompt::polish(&job.language, &mut c.content); Ok(prompt) => self.llm.complete(job.active_model(), &prompt).await.map(|mut c| {
c prompt::polish(&job.language, &mut c.content);
}), c
Err(e) => Err(e), }),
Err(e) => Err(e),
}
};
let result = tokio::select! {
result = work => result,
() = self.superseded("analyses", job.id, job.started_at) => {
tracing::info!(id = %job.id, "analysis restarted by an admin; this attempt is dropped");
return;
}
}; };
if let Err(e) = self.finish(&job, result, started.elapsed()).await { if let Err(e) = self.finish(&job, result, started.elapsed()).await {
tracing::error!(id = %job.id, error = %e, "saving the analysis result failed"); tracing::error!(id = %job.id, error = %e, "saving the analysis result failed");
@@ -248,7 +306,7 @@ impl Analyzer {
sqlx::query( sqlx::query(
"UPDATE analyses SET status = 'done', content = $2, model_used = $3, input_tokens = $4, \ "UPDATE analyses SET status = 'done', content = $2, model_used = $3, input_tokens = $4, \
output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \ output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \
WHERE id = $1 AND status = 'running'", WHERE id = $1 AND status = 'running' AND started_at = $7",
) )
.bind(job.id) .bind(job.id)
.bind(&c.content) .bind(&c.content)
@@ -256,6 +314,7 @@ impl Analyzer {
.bind(c.input_tokens) .bind(c.input_tokens)
.bind(c.output_tokens) .bind(c.output_tokens)
.bind(c.cost_usd) .bind(c.cost_usd)
.bind(job.started_at)
.execute(&self.db) .execute(&self.db)
.await?; .await?;
tracing::info!( tracing::info!(
@@ -263,36 +322,75 @@ impl Analyzer {
cost_usd = c.cost_usd, secs = took.as_secs(), "analysis done" cost_usd = c.cost_usd, secs = took.as_secs(), "analysis done"
); );
} }
Err(LlmError::Transient(message)) if job.attempts < self.max_attempts => {
let wait = backoff(job.attempts);
sqlx::query(
"UPDATE analyses SET status = 'pending', error = $2, \
next_attempt_at = now() + make_interval(secs => $3) WHERE id = $1 AND status = 'running'",
)
.bind(job.id)
.bind(&message)
.bind(wait.as_secs_f64())
.execute(&self.db)
.await?;
tracing::warn!(id = %job.id, attempts = job.attempts, retry_in_secs = wait.as_secs(), error = %message, "analysis will be retried");
}
Err(e) => { Err(e) => {
self.fail(job.id, e.message()).await?; let can_fall_back = !job.on_fallback && job.fallback_model.is_some();
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "analysis failed"); let next = next_step(&e, job.attempts, self.max_attempts, can_fall_back);
let outcome = match next {
Next::Retry(_) => "retry",
Next::Fallback => "fallback",
Next::Fail => "failed",
};
self.log_error(
"analysis",
job.id,
&job.kind,
job.active_model(),
job.attempts,
outcome,
e.message(),
took,
)
.await;
match next {
Next::Retry(wait) => {
sqlx::query(
"UPDATE analyses SET status = 'pending', error = $2, \
next_attempt_at = now() + make_interval(secs => $3) \
WHERE id = $1 AND status = 'running' AND started_at = $4",
)
.bind(job.id)
.bind(e.message())
.bind(wait.as_secs_f64())
.bind(job.started_at)
.execute(&self.db)
.await?;
tracing::warn!(id = %job.id, attempts = job.attempts, retry_in_secs = wait.as_secs(), error = %e.message(), "analysis will be retried");
}
Next::Fallback => {
sqlx::query(
"UPDATE analyses SET status = 'pending', error = $2, on_fallback = true, attempts = 0, \
next_attempt_at = now() WHERE id = $1 AND status = 'running' AND started_at = $3",
)
.bind(job.id)
.bind(e.message())
.bind(job.started_at)
.execute(&self.db)
.await?;
tracing::warn!(
id = %job.id, model = %job.model, fallback = ?job.fallback_model, error = %e.message(),
"the chosen model failed, the default model takes over"
);
}
Next::Fail => {
self.fail(job.id, job.started_at, e.message()).await?;
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "analysis failed");
}
}
} }
} }
Ok(()) Ok(())
} }
/// Marks the analysis failed and gives its unit back (once). /// Marks the analysis failed and gives its unit back (once).
async fn fail(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> { async fn fail(&self, id: Uuid, started_at: time::OffsetDateTime, message: &str) -> Result<(), sqlx::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await?;
let row: Option<(Option<Uuid>, i32, String)> = sqlx::query_as( let row: Option<(Option<Uuid>, i32, String)> = sqlx::query_as(
"UPDATE analyses SET status = 'failed', error = $2, completed_at = now() \ "UPDATE analyses SET status = 'failed', error = $2, completed_at = now() \
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged, kind", WHERE id = $1 AND status = 'running' AND started_at = $3 RETURNING requested_by, credits_charged, kind",
) )
.bind(id) .bind(id)
.bind(message) .bind(message)
.bind(started_at)
.fetch_optional(&mut *tx) .fetch_optional(&mut *tx)
.await?; .await?;
if let Some((Some(user), units, kind)) = row if let Some((Some(user), units, kind)) = row
@@ -345,6 +443,53 @@ impl Analyzer {
Ok(result.rows_affected()) Ok(result.rows_affected())
} }
/// Records a failed attempt in the admin error log (a failure here is only logged).
#[allow(clippy::too_many_arguments)]
pub(crate) async fn log_error(
&self,
source: &str,
item_id: Uuid,
kind: &str,
model: &str,
attempt: i32,
outcome: &str,
error: &str,
took: Duration,
) {
let result = sqlx::query(
"INSERT INTO generation_errors (source, item_id, kind, model, attempt, outcome, error, seconds) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
)
.bind(source)
.bind(item_id)
.bind(kind)
.bind(model)
.bind(attempt)
.bind(outcome)
.bind(error.chars().take(2000).collect::<String>())
.bind(i32::try_from(took.as_secs()).unwrap_or(i32::MAX))
.execute(&self.db)
.await;
if let Err(e) = result {
tracing::error!(error = %e, "writing the error log");
}
}
/// Resolves when this attempt is no longer the current one (an admin
/// restarted it); checked every 10 seconds, never on a database error.
async fn superseded(&self, table: &str, id: Uuid, started_at: time::OffsetDateTime) {
let query = format!("SELECT status = 'running' AND started_at = $2 FROM {table} WHERE id = $1");
loop {
tokio::time::sleep(Duration::from_secs(10)).await;
match sqlx::query_scalar::<_, Option<bool>>(&query).bind(id).bind(started_at).fetch_optional(&self.db).await
{
Ok(Some(Some(true))) => {}
Ok(_) => return,
Err(_) => {}
}
}
}
/// On shutdown: analyses still being written go back to `pending` at once. /// On shutdown: analyses still being written go back to `pending` at once.
async fn release_running(&self) { async fn release_running(&self) {
let ids: Vec<Uuid> = self.running.lock().unwrap().drain().collect(); let ids: Vec<Uuid> = self.running.lock().unwrap().drain().collect();
+86 -31
View File
@@ -1,7 +1,7 @@
//! Answers to questions about a finished analysis (`questions` rows). The //! Answers to questions about a finished analysis (`questions` rows). The
//! model of the analysis answers, seeing the chart data the analysis was //! question model answers, seeing the chart data the analysis was written
//! written from, the analysis itself and the earlier questions about it. //! from, the analysis itself and the earlier questions about it. Retries,
//! Retries and refunds work as for analyses. //! fallback and refunds work as for analyses.
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@@ -10,7 +10,7 @@ use uuid::Uuid;
use super::openrouter::{Completion, LlmError}; use super::openrouter::{Completion, LlmError};
use super::prompt::{self, Kind}; use super::prompt::{self, Kind};
use super::{Analyzer, backoff}; use super::{Analyzer, Next, active_model, next_step};
/// The earlier questions and answers given to the model, at most this many. /// The earlier questions and answers given to the model, at most this many.
const HISTORY: i64 = 6; const HISTORY: i64 = 6;
@@ -25,6 +25,10 @@ pub struct QuestionJob {
pub prompt_version: String, pub prompt_version: String,
pub attempts: i32, pub attempts: i32,
pub created_at: time::OffsetDateTime, pub created_at: time::OffsetDateTime,
pub fallback_model: Option<String>,
pub on_fallback: bool,
/// The token of this attempt (see `Job::started_at`).
pub started_at: time::OffsetDateTime,
} }
impl Analyzer { impl Analyzer {
@@ -34,7 +38,8 @@ impl Analyzer {
"UPDATE questions SET status = 'running', started_at = now(), attempts = attempts + 1 \ "UPDATE questions SET status = 'running', started_at = now(), attempts = attempts + 1 \
WHERE id = (SELECT id FROM questions WHERE status = 'pending' AND next_attempt_at <= now() \ WHERE id = (SELECT id FROM questions WHERE status = 'pending' AND next_attempt_at <= now() \
ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \ ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \
RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at", RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at, \
fallback_model, on_fallback, started_at",
) )
.fetch_optional(&self.db) .fetch_optional(&self.db)
.await?; .await?;
@@ -46,8 +51,9 @@ impl Analyzer {
async fn question_prompt(&self, job: &QuestionJob) -> Result<prompt::Prompt, LlmError> { async fn question_prompt(&self, job: &QuestionJob) -> Result<prompt::Prompt, LlmError> {
let db_err = |e: sqlx::Error| LlmError::Transient(format!("database: {e}")); let db_err = |e: sqlx::Error| LlmError::Transient(format!("database: {e}"));
let (kind, chart_id, forecast_id, content): (String, Option<Uuid>, Option<Uuid>, Option<Value>) = type Row = (String, Option<Uuid>, Option<Uuid>, Option<Uuid>, Option<Value>);
sqlx::query_as("SELECT kind, chart_id, forecast_id, content FROM analyses WHERE id = $1") let (kind, chart_id, synastry_id, forecast_id, content): Row =
sqlx::query_as("SELECT kind, chart_id, synastry_id, forecast_id, content FROM analyses WHERE id = $1")
.bind(job.analysis_id) .bind(job.analysis_id)
.fetch_optional(&self.db) .fetch_optional(&self.db)
.await .await
@@ -57,6 +63,7 @@ impl Analyzer {
let content = content.ok_or_else(|| LlmError::Permanent("the analysis has no text".into()))?; let content = content.ok_or_else(|| LlmError::Permanent("the analysis has no text".into()))?;
let chart_text = match kind { let chart_text = match kind {
Kind::Forecast => self.forecast_text(forecast_id).await?, Kind::Forecast => self.forecast_text(forecast_id).await?,
Kind::Synastry => self.synastry_text(synastry_id).await?,
_ => self.natal_text(chart_id).await?, _ => self.natal_text(chart_id).await?,
}; };
let analysis = prompt::to_markdown(kind, &job.language, &content); let analysis = prompt::to_markdown(kind, &job.language, &content);
@@ -88,12 +95,24 @@ impl Analyzer {
/// Answers one claimed question and records the outcome. /// Answers one claimed question and records the outcome.
pub async fn handle_question(&self, job: QuestionJob) { pub async fn handle_question(&self, job: QuestionJob) {
let started = Instant::now(); let started = Instant::now();
let result = match self.question_prompt(&job).await { let work = async {
Ok(p) => self.llm.complete(&job.model, &p).await.map(|mut c| { match self.question_prompt(&job).await {
prompt::polish(&job.language, &mut c.content); Ok(p) => {
c let model = active_model(&job.model, job.fallback_model.as_deref(), job.on_fallback);
}), self.llm.complete(model, &p).await.map(|mut c| {
Err(e) => Err(e), prompt::polish(&job.language, &mut c.content);
c
})
}
Err(e) => Err(e),
}
};
let result = tokio::select! {
result = work => result,
() = self.superseded("questions", job.id, job.started_at) => {
tracing::info!(id = %job.id, "answer restarted by an admin; this attempt is dropped");
return;
}
}; };
if let Err(e) = self.finish_question(&job, result, started.elapsed()).await { if let Err(e) = self.finish_question(&job, result, started.elapsed()).await {
tracing::error!(id = %job.id, error = %e, "saving the answer failed"); tracing::error!(id = %job.id, error = %e, "saving the answer failed");
@@ -113,7 +132,7 @@ impl Analyzer {
sqlx::query( sqlx::query(
"UPDATE questions SET status = 'done', answer = $2, model_used = $3, input_tokens = $4, \ "UPDATE questions SET status = 'done', answer = $2, model_used = $3, input_tokens = $4, \
output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \ output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \
WHERE id = $1 AND status = 'running'", WHERE id = $1 AND status = 'running' AND started_at = $7",
) )
.bind(job.id) .bind(job.id)
.bind(&answer) .bind(&answer)
@@ -121,6 +140,7 @@ impl Analyzer {
.bind(c.input_tokens) .bind(c.input_tokens)
.bind(c.output_tokens) .bind(c.output_tokens)
.bind(c.cost_usd) .bind(c.cost_usd)
.bind(job.started_at)
.execute(&self.db) .execute(&self.db)
.await?; .await?;
tracing::info!( tracing::info!(
@@ -128,36 +148,71 @@ impl Analyzer {
secs = took.as_secs(), "answer done" secs = took.as_secs(), "answer done"
); );
} }
Err(LlmError::Transient(message)) if job.attempts < self.max_attempts => {
let wait = backoff(job.attempts);
sqlx::query(
"UPDATE questions SET status = 'pending', error = $2, \
next_attempt_at = now() + make_interval(secs => $3) WHERE id = $1 AND status = 'running'",
)
.bind(job.id)
.bind(&message)
.bind(wait.as_secs_f64())
.execute(&self.db)
.await?;
tracing::warn!(id = %job.id, attempts = job.attempts, error = %message, "answer will be retried");
}
Err(e) => { Err(e) => {
self.fail_question(job.id, e.message()).await?; let can_fall_back = !job.on_fallback && job.fallback_model.is_some();
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed"); let next = next_step(&e, job.attempts, self.max_attempts, can_fall_back);
let outcome = match next {
Next::Retry(_) => "retry",
Next::Fallback => "fallback",
Next::Fail => "failed",
};
let model = active_model(&job.model, job.fallback_model.as_deref(), job.on_fallback);
self.log_error("question", job.id, "question", model, job.attempts, outcome, e.message(), took).await;
match next {
Next::Retry(wait) => {
sqlx::query(
"UPDATE questions SET status = 'pending', error = $2, \
next_attempt_at = now() + make_interval(secs => $3) \
WHERE id = $1 AND status = 'running' AND started_at = $4",
)
.bind(job.id)
.bind(e.message())
.bind(wait.as_secs_f64())
.bind(job.started_at)
.execute(&self.db)
.await?;
tracing::warn!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer will be retried");
}
Next::Fallback => {
sqlx::query(
"UPDATE questions SET status = 'pending', error = $2, on_fallback = true, attempts = 0, \
next_attempt_at = now() WHERE id = $1 AND status = 'running' AND started_at = $3",
)
.bind(job.id)
.bind(e.message())
.bind(job.started_at)
.execute(&self.db)
.await?;
tracing::warn!(
id = %job.id, model = %job.model, error = %e.message(),
"the chosen model failed, the default model answers"
);
}
Next::Fail => {
self.fail_question(job.id, job.started_at, e.message()).await?;
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed");
}
}
} }
} }
Ok(()) Ok(())
} }
/// Marks the question failed and gives its unit back (once). /// Marks the question failed and gives its unit back (once).
async fn fail_question(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> { async fn fail_question(
&self,
id: Uuid,
started_at: time::OffsetDateTime,
message: &str,
) -> Result<(), sqlx::Error> {
let mut tx = self.db.begin().await?; let mut tx = self.db.begin().await?;
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as( let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
"UPDATE questions SET status = 'failed', error = $2, completed_at = now() \ "UPDATE questions SET status = 'failed', error = $2, completed_at = now() \
WHERE id = $1 AND status = 'running' RETURNING asked_by, credits_charged", WHERE id = $1 AND status = 'running' AND started_at = $3 RETURNING asked_by, credits_charged",
) )
.bind(id) .bind(id)
.bind(message) .bind(message)
.bind(started_at)
.fetch_optional(&mut *tx) .fetch_optional(&mut *tx)
.await?; .await?;
if let Some((Some(user), units)) = row if let Some((Some(user), units)) = row
+2 -1
View File
@@ -8,7 +8,7 @@ use tokio::sync::watch;
const INTERVAL: Duration = Duration::from_secs(3600); const INTERVAL: Duration = Duration::from_secs(3600);
/// (what, statement) pairs; each returns the number of deleted rows. /// (what, statement) pairs; each returns the number of deleted rows.
const STATEMENTS: [(&str, &str); 6] = [ const STATEMENTS: [(&str, &str); 7] = [
("sessions", "DELETE FROM sessions WHERE expires_at < now()"), ("sessions", "DELETE FROM sessions WHERE expires_at < now()"),
("auth_tokens", "DELETE FROM auth_tokens WHERE expires_at < now()"), ("auth_tokens", "DELETE FROM auth_tokens WHERE expires_at < now()"),
// Sent e-mails contain links with tokens; keep them a week for support. // Sent e-mails contain links with tokens; keep them a week for support.
@@ -26,6 +26,7 @@ const STATEMENTS: [(&str, &str); 6] = [
OR (accepted_at IS NOT NULL AND accepted_by IS NULL)", OR (accepted_at IS NOT NULL AND accepted_by IS NULL)",
), ),
("prompt_previews", "DELETE FROM prompt_previews WHERE created_at < now() - interval '30 days'"), ("prompt_previews", "DELETE FROM prompt_previews WHERE created_at < now() - interval '30 days'"),
("generation_errors", "DELETE FROM generation_errors WHERE created_at < now() - interval '90 days'"),
]; ];
pub async fn run_once(db: &PgPool) -> Result<Vec<(&'static str, u64)>, sqlx::Error> { pub async fn run_once(db: &PgPool) -> Result<Vec<(&'static str, u64)>, sqlx::Error> {
+144 -1
View File
@@ -212,7 +212,8 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
("emails", 1), ("emails", 1),
("invites", 0), ("invites", 0),
("invitations", 0), ("invitations", 0),
("prompt_previews", 0) ("prompt_previews", 0),
("generation_errors", 0)
] ]
); );
let (left,): (Vec<Uuid>,) = let (left,): (Vec<Uuid>,) =
@@ -533,6 +534,18 @@ mod analyses {
let (status, attempts, ..) = row(&db, id).await; let (status, attempts, ..) = row(&db, id).await;
assert_eq!((status.as_str(), attempts), ("failed", 3)); assert_eq!((status.as_str(), attempts), ("failed", 3));
assert_eq!(refunds(&db, s.user).await, vec![1]); assert_eq!(refunds(&db, s.user).await, vec![1]);
// every failed attempt is in the admin error log
let log: Vec<(String, i32, String, String)> = sqlx::query_as(
"SELECT source, attempt, outcome, model FROM generation_errors WHERE item_id = $1 ORDER BY id",
)
.bind(id)
.fetch_all(&db)
.await
.unwrap();
let expected = |attempt: i32, outcome: &str| {
("analysis".to_string(), attempt, outcome.to_string(), "test/model".to_string())
};
assert_eq!(log, vec![expected(1, "retry"), expected(2, "retry"), expected(3, "failed")]);
} }
#[sqlx::test(migrator = "db::MIGRATOR")] #[sqlx::test(migrator = "db::MIGRATOR")]
@@ -736,4 +749,134 @@ mod analyses {
.unwrap(); .unwrap();
assert_eq!(refunds, vec![("question".to_string(), 1)]); assert_eq!(refunds, vec![("question".to_string(), 1)]);
} }
#[test]
fn the_chosen_model_gets_two_attempts_then_the_default_takes_over() {
use crate::analysis::openrouter::LlmError;
use crate::analysis::{Next, next_step};
let t = || LlmError::Transient("overloaded".into());
let p = || LlmError::Permanent("no such model".into());
assert_eq!(next_step(&t(), 1, 3, true), Next::Retry(backoff(1)));
assert_eq!(next_step(&t(), 2, 3, true), Next::Fallback);
assert_eq!(next_step(&p(), 1, 3, true), Next::Fallback);
// without a fallback (or already on it): retries, then failure
assert_eq!(next_step(&t(), 2, 3, false), Next::Retry(backoff(2)));
assert_eq!(next_step(&t(), 3, 3, false), Next::Fail);
assert_eq!(next_step(&p(), 1, 3, false), Next::Fail);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn the_default_model_writes_when_the_chosen_one_fails(db: PgPool) {
let s = setup(&db).await;
let id = request_analysis(&db, &s).await;
sqlx::query("UPDATE analyses SET model = 'cheap/model', fallback_model = 'test/model' WHERE id = $1")
.bind(id)
.execute(&db)
.await
.unwrap();
let broken = json!({"title": "Csak cím"});
let (fake, url) = fake_openrouter(vec![reply(&broken), reply(&broken), reply(&content(Kind::Natal))]).await;
let a = analyzer(&db, &url);
assert!(run_once(&a).await);
make_due(&db, id).await;
assert!(run_once(&a).await); // second failure: the default model takes over at once
assert!(run_once(&a).await);
let (status, on_fallback, credits): (String, bool, i32) =
sqlx::query_as("SELECT status, on_fallback, credits_charged FROM analyses WHERE id = $1")
.bind(id)
.fetch_one(&db)
.await
.unwrap();
assert_eq!((status.as_str(), on_fallback, credits), ("done", true, 1));
assert!(refunds(&db, s.user).await.is_empty());
let models: Vec<String> =
fake.requests.lock().unwrap().iter().map(|b| b["model"].as_str().unwrap().to_string()).collect();
assert_eq!(models, ["cheap/model", "cheap/model", "test/model"]);
let outcomes: Vec<(String, String)> =
sqlx::query_as("SELECT outcome, model FROM generation_errors WHERE item_id = $1 ORDER BY id")
.bind(id)
.fetch_all(&db)
.await
.unwrap();
assert_eq!(
outcomes,
[("retry".to_string(), "cheap/model".to_string()), ("fallback".to_string(), "cheap/model".to_string())]
);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn a_restarted_attempt_does_not_write_its_result(db: PgPool) {
let s = setup(&db).await;
let id = request_analysis(&db, &s).await;
let (_, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await;
let a = analyzer(&db, &url);
let job = a.claim().await.unwrap().expect("a job");
// an admin restarts it while the worker is still writing it
sqlx::query("UPDATE analyses SET status = 'pending', started_at = NULL, attempts = 0 WHERE id = $1")
.bind(id)
.execute(&db)
.await
.unwrap();
a.handle(job).await;
let (status, content): (String, Option<Value>) =
sqlx::query_as("SELECT status, content FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
assert_eq!((status.as_str(), content), ("pending", None));
// the new attempt writes it
assert!(run_once(&a).await);
let status: String =
sqlx::query_scalar("SELECT status FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
assert_eq!(status, "done");
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn answers_a_question_about_a_relationship_with_both_charts(db: PgPool) {
let s = setup(&db).await;
let response = fixture("synastry-response.json");
let (chart2, synastry, analysis) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
sqlx::query(
"INSERT INTO charts (id, owner_id, label, is_primary, kind, birth_data, result, chart_hash) \
VALUES ($1, $2, 'Partner', false, 'person', '{}', $3, 'hash2')",
)
.bind(chart2)
.bind(s.user)
.bind(&response["person2"])
.execute(&db)
.await
.unwrap();
sqlx::query(
"INSERT INTO synastries (id, owner_id, chart1_id, chart2_id, result, synastry_hash) \
VALUES ($1, $2, $3, $4, $5, 'sh')",
)
.bind(synastry)
.bind(s.user)
.bind(s.chart)
.bind(chart2)
.bind(&response)
.execute(&db)
.await
.unwrap();
sqlx::query(
"INSERT INTO analyses (id, kind, synastry_id, requested_by, language, model, prompt_version, cache_key, \
status, content) VALUES ($1, 'synastry', $2, $3, 'hu', 'test/model', 'v1', 'k', 'done', $4)",
)
.bind(analysis)
.bind(synastry)
.bind(s.user)
.bind(content(Kind::Synastry))
.execute(&db)
.await
.unwrap();
let id = ask(&db, analysis, s.user, "Mire figyeljünk egy vitában?", "pending", None).await;
let (fake, url) = fake_openrouter(vec![reply(&json!({"answer": "Arra, hogy..."}))]).await;
let a = analyzer(&db, &url);
assert!(answer_once(&a).await);
let status: String =
sqlx::query_scalar("SELECT status FROM questions WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
assert_eq!(status, "done");
let requests = fake.requests.lock().unwrap();
let user = requests[0]["messages"][1]["content"].as_str().unwrap();
assert!(user.starts_with("Képletadatok:\n\nSYNASTRY") && user.contains("Person 2"), "both charts");
assert!(!user.contains("Szeged") && !user.contains("Pécs"));
}
} }