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
+50 -4
View File
@@ -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);