conversation.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import asyncio
  2. from openhands.core.config import AppConfig
  3. from openhands.events.stream import EventStream
  4. from openhands.runtime import get_runtime_cls
  5. from openhands.runtime.base import Runtime
  6. from openhands.security import SecurityAnalyzer, options
  7. from openhands.storage.files import FileStore
  8. from openhands.utils.async_utils import call_sync_from_async
  9. class Conversation:
  10. sid: str
  11. file_store: FileStore
  12. event_stream: EventStream
  13. runtime: Runtime
  14. def __init__(
  15. self,
  16. sid: str,
  17. file_store: FileStore,
  18. config: AppConfig,
  19. ):
  20. self.sid = sid
  21. self.config = config
  22. self.file_store = file_store
  23. self.event_stream = EventStream(sid, file_store)
  24. if config.security.security_analyzer:
  25. self.security_analyzer = options.SecurityAnalyzers.get(
  26. config.security.security_analyzer, SecurityAnalyzer
  27. )(self.event_stream)
  28. runtime_cls = get_runtime_cls(self.config.runtime)
  29. self.runtime = runtime_cls(
  30. config=config,
  31. event_stream=self.event_stream,
  32. sid=self.sid,
  33. attach_to_existing=True,
  34. headless_mode=False,
  35. )
  36. async def connect(self):
  37. await self.runtime.connect()
  38. async def disconnect(self):
  39. asyncio.create_task(call_sync_from_async(self.runtime.close))