feat: Pipeline-System - Backend Infrastructure (Issue #28, Phase 1)
Implementiert konfigurierbare mehrstufige Analysen. Admins können
mehrere Pipeline-Konfigurationen erstellen mit unterschiedlichen
Modulen, Zeiträumen und Prompts.
**Backend:**
- Migration 019: pipeline_configs Tabelle + ai_prompts erweitert
- Pipeline-Config Models: PipelineConfigCreate, PipelineConfigUpdate
- Pipeline-Executor: refactored für config-basierte Ausführung
- CRUD-Endpoints: /api/prompts/pipeline-configs (list, create, update, delete, set-default)
- Reset-to-Default: /api/prompts/{id}/reset-to-default für System-Prompts
**Features:**
- 3 Seed-Configs: "Alltags-Check" (default), "Schlaf & Erholung", "Wettkampf-Analyse"
- Dynamische Platzhalter: {{stage1_<slug>}} für alle Stage-1-Ergebnisse
- Backward-compatible: /api/insights/pipeline ohne config_id nutzt default
**Dateien:**
- backend/migrations/019_pipeline_system.sql
- backend/models.py (PipelineConfigCreate, PipelineConfigUpdate)
- backend/routers/insights.py (analyze_pipeline refactored)
- backend/routers/prompts.py (Pipeline-Config CRUD + Reset-to-Default)
**Nächste Schritte:**
- Frontend: Pipeline-Config Dialog + Admin-UI
- Design: Mobile-Responsive + Icons
Issue #28 Progress: Backend 3/3 ✅ | Frontend 0/3 🔲 | Design 0/3 🔲
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e7ef718e0
commit
6627b5eee7
4 files changed
+462
-25
No files matched your search
+200
-1
@@ -12,7 +12,10 @@ from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from auth import require_auth, require_admin
|
||||
from models import PromptCreate, PromptUpdate, PromptGenerateRequest
|
||||
from models import (
|
||||
PromptCreate, PromptUpdate, PromptGenerateRequest,
|
||||
PipelineConfigCreate, PipelineConfigUpdate
|
||||
)
|
||||
from placeholder_resolver import (
|
||||
resolve_placeholders,
|
||||
get_unknown_placeholders,
|
||||
@@ -485,3 +488,199 @@ async def optimize_prompt(prompt_id: str, session: dict=Depends(require_admin)):
|
||||
)
|
||||
|
||||
return analysis
|
||||
|
||||
|
||||
# ── Pipeline Config Management (Issue #28) ────────────────────────────────────
|
||||
|
||||
@router.get("/pipeline-configs")
|
||||
def list_pipeline_configs(session: dict=Depends(require_auth)):
|
||||
"""
|
||||
List pipeline configurations.
|
||||
- Admins: see ALL configs
|
||||
- Users: see only active configs
|
||||
"""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
is_admin = session.get('role') == 'admin'
|
||||
|
||||
if is_admin:
|
||||
cur.execute("SELECT * FROM pipeline_configs ORDER BY is_default DESC, name")
|
||||
else:
|
||||
cur.execute("SELECT * FROM pipeline_configs WHERE active=true ORDER BY is_default DESC, name")
|
||||
|
||||
return [r2d(r) for r in cur.fetchall()]
|
||||
|
||||
|
||||
@router.post("/pipeline-configs")
|
||||
def create_pipeline_config(p: PipelineConfigCreate, session: dict=Depends(require_admin)):
|
||||
"""Create new pipeline configuration (admin only)."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Check if name already exists
|
||||
cur.execute("SELECT id FROM pipeline_configs WHERE name=%s", (p.name,))
|
||||
if cur.fetchone():
|
||||
raise HTTPException(status_code=400, detail=f"Pipeline config with name '{p.name}' already exists")
|
||||
|
||||
# Validate: stage prompts must exist
|
||||
all_slugs = p.stage1_prompts + [p.stage2_prompt]
|
||||
if p.stage3_prompt:
|
||||
all_slugs.append(p.stage3_prompt)
|
||||
|
||||
for slug in all_slugs:
|
||||
cur.execute("SELECT id FROM ai_prompts WHERE slug=%s", (slug,))
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(status_code=400, detail=f"Prompt '{slug}' does not exist")
|
||||
|
||||
# If is_default=true, unset other defaults
|
||||
if p.is_default:
|
||||
cur.execute("UPDATE pipeline_configs SET is_default=false WHERE is_default=true")
|
||||
|
||||
config_id = str(uuid.uuid4())
|
||||
cur.execute(
|
||||
"""INSERT INTO pipeline_configs (
|
||||
id, name, description, is_default, active,
|
||||
modules, timeframes, stage1_prompts, stage2_prompt, stage3_prompt,
|
||||
created, updated
|
||||
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)""",
|
||||
(
|
||||
config_id, p.name, p.description, p.is_default, p.active,
|
||||
json.dumps(p.modules), json.dumps(p.timeframes),
|
||||
p.stage1_prompts, p.stage2_prompt, p.stage3_prompt
|
||||
)
|
||||
)
|
||||
|
||||
return {"id": config_id, "name": p.name}
|
||||
|
||||
|
||||
@router.put("/pipeline-configs/{config_id}")
|
||||
def update_pipeline_config(config_id: str, p: PipelineConfigUpdate, session: dict=Depends(require_admin)):
|
||||
"""Update pipeline configuration (admin only)."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Check if config exists
|
||||
cur.execute("SELECT id FROM pipeline_configs WHERE id=%s", (config_id,))
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(status_code=404, detail="Pipeline config not found")
|
||||
|
||||
# Build dynamic UPDATE query
|
||||
updates = []
|
||||
values = []
|
||||
|
||||
if p.name is not None:
|
||||
updates.append('name=%s')
|
||||
values.append(p.name)
|
||||
if p.description is not None:
|
||||
updates.append('description=%s')
|
||||
values.append(p.description)
|
||||
if p.is_default is not None:
|
||||
# If setting to default, unset others
|
||||
if p.is_default:
|
||||
cur.execute("UPDATE pipeline_configs SET is_default=false WHERE is_default=true AND id!=%s", (config_id,))
|
||||
updates.append('is_default=%s')
|
||||
values.append(p.is_default)
|
||||
if p.active is not None:
|
||||
updates.append('active=%s')
|
||||
values.append(p.active)
|
||||
if p.modules is not None:
|
||||
updates.append('modules=%s')
|
||||
values.append(json.dumps(p.modules))
|
||||
if p.timeframes is not None:
|
||||
updates.append('timeframes=%s')
|
||||
values.append(json.dumps(p.timeframes))
|
||||
if p.stage1_prompts is not None:
|
||||
updates.append('stage1_prompts=%s')
|
||||
values.append(p.stage1_prompts)
|
||||
if p.stage2_prompt is not None:
|
||||
updates.append('stage2_prompt=%s')
|
||||
values.append(p.stage2_prompt)
|
||||
if p.stage3_prompt is not None:
|
||||
updates.append('stage3_prompt=%s')
|
||||
values.append(p.stage3_prompt)
|
||||
|
||||
if not updates:
|
||||
return {"ok": True}
|
||||
|
||||
cur.execute(
|
||||
f"UPDATE pipeline_configs SET {', '.join(updates)}, updated=CURRENT_TIMESTAMP WHERE id=%s",
|
||||
values + [config_id]
|
||||
)
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.delete("/pipeline-configs/{config_id}")
|
||||
def delete_pipeline_config(config_id: str, session: dict=Depends(require_admin)):
|
||||
"""Delete pipeline configuration (admin only)."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Check if it's the only default
|
||||
cur.execute("SELECT is_default FROM pipeline_configs WHERE id=%s", (config_id,))
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="Pipeline config not found")
|
||||
|
||||
if row['is_default']:
|
||||
# Check if there are other configs
|
||||
cur.execute("SELECT COUNT(*) as count FROM pipeline_configs WHERE id!=%s", (config_id,))
|
||||
if cur.fetchone()['count'] > 0:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Cannot delete the default config. Please set another config as default first."
|
||||
)
|
||||
|
||||
cur.execute("DELETE FROM pipeline_configs WHERE id=%s", (config_id,))
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.post("/pipeline-configs/{config_id}/set-default")
|
||||
def set_default_pipeline_config(config_id: str, session: dict=Depends(require_admin)):
|
||||
"""Set a pipeline config as default (admin only)."""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Check if config exists
|
||||
cur.execute("SELECT id FROM pipeline_configs WHERE id=%s", (config_id,))
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(status_code=404, detail="Pipeline config not found")
|
||||
|
||||
# Unset all other defaults
|
||||
cur.execute("UPDATE pipeline_configs SET is_default=false WHERE is_default=true")
|
||||
|
||||
# Set this one as default
|
||||
cur.execute("UPDATE pipeline_configs SET is_default=true, updated=CURRENT_TIMESTAMP WHERE id=%s", (config_id,))
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.post("/{prompt_id}/reset-to-default")
|
||||
def reset_prompt_to_default(prompt_id: str, session: dict=Depends(require_admin)):
|
||||
"""
|
||||
Reset a system prompt to its default template (admin only).
|
||||
Only works for prompts with is_system_default=true.
|
||||
"""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
cur.execute("SELECT is_system_default, default_template FROM ai_prompts WHERE id=%s", (prompt_id,))
|
||||
row = cur.fetchone()
|
||||
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="Prompt not found")
|
||||
|
||||
if not row['is_system_default']:
|
||||
raise HTTPException(status_code=400, detail="Only system prompts can be reset to default")
|
||||
|
||||
if not row['default_template']:
|
||||
raise HTTPException(status_code=400, detail="No default template available for this prompt")
|
||||
|
||||
# Reset template to default
|
||||
cur.execute(
|
||||
"UPDATE ai_prompts SET template=%s, updated=CURRENT_TIMESTAMP WHERE id=%s",
|
||||
(row['default_template'], prompt_id)
|
||||
)
|
||||
|
||||
return {"ok": True}
|
||||
Reference in new issue
Block a user