| 1 | from agent import AgentContext |
| 2 | from helpers.api import ApiHandler, Request, Response |
| 3 | |
| 4 | |
| 5 | def stop_context(context: AgentContext) -> dict: |
| 6 | was_running = context.is_running() |
| 7 | |
| 8 | context.kill_process() |
| 9 | context.paused = False |
| 10 | context.log.set_progress("", active=False) |
| 11 | |
| 12 | message = "Agent process stopped." |
| 13 | context.log.log(type="info", content=message, finished=True) |
| 14 | |
| 15 | return { |
| 16 | "message": message, |
| 17 | "context": context.id, |
| 18 | "stopped": was_running, |
| 19 | } |
| 20 | |
| 21 | |
| 22 | class Stop(ApiHandler): |
| 23 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 24 | ctxid = input.get("context", "") |
| 25 | if not isinstance(ctxid, str) or not ctxid.strip(): |
| 26 | return Response( |
| 27 | '{"error": "context is required"}', |
| 28 | status=400, |
| 29 | mimetype="application/json", |
| 30 | ) |
| 31 | |
| 32 | context = AgentContext.use(ctxid.strip()) |
| 33 | if not context: |
| 34 | return Response( |
| 35 | '{"error": "Chat context not found"}', |
| 36 | status=404, |
| 37 | mimetype="application/json", |
| 38 | ) |
| 39 | return stop_context(context) |