lingshu-skill 0.0.0-stage → 3.79.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.
Files changed (135) hide show
  1. package/CHANGELOG.md +2571 -0
  2. package/LICENSE +21 -0
  3. package/README.md +698 -3
  4. package/SKILL-lite.md +45 -0
  5. package/SKILL-tiny.md +37 -0
  6. package/SKILL.md +379 -0
  7. package/assets/panel.html +5616 -0
  8. package/cordis-plugin/lingshu-cron/cordis.patch.yml +3 -0
  9. package/cordis-plugin/lingshu-cron/index.js +4 -0
  10. package/cordis-plugin/lingshu-cron/package.json +34 -0
  11. package/cordis-plugin/lingshu-cron/plugin.js +1040 -0
  12. package/cordis-plugin/lingshu-guard/plugin.js +473 -0
  13. package/cordis-plugin/lingshu-im/plugin.js +635 -0
  14. package/cordis-plugin/lingshu-startup/README.md +123 -0
  15. package/cordis-plugin/lingshu-startup/cordis.patch.yml +3 -0
  16. package/cordis-plugin/lingshu-startup/index.js +4 -0
  17. package/cordis-plugin/lingshu-startup/package.json +25 -0
  18. package/cordis-plugin/lingshu-startup/plugin.js +352 -0
  19. package/cordis.patch.yml +3 -0
  20. package/docs/commands.md +243 -0
  21. package/docs/mechanics.md +610 -0
  22. package/docs/models.md +52 -0
  23. package/docs/npm-/345/217/221/345/270/203/350/257/264/346/230/216.md +49 -0
  24. package/docs/panel-api.md +198 -0
  25. package/docs/recovery.md +90 -0
  26. package/docs/tgz-/345/256/211/350/243/205/350/257/264/346/230/216.md +53 -0
  27. package/docs//350/277/233/345/261/225/344/270/216/345/276/205/345/212/236.md +802 -0
  28. package/index.js +102 -0
  29. package/package.json +52 -6
  30. package/scripts/_panel_smoke_v299.py +27 -0
  31. package/scripts/_smoke_v299.py +31 -0
  32. package/scripts/_sync_trae_cn.py +34 -0
  33. package/scripts/assembler.py +197 -0
  34. package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +53 -0
  35. package/scripts/astrbot_plugin_lingshu/PATCH_NOTES.md +28 -0
  36. package/scripts/astrbot_plugin_lingshu/README.md +58 -0
  37. package/scripts/astrbot_plugin_lingshu/_conf_schema.json +92 -0
  38. package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +2571 -0
  39. package/scripts/astrbot_plugin_lingshu/engine/README.md +698 -0
  40. package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +45 -0
  41. package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +37 -0
  42. package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +379 -0
  43. package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +5616 -0
  44. package/scripts/astrbot_plugin_lingshu/engine/docs/commands.md +243 -0
  45. package/scripts/astrbot_plugin_lingshu/engine/docs/mechanics.md +610 -0
  46. package/scripts/astrbot_plugin_lingshu/engine/docs/models.md +52 -0
  47. package/scripts/astrbot_plugin_lingshu/engine/docs/panel-api.md +198 -0
  48. package/scripts/astrbot_plugin_lingshu/engine/docs/recovery.md +90 -0
  49. package/scripts/astrbot_plugin_lingshu/engine/scripts/_panel_smoke_v299.py +27 -0
  50. package/scripts/astrbot_plugin_lingshu/engine/scripts/_smoke_v299.py +31 -0
  51. package/scripts/astrbot_plugin_lingshu/engine/scripts/_sync_trae_cn.py +34 -0
  52. package/scripts/astrbot_plugin_lingshu/engine/scripts/assembler.py +197 -0
  53. package/scripts/astrbot_plugin_lingshu/engine/scripts/behavior_baseline.py +280 -0
  54. package/scripts/astrbot_plugin_lingshu/engine/scripts/bench.py +192 -0
  55. package/scripts/astrbot_plugin_lingshu/engine/scripts/brain.py +46 -0
  56. package/scripts/astrbot_plugin_lingshu/engine/scripts/check_pressure_sync.py +55 -0
  57. package/scripts/astrbot_plugin_lingshu/engine/scripts/check_tag.py +215 -0
  58. package/scripts/astrbot_plugin_lingshu/engine/scripts/checkpoint.py +165 -0
  59. package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +4850 -0
  60. package/scripts/astrbot_plugin_lingshu/engine/scripts/context_ab.py +260 -0
  61. package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +2025 -0
  62. package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +1137 -0
  63. package/scripts/astrbot_plugin_lingshu/engine/scripts/emotion.py +1180 -0
  64. package/scripts/astrbot_plugin_lingshu/engine/scripts/failures.py +107 -0
  65. package/scripts/astrbot_plugin_lingshu/engine/scripts/fallback_embed.py +114 -0
  66. package/scripts/astrbot_plugin_lingshu/engine/scripts/growth.py +4745 -0
  67. package/scripts/astrbot_plugin_lingshu/engine/scripts/growth_parts.py +263 -0
  68. package/scripts/astrbot_plugin_lingshu/engine/scripts/insights.py +131 -0
  69. package/scripts/astrbot_plugin_lingshu/engine/scripts/make_dsh_tgz.py +92 -0
  70. package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_dialogue.py +242 -0
  71. package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_embed.py +159 -0
  72. package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_lesson.py +160 -0
  73. package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_score.py +210 -0
  74. package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_topic.py +177 -0
  75. package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +4352 -0
  76. package/scripts/astrbot_plugin_lingshu/engine/scripts/npm_prepack.py +31 -0
  77. package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_build.py +115 -0
  78. package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_cloud.py +609 -0
  79. package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +2703 -0
  80. package/scripts/astrbot_plugin_lingshu/engine/scripts/panels.py +591 -0
  81. package/scripts/astrbot_plugin_lingshu/engine/scripts/persona.py +2309 -0
  82. package/scripts/astrbot_plugin_lingshu/engine/scripts/retriever.py +195 -0
  83. package/scripts/astrbot_plugin_lingshu/engine/scripts/safety.py +368 -0
  84. package/scripts/astrbot_plugin_lingshu/engine/scripts/session_import.py +736 -0
  85. package/scripts/astrbot_plugin_lingshu/engine/scripts/session_rag.py +228 -0
  86. package/scripts/astrbot_plugin_lingshu/engine/scripts/session_summary.py +123 -0
  87. package/scripts/astrbot_plugin_lingshu/engine/scripts/summarizer.py +252 -0
  88. package/scripts/astrbot_plugin_lingshu/main.py +703 -0
  89. package/scripts/astrbot_plugin_lingshu/make_checksums.py +87 -0
  90. package/scripts/astrbot_plugin_lingshu/metadata.yaml +7 -0
  91. package/scripts/behavior_baseline.py +280 -0
  92. package/scripts/bench.py +192 -0
  93. package/scripts/brain.py +46 -0
  94. package/scripts/check_pressure_sync.py +55 -0
  95. package/scripts/check_tag.py +215 -0
  96. package/scripts/check_version.py +265 -0
  97. package/scripts/checkpoint.py +165 -0
  98. package/scripts/cli.py +4850 -0
  99. package/scripts/context_ab.py +260 -0
  100. package/scripts/core.py +2025 -0
  101. package/scripts/dream.py +1137 -0
  102. package/scripts/emotion.py +1180 -0
  103. package/scripts/failures.py +107 -0
  104. package/scripts/fallback_embed.py +114 -0
  105. package/scripts/growth.py +4745 -0
  106. package/scripts/growth_parts.py +263 -0
  107. package/scripts/hooks/pre-commit +18 -0
  108. package/scripts/hooks/pre-push +20 -0
  109. package/scripts/insights.py +131 -0
  110. package/scripts/make_dsh_tgz.py +92 -0
  111. package/scripts/make_manifest.py +254 -0
  112. package/scripts/make_plugin_zip.py +88 -0
  113. package/scripts/mem_dialogue.py +242 -0
  114. package/scripts/mem_embed.py +159 -0
  115. package/scripts/mem_lesson.py +160 -0
  116. package/scripts/mem_score.py +210 -0
  117. package/scripts/mem_topic.py +177 -0
  118. package/scripts/memory.py +4352 -0
  119. package/scripts/npm_prepack.py +31 -0
  120. package/scripts/panel_build.py +115 -0
  121. package/scripts/panel_cloud.py +609 -0
  122. package/scripts/panel_server.py +2703 -0
  123. package/scripts/panels.py +591 -0
  124. package/scripts/persona.py +2309 -0
  125. package/scripts/recreate_push.ps1 +117 -0
  126. package/scripts/retriever.py +195 -0
  127. package/scripts/run_tests.py +91 -0
  128. package/scripts/safety.py +368 -0
  129. package/scripts/session_import.py +736 -0
  130. package/scripts/session_rag.py +228 -0
  131. package/scripts/session_summary.py +123 -0
  132. package/scripts/summarizer.py +252 -0
  133. package/scripts/sync_engine.py +139 -0
  134. package/scripts/update_dsh_copy.ps1 +107 -0
  135. package/start-panel.bat +60 -0
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """失败可见化:把被吞掉的异常记成可数的账。
4
+
5
+ 灵枢大量使用 fail-open(`except: pass`),这是设计——但"吞了多少、都吞了什么"
6
+ 不可见,就没法判断哪条路径在悄悄坏掉。
7
+
8
+ 本模块只做三件事:
9
+ record(kind, msg, level) 追加一行 JSON(超 512KB 自动轮转 .1)
10
+ summarize(days, top) 按 (kind, 归一化消息) 聚合:次数 / 最近时间 / 样本
11
+ recent(n) 最近 n 条原始记录
12
+
13
+ 约定:只在"本来就要吞掉"的分支里调用,不改变控制流;本身也绝不抛异常。
14
+ """
15
+ from __future__ import annotations
16
+
17
+ import hashlib
18
+ import json
19
+ import re
20
+ import time
21
+ from pathlib import Path
22
+
23
+ MAX_BYTES = 512 * 1024
24
+
25
+
26
+ def _home() -> Path:
27
+ try:
28
+ import core
29
+ return Path(core.HOME)
30
+ except Exception:
31
+ return Path.home() / ".brain-memory"
32
+
33
+
34
+ def path(home=None) -> Path:
35
+ return Path(home or _home()) / "failures.jsonl"
36
+
37
+
38
+ def record(kind: str, msg: str, *, level: str = "warn", ctx: dict | None = None,
39
+ home=None) -> None:
40
+ """记一次被吞掉的失败;任何 IO 错误都静默(日志本身不能成为故障源)。"""
41
+ try:
42
+ p = path(home)
43
+ p.parent.mkdir(parents=True, exist_ok=True)
44
+ try:
45
+ if p.exists() and p.stat().st_size > MAX_BYTES:
46
+ p.replace(p.with_suffix(".jsonl.1"))
47
+ except OSError:
48
+ pass
49
+ row = {"ts": int(time.time()), "kind": str(kind or "other")[:40],
50
+ "level": str(level or "warn")[:10],
51
+ "msg": re.sub(r"\s+", " ", str(msg or ""))[:300]}
52
+ if ctx:
53
+ row["ctx"] = {str(k)[:30]: str(v)[:120] for k, v in list(ctx.items())[:6]}
54
+ with p.open("a", encoding="utf-8") as f:
55
+ f.write(json.dumps(row, ensure_ascii=False) + "\n")
56
+ except Exception:
57
+ pass
58
+
59
+
60
+ def _norm(msg: str) -> str:
61
+ """归一化:数字/长 hex/路径压成占位符,避免每个 id 各算一类。"""
62
+ s = re.sub(r"\b[0-9a-f]{8,}\b", "<hex>", msg)
63
+ s = re.sub(r"\d+", "<n>", s)
64
+ s = re.sub(r"[A-Za-z]:\\[^\s'\"]+", "<path>", s)
65
+ return s[:160]
66
+
67
+
68
+ def _read(home=None) -> list[dict]:
69
+ out = []
70
+ for p in (path(home).with_suffix(".jsonl.1"), path(home)):
71
+ try:
72
+ for line in p.read_text(encoding="utf-8", errors="replace").splitlines():
73
+ line = line.strip()
74
+ if not line:
75
+ continue
76
+ try:
77
+ out.append(json.loads(line))
78
+ except ValueError:
79
+ continue
80
+ except OSError:
81
+ continue
82
+ return out
83
+
84
+
85
+ def summarize(days: float = 7, top: int = 10, *, home=None) -> dict:
86
+ now = time.time()
87
+ since = now - max(0.0, float(days)) * 86400
88
+ rows = [r for r in _read(home) if isinstance(r, dict) and float(r.get("ts") or 0) >= since]
89
+ by_kind: dict[str, int] = {}
90
+ groups: dict[tuple, dict] = {}
91
+ for r in rows:
92
+ kind = str(r.get("kind") or "other")
93
+ by_kind[kind] = by_kind.get(kind, 0) + 1
94
+ msg = str(r.get("msg") or "")
95
+ key = (kind, _norm(msg))
96
+ g = groups.setdefault(key, {"kind": kind, "normalized": _norm(msg),
97
+ "count": 0, "last_ts": 0, "sample": msg[:200]})
98
+ g["count"] += 1
99
+ g["last_ts"] = max(g["last_ts"], int(r.get("ts") or 0))
100
+ items = sorted(groups.values(), key=lambda g: (-g["count"], -g["last_ts"]))[:max(1, int(top))]
101
+ return {"window_days": float(days), "total": len(rows), "by_kind": by_kind,
102
+ "top": items}
103
+
104
+
105
+ def recent(n: int = 20, *, home=None) -> list[dict]:
106
+ rows = sorted(_read(home), key=lambda r: -int(r.get("ts") or 0))
107
+ return rows[:max(1, int(n))]
@@ -0,0 +1,114 @@
1
+ # -*- coding: utf-8 -*-
2
+ """灵枢 —— 纯 stdlib 降级向量(hash trick 嵌入)。
3
+
4
+ 零依赖铁律下的向量兜底:不装 Ollama 时,用字符级 hash 嵌入给语义召回
5
+ 提供一个"聊胜于无"的 Dense 通道。思路:
6
+
7
+ * 分词:中文按 2-gram(与 memory.terms 一致)、英文/数字按词;
8
+ * 每个 token 用 md5 hash 到固定维度(默认 256)的桶里,带 ±1 符号
9
+ (md5 第 17 位十六进制位决定正负,等价一个免费的第二哈希);
10
+ * TF 做 1+log 亚线性加权(长文不至于一词独大),最后 L2 归一化。
11
+
12
+ 向量存记忆条目 m["fvec"](list[float],4 位小数);查询时同样编码后算
13
+ 余弦。这是局部敏感哈希式词面语义:同义改写命中有限,但跨词序/跨词形
14
+ ("并发方案" vs "那个并发的方案")比纯关键词稳。真嵌入(Ollama)可用时
15
+ 优先用真的,本模块只在不可达/未配置时接管。
16
+
17
+ 坏输入纪律:任何异常一律返回 None 或零相似,绝不抛出、绝不阻断主链路。
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import hashlib
23
+ import json
24
+ import math
25
+ import os
26
+ import re
27
+ from pathlib import Path
28
+
29
+ _CJK = re.compile(r"[\u4e00-\u9fff]+")
30
+ _WORD = re.compile(r"[a-z0-9]+")
31
+
32
+
33
+ def cfg() -> dict | None:
34
+ """读 config.json 的 embed.fallback 节;未启用返回 None。
35
+
36
+ 默认开启(enabled 缺省 True),dims 默认 256。读不到配置/坏 JSON
37
+ 一律按未启用处理(返回 None),不炸。
38
+ """
39
+ try:
40
+ # 数据目录解析:优先跟随 core.HOME(测试/--home 会改它),
41
+ # 其次 BRAIN_MEMORY_HOME,最后默认 ~/.brain-memory。
42
+ # 早期版本写死 Path.home(),导致隔离 HOME 的测试拿不到兜底配置。
43
+ try:
44
+ import core # 延迟 import,避免循环
45
+ home = Path(core.HOME)
46
+ except Exception:
47
+ home = Path(os.environ.get("BRAIN_MEMORY_HOME") or (Path.home() / ".brain-memory"))
48
+ cfg_path = home / "config.json"
49
+ if not cfg_path.is_file():
50
+ return None
51
+ data = json.loads(cfg_path.read_text(encoding="utf-8"))
52
+ emb = data.get("embed") if isinstance(data.get("embed"), dict) else {}
53
+ fb = emb.get("fallback") if isinstance(emb.get("fallback"), dict) else {}
54
+ if fb.get("enabled") is False:
55
+ return None
56
+ dims = int(fb.get("dims") or 256)
57
+ if dims < 32 or dims > 4096:
58
+ dims = 256
59
+ return {"enabled": True, "dims": dims}
60
+ except Exception:
61
+ return None
62
+
63
+
64
+ def tokenize(text: str) -> list:
65
+ """中文 2-gram + 英文/数字词。坏输入返回空表,绝不抛。"""
66
+ try:
67
+ t = str(text or "").lower()
68
+ toks = []
69
+ for seg in _CJK.findall(t):
70
+ if len(seg) == 1:
71
+ toks.append(seg)
72
+ for i in range(len(seg) - 1):
73
+ toks.append(seg[i:i + 2])
74
+ toks.extend(_WORD.findall(t))
75
+ return toks
76
+ except Exception:
77
+ return []
78
+
79
+
80
+ def encode(text: str, dims: int = 256) -> list | None:
81
+ """文本 → L2 归一化 hash 嵌入向量;空文本/坏输入返回 None。"""
82
+ try:
83
+ dims = int(dims)
84
+ toks = tokenize(text)
85
+ if not toks or dims <= 0:
86
+ return None
87
+ tf = {}
88
+ for tk in toks:
89
+ tf[tk] = tf.get(tk, 0) + 1
90
+ vec = [0.0] * dims
91
+ for tk, c in tf.items():
92
+ h = hashlib.md5(tk.encode("utf-8", "ignore")).hexdigest()
93
+ bucket = int(h[:8], 16) % dims
94
+ sign = 1.0 if int(h[8], 16) % 2 == 0 else -1.0
95
+ vec[bucket] += sign * (1.0 + math.log(c))
96
+ norm = math.sqrt(sum(x * x for x in vec))
97
+ if norm <= 0:
98
+ return None
99
+ return [round(x / norm, 4) for x in vec]
100
+ except Exception:
101
+ return None
102
+
103
+
104
+ def cosine(a: list, b: list) -> float:
105
+ """余弦相似度;维度不符/坏输入返回 0.0(向量已归一化,可直接内积)。"""
106
+ try:
107
+ if not isinstance(a, list) or not isinstance(b, list) or len(a) != len(b):
108
+ return 0.0
109
+ dot = sum(x * y for x, y in zip(a, b))
110
+ na = math.sqrt(sum(x * x for x in a)) or 1.0
111
+ nb = math.sqrt(sum(x * x for x in b)) or 1.0
112
+ return max(0.0, min(1.0, dot / (na * nb)))
113
+ except Exception:
114
+ return 0.0