ljsysfurry commited on
Commit
d3e97cf
·
verified ·
1 Parent(s): 101dd80

Upload agentframe/memory/store.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. agentframe/memory/store.py +53 -0
agentframe/memory/store.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """持久化: 状态保存/加载 (JSON 快照)"""
2
+ import json
3
+ import os
4
+ import numpy as np
5
+
6
+
7
+ def _serialize(obj):
8
+ """递归序列化 (处理 numpy 类型)"""
9
+ if isinstance(obj, np.ndarray):
10
+ return {"__ndarray__": True, "shape": list(obj.shape),
11
+ "dtype": str(obj.dtype), "data": obj.tobytes().hex()}
12
+ if isinstance(obj, np.generic):
13
+ return obj.item()
14
+ if isinstance(obj, dict):
15
+ return {str(k): _serialize(v) for k, v in obj.items()}
16
+ if isinstance(obj, (list, tuple)):
17
+ return [_serialize(v) for v in obj]
18
+ if hasattr(obj, "__dict__"):
19
+ return _serialize(obj.__dict__)
20
+ return obj
21
+
22
+
23
+ def _deserialize(obj):
24
+ """递归反序列化"""
25
+ if isinstance(obj, dict):
26
+ if obj.get("__ndarray__"):
27
+ arr = np.frombuffer(bytes.fromhex(obj["data"]), dtype=obj["dtype"])
28
+ return arr.reshape(obj["shape"])
29
+ return {k: _deserialize(v) for k, v in obj.items()}
30
+ if isinstance(obj, list):
31
+ return [_deserialize(v) for v in obj]
32
+ return obj
33
+
34
+
35
+ class StateStore:
36
+ """JSON 快照存储: 保存引擎状态到文件, 可恢复"""
37
+
38
+ def __init__(self, path: str):
39
+ self.path = path
40
+
41
+ def save(self, state: dict):
42
+ os.makedirs(os.path.dirname(self.path) or ".", exist_ok=True)
43
+ with open(self.path, "w") as f:
44
+ json.dump(_serialize(state), f, ensure_ascii=False)
45
+
46
+ def load(self) -> dict:
47
+ if not os.path.exists(self.path):
48
+ return {}
49
+ with open(self.path) as f:
50
+ return _deserialize(json.load(f))
51
+
52
+ def exists(self) -> bool:
53
+ return os.path.exists(self.path)