feat: send achieved goal from interrupt->manager->semantic
ref: N25B-400
This commit is contained in:
@@ -42,6 +42,16 @@ class BDIProgramManager(BaseAgent):
|
||||
def _initialize_internal_state(self, program: Program):
|
||||
self._program = program
|
||||
self._phase = program.phases[0] # start in first phase
|
||||
self._goal_mapping: dict[str, Goal] = {}
|
||||
for phase in program.phases:
|
||||
for goal in phase.goals:
|
||||
self._populate_goal_mapping_with_goal(goal)
|
||||
|
||||
def _populate_goal_mapping_with_goal(self, goal: Goal):
|
||||
self._goal_mapping[str(goal.id)] = goal
|
||||
for step in goal.plan.steps:
|
||||
if isinstance(step, Goal):
|
||||
self._populate_goal_mapping_with_goal(step)
|
||||
|
||||
async def _create_agentspeak_and_send_to_bdi(self, program: Program):
|
||||
"""
|
||||
@@ -73,6 +83,9 @@ class BDIProgramManager(BaseAgent):
|
||||
phases = json.loads(msg.body)
|
||||
|
||||
await self._transition_phase(phases["old"], phases["new"])
|
||||
case "achieve_goal":
|
||||
goal_id = msg.body
|
||||
self._send_achieved_goal_to_semantic_belief_extractor(goal_id)
|
||||
|
||||
async def _transition_phase(self, old: str, new: str):
|
||||
if old != str(self._phase.id):
|
||||
@@ -138,6 +151,19 @@ class BDIProgramManager(BaseAgent):
|
||||
|
||||
await self.send(message)
|
||||
|
||||
@staticmethod
|
||||
def _extract_goals_from_goal(goal: Goal) -> list[Goal]:
|
||||
"""
|
||||
Extract all goals from a given goal, that is: the goal itself and any subgoals.
|
||||
|
||||
:return: All goals within and including the given goal.
|
||||
"""
|
||||
goals: list[Goal] = [goal]
|
||||
for plan in goal.plan:
|
||||
if isinstance(plan, Goal):
|
||||
goals.extend(BDIProgramManager._extract_goals_from_goal(plan))
|
||||
return goals
|
||||
|
||||
def _extract_current_goals(self) -> list[Goal]:
|
||||
"""
|
||||
Extract all goals from the program, including subgoals.
|
||||
@@ -146,15 +172,8 @@ class BDIProgramManager(BaseAgent):
|
||||
"""
|
||||
goals: list[Goal] = []
|
||||
|
||||
def extract_goals_from_goal(goal_: Goal) -> list[Goal]:
|
||||
goals_: list[Goal] = [goal]
|
||||
for plan in goal_.plan:
|
||||
if isinstance(plan, Goal):
|
||||
goals_.extend(extract_goals_from_goal(plan))
|
||||
return goals_
|
||||
|
||||
for goal in self._phase.goals:
|
||||
goals.extend(extract_goals_from_goal(goal))
|
||||
goals.extend(self._extract_goals_from_goal(goal))
|
||||
|
||||
return goals
|
||||
|
||||
@@ -173,6 +192,25 @@ class BDIProgramManager(BaseAgent):
|
||||
|
||||
await self.send(message)
|
||||
|
||||
async def _send_achieved_goal_to_semantic_belief_extractor(self, achieved_goal_id: str):
|
||||
"""
|
||||
Inform the semantic belief extractor when a goal is marked achieved.
|
||||
|
||||
:param achieved_goal_id: The id of the achieved goal.
|
||||
"""
|
||||
goal = self._goal_mapping.get(achieved_goal_id)
|
||||
if goal is None:
|
||||
self.logger.debug(f"Goal with ID {achieved_goal_id} marked achieved but was not found.")
|
||||
return
|
||||
|
||||
goals = self._extract_goals_from_goal(goal)
|
||||
message = InternalMessage(
|
||||
to=settings.agent_settings.text_belief_extractor_name,
|
||||
body=GoalList(goals=goals).model_dump_json(),
|
||||
thread="achieved_goals",
|
||||
)
|
||||
await self.send(message)
|
||||
|
||||
async def _send_clear_llm_history(self):
|
||||
"""
|
||||
Clear the LLM Agent's conversation history.
|
||||
|
||||
@@ -124,6 +124,14 @@ class UserInterruptAgent(BaseAgent):
|
||||
"Forwarded button press (override) with context '%s' to BDI Core.",
|
||||
event_context,
|
||||
)
|
||||
|
||||
goal_achieve_msg = InternalMessage(
|
||||
to=settings.agent_settings.bdi_program_manager_name,
|
||||
thread="achieve_goal",
|
||||
body=ui_id,
|
||||
)
|
||||
|
||||
await self.send(goal_achieve_msg)
|
||||
else:
|
||||
self.logger.warning("Could not determine which element to override.")
|
||||
elif event_type == "pause":
|
||||
|
||||
Reference in New Issue
Block a user