3737_SORT_ORDER_BY = {
3838 # heat_base is the persisted base heat; effective_heat is a function
3939 # call that we skip here for listing speed.
40- "heat" : "heat_base DESC, id DESC" ,
40+ "heat" : "heat_base DESC, id DESC" ,
4141 "recent" : "created_at DESC, id DESC" ,
4242 "oldest" : "created_at ASC, id ASC" ,
4343}
4444_SORT_KEY_COLUMN = {
45- "heat" : "heat_base" ,
45+ "heat" : "heat_base" ,
4646 "recent" : "created_at" ,
4747 "oldest" : "created_at" ,
4848}
4949_SORT_DIRECTION = {
50- "heat" : "<" , # next page has smaller heat or same heat with smaller id
50+ "heat" : "<" , # next page has smaller heat or same heat with smaller id
5151 "recent" : "<" ,
5252 "oldest" : ">" ,
5353}
@@ -102,31 +102,31 @@ def _row_to_node(row: Any) -> dict:
102102 if d .get ("importance" ) and d ["importance" ] >= 0.75 :
103103 emotion = "urgency"
104104 return {
105- "id" : "memory:" + str (d .get ("id" )),
106- "memory_id" : d .get ("id" ),
107- "type" : "memory" ,
108- "kind" : "memory" ,
109- "label" : d .get ("content" ) or "" ,
110- "content" : d .get ("content" ) or "" ,
111- "domain" : d .get ("domain" ) or "" ,
112- "domain_id" : "domain:" + (d .get ("domain" ) or "__global__" ),
113- "tags" : d .get ("tags" ) or [],
114- "heat" : float (d .get ("heat_base" ) or 0.0 ),
115- "importance" : float (d .get ("importance" ) or 0.0 ),
116- "stage" : d .get ("consolidation_stage" ) or "labile" ,
105+ "id" : "memory:" + str (d .get ("id" )),
106+ "memory_id" : d .get ("id" ),
107+ "type" : "memory" ,
108+ "kind" : "memory" ,
109+ "label" : d .get ("content" ) or "" ,
110+ "content" : d .get ("content" ) or "" ,
111+ "domain" : d .get ("domain" ) or "" ,
112+ "domain_id" : "domain:" + (d .get ("domain" ) or "__global__" ),
113+ "tags" : d .get ("tags" ) or [],
114+ "heat" : float (d .get ("heat_base" ) or 0.0 ),
115+ "importance" : float (d .get ("importance" ) or 0.0 ),
116+ "stage" : d .get ("consolidation_stage" ) or "labile" ,
117117 "consolidationStage" : d .get ("consolidation_stage" ) or "labile" ,
118118 "consolidation_stage" : d .get ("consolidation_stage" ) or "labile" ,
119- "createdAt" : d .get ("created_at" ),
120- "lastAccessed" : d .get ("last_accessed" ),
121- "isProtected" : bool (d .get ("is_protected" )),
122- "is_protected" : bool (d .get ("is_protected" )),
123- "isGlobal" : bool (d .get ("is_global" )),
124- "is_global" : bool (d .get ("is_global" )),
125- "emotion" : emotion ,
126- "emotional_valence" : float (val ) if val is not None else 0.0 ,
127- "store_type" : d .get ("store_type" ) or "episodic" ,
128- "access_count" : int (d .get ("access_count" ) or 0 ),
129- "useful_count" : int (d .get ("useful_count" ) or 0 ),
119+ "createdAt" : d .get ("created_at" ),
120+ "lastAccessed" : d .get ("last_accessed" ),
121+ "isProtected" : bool (d .get ("is_protected" )),
122+ "is_protected" : bool (d .get ("is_protected" )),
123+ "isGlobal" : bool (d .get ("is_global" )),
124+ "is_global" : bool (d .get ("is_global" )),
125+ "emotion" : emotion ,
126+ "emotional_valence" : float (val ) if val is not None else 0.0 ,
127+ "store_type" : d .get ("store_type" ) or "episodic" ,
128+ "access_count" : int (d .get ("access_count" ) or 0 ),
129+ "useful_count" : int (d .get ("useful_count" ) or 0 ),
130130 }
131131
132132
@@ -212,7 +212,9 @@ def serve(handler, store) -> None:
212212 if emotion not in ("urgent" , "positive" , "negative" , "neutral" , None ):
213213 emotion = None
214214 try :
215- min_heat = float (qs .get ("min_heat" , ["" ])[0 ]) if qs .get ("min_heat" , ["" ])[0 ] else None
215+ min_heat = (
216+ float (qs .get ("min_heat" , ["" ])[0 ]) if qs .get ("min_heat" , ["" ])[0 ] else None
217+ )
216218 except (TypeError , ValueError ):
217219 min_heat = None
218220 protected_only = qs .get ("protected" , ["0" ])[0 ] in ("1" , "true" , "yes" )
@@ -252,11 +254,15 @@ def serve(handler, store) -> None:
252254 cur = store ._execute (sql , tuple (params ))
253255 rows = cur .fetchall ()
254256 except Exception as exc :
255- _send_json (handler , 500 , {
256- "status" : "error" ,
257- "reason" : "query_failed" ,
258- "detail" : f"{ type (exc ).__name__ } : { exc } " ,
259- })
257+ _send_json (
258+ handler ,
259+ 500 ,
260+ {
261+ "status" : "error" ,
262+ "reason" : "query_failed" ,
263+ "detail" : f"{ type (exc ).__name__ } : { exc } " ,
264+ },
265+ )
260266 return
261267
262268 has_more = len (rows ) > limit
@@ -267,16 +273,22 @@ def serve(handler, store) -> None:
267273 last = dict (items [- 1 ]) if not isinstance (items [- 1 ], dict ) else items [- 1 ]
268274 sort_value : Any
269275 if sort == "heat" :
270- sort_value = float (last ["heat_base" ]) if last .get ("heat_base" ) is not None else 0.0
276+ sort_value = (
277+ float (last ["heat_base" ]) if last .get ("heat_base" ) is not None else 0.0
278+ )
271279 else :
272280 ts = last .get ("created_at" )
273281 sort_value = ts .isoformat () if hasattr (ts , "isoformat" ) else ts
274282 next_cursor = _encode_cursor ({"k" : sort_value , "id" : last ["id" ]})
275283
276284 nodes = [_row_to_node (r ) for r in items ]
277- _send_json (handler , 200 , {
278- "items" : nodes ,
279- "next_cursor" : next_cursor ,
280- "page_count" : len (nodes ),
281- "sort" : sort ,
282- })
285+ _send_json (
286+ handler ,
287+ 200 ,
288+ {
289+ "items" : nodes ,
290+ "next_cursor" : next_cursor ,
291+ "page_count" : len (nodes ),
292+ "sort" : sort ,
293+ },
294+ )
0 commit comments