fix: correct belief management

There was an issue in how we treated beliefs, specifically with multiple beliefs of the same name but different arguments. This is fixed with this commit.

Also implemented correct updating of the "responded" belief, when the user_said belief is updated (when we get a new user message, we state that we have not yet responded to that message)

ref: N25B-197
This commit is contained in:
2025-10-29 11:23:56 +01:00
parent dc811fd625
commit 3b7aeafe5e
3 changed files with 53 additions and 29 deletions

View File

@@ -1,9 +1,11 @@
import asyncio
import logging
import agentspeak
import spade_bdi.bdi
from spade_bdi.bdi import BDIAgent
from control_backend.agents.bdi.behaviours.belief_setter import BeliefSetter
from control_backend.agents.bdi.behaviours.belief_setter import BeliefSetterBehaviour
class BDICoreAgent(BDIAgent):
@@ -11,13 +13,12 @@ class BDICoreAgent(BDIAgent):
This is the Brain agent that does the belief inference with AgentSpeak.
This is a continous process that happens automatically in the background.
This class contains all the actions that can be called from AgentSpeak plans.
It has the BeliefSetter behaviour.
"""
logger = logging.getLogger("BDI Core")
async def setup(self):
belief_setter = BeliefSetter()
belief_setter = BeliefSetterBehaviour()
self.add_behaviour(belief_setter)
def add_custom_actions(self, actions):
@@ -33,3 +34,13 @@ class BDICoreAgent(BDIAgent):
def _send_to_llm(self, message) -> str:
"""TODO: implement"""
return f"This is a reply to {message}"
async def main():
bdi = BDICoreAgent("test_agent@localhost", "test_agent", "src/control_backend/agents/bdi/rules.asl")
await bdi.start()
bdi.bdi.set_belief("test", "one", "two")
print(bdi.bdi.get_beliefs())
if __name__ == "__main__":
asyncio.run(main())

View File

@@ -1,19 +1,17 @@
import asyncio
import json
import logging
from spade.agent import Message
from spade.behaviour import CyclicBehaviour
from spade_bdi.bdi import BDIAgent
from spade_bdi.bdi import BDIAgent, BeliefNotInitiated
from control_backend.core.config import settings
class BeliefSetter(CyclicBehaviour):
class BeliefSetterBehaviour(CyclicBehaviour):
"""
This is the behaviour that the BDI agent runs. This behaviour waits for incoming
message and processes it based on sender. Currently, it only waits for messages
containing beliefs from BeliefCollector and adds these to its KB.
message and processes it based on sender.
"""
agent: BDIAgent
@@ -24,7 +22,7 @@ class BeliefSetter(CyclicBehaviour):
if msg:
self.logger.info(f"Received message {msg.body}")
self._process_message(msg)
await asyncio.sleep(1)
def _process_message(self, message: Message):
sender = message.sender.node # removes host from jid and converts to str
@@ -44,19 +42,28 @@ class BeliefSetter(CyclicBehaviour):
match message.thread:
case "beliefs":
try:
beliefs: dict[str, list[list[str]]] = json.loads(message.body)
beliefs: dict[str, list[str]] = json.loads(message.body)
self._set_beliefs(beliefs)
except json.JSONDecodeError as e:
self.logger.error("Could not decode beliefs into JSON format: %s", e)
case _:
pass
def _set_beliefs(self, beliefs: dict[str, list[list[str]]]):
def _set_beliefs(self, beliefs: dict[str, list[str]]):
"""Remove previous values for beliefs and update them with the provided values."""
if self.agent.bdi is None:
self.logger.warning("Cannot set beliefs, since agent's BDI is not yet initialized.")
return
for belief, arguments_list in beliefs.items():
for arguments in arguments_list:
self.agent.bdi.set_belief(belief, *arguments)
self.logger.info("Set belief %s with arguments %s", belief, arguments)
# Set new beliefs (outdated beliefs are automatically removed)
for belief, arguments in beliefs.items():
self.agent.bdi.set_belief(belief, *arguments)
# Special case: if there's a new user message, we need to flag that we haven't responded yet
if belief == "user_said":
try:
self.agent.bdi.remove_belief("responded")
except BeliefNotInitiated:
pass
self.logger.info("Set belief %s with arguments %s", belief, arguments)