refactor: agents inherit logger from BaseAgent

Created a class `BaseAgent`, from which all agents inherit. They get
assigned a logger with a nice name (something like
`control_backend.agents.AgentName`).

The BDI core takes care of its own logger, as bdi is still a module.

ref: N25B-241
This commit is contained in:
2025-11-04 20:48:55 +01:00
parent d43cb9394a
commit a98018ddda
15 changed files with 174 additions and 159 deletions

View File

@@ -1,17 +1,14 @@
import logging
import numpy as np
import torch
import zmq
import zmq.asyncio as azmq
from spade.agent import Agent
from spade.behaviour import CyclicBehaviour
from .transcription.transcription_agent import TranscriptionAgent
from control_backend.agents import BaseAgent
from control_backend.core.config import settings
from control_backend.core.zmq_context import context as zmq_context
logger = logging.getLogger(__name__)
from .transcription.transcription_agent import TranscriptionAgent
class SocketPoller[T]:
@@ -60,7 +57,9 @@ class Streaming(CyclicBehaviour):
data = await self.audio_in_poller.poll()
if data is None:
if len(self.audio_buffer) > 0:
logger.debug("No audio data received. Discarding buffer until new data arrives.")
self.agent.logger.debug(
"No audio data received. Discarding buffer until new data arrives."
)
self.audio_buffer = np.array([], dtype=np.float32)
self.i_since_speech = 100
return
@@ -71,7 +70,7 @@ class Streaming(CyclicBehaviour):
if prob > 0.5:
if self.i_since_speech > 3:
logger.debug("Speech started.")
self.agent.logger.debug("Speech started.")
self.audio_buffer = np.append(self.audio_buffer, chunk)
self.i_since_speech = 0
return
@@ -84,7 +83,7 @@ class Streaming(CyclicBehaviour):
# Speech probably ended. Make sure we have a usable amount of data.
if len(self.audio_buffer) >= 3 * len(chunk):
logger.debug("Speech ended.")
self.agent.logger.debug("Speech ended.")
await self.audio_out_socket.send(self.audio_buffer[: -2 * len(chunk)].tobytes())
# At this point, we know that the speech has ended.
@@ -92,7 +91,7 @@ class Streaming(CyclicBehaviour):
self.audio_buffer = chunk
class VADAgent(Agent):
class VADAgent(BaseAgent):
"""
An agent which listens to an audio stream, does Voice Activity Detection (VAD), and sends
fragments with detected speech to other agents over ZeroMQ.
@@ -135,12 +134,12 @@ class VADAgent(Agent):
self.audio_out_socket = zmq_context.socket(zmq.PUB)
return self.audio_out_socket.bind_to_random_port("tcp://*", max_tries=100)
except zmq.ZMQBindError:
logger.error("Failed to bind an audio output socket after 100 tries.")
self.logger.error("Failed to bind an audio output socket after 100 tries.")
self.audio_out_socket = None
return None
async def setup(self):
logger.info("Setting up %s", self.jid)
self.logger.info("Setting up %s", self.jid)
self._connect_audio_in_socket()
@@ -157,4 +156,4 @@ class VADAgent(Agent):
transcriber = TranscriptionAgent(audio_out_address)
await transcriber.start()
logger.info("Finished setting up %s", self.jid)
self.logger.info("Finished setting up %s", self.jid)