//! Answers to questions about a finished analysis (`questions` rows). The //! question model answers, seeing the chart data the analysis was written //! from, the analysis itself and the earlier questions about it. Retries, //! fallback and refunds work as for analyses. use std::time::{Duration, Instant}; use serde_json::Value; use uuid::Uuid; use super::openrouter::{Completion, LlmError}; use super::prompt::{self, Kind}; use super::{Analyzer, Next, active_model, next_step}; /// The earlier questions and answers given to the model, at most this many. const HISTORY: i64 = 6; #[derive(Debug, sqlx::FromRow)] pub struct QuestionJob { pub id: Uuid, pub analysis_id: Uuid, pub question: String, pub language: String, pub model: String, pub prompt_version: String, pub attempts: i32, pub created_at: time::OffsetDateTime, pub fallback_model: Option, pub on_fallback: bool, /// The token of this attempt (see `Job::started_at`). pub started_at: time::OffsetDateTime, } impl Analyzer { /// Takes the next due question, if any. pub async fn claim_question(&self) -> Result, sqlx::Error> { let job: Option = sqlx::query_as( "UPDATE questions SET status = 'running', started_at = now(), attempts = attempts + 1 \ WHERE id = (SELECT id FROM questions WHERE status = 'pending' AND next_attempt_at <= now() \ ORDER BY next_attempt_at LIMIT 1 FOR UPDATE SKIP LOCKED) \ RETURNING id, analysis_id, question, language, model, prompt_version, attempts, created_at, \ fallback_model, on_fallback, started_at", ) .fetch_optional(&self.db) .await?; if let Some(job) = &job { self.running.lock().unwrap().insert(job.id); } Ok(job) } async fn question_prompt(&self, job: &QuestionJob) -> Result { let db_err = |e: sqlx::Error| LlmError::Transient(format!("database: {e}")); type Row = (String, Option, Option, Option, Option); let (kind, chart_id, synastry_id, forecast_id, content): Row = sqlx::query_as("SELECT kind, chart_id, synastry_id, forecast_id, content FROM analyses WHERE id = $1") .bind(job.analysis_id) .fetch_optional(&self.db) .await .map_err(db_err)? .ok_or_else(|| LlmError::Permanent("the analysis was deleted".into()))?; let kind = Kind::parse(&kind).ok_or_else(|| LlmError::Permanent(format!("unknown kind '{kind}'")))?; let content = content.ok_or_else(|| LlmError::Permanent("the analysis has no text".into()))?; let chart_text = match kind { Kind::Forecast => self.forecast_text(forecast_id).await?, Kind::Synastry => self.synastry_text(synastry_id).await?, _ => self.natal_text(chart_id).await?, }; let analysis = prompt::to_markdown(kind, &job.language, &content); let mut history: Vec<(String, String)> = sqlx::query_as( "SELECT question, answer FROM questions \ WHERE analysis_id = $1 AND status = 'done' AND answer IS NOT NULL AND created_at < $2 \ ORDER BY created_at DESC LIMIT $3", ) .bind(job.analysis_id) .bind(job.created_at) .bind(HISTORY) .fetch_all(&self.db) .await .map_err(db_err)?; history.reverse(); let (_, revision) = prompt::split_version(&job.prompt_version); let system: Option = match revision { Some(id) => sqlx::query_scalar("SELECT system FROM prompt_revisions WHERE id = $1") .bind(id) .fetch_optional(&self.db) .await .map_err(db_err)? .flatten(), None => None, }; Ok(prompt::build_question(&job.language, &chart_text, &analysis, &history, &job.question, system.as_deref())) } /// Answers one claimed question and records the outcome. pub async fn handle_question(&self, job: QuestionJob) { let started = Instant::now(); let work = async { match self.question_prompt(&job).await { Ok(p) => { let model = active_model(&job.model, job.fallback_model.as_deref(), job.on_fallback); self.llm.complete(model, &p).await.map(|mut c| { prompt::polish(&job.language, &mut c.content); c }) } Err(e) => Err(e), } }; let result = tokio::select! { result = work => result, () = self.superseded("questions", job.id, job.started_at) => { tracing::info!(id = %job.id, "answer restarted by an admin; this attempt is dropped"); // Not ours any more: a stop must not hand it back to the queue. self.running.lock().unwrap().remove(&job.id); return; } }; if let Err(e) = self.finish_question(&job, result, started.elapsed()).await { tracing::error!(id = %job.id, error = %e, "saving the answer failed"); } self.running.lock().unwrap().remove(&job.id); } async fn finish_question( &self, job: &QuestionJob, result: Result, took: Duration, ) -> Result<(), sqlx::Error> { match result { Ok(c) => { let answer = c.content["answer"].as_str().unwrap_or_default().trim().to_string(); sqlx::query( "UPDATE questions SET status = 'done', answer = $2, model_used = $3, input_tokens = $4, \ output_tokens = $5, cost_usd = $6, error = NULL, completed_at = now() \ WHERE id = $1 AND status = 'running' AND started_at = $7", ) .bind(job.id) .bind(&answer) .bind(&c.model) .bind(c.input_tokens) .bind(c.output_tokens) .bind(c.cost_usd) .bind(job.started_at) .execute(&self.db) .await?; tracing::info!( id = %job.id, model = %c.model, cost_usd = c.cost_usd, words = answer.split_whitespace().count(), secs = took.as_secs(), "answer done" ); } Err(e) => { let can_fall_back = !job.on_fallback && job.fallback_model.is_some(); let next = next_step(&e, job.attempts, self.max_attempts, can_fall_back); let outcome = match next { Next::Retry(_) => "retry", Next::Fallback => "fallback", Next::Fail => "failed", }; let model = active_model(&job.model, job.fallback_model.as_deref(), job.on_fallback); self.log_error("question", job.id, "question", model, job.attempts, outcome, e.message(), took).await; match next { Next::Retry(wait) => { sqlx::query( "UPDATE questions SET status = 'pending', error = $2, \ next_attempt_at = now() + make_interval(secs => $3) \ WHERE id = $1 AND status = 'running' AND started_at = $4", ) .bind(job.id) .bind(e.message()) .bind(wait.as_secs_f64()) .bind(job.started_at) .execute(&self.db) .await?; tracing::warn!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer will be retried"); } Next::Fallback => { sqlx::query( "UPDATE questions SET status = 'pending', error = $2, on_fallback = true, attempts = 0, \ next_attempt_at = now() WHERE id = $1 AND status = 'running' AND started_at = $3", ) .bind(job.id) .bind(e.message()) .bind(job.started_at) .execute(&self.db) .await?; tracing::warn!( id = %job.id, model = %job.model, error = %e.message(), "the chosen model failed, the default model answers" ); } Next::Fail => { self.fail_question(job.id, job.started_at, e.message()).await?; tracing::error!(id = %job.id, attempts = job.attempts, error = %e.message(), "answer failed"); } } } } Ok(()) } /// Marks the question failed and gives its unit back (once). async fn fail_question( &self, id: Uuid, started_at: time::OffsetDateTime, message: &str, ) -> Result<(), sqlx::Error> { let mut tx = self.db.begin().await?; let row: Option<(Option, i32)> = sqlx::query_as( "UPDATE questions SET status = 'failed', error = $2, completed_at = now() \ WHERE id = $1 AND status = 'running' AND started_at = $3 RETURNING asked_by, credits_charged", ) .bind(id) .bind(message) .bind(started_at) .fetch_optional(&mut *tx) .await?; if let Some((Some(user), units)) = row && units > 0 { sqlx::query( "INSERT INTO credit_ledger (id, user_id, unit, delta, reason, question_id, note) \ VALUES ($1, $2, 'question', $3, 'refund', $4, 'answer failed') ON CONFLICT DO NOTHING", ) .bind(Uuid::now_v7()) .bind(user) .bind(units) .bind(id) .execute(&mut *tx) .await?; } tx.commit().await } }