Paid detailed analyses with per-kind units; example analysis page

- Credits become analysis units (migration 0006): a natal analysis uses
  one 'natal' unit, a synastry analysis one 'synastry' unit. No monthly
  free credits, no model tiers: ANALYSIS_MODEL (default Opus 5.5) writes
  every paid analysis. Admins grant units per kind; the worker refunds
  the right unit on failure.
- Chart page: the detailed analysis card (offer with units left and a
  confirmation, progress while writing, the finished six sections).
- Public /pelda page: a full Opus analysis of a made-up chart, with the
  free summary and a login call to action.
- The worker sends the JSON schema with properties in 'required' order.
  serde_json sorted them, so the title came after ~9000 tokens of text
  and Opus sometimes left it empty (3 failed attempts in a live test);
  with the order fixed the same analysis succeeded at the first try.

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-16 17:39:15 +00:00
co-authored by Claude Opus 5.5
parent 450174c273
commit ce3d3804a7
29 changed files with 1309 additions and 349 deletions
+5 -105
View File
@@ -1,57 +1,6 @@
//! Configuration from the environment.
use anyhow::{Context, bail};
use db::Role;
use serde::Serialize;
/// A model users may choose for analyses.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct LlmModel {
/// OpenRouter model id.
pub id: String,
/// Credits charged per new analysis.
pub credits: i32,
/// Role needed to use it (`None`: everyone).
pub role: Option<Role>,
}
/// Parses `id[=credits][@role]` entries; `credits` defaults to `default_credits`.
fn parse_models(entries: &[String], default_credits: i32) -> anyhow::Result<Vec<LlmModel>> {
entries
.iter()
.map(|entry| {
let (rest, role) = match entry.split_once('@') {
Some((rest, role)) => {
let role = Role::parse(role.trim())
.with_context(|| format!("unknown role '{role}' in LLM_MODELS entry '{entry}'"))?;
(rest, Some(role))
}
None => (entry.as_str(), None),
};
let (id, credits) = match rest.split_once('=') {
Some((id, credits)) => (
id,
credits
.trim()
.parse::<i32>()
.ok()
.filter(|c| *c >= 0)
.with_context(|| format!("invalid credits in LLM_MODELS entry '{entry}'"))?,
),
None => (rest, default_credits),
};
let id = id.trim();
if id.is_empty() {
bail!("empty model id in LLM_MODELS entry '{entry}'");
}
Ok(LlmModel { id: id.to_owned(), credits, role })
})
.collect()
}
/// Free tier, paid tier, VIP tier (model choice of 2026-11).
const DEFAULT_LLM_MODELS: &str =
"anthropic/claude-haiku-5.5=1,openai/gpt-5.6-terra=3@premium,anthropic/claude-opus-5.5=10@vip";
#[derive(Debug, Clone)]
pub struct Config {
@@ -85,13 +34,9 @@ pub struct Config {
pub bootstrap_admin_emails: Vec<String>,
/// `SUPPORTED_LOCALES` (default `hu,en`); the first is the default.
pub supported_locales: Vec<String>,
/// `MONTHLY_FREE_CREDITS` (default 10).
pub monthly_free_credits: i32,
/// `LLM_MODELS`: comma-separated `model[=credits][@role]` entries users
/// may choose from; the first is the default and should need no role.
/// Credits default to `ANALYSIS_COST` (default 1). Default:
/// [`DEFAULT_LLM_MODELS`].
pub llm_models: Vec<LlmModel>,
/// `ANALYSIS_MODEL`: writes the paid detailed analyses (default
/// `anthropic/claude-opus-5.5`, the best of the 2026-11 comparison).
pub analysis_model: String,
/// `SUMMARY_MODEL`: writes the free short summary of every saved chart
/// (default `anthropic/claude-sonnet-5.5`, about 1 US cent each).
pub summary_model: String,
@@ -133,18 +78,6 @@ impl Config {
if supported_locales.is_empty() {
supported_locales = vec!["hu".into(), "en".into()];
}
let analysis_cost: i32 = parsed("ANALYSIS_COST", 1)?;
if analysis_cost < 0 {
bail!("ANALYSIS_COST must not be negative");
}
let mut model_entries = list("LLM_MODELS");
if model_entries.is_empty() {
model_entries = DEFAULT_LLM_MODELS.split(',').map(str::to_owned).collect();
}
let llm_models = parse_models(&model_entries, analysis_cost)?;
if llm_models[0].role.is_some() {
bail!("the first LLM_MODELS entry is the default and must not need a role");
}
let config = Self {
database_url: var("DATABASE_URL").context("DATABASE_URL is required")?,
horoscope_api_url: var("HOROSCOPE_API_URL").unwrap_or_else(|| "http://localhost:8080".into()),
@@ -159,8 +92,7 @@ impl Config {
.unwrap_or_else(|| "https://www.googleapis.com/oauth2/v3/certs".into()),
bootstrap_admin_emails: list("BOOTSTRAP_ADMIN_EMAILS").into_iter().map(|e| e.to_lowercase()).collect(),
supported_locales,
monthly_free_credits: parsed("MONTHLY_FREE_CREDITS", 10)?,
llm_models,
analysis_model: var("ANALYSIS_MODEL").unwrap_or_else(|| "anthropic/claude-opus-5.5".into()),
summary_model: var("SUMMARY_MODEL").unwrap_or_else(|| "anthropic/claude-sonnet-5.5".into()),
prompt_version: var("ANALYSIS_PROMPT_VERSION").unwrap_or_else(|| "v2".into()),
max_charts_per_user: parsed("MAX_CHARTS_PER_USER", 50)?,
@@ -168,9 +100,6 @@ impl Config {
if config.session_ttl_days < 1 {
bail!("SESSION_TTL_DAYS must be at least 1");
}
if config.monthly_free_credits < 0 {
bail!("MONTHLY_FREE_CREDITS must not be negative");
}
Ok(config)
}
@@ -190,39 +119,10 @@ impl Config {
google_jwks_url,
bootstrap_admin_emails: vec!["admin@example.com".into()],
supported_locales: vec!["hu".into(), "en".into()],
monthly_free_credits: 2,
llm_models: parse_models(
&["openrouter/auto".into(), "anthropic/claude-test=1".into(), "anthropic/claude-vip=2@vip".into()],
1,
)
.unwrap(),
analysis_model: "anthropic/claude-test".into(),
summary_model: "anthropic/claude-summary".into(),
prompt_version: "v1".into(),
max_charts_per_user: 50,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_model_entries() {
let models = parse_models(&DEFAULT_LLM_MODELS.split(',').map(str::to_owned).collect::<Vec<_>>(), 1).unwrap();
assert_eq!(
models,
vec![
LlmModel { id: "anthropic/claude-haiku-5.5".into(), credits: 1, role: None },
LlmModel { id: "openai/gpt-5.6-terra".into(), credits: 3, role: Some(Role::Premium) },
LlmModel { id: "anthropic/claude-opus-5.5".into(), credits: 10, role: Some(Role::Vip) },
]
);
let models = parse_models(&["a/b:free".into(), "c/d@vip".into()], 2).unwrap();
assert_eq!((models[0].id.as_str(), models[0].credits), ("a/b:free", 2));
assert_eq!((models[1].credits, models[1].role), (2, Some(Role::Vip)));
assert!(parse_models(&["a/b@boss".into()], 1).is_err());
assert!(parse_models(&["a/b=-1".into()], 1).is_err());
assert!(parse_models(&["=3".into()], 1).is_err());
}
}