#!/usr/bin/env python3
"""
Synapse Layer Cookbook — 03: Cross-Agent Memory
Agent A stores, Agent B recalls — seamlessly.
https://synapselayer.org/docs

This is the key differentiator vs Mem0, Zep, and LangMem:
no other memory layer supports cross-agent recall with
Trust Quotient scoring out of the box.

Prerequisites:
    pip install synapse-layer
    export SYNAPSE_API_KEY=sk_live_...
"""

from synapse_layer import SynapseClient
import os

client = SynapseClient(api_key=os.environ["SYNAPSE_API_KEY"])


def agent_a_onboarding():
    """Agent A: Onboarding Agent — collects and stores user profile."""
    print("=== Agent A: Onboarding Agent ===")

    client.store(
        content="User is a senior ML engineer at Acme Corp. "
                "Works on recommendation systems.",
        agent="onboarding-agent",
        memory_type="profile",
        tags=["role", "company", "domain"],
    )
    client.store(
        content="User prefers Python. Uses PyTorch for training "
                "and FastAPI for serving.",
        agent="onboarding-agent",
        memory_type="preference",
        tags=["language", "framework"],
    )
    client.store(
        content="User's current challenge: cold-start problem in "
                "recommendations for new users.",
        agent="onboarding-agent",
        memory_type="context",
        tags=["challenge", "active-project"],
    )
    print("Stored 3 memories as onboarding-agent.\n")


def agent_b_support():
    """Agent B: Support Agent — different session, different agent.
    Recalls memories from Agent A with Trust Quotient filtering."""
    print("=== Agent B: Support Agent ===")
    print("(Different session, different agent)\n")

    memories = client.recall(
        query="What is the user working on and what tools do they use?",
        limit=10,
        min_tq=0.6,
    )

    print(f"Found {len(memories)} memories from other agents:")
    for m in memories:
        print(f"  [{m.agent}] [TQ:{m.trust_quotient:.2f}] {m.content}")

    print("\n--- Support agent now has full context without asking ---")
    print("--- the user to repeat themselves.                    ---")


if __name__ == "__main__":
    agent_a_onboarding()
    agent_b_support()


"""
Expected output:
    === Agent A: Onboarding Agent ===
    Stored 3 memories as onboarding-agent.

    === Agent B: Support Agent ===
    (Different session, different agent)

    Found 3 memories from other agents:
      [onboarding-agent] [TQ:0.93] User is a senior ML engineer at Acme Corp. Works on recommendation systems.
      [onboarding-agent] [TQ:0.89] User prefers Python. Uses PyTorch for training and FastAPI for serving.
      [onboarding-agent] [TQ:0.87] User's current challenge: cold-start problem in recommendations for new users.

    --- Support agent now has full context without asking ---
    --- the user to repeat themselves.                    ---
"""
