diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index 2726ef6..53fe095 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ diff --git a/core/views.py b/core/views.py index 1bc3e9a..73e0364 100644 --- a/core/views.py +++ b/core/views.py @@ -210,14 +210,14 @@ def chat_view(request, conversation_id=None): **Execution Loop:** -1. You issue a command. -2. The system executes it. -3. The system returns a result message to you, like `[SYSTEM] Command 'add_task' executed successfully. New task ID: 5`. -4. You can then issue another command. +1. You can issue a series of commands to be executed sequentially. +2. The system executes each command and provides a result. +3. The loop will stop if you call `send_message` or after a maximum of 7 iterations. **VERY IMPORTANT:** -- To talk to the user, you MUST use the `send_message` command. -- ONLY use other commands if the user explicitly asks you to `add`, `edit`, or `delete` tasks. +- To talk to the user, you MUST use the `send_message` command. This command will STOP the execution loop. +- If you need to perform multiple actions (e.g., add a task and then comment on it), issue the action commands (`add_task`, `edit_task`, etc.) *before* using `send_message`. +- ONLY use commands other than `send_message` if the user explicitly asks you to `add`, `edit`, or `delete` tasks. **Examples:** @@ -259,20 +259,25 @@ def chat_view(request, conversation_id=None): "content": f"Here is the current list of tasks:\n{task_list_str}" } - logger.info("Sending request to AI...") - response = LocalAIApi.create_response({ - "input": [ - system_message, - tasks_context, - ] + history, - "text": {"format": {"type": "json_object"}}, - }) + logger.info("Starting AI processing loop...") - if not response.get("success"): - logger.error(f"AI API request failed with status {response.get('status')}. Full error: {response.get('response')}") - ai_text = "I couldn't process that. Please try again." - Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') - else: + for i in range(7): # Loop up to 7 times + logger.info(f"AI loop iteration {i+1}") + + response = LocalAIApi.create_response({ + "input": [ + system_message, + tasks_context, + ] + history, + "text": {"format": {"type": "json_object"}}, + }) + + if not response.get("success"): + logger.error(f"AI API request failed with status {response.get('status')}. Full error: {response.get('response')}") + ai_text = "I couldn't process that. Please try again." + Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') + break + logger.info(f"AI raw response: {response}") ai_text = LocalAIApi.extract_text(response) logger.info(f"Extracted AI text: {ai_text}") @@ -281,23 +286,37 @@ def chat_view(request, conversation_id=None): logger.warning("AI response was empty.") ai_text = "I couldn't process that. Please try again." Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') - else: - ai_message_content = ai_text - sender = 'ai' - - try: - command_json = json.loads(ai_text) - if 'command' in command_json: - command_name = command_json.get('command', {}).get('name') - command_result = execute_command(command_json['command']) - - ai_message_content = command_result - sender = 'ai' if command_name == 'send_message' else 'system' - except (json.JSONDecodeError, TypeError): - # Not a JSON command, treat as a raw message. - pass + break + + try: + command_json = json.loads(ai_text) + if 'command' in command_json: + command_name = command_json.get('command', {}).get('name') + command_result = execute_command(command_json['command']) + + sender = 'ai' if command_name == 'send_message' else 'system' + Message.objects.create(conversation=selected_conversation, content=command_result, sender=sender) + + if command_name == 'send_message': + break # Exit loop if send_message is called + + # Add system message with command result to history for next iteration + history.append({"role": "user", "content": command_result}) - Message.objects.create(conversation=selected_conversation, content=ai_message_content, sender=sender) + else: + # If it's a JSON but not a command, save it as a message and break + Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') + break + + except (json.JSONDecodeError, TypeError): + # Not a JSON command, treat as a raw message and break + Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') + break + else: + # This block executes if the loop completes without a 'break' + logger.warning("AI loop finished after 7 iterations without sending a message.") + final_message = "I seem to be stuck in a loop. Could you clarify what you'd like me to do?" + Message.objects.create(conversation=selected_conversation, content=final_message, sender='ai') except Exception as e: logger.error(f"An unexpected error occurred: {e}", exc_info=True)