chore: apply suggestion

Changed `add_background_task` to `add_behavior` and added extra docs.
This commit is contained in:
2025-11-22 10:28:52 +01:00
parent f8f833df64
commit 1f9926fe00
11 changed files with 24 additions and 18 deletions

View File

@@ -45,7 +45,7 @@ class RobotSpeechAgent(BaseAgent):
self.subsocket.connect(settings.zmq_settings.internal_sub_address)
self.subsocket.setsockopt(zmq.SUBSCRIBE, b"command")
await self.add_background_task(self._zmq_command_loop())
await self.add_behavior(self._zmq_command_loop())
self.logger.info("Finished setting up %s", self.name)

View File

@@ -31,7 +31,7 @@ class BDICoreAgent(BaseAgent):
await self._load_asl()
# Start the BDI cycle loop
await self.add_background_task(self._bdi_loop())
await self.add_behavior(self._bdi_loop())
self.logger.debug("Setup complete.")
async def _load_asl(self):

View File

@@ -37,7 +37,7 @@ class RICommunicationAgent(BaseAgent):
if await self._negotiate_connection():
self.connected = True
await self.add_background_task(self._listen_loop())
await self.add_behavior(self._listen_loop())
else:
self.logger.warning("Failed to negotiate connection during setup.")

View File

@@ -37,7 +37,7 @@ class TranscriptionAgent(BaseAgent):
self.speech_recognizer.load_model() # Warmup
# Start background loop
await self.add_background_task(self._transcribing_loop())
await self.add_behavior(self._transcribing_loop())
self.logger.info("Finished setting up %s", self.name)

View File

@@ -93,7 +93,7 @@ class VADAgent(BaseAgent):
# Warmup/reset
await self.reset_stream()
await self.add_background_task(self._streaming_loop())
await self.add_behavior(self._streaming_loop())
# Start agents dependent on the output audio fragments here
transcriber = TranscriptionAgent(audio_out_address)

View File

@@ -1,6 +1,7 @@
import asyncio
import logging
from abc import ABC, abstractmethod
from collections.abc import Coroutine
from dataclasses import dataclass
# Central directory to resolve agent names to instances
@@ -69,7 +70,7 @@ class BaseAgent(ABC):
await self.setup()
# Start processing inbox
await self.add_background_task(self._process_inbox())
await self.add_behavior(self._process_inbox())
async def stop(self):
"""Stops the agent."""
@@ -98,8 +99,13 @@ class BaseAgent(ABC):
"""Override this to handle incoming messages."""
raise NotImplementedError
async def add_background_task(self, coro):
"""Helper to add a behavior to the agent."""
async def add_behavior(self, coro: Coroutine):
"""
Helper to add a behavior to the agent. To add asynchronous behavior to an agent, define
an `async` function and add it to the task list by calling :func:`add_background_task`
with it. This should happen in the :func:`setup` method of the agent. For an example, see:
:func:`~control_backend.agents.bdi.BDICoreAgent`.
"""
task = asyncio.create_task(coro)
self._tasks.add(task)
task.add_done_callback(self._tasks.discard)