@@ -38,32 +38,24 @@ def __init__(
38
38
user_data : AuthUserInfo = None ,
39
39
ws : WebSocket = None ,
40
40
):
41
+ """Initialize the StrayCat object."""
42
+
43
+ # user data
41
44
self .__user_id = user_id
42
45
self .__user_data = user_data
43
46
44
- # get working memory from cache or create a new one
45
- log .warning (f"GET working memory for { user_id } " )
46
- self .working_memory = self .cache .get_value (f"{ user_id } _working_memory" ) or WorkingMemory ()
47
-
48
47
# attribute to store ws connection
49
48
self .__ws = ws
50
49
50
+ # main event loop (for ws messages)
51
51
self .__main_loop = main_loop
52
+
53
+ # get working memory from cache or create a new one
54
+ self .load_working_memory_from_cache ()
52
55
53
56
def __repr__ (self ):
54
57
return f"StrayCat(user_id={ self .user_id } )"
55
58
56
- #def __del__(self):
57
- # log.critical(f"StrayCat __del__ called for {self.user_id}")
58
- #self.__main_loop = None
59
- #self.__ws = None
60
- # when the garbage collector deletes the stray, we update working memory in cache
61
- # self.update_working_memory_cache()
62
- #self.__user_id = None
63
- #self.__user_data = None
64
- # del self
65
-
66
-
67
59
def __send_ws_json (self , data : Any ):
68
60
# Run the corutine in the main event loop in the main thread
69
61
# and wait for the result
@@ -101,9 +93,15 @@ def __build_why(self) -> MessageWhy:
101
93
102
94
return why
103
95
96
+ def load_working_memory_from_cache (self ):
97
+ """Load the working memory from the cache."""
98
+ log .warning (f"GET working memory for { self .user_id } " )
99
+ self .working_memory = \
100
+ self .cache .get_value (f"{ self .user_id } _working_memory" ) or WorkingMemory ()
101
+
104
102
def update_working_memory_cache (self ):
105
103
"""Update the working memory in the cache."""
106
- log .warning (f"SAVE working memory for { self .user_id } " )
104
+ log .critical (f"SAVE { self .user_id } " )
107
105
updated_cache_item = CacheItem (f"{ self .user_id } _working_memory" , self .working_memory , - 1 )
108
106
self .cache .insert (updated_cache_item )
109
107
@@ -392,7 +390,7 @@ def __call__(self, message_dict):
392
390
user_message = UserMessage .model_validate (message_dict )
393
391
log .info (user_message )
394
392
395
- ### setup working memory
393
+ ### setup working memory for this convo turn
396
394
# keeping track of model interactions
397
395
self .working_memory .model_interactions = []
398
396
# latest user message
@@ -488,8 +486,14 @@ def __call__(self, message_dict):
488
486
489
487
def run (self , user_message_json , return_message = False ):
490
488
try :
489
+
490
+ # load working memory from cache
491
+ self .load_working_memory_from_cache ()
492
+ # run main flow
491
493
cat_message = self .__call__ (user_message_json )
494
+ # save working memory to cache
492
495
self .update_working_memory_cache ()
496
+
493
497
if return_message :
494
498
# return the message for HTTP usage
495
499
return cat_message
0 commit comments