Modularize existing work logic
26
main.py
@ -24,8 +24,9 @@ all_discovered_mcp_tools: list[dict] = []
|
|||||||
exit_stack = AsyncExitStack()
|
exit_stack = AsyncExitStack()
|
||||||
# Stores loaded persona data (as a string for easy injection into prompt)
|
# Stores loaded persona data (as a string for easy injection into prompt)
|
||||||
wolfhart_persona_details: str | None = None
|
wolfhart_persona_details: str | None = None
|
||||||
# --- Use standard thread-safe queue ---
|
# --- Use standard thread-safe queues ---
|
||||||
trigger_queue: ThreadSafeQueue = ThreadSafeQueue() # Use standard Queue
|
trigger_queue: ThreadSafeQueue = ThreadSafeQueue() # UI Thread -> Main Loop
|
||||||
|
command_queue: ThreadSafeQueue = ThreadSafeQueue() # Main Loop -> UI Thread
|
||||||
# --- End Change ---
|
# --- End Change ---
|
||||||
ui_monitor_task: asyncio.Task | None = None # To track the UI monitor task
|
ui_monitor_task: asyncio.Task | None = None # To track the UI monitor task
|
||||||
|
|
||||||
@ -205,8 +206,9 @@ async def run_main_with_exit_stack():
|
|||||||
# 3. Start UI Monitoring in a separate thread
|
# 3. Start UI Monitoring in a separate thread
|
||||||
print("\n--- Starting UI monitoring thread ---")
|
print("\n--- Starting UI monitoring thread ---")
|
||||||
loop = asyncio.get_running_loop() # Get loop for run_in_executor
|
loop = asyncio.get_running_loop() # Get loop for run_in_executor
|
||||||
|
# Use the new monitoring loop function, passing both queues
|
||||||
monitor_task = loop.create_task(
|
monitor_task = loop.create_task(
|
||||||
asyncio.to_thread(ui_interaction.monitor_chat_for_trigger, trigger_queue),
|
asyncio.to_thread(ui_interaction.run_ui_monitoring_loop, trigger_queue, command_queue), # Pass command_queue
|
||||||
name="ui_monitor"
|
name="ui_monitor"
|
||||||
)
|
)
|
||||||
ui_monitor_task = monitor_task # Store task reference for shutdown
|
ui_monitor_task = monitor_task # Store task reference for shutdown
|
||||||
@ -265,15 +267,16 @@ async def run_main_with_exit_stack():
|
|||||||
if thoughts:
|
if thoughts:
|
||||||
print(f"AI Thoughts: {thoughts[:150]}..." if len(thoughts) > 150 else f"AI Thoughts: {thoughts}")
|
print(f"AI Thoughts: {thoughts[:150]}..." if len(thoughts) > 150 else f"AI Thoughts: {thoughts}")
|
||||||
|
|
||||||
# 只有當有效回應時才發送到遊戲
|
# 只有當有效回應時才發送到遊戲 (via command queue)
|
||||||
if bot_dialogue and valid_response:
|
if bot_dialogue and valid_response:
|
||||||
print("Preparing to send dialogue response via UI...")
|
print("Sending 'send_reply' command to UI thread...")
|
||||||
send_success = await asyncio.to_thread(
|
command_to_send = {'action': 'send_reply', 'text': bot_dialogue}
|
||||||
ui_interaction.paste_and_send_reply,
|
try:
|
||||||
bot_dialogue
|
# Put command into the queue for the UI thread to handle
|
||||||
)
|
await loop.run_in_executor(None, command_queue.put, command_to_send)
|
||||||
if send_success: print("Response sent successfully.")
|
print("Command placed in queue.")
|
||||||
else: print("Error: Failed to send response via UI.")
|
except Exception as q_err:
|
||||||
|
print(f"Error putting command in queue: {q_err}")
|
||||||
else:
|
else:
|
||||||
print("Not sending response: Invalid or empty dialogue content.")
|
print("Not sending response: Invalid or empty dialogue content.")
|
||||||
|
|
||||||
@ -309,4 +312,3 @@ if __name__ == "__main__":
|
|||||||
print(f"Top-level error during asyncio.run execution: {e}")
|
print(f"Top-level error during asyncio.run execution: {e}")
|
||||||
finally:
|
finally:
|
||||||
print("Program exited.")
|
print("Program exited.")
|
||||||
|
|
||||||
|
|||||||
BIN
templates/In_private_room.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
templates/In_world_room.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
templates/Previous_page.png
Normal file
|
After Width: | Height: | Size: 4.6 KiB |
BIN
templates/Private_Label_normal.png
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
templates/World_Label_normal.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
templates/World_map.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 4.6 KiB |