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:
@@ -4,7 +4,7 @@
|
||||
//! (`running`), builds the prompt from the stored chart data, asks the model
|
||||
//! through OpenRouter and stores the structured answer (`done`). Temporary
|
||||
//! failures are retried with backoff; after the last attempt, or on a
|
||||
//! permanent error, the analysis is `failed` and the credits are refunded.
|
||||
//! permanent error, the analysis is `failed` and the unit is refunded.
|
||||
|
||||
pub mod compact;
|
||||
pub mod openrouter;
|
||||
@@ -165,27 +165,29 @@ impl Analyzer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Marks the analysis failed and gives the credits back (once).
|
||||
/// Marks the analysis failed and gives its unit back (once).
|
||||
async fn fail(&self, id: Uuid, message: &str) -> Result<(), sqlx::Error> {
|
||||
let mut tx = self.db.begin().await?;
|
||||
let row: Option<(Option<Uuid>, i32)> = sqlx::query_as(
|
||||
let row: Option<(Option<Uuid>, i32, String)> = sqlx::query_as(
|
||||
"UPDATE analyses SET status = 'failed', error = $2, completed_at = now() \
|
||||
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged",
|
||||
WHERE id = $1 AND status = 'running' RETURNING requested_by, credits_charged, kind",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(message)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?;
|
||||
if let Some((Some(user), credits)) = row
|
||||
&& credits > 0
|
||||
if let Some((Some(user), units, kind)) = row
|
||||
&& units > 0
|
||||
{
|
||||
let unit = if kind == "synastry" { "synastry" } else { "natal" };
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, delta, reason, analysis_id, note) \
|
||||
VALUES ($1, $2, $3, 'refund', $4, 'analysis failed') ON CONFLICT DO NOTHING",
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, analysis_id, note) \
|
||||
VALUES ($1, $2, $3, $4, 'refund', $5, 'analysis failed') ON CONFLICT DO NOTHING",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(user)
|
||||
.bind(credits)
|
||||
.bind(unit)
|
||||
.bind(units)
|
||||
.bind(id)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::ser::{Serialize, SerializeMap, Serializer};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::prompt::{Prompt, validate};
|
||||
@@ -44,6 +45,77 @@ impl LlmError {
|
||||
}
|
||||
}
|
||||
|
||||
/// A JSON schema serialized with every `properties` object in the order of
|
||||
/// its sibling `required` list. `serde_json` sorts object keys, and models
|
||||
/// write strict structured output in schema order: sorted, the title came
|
||||
/// after 9000 tokens of text and was sometimes left empty.
|
||||
pub struct Ordered<'a>(pub &'a Value);
|
||||
|
||||
impl Serialize for Ordered<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
match self.0 {
|
||||
Value::Object(map) => {
|
||||
let order: Vec<&str> =
|
||||
map.get("required").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect();
|
||||
let mut out = serializer.serialize_map(Some(map.len()))?;
|
||||
for (key, value) in map {
|
||||
match (key.as_str(), value) {
|
||||
("properties", Value::Object(props)) => out.serialize_entry(key, &Properties(props, &order))?,
|
||||
_ => out.serialize_entry(key, &Ordered(value))?,
|
||||
}
|
||||
}
|
||||
out.end()
|
||||
}
|
||||
Value::Array(items) => serializer.collect_seq(items.iter().map(Ordered)),
|
||||
other => other.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The request body; a struct (not `json!`) so the schema keeps its order.
|
||||
#[derive(serde::Serialize)]
|
||||
struct Request<'a> {
|
||||
model: &'a str,
|
||||
messages: Value,
|
||||
response_format: ResponseFormat<'a>,
|
||||
provider: Value,
|
||||
max_tokens: u32,
|
||||
usage: Value,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct ResponseFormat<'a> {
|
||||
#[serde(rename = "type")]
|
||||
kind: &'static str,
|
||||
json_schema: JsonSchema<'a>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct JsonSchema<'a> {
|
||||
name: &'static str,
|
||||
strict: bool,
|
||||
schema: Ordered<'a>,
|
||||
}
|
||||
|
||||
/// `properties` in `required` order (any others after them).
|
||||
struct Properties<'a>(&'a serde_json::Map<String, Value>, &'a [&'a str]);
|
||||
|
||||
impl Serialize for Properties<'_> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
let (props, order) = (self.0, self.1);
|
||||
let mut out = serializer.serialize_map(Some(props.len()))?;
|
||||
for key in order {
|
||||
if let Some(value) = props.get(*key) {
|
||||
out.serialize_entry(key, &Ordered(value))?;
|
||||
}
|
||||
}
|
||||
for (key, value) in props.iter().filter(|(k, _)| !order.contains(&k.as_str())) {
|
||||
out.serialize_entry(key, &Ordered(value))?;
|
||||
}
|
||||
out.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// The JSON object in a reply, also when wrapped in a ``` fence or prose.
|
||||
fn extract_json(text: &str) -> Option<Value> {
|
||||
let trimmed = text.trim();
|
||||
@@ -74,21 +146,21 @@ impl OpenRouter {
|
||||
}
|
||||
|
||||
pub async fn complete(&self, model: &str, prompt: &Prompt) -> Result<Completion, LlmError> {
|
||||
let body = json!({
|
||||
"model": model,
|
||||
"messages": [
|
||||
let body = Request {
|
||||
model,
|
||||
messages: json!([
|
||||
{"role": "system", "content": prompt.system},
|
||||
{"role": "user", "content": prompt.user},
|
||||
],
|
||||
"response_format": {
|
||||
"type": "json_schema",
|
||||
"json_schema": {"name": prompt.schema_name, "strict": true, "schema": prompt.schema},
|
||||
]),
|
||||
response_format: ResponseFormat {
|
||||
kind: "json_schema",
|
||||
json_schema: JsonSchema { name: prompt.schema_name, strict: true, schema: Ordered(&prompt.schema) },
|
||||
},
|
||||
// Only route to providers that honour the schema.
|
||||
"provider": {"require_parameters": true},
|
||||
"max_tokens": MAX_TOKENS,
|
||||
"usage": {"include": true},
|
||||
});
|
||||
provider: json!({"require_parameters": true}),
|
||||
max_tokens: MAX_TOKENS,
|
||||
usage: json!({"include": true}),
|
||||
};
|
||||
let mut request = self
|
||||
.http
|
||||
.post(format!("{}/chat/completions", self.base_url))
|
||||
@@ -145,6 +217,22 @@ impl OpenRouter {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn schema_properties_follow_required_order() {
|
||||
let schema = super::super::prompt::schema(super::super::prompt::Kind::Natal);
|
||||
let text = serde_json::to_string(&Ordered(&schema)).unwrap();
|
||||
let pos = |needle: &str| text.find(needle).unwrap();
|
||||
assert!(pos("\"title\":{") < pos("\"summary\":{"));
|
||||
assert!(pos("\"summary\":{") < pos("\"sections\":{"));
|
||||
assert!(pos("\"personality\":{") < pos("\"growth\":{"));
|
||||
assert!(pos("\"strengths\":{") < pos("\"advice\":{"));
|
||||
// inside a section the title comes before the body
|
||||
let section = &text[pos("\"personality\":{")..];
|
||||
assert!(section.find("\"title\":{").unwrap() < section.find("\"body\":{").unwrap());
|
||||
// the content is unchanged
|
||||
assert_eq!(serde_json::from_str::<Value>(&text).unwrap(), schema);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracts_json_from_fences_and_prose() {
|
||||
assert_eq!(extract_json(r#"{"a":1}"#), Some(json!({"a": 1})));
|
||||
|
||||
@@ -324,11 +324,13 @@ mod analyses {
|
||||
}
|
||||
|
||||
async fn refunds(db: &PgPool, user: Uuid) -> Vec<i32> {
|
||||
sqlx::query_scalar("SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund'")
|
||||
.bind(user)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap()
|
||||
sqlx::query_scalar(
|
||||
"SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund' AND unit = 'natal'",
|
||||
)
|
||||
.bind(user)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn make_due(db: &PgPool, id: Uuid) {
|
||||
|
||||
Reference in New Issue
Block a user