cli.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import asyncio
  2. import logging
  3. from typing import Type
  4. from termcolor import colored
  5. import openhands.agenthub # noqa F401 (we import this to get the agents registered)
  6. from openhands import __version__
  7. from openhands.controller import AgentController
  8. from openhands.controller.agent import Agent
  9. from openhands.core.config import (
  10. get_parser,
  11. load_app_config,
  12. )
  13. from openhands.core.logger import openhands_logger as logger
  14. from openhands.core.schema import AgentState
  15. from openhands.events import EventSource, EventStream, EventStreamSubscriber
  16. from openhands.events.action import (
  17. Action,
  18. ChangeAgentStateAction,
  19. CmdRunAction,
  20. MessageAction,
  21. )
  22. from openhands.events.event import Event
  23. from openhands.events.observation import (
  24. AgentStateChangedObservation,
  25. CmdOutputObservation,
  26. )
  27. from openhands.llm.llm import LLM
  28. from openhands.runtime import get_runtime_cls
  29. from openhands.runtime.runtime import Runtime
  30. from openhands.storage import get_file_store
  31. def display_message(message: str):
  32. print(colored('🤖 ' + message + '\n', 'yellow'))
  33. def display_command(command: str):
  34. print('❯ ' + colored(command + '\n', 'green'))
  35. def display_command_output(output: str):
  36. lines = output.split('\n')
  37. for line in lines:
  38. if line.startswith('[Python Interpreter') or line.startswith('openhands@'):
  39. # TODO: clean this up once we clean up terminal output
  40. continue
  41. print(colored(line, 'blue'))
  42. print('\n')
  43. def display_event(event: Event):
  44. if isinstance(event, Action):
  45. if hasattr(event, 'thought'):
  46. display_message(event.thought)
  47. if isinstance(event, MessageAction):
  48. if event.source != EventSource.USER:
  49. display_message(event.content)
  50. if isinstance(event, CmdRunAction):
  51. display_command(event.command)
  52. if isinstance(event, CmdOutputObservation):
  53. display_command_output(event.content)
  54. async def main():
  55. """Runs the agent in CLI mode"""
  56. parser = get_parser()
  57. # Add the version argument
  58. parser.add_argument(
  59. '-v',
  60. '--version',
  61. action='version',
  62. version=f'{__version__}',
  63. help='Show the version number and exit',
  64. default=None,
  65. )
  66. args = parser.parse_args()
  67. if args.version:
  68. print(f'OpenHands version: {__version__}')
  69. return
  70. logger.setLevel(logging.WARNING)
  71. config = load_app_config(config_file=args.config_file)
  72. sid = 'cli'
  73. agent_cls: Type[Agent] = Agent.get_cls(config.default_agent)
  74. agent_config = config.get_agent_config(config.default_agent)
  75. llm_config = config.get_llm_config_from_agent(config.default_agent)
  76. agent = agent_cls(
  77. llm=LLM(config=llm_config),
  78. config=agent_config,
  79. )
  80. file_store = get_file_store(config.file_store, config.file_store_path)
  81. event_stream = EventStream(sid, file_store)
  82. runtime_cls = get_runtime_cls(config.runtime)
  83. runtime: Runtime = runtime_cls( # noqa: F841
  84. config=config,
  85. event_stream=event_stream,
  86. sid=sid,
  87. plugins=agent_cls.sandbox_plugins,
  88. )
  89. controller = AgentController(
  90. agent=agent,
  91. max_iterations=config.max_iterations,
  92. max_budget_per_task=config.max_budget_per_task,
  93. agent_to_llm_config=config.get_agent_to_llm_config_map(),
  94. event_stream=event_stream,
  95. )
  96. if controller is not None:
  97. controller.agent_task = asyncio.create_task(controller.start_step_loop())
  98. async def prompt_for_next_task():
  99. next_message = input('How can I help? >> ')
  100. if next_message == 'exit':
  101. event_stream.add_event(
  102. ChangeAgentStateAction(AgentState.STOPPED), EventSource.USER
  103. )
  104. return
  105. action = MessageAction(content=next_message)
  106. event_stream.add_event(action, EventSource.USER)
  107. async def on_event(event: Event):
  108. display_event(event)
  109. if isinstance(event, AgentStateChangedObservation):
  110. if event.agent_state == AgentState.ERROR:
  111. print('An error occurred. Please try again.')
  112. if event.agent_state in [
  113. AgentState.AWAITING_USER_INPUT,
  114. AgentState.FINISHED,
  115. AgentState.ERROR,
  116. ]:
  117. await prompt_for_next_task()
  118. event_stream.subscribe(EventStreamSubscriber.MAIN, on_event)
  119. await prompt_for_next_task()
  120. while controller.state.agent_state not in [
  121. AgentState.STOPPED,
  122. ]:
  123. await asyncio.sleep(1) # Give back control for a tick, so the agent can run
  124. print('Exiting...')
  125. await controller.close()
  126. if __name__ == '__main__':
  127. loop = asyncio.get_event_loop()
  128. try:
  129. loop.run_until_complete(main())
  130. finally:
  131. pass