Remove while True in AgentController (#5868)

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
Co-authored-by: amanape <83104063+amanape@users.noreply.github.com>
This commit is contained in:
Robert Brennan
2024-12-31 16:10:36 -05:00
committed by GitHub
parent a2e9e206e8
commit d29cc61aa2
10 changed files with 207 additions and 153 deletions

View File

@@ -84,39 +84,6 @@ class AgentSession:
'Session already started. You need to close this session and start a new one.'
)
asyncio.get_event_loop().run_in_executor(
None,
self._start_thread,
runtime_name,
config,
agent,
max_iterations,
max_budget_per_task,
agent_to_llm_config,
agent_configs,
github_token,
selected_repository,
)
def _start_thread(self, *args):
try:
asyncio.run(self._start(*args), debug=True)
except RuntimeError:
logger.error(f'Error starting session: {RuntimeError}', exc_info=True)
logger.debug('Session Finished')
async def _start(
self,
runtime_name: str,
config: AppConfig,
agent: Agent,
max_iterations: int,
max_budget_per_task: float | None = None,
agent_to_llm_config: dict[str, LLMConfig] | None = None,
agent_configs: dict[str, AgentConfig] | None = None,
github_token: str | None = None,
selected_repository: str | None = None,
):
if self._closed:
logger.warning('Session closed before starting')
return
@@ -141,9 +108,7 @@ class AgentSession:
self.event_stream.add_event(
ChangeAgentStateAction(AgentState.INIT), EventSource.ENVIRONMENT
)
self.controller.agent_task = self.controller.start_step_loop()
self._initializing = False
await self.controller.agent_task # type: ignore
def close(self):
"""Closes the Agent session"""

View File

@@ -351,12 +351,13 @@ class SessionManager:
sid=sid, file_store=self.file_store, config=self.config, sio=self.sio
)
self._local_agent_loops_by_sid[sid] = session
await session.initialize_agent(settings)
asyncio.create_task(session.initialize_agent(settings))
event_stream = await self._get_event_stream(sid)
if not event_stream:
logger.error(f'No event stream after starting agent loop: {sid}')
raise RuntimeError(f'no_event_stream:{sid}')
asyncio.create_task(self._cleanup_session_later(sid))
return event_stream
async def _get_event_stream(self, sid: str) -> EventStream | None:

View File

@@ -82,7 +82,6 @@ class Session:
settings.security_analyzer or self.config.security.security_analyzer
)
max_iterations = settings.max_iterations or self.config.max_iterations
# override default LLM config
default_llm_config = self.config.get_llm_config()
default_llm_config.model = settings.llm_model or ''
@@ -120,7 +119,10 @@ class Session:
)
return
async def on_event(self, event: Event):
def on_event(self, event: Event):
asyncio.get_event_loop().run_until_complete(self._on_event(event))
async def _on_event(self, event: Event):
"""Callback function for events that mainly come from the agent.
Event is the base class for any agent action and observation.