main
py 70 lines 2.13 KB
Raw
1 from agent import AgentContext
2 from helpers.api import ApiHandler, Request, Response
3 from helpers.print_style import PrintStyle
4 from helpers import persist_chat
5 import json
6
7
8 class ApiResetChat(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 # Reset the chat context (clears history but keeps context alive)
47 context.reset()
48 # Save the reset context to persist the changes
49 persist_chat.save_tmp_chat(context)
50 persist_chat.remove_msg_files(context_id)
51
52 # Log the reset
53 PrintStyle(
54 background_color="#3498DB", font_color="white", bold=True, padding=True
55 ).print(f"API Chat reset: {context_id}")
56
57 # Return success response
58 return {
59 "success": True,
60 "message": "Chat reset successfully",
61 "context_id": context_id
62 }
63
64 except Exception as e:
65 PrintStyle.error(f"API reset chat error: {str(e)}")
66 return Response(
67 json.dumps({"error": f"Internal server error: {str(e)}"}),
68 status=500,
69 mimetype="application/json"
70 )