-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_store.py
More file actions
65 lines (47 loc) · 2.04 KB
/
Copy pathsession_store.py
File metadata and controls
65 lines (47 loc) · 2.04 KB
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
from dataclasses import dataclass, field
@dataclass
class Session:
"""App-level session data keyed by thread_id (not LangGraph checkpoint state)."""
messages: list[dict[str, str]] = field(default_factory=list)
mentioned_cities: list[str] = field(default_factory=list)
class SessionStore:
def __init__(self) -> None:
self._sessions: dict[str, Session] = {}
def get_or_create(self, thread_id: str) -> Session:
if thread_id not in self._sessions:
self._sessions[thread_id] = Session()
return self._sessions[thread_id]
def get(self, thread_id: str) -> Session | None:
return self._sessions.get(thread_id)
def clear(self, thread_id: str) -> None:
self._sessions.pop(thread_id, None)
def build_llm_user_message(session: Session, user_message: str) -> str:
context = _build_session_context(session)
if context:
return f"{context}\n\n{user_message}"
return user_message
def _build_session_context(session: Session) -> str | None:
if not session.mentioned_cities:
return None
cities = ", ".join(session.mentioned_cities[-3:])
return f"Session context: User previously asked about weather in {cities}."
def update_session_from_turn(
session: Session,
user_message: str,
reply: str,
agent_messages: list,
) -> None:
session.messages.append({"role": "user", "content": user_message})
session.messages.append({"role": "assistant", "content": reply})
for msg in agent_messages:
tool_calls = getattr(msg, "tool_calls", None)
if not tool_calls:
continue
for tool_call in tool_calls:
name = tool_call.get("name") if isinstance(tool_call, dict) else tool_call["name"]
args = tool_call.get("args") if isinstance(tool_call, dict) else tool_call["args"]
if name != "get_weather":
continue
city = args.get("city", "").strip()
if city and city not in session.mentioned_cities:
session.mentioned_cities.append(city)