This commit is contained in:
Lars committed 2026-08-25 13:57:23 +02:00
1 parent a8caeb0dd4
commit fd7102e3b3
95 files changed
+16520 -193

No files matched your search

+55 -5
View File
@@ -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()),
}