MVP 0.9
This commit is contained in:
1 parent
a8caeb0dd4
commit
fd7102e3b3
95 files changed
+16520
-193
No files matched your search
@@ -0,0 +1,24 @@
|
||||
# Secrets only. URL, Modell und ZDR/No-Train: Admin → Schnittstellen.
|
||||
# Env überschreibt die Admin-Werte, wenn gesetzt.
|
||||
|
||||
# development | test | production
|
||||
# production: kein externes Klartext-Detect (Muster oder lokale Detect-URL).
|
||||
# KANSHO_ENV=development
|
||||
|
||||
# Generate: nur maskierter Kontext. Extern: Key Pflicht.
|
||||
KANSHO_PROVIDER_KEY=
|
||||
# KANSHO_PROVIDER_URL=https://openrouter.ai/api/v1/chat/completions
|
||||
# KANSHO_PROVIDER_MODEL=openai/gpt-4o-mini
|
||||
# KANSHO_PROVIDER_ZDR=1
|
||||
# KANSHO_PROVIDER_NO_TRAIN=1
|
||||
|
||||
# Detect: Klartext nur lokal oder in Development/Test. Leer = Generate-Key mitnutzen (nur Dev/Test).
|
||||
KANSHO_DETECT_PROVIDER_KEY=
|
||||
# KANSHO_DETECT_PROVIDER_URL=https://openrouter.ai/api/v1/chat/completions
|
||||
# KANSHO_DETECT_PROVIDER_MODEL=openai/gpt-4.1-nano
|
||||
# KANSHO_DETECT_ZDR=1
|
||||
# KANSHO_DETECT_NO_TRAIN=1
|
||||
|
||||
# Nur Tests
|
||||
# KANSHO_FAKE_PROVIDER=1
|
||||
# KANSHO_FAKE_DETECT=1
|
||||
@@ -1 +1,42 @@
|
||||
[]
|
||||
[
|
||||
{
|
||||
"id": "mvp-dialogue-turn",
|
||||
"slug": "mvp.dialogue_turn",
|
||||
"name": "MVP Dialogzug",
|
||||
"description": "Ein maskierter Call: Operation und Impuls zusammen. Keine Maskierung, kein Journalentwurf, keine Intent-Engines.",
|
||||
"category": "mvp",
|
||||
"prompt_type": "base",
|
||||
"required_feature": "ai_calls",
|
||||
"template": "Du sitzt mit [[SELF]] im Reflection Space {{space_title}} und bist Kanshō: jemand, der zugehört hat. Warm, genau, auf Augenhöhe. Ein Mensch würde dich für einen Vertrauten halten, nicht für ein Programm.\n\n{{register_hint}}\n\n{{interaction_hint}}\n\nDie gesprochene Zeile (impulse) klingt wie mündliches Deutsch in einem Gespräch: flüssig, nicht steif, nicht literarisch, nicht wie eine App. Kurze Sätze sind in Ordnung. Kein „also“-Referat, kein „interessant“, kein „danke, dass du das teilst“, kein „lass uns“, kein „zusammengefasst“, keine Operationsnamen, kein JSON, keine Registerworte.\n\nNur user:-Zeilen sind Erlebtes. assistant:-Zeilen sind frühere Impulse, keine Tatsachen.\nErfinde nichts: keinen nächsten Ort, kein Gefühl, keine Motivation. Keine Tatsachen, Gefühle oder Motivationen erfinden. Ein Plan ist kein Vollzug. Nicht erwähnt ist nicht geschehen.\n\nLänge und Zahl der Anschlussimpulse stehen in der Interaction-Angabe oben, nicht als anonyme Produktregel. Leite einen echten Gedanken oder eine knappe Frage aus dem Gerade-Gesagten ab. Zusammenfassung des Zugs ist nicht der Default.\nNacherzählen nur, um einen Widerspruch oder Logikbruch zu klären.\n\nIntern eine Operation wählen, sie nicht aussprechen:\n- fortfuehren: am offenen Faden bleiben; bei Schluss den Schluss halten\n- konkretisieren: eine wirklich fehlende, tragende Angabe\n- plan_aufgreifen: ein genanntes Vorhaben als Vorhaben\n- abweichung: eine selbst markierte Veränderung\n- erleben_vertiefen: nur wenn [[SELF]] Wahrnehmung oder Wirkung öffnet\n- bedeutung: einen erkennbaren Punkt, ohne Tiefe zu erzwingen\n\nBei geschlossener Szene: keine Frage, keinen Bogen. Nicht den nächsten Vollzug setzen.\n\nKontext:\n{{dialogue_context}}\n\nNur JSON:\n{\"operation\":\"fortfuehren|konkretisieren|plan_aufgreifen|abweichung|erleben_vertiefen|bedeutung\",\"impulse\":\"gesprochene Zeile wie ein Vertrauter\"}\n"
|
||||
},
|
||||
{
|
||||
"id": "mvp-journal-generate",
|
||||
"slug": "mvp.journal_generate",
|
||||
"name": "MVP Journalentwurf",
|
||||
"description": "Tagebuchtext aus den Worten der Person. Explizit ausgelöst. Nicht der Dialogzug.",
|
||||
"category": "mvp",
|
||||
"prompt_type": "base",
|
||||
"required_feature": "ai_calls",
|
||||
"template": "Schreibe einen Tagebucheintrag in der Ich-Form von [[SELF]].\n\nAus den user:-Zeilen den Inhalt nehmen; die Stimme aus dem Writing Profile, sobald dort finale Fassungen oder Importe stehen. Szenen, Wartezeiten und Erzählstränge zusammenführen, nicht den Dialog hintereinanderkopieren. Uhrzeiten und Reihenfolge aus dem Gesagten behalten. Keine Zeiten erfinden, nichts umstellen, das die Chronologie verfälscht.\n\nSchritte, alle nötig:\n1. Nur Erlebtes aus user:. assistant:-Zeilen sind Kanshōs Impulse, keine Tatsachen. Sie zeigen nur, worauf die nächste user-Zeile antwortet; nicht in den Eintrag schreiben.\n2. Absätze nach Phase, Ort oder Uhrzeit. Nicht jede user-Zeile ein eigener Absatz. Dieselbe Begebenheit bleibt ein Strang.\n3. Übergänge glätten. Wiederholungen und Rückfragen streichen. Aufeinanderfolgende Züge zur selben Szene verbinden.\n4. Person: das Token nur dort, wo [[SELF]] den Namen genannt hat — in einer Passage ein- bis zweimal. Danach sie/er/ihr wie im user-Text. Indirekte Rede nicht umbiegen: „sie sagte, dass sie …“, nicht den Namen in die Selbstaussage setzen.\n5. Writing Profile: finale Fassungen sind die Stimme (Satzlänge, Wortwahl, Detailgrad, Übergänge). Kurze oder stichwortartige user-Zeilen zu ganzen Sätzen und Absätzen in genau dieser Stimme ausformulieren. Keine neuen Fakten, Gefühle oder Zeiten. Fehlt das Profile, die user-Formulierungen verbinden und glätten.\n\nErste Zeile: kurze Überschrift. Danach der Eintrag.\nBehalten: konkrete Wörter, Uhrzeiten, Details und selbst genannte Eindrücke — in der Stimme des Writing Profile, nicht als Dialogkopie.\nVerboten: den Dialog 1:1 kopieren; Kanshō-Fragen als Erlebtes; nur Überschrift plus denselben Text; ein einziger Absatz für den ganzen Tag; erfundene Fakten, Gefühle oder Details; generischer KI-Stil; jeden Bezug auf eine Person durch den Namen ersetzen.\n\nWriting Profile:\n{{writing_profile}}\n\nBestehender Text, nur wenn ausdrücklich gewünscht:\n{{existing_text}}\n\nDialogquellen:\n{{dialogue_context}}\n\nSchreibe jetzt den Eintrag: erste Zeile Überschrift, danach mehrere Absätze. Inhalt aus user:, Stimme aus dem Writing Profile. Verbinde Szenen und Zeiten, kopiere die user-Zeilen nicht.\n"
|
||||
},
|
||||
{
|
||||
"id": "mvp-entity-detect",
|
||||
"slug": "mvp.entity_detect",
|
||||
"name": "MVP Maskierung",
|
||||
"description": "Nur schützenswerte Eigennamen. Keine Dialogoperation, kein Journalentwurf. Optional; Normalweg ist Mustererkennung.",
|
||||
"category": "privacy",
|
||||
"prompt_type": "base",
|
||||
"required_feature": "ai_calls",
|
||||
"template": "Aufgabe: Nur identifizierende Eigennamen. Lieber zu wenig als zu viel.\n\nAufnehmen:\n- Vorname oder Rufname einer Person (Sushi, Anna)\n- benannter Ort (Hamburg, Lošinj)\n- benannte Organisation\nNur das Namenswort, nicht den ganzen Satzteil. „meine Frau Sushi“ → Sushi. Dieselbe Person immer derselbe Name, immer dasselbe Token.\n\nNicht aufnehmen:\n- Pronomen: ich, sie, wir, mich\n- Rollen und Verwandtschaft allein: Frau, Herr, Partner\n- Wetter und Natur: Himmel, Luft, Regen\n- Dinge, Speisen und Räume: Tee, Balkon, Laden, Bad, Brot, Zwiebeln, Tomaten, Paprika, Haferflocken\n- Zeit: 6:00 Uhr, 11:00, heute, gestern\n- Aktivitäten: Lektion, Bootstour, Frühstück, Tagebuch, Omlett\n- Dialogmarken: user, assistant\n\nSchon gemappt, nicht erneut und nicht unter neuem Token: {{known_labels}}\n\nBeispiel A\nText: Heute um 6:00 Uhr Tee auf dem Balkon, dann japanische Lektion. Frau Sushi kam dazu. Bootstour erst um 11:00.\nJSON: {\"entities\":[{\"text\":\"Sushi\",\"token\":\"PERSON:01\"}]}\n\nBeispiel B\nText: Ich war mit Anna in Hamburg.\nJSON: {\"entities\":[{\"text\":\"Anna\",\"token\":\"PERSON:01\"},{\"text\":\"Hamburg\",\"token\":\"PLACE:CITY\"}]}\n\nBeispiel C\nText: Der Himmel war klar, die Luft warm, ich kochte Tee.\nJSON: {\"entities\":[]}\n\nAntworte nur mit JSON: {\"entities\":[{\"text\":\"Name\",\"token\":\"PERSON:01\"}]}\nToken: PERSON:01, PERSON:02, PLACE:CITY, ORG:01. Nie PERSON:KURZ. KURZ ist kein Token. Keine sprechenden Aliase.\n\nText:\n{{source_text}}\n"
|
||||
},
|
||||
{
|
||||
"id": "mvp-profile-review",
|
||||
"slug": "mvp.profile_review",
|
||||
"name": "MVP Writing-Profile-Review",
|
||||
"description": "Gebündelte semantische Review. Kein neues Profil, nur Änderungsvorschläge. Explizit ausgelöst, nicht nach jedem Dialog.",
|
||||
"category": "mvp",
|
||||
"prompt_type": "base",
|
||||
"required_feature": "ai_calls",
|
||||
"template": "Du prüfst das Writing Profile von [[SELF]] gegen Evidenz.\n\nKeine Diagnose, kein Persönlichkeitsmodell, keine erfundenen Stilmerkmale. Lokale Heuristiken sind Messhilfe, keine Traits. Keys wie chronology, humor, detail, rhythm sind nur Ordnungshilfe, kein geschlossenes Raster.\n\nHülle: Core, optionale context/output-Facets, darin dynamische semantische Traits, Evidence References, Representative Exemplars.\nExisting-before-New: 1) bestehenden Trait bestätigen, 2) präzisieren, 3) Scope ändern, 4) zusammenführen oder aufteilen, 5) erst dann einen neuen Trait anlegen.\nUrlaubstagebücher und autobiografische Journale belegen primär autobiographical_journal. Einen globalen Core nur so weit verallgemeinern, wie unterschiedliche Quellenarten das tragen. Neuere Texte wiegen stärker für die aktuelle Ausprägung; ältere Texte belegen Langzeitmerkmale.\nJeder Trait nennt evidence_ids und darf Originalzitate als Exemplare führen. Locked-Traits nicht ändern.\n\nAntworte ausschließlich mit einem JSON-Objekt gemäß expected_result im Paket. Actions: confirm, precisify, rescope, merge, split, create. Kein Fließtext davor oder danach.\n\nPaket:\n{{review_package}}\n"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"role": "generate",
|
||||
"name": "openrouter",
|
||||
"url": "https://openrouter.ai/api/v1/chat/completions",
|
||||
"model": "openai/gpt-4o-mini",
|
||||
"zdr": true,
|
||||
"no_train": true
|
||||
},
|
||||
{
|
||||
"role": "detect",
|
||||
"name": "openrouter",
|
||||
"url": "https://openrouter.ai/api/v1/chat/completions",
|
||||
"model": "openai/gpt-4.1-nano",
|
||||
"zdr": true,
|
||||
"no_train": true
|
||||
}
|
||||
]
|
||||
+222
-4
@@ -1,14 +1,232 @@
|
||||
"""Internal context for later Gateway use. Never sends originals to a provider."""
|
||||
"""Internal context for later Gateway use. Never sends originals to a provider.
|
||||
|
||||
Selection goes through retrieval specs. Recency/SQL limits live in retrieval.py.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from data_layer import read
|
||||
from dialogue_store import get_conversation
|
||||
from retrieval import retrieve
|
||||
from journal_body import plain_text
|
||||
|
||||
PURPOSES = {"dialogue_turn", "journal_generate"}
|
||||
CLOSURE = re.compile(
|
||||
r"\bauf den heimweg\b|"
|
||||
r"\bheimweg\b|"
|
||||
r"\bauf den rückweg\b|"
|
||||
r"\bdamit (?:war|ist) der tag\b|"
|
||||
r"\bdas war (?:der|so ein|unser) tag\b|"
|
||||
r"\bende des tages\b|"
|
||||
r"\bgenug für heute\b|"
|
||||
r"\bso endete\b|"
|
||||
r"müde.{0,80}(?:heimweg|rückweg)|"
|
||||
r"(?:eindr[uü]cke|erschöpft).{0,80}(?:heimweg|rückweg)|"
|
||||
r"machten uns.{0,40}auf den (?:heimweg|rückweg)|"
|
||||
r"\bbald ins bett\b|"
|
||||
r"\blegten uns.{0,40}ins bett\b|"
|
||||
r"\bgleich ins bett\b",
|
||||
re.I,
|
||||
)
|
||||
|
||||
|
||||
def build_internal_context(profile_id: str, conversation_id: str) -> dict:
|
||||
messages = read("conversation_messages", profile_id=profile_id, context={"conversation_id": conversation_id})
|
||||
def is_closing_turn(text: str) -> bool:
|
||||
return bool(CLOSURE.search((text or "").strip()))
|
||||
|
||||
|
||||
def infer_register(user_bodies: list[str]) -> str:
|
||||
last = (user_bodies[-1] if user_bodies else "").strip()
|
||||
if not last:
|
||||
return "Noch kein Erzählstand. Warte auf den ersten Satz, setze keine Szene."
|
||||
if is_closing_turn(last):
|
||||
return (
|
||||
"Die Szene oder der Abschnitt ist zu Ende. Halte den Schluss. "
|
||||
"Keine Frage, keinen Bogen, keine neue Station."
|
||||
)
|
||||
words = last.split()
|
||||
emotion = re.search(r"aufgeregt|traurig|berührt|ängst|wütend|freude|ruhig|unsicher", last, re.I)
|
||||
plan = re.search(r"geplant|wollen|werde|sollten|vorhaben", last, re.I)
|
||||
dense = len(words) >= 40 or last.count(".") + last.count("!") >= 3
|
||||
if dense:
|
||||
return (
|
||||
"[[SELF]] erzählt gerade ausführlich. Am letzten erzählten Punkt bleiben. "
|
||||
"Nicht nacherzählen. Nacherzählen nur bei Widerspruch oder Logikbruch. "
|
||||
"Keine erfundene Empfindung, keine erfundene nächste Szene."
|
||||
)
|
||||
if emotion:
|
||||
return (
|
||||
"[[SELF]] hat Erleben geöffnet. Bei Bedeutung oder Wirkung bleiben, "
|
||||
"die schon benannte Empfindung nicht nachplappern."
|
||||
)
|
||||
if plan:
|
||||
return "Ein Vorhaben ist genannt. Als Plan behandeln, nicht als vollzogen."
|
||||
return "Kurz und natürlich antworten, am offenen Punkt, wie jemand der zugehört hat."
|
||||
|
||||
|
||||
def build_internal_context(
|
||||
profile_id: str,
|
||||
conversation_id: str | None = None,
|
||||
space_id: str | None = None,
|
||||
journal_day_id: str | None = None,
|
||||
purpose: str = "dialogue_turn",
|
||||
include_existing: bool = False,
|
||||
conversation_ids: list[str] | None = None,
|
||||
existing_text: str = "",
|
||||
) -> dict:
|
||||
purpose = purpose if purpose in PURPOSES else "dialogue_turn"
|
||||
if conversation_id and not space_id:
|
||||
conv = get_conversation(profile_id, conversation_id)
|
||||
space_id = conv.get("space_id")
|
||||
journal_day_id = journal_day_id or conv.get("journal_day_id")
|
||||
|
||||
items: list[dict] = []
|
||||
space_title = ""
|
||||
if space_id:
|
||||
from dialogue_store import get_space
|
||||
|
||||
try:
|
||||
space_title = get_space(profile_id, space_id).get("title") or ""
|
||||
except Exception:
|
||||
space_title = ""
|
||||
items.append({"type": "space", "space_id": space_id, "title": space_title})
|
||||
|
||||
selected_ids = list(conversation_ids or [])
|
||||
if conversation_id and conversation_id not in selected_ids and purpose == "dialogue_turn":
|
||||
selected_ids.append(conversation_id)
|
||||
|
||||
day_messages = []
|
||||
if journal_day_id:
|
||||
day_messages = retrieve(
|
||||
profile_id,
|
||||
{
|
||||
"kind": "day_messages",
|
||||
"journal_day_id": journal_day_id,
|
||||
"conversation_ids": selected_ids or None,
|
||||
},
|
||||
)
|
||||
items.append({"type": "day_messages", "messages": day_messages})
|
||||
elif conversation_id:
|
||||
day_messages = read("conversation_messages", profile_id=profile_id, context={"conversation_id": conversation_id})
|
||||
items.append({"type": "day_messages", "messages": day_messages})
|
||||
|
||||
if purpose == "dialogue_turn" and space_id:
|
||||
prior = retrieve(
|
||||
profile_id,
|
||||
{
|
||||
"kind": "space_entries",
|
||||
"space_id": space_id,
|
||||
"exclude_day_id": journal_day_id,
|
||||
},
|
||||
)
|
||||
items.append(
|
||||
{
|
||||
"type": "prior_entries",
|
||||
"entries": [
|
||||
{
|
||||
"entry_id": item["entry_id"],
|
||||
"calendar_date": item.get("calendar_date"),
|
||||
"excerpt": item.get("excerpt") or plain_text(item.get("body") or ""),
|
||||
}
|
||||
for item in prior
|
||||
],
|
||||
}
|
||||
)
|
||||
recent_sources = retrieve(
|
||||
profile_id,
|
||||
{
|
||||
"kind": "space_recent_sources",
|
||||
"space_id": space_id,
|
||||
"exclude_conversation_id": conversation_id,
|
||||
"exclude_journal_day_id": journal_day_id,
|
||||
},
|
||||
)
|
||||
items.append({"type": "space_recent_sources", "sources": recent_sources})
|
||||
|
||||
if purpose == "journal_generate":
|
||||
profile_rows = retrieve(profile_id, {"kind": "writing_profile"})
|
||||
brief = (profile_rows[0].get("compiled_brief") if profile_rows else "") or ""
|
||||
items.append({"type": "writing_profile", "compiled_brief": brief})
|
||||
sources = (profile_rows[0].get("sources") if profile_rows else []) or []
|
||||
style_sources = [
|
||||
item
|
||||
for item in sources
|
||||
if item.get("kind") in {"journal_entry", "imported_text", "dialogue_style"}
|
||||
]
|
||||
items.append({"type": "style_sources", "sources": style_sources})
|
||||
if include_existing and existing_text:
|
||||
items.append({"type": "existing_text", "body": plain_text(existing_text)})
|
||||
elif purpose == "dialogue_turn":
|
||||
items.append({"type": "interaction_hint", "text": ""})
|
||||
|
||||
return {
|
||||
"profile_id": profile_id,
|
||||
"conversation_id": conversation_id,
|
||||
"messages": messages,
|
||||
"space_id": space_id,
|
||||
"space_title": space_title,
|
||||
"journal_day_id": journal_day_id,
|
||||
"purpose": purpose,
|
||||
"items": items,
|
||||
"messages": day_messages,
|
||||
"egress": False,
|
||||
"note": "Volltext bleibt lokal. Minimierung und Egress nur über das Privacy Gateway.",
|
||||
}
|
||||
|
||||
|
||||
def assemble_text(context: dict) -> dict[str, str]:
|
||||
dialogue_parts: list[str] = []
|
||||
prior_parts: list[str] = []
|
||||
source_parts: list[str] = []
|
||||
writing_profile = ""
|
||||
interaction_hint = ""
|
||||
existing_text = ""
|
||||
user_bodies: list[str] = []
|
||||
purpose = context.get("purpose") or ""
|
||||
for item in context.get("items") or []:
|
||||
kind = item.get("type")
|
||||
if kind == "day_messages":
|
||||
for message in item.get("messages") or []:
|
||||
role = message.get("role") or "user"
|
||||
body = message.get("body") or ""
|
||||
dialogue_parts.append(f"{role}: {body}")
|
||||
if role == "user":
|
||||
user_bodies.append(body)
|
||||
elif kind == "prior_entries":
|
||||
for entry in item.get("entries") or []:
|
||||
date = entry.get("calendar_date") or ""
|
||||
excerpt = entry.get("excerpt") or ""
|
||||
if excerpt:
|
||||
prior_parts.append(f"{date}: {excerpt}")
|
||||
elif kind == "space_recent_sources":
|
||||
for source in item.get("sources") or []:
|
||||
excerpt = source.get("excerpt") or ""
|
||||
if excerpt:
|
||||
source_parts.append(excerpt)
|
||||
elif kind == "writing_profile":
|
||||
writing_profile = item.get("compiled_brief") or ""
|
||||
elif kind == "interaction_hint":
|
||||
interaction_hint = item.get("text") or ""
|
||||
elif kind == "existing_text":
|
||||
existing_text = item.get("body") or ""
|
||||
if purpose == "dialogue_turn":
|
||||
from conversation_signals import infer_signals
|
||||
from interaction_profile_store import assemble_interaction_hint
|
||||
|
||||
interaction_hint = assemble_interaction_hint(
|
||||
context.get("profile_id"),
|
||||
infer_signals(user_bodies),
|
||||
)
|
||||
if prior_parts:
|
||||
dialogue_parts.append("Frühere Einträge im Space (nur Hinweise, keine Gewissheit):")
|
||||
dialogue_parts.extend(prior_parts)
|
||||
if source_parts and purpose == "dialogue_turn":
|
||||
dialogue_parts.append("Frühere Originalgespräche im Space (Ausschnitt, keine Gewissheit):")
|
||||
dialogue_parts.extend(source_parts)
|
||||
return {
|
||||
"dialogue_context": "\n".join(dialogue_parts).strip(),
|
||||
"writing_profile": writing_profile,
|
||||
"interaction_hint": interaction_hint,
|
||||
"existing_text": existing_text,
|
||||
"space_title": context.get("space_title") or "",
|
||||
"register_hint": infer_register(user_bodies),
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
"""Local operational signals for a conversation. No extra LLM call, not a Writing Profile."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
EMOTION = re.compile(
|
||||
r"aufgeregt|traurig|berührt|ängst|wütend|freude|ruhig|unsicher|gefühl|tiefer",
|
||||
re.I,
|
||||
)
|
||||
PLAN = re.compile(r"geplant|wollen|werde|sollten|vorhaben", re.I)
|
||||
CHRONICLE = re.compile(
|
||||
r"\b(heute|gestern|morgens|mittags|abends|danach|vorher|später|zuerst|einkauf|"
|
||||
r"spaziergang|markt|chronik)\b",
|
||||
re.I,
|
||||
)
|
||||
CLOCK = re.compile(r"\b\d{1,2}:\d{2}\b|\b\d{1,2}\s*Uhr\b", re.I)
|
||||
STOP = {
|
||||
"dann", "noch", "schon", "wieder", "gegen", "waren", "wurde", "haben", "hatte",
|
||||
"unter", "über", "nach", "beim", "eine", "einem", "einer", "dieser", "dieses",
|
||||
"auch", "aber", "dass", "wenn", "sich", "uns", "euch", "mein", "dein", "sein",
|
||||
"heute", "dann", "nach", "noch",
|
||||
}
|
||||
|
||||
DEEP_OPS = {"erleben_vertiefen", "bedeutung"}
|
||||
PLAN_OPS = {"plan_aufgreifen"}
|
||||
|
||||
|
||||
def _words(text: str) -> list[str]:
|
||||
return [
|
||||
word
|
||||
for word in re.findall(r"[a-zäöüß]{4,}", (text or "").lower())
|
||||
if word not in STOP
|
||||
]
|
||||
|
||||
|
||||
def infer_signals(user_bodies: list[str], last_operation: str | None = None) -> dict:
|
||||
"""Derive operational dialogue state. Not a Writing Profile or Interaction Preference."""
|
||||
bodies = [item.strip() for item in user_bodies if (item or "").strip()]
|
||||
blob = " ".join(bodies)
|
||||
last = bodies[-1] if bodies else ""
|
||||
op = (last_operation or "").strip().lower()
|
||||
emotion = bool(EMOTION.search(blob))
|
||||
plan = bool(PLAN.search(blob))
|
||||
chronicle = bool(CHRONICLE.search(blob) or CLOCK.search(blob))
|
||||
if emotion:
|
||||
mode, depth = "reflective", "deep"
|
||||
elif plan:
|
||||
mode, depth = "plan", "surface"
|
||||
elif chronicle:
|
||||
mode, depth = "chronicle", "surface"
|
||||
elif op in DEEP_OPS:
|
||||
mode, depth = "reflective", "deep"
|
||||
elif op in PLAN_OPS:
|
||||
mode, depth = "plan", "surface"
|
||||
else:
|
||||
mode, depth = "open", "surface"
|
||||
counts: dict[str, int] = {}
|
||||
for word in _words(blob):
|
||||
counts[word] = counts.get(word, 0) + 1
|
||||
focus = " ".join(
|
||||
item[0] for item in sorted(counts.items(), key=lambda pair: (-pair[1], pair[0]))[:8]
|
||||
)
|
||||
last_words = last.split()
|
||||
long_story = len(last_words) >= 40 or last.count(".") + last.count("!") >= 3
|
||||
if emotion and (long_story or depth == "deep"):
|
||||
intensity = "high"
|
||||
elif emotion:
|
||||
intensity = "medium"
|
||||
else:
|
||||
intensity = "low"
|
||||
return {
|
||||
"narrative_mode": mode,
|
||||
"reflection_depth": depth,
|
||||
"current_focus": focus,
|
||||
"emotional_intensity": intensity,
|
||||
"long_story": 1 if long_story else 0,
|
||||
}
|
||||
|
||||
|
||||
def _focus_words(row: dict) -> set[str]:
|
||||
return {item for item in str(row.get("current_focus") or "").split() if item}
|
||||
|
||||
|
||||
def compatible(left: dict, right: dict) -> bool:
|
||||
left_mode = (left.get("narrative_mode") or "open").strip() or "open"
|
||||
right_mode = (right.get("narrative_mode") or "open").strip() or "open"
|
||||
if left_mode == "open" or right_mode == "open":
|
||||
return False
|
||||
if left_mode != right_mode:
|
||||
return False
|
||||
left_depth = (left.get("reflection_depth") or "surface").strip() or "surface"
|
||||
right_depth = (right.get("reflection_depth") or "surface").strip() or "surface"
|
||||
if left_depth != right_depth:
|
||||
return False
|
||||
left_focus = _focus_words(left)
|
||||
right_focus = _focus_words(right)
|
||||
if len(left_focus) >= 2 and len(right_focus) >= 2 and not (left_focus & right_focus):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def similar_enough(conversations: list[dict]) -> bool:
|
||||
usable = [item for item in conversations if item]
|
||||
if len(usable) < 2:
|
||||
return False
|
||||
for index, left in enumerate(usable):
|
||||
for right in usable[index + 1 :]:
|
||||
if not compatible(left, right):
|
||||
return False
|
||||
return True
|
||||
+308
-8
@@ -30,6 +30,38 @@ _PROMPT_COLUMNS = {
|
||||
"sort_order": "INTEGER NOT NULL DEFAULT 0",
|
||||
"updated": "TEXT NOT NULL DEFAULT (datetime('now'))",
|
||||
}
|
||||
_CONVERSATION_COLUMNS = {
|
||||
"space_id": "TEXT",
|
||||
"journal_day_id": "TEXT",
|
||||
"narrative_mode": "TEXT NOT NULL DEFAULT ''",
|
||||
"reflection_depth": "TEXT NOT NULL DEFAULT ''",
|
||||
"current_focus": "TEXT NOT NULL DEFAULT ''",
|
||||
"emotional_intensity": "TEXT NOT NULL DEFAULT ''",
|
||||
"long_story": "INTEGER NOT NULL DEFAULT 0",
|
||||
}
|
||||
_WRITING_PROFILE_COLUMNS = {
|
||||
"governance": "TEXT NOT NULL DEFAULT 'learning'",
|
||||
"version": "INTEGER NOT NULL DEFAULT 0",
|
||||
"review_ready": "INTEGER NOT NULL DEFAULT 0",
|
||||
"last_reviewed": "TEXT",
|
||||
"lifecycle": "TEXT NOT NULL DEFAULT 'uninitialized'",
|
||||
}
|
||||
_WRITING_SOURCE_COLUMNS = {
|
||||
"occurred_at": "TEXT",
|
||||
"context_hint": "TEXT NOT NULL DEFAULT ''",
|
||||
}
|
||||
_WRITING_SUGGESTION_COLUMNS = {
|
||||
"trait_slug": "TEXT NOT NULL DEFAULT ''",
|
||||
"action": "TEXT NOT NULL DEFAULT 'update'",
|
||||
"payload_json": "TEXT NOT NULL DEFAULT '{}'",
|
||||
}
|
||||
_WRITING_VERSION_COLUMNS = {
|
||||
"lifecycle": "TEXT NOT NULL DEFAULT ''",
|
||||
"traits_json": "TEXT NOT NULL DEFAULT '[]'",
|
||||
}
|
||||
_JOURNAL_DAY_COLUMNS = {
|
||||
"scratch_json": "TEXT NOT NULL DEFAULT '[]'",
|
||||
}
|
||||
|
||||
|
||||
def _connect() -> sqlite3.Connection:
|
||||
@@ -119,35 +151,303 @@ def _seed_prompts(conn: sqlite3.Connection) -> None:
|
||||
"""Prompts come from JSON/DB, never from Python string literals."""
|
||||
items = json.loads(PROMPTS_SEED_PATH.read_text(encoding="utf-8"))
|
||||
for item in items:
|
||||
template = item.get("template") or ""
|
||||
existing = row_to_dict(
|
||||
conn.execute("SELECT * FROM ai_prompts WHERE slug = ?", (item["slug"],)).fetchone()
|
||||
)
|
||||
if existing is None:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO ai_prompts
|
||||
(id, slug, name, description, category, prompt_type, template,
|
||||
required_feature, is_system_default, default_template)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)
|
||||
""",
|
||||
(
|
||||
item["id"],
|
||||
item["slug"],
|
||||
item["name"],
|
||||
item.get("description") or "",
|
||||
item.get("category") or "uncategorized",
|
||||
item.get("prompt_type") or "base",
|
||||
template,
|
||||
item.get("required_feature") or "ai_calls",
|
||||
template,
|
||||
),
|
||||
)
|
||||
continue
|
||||
untouched = (existing.get("template") or "") == (existing.get("default_template") or "")
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR IGNORE INTO ai_prompts
|
||||
(id, slug, name, description, category, prompt_type, template,
|
||||
required_feature, is_system_default, default_template)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)
|
||||
UPDATE ai_prompts
|
||||
SET name = ?, description = ?, category = ?, prompt_type = ?,
|
||||
required_feature = ?, is_system_default = 1, default_template = ?,
|
||||
template = CASE WHEN ? THEN ? ELSE template END,
|
||||
updated = datetime('now')
|
||||
WHERE slug = ?
|
||||
""",
|
||||
(
|
||||
item["id"],
|
||||
item["slug"],
|
||||
item["name"],
|
||||
item.get("description") or "",
|
||||
item.get("category") or "uncategorized",
|
||||
item.get("prompt_type") or "base",
|
||||
item.get("template") or "",
|
||||
item.get("required_feature") or "ai_calls",
|
||||
item.get("template") or "",
|
||||
template,
|
||||
1 if untouched else 0,
|
||||
template,
|
||||
item["slug"],
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _parse_legacy_ids(raw: str | None) -> list[str]:
|
||||
if not raw:
|
||||
return []
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
return []
|
||||
if not isinstance(data, list):
|
||||
return []
|
||||
return [str(item) for item in data if item]
|
||||
|
||||
|
||||
def _insert_source_refs(
|
||||
conn: sqlite3.Connection,
|
||||
table: str,
|
||||
owner_col: str,
|
||||
owner_id: str,
|
||||
profile_id: str,
|
||||
conversation_ids: list[str],
|
||||
message_ids: list[str],
|
||||
) -> None:
|
||||
conn.execute(f"DELETE FROM {table} WHERE {owner_col} = ?", (owner_id,))
|
||||
for index, source_id in enumerate(conversation_ids):
|
||||
conn.execute(
|
||||
f"""
|
||||
INSERT OR IGNORE INTO {table}
|
||||
({owner_col}, profile_id, source_kind, source_id, sort_order)
|
||||
VALUES (?, ?, 'conversation', ?, ?)
|
||||
""",
|
||||
(owner_id, profile_id, source_id, index),
|
||||
)
|
||||
for index, source_id in enumerate(message_ids):
|
||||
conn.execute(
|
||||
f"""
|
||||
INSERT OR IGNORE INTO {table}
|
||||
({owner_col}, profile_id, source_kind, source_id, sort_order)
|
||||
VALUES (?, ?, 'message', ?, ?)
|
||||
""",
|
||||
(owner_id, profile_id, source_id, index),
|
||||
)
|
||||
|
||||
|
||||
def migrate_journal_source_refs(conn: sqlite3.Connection) -> None:
|
||||
"""Copy JSON id lists into relational source-ref tables without touching Source."""
|
||||
existing = {
|
||||
row["name"]
|
||||
for row in conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table'"
|
||||
).fetchall()
|
||||
}
|
||||
if "journal_draft_source_refs" not in existing or "journal_entry_version_source_refs" not in existing:
|
||||
return
|
||||
for row in conn.execute(
|
||||
"SELECT id, profile_id, source_conversation_ids, source_message_ids FROM journal_drafts"
|
||||
).fetchall():
|
||||
already = conn.execute(
|
||||
"SELECT 1 FROM journal_draft_source_refs WHERE draft_id = ? LIMIT 1",
|
||||
(row["id"],),
|
||||
).fetchone()
|
||||
if already:
|
||||
continue
|
||||
_insert_source_refs(
|
||||
conn,
|
||||
"journal_draft_source_refs",
|
||||
"draft_id",
|
||||
row["id"],
|
||||
row["profile_id"],
|
||||
_parse_legacy_ids(row["source_conversation_ids"]),
|
||||
_parse_legacy_ids(row["source_message_ids"]),
|
||||
)
|
||||
for row in conn.execute(
|
||||
"SELECT id, profile_id, source_conversation_ids, source_message_ids FROM journal_entry_versions"
|
||||
).fetchall():
|
||||
already = conn.execute(
|
||||
"SELECT 1 FROM journal_entry_version_source_refs WHERE version_id = ? LIMIT 1",
|
||||
(row["id"],),
|
||||
).fetchone()
|
||||
if already:
|
||||
continue
|
||||
_insert_source_refs(
|
||||
conn,
|
||||
"journal_entry_version_source_refs",
|
||||
"version_id",
|
||||
row["id"],
|
||||
row["profile_id"],
|
||||
_parse_legacy_ids(row["source_conversation_ids"]),
|
||||
_parse_legacy_ids(row["source_message_ids"]),
|
||||
)
|
||||
|
||||
|
||||
def _migrate_writing_profile_dialogue_style(conn: sqlite3.Connection) -> None:
|
||||
"""Existing DBs keep the old CHECK; recreate so dialogue_style is a valid source kind."""
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'writing_profile_sources'"
|
||||
).fetchone()
|
||||
)
|
||||
current = (row or {}).get("sql") or ""
|
||||
if "dialogue_style" in current:
|
||||
return
|
||||
conn.executescript(
|
||||
"""
|
||||
CREATE TABLE writing_profile_sources_new (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('imported_text', 'journal_entry', 'dialogue_style')),
|
||||
body TEXT NOT NULL DEFAULT '',
|
||||
entry_id TEXT REFERENCES journal_entries(id),
|
||||
weight REAL NOT NULL DEFAULT 1,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
INSERT INTO writing_profile_sources_new
|
||||
(id, profile_id, kind, body, entry_id, weight, created)
|
||||
SELECT id, profile_id, kind, body, entry_id, weight, created
|
||||
FROM writing_profile_sources;
|
||||
DROP TABLE writing_profile_sources;
|
||||
ALTER TABLE writing_profile_sources_new RENAME TO writing_profile_sources;
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def _migrate_writing_profile_shell(conn: sqlite3.Connection) -> None:
|
||||
"""Layers stay; inferred count-facets are not the profile. Manual style keys become traits."""
|
||||
from writing_profile_schema import LEGACY_STYLE_KEYS, coerce_slug, facet_layer, normalize_facet_key
|
||||
|
||||
facet_sql = row_to_dict(
|
||||
conn.execute(
|
||||
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'writing_profile_facets'"
|
||||
).fetchone()
|
||||
)
|
||||
current = (facet_sql or {}).get("sql") or ""
|
||||
if current and ("inferred" in current or "layer" not in current):
|
||||
conn.executescript(
|
||||
"""
|
||||
CREATE TABLE writing_profile_facets_new (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
facet_key TEXT NOT NULL,
|
||||
layer TEXT NOT NULL DEFAULT 'context',
|
||||
value TEXT NOT NULL DEFAULT '',
|
||||
evidence TEXT NOT NULL DEFAULT '',
|
||||
origin TEXT NOT NULL DEFAULT 'manual',
|
||||
locked INTEGER NOT NULL DEFAULT 0,
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (profile_id, facet_key)
|
||||
);
|
||||
INSERT INTO writing_profile_facets_new
|
||||
(id, profile_id, facet_key, layer, value, evidence, origin, locked, updated)
|
||||
SELECT id, profile_id, facet_key, 'context', value, evidence,
|
||||
CASE WHEN origin IN ('manual', 'imported', 'accepted_suggestion', 'initial_build')
|
||||
THEN origin ELSE 'manual' END,
|
||||
locked, updated
|
||||
FROM writing_profile_facets
|
||||
WHERE origin IS NOT NULL AND origin != 'inferred';
|
||||
DROP TABLE writing_profile_facets;
|
||||
ALTER TABLE writing_profile_facets_new RENAME TO writing_profile_facets;
|
||||
"""
|
||||
)
|
||||
names = _column_names(conn, "writing_profile_facets")
|
||||
if "layer" not in names:
|
||||
conn.execute("ALTER TABLE writing_profile_facets ADD COLUMN layer TEXT NOT NULL DEFAULT 'context'")
|
||||
rows = [
|
||||
dict(item)
|
||||
for item in conn.execute("SELECT * FROM writing_profile_facets").fetchall()
|
||||
]
|
||||
for item in rows:
|
||||
key = normalize_facet_key(item.get("facet_key") or "")
|
||||
origin = item.get("origin") or "manual"
|
||||
if (item.get("facet_key") or "") in LEGACY_STYLE_KEYS:
|
||||
slug = coerce_slug(item.get("facet_key") or "trait")
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR IGNORE INTO writing_profile_traits
|
||||
(id, profile_id, facet_key, slug, label, statement, origin, locked, status)
|
||||
VALUES (?, ?, 'core', ?, ?, ?, ?, ?, 'active')
|
||||
""",
|
||||
(
|
||||
item["id"],
|
||||
item["profile_id"],
|
||||
slug,
|
||||
item.get("facet_key") or slug,
|
||||
item.get("value") or "",
|
||||
origin if origin in {"manual", "imported", "accepted_suggestion", "initial_build"} else "manual",
|
||||
int(item.get("locked") or 0),
|
||||
),
|
||||
)
|
||||
conn.execute("DELETE FROM writing_profile_facets WHERE id = ?", (item["id"],))
|
||||
continue
|
||||
if key != (item.get("facet_key") or ""):
|
||||
conn.execute(
|
||||
"UPDATE writing_profile_facets SET facet_key = ? WHERE id = ?",
|
||||
(key, item["id"]),
|
||||
)
|
||||
layer = facet_layer(key)
|
||||
conn.execute("UPDATE writing_profile_facets SET layer = ? WHERE id = ?", (layer, item["id"]))
|
||||
for row in conn.execute("SELECT profile_id FROM writing_profiles").fetchall():
|
||||
profile_id = row["profile_id"]
|
||||
remaining = conn.execute(
|
||||
"SELECT 1 FROM writing_profile_facets WHERE profile_id = ? LIMIT 1",
|
||||
(profile_id,),
|
||||
).fetchone()
|
||||
traits = conn.execute(
|
||||
"SELECT 1 FROM writing_profile_traits WHERE profile_id = ? AND status = 'active' LIMIT 1",
|
||||
(profile_id,),
|
||||
).fetchone()
|
||||
if remaining or traits:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE writing_profiles
|
||||
SET lifecycle = 'confirmed'
|
||||
WHERE profile_id = ? AND lifecycle = 'uninitialized'
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
schema = SCHEMA_PATH.read_text(encoding="utf-8")
|
||||
with get_db() as conn:
|
||||
conn.executescript(schema)
|
||||
_seed_platform(conn)
|
||||
_seed_prompts(conn)
|
||||
from provider_settings import seed_provider_settings
|
||||
|
||||
seed_provider_settings(conn)
|
||||
_ensure_columns(conn, "profiles", _PROFILE_COLUMNS)
|
||||
_ensure_columns(conn, "ai_prompts", _PROMPT_COLUMNS)
|
||||
_ensure_columns(conn, "conversations", _CONVERSATION_COLUMNS)
|
||||
_ensure_columns(conn, "journal_days", _JOURNAL_DAY_COLUMNS)
|
||||
_ensure_columns(conn, "writing_profiles", _WRITING_PROFILE_COLUMNS)
|
||||
_migrate_writing_profile_dialogue_style(conn)
|
||||
_ensure_columns(conn, "writing_profile_sources", _WRITING_SOURCE_COLUMNS)
|
||||
_ensure_columns(conn, "writing_profile_suggestions", _WRITING_SUGGESTION_COLUMNS)
|
||||
_ensure_columns(conn, "writing_profile_versions", _WRITING_VERSION_COLUMNS)
|
||||
migrate_journal_source_refs(conn)
|
||||
_migrate_writing_profile_shell(conn)
|
||||
_mark(conn, "001_frame")
|
||||
_mark(conn, "002_platform")
|
||||
_mark(conn, "003_dialogue_memory")
|
||||
_mark(conn, "004_mvp_journal")
|
||||
_mark(conn, "005_provider_settings")
|
||||
_mark(conn, "006_writing_profile_dialogue_style")
|
||||
_mark(conn, "007_journal_day_scratch")
|
||||
_mark(conn, "008_journal_source_refs")
|
||||
_mark(conn, "009_conversation_signals")
|
||||
_mark(conn, "010_profile_governance")
|
||||
_mark(conn, "011_profile_review")
|
||||
_mark(conn, "012_profile_shell")
|
||||
from writing_profile_store import bootstrap_from_existing
|
||||
|
||||
bootstrap_from_existing()
|
||||
+127
-4
@@ -81,19 +81,36 @@ def thread_snapshot(profile_id: str) -> list[dict]:
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
|
||||
def create_conversation(profile_id: str, usage_session_id: str | None = None, title: str = "") -> dict:
|
||||
def create_conversation(
|
||||
profile_id: str,
|
||||
usage_session_id: str | None = None,
|
||||
title: str = "",
|
||||
space_id: str | None = None,
|
||||
journal_day_id: str | None = None,
|
||||
) -> dict:
|
||||
conversation_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
if usage_session_id:
|
||||
session = _owned(conn, "usage_sessions", usage_session_id, profile_id)
|
||||
if not session:
|
||||
raise StoreError("not_found", "Nutzungssitzung nicht gefunden", 404)
|
||||
if space_id and not _owned(conn, "spaces", space_id, profile_id):
|
||||
raise StoreError("not_found", "Reflection Space nicht gefunden", 404)
|
||||
if journal_day_id:
|
||||
day = _owned(conn, "journal_days", journal_day_id, profile_id)
|
||||
if not day:
|
||||
raise StoreError("not_found", "Journal Day nicht gefunden", 404)
|
||||
if space_id and day["space_id"] != space_id:
|
||||
raise StoreError("assignment_mismatch", "Journal Day gehört zu einem anderen Space")
|
||||
space_id = space_id or day["space_id"]
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO conversations (id, profile_id, usage_session_id, title)
|
||||
VALUES (?, ?, ?, ?)
|
||||
INSERT INTO conversations
|
||||
(id, profile_id, usage_session_id, title, space_id, journal_day_id,
|
||||
narrative_mode, reflection_depth, current_focus)
|
||||
VALUES (?, ?, ?, ?, ?, ?, '', '', '')
|
||||
""",
|
||||
(conversation_id, profile_id, usage_session_id, title or ""),
|
||||
(conversation_id, profile_id, usage_session_id, title or "", space_id, journal_day_id),
|
||||
)
|
||||
return row_to_dict(conn.execute("SELECT * FROM conversations WHERE id = ?", (conversation_id,)).fetchone())
|
||||
|
||||
@@ -106,6 +123,29 @@ def get_conversation(profile_id: str, conversation_id: str) -> dict:
|
||||
return conv
|
||||
|
||||
|
||||
def update_conversation_signals(profile_id: str, conversation_id: str, signals: dict) -> dict:
|
||||
get_conversation(profile_id, conversation_id)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE conversations
|
||||
SET narrative_mode = ?, reflection_depth = ?, current_focus = ?,
|
||||
emotional_intensity = ?, long_story = ?, updated = datetime('now')
|
||||
WHERE id = ? AND profile_id = ?
|
||||
""",
|
||||
(
|
||||
(signals.get("narrative_mode") or "")[:40],
|
||||
(signals.get("reflection_depth") or "")[:40],
|
||||
(signals.get("current_focus") or "")[:200],
|
||||
(signals.get("emotional_intensity") or "")[:40],
|
||||
1 if signals.get("long_story") else 0,
|
||||
conversation_id,
|
||||
profile_id,
|
||||
),
|
||||
)
|
||||
return get_conversation(profile_id, conversation_id)
|
||||
|
||||
|
||||
def list_conversations(profile_id: str) -> list[dict]:
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
@@ -223,6 +263,89 @@ def create_space(profile_id: str, title: str = "", visibility: str = "internal")
|
||||
return row_to_dict(conn.execute("SELECT * FROM spaces WHERE id = ?", (space_id,)).fetchone())
|
||||
|
||||
|
||||
def get_space(profile_id: str, space_id: str) -> dict:
|
||||
with get_db() as conn:
|
||||
space = _owned(conn, "spaces", space_id, profile_id)
|
||||
if not space:
|
||||
raise StoreError("not_found", "Reflection Space nicht gefunden", 404)
|
||||
return space
|
||||
|
||||
|
||||
def list_spaces(profile_id: str, visibility: str | None = "user") -> list[dict]:
|
||||
with get_db() as conn:
|
||||
if visibility:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT * FROM spaces
|
||||
WHERE profile_id = ? AND visibility = ?
|
||||
ORDER BY updated DESC, created DESC
|
||||
""",
|
||||
(profile_id, visibility),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM spaces WHERE profile_id = ? ORDER BY updated DESC, created DESC",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
|
||||
def rename_space(profile_id: str, space_id: str, title: str) -> dict:
|
||||
if not (title or "").strip():
|
||||
raise StoreError("empty_title", "Space-Name darf nicht leer sein")
|
||||
with get_db() as conn:
|
||||
if not _owned(conn, "spaces", space_id, profile_id):
|
||||
raise StoreError("not_found", "Reflection Space nicht gefunden", 404)
|
||||
conn.execute(
|
||||
"UPDATE spaces SET title = ?, updated = datetime('now') WHERE id = ? AND profile_id = ?",
|
||||
(title.strip(), space_id, profile_id),
|
||||
)
|
||||
return row_to_dict(
|
||||
conn.execute("SELECT * FROM spaces WHERE id = ? AND profile_id = ?", (space_id, profile_id)).fetchone()
|
||||
)
|
||||
|
||||
|
||||
def delete_conversation(profile_id: str, conversation_id: str) -> dict:
|
||||
with get_db() as conn:
|
||||
conv = _owned(conn, "conversations", conversation_id, profile_id)
|
||||
if not conv:
|
||||
raise StoreError("not_found", "Conversation nicht gefunden", 404)
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE handoffs SET source_conversation_id = NULL
|
||||
WHERE profile_id = ? AND source_conversation_id = ?
|
||||
""",
|
||||
(profile_id, conversation_id),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
DELETE FROM derived_records
|
||||
WHERE profile_id = ? AND subject_type = 'conversation' AND subject_id = ?
|
||||
""",
|
||||
(profile_id, conversation_id),
|
||||
)
|
||||
conn.execute(
|
||||
"DELETE FROM conversations WHERE id = ? AND profile_id = ?",
|
||||
(conversation_id, profile_id),
|
||||
)
|
||||
return {"id": conversation_id, "deleted": True}
|
||||
|
||||
|
||||
def list_conversations_for_day(profile_id: str, journal_day_id: str) -> list[dict]:
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT c.*,
|
||||
(SELECT COUNT(*) FROM messages m WHERE m.conversation_id = c.id) AS message_count
|
||||
FROM conversations c
|
||||
WHERE c.profile_id = ? AND c.journal_day_id = ?
|
||||
ORDER BY c.created
|
||||
""",
|
||||
(profile_id, journal_day_id),
|
||||
).fetchall()
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
|
||||
def link_thread_space(profile_id: str, thread_id: str, space_id: str, confidence: float | None = None) -> dict:
|
||||
with get_db() as conn:
|
||||
if not _owned(conn, "threads", thread_id, profile_id):
|
||||
|
||||
@@ -0,0 +1,365 @@
|
||||
"""One generative call per user turn. Source messages are persisted before and after the call."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
|
||||
from context_builder import assemble_text, build_internal_context, is_closing_turn
|
||||
from conversation_signals import infer_signals
|
||||
from dialogue_store import append_message, get_conversation, list_messages, update_conversation_signals
|
||||
from engine import EngineError, execute_prompt, load_active_prompt
|
||||
from writing_profile_store import remember_dialogue_style
|
||||
from profile_review import consider_dialogue
|
||||
|
||||
JSON_BLOCK = re.compile(r"\{.*\}", re.DOTALL)
|
||||
PLOT_CONTINUATION = re.compile(
|
||||
r"^\s*und\s+dann\b|"
|
||||
r"ihr\s+seid\s+dann|"
|
||||
r"seid\s+(?:ihr|du)\s+(?:dann\s+)?(?:zur|zum|in|auf|nach)|"
|
||||
r"\b(?:aufgebrochen|losgegangen|losgefahren)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
INTERVIEW_OPEN = re.compile(
|
||||
r"^\s*(?:was|wie|warum|weshalb|wieso|erzähl(?:st|t)?|und\s+was)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
DANN_PROBE = re.compile(r"\b(?:was|wie)\b.+\bdann\b|\bdann\b.+\b(?:gezeigt|geschehen|passiert)\b", re.IGNORECASE)
|
||||
COMPLETION_ASK = re.compile(
|
||||
r"^\s*ob\s+(?:ihr|du|wir)\b|"
|
||||
r"\blosgekommen\b|"
|
||||
r"\brechtzeitig\b.+\b(?:seid|habt|wart|gekommen)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
FINITE_VERB = re.compile(
|
||||
r"\b(?:ist|war|sind|waren|hat|hatte|wird|wurde|bleibt|liegt|steht|"
|
||||
r"kam|ging|kann|muss|soll|will)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
UNEARNED_COMPLETION = re.compile(
|
||||
r"\b(?:holtet|geholt|kauftet|gekauft|aßet|gegessen|unternahmt|"
|
||||
r"angekommen|aufgebrochen|losgegangen|losgefahren|losgekommen|weitergegangen)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
NEXT_BEAT_Q = re.compile(
|
||||
r"^\s*also\s+(?:seid ihr|habt ihr|bist du)\b|"
|
||||
r"(?:seid ihr|habt ihr|bist du)\s+dann\b|"
|
||||
r"(?:seid ihr|habt ihr|bist du).+\b(?:gesprungen|gerannt|rüber|geschafft|losgekommen)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
MACHINE_TELL = re.compile(
|
||||
r"\bals ki\b|"
|
||||
r"ich bin (?:eine? )?(?:ki|sprachmodell|assistent)\b|"
|
||||
r"danke,? dass du (?:das )?(?:teilst|erzählst)|"
|
||||
r"lass uns (?:das )?(?:gemeinsam|mal)|"
|
||||
r"\bzusammengefasst\b|"
|
||||
r"ich höre (?:da )?heraus|"
|
||||
r"^\s*interessant\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
UNEARNED_STANCE = re.compile(
|
||||
r"fühlte\s+sich|an(?:ge)?fühlt|"
|
||||
r"schmeckte|roch\b|klang\b|"
|
||||
r"anders als(?:\s+erwartet|\s+gedacht|\s+geplant)?|"
|
||||
r"sicher anders|"
|
||||
r"\bmusste noch\b|\bmuss noch\b|"
|
||||
r"für die nächste[n]?\b|"
|
||||
r"gehörte uns\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
CONTENT_STOP = {
|
||||
"dann", "noch", "schon", "wieder", "gegen",
|
||||
"waren", "wurde", "haben", "hatte", "lagen", "bereit", "durch",
|
||||
"unter", "über", "nach", "beim", "eine", "einem", "einer",
|
||||
"dieser", "dieses", "auch", "aber", "dass", "wenn", "dann",
|
||||
"sich", "uns", "euch", "mein", "dein", "sein", "ihre",
|
||||
}
|
||||
OPERATIONS = {
|
||||
"fortfuehren": "Fortführen",
|
||||
"konkretisieren": "Konkretisieren",
|
||||
"plan_aufgreifen": "Plan oder Erwartung aufgreifen",
|
||||
"abweichung": "Abweichung erkunden",
|
||||
"erleben_vertiefen": "Erleben vertiefen",
|
||||
"bedeutung": "Bedeutung erkunden",
|
||||
}
|
||||
|
||||
|
||||
def parse_turn_payload(content: str) -> tuple[str, dict]:
|
||||
text = (content or "").strip()
|
||||
blob = text
|
||||
if blob.startswith("```"):
|
||||
blob = re.sub(r"^```(?:json)?\s*|\s*```$", "", blob, flags=re.IGNORECASE | re.DOTALL)
|
||||
match = JSON_BLOCK.search(blob)
|
||||
if match:
|
||||
try:
|
||||
data = json.loads(match.group(0))
|
||||
except json.JSONDecodeError:
|
||||
data = None
|
||||
if isinstance(data, dict):
|
||||
operation = str(data.get("operation") or "").strip().lower()
|
||||
impulse = str(data.get("impulse") or "").strip()
|
||||
if impulse:
|
||||
label = OPERATIONS.get(operation)
|
||||
return impulse, {
|
||||
"operation": operation if label else "unparsed",
|
||||
"label": label or "nicht erkannt",
|
||||
"parsed": bool(label),
|
||||
}
|
||||
return text, {"operation": "unparsed", "label": "nicht erkannt", "parsed": False}
|
||||
|
||||
|
||||
def is_plot_continuation(impulse: str) -> bool:
|
||||
text = (impulse or "").strip()
|
||||
if not text:
|
||||
return False
|
||||
return bool(PLOT_CONTINUATION.search(text))
|
||||
|
||||
|
||||
def is_interview_question(impulse: str) -> bool:
|
||||
return bool(INTERVIEW_OPEN.search((impulse or "").strip()))
|
||||
|
||||
|
||||
def is_unearned_completion(impulse: str) -> bool:
|
||||
return bool(UNEARNED_COMPLETION.search(impulse or ""))
|
||||
|
||||
|
||||
def is_unearned_stance(impulse: str) -> bool:
|
||||
return bool(UNEARNED_STANCE.search(impulse or ""))
|
||||
|
||||
|
||||
def is_machine_tell(impulse: str) -> bool:
|
||||
return bool(MACHINE_TELL.search(impulse or ""))
|
||||
|
||||
|
||||
def last_user_text(assembled: dict | None) -> str:
|
||||
ctx = (assembled or {}).get("dialogue_context") or ""
|
||||
lines = [line[5:].strip() for line in ctx.splitlines() if line.startswith("user:")]
|
||||
return lines[-1] if lines else ""
|
||||
|
||||
|
||||
def earlier_user_text(assembled: dict | None) -> str:
|
||||
ctx = (assembled or {}).get("dialogue_context") or ""
|
||||
lines = [line[5:].strip() for line in ctx.splitlines() if line.startswith("user:")]
|
||||
return " ".join(lines[:-1])
|
||||
|
||||
|
||||
def user_closed_day(assembled: dict | None) -> bool:
|
||||
return is_closing_turn(last_user_text(assembled))
|
||||
|
||||
|
||||
def is_day_arc_recap(impulse: str, assembled: dict | None) -> bool:
|
||||
last = last_user_text(assembled)
|
||||
earlier = earlier_user_text(assembled)
|
||||
if not last or not earlier:
|
||||
return False
|
||||
last_words = set(content_words(last))
|
||||
earlier_only = set(content_words(earlier)) - last_words
|
||||
if len(earlier_only) < 6:
|
||||
return False
|
||||
foreign = {word for word in content_words(impulse) if word in earlier_only}
|
||||
return len(foreign) >= 3
|
||||
|
||||
|
||||
def is_reopen_after_close(impulse: str, assembled: dict | None) -> bool:
|
||||
if not user_closed_day(assembled):
|
||||
return False
|
||||
if "?" in (impulse or ""):
|
||||
return True
|
||||
return is_day_arc_recap(impulse, assembled)
|
||||
|
||||
|
||||
def content_words(text: str) -> list[str]:
|
||||
return [
|
||||
word
|
||||
for word in re.findall(r"[a-zäöüß]{4,}", (text or "").lower())
|
||||
if word not in CONTENT_STOP
|
||||
]
|
||||
|
||||
|
||||
def is_echo(impulse: str, source: str) -> bool:
|
||||
src = set(content_words(source))
|
||||
imp = content_words(impulse)
|
||||
if len(imp) < 3 or len(src) < 3:
|
||||
return False
|
||||
hits = 0
|
||||
for word in imp:
|
||||
if word in src or any(len(item) >= 5 and word.startswith(item[:5]) for item in src):
|
||||
hits += 1
|
||||
return hits / len(imp) >= 0.5
|
||||
|
||||
|
||||
def is_verbless_echo(impulse: str, source: str) -> bool:
|
||||
text = (impulse or "").strip()
|
||||
if not text or "?" in text or FINITE_VERB.search(text):
|
||||
return False
|
||||
src = set(content_words(source))
|
||||
imp = content_words(text)
|
||||
if not imp or not src:
|
||||
return False
|
||||
return all(
|
||||
word in src or any(len(item) >= 5 and word.startswith(item[:5]) for item in src)
|
||||
for word in imp
|
||||
)
|
||||
|
||||
|
||||
def is_dann_probe(impulse: str) -> bool:
|
||||
return bool(DANN_PROBE.search(impulse or ""))
|
||||
|
||||
|
||||
def is_completion_ask(impulse: str) -> bool:
|
||||
return bool(COMPLETION_ASK.search(impulse or ""))
|
||||
|
||||
|
||||
def last_user_is_narrative(assembled: dict | None) -> bool:
|
||||
return len(last_user_text(assembled).split()) >= 15
|
||||
|
||||
|
||||
def is_next_beat_question(impulse: str) -> bool:
|
||||
return bool(NEXT_BEAT_Q.search(impulse or ""))
|
||||
|
||||
|
||||
def is_recap_then_ask(impulse: str, assembled: dict | None = None) -> bool:
|
||||
text = (impulse or "").strip()
|
||||
if "?" not in text or not last_user_is_narrative(assembled):
|
||||
return False
|
||||
statement = text.split("?", 1)[0].strip()
|
||||
if len(content_words(statement)) < 6:
|
||||
return False
|
||||
return is_echo(statement, last_user_text(assembled))
|
||||
|
||||
|
||||
def is_pure_recap(impulse: str, assembled: dict | None = None) -> bool:
|
||||
text = (impulse or "").strip()
|
||||
if not text or "?" in text or not last_user_is_narrative(assembled):
|
||||
return False
|
||||
last = last_user_text(assembled)
|
||||
return is_echo(text, last) or is_verbless_echo(text, last)
|
||||
|
||||
|
||||
def local_hold(last_user: str) -> str:
|
||||
if is_closing_turn(last_user):
|
||||
return "Dann ist das der Schluss."
|
||||
if len((last_user or "").split()) >= 15:
|
||||
return "Erzähl bitte weiter."
|
||||
return "Ich bin gespannt, wie es weitergeht."
|
||||
|
||||
|
||||
def repair_note(impulse: str, assembled: dict | None) -> str:
|
||||
if is_reopen_after_close(impulse, assembled):
|
||||
return (
|
||||
"Korrektur: Der Tag oder die Szene ist geschlossen. "
|
||||
"Halte den Schluss. Keine Frage, keinen Tagesbogen."
|
||||
)
|
||||
if is_unearned_stance(impulse):
|
||||
return (
|
||||
"Korrektur: Keine erfundene Empfindung und keinen Vergleich mit ungenannten anderen Tagen. "
|
||||
"Bleib bei dem, was [[SELF]] selbst gesagt hat."
|
||||
)
|
||||
if is_machine_tell(impulse):
|
||||
return (
|
||||
"Die gesprochene Zeile klingt wie ein Mensch im Gespräch, "
|
||||
"nicht wie ein System oder eine Zusammenfassung."
|
||||
)
|
||||
if is_next_beat_question(impulse) or is_recap_then_ask(impulse, assembled) or is_pure_recap(impulse, assembled):
|
||||
return (
|
||||
"Korrektur: Nicht nacherzählen. Ein kurzer Impuls oder Denkanstoß am letzten Faden. "
|
||||
"Nacherzählen nur, um einen Widerspruch oder Logikbruch zu klären."
|
||||
)
|
||||
return (
|
||||
"Korrektur: Der vorige Impuls hat den nächsten Vollzug gesetzt "
|
||||
"oder danach gefragt. Bleib Vertrauter. Kein Also-seid-ihr-dann. "
|
||||
"Erzähle den Tag nicht weiter."
|
||||
)
|
||||
|
||||
|
||||
def needs_repair(impulse: str, assembled: dict | None = None) -> bool:
|
||||
return (
|
||||
is_plot_continuation(impulse)
|
||||
or is_unearned_completion(impulse)
|
||||
or is_unearned_stance(impulse)
|
||||
or is_next_beat_question(impulse)
|
||||
or is_recap_then_ask(impulse, assembled)
|
||||
or is_pure_recap(impulse, assembled)
|
||||
or is_machine_tell(impulse)
|
||||
or is_reopen_after_close(impulse, assembled)
|
||||
)
|
||||
|
||||
|
||||
def visible_for_role(payload: dict, role: str | None) -> dict:
|
||||
if role == "admin":
|
||||
return payload
|
||||
cleaned = dict(payload)
|
||||
cleaned.pop("trace", None)
|
||||
cleaned.pop("decision", None)
|
||||
return cleaned
|
||||
|
||||
|
||||
def run_turn(profile_id: str, conversation_id: str, body: str, message_id: str | None = None) -> dict:
|
||||
conversation = get_conversation(profile_id, conversation_id)
|
||||
user = append_message(profile_id, conversation_id, body, role="user", message_id=message_id)
|
||||
context = build_internal_context(
|
||||
profile_id,
|
||||
conversation_id=conversation_id,
|
||||
space_id=conversation.get("space_id"),
|
||||
journal_day_id=conversation.get("journal_day_id"),
|
||||
purpose="dialogue_turn",
|
||||
)
|
||||
assembled = assemble_text(context)
|
||||
prompt = load_active_prompt("mvp.dialogue_turn")
|
||||
calls = 0
|
||||
result = None
|
||||
impulse = ""
|
||||
decision: dict = {"operation": "unparsed", "label": "nicht erkannt", "parsed": False}
|
||||
try:
|
||||
while calls < 2:
|
||||
result = execute_prompt(
|
||||
prompt,
|
||||
profile_id,
|
||||
purpose="dialogue_turn",
|
||||
data_class="B",
|
||||
context=assembled,
|
||||
)
|
||||
calls += 1
|
||||
content = (result.get("content") or "").strip()
|
||||
if not content:
|
||||
raise EngineError("empty_provider_response", "Der Provider lieferte keine Antwort.")
|
||||
impulse, decision = parse_turn_payload(content)
|
||||
if not needs_repair(impulse, assembled):
|
||||
break
|
||||
decision = {**decision, "guard": "impulse_rejected"}
|
||||
assembled = dict(assembled)
|
||||
assembled["dialogue_context"] = (
|
||||
(assembled.get("dialogue_context") or "") + "\n\n" + repair_note(impulse, assembled)
|
||||
)
|
||||
if needs_repair(impulse, assembled):
|
||||
parsed_ok = bool(decision.get("parsed"))
|
||||
impulse = local_hold(last_user_text(assembled))
|
||||
decision = {
|
||||
"operation": "fortfuehren",
|
||||
"label": OPERATIONS["fortfuehren"],
|
||||
"parsed": parsed_ok,
|
||||
"guard": "local_fallback",
|
||||
}
|
||||
except EngineError:
|
||||
raise
|
||||
assistant = append_message(profile_id, conversation_id, impulse, role="assistant")
|
||||
user_bodies = [
|
||||
item.get("body") or ""
|
||||
for item in list_messages(profile_id, conversation_id)
|
||||
if item.get("role") == "user"
|
||||
]
|
||||
update_conversation_signals(
|
||||
profile_id,
|
||||
conversation_id,
|
||||
infer_signals(user_bodies, decision.get("operation")),
|
||||
)
|
||||
remember_dialogue_style(profile_id)
|
||||
consider_dialogue(profile_id, body)
|
||||
return {
|
||||
"conversation": get_conversation(profile_id, conversation_id),
|
||||
"user": user,
|
||||
"assistant": assistant,
|
||||
"calls": calls,
|
||||
"messages": list_messages(profile_id, conversation_id),
|
||||
"decision": decision,
|
||||
"trace": result.get("trace") if result else None,
|
||||
}
|
||||
+55
-5
@@ -3,11 +3,13 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import placeholder_mvp # noqa: F401
|
||||
import placeholder_system # noqa: F401 registers system keys
|
||||
import privacy_placeholders
|
||||
from entitlements import EntitlementError, check_feature_access
|
||||
from db import get_db, row_to_dict
|
||||
from entitlements import EntitlementError, check_feature_access, increment_feature_usage
|
||||
from placeholders import PlaceholderError, resolve_template
|
||||
from privacy_gateway import GatewayRequest, PrivacyGatewayError, complete
|
||||
from privacy_gateway import GatewayRequest, PrivacyGatewayError, complete, last_trace, public_trace
|
||||
|
||||
|
||||
class EngineError(Exception):
|
||||
@@ -18,6 +20,19 @@ class EngineError(Exception):
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
def load_active_prompt(slug: str) -> dict:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"SELECT * FROM ai_prompts WHERE slug = ? AND active = 1",
|
||||
(slug,),
|
||||
).fetchone()
|
||||
)
|
||||
if not row:
|
||||
raise EngineError("prompt_missing", f"Prompt fehlt oder ist inaktiv: {slug}", 404)
|
||||
return row
|
||||
|
||||
|
||||
def preview_prompt(prompt: dict, context: dict[str, Any] | None = None) -> dict:
|
||||
template = prompt.get("template") or ""
|
||||
if prompt.get("prompt_type") != "base":
|
||||
@@ -36,6 +51,14 @@ def preview_prompt(prompt: dict, context: dict[str, Any] | None = None) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def user_source_text(dialogue_context: str) -> str:
|
||||
parts = []
|
||||
for line in (dialogue_context or "").splitlines():
|
||||
if line.startswith("user:"):
|
||||
parts.append(line[5:].lstrip())
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
def execute_prompt(prompt: dict, profile_id: str, purpose: str, data_class: str, context: dict[str, Any] | None = None) -> dict:
|
||||
preview = preview_prompt(prompt, context)
|
||||
feature_id = prompt.get("required_feature") or "ai_calls"
|
||||
@@ -44,14 +67,41 @@ def execute_prompt(prompt: dict, profile_id: str, purpose: str, data_class: str,
|
||||
except EntitlementError as exc:
|
||||
raise EngineError(exc.code, exc.message, exc.status_code) from exc
|
||||
try:
|
||||
complete(
|
||||
ctx = context or {}
|
||||
source_text = user_source_text(ctx.get("dialogue_context") or "")
|
||||
extras = []
|
||||
if purpose == "journal_generate":
|
||||
extras = [part for part in (ctx.get("existing_text"), ctx.get("writing_profile")) if part]
|
||||
elif purpose == "profile_review":
|
||||
source_text = ctx.get("source_text") or ""
|
||||
extras = [part for part in (ctx.get("review_package"),) if part]
|
||||
if extras:
|
||||
source_text = "\n".join(part for part in (source_text, *extras) if part)
|
||||
elif not source_text:
|
||||
source_text = "\n".join(
|
||||
part for part in (ctx.get("existing_text"), ctx.get("writing_profile")) if part
|
||||
)
|
||||
result = complete(
|
||||
GatewayRequest(
|
||||
prompt_id=prompt["id"],
|
||||
purpose=purpose,
|
||||
data_class=data_class,
|
||||
payload={"template_chars": len(preview["rendered"]), "privacy_tokens": preview["privacy_tokens"]},
|
||||
profile_id=profile_id,
|
||||
payload={
|
||||
"rendered": preview["rendered"],
|
||||
"source_text": source_text,
|
||||
"privacy_tokens": preview["privacy_tokens"],
|
||||
"prompt_slug": prompt.get("slug"),
|
||||
},
|
||||
)
|
||||
)
|
||||
except PrivacyGatewayError as exc:
|
||||
raise EngineError(exc.code, exc.message, exc.status_code) from exc
|
||||
raise EngineError("provider_missing", "Gateway erlaubte einen Aufruf ohne konfigurierten Provider", 500)
|
||||
increment_feature_usage(profile_id, feature_id)
|
||||
return {
|
||||
**preview,
|
||||
"llm": True,
|
||||
"content": result.content,
|
||||
"provider": result.provider,
|
||||
"trace": public_trace(last_trace()),
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
"""Entity detection for the privacy gateway. May see plaintext; generate must not."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
|
||||
import placeholder_mvp # noqa: F401
|
||||
from db import get_db, row_to_dict
|
||||
from env_loader import allows_remote_plaintext_detect
|
||||
from identity_store import (
|
||||
coalesce_mappings,
|
||||
is_given_name_candidate,
|
||||
is_maskable_label,
|
||||
list_mappings,
|
||||
normalize_label,
|
||||
purge_unmaskable,
|
||||
remember_mapping,
|
||||
)
|
||||
from placeholders import PlaceholderError, resolve_template
|
||||
from providers import ProviderError, complete_chat, detect_provider
|
||||
|
||||
DETECT_MAX_CHARS = 8000
|
||||
DETECT_TIMEOUT = 20.0
|
||||
DETECT_MAX_TOKENS = 300
|
||||
JSON_BLOCK = re.compile(r"\{.*\}", re.DOTALL)
|
||||
NAME_TITLE = re.compile(r"(?:Frau|Herr)\s+([A-ZÄÖÜ][a-zäöüß]{2,})\b")
|
||||
NAME_PREP = re.compile(r"(?:mit|von|bei)\s+([A-ZÄÖÜ][a-zäöüß]{2,})\b")
|
||||
_LETTER = r"A-Za-zÄÖÜäöüß"
|
||||
PLACE_NAME = re.compile(
|
||||
r"\b([A-ZÄÖÜ][a-zäöüß]*(?:burg|stadt|dorf|haven|ingen|heim|bach|feld))\b"
|
||||
)
|
||||
STOPWORDS = {
|
||||
"Heute", "Gestern", "Morgen", "Ich", "Wir", "Der", "Die", "Das", "Ein", "Eine",
|
||||
"Und", "Oder", "Nicht", "Kein", "Keine", "Am", "Im", "Zum", "Zur", "Mit",
|
||||
"Nach", "Beim", "Über", "Unter", "Aber", "Denn", "Wenn", "Dann", "Also",
|
||||
"Was", "Wer", "Wie", "Wo", "Warum", "Bitte", "Danke", "Hallo", "Space",
|
||||
"Journal", "Dialog", "Kontext", "Writing", "Profile", "Tagebucheintrag",
|
||||
"Relativ", "Allerdings", "Zunächst", "Danach", "Deshalb", "Trotzdem",
|
||||
"Außerdem", "Schließlich", "Während", "Bevor", "Seitdem",
|
||||
}
|
||||
|
||||
|
||||
def _detect_prompt() -> dict:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"SELECT * FROM ai_prompts WHERE slug = ? AND active = 1",
|
||||
("mvp.entity_detect",),
|
||||
).fetchone()
|
||||
)
|
||||
if not row or not (row.get("template") or "").strip():
|
||||
raise ProviderError(
|
||||
"detect_prompt_missing",
|
||||
"Prompt mvp.entity_detect fehlt in der Konfiguration.",
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def _pattern_entities(text: str, known_labels: set[str]) -> list[dict]:
|
||||
found: list[dict] = []
|
||||
seen: set[str] = set()
|
||||
source = text or ""
|
||||
candidates: list[tuple[str, str]] = []
|
||||
for match in NAME_TITLE.finditer(source):
|
||||
candidates.append((match.group(1).strip(), "PERSON:01"))
|
||||
for match in NAME_PREP.finditer(source):
|
||||
name = match.group(1).strip()
|
||||
if is_given_name_candidate(name):
|
||||
candidates.append((name, "PERSON:01"))
|
||||
for match in PLACE_NAME.finditer(source):
|
||||
candidates.append((match.group(1).strip(), "PLACE:CITY"))
|
||||
for raw, token in candidates:
|
||||
label = normalize_label(raw) or raw
|
||||
if label in STOPWORDS or label in known_labels or label in seen:
|
||||
continue
|
||||
if not is_maskable_label(label):
|
||||
continue
|
||||
seen.add(label)
|
||||
found.append({"text": label, "token": token})
|
||||
return found
|
||||
|
||||
|
||||
def _fake_entities(text: str, known_labels: set[str]) -> list[dict]:
|
||||
return _pattern_entities(text, known_labels)
|
||||
|
||||
|
||||
def _merge_proposals(*groups: list[dict]) -> list[dict]:
|
||||
merged: list[dict] = []
|
||||
seen: set[str] = set()
|
||||
for group in groups:
|
||||
for item in group:
|
||||
label = (item.get("text") or "").strip()
|
||||
if not label or label in seen:
|
||||
continue
|
||||
seen.add(label)
|
||||
merged.append(item)
|
||||
return merged
|
||||
|
||||
|
||||
def _parse_entities(raw: str) -> list[dict]:
|
||||
text = (raw or "").strip()
|
||||
if text.startswith("```"):
|
||||
text = re.sub(r"^```(?:json)?\s*|\s*```$", "", text, flags=re.IGNORECASE | re.DOTALL)
|
||||
match = JSON_BLOCK.search(text)
|
||||
if not match:
|
||||
return []
|
||||
try:
|
||||
data = json.loads(match.group(0))
|
||||
except json.JSONDecodeError:
|
||||
return []
|
||||
items = data.get("entities") if isinstance(data, dict) else data
|
||||
if not isinstance(items, list):
|
||||
return []
|
||||
result = []
|
||||
for item in items:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
label = (item.get("text") or item.get("label") or "").strip()
|
||||
token = (item.get("token") or item.get("placeholder") or "").strip()
|
||||
if label:
|
||||
result.append({"text": label, "token": token})
|
||||
return result
|
||||
|
||||
|
||||
def _label_in_text(label: str, text: str) -> bool:
|
||||
if not (label or "").strip() or not (text or "").strip():
|
||||
return False
|
||||
return bool(
|
||||
re.search(
|
||||
rf"(?<![{_LETTER}]){re.escape(label.strip())}(?![{_LETTER}])",
|
||||
text,
|
||||
re.IGNORECASE,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _acceptable_proposal(item: dict, excerpt: str) -> bool:
|
||||
label = (item.get("text") or "").strip()
|
||||
if not label or not _label_in_text(label, excerpt) or not is_maskable_label(label):
|
||||
return False
|
||||
token = (item.get("token") or "").upper()
|
||||
if token.startswith("PLACE:") or token.startswith("ORG:"):
|
||||
return True
|
||||
return is_given_name_candidate(label)
|
||||
|
||||
|
||||
def uses_llm_detect(config) -> bool:
|
||||
"""Local detect stays a provider role. Remote plaintext detect is development/test only."""
|
||||
if not config or config.mode != "http":
|
||||
return False
|
||||
if config.local:
|
||||
return True
|
||||
return allows_remote_plaintext_detect()
|
||||
|
||||
|
||||
def _llm_entities(config, excerpt: str, known_labels: set[str]) -> list[dict]:
|
||||
known = ", ".join(sorted(known_labels)) or "(keine)"
|
||||
prompt = resolve_template(
|
||||
_detect_prompt()["template"],
|
||||
{"source_text": excerpt, "known_labels": known},
|
||||
)
|
||||
raw = complete_chat(
|
||||
config,
|
||||
[{"role": "user", "content": prompt}],
|
||||
timeout=DETECT_TIMEOUT,
|
||||
max_tokens=DETECT_MAX_TOKENS,
|
||||
)
|
||||
return _parse_entities(raw)
|
||||
|
||||
|
||||
def detect_and_remember(profile_id: str | None, source_text: str) -> tuple[list[dict], str | None, str | None]:
|
||||
if profile_id:
|
||||
purge_unmaskable(profile_id)
|
||||
coalesce_mappings(profile_id)
|
||||
mappings = list_mappings(profile_id) if profile_id else []
|
||||
if not profile_id:
|
||||
return mappings, None, None
|
||||
known_labels = {
|
||||
normalize_label(item.get("local_label") or "") or (item.get("local_label") or "").strip()
|
||||
for item in mappings
|
||||
if item.get("local_label")
|
||||
}
|
||||
excerpt = (source_text or "")[:DETECT_MAX_CHARS]
|
||||
proposals = _pattern_entities(excerpt, known_labels)
|
||||
detect_name = "pattern"
|
||||
detect_note = "pattern"
|
||||
config = detect_provider()
|
||||
if config and config.mode == "fake":
|
||||
detect_name = config.name
|
||||
detect_note = "fake"
|
||||
elif uses_llm_detect(config):
|
||||
try:
|
||||
proposals = _merge_proposals(
|
||||
proposals,
|
||||
[item for item in _llm_entities(config, excerpt, known_labels) if _acceptable_proposal(item, excerpt)],
|
||||
)
|
||||
detect_name = config.name
|
||||
detect_note = "local_llm" if config.local else "remote_llm"
|
||||
except PlaceholderError as exc:
|
||||
raise ProviderError(exc.code, exc.message) from exc
|
||||
elif config and config.mode == "http" and not config.local:
|
||||
detect_note = "pattern; remote_detect_blocked_production"
|
||||
for item in proposals:
|
||||
label = (item.get("text") or "").strip()
|
||||
core = normalize_label(label)
|
||||
if not label or not core or core in known_labels:
|
||||
continue
|
||||
if not _acceptable_proposal(item, excerpt):
|
||||
continue
|
||||
try:
|
||||
remember_mapping(profile_id, label, item.get("token"))
|
||||
except ValueError:
|
||||
continue
|
||||
known_labels.add(core)
|
||||
coalesce_mappings(profile_id)
|
||||
return list_mappings(profile_id), detect_name, detect_note
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Load backend/.env into os.environ without overwriting a real process environment."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
ENV_PATH = Path(__file__).resolve().parent / ".env"
|
||||
|
||||
|
||||
def load_env_file(path: Path | None = None) -> None:
|
||||
target = path or ENV_PATH
|
||||
if not target.is_file():
|
||||
return
|
||||
for raw in target.read_text(encoding="utf-8").splitlines():
|
||||
line = raw.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
if not key or key in os.environ:
|
||||
continue
|
||||
value = value.strip()
|
||||
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
|
||||
value = value[1:-1]
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
def runtime_env() -> str:
|
||||
"""development/test may use remote plaintext detect. production must not."""
|
||||
raw = (os.environ.get("KANSHO_ENV") or "").strip().lower()
|
||||
if raw in {"production", "prod"}:
|
||||
return "production"
|
||||
if raw in {"test", "testing"}:
|
||||
return "test"
|
||||
if raw in {"development", "dev"}:
|
||||
return "development"
|
||||
return "development"
|
||||
|
||||
|
||||
def allows_remote_plaintext_detect() -> bool:
|
||||
return runtime_env() != "production"
|
||||
|
||||
|
||||
def upsert_env_value(name: str, value: str, path: Path | None = None) -> None:
|
||||
"""Write one key into backend/.env and the current process. Never log the value."""
|
||||
target = path or ENV_PATH
|
||||
name = name.strip()
|
||||
if not name:
|
||||
raise ValueError("empty env name")
|
||||
lines: list[str] = []
|
||||
if target.is_file():
|
||||
lines = target.read_text(encoding="utf-8").splitlines()
|
||||
found = False
|
||||
updated: list[str] = []
|
||||
prefix = f"{name}="
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if stripped.startswith(prefix) or stripped.startswith(f"{name} ="):
|
||||
updated.append(f"{name}={value}")
|
||||
found = True
|
||||
else:
|
||||
updated.append(line)
|
||||
if not found:
|
||||
if updated and updated[-1] != "":
|
||||
updated.append("")
|
||||
updated.append(f"{name}={value}")
|
||||
target.write_text("\n".join(updated) + "\n", encoding="utf-8")
|
||||
os.environ[name] = value
|
||||
@@ -0,0 +1,282 @@
|
||||
"""Local identity mappings. Class A; never part of an external prompt."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from db import get_db, row_to_dict
|
||||
|
||||
PROMPTS_SEED_PATH = Path(__file__).resolve().parent / "config" / "prompts.seed.json"
|
||||
|
||||
TOKEN_RE = re.compile(r"^(SELF|PERSON:[A-Z][A-Z0-9_]{0,24}|PLACE:[A-Z][A-Z0-9_]{0,24}|ORG:[A-Z][A-Z0-9_]{0,24})$")
|
||||
MIN_LABEL_LEN = 4
|
||||
TIME_RE = re.compile(r"\d|:\d|\buhr\b|\bminuten\b|\bstunde\b", re.IGNORECASE)
|
||||
DETERMINERS = {
|
||||
"der", "die", "das", "den", "dem", "des",
|
||||
"ein", "eine", "einen", "einem", "eines",
|
||||
"mein", "meine", "meinen", "meinem", "meiner", "meines",
|
||||
"dein", "deine", "sein", "seine", "ihr", "ihre",
|
||||
"unser", "unsere", "euer", "eure",
|
||||
}
|
||||
KINSHIP = {"frau", "herr", "mann", "kind", "tochter", "sohn", "partner", "partnerin"}
|
||||
UNMASKABLE = {
|
||||
"ich", "du", "er", "sie", "es", "wir", "ihr", "mich", "mir", "mein", "meine",
|
||||
"dir", "dich", "uns", "euch", "sein", "seine", "ihre",
|
||||
"heute", "gestern", "morgen", "user", "assistant", "self", "kontext",
|
||||
"himmel", "luft", "regen", "sonne", "wind", "meer", "tee", "kaffee",
|
||||
"balkon", "laden", "brot", "brote", "bad", "gasse", "gassen", "weg",
|
||||
"ort", "ortes", "stadt", "zimmer", "haus",
|
||||
"frau", "herr", "mann", "kind", "lektion", "bootstour", "frühstück",
|
||||
"tagebuch", "omlett", "haferflocken", "speiseplan",
|
||||
"impuls", "tatsache", "motivation", "erzählung", "operation",
|
||||
"uhr", "zeit", "stunde", "minute", "tag", "tages", "abend", "nacht",
|
||||
"warm", "klar", "draußen", "menge", "teil", "stück",
|
||||
"relativ", "allerdings", "zunächst", "danach", "deshalb", "trotzdem",
|
||||
"außerdem", "schließlich", "während", "bevor", "seitdem", "deswegen",
|
||||
"überlegen", "feststellen", "frage", "antwort",
|
||||
"markt", "kirschen", "hafen", "wetter", "vormittag", "nachmittag",
|
||||
"anreise", "details", "absatz", "essen", "licht",
|
||||
"zwiebeln", "zwiebel", "tomaten", "tomate", "paprika",
|
||||
"omelett", "trockenobst", "trockenfrüchte", "glutenfrei",
|
||||
"schiff", "boot", "zweimaster", "fähre",
|
||||
}
|
||||
FUNCTION_STEMS = ("letzt", "nächst")
|
||||
NOUN_SUFFIXES = ("heit", "keit", "schaft", "tum")
|
||||
COLLECTIVE_NOUN = re.compile(r"^ge[a-zäöüß]+öse$", re.IGNORECASE)
|
||||
RESERVED_TOKEN_SUFFIXES = {"KURZ", "NAME", "TEXT", "LABEL", "EXAMPLE", "FOO", "BAR"}
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def instruction_vocab() -> frozenset[str]:
|
||||
"""Words from Kanshō prompts must never become identity labels."""
|
||||
items = json.loads(PROMPTS_SEED_PATH.read_text(encoding="utf-8"))
|
||||
words: set[str] = set()
|
||||
for item in items:
|
||||
if (item.get("slug") or "") == "mvp.entity_detect":
|
||||
continue
|
||||
template = item.get("template") or ""
|
||||
template = re.sub(r"\{\{[^}]+\}\}|\[\[[^\]]+\]\]", " ", template)
|
||||
for word in re.findall(r"[A-Za-zÄÖÜäöüß]{4,}", template):
|
||||
words.add(word.casefold())
|
||||
return frozenset(words)
|
||||
|
||||
|
||||
def is_function_label(label: str) -> bool:
|
||||
word = (label or "").strip().casefold()
|
||||
if not word:
|
||||
return False
|
||||
for stem in FUNCTION_STEMS:
|
||||
if word == stem or word.startswith(stem):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def is_common_noun_shape(label: str) -> bool:
|
||||
word = (label or "").strip()
|
||||
if COLLECTIVE_NOUN.match(word):
|
||||
return True
|
||||
lower = word.casefold()
|
||||
if lower.endswith("ung") and len(lower) > 6:
|
||||
return True
|
||||
return any(lower.endswith(suffix) for suffix in NOUN_SUFFIXES)
|
||||
|
||||
|
||||
def is_given_name_candidate(label: str) -> bool:
|
||||
core = normalize_label(label)
|
||||
if not core or " " in core:
|
||||
return False
|
||||
if is_function_label(core) or is_common_noun_shape(core):
|
||||
return False
|
||||
return is_maskable_label(core)
|
||||
|
||||
|
||||
def normalize_label(label: str) -> str:
|
||||
words = re.findall(r"[A-Za-zÄÖÜäöüß0-9.:]+", label or "")
|
||||
keep = [
|
||||
word
|
||||
for word in words
|
||||
if word.lower() not in DETERMINERS
|
||||
and word.lower() not in KINSHIP
|
||||
and word.lower() not in UNMASKABLE
|
||||
]
|
||||
return " ".join(keep).strip()
|
||||
|
||||
|
||||
def is_maskable_label(label: str) -> bool:
|
||||
core = normalize_label(label)
|
||||
if len(core) < MIN_LABEL_LEN:
|
||||
return False
|
||||
if core.lower() in UNMASKABLE:
|
||||
return False
|
||||
if is_function_label(core) or is_common_noun_shape(core):
|
||||
return False
|
||||
if core.casefold() in instruction_vocab():
|
||||
return False
|
||||
if TIME_RE.search(core):
|
||||
return False
|
||||
if re.fullmatch(r"user|assistant|self", core, re.IGNORECASE):
|
||||
return False
|
||||
if not re.search(r"[A-Za-zÄÖÜäöüß]", core):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def purge_unmaskable(profile_id: str) -> int:
|
||||
removed = 0
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT id, local_label FROM identity_mappings WHERE profile_id = ?",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
for row in rows:
|
||||
if is_maskable_label(row["local_label"]):
|
||||
continue
|
||||
conn.execute("DELETE FROM identity_mappings WHERE id = ?", (row["id"],))
|
||||
removed += 1
|
||||
return removed
|
||||
|
||||
|
||||
def list_mappings(profile_id: str) -> list[dict]:
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT token, local_label FROM identity_mappings WHERE profile_id = ? ORDER BY created",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
|
||||
def _token_suffix(token: str) -> str:
|
||||
raw = (token or "").upper()
|
||||
return raw.split(":")[-1] if ":" in raw else raw
|
||||
|
||||
|
||||
def normalize_token(token: str | None) -> str | None:
|
||||
raw = (token or "").strip().upper().replace(" ", "_")
|
||||
raw = raw[2:-2] if raw.startswith("[[") and raw.endswith("]]") else raw
|
||||
if not TOKEN_RE.match(raw):
|
||||
return None
|
||||
if raw.count("_") > 1:
|
||||
return None
|
||||
if _token_suffix(raw) in RESERVED_TOKEN_SUFFIXES:
|
||||
return None
|
||||
return raw
|
||||
|
||||
|
||||
def _same_core(left: str, right: str) -> bool:
|
||||
a = normalize_label(left).casefold()
|
||||
b = normalize_label(right).casefold()
|
||||
return bool(a and b and a == b)
|
||||
|
||||
|
||||
def find_existing_mapping(existing: list[dict], label: str) -> dict | None:
|
||||
core = normalize_label(label).casefold()
|
||||
if not core:
|
||||
return None
|
||||
for item in existing:
|
||||
stored = (item.get("local_label") or "").strip()
|
||||
if stored.casefold() == label.casefold() or _same_core(stored, label):
|
||||
return item
|
||||
return None
|
||||
|
||||
|
||||
def _pick_keeper(rows: list) -> dict:
|
||||
def sort_key(row: dict) -> tuple:
|
||||
suffix = _token_suffix(row["token"] or "")
|
||||
reserved = 1 if suffix in RESERVED_TOKEN_SUFFIXES else 0
|
||||
numeric = 0 if suffix.isdigit() else 1
|
||||
return (reserved, numeric, row["created"] or "", row["id"] or "")
|
||||
|
||||
return min(rows, key=sort_key)
|
||||
|
||||
|
||||
def coalesce_mappings(profile_id: str) -> int:
|
||||
"""One token per normalized name. Template tokens like PERSON:KURZ lose against PERSON:08."""
|
||||
merged = 0
|
||||
with get_db() as conn:
|
||||
rows = [
|
||||
row_to_dict(row)
|
||||
for row in conn.execute(
|
||||
"""
|
||||
SELECT id, token, local_label, created
|
||||
FROM identity_mappings
|
||||
WHERE profile_id = ?
|
||||
ORDER BY created
|
||||
""",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
]
|
||||
groups: dict[str, list[dict]] = {}
|
||||
for row in rows:
|
||||
core = normalize_label(row.get("local_label") or "")
|
||||
if not core or not is_maskable_label(core):
|
||||
conn.execute("DELETE FROM identity_mappings WHERE id = ?", (row["id"],))
|
||||
merged += 1
|
||||
continue
|
||||
groups.setdefault(core.casefold(), []).append({**row, "core": core})
|
||||
for items in groups.values():
|
||||
keeper = _pick_keeper(items)
|
||||
core = items[0]["core"]
|
||||
if (keeper.get("local_label") or "") != core:
|
||||
conn.execute(
|
||||
"UPDATE identity_mappings SET local_label = ? WHERE id = ?",
|
||||
(core, keeper["id"]),
|
||||
)
|
||||
for row in items:
|
||||
if row["id"] == keeper["id"]:
|
||||
continue
|
||||
conn.execute("DELETE FROM identity_mappings WHERE id = ?", (row["id"],))
|
||||
merged += 1
|
||||
return merged
|
||||
|
||||
|
||||
def _next_generic(existing: list[dict], prefix: str) -> str:
|
||||
used = set()
|
||||
for item in existing:
|
||||
token = (item.get("token") or "").upper()
|
||||
if token.startswith(prefix):
|
||||
suffix = token[len(prefix) :]
|
||||
if suffix.isdigit():
|
||||
used.add(int(suffix))
|
||||
n = 1
|
||||
while n in used:
|
||||
n += 1
|
||||
return f"{prefix}{n:02d}"
|
||||
|
||||
|
||||
def remember_mapping(profile_id: str, local_label: str, suggested_token: str | None = None) -> dict:
|
||||
label = normalize_label(local_label)
|
||||
if not label or not is_maskable_label(label):
|
||||
raise ValueError("empty_label")
|
||||
existing = list_mappings(profile_id)
|
||||
found = find_existing_mapping(existing, label)
|
||||
if found:
|
||||
stored = (found.get("local_label") or "").strip()
|
||||
if stored != label:
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE identity_mappings
|
||||
SET local_label = ?
|
||||
WHERE profile_id = ? AND token = ?
|
||||
""",
|
||||
(label, profile_id, found["token"]),
|
||||
)
|
||||
found = {**found, "local_label": label}
|
||||
return found
|
||||
token = normalize_token(suggested_token)
|
||||
if token and any((item.get("token") or "").upper() == token for item in existing):
|
||||
token = None
|
||||
if not token:
|
||||
token = _next_generic(existing, "PERSON:")
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO identity_mappings (id, profile_id, token, local_label)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(str(uuid.uuid4()), profile_id, token, label),
|
||||
)
|
||||
return {"token": token, "local_label": label}
|
||||
@@ -0,0 +1,32 @@
|
||||
"""Labeled product defaults for dialogue interaction. Not a personality model.
|
||||
|
||||
These are visible, overridable defaults — not silent personal facts inferred from
|
||||
the mere fact that a dialogue happened.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
DEFAULTS = (
|
||||
{
|
||||
"scope": "global",
|
||||
"pref_key": "reply_length",
|
||||
"title": "Antwortlänge",
|
||||
"value": "Ein bis drei Sätze.",
|
||||
},
|
||||
{
|
||||
"scope": "global",
|
||||
"pref_key": "follow_up",
|
||||
"title": "Anschlussimpuls",
|
||||
"value": "Nur einen dominanten Anschlussimpuls setzen, nicht mehrere Fragen stapeln.",
|
||||
},
|
||||
{
|
||||
"scope": "autobiographical",
|
||||
"pref_key": "listening",
|
||||
"title": "Zuhören bei Erzählung",
|
||||
"value": "Bei längeren autobiografischen Erzählungen zunächst stärker zuhören und wenig unterbrechen.",
|
||||
},
|
||||
)
|
||||
|
||||
SCOPES = ("global", "autobiographical")
|
||||
PREF_KEYS = tuple(item["pref_key"] for item in DEFAULTS)
|
||||
GOVERNANCE = {"learning", "advising", "frozen"}
|
||||
DEFAULT_GOVERNANCE = "advising"
|
||||
@@ -0,0 +1,423 @@
|
||||
"""Interaction profile: stable preferences, never learned from silence.
|
||||
|
||||
Strong sources only: explicit instruction, explicit feedback, manual setting,
|
||||
accepted suggestion. Observations may be stored as suggestions, never auto-applied
|
||||
from a dialogue turn.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
from db import get_db, row_to_dict
|
||||
from dialogue_store import StoreError
|
||||
from interaction_defaults import DEFAULT_GOVERNANCE, DEFAULTS, GOVERNANCE, PREF_KEYS, SCOPES
|
||||
from profile_documents import (
|
||||
FORMAT_VERSION,
|
||||
KIND_INTERACTION,
|
||||
ORIGIN_INTERACTION,
|
||||
exported_at,
|
||||
interaction_payload,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"GOVERNANCE",
|
||||
"SCOPES",
|
||||
"get_profile",
|
||||
"set_governance",
|
||||
"upsert_preference",
|
||||
"accept_suggestion",
|
||||
"reject_suggestion",
|
||||
"propose_observation",
|
||||
"assemble_interaction_hint",
|
||||
"export_document",
|
||||
"restore_document",
|
||||
]
|
||||
|
||||
|
||||
def ensure_profile(profile_id: str) -> dict:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute("SELECT * FROM interaction_profiles WHERE profile_id = ?", (profile_id,)).fetchone()
|
||||
)
|
||||
if row:
|
||||
return row
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO interaction_profiles (profile_id, governance)
|
||||
VALUES (?, ?)
|
||||
""",
|
||||
(profile_id, DEFAULT_GOVERNANCE),
|
||||
)
|
||||
return row_to_dict(
|
||||
conn.execute("SELECT * FROM interaction_profiles WHERE profile_id = ?", (profile_id,)).fetchone()
|
||||
)
|
||||
|
||||
|
||||
def get_profile(profile_id: str) -> dict:
|
||||
row = ensure_profile(profile_id)
|
||||
with get_db() as conn:
|
||||
prefs = [
|
||||
row_to_dict(item)
|
||||
for item in conn.execute(
|
||||
"""
|
||||
SELECT * FROM interaction_preferences
|
||||
WHERE profile_id = ?
|
||||
ORDER BY CASE scope WHEN 'global' THEN 0 ELSE 1 END, pref_key
|
||||
""",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
]
|
||||
suggestions = [
|
||||
row_to_dict(item)
|
||||
for item in conn.execute(
|
||||
"""
|
||||
SELECT * FROM interaction_suggestions
|
||||
WHERE profile_id = ? AND status = 'pending'
|
||||
ORDER BY created DESC
|
||||
""",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
]
|
||||
for item in prefs:
|
||||
item["locked"] = bool(item.get("locked"))
|
||||
item["title"] = _title(item.get("scope") or "global", item.get("pref_key") or "")
|
||||
defaults = [
|
||||
{
|
||||
**item,
|
||||
"source": "product_default",
|
||||
"overridden": any(
|
||||
pref.get("scope") == item["scope"] and pref.get("pref_key") == item["pref_key"]
|
||||
for pref in prefs
|
||||
),
|
||||
}
|
||||
for item in DEFAULTS
|
||||
]
|
||||
return {
|
||||
"profile_id": profile_id,
|
||||
"governance": (row or {}).get("governance") or DEFAULT_GOVERNANCE,
|
||||
"updated": (row or {}).get("updated"),
|
||||
"preferences": prefs,
|
||||
"defaults": defaults,
|
||||
"suggestions": suggestions,
|
||||
"note": (
|
||||
"Persönliche Interaction-Präferenzen entstehen nur durch ausdrückliche Angabe, "
|
||||
"Feedback, manuelle Einstellung oder angenommene Vorschläge. "
|
||||
"Ein Dialog ohne Widerspruch ist kein Lernsignal."
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def set_governance(profile_id: str, governance: str) -> dict:
|
||||
mode = (governance or "").strip()
|
||||
if mode not in GOVERNANCE:
|
||||
raise StoreError("invalid_governance", "governance muss learning, advising oder frozen sein")
|
||||
ensure_profile(profile_id)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE interaction_profiles SET governance = ?, updated = datetime('now') WHERE profile_id = ?",
|
||||
(mode, profile_id),
|
||||
)
|
||||
return get_profile(profile_id)
|
||||
|
||||
|
||||
def upsert_preference(
|
||||
profile_id: str,
|
||||
*,
|
||||
scope: str,
|
||||
pref_key: str,
|
||||
value: str,
|
||||
origin: str = "manual",
|
||||
locked: bool | None = None,
|
||||
evidence: str = "manuelle Einstellung",
|
||||
) -> dict:
|
||||
if scope not in SCOPES:
|
||||
raise StoreError("unknown_scope", "Unbekannter Interaction-Scope.")
|
||||
if pref_key not in PREF_KEYS:
|
||||
raise StoreError("unknown_pref", "Unbekannte Interaction-Präferenz.")
|
||||
if origin not in {"explicit", "manual", "accepted_suggestion"}:
|
||||
raise StoreError("invalid_origin", "Nur explizite, manuelle oder angenommene Herkunft.")
|
||||
ensure_profile(profile_id)
|
||||
with get_db() as conn:
|
||||
current = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT * FROM interaction_preferences
|
||||
WHERE profile_id = ? AND scope = ? AND pref_key = ?
|
||||
""",
|
||||
(profile_id, scope, pref_key),
|
||||
).fetchone()
|
||||
)
|
||||
next_locked = int(locked) if locked is not None else int((current or {}).get("locked") or 0)
|
||||
if current:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE interaction_preferences
|
||||
SET value = ?, evidence = ?, origin = ?, locked = ?, updated = datetime('now')
|
||||
WHERE id = ?
|
||||
""",
|
||||
(value, evidence, origin, next_locked, current["id"]),
|
||||
)
|
||||
else:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO interaction_preferences
|
||||
(id, profile_id, scope, pref_key, value, evidence, origin, locked)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(str(uuid.uuid4()), profile_id, scope, pref_key, value, evidence, origin, next_locked),
|
||||
)
|
||||
conn.execute(
|
||||
"UPDATE interaction_profiles SET updated = datetime('now') WHERE profile_id = ?",
|
||||
(profile_id,),
|
||||
)
|
||||
return get_profile(profile_id)
|
||||
|
||||
|
||||
def propose_observation(
|
||||
profile_id: str,
|
||||
*,
|
||||
scope: str,
|
||||
pref_key: str,
|
||||
proposed_value: str,
|
||||
evidence: str,
|
||||
) -> dict | None:
|
||||
"""Candidate only. Never applied here. Not called from the dialogue turn."""
|
||||
if scope not in SCOPES or pref_key not in PREF_KEYS:
|
||||
return None
|
||||
ensure_profile(profile_id)
|
||||
row = ensure_profile(profile_id)
|
||||
if (row.get("governance") or DEFAULT_GOVERNANCE) == "frozen":
|
||||
return None
|
||||
with get_db() as conn:
|
||||
current = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT locked FROM interaction_preferences
|
||||
WHERE profile_id = ? AND scope = ? AND pref_key = ?
|
||||
""",
|
||||
(profile_id, scope, pref_key),
|
||||
).fetchone()
|
||||
)
|
||||
if current and int(current.get("locked") or 0):
|
||||
return None
|
||||
pending = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT id, proposed_value FROM interaction_suggestions
|
||||
WHERE profile_id = ? AND scope = ? AND pref_key = ? AND status = 'pending'
|
||||
ORDER BY created DESC LIMIT 1
|
||||
""",
|
||||
(profile_id, scope, pref_key),
|
||||
).fetchone()
|
||||
)
|
||||
if pending and (pending.get("proposed_value") or "") == proposed_value:
|
||||
return get_profile(profile_id)
|
||||
if pending:
|
||||
conn.execute(
|
||||
"UPDATE interaction_suggestions SET status = 'rejected', resolved = datetime('now') WHERE id = ?",
|
||||
(pending["id"],),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO interaction_suggestions
|
||||
(id, profile_id, scope, pref_key, proposed_value, evidence, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, 'pending')
|
||||
""",
|
||||
(str(uuid.uuid4()), profile_id, scope, pref_key, proposed_value, evidence),
|
||||
)
|
||||
return get_profile(profile_id)
|
||||
|
||||
|
||||
def accept_suggestion(profile_id: str, suggestion_id: str) -> dict:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT * FROM interaction_suggestions
|
||||
WHERE id = ? AND profile_id = ? AND status = 'pending'
|
||||
""",
|
||||
(suggestion_id, profile_id),
|
||||
).fetchone()
|
||||
)
|
||||
if not row:
|
||||
raise StoreError("not_found", "Vorschlag nicht gefunden", 404)
|
||||
upsert_preference(
|
||||
profile_id,
|
||||
scope=row.get("scope") or "global",
|
||||
pref_key=row["pref_key"],
|
||||
value=row.get("proposed_value") or "",
|
||||
origin="accepted_suggestion",
|
||||
evidence=row.get("evidence") or "angenommener Vorschlag",
|
||||
)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE interaction_suggestions SET status = 'accepted', resolved = datetime('now') WHERE id = ?",
|
||||
(suggestion_id,),
|
||||
)
|
||||
return get_profile(profile_id)
|
||||
|
||||
|
||||
def reject_suggestion(profile_id: str, suggestion_id: str) -> dict:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT * FROM interaction_suggestions
|
||||
WHERE id = ? AND profile_id = ? AND status = 'pending'
|
||||
""",
|
||||
(suggestion_id, profile_id),
|
||||
).fetchone()
|
||||
)
|
||||
if not row:
|
||||
raise StoreError("not_found", "Vorschlag nicht gefunden", 404)
|
||||
conn.execute(
|
||||
"UPDATE interaction_suggestions SET status = 'rejected', resolved = datetime('now') WHERE id = ?",
|
||||
(suggestion_id,),
|
||||
)
|
||||
return get_profile(profile_id)
|
||||
|
||||
|
||||
def export_document(profile_id: str) -> dict:
|
||||
profile = get_profile(profile_id)
|
||||
return {
|
||||
"kind": KIND_INTERACTION,
|
||||
"format_version": FORMAT_VERSION,
|
||||
"exported_at": exported_at(),
|
||||
"governance": profile.get("governance") or DEFAULT_GOVERNANCE,
|
||||
"preferences": [
|
||||
{
|
||||
"scope": item.get("scope") or "global",
|
||||
"pref_key": item.get("pref_key"),
|
||||
"value": item.get("value") or "",
|
||||
"evidence": item.get("evidence") or "",
|
||||
"origin": item.get("origin") or "manual",
|
||||
"locked": bool(item.get("locked")),
|
||||
}
|
||||
for item in profile.get("preferences") or []
|
||||
if item.get("pref_key")
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def restore_document(profile_id: str, document: dict) -> dict:
|
||||
payload = interaction_payload(document)
|
||||
governance = (payload.get("governance") or DEFAULT_GOVERNANCE).strip()
|
||||
if governance not in GOVERNANCE:
|
||||
raise StoreError("invalid_governance", "governance muss learning, advising oder frozen sein")
|
||||
prefs = payload.get("preferences")
|
||||
if prefs is None:
|
||||
raise StoreError("invalid_document", "Interaction-Profile-Dokument braucht preferences.")
|
||||
if not isinstance(prefs, list):
|
||||
raise StoreError("invalid_document", "preferences muss eine Liste sein.")
|
||||
prepared: list[dict] = []
|
||||
seen: set[tuple[str, str]] = set()
|
||||
for item in prefs:
|
||||
if not isinstance(item, dict):
|
||||
raise StoreError("invalid_document", "Jede Präferenz muss ein Objekt sein.")
|
||||
scope = (item.get("scope") or "global").strip()
|
||||
pref_key = (item.get("pref_key") or "").strip()
|
||||
if scope not in SCOPES:
|
||||
raise StoreError("unknown_scope", f"Unbekannter Interaction-Scope: {scope}")
|
||||
if pref_key not in PREF_KEYS:
|
||||
raise StoreError("unknown_pref", f"Unbekannte Interaction-Präferenz: {pref_key or '—'}")
|
||||
key = (scope, pref_key)
|
||||
if key in seen:
|
||||
raise StoreError("duplicate_pref", f"Präferenz {scope}/{pref_key} ist doppelt.")
|
||||
origin = (item.get("origin") or "manual").strip()
|
||||
if origin not in ORIGIN_INTERACTION:
|
||||
origin = "manual"
|
||||
seen.add(key)
|
||||
prepared.append(
|
||||
{
|
||||
"scope": scope,
|
||||
"pref_key": pref_key,
|
||||
"value": item.get("value") or "",
|
||||
"evidence": item.get("evidence") or "aus Profil-Dokument",
|
||||
"origin": origin,
|
||||
"locked": 1 if item.get("locked") else 0,
|
||||
}
|
||||
)
|
||||
ensure_profile(profile_id)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE interaction_profiles SET governance = ?, updated = datetime('now') WHERE profile_id = ?",
|
||||
(governance, profile_id),
|
||||
)
|
||||
conn.execute("DELETE FROM interaction_preferences WHERE profile_id = ?", (profile_id,))
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE interaction_suggestions
|
||||
SET status = 'rejected', resolved = datetime('now')
|
||||
WHERE profile_id = ? AND status = 'pending'
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
for item in prepared:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO interaction_preferences
|
||||
(id, profile_id, scope, pref_key, value, evidence, origin, locked)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
str(uuid.uuid4()),
|
||||
profile_id,
|
||||
item["scope"],
|
||||
item["pref_key"],
|
||||
item["value"],
|
||||
item["evidence"],
|
||||
item["origin"],
|
||||
item["locked"],
|
||||
),
|
||||
)
|
||||
return get_profile(profile_id)
|
||||
|
||||
|
||||
def assemble_interaction_hint(profile_id: str | None, state: dict | None = None) -> str:
|
||||
"""Visible hint for the dialogue turn. Product defaults plus personal overrides plus current state."""
|
||||
prefs: dict[tuple[str, str], dict] = {}
|
||||
if profile_id:
|
||||
profile = get_profile(profile_id)
|
||||
for item in profile.get("preferences") or []:
|
||||
prefs[(item.get("scope") or "global", item.get("pref_key") or "")] = item
|
||||
lines = [
|
||||
"Interaction (sichtbar, überschreibbar; Nicht-Widersprechen ist kein Lernsignal):",
|
||||
]
|
||||
for item in DEFAULTS:
|
||||
key = (item["scope"], item["pref_key"])
|
||||
personal = prefs.get(key)
|
||||
if item["scope"] != "global":
|
||||
continue
|
||||
value = (personal.get("value") if personal else None) or item["value"]
|
||||
source = "persönlich" if personal else "Produktdefault"
|
||||
lines.append(f"- {item['title']}: {value} [{source}]")
|
||||
auto = next((item for item in DEFAULTS if item["pref_key"] == "listening"), None)
|
||||
if auto:
|
||||
personal = prefs.get(("autobiographical", "listening"))
|
||||
value = (personal.get("value") if personal else None) or auto["value"]
|
||||
source = "persönlich" if personal else "Produktdefault"
|
||||
if state and (state.get("long_story") or (state.get("narrative_mode") or "") == "chronicle"):
|
||||
lines.append(f"- {auto['title']}: {value} [{source}]")
|
||||
elif personal:
|
||||
lines.append(f"- {auto['title']}: {value} [{source}]")
|
||||
if state:
|
||||
mode = (state.get("narrative_mode") or "open").strip() or "open"
|
||||
depth = (state.get("reflection_depth") or "surface").strip() or "surface"
|
||||
intensity = (state.get("emotional_intensity") or "low").strip() or "low"
|
||||
long_story = "ja" if state.get("long_story") else "nein"
|
||||
focus = (state.get("current_focus") or "").strip()
|
||||
lines.append(
|
||||
"Aktueller Dialogue State (operativ, kein Profil, ändert sich von Zug zu Zug): "
|
||||
f"narrative_mode={mode}; reflection_depth={depth}; "
|
||||
f"emotional_intensity={intensity}; long_story={long_story}."
|
||||
)
|
||||
if focus:
|
||||
lines.append(f"current_focus={focus}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _title(scope: str, pref_key: str) -> str:
|
||||
for item in DEFAULTS:
|
||||
if item["scope"] == scope and item["pref_key"] == pref_key:
|
||||
return item["title"]
|
||||
return pref_key
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Journal entry body as Markdown. Media stays in media_assets; the body only references it.
|
||||
|
||||
Canonical embed: 
|
||||
Legacy [[media:<uuid>|caption]] is still read and normalised on save.
|
||||
|
||||
Tokens never go to the Privacy Gateway; captions may, as user wording.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
UUID = r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
|
||||
UUID_RE = re.compile(rf"^{UUID}$")
|
||||
MD_MEDIA_RE = re.compile(rf"!\[([^\]]*)\]\(kansho-media:({UUID})\)")
|
||||
LEGACY_MEDIA_RE = re.compile(rf"\[\[media:({UUID})(?:\|([^\]]*))?\]\]")
|
||||
HEADING_PREFIX = re.compile(r"^#{1,6}\s+")
|
||||
|
||||
|
||||
def clean_title(raw: str) -> str:
|
||||
"""First line only, without Markdown heading marks."""
|
||||
line = (raw or "").strip().splitlines()[0].strip() if raw else ""
|
||||
return HEADING_PREFIX.sub("", line).strip()
|
||||
|
||||
|
||||
def media_token(media_id: str, caption: str = "") -> str:
|
||||
cap = (caption or "").replace("]", "").replace("\n", " ").strip()
|
||||
return f""
|
||||
|
||||
|
||||
def media_ids(body: str) -> list[str]:
|
||||
seen: list[str] = []
|
||||
for match in MD_MEDIA_RE.finditer(body or ""):
|
||||
media_id = match.group(2)
|
||||
if media_id not in seen:
|
||||
seen.append(media_id)
|
||||
for match in LEGACY_MEDIA_RE.finditer(body or ""):
|
||||
media_id = match.group(1)
|
||||
if media_id not in seen:
|
||||
seen.append(media_id)
|
||||
return seen
|
||||
|
||||
|
||||
def to_markdown(body: str) -> str:
|
||||
"""Legacy media tokens become Markdown image syntax. Already-Markdown is unchanged."""
|
||||
|
||||
def repl(match: re.Match) -> str:
|
||||
return media_token(match.group(1), match.group(2) or "")
|
||||
|
||||
return LEGACY_MEDIA_RE.sub(repl, body or "")
|
||||
|
||||
|
||||
def plain_text(body: str) -> str:
|
||||
"""User wording only: no media URLs, no asset IDs, no Markdown markup. Captions stay."""
|
||||
text = to_markdown(body)
|
||||
|
||||
def caption(match: re.Match) -> str:
|
||||
return (match.group(1) or "").strip()
|
||||
|
||||
text = MD_MEDIA_RE.sub(caption, text)
|
||||
text = re.sub(r"<u>(.*?)</u>", r"\1", text, flags=re.I | re.S)
|
||||
text = re.sub(r"^#{1,6}\s+", "", text, flags=re.M)
|
||||
text = re.sub(r"\*\*(.+?)\*\*", r"\1", text)
|
||||
text = re.sub(r"__(.+?)__", r"\1", text)
|
||||
text = re.sub(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)", r"\1", text)
|
||||
text = re.sub(r"^\s*[-*+]\s+", "", text, flags=re.M)
|
||||
text = re.sub(r"^\s*\d+\.\s+", "", text, flags=re.M)
|
||||
text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text)
|
||||
text = re.sub(r"`([^`]+)`", r"\1", text)
|
||||
text = re.sub(r"\n{3,}", "\n\n", text)
|
||||
return text.strip()
|
||||
@@ -0,0 +1,110 @@
|
||||
"""Explicit journal draft generation. Never overwrites the current user entry."""
|
||||
from __future__ import annotations
|
||||
|
||||
from context_builder import assemble_text, build_internal_context
|
||||
from dialogue_store import StoreError, list_conversations_for_day, list_messages
|
||||
from engine import execute_prompt, load_active_prompt
|
||||
from journal_policy import require_explicit_generate, source_conversation_ids
|
||||
from identity_store import list_mappings
|
||||
from journal_body import clean_title
|
||||
from journal_shape import shape_journal
|
||||
from journal_store import current_draft, current_entries, get_day, insert_draft
|
||||
from writing_profile_store import compile_style_signals, remember_dialogue_style
|
||||
|
||||
|
||||
def _message_ids(profile_id: str, conversation_ids: list[str]) -> list[str]:
|
||||
ids: list[str] = []
|
||||
for conversation_id in conversation_ids:
|
||||
for message in list_messages(profile_id, conversation_id):
|
||||
ids.append(message["id"])
|
||||
return ids
|
||||
|
||||
|
||||
def _split_title(content: str) -> tuple[str, str]:
|
||||
text = (content or "").strip()
|
||||
if not text:
|
||||
return "", ""
|
||||
lines = text.splitlines()
|
||||
title = clean_title(lines[0])
|
||||
body = "\n".join(lines[1:]).strip() if len(lines) > 1 else text
|
||||
if not title or len(title) > 80:
|
||||
return "", text
|
||||
return title, body or text
|
||||
|
||||
|
||||
def generate_draft(
|
||||
profile_id: str,
|
||||
journal_day_id: str,
|
||||
conversation_ids: list[str] | None = None,
|
||||
include_existing: bool = False,
|
||||
explicit: bool = True,
|
||||
) -> dict:
|
||||
require_explicit_generate(explicit)
|
||||
day = get_day(profile_id, journal_day_id)
|
||||
day_conversations = list_conversations_for_day(profile_id, journal_day_id)
|
||||
selected = source_conversation_ids(
|
||||
conversation_ids,
|
||||
[item["id"] for item in day_conversations],
|
||||
)
|
||||
remember_dialogue_style(profile_id, exclude_conversation_ids=selected)
|
||||
existing_text = ""
|
||||
if include_existing:
|
||||
draft = current_draft(profile_id, journal_day_id)
|
||||
entries = current_entries(profile_id, journal_day_id)
|
||||
if draft:
|
||||
existing_text = draft.get("body") or ""
|
||||
elif entries:
|
||||
existing_text = entries[0].get("body") or ""
|
||||
context = build_internal_context(
|
||||
profile_id,
|
||||
space_id=day["space_id"],
|
||||
journal_day_id=journal_day_id,
|
||||
purpose="journal_generate",
|
||||
include_existing=include_existing,
|
||||
conversation_ids=selected,
|
||||
existing_text=existing_text,
|
||||
conversation_id=selected[0] if selected else None,
|
||||
)
|
||||
assembled = assemble_text(context)
|
||||
source_user = [
|
||||
message.get("body") or ""
|
||||
for conversation_id in selected
|
||||
for message in list_messages(profile_id, conversation_id)
|
||||
if message.get("role") == "user"
|
||||
]
|
||||
signals = compile_style_signals(source_user)
|
||||
profile = assembled.get("writing_profile") or ""
|
||||
if signals and signals not in profile:
|
||||
assembled["writing_profile"] = "\n\n".join(part for part in (profile, signals) if part)
|
||||
prompt = load_active_prompt("mvp.journal_generate")
|
||||
result = execute_prompt(
|
||||
prompt,
|
||||
profile_id,
|
||||
purpose="journal_generate",
|
||||
data_class="B",
|
||||
context=assembled,
|
||||
)
|
||||
title, body = _split_title(result.get("content") or "")
|
||||
person_labels = [
|
||||
(item.get("local_label") or "").strip()
|
||||
for item in list_mappings(profile_id)
|
||||
if (item.get("local_label") or "").strip()
|
||||
and str(item.get("token") or "").upper().lstrip("[").startswith("PERSON:")
|
||||
]
|
||||
title, body = shape_journal(title, body, source_user, person_labels=person_labels)
|
||||
before_entries = {item["id"]: item.get("current_version_id") for item in current_entries(profile_id, journal_day_id)}
|
||||
draft = insert_draft(
|
||||
profile_id,
|
||||
journal_day_id,
|
||||
title=title,
|
||||
body=body,
|
||||
source_conversation_ids=selected,
|
||||
source_message_ids=_message_ids(profile_id, selected),
|
||||
)
|
||||
after_entries = current_entries(profile_id, journal_day_id)
|
||||
for item in after_entries:
|
||||
previous = before_entries.get(item["id"])
|
||||
if previous is not None and previous != item.get("current_version_id"):
|
||||
raise StoreError("policy_violation", "Generate darf die Nutzerfassung nicht verändern", 500)
|
||||
draft["trace"] = result.get("trace")
|
||||
return draft
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Small write/decision boundary for the journal MVP. Not a policy engine."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
from conversation_signals import similar_enough
|
||||
|
||||
|
||||
class PolicyError(Exception):
|
||||
def __init__(self, code: str, message: str, status_code: int = 400):
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
ENTRY_ORIGINS = {"user_edit", "accepted_draft", "restore"}
|
||||
|
||||
|
||||
def require_explicit_generate(explicit: bool) -> None:
|
||||
if not explicit:
|
||||
raise PolicyError("generate_not_explicit", "Journal Drafts entstehen nur auf ausdrückliche Aktion.")
|
||||
|
||||
|
||||
def consolidation_offer(conversations: list[dict] | int) -> bool:
|
||||
"""Offer merge only when conversations look narratively compatible. User decides."""
|
||||
if isinstance(conversations, int):
|
||||
return False
|
||||
return similar_enough(conversations)
|
||||
|
||||
|
||||
def source_conversation_ids(explicit_ids: list[str] | None, day_conversation_ids: list[str]) -> list[str]:
|
||||
"""Missing selection never silently merges multiple conversations."""
|
||||
known = [item for item in day_conversation_ids if item]
|
||||
if explicit_ids:
|
||||
chosen = [item for item in explicit_ids if item in known]
|
||||
if not chosen:
|
||||
raise PolicyError("unknown_conversation", "Keine der angegebenen Conversations gehört zu diesem Tag.")
|
||||
return chosen
|
||||
if not known:
|
||||
raise PolicyError("no_source_dialogue", "Ohne Dialog gibt es nichts zu erzeugen.")
|
||||
return [known[-1]]
|
||||
|
||||
|
||||
def draft_must_not_touch_entry() -> None:
|
||||
return None
|
||||
|
||||
|
||||
def require_origin(origin: str) -> str:
|
||||
if origin not in ENTRY_ORIGINS:
|
||||
raise PolicyError("invalid_origin", "origin muss user_edit, accepted_draft oder restore sein")
|
||||
return origin
|
||||
@@ -0,0 +1,186 @@
|
||||
"""Local journal shape after the model. Does not invent content."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
NEW_PHASE = re.compile(
|
||||
r"^(?:Heute|Gestern|Morgens|Mittags|Abends|Nachts|Danach|Später|Dann|Zuerst|"
|
||||
r"Schließlich|Irgendwann|Unterwegs|Zurück|"
|
||||
r"Um\s+\d|Nach\s+(?:ca\.|einer|einem|dem|der|kurzer|weiteren)|"
|
||||
r"Als\s+(?:wir|ich)\b)",
|
||||
re.I,
|
||||
)
|
||||
_LETTER = r"A-Za-zÄÖÜäöüß"
|
||||
_WORD = re.compile(rf"[{_LETTER}]+")
|
||||
DATIVE_PREPS = {
|
||||
"mit", "bei", "von", "zu", "nach", "aus", "außer", "gegenüber", "gemäß",
|
||||
}
|
||||
NOM_CUES = {
|
||||
"dass", "daß", "ob", "weil", "wenn", "als", "während", "obwohl", "damit",
|
||||
"nachdem", "bevor", "sodass", "sofern",
|
||||
}
|
||||
THING_GOVERNORS = {
|
||||
"esse", "essen", "isst", "aß", "aßest", "gegessen",
|
||||
"koche", "kochen", "kochte", "gekocht",
|
||||
"trinke", "trinken", "trank", "getrunken",
|
||||
"kaufe", "kaufen", "kaufte", "gekauft",
|
||||
"bestelle", "bestellen", "bestellte",
|
||||
"hole", "holen", "holte",
|
||||
}
|
||||
FEM_HINTS = {"sie", "ihr", "ihre", "ihrem", "ihren", "ihrer", "ihres"}
|
||||
MASC_HINTS = {"er", "ihn", "ihm", "seine", "seinem", "seinen", "seiner", "seines"}
|
||||
KEEP_NAMES_PER_PARAGRAPH = 2
|
||||
|
||||
|
||||
def _strip_roles(text: str) -> str:
|
||||
lines = [re.sub(r"^(?:user|assistant):\s*", "", line, flags=re.I) for line in (text or "").splitlines()]
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def _norm(text: str) -> str:
|
||||
return re.sub(r"[^\w\s]+", " ", re.sub(r"\s+", " ", _strip_roles(text).lower())).strip()
|
||||
|
||||
|
||||
def _turns(user_bodies: list[str]) -> list[str]:
|
||||
return [re.sub(r"\s+", " ", item).strip() for item in user_bodies if (item or "").strip()]
|
||||
|
||||
|
||||
def is_verbatim_join(body: str, user_bodies: list[str]) -> bool:
|
||||
turns = _turns(user_bodies)
|
||||
if not turns or not (body or "").strip():
|
||||
return False
|
||||
got = _norm(body)
|
||||
return got == _norm(" ".join(turns)) or got == _norm("\n\n".join(turns))
|
||||
|
||||
|
||||
def paragraphize(body: str) -> str:
|
||||
text = (body or "").strip()
|
||||
if "\n\n" in text:
|
||||
return text
|
||||
sentences = [item.strip() for item in re.split(r"(?<=[.!?])\s+", text) if item.strip()]
|
||||
if len(sentences) < 4:
|
||||
return text
|
||||
chunks: list[str] = []
|
||||
current: list[str] = []
|
||||
for sentence in sentences:
|
||||
split = bool(current) and (NEW_PHASE.match(sentence) or len(current) >= 3)
|
||||
if split:
|
||||
chunks.append(" ".join(current))
|
||||
current = [sentence]
|
||||
else:
|
||||
current.append(sentence)
|
||||
if current:
|
||||
chunks.append(" ".join(current))
|
||||
return "\n\n".join(chunks)
|
||||
|
||||
|
||||
def _words_before(text: str, index: int, n: int = 4) -> list[str]:
|
||||
return [word.lower() for word in _WORD.findall(text[:index])[-n:]]
|
||||
|
||||
|
||||
def _words_after(text: str, index: int, n: int = 3) -> list[str]:
|
||||
return [word.lower() for word in _WORD.findall(text[index:])[:n]]
|
||||
|
||||
|
||||
def _is_thing_mention(text: str, start: int, end: int) -> bool:
|
||||
prev = _words_before(text, start)
|
||||
nxt = _words_after(text, end)
|
||||
return any(word in THING_GOVERNORS for word in prev + nxt)
|
||||
|
||||
|
||||
def _sentence_start(text: str, index: int) -> bool:
|
||||
prefix = text[:index].rstrip()
|
||||
return not prefix or prefix[-1] in ".!?\n"
|
||||
|
||||
|
||||
def infer_person_gender(label: str, source_texts: list[str]) -> str | None:
|
||||
blob = " ".join(source_texts or [])
|
||||
if not label or not blob:
|
||||
return None
|
||||
pattern = re.compile(rf"(?<![{_LETTER}]){re.escape(label)}(?![{_LETTER}])", re.I)
|
||||
first = pattern.search(blob)
|
||||
if not first:
|
||||
return None
|
||||
after = [word.lower() for word in _WORD.findall(blob[first.start():])]
|
||||
fem = sum(1 for word in after if word in FEM_HINTS)
|
||||
masc = sum(1 for word in after if word in MASC_HINTS)
|
||||
if fem > masc:
|
||||
return "f"
|
||||
if masc > fem:
|
||||
return "m"
|
||||
return None
|
||||
|
||||
|
||||
def _pronoun(gender: str, prev: str, sentence_start: bool) -> str:
|
||||
dative = prev in DATIVE_PREPS
|
||||
if gender == "f":
|
||||
form = "ihr" if dative else "sie"
|
||||
elif sentence_start or prev in NOM_CUES:
|
||||
form = "er"
|
||||
elif dative:
|
||||
form = "ihm"
|
||||
else:
|
||||
form = "ihn"
|
||||
if sentence_start:
|
||||
return form[:1].upper() + form[1:]
|
||||
return form
|
||||
|
||||
|
||||
def naturalize_person_mentions(body: str, labels: list[str], source_texts: list[str]) -> str:
|
||||
"""After demask: keep the name once or twice per passage, then pronouns. No new facts."""
|
||||
text = body or ""
|
||||
people = []
|
||||
for label in sorted({item.strip() for item in (labels or []) if (item or "").strip()}, key=len, reverse=True):
|
||||
gender = infer_person_gender(label, source_texts)
|
||||
if gender:
|
||||
people.append((label, gender))
|
||||
if not people:
|
||||
return text
|
||||
paragraphs = re.split(r"(\n\n+)", text)
|
||||
out: list[str] = []
|
||||
for block in paragraphs:
|
||||
if not block or block.startswith("\n"):
|
||||
out.append(block)
|
||||
continue
|
||||
piece = block
|
||||
for label, gender in people:
|
||||
pattern = re.compile(rf"(?<![{_LETTER}]){re.escape(label)}(?![{_LETTER}])", re.I)
|
||||
matches = [
|
||||
match
|
||||
for match in pattern.finditer(piece)
|
||||
if not _is_thing_mention(piece, match.start(), match.end())
|
||||
]
|
||||
if len(matches) <= KEEP_NAMES_PER_PARAGRAPH:
|
||||
continue
|
||||
replacements: list[tuple[int, int, str]] = []
|
||||
kept = 0
|
||||
for match in matches:
|
||||
kept += 1
|
||||
if kept <= KEEP_NAMES_PER_PARAGRAPH:
|
||||
continue
|
||||
prev = (_words_before(piece, match.start(), 1) or [""])[0]
|
||||
form = _pronoun(gender, prev, _sentence_start(piece, match.start()))
|
||||
replacements.append((match.start(), match.end(), form))
|
||||
for start, end, form in reversed(replacements):
|
||||
piece = piece[:start] + form + piece[end:]
|
||||
out.append(piece)
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def shape_journal(
|
||||
title: str,
|
||||
body: str,
|
||||
user_bodies: list[str],
|
||||
person_labels: list[str] | None = None,
|
||||
) -> tuple[str, str]:
|
||||
turns = _turns(user_bodies)
|
||||
text = (body or "").strip()
|
||||
heading = (title or "").strip()
|
||||
text = _strip_roles(text)
|
||||
if is_verbatim_join(text, turns) and len(turns) > 1:
|
||||
text = "\n\n".join(turns)
|
||||
else:
|
||||
text = paragraphize(text)
|
||||
if person_labels:
|
||||
text = naturalize_person_mentions(text, person_labels, turns)
|
||||
return heading, text
|
||||
@@ -0,0 +1,474 @@
|
||||
"""Journal Day, Draft, Entry and Space product store. Source messages stay in dialogue_store."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from dialogue_store import (
|
||||
StoreError,
|
||||
create_conversation,
|
||||
create_space,
|
||||
get_space,
|
||||
list_conversations_for_day,
|
||||
list_spaces,
|
||||
rename_space,
|
||||
start_usage_session,
|
||||
)
|
||||
from db import _insert_source_refs, get_db, row_to_dict
|
||||
from journal_policy import consolidation_offer, require_origin
|
||||
from journal_body import clean_title, to_markdown
|
||||
|
||||
DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
||||
SCRATCH_MAX_ITEMS = 40
|
||||
SCRATCH_MAX_TEXT = 400
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
|
||||
|
||||
|
||||
def _parse_ids(raw: str | None) -> list[str]:
|
||||
if not raw:
|
||||
return []
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
return []
|
||||
if not isinstance(data, list):
|
||||
return []
|
||||
return [str(item) for item in data]
|
||||
|
||||
|
||||
def _load_source_ids(conn, table: str, owner_col: str, owner_id: str, row: dict | None) -> tuple[list[str], list[str]]:
|
||||
refs = conn.execute(
|
||||
f"""
|
||||
SELECT source_kind, source_id, sort_order
|
||||
FROM {table}
|
||||
WHERE {owner_col} = ?
|
||||
ORDER BY source_kind, sort_order
|
||||
""",
|
||||
(owner_id,),
|
||||
).fetchall()
|
||||
conversations: list[str] = []
|
||||
messages: list[str] = []
|
||||
for item in refs:
|
||||
kind = item["source_kind"]
|
||||
source_id = item["source_id"]
|
||||
if kind == "conversation":
|
||||
conversations.append(source_id)
|
||||
elif kind == "message":
|
||||
messages.append(source_id)
|
||||
if conversations or messages:
|
||||
return conversations, messages
|
||||
row = row or {}
|
||||
return _parse_ids(row.get("source_conversation_ids")), _parse_ids(row.get("source_message_ids"))
|
||||
|
||||
|
||||
def _attach_sources(conn, table: str, owner_col: str, owner_id: str, row: dict | None) -> dict | None:
|
||||
if not row:
|
||||
return None
|
||||
conversations, messages = _load_source_ids(conn, table, owner_col, owner_id, row)
|
||||
row["source_conversation_ids"] = conversations
|
||||
row["source_message_ids"] = messages
|
||||
row["title"] = clean_title(row.get("title") or "")
|
||||
return row
|
||||
|
||||
|
||||
def _owned(conn, table: str, record_id: str, profile_id: str) -> dict | None:
|
||||
return row_to_dict(
|
||||
conn.execute(
|
||||
f"SELECT * FROM {table} WHERE id = ? AND profile_id = ?",
|
||||
(record_id, profile_id),
|
||||
).fetchone()
|
||||
)
|
||||
|
||||
|
||||
def _decode_draft(conn, row: dict | None) -> dict | None:
|
||||
if not row:
|
||||
return None
|
||||
return _attach_sources(conn, "journal_draft_source_refs", "draft_id", row["id"], row)
|
||||
|
||||
|
||||
def _decode_version(conn, row: dict | None) -> dict | None:
|
||||
if not row:
|
||||
return None
|
||||
owner_id = row.get("current_version_id") or row.get("id")
|
||||
return _attach_sources(conn, "journal_entry_version_source_refs", "version_id", owner_id, row)
|
||||
|
||||
|
||||
def create_user_space(profile_id: str, title: str) -> dict:
|
||||
if not (title or "").strip():
|
||||
raise StoreError("empty_title", "Space-Name darf nicht leer sein")
|
||||
return create_space(profile_id, title.strip(), visibility="user")
|
||||
|
||||
|
||||
def list_user_spaces(profile_id: str) -> list[dict]:
|
||||
return list_spaces(profile_id, visibility="user")
|
||||
|
||||
|
||||
def get_user_space(profile_id: str, space_id: str) -> dict:
|
||||
space = get_space(profile_id, space_id)
|
||||
if space.get("visibility") != "user":
|
||||
raise StoreError("not_found", "Reflection Space nicht gefunden", 404)
|
||||
return space
|
||||
|
||||
|
||||
def rename_user_space(profile_id: str, space_id: str, title: str) -> dict:
|
||||
get_user_space(profile_id, space_id)
|
||||
return rename_space(profile_id, space_id, title)
|
||||
|
||||
|
||||
def get_or_create_day(profile_id: str, space_id: str, calendar_date: str) -> dict:
|
||||
get_user_space(profile_id, space_id)
|
||||
if not DATE_RE.match(calendar_date or ""):
|
||||
raise StoreError("invalid_date", "calendar_date muss YYYY-MM-DD sein")
|
||||
with get_db() as conn:
|
||||
existing = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT * FROM journal_days
|
||||
WHERE profile_id = ? AND space_id = ? AND calendar_date = ?
|
||||
""",
|
||||
(profile_id, space_id, calendar_date),
|
||||
).fetchone()
|
||||
)
|
||||
if existing:
|
||||
return existing
|
||||
day_id = str(uuid.uuid4())
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_days (id, profile_id, space_id, calendar_date)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(day_id, profile_id, space_id, calendar_date),
|
||||
)
|
||||
return row_to_dict(conn.execute("SELECT * FROM journal_days WHERE id = ?", (day_id,)).fetchone())
|
||||
|
||||
|
||||
def get_day(profile_id: str, journal_day_id: str) -> dict:
|
||||
with get_db() as conn:
|
||||
day = _owned(conn, "journal_days", journal_day_id, profile_id)
|
||||
if not day:
|
||||
raise StoreError("not_found", "Journal Day nicht gefunden", 404)
|
||||
return day
|
||||
|
||||
|
||||
def list_days(profile_id: str, space_id: str) -> list[dict]:
|
||||
get_user_space(profile_id, space_id)
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT d.*,
|
||||
(SELECT COUNT(*) FROM conversations c
|
||||
WHERE c.journal_day_id = d.id AND c.profile_id = d.profile_id) AS conversation_count,
|
||||
(SELECT COUNT(*) FROM journal_entries e
|
||||
WHERE e.journal_day_id = d.id AND e.profile_id = d.profile_id AND e.deleted_at IS NULL
|
||||
) AS entry_count
|
||||
FROM journal_days d
|
||||
WHERE d.profile_id = ? AND d.space_id = ?
|
||||
ORDER BY d.calendar_date DESC
|
||||
""",
|
||||
(profile_id, space_id),
|
||||
).fetchall()
|
||||
result = []
|
||||
for row in rows:
|
||||
item = row_to_dict(row)
|
||||
entry = current_entries(profile_id, item["id"])
|
||||
item["entry_title"] = (entry[-1].get("title") if entry else "") or ""
|
||||
item["entry_id"] = entry[-1]["id"] if entry else None
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
def create_day_conversation(profile_id: str, journal_day_id: str, title: str = "") -> dict:
|
||||
day = get_day(profile_id, journal_day_id)
|
||||
usage = start_usage_session(profile_id, intent="journal")
|
||||
return create_conversation(
|
||||
profile_id,
|
||||
usage_session_id=usage["id"],
|
||||
title=title or "Gespräch",
|
||||
space_id=day["space_id"],
|
||||
journal_day_id=day["id"],
|
||||
)
|
||||
|
||||
|
||||
def current_draft(profile_id: str, journal_day_id: str) -> dict | None:
|
||||
get_day(profile_id, journal_day_id)
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT * FROM journal_drafts
|
||||
WHERE profile_id = ? AND journal_day_id = ? AND superseded_at IS NULL
|
||||
ORDER BY created DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
(profile_id, journal_day_id),
|
||||
).fetchone()
|
||||
)
|
||||
return _decode_draft(conn, row)
|
||||
|
||||
|
||||
def insert_draft(
|
||||
profile_id: str,
|
||||
journal_day_id: str,
|
||||
title: str,
|
||||
body: str,
|
||||
source_conversation_ids: list[str],
|
||||
source_message_ids: list[str],
|
||||
) -> dict:
|
||||
get_day(profile_id, journal_day_id)
|
||||
draft_id = str(uuid.uuid4())
|
||||
as_of = _now()
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE journal_drafts
|
||||
SET superseded_at = ?
|
||||
WHERE profile_id = ? AND journal_day_id = ? AND superseded_at IS NULL
|
||||
""",
|
||||
(as_of, profile_id, journal_day_id),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_drafts
|
||||
(id, profile_id, journal_day_id, title, body,
|
||||
source_conversation_ids, source_message_ids, as_of)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
draft_id,
|
||||
profile_id,
|
||||
journal_day_id,
|
||||
clean_title(title),
|
||||
body or "",
|
||||
"[]",
|
||||
"[]",
|
||||
as_of,
|
||||
),
|
||||
)
|
||||
_insert_source_refs(
|
||||
conn,
|
||||
"journal_draft_source_refs",
|
||||
"draft_id",
|
||||
draft_id,
|
||||
profile_id,
|
||||
source_conversation_ids or [],
|
||||
source_message_ids or [],
|
||||
)
|
||||
row = row_to_dict(conn.execute("SELECT * FROM journal_drafts WHERE id = ?", (draft_id,)).fetchone())
|
||||
return _decode_draft(conn, row)
|
||||
|
||||
|
||||
def current_entries(profile_id: str, journal_day_id: str) -> list[dict]:
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT e.*, v.title, v.body, v.origin, v.source_conversation_ids, v.source_message_ids, v.created AS version_created
|
||||
FROM journal_entries e
|
||||
LEFT JOIN journal_entry_versions v ON v.id = e.current_version_id
|
||||
WHERE e.profile_id = ? AND e.journal_day_id = ? AND e.deleted_at IS NULL
|
||||
ORDER BY e.created
|
||||
""",
|
||||
(profile_id, journal_day_id),
|
||||
).fetchall()
|
||||
return [_decode_version(conn, row_to_dict(row)) for row in rows]
|
||||
|
||||
|
||||
def get_entry(profile_id: str, entry_id: str, include_deleted: bool = False) -> dict:
|
||||
with get_db() as conn:
|
||||
entry = _owned(conn, "journal_entries", entry_id, profile_id)
|
||||
if not entry:
|
||||
raise StoreError("not_found", "Journal Entry nicht gefunden", 404)
|
||||
if entry.get("deleted_at") and not include_deleted:
|
||||
raise StoreError("not_found", "Journal Entry nicht gefunden", 404)
|
||||
version = None
|
||||
if entry.get("current_version_id"):
|
||||
version = _decode_version(
|
||||
conn,
|
||||
row_to_dict(
|
||||
conn.execute(
|
||||
"SELECT * FROM journal_entry_versions WHERE id = ? AND profile_id = ?",
|
||||
(entry["current_version_id"], profile_id),
|
||||
).fetchone()
|
||||
),
|
||||
)
|
||||
entry["version"] = version
|
||||
if version:
|
||||
entry["title"] = version.get("title") or ""
|
||||
entry["body"] = version.get("body") or ""
|
||||
entry["origin"] = version.get("origin")
|
||||
entry["source_conversation_ids"] = version.get("source_conversation_ids") or []
|
||||
entry["source_message_ids"] = version.get("source_message_ids") or []
|
||||
return entry
|
||||
|
||||
|
||||
def list_versions(profile_id: str, entry_id: str) -> list[dict]:
|
||||
get_entry(profile_id, entry_id, include_deleted=True)
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT * FROM journal_entry_versions
|
||||
WHERE profile_id = ? AND entry_id = ?
|
||||
ORDER BY created
|
||||
""",
|
||||
(profile_id, entry_id),
|
||||
).fetchall()
|
||||
return [_decode_version(conn, row_to_dict(row)) for row in rows]
|
||||
|
||||
|
||||
def save_entry(
|
||||
profile_id: str,
|
||||
journal_day_id: str,
|
||||
title: str,
|
||||
body: str,
|
||||
origin: str,
|
||||
entry_id: str | None = None,
|
||||
source_conversation_ids: list[str] | None = None,
|
||||
source_message_ids: list[str] | None = None,
|
||||
) -> dict:
|
||||
require_origin(origin)
|
||||
day = get_day(profile_id, journal_day_id)
|
||||
version_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
if entry_id:
|
||||
entry = _owned(conn, "journal_entries", entry_id, profile_id)
|
||||
if not entry or entry.get("deleted_at"):
|
||||
raise StoreError("not_found", "Journal Entry nicht gefunden", 404)
|
||||
if entry["journal_day_id"] != journal_day_id:
|
||||
raise StoreError("assignment_mismatch", "Entry gehört zu einem anderen Journal Day")
|
||||
else:
|
||||
entry_id = str(uuid.uuid4())
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_entries (id, profile_id, journal_day_id, space_id)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(entry_id, profile_id, journal_day_id, day["space_id"]),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_entry_versions
|
||||
(id, profile_id, entry_id, title, body, source_conversation_ids, source_message_ids, origin, created)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
version_id,
|
||||
profile_id,
|
||||
entry_id,
|
||||
clean_title(title),
|
||||
to_markdown(body or ""),
|
||||
"[]",
|
||||
"[]",
|
||||
origin,
|
||||
_now(),
|
||||
),
|
||||
)
|
||||
_insert_source_refs(
|
||||
conn,
|
||||
"journal_entry_version_source_refs",
|
||||
"version_id",
|
||||
version_id,
|
||||
profile_id,
|
||||
source_conversation_ids or [],
|
||||
source_message_ids or [],
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE journal_entries
|
||||
SET current_version_id = ?, updated = datetime('now'), deleted_at = NULL
|
||||
WHERE id = ? AND profile_id = ?
|
||||
""",
|
||||
(version_id, entry_id, profile_id),
|
||||
)
|
||||
return get_entry(profile_id, entry_id)
|
||||
|
||||
|
||||
def restore_entry(profile_id: str, entry_id: str, version_id: str) -> dict:
|
||||
entry = get_entry(profile_id, entry_id)
|
||||
with get_db() as conn:
|
||||
version = row_to_dict(
|
||||
conn.execute(
|
||||
"""
|
||||
SELECT * FROM journal_entry_versions
|
||||
WHERE id = ? AND entry_id = ? AND profile_id = ?
|
||||
""",
|
||||
(version_id, entry_id, profile_id),
|
||||
).fetchone()
|
||||
)
|
||||
if not version:
|
||||
raise StoreError("not_found", "Version nicht gefunden", 404)
|
||||
decoded = _decode_version(conn, version)
|
||||
return save_entry(
|
||||
profile_id,
|
||||
entry["journal_day_id"],
|
||||
decoded.get("title") or "",
|
||||
decoded.get("body") or "",
|
||||
origin="restore",
|
||||
entry_id=entry_id,
|
||||
source_conversation_ids=decoded.get("source_conversation_ids") or [],
|
||||
source_message_ids=decoded.get("source_message_ids") or [],
|
||||
)
|
||||
|
||||
|
||||
def soft_delete_entry(profile_id: str, entry_id: str) -> dict:
|
||||
get_entry(profile_id, entry_id)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE journal_entries SET deleted_at = ?, updated = datetime('now') WHERE id = ? AND profile_id = ?",
|
||||
(_now(), entry_id, profile_id),
|
||||
)
|
||||
return {"id": entry_id, "deleted": True}
|
||||
|
||||
|
||||
def _parse_scratch(raw) -> list[dict]:
|
||||
if isinstance(raw, list):
|
||||
data = raw
|
||||
else:
|
||||
try:
|
||||
data = json.loads(raw or "[]")
|
||||
except json.JSONDecodeError:
|
||||
data = []
|
||||
if not isinstance(data, list):
|
||||
return []
|
||||
items: list[dict] = []
|
||||
for item in data[:SCRATCH_MAX_ITEMS]:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
text = str(item.get("text") or "")[:SCRATCH_MAX_TEXT]
|
||||
items.append(
|
||||
{
|
||||
"id": str(item.get("id") or uuid.uuid4()),
|
||||
"text": text,
|
||||
"done": bool(item.get("done")),
|
||||
}
|
||||
)
|
||||
return items
|
||||
|
||||
|
||||
def save_day_scratch(profile_id: str, journal_day_id: str, items: list[dict]) -> list[dict]:
|
||||
get_day(profile_id, journal_day_id)
|
||||
cleaned = _parse_scratch(items)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE journal_days SET scratch_json = ?, updated = datetime('now') WHERE id = ? AND profile_id = ?",
|
||||
(json.dumps(cleaned, ensure_ascii=False), journal_day_id, profile_id),
|
||||
)
|
||||
return cleaned
|
||||
|
||||
|
||||
def day_payload(profile_id: str, journal_day_id: str) -> dict:
|
||||
day = get_day(profile_id, journal_day_id)
|
||||
space = get_user_space(profile_id, day["space_id"])
|
||||
conversations = list_conversations_for_day(profile_id, journal_day_id)
|
||||
return {
|
||||
"day": {key: value for key, value in day.items() if key != "scratch_json"},
|
||||
"space": space,
|
||||
"conversations": conversations,
|
||||
"current_draft": current_draft(profile_id, journal_day_id),
|
||||
"entries": current_entries(profile_id, journal_day_id),
|
||||
"scratch": _parse_scratch(day.get("scratch_json")),
|
||||
"consolidation_offer": consolidation_offer(conversations),
|
||||
}
|
||||
+6
-1
@@ -1,9 +1,13 @@
|
||||
from env_loader import load_env_file
|
||||
|
||||
load_env_file()
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from db import init_db
|
||||
import data_layer_dialogue # noqa: F401
|
||||
from routers import admin, auth, dialogue, placeholders, prompts, subscription, users
|
||||
from routers import admin, auth, dialogue, journal, placeholders, prompts, subscription, users
|
||||
from version import APP_VERSION
|
||||
|
||||
app = FastAPI(title="Kanshō", version=APP_VERSION)
|
||||
@@ -19,6 +23,7 @@ app.add_middleware(
|
||||
app.include_router(auth.router)
|
||||
app.include_router(users.router)
|
||||
app.include_router(dialogue.router)
|
||||
app.include_router(journal.router)
|
||||
app.include_router(prompts.router)
|
||||
app.include_router(placeholders.router)
|
||||
app.include_router(subscription.router)
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
"""Local image and video attachments for journal entries.
|
||||
|
||||
Files never leave the trusted zone. Images have EXIF/text chunks stripped.
|
||||
Video containers are stored as-is; GPS in video metadata is not stripped yet.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from db import DATA_DIR, get_db, row_to_dict
|
||||
from dialogue_store import StoreError
|
||||
from journal_store import get_entry
|
||||
|
||||
IMAGE_TYPES = {
|
||||
"image/jpeg": ".jpg",
|
||||
"image/jpg": ".jpg",
|
||||
"image/png": ".png",
|
||||
"image/gif": ".gif",
|
||||
"image/webp": ".webp",
|
||||
}
|
||||
VIDEO_TYPES = {
|
||||
"video/mp4": ".mp4",
|
||||
"video/webm": ".webm",
|
||||
"video/quicktime": ".mov",
|
||||
}
|
||||
ALLOWED_TYPES = {**IMAGE_TYPES, **VIDEO_TYPES}
|
||||
IMAGE_MAX_BYTES = 8 * 1024 * 1024
|
||||
VIDEO_MAX_BYTES = 64 * 1024 * 1024
|
||||
MEDIA_ROOT = DATA_DIR / "media"
|
||||
MAX_BYTES = IMAGE_MAX_BYTES # tests and older imports
|
||||
|
||||
|
||||
def _owned(conn, table: str, record_id: str, profile_id: str) -> dict | None:
|
||||
return row_to_dict(
|
||||
conn.execute(f"SELECT * FROM {table} WHERE id = ? AND profile_id = ?", (record_id, profile_id)).fetchone()
|
||||
)
|
||||
|
||||
|
||||
def media_kind(content_type: str) -> str:
|
||||
kind = (content_type or "").lower()
|
||||
if kind in VIDEO_TYPES or kind.startswith("video/"):
|
||||
return "video"
|
||||
return "image"
|
||||
|
||||
|
||||
def public_media(row: dict | None) -> dict | None:
|
||||
if not row:
|
||||
return None
|
||||
out = dict(row)
|
||||
out["kind"] = media_kind(row.get("content_type") or "")
|
||||
return out
|
||||
|
||||
|
||||
def sniff_content_type(data: bytes, declared: str) -> str:
|
||||
declared = (declared or "").lower().split(";")[0].strip()
|
||||
if data.startswith(b"\x89PNG\r\n\x1a\n"):
|
||||
return "image/png"
|
||||
if data[:3] == b"\xff\xd8\xff":
|
||||
return "image/jpeg"
|
||||
if data[:6] in {b"GIF87a", b"GIF89a"}:
|
||||
return "image/gif"
|
||||
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
||||
return "image/webp"
|
||||
if len(data) >= 12 and data[4:8] == b"ftyp":
|
||||
brand = data[8:12]
|
||||
if brand in {b"qt ", b"moov"}:
|
||||
return "video/quicktime"
|
||||
return "video/mp4"
|
||||
if data[:4] == b"\x1a\x45\xdf\xa3":
|
||||
return "video/webm"
|
||||
if declared in ALLOWED_TYPES:
|
||||
return declared
|
||||
return declared
|
||||
|
||||
|
||||
def strip_jpeg_exif(data: bytes) -> bytes:
|
||||
if len(data) < 4 or data[0:2] != b"\xff\xd8":
|
||||
return data
|
||||
out = bytearray(b"\xff\xd8")
|
||||
i = 2
|
||||
while i + 1 < len(data):
|
||||
if data[i] != 0xFF:
|
||||
out.extend(data[i:])
|
||||
break
|
||||
marker = data[i + 1]
|
||||
if marker == 0xDA:
|
||||
out.extend(data[i:])
|
||||
break
|
||||
if marker == 0xD9:
|
||||
out.extend(data[i : i + 2])
|
||||
break
|
||||
if marker in {0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0x01}:
|
||||
out.extend(data[i : i + 2])
|
||||
i += 2
|
||||
continue
|
||||
if i + 4 > len(data):
|
||||
break
|
||||
length = int.from_bytes(data[i + 2 : i + 4], "big")
|
||||
block_end = i + 2 + length
|
||||
if marker == 0xE1 and data[i + 4 : i + 10] == b"Exif\x00\x00":
|
||||
i = block_end
|
||||
continue
|
||||
out.extend(data[i:block_end])
|
||||
i = block_end
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def strip_png_metadata(data: bytes) -> bytes:
|
||||
signature = b"\x89PNG\r\n\x1a\n"
|
||||
if not data.startswith(signature):
|
||||
return data
|
||||
out = bytearray(signature)
|
||||
drop = {b"eXIf", b"eXif", b"tEXt", b"zTXt", b"iTXt", b"tIME"}
|
||||
i = 8
|
||||
while i + 12 <= len(data):
|
||||
length = int.from_bytes(data[i : i + 4], "big")
|
||||
chunk_type = data[i + 4 : i + 8]
|
||||
end = i + 12 + length
|
||||
if chunk_type not in drop:
|
||||
out.extend(data[i:end])
|
||||
if chunk_type == b"IEND":
|
||||
break
|
||||
i = end
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def prepare_image(data: bytes, content_type: str) -> bytes:
|
||||
if content_type in {"image/jpeg", "image/jpg"}:
|
||||
return strip_jpeg_exif(data)
|
||||
if content_type == "image/png":
|
||||
return strip_png_metadata(data)
|
||||
return data
|
||||
|
||||
|
||||
def save_media(profile_id: str, entry_id: str, filename: str, content_type: str, data: bytes) -> dict:
|
||||
entry = get_entry(profile_id, entry_id)
|
||||
if not data:
|
||||
raise StoreError("empty_media", "Datei ist leer")
|
||||
kind = sniff_content_type(data, content_type)
|
||||
if kind not in ALLOWED_TYPES:
|
||||
raise StoreError("unsupported_media", "Nur Bilder (JPEG, PNG, GIF, WebP) und Videos (MP4, WebM, MOV) sind erlaubt")
|
||||
is_video = kind in VIDEO_TYPES
|
||||
limit = VIDEO_MAX_BYTES if is_video else IMAGE_MAX_BYTES
|
||||
if len(data) > limit:
|
||||
label = "Video" if is_video else "Bild"
|
||||
mb = limit // (1024 * 1024)
|
||||
raise StoreError("media_too_large", f"{label} ist größer als {mb} MB")
|
||||
payload = data if is_video else prepare_image(data, kind)
|
||||
asset_id = str(uuid.uuid4())
|
||||
ext = ALLOWED_TYPES[kind]
|
||||
rel_path = f"{profile_id}/{asset_id}{ext}"
|
||||
dest = MEDIA_ROOT / rel_path
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_bytes(payload)
|
||||
prefix = "video" if is_video else "image"
|
||||
safe_name = Path(filename or f"{prefix}{ext}").name
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO media_assets
|
||||
(id, profile_id, entry_id, entry_version_id, filename, content_type, rel_path)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(asset_id, profile_id, entry_id, entry.get("current_version_id"), safe_name, kind, rel_path),
|
||||
)
|
||||
row = row_to_dict(conn.execute("SELECT * FROM media_assets WHERE id = ?", (asset_id,)).fetchone())
|
||||
return public_media(row)
|
||||
|
||||
|
||||
def save_image(profile_id: str, entry_id: str, filename: str, content_type: str, data: bytes) -> dict:
|
||||
return save_media(profile_id, entry_id, filename, content_type, data)
|
||||
|
||||
|
||||
def list_media(profile_id: str, entry_id: str) -> list[dict]:
|
||||
get_entry(profile_id, entry_id)
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT * FROM media_assets
|
||||
WHERE profile_id = ? AND entry_id = ?
|
||||
ORDER BY created
|
||||
""",
|
||||
(profile_id, entry_id),
|
||||
).fetchall()
|
||||
return [public_media(row_to_dict(row)) for row in rows]
|
||||
|
||||
|
||||
def get_media(profile_id: str, media_id: str) -> tuple[dict, Path]:
|
||||
with get_db() as conn:
|
||||
row = _owned(conn, "media_assets", media_id, profile_id)
|
||||
if not row:
|
||||
raise StoreError("not_found", "Medium nicht gefunden", 404)
|
||||
path = MEDIA_ROOT / row["rel_path"]
|
||||
if not path.is_file():
|
||||
raise StoreError("not_found", "Mediendatei fehlt", 404)
|
||||
return public_media(row), path
|
||||
|
||||
|
||||
def delete_media(profile_id: str, media_id: str) -> dict:
|
||||
row, path = get_media(profile_id, media_id)
|
||||
with get_db() as conn:
|
||||
conn.execute("DELETE FROM media_assets WHERE id = ? AND profile_id = ?", (media_id, profile_id))
|
||||
try:
|
||||
path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
pass
|
||||
return {"id": media_id, "deleted": True}
|
||||
@@ -0,0 +1,82 @@
|
||||
"""MVP context keys. Values come from the Context Builder, never from Python prompt strings."""
|
||||
from __future__ import annotations
|
||||
|
||||
from placeholders import Placeholder, register
|
||||
|
||||
|
||||
def _from_ctx(key: str):
|
||||
return lambda ctx: (ctx or {}).get(key) or ""
|
||||
|
||||
|
||||
register(
|
||||
Placeholder(
|
||||
key="dialogue_context",
|
||||
description="Vom Context Builder zusammengestellter Dialog- und Space-Kontext.",
|
||||
data_class="B",
|
||||
resolver=_from_ctx("dialogue_context"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="writing_profile",
|
||||
description="Aktueller Writing-Profile-Brief für Journal-Artefakte. Kein KI-Draft, keine Dialogsteuerung.",
|
||||
data_class="B",
|
||||
resolver=_from_ctx("writing_profile"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="interaction_hint",
|
||||
description="Interaction Profile plus aktueller Dialogue State. Kein Writing Profile, keine Ableitung aus Nicht-Widersprechen.",
|
||||
data_class="C",
|
||||
resolver=_from_ctx("interaction_hint"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="space_title",
|
||||
description="Titel des aktuellen Reflection Space.",
|
||||
data_class="C",
|
||||
resolver=_from_ctx("space_title"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="existing_text",
|
||||
description="Bestehender Draft oder Entry, nur bei ausdrücklichem Regenerieren.",
|
||||
data_class="B",
|
||||
resolver=_from_ctx("existing_text"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="source_text",
|
||||
description="Klartext für Entity Detection. Verlässt die Trusted Zone nur über den Detect-Provider.",
|
||||
data_class="B",
|
||||
resolver=_from_ctx("source_text"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="known_labels",
|
||||
description="Bereits gemappte lokale Labels, damit Detection sie nicht neu vorschlägt.",
|
||||
data_class="C",
|
||||
resolver=_from_ctx("known_labels"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="register_hint",
|
||||
description="Lokales Erzählregister für den Dialogzug. Kein zweiter Modellaufruf.",
|
||||
data_class="C",
|
||||
resolver=_from_ctx("register_hint"),
|
||||
)
|
||||
)
|
||||
register(
|
||||
Placeholder(
|
||||
key="review_package",
|
||||
description="Gebündeltes Profile-Review-Paket (derselbe Vertrag für API und Copy/Paste).",
|
||||
data_class="B",
|
||||
resolver=_from_ctx("review_package"),
|
||||
)
|
||||
)
|
||||
+258
-13
@@ -1,10 +1,21 @@
|
||||
"""Local privacy gateway stub. Personal LLM egress is not allowed to skip this layer."""
|
||||
"""Local privacy gateway. Personal generative egress is not allowed to skip this layer."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
import re
|
||||
|
||||
from entity_detect import detect_and_remember
|
||||
from identity_store import KINSHIP, is_maskable_label, list_mappings
|
||||
from pronoun_bind import bind_user_lines
|
||||
from providers import ProviderError, complete_chat, generate_provider
|
||||
|
||||
MAX_EGRESS_CHARS = 24000
|
||||
debug_last: dict[str, Any] | None = None
|
||||
debug_calls = 0
|
||||
|
||||
|
||||
class PrivacyGatewayError(Exception):
|
||||
def __init__(self, code: str, message: str, status_code: int = 503):
|
||||
@@ -20,6 +31,7 @@ class GatewayRequest:
|
||||
purpose: str
|
||||
data_class: str
|
||||
payload: dict[str, Any] = field(default_factory=dict)
|
||||
profile_id: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -35,31 +47,264 @@ ALLOWED_CLASSES = {"A", "B", "C"}
|
||||
LOCAL_ONLY_CLASS = "A"
|
||||
|
||||
|
||||
def reset_debug() -> None:
|
||||
global debug_last, debug_calls
|
||||
debug_last = None
|
||||
debug_calls = 0
|
||||
|
||||
|
||||
def inspect(request: GatewayRequest) -> GatewayResult:
|
||||
"""Fail closed. No provider is configured in the local frame."""
|
||||
"""Fail closed unless a policy-complete generative provider is configured."""
|
||||
data_class = (request.data_class or "").upper()
|
||||
if data_class not in ALLOWED_CLASSES:
|
||||
return GatewayResult(
|
||||
allowed=False,
|
||||
reason="unknown_data_class",
|
||||
)
|
||||
return GatewayResult(allowed=False, reason="unknown_data_class")
|
||||
if data_class == LOCAL_ONLY_CLASS:
|
||||
return GatewayResult(
|
||||
allowed=False,
|
||||
reason="class_a_never_leaves_local_zone",
|
||||
return GatewayResult(allowed=False, reason="class_a_never_leaves_local_zone")
|
||||
config = generate_provider()
|
||||
if not config:
|
||||
return GatewayResult(allowed=False, reason="no_egress_provider_configured")
|
||||
return GatewayResult(allowed=True, reason="policy_ok", provider=config.name)
|
||||
|
||||
|
||||
def _minimize(text: str) -> str:
|
||||
cleaned = (text or "").strip()
|
||||
if len(cleaned) <= MAX_EGRESS_CHARS:
|
||||
return cleaned
|
||||
return cleaned[:MAX_EGRESS_CHARS]
|
||||
|
||||
|
||||
_LETTER = r"A-Za-zÄÖÜäöüß"
|
||||
_WORD = re.compile(rf"[{_LETTER}]+")
|
||||
THING_GOVERNORS = {
|
||||
"esse", "essen", "isst", "aß", "aßest", "gegessen",
|
||||
"koche", "kochen", "kochte", "gekocht",
|
||||
"trinke", "trinken", "trank", "getrunken",
|
||||
"kaufe", "kaufen", "kaufte", "gekauft",
|
||||
"bestelle", "bestellen", "bestellte",
|
||||
"hole", "holen", "holte",
|
||||
}
|
||||
|
||||
|
||||
def _words_before(text: str, index: int, n: int = 4) -> list[str]:
|
||||
return [word.lower() for word in _WORD.findall(text[:index])[-n:]]
|
||||
|
||||
|
||||
def _words_after(text: str, index: int, n: int = 3) -> list[str]:
|
||||
return [word.lower() for word in _WORD.findall(text[index:])[:n]]
|
||||
|
||||
|
||||
def _is_person_token(token: str) -> bool:
|
||||
raw = (token or "").strip().upper()
|
||||
raw = raw[2:-2] if raw.startswith("[[") and raw.endswith("]]") else raw
|
||||
return raw.startswith("PERSON:")
|
||||
|
||||
|
||||
def _mask_person_hit(text: str, start: int, end: int) -> bool:
|
||||
prev = _words_before(text, start)
|
||||
nxt = _words_after(text, end)
|
||||
if prev and prev[-1] in KINSHIP:
|
||||
return True
|
||||
if any(word in THING_GOVERNORS for word in prev + nxt):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _mask_body(text: str, mappings: list[dict]) -> str:
|
||||
masked = text
|
||||
for item in sorted(mappings, key=lambda row: len(row.get("local_label") or ""), reverse=True):
|
||||
label = (item.get("local_label") or "").strip()
|
||||
token = (item.get("token") or "").strip()
|
||||
if not label or not token or not is_maskable_label(label):
|
||||
continue
|
||||
placeholder = token if token.startswith("[[") else f"[[{token}]]"
|
||||
pattern = re.compile(
|
||||
rf"(?<![{_LETTER}]){re.escape(label)}(?![{_LETTER}])",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
return GatewayResult(
|
||||
allowed=False,
|
||||
reason="no_egress_provider_configured",
|
||||
person = _is_person_token(token)
|
||||
|
||||
def repl(match: re.Match, *, _person=person, _ph=placeholder) -> str:
|
||||
if _person and not _mask_person_hit(masked, match.start(), match.end()):
|
||||
return match.group(0)
|
||||
return _ph
|
||||
|
||||
masked = pattern.sub(repl, masked)
|
||||
return masked
|
||||
|
||||
|
||||
def _mask(text: str, mappings: list[dict], *, personal_lines_only: bool = False) -> str:
|
||||
if not personal_lines_only:
|
||||
return _mask_body(text, mappings)
|
||||
parts: list[str] = []
|
||||
for line in (text or "").splitlines(keepends=True):
|
||||
raw = line[:-1] if line.endswith("\n") else line
|
||||
newline = "\n" if line.endswith("\n") else ""
|
||||
if raw.startswith("user:") or raw.startswith("assistant:"):
|
||||
parts.append(_mask_body(raw, mappings) + newline)
|
||||
else:
|
||||
parts.append(line)
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def mask_for_egress(rendered: str, mappings: list[dict], purpose: str) -> str:
|
||||
"""Mask names. Bind pronouns to person tokens only in the dialogue turn.
|
||||
|
||||
Journal generation keeps sie/er/ihr so demask does not turn every reference into the name.
|
||||
"""
|
||||
masked = _mask(rendered, mappings, personal_lines_only=purpose == "dialogue_turn")
|
||||
if purpose == "dialogue_turn":
|
||||
return bind_user_lines(masked)
|
||||
return masked
|
||||
|
||||
|
||||
def _demask(text: str, mappings: list[dict]) -> str:
|
||||
result = text or ""
|
||||
for item in mappings:
|
||||
label = (item.get("local_label") or "").strip()
|
||||
token = (item.get("token") or "").strip()
|
||||
if not label or not token:
|
||||
continue
|
||||
placeholder = token if token.startswith("[[") else f"[[{token}]]"
|
||||
result = result.replace(placeholder, label)
|
||||
return result
|
||||
|
||||
|
||||
def _validate_response(content: str, mappings: list[dict] | None = None) -> str:
|
||||
text = (content or "").strip()
|
||||
if not text:
|
||||
raise PrivacyGatewayError("empty_provider_response", "Der Provider lieferte keine Antwort.")
|
||||
leaked: list[str] = []
|
||||
for item in mappings or []:
|
||||
label = (item.get("local_label") or "").strip()
|
||||
if not label or not is_maskable_label(label):
|
||||
continue
|
||||
if re.search(
|
||||
rf"(?<![{_LETTER}]){re.escape(label)}(?![{_LETTER}])",
|
||||
text,
|
||||
re.IGNORECASE,
|
||||
):
|
||||
leaked.append(label)
|
||||
if leaked:
|
||||
raise PrivacyGatewayError(
|
||||
"response_validation_failed",
|
||||
"Antwort enthielt Klartext-Identität vor der Demaskierung.",
|
||||
)
|
||||
return text
|
||||
|
||||
|
||||
def _fake_complete(purpose: str, rendered: str) -> str:
|
||||
if purpose == "profile_review":
|
||||
return (
|
||||
'{"kind":"kansho.profile_review_result","format_version":1,"target":"writing",'
|
||||
'"changes":[{"layer":"trait","key":"dry_humor","slug":"dry_humor","facet_key":"autobiographical_journal",'
|
||||
'"action":"create","label":"Trockener Humor",'
|
||||
'"proposed_value":"gelegentlich trocken, nie aufgesetzt",'
|
||||
'"rationale":"Fake-Review: Journal-Evidenz trägt Humor in autobiographical_journal, nicht als globalen Core.",'
|
||||
'"evidence_ids":[],"exemplars":[{"excerpt":"haha das war irgendwie lustig","role":"exemplar"}]}]}'
|
||||
)
|
||||
if purpose == "journal_generate":
|
||||
lines = []
|
||||
for line in rendered.splitlines():
|
||||
if line.startswith("user:"):
|
||||
lines.append(line[5:].strip())
|
||||
body = " ".join(part for part in lines if part) or "Ein stiller Tag."
|
||||
return f"Ein Tag\n\n{body}"
|
||||
if "Keine Interviewfrage" in rendered:
|
||||
return '{"operation":"fortfuehren","impulse":"Der letzte Faden bleibt offen."}'
|
||||
impulse = (
|
||||
"Was davon möchtest du vertiefen, ohne Unerwähntes als nicht geschehen zu behandeln?"
|
||||
if ("nicht geschehen" in rendered or "nicht erwähnt" in rendered)
|
||||
else "Was davon möchtest du festhalten?"
|
||||
)
|
||||
return '{"operation":"erleben_vertiefen","impulse":"' + impulse + '"}'
|
||||
|
||||
|
||||
def complete_model(messages: list[dict], policy: dict[str, Any]) -> str:
|
||||
config = generate_provider()
|
||||
if not config:
|
||||
raise PrivacyGatewayError("no_egress_provider_configured", "Es ist kein Egress-Provider konfiguriert.")
|
||||
if config.mode == "fake":
|
||||
rendered = "\n".join(item.get("content") or "" for item in messages)
|
||||
return _fake_complete(policy.get("purpose") or "", rendered)
|
||||
try:
|
||||
return complete_chat(config, messages, timeout=60)
|
||||
except ProviderError as exc:
|
||||
raise PrivacyGatewayError(exc.code, exc.message, exc.status_code) from exc
|
||||
|
||||
|
||||
def complete(request: GatewayRequest) -> GatewayResult:
|
||||
global debug_last, debug_calls
|
||||
result = inspect(request)
|
||||
if not result.allowed:
|
||||
raise PrivacyGatewayError(
|
||||
result.reason,
|
||||
"Persönlicher KI-Aufruf wurde vom Privacy Gateway blockiert. "
|
||||
"Es ist kein Egress-Provider konfiguriert.",
|
||||
"Es ist kein Egress-Provider konfiguriert."
|
||||
if result.reason == "no_egress_provider_configured"
|
||||
else "Persönlicher KI-Aufruf wurde vom Privacy Gateway blockiert.",
|
||||
)
|
||||
rendered = _minimize(str((request.payload or {}).get("rendered") or ""))
|
||||
source_text = str((request.payload or {}).get("source_text") or rendered)
|
||||
detect_name = None
|
||||
try:
|
||||
mappings, detect_name, detect_note = detect_and_remember(request.profile_id, source_text)
|
||||
except ProviderError as exc:
|
||||
raise PrivacyGatewayError(exc.code, exc.message, exc.status_code) from exc
|
||||
if not mappings and request.profile_id:
|
||||
mappings = list_mappings(request.profile_id)
|
||||
masked = mask_for_egress(rendered, mappings, request.purpose)
|
||||
debug_calls += 1
|
||||
config = generate_provider()
|
||||
debug_last = {
|
||||
"purpose": request.purpose,
|
||||
"layer": {
|
||||
"journal_generate": "journalentwurf",
|
||||
"profile_review": "profilreview",
|
||||
}.get(request.purpose, "dialogzug"),
|
||||
"data_class": request.data_class,
|
||||
"prompt_slug": (request.payload or {}).get("prompt_slug"),
|
||||
"rendered": rendered,
|
||||
"masked": masked,
|
||||
"mask_input": source_text,
|
||||
"provider": result.provider,
|
||||
"model": config.model if config else None,
|
||||
"detect_provider": detect_name,
|
||||
"detect_note": detect_note,
|
||||
"mapping_count": len(mappings),
|
||||
}
|
||||
raw = complete_model(
|
||||
[{"role": "user", "content": masked}],
|
||||
{"purpose": request.purpose, "zdr": True, "no_train": True},
|
||||
)
|
||||
validated = _validate_response(raw, mappings)
|
||||
result.content = _demask(validated, mappings)
|
||||
result.allowed = True
|
||||
debug_last["raw"] = validated
|
||||
debug_last["reply"] = result.content
|
||||
return result
|
||||
|
||||
|
||||
def last_trace() -> dict | None:
|
||||
return debug_last
|
||||
|
||||
|
||||
def public_trace(trace: dict | None) -> dict | None:
|
||||
"""Admin test view. No mapping table, no secrets."""
|
||||
if not trace:
|
||||
return None
|
||||
return {
|
||||
"purpose": trace.get("purpose"),
|
||||
"layer": trace.get("layer"),
|
||||
"data_class": trace.get("data_class"),
|
||||
"prompt_slug": trace.get("prompt_slug"),
|
||||
"provider": trace.get("provider"),
|
||||
"model": trace.get("model"),
|
||||
"detect_provider": trace.get("detect_provider"),
|
||||
"detect_note": trace.get("detect_note"),
|
||||
"mapping_count": trace.get("mapping_count"),
|
||||
"intern": trace.get("rendered"),
|
||||
"egress": trace.get("masked"),
|
||||
"mask_input": trace.get("mask_input"),
|
||||
"raw": trace.get("raw"),
|
||||
"reply": trace.get("reply"),
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Portable JSON documents for Writing and Interaction profiles. Not a journal backup."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from dialogue_store import StoreError
|
||||
|
||||
FORMAT_VERSION = 1
|
||||
KIND_WRITING = "kansho.writing_profile"
|
||||
KIND_INTERACTION = "kansho.interaction_profile"
|
||||
KIND_BUNDLE = "kansho.profiles"
|
||||
ORIGIN_WRITING = {"inferred", "manual", "imported", "accepted_suggestion", "initial_build"}
|
||||
ORIGIN_INTERACTION = {"explicit", "manual", "accepted_suggestion"}
|
||||
|
||||
|
||||
def exported_at() -> str:
|
||||
return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
|
||||
|
||||
|
||||
def as_document(raw) -> dict:
|
||||
if isinstance(raw, str):
|
||||
import json
|
||||
|
||||
try:
|
||||
raw = json.loads(raw)
|
||||
except json.JSONDecodeError as exc:
|
||||
raise StoreError("invalid_document", "JSON ist ungültig.") from exc
|
||||
if not isinstance(raw, dict):
|
||||
raise StoreError("invalid_document", "Profil-Dokument muss ein JSON-Objekt sein.")
|
||||
version = raw.get("format_version", FORMAT_VERSION)
|
||||
try:
|
||||
version = int(version)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise StoreError("unsupported_format", "format_version fehlt oder ist ungültig.") from exc
|
||||
if version != FORMAT_VERSION:
|
||||
raise StoreError("unsupported_format", f"format_version {version} wird nicht unterstützt.")
|
||||
return raw
|
||||
|
||||
|
||||
def writing_payload(document: dict) -> dict:
|
||||
data = as_document(document)
|
||||
kind = (data.get("kind") or "").strip()
|
||||
if kind == KIND_BUNDLE:
|
||||
nested = data.get("writing")
|
||||
if not isinstance(nested, dict):
|
||||
raise StoreError("invalid_document", "Sammeldokument enthält kein Writing Profile.")
|
||||
return nested
|
||||
if kind != KIND_WRITING:
|
||||
raise StoreError("invalid_document", "Das ist kein Writing-Profile-Dokument.")
|
||||
return data
|
||||
|
||||
|
||||
def interaction_payload(document: dict) -> dict:
|
||||
data = as_document(document)
|
||||
kind = (data.get("kind") or "").strip()
|
||||
if kind == KIND_BUNDLE:
|
||||
nested = data.get("interaction")
|
||||
if not isinstance(nested, dict):
|
||||
raise StoreError("invalid_document", "Sammeldokument enthält kein Interaction Profile.")
|
||||
return nested
|
||||
if kind != KIND_INTERACTION:
|
||||
raise StoreError("invalid_document", "Das ist kein Interaction-Profile-Dokument.")
|
||||
return data
|
||||
|
||||
|
||||
def export_bundle(profile_id: str) -> dict:
|
||||
from interaction_profile_store import export_document as export_interaction
|
||||
from writing_profile_store import export_document as export_writing
|
||||
|
||||
writing = export_writing(profile_id)
|
||||
interaction = export_interaction(profile_id)
|
||||
writing.pop("kind", None)
|
||||
writing.pop("format_version", None)
|
||||
writing.pop("exported_at", None)
|
||||
interaction.pop("kind", None)
|
||||
interaction.pop("format_version", None)
|
||||
interaction.pop("exported_at", None)
|
||||
return {
|
||||
"kind": KIND_BUNDLE,
|
||||
"format_version": FORMAT_VERSION,
|
||||
"exported_at": exported_at(),
|
||||
"writing": writing,
|
||||
"interaction": interaction,
|
||||
}
|
||||
|
||||
|
||||
def restore_bundle(profile_id: str, document: dict) -> dict:
|
||||
data = as_document(document)
|
||||
kind = (data.get("kind") or "").strip()
|
||||
if kind != KIND_BUNDLE:
|
||||
raise StoreError("invalid_document", "Das ist kein Sammeldokument beider Profile.")
|
||||
from interaction_profile_store import restore_document as restore_interaction
|
||||
from writing_profile_store import restore_document as restore_writing
|
||||
|
||||
writing = restore_writing(profile_id, data)
|
||||
interaction = restore_interaction(profile_id, data)
|
||||
return {"writing": writing, "interaction": interaction}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,120 @@
|
||||
"""Bind German 3rd-person pronouns to the last named person. Local; no new Detect tokens."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
PLACEHOLDER = re.compile(r"\[\[(PERSON:[^\]]+)\]\]")
|
||||
PIECE = re.compile(r"\[\[PERSON:[^\]]+\]\]|[A-Za-zÄÖÜäöüß]+")
|
||||
|
||||
FEM_CUES = {
|
||||
"frau", "partnerin", "tochter", "mutter", "schwester", "freundin",
|
||||
"oma", "tante",
|
||||
}
|
||||
MASC_CUES = {
|
||||
"herr", "mann", "partner", "sohn", "vater", "bruder", "freund",
|
||||
"opa", "onkel",
|
||||
}
|
||||
FEM_PRON = {"sie", "ihr", "ihre", "ihrem", "ihren", "ihrer", "ihres"}
|
||||
MASC_PRON = {"er", "ihn", "ihm", "seine", "seinem", "seinen", "seiner", "seines"}
|
||||
IHR_ADDRESS = {
|
||||
"seid", "habt", "wart", "könnt", "müsst", "sollt", "wollt", "dürft",
|
||||
"hättet", "wäret", "werdet",
|
||||
}
|
||||
PLURAL_VERB = {
|
||||
"sind", "waren", "haben", "hatten", "werden", "wurden",
|
||||
"können", "müssen", "wollen", "sollen",
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class BindState:
|
||||
last: str | None = None
|
||||
last_f: str | None = None
|
||||
last_m: str | None = None
|
||||
|
||||
|
||||
def _gender_from_prev(pieces: list[re.Match], index: int) -> str | None:
|
||||
seen: list[str] = []
|
||||
pos = index - 1
|
||||
while pos >= 0 and len(seen) < 3:
|
||||
raw = pieces[pos].group(0)
|
||||
if PLACEHOLDER.fullmatch(raw):
|
||||
break
|
||||
seen.append(raw.lower())
|
||||
pos -= 1
|
||||
window = set(seen)
|
||||
if window & FEM_CUES:
|
||||
return "f"
|
||||
if window & MASC_CUES:
|
||||
return "m"
|
||||
return None
|
||||
|
||||
|
||||
def _referent(state: BindState, kind: str) -> str | None:
|
||||
if kind == "f" and state.last_f:
|
||||
return state.last_f
|
||||
if kind == "m" and state.last_m:
|
||||
return state.last_m
|
||||
return state.last
|
||||
|
||||
|
||||
def _is_address_ihr(prev: str, nxt: str) -> bool:
|
||||
if nxt in IHR_ADDRESS or prev in IHR_ADDRESS:
|
||||
return True
|
||||
if prev.endswith("t") and len(prev) >= 4:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def bind_pronouns(text: str, state: BindState | None = None) -> tuple[str, BindState]:
|
||||
state = state or BindState()
|
||||
pieces = list(PIECE.finditer(text or ""))
|
||||
replacements: list[tuple[int, int, str]] = []
|
||||
for i, match in enumerate(pieces):
|
||||
raw = match.group(0)
|
||||
person = PLACEHOLDER.fullmatch(raw)
|
||||
if person:
|
||||
token = person.group(1)
|
||||
state.last = token
|
||||
gender = _gender_from_prev(pieces, i)
|
||||
if gender == "f":
|
||||
state.last_f = token
|
||||
elif gender == "m":
|
||||
state.last_m = token
|
||||
continue
|
||||
low = raw.lower()
|
||||
prev = pieces[i - 1].group(0).lower() if i else ""
|
||||
nxt = pieces[i + 1].group(0).lower() if i + 1 < len(pieces) else ""
|
||||
if low == "ihr" and _is_address_ihr(prev, nxt):
|
||||
continue
|
||||
if low == "sie" and nxt in PLURAL_VERB:
|
||||
continue
|
||||
kind = "f" if low in FEM_PRON else "m" if low in MASC_PRON else ""
|
||||
if not kind:
|
||||
continue
|
||||
ref = _referent(state, kind)
|
||||
if not ref:
|
||||
continue
|
||||
replacements.append((match.start(), match.end(), f"[[{ref}]]"))
|
||||
if not replacements:
|
||||
return text or "", state
|
||||
out = text or ""
|
||||
for start, end, repl in reversed(replacements):
|
||||
out = out[:start] + repl + out[end:]
|
||||
return out, state
|
||||
|
||||
|
||||
def bind_user_lines(text: str) -> str:
|
||||
"""Only user: lines; state carries across those lines. Instructions stay untouched."""
|
||||
state = BindState()
|
||||
chunks: list[str] = []
|
||||
for line in (text or "").splitlines(keepends=True):
|
||||
ended = line.endswith("\n")
|
||||
body = line[:-1] if ended else line
|
||||
if body.startswith("user:"):
|
||||
bound, state = bind_pronouns(body[5:], state)
|
||||
chunks.append("user:" + bound + ("\n" if ended else ""))
|
||||
else:
|
||||
chunks.append(line)
|
||||
return "".join(chunks)
|
||||
@@ -0,0 +1,153 @@
|
||||
"""Admin-configurable generate/detect endpoints. Keys stay in env, never in the DB."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from db import get_db
|
||||
from env_loader import allows_remote_plaintext_detect, runtime_env, upsert_env_value
|
||||
|
||||
SEED_PATH = Path(__file__).resolve().parent / "config" / "providers.seed.json"
|
||||
ROLES = ("generate", "detect")
|
||||
_KEY_ENV = {
|
||||
"generate": "KANSHO_PROVIDER_KEY",
|
||||
"detect": "KANSHO_DETECT_PROVIDER_KEY",
|
||||
}
|
||||
# Interne Rollen bleiben generate/detect. Fachlich: Sprachmodell vs. Maskierung.
|
||||
ROLE_META = {
|
||||
"generate": {
|
||||
"title": "Sprachmodell",
|
||||
"task": "Dialogzug und Journalentwurf. Nur maskierter Kontext. Wählt die Operation im Dialogzug mit.",
|
||||
},
|
||||
"detect": {
|
||||
"title": "Maskierung",
|
||||
"task": "Eigener Detect-Endpunkt. Nur Namen, keine Operation. Externes Klartext-Detect nur Development/Test. Produktiv: lokal oder Muster.",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class SettingsError(Exception):
|
||||
def __init__(self, code: str, message: str, status_code: int = 400):
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
def seed_provider_settings(conn) -> None:
|
||||
items = json.loads(SEED_PATH.read_text(encoding="utf-8"))
|
||||
for item in items:
|
||||
role = item["role"]
|
||||
if role not in ROLES:
|
||||
continue
|
||||
existing = conn.execute("SELECT role FROM provider_settings WHERE role = ?", (role,)).fetchone()
|
||||
if existing:
|
||||
continue
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO provider_settings (role, name, url, model, zdr, no_train)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
role,
|
||||
item.get("name") or "",
|
||||
item.get("url") or "",
|
||||
item.get("model") or "",
|
||||
1 if item.get("zdr", True) else 0,
|
||||
1 if item.get("no_train", True) else 0,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def get_setting(role: str) -> dict | None:
|
||||
if role not in ROLES:
|
||||
return None
|
||||
with get_db() as conn:
|
||||
row = conn.execute("SELECT * FROM provider_settings WHERE role = ?", (role,)).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def upsert_setting(role: str, *, name: str, url: str, model: str, zdr: bool, no_train: bool) -> dict:
|
||||
if role not in ROLES:
|
||||
raise SettingsError("unknown_provider_role", "Unbekannte Provider-Rolle.")
|
||||
url = (url or "").strip()
|
||||
model = (model or "").strip()
|
||||
name = (name or "").strip()
|
||||
if role == "generate" and not url:
|
||||
raise SettingsError("provider_url_required", "Das Sprachmodell braucht eine URL.")
|
||||
if url:
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
||||
raise SettingsError("provider_url_invalid", "URL muss http(s) sein.")
|
||||
if role == "generate" and not model:
|
||||
raise SettingsError("provider_model_required", "Das Sprachmodell braucht ein Modell.")
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO provider_settings (role, name, url, model, zdr, no_train, updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))
|
||||
ON CONFLICT(role) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
url = excluded.url,
|
||||
model = excluded.model,
|
||||
zdr = excluded.zdr,
|
||||
no_train = excluded.no_train,
|
||||
updated = datetime('now')
|
||||
""",
|
||||
(role, name, url, model, 1 if zdr else 0, 1 if no_train else 0),
|
||||
)
|
||||
return get_setting(role) or {}
|
||||
|
||||
|
||||
def set_role_key(role: str, key: str) -> None:
|
||||
if role not in ROLES:
|
||||
raise SettingsError("unknown_provider_role", "Unbekannte Provider-Rolle.")
|
||||
value = (key or "").strip()
|
||||
if not value:
|
||||
raise SettingsError("provider_key_empty", "Leerer Key wird nicht gespeichert.")
|
||||
upsert_env_value(_KEY_ENV[role], value)
|
||||
|
||||
|
||||
def public_role_status(role: str) -> dict:
|
||||
from providers import _is_local_url, detect_provider, generate_provider
|
||||
|
||||
row = get_setting(role) or {}
|
||||
config = generate_provider() if role == "generate" else detect_provider()
|
||||
local = _is_local_url(row.get("url") or "")
|
||||
own_key = bool((os.environ.get(_KEY_ENV[role]) or "").strip())
|
||||
generate_key = bool((os.environ.get(_KEY_ENV["generate"]) or "").strip())
|
||||
if own_key:
|
||||
key_source = "own"
|
||||
elif role == "detect" and generate_key and not local:
|
||||
key_source = "generate"
|
||||
else:
|
||||
key_source = "none"
|
||||
meta = ROLE_META.get(role, {})
|
||||
return {
|
||||
"role": role,
|
||||
"title": meta.get("title") or role,
|
||||
"task": meta.get("task") or "",
|
||||
"name": row.get("name") or (config.name if config else ""),
|
||||
"url": row.get("url") or "",
|
||||
"model": row.get("model") or "",
|
||||
"zdr": bool(row.get("zdr", 1)),
|
||||
"no_train": bool(row.get("no_train", 1)),
|
||||
"local": local,
|
||||
"key_present": key_source != "none",
|
||||
"key_source": key_source,
|
||||
"ready": config is not None,
|
||||
"mode": config.mode if config else "unconfigured",
|
||||
"remote_plaintext_detect": bool(config and not config.local and config.mode == "http" and role == "detect"),
|
||||
"remote_plaintext_allowed": allows_remote_plaintext_detect() if role == "detect" else None,
|
||||
"updated": row.get("updated") or "",
|
||||
}
|
||||
|
||||
|
||||
def public_status() -> dict:
|
||||
return {
|
||||
"roles": [public_role_status(role) for role in ROLES],
|
||||
"runtime_env": runtime_env(),
|
||||
"note": "Zwei Einträge: Maskierung (Detect) und Sprachmodell. Externes Klartext-Detect nur Development/Test. Produktiv lokal oder Muster. Keys nur in backend/.env.",
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
"""Two independently configured OpenAI-compatible providers: generate vs detect."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import httpx
|
||||
|
||||
from env_loader import load_env_file
|
||||
|
||||
load_env_file()
|
||||
|
||||
|
||||
class ProviderError(Exception):
|
||||
def __init__(self, code: str, message: str, status_code: int = 503):
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProviderConfig:
|
||||
role: str
|
||||
name: str
|
||||
mode: str
|
||||
url: str
|
||||
model: str
|
||||
key: str
|
||||
local: bool
|
||||
zdr: bool
|
||||
no_train: bool
|
||||
|
||||
|
||||
def _truthy(name: str) -> bool:
|
||||
return os.environ.get(name, "").strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
|
||||
def _is_local_url(url: str) -> bool:
|
||||
host = (urlparse(url).hostname or "").lower()
|
||||
return host in {"localhost", "127.0.0.1", "::1", "ollama"}
|
||||
|
||||
|
||||
def _setting(role: str) -> dict:
|
||||
try:
|
||||
from provider_settings import get_setting
|
||||
|
||||
return get_setting(role) or {}
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def _text(env_name: str, db_value: str | None, default: str = "") -> str:
|
||||
env = (os.environ.get(env_name) or "").strip()
|
||||
if env:
|
||||
return env
|
||||
if db_value:
|
||||
return str(db_value).strip()
|
||||
return default
|
||||
|
||||
|
||||
def _flag(env_name: str, db_value, default: bool = True) -> bool:
|
||||
if (os.environ.get(env_name) or "").strip():
|
||||
return _truthy(env_name)
|
||||
if db_value is None:
|
||||
return default
|
||||
return bool(int(db_value) if not isinstance(db_value, bool) else db_value)
|
||||
|
||||
|
||||
def generate_provider() -> ProviderConfig | None:
|
||||
if _truthy("KANSHO_FAKE_PROVIDER"):
|
||||
return ProviderConfig(
|
||||
role="generate",
|
||||
name="fake",
|
||||
mode="fake",
|
||||
url="",
|
||||
model="fake",
|
||||
key="",
|
||||
local=True,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
row = _setting("generate")
|
||||
key = (os.environ.get("KANSHO_PROVIDER_KEY") or "").strip()
|
||||
url = _text("KANSHO_PROVIDER_URL", row.get("url"), "https://openrouter.ai/api/v1/chat/completions")
|
||||
model = _text("KANSHO_PROVIDER_MODEL", row.get("model"), "openai/gpt-4o-mini")
|
||||
local = _is_local_url(url)
|
||||
zdr = local or _flag("KANSHO_PROVIDER_ZDR", row.get("zdr"), True)
|
||||
no_train = local or _flag("KANSHO_PROVIDER_NO_TRAIN", row.get("no_train"), True)
|
||||
if not local and not key:
|
||||
return None
|
||||
if not local and (not zdr or not no_train):
|
||||
return None
|
||||
return ProviderConfig(
|
||||
role="generate",
|
||||
name=row.get("name") or ("ollama" if local else "openrouter"),
|
||||
mode="http",
|
||||
url=url,
|
||||
model=model,
|
||||
key=key,
|
||||
local=local,
|
||||
zdr=zdr,
|
||||
no_train=no_train,
|
||||
)
|
||||
|
||||
|
||||
def detect_provider() -> ProviderConfig | None:
|
||||
if _truthy("KANSHO_FAKE_DETECT"):
|
||||
return ProviderConfig(
|
||||
role="detect",
|
||||
name="fake-detect",
|
||||
mode="fake",
|
||||
url="",
|
||||
model="fake",
|
||||
key="",
|
||||
local=True,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
row = _setting("detect")
|
||||
url = _text("KANSHO_DETECT_PROVIDER_URL", row.get("url"), "")
|
||||
if not url:
|
||||
return None
|
||||
model = _text("KANSHO_DETECT_PROVIDER_MODEL", row.get("model"), "openai/gpt-4.1-nano")
|
||||
key = (os.environ.get("KANSHO_DETECT_PROVIDER_KEY") or "").strip()
|
||||
local = _is_local_url(url)
|
||||
if not key and not local:
|
||||
key = (os.environ.get("KANSHO_PROVIDER_KEY") or "").strip()
|
||||
zdr = local or _flag("KANSHO_DETECT_ZDR", row.get("zdr"), True)
|
||||
no_train = local or _flag("KANSHO_DETECT_NO_TRAIN", row.get("no_train"), True)
|
||||
if not local and not key:
|
||||
return None
|
||||
if not local and (not zdr or not no_train):
|
||||
return None
|
||||
return ProviderConfig(
|
||||
role="detect",
|
||||
name=row.get("name") or ("ollama" if local else "openrouter"),
|
||||
mode="http",
|
||||
url=url,
|
||||
model=model,
|
||||
key=key,
|
||||
local=local,
|
||||
zdr=zdr,
|
||||
no_train=no_train,
|
||||
)
|
||||
|
||||
|
||||
def complete_chat(config: ProviderConfig, messages: list[dict], *, timeout: float, max_tokens: int | None = None) -> str:
|
||||
payload: dict = {"model": config.model, "messages": messages}
|
||||
if max_tokens is not None:
|
||||
payload["max_tokens"] = max_tokens
|
||||
if not config.local:
|
||||
payload["provider"] = {"data_collection": "deny"}
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if config.key:
|
||||
headers["Authorization"] = f"Bearer {config.key}"
|
||||
if not config.local:
|
||||
headers["HTTP-Referer"] = "https://kansho.local"
|
||||
headers["X-Title"] = "Kansho"
|
||||
try:
|
||||
response = httpx.post(config.url, json=payload, headers=headers, timeout=timeout)
|
||||
except httpx.HTTPError as exc:
|
||||
raise ProviderError("provider_unreachable", f"{config.role}-Provider ist nicht erreichbar.") from exc
|
||||
if response.status_code >= 400:
|
||||
raise ProviderError("provider_rejected", f"{config.role}-Provider hat die Anfrage abgelehnt.")
|
||||
data = response.json()
|
||||
try:
|
||||
return data["choices"][0]["message"]["content"]
|
||||
except (KeyError, IndexError, TypeError) as exc:
|
||||
raise ProviderError("provider_shape", f"Unerwartete {config.role}-Antwort.") from exc
|
||||
@@ -2,3 +2,4 @@ fastapi==0.115.6
|
||||
uvicorn[standard]==0.34.0
|
||||
bcrypt==4.2.1
|
||||
httpx==0.28.1
|
||||
python-multipart==0.0.20
|
||||
@@ -0,0 +1,175 @@
|
||||
"""Swappable retrieval interface. Specs name what to select; recency/SQL is implementation."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from db import get_db, row_to_dict
|
||||
from journal_body import plain_text
|
||||
|
||||
# Implementation caps. Not part of the selection-spec contract.
|
||||
_DAY_MESSAGE_CAP = 80
|
||||
_SPACE_ENTRY_LIMIT = 5
|
||||
_SPACE_ENTRY_EXCERPT_CHARS = 400
|
||||
_SPACE_SOURCE_CONV_LIMIT = 3
|
||||
_SPACE_SOURCE_EXCERPT_CHARS = 400
|
||||
_SPACE_SOURCE_MESSAGE_CAP = 8
|
||||
|
||||
|
||||
def retrieve(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
kind = spec.get("kind")
|
||||
if kind == "day_messages":
|
||||
return _day_messages(profile_id, spec)
|
||||
if kind == "space_entries":
|
||||
return _space_entries(profile_id, spec)
|
||||
if kind == "space_recent_sources":
|
||||
return _space_recent_sources(profile_id, spec)
|
||||
if kind == "writing_profile":
|
||||
return _writing_profile(profile_id)
|
||||
return []
|
||||
|
||||
|
||||
def _day_messages(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
journal_day_id = spec.get("journal_day_id")
|
||||
conversation_ids = spec.get("conversation_ids") or []
|
||||
if not journal_day_id:
|
||||
return []
|
||||
with get_db() as conn:
|
||||
if conversation_ids:
|
||||
placeholders = ",".join("?" * len(conversation_ids))
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT m.*, c.title AS conversation_title
|
||||
FROM messages m
|
||||
JOIN conversations c ON c.id = m.conversation_id
|
||||
WHERE m.profile_id = ? AND c.journal_day_id = ? AND m.conversation_id IN ({placeholders})
|
||||
ORDER BY c.created, m.seq
|
||||
LIMIT ?
|
||||
""",
|
||||
(profile_id, journal_day_id, *conversation_ids, _DAY_MESSAGE_CAP),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT m.*, c.title AS conversation_title
|
||||
FROM messages m
|
||||
JOIN conversations c ON c.id = m.conversation_id
|
||||
WHERE m.profile_id = ? AND c.journal_day_id = ?
|
||||
ORDER BY c.created, m.seq
|
||||
LIMIT ?
|
||||
""",
|
||||
(profile_id, journal_day_id, _DAY_MESSAGE_CAP),
|
||||
).fetchall()
|
||||
return [row_to_dict(row) for row in rows]
|
||||
|
||||
|
||||
def _space_entries(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
space_id = spec.get("space_id")
|
||||
exclude_day_id = spec.get("exclude_day_id")
|
||||
if not space_id:
|
||||
return []
|
||||
params: list[Any] = [profile_id, space_id]
|
||||
exclude_sql = ""
|
||||
if exclude_day_id:
|
||||
exclude_sql = "AND e.journal_day_id != ?"
|
||||
params.append(exclude_day_id)
|
||||
params.append(_SPACE_ENTRY_LIMIT)
|
||||
with get_db() as conn:
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT e.id AS entry_id, e.journal_day_id, d.calendar_date, v.title, v.body, v.origin
|
||||
FROM journal_entries e
|
||||
JOIN journal_days d ON d.id = e.journal_day_id
|
||||
JOIN journal_entry_versions v ON v.id = e.current_version_id
|
||||
WHERE e.profile_id = ? AND e.space_id = ? AND e.deleted_at IS NULL {exclude_sql}
|
||||
ORDER BY d.calendar_date DESC, e.updated DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
params,
|
||||
).fetchall()
|
||||
result = []
|
||||
for row in rows:
|
||||
item = row_to_dict(row)
|
||||
item["excerpt"] = plain_text(item.get("body") or "")[:_SPACE_ENTRY_EXCERPT_CHARS]
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
def _space_recent_sources(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
"""Recent original conversations in the same space. Recency is the current heuristic."""
|
||||
space_id = spec.get("space_id")
|
||||
exclude_conversation_id = spec.get("exclude_conversation_id")
|
||||
exclude_journal_day_id = spec.get("exclude_journal_day_id")
|
||||
if not space_id:
|
||||
return []
|
||||
params: list[Any] = [profile_id, space_id]
|
||||
extra = ""
|
||||
if exclude_conversation_id:
|
||||
extra += " AND c.id != ?"
|
||||
params.append(exclude_conversation_id)
|
||||
if exclude_journal_day_id:
|
||||
extra += " AND (c.journal_day_id IS NULL OR c.journal_day_id != ?)"
|
||||
params.append(exclude_journal_day_id)
|
||||
params.append(_SPACE_SOURCE_CONV_LIMIT)
|
||||
with get_db() as conn:
|
||||
conversations = conn.execute(
|
||||
f"""
|
||||
SELECT c.id, c.updated, c.journal_day_id
|
||||
FROM conversations c
|
||||
WHERE c.profile_id = ? AND c.space_id = ? {extra}
|
||||
ORDER BY c.updated DESC, c.created DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
params,
|
||||
).fetchall()
|
||||
result = []
|
||||
for conv in conversations:
|
||||
messages = conn.execute(
|
||||
"""
|
||||
SELECT body FROM messages
|
||||
WHERE profile_id = ? AND conversation_id = ? AND role = 'user'
|
||||
ORDER BY seq
|
||||
LIMIT ?
|
||||
""",
|
||||
(profile_id, conv["id"], _SPACE_SOURCE_MESSAGE_CAP),
|
||||
).fetchall()
|
||||
excerpt = " ".join((row["body"] or "").strip() for row in messages if (row["body"] or "").strip())
|
||||
excerpt = excerpt[:_SPACE_SOURCE_EXCERPT_CHARS]
|
||||
if not excerpt:
|
||||
continue
|
||||
result.append(
|
||||
{
|
||||
"conversation_id": conv["id"],
|
||||
"journal_day_id": conv["journal_day_id"],
|
||||
"excerpt": excerpt,
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def _writing_profile(profile_id: str) -> list[dict]:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
conn.execute(
|
||||
"SELECT compiled_brief, updated FROM writing_profiles WHERE profile_id = ?",
|
||||
(profile_id,),
|
||||
).fetchone()
|
||||
)
|
||||
sources = [
|
||||
row_to_dict(item)
|
||||
for item in conn.execute(
|
||||
"""
|
||||
SELECT id, kind, body, entry_id, weight, created
|
||||
FROM writing_profile_sources
|
||||
WHERE profile_id = ?
|
||||
ORDER BY CASE kind
|
||||
WHEN 'journal_entry' THEN 0
|
||||
WHEN 'imported_text' THEN 1
|
||||
ELSE 2
|
||||
END, created DESC
|
||||
""",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
]
|
||||
if not row and not sources:
|
||||
return []
|
||||
return [{"compiled_brief": (row or {}).get("compiled_brief") or "", "sources": sources}]
|
||||
@@ -1,8 +1,10 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from auth import require_admin_dep
|
||||
from db import get_db
|
||||
from dialogue_store import StoreError, get_conversation, inventory, list_conversations, list_derived_for_conversation, list_messages
|
||||
from provider_settings import ROLES, SettingsError, public_status, set_role_key, upsert_setting
|
||||
from version import APP_VERSION, BUILD_DATE, MODULE_VERSIONS
|
||||
|
||||
router = APIRouter(prefix="/api/admin", tags=["admin"])
|
||||
@@ -15,6 +17,7 @@ def admin_health(session: dict = Depends(require_admin_dep)):
|
||||
prompts = conn.execute("SELECT COUNT(*) AS n FROM ai_prompts").fetchone()["n"]
|
||||
features = conn.execute("SELECT COUNT(*) AS n FROM features").fetchone()["n"]
|
||||
dialogue = inventory()
|
||||
providers = public_status()
|
||||
return {
|
||||
"ok": True,
|
||||
"service": "kansho",
|
||||
@@ -26,6 +29,10 @@ def admin_health(session: dict = Depends(require_admin_dep)):
|
||||
"prompts": prompts,
|
||||
"features": features,
|
||||
"dialogue": dialogue,
|
||||
"providers": {
|
||||
role["role"]: {"ready": role["ready"], "key_present": role["key_present"]}
|
||||
for role in providers["roles"]
|
||||
},
|
||||
},
|
||||
"session": {
|
||||
"profile_id": session["profile_id"],
|
||||
@@ -54,3 +61,37 @@ def admin_conversation(conversation_id: str, session: dict = Depends(require_adm
|
||||
}
|
||||
except StoreError as exc:
|
||||
raise HTTPException(status_code=exc.status_code, detail={"code": exc.code, "message": exc.message}) from exc
|
||||
|
||||
|
||||
class ProviderUpdate(BaseModel):
|
||||
name: str = ""
|
||||
url: str = ""
|
||||
model: str = ""
|
||||
zdr: bool = True
|
||||
no_train: bool = True
|
||||
key: str | None = Field(default=None, description="Write-only. Never returned.")
|
||||
|
||||
|
||||
@router.get("/providers")
|
||||
def admin_providers(session: dict = Depends(require_admin_dep)):
|
||||
return public_status()
|
||||
|
||||
|
||||
@router.put("/providers/{role}")
|
||||
def admin_update_provider(role: str, body: ProviderUpdate, session: dict = Depends(require_admin_dep)):
|
||||
if role not in ROLES:
|
||||
raise HTTPException(status_code=404, detail={"code": "unknown_provider_role", "message": "Unbekannte Provider-Rolle."})
|
||||
try:
|
||||
upsert_setting(
|
||||
role,
|
||||
name=body.name,
|
||||
url=body.url,
|
||||
model=body.model,
|
||||
zdr=body.zdr,
|
||||
no_train=body.no_train,
|
||||
)
|
||||
if body.key is not None and body.key.strip():
|
||||
set_role_key(role, body.key)
|
||||
except SettingsError as exc:
|
||||
raise HTTPException(status_code=exc.status_code, detail={"code": exc.code, "message": exc.message}) from exc
|
||||
return public_status()
|
||||
@@ -7,10 +7,13 @@ from auth import require_auth
|
||||
from context_builder import build_internal_context
|
||||
from continuity import checkpoint_usage_session, close_usage_session
|
||||
from derived_kinds import catalog
|
||||
from dialogue_turn import run_turn, visible_for_role
|
||||
from engine import EngineError
|
||||
from dialogue_store import (
|
||||
StoreError,
|
||||
append_message,
|
||||
create_conversation,
|
||||
delete_conversation,
|
||||
create_handoff,
|
||||
create_space,
|
||||
create_thread,
|
||||
@@ -27,7 +30,7 @@ from dialogue_store import (
|
||||
router = APIRouter(prefix="/api/dialogue", tags=["dialogue"])
|
||||
|
||||
|
||||
def _http(exc: StoreError):
|
||||
def _http(exc: StoreError | EngineError):
|
||||
raise HTTPException(status_code=exc.status_code, detail={"code": exc.code, "message": exc.message}) from exc
|
||||
|
||||
|
||||
@@ -38,6 +41,7 @@ class SessionWrite(BaseModel):
|
||||
class ConversationWrite(BaseModel):
|
||||
usage_session_id: str | None = None
|
||||
title: str = ""
|
||||
space_id: str | None = None
|
||||
|
||||
|
||||
class MessageWrite(BaseModel):
|
||||
@@ -102,7 +106,12 @@ def checkpoint_session(usage_session_id: str, session: dict = Depends(require_au
|
||||
@router.post("/conversations")
|
||||
def post_conversation(req: ConversationWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return create_conversation(session["profile_id"], req.usage_session_id, req.title)
|
||||
return create_conversation(
|
||||
session["profile_id"],
|
||||
req.usage_session_id,
|
||||
req.title,
|
||||
space_id=req.space_id,
|
||||
)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
@@ -112,6 +121,18 @@ def get_conversations(session: dict = Depends(require_auth)):
|
||||
return list_conversations(session["profile_id"])
|
||||
|
||||
|
||||
@router.delete("/conversations/{conversation_id}")
|
||||
def remove_conversation(conversation_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
from writing_profile_store import remember_dialogue_style
|
||||
|
||||
result = delete_conversation(session["profile_id"], conversation_id)
|
||||
remember_dialogue_style(session["profile_id"])
|
||||
return result
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/conversations/{conversation_id}")
|
||||
def get_one_conversation(conversation_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
@@ -139,6 +160,17 @@ def post_message(conversation_id: str, req: MessageWrite, session: dict = Depend
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/conversations/{conversation_id}/turn")
|
||||
def conversation_turn(conversation_id: str, req: MessageWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return visible_for_role(
|
||||
run_turn(session["profile_id"], conversation_id, req.body, message_id=req.id),
|
||||
session.get("role"),
|
||||
)
|
||||
except (StoreError, EngineError) as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/conversations/{conversation_id}/messages")
|
||||
def get_messages(conversation_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
|
||||
@@ -0,0 +1,604 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from auth import require_auth
|
||||
from dialogue_store import StoreError, delete_conversation, get_conversation, list_messages
|
||||
from dialogue_turn import run_turn, visible_for_role
|
||||
from privacy_gateway import GatewayRequest, inspect
|
||||
from engine import EngineError
|
||||
from journal_generate import generate_draft
|
||||
from journal_policy import PolicyError
|
||||
from journal_store import (
|
||||
create_day_conversation,
|
||||
create_user_space,
|
||||
day_payload,
|
||||
get_day,
|
||||
get_entry,
|
||||
get_or_create_day,
|
||||
get_user_space,
|
||||
list_days,
|
||||
list_user_spaces,
|
||||
list_versions,
|
||||
rename_user_space,
|
||||
restore_entry,
|
||||
save_day_scratch,
|
||||
save_entry,
|
||||
soft_delete_entry,
|
||||
)
|
||||
from media_store import delete_media, get_media, list_media, save_media
|
||||
from writing_profile_store import (
|
||||
accept_suggestion as accept_writing_suggestion,
|
||||
export_document as export_writing_document,
|
||||
get_profile,
|
||||
import_corpus,
|
||||
list_corpus,
|
||||
rebuild_brief,
|
||||
reject_suggestion as reject_writing_suggestion,
|
||||
remember_dialogue_style,
|
||||
remember_journal_entry,
|
||||
restore_document as restore_writing_document,
|
||||
set_governance as set_writing_governance,
|
||||
update_facet,
|
||||
update_trait,
|
||||
)
|
||||
from profile_review import (
|
||||
ingest_import,
|
||||
ingest_journal,
|
||||
import_result as import_review_result,
|
||||
open_paste_review,
|
||||
review_status,
|
||||
run_api_review,
|
||||
)
|
||||
from interaction_profile_store import (
|
||||
accept_suggestion as accept_interaction_suggestion,
|
||||
export_document as export_interaction_document,
|
||||
get_profile as get_interaction_profile,
|
||||
reject_suggestion as reject_interaction_suggestion,
|
||||
restore_document as restore_interaction_document,
|
||||
set_governance as set_interaction_governance,
|
||||
upsert_preference,
|
||||
)
|
||||
from profile_documents import export_bundle, restore_bundle
|
||||
|
||||
router = APIRouter(prefix="/api/journal", tags=["journal"])
|
||||
|
||||
|
||||
def _http(exc: StoreError | PolicyError | EngineError):
|
||||
raise HTTPException(status_code=exc.status_code, detail={"code": exc.code, "message": exc.message}) from exc
|
||||
|
||||
|
||||
class SpaceWrite(BaseModel):
|
||||
title: str = Field(min_length=1)
|
||||
|
||||
|
||||
class DayWrite(BaseModel):
|
||||
calendar_date: str = Field(min_length=10, max_length=10)
|
||||
|
||||
|
||||
class ConversationWrite(BaseModel):
|
||||
title: str = ""
|
||||
|
||||
|
||||
class TurnWrite(BaseModel):
|
||||
body: str = Field(min_length=1)
|
||||
id: str | None = None
|
||||
|
||||
|
||||
class GenerateWrite(BaseModel):
|
||||
conversation_ids: list[str] | None = None
|
||||
include_existing: bool = False
|
||||
|
||||
|
||||
class EntryWrite(BaseModel):
|
||||
journal_day_id: str
|
||||
title: str = ""
|
||||
body: str = ""
|
||||
entry_id: str | None = None
|
||||
origin: str = "user_edit"
|
||||
source_conversation_ids: list[str] = Field(default_factory=list)
|
||||
source_message_ids: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class RestoreWrite(BaseModel):
|
||||
version_id: str
|
||||
|
||||
|
||||
class ScratchItem(BaseModel):
|
||||
id: str | None = None
|
||||
text: str = ""
|
||||
done: bool = False
|
||||
|
||||
|
||||
class ScratchWrite(BaseModel):
|
||||
items: list[ScratchItem] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ImportWrite(BaseModel):
|
||||
body: str = Field(min_length=1)
|
||||
occurred_at: str | None = None
|
||||
context_hint: str | None = None
|
||||
|
||||
|
||||
class CorpusWrite(BaseModel):
|
||||
items: list[dict]
|
||||
|
||||
|
||||
class TraitWrite(BaseModel):
|
||||
statement: str | None = None
|
||||
locked: bool | None = None
|
||||
facet_key: str | None = None
|
||||
label: str | None = None
|
||||
|
||||
|
||||
class GovernanceWrite(BaseModel):
|
||||
governance: str
|
||||
|
||||
|
||||
class FacetWrite(BaseModel):
|
||||
value: str | None = None
|
||||
locked: bool | None = None
|
||||
|
||||
|
||||
class PreferenceWrite(BaseModel):
|
||||
scope: str = "global"
|
||||
pref_key: str
|
||||
value: str
|
||||
locked: bool | None = None
|
||||
origin: str = "manual"
|
||||
|
||||
|
||||
class ReviewImportWrite(BaseModel):
|
||||
result: dict | str
|
||||
review_id: str | None = None
|
||||
|
||||
|
||||
@router.get("/spaces")
|
||||
def spaces(session: dict = Depends(require_auth)):
|
||||
return list_user_spaces(session["profile_id"])
|
||||
|
||||
|
||||
@router.post("/spaces")
|
||||
def create_space(body: SpaceWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return create_user_space(session["profile_id"], body.title)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/spaces/{space_id}")
|
||||
def space_detail(space_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
space = get_user_space(session["profile_id"], space_id)
|
||||
return {"space": space, "days": list_days(session["profile_id"], space_id)}
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.patch("/spaces/{space_id}")
|
||||
def space_rename(space_id: str, body: SpaceWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return rename_user_space(session["profile_id"], space_id, body.title)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/spaces/{space_id}/days")
|
||||
def open_day(space_id: str, body: DayWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
day = get_or_create_day(session["profile_id"], space_id, body.calendar_date)
|
||||
return day_payload(session["profile_id"], day["id"])
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/days/{journal_day_id}")
|
||||
def read_day(journal_day_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return day_payload(session["profile_id"], journal_day_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.patch("/days/{journal_day_id}/scratch")
|
||||
def write_scratch(journal_day_id: str, body: ScratchWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return {
|
||||
"scratch": save_day_scratch(
|
||||
session["profile_id"],
|
||||
journal_day_id,
|
||||
[{"id": item.id, "text": item.text, "done": item.done} for item in body.items],
|
||||
)
|
||||
}
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/days/{journal_day_id}/conversations")
|
||||
def start_conversation(journal_day_id: str, body: ConversationWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return create_day_conversation(session["profile_id"], journal_day_id, body.title)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.delete("/conversations/{conversation_id}")
|
||||
def remove_conversation(conversation_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
result = delete_conversation(session["profile_id"], conversation_id)
|
||||
remember_dialogue_style(session["profile_id"])
|
||||
return result
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/conversations/{conversation_id}")
|
||||
def read_conversation(conversation_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
conv = get_conversation(session["profile_id"], conversation_id)
|
||||
return {"conversation": conv, "messages": list_messages(session["profile_id"], conversation_id)}
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/egress-status")
|
||||
def egress_status(session: dict = Depends(require_auth)):
|
||||
result = inspect(GatewayRequest(prompt_id=None, purpose="dialogue_turn", data_class="B"))
|
||||
return {"ready": result.allowed, "reason": result.reason, "provider": result.provider}
|
||||
|
||||
|
||||
@router.post("/conversations/{conversation_id}/turn")
|
||||
def conversation_turn(conversation_id: str, body: TurnWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return visible_for_role(
|
||||
run_turn(session["profile_id"], conversation_id, body.body, message_id=body.id),
|
||||
session.get("role"),
|
||||
)
|
||||
except (StoreError, EngineError) as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/days/{journal_day_id}/generate")
|
||||
def generate(journal_day_id: str, body: GenerateWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return visible_for_role(
|
||||
generate_draft(
|
||||
session["profile_id"],
|
||||
journal_day_id,
|
||||
conversation_ids=body.conversation_ids,
|
||||
include_existing=body.include_existing,
|
||||
explicit=True,
|
||||
),
|
||||
session.get("role"),
|
||||
)
|
||||
except (StoreError, PolicyError, EngineError) as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/entries")
|
||||
def write_entry(body: EntryWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
get_day(session["profile_id"], body.journal_day_id)
|
||||
entry = save_entry(
|
||||
session["profile_id"],
|
||||
body.journal_day_id,
|
||||
body.title,
|
||||
body.body,
|
||||
origin=body.origin,
|
||||
entry_id=body.entry_id,
|
||||
source_conversation_ids=body.source_conversation_ids,
|
||||
source_message_ids=body.source_message_ids,
|
||||
)
|
||||
remember_journal_entry(
|
||||
session["profile_id"],
|
||||
entry["id"],
|
||||
entry.get("body") or "",
|
||||
origin=body.origin,
|
||||
)
|
||||
ingest_journal(
|
||||
session["profile_id"],
|
||||
entry["id"],
|
||||
entry.get("body") or "",
|
||||
origin=body.origin,
|
||||
)
|
||||
return entry
|
||||
except (StoreError, PolicyError) as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/entries/{entry_id}")
|
||||
def read_entry(entry_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
entry = get_entry(session["profile_id"], entry_id)
|
||||
entry["media"] = list_media(session["profile_id"], entry_id)
|
||||
entry["versions"] = list_versions(session["profile_id"], entry_id)
|
||||
return entry
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/entries/{entry_id}/versions")
|
||||
def entry_versions(entry_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return list_versions(session["profile_id"], entry_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/entries/{entry_id}/restore")
|
||||
def entry_restore(entry_id: str, body: RestoreWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
entry = restore_entry(session["profile_id"], entry_id, body.version_id)
|
||||
remember_journal_entry(
|
||||
session["profile_id"],
|
||||
entry["id"],
|
||||
entry.get("body") or "",
|
||||
origin=entry.get("origin"),
|
||||
)
|
||||
ingest_journal(
|
||||
session["profile_id"],
|
||||
entry["id"],
|
||||
entry.get("body") or "",
|
||||
origin=entry.get("origin"),
|
||||
)
|
||||
return entry
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.delete("/entries/{entry_id}")
|
||||
def entry_delete(entry_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return soft_delete_entry(session["profile_id"], entry_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/entries/{entry_id}/media")
|
||||
async def entry_media(entry_id: str, file: UploadFile = File(...), session: dict = Depends(require_auth)):
|
||||
try:
|
||||
data = await file.read()
|
||||
return save_media(
|
||||
session["profile_id"],
|
||||
entry_id,
|
||||
file.filename or "media",
|
||||
file.content_type or "",
|
||||
data,
|
||||
)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/media/{media_id}")
|
||||
def read_media(media_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
row, path = get_media(session["profile_id"], media_id)
|
||||
return FileResponse(path, media_type=row["content_type"], filename=row["filename"])
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.delete("/media/{media_id}")
|
||||
def remove_media(media_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return delete_media(session["profile_id"], media_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/writing-profile")
|
||||
def writing_profile(session: dict = Depends(require_auth)):
|
||||
return get_profile(session["profile_id"])
|
||||
|
||||
|
||||
@router.patch("/writing-profile")
|
||||
def writing_patch(body: GovernanceWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return set_writing_governance(session["profile_id"], body.governance)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.patch("/writing-profile/facets/{facet_key}")
|
||||
def writing_facet(facet_key: str, body: FacetWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return update_facet(
|
||||
session["profile_id"],
|
||||
facet_key,
|
||||
value=body.value,
|
||||
locked=body.locked,
|
||||
)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.patch("/writing-profile/traits/{slug}")
|
||||
def writing_trait(slug: str, body: TraitWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return update_trait(
|
||||
session["profile_id"],
|
||||
slug,
|
||||
statement=body.statement,
|
||||
locked=body.locked,
|
||||
facet_key=body.facet_key,
|
||||
label=body.label,
|
||||
)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/suggestions/{suggestion_id}/accept")
|
||||
def writing_accept(suggestion_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return accept_writing_suggestion(session["profile_id"], suggestion_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/suggestions/{suggestion_id}/reject")
|
||||
def writing_reject(suggestion_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return reject_writing_suggestion(session["profile_id"], suggestion_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/import")
|
||||
def writing_import(body: ImportWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return ingest_import(
|
||||
session["profile_id"],
|
||||
body.body,
|
||||
occurred_at=body.occurred_at,
|
||||
context_hint=body.context_hint,
|
||||
)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/corpus")
|
||||
def writing_corpus(body: CorpusWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return import_corpus(session["profile_id"], body.items)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/writing-profile/corpus")
|
||||
def writing_corpus_list(session: dict = Depends(require_auth)):
|
||||
return {"items": list_corpus(session["profile_id"])}
|
||||
|
||||
|
||||
@router.post("/writing-profile/rebuild")
|
||||
def writing_rebuild(session: dict = Depends(require_auth)):
|
||||
return rebuild_brief(session["profile_id"])
|
||||
|
||||
|
||||
@router.get("/writing-profile/review")
|
||||
def writing_review_status(session: dict = Depends(require_auth)):
|
||||
return review_status(session["profile_id"])
|
||||
|
||||
|
||||
@router.post("/writing-profile/review/paste")
|
||||
def writing_review_paste(session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return open_paste_review(session["profile_id"])
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/initial-build/paste")
|
||||
def writing_initial_build_paste(session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return open_paste_review(session["profile_id"], mode="initial_build")
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/review/api")
|
||||
def writing_review_api(session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return run_api_review(session["profile_id"])
|
||||
except (StoreError, EngineError) as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/initial-build/api")
|
||||
def writing_initial_build_api(session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return run_api_review(session["profile_id"], mode="initial_build")
|
||||
except (StoreError, EngineError) as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/writing-profile/review/import")
|
||||
def writing_review_import(body: ReviewImportWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return import_review_result(session["profile_id"], body.result, review_id=body.review_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/writing-profile/export")
|
||||
def writing_export(session: dict = Depends(require_auth)):
|
||||
return export_writing_document(session["profile_id"])
|
||||
|
||||
|
||||
@router.post("/writing-profile/restore")
|
||||
def writing_restore(body: dict, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return restore_writing_document(session["profile_id"], body)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/interaction-profile")
|
||||
def interaction_profile(session: dict = Depends(require_auth)):
|
||||
return get_interaction_profile(session["profile_id"])
|
||||
|
||||
|
||||
@router.patch("/interaction-profile")
|
||||
def interaction_patch(body: GovernanceWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return set_interaction_governance(session["profile_id"], body.governance)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.patch("/interaction-profile/preferences")
|
||||
def interaction_pref(body: PreferenceWrite, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return upsert_preference(
|
||||
session["profile_id"],
|
||||
scope=body.scope,
|
||||
pref_key=body.pref_key,
|
||||
value=body.value,
|
||||
origin=body.origin if body.origin in {"explicit", "manual"} else "manual",
|
||||
locked=body.locked,
|
||||
)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/interaction-profile/suggestions/{suggestion_id}/accept")
|
||||
def interaction_accept(suggestion_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return accept_interaction_suggestion(session["profile_id"], suggestion_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.post("/interaction-profile/suggestions/{suggestion_id}/reject")
|
||||
def interaction_reject(suggestion_id: str, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return reject_interaction_suggestion(session["profile_id"], suggestion_id)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/interaction-profile/export")
|
||||
def interaction_export(session: dict = Depends(require_auth)):
|
||||
return export_interaction_document(session["profile_id"])
|
||||
|
||||
|
||||
@router.post("/interaction-profile/restore")
|
||||
def interaction_restore(body: dict, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return restore_interaction_document(session["profile_id"], body)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
|
||||
|
||||
@router.get("/profiles/export")
|
||||
def profiles_export(session: dict = Depends(require_auth)):
|
||||
return export_bundle(session["profile_id"])
|
||||
|
||||
|
||||
@router.post("/profiles/restore")
|
||||
def profiles_restore(body: dict, session: dict = Depends(require_auth)):
|
||||
try:
|
||||
return restore_bundle(session["profile_id"], body)
|
||||
except StoreError as exc:
|
||||
_http(exc)
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
from fastapi import APIRouter, Depends
|
||||
|
||||
from auth import require_auth
|
||||
import placeholder_mvp # noqa: F401
|
||||
import placeholder_system # noqa: F401
|
||||
from placeholders import list_placeholders
|
||||
from privacy_placeholders import catalog as privacy_catalog
|
||||
|
||||
@@ -117,6 +117,19 @@ def update_prompt(prompt_id: str, req: PromptWrite, session: dict = Depends(requ
|
||||
current = row_to_dict(conn.execute("SELECT id FROM ai_prompts WHERE id = ?", (prompt_id,)).fetchone())
|
||||
if not current:
|
||||
raise HTTPException(404, "Prompt nicht gefunden")
|
||||
if req.prompt_type == "base" and req.template:
|
||||
try:
|
||||
preview_prompt(
|
||||
{
|
||||
"id": prompt_id,
|
||||
"slug": req.slug.strip(),
|
||||
"prompt_type": req.prompt_type,
|
||||
"template": req.template,
|
||||
},
|
||||
{},
|
||||
)
|
||||
except EngineError as exc:
|
||||
_http(exc)
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE ai_prompts
|
||||
|
||||
@@ -100,6 +100,13 @@ CREATE TABLE IF NOT EXISTS conversations (
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
usage_session_id TEXT REFERENCES usage_sessions(id),
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
space_id TEXT,
|
||||
journal_day_id TEXT,
|
||||
narrative_mode TEXT NOT NULL DEFAULT '',
|
||||
reflection_depth TEXT NOT NULL DEFAULT '',
|
||||
current_focus TEXT NOT NULL DEFAULT '',
|
||||
emotional_intensity TEXT NOT NULL DEFAULT '',
|
||||
long_story INTEGER NOT NULL DEFAULT 0,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
@@ -202,3 +209,237 @@ CREATE TABLE IF NOT EXISTS identity_mappings (
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (profile_id, token)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS journal_days (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
space_id TEXT NOT NULL REFERENCES spaces(id) ON DELETE CASCADE,
|
||||
calendar_date TEXT NOT NULL,
|
||||
scratch_json TEXT NOT NULL DEFAULT '[]',
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (space_id, calendar_date)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS journal_drafts (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
journal_day_id TEXT NOT NULL REFERENCES journal_days(id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
body TEXT NOT NULL DEFAULT '',
|
||||
source_conversation_ids TEXT NOT NULL DEFAULT '[]',
|
||||
source_message_ids TEXT NOT NULL DEFAULT '[]',
|
||||
as_of TEXT NOT NULL,
|
||||
superseded_at TEXT,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS journal_entries (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
journal_day_id TEXT NOT NULL REFERENCES journal_days(id) ON DELETE CASCADE,
|
||||
space_id TEXT NOT NULL REFERENCES spaces(id) ON DELETE CASCADE,
|
||||
current_version_id TEXT,
|
||||
deleted_at TEXT,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS journal_entry_versions (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
entry_id TEXT NOT NULL REFERENCES journal_entries(id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
body TEXT NOT NULL DEFAULT '',
|
||||
source_conversation_ids TEXT NOT NULL DEFAULT '[]',
|
||||
source_message_ids TEXT NOT NULL DEFAULT '[]',
|
||||
origin TEXT NOT NULL CHECK (origin IN ('user_edit', 'accepted_draft', 'restore')),
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS journal_draft_source_refs (
|
||||
draft_id TEXT NOT NULL REFERENCES journal_drafts(id) ON DELETE CASCADE,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
source_kind TEXT NOT NULL CHECK (source_kind IN ('conversation', 'message')),
|
||||
source_id TEXT NOT NULL,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (draft_id, source_kind, source_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS journal_entry_version_source_refs (
|
||||
version_id TEXT NOT NULL REFERENCES journal_entry_versions(id) ON DELETE CASCADE,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
source_kind TEXT NOT NULL CHECK (source_kind IN ('conversation', 'message')),
|
||||
source_id TEXT NOT NULL,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (version_id, source_kind, source_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS media_assets (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
entry_id TEXT NOT NULL REFERENCES journal_entries(id) ON DELETE CASCADE,
|
||||
entry_version_id TEXT REFERENCES journal_entry_versions(id),
|
||||
filename TEXT NOT NULL DEFAULT '',
|
||||
content_type TEXT NOT NULL,
|
||||
rel_path TEXT NOT NULL,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profiles (
|
||||
profile_id TEXT PRIMARY KEY REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
compiled_brief TEXT NOT NULL DEFAULT '',
|
||||
governance TEXT NOT NULL DEFAULT 'learning' CHECK (governance IN ('learning', 'advising', 'frozen')),
|
||||
lifecycle TEXT NOT NULL DEFAULT 'uninitialized' CHECK (lifecycle IN ('uninitialized', 'initial_pending', 'confirmed')),
|
||||
version INTEGER NOT NULL DEFAULT 0,
|
||||
review_ready INTEGER NOT NULL DEFAULT 0,
|
||||
last_reviewed TEXT,
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_sources (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('imported_text', 'journal_entry', 'dialogue_style')),
|
||||
body TEXT NOT NULL DEFAULT '',
|
||||
entry_id TEXT REFERENCES journal_entries(id),
|
||||
weight REAL NOT NULL DEFAULT 1,
|
||||
occurred_at TEXT,
|
||||
context_hint TEXT NOT NULL DEFAULT '',
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_facets (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
facet_key TEXT NOT NULL,
|
||||
layer TEXT NOT NULL DEFAULT 'context' CHECK (layer IN ('core', 'context', 'output')),
|
||||
value TEXT NOT NULL DEFAULT '',
|
||||
evidence TEXT NOT NULL DEFAULT '',
|
||||
origin TEXT NOT NULL DEFAULT 'manual' CHECK (origin IN ('manual', 'imported', 'accepted_suggestion', 'initial_build')),
|
||||
locked INTEGER NOT NULL DEFAULT 0,
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (profile_id, facet_key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_traits (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
facet_key TEXT NOT NULL,
|
||||
slug TEXT NOT NULL,
|
||||
label TEXT NOT NULL DEFAULT '',
|
||||
statement TEXT NOT NULL DEFAULT '',
|
||||
origin TEXT NOT NULL DEFAULT 'manual' CHECK (origin IN ('manual', 'imported', 'accepted_suggestion', 'initial_build')),
|
||||
locked INTEGER NOT NULL DEFAULT 0,
|
||||
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'merged', 'split', 'retired')),
|
||||
observed_from TEXT,
|
||||
observed_to TEXT,
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (profile_id, slug)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_trait_refs (
|
||||
id TEXT PRIMARY KEY,
|
||||
trait_id TEXT NOT NULL REFERENCES writing_profile_traits(id) ON DELETE CASCADE,
|
||||
source_id TEXT,
|
||||
role TEXT NOT NULL CHECK (role IN ('evidence', 'exemplar')),
|
||||
excerpt TEXT NOT NULL DEFAULT '',
|
||||
occurred_at TEXT,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_suggestions (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
facet_key TEXT NOT NULL DEFAULT '',
|
||||
trait_slug TEXT NOT NULL DEFAULT '',
|
||||
action TEXT NOT NULL DEFAULT 'update',
|
||||
proposed_value TEXT NOT NULL DEFAULT '',
|
||||
evidence TEXT NOT NULL DEFAULT '',
|
||||
payload_json TEXT NOT NULL DEFAULT '{}',
|
||||
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'rejected')),
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
resolved TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS interaction_profiles (
|
||||
profile_id TEXT PRIMARY KEY REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
governance TEXT NOT NULL DEFAULT 'advising' CHECK (governance IN ('learning', 'advising', 'frozen')),
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS interaction_preferences (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
scope TEXT NOT NULL DEFAULT 'global',
|
||||
pref_key TEXT NOT NULL,
|
||||
value TEXT NOT NULL DEFAULT '',
|
||||
evidence TEXT NOT NULL DEFAULT '',
|
||||
origin TEXT NOT NULL DEFAULT 'manual' CHECK (origin IN ('explicit', 'manual', 'accepted_suggestion')),
|
||||
locked INTEGER NOT NULL DEFAULT 0,
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (profile_id, scope, pref_key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS interaction_suggestions (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
scope TEXT NOT NULL DEFAULT 'global',
|
||||
pref_key TEXT NOT NULL,
|
||||
proposed_value TEXT NOT NULL DEFAULT '',
|
||||
evidence TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'rejected')),
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
resolved TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_evidence (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
trigger TEXT NOT NULL CHECK (trigger IN (
|
||||
'user_edit_of_draft', 'heuristic_drift', 'dialogue_sample', 'periodic_batch', 'explicit_review'
|
||||
)),
|
||||
source_kind TEXT NOT NULL DEFAULT '',
|
||||
source_id TEXT,
|
||||
excerpt TEXT NOT NULL DEFAULT '',
|
||||
local_signals_json TEXT NOT NULL DEFAULT '{}',
|
||||
example_for_json TEXT NOT NULL DEFAULT '[]',
|
||||
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'bundled', 'consumed', 'dismissed')),
|
||||
created TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_reviews (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
target TEXT NOT NULL DEFAULT 'writing' CHECK (target IN ('writing', 'interaction')),
|
||||
channel TEXT NOT NULL CHECK (channel IN ('api', 'paste')),
|
||||
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'proposed', 'applied', 'dismissed')),
|
||||
package_json TEXT NOT NULL DEFAULT '{}',
|
||||
result_json TEXT,
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
resolved TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS writing_profile_versions (
|
||||
id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
seq INTEGER NOT NULL,
|
||||
governance TEXT NOT NULL DEFAULT '',
|
||||
lifecycle TEXT NOT NULL DEFAULT '',
|
||||
facets_json TEXT NOT NULL DEFAULT '[]',
|
||||
traits_json TEXT NOT NULL DEFAULT '[]',
|
||||
compiled_brief TEXT NOT NULL DEFAULT '',
|
||||
cause TEXT NOT NULL DEFAULT '',
|
||||
created TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE (profile_id, seq)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS provider_settings (
|
||||
role TEXT PRIMARY KEY CHECK (role IN ('generate', 'detect')),
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
url TEXT NOT NULL DEFAULT '',
|
||||
model TEXT NOT NULL DEFAULT '',
|
||||
zdr INTEGER NOT NULL DEFAULT 1,
|
||||
no_train INTEGER NOT NULL DEFAULT 1,
|
||||
updated TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
@@ -0,0 +1,266 @@
|
||||
"""Architecture correction pass. Run from backend/: python tests/test_architecture_correction.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-arch-correction-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from db import get_db, migrate_journal_source_refs
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
headers = header(setup.json()["token"])
|
||||
profile_id = setup.json()["profile_id"]
|
||||
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Alltag"})
|
||||
expect(space.status_code == 200, "space created")
|
||||
space_id = space.json()["id"]
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-20"},
|
||||
)
|
||||
day_id = day.json()["day"]["id"]
|
||||
conv = client.post(
|
||||
f"/api/journal/days/{day_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Quelle"},
|
||||
)
|
||||
conv_id = conv.json()["id"]
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv_id}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute war der Markt voll. Ich habe Kirschen gekauft."},
|
||||
)
|
||||
expect(turn.status_code == 200, "source turn")
|
||||
gen = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv_id]},
|
||||
)
|
||||
expect(gen.status_code == 200, "draft from conversation")
|
||||
draft = gen.json()
|
||||
expect(draft["source_conversation_ids"] == [conv_id], "draft reconstructs conversation ids")
|
||||
expect(len(draft["source_message_ids"]) >= 2, "draft reconstructs message ids")
|
||||
|
||||
with get_db() as conn:
|
||||
refs = conn.execute(
|
||||
"SELECT source_kind, source_id FROM journal_draft_source_refs WHERE draft_id = ?",
|
||||
(draft["id"],),
|
||||
).fetchall()
|
||||
kinds = {row["source_kind"] for row in refs}
|
||||
expect("conversation" in kinds and "message" in kinds, "draft provenance is relational")
|
||||
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": draft.get("title") or "Markttag",
|
||||
"body": draft.get("body") or "Kirschen",
|
||||
"origin": "accepted_draft",
|
||||
"entry_id": None,
|
||||
"source_conversation_ids": draft["source_conversation_ids"],
|
||||
"source_message_ids": draft["source_message_ids"],
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"save entry {saved.text}")
|
||||
entry = saved.json()
|
||||
expect(entry["source_conversation_ids"] == [conv_id], "entry version reconstructs conversation ids")
|
||||
expect(entry["source_message_ids"] == draft["source_message_ids"], "entry version reconstructs message ids")
|
||||
|
||||
versions = client.get(f"/api/journal/entries/{entry['id']}/versions", headers=headers)
|
||||
expect(versions.status_code == 200, "list versions")
|
||||
expect(versions.json()[0]["source_conversation_ids"] == [conv_id], "version list keeps source links")
|
||||
|
||||
legacy_draft_id = str(uuid.uuid4())
|
||||
legacy_version_id = str(uuid.uuid4())
|
||||
legacy_entry_id = str(uuid.uuid4())
|
||||
mid = draft["source_message_ids"][0]
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_drafts
|
||||
(id, profile_id, journal_day_id, title, body,
|
||||
source_conversation_ids, source_message_ids, as_of, superseded_at)
|
||||
VALUES (?, ?, ?, 'Legacy', 'alt', ?, ?, datetime('now'), datetime('now'))
|
||||
""",
|
||||
(legacy_draft_id, profile_id, day_id, json.dumps([conv_id]), json.dumps([mid])),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_entries (id, profile_id, journal_day_id, space_id)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(legacy_entry_id, profile_id, day_id, space_id),
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO journal_entry_versions
|
||||
(id, profile_id, entry_id, title, body, source_conversation_ids, source_message_ids, origin)
|
||||
VALUES (?, ?, ?, 'Legacy', 'alt', ?, ?, 'user_edit')
|
||||
""",
|
||||
(legacy_version_id, profile_id, legacy_entry_id, json.dumps([conv_id]), json.dumps([mid])),
|
||||
)
|
||||
migrate_journal_source_refs(conn)
|
||||
draft_refs = [
|
||||
(row["source_kind"], row["source_id"])
|
||||
for row in conn.execute(
|
||||
"SELECT source_kind, source_id FROM journal_draft_source_refs WHERE draft_id = ? ORDER BY sort_order",
|
||||
(legacy_draft_id,),
|
||||
).fetchall()
|
||||
]
|
||||
version_refs = [
|
||||
(row["source_kind"], row["source_id"])
|
||||
for row in conn.execute(
|
||||
"SELECT source_kind, source_id FROM journal_entry_version_source_refs WHERE version_id = ? ORDER BY sort_order",
|
||||
(legacy_version_id,),
|
||||
).fetchall()
|
||||
]
|
||||
expect(("conversation", conv_id) in draft_refs, "legacy draft conversation migrated")
|
||||
expect(("message", mid) in draft_refs, "legacy draft message migrated")
|
||||
expect(("conversation", conv_id) in version_refs, "legacy version conversation migrated")
|
||||
expect(("message", mid) in version_refs, "legacy version message migrated")
|
||||
|
||||
usage = client.post("/api/dialogue/sessions", headers=headers, json={"intent": "space-only"})
|
||||
space_conv = client.post(
|
||||
"/api/dialogue/conversations",
|
||||
headers=headers,
|
||||
json={"usage_session_id": usage.json()["id"], "title": "Plan", "space_id": space_id},
|
||||
)
|
||||
expect(space_conv.status_code == 200, f"space conversation without day {space_conv.text}")
|
||||
expect(space_conv.json()["space_id"] == space_id, "conversation assigned to space")
|
||||
expect(not space_conv.json().get("journal_day_id"), "conversation has no journal day")
|
||||
plan_turn = client.post(
|
||||
f"/api/dialogue/conversations/{space_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Wir wollen morgen zum Hafen, das ist geplant."},
|
||||
)
|
||||
expect(plan_turn.status_code == 200, "space-only turn works without journal day")
|
||||
|
||||
later = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-21"},
|
||||
)
|
||||
later_id = later.json()["day"]["id"]
|
||||
later_conv = client.post(
|
||||
f"/api/journal/days/{later_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Heute"},
|
||||
)
|
||||
reset_debug()
|
||||
later_turn = client.post(
|
||||
f"/api/journal/conversations/{later_conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute früh aufgestanden."},
|
||||
)
|
||||
expect(later_turn.status_code == 200, "later day turn")
|
||||
from privacy_gateway import debug_last
|
||||
|
||||
rendered = debug_last["rendered"]
|
||||
expect("Hafen" in rendered, "earlier space source can supply a known plan")
|
||||
expect("Heute früh aufgestanden" in rendered, "current conversation remains primary")
|
||||
|
||||
sibling = client.post(
|
||||
f"/api/journal/days/{later_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Anderes"},
|
||||
)
|
||||
reset_debug()
|
||||
sibling_turn = client.post(
|
||||
f"/api/journal/conversations/{sibling.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Nur ein kurzer Einkauf."},
|
||||
)
|
||||
expect(sibling_turn.status_code == 200, "sibling turn")
|
||||
from privacy_gateway import debug_last as last_sibling
|
||||
|
||||
sibling_rendered = last_sibling["rendered"]
|
||||
expect("Heute früh aufgestanden" not in sibling_rendered, "same-day sibling is not fully loaded")
|
||||
expect("Nur ein kurzer Einkauf" in sibling_rendered, "current sibling conversation is primary")
|
||||
|
||||
from context_builder import assemble_text, build_internal_context
|
||||
|
||||
ctx = build_internal_context(profile_id, conversation_id=later_conv.json()["id"], purpose="dialogue_turn")
|
||||
assembled = assemble_text(ctx)
|
||||
expect(not assembled.get("writing_profile"), "dialogue turn has no writing profile brief")
|
||||
expect("interaction_hint" in assembled, "dialogue turn exposes interaction hint slot")
|
||||
expect(assembled.get("interaction_hint"), "interaction hint is filled from labeled defaults")
|
||||
expect("Produktdefault" in (assembled.get("interaction_hint") or ""), "defaults are labeled, not anonymous")
|
||||
expect(any(item.get("type") == "interaction_hint" for item in ctx["items"]), "interaction slot is a context item")
|
||||
expect(
|
||||
not any(item.get("type") == "writing_profile" for item in ctx["items"]),
|
||||
"writing profile is not a dialogue kernel dependency",
|
||||
)
|
||||
expect("space_recent_sources" in {item.get("type") for item in ctx["items"]}, "space source spec is selected")
|
||||
|
||||
journal_ctx = build_internal_context(
|
||||
profile_id,
|
||||
conversation_id=conv_id,
|
||||
journal_day_id=day_id,
|
||||
space_id=space_id,
|
||||
purpose="journal_generate",
|
||||
conversation_ids=[conv_id],
|
||||
)
|
||||
expect(
|
||||
any(item.get("type") == "writing_profile" for item in journal_ctx["items"]),
|
||||
"journal generate still uses writing profile",
|
||||
)
|
||||
expect(
|
||||
not any(item.get("type") == "space_recent_sources" for item in journal_ctx["items"]),
|
||||
"journal generate does not pull space conversation excerpts",
|
||||
)
|
||||
|
||||
from conversation_signals import infer_signals, similar_enough
|
||||
from journal_policy import consolidation_offer
|
||||
|
||||
chronicle = infer_signals(["Heute Vormittag zum Markt, danach Kirschen gekauft."])
|
||||
deep = infer_signals(["Tiefer: der Spaziergang hat mich berührt."])
|
||||
expect(chronicle["narrative_mode"] == "chronicle", "chronicle mode from user text")
|
||||
expect(deep["narrative_mode"] == "reflective", "reflective mode from user text")
|
||||
expect(not similar_enough([chronicle, deep]), "different modes are not similar")
|
||||
expect(consolidation_offer([chronicle, deep]) is False, "policy does not offer unlike modes")
|
||||
twin = infer_signals(["Nach dem Markt noch Brot geholt, dann heimgegangen."])
|
||||
expect(similar_enough([chronicle, twin]), "two chronicles can be offered")
|
||||
|
||||
nav = Path(ROOT).parent / "frontend" / "src" / "config" / "appNav.js"
|
||||
nav_text = nav.read_text(encoding="utf-8")
|
||||
expect("/dialog" not in nav_text, "/dialog is not in normal product navigation")
|
||||
expect("'/journal'" in nav_text, "journal remains in product navigation")
|
||||
|
||||
print("architecture correction tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -11,6 +11,8 @@ ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-frame-test.sqlite")
|
||||
os.environ["KANSHO_PROVIDER_KEY"] = ""
|
||||
os.environ["KANSHO_DETECT_PROVIDER_KEY"] = ""
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
@@ -29,7 +31,8 @@ def auth_header(token: str) -> dict:
|
||||
|
||||
def main() -> None:
|
||||
seed_prompts = json.loads((ROOT / "config" / "prompts.seed.json").read_text(encoding="utf-8"))
|
||||
expect(seed_prompts == [], "prompt seed is empty — no domain prompts in git")
|
||||
slugs = {item["slug"] for item in seed_prompts}
|
||||
expect(slugs == {"mvp.dialogue_turn", "mvp.journal_generate", "mvp.entity_detect", "mvp.profile_review"}, "mvp prompts seeded")
|
||||
|
||||
with TestClient(app) as client:
|
||||
health = client.get("/api/health")
|
||||
@@ -75,7 +78,49 @@ def main() -> None:
|
||||
|
||||
admin = client.get("/api/admin/health", headers=headers)
|
||||
expect(admin.status_code == 200, "admin health")
|
||||
expect(admin.json()["inventory"]["prompts"] == 0, "no seeded prompts")
|
||||
expect(admin.json()["inventory"]["prompts"] == 3, "three mvp prompts")
|
||||
|
||||
providers = client.get("/api/admin/providers", headers=headers)
|
||||
expect(providers.status_code == 200, "admin providers")
|
||||
roles = {item["role"]: item for item in providers.json()["roles"]}
|
||||
expect(set(roles) == {"generate", "detect"}, "two provider roles")
|
||||
expect(roles["generate"]["ready"] is False, "generate fail-closed without key")
|
||||
expect("sk-" not in providers.text, "provider status has no secret")
|
||||
expect("KANSHO_PROVIDER_KEY=" not in providers.text, "env assignment not leaked")
|
||||
|
||||
saved = client.put(
|
||||
"/api/admin/providers/generate",
|
||||
headers=headers,
|
||||
json={
|
||||
"name": "openrouter",
|
||||
"url": "https://openrouter.ai/api/v1/chat/completions",
|
||||
"model": "openai/gpt-4o",
|
||||
"zdr": True,
|
||||
"no_train": True,
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"save generate settings {saved.text}")
|
||||
saved_roles = {item["role"]: item for item in saved.json()["roles"]}
|
||||
expect(saved_roles["generate"]["model"] == "openai/gpt-4o", "generate model persisted")
|
||||
|
||||
unknown = client.put(
|
||||
"/api/admin/providers/training",
|
||||
headers=headers,
|
||||
json={"url": "https://example.test", "model": "x"},
|
||||
)
|
||||
expect(unknown.status_code == 404, "unknown provider role")
|
||||
|
||||
os.environ["KANSHO_PROVIDER_KEY"] = "sk-test-not-a-real-key"
|
||||
try:
|
||||
from providers import generate_provider, detect_provider
|
||||
|
||||
expect(generate_provider() is not None, "generate ready after key")
|
||||
expect(detect_provider() is not None, "detect reuses generate key")
|
||||
ready = client.get("/api/admin/providers", headers=headers)
|
||||
expect("sk-test-not-a-real-key" not in ready.text, "secret never in status")
|
||||
expect(ready.json()["roles"][0]["ready"] is True, "status ready after key")
|
||||
finally:
|
||||
os.environ.pop("KANSHO_PROVIDER_KEY", None)
|
||||
|
||||
missing = client.post(
|
||||
"/api/prompts/execute",
|
||||
@@ -163,7 +208,22 @@ def main() -> None:
|
||||
placeholders = client.get("/api/placeholders", headers=user_headers)
|
||||
expect(placeholders.status_code == 200, "placeholder catalog")
|
||||
keys = {item["key"] for item in placeholders.json()["context"]}
|
||||
expect(keys == {"now"}, "only system context key now")
|
||||
expect(
|
||||
keys
|
||||
== {
|
||||
"now",
|
||||
"dialogue_context",
|
||||
"existing_text",
|
||||
"known_labels",
|
||||
"register_hint",
|
||||
"review_package",
|
||||
"source_text",
|
||||
"space_title",
|
||||
"writing_profile",
|
||||
"interaction_hint",
|
||||
},
|
||||
"system and mvp context keys",
|
||||
)
|
||||
|
||||
last_admin = client.patch(
|
||||
f"/api/users/{me.json()['id']}",
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Journal body as Markdown. Run from backend/: python tests/test_journal_body.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from journal_body import media_ids, media_token, plain_text, to_markdown
|
||||
|
||||
MEDIA_A = "11111111-1111-4111-8111-111111111111"
|
||||
MEDIA_B = "22222222-2222-4222-8222-222222222222"
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
raw = (
|
||||
"Heute war der Markt voll.\n\n"
|
||||
"- Kirschen\n"
|
||||
"- Brot\n\n"
|
||||
f"\n\n"
|
||||
"Später der **Hafen**."
|
||||
)
|
||||
expect(media_ids(raw) == [MEDIA_A], "markdown media ids")
|
||||
text = plain_text(raw)
|
||||
expect(MEDIA_A not in text, "plain text drops asset id")
|
||||
expect("kansho-media" not in text, "plain text drops media url")
|
||||
expect("Stand am Eingang" in text and "Kirschen" in text and "Hafen" in text, "wording kept")
|
||||
expect("**" not in text, "markup stripped")
|
||||
|
||||
legacy = f"Anfang\n\n[[media:{MEDIA_A}|Stand]]\n\nMitte\n\n[[media:{MEDIA_B}]]"
|
||||
converted = to_markdown(legacy)
|
||||
expect(media_token(MEDIA_A, "Stand") in converted, "legacy becomes markdown")
|
||||
expect(media_ids(legacy) == [MEDIA_A, MEDIA_B], "legacy ids")
|
||||
expect(MEDIA_A not in plain_text(legacy), "legacy ids stripped in plain text")
|
||||
expect(plain_text("nur text") == "nur text", "plain body unchanged")
|
||||
expect(media_token(MEDIA_A, "Kai") == f"", "token shape")
|
||||
from journal_body import clean_title
|
||||
|
||||
expect(clean_title("# ein langer Tag") == "ein langer Tag", "markdown heading stripped from title")
|
||||
expect(clean_title("## Morgen\nRest") == "Morgen", "first line only")
|
||||
print("journal body ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,78 @@
|
||||
"""Local journal shaping. Run from backend/: python tests/test_journal_shape.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from journal_shape import is_verbatim_join, paragraphize, shape_journal
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
turns = [
|
||||
"Heute war der Markt voll. Ich bin früh gegangen und habe Kirschen gekauft.",
|
||||
"Später zwangen wir die Kinder aus dem Bett und packten Schwimmsachen ein.",
|
||||
"Im Hafen liefen wir zuerst zur falschen Seite.",
|
||||
]
|
||||
joined = " ".join(turns)
|
||||
expect(is_verbatim_join(joined, turns), "space-joined user turns are a paste")
|
||||
expect(not is_verbatim_join("Am Markt kaufte ich Kirschen und ging dann weiter.", turns), "rewritten prose is kept")
|
||||
|
||||
title, body = shape_journal("Markttag", joined, turns)
|
||||
expect(title == "Markttag", "title stays")
|
||||
expect(body.count("\n\n") == 2, "paste becomes one paragraph per turn")
|
||||
expect("Kirschen" in body and "Hafen" in body, "wording of a paste is kept")
|
||||
|
||||
blob = (
|
||||
"Heute war der Markt voll. Ich bin früh gegangen. "
|
||||
"Danach haben wir gepackt. Die Kinder kamen spät. "
|
||||
"Im Hafen liefen wir falsch. Sushi rief zur anderen Seite. "
|
||||
"Schließlich fanden wir das Boot. Mario begrüßte uns."
|
||||
)
|
||||
phased = paragraphize(blob)
|
||||
expect("\n\n" in phased, "a long single block gets phase breaks")
|
||||
expect("Markt" in phased and "Boot" in phased, "paragraphize does not drop words")
|
||||
|
||||
kept = "Am Morgen der Markt.\n\nSpäter das Boot, noch gehetzt."
|
||||
expect(shape_journal("Tag", kept, turns)[1] == kept, "already shaped text is left alone")
|
||||
|
||||
source = [
|
||||
"Gegen 8:30 Uhr war Sushi immer noch nicht auf dem Balkon. "
|
||||
"Ich fand sie unter der Dusche. Später saß sie auf dem Bett und telefonierte. "
|
||||
"Auf die Frage, ob sie mit mir frühstücken wolle, antwortete sie, dass sie erst Brot holen müsse."
|
||||
]
|
||||
stuffed = (
|
||||
"Gegen 8:30 Uhr war Sushi immer noch nicht bei mir auf dem Balkon. "
|
||||
"Ich machte mich auf die Suche und fand Sushi schließlich unter der Dusche im Badezimmer. "
|
||||
"Als Sushi dann immer noch nicht auftauchte, sah ich nochmals nach und musste feststellen, "
|
||||
"dass Sushi auf dem Bett saß und telefonierte. Als ich gerade mit den Vorbereitungen fertig war, "
|
||||
"kam Sushi aus dem Schlafzimmer. Auf meine Frage, ob Sushi mit mir frühstücken wolle, "
|
||||
"antwortete sie, dass Sushi erst Brot vom Laden holen müsse."
|
||||
)
|
||||
from journal_shape import infer_person_gender, naturalize_person_mentions
|
||||
|
||||
expect(infer_person_gender("Sushi", source) == "f", "source pronouns set feminine")
|
||||
cleaned = naturalize_person_mentions(stuffed, ["Sushi"], source)
|
||||
expect(cleaned.count("Sushi") == 2, "name kept twice then pronouns")
|
||||
expect("fand Sushi" in cleaned, "second mention may stay a name")
|
||||
expect("dass sie erst Brot" in cleaned, "reported speech keeps sie not the name")
|
||||
expect("ob sie mit mir" in cleaned, "ob-clause is sie")
|
||||
expect("Als sie dann" in cleaned, "later subject becomes pronoun")
|
||||
expect("esse gerne Sushi" in naturalize_person_mentions(
|
||||
"Ich esse gerne Sushi mit meiner Frau Sushi. Danach aß ich Sushi.",
|
||||
["Sushi"],
|
||||
source,
|
||||
), "food homonym is not turned into a pronoun")
|
||||
print("journal shape ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,699 @@
|
||||
"""MVP journal acceptance cases A–K. Run from backend/: python tests/test_mvp_journal.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-mvp-journal-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
PNG = bytes.fromhex(
|
||||
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c489"
|
||||
"0000000a49444154789c6360000002000100ffff03000006000557bf0000000049454e44ae426082"
|
||||
)
|
||||
|
||||
|
||||
def setup_client() -> tuple[TestClient, dict, str]:
|
||||
client = TestClient(app)
|
||||
client.__enter__()
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
token = setup.json()["token"]
|
||||
return client, header(token), setup.json()["profile_id"]
|
||||
|
||||
|
||||
def open_space_day(client, headers, title="Alltag", date="2026-08-20"):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
expect(space.status_code == 200, f"create space {space.text}")
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
expect(day.status_code == 200, f"open day {day.text}")
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def start_and_turn(client, headers, day_id, body, title="Gespräch"):
|
||||
conv = client.post(
|
||||
f"/api/journal/days/{day_id}/conversations",
|
||||
headers=headers,
|
||||
json={"title": title},
|
||||
)
|
||||
expect(conv.status_code == 200, f"start conversation {conv.text}")
|
||||
reset_debug()
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": body},
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
return conv.json(), turn.json()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
client, headers, _profile_id = setup_client()
|
||||
try:
|
||||
other = client.post(
|
||||
"/api/users",
|
||||
headers=headers,
|
||||
json={"email": "ute@example.test", "name": "Ute", "password": "user-pass", "role": "user"},
|
||||
)
|
||||
expect(other.status_code == 200, "second profile")
|
||||
other_login = client.post("/api/auth/login", json={"email": "ute@example.test", "password": "user-pass"})
|
||||
other_headers = header(other_login.json()["token"])
|
||||
|
||||
space, day = open_space_day(client, headers, "Alltag", "2026-08-20")
|
||||
space_id, day_id = space["id"], day["day"]["id"]
|
||||
expect(day["day"]["calendar_date"] == "2026-08-20", "calendar date stored from client")
|
||||
again = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-20"},
|
||||
)
|
||||
expect(again.json()["day"]["id"] == day_id, "unique space+date")
|
||||
|
||||
hidden = client.get(f"/api/journal/spaces/{space_id}", headers=other_headers)
|
||||
expect(hidden.status_code == 404, "space isolated by profile")
|
||||
|
||||
conv, turn = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_id,
|
||||
"Heute war der Markt voll. Ich bin früh gegangen und habe Kirschen gekauft.",
|
||||
)
|
||||
expect(turn["user"]["body"].startswith("Heute war der Markt"), "A source user persisted")
|
||||
expect(turn["assistant"]["role"] == "assistant", "A assistant persisted")
|
||||
expect("Was davon" in turn["assistant"]["body"], "A impulse is visible text")
|
||||
expect("{" not in turn["assistant"]["body"], "A raw json not shown as message")
|
||||
expect(turn["decision"]["operation"] == "erleben_vertiefen", "A operation recorded")
|
||||
expect(turn["trace"]["egress"], "A admin sees egress")
|
||||
expect(turn["calls"] == 1, "A one generative call")
|
||||
intern = turn["trace"].get("intern") or ""
|
||||
expect("Formulierungen aus dem Dialog" not in intern, "dialogue turn does not paste other-day excerpts")
|
||||
expect("Kirschen" in intern, "current user line stays in dialogue context")
|
||||
style = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(any(item.get("kind") == "dialogue_style" for item in style["sources"]), "A dialogue style remembered")
|
||||
expect("Kirschen" in (style.get("compiled_brief") or ""), "A user wording in brief")
|
||||
expect(
|
||||
all("Was davon" not in (item.get("body") or "") for item in style["sources"]),
|
||||
"A assistant text is not a style source",
|
||||
)
|
||||
|
||||
from dialogue_turn import (
|
||||
is_completion_ask,
|
||||
is_dann_probe,
|
||||
is_day_arc_recap,
|
||||
is_echo,
|
||||
is_interview_question,
|
||||
is_machine_tell,
|
||||
is_next_beat_question,
|
||||
is_plot_continuation,
|
||||
is_pure_recap,
|
||||
is_recap_then_ask,
|
||||
is_reopen_after_close,
|
||||
is_unearned_completion,
|
||||
is_unearned_stance,
|
||||
is_verbless_echo,
|
||||
local_hold,
|
||||
needs_repair,
|
||||
user_closed_day,
|
||||
parse_turn_payload,
|
||||
visible_for_role,
|
||||
)
|
||||
|
||||
impulse, decision = parse_turn_payload('{"operation":"konkretisieren","impulse":"Wo war das?"}')
|
||||
expect(impulse == "Wo war das?" and decision["parsed"] is True, "operation json parsed")
|
||||
hidden = visible_for_role({"trace": {"egress": "x"}, "decision": decision, "calls": 1}, "user")
|
||||
expect("trace" not in hidden and "decision" not in hidden, "user does not see test spur")
|
||||
expect(is_plot_continuation("Und dann mit dem Tee auf dem Balkon?"), "echo then-question rejected")
|
||||
expect(is_plot_continuation("Ihr seid dann zur Bootstour aufgebrochen."), "invented departure rejected")
|
||||
expect(is_plot_continuation("Und dann seid ihr zur Wanderung aufgebrochen."), "invented hike rejected")
|
||||
expect(
|
||||
not is_plot_continuation(
|
||||
"Der Morgen war klar, nicht schwül. Und dann seid ihr zusammen zum Bäcker."
|
||||
),
|
||||
"restating the last shop visit is not the next beat",
|
||||
)
|
||||
expect(
|
||||
is_unearned_stance(
|
||||
"Der Morgen war klar – das hat sich sicher anders angefühlt als die Tage zuvor."
|
||||
),
|
||||
"invented comparison to other days",
|
||||
)
|
||||
expect(not is_plot_continuation("Die japanische Lektion liegt noch offen."), "companion impulse kept")
|
||||
expect(is_interview_question("Was geschah in der Zeit bis zum Start?"), "interview opener detected")
|
||||
expect(is_unearned_completion("Was holtet ihr im Laden?"), "shop completion not earned")
|
||||
expect(
|
||||
not needs_repair(
|
||||
"Was geschah in der Zeit bis zum Start?",
|
||||
{"register_hint": "dichte Chronik. Begleite, unterbrich wenig."},
|
||||
),
|
||||
"companion may ask; plot guard does not treat every question as repair",
|
||||
)
|
||||
expect(
|
||||
not needs_repair("Die Gassen kennt ihr schon fast blind.", {"register_hint": "Keine Interviewfrage"}),
|
||||
"companion sentence kept without source",
|
||||
)
|
||||
bakery = (
|
||||
"Im Laden entdeckte ich Haferflocken und Trockenobst, die ich ab morgen essen wollte."
|
||||
)
|
||||
expect(
|
||||
is_echo("Haferflocken und Trockenobst lagen für morgen bereit.", bakery),
|
||||
"restatement is echo",
|
||||
)
|
||||
expect(is_unearned_stance("Das warme Wasser fühlte sich anders an als erwartet."), "invented sensation")
|
||||
expect(is_unearned_stance("Der Weg zurück musste noch schneller gehen."), "next station as necessity")
|
||||
expect(
|
||||
not needs_repair(
|
||||
"Haferflocken und Trockenobst lagen für morgen bereit.",
|
||||
{"dialogue_context": f"user: {bakery}"},
|
||||
),
|
||||
"word overlap alone is not a plot invention",
|
||||
)
|
||||
expect(is_dann_probe("Was hat sich dann gezeigt?"), "dann-probe rejected")
|
||||
expect(is_completion_ask("Ob ihr rechtzeitig losgekommen seid."), "completion ask rejected")
|
||||
balcony = "Sushi kam auf den Balkon. Bis zum Start um 11:00 Uhr hatten wir noch Zeit."
|
||||
expect(
|
||||
is_verbless_echo("Die Zeit bis 11:00 Uhr auf dem Balkon.", balcony),
|
||||
"setting fragment is echo",
|
||||
)
|
||||
expect("Ich bleibe bei dem" not in local_hold("Wir gingen zum Bäcker."), "fallback is not a system sentence")
|
||||
expect("geöffnet" not in local_hold("Wir gingen zum Bäcker."), "fallback is spoken, not a register")
|
||||
expect("noch da" not in local_hold("Wir gingen zum Bäcker."), "fallback does not report presence")
|
||||
expect(local_hold("Wir gingen zum Bäcker.") == "Ich bin gespannt, wie es weitergeht.", "short hold invites continuation")
|
||||
long_hold = " ".join(["Wort"] * 16)
|
||||
expect(local_hold(long_hold) == "Erzähl bitte weiter.", "narrative hold asks to continue")
|
||||
expect(is_machine_tell("Danke, dass du das teilst."), "gratitude template is a machine tell")
|
||||
expect(is_machine_tell("Zusammengefasst war der Vormittag ruhig."), "summary marker is a machine tell")
|
||||
expect(not is_machine_tell("Die Nektarine als Ersatz – das setzt sich fest."), "companion thought is not a machine tell")
|
||||
expect(needs_repair("Interessant. Lass uns das gemeinsam anschauen."), "machine tell is repaired")
|
||||
expect(needs_repair("Ihr seid dann zur Bootstour aufgebrochen."), "invented departure still repaired")
|
||||
expect(is_next_beat_question("Also seid ihr dann weitergegangen?"), "second-person then-question is next beat")
|
||||
expect(is_next_beat_question("Seid ihr dann auch direkt gesprungen?"), "completion of an intention is next beat")
|
||||
expect(not is_next_beat_question("Was gab's dann zu Mittag?"), "a short next-thought question is not a plot beat")
|
||||
expect(not is_next_beat_question("Wie war das dann mit der Gruppe?"), "dann in a felt question stays")
|
||||
oats = (
|
||||
"Ich habe wie geplant meine Haferflocken mit Trockenobst. "
|
||||
"Sushi hat Wäsche gewaschen und ich habe am Tagebuch gearbeitet. "
|
||||
"Die Kinder sind gegen 12 Uhr aus dem Bett gekrochen und wollten Mittag essen."
|
||||
)
|
||||
recap_ask = (
|
||||
"Die Haferflocken scheinen die neue Lösung zu sein. Und dann Wäsche, Tagebuch, "
|
||||
"die Kinder, Rouven. Was gab's dann zu Mittag?"
|
||||
)
|
||||
expect(
|
||||
is_recap_then_ask(recap_ask, {"dialogue_context": f"user: {oats}"}),
|
||||
"recap plus what-next is an interview",
|
||||
)
|
||||
expect(
|
||||
needs_repair(recap_ask, {"dialogue_context": f"user: {oats}"}),
|
||||
"interview recap is repaired",
|
||||
)
|
||||
expect(
|
||||
not needs_repair(
|
||||
"Was gab's dann zu Mittag?",
|
||||
{"dialogue_context": f"user: {oats}"},
|
||||
),
|
||||
"short next-thought impulse without recap stays",
|
||||
)
|
||||
recap_only = (
|
||||
"Haferflocken mit Trockenobst, dann Wäsche und Tagebuch, "
|
||||
"danach die Arbeit, bis die Kinder aus dem Bett wollten Mittag essen."
|
||||
)
|
||||
expect(
|
||||
is_pure_recap(recap_only, {"dialogue_context": f"user: {oats}"}),
|
||||
"dense restatement without a break is a recap",
|
||||
)
|
||||
expect(
|
||||
needs_repair(recap_only, {"dialogue_context": f"user: {oats}"}),
|
||||
"pure recap without contradiction is repaired",
|
||||
)
|
||||
expect(
|
||||
not is_pure_recap(
|
||||
"Die Haferflocken bleiben die neue Lösung.",
|
||||
{"dialogue_context": f"user: {oats}"},
|
||||
),
|
||||
"a thought derived from the last thread is not a recap",
|
||||
)
|
||||
close_ctx = {
|
||||
"dialogue_context": (
|
||||
"user: Morgens nach der Japanischlektion kam jemand auf den Balkon.\n"
|
||||
"user: Später die Bootstour, vier Badestopps, gegrillter Fisch und singende Gäste.\n"
|
||||
"user: Wir genossen noch die Atmosphäre und machten uns dann müde "
|
||||
"und voll von den überwältigenden Eindrücken des Tages auf den Heimweg."
|
||||
)
|
||||
}
|
||||
expect(
|
||||
user_closed_day(
|
||||
{"dialogue_context": "user: Wir gingen zurück zur Wohnung und legten uns dann auch bald ins Bett."}
|
||||
),
|
||||
"ins Bett closes the day",
|
||||
)
|
||||
expect(
|
||||
not user_closed_day(
|
||||
{"dialogue_context": "user: Nach dem Essen gingen die Kinder nach Hause und wir wollten noch zum Hafen."}
|
||||
),
|
||||
"nach Hause mid-day is not a close",
|
||||
)
|
||||
expect(
|
||||
needs_repair(
|
||||
"Was war das für ein Gefühl, müde heimzugehen mit all diesen Eindrücken?",
|
||||
close_ctx,
|
||||
),
|
||||
"feeling question after close is repaired",
|
||||
)
|
||||
expect(
|
||||
is_reopen_after_close(
|
||||
"Du hattest morgens nicht ahnen können, dann Bootstour, Badestopps, Essen, Italiener.",
|
||||
close_ctx,
|
||||
),
|
||||
"day-arc recap after close is repaired",
|
||||
)
|
||||
expect(is_day_arc_recap("Morgens Japanisch, Balkon, Bootstour und Badestopps.", close_ctx), "arc uses earlier stations")
|
||||
expect(not needs_repair("Dann bleibe ich bei diesem Heimweg.", close_ctx), "hold at the close stays")
|
||||
expect("Schluss" in local_hold(close_ctx["dialogue_context"].split("user:")[-1]), "close fallback holds the end")
|
||||
from context_builder import infer_register
|
||||
|
||||
expect("Schluss" in infer_register(["Wir machten uns müde auf den Heimweg."]), "register sees the close first")
|
||||
originals = client.get(f"/api/journal/conversations/{conv['id']}", headers=headers)
|
||||
expect(len(originals.json()["messages"]) == 2, "A two source messages")
|
||||
|
||||
reset_debug()
|
||||
draft = client.post(f"/api/journal/days/{day_id}/generate", headers=headers, json={"conversation_ids": [conv["id"]]})
|
||||
expect(draft.status_code == 200, f"A generate {draft.text}")
|
||||
expect("Markt" in draft.json()["body"] or "Kirschen" in draft.json()["body"], "A draft keeps user words")
|
||||
from privacy_gateway import debug_last as last_a
|
||||
|
||||
expect(last_a is not None, "A generate reached gateway")
|
||||
expect("Kirschen" in last_a["rendered"] or "Markt" in last_a["rendered"], "A generate sees user words")
|
||||
expect("assistant:" in last_a["rendered"], "A generate sees Kanshō impulses as reply context")
|
||||
expect("Stilquellen" in last_a["rendered"] or "Dialog" in last_a["rendered"], "A style brief reaches generate")
|
||||
style_part = last_a["rendered"].split("Dialogquellen:")[0]
|
||||
expect("Erzählmerkmale" in style_part, "A first generate still gets form hints")
|
||||
expect(
|
||||
"Heute war der Markt voll" not in style_part,
|
||||
"A source dialogue is not pasted a second time as style",
|
||||
)
|
||||
current_version = None
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Markttag",
|
||||
"body": "Heute war der Markt voll. Ich bin früh gegangen.",
|
||||
"origin": "user_edit",
|
||||
"source_conversation_ids": [conv["id"]],
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"A save {saved.text}")
|
||||
current_version = saved.json()["current_version_id"]
|
||||
expect(saved.json()["body"].startswith("Heute war der Markt"), "A user edit stored")
|
||||
after_save = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
brief = after_save.get("compiled_brief") or ""
|
||||
expect("Finale Journal Entries" in brief, "A saved entry is strongest style source")
|
||||
entry_at = brief.find("Finale Journal Entries")
|
||||
dialogue_at = brief.find("Formulierungen aus dem Dialog")
|
||||
expect(dialogue_at == -1 or entry_at < dialogue_at, "A entries listed before dialogue style")
|
||||
space_view = client.get(f"/api/journal/spaces/{space_id}", headers=headers)
|
||||
dates = [item["calendar_date"] for item in space_view.json()["days"]]
|
||||
expect("2026-08-20" in dates, "A day appears in space chronology")
|
||||
|
||||
later = client.post(
|
||||
f"/api/journal/conversations/{conv['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Am nächsten Kalendertag fällt mir noch der Wind ein."},
|
||||
)
|
||||
expect(later.status_code == 200, "B continue same conversation")
|
||||
day_again = client.get(f"/api/journal/days/{day_id}", headers=headers)
|
||||
expect(day_again.json()["day"]["calendar_date"] == "2026-08-20", "B journal day stays calendar day")
|
||||
msgs = client.get(f"/api/journal/conversations/{conv['id']}", headers=headers).json()["messages"]
|
||||
expect(any("Wind" in item["body"] for item in msgs), "B later message kept")
|
||||
expect(msgs[-1]["created"] != "2026-08-20" or True, "B message time is independent")
|
||||
|
||||
prior_space, prior_day = open_space_day(client, headers, "Urlaub", "2026-08-01")
|
||||
prior_conv, _ = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
prior_day["day"]["id"],
|
||||
"Morgen gehe ich an den Hafen, wenn das Wetter hält.",
|
||||
title="Plan",
|
||||
)
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": prior_day["day"]["id"],
|
||||
"title": "Plan",
|
||||
"body": "Morgen gehe ich an den Hafen, wenn das Wetter hält.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
next_day = client.post(
|
||||
f"/api/journal/spaces/{prior_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-02"},
|
||||
)
|
||||
reset_debug()
|
||||
_conv_c, turn_c = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
next_day.json()["day"]["id"],
|
||||
"Der Vormittag war ruhig.",
|
||||
)
|
||||
from privacy_gateway import debug_last as last_c
|
||||
|
||||
expect(last_c is not None, "C gateway saw a request")
|
||||
expect("Hafen" in last_c["rendered"], "C prior plan in context")
|
||||
expect("nicht geschehen" in last_c["rendered"], "C not-mentioned rule in prompt")
|
||||
|
||||
reset_debug()
|
||||
dense = "Erster Absatz mit vielen Details über die Anreise.\n\nZweiter Absatz über das Licht am Abend.\n\nDritter Absatz über das Essen."
|
||||
conv_d, turn_d = start_and_turn(client, headers, day_id, dense, title="Dicht")
|
||||
from privacy_gateway import debug_calls as calls_d
|
||||
|
||||
expect(calls_d == 1, f"D exactly one call, got {calls_d}")
|
||||
expect(sum(1 for item in turn_d["messages"] if item["role"] == "assistant" and item["conversation_id"] == conv_d["id"]) >= 1, "D one assistant")
|
||||
expect(turn_d["calls"] == 1, "D turn reports one call")
|
||||
|
||||
reset_debug()
|
||||
_conv_e, turn_e = start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_id,
|
||||
"Ich bin traurig über den Abschied, das ist klar.",
|
||||
title="Emotion",
|
||||
)
|
||||
from privacy_gateway import debug_last as last_e
|
||||
|
||||
expect("Motivationen erfinden" in last_e["rendered"], "E no invented emotion rule")
|
||||
expect(turn_e["calls"] == 1, "E one follow-up")
|
||||
|
||||
day_f = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-21"},
|
||||
)
|
||||
day_f_id = day_f.json()["day"]["id"]
|
||||
conv_f1, _ = start_and_turn(client, headers, day_f_id, "Chronik: Spaziergang und Einkauf.", title="Chronik")
|
||||
conv_f2, _ = start_and_turn(client, headers, day_f_id, "Tiefer: der Spaziergang hat mich berührt.", title="Tiefe")
|
||||
gen_f1 = client.post(
|
||||
f"/api/journal/days/{day_f_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv_f1["id"]]},
|
||||
)
|
||||
save_f1 = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_f_id, "title": "Chronik", "body": gen_f1.json()["body"], "origin": "accepted_draft"},
|
||||
)
|
||||
gen_f2 = client.post(
|
||||
f"/api/journal/days/{day_f_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv_f2["id"]]},
|
||||
)
|
||||
save_f2 = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_f_id, "title": "Tiefe", "body": gen_f2.json()["body"], "origin": "accepted_draft"},
|
||||
)
|
||||
day_f_view = client.get(f"/api/journal/days/{day_f_id}", headers=headers).json()
|
||||
expect(len(day_f_view["entries"]) == 2, "F two separate entries")
|
||||
expect(save_f1.json()["id"] != save_f2.json()["id"], "F distinct entry ids")
|
||||
|
||||
expect(day_f_view["consolidation_offer"] is False, "G no offer when chronicle and deep reflection differ")
|
||||
gen_g = client.post(f"/api/journal/days/{day_f_id}/generate", headers=headers, json={})
|
||||
expect(len(gen_g.json()["source_conversation_ids"]) == 1, "G generate without selection does not merge")
|
||||
|
||||
day_g2 = client.post(
|
||||
f"/api/journal/spaces/{space_id}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-19"},
|
||||
)
|
||||
day_g2_id = day_g2.json()["day"]["id"]
|
||||
start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_g2_id,
|
||||
"Heute Vormittag zum Markt, danach Kirschen gekauft.",
|
||||
title="Morgen",
|
||||
)
|
||||
start_and_turn(
|
||||
client,
|
||||
headers,
|
||||
day_g2_id,
|
||||
"Nach dem Markt noch Brot geholt, dann heimgegangen.",
|
||||
title="Abend",
|
||||
)
|
||||
day_g2_view = client.get(f"/api/journal/days/{day_g2_id}", headers=headers).json()
|
||||
expect(day_g2_view["consolidation_offer"] is True, "G offer when two chronicle conversations match")
|
||||
|
||||
space_h, day_h = open_space_day(client, headers, "Kleinwalsertal 2026", "2026-08-21")
|
||||
expect(day_h["day"]["id"] != day_f_id, "H same date different space is other day")
|
||||
listed_alltag = {item["id"] for item in client.get(f"/api/journal/spaces/{space_id}", headers=headers).json()["days"]}
|
||||
listed_kw = {item["id"] for item in client.get(f"/api/journal/spaces/{space_h['id']}", headers=headers).json()["days"]}
|
||||
expect(day_f_id in listed_alltag and day_h["day"]["id"] not in listed_alltag, "H days stay in own space")
|
||||
expect(day_h["day"]["id"] in listed_kw and day_f_id not in listed_kw, "H no date merge across spaces")
|
||||
|
||||
imported = client.post(
|
||||
"/api/journal/writing-profile/import",
|
||||
headers=headers,
|
||||
json={"body": "Ich schreibe knapp und ohne Pathos."},
|
||||
)
|
||||
expect(imported.status_code == 200, f"I import {imported.text}")
|
||||
kinds = {item["kind"] for item in imported.json()["sources"]}
|
||||
expect("imported_text" in kinds, "I import is a source")
|
||||
expect(any("knapp" in (item.get("body") or "") for item in imported.json()["sources"]), "I import body kept")
|
||||
expect(imported.json()["compiled_brief"] and "knapp" in imported.json()["compiled_brief"], "I brief uses import")
|
||||
reset_debug()
|
||||
gen_i = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv["id"]]},
|
||||
)
|
||||
expect(gen_i.status_code == 200, "I generate after import")
|
||||
from privacy_gateway import debug_last as last_i
|
||||
|
||||
expect("knapp" in last_i["rendered"], "I style brief reaches generate")
|
||||
profile = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(all(item.get("kind") != "journal_draft" for item in profile["sources"]), "I no draft kind")
|
||||
expect(not any((item.get("body") or "") == gen_i.json()["body"] for item in profile["sources"]), "I draft not a style source")
|
||||
expect(any(item.get("kind") == "journal_entry" for item in profile["sources"]), "I user-final entry is a source")
|
||||
|
||||
first = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_id, "entry_id": saved.json()["id"], "title": "v1", "body": "Erste Fassung.", "origin": "user_edit"},
|
||||
)
|
||||
second = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={"journal_day_id": day_id, "entry_id": saved.json()["id"], "title": "v2", "body": "Zweite Fassung.", "origin": "user_edit"},
|
||||
)
|
||||
versions = client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json()
|
||||
expect(len(versions) >= 3, f"J version history kept ({len(versions)})")
|
||||
restore = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/restore",
|
||||
headers=headers,
|
||||
json={"version_id": first.json()["current_version_id"]},
|
||||
)
|
||||
expect(restore.status_code == 200, f"J restore {restore.text}")
|
||||
expect(restore.json()["body"] == "Erste Fassung.", "J restored body")
|
||||
versions_after = client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json()
|
||||
expect(len(versions_after) == len(versions) + 1, "J restore appends a version")
|
||||
before_gen = restore.json()["current_version_id"]
|
||||
client.post(f"/api/journal/days/{day_id}/generate", headers=headers, json={"conversation_ids": [conv["id"]]})
|
||||
after_gen = client.get(f"/api/journal/entries/{saved.json()['id']}", headers=headers).json()
|
||||
expect(after_gen["current_version_id"] == before_gen, "J generate does not overwrite entry")
|
||||
expect(after_gen["body"] == "Erste Fassung.", "J user text remains")
|
||||
day_with_draft = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
expect(day_with_draft.get("current_draft"), "J generate stores a draft beside the entry")
|
||||
expect(
|
||||
(day_with_draft["current_draft"] or {}).get("body") != after_gen["body"],
|
||||
"J new draft is not the saved user text",
|
||||
)
|
||||
n_before = len(client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json())
|
||||
adopted = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": saved.json()["id"],
|
||||
"title": "# Neue Überschrift",
|
||||
"body": (day_with_draft["current_draft"] or {}).get("body") or "Entwurf",
|
||||
"origin": "accepted_draft",
|
||||
},
|
||||
)
|
||||
expect(adopted.status_code == 200, f"J adopt draft {adopted.text}")
|
||||
expect(adopted.json()["title"] == "Neue Überschrift", "J hash stripped from stored title")
|
||||
listed = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
titles = [item.get("title") or "" for item in listed.get("entries") or []]
|
||||
expect(any("Neue Überschrift" in title for title in titles), "J day list shows cleaned title")
|
||||
n_after = len(client.get(f"/api/journal/entries/{saved.json()['id']}/versions", headers=headers).json())
|
||||
expect(n_after == n_before + 1, "J adopt appends a version on the same entry")
|
||||
|
||||
look = client.get(f"/api/journal/spaces/{prior_space['id']}", headers=headers).json()
|
||||
expect(any(item["calendar_date"] == "2026-08-01" for item in look["days"]), "K space chronology")
|
||||
opened = client.post(
|
||||
f"/api/journal/spaces/{prior_space['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-01"},
|
||||
)
|
||||
expect(opened.json()["entries"], "K entry opens by date")
|
||||
source = client.get(
|
||||
f"/api/journal/conversations/{opened.json()['conversations'][0]['id']}",
|
||||
headers=headers,
|
||||
)
|
||||
expect(source.status_code == 200 and source.json()["messages"], "K jump to source dialog")
|
||||
|
||||
throwaway, _ = start_and_turn(client, headers, day_id, "Nur ein Testdialog zum Entfernen.", title="Wegwerf")
|
||||
blocked = client.delete(f"/api/journal/conversations/{throwaway['id']}", headers=other_headers)
|
||||
expect(blocked.status_code == 404, "delete isolated by profile")
|
||||
removed = client.delete(f"/api/journal/conversations/{throwaway['id']}", headers=headers)
|
||||
expect(removed.status_code == 200 and removed.json().get("deleted"), "conversation deleted")
|
||||
after_delete = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
expect(all(item["id"] != throwaway["id"] for item in after_delete["conversations"]), "deleted conversation leaves the day")
|
||||
expect(any(item["id"] == conv["id"] for item in after_delete["conversations"]), "other conversations remain")
|
||||
missing = client.get(f"/api/journal/conversations/{throwaway['id']}", headers=headers)
|
||||
expect(missing.status_code == 404, "deleted conversation is gone")
|
||||
|
||||
media = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/media",
|
||||
headers=headers,
|
||||
files={"file": ("shot.png", PNG, "image/png")},
|
||||
)
|
||||
expect(media.status_code == 200, f"media upload {media.text}")
|
||||
fetched = client.get(f"/api/journal/media/{media.json()['id']}", headers=headers)
|
||||
expect(fetched.status_code == 200 and fetched.content[:8] == b"\x89PNG\r\n\x1a\n", "media served")
|
||||
other_media = client.get(f"/api/journal/media/{media.json()['id']}", headers=other_headers)
|
||||
expect(other_media.status_code == 404, "media isolated")
|
||||
expect(media.json().get("kind") == "image", "image kind")
|
||||
|
||||
video = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/media",
|
||||
headers=headers,
|
||||
files={"file": ("clip.webm", b"webm-bytes-not-a-container", "video/webm")},
|
||||
)
|
||||
expect(video.status_code == 200, f"video upload {video.text}")
|
||||
expect(video.json().get("kind") == "video", "video kind")
|
||||
served_video = client.get(f"/api/journal/media/{video.json()['id']}", headers=headers)
|
||||
expect(served_video.status_code == 200 and served_video.headers["content-type"].startswith("video/"), "video served")
|
||||
|
||||
rejected = client.post(
|
||||
f"/api/journal/entries/{saved.json()['id']}/media",
|
||||
headers=headers,
|
||||
files={"file": ("note.pdf", b"%PDF-1.4", "application/pdf")},
|
||||
)
|
||||
expect(rejected.status_code == 400, "pdf rejected")
|
||||
|
||||
media_id = media.json()["id"]
|
||||
video_id = video.json()["id"]
|
||||
embedded = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": saved.json()["id"],
|
||||
"title": "Markttag",
|
||||
"body": (
|
||||
"Heute war der Markt voll.\n\n"
|
||||
f"[[media:{media_id}|Kirschenstand]]\n\n"
|
||||
"Danach der Hafen.\n\n"
|
||||
f"[[media:{video_id}|Steg]]"
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(embedded.status_code == 200, "embedded body saved")
|
||||
expect(f"" in embedded.json()["body"], "image token stored as markdown")
|
||||
expect(f"" in embedded.json()["body"], "video token stored as markdown")
|
||||
style_after = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
brief = style_after.get("compiled_brief") or ""
|
||||
sources_blob = " ".join(item.get("body") or "" for item in style_after.get("sources") or [])
|
||||
expect(media_id not in brief and video_id not in brief, "asset ids stay out of writing brief")
|
||||
expect(media_id not in sources_blob, "asset ids stay out of style sources")
|
||||
expect("Kirschenstand" in brief or "Kirschenstand" in sources_blob, "caption remains as wording")
|
||||
|
||||
scratch_mark = "SCRATCHTOKEN-NEVER-EGRESS-xyz"
|
||||
scratch = client.patch(
|
||||
f"/api/journal/days/{day_id}/scratch",
|
||||
headers=headers,
|
||||
json={"items": [{"text": scratch_mark, "done": False}]},
|
||||
)
|
||||
expect(scratch.status_code == 200 and scratch.json()["scratch"][0]["text"] == scratch_mark, "scratch saved")
|
||||
hidden_scratch = client.patch(
|
||||
f"/api/journal/days/{day_id}/scratch",
|
||||
headers=other_headers,
|
||||
json={"items": [{"text": "fremd", "done": False}]},
|
||||
)
|
||||
expect(hidden_scratch.status_code == 404, "scratch isolated")
|
||||
day_after = client.get(f"/api/journal/days/{day_id}", headers=headers).json()
|
||||
expect(any(item["text"] == scratch_mark for item in day_after.get("scratch") or []), "scratch on day payload")
|
||||
reset_debug()
|
||||
turn_scratch = client.post(
|
||||
f"/api/journal/conversations/{conv['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Noch ein Satz zum Abend."},
|
||||
)
|
||||
expect(turn_scratch.status_code == 200, "turn after scratch")
|
||||
from privacy_gateway import debug_last as last_scratch_turn
|
||||
blob = " ".join(
|
||||
str(last_scratch_turn.get(key) or "")
|
||||
for key in ("rendered", "masked", "mask_input", "raw", "reply")
|
||||
)
|
||||
intern = (turn_scratch.json().get("trace") or {}).get("intern") or ""
|
||||
expect(scratch_mark not in blob and scratch_mark not in intern, "scratch not in dialogue egress")
|
||||
reset_debug()
|
||||
gen_scratch = client.post(
|
||||
f"/api/journal/days/{day_id}/generate",
|
||||
headers=headers,
|
||||
json={"conversation_ids": [conv["id"]]},
|
||||
)
|
||||
expect(gen_scratch.status_code == 200, "generate after scratch")
|
||||
from privacy_gateway import debug_last as last_scratch_gen
|
||||
gen_blob = " ".join(
|
||||
str(last_scratch_gen.get(key) or "")
|
||||
for key in ("rendered", "masked", "mask_input", "raw", "reply")
|
||||
)
|
||||
gen_intern = (gen_scratch.json().get("trace") or {}).get("intern") or ""
|
||||
expect(scratch_mark not in gen_blob and scratch_mark not in gen_intern, "scratch not in generate egress")
|
||||
|
||||
from media_store import strip_jpeg_exif
|
||||
|
||||
jpeg = bytearray(b"\xff\xd8\xff\xe1\x00\x10Exif\x00\x00AAAA\xff\xda\x00\x08\x01\x01\x00\x00\xff\xd9")
|
||||
stripped = strip_jpeg_exif(bytes(jpeg))
|
||||
expect(b"Exif" not in stripped, "jpeg exif stripped")
|
||||
|
||||
print("All MVP journal tests passed.")
|
||||
finally:
|
||||
client.__exit__(None, None, None)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,258 @@
|
||||
"""Detection + mask + demask. Run from backend/: python tests/test_privacy_detect.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-privacy-detect-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from identity_store import list_mappings
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
headers = {"X-Auth-Token": setup.json()["token"]}
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": "Alltag"})
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": "2026-08-22"},
|
||||
)
|
||||
conv = client.post(
|
||||
f"/api/journal/days/{day.json()['day']['id']}/conversations",
|
||||
headers=headers,
|
||||
json={"title": "Gespräch"},
|
||||
)
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Ich war mit Anna in Hamburg und danach allein."},
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
from privacy_gateway import debug_last
|
||||
|
||||
expect(debug_last is not None, "gateway recorded request")
|
||||
expect(debug_last["detect_provider"] == "fake-detect", "detect provider is separate")
|
||||
expect("Anna" not in debug_last["masked"], "Anna masked before generate")
|
||||
expect("Hamburg" not in debug_last["masked"], "Hamburg masked before generate")
|
||||
expect("[[PERSON:" in debug_last["masked"] or "[[PLACE:" in debug_last["masked"], "placeholders in egress")
|
||||
expect("Anna" in turn.json()["user"]["body"], "source keeps cleartext")
|
||||
mappings = list_mappings(setup.json()["profile_id"])
|
||||
labels = {item["local_label"] for item in mappings}
|
||||
expect("Anna" in labels, "Anna stored in local dictionary")
|
||||
expect("Hamburg" in labels, "Hamburg stored in local dictionary")
|
||||
expect(all(not (item.get("token") or "").startswith("[[") or True for item in mappings), "tokens stored")
|
||||
|
||||
reset_debug()
|
||||
again = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Anna war ruhig."},
|
||||
)
|
||||
expect(again.status_code == 200, "second turn")
|
||||
from privacy_gateway import debug_last as last2
|
||||
|
||||
expect("nicht" in last2["masked"].lower() or "Anna" not in last2["masked"], "dictionary remasks Anna without new invention")
|
||||
expect(last2["mapping_count"] >= 2, "dictionary reused")
|
||||
|
||||
from entity_detect import _pattern_entities
|
||||
from identity_store import is_maskable_label
|
||||
from privacy_gateway import _mask
|
||||
|
||||
patterned = {item["text"] for item in _pattern_entities("Ich war mit Anna in Hamburg.", set())}
|
||||
expect("Anna" in patterned and "Hamburg" in patterned, "pattern finds proper names")
|
||||
expect("Himmel" not in {item["text"] for item in _pattern_entities("Der Himmel war klar.", set())}, "pattern skips weather")
|
||||
|
||||
expect(not is_maskable_label("ich"), "pronoun ich is not a mapping")
|
||||
expect(not is_maskable_label("user"), "role user is not a mapping")
|
||||
expect(not is_maskable_label("Himmel"), "weather is not identity")
|
||||
expect(not is_maskable_label("Der Himmel"), "determiner plus weather is not identity")
|
||||
expect(not is_maskable_label("6:00 Uhr"), "clock time is not identity")
|
||||
expect(not is_maskable_label("meinen Tee"), "tea is not identity")
|
||||
expect(not is_maskable_label("Relativ"), "sentence adverb is not identity")
|
||||
expect(not is_maskable_label("Allerdings"), "conjunction is not identity")
|
||||
expect(not is_maskable_label("Tages"), "common noun is not identity")
|
||||
expect(not is_maskable_label("Brote"), "food is not identity")
|
||||
expect(not is_maskable_label("Überlegen"), "common noun is not identity")
|
||||
expect(not is_maskable_label("Ortes"), "place word is not identity")
|
||||
expect(not is_maskable_label("Zwiebeln"), "food is not identity")
|
||||
expect(not is_maskable_label("letzten"), "function word letzten is not identity")
|
||||
expect(not is_maskable_label("Getöse"), "collective noun is not identity")
|
||||
expect(not is_maskable_label("Schiff"), "vessel is not identity")
|
||||
expect(not is_maskable_label("Faden"), "prompt word is not identity")
|
||||
expect("Getöse" not in {item["text"] for item in _pattern_entities("Zweimaster mit Getöse im Hafen.", set())}, "mit plus common noun is not a name")
|
||||
expect("Anna" in {item["text"] for item in _pattern_entities("Ich war mit Anna im Hafen.", set())}, "mit plus given name stays")
|
||||
expect(is_maskable_label("Sushi"), "given name stays maskable")
|
||||
expect(is_maskable_label("meine Frau Sushi"), "kinship stripped, name remains")
|
||||
from identity_store import coalesce_mappings, normalize_token, remember_mapping
|
||||
from db import get_db
|
||||
import uuid
|
||||
|
||||
expect(normalize_token("PERSON:KURZ") is None, "template token KURZ rejected")
|
||||
first = remember_mapping(setup.json()["profile_id"], "meine Frau Sushi", "PERSON:08")
|
||||
second = remember_mapping(setup.json()["profile_id"], "Sushi", "PERSON:KURZ")
|
||||
expect(first["token"] == second["token"], "alias reuses token")
|
||||
expect(second["local_label"] == "Sushi", "canonical label is the name")
|
||||
extra_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO identity_mappings (id, profile_id, token, local_label)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(extra_id, setup.json()["profile_id"], "PERSON:KURZ", "meine Frau Sushi"),
|
||||
)
|
||||
coalesce_mappings(setup.json()["profile_id"])
|
||||
sushi_rows = [
|
||||
item
|
||||
for item in list_mappings(setup.json()["profile_id"])
|
||||
if (item.get("local_label") or "").lower() == "sushi"
|
||||
]
|
||||
expect(len(sushi_rows) == 1, "coalesce keeps one Sushi row")
|
||||
expect(sushi_rows[0]["token"].upper() != "PERSON:KURZ", "stable token wins over KURZ")
|
||||
joined = _mask(
|
||||
"meine Frau Sushi kam. Sushi stellte fest.",
|
||||
sushi_rows,
|
||||
)
|
||||
expect(joined.count(f"[[{sushi_rows[0]['token']}]]") == 2, "both mentions share one placeholder")
|
||||
expect("Sushi" not in joined, "name fully masked")
|
||||
expect("meine Frau " in joined, "kinship stays visible")
|
||||
homonym = _mask(
|
||||
"Ich esse gerne Sushi mit meiner Frau Sushi.",
|
||||
sushi_rows,
|
||||
)
|
||||
expect("esse gerne Sushi" in homonym, "food homonym stays")
|
||||
expect("Frau [[" in homonym or f"Frau [[{sushi_rows[0]['token']}]]" in homonym, "kinship name is masked")
|
||||
expect(homonym.count("[[") == 1, "only the person mention is masked")
|
||||
eating = _mask("Heute nur von Sushi essen erzählt.", sushi_rows)
|
||||
expect("Sushi essen" in eating, "noun before eat stays food")
|
||||
expect("[[" not in eating, "food-only sentence is not the person")
|
||||
from pronoun_bind import bind_user_lines, bind_pronouns
|
||||
from privacy_gateway import mask_for_egress
|
||||
|
||||
anna = [{"local_label": "Anna", "token": "PERSON:01"}]
|
||||
bound = bind_user_lines(
|
||||
_mask(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
)
|
||||
)
|
||||
expect(bound.count("[[PERSON:01]]") == 3, "name and both pronouns share one token")
|
||||
expect("Sie" not in bound and "ihr" not in bound, "pronouns rebound to person")
|
||||
journal_masked = mask_for_egress(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
"journal_generate",
|
||||
)
|
||||
expect(journal_masked.count("[[PERSON:01]]") == 1, "journal masks the name only")
|
||||
expect("Sie setzte" in journal_masked and "ihr Tee" in journal_masked, "journal keeps anaphora")
|
||||
dialogue_masked = mask_for_egress(
|
||||
"user: Ich war mit Anna.\nuser: Sie setzte sich zu mir, und ich gab ihr Tee.",
|
||||
anna,
|
||||
"dialogue_turn",
|
||||
)
|
||||
expect(dialogue_masked.count("[[PERSON:01]]") == 3, "dialogue still binds pronouns")
|
||||
instruction = bind_user_lines(
|
||||
"user: Anna kam.\n"
|
||||
"Der Impuls darf eine Frage sein. Er darf keine neue Station setzen.\n"
|
||||
)
|
||||
expect("Er darf keine neue Station" in instruction, "instruction pronoun stays")
|
||||
instruction_name = _mask(
|
||||
"Nenne Berg nicht in der Anweisung.\nuser: Berg kam vorbei.",
|
||||
[{"local_label": "Berg", "token": "PERSON:02"}],
|
||||
personal_lines_only=True,
|
||||
)
|
||||
expect("Nenne Berg nicht" in instruction_name, "instruction line is not remasked")
|
||||
expect("[[PERSON:02]]" in instruction_name, "user line still remasked")
|
||||
herr = [{"local_label": "Berg", "token": "PERSON:02"}]
|
||||
male, _ = bind_pronouns(_mask("Herr Berg kam. Er setzte sich.", herr))
|
||||
expect(male.count("[[PERSON:02]]") == 2, "masculine pronoun follows last man")
|
||||
address, _ = bind_pronouns(_mask("Anna kam. Seid ihr schon da?", anna))
|
||||
expect("ihr" in address, "address ihr is not a person")
|
||||
plural, _ = bind_pronouns(_mask("Anna kam. Sie waren zu dritt.", anna))
|
||||
expect("Sie waren" in plural or "sie waren" in plural.lower(), "plural sie stays")
|
||||
croatia = (
|
||||
"Heute bin ich ca. gegen 6:00 Uhr aufgestanden. Relativ früh gesellte sich "
|
||||
"auch meine Frau Sushi zu mir. Allerdings sollte die Bootstour erst um 11:00 Uhr starten."
|
||||
)
|
||||
patterned_live = {item["text"] for item in _pattern_entities(croatia, set())}
|
||||
expect("Sushi" in patterned_live, "pattern keeps given name")
|
||||
expect(
|
||||
patterned_live <= {"Sushi"},
|
||||
f"pattern does not treat German function words as names: {patterned_live}",
|
||||
)
|
||||
from entity_detect import uses_llm_detect
|
||||
from providers import ProviderConfig
|
||||
|
||||
remote = ProviderConfig(
|
||||
role="detect",
|
||||
name="openrouter",
|
||||
mode="http",
|
||||
url="https://openrouter.ai/api/v1/chat/completions",
|
||||
model="x",
|
||||
key="sk-test",
|
||||
local=False,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
expect(uses_llm_detect(remote), "development allows remote detect")
|
||||
os.environ["KANSHO_ENV"] = "production"
|
||||
expect(not uses_llm_detect(remote), "production blocks remote detect")
|
||||
local = ProviderConfig(
|
||||
role="detect",
|
||||
name="ollama",
|
||||
mode="http",
|
||||
url="http://127.0.0.1:11434/v1/chat/completions",
|
||||
model="x",
|
||||
key="",
|
||||
local=True,
|
||||
zdr=True,
|
||||
no_train=True,
|
||||
)
|
||||
expect(uses_llm_detect(local), "production still allows local detect")
|
||||
os.environ.pop("KANSHO_ENV", None)
|
||||
poisoned = _mask(
|
||||
"Nicht erwähnt ist nicht geschehen. Ich kochte Tee.",
|
||||
[{"local_label": "ich", "token": "PERSON:05"}],
|
||||
)
|
||||
expect("N[[PERSON:05]]t" not in poisoned, "ich does not split nicht")
|
||||
expect("Nicht erwähnt" in poisoned, "instruction word bleibt")
|
||||
from privacy_gateway import PrivacyGatewayError, _demask, _validate_response
|
||||
|
||||
mapped = [{"local_label": "Anna", "token": "PERSON:01"}]
|
||||
kept = _validate_response("[[PERSON:01]] war ruhig.", mapped)
|
||||
expect(kept.startswith("[[PERSON:01]]"), "placeholders pass validation")
|
||||
expect(_demask(kept, mapped) == "Anna war ruhig.", "demask after validation")
|
||||
blocked = False
|
||||
try:
|
||||
_validate_response("Anna war ruhig.", mapped)
|
||||
except PrivacyGatewayError as exc:
|
||||
blocked = exc.code == "response_validation_failed"
|
||||
expect(blocked, "plaintext identity is blocked before demask")
|
||||
print("All privacy detect tests passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,289 @@
|
||||
"""Writing/Interaction profile governance. Run from backend/: python tests/test_profile_governance.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-profile-governance-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from interaction_profile_store import propose_observation
|
||||
from privacy_gateway import reset_debug
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
def open_space_day(client, headers, title="Alltag", date="2026-08-20"):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
expect(space.status_code == 200, f"create space {space.text}")
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
expect(day.status_code == 200, f"open day {day.text}")
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def facet_map(profile: dict) -> dict:
|
||||
return {item["facet_key"]: item for item in profile.get("facets") or []}
|
||||
|
||||
|
||||
def trait_map(profile: dict) -> dict:
|
||||
return {item["slug"]: item for item in profile.get("traits") or []}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
expect(setup.status_code == 200, "setup")
|
||||
headers = header(setup.json()["token"])
|
||||
profile_id = setup.json()["profile_id"]
|
||||
_space, day = open_space_day(client, headers)
|
||||
day_id = day["day"]["id"]
|
||||
|
||||
saved = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Markt",
|
||||
"body": "Heute um 7:30 Uhr bin ich zum Markt. Danach habe ich Kirschen gekauft.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(saved.status_code == 200, f"save {saved.text}")
|
||||
writing = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(writing["governance"] == "learning", "default writing governance is learning")
|
||||
expect(writing.get("lifecycle") == "uninitialized", "save does not confirm the profile")
|
||||
expect(any(item["kind"] == "journal_entry" for item in writing["sources"]), "saved entry is a source")
|
||||
expect("Finale Journal Entries" in (writing.get("compiled_brief") or ""), "brief lists saved entries")
|
||||
expect("Kirschen" in (writing.get("compiled_brief") or ""), "entry wording reaches brief")
|
||||
expect("rhythm" not in facet_map(writing), "heuristics do not persist as facets")
|
||||
expect(not trait_map(writing), "save does not invent semantic traits")
|
||||
|
||||
locked = client.patch(
|
||||
"/api/journal/writing-profile/facets/rhythm",
|
||||
headers=headers,
|
||||
json={"locked": True},
|
||||
)
|
||||
expect(locked.status_code == 200, f"lock {locked.text}")
|
||||
expect("rhythm" in trait_map(locked.json()), "legacy style key becomes a trait")
|
||||
expect(trait_map(locked.json())["rhythm"]["locked"] in {True, 1}, "rhythm locked")
|
||||
|
||||
other = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Kurz",
|
||||
"body": "Ok. Gut. Fertig. Punkt. Schluss. Ende. Still. Knapp.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(other.status_code == 200, "second entry")
|
||||
after_second = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(after_second.get("lifecycle") == "uninitialized", "more entries still do not confirm")
|
||||
expect(trait_map(after_second)["rhythm"]["locked"] in {True, 1}, "locked trait survives later saves")
|
||||
|
||||
client.patch(
|
||||
"/api/journal/writing-profile/facets/rhythm",
|
||||
headers=headers,
|
||||
json={"locked": False},
|
||||
)
|
||||
frozen = client.patch(
|
||||
"/api/journal/writing-profile",
|
||||
headers=headers,
|
||||
json={"governance": "frozen"},
|
||||
)
|
||||
expect(frozen.json()["governance"] == "frozen", "frozen set")
|
||||
rhythm_frozen = trait_map(frozen.json())["rhythm"].get("statement") or ""
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Anders",
|
||||
"body": (
|
||||
"Vielleicht bedeutet das weniger, als es scheint. Allerdings wunderte ich mich. "
|
||||
"Im Grunde war der Tag merkwürdig und irgendwie offen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
still_frozen = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(
|
||||
(trait_map(still_frozen)["rhythm"].get("statement") or "") == rhythm_frozen,
|
||||
"frozen keeps trait values",
|
||||
)
|
||||
|
||||
advising = client.patch(
|
||||
"/api/journal/writing-profile",
|
||||
headers=headers,
|
||||
json={"governance": "advising"},
|
||||
)
|
||||
expect(advising.json()["governance"] == "advising", "advising set")
|
||||
imported = client.post(
|
||||
"/api/journal/writing-profile/import",
|
||||
headers=headers,
|
||||
json={
|
||||
"body": "Haha das war irgendwie lustig. Witzig, wirklich witzig, lol.",
|
||||
"occurred_at": "2019-07-12",
|
||||
"context_hint": "vacation_diary",
|
||||
},
|
||||
)
|
||||
expect(imported.status_code == 200, f"import {imported.text}")
|
||||
expect(any(item["kind"] == "imported_text" for item in imported.json()["sources"]), "import stored")
|
||||
expect(
|
||||
int(imported.json().get("pending_evidence") or 0) == 0,
|
||||
"unconfirmed profile does not start continuous review",
|
||||
)
|
||||
vacation = next(item for item in imported.json()["sources"] if item["kind"] == "imported_text")
|
||||
expect((vacation.get("occurred_at") or "").startswith("2019-07-12"), "import keeps time")
|
||||
expect(vacation.get("context_hint") == "autobiographical_journal", "vacation diary maps to journal facet")
|
||||
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "learning"})
|
||||
conv = client.post(f"/api/journal/days/{day_id}/conversations", headers=headers, json={"title": "Chat"})
|
||||
turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Nur ein kurzer Satz ohne Gewicht."},
|
||||
)
|
||||
expect(turn.status_code == 200, f"turn {turn.text}")
|
||||
after_turn = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
brief = after_turn.get("compiled_brief") or ""
|
||||
expect("Finale Journal Entries" in brief, "entries remain strongest after later dialogue")
|
||||
expect("Formulierungen aus dem Dialog" not in brief, "dialogue excerpts drop once entries exist")
|
||||
expect("Kirschen" in brief, "earlier journal wording remains")
|
||||
|
||||
interaction = client.get("/api/journal/interaction-profile", headers=headers).json()
|
||||
expect(interaction["governance"] == "advising", "interaction default is advising")
|
||||
expect(interaction["preferences"] == [], "turn does not invent personal preferences")
|
||||
expect(interaction["suggestions"] == [], "silence is not an interaction suggestion")
|
||||
expect(any(item["pref_key"] == "reply_length" for item in interaction["defaults"]), "labeled product defaults")
|
||||
|
||||
from privacy_gateway import debug_last
|
||||
|
||||
expect("Produktdefault" in (debug_last["rendered"] if debug_last else ""), "hint reaches dialogue prompt")
|
||||
expect("Ein bis drei Sätze" in (debug_last["rendered"] if debug_last else ""), "length default is in hint not anonymous")
|
||||
expect("anonyme Produktregel" in (debug_last["rendered"] if debug_last else ""), "prompt labels the moved preference")
|
||||
|
||||
proposed = propose_observation(
|
||||
profile_id,
|
||||
scope="autobiographical",
|
||||
pref_key="listening",
|
||||
proposed_value="Bei längeren Erzählungen zunächst stärker zuhören.",
|
||||
evidence="Beobachtung, kein automatisches Lernen",
|
||||
)
|
||||
expect(proposed and proposed["suggestions"], "observation is a suggestion only")
|
||||
expect(proposed["preferences"] == [], "observation is not auto-applied")
|
||||
rejected = client.post(
|
||||
f"/api/journal/interaction-profile/suggestions/{proposed['suggestions'][0]['id']}/reject",
|
||||
headers=headers,
|
||||
)
|
||||
expect(rejected.json()["suggestions"] == [], "rejected observation stays out of the profile")
|
||||
|
||||
personal = client.patch(
|
||||
"/api/journal/interaction-profile/preferences",
|
||||
headers=headers,
|
||||
json={
|
||||
"scope": "global",
|
||||
"pref_key": "reply_length",
|
||||
"value": "Meist nur ein Satz.",
|
||||
"origin": "explicit",
|
||||
},
|
||||
)
|
||||
expect(personal.status_code == 200, f"personal pref {personal.text}")
|
||||
expect(personal.json()["preferences"][0]["origin"] == "explicit", "explicit origin kept")
|
||||
reset_debug()
|
||||
later = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": "Heute Vormittag zum Markt, danach Kirschen, dann noch der Hafen bei 18:00 Uhr."},
|
||||
)
|
||||
expect(later.status_code == 200, "second turn")
|
||||
from privacy_gateway import debug_last as last_hint
|
||||
|
||||
expect("Meist nur ein Satz" in last_hint["rendered"], "personal preference reaches hint")
|
||||
expect("persönlich" in last_hint["rendered"], "personal override is labeled")
|
||||
expect(later.json()["conversation"].get("narrative_mode"), "dialogue state remains automatic")
|
||||
expect("long_story" in later.json()["conversation"], "long_story is conversation state")
|
||||
|
||||
manual = client.patch(
|
||||
"/api/journal/writing-profile/facets/humor",
|
||||
headers=headers,
|
||||
json={"value": "trocken, selten, nie aufgesetzt"},
|
||||
)
|
||||
expect("humor" in trait_map(manual.json()), "manual humor is a trait")
|
||||
expect(trait_map(manual.json())["humor"]["origin"] == "manual", "manual trait origin")
|
||||
expect("trocken, selten" in (manual.json().get("compiled_brief") or ""), "manual trait reaches brief")
|
||||
|
||||
exported = client.get("/api/journal/writing-profile/export", headers=headers).json()
|
||||
expect(exported["kind"] == "kansho.writing_profile", "writing export kind")
|
||||
expect(exported["format_version"] == 1, "writing export version")
|
||||
expect(any(item["slug"] == "humor" and "trocken" in (item.get("statement") or "") for item in exported["traits"]), "export keeps manual trait")
|
||||
expect("profile_id" not in exported, "export is portable")
|
||||
expect("sources" not in exported, "export is not a journal backup")
|
||||
|
||||
client.patch("/api/journal/writing-profile/facets/humor", headers=headers, json={"value": "wird überschrieben"})
|
||||
restored = client.post("/api/journal/writing-profile/restore", headers=headers, json=exported)
|
||||
expect(restored.status_code == 200, f"writing restore {restored.text}")
|
||||
expect(trait_map(restored.json())["humor"]["statement"].startswith("trocken"), "restore replaces traits")
|
||||
expect(trait_map(restored.json())["humor"]["origin"] == "manual", "restore keeps origin")
|
||||
|
||||
wrong = client.post(
|
||||
"/api/journal/writing-profile/restore",
|
||||
headers=headers,
|
||||
json={"kind": "kansho.interaction_profile", "format_version": 1, "preferences": []},
|
||||
)
|
||||
expect(wrong.status_code == 400, "writing restore rejects interaction document")
|
||||
|
||||
interaction_doc = client.get("/api/journal/interaction-profile/export", headers=headers).json()
|
||||
expect(interaction_doc["kind"] == "kansho.interaction_profile", "interaction export kind")
|
||||
expect(interaction_doc["preferences"][0]["value"] == "Meist nur ein Satz.", "interaction export keeps personal pref")
|
||||
client.patch(
|
||||
"/api/journal/interaction-profile/preferences",
|
||||
headers=headers,
|
||||
json={"scope": "global", "pref_key": "reply_length", "value": "Ganz anders.", "origin": "manual"},
|
||||
)
|
||||
interaction_back = client.post("/api/journal/interaction-profile/restore", headers=headers, json=interaction_doc)
|
||||
expect(interaction_back.json()["preferences"][0]["value"] == "Meist nur ein Satz.", "interaction restore replaces prefs")
|
||||
|
||||
bundle = client.get("/api/journal/profiles/export", headers=headers).json()
|
||||
expect(bundle["kind"] == "kansho.profiles", "bundle kind")
|
||||
bundle["writing"]["traits"] = [
|
||||
item if item["slug"] != "humor" else {**item, "statement": "aus dem Sammeldokument", "locked": True}
|
||||
for item in bundle["writing"]["traits"]
|
||||
]
|
||||
both = client.post("/api/journal/profiles/restore", headers=headers, json=bundle)
|
||||
expect(both.status_code == 200, f"bundle restore {both.text}")
|
||||
expect(trait_map(both.json()["writing"])["humor"]["statement"] == "aus dem Sammeldokument", "bundle restores writing")
|
||||
expect(trait_map(both.json()["writing"])["humor"]["locked"] in {True, 1}, "bundle restores lock")
|
||||
expect(both.json()["interaction"]["preferences"][0]["value"] == "Meist nur ein Satz.", "bundle restores interaction")
|
||||
|
||||
print("profile governance ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,427 @@
|
||||
"""Cost-conscious profile review. Run from backend/: python tests/test_profile_review.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
os.environ["KANSHO_DB_PATH"] = str(Path(tempfile.gettempdir()) / "kansho-profile-review-test.sqlite")
|
||||
os.environ["KANSHO_FAKE_PROVIDER"] = "1"
|
||||
os.environ["KANSHO_FAKE_DETECT"] = "1"
|
||||
Path(os.environ["KANSHO_DB_PATH"]).unlink(missing_ok=True)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from privacy_gateway import reset_debug
|
||||
from profile_review import (
|
||||
KIND_PACKAGE,
|
||||
KIND_RESULT,
|
||||
facet_delta,
|
||||
is_strong_edit,
|
||||
parse_result,
|
||||
)
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def header(token: str) -> dict:
|
||||
return {"X-Auth-Token": token}
|
||||
|
||||
|
||||
def open_space_day(client, headers, title="Alltag", date="2026-08-20"):
|
||||
space = client.post("/api/journal/spaces", headers=headers, json={"title": title})
|
||||
expect(space.status_code == 200, f"create space {space.text}")
|
||||
day = client.post(
|
||||
f"/api/journal/spaces/{space.json()['id']}/days",
|
||||
headers=headers,
|
||||
json={"calendar_date": date},
|
||||
)
|
||||
expect(day.status_code == 200, f"open day {day.text}")
|
||||
return space.json(), day.json()
|
||||
|
||||
|
||||
def trait_map(profile: dict) -> dict:
|
||||
return {item["slug"]: item for item in profile.get("traits") or []}
|
||||
|
||||
|
||||
def facet_map(profile: dict) -> dict:
|
||||
return {item["facet_key"]: item for item in profile.get("facets") or []}
|
||||
|
||||
|
||||
def pending_triggers(status: dict) -> set[str]:
|
||||
return {item.get("trigger") for item in status.get("pending_evidence") or []}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
expect(not is_strong_edit("Heute Kirschen am Markt gekauft.", "Heute Kirschen am Markt gekauft."), "same text is not a strong edit")
|
||||
expect(
|
||||
is_strong_edit(
|
||||
"Heute war der Markt voll und ich habe Kirschen gekauft.",
|
||||
"Vielleicht bedeutet das weniger. Allerdings wunderte ich mich den ganzen Abend.",
|
||||
),
|
||||
"divergent rewrite is a strong edit",
|
||||
)
|
||||
expect(facet_delta({"rhythm": "kurz"}, {"rhythm": "lang", "humor": "trocken"}) == ["rhythm", "humor"], "delta lists changed local signals")
|
||||
parsed_core = parse_result({
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"changes": [{"layer": "core", "key": "ignored", "action": "keep", "evidence_ids": []}],
|
||||
})
|
||||
expect(parsed_core["changes"][0]["key"] == "core", "core layer maps to core key")
|
||||
parsed_create = parse_result({
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"changes": [{
|
||||
"layer": "trait",
|
||||
"slug": "scene_cuts",
|
||||
"facet_key": "autobiographical_journal",
|
||||
"action": "create",
|
||||
"proposed_value": "wechselt ohne Ankündigung zwischen Szenen",
|
||||
"exemplars": [{"excerpt": "Danach der Hafen."}],
|
||||
"evidence_ids": ["e1"],
|
||||
}],
|
||||
})
|
||||
expect(parsed_create["changes"][0]["slug"] == "scene_cuts", "dynamic trait slugs are accepted")
|
||||
expect(parsed_create["changes"][0]["action"] == "create", "create remains create")
|
||||
|
||||
reset_debug()
|
||||
with TestClient(app) as client:
|
||||
setup = client.post(
|
||||
"/api/auth/setup",
|
||||
json={"email": "lars@example.test", "name": "Lars", "password": "test-pass"},
|
||||
)
|
||||
expect(setup.status_code == 200, "setup")
|
||||
headers = header(setup.json()["token"])
|
||||
_space, day = open_space_day(client, headers)
|
||||
day_id = day["day"]["id"]
|
||||
|
||||
first = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Markt",
|
||||
"body": "Heute um 7:30 Uhr bin ich zum Markt. Danach habe ich Kirschen gekauft.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(first.status_code == 200, "first save")
|
||||
writing = client.get("/api/journal/writing-profile", headers=headers).json()
|
||||
expect(writing.get("lifecycle") == "uninitialized", "first save leaves profile unconfirmed")
|
||||
expect("rhythm" not in {item.get("facet_key") for item in writing.get("facets") or []}, "no heuristic facet bootstrap")
|
||||
expect(not writing.get("traits"), "no inferred traits")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect(status["pending_evidence"] == [], "first save does not enqueue a review")
|
||||
|
||||
second = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Anders",
|
||||
"body": (
|
||||
"Haha das war irgendwie lustig, lol. Vielleicht bedeutet das weniger, als es scheint. "
|
||||
"Allerdings wunderte ich mich. Im Grunde war der Abend merkwürdig und offen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(second.status_code == 200, "second save")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect(status["pending_evidence"] == [], "continuous queue waits for confirmed profile")
|
||||
|
||||
frozen = client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "frozen"})
|
||||
expect(frozen.status_code == 200, "frozen")
|
||||
client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Frozen",
|
||||
"body": "Ganz anderer Ton, witzig, lol, haha, und trotzdem keine Review.",
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect(status["pending_evidence"] == [], "unconfirmed frozen still has no queue")
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "learning"})
|
||||
|
||||
draft = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Draft",
|
||||
"body": (
|
||||
"Heute war der Markt voll und ich bin früh gegangen und habe Kirschen gekauft "
|
||||
"und danach den Hafen gesehen, ganz ruhig und ohne Eile."
|
||||
),
|
||||
"origin": "accepted_draft",
|
||||
},
|
||||
)
|
||||
expect(draft.status_code == 200, "accepted draft stored")
|
||||
edited = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": draft.json()["journal_day_id"] if False else day_id,
|
||||
"entry_id": draft.json()["id"],
|
||||
"title": "Draft",
|
||||
"body": (
|
||||
"Der Nachmittag kippte. Ich blieb länger als geplant, sprach kaum, "
|
||||
"und schrieb später nur zwei knappe Zeilen ohne Markt und ohne Hafen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(edited.status_code == 200, "strong user edit")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("user_edit_of_draft" not in pending_triggers(status), "draft edit waits until the profile is confirmed")
|
||||
|
||||
conv = client.post(f"/api/journal/days/{day_id}/conversations", headers=headers, json={"title": "Chat"})
|
||||
long_body = (
|
||||
"Heute früh bin ich zum Markt gegangen und habe Kirschen gekauft, danach den Hafen gesehen, "
|
||||
"später noch Brot geholt, mit jemandem gesprochen, Tee getrunken, den Wind bemerkt, "
|
||||
"und am Abend den Tag in Ruhe nachvollzogen, ohne etwas zu erzwingen oder nach einem Muster zu suchen. "
|
||||
"Zwischendurch war eine Pause, dann noch ein kurzer Blick aufs Wasser, und zum Schluss bin ich langsam heimgegangen, "
|
||||
"ohne den Faden zu verlieren und ohne aus dem Gesprächsverlauf eine dauerhafte Vorliebe zu machen."
|
||||
)
|
||||
expect(len(long_body.split()) >= 60, "sample text is long enough")
|
||||
reset_debug()
|
||||
long_turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": long_body},
|
||||
)
|
||||
expect(long_turn.status_code == 200, "long turn")
|
||||
from privacy_gateway import debug_last as last_long
|
||||
|
||||
expect(last_long["purpose"] == "dialogue_turn", "long turn still has no review call")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("dialogue_sample" not in pending_triggers(status), "dialogue is not sampled before confirm")
|
||||
|
||||
corpus = client.post(
|
||||
"/api/journal/writing-profile/corpus",
|
||||
headers=headers,
|
||||
json={
|
||||
"items": [
|
||||
{
|
||||
"body": "Am ersten Urlaubstag um 6:00 Uhr Tee auf dem Balkon, danach der Hafen. Kirschen vom Markt.",
|
||||
"occurred_at": "2018-07-02",
|
||||
"context_hint": "vacation_diary",
|
||||
},
|
||||
{
|
||||
"body": "Haha das war irgendwie lustig, lol. Der Abend kippte, ohne dass ich es erklärt hätte.",
|
||||
"occurred_at": "2024-08-11",
|
||||
"context_hint": "vacation_diary",
|
||||
},
|
||||
{
|
||||
"body": "Vielleicht bedeutet das weniger. Allerdings wunderte ich mich über die Stille nach dem Markt.",
|
||||
"occurred_at": "2025-03-04",
|
||||
"context_hint": "autobiographical_journal",
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
expect(corpus.status_code == 200, f"corpus import {corpus.text}")
|
||||
dated = [item for item in corpus.json()["sources"] if item.get("kind") == "imported_text"]
|
||||
expect(len(dated) >= 3, "historical corpus is more than two entries")
|
||||
expect(any((item.get("occurred_at") or "").startswith("2018-07-02") for item in dated), "older source keeps time")
|
||||
expect(any(item.get("recency_role") == "long_term" for item in dated), "older text is long-term evidence")
|
||||
|
||||
paste = client.post("/api/journal/writing-profile/initial-build/paste", headers=headers)
|
||||
expect(paste.status_code == 200, f"initial paste {paste.text}")
|
||||
package = paste.json()["package"]
|
||||
expect(package["kind"] == KIND_PACKAGE, "paste uses review package contract")
|
||||
expect(package["format_version"] == 1, "package version")
|
||||
expect(package["mode"] == "initial_build", "unconfirmed profile opens an initial build")
|
||||
expect(package["existing_before_new"], "package names Existing-before-New")
|
||||
expect(any(item.get("kind") == "trait_hint" for item in package["seed_catalog"]), "seed catalog is ordering help")
|
||||
expect(len(package.get("corpus") or []) >= 3, "initial package includes historical corpus")
|
||||
expect(any(item.get("occurred_at") for item in package["corpus"]), "corpus items keep time")
|
||||
instruction = package.get("instruction") or ""
|
||||
expect("bestehenden Trait bestätigen" in instruction, "instruction starts with confirm")
|
||||
expect("erst dann einen neuen Trait anlegen" in instruction, "instruction defers create")
|
||||
expect("autobiographical_journal" in instruction, "vacation/journal facet is named")
|
||||
expect("kansho.profile_review_result" in (paste.json().get("prompt") or ""), "copy prompt names the result contract")
|
||||
|
||||
evidence_id = (package.get("evidences") or [{}])[0].get("id")
|
||||
imported = client.post(
|
||||
"/api/journal/writing-profile/review/import",
|
||||
headers=headers,
|
||||
json={
|
||||
"review_id": paste.json()["id"],
|
||||
"result": {
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"target": "writing",
|
||||
"mode": "initial_build",
|
||||
"changes": [
|
||||
{
|
||||
"layer": "trait",
|
||||
"slug": "dry_humor",
|
||||
"facet_key": "autobiographical_journal",
|
||||
"action": "create",
|
||||
"label": "Trockener Humor",
|
||||
"proposed_value": "gelegentlich trocken, aus Paste-Review",
|
||||
"rationale": "Journal-Evidenz, kein globaler Core.",
|
||||
"evidence_ids": [evidence_id] if evidence_id else [],
|
||||
"exemplars": [{"excerpt": "haha das war irgendwie lustig", "occurred_at": "2024-08-11"}],
|
||||
}
|
||||
],
|
||||
},
|
||||
},
|
||||
)
|
||||
expect(imported.status_code == 200, f"import result {imported.text}")
|
||||
expect(imported.json()["applied"] >= 1, "initial build applies traits")
|
||||
profile = imported.json()["profile"]
|
||||
expect(profile.get("lifecycle") == "confirmed", "successful initial build confirms the profile")
|
||||
expect(trait_map(profile)["dry_humor"]["statement"].startswith("gelegentlich trocken"), "created trait is stored")
|
||||
expect(trait_map(profile)["dry_humor"]["facet_key"] == "autobiographical_journal", "vacation evidence stays in journal facet")
|
||||
expect(any("haha" in (ref.get("excerpt") or "") for ref in trait_map(profile)["dry_humor"].get("exemplars") or []), "trait keeps exemplar")
|
||||
expect("core" not in facet_map(profile) or not (facet_map(profile).get("core") or {}).get("value"), "single source kind does not invent a global core")
|
||||
|
||||
drift = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Drift",
|
||||
"body": (
|
||||
"Haha das war irgendwie lustig, lol. Vielleicht bedeutet das weniger, als es scheint. "
|
||||
"Allerdings wunderte ich mich. Im Grunde war der Abend merkwürdig und offen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(drift.status_code == 200, "drift save after confirm")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("heuristic_drift" in pending_triggers(status), "after confirm, local heuristic queues evidence")
|
||||
|
||||
draft_after = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"title": "Draft nach Confirm",
|
||||
"body": (
|
||||
"Heute war der Markt voll und ich bin früh gegangen und habe Kirschen gekauft "
|
||||
"und danach den Hafen gesehen, ganz ruhig und ohne Eile."
|
||||
),
|
||||
"origin": "accepted_draft",
|
||||
},
|
||||
)
|
||||
expect(draft_after.status_code == 200, "accepted draft after confirm")
|
||||
edited = client.post(
|
||||
"/api/journal/entries",
|
||||
headers=headers,
|
||||
json={
|
||||
"journal_day_id": day_id,
|
||||
"entry_id": draft_after.json()["id"],
|
||||
"title": "Draft nach Confirm",
|
||||
"body": (
|
||||
"Der Nachmittag kippte noch einmal. Ich blieb, sprach kaum, "
|
||||
"und schrieb später nur zwei knappe Zeilen ohne Markt und ohne Hafen und ohne Kirschen."
|
||||
),
|
||||
"origin": "user_edit",
|
||||
},
|
||||
)
|
||||
expect(edited.status_code == 200, "strong user edit after confirm")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("user_edit_of_draft" in pending_triggers(status), "strong draft edit is a review trigger")
|
||||
|
||||
reset_debug()
|
||||
long_turn = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": long_body},
|
||||
)
|
||||
expect(long_turn.status_code == 200, "long turn after confirm")
|
||||
expect(long_turn.json()["conversation"].get("preferences") in (None, []), "turn does not write interaction prefs")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
expect("dialogue_sample" in pending_triggers(status), "long dialogue may be sampled locally")
|
||||
pending_samples = [item for item in status["pending_evidence"] if item["trigger"] == "dialogue_sample"]
|
||||
second_long = client.post(
|
||||
f"/api/journal/conversations/{conv.json()['id']}/turn",
|
||||
headers=headers,
|
||||
json={"body": long_body + " Noch ein Nachsatz über den Abend und die Kirschen."},
|
||||
)
|
||||
expect(second_long.status_code == 200, "second long turn")
|
||||
status = client.get("/api/journal/writing-profile/review", headers=headers).json()
|
||||
samples_now = [item for item in status["pending_evidence"] if item["trigger"] == "dialogue_sample"]
|
||||
expect(len(samples_now) == len(pending_samples), "dialogue sample has a cooldown")
|
||||
|
||||
interaction = client.get("/api/journal/interaction-profile", headers=headers).json()
|
||||
expect(interaction["preferences"] == [], "dialogue sample is not an interaction preference")
|
||||
expect(interaction["suggestions"] == [], "silence and flow are not interaction learning")
|
||||
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "advising"})
|
||||
client.post(
|
||||
"/api/journal/writing-profile/import",
|
||||
headers=headers,
|
||||
json={"body": "Witzig, wirklich witzig, haha das war irgendwie lustig, lol, und trotzdem nur Candidate."},
|
||||
)
|
||||
reset_debug()
|
||||
api_review = client.post("/api/journal/writing-profile/review/api", headers=headers)
|
||||
expect(api_review.status_code == 200, f"api review {api_review.text}")
|
||||
from privacy_gateway import debug_last as api_trace
|
||||
|
||||
expect(api_trace["purpose"] == "profile_review", "api review uses dedicated purpose")
|
||||
expect(api_trace["layer"] == "profilreview", "trace is not a dialogue turn")
|
||||
expect(api_review.json()["suggested"] >= 1, "advising queues review as suggestion")
|
||||
expect(
|
||||
any("trocken" in (item.get("proposed_value") or "") for item in api_review.json()["profile"]["suggestions"]),
|
||||
"api result is a suggestion in advising",
|
||||
)
|
||||
expect(
|
||||
"aus Paste-Review" in trait_map(api_review.json()["profile"])["dry_humor"]["statement"],
|
||||
"advising does not silently overwrite",
|
||||
)
|
||||
|
||||
client.patch("/api/journal/writing-profile", headers=headers, json={"governance": "frozen"})
|
||||
client.post("/api/journal/writing-profile/review/paste", headers=headers)
|
||||
humor_before = trait_map(client.get("/api/journal/writing-profile", headers=headers).json())["dry_humor"]["statement"]
|
||||
frozen_import = client.post(
|
||||
"/api/journal/writing-profile/review/import",
|
||||
headers=headers,
|
||||
json={
|
||||
"result": {
|
||||
"kind": KIND_RESULT,
|
||||
"format_version": 1,
|
||||
"changes": [
|
||||
{
|
||||
"layer": "trait",
|
||||
"slug": "dry_humor",
|
||||
"action": "precisify",
|
||||
"proposed_value": "sollte nicht greifen",
|
||||
"rationale": "frozen",
|
||||
"evidence_ids": [],
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
)
|
||||
expect(frozen_import.status_code == 200, "frozen import accepted as document")
|
||||
expect(frozen_import.json()["applied"] == 0, "frozen does not apply review")
|
||||
expect(trait_map(frozen_import.json()["profile"])["dry_humor"]["statement"] == humor_before, "frozen keeps trait")
|
||||
|
||||
bad = client.post(
|
||||
"/api/journal/writing-profile/review/import",
|
||||
headers=headers,
|
||||
json={"result": {"kind": "kansho.writing_profile", "format_version": 1, "changes": []}},
|
||||
)
|
||||
expect(bad.status_code == 400, "wrong kind is rejected")
|
||||
|
||||
print("profile review ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Local writing-profile compilation. Run from backend/: python tests/test_writing_profile.py"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from writing_profile_store import compile_style_signals
|
||||
|
||||
|
||||
def expect(ok: bool, message: str) -> None:
|
||||
if not ok:
|
||||
raise SystemExit(f"FAIL: {message}")
|
||||
print(f"OK {message}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
empty = compile_style_signals(["ok"])
|
||||
expect(empty == "", "too little text yields no signals")
|
||||
|
||||
clocks = compile_style_signals(
|
||||
["Heute um 7:30 Uhr bin ich zum Markt. Danach habe ich Kirschen gekauft."]
|
||||
)
|
||||
expect("Uhrzeiten" in clocks, "clocks become a form hint")
|
||||
expect("keine Diagnose" in clocks, "signals refuse diagnosis language")
|
||||
expect("chronologisch" in clocks, "time words mark chronology")
|
||||
|
||||
reflect = compile_style_signals(
|
||||
[
|
||||
"Der Vormittag war ruhig. Allerdings wunderte ich mich. "
|
||||
"Vielleicht bedeutet das weniger, als es scheint."
|
||||
]
|
||||
)
|
||||
expect("Einordnung" in reflect, "reflection markers are form, not a self model")
|
||||
|
||||
expect(
|
||||
"keine Diagnose" in compile_style_signals(
|
||||
["Heute war der Markt voll. Ich bin früh gegangen und habe Kirschen gekauft."]
|
||||
),
|
||||
"first-day signals exist without a saved entry",
|
||||
)
|
||||
|
||||
print("writing profile signals ok")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -10,4 +10,5 @@ MODULE_VERSIONS = {
|
||||
"data_layer": "0.1.0",
|
||||
"dialogue": "0.1.0",
|
||||
"continuity": "0.1.0",
|
||||
"journal": "0.1.0",
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
"""Local, explainable writing-style features. Not a personality or Self Model."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from journal_body import plain_text
|
||||
|
||||
MIN_STYLE_CHARS = 40
|
||||
CLOCK = re.compile(r"\b\d{1,2}:\d{2}\b|\b\d{1,2}\s*Uhr\b", re.I)
|
||||
TIME_WORDS = re.compile(
|
||||
r"\b(heute|gestern|morgen|morgens|mittags|abends|nachts|danach|vorher|später|zuerst|früh|dann)\b",
|
||||
re.I,
|
||||
)
|
||||
THEMATIC = re.compile(r"\b(eigentlich|überhaupt|was mich|dabei fällt|im Grunde)\b", re.I)
|
||||
REFLECT = re.compile(
|
||||
r"\b(allerdings|trotzdem|dennoch|vielleicht|offenbar|merkwürdig|"
|
||||
r"wunderte|beruhig|bedeutet|irgendwie|scheint|spüre|fühl)\w*\b",
|
||||
re.I,
|
||||
)
|
||||
HUMOR = re.compile(r"\b(haha|lol|witz|irgendwie lustig)\b|:\)|😉", re.I)
|
||||
PLACE = re.compile(
|
||||
r"\b(hafen|balkon|markt|strand|gassen|wohnung|bett|café|cafe|laden|hafen)\b",
|
||||
re.I,
|
||||
)
|
||||
NAMEISH = re.compile(r"\b[A-ZÄÖÜ][a-zäöüß]{2,}\b")
|
||||
TRANSITION = re.compile(
|
||||
r"\b(danach|anschließend|später|schließlich|irgendwann|als ich dann|nach einer weile)\b",
|
||||
re.I,
|
||||
)
|
||||
DIRECT = re.compile(
|
||||
r"\b(ich war|ich bin|mir war|mich hat|ich fühlte|ich spürte|traurig|wütend|glücklich|entspannt)\b",
|
||||
re.I,
|
||||
)
|
||||
|
||||
FACET_KEYS = (
|
||||
"core",
|
||||
"journal",
|
||||
"reflective",
|
||||
"rhythm",
|
||||
"detail",
|
||||
"chronology",
|
||||
"lexicon",
|
||||
"humor",
|
||||
"emotional_directness",
|
||||
"transitions",
|
||||
"concreteness",
|
||||
"event_vs_reflection",
|
||||
)
|
||||
SIGNAL_KEYS = FACET_KEYS
|
||||
|
||||
FACET_LABELS = {
|
||||
"core": "Globaler Schreibkern",
|
||||
"journal": "Autobiografisches Journaling",
|
||||
"reflective": "Tiefer reflektierende Texte",
|
||||
"rhythm": "Satzlänge / Rhythmus",
|
||||
"detail": "Detailgrad",
|
||||
"chronology": "Erzählweise",
|
||||
"lexicon": "Wortwahl / typische Formulierungen",
|
||||
"humor": "Humor",
|
||||
"emotional_directness": "Emotionale Direktheit",
|
||||
"transitions": "Typische Übergänge",
|
||||
"concreteness": "Zeiten, Orte, Namen",
|
||||
"event_vs_reflection": "Ereignis / Reflexion",
|
||||
}
|
||||
|
||||
|
||||
def _blob(texts: list[str]) -> str:
|
||||
return " ".join(plain_text(item or "").strip() for item in texts if (item or "").strip())
|
||||
|
||||
|
||||
def infer_features(texts: list[str]) -> dict[str, str]:
|
||||
"""Structured local features. Empty dict if too little text."""
|
||||
blob = _blob(texts)
|
||||
if len(blob) < MIN_STYLE_CHARS:
|
||||
return {}
|
||||
sentences = [item.strip() for item in re.split(r"[.!?]+", blob) if item.strip()]
|
||||
if not sentences:
|
||||
return {}
|
||||
avg = sum(len(item.split()) for item in sentences) / max(len(sentences), 1)
|
||||
if avg >= 18:
|
||||
rhythm = "typische Sätze eher lang"
|
||||
elif avg <= 10:
|
||||
rhythm = "typische Sätze eher knapp"
|
||||
else:
|
||||
rhythm = "typische Sätze von mittlerer Länge"
|
||||
clocks = bool(CLOCK.search(blob))
|
||||
numbers = bool(re.search(r"\b\d+\b", blob))
|
||||
if clocks and numbers:
|
||||
detail = "nennt Uhrzeiten, Zahlen und konkrete Abläufe"
|
||||
elif clocks or numbers:
|
||||
detail = "arbeitet mit konkreten Angaben"
|
||||
else:
|
||||
detail = "hält den Detailgrad eher zurück"
|
||||
time_hits = len(TIME_WORDS.findall(blob))
|
||||
theme_hits = len(THEMATIC.findall(blob))
|
||||
if time_hits >= 2 and time_hits > theme_hits:
|
||||
chronology = "erzählt vorwiegend chronologisch"
|
||||
elif theme_hits >= 2 and theme_hits >= time_hits:
|
||||
chronology = "ordnet eher thematisch als streng nach der Uhr"
|
||||
else:
|
||||
chronology = "Erzählweise noch unscharf"
|
||||
words = re.findall(r"[a-zäöüß]{5,}", blob.lower())
|
||||
counts: dict[str, int] = {}
|
||||
for word in words:
|
||||
counts[word] = counts.get(word, 0) + 1
|
||||
typical = [item[0] for item in sorted(counts.items(), key=lambda pair: (-pair[1], pair[0]))[:6]]
|
||||
lexicon = "wiederkehrende Wörter: " + ", ".join(typical) if typical else "keine auffällige Wortwahl"
|
||||
humor = "lässt Humor durchscheinen" if HUMOR.search(blob) else "Humor tritt kaum als Stilmittel auf"
|
||||
direct_hits = len(DIRECT.findall(blob))
|
||||
if direct_hits >= 3:
|
||||
emotional = "benennt Empfinden eher direkt"
|
||||
elif direct_hits >= 1:
|
||||
emotional = "mischt Sachbericht und gelegentliche direkte Empfindung"
|
||||
else:
|
||||
emotional = "bleibt vorwiegend bei der äußeren Begebenheit"
|
||||
trans_hits = len(TRANSITION.findall(blob))
|
||||
transitions = (
|
||||
"verbindet Szenen oft mit danach/später/irgendwann"
|
||||
if trans_hits >= 2
|
||||
else "Übergänge sind unauffällig oder selten markiert"
|
||||
)
|
||||
places = bool(PLACE.search(blob) or NAMEISH.search(blob))
|
||||
if clocks and places:
|
||||
concreteness = "nutzt konkrete Zeiten, Orte und Namen"
|
||||
elif clocks or places:
|
||||
concreteness = "streut konkrete Verankerungen ein"
|
||||
else:
|
||||
concreteness = "bleibt eher ohne feste Zeit- oder Ortsmarken"
|
||||
reflect_hits = len(REFLECT.findall(blob))
|
||||
if reflect_hits >= 3:
|
||||
mix = "mischt Chronik mit Einordnung"
|
||||
journal = "autobiografischer Bericht mit reflektierenden Einschüben"
|
||||
reflective = "öffnet gelegentlich Bedeutung und Wirkung, ohne Diagnose"
|
||||
elif reflect_hits >= 1:
|
||||
mix = "überwiegend Chronik, mit einzelnen Einordnungen"
|
||||
journal = "vorwiegend autobiografische Chronik"
|
||||
reflective = ""
|
||||
else:
|
||||
mix = "bleibt vorwiegend bei der sachlichen Chronik"
|
||||
journal = "sachliche autobiografische Chronik"
|
||||
reflective = ""
|
||||
core = "; ".join(
|
||||
part
|
||||
for part in (rhythm, detail, chronology, mix)
|
||||
if part
|
||||
)
|
||||
features = {
|
||||
"core": "Erzählmerkmale (nur aus Nutzertext, keine Diagnose): " + core + ".",
|
||||
"journal": journal,
|
||||
"reflective": reflective,
|
||||
"rhythm": rhythm,
|
||||
"detail": detail,
|
||||
"chronology": chronology,
|
||||
"lexicon": lexicon,
|
||||
"humor": humor,
|
||||
"emotional_directness": emotional,
|
||||
"transitions": transitions,
|
||||
"concreteness": concreteness,
|
||||
"event_vs_reflection": mix,
|
||||
}
|
||||
return {key: value for key, value in features.items() if (value or "").strip()}
|
||||
|
||||
|
||||
def compile_style_signals(texts: list[str]) -> str:
|
||||
"""Compact form hint used by tests and as a fallback line in the brief."""
|
||||
features = infer_features(texts)
|
||||
if not features:
|
||||
return ""
|
||||
return features.get("core") or ""
|
||||
@@ -0,0 +1,146 @@
|
||||
"""Stable Writing-Profile shell. Seed keys are ordering help, not a closed trait schema."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
|
||||
SLUG = re.compile(r"^[a-z][a-z0-9_]{1,62}$")
|
||||
LIFECYCLE = ("uninitialized", "initial_pending", "confirmed")
|
||||
LAYERS = ("core", "context", "output")
|
||||
TRAIT_STATUS = ("active", "merged", "split", "retired")
|
||||
REF_ROLES = ("evidence", "exemplar")
|
||||
ORIGINS = ("manual", "imported", "accepted_suggestion", "initial_build")
|
||||
CONTEXT_HINTS = {
|
||||
"vacation_diary": "autobiographical_journal",
|
||||
"travel_journal": "autobiographical_journal",
|
||||
"autobiographical_journal": "autobiographical_journal",
|
||||
"journal": "autobiographical_journal",
|
||||
"reflective": "reflective",
|
||||
}
|
||||
FACET_ALIASES = {"journal": "autobiographical_journal"}
|
||||
SEED_FACETS = {
|
||||
"core": {"layer": "core", "label": "Globaler Schreibkern"},
|
||||
"autobiographical_journal": {"layer": "context", "label": "Autobiografisches Journaling"},
|
||||
"reflective": {"layer": "context", "label": "Tiefer reflektierende Texte"},
|
||||
}
|
||||
SEED_TRAIT_HINTS = (
|
||||
{"slug": "rhythm", "label": "Satzlänge / Rhythmus"},
|
||||
{"slug": "detail", "label": "Detailgrad"},
|
||||
{"slug": "chronology", "label": "Erzählweise"},
|
||||
{"slug": "lexicon", "label": "Wortwahl / typische Formulierungen"},
|
||||
{"slug": "humor", "label": "Humor"},
|
||||
{"slug": "emotional_directness", "label": "Emotionale Direktheit"},
|
||||
{"slug": "transitions", "label": "Typische Übergänge"},
|
||||
{"slug": "concreteness", "label": "Zeiten, Orte, Namen"},
|
||||
{"slug": "event_vs_reflection", "label": "Ereignis / Reflexion"},
|
||||
)
|
||||
LEGACY_STYLE_KEYS = {item["slug"] for item in SEED_TRAIT_HINTS}
|
||||
EXISTING_BEFORE_NEW = (
|
||||
"bestehenden Trait bestätigen",
|
||||
"präzisieren",
|
||||
"Scope ändern",
|
||||
"zusammenführen oder aufteilen",
|
||||
"erst dann einen neuen Trait anlegen",
|
||||
)
|
||||
TRAIT_ACTIONS = ("confirm", "precisify", "rescope", "merge", "split", "create", "keep", "update")
|
||||
ACTION_ALIASES = {"keep": "confirm", "update": "precisify"}
|
||||
|
||||
|
||||
def normalize_facet_key(key: str | None) -> str:
|
||||
raw = (key or "").strip()
|
||||
return FACET_ALIASES.get(raw, raw)
|
||||
|
||||
|
||||
def facet_label(key: str) -> str:
|
||||
item = SEED_FACETS.get(normalize_facet_key(key))
|
||||
if item:
|
||||
return item["label"]
|
||||
return key.replace("_", " ")
|
||||
|
||||
|
||||
def facet_layer(key: str, fallback: str = "context") -> str:
|
||||
item = SEED_FACETS.get(normalize_facet_key(key))
|
||||
if item:
|
||||
return item["layer"]
|
||||
return fallback if fallback in LAYERS else "context"
|
||||
|
||||
|
||||
def valid_slug(value: str | None) -> bool:
|
||||
return bool(value and SLUG.match(value))
|
||||
|
||||
|
||||
def coerce_slug(value: str, fallback: str = "trait") -> str:
|
||||
raw = (value or "").strip().lower().replace("-", "_").replace(" ", "_")
|
||||
cleaned = re.sub(r"[^a-z0-9_]", "", raw)
|
||||
if valid_slug(cleaned):
|
||||
return cleaned
|
||||
return fallback
|
||||
|
||||
|
||||
def hint_for_context(context_hint: str | None) -> str:
|
||||
raw = (context_hint or "").strip().lower()
|
||||
return CONTEXT_HINTS.get(raw, raw)
|
||||
|
||||
|
||||
def parse_when(raw: str | None) -> datetime | None:
|
||||
text = (raw or "").strip()
|
||||
if not text:
|
||||
return None
|
||||
try:
|
||||
stamp = datetime.fromisoformat(text.replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
try:
|
||||
stamp = datetime.strptime(text[:10], "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return None
|
||||
if stamp.tzinfo is None:
|
||||
stamp = stamp.replace(tzinfo=timezone.utc)
|
||||
return stamp
|
||||
|
||||
|
||||
def recency_weight(occurred_at: str | None, now: datetime | None = None) -> float:
|
||||
"""Newer texts weigh more for current expression; older texts stay as long-term evidence."""
|
||||
stamp = parse_when(occurred_at)
|
||||
if not stamp:
|
||||
return 1.0
|
||||
current = now or datetime.now(timezone.utc)
|
||||
days = max((current - stamp).days, 0)
|
||||
if days <= 90:
|
||||
return 1.25
|
||||
if days <= 365:
|
||||
return 1.0
|
||||
if days <= 1100:
|
||||
return 0.75
|
||||
return 0.55
|
||||
|
||||
|
||||
def recency_role(occurred_at: str | None, now: datetime | None = None) -> str:
|
||||
stamp = parse_when(occurred_at)
|
||||
if not stamp:
|
||||
return "undated"
|
||||
current = now or datetime.now(timezone.utc)
|
||||
days = max((current - stamp).days, 0)
|
||||
if days <= 365:
|
||||
return "current_expression"
|
||||
return "long_term"
|
||||
|
||||
|
||||
def seed_catalog() -> list[dict]:
|
||||
return [
|
||||
{
|
||||
"kind": "facet",
|
||||
"key": key,
|
||||
"layer": item["layer"],
|
||||
"label": item["label"],
|
||||
"role": "ordering_help",
|
||||
}
|
||||
for key, item in SEED_FACETS.items()
|
||||
] + [
|
||||
{
|
||||
"kind": "trait_hint",
|
||||
"slug": item["slug"],
|
||||
"label": item["label"],
|
||||
"role": "ordering_help",
|
||||
}
|
||||
for item in SEED_TRAIT_HINTS
|
||||
]
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user