Update AI Prompt System and Admin API
Deploy Development / deploy (push) Successful in 42s
Test Suite / pytest-backend (push) Successful in 39s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Successful in 33s
Test Suite / playwright-tests (push) Successful in 1m16s
Deploy Development / deploy (push) Successful in 42s
Test Suite / pytest-backend (push) Successful in 39s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Successful in 33s
Test Suite / playwright-tests (push) Successful in 1m16s
- Incremented version to 1.1 and updated the status to reflect the implementation of core features including `ai_prompts`, `prompt_resolver`, and the Superadmin HTTP API. - Documented the current API endpoints for managing AI prompts, including CRUD operations and preview functionality. - Introduced a new placeholder catalog and preview capabilities for the Superadmin interface. - Enhanced the backend with new functions for handling AI prompt templates and integrated them into the API. - Updated frontend components to include navigation and routing for the new Admin AI Prompts page. - Incremented application version to 0.8.158 and updated changelog to reflect these changes.
This commit is contained in:
1 parent
f9e295bce0
commit
2148d0aa7f
13 files changed
+900
-46
No files matched your search
+73
-32
@@ -17,6 +17,7 @@ from typing import Any, Dict, List, Mapping, MutableMapping, Optional, Sequence,
|
||||
from fastapi import HTTPException
|
||||
|
||||
from openrouter_chat import OpenRouterError, normalize_openrouter_env, openrouter_chat_completion
|
||||
from prompt_resolver import render_mustache_template
|
||||
|
||||
_LOGGER = logging.getLogger("shinkan.exercise_ai")
|
||||
|
||||
@@ -506,12 +507,46 @@ def _load_prompt_row(cur, slug: str) -> Optional[Dict[str, Any]]:
|
||||
return d
|
||||
|
||||
|
||||
def _render_template(template: str, ctx: Dict[str, str]) -> str:
|
||||
out = template or ""
|
||||
for key, val in ctx.items():
|
||||
placeholder = "{{" + key + "}}"
|
||||
out = out.replace(placeholder, val if val is not None else "")
|
||||
return out
|
||||
def build_exercise_placeholder_variables(
|
||||
cur,
|
||||
*,
|
||||
slug: str,
|
||||
title: Optional[str],
|
||||
goal: Optional[str],
|
||||
execution: Optional[str],
|
||||
focus_area_hint: Optional[str],
|
||||
focus_areas_context: Optional[Sequence[Tuple[int, bool]]],
|
||||
) -> Dict[str, str]:
|
||||
"""
|
||||
Baut die Variable-Map fuer {{platzhalter}} passend zur Slug fuer Uebungs-KI.
|
||||
"""
|
||||
s = (slug or "").strip().lower()
|
||||
if s == "pipeline":
|
||||
return {}
|
||||
g_plain = strip_html_to_plain(goal)
|
||||
e_plain = strip_html_to_plain(execution)
|
||||
t_title = (title or "").strip()
|
||||
focus = (focus_area_hint or "").strip()
|
||||
ctx: Dict[str, str] = {
|
||||
"exercise_title": t_title or "-",
|
||||
"exercise_focus_area": focus or "-",
|
||||
"exercise_goal": g_plain or "-",
|
||||
"exercise_execution": e_plain or "-",
|
||||
}
|
||||
if s == "exercise_summary":
|
||||
return ctx
|
||||
if s == "exercise_skill_suggestions":
|
||||
catalog = build_contextual_skills_catalog_block(
|
||||
cur,
|
||||
title=t_title,
|
||||
goal_plain=g_plain,
|
||||
execution_plain=e_plain,
|
||||
focus_hint=focus or None,
|
||||
focus_ctx=focus_areas_context,
|
||||
)
|
||||
ctx["skills_catalog"] = catalog
|
||||
return ctx
|
||||
raise ValueError(f"Kein Platzhalter-Kontext fuer slug={slug!r} definiert.")
|
||||
|
||||
|
||||
def _first_balanced_json_array(text: str) -> Optional[str]:
|
||||
@@ -699,18 +734,25 @@ def run_exercise_ai_suggestion(
|
||||
prow = _load_prompt_row(cur, "exercise_summary")
|
||||
if not prow:
|
||||
raise HTTPException(status_code=503, detail="Prompt exercise_summary nicht aktiv oder fehlt in DB.")
|
||||
ctx = {
|
||||
"exercise_title": t_title or "-",
|
||||
"exercise_focus_area": focus or "-",
|
||||
"exercise_goal": g_plain or "-",
|
||||
"exercise_execution": e_plain or "-",
|
||||
}
|
||||
prompt = _render_template(str(prow["template"]), ctx)
|
||||
try:
|
||||
ctx = build_exercise_placeholder_variables(
|
||||
cur,
|
||||
slug="exercise_summary",
|
||||
title=title,
|
||||
goal=goal,
|
||||
execution=execution,
|
||||
focus_area_hint=focus_area_hint,
|
||||
focus_areas_context=focus_areas_context,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
rendered = render_mustache_template(str(prow["template"]), ctx)
|
||||
prompt = rendered.text
|
||||
if _ai_debug_on():
|
||||
_LOGGER.warning(
|
||||
"AI_DEBUG exercise_ai summary prompt_slug=exercise_summary prompt_chars=%s unreplaced_mustache_pairs=%s",
|
||||
"AI_DEBUG exercise_ai summary prompt_slug=exercise_summary prompt_chars=%s placeholders_remaining=%s",
|
||||
len(prompt),
|
||||
prompt.count("{{"),
|
||||
len(rendered.placeholders_remaining),
|
||||
)
|
||||
try:
|
||||
raw = openrouter_chat_completion(api_key=key, model=model, user_content=prompt)
|
||||
@@ -735,27 +777,25 @@ def run_exercise_ai_suggestion(
|
||||
status_code=503,
|
||||
detail="Prompt exercise_skill_suggestions nicht aktiv oder fehlt in DB.",
|
||||
)
|
||||
catalog = build_contextual_skills_catalog_block(
|
||||
cur,
|
||||
title=t_title,
|
||||
goal_plain=g_plain,
|
||||
execution_plain=e_plain,
|
||||
focus_hint=focus or None,
|
||||
focus_ctx=focus_areas_context,
|
||||
)
|
||||
ctx = {
|
||||
"exercise_title": t_title or "-",
|
||||
"exercise_focus_area": focus or "-",
|
||||
"exercise_goal": g_plain or "-",
|
||||
"exercise_execution": e_plain or "-",
|
||||
"skills_catalog": catalog,
|
||||
}
|
||||
prompt = _render_template(str(srow["template"]), ctx)
|
||||
try:
|
||||
ctx = build_exercise_placeholder_variables(
|
||||
cur,
|
||||
slug="exercise_skill_suggestions",
|
||||
title=title,
|
||||
goal=goal,
|
||||
execution=execution,
|
||||
focus_area_hint=focus_area_hint,
|
||||
focus_areas_context=focus_areas_context,
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
rendered = render_mustache_template(str(srow["template"]), ctx)
|
||||
prompt = rendered.text
|
||||
if _ai_debug_on():
|
||||
_LOGGER.warning(
|
||||
"AI_DEBUG exercise_ai skills prompt_slug=exercise_skill_suggestions catalog_chars=%s prompt_chars=%s "
|
||||
"template_has_skills_placeholder=%s",
|
||||
len(catalog),
|
||||
len(ctx.get("skills_catalog") or ""),
|
||||
len(prompt),
|
||||
"{{skills_catalog}}" in str(srow.get("template") or ""),
|
||||
)
|
||||
@@ -808,6 +848,7 @@ def run_exercise_ai_suggestion(
|
||||
|
||||
__all__ = [
|
||||
"build_contextual_skills_catalog_block",
|
||||
"build_exercise_placeholder_variables",
|
||||
"run_exercise_ai_suggestion",
|
||||
"strip_html_to_plain",
|
||||
]
|
||||
Reference in new issue
Block a user