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:
feco
2026-11-15 12:18:40 +00:00
co-authored by Claude Opus 5.5
parent 771ece41b1
commit 812b2d4f73
19 changed files with 1615 additions and 35 deletions
+384
View File
@@ -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}");
}
}
}
+272
View File
@@ -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;
}
+155
View File
@@ -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);
}
}
+224
View File
@@ -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
}