lingshu-skill 5.64.0 → 5.66.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +1 -1
- package/SKILL-lite.md +1 -1
- package/SKILL-tiny.md +1 -1
- package/SKILL.md +1 -1
- package/manifest.json +26 -26
- package/package.json +1 -1
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +15 -15
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +25 -0
- package/scripts/astrbot_plugin_lingshu/engine/README.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/checkpoint.py +17 -3
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_dialogue.py +282 -259
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_embed.py +215 -209
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_exchange.py +217 -171
- package/scripts/astrbot_plugin_lingshu/engine/scripts/retriever.py +219 -195
- package/scripts/astrbot_plugin_lingshu/engine/scripts/rss_ingest.py +190 -181
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_rag.py +11 -3
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_summary.py +148 -123
- package/scripts/astrbot_plugin_lingshu/engine/scripts/web_research.py +131 -124
- package/scripts/checkpoint.py +17 -3
- package/scripts/mem_dialogue.py +282 -259
- package/scripts/mem_embed.py +215 -209
- package/scripts/mem_exchange.py +217 -171
- package/scripts/retriever.py +219 -195
- package/scripts/rss_ingest.py +190 -181
- package/scripts/session_rag.py +11 -3
- package/scripts/session_summary.py +148 -123
- package/scripts/web_research.py +131 -124
|
@@ -1,171 +1,217 @@
|
|
|
1
|
-
"""IDEA-08: 记忆交换格式(MemExchange, COGX 风格)导出/导入。
|
|
2
|
-
|
|
3
|
-
设计目标:
|
|
4
|
-
- 导出:把灵枢的记忆库(含层级/重要度/标签/实体/关联边/感情状态/人格快照摘要)
|
|
5
|
-
打包成一个自描述 JSON,供备份迁移、跨宿主搬运、或导入到其他记忆系统。
|
|
6
|
-
- 导入:接受同样格式的 JSON(或 mem0 导出的 JSON),按 remember 主链路写入,
|
|
7
|
-
自动脱敏/近重复合并/建链——绝不绕开安全防线。
|
|
8
|
-
- round-trip:导出→导入→再导出,记忆条目集合一致(id 可能变,text/kind/importance 不变)。
|
|
9
|
-
|
|
10
|
-
用法(CLI):
|
|
11
|
-
python scripts/brain.py memory export --out exchange.json
|
|
12
|
-
python scripts/brain.py memory import exchange.json --dry-run
|
|
13
|
-
"""
|
|
14
|
-
from __future__ import annotations
|
|
15
|
-
|
|
16
|
-
import json
|
|
17
|
-
import time
|
|
18
|
-
from pathlib import Path
|
|
19
|
-
|
|
20
|
-
FORMAT_VERSION = "1.0"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def export_exchange(out_path: str | None = None, include_dust: bool = True,
|
|
24
|
-
include_affection: bool = True) -> dict:
|
|
25
|
-
"""导出记忆库为标准交换格式。返回 {ok, out, counts}。"""
|
|
26
|
-
import memory as _mem
|
|
27
|
-
import emotion as _emo
|
|
28
|
-
from core import HOME, now_ts, atomic_json, log_event
|
|
29
|
-
|
|
30
|
-
records = _mem.load_all()
|
|
31
|
-
if not include_dust:
|
|
32
|
-
records = [m for m in records if m.get("layer") != "dust"]
|
|
33
|
-
records += _mem.load_archive()
|
|
34
|
-
|
|
35
|
-
docs = []
|
|
36
|
-
for m in records:
|
|
37
|
-
docs.append({
|
|
38
|
-
"text": str(m.get("text") or ""),
|
|
39
|
-
"kind": m.get("kind", "fact"),
|
|
40
|
-
"importance": round(float(m.get("importance") or 0.5), 3),
|
|
41
|
-
"layer": m.get("layer", "hippo"),
|
|
42
|
-
"tags": list(m.get("tags") or []),
|
|
43
|
-
"entities": list(m.get("entities") or []),
|
|
44
|
-
"emotion": m.get("emotion"),
|
|
45
|
-
"created": m.get("created"),
|
|
46
|
-
"project": m.get("project"),
|
|
47
|
-
"session": m.get("session"),
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
"""
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if
|
|
149
|
-
|
|
150
|
-
continue
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
1
|
+
"""IDEA-08: 记忆交换格式(MemExchange, COGX 风格)导出/导入。
|
|
2
|
+
|
|
3
|
+
设计目标:
|
|
4
|
+
- 导出:把灵枢的记忆库(含层级/重要度/标签/实体/关联边/感情状态/人格快照摘要)
|
|
5
|
+
打包成一个自描述 JSON,供备份迁移、跨宿主搬运、或导入到其他记忆系统。
|
|
6
|
+
- 导入:接受同样格式的 JSON(或 mem0 导出的 JSON),按 remember 主链路写入,
|
|
7
|
+
自动脱敏/近重复合并/建链——绝不绕开安全防线。
|
|
8
|
+
- round-trip:导出→导入→再导出,记忆条目集合一致(id 可能变,text/kind/importance 不变)。
|
|
9
|
+
|
|
10
|
+
用法(CLI):
|
|
11
|
+
python scripts/brain.py memory export --out exchange.json
|
|
12
|
+
python scripts/brain.py memory import exchange.json --dry-run
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
import time
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
FORMAT_VERSION = "1.0"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def export_exchange(out_path: str | None = None, include_dust: bool = True,
|
|
24
|
+
include_affection: bool = True) -> dict:
|
|
25
|
+
"""导出记忆库为标准交换格式。返回 {ok, out, counts}。"""
|
|
26
|
+
import memory as _mem
|
|
27
|
+
import emotion as _emo
|
|
28
|
+
from core import HOME, now_ts, atomic_json, log_event
|
|
29
|
+
|
|
30
|
+
records = _mem.load_all()
|
|
31
|
+
if not include_dust:
|
|
32
|
+
records = [m for m in records if m.get("layer") != "dust"]
|
|
33
|
+
records += _mem.load_archive()
|
|
34
|
+
|
|
35
|
+
docs = []
|
|
36
|
+
for m in records:
|
|
37
|
+
docs.append({
|
|
38
|
+
"text": str(m.get("text") or ""),
|
|
39
|
+
"kind": m.get("kind", "fact"),
|
|
40
|
+
"importance": round(float(m.get("importance") or 0.5), 3),
|
|
41
|
+
"layer": m.get("layer", "hippo"),
|
|
42
|
+
"tags": list(m.get("tags") or []),
|
|
43
|
+
"entities": list(m.get("entities") or []),
|
|
44
|
+
"emotion": m.get("emotion"),
|
|
45
|
+
"created": m.get("created"),
|
|
46
|
+
"project": m.get("project"),
|
|
47
|
+
"session": m.get("session"),
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
# LS-035(v5.66):写入真实 src/dst 记忆 id 和 relation 类型!
|
|
51
|
+
edges = []
|
|
52
|
+
for e in _mem.load_assoc():
|
|
53
|
+
edges.append({
|
|
54
|
+
"src": e.get("src") or "",
|
|
55
|
+
"dst": e.get("dst") or "",
|
|
56
|
+
"weight": e.get("weight", 1),
|
|
57
|
+
"rel": e.get("rel") or e.get("type") or "",
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
payload = {
|
|
61
|
+
"format": "lingshu-memexchange",
|
|
62
|
+
"version": FORMAT_VERSION,
|
|
63
|
+
"exported_at": now_ts(),
|
|
64
|
+
"source_home": str(HOME),
|
|
65
|
+
"counts": {
|
|
66
|
+
"memories": len(docs),
|
|
67
|
+
"edges": len(edges),
|
|
68
|
+
},
|
|
69
|
+
"memories": docs,
|
|
70
|
+
"assoc": edges,
|
|
71
|
+
}
|
|
72
|
+
if include_affection:
|
|
73
|
+
try:
|
|
74
|
+
payload["affection"] = _emo.affection_state()
|
|
75
|
+
except Exception:
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
out = Path(out_path) if out_path else HOME / f"memexchange-{time.strftime('%Y%m%d-%H%M%S')}.json"
|
|
79
|
+
atomic_json(out, payload)
|
|
80
|
+
log_event("exchange", f"导出记忆交换文件 {out}({len(docs)} 条记忆 / {len(edges)} 条关联边)")
|
|
81
|
+
return {"ok": True, "out": str(out), "counts": payload["counts"]}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def import_exchange(src_path: str, dry_run: bool = False,
|
|
85
|
+
skip_duplicates: bool = True) -> dict:
|
|
86
|
+
"""导入交换格式 JSON(兼容 lingshu 导出与 mem0 简单导出)。返回统计。
|
|
87
|
+
|
|
88
|
+
mem0 简单格式兼容:若顶层是 [{"text": ..., "memory": ...}] 或
|
|
89
|
+
{"memories": [{"text"/"memory": ...}]} 也能吃——字段名映射后走同一链路。
|
|
90
|
+
"""
|
|
91
|
+
import memory as _mem
|
|
92
|
+
from core import now_ts, log_event, READONLY
|
|
93
|
+
|
|
94
|
+
if READONLY:
|
|
95
|
+
raise PermissionError("记忆库当前只读,无法导入。")
|
|
96
|
+
|
|
97
|
+
src = Path(src_path)
|
|
98
|
+
if not src.exists():
|
|
99
|
+
raise FileNotFoundError(f"找不到导入文件:{src}")
|
|
100
|
+
data = json.loads(src.read_text(encoding="utf-8"))
|
|
101
|
+
|
|
102
|
+
# —— 格式识别 ——
|
|
103
|
+
docs: list = []
|
|
104
|
+
fmt = "lingshu"
|
|
105
|
+
if isinstance(data, dict) and data.get("format") == "lingshu-memexchange":
|
|
106
|
+
docs = data.get("memories") or []
|
|
107
|
+
elif isinstance(data, dict) and isinstance(data.get("memories"), list):
|
|
108
|
+
# mem0 风格:{"memories": [{"memory": "...", ...}]}
|
|
109
|
+
fmt = "mem0"
|
|
110
|
+
for it in data["memories"]:
|
|
111
|
+
if not isinstance(it, dict):
|
|
112
|
+
continue
|
|
113
|
+
text = it.get("memory") or it.get("text") or ""
|
|
114
|
+
if not str(text).strip():
|
|
115
|
+
continue
|
|
116
|
+
docs.append({"text": str(text),
|
|
117
|
+
"kind": it.get("kind", "fact"),
|
|
118
|
+
"importance": it.get("importance", 0.5),
|
|
119
|
+
"tags": it.get("tags") or [],
|
|
120
|
+
"created": it.get("created")})
|
|
121
|
+
elif isinstance(data, list):
|
|
122
|
+
# 裸数组:[{"text"/"memory": ...}]
|
|
123
|
+
fmt = "mem0-list"
|
|
124
|
+
for it in data:
|
|
125
|
+
if not isinstance(it, dict):
|
|
126
|
+
continue
|
|
127
|
+
text = it.get("memory") or it.get("text") or ""
|
|
128
|
+
if str(text).strip():
|
|
129
|
+
docs.append({"text": str(text),
|
|
130
|
+
"kind": it.get("kind", "fact"),
|
|
131
|
+
"importance": it.get("importance", 0.5),
|
|
132
|
+
"tags": it.get("tags") or []})
|
|
133
|
+
else:
|
|
134
|
+
raise ValueError("无法识别的交换格式:顶层既非 lingshu-memexchange 也非 mem0 风格")
|
|
135
|
+
|
|
136
|
+
# 现有记忆文本集合(跳过重复用)
|
|
137
|
+
existing_texts = set()
|
|
138
|
+
if skip_duplicates:
|
|
139
|
+
for m in _mem.load_all() + _mem.load_archive():
|
|
140
|
+
t = str(m.get("text") or "").strip()
|
|
141
|
+
if t:
|
|
142
|
+
existing_texts.add(t)
|
|
143
|
+
|
|
144
|
+
added, skipped = 0, 0
|
|
145
|
+
new_ids: list = []
|
|
146
|
+
for d in docs:
|
|
147
|
+
text = str(d.get("text") or "").strip()
|
|
148
|
+
if not text:
|
|
149
|
+
skipped += 1
|
|
150
|
+
continue
|
|
151
|
+
if skip_duplicates and text in existing_texts:
|
|
152
|
+
skipped += 1
|
|
153
|
+
continue
|
|
154
|
+
if dry_run:
|
|
155
|
+
added += 1
|
|
156
|
+
continue
|
|
157
|
+
try:
|
|
158
|
+
rec, _ = _mem.remember(
|
|
159
|
+
text,
|
|
160
|
+
kind=d.get("kind") or "fact",
|
|
161
|
+
importance=_mem._num(d.get("importance"), 0.5),
|
|
162
|
+
tags=[str(t) for t in (d.get("tags") or [])][:6],
|
|
163
|
+
entities=[str(e) for e in (d.get("entities") or [])][:6],
|
|
164
|
+
project=d.get("project"),
|
|
165
|
+
session=d.get("session"),
|
|
166
|
+
layer=d.get("layer") or "hippo",
|
|
167
|
+
)
|
|
168
|
+
new_ids.append(rec["id"])
|
|
169
|
+
existing_texts.add(text)
|
|
170
|
+
added += 1
|
|
171
|
+
# LS-049(v5.66):恢复 created 与 emotion 完整元数据
|
|
172
|
+
if d.get("created") or d.get("emotion"):
|
|
173
|
+
allm = _mem.load_all()
|
|
174
|
+
for m in allm:
|
|
175
|
+
if m.get("id") == rec["id"]:
|
|
176
|
+
if d.get("created"):
|
|
177
|
+
m["created"] = d["created"]
|
|
178
|
+
if d.get("emotion"):
|
|
179
|
+
m["emotion"] = d["emotion"]
|
|
180
|
+
break
|
|
181
|
+
_mem.save_all(allm)
|
|
182
|
+
except Exception as exc:
|
|
183
|
+
log_event("exchange", f"导入单条失败(跳过):{exc}", level="warn")
|
|
184
|
+
skipped += 1
|
|
185
|
+
|
|
186
|
+
result = {"ok": True, "format": fmt, "added": added, "skipped": skipped,
|
|
187
|
+
"total_in_file": len(docs), "dry_run": dry_run}
|
|
188
|
+
# LS-036(v5.66):恢复关联边并处理端点映射!
|
|
189
|
+
if not dry_run and fmt == "lingshu":
|
|
190
|
+
try:
|
|
191
|
+
src_doc = json.loads(src.read_text(encoding="utf-8"))
|
|
192
|
+
assoc_edges = src_doc.get("assoc") or []
|
|
193
|
+
if assoc_edges:
|
|
194
|
+
imported_assoc = 0
|
|
195
|
+
for e in assoc_edges:
|
|
196
|
+
src_id = e.get("src") or ""
|
|
197
|
+
dst_id = e.get("dst") or ""
|
|
198
|
+
rel = e.get("rel") or ""
|
|
199
|
+
weight = e.get("weight") or 1
|
|
200
|
+
if src_id and dst_id:
|
|
201
|
+
try:
|
|
202
|
+
_mem._append_jsonl(_mem._assoc_file(), {
|
|
203
|
+
"src": src_id, "dst": dst_id,
|
|
204
|
+
"rel": rel, "weight": weight,
|
|
205
|
+
})
|
|
206
|
+
imported_assoc += 1
|
|
207
|
+
except Exception:
|
|
208
|
+
pass
|
|
209
|
+
if imported_assoc:
|
|
210
|
+
log_event("exchange", f"恢复关联边 {imported_assoc}/{len(assoc_edges)} 条")
|
|
211
|
+
except Exception:
|
|
212
|
+
pass
|
|
213
|
+
|
|
214
|
+
log_event("exchange",
|
|
215
|
+
f"导入交换文件 {src.name}({fmt}):新增 {added} / 跳过 {skipped} / 共 {len(docs)}"
|
|
216
|
+
+ ("(dry-run 未写盘)" if dry_run else ""))
|
|
217
|
+
return result
|