Update Web-LLM.py

This commit is contained in:
Hafeez 2024-11-25 16:37:25 +05:30 committed by GitHub
parent 1b87b3188d
commit f7cf4fa017
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -86,13 +86,15 @@ def get_multiline_input() -> str:
sys.stdout.write('\n') # New line for clean display sys.stdout.write('\n') # New line for clean display
if current_line: if current_line:
lines.append(''.join(current_line)) lines.append(''.join(current_line))
return ' '.join(lines).strip() result = ''.join(lines).strip() if lines else ''.join(current_line).strip()
return result if result else '' # Return empty string instead of None
# Handle special characters # Handle special characters
elif char in [b'\r', b'\n']: # Enter elif char in [b'\r', b'\n']: # Enter
sys.stdout.write('\n') sys.stdout.write('\n')
lines.append(''.join(current_line)) if current_line: # Only append if there's content
current_line = [] lines.append(''.join(current_line))
current_line = []
elif char == b'\x08': # Backspace elif char == b'\x08': # Backspace
if current_line: if current_line:
@ -180,6 +182,7 @@ def handle_research_mode(research_manager, query):
try: try:
# Start the research # Start the research
research_manager.start_research(query) research_manager.start_research(query)
research_active = True # Flag to track research state
submit_key = "CTRL+Z" if os.name == 'nt' else "CTRL+D" submit_key = "CTRL+Z" if os.name == 'nt' else "CTRL+D"
print(f"\n{Fore.YELLOW}Research Running. Available Commands:{Style.RESET_ALL}") print(f"\n{Fore.YELLOW}Research Running. Available Commands:{Style.RESET_ALL}")
@ -190,12 +193,20 @@ def handle_research_mode(research_manager, query):
print("'q' = Quit research") print("'q' = Quit research")
# While the research is active, keep checking for commands # While the research is active, keep checking for commands
while research_manager.is_active(): while research_active and research_manager.is_active():
try: try:
command = get_multiline_input().strip().lower() # Ensure input is captured print(f"\n{Fore.GREEN}Enter command (s/f/p/q) and press {submit_key} to submit:{Style.RESET_ALL}")
command = get_multiline_input().strip().lower()
# Handle empty input
if not command:
continue
if command == 's': # Show status command if command == 's': # Show status command
print("\n" + research_manager.get_progress()) status = research_manager.get_progress()
print("\n" + status)
# Don't break or stop research after showing status
continue
elif command == 'f': # Show current focus command elif command == 'f': # Show current focus command
if research_manager.current_focus: if research_manager.current_focus:
@ -205,42 +216,53 @@ def handle_research_mode(research_manager, query):
print(f"Reasoning: {research_manager.current_focus.reasoning}") print(f"Reasoning: {research_manager.current_focus.reasoning}")
else: else:
print(f"\n{Fore.YELLOW}No current focus area{Style.RESET_ALL}") print(f"\n{Fore.YELLOW}No current focus area{Style.RESET_ALL}")
continue
elif command == 'p': # Pause research progress command elif command == 'p': # Pause research progress command
research_manager.pause_and_assess() research_manager.pause_and_assess()
continue
elif command == 'q': # Quit research elif command == 'q': # Quit research
print(f"\n{Fore.YELLOW}Research terminated by user.{Style.RESET_ALL}") print(f"\n{Fore.YELLOW}Research terminated by user.{Style.RESET_ALL}")
break # Exit the loop and end the research session research_active = False
break
else: else:
print(f"{Fore.RED}Unknown command. Please enter a valid command.{Style.RESET_ALL}") print(f"{Fore.RED}Unknown command. Please enter a valid command (s/f/p/q).{Style.RESET_ALL}")
continue
except KeyboardInterrupt: except KeyboardInterrupt:
print(f"\n{Fore.YELLOW}Research interrupted by user.{Style.RESET_ALL}") print(f"\n{Fore.YELLOW}Research interrupted by user.{Style.RESET_ALL}")
break # Handle interrupt gracefully research_active = False
break
except Exception as e: except Exception as e:
logger.error(f"Error processing command: {str(e)}") logger.error(f"Error processing command: {str(e)}")
print(f"{Fore.RED}An error occurred: {str(e)}{Style.RESET_ALL}") print(f"{Fore.RED}An error occurred: {str(e)}{Style.RESET_ALL}")
continue
# After finishing or quitting research, show the summary # Only terminate if research is no longer active
summary = research_manager.terminate_research() if not research_active:
research_manager._cleanup_research_ui() # Now this should work without errors print("\nInitiating research termination...")
summary = research_manager.terminate_research()
# Show research summary try:
print(f"\n{Fore.GREEN}Research Summary:{Style.RESET_ALL}") research_manager._cleanup_research_ui()
print(summary) except Exception as e:
logger.error(f"Error during UI cleanup: {str(e)}")
# Only proceed to conversation mode if research is complete print(f"\n{Fore.GREEN}Research Summary:{Style.RESET_ALL}")
if research_manager.research_complete and research_manager.research_summary: print(summary)
time.sleep(0.5) # Small delay to ensure clean transition
research_manager.start_conversation_mode() if research_manager.research_complete and research_manager.research_summary:
time.sleep(0.5)
research_manager.start_conversation_mode()
except KeyboardInterrupt: except KeyboardInterrupt:
print(f"\n{Fore.YELLOW}Research interrupted.{Style.RESET_ALL}") print(f"\n{Fore.YELLOW}Research interrupted.{Style.RESET_ALL}")
research_manager.terminate_research() research_manager.terminate_research()
except Exception as e: except Exception as e:
logger.error(f"Research error: {str(e)}")
print(f"\n{Fore.RED}Research error: {str(e)}{Style.RESET_ALL}") print(f"\n{Fore.RED}Research error: {str(e)}{Style.RESET_ALL}")
research_manager.terminate_research() research_manager.terminate_research()
def main(): def main():