| 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 | ) |