dsh-seal-scripts 0.0.0-stage → 0.4.8
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/LICENSE +21 -0
- package/README.en.md +318 -0
- package/README.md +342 -2
- package/cordis.patch.yml +6 -0
- package/index.js +12 -0
- package/lib/client.js +1465 -0
- package/lib/core/assembly.js +321 -0
- package/lib/core/catalog-snapshot.js +78 -0
- package/lib/core/diary.js +100 -0
- package/lib/core/health.js +72 -0
- package/lib/core/index.js +491 -0
- package/lib/core/inject.js +146 -0
- package/lib/core/lang.js +251 -0
- package/lib/core/locale.js +56 -0
- package/lib/core/memory-file.js +214 -0
- package/lib/core/migrate.js +97 -0
- package/lib/core/path-guard.js +43 -0
- package/lib/core/project-key.js +52 -0
- package/lib/core/render.js +228 -0
- package/lib/core/schema-runner.js +281 -0
- package/lib/core/schema-tools.js +794 -0
- package/lib/core/schema.js +493 -0
- package/lib/core/search.js +254 -0
- package/lib/core/settings.js +158 -0
- package/lib/core/store.js +408 -0
- package/lib/core/tool-transfer.js +249 -0
- package/lib/core/tool.js +602 -0
- package/lib/core/zip.js +226 -0
- package/lib/core/zstd-js.js +705 -0
- package/lib/core/zstd.js +103 -0
- package/lib/host/api.js +9 -0
- package/lib/host/capabilities.js +155 -0
- package/lib/host/compact-delete.js +76 -0
- package/lib/host/delete.js +136 -0
- package/lib/host/host-contract.js +73 -0
- package/lib/host/http-util.js +82 -0
- package/lib/host/lifecycle.js +66 -0
- package/lib/host/register.js +63 -0
- package/lib/host/routes/memories.js +102 -0
- package/lib/host/routes/options.js +46 -0
- package/lib/host/routes/sessions.js +194 -0
- package/lib/host/routes/tools.js +103 -0
- package/lib/host/summary.js +71 -0
- package/package.json +71 -4
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 搜索(设计文档第十二节):按「工作区的回顾文件夹」搜,不是文件系统递归。
|
|
3
|
+
*
|
|
4
|
+
* 候选顺序(0.1.26 起):
|
|
5
|
+
* 1. 本工作区:自己第一,其余子级从最深一层往回收
|
|
6
|
+
* 2. 本工作区的平级:先探到最深一层,从最深往回收,最后才轮到平级自己
|
|
7
|
+
* 3. 再往上一层以后:上一级 -> 它的平级 -> 它们的第 1 层子 -> 第 2 层子 …(由外到内)
|
|
8
|
+
*
|
|
9
|
+
* 范围:本工作区及其子级是「本地范围」,不需要用户许可;平级与上级是「外圈」,
|
|
10
|
+
* 只有 allowOuter 为真(用户点头之后)才纳入。
|
|
11
|
+
*
|
|
12
|
+
* 读取规则:一次只交给模型一条候选;只有「本工作区自己」那一条被读出去时才改深度
|
|
13
|
+
* (0 -> 1、1 -> 2 并锁定)。子级、平级、上级都是只读、不计。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
import { memoryDir, memoryRoot, readAssembly, writeEntry } from './store.js'
|
|
19
|
+
import { listMemoryFiles, listSidecars } from './migrate.js'
|
|
20
|
+
import { decodeProjectKey } from './project-key.js'
|
|
21
|
+
import { parseMemory } from './memory-file.js'
|
|
22
|
+
import { readField } from './lang.js'
|
|
23
|
+
import { effectiveCount } from './assembly.js'
|
|
24
|
+
|
|
25
|
+
/** 所有还留着回顾的工作区。工作区路径优先取载入文件里记的,取不到才反解目录名。 */
|
|
26
|
+
export function knownWorkspaces(dshHome) {
|
|
27
|
+
const rootDir = memoryRoot(dshHome)
|
|
28
|
+
let names = []
|
|
29
|
+
try {
|
|
30
|
+
names = fs.readdirSync(rootDir, { withFileTypes: true })
|
|
31
|
+
.filter((d) => d.isDirectory() && !d.name.startsWith('.'))
|
|
32
|
+
.map((d) => d.name)
|
|
33
|
+
} catch {
|
|
34
|
+
return []
|
|
35
|
+
}
|
|
36
|
+
const out = []
|
|
37
|
+
for (const name of names) {
|
|
38
|
+
const dir = path.join(rootDir, name)
|
|
39
|
+
// 工作区路径记在任一边车里(同一个目录的边车都写同一个 workspace);
|
|
40
|
+
// 没有边车(或边车坏了)就反解目录名。
|
|
41
|
+
let workspace = null
|
|
42
|
+
for (const side of listSidecars(dir)) {
|
|
43
|
+
try {
|
|
44
|
+
const data = JSON.parse(fs.readFileSync(path.join(dir, side), 'utf8'))
|
|
45
|
+
// path.isAbsolute 两端都对:POSIX 看 '/',Windows 看 'E:\\' 这种盘符
|
|
46
|
+
if (data && typeof data.workspace === 'string' && path.isAbsolute(data.workspace)) {
|
|
47
|
+
workspace = data.workspace
|
|
48
|
+
break
|
|
49
|
+
}
|
|
50
|
+
} catch { /* 坏边车:换下一份 */ }
|
|
51
|
+
}
|
|
52
|
+
const ws = workspace ?? decodeProjectKey(name)
|
|
53
|
+
// 条目数就是 .md 的数量:.md 是唯一真相,不再信任何清单里的计数。
|
|
54
|
+
const count = listMemoryFiles(dir).length
|
|
55
|
+
out.push({ workspace: ws, dir, count })
|
|
56
|
+
}
|
|
57
|
+
return out.sort((a, b) => a.workspace.localeCompare(b.workspace))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function parentOf(p) {
|
|
61
|
+
const d = path.dirname(p)
|
|
62
|
+
return d === p ? null : d
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** child 是否在 parent 下面(按路径分段判断,POSIX 与 Windows 都成立)。 */
|
|
66
|
+
function isUnder(child, parent) {
|
|
67
|
+
const rel = path.relative(parent, child)
|
|
68
|
+
return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** ancestor 到 w 隔了几层;w 就是 ancestor 时是 0,不在其下是 -1。 */
|
|
72
|
+
function depthOf(ancestor, w) {
|
|
73
|
+
const rel = path.relative(ancestor, w)
|
|
74
|
+
if (rel === '') return 0
|
|
75
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) return -1
|
|
76
|
+
return rel.split(path.sep).length
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 由近到远排出候选工作区。
|
|
81
|
+
* @returns {{workspace:string, scope:'self'|'child'|'sibling'|'outer'}[]}
|
|
82
|
+
*/
|
|
83
|
+
export function expansionOrder(cwd, all) {
|
|
84
|
+
const ws = all.filter((w) => w.count > 0).map((w) => w.workspace)
|
|
85
|
+
const seen = new Set()
|
|
86
|
+
const out = []
|
|
87
|
+
const push = (w, scope) => {
|
|
88
|
+
if (typeof w !== 'string' || w === '' || seen.has(w)) return
|
|
89
|
+
seen.add(w)
|
|
90
|
+
out.push({ workspace: w, scope })
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ① 本工作区这一支:自己第一,其余从最深一层往回收(和平级同一套)
|
|
94
|
+
const self = ws.filter((w) => isUnder(w, cwd))
|
|
95
|
+
push(cwd, 'self')
|
|
96
|
+
self.sort((a, b) => depthOf(cwd, b) - depthOf(cwd, a) || a.localeCompare(b))
|
|
97
|
+
for (const w of self) push(w, 'child')
|
|
98
|
+
|
|
99
|
+
// ② 平级:先探到最深一层,从最深往回收,最后才是平级自己
|
|
100
|
+
const parent = parentOf(cwd)
|
|
101
|
+
const sibs = parent ? ws.filter((w) => parentOf(w) === parent && w !== cwd) : []
|
|
102
|
+
if (sibs.length > 0) {
|
|
103
|
+
const byDepth = new Map()
|
|
104
|
+
for (const s of sibs) {
|
|
105
|
+
for (const w of ws) {
|
|
106
|
+
const d = depthOf(s, w)
|
|
107
|
+
if (d < 1) continue
|
|
108
|
+
if (!byDepth.has(d)) byDepth.set(d, [])
|
|
109
|
+
byDepth.get(d).push(w)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const deepest = byDepth.size > 0 ? Math.max(...byDepth.keys()) : 0
|
|
113
|
+
for (let d = deepest; d >= 1; d--) {
|
|
114
|
+
for (const w of (byDepth.get(d) ?? []).slice().sort()) push(w, 'sibling')
|
|
115
|
+
}
|
|
116
|
+
for (const s of sibs.slice().sort()) push(s, 'sibling')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ③ 再往上:上一级 -> 它的平级 -> 它们的第 1 层子 -> 第 2 层子 …(一层推平再下一层)
|
|
120
|
+
let cur = cwd
|
|
121
|
+
for (;;) {
|
|
122
|
+
const up = parentOf(cur)
|
|
123
|
+
// 到盘根就停:盘根本身不是工作区,往上也没有东西可找
|
|
124
|
+
if (up === null || up === '' || up === path.parse(up).root) break
|
|
125
|
+
push(up, 'outer')
|
|
126
|
+
const grand = parentOf(up)
|
|
127
|
+
const group = grand ? ws.filter((w) => parentOf(w) === grand && w !== up) : []
|
|
128
|
+
for (const g of group.slice().sort()) push(g, 'outer')
|
|
129
|
+
const byDepth = new Map()
|
|
130
|
+
for (const g of group) {
|
|
131
|
+
for (const w of ws) {
|
|
132
|
+
const d = depthOf(g, w)
|
|
133
|
+
if (d < 1) continue
|
|
134
|
+
if (!byDepth.has(d)) byDepth.set(d, [])
|
|
135
|
+
byDepth.get(d).push(w)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const deepest = byDepth.size > 0 ? Math.max(...byDepth.keys()) : 0
|
|
139
|
+
for (let d = 1; d <= deepest; d++) {
|
|
140
|
+
for (const w of (byDepth.get(d) ?? []).slice().sort()) push(w, 'outer')
|
|
141
|
+
}
|
|
142
|
+
cur = up
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 兜底:路径关系排不进来的(比如别的盘符上的工作区),按字母序补在后面
|
|
146
|
+
for (const w of ws.slice().sort()) push(w, 'outer')
|
|
147
|
+
return out
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const PASSES = [
|
|
151
|
+
{ level: 'keywords', take: (p) => readField(p.meta, 'keywords') },
|
|
152
|
+
{ level: 'sum', take: (p) => String((p.sections && p.sections.sum) || '') },
|
|
153
|
+
{ level: 'body', take: (p) => String((p.sections && p.sections.body) || '') }
|
|
154
|
+
]
|
|
155
|
+
const LEVEL_RANK = { keywords: 0, sum: 1, body: 2 }
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 把 need 拆成搜索词:整句一次,另按标点/空白拆开,只留 2 字以上的碎片。
|
|
159
|
+
* 中文没有分词,靠子串命中,所以碎片比整句更有用。
|
|
160
|
+
*/
|
|
161
|
+
export function termsOf(need) {
|
|
162
|
+
const raw = String(need === undefined || need === null ? '' : need).trim()
|
|
163
|
+
if (raw === '') throw new Error('need 不能为空')
|
|
164
|
+
const out = new Set([raw.toLowerCase()])
|
|
165
|
+
for (const part of raw.split(/[\s,,。、;;::!!??()()【】\[\]"'“”‘’·…\-—_/\\|]+/)) {
|
|
166
|
+
const t = part.trim().toLowerCase()
|
|
167
|
+
if (t.length >= 2) out.add(t)
|
|
168
|
+
}
|
|
169
|
+
return [...out]
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 这一份回顾在哪一轮命中:关键词 -> 摘要 -> 原文,先命中的为准。 */
|
|
173
|
+
function bestPass(parsed, terms) {
|
|
174
|
+
for (const pass of PASSES) {
|
|
175
|
+
const text = pass.take(parsed)
|
|
176
|
+
if (text === '') continue
|
|
177
|
+
const hay = text.toLowerCase()
|
|
178
|
+
let matched = 0
|
|
179
|
+
for (const t of terms) if (hay.indexOf(t) >= 0) matched++
|
|
180
|
+
if (matched > 0) return { level: pass.level, matched, text }
|
|
181
|
+
}
|
|
182
|
+
return null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 按新顺序收集候选,一次调用只交给模型一条。
|
|
187
|
+
* @returns {Promise<{candidates:object[], blocked:number}>} blocked = 外圈还有几份(没许可时只数不读)
|
|
188
|
+
*/
|
|
189
|
+
export async function listCandidates({ dshHome, cwd, need, allowOuter = false }) {
|
|
190
|
+
const terms = termsOf(need)
|
|
191
|
+
const all = knownWorkspaces(dshHome)
|
|
192
|
+
const order = expansionOrder(cwd, all)
|
|
193
|
+
const candidates = []
|
|
194
|
+
let blocked = 0
|
|
195
|
+
|
|
196
|
+
for (const item of order) {
|
|
197
|
+
const local = item.scope === 'self' || item.scope === 'child'
|
|
198
|
+
const dir = memoryDir(dshHome, item.workspace)
|
|
199
|
+
const data = await readAssembly(dir, item.workspace)
|
|
200
|
+
if (!data.entries || data.entries.length === 0) continue
|
|
201
|
+
// 没许可:外圈只数有几份,不读文件、不给内容
|
|
202
|
+
if (!local && !allowOuter) {
|
|
203
|
+
blocked += data.entries.length
|
|
204
|
+
continue
|
|
205
|
+
}
|
|
206
|
+
const parsedList = []
|
|
207
|
+
for (const entry of data.entries) {
|
|
208
|
+
if (!entry || typeof entry.file !== 'string') continue
|
|
209
|
+
let text = ''
|
|
210
|
+
try { text = await fs.promises.readFile(path.join(dir, entry.file), 'utf8') } catch { continue }
|
|
211
|
+
parsedList.push({ entry, parsed: parseMemory(text) })
|
|
212
|
+
}
|
|
213
|
+
const hits = []
|
|
214
|
+
for (const it of parsedList) {
|
|
215
|
+
const m = bestPass(it.parsed, terms)
|
|
216
|
+
if (m === null) continue
|
|
217
|
+
hits.push({
|
|
218
|
+
file: it.entry.file,
|
|
219
|
+
workspace: item.workspace,
|
|
220
|
+
scope: item.scope,
|
|
221
|
+
local: item.scope === 'self',
|
|
222
|
+
compactedAt: it.entry.compactedAt,
|
|
223
|
+
level: m.level,
|
|
224
|
+
matched: m.matched,
|
|
225
|
+
content: m.text
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
hits.sort((a, b) =>
|
|
229
|
+
(LEVEL_RANK[a.level] - LEVEL_RANK[b.level]) ||
|
|
230
|
+
(b.matched - a.matched) ||
|
|
231
|
+
String(b.compactedAt).localeCompare(String(a.compactedAt)))
|
|
232
|
+
candidates.push(...hits)
|
|
233
|
+
}
|
|
234
|
+
return { candidates, blocked }
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 本工作区命中才改深度:0 -> 1 并锁定;1 -> 2 并锁定;2 或 3 不动。 */
|
|
238
|
+
export async function bumpHits(dir, workspace, files, now) {
|
|
239
|
+
const want = new Set(files)
|
|
240
|
+
let changed = false
|
|
241
|
+
for (const file of want) {
|
|
242
|
+
// 只读这一份、只写这一份:没有共享清单,也就没有需要抢的锁。
|
|
243
|
+
const data = await readAssembly(dir, workspace)
|
|
244
|
+
const entry = data.entries.find((e) => e.file === file)
|
|
245
|
+
if (!entry) continue
|
|
246
|
+
const before = effectiveCount(entry, now)
|
|
247
|
+
if (before === 0) { entry.count = 1; entry.pinned = true }
|
|
248
|
+
else if (before === 1) { entry.count = 2; entry.pinned = true }
|
|
249
|
+
else continue
|
|
250
|
+
await writeEntry(dir, workspace, entry)
|
|
251
|
+
changed = true
|
|
252
|
+
}
|
|
253
|
+
return changed
|
|
254
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 选项:设置页「选项」标签里那几个开关。
|
|
3
|
+
*
|
|
4
|
+
* 存在 <dshHome>/session-memory/_options.json,与回顾同放在记忆根下,一个文件、
|
|
5
|
+
* 全部工作区共用。注入路径每一步都要读它,而 systemPrompt.context() 的 text 是
|
|
6
|
+
* 同步求值的,所以读用 readFileSync;写只来自设置页,用原子写。
|
|
7
|
+
*
|
|
8
|
+
* 七个键:
|
|
9
|
+
* autoReviewEnabled 自动回顾(0.3.10):总闸。开着才检测压缩、才建回顾文件、才注入;
|
|
10
|
+
* 默认关。关着时下面的 injectEnabled 一律按关算(不写盘、不动已存的值)。
|
|
11
|
+
* injectEnabled 回顾注入:总开关(0.2.8)。0.3.1 起只管回顾正文与日记提醒(B、C 两段);
|
|
12
|
+
* 工具组清单(A 段)由 toolListEnabled 单独管。关掉后其余四项只变灰不可改,
|
|
13
|
+
* 已存的值原样保留(不还原默认)
|
|
14
|
+
* toolListEnabled 注入工具组清单(0.3.1):只管 session-recall:toolSequence 那一段,默认开
|
|
15
|
+
* toolListRefresh 清单词刷新时刻(0.4.5):'newSession' 新会话开始之前(默认)/ 'realtime' 会话内实时刷新。
|
|
16
|
+
* 只管工具组那一段什么时候重算:newSession 在会话首次装配时算一次就固定,
|
|
17
|
+
* 会话中途装/删工具脚本不改系统提示词(否则整份会话会被当未缓存输入重发);
|
|
18
|
+
* fullLoad 全量加载:深度 3 不再过滤工具内容(载入框与真正注入一起变)
|
|
19
|
+
* reminderChannel 概括提醒:context = 并进回顾正文(0.1.46 的做法)/ system = 系统提示词段
|
|
20
|
+
* stopLowering 停止降低:所有深度停在载入文件里记下的那个数,不再按自然日衰减
|
|
21
|
+
* loadLevel 载入数值:'follow' 跟随深度,或 3/2/1/0 强制这一级(只改载入)
|
|
22
|
+
*
|
|
23
|
+
* 两条规矩:
|
|
24
|
+
* 1. 停止降低开启时,载入数值没有意义(都停在记下的深度),一律退回 'follow'。
|
|
25
|
+
* 2. 自动回顾关着时,回顾注入没有意义(没有回顾可注入),一律按关算——
|
|
26
|
+
* 这只发生在「读出来给别处用」那一层(readOptionsSync / writeOptions 的返回值),
|
|
27
|
+
* **不写进文件**:用户重新打开它时,原来那个「回顾注入」开关还在原位。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import fs from 'node:fs'
|
|
31
|
+
import fsPromises from 'node:fs/promises'
|
|
32
|
+
import path from 'node:path'
|
|
33
|
+
import { writeFileAtomic } from './store.js'
|
|
34
|
+
|
|
35
|
+
/** 选项文件名(位于记忆根下,不在任何工作区目录里)。 */
|
|
36
|
+
export const OPTIONS_FILE = '_options.json'
|
|
37
|
+
|
|
38
|
+
/** 载入数值的「跟随深度」档。 */
|
|
39
|
+
export const FOLLOW_DEPTH = 'follow'
|
|
40
|
+
|
|
41
|
+
/** 概括提醒的两个通道。 */
|
|
42
|
+
export const REMINDER_CONTEXT = 'context'
|
|
43
|
+
export const REMINDER_SYSTEM = 'system'
|
|
44
|
+
|
|
45
|
+
/** 工具组清单什么时候重算(0.4.5)。 */
|
|
46
|
+
export const REFRESH_NEW_SESSION = 'newSession'
|
|
47
|
+
export const REFRESH_REALTIME = 'realtime'
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 落盘默认值:自动回顾关(0.3.10,按用户要求默认关)、注入开、工具组清单开、
|
|
51
|
+
* 全量关、提醒在上下文(0.1.46)、停止降低关、载入跟随深度。
|
|
52
|
+
*
|
|
53
|
+
* 「注入开」是**存储值**的默认,不是默认生效值:自动回顾关着时读出来的 injectEnabled 是 false。
|
|
54
|
+
*/
|
|
55
|
+
export const DEFAULT_OPTIONS = Object.freeze({
|
|
56
|
+
autoReviewEnabled: false,
|
|
57
|
+
injectEnabled: true,
|
|
58
|
+
toolListEnabled: true,
|
|
59
|
+
toolListRefresh: REFRESH_NEW_SESSION,
|
|
60
|
+
fullLoad: false,
|
|
61
|
+
reminderChannel: REMINDER_CONTEXT,
|
|
62
|
+
stopLowering: false,
|
|
63
|
+
loadLevel: FOLLOW_DEPTH
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 选项文件路径。
|
|
68
|
+
* @param {string} dshHome
|
|
69
|
+
* @returns {string}
|
|
70
|
+
*/
|
|
71
|
+
export function optionsPath(dshHome) {
|
|
72
|
+
return path.join(String(dshHome), 'session-memory', OPTIONS_FILE)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 把任意输入收成一份合法的**存储**选项。坏值一律回落默认,不抛错。
|
|
77
|
+
*
|
|
78
|
+
* 只做逐键收紧,不叠「自动回顾关 ⇒ 注入关」那条连带后果:那是读出来用的时候算的
|
|
79
|
+
* (applyAutoReview),否则一次保存就会把用户存的「回顾注入」状态抹成关。
|
|
80
|
+
* @param {object|undefined|null} raw
|
|
81
|
+
* @returns {{autoReviewEnabled:boolean, injectEnabled:boolean, toolListEnabled:boolean, fullLoad:boolean, reminderChannel:'context'|'system', stopLowering:boolean, loadLevel:'follow'|0|1|2|3}}
|
|
82
|
+
*/
|
|
83
|
+
export function normalizeOptions(raw) {
|
|
84
|
+
const src = raw !== null && typeof raw === 'object' ? raw : {}
|
|
85
|
+
// 只有明确写 true 才算开:缺键(老选项文件)与坏值都按「自动回顾默认关」走。
|
|
86
|
+
const autoReviewEnabled = src.autoReviewEnabled === true
|
|
87
|
+
// 只有明确写 false 才算关;缺键、坏值都保持默认开(老选项文件没有这个键)。
|
|
88
|
+
const injectEnabled = src.injectEnabled !== false
|
|
89
|
+
const stopLowering = src.stopLowering === true
|
|
90
|
+
const rawLevel = src.loadLevel
|
|
91
|
+
const num = typeof rawLevel === 'number'
|
|
92
|
+
? rawLevel
|
|
93
|
+
: (typeof rawLevel === 'string' && /^[0-3]$/.test(rawLevel.trim()) ? Number(rawLevel.trim()) : NaN)
|
|
94
|
+
let loadLevel = Number.isInteger(num) && num >= 0 && num <= 3 ? num : FOLLOW_DEPTH
|
|
95
|
+
// 停止降低时「载入数值」禁用:都停在记下的深度,强制某一级没有意义,按规矩退回跟随。
|
|
96
|
+
// 注意:这不套用到「回顾注入」关闭上——关总开关只禁用设置,不动已存的值。
|
|
97
|
+
if (stopLowering) loadLevel = FOLLOW_DEPTH
|
|
98
|
+
return {
|
|
99
|
+
autoReviewEnabled,
|
|
100
|
+
injectEnabled,
|
|
101
|
+
toolListEnabled: src.toolListEnabled !== false,
|
|
102
|
+
// 0.4.5:只有明确写 realtime 才实时刷新;缺键(老选项文件)、坏值都按默认「新会话开始之前」。
|
|
103
|
+
toolListRefresh: src.toolListRefresh === REFRESH_REALTIME ? REFRESH_REALTIME : REFRESH_NEW_SESSION,
|
|
104
|
+
fullLoad: src.fullLoad === true,
|
|
105
|
+
reminderChannel: src.reminderChannel === REMINDER_SYSTEM ? REMINDER_SYSTEM : REMINDER_CONTEXT,
|
|
106
|
+
stopLowering,
|
|
107
|
+
loadLevel
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 叠「自动回顾」的连带后果:它一关,回顾注入一律按关算。
|
|
113
|
+
* 只改返回值,不改文件;自动回顾打开时存储值原样返回。
|
|
114
|
+
* @param {object} stored normalizeOptions 的结果
|
|
115
|
+
* @returns {object}
|
|
116
|
+
*/
|
|
117
|
+
export function applyAutoReview(stored) {
|
|
118
|
+
const base = stored !== null && typeof stored === 'object' ? stored : { ...DEFAULT_OPTIONS }
|
|
119
|
+
if (base.autoReviewEnabled === true) return base
|
|
120
|
+
return { ...base, injectEnabled: false }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 读**存储**值(不做自动回顾连带收紧);坏文件、缺 dshHome 都给默认。 */
|
|
124
|
+
function readStoredSync(dshHome) {
|
|
125
|
+
if (typeof dshHome !== 'string' || dshHome.trim() === '') return { ...DEFAULT_OPTIONS }
|
|
126
|
+
let raw
|
|
127
|
+
try {
|
|
128
|
+
raw = JSON.parse(fs.readFileSync(optionsPath(dshHome), 'utf8'))
|
|
129
|
+
} catch {
|
|
130
|
+
return { ...DEFAULT_OPTIONS }
|
|
131
|
+
}
|
|
132
|
+
return normalizeOptions(raw)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 读选项(**生效值**)。文件不存在、坏了、没有 dshHome 都当默认值,绝不抛错
|
|
137
|
+
* (注入路径每一步都过这里)。自动回顾关着时 injectEnabled 一律是 false。
|
|
138
|
+
* @param {string} dshHome
|
|
139
|
+
* @returns {object}
|
|
140
|
+
*/
|
|
141
|
+
export function readOptionsSync(dshHome) {
|
|
142
|
+
return applyAutoReview(readStoredSync(dshHome))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 改选项:拿现有**存储**值合并补丁,收紧后原子落盘;返回生效值给设置页显示。
|
|
147
|
+
* @param {string} dshHome
|
|
148
|
+
* @param {object} patch 只写要改的键
|
|
149
|
+
* @returns {Promise<object>} 落盘后的生效选项
|
|
150
|
+
*/
|
|
151
|
+
export async function writeOptions(dshHome, patch) {
|
|
152
|
+
if (typeof dshHome !== 'string' || dshHome.trim() === '') throw new Error('没有 dshHome,选项写不进去')
|
|
153
|
+
const merged = { ...readStoredSync(dshHome), ...(patch !== null && typeof patch === 'object' ? patch : {}) }
|
|
154
|
+
const next = normalizeOptions(merged)
|
|
155
|
+
await fsPromises.mkdir(path.dirname(optionsPath(dshHome)), { recursive: true })
|
|
156
|
+
await writeFileAtomic(optionsPath(dshHome), JSON.stringify(next, null, 2) + '\n')
|
|
157
|
+
return applyAutoReview(next)
|
|
158
|
+
}
|