lingshu-skill 3.96.0 → 3.98.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.
@@ -0,0 +1,157 @@
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
+
65
+ # —— 向量与重排 ——
66
+ {"key": "embed.timeout_sec", "group": "向量与重排", "label": "向量请求超时(秒)",
67
+ "kind": "int", "min": 3, "max": 120, "step": 1,
68
+ "hint": "本地 Ollama 冷启动较慢,调大可减少误降级"},
69
+ {"key": "embed.fail_cooldown_sec", "group": "向量与重排", "label": "失败冷却(秒)",
70
+ "kind": "int", "min": 30, "max": 3600, "step": 30,
71
+ "hint": "连不上向量服务后,多久内不再重试(直接走降级)"},
72
+ {"key": "embed.fallback.enabled", "group": "向量与重排", "label": "降级向量兜底",
73
+ "kind": "bool", "hint": "没有真嵌入时用零依赖哈希向量接管"},
74
+ {"key": "rerank.top_n", "group": "向量与重排", "label": "重排候选数",
75
+ "kind": "int", "min": 3, "max": 100, "step": 1,
76
+ "hint": "送进 rerank 的候选条数,越大越准也越慢"},
77
+
78
+ # —— 面板与宿主 ——
79
+ {"key": "panel_port", "group": "面板与宿主", "label": "面板端口",
80
+ "kind": "int", "min": 1024, "max": 65535, "step": 1, "restart": True,
81
+ "hint": "改完需要重启面板才生效"},
82
+ {"key": "extensions_enabled", "group": "面板与宿主", "label": "启用扩展模块",
83
+ "kind": "bool",
84
+ "hint": "扩展是任意本地 Python 代码,等于完全信任本机——只装你自己写的"},
85
+ ]
86
+
87
+ _BY_KEY = {x["key"]: x for x in CONFIG_SCHEMA}
88
+
89
+
90
+ def _get_path(cfg: dict, key: str, default=None):
91
+ node: Any = cfg
92
+ for seg in str(key).split("."):
93
+ if not isinstance(node, dict) or seg not in node:
94
+ return default
95
+ node = node[seg]
96
+ return node
97
+
98
+
99
+ def _default_of(key: str):
100
+ return _get_path(core.DEFAULT_CONFIG, key)
101
+
102
+
103
+ def all_items() -> list[dict]:
104
+ """面板用:每项的元数据 + 当前值 + 默认值。"""
105
+ cfg = core.load_config()
106
+ out = []
107
+ for spec in CONFIG_SCHEMA:
108
+ item = dict(spec)
109
+ item["value"] = _get_path(cfg, spec["key"], _default_of(spec["key"]))
110
+ item["default"] = _default_of(spec["key"])
111
+ out.append(item)
112
+ return out
113
+
114
+
115
+ def coerce(spec: dict, raw) -> Any:
116
+ """按 schema 校验并转换一个值;不合法抛 ValueError(消息可直接给用户看)。"""
117
+ kind = spec["kind"]
118
+ if kind == "bool":
119
+ if isinstance(raw, bool):
120
+ return raw
121
+ s = str(raw).strip().lower()
122
+ if s in ("true", "1", "yes", "on"):
123
+ return True
124
+ if s in ("false", "0", "no", "off"):
125
+ return False
126
+ raise ValueError(f"{spec['label']} 是开关,只接受 true/false")
127
+ if kind in ("int", "float"):
128
+ try:
129
+ num = float(raw)
130
+ except (TypeError, ValueError):
131
+ raise ValueError(f"{spec['label']} 需要是数字")
132
+ if kind == "int":
133
+ if num != int(num):
134
+ raise ValueError(f"{spec['label']} 需要是整数")
135
+ num = int(num)
136
+ lo, hi = spec.get("min"), spec.get("max")
137
+ if lo is not None and num < lo:
138
+ raise ValueError(f"{spec['label']} 不能小于 {lo}")
139
+ if hi is not None and num > hi:
140
+ raise ValueError(f"{spec['label']} 不能大于 {hi}")
141
+ return num
142
+ if kind == "choice":
143
+ s = str(raw).strip()
144
+ if s not in spec["choices"]:
145
+ raise ValueError(f"{spec['label']} 只能是 {'/'.join(spec['choices'])}")
146
+ return s
147
+ raise ValueError("未知的配置类型")
148
+
149
+
150
+ def set_value(key: str, raw) -> Any:
151
+ """校验后写盘,返回写入后的值。未知键直接拒绝(白名单)。"""
152
+ spec = _BY_KEY.get(str(key))
153
+ if not spec:
154
+ raise KeyError(f"不允许从面板修改这个配置:{key}")
155
+ val = coerce(spec, raw)
156
+ core.config_set(key, json.dumps(val, ensure_ascii=False))
157
+ return val