I hate this fucking language.

This commit is contained in:
Frank Schwenk
2026-06-10 09:43:50 +02:00
parent 66fa24872d
commit fef0454895
10 changed files with 193 additions and 43 deletions
+85 -19
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import logging
from datetime import datetime
from typing import Any
from zoneinfo import ZoneInfo
@@ -12,6 +13,19 @@ from server.messages import pick
from server.settings import settings
from server.stats import compute_stats
logger = logging.getLogger(__name__)
MOTIVATION_SYSTEM = (
"Du schreibst Tages-Motivationssprüche für 'ADHD live, laugh, toaster bath motivational'. "
"Stil: dark humor, Sarkasmus, passiv-aggressiv, kreativ-motivierend — nie gemein, nie medizinisch. "
"Genau ein Satz auf Deutsch, max 25 Wörter. Keine Anführungszeichen um den Spruch."
)
ORACLE_SYSTEM = (
"Du bist das wöchentliche KI-Orakel einer ADHS-Medikamenten-App. "
"Passiv-aggressiv, trocken, max 4 Sätze auf Deutsch. Keine medizinischen Ratschläge."
)
async def _get_cache(db: aiosqlite.Connection, key: str) -> str | None:
cur = await db.execute("SELECT content FROM ai_cache WHERE key = ?", (key,))
@@ -28,7 +42,7 @@ async def _set_cache(db: aiosqlite.Connection, key: str, content: str) -> None:
await db.commit()
async def _call_openrouter(prompt: str) -> str | None:
async def _call_openrouter(prompt: str, *, system: str) -> str | None:
if not settings.openrouter_api_key:
return None
try:
@@ -43,10 +57,7 @@ async def _call_openrouter(prompt: str) -> str | None:
json={
"model": settings.openrouter_model,
"messages": [
{
"role": "system",
"content": "Du bist ein sarkastischer, dark-humor Medikamenten-Coach für jemanden mit ADHS. Kurz (max 2 Sätze), deutsch, witzig aber nicht gemein. Keine medizinischen Ratschläge.",
},
{"role": "system", "content": system},
{"role": "user", "content": prompt},
],
"max_tokens": 120,
@@ -56,29 +67,62 @@ async def _call_openrouter(prompt: str) -> str | None:
data = resp.json()
return data["choices"][0]["message"]["content"].strip()
except Exception:
logger.exception("OpenRouter request failed")
return None
async def get_roast_of_the_day(db: aiosqlite.Connection, tz: ZoneInfo) -> dict[str, Any]:
day = datetime.now(tz).strftime("%Y-%m-%d")
key = f"roast:{day}"
cached = await _get_cache(db, key)
def _motivation_key(day: str) -> str:
return f"motivation:{day}"
async def _cached_motivation(db: aiosqlite.Connection, day: str) -> str | None:
cached = await _get_cache(db, _motivation_key(day))
if cached:
return cached
return await _get_cache(db, f"roast:{day}")
async def generate_daily_motivation(
db: aiosqlite.Connection,
tz: ZoneInfo,
day: str | None = None,
) -> dict[str, Any]:
day = day or datetime.now(tz).strftime("%Y-%m-%d")
key = _motivation_key(day)
cached = await _cached_motivation(db, day)
if cached:
return {"text": cached, "source": "cache", "day": day}
stats = await compute_stats(db)
prompt = f"Roast-of-the-Day für jemanden mit ADHS. Streak: {stats['streak']} Tage, Compliance 90d: {stats['compliance_percent']}%. Ein sarkastischer Spruch."
text = await _call_openrouter(prompt)
prompt = (
"Schreibe den Motivationsspruch für heute. "
f"Streak: {stats['streak']} Tage. "
f"Compliance über {stats['compliance_window_days']} aktive Tage: {stats['compliance_percent']}%. "
"Beispiel-Stil: Dein Gehirn ist nicht kaputt. Es läuft nur auf einem anderen Betriebssystem — ohne Support."
)
text = await _call_openrouter(prompt, system=MOTIVATION_SYSTEM)
if not text:
text = pick("roast_fallback")
text = pick("motivation_fallback")
source = "fallback"
else:
source = "ai"
await _set_cache(db, key, text)
logger.info("Generated daily motivation for %s (%s)", day, source)
return {"text": text, "source": source, "day": day}
async def get_daily_motivation(db: aiosqlite.Connection, tz: ZoneInfo) -> dict[str, Any]:
day = datetime.now(tz).strftime("%Y-%m-%d")
cached = await _cached_motivation(db, day)
if cached:
return {"text": cached, "source": "cache", "day": day}
return {"text": pick("motivation_fallback"), "source": "fallback", "day": day}
async def get_roast_of_the_day(db: aiosqlite.Connection, tz: ZoneInfo) -> dict[str, Any]:
return await get_daily_motivation(db, tz)
async def get_oracle(db: aiosqlite.Connection, tz: ZoneInfo) -> dict[str, Any]:
week = datetime.now(tz).strftime("%Y-W%W")
key = f"oracle:{week}"
@@ -87,14 +131,36 @@ async def get_oracle(db: aiosqlite.Connection, tz: ZoneInfo) -> dict[str, Any]:
return {"text": cached, "source": "cache", "week": week}
stats = await compute_stats(db)
prompt = (
f"Wöchentlicher KI-Orakel-Report (passiv-aggressiv, max 4 Sätze). "
f"Streak: {stats['streak']}, Compliance 90d: {stats['compliance_percent']}%, "
f"genommen gesamt: {stats['total_taken']}."
days_active = stats["days_active"]
window = stats["compliance_window_days"]
young_app = days_active < 90
context = (
f"App aktiv seit {days_active} Tag(en), erste Einnahme am {stats['first_day']}. "
if young_app
else ""
)
text = await _call_openrouter(prompt)
caveat = (
"Die App ist noch jung — lange Streaks oder 90-Tage-Compliance sind noch nicht realistisch erreichbar. "
"Bewerte nur die verfügbaren Daten, sei fair aber sarkastisch. "
if young_app
else ""
)
prompt = (
f"Wöchentlicher Orakel-Report. {context}{caveat}"
f"Streak: {stats['streak']} Tage. "
f"Compliance über {window} Tag(e): {stats['compliance_percent']}%. "
f"Genommen gesamt: {stats['total_taken']}."
)
text = await _call_openrouter(prompt, system=ORACLE_SYSTEM)
if not text:
text = pick("streak", streak=stats["streak"]) + f" Compliance: {stats['compliance_percent']}%."
if young_app:
text = (
f"Tag {days_active} deiner Medis-Karriere. "
f"Streak: {stats['streak']}. Compliance ({window} Tage): {stats['compliance_percent']}%. "
"Das Orakel ist beeindruckt — oder gelangweilt. Grenzwertig."
)
else:
text = pick("streak", streak=stats["streak"]) + f" Compliance: {stats['compliance_percent']}%."
source = "fallback"
else:
source = "ai"