import re
import json
def process_agent_decision(session_id, raw_output):
# 1. Extract Memory
memory_match = re.search(r"<memory>(.*?)</memory>", raw_output, re.S)
if memory_match:
sb.memory.add(content=memory_match.group(1).strip(), session_id=session_id)
# 2. Extract State Update
state_match = re.search(r"<state_update>(.*?)</state_update>", raw_output, re.S)
if state_match:
try:
new_state = json.loads(state_match.group(1).strip())
sb.sessions.update_state(session_id=session_id, state=new_state)
except json.JSONDecodeError:
pass
# 3. Clean output for the user
final_output = re.sub(r"<(memory|state_update)>.*?</\1>", "", raw_output, flags=re.S).strip()
return final_output