55"""
66
77import asyncio
8- import contextlib
98import re
109from collections .abc import AsyncIterator
1110from typing import TYPE_CHECKING , Any , Literal , TypedDict
@@ -396,11 +395,10 @@ async def set_working_memory_data(
396395 # Get existing memory if preserving
397396 existing_memory = None
398397 if preserve_existing :
399- with contextlib .suppress (Exception ):
400- existing_memory = await self .get_working_memory (
401- session_id = session_id ,
402- namespace = namespace ,
403- )
398+ existing_memory = await self .get_working_memory (
399+ session_id = session_id ,
400+ namespace = namespace ,
401+ )
404402
405403 # Create new working memory with the data
406404 working_memory = WorkingMemory (
@@ -454,12 +452,10 @@ async def add_memories_to_working_memory(
454452 ```
455453 """
456454 # Get existing memory
457- existing_memory = None
458- with contextlib .suppress (Exception ):
459- existing_memory = await self .get_working_memory (
460- session_id = session_id ,
461- namespace = namespace ,
462- )
455+ existing_memory = await self .get_working_memory (
456+ session_id = session_id ,
457+ namespace = namespace ,
458+ )
463459
464460 # Determine final memories list
465461 if replace or not existing_memory :
@@ -1987,6 +1983,7 @@ async def update_working_memory_data(
19871983 data_updates : dict [str , Any ],
19881984 namespace : str | None = None ,
19891985 merge_strategy : Literal ["replace" , "merge" , "deep_merge" ] = "merge" ,
1986+ user_id : str | None = None ,
19901987 ) -> WorkingMemoryResponse :
19911988 """
19921989 Update specific data fields in working memory without replacing everything.
@@ -1996,16 +1993,15 @@ async def update_working_memory_data(
19961993 data_updates: Dictionary of updates to apply
19971994 namespace: Optional namespace
19981995 merge_strategy: How to handle existing data
1996+ user_id: Optional user ID for the session
19991997
20001998 Returns:
20011999 WorkingMemoryResponse with updated memory
20022000 """
20032001 # Get existing memory
2004- existing_memory = None
2005- with contextlib .suppress (Exception ):
2006- existing_memory = await self .get_working_memory (
2007- session_id = session_id , namespace = namespace
2008- )
2002+ existing_memory = await self .get_working_memory (
2003+ session_id = session_id , namespace = namespace , user_id = user_id
2004+ )
20092005
20102006 # Determine final data based on merge strategy
20112007 if existing_memory and existing_memory .data :
@@ -2040,6 +2036,7 @@ async def append_messages_to_working_memory(
20402036 namespace : str | None = None ,
20412037 model_name : str | None = None ,
20422038 context_window_max : int | None = None ,
2039+ user_id : str | None = None ,
20432040 ) -> WorkingMemoryResponse :
20442041 """
20452042 Append new messages to existing working memory.
@@ -2057,11 +2054,9 @@ async def append_messages_to_working_memory(
20572054 WorkingMemoryResponse with updated memory (potentially summarized if token limit exceeded)
20582055 """
20592056 # Get existing memory
2060- existing_memory = None
2061- with contextlib .suppress (Exception ):
2062- existing_memory = await self .get_working_memory (
2063- session_id = session_id , namespace = namespace
2064- )
2057+ existing_memory = await self .get_working_memory (
2058+ session_id = session_id , namespace = namespace , user_id = user_id
2059+ )
20652060
20662061 # Validate new messages have required structure
20672062 for msg in messages :
@@ -2104,6 +2099,7 @@ async def memory_prompt(
21042099 model_name : str | None = None ,
21052100 context_window_max : int | None = None ,
21062101 long_term_search : dict [str , Any ] | None = None ,
2102+ user_id : str | None = None ,
21072103 ) -> dict [str , Any ]:
21082104 """
21092105 Hydrate a user query with memory context and return a prompt ready to send to an LLM.
@@ -2116,6 +2112,7 @@ async def memory_prompt(
21162112 model_name: Optional model name to determine context window size
21172113 context_window_max: Optional direct specification of context window tokens
21182114 long_term_search: Optional search parameters for long-term memory
2115+ user_id: Optional user ID for the session
21192116
21202117 Returns:
21212118 Dict with messages hydrated with relevant memory context
@@ -2160,6 +2157,8 @@ async def memory_prompt(
21602157 )
21612158 if effective_context_window_max is not None :
21622159 session_params ["context_window_max" ] = str (effective_context_window_max )
2160+ if user_id is not None :
2161+ session_params ["user_id" ] = user_id
21632162 payload ["session" ] = session_params
21642163
21652164 # Add long-term search parameters if provided
0 commit comments