refix
This commit is contained in:
@@ -26,6 +26,7 @@ from backend.app.services.power_curve import calculate_power_curve
|
||||
from backend.app.services.intervals import detect_intervals
|
||||
from backend.app.services.ai_summary import generate_summary
|
||||
from backend.app.services.coaching import link_activity_to_plan
|
||||
from backend.app.models.training import TrainingPlan
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -205,6 +206,7 @@ async def get_activity_power_curve(
|
||||
@router.post("/{activity_id}/ai-summary")
|
||||
async def generate_ai_summary(
|
||||
activity_id: uuid.UUID,
|
||||
body: dict | None = None,
|
||||
rider: Rider = Depends(get_current_rider),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
@@ -212,16 +214,31 @@ async def generate_ai_summary(
|
||||
if not activity or activity.rider_id != rider.id:
|
||||
raise HTTPException(status_code=404, detail="Activity not found")
|
||||
|
||||
force = (body or {}).get("force", False)
|
||||
|
||||
# Check for existing diary entry with summary
|
||||
query = select(DiaryEntry).where(DiaryEntry.activity_id == activity_id)
|
||||
result = await session.execute(query)
|
||||
diary = result.scalar_one_or_none()
|
||||
|
||||
if diary and diary.ai_summary:
|
||||
if diary and diary.ai_summary and not force:
|
||||
return {"summary": diary.ai_summary}
|
||||
|
||||
# If linked to a plan, find the planned workout for comparison
|
||||
planned_workout = None
|
||||
if activity.training_plan_id and activity.plan_week and activity.plan_day:
|
||||
plan = await session.get(TrainingPlan, activity.training_plan_id)
|
||||
if plan and plan.weeks_json:
|
||||
for week in plan.weeks_json.get("weeks", []):
|
||||
if week.get("week_number") == activity.plan_week:
|
||||
for day in week.get("days", []):
|
||||
if day.get("day") == activity.plan_day:
|
||||
planned_workout = day
|
||||
break
|
||||
break
|
||||
|
||||
# Generate new summary
|
||||
summary = await generate_summary(activity, rider_ftp=rider.ftp)
|
||||
summary = await generate_summary(activity, rider_ftp=rider.ftp, planned_workout=planned_workout)
|
||||
|
||||
if diary:
|
||||
diary.ai_summary = summary
|
||||
|
||||
Reference in New Issue
Block a user