| 1 | """POST /api/plugins/_a0_connector/v1/pause.""" |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from helpers.api import Request, Response |
| 5 | import plugins._a0_connector.api.v1.base as connector_base |
| 6 | |
| 7 | |
| 8 | class Pause(connector_base.ProtectedConnectorApiHandler): |
| 9 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 10 | from agent import AgentContext |
| 11 | |
| 12 | context_id = str(input.get("context_id", "")).strip() |
| 13 | raw_paused = input.get("paused", True) |
| 14 | if isinstance(raw_paused, str): |
| 15 | paused = raw_paused.strip().lower() not in {"", "0", "false", "no", "off"} |
| 16 | else: |
| 17 | paused = bool(raw_paused) |
| 18 | |
| 19 | if not context_id: |
| 20 | return Response( |
| 21 | response='{"error": "context_id is required"}', |
| 22 | status=400, |
| 23 | mimetype="application/json", |
| 24 | ) |
| 25 | |
| 26 | context = AgentContext.get(context_id) |
| 27 | if context is None: |
| 28 | return Response( |
| 29 | response='{"error": "Context not found"}', |
| 30 | status=404, |
| 31 | mimetype="application/json", |
| 32 | ) |
| 33 | |
| 34 | if paused and not context.is_running(): |
| 35 | return Response( |
| 36 | response='{"error": "Context is not currently running"}', |
| 37 | status=409, |
| 38 | mimetype="application/json", |
| 39 | ) |
| 40 | |
| 41 | context.paused = paused |
| 42 | return { |
| 43 | "ok": True, |
| 44 | "context_id": context_id, |
| 45 | "paused": paused, |
| 46 | "status": "paused" if paused else "running", |
| 47 | "message": "Agent paused." if paused else "Agent unpaused.", |
| 48 | } |