feat: unified prompt executor - Phase 2 complete (Issue #28)
Backend:
- prompt_executor.py: Universal executor for base + pipeline prompts
- Dynamic placeholder resolution
- JSON output validation
- Multi-stage parallel execution (sequential impl)
- Reference and inline prompt support
- Data loading per module (körper, ernährung, training, schlaf, vitalwerte)
Endpoints:
- POST /api/prompts/execute - Execute unified prompts
- POST /api/prompts/unified - Create unified prompts
- PUT /api/prompts/unified/{id} - Update unified prompts
Frontend:
- api.js: executeUnifiedPrompt, createUnifiedPrompt, updateUnifiedPrompt
Next: Phase 3 - Frontend UI consolidation
This commit is contained in:
1 parent
33653fdfd4
commit
7be7266477
3 files changed
+568
No files matched your search
@@ -684,3 +684,209 @@ def reset_prompt_to_default(prompt_id: str, session: dict=Depends(require_admin)
|
||||
)
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════════════════
|
||||
# UNIFIED PROMPT SYSTEM (Issue #28 Phase 2)
|
||||
# ══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
from prompt_executor import execute_prompt_with_data
|
||||
from models import UnifiedPromptCreate, UnifiedPromptUpdate
|
||||
|
||||
|
||||
@router.post("/execute")
|
||||
async def execute_unified_prompt(
|
||||
prompt_slug: str,
|
||||
modules: Optional[dict] = None,
|
||||
timeframes: Optional[dict] = None,
|
||||
session: dict = Depends(require_auth)
|
||||
):
|
||||
"""
|
||||
Execute a unified prompt (base or pipeline type).
|
||||
|
||||
Args:
|
||||
prompt_slug: Slug of prompt to execute
|
||||
modules: Dict of enabled modules (e.g., {"körper": true})
|
||||
timeframes: Dict of timeframes per module (e.g., {"körper": 30})
|
||||
|
||||
Returns:
|
||||
Execution result with outputs
|
||||
"""
|
||||
profile_id = session['profile_id']
|
||||
|
||||
# Use default modules/timeframes if not provided
|
||||
if not modules:
|
||||
modules = {
|
||||
'körper': True,
|
||||
'ernährung': True,
|
||||
'training': True,
|
||||
'schlaf': True,
|
||||
'vitalwerte': True
|
||||
}
|
||||
|
||||
if not timeframes:
|
||||
timeframes = {
|
||||
'körper': 30,
|
||||
'ernährung': 30,
|
||||
'training': 14,
|
||||
'schlaf': 14,
|
||||
'vitalwerte': 7
|
||||
}
|
||||
|
||||
# Execute with prompt_executor
|
||||
result = await execute_prompt_with_data(
|
||||
prompt_slug=prompt_slug,
|
||||
profile_id=profile_id,
|
||||
modules=modules,
|
||||
timeframes=timeframes,
|
||||
openrouter_call_func=call_openrouter
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/unified")
|
||||
def create_unified_prompt(p: UnifiedPromptCreate, session: dict = Depends(require_admin)):
|
||||
"""
|
||||
Create a new unified prompt (base or pipeline type).
|
||||
Admin only.
|
||||
"""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Check for duplicate slug
|
||||
cur.execute("SELECT id FROM ai_prompts WHERE slug=%s", (p.slug,))
|
||||
if cur.fetchone():
|
||||
raise HTTPException(status_code=400, detail="Slug already exists")
|
||||
|
||||
# Validate type
|
||||
if p.type not in ['base', 'pipeline']:
|
||||
raise HTTPException(status_code=400, detail="Type must be 'base' or 'pipeline'")
|
||||
|
||||
# Validate base type has template
|
||||
if p.type == 'base' and not p.template:
|
||||
raise HTTPException(status_code=400, detail="Base prompts require a template")
|
||||
|
||||
# Validate pipeline type has stages
|
||||
if p.type == 'pipeline' and not p.stages:
|
||||
raise HTTPException(status_code=400, detail="Pipeline prompts require stages")
|
||||
|
||||
# Convert stages to JSONB
|
||||
stages_json = None
|
||||
if p.stages:
|
||||
stages_json = json.dumps([
|
||||
{
|
||||
'stage': s.stage,
|
||||
'prompts': [
|
||||
{
|
||||
'source': pr.source,
|
||||
'slug': pr.slug,
|
||||
'template': pr.template,
|
||||
'output_key': pr.output_key,
|
||||
'output_format': pr.output_format,
|
||||
'output_schema': pr.output_schema
|
||||
}
|
||||
for pr in s.prompts
|
||||
]
|
||||
}
|
||||
for s in p.stages
|
||||
])
|
||||
|
||||
prompt_id = str(uuid.uuid4())
|
||||
|
||||
cur.execute(
|
||||
"""INSERT INTO ai_prompts
|
||||
(id, slug, name, display_name, description, template, category, active, sort_order,
|
||||
type, stages, output_format, output_schema)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
|
||||
(
|
||||
prompt_id, p.slug, p.name, p.display_name, p.description,
|
||||
p.template, p.category, p.active, p.sort_order,
|
||||
p.type, stages_json, p.output_format,
|
||||
json.dumps(p.output_schema) if p.output_schema else None
|
||||
)
|
||||
)
|
||||
|
||||
return {"id": prompt_id, "slug": p.slug}
|
||||
|
||||
|
||||
@router.put("/unified/{prompt_id}")
|
||||
def update_unified_prompt(prompt_id: str, p: UnifiedPromptUpdate, session: dict = Depends(require_admin)):
|
||||
"""
|
||||
Update a unified prompt.
|
||||
Admin only.
|
||||
"""
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
|
||||
# Check if exists
|
||||
cur.execute("SELECT id FROM ai_prompts WHERE id=%s", (prompt_id,))
|
||||
if not cur.fetchone():
|
||||
raise HTTPException(status_code=404, detail="Prompt not found")
|
||||
|
||||
# Build update query
|
||||
updates = []
|
||||
values = []
|
||||
|
||||
if p.name is not None:
|
||||
updates.append('name=%s')
|
||||
values.append(p.name)
|
||||
if p.display_name is not None:
|
||||
updates.append('display_name=%s')
|
||||
values.append(p.display_name)
|
||||
if p.description is not None:
|
||||
updates.append('description=%s')
|
||||
values.append(p.description)
|
||||
if p.type is not None:
|
||||
if p.type not in ['base', 'pipeline']:
|
||||
raise HTTPException(status_code=400, detail="Type must be 'base' or 'pipeline'")
|
||||
updates.append('type=%s')
|
||||
values.append(p.type)
|
||||
if p.category is not None:
|
||||
updates.append('category=%s')
|
||||
values.append(p.category)
|
||||
if p.active is not None:
|
||||
updates.append('active=%s')
|
||||
values.append(p.active)
|
||||
if p.sort_order is not None:
|
||||
updates.append('sort_order=%s')
|
||||
values.append(p.sort_order)
|
||||
if p.template is not None:
|
||||
updates.append('template=%s')
|
||||
values.append(p.template)
|
||||
if p.output_format is not None:
|
||||
updates.append('output_format=%s')
|
||||
values.append(p.output_format)
|
||||
if p.output_schema is not None:
|
||||
updates.append('output_schema=%s')
|
||||
values.append(json.dumps(p.output_schema))
|
||||
if p.stages is not None:
|
||||
stages_json = json.dumps([
|
||||
{
|
||||
'stage': s.stage,
|
||||
'prompts': [
|
||||
{
|
||||
'source': pr.source,
|
||||
'slug': pr.slug,
|
||||
'template': pr.template,
|
||||
'output_key': pr.output_key,
|
||||
'output_format': pr.output_format,
|
||||
'output_schema': pr.output_schema
|
||||
}
|
||||
for pr in s.prompts
|
||||
]
|
||||
}
|
||||
for s in p.stages
|
||||
])
|
||||
updates.append('stages=%s')
|
||||
values.append(stages_json)
|
||||
|
||||
if not updates:
|
||||
return {"ok": True}
|
||||
|
||||
cur.execute(
|
||||
f"UPDATE ai_prompts SET {', '.join(updates)}, updated=CURRENT_TIMESTAMP WHERE id=%s",
|
||||
values + [prompt_id]
|
||||
)
|
||||
|
||||
return {"ok": True}
|
||||
Reference in new issue
Block a user