main
py 68 lines 2.01 KB
Raw
1 from agent import AgentContext
2 from helpers.api import ApiHandler, Request, Response
3 from helpers.persist_chat import remove_chat
4 from helpers.print_style import PrintStyle
5 import json
6
7
8 class ApiTerminateChat(ApiHandler):
9 @classmethod
10 def requires_auth(cls) -> bool:
11 return False
12
13 @classmethod
14 def requires_csrf(cls) -> bool:
15 return False
16
17 @classmethod
18 def requires_api_key(cls) -> bool:
19 return True
20
21 @classmethod
22 def get_methods(cls) -> list[str]:
23 return ["POST"]
24
25 async def process(self, input: dict, request: Request) -> dict | Response:
26 try:
27 # Get context_id from input
28 context_id = input.get("context_id")
29
30 if not context_id:
31 return Response(
32 '{"error": "context_id is required"}',
33 status=400,
34 mimetype="application/json"
35 )
36
37 # Check if context exists
38 context = AgentContext.use(context_id)
39 if not context:
40 return Response(
41 '{"error": "Chat context not found"}',
42 status=404,
43 mimetype="application/json"
44 )
45
46 # Delete the chat context
47 AgentContext.remove(context.id)
48 remove_chat(context.id)
49
50 # Log the deletion
51 PrintStyle(
52 background_color="#E74C3C", font_color="white", bold=True, padding=True
53 ).print(f"API Chat deleted: {context_id}")
54
55 # Return success response
56 return {
57 "success": True,
58 "message": "Chat deleted successfully",
59 "context_id": context_id
60 }
61
62 except Exception as e:
63 PrintStyle.error(f"API terminate chat error: {str(e)}")
64 return Response(
65 json.dumps({"error": f"Internal server error: {str(e)}"}),
66 status=500,
67 mimetype="application/json"
68 )