"""helper_agent.py, M9b: the same framework, a friendly NON-security agent.

Agents aren't just for security. This is a personal helper that can do math and
remember notes for you, same LangGraph setup as the SOC agent, different tools.
It shows the pattern is general, and demonstrates two kinds of memory:
  - tool memory  (save_note / list_notes keep a list)
  - conversation memory (MemorySaver remembers what you said earlier in the chat)

Setup:  pip install langgraph langchain-anthropic   (Python 3.10-3.12; see install guide)
Run (venv active, key in .env, from this folder):
    python helper_agent.py
"""

import os
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from langgraph.checkpoint.memory import MemorySaver

from tools import calculate as _calculate, save_note as _save_note, list_notes as _list_notes

load_dotenv()


@tool
def calculate(expression: str) -> str:
    """Do basic arithmetic, e.g. '12.5 * 8' or '(100 - 20) / 4'."""
    return _calculate(expression)


@tool
def save_note(note: str) -> str:
    """Save a short note so you can recall it later."""
    return _save_note(note)


@tool
def list_notes() -> str:
    """List all the notes saved so far."""
    return _list_notes()


model = ChatAnthropic(model="claude-opus-4-8", max_tokens=1024)
agent = create_react_agent(
    model,
    tools=[calculate, save_note, list_notes],
    prompt="You are a friendly personal assistant. Use your tools to do math and to "
           "remember and recall the user's notes. Keep replies short and warm.",
    checkpointer=MemorySaver(),
)


if __name__ == "__main__":
    config = {"configurable": {"thread_id": "me"}}
    print("Personal helper. Try: 'Remember I parked on level 3', then later 'where did I park?'")
    print("Or: 'what's 18% of 240?'   (type 'quit' to exit)\n")
    while True:
        q = input("You: ")
        if q.strip().lower() in {"quit", "exit"}:
            break
        result = agent.invoke({"messages": [{"role": "user", "content": q}]}, config)
        print("Helper:", result["messages"][-1].content, "\n")
