Admin: the yearly forecast prompt is editable and can be tried on own forecasts
CI / test (push) Successful in 59s
CI / build (push) Successful in 5m29s
CI / deploy (push) Skipped

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-18 11:10:40 +00:00
co-authored by Claude Opus 5.5
parent f1bd34c394
commit b0e35433ac
7 changed files with 168 additions and 32 deletions
+24 -21
View File
@@ -27,7 +27,7 @@ use crate::error::{ApiError, ApiResult};
use crate::views::{rfc3339, rfc3339_opt};
/// The kinds whose prompt the admin page edits.
const EDITABLE: [Kind; 3] = [Kind::Summary, Kind::Natal, Kind::Synastry];
const EDITABLE: [Kind; 4] = [Kind::Summary, Kind::Natal, Kind::Synastry, Kind::Forecast];
const MAX_CHARS: usize = 50_000;
const HISTORY: i64 = 20;
/// Test runs of one admin waiting or being written at the same time.
@@ -162,6 +162,8 @@ pub struct StartPreview {
chart_id: Option<Uuid>,
/// One of the admin's own comparisons, for a synastry analysis.
synastry_id: Option<Uuid>,
/// A yearly forecast of one of the admin's own charts, for a forecast analysis.
forecast_id: Option<Uuid>,
}
#[derive(sqlx::FromRow)]
@@ -170,6 +172,7 @@ struct PreviewRow {
kind: String,
chart_id: Option<Uuid>,
synastry_id: Option<Uuid>,
forecast_id: Option<Uuid>,
language: String,
model: String,
status: String,
@@ -184,7 +187,7 @@ struct PreviewRow {
completed_at: Option<OffsetDateTime>,
}
const PREVIEW_COLUMNS: &str = "id, kind, chart_id, synastry_id, language, model, status, content, error, model_used, \
const PREVIEW_COLUMNS: &str = "id, kind, chart_id, synastry_id, forecast_id, language, model, status, content, error, model_used, \
input_tokens, output_tokens, cost_usd, created_at, started_at, completed_at";
impl PreviewRow {
@@ -192,6 +195,7 @@ impl PreviewRow {
let seconds = self.started_at.zip(self.completed_at).map(|(s, c)| (c - s).whole_seconds());
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, "model": self.model, "status": self.status, "content": self.content,
"error": self.error, "model_used": self.model_used, "input_tokens": self.input_tokens,
"output_tokens": self.output_tokens, "cost_usd": self.cost_usd, "created_at": rfc3339(self.created_at),
@@ -208,29 +212,27 @@ pub async fn start_preview(
) -> ApiResult<(StatusCode, Json<Value>)> {
let kind = parse_target(&state, &kind, &language)?;
let system = clean_system(&req.system)?;
// The target must be the admin's own: a chart, or for a synastry analysis a comparison.
let (chart_id, synastry_id, found) = match (kind, req.chart_id, req.synastry_id) {
(Kind::Synastry, None, Some(id)) => {
let row: Option<(Uuid,)> = sqlx::query_as("SELECT id FROM synastries WHERE id = $1 AND owner_id = $2")
.bind(id)
.bind(admin.id())
.fetch_optional(&state.db)
.await?;
(None, Some(id), row.map(|_| "synastry"))
// The target must be the admin's own: a chart, a comparison or a forecast.
let (chart_id, synastry_id, forecast_id) = (req.chart_id, req.synastry_id, req.forecast_id);
let (query, id, what) = match (kind, chart_id, synastry_id, forecast_id) {
(Kind::Summary | Kind::Natal, Some(id), None, None) => {
("SELECT id FROM charts WHERE id = $1 AND owner_id = $2", id, "chart")
}
(Kind::Summary | Kind::Natal, Some(id), None) => {
let row: Option<(Uuid,)> = sqlx::query_as("SELECT id FROM charts WHERE id = $1 AND owner_id = $2")
.bind(id)
.bind(admin.id())
.fetch_optional(&state.db)
.await?;
(Some(id), None, row.map(|_| "chart"))
(Kind::Synastry, None, Some(id), None) => {
("SELECT id FROM synastries WHERE id = $1 AND owner_id = $2", id, "synastry")
}
(Kind::Forecast, None, None, Some(id)) => (
"SELECT f.id FROM forecasts f JOIN charts c ON c.id = f.chart_id WHERE f.id = $1 AND c.owner_id = $2",
id,
"forecast",
),
(Kind::Synastry, ..) => return Err(ApiError::BadRequest("a synastry analysis needs synastry_id".into())),
(Kind::Forecast, ..) => return Err(ApiError::BadRequest("a forecast analysis needs forecast_id".into())),
_ => return Err(ApiError::BadRequest("this analysis needs chart_id".into())),
};
let found: Option<(Uuid,)> = sqlx::query_as(query).bind(id).bind(admin.id()).fetch_optional(&state.db).await?;
if found.is_none() {
return Err(ApiError::NotFound(if synastry_id.is_some() { "synastry" } else { "chart" }));
return Err(ApiError::NotFound(what));
}
let (open,): (i64,) = sqlx::query_as(
"SELECT count(*) FROM prompt_previews WHERE created_by = $1 AND status IN ('pending', 'running')",
@@ -249,13 +251,14 @@ pub async fn start_preview(
_ => &state.config.analysis_model,
};
let row: PreviewRow = sqlx::query_as(&format!(
"INSERT INTO prompt_previews (id, kind, chart_id, synastry_id, language, model, system, created_by) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING {PREVIEW_COLUMNS}"
"INSERT INTO prompt_previews (id, kind, chart_id, synastry_id, forecast_id, language, model, system, created_by) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING {PREVIEW_COLUMNS}"
))
.bind(Uuid::now_v7())
.bind(kind.as_str())
.bind(chart_id)
.bind(synastry_id)
.bind(forecast_id)
.bind(&language)
.bind(model)
.bind(&system)