File size: 7,435 Bytes
45f00f2
 
 
 
fbf7ee2
45f00f2
 
 
 
 
 
 
 
 
 
 
 
 
fbf7ee2
45f00f2
 
 
 
fbf7ee2
45f00f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8c0485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45f00f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8c0485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45f00f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import json
from typing import TypedDict, Annotated

from dotenv import load_dotenv
from langchain_core.messages import BaseMessage, HumanMessage,ToolMessage,AIMessage,SystemMessage
from langchain_groq import ChatGroq

from langgraph.graph import START, END, StateGraph
from langgraph.graph.message import add_messages

# Short-term memory
from langgraph.checkpoint.memory import InMemorySaver

# Long-term memory
from langgraph.store.memory import InMemoryStore

# Agent
from langchain.agents import create_agent
from tool import tool_tavily,time_date,calculator,python_exec,get_weather,wikipedia_search,scrape_website,read_file,format_json,generate_sql,system_info,save_user_preference,list_folders


load_dotenv()

tools = [tool_tavily,time_date,calculator,python_exec,get_weather,wikipedia_search,scrape_website,read_file,format_json,generate_sql,system_info,save_user_preference,list_folders]

# -----------------------------
# Memory Systems
# -----------------------------

checkpointer = InMemorySaver()
store = InMemoryStore()

# -----------------------------
# LLM
# -----------------------------

llm = ChatGroq(
    model="qwen/qwen3-32b",
    temperature=0,
    reasoning_format="parsed",
)

# -----------------------------
# State
# -----------------------------

def get_last_ai_message(messages):
    """Get the final AI response safely"""
    for msg in reversed(messages):
        if isinstance(msg, AIMessage) and msg.content:
            return msg.content if isinstance(msg.content, str) else str(msg.content)
    return "No response"


def extract_tool_logs(messages, current_user_message):
    """
    Extract tool calls and tool outputs from the latest turn only
    """
    latest_turn = []
    found_user_message = False

    # Walk backwards until we find the current user message
    for msg in reversed(messages):
        latest_turn.append(msg)
        if isinstance(msg, HumanMessage) and msg.content == current_user_message:
            found_user_message = True
            break

    if found_user_message:
        latest_turn = list(reversed(latest_turn))
    else:
        latest_turn = messages

    logs = []

    for msg in latest_turn:
        # AI asked to call tool
        if isinstance(msg, AIMessage) and getattr(msg, "tool_calls", None):
            for call in msg.tool_calls:
                tool_name = call.get("name", "unknown_tool")
                tool_args = call.get("args", {})

                logs.append(
                    f"### 🔧 Calling `{tool_name}`\n"
                    f"```json\n{json.dumps(tool_args, indent=2)}\n```"
                )

        # Tool returned output
        elif isinstance(msg, ToolMessage):
            tool_name = getattr(msg, "name", "unknown_tool")
            tool_output = msg.content if isinstance(msg.content, str) else str(msg.content)

            logs.append(
                f"### ✅ Output from `{tool_name}`\n"
                f"```\n{tool_output[:1000]}\n```"
            )

    return "\n\n".join(logs) if logs else "_No tools used in this response._"

class ChatState(TypedDict):
    messages: Annotated[list[BaseMessage], add_messages]


# -----------------------------
# Memory Analyzer Node
# -----------------------------

def memory_analyzer(state: ChatState, config, store):

    user_id = config["configurable"]["user_id"]

    last_message = state["messages"][-1].content

    prompt = f"""
Analyze the message and decide if it contains useful long-term memory.

Possible memory categories:
- preferences
- profile
- interests

Return JSON format:

{{
 "store_memory": true/false,
 "category": "preferences | profile | interests | project",
 "key": "memory_key",
 "value": {{}}
}}

Message:
{last_message}
"""

    result = llm.invoke([HumanMessage(content=prompt)])

    try:
        data = json.loads(result.content)

        if data.get("store_memory"):

            namespace = (user_id, data["category"])

            store.put(
                namespace,
                data["key"],
                data["value"]
            )

            print("Stored memory:", data)

    except Exception as e:
        pass

    return {}


# -----------------------------
# Memory Retrieval Node
# -----------------------------

def memory_retrieval(state: ChatState, config, store):

    user_id = config["configurable"]["user_id"]
    query = state["messages"][-1].content

    categories = [
        "preferences",
        "profile",
        "interests",
        "project"
    ]

    all_memories = []

    for category in categories:

        namespace = (user_id, category)

        results = store.search(
            namespace,
            query=query,
            limit=3
        )

        all_memories.extend(results)

    memory_text = "\n".join(
        [f"{m.namespace[1]}: {m.value}" for m in all_memories]
    )

    if memory_text:

        state["messages"].append(
            HumanMessage(
                content=f"Relevant user memories:\n{memory_text}"
            )
        )

    return {"messages": state["messages"]}


# def memory_retrieval(state: ChatState, config, store):

#     user_id = config["configurable"]["user_id"]
#     query = state["messages"][-1].content

#     categories = [
#         "preferences",
#         "profile",
#         "interests",
#         "project"
#     ]

#     all_memories = []

#     for category in categories:
#         namespace = (user_id, category)
#         results = store.search(namespace, query=query, limit=3)
#         all_memories.extend(results)

#     memory_text = "\n".join([f"{m.namespace[1]}: {m.value}" for m in all_memories])

#     # FIX: Only return the new message(s) added in this step, not the whole state.
#     if memory_text:
#         return {
#             "messages": [
#                 HumanMessage(content=f"Relevant user memories:\n{memory_text}")
#             ]
#         }
    
#     return {}

# -----------------------------
# Chat Node
# -----------------------------


def chat_node(state: ChatState):
    agent = create_agent(llm, tools=tools)
    messages = state["messages"]
    
    response = agent.invoke({"messages": messages})
      
    # Return only messages added by the agent (exclude the ones we sent in)
    new_messages = response["messages"][len(messages):]
    return {"messages": new_messages}

# -----------------------------
# Graph
# -----------------------------

graph = StateGraph(ChatState)

graph.add_node("memory_analyzer", memory_analyzer)
graph.add_node("memory_retrieval", memory_retrieval)
graph.add_node("chat_node", chat_node)

graph.add_edge(START, "memory_analyzer")
graph.add_edge("memory_analyzer", "memory_retrieval")
graph.add_edge("memory_retrieval", "chat_node")
graph.add_edge("chat_node", END)

chatbot = graph.compile(
    checkpointer=checkpointer,
    store=store
)

# -----------------------------
# Chat Loop
# -----------------------------

# config = {
#     "configurable": {
#         "thread_id": "thread_1",
#         "user_id": "user_123"
#     }
# }

# messages = []

# while True:

#     user_input = input("\nUser: ")

#     if user_input.lower() in ["quit", "exit"]:
#         break

#     messages.append(HumanMessage(content=user_input))

#     result = chatbot.invoke(
#         {"messages": messages},
#         config
#     )

#     response = result["messages"][-1].content

#     print("\nAssistant:", response)

#     messages = result["messages"]