Write text analyses with OpenRouter in the worker
- analysis job: claims pending analyses (SKIP LOCKED, NOTIFY wake-up), builds a compact chart text without birth data, asks OpenRouter with a strict JSON schema, validates and stores the answer, model used, tokens and cost; retries transient errors, refunds credits on final failure, requeues interrupted jobs - prompt v1 for natal and synastry analyses, Hungarian style guide - compare-models subcommand for blind model comparison - migration 0003: attempts, next_attempt_at, model_used, cost_usd, notify trigger, one refund per analysis - db: rebuild when migrations change - compose: worker reads OPENROUTER_API_KEY from .env.secrets Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,384 @@
|
||||
//! Compact text views of charts for the language model.
|
||||
//!
|
||||
//! Only astrological positions go in: no names, birth date, time, place or
|
||||
//! coordinates. Plain text is shorter than the JSON and easier for models.
|
||||
|
||||
use std::fmt::Write;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
/// Points listed one per line; asteroids go on a shorter line.
|
||||
const MAIN_POINTS: [&str; 14] = [
|
||||
"sun",
|
||||
"moon",
|
||||
"mercury",
|
||||
"venus",
|
||||
"mars",
|
||||
"jupiter",
|
||||
"saturn",
|
||||
"uranus",
|
||||
"neptune",
|
||||
"pluto",
|
||||
"chiron",
|
||||
"north_node",
|
||||
"south_node",
|
||||
"lilith",
|
||||
];
|
||||
const ASTEROIDS: [&str; 4] = ["ceres", "pallas", "juno", "vesta"];
|
||||
const ANGLES: [&str; 4] = ["ascendant", "midheaven", "descendant", "imum_coeli"];
|
||||
const MAX_SYNASTRY_ASPECTS: usize = 45;
|
||||
|
||||
fn point_name(id: &str) -> String {
|
||||
match id {
|
||||
"north_node" => "North Node".into(),
|
||||
"south_node" => "South Node".into(),
|
||||
"lilith" => "Lilith (Black Moon)".into(),
|
||||
"midheaven" => "Midheaven (MC)".into(),
|
||||
"imum_coeli" => "Imum Coeli (IC)".into(),
|
||||
"ascendant" => "Ascendant".into(),
|
||||
"descendant" => "Descendant".into(),
|
||||
other => capitalize(other),
|
||||
}
|
||||
}
|
||||
|
||||
fn capitalize(s: &str) -> String {
|
||||
let mut chars = s.chars();
|
||||
chars.next().map(|c| c.to_uppercase().chain(chars).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn words(s: &str) -> String {
|
||||
s.replace('_', " ")
|
||||
}
|
||||
|
||||
/// "Cancer 0°24'" from a position object (sign + degree_in_sign).
|
||||
fn position(p: &Value) -> String {
|
||||
let degree = p["degree_in_sign"].as_f64().unwrap_or(0.0);
|
||||
// Rounded to the second first, as in the `degree_in_sign_dms` field; never 30°.
|
||||
let secs = ((degree * 3600.0).round() as i64).clamp(0, 30 * 3600 - 1);
|
||||
let (d, m) = (secs / 3600, secs % 3600 / 60);
|
||||
format!("{} {d}°{m:02}'", capitalize(p["sign"].as_str().unwrap_or("?")))
|
||||
}
|
||||
|
||||
fn time_known(chart: &Value) -> bool {
|
||||
chart["time"]["time_known"].as_bool().unwrap_or(false)
|
||||
}
|
||||
|
||||
fn is_main(id: &str) -> bool {
|
||||
MAIN_POINTS.contains(&id) || ANGLES.contains(&id)
|
||||
}
|
||||
|
||||
fn points(chart: &Value) -> impl Iterator<Item = &Value> {
|
||||
chart["points"].as_array().into_iter().flatten()
|
||||
}
|
||||
|
||||
fn names(list: &Value) -> String {
|
||||
list.as_array().into_iter().flatten().filter_map(Value::as_str).map(point_name).collect::<Vec<_>>().join(", ")
|
||||
}
|
||||
|
||||
/// One line per point: "Sun: Cancer 0°24', house 3, retrograde, domicile".
|
||||
fn point_lines(out: &mut String, chart: &Value, prefix: &str, houses: bool) {
|
||||
for p in points(chart).filter(|p| MAIN_POINTS.contains(&p["id"].as_str().unwrap_or(""))) {
|
||||
let mut line = format!("{prefix}- {}: {}", point_name(p["id"].as_str().unwrap_or("?")), position(p));
|
||||
if houses && let Some(h) = p["house"].as_i64() {
|
||||
let _ = write!(line, ", house {h}");
|
||||
}
|
||||
if p["retrograde"].as_bool() == Some(true) {
|
||||
line.push_str(", retrograde");
|
||||
}
|
||||
for dignity in p["dignities"].as_array().into_iter().flatten().filter_map(Value::as_str) {
|
||||
let _ = write!(line, ", {}", words(dignity));
|
||||
}
|
||||
if p["out_of_bounds"].as_bool() == Some(true) {
|
||||
line.push_str(", out of bounds");
|
||||
}
|
||||
let _ = writeln!(out, "{line}");
|
||||
}
|
||||
let minor: Vec<String> = points(chart)
|
||||
.filter(|p| ASTEROIDS.contains(&p["id"].as_str().unwrap_or("")))
|
||||
.map(|p| {
|
||||
let house = if houses {
|
||||
p["house"].as_i64().map(|h| format!(" (house {h})")).unwrap_or_default()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
format!("{} {}{house}", point_name(p["id"].as_str().unwrap_or("?")), position(p))
|
||||
})
|
||||
.collect();
|
||||
if !minor.is_empty() {
|
||||
let _ = writeln!(out, "{prefix}- Asteroids: {}", minor.join("; "));
|
||||
}
|
||||
}
|
||||
|
||||
fn angle_lines(out: &mut String, chart: &Value, prefix: &str) {
|
||||
let angles = &chart["angles"];
|
||||
for (id, label) in [("ascendant", "Ascendant"), ("midheaven", "Midheaven (MC)")] {
|
||||
if angles[id].is_object() {
|
||||
let _ = writeln!(out, "{prefix}- {label}: {}", position(&angles[id]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const BIRTH_TIME_UNKNOWN: &str = "Birth time: UNKNOWN. The chart is cast for noon, so there are no houses, \
|
||||
no Ascendant and no Midheaven, and the Moon may be up to about 7° off (its sign is uncertain near a sign boundary).";
|
||||
|
||||
/// A natal chart (the horoscope-api `/v1/natal` result).
|
||||
pub fn natal(chart: &Value) -> String {
|
||||
let mut out = String::new();
|
||||
let settings = &chart["settings"];
|
||||
let known = time_known(chart);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"NATAL CHART ({} zodiac, {} houses)",
|
||||
settings["zodiac"].as_str().unwrap_or("tropical"),
|
||||
capitalize(&words(
|
||||
chart["houses"]["system"].as_str().or(settings["house_system"].as_str()).unwrap_or("placidus")
|
||||
))
|
||||
);
|
||||
let _ = writeln!(out, "{}", if known { "Birth time: known." } else { BIRTH_TIME_UNKNOWN });
|
||||
let summary = &chart["summary"];
|
||||
if let Some(sect) = summary["sect"].as_str() {
|
||||
let _ = writeln!(out, "Sect: {sect} chart.");
|
||||
}
|
||||
let phase = &chart["moon_phase"];
|
||||
if let Some(name) = phase["phase"].as_str() {
|
||||
let illumination = phase["illumination"].as_f64().map(|i| format!(" ({:.0}% illuminated)", i * 100.0));
|
||||
let _ = writeln!(out, "Moon phase: {}{}.", words(name), illumination.unwrap_or_default());
|
||||
}
|
||||
|
||||
if known {
|
||||
out.push_str("\nAngles:\n");
|
||||
angle_lines(&mut out, chart, "");
|
||||
let ruler = &summary["chart_ruler"];
|
||||
for kind in ["traditional", "modern"] {
|
||||
let r = &ruler[kind];
|
||||
if let Some(planet) = r["planet"].as_str() {
|
||||
if kind == "modern" && ruler["traditional"]["planet"] == r["planet"] {
|
||||
continue;
|
||||
}
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"- Chart ruler ({kind}): {} in {}, house {}",
|
||||
point_name(planet),
|
||||
capitalize(r["sign"].as_str().unwrap_or("?")),
|
||||
r["house"]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str("\nPlanets and points:\n");
|
||||
point_lines(&mut out, chart, "", known);
|
||||
|
||||
if known && let Some(cusps) = chart["houses"]["cusps"].as_array() {
|
||||
let list: Vec<String> = cusps.iter().map(|c| format!("{} {}", c["house"], position(c))).collect();
|
||||
let _ = writeln!(out, "\nHouse cusps: {}", list.join(" | "));
|
||||
}
|
||||
|
||||
aspect_lines(&mut out, &chart["aspects"], "\nAspects (tightest first; orb in degrees):", usize::MAX);
|
||||
|
||||
out.push_str("\nBalance (main planets):\n");
|
||||
for (title, key) in [("Elements", "elements"), ("Modalities", "modalities")] {
|
||||
if let Some(groups) = summary[key].as_object() {
|
||||
let list: Vec<String> = groups
|
||||
.iter()
|
||||
.map(|(name, g)| {
|
||||
let members = names(&g["points"]);
|
||||
if members.is_empty() { format!("{name} 0") } else { format!("{name} {} ({members})", g["count"]) }
|
||||
})
|
||||
.collect();
|
||||
let _ = writeln!(out, "- {title}: {}", list.join("; "));
|
||||
}
|
||||
}
|
||||
if known && let Some(h) = summary["hemispheres"].as_object() {
|
||||
let list: Vec<String> = h.iter().map(|(name, g)| format!("{} {}", words(name), g["count"])).collect();
|
||||
let _ = writeln!(out, "- Hemispheres: {}", list.join(", "));
|
||||
}
|
||||
let retro = names(&summary["retrograde"]);
|
||||
if !retro.is_empty() {
|
||||
let _ = writeln!(out, "- Retrograde: {retro}");
|
||||
}
|
||||
if known && let Some(fortune) = chart["lots"]["fortune"].as_object() {
|
||||
let fortune = Value::Object(fortune.clone());
|
||||
let _ = writeln!(out, "- Part of Fortune: {}, house {}", position(&fortune), fortune["house"]);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A synastry: both natal charts plus the synastry result (inter-aspects,
|
||||
/// house overlays, composite) as stored by the backend.
|
||||
pub fn synastry(chart1: &Value, chart2: &Value, result: &Value) -> String {
|
||||
let mut out = String::from("SYNASTRY: two natal charts compared (Person 1 and Person 2), tropical zodiac.\n");
|
||||
for (label, chart) in [("Person 1", chart1), ("Person 2", chart2)] {
|
||||
let _ = writeln!(out, "\n{label}:");
|
||||
if time_known(chart) {
|
||||
let _ = writeln!(out, " Birth time: known.");
|
||||
angle_lines(&mut out, chart, " ");
|
||||
} else {
|
||||
let _ = writeln!(out, " {BIRTH_TIME_UNKNOWN}");
|
||||
}
|
||||
point_lines(&mut out, chart, " ", time_known(chart));
|
||||
}
|
||||
|
||||
let mut aspects: Vec<&Value> = result["aspects"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|a| is_main(a["person1"].as_str().unwrap_or("")) && is_main(a["person2"].as_str().unwrap_or("")))
|
||||
.collect();
|
||||
aspects.sort_by(|a, b| a["orb"].as_f64().unwrap_or(99.0).total_cmp(&b["orb"].as_f64().unwrap_or(99.0)));
|
||||
if !aspects.is_empty() {
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"\nInter-chart aspects (Person 1's point first; tightest {} of {}):",
|
||||
aspects.len().min(MAX_SYNASTRY_ASPECTS),
|
||||
aspects.len()
|
||||
);
|
||||
for a in aspects.iter().take(MAX_SYNASTRY_ASPECTS) {
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"- {} {} {} (orb {:.1}°, {})",
|
||||
point_name(a["person1"].as_str().unwrap_or("?")),
|
||||
words(a["aspect"].as_str().unwrap_or("?")),
|
||||
point_name(a["person2"].as_str().unwrap_or("?")),
|
||||
a["orb"].as_f64().unwrap_or(0.0),
|
||||
a["nature"].as_str().unwrap_or("neutral")
|
||||
);
|
||||
}
|
||||
}
|
||||
if let Some(by_nature) = result["summary"]["by_nature"].as_object() {
|
||||
let list: Vec<String> = by_nature.iter().map(|(k, v)| format!("{k} {v}")).collect();
|
||||
let _ = writeln!(out, "All aspects by nature: {}", list.join(", "));
|
||||
}
|
||||
|
||||
let overlays = &result["house_overlays"];
|
||||
for (key, label) in [
|
||||
("person1_in_person2", "Person 1's planets in Person 2's houses"),
|
||||
("person2_in_person1", "Person 2's planets in Person 1's houses"),
|
||||
] {
|
||||
let list: Vec<String> = overlays[key]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|o| MAIN_POINTS.contains(&o["point"].as_str().unwrap_or("")))
|
||||
.filter_map(|o| Some(format!("{} {}", point_name(o["point"].as_str()?), o["house"].as_i64()?)))
|
||||
.collect();
|
||||
if !list.is_empty() {
|
||||
let _ = writeln!(out, "\n{label}: {}", list.join(", "));
|
||||
}
|
||||
}
|
||||
|
||||
let composite = &result["composite"];
|
||||
if composite["points"].is_array() {
|
||||
let both_known = time_known(chart1) && time_known(chart2);
|
||||
let _ = writeln!(out, "\nComposite chart ({} method):", composite["method"].as_str().unwrap_or("midpoint"));
|
||||
if both_known {
|
||||
angle_lines(&mut out, composite, "");
|
||||
}
|
||||
point_lines(&mut out, composite, "", both_known);
|
||||
aspect_lines(&mut out, &composite["aspects"], "Composite aspects (tightest first):", 12);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Aspects between main points and angles, tightest first.
|
||||
fn aspect_lines(out: &mut String, aspects: &Value, heading: &str, limit: usize) {
|
||||
let mut list: Vec<&Value> = aspects
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|a| is_main(a["point1"].as_str().unwrap_or("")) && is_main(a["point2"].as_str().unwrap_or("")))
|
||||
.collect();
|
||||
if list.is_empty() {
|
||||
return;
|
||||
}
|
||||
list.sort_by(|a, b| a["orb"].as_f64().unwrap_or(99.0).total_cmp(&b["orb"].as_f64().unwrap_or(99.0)));
|
||||
let _ = writeln!(out, "{heading}");
|
||||
for a in list.into_iter().take(limit) {
|
||||
let motion = match a["applying"].as_bool() {
|
||||
Some(true) => ", applying",
|
||||
Some(false) => ", separating",
|
||||
None => "",
|
||||
};
|
||||
let _ = writeln!(
|
||||
out,
|
||||
"- {} {} {} (orb {:.1}°{motion})",
|
||||
point_name(a["point1"].as_str().unwrap_or("?")),
|
||||
words(a["aspect"].as_str().unwrap_or("?")),
|
||||
point_name(a["point2"].as_str().unwrap_or("?")),
|
||||
a["orb"].as_f64().unwrap_or(0.0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn fixture(name: &str) -> Value {
|
||||
let path = format!("{}/testdata/{name}", env!("CARGO_MANIFEST_DIR"));
|
||||
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
/// Birth data that must never reach the model.
|
||||
fn assert_private(text: &str) {
|
||||
for secret in ["1990", "1988", "14:30", "08:10", "Szeged", "Pécs", "Hungary", "HU", "46.", "20.1", "Budapest"]
|
||||
{
|
||||
assert!(!text.contains(secret), "'{secret}' leaked into:\n{text}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn natal_with_birth_time() {
|
||||
let text = natal(&fixture("natal-1990-03-15.json"));
|
||||
assert_private(&text);
|
||||
for expected in [
|
||||
"Birth time: known.",
|
||||
"- Ascendant: Leo 19°23'",
|
||||
"- Chart ruler (traditional): Sun in Pisces, house 8",
|
||||
"- Sun: Pisces 24°42', house 8",
|
||||
"- Pluto: Scorpio 17°36', house 4, retrograde, domicile",
|
||||
"- Asteroids: Ceres Gemini 25°43' (house 11);",
|
||||
"House cusps: 1 Leo 19°23' | 2 Virgo 9°35'",
|
||||
"- Moon trine Chiron (orb 0.1°, applying)",
|
||||
"- Elements: air 2 (Venus, Mars); earth 3 (Saturn, Uranus, Neptune); fire 0;",
|
||||
"- Part of Fortune: Aries 5°10', house 8",
|
||||
] {
|
||||
assert!(text.contains(expected), "missing '{expected}' in:\n{text}");
|
||||
}
|
||||
// Tightest aspect first.
|
||||
assert!(text.find("Moon trine Chiron").unwrap() < text.find("Moon square Ascendant").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn natal_without_birth_time_has_no_houses() {
|
||||
let text = natal(&fixture("natal-1988-11-02-notime.json"));
|
||||
assert_private(&text);
|
||||
assert!(text.contains("Birth time: UNKNOWN"));
|
||||
for absent in
|
||||
["house ", "House cusps", "Ascendant:", "Midheaven (MC):", "Chart ruler", "Hemispheres", "Fortune"]
|
||||
{
|
||||
assert!(!text.contains(absent), "'{absent}' in:\n{text}");
|
||||
}
|
||||
assert!(text.contains("- Venus: Libra 4°12', domicile"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn synastry_view() {
|
||||
// The full horoscope-api response, with both persons' birth data in it.
|
||||
let response = fixture("synastry-response.json");
|
||||
let text = synastry(&response["person1"], &response["person2"], &response);
|
||||
assert_private(&text);
|
||||
for expected in [
|
||||
"Person 1:\n Birth time: known.",
|
||||
" - Sun: Pisces 24°42', house 8",
|
||||
"Inter-chart aspects (Person 1's point first; tightest 45 of",
|
||||
"(orb 0.0°, harmonious)",
|
||||
"All aspects by nature: ",
|
||||
"Person 1's planets in Person 2's houses: Sun 4, Moon 12",
|
||||
"Composite chart (midpoint method):\n- Ascendant: Libra 8°39'",
|
||||
"Composite aspects (tightest first):\n- Saturn conjunction Neptune (orb 0.1°)",
|
||||
] {
|
||||
assert!(text.contains(expected), "missing '{expected}' in:\n{text}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
//! Writes the requested text analyses (`analyses` rows in `pending` state).
|
||||
//!
|
||||
//! The backend inserts the row and charges the credits; this job claims it
|
||||
//! (`running`), builds the prompt from the stored chart data, asks the model
|
||||
//! through OpenRouter and stores the structured answer (`done`). Temporary
|
||||
//! failures are retried with backoff; after the last attempt, or on a
|
||||
//! permanent error, the analysis is `failed` and the credits are refunded.
|
||||
|
||||
pub mod compact;
|
||||
pub mod openrouter;
|
||||
pub mod prompt;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
use tokio::sync::{Semaphore, watch};
|
||||
use uuid::Uuid;
|
||||
|
||||
use self::openrouter::{Completion, LlmError, OpenRouter};
|
||||
use self::prompt::{Kind, PROMPT_VERSION, Prompt};
|
||||
|
||||
/// A `running` analysis older than this was left behind by a stopped worker.
|
||||
const STUCK_AFTER_MINUTES: i32 = 15;
|
||||
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
pub struct Job {
|
||||
pub id: Uuid,
|
||||
pub kind: String,
|
||||
pub chart_id: Option<Uuid>,
|
||||
pub synastry_id: Option<Uuid>,
|
||||
pub language: String,
|
||||
pub model: String,
|
||||
pub prompt_version: String,
|
||||
pub attempts: i32,
|
||||
}
|
||||
|
||||
pub struct Analyzer {
|
||||
pub db: PgPool,
|
||||
pub llm: OpenRouter,
|
||||
pub max_attempts: i32,
|
||||
pub concurrency: usize,
|
||||
/// Analyses being written right now (put back to `pending` on shutdown).
|
||||
running: Mutex<HashSet<Uuid>>,
|
||||
}
|
||||
|
||||
/// Wait before the next attempt: 30 s, 2 min, 8 min, ... at most 1 hour.
|
||||
pub fn backoff(attempts: i32) -> Duration {
|
||||
let exp = attempts.clamp(1, 6).saturating_sub(1) as u32;
|
||||
Duration::from_secs((30 * 4u64.pow(exp)).min(3600))
|
||||
}
|
||||
|
||||
impl Analyzer {
|
||||
pub fn new(db: PgPool, llm: OpenRouter, max_attempts: i32, concurrency: usize) -> Self {
|
||||
Self { db, llm, max_attempts, concurrency: concurrency.max(1), running: Mutex::default() }
|
||||
}
|
||||
|
||||
/// Takes the next due analysis, if any.
|
||||
pub async fn claim(&self) -> Result<Option<Job>, sqlx::Error> {
|
||||
let job: Option<Job> = sqlx::query_as(
|
||||
"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() \
|
||||
ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \
|
||||
RETURNING id, kind, chart_id, synastry_id, language, model, prompt_version, attempts",
|
||||
)
|
||||
.fetch_optional(&self.db)
|
||||
.await?;
|
||||
if let Some(job) = &job {
|
||||
self.running.lock().unwrap().insert(job.id);
|
||||
}
|
||||
Ok(job)
|
||||
}
|
||||
|
||||
/// Builds the prompt from the stored chart or synastry.
|
||||
pub async fn prompt_for(&self, job: &Job) -> Result<Prompt, LlmError> {
|
||||
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 gone = || LlmError::Permanent("the chart was deleted".into());
|
||||
let text = match kind {
|
||||
Kind::Natal => {
|
||||
let (chart,): (Value,) = sqlx::query_as("SELECT result FROM charts WHERE id = $1")
|
||||
.bind(job.chart_id)
|
||||
.fetch_optional(&self.db)
|
||||
.await
|
||||
.map_err(db_err)?
|
||||
.ok_or_else(gone)?;
|
||||
compact::natal(&chart)
|
||||
}
|
||||
Kind::Synastry => {
|
||||
let (result, chart1, chart2): (Value, Value, Value) = sqlx::query_as(
|
||||
"SELECT s.result, c1.result, c2.result FROM synastries s \
|
||||
JOIN charts c1 ON c1.id = s.chart1_id JOIN charts c2 ON c2.id = s.chart2_id WHERE s.id = $1",
|
||||
)
|
||||
.bind(job.synastry_id)
|
||||
.fetch_optional(&self.db)
|
||||
.await
|
||||
.map_err(db_err)?
|
||||
.ok_or_else(gone)?;
|
||||
compact::synastry(&chart1, &chart2, &result)
|
||||
}
|
||||
};
|
||||
if job.prompt_version != PROMPT_VERSION {
|
||||
tracing::warn!(id = %job.id, requested = %job.prompt_version, current = PROMPT_VERSION, "prompt version differs");
|
||||
}
|
||||
Ok(prompt::build(kind, &job.language, &text))
|
||||
}
|
||||
|
||||
/// Writes one claimed analysis and records the outcome.
|
||||
pub async fn handle(&self, job: Job) {
|
||||
let started = Instant::now();
|
||||
let result = match self.prompt_for(&job).await {
|
||||
Ok(prompt) => self.llm.complete(&job.model, &prompt).await,
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
if let Err(e) = self.finish(&job, result, started.elapsed()).await {
|
||||
tracing::error!(id = %job.id, error = %e, "saving the analysis result failed");
|
||||
}
|
||||
self.running.lock().unwrap().remove(&job.id);
|
||||
}
|
||||
|
||||
async fn finish(&self, job: &Job, result: Result<Completion, LlmError>, took: Duration) -> Result<(), sqlx::Error> {
|
||||
match result {
|
||||
Ok(c) => {
|
||||
sqlx::query(
|
||||
"UPDATE analyses SET status = 'done', content = $2, model_used = $3, input_tokens = $4, \
|
||||
output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \
|
||||
WHERE id = $1 AND status = 'running'",
|
||||
)
|
||||
.bind(job.id)
|
||||
.bind(&c.content)
|
||||
.bind(&c.model)
|
||||
.bind(c.input_tokens)
|
||||
.bind(c.output_tokens)
|
||||
.bind(c.cost_usd)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
tracing::info!(
|
||||
id = %job.id, model = %c.model, input_tokens = c.input_tokens, output_tokens = c.output_tokens,
|
||||
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) => {
|
||||
self.fail(job.id, e.message()).await?;
|
||||
tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "analysis failed");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Marks the analysis failed and gives the credits back (once).
|
||||
async fn fail(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> {
|
||||
let mut tx = self.db.begin().await?;
|
||||
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
|
||||
"UPDATE analyses SET status = 'failed', error = $2, completed_at = now() \
|
||||
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(message)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?;
|
||||
if let Some((Some(user), credits)) = row
|
||||
&& credits > 0
|
||||
{
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, delta, reason, analysis_id, note) \
|
||||
VALUES ($1, $2, $3, 'refund', $4, 'analysis failed') ON CONFLICT DO NOTHING",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(user)
|
||||
.bind(credits)
|
||||
.bind(id)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
tx.commit().await
|
||||
}
|
||||
|
||||
/// Puts analyses left `running` by a stopped worker back in the queue.
|
||||
pub async fn requeue_stuck(&self) -> Result<u64, sqlx::Error> {
|
||||
let result = sqlx::query(
|
||||
"UPDATE analyses SET status = 'pending', error = 'interrupted', next_attempt_at = now() \
|
||||
WHERE status = 'running' AND started_at < now() - make_interval(mins => $1)",
|
||||
)
|
||||
.bind(STUCK_AFTER_MINUTES)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(result.rows_affected())
|
||||
}
|
||||
|
||||
/// On shutdown: analyses still being written go back to `pending` at once.
|
||||
async fn release_running(&self) {
|
||||
let ids: Vec<Uuid> = self.running.lock().unwrap().drain().collect();
|
||||
if ids.is_empty() {
|
||||
return;
|
||||
}
|
||||
let result = sqlx::query(
|
||||
"UPDATE analyses SET status = 'pending', error = 'interrupted', next_attempt_at = now(), \
|
||||
attempts = greatest(attempts - 1, 0) WHERE id = ANY($1) AND status = 'running'",
|
||||
)
|
||||
.bind(&ids)
|
||||
.execute(&self.db)
|
||||
.await;
|
||||
match result {
|
||||
Ok(r) => tracing::info!(released = r.rows_affected(), "unfinished analyses put back in the queue"),
|
||||
Err(e) => tracing::error!(error = %e, "releasing unfinished analyses"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(analyzer: Arc<Analyzer>, poll: Duration, stop: watch::Receiver<bool>) {
|
||||
let slots = Arc::new(Semaphore::new(analyzer.concurrency));
|
||||
let mut tasks = tokio::task::JoinSet::new();
|
||||
let mut listener = crate::listen(&analyzer.db, "analyses").await;
|
||||
let mut last_requeue: Option<Instant> = None;
|
||||
loop {
|
||||
if *stop.borrow() {
|
||||
break;
|
||||
}
|
||||
if last_requeue.is_none_or(|t| t.elapsed() > Duration::from_secs(60)) {
|
||||
last_requeue = Some(Instant::now());
|
||||
match analyzer.requeue_stuck().await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::warn!(requeued = n, "interrupted analyses requeued"),
|
||||
Err(e) => tracing::error!(error = %e, "requeueing analyses"),
|
||||
}
|
||||
}
|
||||
// Fill the free slots.
|
||||
while let Ok(permit) = slots.clone().try_acquire_owned() {
|
||||
match analyzer.claim().await {
|
||||
Ok(Some(job)) => {
|
||||
let analyzer = analyzer.clone();
|
||||
tasks.spawn(async move {
|
||||
analyzer.handle(job).await;
|
||||
drop(permit);
|
||||
});
|
||||
}
|
||||
Ok(None) => break,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "claiming an analysis");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
while tasks.try_join_next().is_some() {}
|
||||
|
||||
tokio::select! {
|
||||
() = crate::notified(listener.as_mut()) => {}
|
||||
() = tokio::time::sleep(poll) => {}
|
||||
Some(_) = tasks.join_next(), if !tasks.is_empty() => {}
|
||||
() = crate::stopped(stop.clone()) => break,
|
||||
}
|
||||
}
|
||||
// Give running analyses a moment, then hand the rest back to the queue.
|
||||
let _ = tokio::time::timeout(Duration::from_secs(10), async { while tasks.join_next().await.is_some() {} }).await;
|
||||
tasks.abort_all();
|
||||
analyzer.release_running().await;
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
//! OpenRouter chat completions with structured (JSON schema) output.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::prompt::{Prompt, validate};
|
||||
|
||||
pub const DEFAULT_BASE_URL: &str = "https://openrouter.ai/api/v1";
|
||||
/// Room for reasoning tokens as well as the answer.
|
||||
const MAX_TOKENS: u32 = 16_000;
|
||||
|
||||
pub struct OpenRouter {
|
||||
http: reqwest::Client,
|
||||
base_url: String,
|
||||
api_key: String,
|
||||
/// Sent as `HTTP-Referer` (app attribution on openrouter.ai).
|
||||
referer: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Completion {
|
||||
pub content: Value,
|
||||
/// The model that answered (differs from the requested one for routers like `openrouter/auto`).
|
||||
pub model: String,
|
||||
pub input_tokens: Option<i32>,
|
||||
pub output_tokens: Option<i32>,
|
||||
pub cost_usd: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum LlmError {
|
||||
/// Worth retrying later (network, rate limit, provider outage, malformed answer).
|
||||
Transient(String),
|
||||
/// Retrying will not help (bad request, unknown model, rejected key).
|
||||
Permanent(String),
|
||||
}
|
||||
|
||||
impl LlmError {
|
||||
pub fn message(&self) -> &str {
|
||||
match self {
|
||||
LlmError::Transient(m) | LlmError::Permanent(m) => m,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The JSON object in a reply, also when wrapped in a ``` fence or prose.
|
||||
fn extract_json(text: &str) -> Option<Value> {
|
||||
let trimmed = text.trim();
|
||||
if let Ok(v) = serde_json::from_str(trimmed) {
|
||||
return Some(v);
|
||||
}
|
||||
let (start, end) = (trimmed.find('{')?, trimmed.rfind('}')?);
|
||||
serde_json::from_str(trimmed.get(start..=end)?).ok()
|
||||
}
|
||||
|
||||
fn error_message(body: &Value, fallback: &str) -> String {
|
||||
let e = &body["error"];
|
||||
let message = e["message"].as_str().unwrap_or(fallback);
|
||||
match e["metadata"]["raw"].as_str() {
|
||||
Some(raw) => format!("{message}: {}", raw.chars().take(300).collect::<String>()),
|
||||
None => message.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenRouter {
|
||||
pub fn new(base_url: &str, api_key: &str, referer: Option<String>) -> Self {
|
||||
let http = reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(300))
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.expect("HTTP client");
|
||||
Self { http, base_url: base_url.trim_end_matches('/').to_owned(), api_key: api_key.to_owned(), referer }
|
||||
}
|
||||
|
||||
pub async fn complete(&self, model: &str, prompt: &Prompt) -> Result<Completion, LlmError> {
|
||||
let body = json!({
|
||||
"model": model,
|
||||
"messages": [
|
||||
{"role": "system", "content": prompt.system},
|
||||
{"role": "user", "content": prompt.user},
|
||||
],
|
||||
"response_format": {
|
||||
"type": "json_schema",
|
||||
"json_schema": {"name": prompt.schema_name, "strict": true, "schema": prompt.schema},
|
||||
},
|
||||
// Only route to providers that honour the schema.
|
||||
"provider": {"require_parameters": true},
|
||||
"max_tokens": MAX_TOKENS,
|
||||
"usage": {"include": true},
|
||||
});
|
||||
let mut request = self
|
||||
.http
|
||||
.post(format!("{}/chat/completions", self.base_url))
|
||||
.bearer_auth(&self.api_key)
|
||||
.header("X-Title", "Sorsfonalak")
|
||||
.json(&body);
|
||||
if let Some(referer) = &self.referer {
|
||||
request = request.header("HTTP-Referer", referer);
|
||||
}
|
||||
let response = request.send().await.map_err(|e| LlmError::Transient(format!("request failed: {e}")))?;
|
||||
let status = response.status();
|
||||
let text = response.text().await.map_err(|e| LlmError::Transient(format!("reading response: {e}")))?;
|
||||
let reply: Value = serde_json::from_str(&text).unwrap_or(Value::Null);
|
||||
|
||||
if !status.is_success() {
|
||||
let message = format!(
|
||||
"HTTP {}: {}",
|
||||
status.as_u16(),
|
||||
error_message(&reply, &text.chars().take(300).collect::<String>())
|
||||
);
|
||||
// 402: our OpenRouter balance ran out; 408/429/5xx: try again later.
|
||||
return Err(match status.as_u16() {
|
||||
402 | 408 | 429 | 500..=599 => LlmError::Transient(message),
|
||||
_ => LlmError::Permanent(message),
|
||||
});
|
||||
}
|
||||
if reply["error"].is_object() {
|
||||
return Err(LlmError::Transient(format!("provider error: {}", error_message(&reply, "unknown"))));
|
||||
}
|
||||
let choice = &reply["choices"][0];
|
||||
if choice["finish_reason"] == "length" {
|
||||
return Err(LlmError::Transient("answer cut off at the token limit".into()));
|
||||
}
|
||||
let raw = choice["message"]["content"].as_str().unwrap_or_default();
|
||||
let content = extract_json(raw).ok_or_else(|| {
|
||||
LlmError::Transient(format!("answer is not JSON: {}", raw.chars().take(200).collect::<String>()))
|
||||
})?;
|
||||
validate(&prompt.schema, &content)
|
||||
.map_err(|e| LlmError::Transient(format!("answer does not match the schema: {e}")))?;
|
||||
|
||||
let usage = &reply["usage"];
|
||||
let tokens = |key: &str| usage[key].as_i64().and_then(|n| i32::try_from(n).ok());
|
||||
Ok(Completion {
|
||||
content,
|
||||
model: reply["model"].as_str().unwrap_or(model).to_owned(),
|
||||
input_tokens: tokens("prompt_tokens"),
|
||||
output_tokens: tokens("completion_tokens"),
|
||||
cost_usd: usage["cost"].as_f64(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn extracts_json_from_fences_and_prose() {
|
||||
assert_eq!(extract_json(r#"{"a":1}"#), Some(json!({"a": 1})));
|
||||
assert_eq!(extract_json("```json\n{\"a\": 1}\n```"), Some(json!({"a": 1})));
|
||||
assert_eq!(extract_json("Here you go: {\"a\": {\"b\": 2}} Enjoy!"), Some(json!({"a": {"b": 2}})));
|
||||
assert_eq!(extract_json("no json here"), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
//! Prompts and output schemas of the analyses (prompt version [`PROMPT_VERSION`]).
|
||||
//!
|
||||
//! The model answers with one JSON object (OpenRouter structured output);
|
||||
//! [`validate`] checks it against the same schema before it is stored.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// Matches the backend's `ANALYSIS_PROMPT_VERSION` (part of the cache key).
|
||||
pub const PROMPT_VERSION: &str = "v1";
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
Natal,
|
||||
Synastry,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
pub fn parse(s: &str) -> Option<Kind> {
|
||||
match s {
|
||||
"natal" => Some(Kind::Natal),
|
||||
"synastry" => Some(Kind::Synastry),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Prompt {
|
||||
pub system: String,
|
||||
pub user: String,
|
||||
pub schema_name: &'static str,
|
||||
pub schema: Value,
|
||||
}
|
||||
|
||||
/// (key, what the section covers) in reading order.
|
||||
const NATAL_SECTIONS: [(&str, &str); 6] = [
|
||||
("personality", "Core identity: the Sun, Moon, Ascendant and chart ruler read together"),
|
||||
("emotions", "Emotional needs, security and inner life (Moon, 4th house, water emphasis)"),
|
||||
("mind", "Thinking, communication and learning (Mercury, 3rd and 9th houses)"),
|
||||
("love", "Love, attraction and relationships (Venus, Mars, 5th and 7th houses, Descendant)"),
|
||||
("work", "Calling, career, money and daily work (MC, 10th, 2nd and 6th houses, Saturn, Jupiter)"),
|
||||
(
|
||||
"growth",
|
||||
"Main inner tensions, life lessons and the path of growth (tight squares and oppositions, Saturn, Chiron, Nodes)",
|
||||
),
|
||||
];
|
||||
|
||||
const SYNASTRY_SECTIONS: [(&str, &str); 5] = [
|
||||
("attraction", "What draws them together: Sun, Moon, Venus, Mars and Ascendant contacts"),
|
||||
("communication", "How they think and talk together (Mercury contacts, air emphasis, 3rd house overlays)"),
|
||||
("emotional_bond", "Emotional closeness, safety and care (Moon contacts, 4th and 8th house overlays)"),
|
||||
("friction", "Where they rub against each other and how to handle it (challenging aspects, Saturn, Pluto, Mars)"),
|
||||
("long_term", "Long-term potential and shared purpose, including the composite chart"),
|
||||
];
|
||||
|
||||
fn sections(kind: Kind) -> &'static [(&'static str, &'static str)] {
|
||||
match kind {
|
||||
Kind::Natal => &NATAL_SECTIONS,
|
||||
Kind::Synastry => &SYNASTRY_SECTIONS,
|
||||
}
|
||||
}
|
||||
|
||||
/// The JSON schema of the answer (strict mode: every property required).
|
||||
pub fn schema(kind: Kind) -> Value {
|
||||
let mut props = serde_json::Map::new();
|
||||
for (key, about) in sections(kind) {
|
||||
props.insert(
|
||||
(*key).into(),
|
||||
json!({
|
||||
"type": "object",
|
||||
"description": about,
|
||||
"additionalProperties": false,
|
||||
"required": ["title", "body"],
|
||||
"properties": {
|
||||
"title": {"type": "string", "description": "Short, specific section title (not just the topic name)"},
|
||||
"body": {"type": "string", "description": "2-4 paragraphs separated by a blank line"}
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
let keys: Vec<&str> = sections(kind).iter().map(|(k, _)| *k).collect();
|
||||
json!({
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["title", "summary", "sections", "strengths", "challenges", "advice"],
|
||||
"properties": {
|
||||
"title": {"type": "string", "description": "Short, evocative title for this analysis (at most 8 words)"},
|
||||
"summary": {"type": "string", "description": "3-4 sentence overview"},
|
||||
"sections": {"type": "object", "additionalProperties": false, "required": keys, "properties": props},
|
||||
"strengths": {"type": "array", "items": {"type": "string"}, "description": "3-5 key strengths, one sentence each"},
|
||||
"challenges": {"type": "array", "items": {"type": "string"}, "description": "3-5 key challenges, one sentence each"},
|
||||
"advice": {"type": "string", "description": "A closing, encouraging paragraph with concrete guidance"}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const HUNGARIAN_STYLE: &str = "\
|
||||
Write natural, idiomatic Hungarian, as a native Hungarian author would; it must not read like a translation from English. \
|
||||
Use correct Hungarian suffixes and vowel harmony, Hungarian quotation marks („…”) and the en dash (–). \
|
||||
Use the usual Hungarian astrological terms:
|
||||
- signs: Kos, Bika, Ikrek, Rák, Oroszlán, Szűz, Mérleg, Skorpió, Nyilas, Bak, Vízöntő, Halak (\"a Nap a Rák jegyében\", \"Kos Aszcendens\");
|
||||
- planets and points: Nap, Hold, Merkúr, Vénusz, Mars, Jupiter, Szaturnusz, Uránusz, Neptunusz, Plútó, Chiron (Kiron), \
|
||||
Észak-Holdcsomópont, Dél-Holdcsomópont, Lilith (Fekete Hold), Aszcendens, Descendens, Medium Coeli (MC), Imum Coeli (IC), Szerencsepont;
|
||||
- aspects: együttállás (konjunkció), szextil, kvadrát (négyzet), trigon, szembenállás (oppozíció);
|
||||
- other: \"a 3. házban\", \"a horoszkóp uralkodója\", retrográd, uralom (domicilium), száműzetés, exaltáció, esés, nappali/éjszakai horoszkóp.";
|
||||
|
||||
fn language_name(language: &str) -> &'static str {
|
||||
match language {
|
||||
"hu" => "Hungarian",
|
||||
_ => "English",
|
||||
}
|
||||
}
|
||||
|
||||
fn rules(kind: Kind, language: &str) -> String {
|
||||
let (role, reader) = match kind {
|
||||
Kind::Natal => (
|
||||
"a personal natal chart interpretation",
|
||||
"Speak to the owner of the chart directly, in the informal second person singular (in Hungarian: tegező forma, \"te\").",
|
||||
),
|
||||
Kind::Synastry => (
|
||||
"a relationship (synastry) interpretation for two people",
|
||||
"Write for both people at once, in the informal second person plural (in Hungarian: \"ti\", \"kettőtök\"). \
|
||||
When you must tell them apart, call them Person 1 and Person 2 (in Hungarian: \"az első fél\" and \"a második fél\"). \
|
||||
Do not assume their genders or the type of relationship.",
|
||||
),
|
||||
};
|
||||
let mut text = format!(
|
||||
"You are an experienced, warm and psychologically minded astrologer. You write {role} for the Sorsfonalak app.
|
||||
|
||||
Write in {lang}.
|
||||
|
||||
Rules:
|
||||
- Base every statement on the chart data given. Never invent positions, aspects or houses that are not listed.
|
||||
- When you interpret a placement, name its astrological basis in a short clause, but keep the focus on the person, not on jargon.
|
||||
- Synthesize: weave signs, houses, aspects and rulers into one coherent portrait; show how tensions can be integrated. \
|
||||
Give the most weight to the Sun, Moon, Ascendant, chart ruler, tight aspects (orb under 3°) and strong emphases or gaps in elements and modalities.
|
||||
- If a birth time is unknown, do not mention houses, the Ascendant or the Midheaven for that chart, and be careful with the Moon's sign.
|
||||
- {reader}
|
||||
- Tone: warm, honest, empowering and concrete. Not fatalistic: no predictions of illness, death, accidents or specific events, \
|
||||
and no medical, legal or financial advice.
|
||||
- Avoid generic horoscope clichés and filler: every paragraph must say something specific to this chart.
|
||||
- Texts are plain paragraphs separated by a blank line, without headings or lists inside; **bold** may be used sparingly.
|
||||
- Length: each section 2-4 paragraphs (about 150-250 words); the whole analysis about 1300-1800 words.
|
||||
- Answer with a single JSON object that matches the given schema, and nothing else.",
|
||||
lang = language_name(language),
|
||||
);
|
||||
if language == "hu" {
|
||||
text.push_str("\n\n");
|
||||
text.push_str(HUNGARIAN_STYLE);
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
pub fn build(kind: Kind, language: &str, chart_text: &str) -> Prompt {
|
||||
let mut user = String::from("Chart data:\n\n");
|
||||
user.push_str(chart_text);
|
||||
user.push_str("\nSections to write, in this order:\n");
|
||||
for (key, about) in sections(kind) {
|
||||
user.push_str(&format!("- {key}: {about}\n"));
|
||||
}
|
||||
user.push_str("\nWrite the analysis now.");
|
||||
Prompt {
|
||||
system: rules(kind, language),
|
||||
user,
|
||||
schema_name: match kind {
|
||||
Kind::Natal => "natal_analysis",
|
||||
Kind::Synastry => "synastry_analysis",
|
||||
},
|
||||
schema: schema(kind),
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks an answer against our schema subset (objects with required
|
||||
/// properties, non-empty strings, string arrays with at least two items).
|
||||
pub fn validate(schema: &Value, value: &Value) -> Result<(), String> {
|
||||
fn walk(schema: &Value, value: &Value, path: &str) -> Result<(), String> {
|
||||
match schema["type"].as_str() {
|
||||
Some("object") => {
|
||||
let obj = value.as_object().ok_or_else(|| format!("{path}: expected an object"))?;
|
||||
for key in schema["required"].as_array().into_iter().flatten().filter_map(Value::as_str) {
|
||||
let child = obj.get(key).ok_or_else(|| format!("{path}.{key}: missing"))?;
|
||||
walk(&schema["properties"][key], child, &format!("{path}.{key}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some("array") => {
|
||||
let items = value.as_array().ok_or_else(|| format!("{path}: expected an array"))?;
|
||||
if items.len() < 2 {
|
||||
return Err(format!("{path}: too few items"));
|
||||
}
|
||||
items.iter().enumerate().try_for_each(|(i, v)| walk(&schema["items"], v, &format!("{path}[{i}]")))
|
||||
}
|
||||
Some("string") => match value.as_str() {
|
||||
Some(s) if !s.trim().is_empty() => Ok(()),
|
||||
_ => Err(format!("{path}: expected a non-empty string")),
|
||||
},
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
walk(schema, value, "$")
|
||||
}
|
||||
|
||||
/// The analysis as Markdown (for reading and comparing outputs).
|
||||
pub fn to_markdown(kind: Kind, language: &str, content: &Value) -> String {
|
||||
let (strengths, challenges, advice) = if language == "hu" {
|
||||
("Erősségek", "Kihívások", "Útravaló")
|
||||
} else {
|
||||
("Strengths", "Challenges", "Advice")
|
||||
};
|
||||
let mut md =
|
||||
format!("# {}\n\n_{}_\n\n", content["title"].as_str().unwrap_or(""), content["summary"].as_str().unwrap_or(""));
|
||||
for (key, _) in sections(kind) {
|
||||
let s = &content["sections"][*key];
|
||||
md.push_str(&format!("## {}\n\n{}\n\n", s["title"].as_str().unwrap_or(key), s["body"].as_str().unwrap_or("")));
|
||||
}
|
||||
for (title, key) in [(strengths, "strengths"), (challenges, "challenges")] {
|
||||
md.push_str(&format!("## {title}\n\n"));
|
||||
for item in content[key].as_array().into_iter().flatten().filter_map(Value::as_str) {
|
||||
md.push_str(&format!("- {item}\n"));
|
||||
}
|
||||
md.push('\n');
|
||||
}
|
||||
md.push_str(&format!("## {advice}\n\n{}\n", content["advice"].as_str().unwrap_or("")));
|
||||
md
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
//! `worker compare-models`: writes the same natal analysis with several
|
||||
//! models for a blind comparison.
|
||||
//!
|
||||
//! ```text
|
||||
//! OPENROUTER_API_KEY=... worker compare-models --chart natal.json \
|
||||
//! --models minimax/minimax-m3,anthropic/claude-sonnet-5.5 [--language hu] [--out DIR]
|
||||
//! ```
|
||||
//!
|
||||
//! `--chart` is a horoscope-api `/v1/natal` response. The models get shuffled
|
||||
//! letters: `A.md`, `B.md`, ... hold the analyses, `key.json` tells which
|
||||
//! letter is which model (with tokens, cost and time). The output on the
|
||||
//! console does not reveal the mapping.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::{Context, bail};
|
||||
use rand::seq::SliceRandom;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::analysis::openrouter::{DEFAULT_BASE_URL, OpenRouter};
|
||||
use crate::analysis::{compact, prompt};
|
||||
|
||||
struct Args {
|
||||
chart: PathBuf,
|
||||
models: Vec<String>,
|
||||
language: String,
|
||||
out: PathBuf,
|
||||
}
|
||||
|
||||
fn parse(args: Vec<String>) -> anyhow::Result<Args> {
|
||||
let (mut chart, mut models, mut language, mut out) = (None, Vec::new(), "hu".to_owned(), None);
|
||||
let mut it = args.into_iter();
|
||||
while let Some(flag) = it.next() {
|
||||
let mut value = || it.next().with_context(|| format!("{flag} needs a value"));
|
||||
match flag.as_str() {
|
||||
"--chart" => chart = Some(PathBuf::from(value()?)),
|
||||
"--models" => models = value()?.split(',').map(|m| m.trim().to_owned()).filter(|m| !m.is_empty()).collect(),
|
||||
"--language" => language = value()?,
|
||||
"--out" => out = Some(PathBuf::from(value()?)),
|
||||
other => bail!("unknown argument '{other}'"),
|
||||
}
|
||||
}
|
||||
let chart = chart.context("--chart is required")?;
|
||||
if models.is_empty() {
|
||||
bail!("--models is required (comma separated OpenRouter model ids)");
|
||||
}
|
||||
if models.len() > 26 {
|
||||
bail!("at most 26 models");
|
||||
}
|
||||
let out = out.unwrap_or_else(|| {
|
||||
let stamp = time::OffsetDateTime::now_utc().unix_timestamp();
|
||||
PathBuf::from(format!("model-compare-{stamp}"))
|
||||
});
|
||||
Ok(Args { chart, models, language, out })
|
||||
}
|
||||
|
||||
pub async fn main(args: Vec<String>) -> anyhow::Result<()> {
|
||||
let args = parse(args)?;
|
||||
let key = std::env::var("OPENROUTER_API_KEY")
|
||||
.ok()
|
||||
.filter(|k| !k.trim().is_empty())
|
||||
.context("OPENROUTER_API_KEY is not set")?;
|
||||
let base_url = std::env::var("OPENROUTER_BASE_URL").unwrap_or_else(|_| DEFAULT_BASE_URL.into());
|
||||
|
||||
let raw = std::fs::read_to_string(&args.chart).with_context(|| format!("reading {}", args.chart.display()))?;
|
||||
let mut chart: Value = serde_json::from_str(&raw).context("--chart is not JSON")?;
|
||||
if chart["result"].is_object() {
|
||||
chart = chart["result"].take();
|
||||
}
|
||||
let built = prompt::build(prompt::Kind::Natal, &args.language, &compact::natal(&chart));
|
||||
|
||||
std::fs::create_dir_all(&args.out)?;
|
||||
std::fs::write(
|
||||
args.out.join("prompt.md"),
|
||||
format!(
|
||||
"# System\n\n{}\n\n# User\n\n{}\n\n# Schema\n\n```json\n{:#}\n```\n",
|
||||
built.system, built.user, built.schema
|
||||
),
|
||||
)?;
|
||||
|
||||
let mut models = args.models.clone();
|
||||
models.shuffle(&mut rand::rng());
|
||||
let llm = std::sync::Arc::new(OpenRouter::new(&base_url, &key, None));
|
||||
let built = std::sync::Arc::new(built);
|
||||
println!("Writing {} analyses in parallel (this takes a minute or two)...", models.len());
|
||||
|
||||
let mut tasks = tokio::task::JoinSet::new();
|
||||
for (i, model) in models.iter().enumerate() {
|
||||
let (llm, built, model) = (llm.clone(), built.clone(), model.clone());
|
||||
let letter = char::from(b'A' + i as u8);
|
||||
tasks.spawn(async move {
|
||||
let started = Instant::now();
|
||||
let result = llm.complete(&model, &built).await;
|
||||
(letter, model, result, started.elapsed().as_secs_f64())
|
||||
});
|
||||
}
|
||||
|
||||
let mut key_entries = Vec::new();
|
||||
let mut total_cost = 0.0;
|
||||
while let Some(joined) = tasks.join_next().await {
|
||||
let (letter, model, result, secs) = joined?;
|
||||
match result {
|
||||
Ok(c) => {
|
||||
let md = prompt::to_markdown(prompt::Kind::Natal, &args.language, &c.content);
|
||||
let words = md.split_whitespace().count();
|
||||
std::fs::write(args.out.join(format!("{letter}.md")), &md)?;
|
||||
std::fs::write(args.out.join(format!("{letter}.json")), serde_json::to_string_pretty(&c.content)?)?;
|
||||
total_cost += c.cost_usd.unwrap_or(0.0);
|
||||
println!("{letter}: ok, {words} words");
|
||||
key_entries.push(json!({
|
||||
"letter": letter.to_string(), "model": model, "model_used": c.model, "ok": true, "words": words,
|
||||
"input_tokens": c.input_tokens, "output_tokens": c.output_tokens, "cost_usd": c.cost_usd,
|
||||
"seconds": (secs * 10.0).round() / 10.0,
|
||||
}));
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{letter}: failed (details in key.json)");
|
||||
key_entries.push(json!({
|
||||
"letter": letter.to_string(), "model": model, "ok": false, "error": e.message(),
|
||||
"seconds": (secs * 10.0).round() / 10.0,
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
key_entries.sort_by(|a, b| a["letter"].as_str().cmp(&b["letter"].as_str()));
|
||||
std::fs::write(args.out.join("key.json"), serde_json::to_string_pretty(&key_entries)?)?;
|
||||
println!("Total cost: ${total_cost:.4}. Output: {}", args.out.display());
|
||||
Ok(())
|
||||
}
|
||||
@@ -16,6 +16,15 @@ pub struct Config {
|
||||
pub poll_interval: Duration,
|
||||
/// After this many failed attempts an e-mail is marked as failed.
|
||||
pub max_email_attempts: i32,
|
||||
/// Unset: analyses stay pending.
|
||||
pub openrouter_api_key: Option<String>,
|
||||
pub openrouter_base_url: String,
|
||||
/// Frontend address, sent to OpenRouter as `HTTP-Referer`.
|
||||
pub public_base_url: Option<String>,
|
||||
/// Analyses written at the same time.
|
||||
pub analysis_concurrency: usize,
|
||||
/// After this many failed attempts an analysis fails and is refunded.
|
||||
pub max_analysis_attempts: i32,
|
||||
}
|
||||
|
||||
fn var(name: &str) -> Option<String> {
|
||||
@@ -41,6 +50,12 @@ impl Config {
|
||||
mail_from: mail_from.parse().with_context(|| format!("invalid MAIL_FROM '{mail_from}'"))?,
|
||||
poll_interval: Duration::from_secs(parsed("POLL_INTERVAL_SECS", 30)?),
|
||||
max_email_attempts: parsed("MAX_EMAIL_ATTEMPTS", 8)?,
|
||||
openrouter_api_key: var("OPENROUTER_API_KEY"),
|
||||
openrouter_base_url: var("OPENROUTER_BASE_URL")
|
||||
.unwrap_or_else(|| crate::analysis::openrouter::DEFAULT_BASE_URL.into()),
|
||||
public_base_url: var("PUBLIC_BASE_URL"),
|
||||
analysis_concurrency: parsed("ANALYSIS_CONCURRENCY", 4)?,
|
||||
max_analysis_attempts: parsed("MAX_ANALYSIS_ATTEMPTS", 3)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ use lettre::message::{Mailbox, MultiPart};
|
||||
use lettre::{AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
use sqlx::postgres::PgListener;
|
||||
use tokio::sync::watch;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -174,18 +173,8 @@ impl Delivery {
|
||||
}
|
||||
}
|
||||
|
||||
async fn listen(db: &PgPool) -> Option<PgListener> {
|
||||
let result = async {
|
||||
let mut listener = PgListener::connect_with(db).await?;
|
||||
listener.listen(CHANNEL).await?;
|
||||
Ok::<_, sqlx::Error>(listener)
|
||||
}
|
||||
.await;
|
||||
result.map_err(|e| tracing::warn!(error = %e, "LISTEN failed; polling only")).ok()
|
||||
}
|
||||
|
||||
pub async fn run(delivery: Delivery, poll: Duration, stop: watch::Receiver<bool>) {
|
||||
let mut listener = listen(&delivery.db).await;
|
||||
let mut listener = crate::listen(&delivery.db, CHANNEL).await;
|
||||
loop {
|
||||
if *stop.borrow() {
|
||||
break;
|
||||
@@ -195,20 +184,8 @@ pub async fn run(delivery: Delivery, poll: Duration, stop: watch::Receiver<bool>
|
||||
Ok(_) => {}
|
||||
Err(e) => tracing::error!(error = %e, "e-mail delivery"),
|
||||
}
|
||||
let notified = async {
|
||||
match listener.as_mut() {
|
||||
// Reconnects by itself; an error means the connection was lost for now.
|
||||
Some(l) => {
|
||||
if let Err(e) = l.recv().await {
|
||||
tracing::warn!(error = %e, "LISTEN connection lost");
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
None => std::future::pending().await,
|
||||
}
|
||||
};
|
||||
tokio::select! {
|
||||
() = notified => {}
|
||||
() = crate::notified(listener.as_mut()) => {}
|
||||
() = tokio::time::sleep(poll) => {}
|
||||
() = crate::stopped(stop.clone()) => break,
|
||||
}
|
||||
|
||||
@@ -3,17 +3,21 @@
|
||||
//!
|
||||
//! * e-mail delivery from `email_outbox` over SMTP (Mailpit in development)
|
||||
//! * hourly cleanup of expired sessions, tokens, invites and old e-mails
|
||||
//! * later: writing the text analyses (LLM)
|
||||
//! * writing the requested text analyses with an LLM (OpenRouter)
|
||||
//!
|
||||
//! Configuration: see [`config::Config`]; plus `BIND_ADDR` (default
|
||||
//! `0.0.0.0:8082`, only `/healthz`), `LOG_FORMAT` and `RUST_LOG`.
|
||||
//! `worker healthcheck` performs a local health check (for Docker).
|
||||
//! `worker healthcheck` performs a local health check (for Docker);
|
||||
//! `worker compare-models ...` writes one analysis with several models (see [`compare`]).
|
||||
|
||||
mod analysis;
|
||||
mod cleanup;
|
||||
mod compare;
|
||||
mod config;
|
||||
mod email;
|
||||
mod templates;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
@@ -22,6 +26,7 @@ use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::get;
|
||||
use sqlx::PgPool;
|
||||
use sqlx::postgres::PgListener;
|
||||
use tokio::sync::watch;
|
||||
|
||||
use crate::config::Config;
|
||||
@@ -33,6 +38,31 @@ pub async fn stopped(mut stop: watch::Receiver<bool>) {
|
||||
let _ = stop.wait_for(|s| *s).await;
|
||||
}
|
||||
|
||||
/// Subscribes to a NOTIFY channel; `None` (polling only) if that fails.
|
||||
pub async fn listen(db: &PgPool, channel: &str) -> Option<PgListener> {
|
||||
let result = async {
|
||||
let mut listener = PgListener::connect_with(db).await?;
|
||||
listener.listen(channel).await?;
|
||||
Ok::<_, sqlx::Error>(listener)
|
||||
}
|
||||
.await;
|
||||
result.map_err(|e| tracing::warn!(channel, error = %e, "LISTEN failed; polling only")).ok()
|
||||
}
|
||||
|
||||
/// Resolves on the next notification; never without a listener.
|
||||
pub async fn notified(listener: Option<&mut PgListener>) {
|
||||
match listener {
|
||||
// Reconnects by itself; an error means the connection is down for now.
|
||||
Some(l) => {
|
||||
if let Err(e) = l.recv().await {
|
||||
tracing::warn!(error = %e, "LISTEN connection lost");
|
||||
tokio::time::sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
None => std::future::pending().await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn healthz(State(db): State<PgPool>) -> StatusCode {
|
||||
match sqlx::query("SELECT 1").execute(&db).await {
|
||||
Ok(_) => StatusCode::OK,
|
||||
@@ -43,8 +73,13 @@ async fn healthz(State(db): State<PgPool>) -> StatusCode {
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let addr = service_kit::bind_addr(DEFAULT_BIND_ADDR)?;
|
||||
if std::env::args().nth(1).as_deref() == Some("healthcheck") {
|
||||
return service_kit::healthcheck(addr);
|
||||
match std::env::args().nth(1).as_deref() {
|
||||
Some("healthcheck") => return service_kit::healthcheck(addr),
|
||||
Some("compare-models") => {
|
||||
service_kit::init_logging();
|
||||
return compare::main(std::env::args().skip(2).collect()).await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
service_kit::init_logging();
|
||||
let config = Config::from_env()?;
|
||||
@@ -61,6 +96,17 @@ async fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
None => tracing::warn!("SMTP_URL is not set: e-mails stay in the outbox"),
|
||||
}
|
||||
match &config.openrouter_api_key {
|
||||
Some(key) => {
|
||||
let llm =
|
||||
analysis::openrouter::OpenRouter::new(&config.openrouter_base_url, key, config.public_base_url.clone());
|
||||
let analyzer =
|
||||
analysis::Analyzer::new(pool.clone(), llm, config.max_analysis_attempts, config.analysis_concurrency);
|
||||
tracing::info!(concurrency = analyzer.concurrency, "analyses enabled");
|
||||
jobs.spawn(analysis::run(Arc::new(analyzer), config.poll_interval, stop.clone()));
|
||||
}
|
||||
None => tracing::warn!("OPENROUTER_API_KEY is not set: analyses stay pending"),
|
||||
}
|
||||
jobs.spawn(cleanup::run(pool.clone(), stop.clone()));
|
||||
|
||||
let app = Router::new().route("/healthz", get(healthz)).with_state(pool);
|
||||
|
||||
@@ -180,3 +180,304 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
|
||||
sqlx::query_as("SELECT array_agg(id ORDER BY id) FROM email_outbox").fetch_one(&db).await.unwrap();
|
||||
assert_eq!(left, vec![recent, unsent]);
|
||||
}
|
||||
|
||||
// --- analyses ----------------------------------------------------------------------------
|
||||
|
||||
mod analyses {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::State;
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::{Value, json};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::analysis::openrouter::OpenRouter;
|
||||
use crate::analysis::prompt::{Kind, schema, validate};
|
||||
use crate::analysis::{Analyzer, backoff};
|
||||
|
||||
/// Scripted replies (then the last one forever) and the requests received.
|
||||
#[derive(Clone, Default)]
|
||||
struct Fake {
|
||||
replies: Arc<Mutex<Vec<(u16, Value)>>>,
|
||||
requests: Arc<Mutex<Vec<Value>>>,
|
||||
}
|
||||
|
||||
async fn completions(State(fake): State<Fake>, Json(body): Json<Value>) -> (StatusCode, Json<Value>) {
|
||||
fake.requests.lock().unwrap().push(body);
|
||||
let mut replies = fake.replies.lock().unwrap();
|
||||
let (status, reply) = if replies.len() > 1 { replies.remove(0) } else { replies[0].clone() };
|
||||
(StatusCode::from_u16(status).unwrap(), Json(reply))
|
||||
}
|
||||
|
||||
async fn fake_openrouter(replies: Vec<(u16, Value)>) -> (Fake, String) {
|
||||
let fake = Fake { replies: Arc::new(Mutex::new(replies)), ..Default::default() };
|
||||
let app =
|
||||
axum::Router::new().route("/chat/completions", axum::routing::post(completions)).with_state(fake.clone());
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
(fake, format!("http://{addr}"))
|
||||
}
|
||||
|
||||
fn content(kind: Kind) -> Value {
|
||||
let schema = schema(kind);
|
||||
let keys: Vec<&str> = schema["properties"]["sections"]["required"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|k| k.as_str().unwrap())
|
||||
.collect();
|
||||
let sections: serde_json::Map<String, Value> = keys
|
||||
.iter()
|
||||
.map(|k| ((*k).to_owned(), json!({"title": format!("Cím {k}"), "body": "Első bekezdés.\n\nMásodik."})))
|
||||
.collect();
|
||||
json!({
|
||||
"title": "Csendes erő", "summary": "Összefoglaló.", "sections": sections,
|
||||
"strengths": ["Kitartás.", "Empátia."], "challenges": ["Túlgondolás.", "Visszahúzódás."],
|
||||
"advice": "Bízz magadban."
|
||||
})
|
||||
}
|
||||
|
||||
fn reply(content: &Value) -> (u16, Value) {
|
||||
(
|
||||
200,
|
||||
json!({
|
||||
"model": "test/model-2026",
|
||||
"choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": content.to_string()}}],
|
||||
"usage": {"prompt_tokens": 2100, "completion_tokens": 4800, "cost": 0.0123}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn fixture(name: &str) -> Value {
|
||||
let path = format!("{}/testdata/{name}", env!("CARGO_MANIFEST_DIR"));
|
||||
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
struct Setup {
|
||||
user: Uuid,
|
||||
chart: Uuid,
|
||||
}
|
||||
|
||||
async fn setup(db: &PgPool) -> Setup {
|
||||
let (user, chart) = (Uuid::now_v7(), Uuid::now_v7());
|
||||
sqlx::query("INSERT INTO users (id, email) VALUES ($1, 'anna@example.com')")
|
||||
.bind(user)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query(
|
||||
"INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \
|
||||
VALUES ($1, $2, 'Saját', true, '{}', $3, 'hash1')",
|
||||
)
|
||||
.bind(chart)
|
||||
.bind(user)
|
||||
.bind(fixture("natal-1990-03-15.json"))
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
Setup { user, chart }
|
||||
}
|
||||
|
||||
async fn request_analysis(db: &PgPool, s: &Setup) -> Uuid {
|
||||
let id = Uuid::now_v7();
|
||||
sqlx::query(
|
||||
"INSERT INTO analyses (id, kind, chart_id, requested_by, language, model, prompt_version, cache_key, credits_charged) \
|
||||
VALUES ($1, 'natal', $2, $3, 'hu', 'test/model', 'v1', 'key', 1)",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(s.chart)
|
||||
.bind(s.user)
|
||||
.execute(db)
|
||||
.await
|
||||
.unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
fn analyzer(db: &PgPool, base_url: &str) -> Analyzer {
|
||||
Analyzer::new(db.clone(), OpenRouter::new(base_url, "test-key", None), 3, 2)
|
||||
}
|
||||
|
||||
async fn run_once(a: &Analyzer) -> bool {
|
||||
match a.claim().await.unwrap() {
|
||||
Some(job) => {
|
||||
a.handle(job).await;
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
type Row = (String, i32, Option<String>, Option<String>, Option<i32>, Option<f64>, bool);
|
||||
|
||||
async fn row(db: &PgPool, id: Uuid) -> Row {
|
||||
sqlx::query_as(
|
||||
"SELECT status, attempts, error, model_used, output_tokens, cost_usd, next_attempt_at > now() \
|
||||
FROM analyses WHERE id = $1",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn refunds(db: &PgPool, user: Uuid) -> Vec<i32> {
|
||||
sqlx::query_scalar("SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund'")
|
||||
.bind(user)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn make_due(db: &PgPool, id: Uuid) {
|
||||
sqlx::query("UPDATE analyses SET next_attempt_at = now() WHERE id = $1").bind(id).execute(db).await.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn writes_the_analysis(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let id = request_analysis(&db, &s).await;
|
||||
let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await;
|
||||
let a = analyzer(&db, &url);
|
||||
|
||||
assert!(run_once(&a).await);
|
||||
assert!(!run_once(&a).await, "nothing else is due");
|
||||
assert_eq!(
|
||||
row(&db, id).await,
|
||||
("done".into(), 1, None, Some("test/model-2026".into()), Some(4800), Some(0.0123), false)
|
||||
);
|
||||
let (stored,): (Value,) =
|
||||
sqlx::query_as("SELECT content FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap();
|
||||
assert_eq!(stored["title"], "Csendes erő");
|
||||
|
||||
let requests = fake.requests.lock().unwrap();
|
||||
let body = &requests[0];
|
||||
assert_eq!(body["model"], "test/model");
|
||||
assert_eq!(body["response_format"]["json_schema"]["strict"], true);
|
||||
assert_eq!(body["provider"]["require_parameters"], true);
|
||||
let system = body["messages"][0]["content"].as_str().unwrap();
|
||||
let user = body["messages"][1]["content"].as_str().unwrap();
|
||||
assert!(system.contains("Write in Hungarian.") && system.contains("Szaturnusz"));
|
||||
assert!(user.contains("- Sun: Pisces 24°42', house 8") && user.contains("- personality: "));
|
||||
// No birth data in the prompt.
|
||||
for secret in ["1990", "14:30", "Szeged", "Hungary"] {
|
||||
assert!(!user.contains(secret) && !system.contains(secret), "{secret} leaked");
|
||||
}
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn retries_bad_answers_then_fails_and_refunds(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let id = request_analysis(&db, &s).await;
|
||||
let broken = json!({"title": "Csak cím"});
|
||||
let (_, url) = fake_openrouter(vec![reply(&broken)]).await;
|
||||
let a = analyzer(&db, &url);
|
||||
|
||||
assert!(run_once(&a).await);
|
||||
let (status, attempts, error, _, _, _, later) = row(&db, id).await;
|
||||
assert_eq!((status.as_str(), attempts, later), ("pending", 1, true));
|
||||
assert!(error.unwrap().contains("does not match the schema"));
|
||||
assert!(!run_once(&a).await, "the retry waits");
|
||||
assert!(refunds(&db, s.user).await.is_empty());
|
||||
|
||||
for _ in 0..2 {
|
||||
make_due(&db, id).await;
|
||||
assert!(run_once(&a).await);
|
||||
}
|
||||
let (status, attempts, ..) = row(&db, id).await;
|
||||
assert_eq!((status.as_str(), attempts), ("failed", 3));
|
||||
assert_eq!(refunds(&db, s.user).await, vec![1]);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn permanent_errors_fail_at_once_and_retry_after_outage(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let bad_model = request_analysis(&db, &s).await;
|
||||
let (_, url) = fake_openrouter(vec![(400, json!({"error": {"message": "unknown model"}}))]).await;
|
||||
assert!(run_once(&analyzer(&db, &url)).await);
|
||||
let (status, attempts, error, ..) = row(&db, bad_model).await;
|
||||
assert_eq!((status.as_str(), attempts), ("failed", 1));
|
||||
assert_eq!(error.as_deref(), Some("HTTP 400: unknown model"));
|
||||
assert_eq!(refunds(&db, s.user).await, vec![1]);
|
||||
|
||||
// A provider outage (5xx) is retried and then succeeds.
|
||||
let outage = request_analysis(&db, &s).await;
|
||||
let (_, url) =
|
||||
fake_openrouter(vec![(503, json!({"error": {"message": "overloaded"}})), reply(&content(Kind::Natal))])
|
||||
.await;
|
||||
let a = analyzer(&db, &url);
|
||||
assert!(run_once(&a).await);
|
||||
assert_eq!(row(&db, outage).await.0, "pending");
|
||||
make_due(&db, outage).await;
|
||||
assert!(run_once(&a).await);
|
||||
assert_eq!(row(&db, outage).await.0, "done");
|
||||
assert_eq!(refunds(&db, s.user).await, vec![1], "no refund for the successful one");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn synastry_prompt_and_stuck_jobs(db: PgPool) {
|
||||
let s = setup(&db).await;
|
||||
let response = fixture("synastry-response.json");
|
||||
let (chart2, synastry) = (Uuid::now_v7(), Uuid::now_v7());
|
||||
sqlx::query(
|
||||
"INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \
|
||||
VALUES ($1, $2, 'Partner', false, '{}', $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();
|
||||
let id = Uuid::now_v7();
|
||||
sqlx::query(
|
||||
"INSERT INTO analyses (id, kind, synastry_id, requested_by, language, model, prompt_version, cache_key, \
|
||||
status, started_at) VALUES ($1, 'synastry', $2, $3, 'en', 'test/model', 'v1', 'k2', 'running', now() - interval '20 minutes')",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(synastry)
|
||||
.bind(s.user)
|
||||
.execute(&db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Synastry))]).await;
|
||||
let a = analyzer(&db, &url);
|
||||
assert!(!run_once(&a).await, "running jobs are not claimed");
|
||||
assert_eq!(a.requeue_stuck().await.unwrap(), 1);
|
||||
assert!(run_once(&a).await);
|
||||
assert_eq!(row(&db, id).await.0, "done");
|
||||
|
||||
let requests = fake.requests.lock().unwrap();
|
||||
let user = requests[0]["messages"][1]["content"].as_str().unwrap();
|
||||
assert!(user.starts_with("Chart data:\n\nSYNASTRY") && user.contains("- attraction: "));
|
||||
assert!(requests[0]["messages"][0]["content"].as_str().unwrap().contains("Write in English."));
|
||||
assert!(!user.contains("Szeged") && !user.contains("Pécs"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schema_validation_and_backoff() {
|
||||
let schema = schema(Kind::Natal);
|
||||
assert_eq!(validate(&schema, &content(Kind::Natal)), Ok(()));
|
||||
let mut c = content(Kind::Natal);
|
||||
c["sections"]["love"]["body"] = json!(" ");
|
||||
assert_eq!(validate(&schema, &c), Err("$.sections.love.body: expected a non-empty string".into()));
|
||||
c["sections"]["love"]["body"] = json!("Szöveg.");
|
||||
c["sections"].as_object_mut().unwrap().remove("work");
|
||||
assert!(validate(&schema, &c).unwrap_err().contains("work: missing"));
|
||||
assert_eq!([1, 2, 3, 9].map(|n| backoff(n).as_secs()), [30, 120, 480, 3600]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user