Neue Datei: agent_tasks_api.py
Du bist Claude Code. Deine Aufgabe ist Task 6 von 7: Erstelle einen FastAPI-Microservice der agent_task_handler.py als HTTP-API kapselt, damit das Dashboard Notion-Agent-Tasks anlegen, aktualisieren und abfragen kann.
VORAUSSETZUNG: agent_task_handler.py mit Funktionen createagenttask(), updatestatus(), synctasks() ist vorhanden und funktioniert (getestet via test_bridge-5.py).
AUFGABEN:
from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import Optional from agent_task_handler import createagenttask, updatestatus, synctasks
app = FastAPI(title="Agent Tasks API", version="1.0.0")
app.add_middleware( CORSMiddleware, allow_origins=[""], # Fuer lokale Entwicklung; in Prod einschraenken allow_methods=["GET", "POST"], allow_headers=[""], )
class CreateTaskRequest(BaseModel): title: str description: str = "" agentId: Optional[str] = None priority: str = "Normal"
class UpdateStatusRequest(BaseModel): pageId: str status: str
@app.get("/api/agent-tasks") def list_agent_tasks(): try: return synctasks() except Exception as e: raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/agent-tasks") def create_agent_task(req: CreateTaskRequest): try: result = createagenttask( req.title, req.description, agentid=req.agentId, priority=req.priority.capitalize(), ) return { "id": result.get("id"), "url": result.get("url"), "status": "Neu" } except Exception as e: raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/agent-tasks/status") def update_agent_task_status(req: UpdateStatusRequest): try: updatestatus(req.pageId, req.status) return { "ok": True } except Exception as e: raise HTTPException(status_code=500, detail=str(e))
@app.get("/health") def health(): return { "status": "ok" }
async function createNotionAgentTaskFromPrompt({ title, description, agentId, priority }) { try { const res = await fetch('/api/agent-tasks', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, description, agentId, priority }), }); if (!res.ok) throw new Error('API error: ' + res.status); return await res.json(); } catch (err) { console.warn('[createNotionAgentTask] Backend nicht erreichbar, Fallback:', err); return { notionPageId: null, status: 'Neu' }; } }
ERWARTETES ERGEBNIS: