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
+10 -12
View File
@@ -28,12 +28,12 @@ pub async fn me(State(state): St, auth: AuthUser) -> ApiResult<Json<Value>> {
.bind(auth.id())
.fetch_optional(&state.db)
.await?;
let balance = credits::balance(&state.db, auth.id(), state.config.monthly_free_credits).await?;
let balance = credits::balance(&mut *state.db.acquire().await?, auth.id()).await?;
Ok(Json(json!({
"user": UserView::from(&auth.user),
"providers": providers.into_iter().map(|(p,)| p).collect::<Vec<_>>(),
"primary_chart_id": primary.map(|(id,)| id),
"credits": balance,
"analyses_left": balance.view(),
})))
}
@@ -112,22 +112,20 @@ pub async fn change_password(
}
pub async fn credits(State(state): St, auth: AuthUser) -> ApiResult<Json<Value>> {
let balance = credits::balance(&state.db, auth.id(), state.config.monthly_free_credits).await?;
type Row = (Uuid, i32, String, Option<String>, Option<Uuid>, Option<String>, OffsetDateTime);
let balance = credits::balance(&mut *state.db.acquire().await?, auth.id()).await?;
type Row = (Uuid, String, i32, String, Option<Uuid>, Option<String>, OffsetDateTime);
let ledger: Vec<Row> = sqlx::query_as(
"SELECT id, delta, reason, period, analysis_id, note, created_at FROM credit_ledger \
"SELECT id, unit, delta, reason, analysis_id, note, created_at FROM credit_ledger \
WHERE user_id = $1 ORDER BY created_at DESC LIMIT 100",
)
.bind(auth.id())
.fetch_all(&state.db)
.await?;
Ok(Json(json!({
"balance": balance,
"monthly_free_credits": state.config.monthly_free_credits,
"llm_models": state.config.llm_models,
"period": credits::current_period(),
"ledger": ledger.into_iter().map(|(id, delta, reason, period, analysis_id, note, at)| json!({
"id": id, "delta": delta, "reason": reason, "period": period,
"balance": balance.view(),
"analysis_model": state.config.analysis_model,
"ledger": ledger.into_iter().map(|(id, unit, delta, reason, analysis_id, note, at)| json!({
"id": id, "unit": unit, "delta": delta, "reason": reason,
"analysis_id": analysis_id, "note": note, "created_at": rfc3339(at),
})).collect::<Vec<_>>(),
})))
@@ -153,7 +151,7 @@ pub async fn export(State(state): St, auth: AuthUser) -> ApiResult<impl IntoResp
"synastries": rows("SELECT json_build_object('id', s.id, 'owner_id', s.owner_id, 'chart1_id', s.chart1_id, 'chart2_id', s.chart2_id, 'result', s.result, 'created_at', s.created_at) FROM synastries s JOIN charts c1 ON c1.id = s.chart1_id JOIN charts c2 ON c2.id = s.chart2_id WHERE s.owner_id = $1 OR c1.owner_id = $1 OR c2.owner_id = $1").await?,
"invites": rows("SELECT json_build_object('id', id, 'chart_id', inviter_chart_id, 'message', message, 'created_at', created_at, 'expires_at', expires_at, 'accepted_at', accepted_at) FROM synastry_invites WHERE inviter_id = $1").await?,
"analyses": rows("SELECT json_build_object('id', a.id, 'kind', a.kind, 'chart_id', a.chart_id, 'synastry_id', a.synastry_id, 'language', a.language, 'model', a.model, 'status', a.status, 'content', a.content, 'created_at', a.created_at) FROM analyses a WHERE a.requested_by = $1 OR a.chart_id IN (SELECT id FROM charts WHERE owner_id = $1)").await?,
"credit_ledger": rows("SELECT json_build_object('delta', delta, 'reason', reason, 'period', period, 'note', note, 'created_at', created_at) FROM credit_ledger WHERE user_id = $1 ORDER BY created_at").await?,
"credit_ledger": rows("SELECT json_build_object('unit', unit, 'delta', delta, 'reason', reason, 'note', note, 'created_at', created_at) FROM credit_ledger WHERE user_id = $1 ORDER BY created_at").await?,
"shares": rows("SELECT json_build_object('slug', slug, 'kind', kind, 'title', title, 'show_birth_data', show_birth_data, 'view_count', view_count, 'created_at', created_at, 'revoked_at', revoked_at) FROM shares WHERE owner_id = $1").await?,
});
Ok(([("content-disposition", "attachment; filename=\"sorsfonalak-export.json\"")], Json(document)))