OpenHands/openhands/server/session/conversation.py
Xingyao Wang ca424ec15d
[agent] Add LLM risk analyzer (#9349)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: Graham Neubig <neubig@gmail.com>
Co-authored-by: llamantino <213239228+llamantino@users.noreply.github.com>
Co-authored-by: mamoodi <mamoodiha@gmail.com>
Co-authored-by: Tim O'Farrell <tofarr@gmail.com>
Co-authored-by: Hiep Le <69354317+hieptl@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ryan H. Tran <descience.thh10@gmail.com>
Co-authored-by: Neeraj Panwar <49247372+npneeraj@users.noreply.github.com>
Co-authored-by: sp.wack <83104063+amanape@users.noreply.github.com>
Co-authored-by: Insop <1240382+insop@users.noreply.github.com>
Co-authored-by: test <test@test.com>
Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
Co-authored-by: Zhonghao Jiang <zhonghao.J@outlook.com>
Co-authored-by: Ray Myers <ray.myers@gmail.com>
2025-08-22 14:02:36 +00:00

67 lines
2.0 KiB
Python

import asyncio
from openhands.core.config import OpenHandsConfig
from openhands.events.stream import EventStream
from openhands.llm.llm_registry import LLMRegistry
from openhands.runtime import get_runtime_cls
from openhands.runtime.base import Runtime
from openhands.storage.files import FileStore
from openhands.utils.async_utils import call_sync_from_async
class ServerConversation:
sid: str
file_store: FileStore
event_stream: EventStream
runtime: Runtime
user_id: str | None
_attach_to_existing: bool = False
def __init__(
self,
sid: str,
file_store: FileStore,
config: OpenHandsConfig,
user_id: str | None,
event_stream: EventStream | None = None,
runtime: Runtime | None = None,
):
self.sid = sid
self.config = config
self.file_store = file_store
self.user_id = user_id
if event_stream is None:
event_stream = EventStream(sid, file_store, user_id)
self.event_stream = event_stream
if runtime:
self._attach_to_existing = True
else:
runtime_cls = get_runtime_cls(self.config.runtime)
runtime = runtime_cls(
llm_registry=LLMRegistry(self.config),
config=config,
event_stream=self.event_stream,
sid=self.sid,
attach_to_existing=True,
headless_mode=False,
)
self.runtime = runtime
@property
def security_analyzer(self):
"""Access security analyzer through runtime."""
return self.runtime.security_analyzer
async def connect(self) -> None:
if not self._attach_to_existing:
await self.runtime.connect()
async def disconnect(self) -> None:
if self._attach_to_existing:
return
if self.event_stream:
self.event_stream.close()
asyncio.create_task(call_sync_from_async(self.runtime.close))