session.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import asyncio
  2. import os
  3. from typing import Optional
  4. from fastapi import WebSocketDisconnect
  5. from opendevin import config
  6. from opendevin.action import (
  7. Action,
  8. NullAction,
  9. )
  10. from opendevin.observation import NullObservation
  11. from opendevin.agent import Agent
  12. from opendevin.controller import AgentController
  13. from opendevin.llm.llm import LLM
  14. from opendevin.observation import Observation, UserMessageObservation
  15. DEFAULT_API_KEY = config.get_or_none("LLM_API_KEY")
  16. DEFAULT_BASE_URL = config.get_or_none("LLM_BASE_URL")
  17. DEFAULT_WORKSPACE_DIR = config.get_or_default("WORKSPACE_DIR", os.path.join(os.getcwd(), "workspace"))
  18. LLM_MODEL = config.get_or_default("LLM_MODEL", "gpt-4-0125-preview")
  19. CONTAINER_IMAGE = config.get_or_default("SANDBOX_CONTAINER_IMAGE", "ghcr.io/opendevin/sandbox")
  20. class Session:
  21. """Represents a session with an agent.
  22. Attributes:
  23. websocket: The WebSocket connection associated with the session.
  24. controller: The AgentController instance for controlling the agent.
  25. agent: The Agent instance representing the agent.
  26. agent_task: The task representing the agent's execution.
  27. """
  28. def __init__(self, websocket):
  29. """Initializes a new instance of the Session class.
  30. Args:
  31. websocket: The WebSocket connection associated with the session.
  32. """
  33. self.websocket = websocket
  34. self.controller: Optional[AgentController] = None
  35. self.agent: Optional[Agent] = None
  36. self.agent_task = None
  37. asyncio.create_task(self.create_controller(), name="create controller") # FIXME: starting the docker container synchronously causes a websocket error...
  38. async def send_error(self, message):
  39. """Sends an error message to the client.
  40. Args:
  41. message: The error message to send.
  42. """
  43. await self.send({"error": True, "message": message})
  44. async def send_message(self, message):
  45. """Sends a message to the client.
  46. Args:
  47. message: The message to send.
  48. """
  49. await self.send({"message": message})
  50. async def send(self, data):
  51. """Sends data to the client.
  52. Args:
  53. data: The data to send.
  54. """
  55. if self.websocket is None:
  56. return
  57. try:
  58. await self.websocket.send_json(data)
  59. except Exception as e:
  60. print("Error sending data to client", e)
  61. async def start_listening(self):
  62. """Starts listening for messages from the client."""
  63. try:
  64. while True:
  65. try:
  66. data = await self.websocket.receive_json()
  67. except ValueError:
  68. await self.send_error("Invalid JSON")
  69. continue
  70. action = data.get("action", None)
  71. if action is None:
  72. await self.send_error("Invalid event")
  73. continue
  74. if action == "initialize":
  75. await self.create_controller(data)
  76. elif action == "start":
  77. await self.start_task(data)
  78. else:
  79. if self.controller is None:
  80. await self.send_error("No agent started. Please wait a second...")
  81. elif action == "chat":
  82. self.controller.add_history(NullAction(), UserMessageObservation(data["message"]))
  83. else:
  84. await self.send_error("I didn't recognize this action:" + action)
  85. except WebSocketDisconnect as e:
  86. self.websocket = None
  87. if self.agent_task:
  88. self.agent_task.cancel()
  89. print("Client websocket disconnected", e)
  90. async def create_controller(self, start_event=None):
  91. """Creates an AgentController instance.
  92. Args:
  93. start_event: The start event data (optional).
  94. """
  95. directory = DEFAULT_WORKSPACE_DIR
  96. if start_event and "directory" in start_event["args"]:
  97. directory = start_event["args"]["directory"]
  98. agent_cls = "MonologueAgent"
  99. if start_event and "agent_cls" in start_event["args"]:
  100. agent_cls = start_event["args"]["agent_cls"]
  101. model = LLM_MODEL
  102. if start_event and "model" in start_event["args"]:
  103. model = start_event["args"]["model"]
  104. api_key = DEFAULT_API_KEY
  105. if start_event and "api_key" in start_event["args"]:
  106. api_key = start_event["args"]["api_key"]
  107. api_base = DEFAULT_BASE_URL
  108. if start_event and "api_base" in start_event["args"]:
  109. api_base = start_event["args"]["api_base"]
  110. container_image = CONTAINER_IMAGE
  111. if start_event and "container_image" in start_event["args"]:
  112. container_image = start_event["args"]["container_image"]
  113. max_iterations = 100
  114. if start_event and "max_iterations" in start_event["args"]:
  115. max_iterations = start_event["args"]["max_iterations"]
  116. if not os.path.exists(directory):
  117. print(f"Workspace directory {directory} does not exist. Creating it...")
  118. os.makedirs(directory)
  119. directory = os.path.relpath(directory, os.getcwd())
  120. llm = LLM(model=model, api_key=api_key, base_url=api_base)
  121. AgentCls = Agent.get_cls(agent_cls)
  122. self.agent = AgentCls(llm)
  123. try:
  124. self.controller = AgentController(self.agent, workdir=directory, max_iterations=max_iterations, container_image=container_image, callbacks=[self.on_agent_event])
  125. except Exception:
  126. print("Error creating controller.")
  127. await self.send_error("Error creating controller. Please check Docker is running using `docker ps`.")
  128. return
  129. await self.send({"action": "initialize", "message": "Control loop started."})
  130. async def start_task(self, start_event):
  131. """Starts a task for the agent.
  132. Args:
  133. start_event: The start event data.
  134. """
  135. if "task" not in start_event["args"]:
  136. await self.send_error("No task specified")
  137. return
  138. await self.send_message("Starting new task...")
  139. task = start_event["args"]["task"]
  140. if self.controller is None:
  141. await self.send_error("No agent started. Please wait a second...")
  142. return
  143. try:
  144. self.agent_task = await asyncio.create_task(self.controller.start_loop(task), name="agent loop")
  145. except Exception:
  146. await self.send_error("Error during task loop.")
  147. def on_agent_event(self, event: Observation | Action):
  148. """Callback function for agent events.
  149. Args:
  150. event: The agent event (Observation or Action).
  151. """
  152. if isinstance(event, NullAction):
  153. return
  154. if isinstance(event, NullObservation):
  155. return
  156. event_dict = event.to_dict()
  157. asyncio.create_task(self.send(event_dict), name="send event in callback")