Skip to content

Commit fc4914d

Browse files
authored
Consolidate api and message files
2 parents e174c0f + 24151e2 commit fc4914d

File tree

8 files changed

+121
-595
lines changed

8 files changed

+121
-595
lines changed

agent_memory_server/api.py

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from mcp.server.fastmcp.prompts import base
44
from mcp.types import TextContent
55

6-
from agent_memory_server import long_term_memory, messages, working_memory
6+
from agent_memory_server import long_term_memory, working_memory
77
from agent_memory_server.auth import UserInfo, get_current_user
88
from agent_memory_server.config import settings
99
from agent_memory_server.dependencies import get_background_tasks
@@ -51,89 +51,6 @@ def _get_effective_window_size(
5151
return effective_window_size
5252

5353

54-
@router.get("/sessions/", response_model=SessionListResponse)
55-
async def list_sessions(
56-
options: GetSessionsQuery = Depends(),
57-
current_user: UserInfo = Depends(get_current_user),
58-
):
59-
"""
60-
Get a list of session IDs, with optional pagination.
61-
62-
Args:
63-
options: Query parameters (page, size, namespace)
64-
65-
Returns:
66-
List of session IDs
67-
"""
68-
redis = await get_redis_conn()
69-
70-
total, session_ids = await messages.list_sessions(
71-
redis=redis,
72-
limit=options.limit,
73-
offset=options.offset,
74-
namespace=options.namespace,
75-
)
76-
77-
return SessionListResponse(
78-
sessions=session_ids,
79-
total=total,
80-
)
81-
82-
83-
@router.get("/sessions/{session_id}/memory", response_model=WorkingMemoryResponse)
84-
async def get_session_memory(
85-
session_id: str,
86-
namespace: str | None = None,
87-
window_size: int = settings.window_size,
88-
model_name: ModelNameLiteral | None = None,
89-
context_window_max: int | None = None,
90-
current_user: UserInfo = Depends(get_current_user),
91-
):
92-
"""
93-
Get working memory for a session.
94-
95-
This includes stored conversation messages, context, and structured memory records.
96-
97-
Args:
98-
session_id: The session ID
99-
namespace: The namespace to use for the session
100-
window_size: The number of messages to include in the response
101-
model_name: The client's LLM model name (will determine context window size if provided)
102-
context_window_max: Direct specification of the context window max tokens (overrides model_name)
103-
104-
Returns:
105-
Working memory containing messages, context, and structured memory records
106-
"""
107-
redis = await get_redis_conn()
108-
effective_window_size = _get_effective_window_size(
109-
window_size=window_size,
110-
context_window_max=context_window_max,
111-
model_name=model_name,
112-
)
113-
114-
# Get unified working memory
115-
working_mem = await working_memory.get_working_memory(
116-
session_id=session_id,
117-
namespace=namespace,
118-
redis_client=redis,
119-
)
120-
121-
if not working_mem:
122-
# Return empty working memory if none exists
123-
working_mem = WorkingMemory(
124-
messages=[],
125-
memories=[],
126-
session_id=session_id,
127-
namespace=namespace,
128-
)
129-
130-
# Apply window size to messages if needed
131-
if len(working_mem.messages) > effective_window_size:
132-
working_mem.messages = working_mem.messages[-effective_window_size:]
133-
134-
return working_mem
135-
136-
13754
async def _summarize_working_memory(
13855
memory: WorkingMemory,
13956
window_size: int,
@@ -218,6 +135,89 @@ async def _summarize_working_memory(
218135
return updated_memory
219136

220137

138+
@router.get("/sessions/", response_model=SessionListResponse)
139+
async def list_sessions(
140+
options: GetSessionsQuery = Depends(),
141+
current_user: UserInfo = Depends(get_current_user),
142+
):
143+
"""
144+
Get a list of session IDs, with optional pagination.
145+
146+
Args:
147+
options: Query parameters (page, size, namespace)
148+
149+
Returns:
150+
List of session IDs
151+
"""
152+
redis = await get_redis_conn()
153+
154+
total, session_ids = await working_memory.list_sessions(
155+
redis=redis,
156+
limit=options.limit,
157+
offset=options.offset,
158+
namespace=options.namespace,
159+
)
160+
161+
return SessionListResponse(
162+
sessions=session_ids,
163+
total=total,
164+
)
165+
166+
167+
@router.get("/sessions/{session_id}/memory", response_model=WorkingMemoryResponse)
168+
async def get_session_memory(
169+
session_id: str,
170+
namespace: str | None = None,
171+
window_size: int = settings.window_size,
172+
model_name: ModelNameLiteral | None = None,
173+
context_window_max: int | None = None,
174+
current_user: UserInfo = Depends(get_current_user),
175+
):
176+
"""
177+
Get working memory for a session.
178+
179+
This includes stored conversation messages, context, and structured memory records.
180+
181+
Args:
182+
session_id: The session ID
183+
namespace: The namespace to use for the session
184+
window_size: The number of messages to include in the response
185+
model_name: The client's LLM model name (will determine context window size if provided)
186+
context_window_max: Direct specification of the context window max tokens (overrides model_name)
187+
188+
Returns:
189+
Working memory containing messages, context, and structured memory records
190+
"""
191+
redis = await get_redis_conn()
192+
effective_window_size = _get_effective_window_size(
193+
window_size=window_size,
194+
context_window_max=context_window_max,
195+
model_name=model_name,
196+
)
197+
198+
# Get unified working memory
199+
working_mem = await working_memory.get_working_memory(
200+
session_id=session_id,
201+
namespace=namespace,
202+
redis_client=redis,
203+
)
204+
205+
if not working_mem:
206+
# Return empty working memory if none exists
207+
working_mem = WorkingMemory(
208+
messages=[],
209+
memories=[],
210+
session_id=session_id,
211+
namespace=namespace,
212+
)
213+
214+
# Apply window size to messages if needed
215+
if len(working_mem.messages) > effective_window_size:
216+
working_mem.messages = working_mem.messages[-effective_window_size:]
217+
218+
return working_mem
219+
220+
221221
@router.put("/sessions/{session_id}/memory", response_model=WorkingMemoryResponse)
222222
async def put_session_memory(
223223
session_id: str,
@@ -249,7 +249,7 @@ async def put_session_memory(
249249
if not mem.id:
250250
raise HTTPException(
251251
status_code=400,
252-
detail="All memory records in working memory must have an id",
252+
detail="All memory records in working memory must have an ID",
253253
)
254254

255255
# Handle summarization if needed (before storing)

agent_memory_server/long_term_memory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,13 @@ async def search_memories(
993993
session_ids_to_search = [session_id.eq]
994994
else:
995995
# Get all sessions for broader search
996-
from agent_memory_server import messages
996+
from agent_memory_server import working_memory
997997

998998
namespace_value = None
999999
if namespace and hasattr(namespace, "eq"):
10001000
namespace_value = namespace.eq
10011001

1002-
_, session_ids_to_search = await messages.list_sessions(
1002+
_, session_ids_to_search = await working_memory.list_sessions(
10031003
redis=redis,
10041004
limit=1000, # Get a reasonable number of sessions
10051005
offset=0,

0 commit comments

Comments
 (0)