Tarot readings: AI-written, drawn on the server, sold as units
CI / test (push) Successful in 1m29s
CI / build (push) Successful in 5m52s
CI / deploy (push) Skipped

A new Kártyajóslás page: choose a spread (1, 3 or 5 cards), ask a
question, pick the cards from the fanned deck and watch them turn over;
the reading is written by the detailed-analysis model from the cards,
their positions and base meanings, the question and the reader's Sun,
Moon and Ascendant. The server shuffles and draws, so nobody can draw
again for free. A reading is a 'tarot' analysis (worker, retries,
fallback, refunds and admin regeneration as for analyses), uses one
'tarot' unit, and survives recalculations; deleting the account
deletes it.

Packages carry 2/5/10 readings, the new +5 kártyajóslás addon costs
1 000 Ft. The card artwork comes from the tarot design studio
(celestial astrolabe style); the deck JSON is shared by the frontend
and the prompt. A public example is at /kartyajoslas/pelda.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-20 14:35:50 +00:00
co-authored by Claude Opus 5.5
parent 54af54cd39
commit b0fc547b73
36 changed files with 2957 additions and 46 deletions
+12 -3
View File
@@ -52,6 +52,8 @@ pub struct Job {
pub model: String,
pub prompt_version: String,
pub attempts: i32,
/// A tarot reading's spread, question and cards (kind `tarot`).
pub tarot: Option<Value>,
/// The default model, when the chosen one is another (see the module docs).
pub fallback_model: Option<String>,
pub on_fallback: bool,
@@ -112,7 +114,8 @@ pub struct PreviewJob {
/// Which analyses a claim may take.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lane {
/// The short summary of a chart (questions are claimed separately).
/// The short summary of a chart and the tarot readings (questions are
/// claimed separately).
Quick,
/// The detailed analyses and the yearly forecast.
Slow,
@@ -121,7 +124,7 @@ pub enum Lane {
impl Lane {
fn kinds(self) -> &'static [&'static str] {
match self {
Lane::Quick => &["summary"],
Lane::Quick => &["summary", "tarot"],
Lane::Slow => &["natal", "synastry", "forecast"],
}
}
@@ -162,7 +165,7 @@ impl Analyzer {
AND kind = ANY($1) \
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, \
fallback_model, on_fallback, started_at",
tarot, fallback_model, on_fallback, started_at",
)
.bind(lane.kinds())
.fetch_optional(&self.db)
@@ -175,6 +178,11 @@ impl Analyzer {
/// Builds the prompt from the stored chart or synastry.
pub async fn prompt_for(&self, job: &Job) -> Result<Prompt, LlmError> {
if job.kind == "tarot" {
let value = job.tarot.as_ref().ok_or_else(|| LlmError::Permanent("the reading has no cards".into()))?;
let reading = prompt::TarotReading::parse(value).map_err(LlmError::Permanent)?;
return Ok(prompt::build_tarot(&job.language, &reading));
}
let kind = Kind::parse(&job.kind).ok_or_else(|| LlmError::Permanent(format!("unknown kind '{}'", job.kind)))?;
let db_err = |e: sqlx::Error| LlmError::Transient(format!("database: {e}"));
let text = match kind {
@@ -434,6 +442,7 @@ impl Analyzer {
let unit = match kind.as_str() {
"synastry" => "synastry",
"forecast" => "forecast",
"tarot" => "tarot",
_ => "natal",
};
sqlx::query(
+62
View File
@@ -927,4 +927,66 @@ mod analyses {
assert!(user.starts_with("Képletadatok:\n\nSYNASTRY") && user.contains("Person 2"), "both charts");
assert!(!user.contains("Szeged") && !user.contains("Pécs"));
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn tarot_readings_are_written_in_the_quick_lane_and_refunded_on_failure(db: PgPool) {
let s = setup(&db).await;
let tarot = json!({"spread": "trinity", "question": "Merre induljak tovább?",
"cards": [{"card": "major-0", "reversed": false}, {"card": "cups-3", "reversed": true},
{"card": "major-17", "reversed": false}],
"sun": "cancer", "moon": "pisces", "ascendant": "aries"});
let reading = |id: Uuid| {
sqlx::query(
"INSERT INTO analyses (id, kind, requested_by, language, model, prompt_version, cache_key, \
credits_charged, tarot) \
VALUES ($1, 'tarot', $2, 'hu', 'test/analysis', 'v3', 'tarot|x', 1, $3)",
)
.bind(id)
.bind(s.user)
.bind(&tarot)
};
let done = Uuid::now_v7();
reading(done).execute(&db).await.unwrap();
let written = json!({"title": "Új kezdet csendben", "summary": "Összefoglaló.",
"cards": [{"body": "Első."}, {"body": "Második."}, {"body": "Harmadik."}],
"synthesis": "Együtt.", "advice": "Lépj."});
let (fake, url) = fake_openrouter(vec![reply(&written), (400, json!({"error": {"message": "bad"}}))]).await;
let a = analyzer(&db, &url);
// Not in the slow lane: a reading should not wait behind long analyses.
assert!(a.claim(Lane::Slow).await.unwrap().is_none());
let job = a.claim(Lane::Quick).await.unwrap().expect("the reading");
a.handle(job).await;
let (status, title): (String, Option<String>) =
sqlx::query_as("SELECT status, content->>'title' FROM analyses WHERE id = $1")
.bind(done)
.fetch_one(&db)
.await
.unwrap();
assert_eq!((status.as_str(), title.as_deref()), ("done", Some("Új kezdet csendben")));
{
let requests = fake.requests.lock().unwrap();
assert_eq!(requests[0]["model"], "test/analysis");
let user = requests[0]["messages"][1]["content"].as_str().unwrap();
assert!(
user.contains("Merre induljak tovább?") && user.contains("A Bolond") && user.contains("Nap: Rák"),
"{user}"
);
assert_eq!(requests[0]["response_format"]["json_schema"]["name"], "tarot_reading");
}
// A permanent failure gives the tarot unit back.
let failed = Uuid::now_v7();
reading(failed).execute(&db).await.unwrap();
let job = a.claim(Lane::Quick).await.unwrap().expect("the second reading");
a.handle(job).await;
let (status, refund): (String, Option<String>) = sqlx::query_as(
"SELECT a.status, (SELECT unit FROM credit_ledger WHERE analysis_id = a.id AND reason = 'refund') \
FROM analyses a WHERE a.id = $1",
)
.bind(failed)
.fetch_one(&db)
.await
.unwrap();
assert_eq!((status.as_str(), refund.as_deref()), ("failed", Some("tarot")));
}
}