"""Sync API: manual trigger + bundle exchange endpoints (PLAN.md Phase 7).""" from __future__ import annotations from fastapi import APIRouter, HTTPException from pydantic import BaseModel from app.config import settings from app.deps import get_store from app.sync.sync_daemon import build_bundle, merge_bundle, sync_with_peer router = APIRouter(prefix="/api/sync", tags=["sync"]) @router.get("/bundle") def get_bundle(): return build_bundle(get_store()) @router.post("/bundle") def post_bundle(bundle: dict): return {"applied": merge_bundle(get_store(), bundle)} class SyncBody(BaseModel): peer_url: str | None = None @router.post("") def trigger_sync(body: SyncBody | None = None): peer = (body.peer_url if body else None) or settings.sync.peer_url if not peer: raise HTTPException(400, "no peer_url given and sync.peer_url not configured") try: return sync_with_peer(get_store(), peer) except Exception as e: raise HTTPException(502, f"sync with {peer} failed: {e}")