ai agent!

This commit is contained in:
Flatlogic Bot 2025-11-20 00:53:41 +00:00
parent 3b7559b157
commit e2769f2d2d
2 changed files with 54 additions and 35 deletions

View File

@ -210,14 +210,14 @@ def chat_view(request, conversation_id=None):
**Execution Loop:** **Execution Loop:**
1. You issue a command. 1. You can issue a series of commands to be executed sequentially.
2. The system executes it. 2. The system executes each command and provides a result.
3. The system returns a result message to you, like `[SYSTEM] Command 'add_task' executed successfully. New task ID: 5`. 3. The loop will stop if you call `send_message` or after a maximum of 7 iterations.
4. You can then issue another command.
**VERY IMPORTANT:** **VERY IMPORTANT:**
- To talk to the user, you MUST use the `send_message` command. - To talk to the user, you MUST use the `send_message` command. This command will STOP the execution loop.
- ONLY use other commands if the user explicitly asks you to `add`, `edit`, or `delete` tasks. - 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:** **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}" "content": f"Here is the current list of tasks:\n{task_list_str}"
} }
logger.info("Sending request to AI...") logger.info("Starting AI processing loop...")
response = LocalAIApi.create_response({
"input": [
system_message,
tasks_context,
] + history,
"text": {"format": {"type": "json_object"}},
})
if not response.get("success"): for i in range(7): # Loop up to 7 times
logger.error(f"AI API request failed with status {response.get('status')}. Full error: {response.get('response')}") logger.info(f"AI loop iteration {i+1}")
ai_text = "I couldn't process that. Please try again."
Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') response = LocalAIApi.create_response({
else: "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}") logger.info(f"AI raw response: {response}")
ai_text = LocalAIApi.extract_text(response) ai_text = LocalAIApi.extract_text(response)
logger.info(f"Extracted AI text: {ai_text}") 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.") logger.warning("AI response was empty.")
ai_text = "I couldn't process that. Please try again." ai_text = "I couldn't process that. Please try again."
Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai') Message.objects.create(conversation=selected_conversation, content=ai_text, sender='ai')
else: break
ai_message_content = ai_text
sender = 'ai' try:
command_json = json.loads(ai_text)
try: if 'command' in command_json:
command_json = json.loads(ai_text) command_name = command_json.get('command', {}).get('name')
if 'command' in command_json: command_result = execute_command(command_json['command'])
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)
ai_message_content = command_result
sender = 'ai' if command_name == 'send_message' else 'system' if command_name == 'send_message':
except (json.JSONDecodeError, TypeError): break # Exit loop if send_message is called
# Not a JSON command, treat as a raw message.
pass # 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: except Exception as e:
logger.error(f"An unexpected error occurred: {e}", exc_info=True) logger.error(f"An unexpected error occurred: {e}", exc_info=True)