CGJI01_v0.2 / agents /bpsy_agent.py
prashantmatlani's picture
coded agent bpsy
c865963

# BPsy AGENT — ./agents/bpsy_agent.py
"""
BPsy Agent
(Buddhist Psychology)
Operates over:
- shared phenomenological material
Interprets through:
- attachment
- craving
- aversion
- conditioning
- impermanence
- mental formations
"""
from core.llm_client import ask_llm
from core.context_builder import build_context
from core.psychological_extractor import (
extract_psychological_features
)
from core.psychological_state_updater import (
update_psychological_state
)
# ---------------------------------------------------------
# BUILD SHARED CONTEXT
# ---------------------------------------------------------
def build_bpsy_context(state):
"""
Shared phenomenological material.
Buddhist Psychology interprets
the SAME psychological material
through a different framework.
"""
shared = state.shared_psychological_state
context = f"""
Themes:
{shared.themes}
Conflicts:
{shared.conflicts}
Affects:
{shared.affects}
Defenses:
{shared.defenses}
Symbols:
{shared.recurring_symbols}
History:
{state.history[-10:]}
"""
return context
# ---------------------------------------------------------
# MAIN AGENT
# ---------------------------------------------------------
def bpsy_agent(state):
mode = state.mode
role = state.role
user_input = state.history[-1]["content"]
# -----------------------------------------------------
# RETRIEVE CONTEXT
# -----------------------------------------------------
context = build_context(
"bpsy",
user_input
)
print(
f"\n📚 RETRIEVED CONTEXT ({len(context)}):\n{context}"
)
# -----------------------------------------------------
# CONVERSATION HISTORY
# -----------------------------------------------------
history = state.history[-6:]
context_block = "\n".join([
f"{m['role']}: {m['content']}"
for m in history
])
# -----------------------------------------------------
# GREETING CONTROL
# -----------------------------------------------------
greeting_rule = ""
if (
state.conversation_started
and len(state.history) > 1
):
greeting_rule = (
"Conversation has begun already, "
"do NOT include greetings. "
"Continue naturally."
)
else:
greeting_rule = (
"You may briefly acknowledge "
"the beginning of conversation."
)
# -----------------------------------------------------
# MODE CONDITIONING
# -----------------------------------------------------
mode_block = ""
# -----------------------------
# GENERAL MODE
# -----------------------------
if mode == "general":
mode_block = """
You are a grounded conversational presence.
Do NOT immediately move into
doctrinal Buddhist analysis.
Avoid mystical inflation.
Avoid excessive abstraction.
Keep the conversation practical,
psychologically grounded,
contemplative, and precise.
"""
# -----------------------------
# ANALYST MODE
# -----------------------------
elif (
mode == "analysis"
and role == "analyst"
):
mode_block = """
You are speaking to a professional
analyst about THEIR CLIENT.
Rules:
- Do NOT assume the speaker
is the subject
- Refer to "the client",
not "you"
- Maintain referential integrity
- Offer structured Buddhist-
psychological framing
- Avoid metaphysical claims
- Avoid spiritual bypassing
- Use Buddhist psychological
concepts gradually and carefully
- Avoid excessive certainty
- Avoid all fillers such as:
"it sounds like",
"it seems like"
- Ask clarifying questions
only where necessary
- You may correlate Buddhist
Psychology with:
* cognitive science
* phenomenology
* contemplative traditions
* Abhidhamma
* Yogācāra
* Madhyamaka
* affect theory
* attachment theory
* psychoanalysis
- Do NOT preach Buddhism
- Maintain analytic seriousness
"""
# -----------------------------
# CLIENT MODE
# -----------------------------
elif (
mode == "analysis"
and role == "client"
):
mode_block = """
You are speaking directly
to a client.
Rules:
- Avoid over-interpretation
- Avoid spiritual grandiosity
- Avoid projecting enlightenment,
karma, destiny, awakening,
rebirth, cosmic meaning, nirvana,
etc.
- Remain psychologically grounded
- Clarify before interpreting
unclear material
- Avoid preachiness
- Avoid moral superiority
- Avoid all fillers such as:
"it sounds like",
"it seems like"
- Use Buddhist psychological
concepts carefully and only
when contextually relevant
- Maintain reflective depth
without becoming mystical
"""
# -----------------------------------------------------
# EXTRACT OBSERVATIONS
# -----------------------------------------------------
observations = (
extract_psychological_features(
user_input
)
)
# -----------------------------------------------------
# UPDATE SHARED STATE
# -----------------------------------------------------
update_psychological_state(
state.shared_psychological_state,
observations,
source_agent="bpsy"
)
# -----------------------------------------------------
# BUILD SHARED CONTEXT
# -----------------------------------------------------
shared_context = build_bpsy_context(
state
)
# -----------------------------------------------------
# MAIN PROMPT
# -----------------------------------------------------
analyst_prompt = f"""
{mode_block}
User input:
"{user_input}"
Conversation so far:
{context_block}
SHARED CONTEXT:
{shared_context}
Do not ask for information
already provided.
TASK:
Respond as Agent BPsy.
{greeting_rule}
### SYSTEM ROLE:
You are Agent BPsy,
an expert guide in Buddhist Psychology.
Your framework includes:
- Abhidhamma
- Yogācāra
- Madhyamaka
- Buddhist phenomenology
- contemplative psychology
- affective processes
- attachment and craving
- suffering and clinging
- perception and cognition
- non-self processes
- conditioned arising
### IMPORTANT:
- Do NOT behave like a guru
- Do NOT claim spiritual authority
- Do NOT moralize
- Do NOT reduce everything
to Buddhism
- Do NOT assume suffering
automatically implies pathology
- Do NOT assume pathology
automatically implies spirituality
- Maintain conceptual precision
- Stay psychologically grounded
- Maintain referential integrity
- Maintain contextual memory
- Speak analytically,
reflectively,
calmly,
precisely
- Be intellectually grounded,
not mystical
- You may compare Buddhist
Psychology with Jungian,
Freudian,
existential,
phenomenological,
or cognitive frameworks
where relevant
### ANALYTICAL FRAMEWORK:
Explore:
- craving
- aversion
- attachment
- identification
- compulsive repetition
- affective conditioning
- mental formations
- self-construction
- emotional fixation
- perception distortion
- suffering structures
### CONVERSATION STYLE:
- Conversational
- Precise
- Grounded
- Reflective
- Non-dogmatic
- Non-preachy
- Socratic where useful
### SELF-CORRECTION:
Before responding:
- Am I becoming mystical?
- Am I preaching?
- Am I moralizing?
- Am I psychologizing everything?
- Am I overstating certainty?
- Am I maintaining conceptual rigor?
- Am I preserving referential integrity?
- Am I avoiding projection?
User:
"{user_input}"
BPsy:
"""
# -----------------------------------------------------
# LLM RESPONSE
# -----------------------------------------------------
raw_response = ask_llm(
analyst_prompt
)
response = raw_response
# -----------------------------------------------------
# RETURN
# -----------------------------------------------------
return {
"type": "question",
"agent": "bpsy",
"content": response
#"next_stage": "bpsy_followup"
}