Add project registry update flow
This commit is contained in:
@@ -39,6 +39,7 @@ from atocore.projects.registry import (
|
||||
list_registered_projects,
|
||||
register_project,
|
||||
refresh_registered_project,
|
||||
update_project,
|
||||
)
|
||||
from atocore.retrieval.retriever import retrieve
|
||||
from atocore.retrieval.vector_store import get_vector_store
|
||||
@@ -77,6 +78,12 @@ class ProjectRegistrationProposalRequest(BaseModel):
|
||||
ingest_roots: list[dict]
|
||||
|
||||
|
||||
class ProjectUpdateRequest(BaseModel):
|
||||
aliases: list[str] | None = None
|
||||
description: str | None = None
|
||||
ingest_roots: list[dict] | None = None
|
||||
|
||||
|
||||
class QueryRequest(BaseModel):
|
||||
prompt: str
|
||||
top_k: int = 10
|
||||
@@ -217,6 +224,23 @@ def api_project_registration(req: ProjectRegistrationProposalRequest) -> dict:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.put("/projects/{project_name}")
|
||||
def api_project_update(project_name: str, req: ProjectUpdateRequest) -> dict:
|
||||
"""Update an existing project registration."""
|
||||
try:
|
||||
return update_project(
|
||||
project_name=project_name,
|
||||
aliases=req.aliases,
|
||||
description=req.description,
|
||||
ingest_roots=req.ingest_roots,
|
||||
)
|
||||
except ValueError as e:
|
||||
detail = str(e)
|
||||
if detail.startswith("Unknown project"):
|
||||
raise HTTPException(status_code=404, detail=detail)
|
||||
raise HTTPException(status_code=400, detail=detail)
|
||||
|
||||
|
||||
@router.post("/projects/{project_name}/refresh", response_model=ProjectRefreshResponse)
|
||||
def api_refresh_project(project_name: str, purge_deleted: bool = False) -> ProjectRefreshResponse:
|
||||
"""Refresh one registered project from its configured ingest roots."""
|
||||
|
||||
@@ -122,6 +122,78 @@ def register_project(
|
||||
}
|
||||
|
||||
|
||||
def update_project(
|
||||
project_name: str,
|
||||
aliases: list[str] | tuple[str, ...] | None = None,
|
||||
description: str | None = None,
|
||||
ingest_roots: list[dict] | tuple[dict, ...] | None = None,
|
||||
) -> dict:
|
||||
"""Update an existing project registration in the registry file."""
|
||||
existing = get_registered_project(project_name)
|
||||
if existing is None:
|
||||
raise ValueError(f"Unknown project: {project_name}")
|
||||
|
||||
final_aliases = _normalize_aliases(aliases) if aliases is not None else list(existing.aliases)
|
||||
final_description = description.strip() if description is not None else existing.description
|
||||
final_roots = (
|
||||
_normalize_ingest_roots(ingest_roots)
|
||||
if ingest_roots is not None
|
||||
else [asdict(root) for root in existing.ingest_roots]
|
||||
)
|
||||
if not final_roots:
|
||||
raise ValueError("At least one ingest root is required")
|
||||
|
||||
collisions = _find_name_collisions(
|
||||
existing.project_id,
|
||||
final_aliases,
|
||||
exclude_project_id=existing.project_id,
|
||||
)
|
||||
if collisions:
|
||||
collision_names = ", ".join(collision["name"] for collision in collisions)
|
||||
raise ValueError(f"Project update has collisions: {collision_names}")
|
||||
|
||||
updated_entry = {
|
||||
"id": existing.project_id,
|
||||
"aliases": final_aliases,
|
||||
"description": final_description,
|
||||
"ingest_roots": final_roots,
|
||||
}
|
||||
|
||||
resolved_roots = []
|
||||
for root in final_roots:
|
||||
source_ref = ProjectSourceRef(
|
||||
source=root["source"],
|
||||
subpath=root["subpath"],
|
||||
label=root.get("label", ""),
|
||||
)
|
||||
resolved_path = _resolve_ingest_root(source_ref)
|
||||
resolved_roots.append(
|
||||
{
|
||||
**root,
|
||||
"path": str(resolved_path),
|
||||
"exists": resolved_path.exists(),
|
||||
"is_dir": resolved_path.is_dir(),
|
||||
}
|
||||
)
|
||||
|
||||
registry_path = _config.settings.resolved_project_registry_path
|
||||
payload = _load_registry_payload(registry_path)
|
||||
payload["projects"] = [
|
||||
updated_entry if str(entry.get("id", "")).strip() == existing.project_id else entry
|
||||
for entry in payload.get("projects", [])
|
||||
]
|
||||
_write_registry_payload(registry_path, payload)
|
||||
|
||||
return {
|
||||
"project": updated_entry,
|
||||
"resolved_ingest_roots": resolved_roots,
|
||||
"collisions": [],
|
||||
"registry_path": str(registry_path),
|
||||
"valid": True,
|
||||
"status": "updated",
|
||||
}
|
||||
|
||||
|
||||
def load_project_registry() -> list[RegisteredProject]:
|
||||
"""Load project registry entries from JSON config."""
|
||||
registry_path = _config.settings.resolved_project_registry_path
|
||||
@@ -294,13 +366,19 @@ def _validate_unique_project_names(projects: list[RegisteredProject]) -> None:
|
||||
seen[key] = project.project_id
|
||||
|
||||
|
||||
def _find_name_collisions(project_id: str, aliases: list[str]) -> list[dict]:
|
||||
def _find_name_collisions(
|
||||
project_id: str,
|
||||
aliases: list[str],
|
||||
exclude_project_id: str | None = None,
|
||||
) -> list[dict]:
|
||||
collisions: list[dict] = []
|
||||
existing = load_project_registry()
|
||||
requested_names = [project_id, *aliases]
|
||||
for requested in requested_names:
|
||||
requested_key = requested.lower()
|
||||
for project in existing:
|
||||
if exclude_project_id is not None and project.project_id == exclude_project_id:
|
||||
continue
|
||||
project_names = [project.project_id, *project.aliases]
|
||||
if requested_key in {name.lower() for name in project_names}:
|
||||
collisions.append(
|
||||
|
||||
Reference in New Issue
Block a user