//! Text analyses (LLM) of charts and synastries. //! //! A request creates a `pending` analysis and spends one unit of its kind //! (`natal`, `synastry` or `forecast`, see [`credits`]; admins pay nothing); the worker //! writes the text later. A finished analysis with the same cache key (same //! chart or synastry hash, prompt version, model and language) is copied //! instead, free of charge, so the same chart is never paid for twice. //! //! Every saved chart also gets a free short summary (`summary` kind, written //! by `SUMMARY_MODEL`); the detailed analyses are paid. use analysis_prompt::Kind; use axum::Json; use axum::body::Bytes; use axum::extract::{Path, State}; use axum::http::StatusCode; use axum_extra::extract::WithRejection; use serde::Deserialize; use serde_json::{Value, json}; use sqlx::PgPool; use time::OffsetDateTime; use uuid::Uuid; use super::auth::validate_locale; use super::charts::{ChartRow, find_chart, owned_chart}; use super::forecasts::{find_forecast, owned_forecast}; use super::synastries::{load_synastry, visible_synastry}; use super::{IdPath, St}; use crate::AppState; use crate::auth::AuthUser; use crate::credits::{self, Unit}; use crate::error::{ApiError, ApiResult}; use crate::views::{rfc3339, rfc3339_opt, sha256_hex}; use db::Role; #[derive(Debug, Clone, sqlx::FromRow)] /// An analysis as users see it; the model is left out on purpose (a business secret). pub struct AnalysisRow { pub id: Uuid, pub kind: String, pub chart_id: Option, pub synastry_id: Option, pub forecast_id: Option, pub language: String, pub prompt_version: String, pub status: String, pub content: Option, pub error: Option, pub credits_charged: i32, pub created_at: OffsetDateTime, pub completed_at: Option, /// Attempts so far; above 1 the page says it is being tried again. pub attempts: i32, pub next_attempt_at: OffsetDateTime, /// When the current text was asked for (set again by a regeneration). pub requested_at: OffsetDateTime, } const ANALYSIS_COLUMNS: &str = "id, kind, chart_id, synastry_id, forecast_id, language, prompt_version, status, \ content, error, \ credits_charged, created_at, completed_at, attempts, next_attempt_at, requested_at"; impl AnalysisRow { pub fn view(&self) -> Value { json!({ "id": self.id, "kind": self.kind, "chart_id": self.chart_id, "synastry_id": self.synastry_id, "forecast_id": self.forecast_id, "language": self.language, "prompt_version": self.prompt_version, "status": self.status, "content": self.content, "error": self.error, "credits_charged": self.credits_charged, "created_at": rfc3339(self.created_at), "completed_at": rfc3339_opt(self.completed_at), "attempts": self.attempts, "next_attempt_at": rfc3339(self.next_attempt_at), "requested_at": rfc3339(self.requested_at), }) } } pub async fn find_analysis(db: &PgPool, id: Uuid) -> Result, ApiError> { Ok(sqlx::query_as(&format!("SELECT {ANALYSIS_COLUMNS} FROM analyses WHERE id = $1")) .bind(id) .fetch_optional(db) .await?) } /// Whether `user` may read the analysis: the owner of the chart (or of the /// forecast's chart), or a member of the synastry. pub async fn can_see(db: &PgPool, analysis: &AnalysisRow, user: Uuid) -> Result { if let Some(chart_id) = analysis.chart_id { return Ok(find_chart(db, chart_id).await?.is_some_and(|c| c.owner_id == user)); } if let Some(synastry_id) = analysis.synastry_id { return Ok(load_synastry(db, synastry_id).await?.is_some_and(|s| s.is_member(user))); } if let Some(forecast_id) = analysis.forecast_id && let Some(forecast) = find_forecast(db, forecast_id).await? { return Ok(find_chart(db, forecast.chart_id).await?.is_some_and(|c| c.owner_id == user)); } Ok(false) } #[derive(Deserialize, Default)] #[serde(deny_unknown_fields)] pub struct AnalysisRequest { /// Defaults to the user's locale. language: Option, } /// The request body is optional: empty means all defaults. fn optional_body(body: &[u8]) -> Result { if body.iter().all(u8::is_ascii_whitespace) { return Ok(AnalysisRequest::default()); } serde_json::from_slice(body).map_err(|e| ApiError::BadRequest(format!("invalid request body: {e}"))) } pub(super) enum Target { Natal { chart_id: Uuid, hash: String }, Summary { chart_id: Uuid, hash: String }, Synastry { synastry_id: Uuid, hash: String }, Forecast { forecast_id: Uuid, hash: String }, } async fn request( state: &AppState, auth: &AuthUser, target: Target, req: AnalysisRequest, ) -> ApiResult<(StatusCode, Json)> { let language = validate_locale(state, Some(req.language.as_deref().unwrap_or(&auth.user.locale)))?; // Admins use no unit. let admin = auth.user.has_role(Role::Admin); let unit = match target { _ if admin => None, Target::Synastry { .. } => Some(Unit::Synastry), Target::Forecast { .. } => Some(Unit::Forecast), _ => Some(Unit::Natal), }; let (status, row) = queue(state, auth.id(), &target, &language, unit).await?; Ok((status, Json(row.view()))) } /// Returns the existing analysis with the same cache key for this target, /// copies a finished one from another chart with the same data, or queues a /// new one (spending one `unit`, if given). 200, 201 and 202 respectively. /// The cache key and the prompt version of a new analysis. The cache key leaves /// out the admin-edited prompt revision, so a chart keeps its analysis when the /// prompt changes; copying from another chart needs the same revision. pub(super) async fn versions( state: &AppState, kind: &str, hash: &str, model: &str, language: &str, ) -> Result<(String, String), sqlx::Error> { let base_version = &state.config.prompt_version; let cache_key = sha256_hex(&format!("{kind}|{hash}|{base_version}|{model}|{language}")); let prompt_version = match super::prompts::current_revision(&state.db, kind, language).await? { Some(id) => format!("{base_version}-r{id}"), None => base_version.clone(), }; Ok((cache_key, prompt_version)) } pub(super) async fn queue( state: &AppState, user: Uuid, target: &Target, language: &str, unit: Option, ) -> Result<(StatusCode, AnalysisRow), ApiError> { let (kind, hash, chart_id, synastry_id, forecast_id) = match target { Target::Natal { chart_id, hash } => ("natal", hash, Some(*chart_id), None, None), Target::Summary { chart_id, hash } => ("summary", hash, Some(*chart_id), None, None), Target::Synastry { synastry_id, hash } => ("synastry", hash, None, Some(*synastry_id), None), Target::Forecast { forecast_id, hash } => ("forecast", hash, None, None, Some(*forecast_id)), }; let (model, fallback) = &crate::models::models_for(state, Kind::parse(kind).expect("known kind")).await?; let (cache_key, prompt_version) = &versions(state, kind, hash, model, language).await?; // Already requested for this chart/synastry: return it. let existing: Option = sqlx::query_as(&format!( "SELECT {ANALYSIS_COLUMNS} FROM analyses \ WHERE (chart_id = $1 OR synastry_id = $2 OR forecast_id = $4) AND cache_key = $3 AND status <> 'failed' \ ORDER BY created_at DESC LIMIT 1" )) .bind(chart_id) .bind(synastry_id) .bind(cache_key) .bind(forecast_id) .fetch_optional(&state.db) .await?; if let Some(row) = existing { return Ok((StatusCode::OK, row)); } let insert = format!( "INSERT INTO analyses (id, kind, chart_id, synastry_id, requested_by, language, model, prompt_version, \ cache_key, status, content, input_tokens, output_tokens, credits_charged, completed_at, \ forecast_id, fallback_model) \ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING {ANALYSIS_COLUMNS}" ); let id = Uuid::now_v7(); // The same chart elsewhere (another user, same birth data) was already analysed: copy it. let cached: Option<(Value, Option, Option)> = sqlx::query_as( "SELECT content, input_tokens, output_tokens FROM analyses \ WHERE cache_key = $1 AND prompt_version = $2 AND status = 'done' LIMIT 1", ) .bind(cache_key) .bind(prompt_version) .fetch_optional(&state.db) .await?; if let Some((content, input_tokens, output_tokens)) = cached { let row: AnalysisRow = sqlx::query_as(&insert) .bind(id) .bind(kind) .bind(chart_id) .bind(synastry_id) .bind(user) .bind(language) .bind(model) .bind(prompt_version) .bind(cache_key) .bind("done") .bind(content) .bind(input_tokens) .bind(output_tokens) .bind(0) .bind(OffsetDateTime::now_utc()) .bind(forecast_id) .bind(fallback) .fetch_one(&state.db) .await?; return Ok((StatusCode::CREATED, row)); } // New analysis: spend a unit and queue it for the worker. let mut tx = state.db.begin().await?; let row: AnalysisRow = sqlx::query_as(&insert) .bind(id) .bind(kind) .bind(chart_id) .bind(synastry_id) .bind(user) .bind(language) .bind(model) .bind(prompt_version) .bind(cache_key) .bind("pending") .bind(None::) .bind(None::) .bind(None::) .bind(i32::from(unit.is_some())) .bind(None::) .bind(forecast_id) .bind(fallback) .fetch_one(&mut *tx) .await?; if let Some(unit) = unit { credits::spend(&mut tx, user, unit, id).await?; } tx.commit().await?; Ok((StatusCode::ACCEPTED, row)) } /// The free short summary of a chart (in the owner's language): queued if /// it does not exist yet or the last attempt failed. pub async fn ensure_summary(state: &AppState, owner: &AuthUser, chart: &ChartRow) -> Result { let target = Target::Summary { chart_id: chart.id, hash: chart.chart_hash.clone() }; Ok(queue(state, owner.id(), &target, &owner.user.locale, None).await?.1) } pub async fn summary_for_chart( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, ) -> ApiResult> { let chart = owned_chart(&state.db, id, auth.id()).await?; Ok(Json(ensure_summary(&state, &auth, &chart).await?.view())) } async fn list_where(db: &PgPool, column: &str, id: Uuid) -> Result, ApiError> { let rows: Vec = sqlx::query_as(&format!( "SELECT {ANALYSIS_COLUMNS} FROM analyses WHERE {column} = $1 ORDER BY created_at DESC" )) .bind(id) .fetch_all(db) .await?; Ok(Json(json!({ "analyses": rows.iter().map(AnalysisRow::view).collect::>() }))) } pub async fn list_for_chart( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, ) -> ApiResult> { owned_chart(&state.db, id, auth.id()).await?; list_where(&state.db, "chart_id", id).await } pub async fn request_for_chart( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, body: Bytes, ) -> ApiResult<(StatusCode, Json)> { let chart = owned_chart(&state.db, id, auth.id()).await?; let req = optional_body(&body)?; request(&state, &auth, Target::Natal { chart_id: chart.id, hash: chart.chart_hash }, req).await } pub async fn list_for_synastry( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, ) -> ApiResult> { visible_synastry(&state.db, id, auth.id()).await?; list_where(&state.db, "synastry_id", id).await } pub async fn request_for_synastry( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, body: Bytes, ) -> ApiResult<(StatusCode, Json)> { let s = visible_synastry(&state.db, id, auth.id()).await?; let req = optional_body(&body)?; let target = Target::Synastry { synastry_id: s.synastry.id, hash: s.synastry.synastry_hash }; request(&state, &auth, target, req).await } pub async fn get_one(State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath) -> ApiResult> { let analysis = find_analysis(&state.db, id).await?.ok_or(ApiError::NotFound("analysis"))?; if !can_see(&state.db, &analysis, auth.id()).await? { return Err(ApiError::NotFound("analysis")); } Ok(Json(analysis.view())) } pub async fn list_for_forecast( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, ) -> ApiResult> { owned_forecast(&state.db, id, auth.id()).await?; list_where(&state.db, "forecast_id", id).await } pub async fn request_for_forecast( State(state): St, auth: AuthUser, WithRejection(Path(id), _): IdPath, body: Bytes, ) -> ApiResult<(StatusCode, Json)> { let (forecast, _) = owned_forecast(&state.db, id, auth.id()).await?; let req = optional_body(&body)?; request(&state, &auth, Target::Forecast { forecast_id: forecast.id, hash: forecast.forecast_hash }, req).await } /// The hash of what the analysis is about (chart, comparison or forecast), /// part of its cache key; `None` when that was deleted. pub(super) async fn target_hash(db: &PgPool, row: &AnalysisRow) -> Result, sqlx::Error> { let (query, id) = match row.kind.as_str() { "synastry" => ("SELECT synastry_hash FROM synastries WHERE id = $1", row.synastry_id), "forecast" => ("SELECT forecast_hash FROM forecasts WHERE id = $1", row.forecast_id), _ => ("SELECT chart_hash FROM charts WHERE id = $1", row.chart_id), }; sqlx::query_scalar(query).bind(id).fetch_optional(db).await } /// Admin: writes an analysis again in place (same id, so links stay), with the /// current prompt and model. Nobody is charged. pub async fn regenerate( State(state): St, _admin: crate::auth::AdminUser, WithRejection(Path(id), _): IdPath, ) -> ApiResult> { let row = find_analysis(&state.db, id).await?.ok_or(ApiError::NotFound("analysis"))?; if row.status == "pending" || row.status == "running" { return Err(ApiError::conflict("analysis_in_progress", "Ez az elemzés éppen készül.")); } let kind = Kind::parse(&row.kind).ok_or_else(|| ApiError::Internal(format!("unknown kind {}", row.kind)))?; let hash = target_hash(&state.db, &row).await?; let hash = hash.ok_or(ApiError::NotFound("analysis"))?; let (model, fallback) = crate::models::models_for(&state, kind).await?; let (cache_key, prompt_version) = versions(&state, kind.as_str(), &hash, &model, &row.language).await?; let mut tx = state.db.begin().await?; let row: AnalysisRow = sqlx::query_as(&format!( "UPDATE analyses SET status = 'pending', content = NULL, error = NULL, model_used = NULL, \ input_tokens = NULL, output_tokens = NULL, cost_usd = NULL, attempts = 0, next_attempt_at = now(), \ started_at = NULL, completed_at = NULL, model = $2, prompt_version = $3, cache_key = $4, \ fallback_model = $5, on_fallback = false, requested_at = now() \ WHERE id = $1 AND status IN ('done', 'failed') RETURNING {ANALYSIS_COLUMNS}" )) .bind(id) .bind(&model) .bind(&prompt_version) .bind(&cache_key) .bind(&fallback) .fetch_optional(&mut *tx) .await? .ok_or_else(|| ApiError::conflict("analysis_in_progress", "Ez az elemzés éppen készül."))?; // Wakes the worker, as a new analysis does. sqlx::query("SELECT pg_notify('analyses', '')").execute(&mut *tx).await?; tx.commit().await?; tracing::info!(%id, kind = kind.as_str(), "analysis regenerated"); Ok(Json(row.view())) }