CGJI01_v0.2 / core /conversation_manager.py
prashantmatlani's picture
user profile feature
f7df0f1
# CONVERSATION MANAGER - core/conversation_manager.py
# ConversationState separate what the system knows/has information about
class ConversationState:
def __init__(self):
self.stage = "jung"
self.history = []
self.initial_query = None
self.current_agent = "jung"
self.agent_histories = {}
self.conversation_started = False # Flag to track the start of the conversation
self.mode = "general" # default
self.role = "client" # client or analyst
# Memory element
self.user_profile = {
"themes": [],
"patterns": [],
"preferences": []
}
# -----------------------------
# ADD USER INPUT (SINGLE SOURCE)
# -----------------------------
def add_user_input(self, text):
# Prevent duplicate consecutive entries
if self.history and self.history[-1]["role"] == "client" and self.history[-1]["content"] == text:
return
self.history.append({"role": "client", "content": text})
agent = self.current_agent
if agent not in self.agent_histories:
self.agent_histories[agent] = []
self.agent_histories[agent].append({
"role": "client",
"text": text
})
# -----------------------------
# ADD AGENT OUTPUT
# -----------------------------
def add_system_output(self, text):
if self.history and self.history[-1]["role"] == "agent" and self.history[-1]["content"] == text:
return
self.history.append({"role": "agent", "content": text})
agent = self.current_agent
if agent not in self.agent_histories:
self.agent_histories[agent] = []
self.agent_histories[agent].append({
"role": "agent",
"text": text
})
# ---------------------------------------------------------------
# REHYDRAION - CONVERT DICT/JSON BACK TO ConversationState Object
# ---------------------------------------------------------------
def from_dict(data):
state = ConversationState()
# Restore history
state.history = data.get("history", [])
# Restore agent logs safely
state.agent_logs = data.get("agents", {}) or {}
# Restore initial query
state.initial_query = next(
(m["content"] for m in state.history if m.get("role") == "client"),
""
)
# Restore flags
state.conversation_started = True
state.current_agent = "jung"
return state
# -------------
# USER PROFILE
# -------------
def update_user_profile(state, user_input, jung_output):
text = (user_input + " " + jung_output).lower()
THEMES = {
"family": ["family", "wife", "husband", "parents"],
"work": ["job", "career", "office"],
"identity": ["purpose", "meaning", "self"],
"dreams": ["dream", "nightmare"]
}
for theme, keywords in THEMES.items():
if any(word in text for word in keywords):
if theme not in state.user_profile["themes"]:
state.user_profile["themes"].append(theme)