|
3 | 3 | from mcp.server.fastmcp.prompts import base |
4 | 4 | from mcp.types import TextContent |
5 | 5 |
|
6 | | -from agent_memory_server import long_term_memory, messages, working_memory |
| 6 | +from agent_memory_server import long_term_memory, working_memory |
7 | 7 | from agent_memory_server.auth import UserInfo, get_current_user |
8 | 8 | from agent_memory_server.config import settings |
9 | 9 | from agent_memory_server.dependencies import get_background_tasks |
@@ -51,89 +51,6 @@ def _get_effective_window_size( |
51 | 51 | return effective_window_size |
52 | 52 |
|
53 | 53 |
|
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 | | - |
137 | 54 | async def _summarize_working_memory( |
138 | 55 | memory: WorkingMemory, |
139 | 56 | window_size: int, |
@@ -218,6 +135,89 @@ async def _summarize_working_memory( |
218 | 135 | return updated_memory |
219 | 136 |
|
220 | 137 |
|
| 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 | + |
221 | 221 | @router.put("/sessions/{session_id}/memory", response_model=WorkingMemoryResponse) |
222 | 222 | async def put_session_memory( |
223 | 223 | session_id: str, |
@@ -249,7 +249,7 @@ async def put_session_memory( |
249 | 249 | if not mem.id: |
250 | 250 | raise HTTPException( |
251 | 251 | 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", |
253 | 253 | ) |
254 | 254 |
|
255 | 255 | # Handle summarization if needed (before storing) |
|
0 commit comments