import uuid
from maximem_synap import MaximemSynapSDK
sdk = MaximemSynapSDK(api_key=...)
await sdk.initialize()
# ---- Per-customer onboarding: load their playbook once ----
async def onboard_customer(customer_id: str, playbook_text: str):
await sdk.memories.create(
document=playbook_text,
document_type="document",
customer_id=customer_id, # CUSTOMER scope — visible to all users in this customer
# no user_id → not user-scoped
metadata={"source": "onboarding_playbook"},
)
# ---- Per-turn agent loop, called for every user message ----
async def handle_turn(customer_id: str, user_id: str, conversation_id: str, user_message: str) -> str:
# Retrieval automatically merges USER + CUSTOMER + CLIENT scopes
ctx = await sdk.conversation.context.fetch(
conversation_id=conversation_id,
search_query=[user_message],
max_results=10,
)
# Build the system prompt with the merged scoped facts
facts_block = "\n".join(f"- {f.content}" for f in ctx.facts)
system_prompt = (
"Known facts (merged across user, customer, and client scopes):\n"
+ (facts_block or "(none)") + "\n\n"
"Respond using the customer org context as authoritative."
)
reply = await call_your_llm(system_prompt, user_message)
# Ingest at USER scope so personal context stays personal
await sdk.memories.create(
document=f"User: {user_message}\nAssistant: {reply}",
document_type="ai-chat-conversation",
user_id=user_id,
customer_id=customer_id, # both → user-scope, but linked to customer
metadata={"conversation_id": conversation_id},
)
return reply