Admin: the yearly forecast prompt is editable and can be tried on own forecasts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -929,7 +929,8 @@ async fn admins_edit_analysis_prompts(pool: PgPool) {
|
||||
let prompts = r.body["prompts"].as_array().unwrap();
|
||||
let keys: Vec<String> =
|
||||
prompts.iter().map(|p| format!("{}/{}", p["kind"], p["language"]).replace('"', "")).collect();
|
||||
assert_eq!(keys, ["summary/hu", "summary/en", "natal/hu", "natal/en", "synastry/hu", "synastry/en"]);
|
||||
let expected = ["summary", "natal", "synastry", "forecast"].map(|k| [format!("{k}/hu"), format!("{k}/en")]);
|
||||
assert_eq!(keys, expected.concat());
|
||||
let natal = &prompts[2];
|
||||
assert_eq!((&natal["custom"], &natal["system"]), (&json!(false), &natal["default"]));
|
||||
assert!(natal["user_template"].as_str().unwrap().contains("{chart data}"));
|
||||
@@ -1048,6 +1049,32 @@ async fn admins_try_prompt_drafts_on_own_charts(pool: PgPool) {
|
||||
"{}",
|
||||
r.body
|
||||
);
|
||||
|
||||
// a yearly forecast is tried on a forecast of one of the admin's charts
|
||||
sqlx::query("UPDATE prompt_previews SET status = 'done'").execute(&pool).await.unwrap();
|
||||
let own_id = own["id"].as_str().unwrap();
|
||||
let forecast = app.post(&format!("/v1/charts/{own_id}/forecasts"), Some(&admin), json!({})).await;
|
||||
assert_eq!(forecast.status, StatusCode::CREATED, "{}", forecast.body);
|
||||
let annas_id = annas["id"].as_str().unwrap();
|
||||
let annas_forecast = app.post(&format!("/v1/charts/{annas_id}/forecasts"), Some(&anna), json!({})).await;
|
||||
let uri = "/v1/admin/prompts/forecast/hu/preview";
|
||||
let r = app.call("POST", uri, Some(&admin), Some(json!({"system": "Próba.", "chart_id": own["id"]}))).await;
|
||||
assert_eq!(r.status, StatusCode::BAD_REQUEST);
|
||||
let body = json!({"system": "Próba.", "forecast_id": annas_forecast.body["id"]});
|
||||
assert_eq!(app.call("POST", uri, Some(&admin), Some(body)).await.status, StatusCode::NOT_FOUND);
|
||||
let body = json!({"system": "Próba.", "forecast_id": forecast.body["id"]});
|
||||
let r = app.call("POST", uri, Some(&admin), Some(body)).await;
|
||||
assert_eq!(
|
||||
(r.status, r.body["forecast_id"].as_str()),
|
||||
(StatusCode::ACCEPTED, forecast.body["id"].as_str()),
|
||||
"{}",
|
||||
r.body
|
||||
);
|
||||
// and its prompt is listed and can be saved like the others
|
||||
let prompts = app.get("/v1/admin/prompts", Some(&admin)).await.body;
|
||||
assert!(prompts["prompts"].as_array().unwrap().iter().any(|p| p["kind"] == "forecast"));
|
||||
let r = app.call("PUT", "/v1/admin/prompts/forecast/hu", Some(&admin), Some(json!({"system": "Saját."}))).await;
|
||||
assert_eq!((r.status, &r.body["custom"]), (StatusCode::OK, &json!(true)), "{}", r.body);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
|
||||
Reference in New Issue
Block a user