from __future__ import annotations import asyncio from typing import Any from fastapi import FastAPI, File, HTTPException, UploadFile from fastapi.responses import JSONResponse, StreamingResponse from app.models.contracts import ChatRequest, ConversationPatch from app.services.agent_service import AgentService from app.services.library import CdslLibrary from app.services.storage import WorkspaceStore, safe_conversation_id, safe_task_id from app.services.attachments import attachment_record, classify_upload, extract_document_text from app.services.image_processing import image_metadata from app.services.review_renderer import renderer_status from app.settings import get_settings settings = get_settings() store = WorkspaceStore(settings) library = CdslLibrary(settings) agent = AgentService(settings, store, library) app = FastAPI(title="CDSL CAD Agent API", version="0.1.0") @app.on_event("startup") async def resume_autonomous_generation() -> None: """Restore durable autonomous agent tasks after a backend process restart.""" if settings.resume_running_tasks_on_startup: await agent.resume_running_tasks() @app.get("/health") async def health() -> dict[str, Any]: return { "ok": True, "service": "cdsl-cad-backend", "llm_configured": settings.llm_configured, "library_index": (settings.library_root / "index" / "catalog.json").is_file(), } @app.get("/v1/config") async def config() -> dict[str, Any]: providers = [] for provider in settings.providers: if not provider.configured: continue providers.append({ "id": provider.id, "label": provider.label, "models": [ { "id": model.id, "vision": model.vision, } for model in provider.models ], }) try: settings.resolve_review_model() renderer_ready, renderer_detail = renderer_status() review_error = "" if renderer_ready else renderer_detail except ValueError as error: review_error = str(error) return { "default_provider": settings.default_provider_id, "default_model": settings.llm_model, "providers": providers, "model": settings.llm_model, "configured": settings.llm_configured, "library_samples": library.count(), "autonomous_generation": settings.autonomous_generation, "review_configured": not review_error, "review_error": review_error, } @app.post("/v1/chat/stream") async def chat_stream(payload: ChatRequest) -> StreamingResponse: return StreamingResponse( agent.stream(payload.messages, payload.conversation_id, payload.selected_task_id, payload.provider_id, payload.model_id, payload.viewer_context), media_type="text/event-stream", headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}, ) @app.get("/v1/conversations/{conversation_id}") async def read_conversation(conversation_id: str) -> JSONResponse: try: record = store.read_conversation(safe_conversation_id(conversation_id)) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error if record is None: raise HTTPException(status_code=404, detail="Conversation not found") return JSONResponse(record) @app.post("/v1/conversations") async def create_conversation() -> JSONResponse: return JSONResponse(store.ensure_conversation(None)) @app.patch("/v1/conversations/{conversation_id}") async def patch_conversation(conversation_id: str, payload: ConversationPatch) -> JSONResponse: try: record = store.ensure_conversation(safe_conversation_id(conversation_id), payload.current_task_id) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error return JSONResponse(record) @app.post("/v1/conversations/{conversation_id}/attachments") async def upload_conversation_attachment( conversation_id: str, file: UploadFile = File(...), ) -> JSONResponse: data = await file.read() filename = file.filename or "attachment" try: conversation = safe_conversation_id(conversation_id) current = store.read_conversation(conversation) if current is None: raise HTTPException(status_code=404, detail="Conversation not found") active_task_id = str(current.get("current_task_id") or "") active_task = store.read_task(active_task_id) if active_task_id else None if str((active_task or {}).get("lifecycle") or "") == "running": raise HTTPException(status_code=409, detail="CAD task is running; attachments are locked until it reaches a terminal state") kind = classify_upload(filename, file.content_type or "", len(data)) relative_path, _ = store.write_conversation_upload(conversation, filename, data) extracted_path = "" if kind == "document": extracted_path = relative_path + ".txt" extracted = extract_document_text(data) store.conversation_attachment_path(conversation, extracted_path).write_text(extracted, encoding="utf-8") metadata = image_metadata(data) if kind == "image" else {} record = attachment_record(conversation, filename, file.content_type or "", relative_path, data, kind, extracted_path, metadata) store.add_conversation_attachment(conversation, record) return JSONResponse(record) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error @app.get("/v1/tasks/{task_id}") async def read_task(task_id: str) -> JSONResponse: try: task = store.read_task(safe_task_id(task_id)) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error if task is None: raise HTTPException(status_code=404, detail="Task not found") # Keep the task endpoint self-contained for a reconnecting UI. Requirements # are immutable during a run; the active revision remains preview-only. task["preview_revision"] = str(task.get("active_revision") or task.get("current_revision") or "") requirements = store.read_requirements_document(task["task_id"]) task["requirements_markdown"] = requirements or None completion_checklist = store.read_completion_checklist(task["task_id"]) task["completion_checklist_markdown"] = completion_checklist or None task["agent_state"] = store.read_agent_state(task["task_id"]) return JSONResponse(task) @app.get("/v1/tasks/{task_id}/artifacts/{artifact_path:path}") async def read_artifact(task_id: str, artifact_path: str) -> StreamingResponse: from fastapi.responses import FileResponse try: safe_id = safe_task_id(task_id) path = store.artifact_path(safe_id, artifact_path) except ValueError as error: raise HTTPException(status_code=400, detail=str(error)) from error if not path.is_file(): raise HTTPException(status_code=404, detail="Artifact not found") task = store.read_task(safe_id) or {} parts = artifact_path.split("/") revision_id = parts[1] if len(parts) >= 3 and parts[0] == "revisions" else "" revision = next((item for item in task.get("revisions") or () if isinstance(item, dict) and item.get("revision_id") == revision_id), None) published_revision = str(task.get("published_revision") or "") active_revision = str(task.get("active_revision") or task.get("current_revision") or "") if not isinstance(revision, dict): # The task directory also contains candidate staging, agent audit and # frozen-input files. None of those are a public artifact surface. raise HTTPException(status_code=403, detail="This task artifact is not public") if revision_id == published_revision: published_paths = { str(revision.get("cdsl_path") or ""), str(revision.get("step_path") or ""), str(revision.get("glb_path") or ""), str(revision.get("report_path") or ""), } if artifact_path not in published_paths: raise HTTPException(status_code=403, detail="Only final delivery artifacts are downloadable") return FileResponse(path, filename=path.name) # A running task may render its current checkpoint in the browser, but # cannot expose any other checkpoint artifact or a failed-task preview. if ( str(task.get("lifecycle") or "") != "running" or revision_id != active_revision or artifact_path != str(revision.get("glb_path") or "") ): raise HTTPException(status_code=403, detail="Only the published revision is downloadable") return FileResponse(path, media_type="model/gltf-binary", headers={"Content-Disposition": "inline"})