lingshu-skill 5.43.0 → 5.45.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 +29 -0
- package/README.md +1 -1
- package/SKILL-lite.md +1 -1
- package/SKILL-tiny.md +1 -1
- package/SKILL.md +1 -1
- package/package.json +1 -1
- package/scripts/_tmp_speed_survey.py +49 -0
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +11 -10
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +29 -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/_tmp_speed_survey.py +49 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +14 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +22 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +14 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_settings.py +211 -209
- package/scripts/astrbot_plugin_lingshu/engine/scripts/persona.py +30 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/startup_fast.py +38 -0
- package/scripts/cli.py +14 -1
- package/scripts/memory.py +22 -0
- package/scripts/panel_server.py +14 -0
- package/scripts/panel_settings.py +211 -209
- package/scripts/persona.py +30 -0
- package/scripts/startup_fast.py +38 -0
package/scripts/panel_server.py
CHANGED
|
@@ -936,6 +936,20 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
936
936
|
return
|
|
937
937
|
self._json({"packet": packet})
|
|
938
938
|
return
|
|
939
|
+
if path == "/api/startup-fast":
|
|
940
|
+
# SPEED-2: startup 快路径——CLI 在面板常驻时优先走这里拿注入包,
|
|
941
|
+
# 跳过本进程 import brain(~151ms)。鉴权与 /api/overview 相同(token/origin 已在上游闸门)。
|
|
942
|
+
qs = parse_qs(url.query)
|
|
943
|
+
q = (qs.get("q") or [""])[0]
|
|
944
|
+
budget = (qs.get("budget") or [""])[0]
|
|
945
|
+
try:
|
|
946
|
+
packet = (brain.build_injection(q, budget_tokens=int(budget))
|
|
947
|
+
if budget.isdigit() else brain.build_injection(q))
|
|
948
|
+
except Exception as exc:
|
|
949
|
+
self._json({"error": f"生成注入包失败:{exc}"}, 500)
|
|
950
|
+
return
|
|
951
|
+
self._json({"packet": packet, "via": "serve"})
|
|
952
|
+
return
|
|
939
953
|
if path == "/api/conflicts":
|
|
940
954
|
limit = _int_param(url, "limit", 50, 1, 200)
|
|
941
955
|
self._json({"conflicts": brain.detect_conflicts(limit)})
|
|
@@ -1,209 +1,211 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""面板「设置」页的配置清单与校验(白名单)。
|
|
3
|
-
|
|
4
|
-
为什么单独一个模块:面板要能改开关/数值,但**绝不能任意写 config.json**——
|
|
5
|
-
配错一个键就能让注入崩掉或把路径指飞。这里只列"安全且常用"的键,带类型/范围/
|
|
6
|
-
枚举校验,面板和 API 都走这一份,避免两处各写一套校验。
|
|
7
|
-
"""
|
|
8
|
-
from __future__ import annotations
|
|
9
|
-
|
|
10
|
-
import json
|
|
11
|
-
from typing import Any
|
|
12
|
-
|
|
13
|
-
import core
|
|
14
|
-
|
|
15
|
-
# group 顺序即面板显示顺序;kind ∈ bool / int / float / choice
|
|
16
|
-
CONFIG_SCHEMA: list[dict[str, Any]] = [
|
|
17
|
-
# —— 注入与预算 ——
|
|
18
|
-
{"key": "budget_tokens", "group": "注入与预算", "label": "注入 token 预算",
|
|
19
|
-
"kind": "int", "min": 100, "max": 20000, "step": 100,
|
|
20
|
-
"hint": "每次 startup 注入的记忆块上限;调大=记得更多,调小=更省上下文"},
|
|
21
|
-
{"key": "inject_max_chars", "group": "注入与预算", "label": "单条记忆注入字符上限",
|
|
22
|
-
"kind": "int", "min": 80, "max": 5000, "step": 20,
|
|
23
|
-
"hint": "一条记忆在注入里最多显示多少字,超出截断"},
|
|
24
|
-
{"key": "inject_mode", "group": "注入与预算", "label": "注入模式",
|
|
25
|
-
"kind": "choice", "choices": ["full", "lazy"],
|
|
26
|
-
"hint": "full=带正文;lazy=只带要点+id,正文交给 recall 现取(更省 token)"},
|
|
27
|
-
{"key": "remember_max_chars", "group": "注入与预算", "label": "单条记忆原文上限",
|
|
28
|
-
"kind": "int", "min": 200, "max": 8000, "step": 100,
|
|
29
|
-
"hint": "remember 写入时超过就截断,只影响写入上限"},
|
|
30
|
-
|
|
31
|
-
# —— 水位与节奏 ——
|
|
32
|
-
{"key": "review_threshold", "group": "水位与节奏", "label": "清点线",
|
|
33
|
-
"kind": "float", "min": 0.1, "max": 0.95, "step": 0.05,
|
|
34
|
-
"hint": "上下文用到这个比例就该补记清点(默认 0.6)"},
|
|
35
|
-
{"key": "dream_threshold", "group": "水位与节奏", "label": "做梦线",
|
|
36
|
-
"kind": "float", "min": 0.1, "max": 0.99, "step": 0.05,
|
|
37
|
-
"hint": "到这条线建议 dream 压缩(默认 0.75)"},
|
|
38
|
-
{"key": "escape_threshold", "group": "水位与节奏", "label": "封存线",
|
|
39
|
-
"kind": "float", "min": 0.1, "max": 1.0, "step": 0.05,
|
|
40
|
-
"hint": "到这条线先 capture 原样封存保底(默认 0.9)"},
|
|
41
|
-
{"key": "mood_inject_per_day", "group": "水位与节奏", "label": "每日心情注入上限",
|
|
42
|
-
"kind": "int", "min": 0, "max": 50, "step": 1,
|
|
43
|
-
"hint": "一天最多在注入里提醒几次心情;0=不提醒"},
|
|
44
|
-
{"key": "mood_inject_change_delta", "group": "水位与节奏", "label": "心情有变化才注入",
|
|
45
|
-
"kind": "bool", "hint": "开=只在心情明显变化时提醒,避免每次都念一遍"},
|
|
46
|
-
|
|
47
|
-
# —— 记忆行为 ——
|
|
48
|
-
{"key": "emotion_auto_extract", "group": "记忆行为", "label": "对话后自动评价情绪",
|
|
49
|
-
"kind": "bool", "hint": "封存时顺手写一条情绪事件(强度封顶,不猜)"},
|
|
50
|
-
{"key": "session_state_cache", "group": "记忆行为", "label": "会话状态缓存",
|
|
51
|
-
"kind": "bool", "hint": "库没变时复用上次注入文本,跨天首轮更快"},
|
|
52
|
-
{"key": "session_state_ttl", "group": "记忆行为", "label": "缓存有效期(秒)",
|
|
53
|
-
"kind": "int", "min": 30, "max": 86400, "step": 30,
|
|
54
|
-
"hint": "缓存多久内有效,过期重新生成"},
|
|
55
|
-
{"key": "dialogue_ingest", "group": "记忆行为", "label": "导入其它 AI 的对话",
|
|
56
|
-
"kind": "bool", "hint": "把 Claude/Codex 等会话记录摄取进记忆库"},
|
|
57
|
-
{"key": "dialogue_ingest_auto", "group": "记忆行为", "label": "自动导入(无需手动触发)",
|
|
58
|
-
"kind": "bool", "hint": "开启后按需自动摄取,注意会消耗处理时间"},
|
|
59
|
-
{"key": "context_ab.enabled", "group": "记忆行为", "label": "上下文 A/B 对照",
|
|
60
|
-
"kind": "bool", "hint": "抽样记录不同注入策略的效果,用于调参"},
|
|
61
|
-
{"key": "model_tier", "group": "记忆行为", "label": "模型档位",
|
|
62
|
-
"kind": "choice", "choices": ["auto", "small", "large"],
|
|
63
|
-
"hint": "small=关掉高风险自动能力(小模型建议);auto=按需"},
|
|
64
|
-
{"key": "dream_local_extract", "group": "记忆行为", "label": "做梦本地实体抽取",
|
|
65
|
-
"kind": "bool", "hint": "合并摘要附带簇内关联实体(纯本地词表,无 LLM 无网络),提高摘要可检索性(IDEA-09)"},
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
{"key": "
|
|
77
|
-
"kind": "
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"
|
|
89
|
-
{"key": "
|
|
90
|
-
"kind": "bool", "hint": "
|
|
91
|
-
{"key": "
|
|
92
|
-
"kind": "bool", "hint": "
|
|
93
|
-
{"key": "
|
|
94
|
-
"kind": "
|
|
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
|
-
node
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
item
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if s in ("
|
|
177
|
-
return
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
num
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if
|
|
192
|
-
raise ValueError(f"{spec['label']}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""面板「设置」页的配置清单与校验(白名单)。
|
|
3
|
+
|
|
4
|
+
为什么单独一个模块:面板要能改开关/数值,但**绝不能任意写 config.json**——
|
|
5
|
+
配错一个键就能让注入崩掉或把路径指飞。这里只列"安全且常用"的键,带类型/范围/
|
|
6
|
+
枚举校验,面板和 API 都走这一份,避免两处各写一套校验。
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
import core
|
|
14
|
+
|
|
15
|
+
# group 顺序即面板显示顺序;kind ∈ bool / int / float / choice
|
|
16
|
+
CONFIG_SCHEMA: list[dict[str, Any]] = [
|
|
17
|
+
# —— 注入与预算 ——
|
|
18
|
+
{"key": "budget_tokens", "group": "注入与预算", "label": "注入 token 预算",
|
|
19
|
+
"kind": "int", "min": 100, "max": 20000, "step": 100,
|
|
20
|
+
"hint": "每次 startup 注入的记忆块上限;调大=记得更多,调小=更省上下文"},
|
|
21
|
+
{"key": "inject_max_chars", "group": "注入与预算", "label": "单条记忆注入字符上限",
|
|
22
|
+
"kind": "int", "min": 80, "max": 5000, "step": 20,
|
|
23
|
+
"hint": "一条记忆在注入里最多显示多少字,超出截断"},
|
|
24
|
+
{"key": "inject_mode", "group": "注入与预算", "label": "注入模式",
|
|
25
|
+
"kind": "choice", "choices": ["full", "lazy"],
|
|
26
|
+
"hint": "full=带正文;lazy=只带要点+id,正文交给 recall 现取(更省 token)"},
|
|
27
|
+
{"key": "remember_max_chars", "group": "注入与预算", "label": "单条记忆原文上限",
|
|
28
|
+
"kind": "int", "min": 200, "max": 8000, "step": 100,
|
|
29
|
+
"hint": "remember 写入时超过就截断,只影响写入上限"},
|
|
30
|
+
|
|
31
|
+
# —— 水位与节奏 ——
|
|
32
|
+
{"key": "review_threshold", "group": "水位与节奏", "label": "清点线",
|
|
33
|
+
"kind": "float", "min": 0.1, "max": 0.95, "step": 0.05,
|
|
34
|
+
"hint": "上下文用到这个比例就该补记清点(默认 0.6)"},
|
|
35
|
+
{"key": "dream_threshold", "group": "水位与节奏", "label": "做梦线",
|
|
36
|
+
"kind": "float", "min": 0.1, "max": 0.99, "step": 0.05,
|
|
37
|
+
"hint": "到这条线建议 dream 压缩(默认 0.75)"},
|
|
38
|
+
{"key": "escape_threshold", "group": "水位与节奏", "label": "封存线",
|
|
39
|
+
"kind": "float", "min": 0.1, "max": 1.0, "step": 0.05,
|
|
40
|
+
"hint": "到这条线先 capture 原样封存保底(默认 0.9)"},
|
|
41
|
+
{"key": "mood_inject_per_day", "group": "水位与节奏", "label": "每日心情注入上限",
|
|
42
|
+
"kind": "int", "min": 0, "max": 50, "step": 1,
|
|
43
|
+
"hint": "一天最多在注入里提醒几次心情;0=不提醒"},
|
|
44
|
+
{"key": "mood_inject_change_delta", "group": "水位与节奏", "label": "心情有变化才注入",
|
|
45
|
+
"kind": "bool", "hint": "开=只在心情明显变化时提醒,避免每次都念一遍"},
|
|
46
|
+
|
|
47
|
+
# —— 记忆行为 ——
|
|
48
|
+
{"key": "emotion_auto_extract", "group": "记忆行为", "label": "对话后自动评价情绪",
|
|
49
|
+
"kind": "bool", "hint": "封存时顺手写一条情绪事件(强度封顶,不猜)"},
|
|
50
|
+
{"key": "session_state_cache", "group": "记忆行为", "label": "会话状态缓存",
|
|
51
|
+
"kind": "bool", "hint": "库没变时复用上次注入文本,跨天首轮更快"},
|
|
52
|
+
{"key": "session_state_ttl", "group": "记忆行为", "label": "缓存有效期(秒)",
|
|
53
|
+
"kind": "int", "min": 30, "max": 86400, "step": 30,
|
|
54
|
+
"hint": "缓存多久内有效,过期重新生成"},
|
|
55
|
+
{"key": "dialogue_ingest", "group": "记忆行为", "label": "导入其它 AI 的对话",
|
|
56
|
+
"kind": "bool", "hint": "把 Claude/Codex 等会话记录摄取进记忆库"},
|
|
57
|
+
{"key": "dialogue_ingest_auto", "group": "记忆行为", "label": "自动导入(无需手动触发)",
|
|
58
|
+
"kind": "bool", "hint": "开启后按需自动摄取,注意会消耗处理时间"},
|
|
59
|
+
{"key": "context_ab.enabled", "group": "记忆行为", "label": "上下文 A/B 对照",
|
|
60
|
+
"kind": "bool", "hint": "抽样记录不同注入策略的效果,用于调参"},
|
|
61
|
+
{"key": "model_tier", "group": "记忆行为", "label": "模型档位",
|
|
62
|
+
"kind": "choice", "choices": ["auto", "small", "large"],
|
|
63
|
+
"hint": "small=关掉高风险自动能力(小模型建议);auto=按需"},
|
|
64
|
+
{"key": "dream_local_extract", "group": "记忆行为", "label": "做梦本地实体抽取",
|
|
65
|
+
"kind": "bool", "hint": "合并摘要附带簇内关联实体(纯本地词表,无 LLM 无网络),提高摘要可检索性(IDEA-09)"},
|
|
66
|
+
{"key": "startup_use_serve", "group": "水位与节奏", "label": "startup 走常驻快路径",
|
|
67
|
+
"kind": "bool", "hint": "面板开着时 startup 优先从常驻服务拿注入包(省 ~150ms+消灭冷启动 5s),失败自动回退本进程(SPEED-2)"},
|
|
68
|
+
|
|
69
|
+
# —— 向量与重排 ——
|
|
70
|
+
{"key": "embed.timeout_sec", "group": "向量与重排", "label": "向量请求超时(秒)",
|
|
71
|
+
"kind": "int", "min": 3, "max": 120, "step": 1,
|
|
72
|
+
"hint": "本地 Ollama 冷启动较慢,调大可减少误降级"},
|
|
73
|
+
{"key": "embed.fail_cooldown_sec", "group": "向量与重排", "label": "失败冷却(秒)",
|
|
74
|
+
"kind": "int", "min": 30, "max": 3600, "step": 30,
|
|
75
|
+
"hint": "连不上向量服务后,多久内不再重试(直接走降级)"},
|
|
76
|
+
{"key": "embed.fallback.enabled", "group": "向量与重排", "label": "降级向量兜底",
|
|
77
|
+
"kind": "bool", "hint": "没有真嵌入时用零依赖哈希向量接管"},
|
|
78
|
+
{"key": "rerank.top_n", "group": "向量与重排", "label": "重排候选数",
|
|
79
|
+
"kind": "int", "min": 3, "max": 100, "step": 1,
|
|
80
|
+
"hint": "送进 rerank 的候选条数,越大越准也越慢"},
|
|
81
|
+
|
|
82
|
+
# —— 面板与宿主 ——
|
|
83
|
+
{"key": "emotion_memory_weight", "group": "情绪与人格", "label": "情绪驱动记忆权重",
|
|
84
|
+
"kind": "bool",
|
|
85
|
+
"hint": "负面且强烈的情绪会让相关记忆更重要、更容易被想起(累计增量封顶 0.30)"},
|
|
86
|
+
{"key": "emotion_inertia_enabled", "group": "情绪与人格", "label": "情绪惯性",
|
|
87
|
+
"kind": "bool",
|
|
88
|
+
"hint": "情绪上来快、下去慢:负面衰减更慢 + 强情绪冷却 + 标签需 2 条事件才切换"},
|
|
89
|
+
{"key": "emotion_negative_slow", "group": "情绪与人格", "label": "负面情绪更耐放",
|
|
90
|
+
"kind": "bool", "hint": "负面情绪衰减速率更慢(半衰期 ×1.25)"},
|
|
91
|
+
{"key": "emotion_cooldown_enabled", "group": "情绪与人格", "label": "强情绪冷却",
|
|
92
|
+
"kind": "bool", "hint": "强度>0.7 之后 3 个事件内同组增益减半,防越说越气"},
|
|
93
|
+
{"key": "emotion_label_gate_enabled", "group": "情绪与人格", "label": "心情标签防跳变",
|
|
94
|
+
"kind": "bool", "hint": "标签需 ≥2 条情绪事件支撑才切换,避免单句话横跳"},
|
|
95
|
+
{"key": "speak_up_prob", "group": "水位与节奏", "label": "主动搭话概率",
|
|
96
|
+
"kind": "float", "min": 0, "max": 1, "step": 0.05,
|
|
97
|
+
"hint": "每轮结束按此概率让灵枢自己说一句(纯模板、不进模型);0=关闭"},
|
|
98
|
+
{"key": "speak_up_cooldown_min", "group": "水位与节奏", "label": "主动搭话冷却(分钟)",
|
|
99
|
+
"kind": "int", "min": 0, "max": 1440, "step": 5,
|
|
100
|
+
"hint": "两次主动搭话之间至少间隔多久,防刷屏"},
|
|
101
|
+
{"key": "use_index_memory", "group": "注入与预算", "label": "索引法记忆",
|
|
102
|
+
"kind": "enum", "options": ["auto", "always", "off"],
|
|
103
|
+
"hint": "注入只送一句索引+id,正文用 recall 取;auto=水位≥60% 才切(省 40%+ 注入量)"},
|
|
104
|
+
{"key": "update_source", "group": "自动更新与备份", "label": "更新下载源",
|
|
105
|
+
"kind": "enum", "options": ["auto", "npm", "git"],
|
|
106
|
+
"hint": "auto=npm 优先(失败退回 Gitee/GitHub);npm=只走 npm 官方包;git=只走 Gitee/GitHub"},
|
|
107
|
+
{"key": "panel_idle_minutes", "group": "面板与宿主", "label": "空闲自动退出(分钟)",
|
|
108
|
+
"kind": "int", "min": 0, "max": 1440, "step": 5,
|
|
109
|
+
"hint": "关掉网页后,多少分钟没请求就自动退出服务(默认 1);0=永不退出(需重启面板生效)"},
|
|
110
|
+
{"key": "panel_port", "group": "面板与宿主", "label": "面板端口",
|
|
111
|
+
"kind": "int", "min": 1024, "max": 65535, "step": 1, "restart": True,
|
|
112
|
+
"hint": "改完需要重启面板才生效"},
|
|
113
|
+
{"key": "extensions_enabled", "group": "面板与宿主", "label": "启用扩展模块",
|
|
114
|
+
"kind": "bool",
|
|
115
|
+
"hint": "扩展是任意本地 Python 代码,等于完全信任本机——只装你自己写的"},
|
|
116
|
+
|
|
117
|
+
# —— 自动更新与备份 ——
|
|
118
|
+
{"key": "auto_update", "group": "自动更新与备份", "label": "自动更新灵枢",
|
|
119
|
+
"kind": "bool",
|
|
120
|
+
"hint": "开启后定期检查新版本并自动更新;更新前会先备份记忆库,失败自动回滚"},
|
|
121
|
+
{"key": "auto_update_interval_hours", "group": "自动更新与备份",
|
|
122
|
+
"label": "检查更新间隔(小时)", "kind": "int", "min": 1, "max": 720, "step": 1,
|
|
123
|
+
"hint": "每隔多久查一次新版本(默认 24 小时)"},
|
|
124
|
+
{"key": "backup_interval_hours", "group": "自动更新与备份", "label": "自动备份间隔(小时)",
|
|
125
|
+
"kind": "int", "min": 1, "max": 168, "step": 1,
|
|
126
|
+
"hint": "每隔多久自动备份一次记忆库(默认 6 小时;原来是每天一次)"},
|
|
127
|
+
{"key": "backup_min_new_records", "group": "自动更新与备份", "label": "新增记录触发阈值",
|
|
128
|
+
"kind": "int", "min": 0, "max": 10000, "step": 10,
|
|
129
|
+
"hint": "新增这么多条记录就补一次备份(0=只看时间间隔)"},
|
|
130
|
+
{"key": "backup_keep_hourly", "group": "自动更新与备份", "label": "保留:最近几份",
|
|
131
|
+
"kind": "int", "min": 1, "max": 100, "step": 1,
|
|
132
|
+
"hint": "小时层保留份数;最新一份永不删"},
|
|
133
|
+
{"key": "backup_keep_daily", "group": "自动更新与备份", "label": "保留:每天几份",
|
|
134
|
+
"kind": "int", "min": 0, "max": 90, "step": 1,
|
|
135
|
+
"hint": "天层保留(每天最多留 1 份,这里填天数)"},
|
|
136
|
+
{"key": "backup_keep_weekly", "group": "自动更新与备份", "label": "保留:每周几份",
|
|
137
|
+
"kind": "int", "min": 0, "max": 52, "step": 1,
|
|
138
|
+
"hint": "周层保留(每周最多留 1 份,这里填周数)"},
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
_BY_KEY = {x["key"]: x for x in CONFIG_SCHEMA}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _get_path(cfg: dict, key: str, default=None):
|
|
145
|
+
node: Any = cfg
|
|
146
|
+
for seg in str(key).split("."):
|
|
147
|
+
if not isinstance(node, dict) or seg not in node:
|
|
148
|
+
return default
|
|
149
|
+
node = node[seg]
|
|
150
|
+
return node
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _default_of(key: str):
|
|
154
|
+
return _get_path(core.DEFAULT_CONFIG, key)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def all_items() -> list[dict]:
|
|
158
|
+
"""面板用:每项的元数据 + 当前值 + 默认值。"""
|
|
159
|
+
cfg = core.load_config()
|
|
160
|
+
out = []
|
|
161
|
+
for spec in CONFIG_SCHEMA:
|
|
162
|
+
item = dict(spec)
|
|
163
|
+
item["value"] = _get_path(cfg, spec["key"], _default_of(spec["key"]))
|
|
164
|
+
item["default"] = _default_of(spec["key"])
|
|
165
|
+
out.append(item)
|
|
166
|
+
return out
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def coerce(spec: dict, raw) -> Any:
|
|
170
|
+
"""按 schema 校验并转换一个值;不合法抛 ValueError(消息可直接给用户看)。"""
|
|
171
|
+
kind = spec["kind"]
|
|
172
|
+
if kind == "bool":
|
|
173
|
+
if isinstance(raw, bool):
|
|
174
|
+
return raw
|
|
175
|
+
s = str(raw).strip().lower()
|
|
176
|
+
if s in ("true", "1", "yes", "on"):
|
|
177
|
+
return True
|
|
178
|
+
if s in ("false", "0", "no", "off"):
|
|
179
|
+
return False
|
|
180
|
+
raise ValueError(f"{spec['label']} 是开关,只接受 true/false")
|
|
181
|
+
if kind in ("int", "float"):
|
|
182
|
+
try:
|
|
183
|
+
num = float(raw)
|
|
184
|
+
except (TypeError, ValueError):
|
|
185
|
+
raise ValueError(f"{spec['label']} 需要是数字")
|
|
186
|
+
if kind == "int":
|
|
187
|
+
if num != int(num):
|
|
188
|
+
raise ValueError(f"{spec['label']} 需要是整数")
|
|
189
|
+
num = int(num)
|
|
190
|
+
lo, hi = spec.get("min"), spec.get("max")
|
|
191
|
+
if lo is not None and num < lo:
|
|
192
|
+
raise ValueError(f"{spec['label']} 不能小于 {lo}")
|
|
193
|
+
if hi is not None and num > hi:
|
|
194
|
+
raise ValueError(f"{spec['label']} 不能大于 {hi}")
|
|
195
|
+
return num
|
|
196
|
+
if kind == "choice":
|
|
197
|
+
s = str(raw).strip()
|
|
198
|
+
if s not in spec["choices"]:
|
|
199
|
+
raise ValueError(f"{spec['label']} 只能是 {'/'.join(spec['choices'])}")
|
|
200
|
+
return s
|
|
201
|
+
raise ValueError("未知的配置类型")
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def set_value(key: str, raw) -> Any:
|
|
205
|
+
"""校验后写盘,返回写入后的值。未知键直接拒绝(白名单)。"""
|
|
206
|
+
spec = _BY_KEY.get(str(key))
|
|
207
|
+
if not spec:
|
|
208
|
+
raise KeyError(f"不允许从面板修改这个配置:{key}")
|
|
209
|
+
val = coerce(spec, raw)
|
|
210
|
+
core.config_set(key, json.dumps(val, ensure_ascii=False))
|
|
211
|
+
return val
|
package/scripts/persona.py
CHANGED
|
@@ -14,6 +14,11 @@ import shutil
|
|
|
14
14
|
import sys
|
|
15
15
|
import threading
|
|
16
16
|
import time
|
|
17
|
+
|
|
18
|
+
# SPEED-1: load_persona 模块级微缓存(见 load_persona docstring)
|
|
19
|
+
_lp_cache = None
|
|
20
|
+
_lp_cache_mtime = None
|
|
21
|
+
_lp_cache_time = 0.0
|
|
17
22
|
import zipfile
|
|
18
23
|
from contextlib import contextmanager, nullcontext
|
|
19
24
|
from datetime import datetime
|
|
@@ -259,6 +264,17 @@ def compile_persona() -> dict:
|
|
|
259
264
|
p["drives"] = {}
|
|
260
265
|
p.setdefault("onboarding", default_onboarding())
|
|
261
266
|
if not READONLY:
|
|
267
|
+
# 空态守卫(与 save_persona 同一闸):盘上有人格且待写入为空态时拒绝——
|
|
268
|
+
# compile_persona 原先直写 atomic_json 绕过了 save_persona 的守卫,
|
|
269
|
+
# 曾导致 startup 路径在竞态窗口把清空态合法化写盘(12-03 18:20 事故)。
|
|
270
|
+
_disk_now = _slurp_json(HOME / "persona.json") if (HOME / "persona.json").exists() else None
|
|
271
|
+
_disk_named = bool((( _disk_now or {}).get("identity") or {}).get("名字"))
|
|
272
|
+
_incoming_named = bool(((p.get("identity") or {}).get("名字") or "").strip())
|
|
273
|
+
if _disk_named and not _incoming_named:
|
|
274
|
+
log_event("persona",
|
|
275
|
+
"compile_persona 拒绝覆盖:盘上人格有名字而重建产物为空态——疑似竞态,已拦下。",
|
|
276
|
+
level="error")
|
|
277
|
+
return _slurp_json(HOME / "persona.json") # 返回盘上完好人格
|
|
262
278
|
atomic_json(HOME / "persona.json", p)
|
|
263
279
|
return p
|
|
264
280
|
|
|
@@ -288,6 +304,17 @@ def _validate_persona_struct(obj, where: str = "persona.json") -> None:
|
|
|
288
304
|
def load_persona() -> dict:
|
|
289
305
|
ensure_home()
|
|
290
306
|
path = HOME / "persona.json"
|
|
307
|
+
# SPEED-1 模块级微缓存(TTL 2s):build_injection 一次会调 24 次 load_persona,
|
|
308
|
+
# 每次都重读盘+跑迁移检查 ≈ 4ms × 24 ≈ 100ms。写路径(save_persona)主动失效,
|
|
309
|
+
# mtime 双检兜底外部改动——正确性与磁盘同源,只省重复读。
|
|
310
|
+
global _lp_cache, _lp_cache_mtime, _lp_cache_time
|
|
311
|
+
try:
|
|
312
|
+
_mt = path.stat().st_mtime_ns
|
|
313
|
+
except OSError:
|
|
314
|
+
_mt = None
|
|
315
|
+
if (_lp_cache is not None and _mt is not None and _mt == _lp_cache_mtime
|
|
316
|
+
and time.time() - _lp_cache_time < 2.0):
|
|
317
|
+
return json.loads(json.dumps(_lp_cache, ensure_ascii=False)) # 深拷贝防污染
|
|
291
318
|
if path.exists():
|
|
292
319
|
p = _slurp_json(path)
|
|
293
320
|
if not isinstance(p, dict):
|
|
@@ -337,10 +364,13 @@ def load_persona() -> dict:
|
|
|
337
364
|
changed = True
|
|
338
365
|
if changed and not READONLY:
|
|
339
366
|
save_persona(p)
|
|
367
|
+
_lp_cache, _lp_cache_mtime, _lp_cache_time = p, _mt, time.time()
|
|
340
368
|
return p
|
|
341
369
|
|
|
342
370
|
|
|
343
371
|
def save_persona(p: dict) -> None:
|
|
372
|
+
global _lp_cache, _lp_cache_mtime, _lp_cache_time
|
|
373
|
+
_lp_cache = None # 写路径主动失效缓存
|
|
344
374
|
# 空人格写盘守卫(2026-12-03 两次清空事故加固):
|
|
345
375
|
# 盘上已有完整人格(有名字)时,拒绝用 identity+traits 双空的默认态覆盖——
|
|
346
376
|
# 除非是 init --force 的显式重置(调用方传 _allow_empty=True 绕过)。
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""SPEED-2: startup 走常驻 serve 的 HTTP 快路径(可选优化,失败静默回退)。
|
|
2
|
+
|
|
3
|
+
原理:面板 serve(panel_server.py)常驻时,memory/graph 模块已加载、
|
|
4
|
+
系统页缓存已热——通过 /api/startup-fast 拿注入包能跳过本进程的
|
|
5
|
+
import brain(~151ms)+ 冷读盘,实测省 ~150-200ms/次。
|
|
6
|
+
|
|
7
|
+
协议:
|
|
8
|
+
- GET /api/startup-fast?q=<query>&budget=<tokens>
|
|
9
|
+
- 响应 {"packet": str, "via": "serve"};任何失败返回非 200,调用方回退本进程。
|
|
10
|
+
- 超时 1200ms:快路径的价值就是快,超时宁可直接本进程跑(总成本更低)。
|
|
11
|
+
|
|
12
|
+
调用方(cli.py cmd_startup)在 config.startup_use_serve 为 True(默认 true)
|
|
13
|
+
时先试这条路;面板没开/超时/出错一律静默回退,零风险。
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
import urllib.request
|
|
19
|
+
|
|
20
|
+
_FAST_TIMEOUT = 1.2 # 秒
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def try_serve_startup(query: str = "", budget_tokens: int | None = None,
|
|
24
|
+
port: int = 18997) -> str | None:
|
|
25
|
+
"""尝试从常驻面板拿注入包。成功返回注入文本,任何失败返回 None。"""
|
|
26
|
+
try:
|
|
27
|
+
qs = f"q={urllib.parse.quote(query or '')}"
|
|
28
|
+
if budget_tokens:
|
|
29
|
+
qs += f"&budget={int(budget_tokens)}"
|
|
30
|
+
req = urllib.request.Request(
|
|
31
|
+
f"http://127.0.0.1:{port}/api/startup-fast?{qs}",
|
|
32
|
+
headers={"User-Agent": "lingshu-startup-fast"})
|
|
33
|
+
with urllib.request.urlopen(req, timeout=_FAST_TIMEOUT) as r:
|
|
34
|
+
data = json.loads(r.read().decode("utf-8"))
|
|
35
|
+
packet = data.get("packet")
|
|
36
|
+
return packet if isinstance(packet, str) and packet else None
|
|
37
|
+
except Exception:
|
|
38
|
+
return None # 静默回退:面板没开/超时/任何错都不影响主链路
|