main.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import asyncio
  2. import sys
  3. from typing import Callable, Optional, Type
  4. import agenthub # noqa F401 (we import this to get the agents registered)
  5. from opendevin.controller import AgentController
  6. from opendevin.controller.agent import Agent
  7. from opendevin.controller.state.state import State
  8. from opendevin.core.config import args, get_llm_config_arg
  9. from opendevin.core.logger import opendevin_logger as logger
  10. from opendevin.core.schema import AgentState
  11. from opendevin.events import EventSource, EventStream, EventStreamSubscriber
  12. from opendevin.events.action import MessageAction
  13. from opendevin.events.event import Event
  14. from opendevin.events.observation import AgentStateChangedObservation
  15. from opendevin.llm.llm import LLM
  16. from opendevin.runtime.sandbox import Sandbox
  17. from opendevin.runtime.server.runtime import ServerRuntime
  18. def read_task_from_file(file_path: str) -> str:
  19. """Read task from the specified file."""
  20. with open(file_path, 'r', encoding='utf-8') as file:
  21. return file.read()
  22. def read_task_from_stdin() -> str:
  23. """Read task from stdin."""
  24. return sys.stdin.read()
  25. async def main(
  26. task_str: str = '',
  27. exit_on_message: bool = False,
  28. fake_user_response_fn: Optional[Callable[[Optional[State]], str]] = None,
  29. sandbox: Optional[Sandbox] = None,
  30. ) -> Optional[State]:
  31. """Main coroutine to run the agent controller with task input flexibility.
  32. It's only used when you launch opendevin backend directly via cmdline.
  33. Args:
  34. task_str: The task to run.
  35. exit_on_message: quit if agent asks for a message from user (optional)
  36. fake_user_response_fn: An optional function that receives the current state (could be None) and returns a fake user response.
  37. sandbox: An optional sandbox to run the agent in.
  38. """
  39. # Determine the task source
  40. if task_str:
  41. task = task_str
  42. elif args.file:
  43. task = read_task_from_file(args.file)
  44. elif args.task:
  45. task = args.task
  46. elif not sys.stdin.isatty():
  47. task = read_task_from_stdin()
  48. else:
  49. raise ValueError('No task provided. Please specify a task through -t, -f.')
  50. # only one of model_name or llm_config is required
  51. if args.llm_config:
  52. # --llm_config
  53. # llm_config can contain any of the attributes of LLMConfig
  54. llm_config = get_llm_config_arg(args.llm_config)
  55. if llm_config is None:
  56. raise ValueError(f'Invalid toml file, cannot read {args.llm_config}')
  57. logger.info(
  58. f'Running agent {args.agent_cls} (model: {llm_config.model}, llm_config: {args.llm_config}) with task: "{task}"'
  59. )
  60. # create LLM instance with the given config
  61. llm = LLM(llm_config=llm_config)
  62. else:
  63. # --model-name model_name
  64. logger.info(
  65. f'Running agent {args.agent_cls} (model: {args.model_name}), with task: "{task}"'
  66. )
  67. llm = LLM(args.model_name)
  68. AgentCls: Type[Agent] = Agent.get_cls(args.agent_cls)
  69. agent = AgentCls(llm=llm)
  70. event_stream = EventStream('main')
  71. controller = AgentController(
  72. agent=agent,
  73. max_iterations=args.max_iterations,
  74. max_budget_per_task=args.max_budget_per_task,
  75. max_chars=args.max_chars,
  76. event_stream=event_stream,
  77. )
  78. runtime = ServerRuntime(event_stream=event_stream, sandbox=sandbox)
  79. runtime.init_sandbox_plugins(controller.agent.sandbox_plugins)
  80. runtime.init_runtime_tools(controller.agent.runtime_tools, is_async=False)
  81. await event_stream.add_event(MessageAction(content=task), EventSource.USER)
  82. async def on_event(event: Event):
  83. if isinstance(event, AgentStateChangedObservation):
  84. if event.agent_state == AgentState.AWAITING_USER_INPUT:
  85. if exit_on_message:
  86. message = '/exit'
  87. elif fake_user_response_fn is None:
  88. message = input('Request user input >> ')
  89. else:
  90. message = fake_user_response_fn(controller.get_state())
  91. action = MessageAction(content=message)
  92. await event_stream.add_event(action, EventSource.USER)
  93. event_stream.subscribe(EventStreamSubscriber.MAIN, on_event)
  94. while controller.get_agent_state() not in [
  95. AgentState.FINISHED,
  96. AgentState.ERROR,
  97. AgentState.PAUSED,
  98. AgentState.STOPPED,
  99. ]:
  100. await asyncio.sleep(1) # Give back control for a tick, so the agent can run
  101. await controller.close()
  102. runtime.close()
  103. return controller.get_state()
  104. if __name__ == '__main__':
  105. asyncio.run(main())