feat(api): manage declarative plugins
This commit is contained in:
@ -1971,6 +1971,71 @@ def create_app(
|
||||
)
|
||||
return result
|
||||
|
||||
@app.get("/api/plugins")
|
||||
async def list_plugins(request: Request) -> list[dict[str, Any]]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
return [_plugin_payload(loaded, state) for state in loaded.plugin_manager.list_plugins()] # type: ignore[union-attr]
|
||||
|
||||
@app.post("/api/plugins/sync")
|
||||
async def sync_plugins(request: Request) -> list[dict[str, Any]]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
try:
|
||||
states = loaded.plugin_manager.sync_enabled().values() # type: ignore[union-attr]
|
||||
except ValueError as exc:
|
||||
raise _plugin_http_error(exc) from exc
|
||||
return [_plugin_payload(loaded, state) for state in states]
|
||||
|
||||
@app.post("/api/plugins/{plugin_id}/enable")
|
||||
async def enable_plugin(plugin_id: str, request: Request) -> dict[str, Any]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
try:
|
||||
state = loaded.plugin_manager.enable(plugin_id) # type: ignore[union-attr]
|
||||
except ValueError as exc:
|
||||
raise _plugin_http_error(exc) from exc
|
||||
return _plugin_payload(loaded, state)
|
||||
|
||||
@app.post("/api/plugins/{plugin_id}/pause")
|
||||
async def pause_plugin(plugin_id: str, request: Request) -> dict[str, Any]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
try:
|
||||
state = loaded.plugin_manager.pause(plugin_id) # type: ignore[union-attr]
|
||||
except ValueError as exc:
|
||||
raise _plugin_http_error(exc) from exc
|
||||
return _plugin_payload(loaded, state)
|
||||
|
||||
@app.post("/api/plugins/{plugin_id}/resume")
|
||||
async def resume_plugin(plugin_id: str, request: Request) -> dict[str, Any]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
try:
|
||||
state = loaded.plugin_manager.resume(plugin_id) # type: ignore[union-attr]
|
||||
except ValueError as exc:
|
||||
raise _plugin_http_error(exc) from exc
|
||||
return _plugin_payload(loaded, state)
|
||||
|
||||
@app.post("/api/plugins/{plugin_id}/disable")
|
||||
async def disable_plugin(plugin_id: str, request: Request, payload: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
try:
|
||||
state = loaded.plugin_manager.disable( # type: ignore[union-attr]
|
||||
plugin_id,
|
||||
disable_linked_skills=bool((payload or {}).get("disable_linked_skills")),
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise _plugin_http_error(exc) from exc
|
||||
return _plugin_payload(loaded, state)
|
||||
|
||||
@app.post("/api/plugins/{plugin_id}/skills/{skill_name}/adopt")
|
||||
async def adopt_plugin_skill(plugin_id: str, skill_name: str, request: Request) -> dict[str, Any]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
try:
|
||||
loaded.plugin_manager.adopt(plugin_id, skill_name) # type: ignore[union-attr]
|
||||
state = loaded.plugin_manager.state_store.get_plugin(plugin_id) # type: ignore[union-attr]
|
||||
except ValueError as exc:
|
||||
raise _plugin_http_error(exc) from exc
|
||||
if state is None:
|
||||
raise HTTPException(status_code=404, detail="Plugin not found")
|
||||
return _plugin_payload(loaded, state)
|
||||
|
||||
@app.get("/api/skills")
|
||||
async def list_skills(request: Request) -> list[dict[str, Any]]:
|
||||
loaded = get_agent_service(request).create_loop().boot()
|
||||
@ -4123,6 +4188,43 @@ def _skill_draft_http_error(exc: ValueError) -> HTTPException:
|
||||
return HTTPException(status_code=status_code, detail=detail)
|
||||
|
||||
|
||||
def _plugin_payload(loaded: Any, state: Any) -> dict[str, Any]:
|
||||
manifest = loaded.plugin_manager.manifests.get(state.plugin_id) # type: ignore[union-attr]
|
||||
return {
|
||||
"id": state.plugin_id,
|
||||
"name": manifest.name if manifest is not None else state.plugin_id,
|
||||
"discovered_version": manifest.version if manifest is not None else None,
|
||||
"installed_version": state.installed_version,
|
||||
"enabled": state.enabled,
|
||||
"status": state.status,
|
||||
"last_error": state.last_error,
|
||||
"manifest_path": manifest.display_path if manifest is not None else state.manifest_path,
|
||||
"updates_paused": state.updates_paused,
|
||||
"skills": [
|
||||
{
|
||||
"name": name,
|
||||
"status": binding.status,
|
||||
"current_beaver_version": binding.current_beaver_version,
|
||||
"accepted_upstream_tree_hash": binding.accepted_upstream_tree_hash,
|
||||
"observed_upstream_tree_hash": binding.observed_upstream_tree_hash,
|
||||
"accepted_beaver_version": binding.accepted_beaver_version,
|
||||
"pending_candidate_id": binding.pending_candidate_id,
|
||||
}
|
||||
for name, binding in sorted(state.skills.items())
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _plugin_http_error(exc: ValueError) -> HTTPException:
|
||||
detail = str(exc)
|
||||
lowered = detail.lower()
|
||||
if "unknown plugin" in lowered or "unknown plugin state" in lowered or "not found" in lowered:
|
||||
return HTTPException(status_code=404, detail=detail)
|
||||
if "conflict" in lowered or "busy" in lowered:
|
||||
return HTTPException(status_code=409, detail=detail)
|
||||
return HTTPException(status_code=400, detail=detail)
|
||||
|
||||
|
||||
def _mask_secret(value: str | None) -> str:
|
||||
secret = _clean_text(value)
|
||||
if not secret:
|
||||
|
||||
Reference in New Issue
Block a user