dsh-chief 1.2.0 → 1.3.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 +51 -0
- package/README.en.md +28 -13
- package/README.md +27 -12
- package/api-routes.js +1274 -0
- package/archive-cabinets.js +766 -0
- package/archive-core.js +761 -0
- package/archive-dossiers.js +1185 -0
- package/archive-precipitated.js +428 -0
- package/archives.js +63 -2991
- package/auto-continue.js +11 -1
- package/board-actions.js +1879 -0
- package/board-schema.js +580 -0
- package/board-store.js +392 -0
- package/chief-approvals.js +841 -0
- package/chief-archives.js +905 -0
- package/chief-commands.js +2513 -0
- package/chief-compaction.js +591 -0
- package/chief-executor.js +859 -153
- package/chief-models.js +578 -0
- package/chief-network.js +720 -0
- package/chief-review.js +1064 -0
- package/chief-state.js +1777 -0
- package/chief-view.js +553 -0
- package/chief.js +94 -7492
- package/client.js +11959 -10923
- package/digest.js +8 -8
- package/index.js +154 -6095
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/model-tools.js +755 -0
- package/package.json +48 -17
- package/runtime-hooks.js +629 -0
- package/subagent-runtime.js +1399 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* archive-precipitated.js
|
|
3
|
+
*
|
|
4
|
+
* Double-blind precipitated cold vault storage, flip-out gating, and lock-safe vault promotion.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs'
|
|
8
|
+
import path from 'node:path'
|
|
9
|
+
import {
|
|
10
|
+
POINTS_CAP,
|
|
11
|
+
PRECIPITATED_CAP,
|
|
12
|
+
SECRET_PATTERN,
|
|
13
|
+
nextId,
|
|
14
|
+
str,
|
|
15
|
+
clamp,
|
|
16
|
+
cleanMultiline,
|
|
17
|
+
resolveProjectKey,
|
|
18
|
+
normalizeVaultScope,
|
|
19
|
+
cleanRootSessionId,
|
|
20
|
+
resolveTargetOpts,
|
|
21
|
+
globalPrecipitatedFilePath,
|
|
22
|
+
acquireFileLock,
|
|
23
|
+
atomicWriteJson,
|
|
24
|
+
readJsonSafe,
|
|
25
|
+
normalizePoint,
|
|
26
|
+
normalizeTags,
|
|
27
|
+
firstMeaningfulSentence,
|
|
28
|
+
extractStructuredPoints,
|
|
29
|
+
invokeArchiveMutatorHook,
|
|
30
|
+
} from './archive-core.js'
|
|
31
|
+
import { loadGlobalVaultRaw, loadArchiveStore } from './archive-cabinets.js'
|
|
32
|
+
|
|
33
|
+
export function hasAnyPrecipitatedFilesOnDisk(targetOpt) {
|
|
34
|
+
const globalFile = globalPrecipitatedFilePath(targetOpt)
|
|
35
|
+
if (fs.existsSync(globalFile)) return true
|
|
36
|
+
const dir = path.dirname(globalFile)
|
|
37
|
+
try {
|
|
38
|
+
if (!fs.existsSync(dir)) return false
|
|
39
|
+
return fs.readdirSync(dir).some(name => name.endsWith('.json'))
|
|
40
|
+
} catch {
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function normalizePrecipitatedEntry(raw) {
|
|
46
|
+
if (!raw || typeof raw !== 'object') return null
|
|
47
|
+
const title = clamp(raw.title || '', 180)
|
|
48
|
+
const rawBody = cleanMultiline(raw.rawBody || raw.body || '', 30000)
|
|
49
|
+
if (!title && !rawBody) return null
|
|
50
|
+
const points = (Array.isArray(raw.points) ? raw.points : [])
|
|
51
|
+
.map((p, idx) => normalizePoint(p, idx))
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.slice(0, POINTS_CAP)
|
|
54
|
+
const validCategories = ['coords', 'incidents', 'contracts', 'ops', 'plans', 'architecture', 'env', 'contract', 'troubleshooting', 'workflow', 'general']
|
|
55
|
+
const category = validCategories.includes(raw.category) ? raw.category : 'general'
|
|
56
|
+
const flippedOutSessions = Array.isArray(raw.flippedOutSessions)
|
|
57
|
+
? [...new Set(raw.flippedOutSessions.map(s => str(s, '').trim()).filter(Boolean))].slice(0, 32)
|
|
58
|
+
: []
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
id: str(raw.id, '').trim() || nextId('prec'),
|
|
62
|
+
title: title || '跨会话可复用工程经验',
|
|
63
|
+
category,
|
|
64
|
+
summary: clamp(raw.summary || (points[0] && points[0].cue) || firstMeaningfulSentence(rawBody) || title, 180),
|
|
65
|
+
points: points.length ? points : extractStructuredPoints(title, rawBody),
|
|
66
|
+
rawBody,
|
|
67
|
+
purified: raw.purified !== false,
|
|
68
|
+
sourceCabinetId: str(raw.sourceCabinetId || raw.sourceCabinet, '').trim(),
|
|
69
|
+
sourceDrawer: str(raw.sourceDrawer, '').trim(),
|
|
70
|
+
sourceSession: str(raw.sourceSession, '').trim(),
|
|
71
|
+
sourceTitles: Array.isArray(raw.sourceTitles) ? raw.sourceTitles.map(t => clamp(t, 80)).slice(0, 40) : [],
|
|
72
|
+
tags: normalizeTags(Array.isArray(raw.tags) ? raw.tags : []),
|
|
73
|
+
flippedOutSessions,
|
|
74
|
+
by: clamp(raw.by || 'chief', 40),
|
|
75
|
+
createdAt: Number(raw.createdAt) || Date.now(),
|
|
76
|
+
updatedAt: Number(raw.updatedAt) || Number(raw.createdAt) || Date.now(),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** One-time lossless merge from legacy per-project `precipitated/*.json` files into unified `precipitated/vault-global.json`. */
|
|
81
|
+
function ensureGlobalPrecipitatedVault(targetOpt) {
|
|
82
|
+
const globalFile = globalPrecipitatedFilePath(targetOpt)
|
|
83
|
+
if (fs.existsSync(globalFile)) return globalFile
|
|
84
|
+
const dir = path.dirname(globalFile)
|
|
85
|
+
if (!fs.existsSync(dir)) return globalFile
|
|
86
|
+
let legacyFiles = []
|
|
87
|
+
try {
|
|
88
|
+
legacyFiles = fs.readdirSync(dir)
|
|
89
|
+
.filter(name => name.endsWith('.json') && name !== 'vault-global.json')
|
|
90
|
+
.map(name => path.join(dir, name))
|
|
91
|
+
} catch {
|
|
92
|
+
legacyFiles = []
|
|
93
|
+
}
|
|
94
|
+
if (!legacyFiles.length) return globalFile
|
|
95
|
+
|
|
96
|
+
const merged = []
|
|
97
|
+
const seenIds = new Set()
|
|
98
|
+
const seenKeys = new Set()
|
|
99
|
+
for (const lf of legacyFiles) {
|
|
100
|
+
const parsed = readJsonSafe(lf, null)
|
|
101
|
+
const rawItems = Array.isArray(parsed && parsed.items) ? parsed.items : []
|
|
102
|
+
for (const rawItem of rawItems) {
|
|
103
|
+
const norm = normalizePrecipitatedEntry(rawItem)
|
|
104
|
+
if (!norm) continue
|
|
105
|
+
const dedupeKey = `${norm.title}::${norm.createdAt || 0}`
|
|
106
|
+
if (seenIds.has(norm.id) || seenKeys.has(dedupeKey)) continue
|
|
107
|
+
seenIds.add(norm.id)
|
|
108
|
+
seenKeys.add(dedupeKey)
|
|
109
|
+
merged.push(norm)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!merged.length) return globalFile
|
|
113
|
+
merged.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0))
|
|
114
|
+
atomicWriteJson(globalFile, {
|
|
115
|
+
version: 1,
|
|
116
|
+
projectKey: 'global',
|
|
117
|
+
updatedAt: Date.now(),
|
|
118
|
+
items: merged.slice(0, PRECIPITATED_CAP),
|
|
119
|
+
})
|
|
120
|
+
return globalFile
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function loadPrecipitatedStoreRaw(_projectKey = 'proj-default', targetOpt) {
|
|
124
|
+
const file = ensureGlobalPrecipitatedVault(targetOpt)
|
|
125
|
+
if (!fs.existsSync(file)) {
|
|
126
|
+
return {
|
|
127
|
+
version: 1,
|
|
128
|
+
projectKey: 'global',
|
|
129
|
+
updatedAt: 0,
|
|
130
|
+
items: [],
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const parsed = readJsonSafe(file, null)
|
|
134
|
+
const items = (Array.isArray(parsed && parsed.items) ? parsed.items : [])
|
|
135
|
+
.map(normalizePrecipitatedEntry)
|
|
136
|
+
.filter(Boolean)
|
|
137
|
+
.slice(0, PRECIPITATED_CAP)
|
|
138
|
+
return {
|
|
139
|
+
version: 1,
|
|
140
|
+
projectKey: 'global',
|
|
141
|
+
updatedAt: Number(parsed && parsed.updatedAt) || Date.now(),
|
|
142
|
+
items,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function savePrecipitatedStoreRaw(_projectKey = 'proj-default', store, targetOpt) {
|
|
147
|
+
const file = globalPrecipitatedFilePath(targetOpt)
|
|
148
|
+
const normalized = {
|
|
149
|
+
version: 1,
|
|
150
|
+
projectKey: 'global',
|
|
151
|
+
updatedAt: Date.now(),
|
|
152
|
+
items: (Array.isArray(store && store.items) ? store.items : [])
|
|
153
|
+
.map(normalizePrecipitatedEntry)
|
|
154
|
+
.filter(Boolean)
|
|
155
|
+
.slice(0, PRECIPITATED_CAP),
|
|
156
|
+
}
|
|
157
|
+
atomicWriteJson(file, normalized)
|
|
158
|
+
return normalized
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* STRICT 0-TOKEN GUARD:
|
|
163
|
+
* Unless `hasExplicitFlipOutFlag === true` (tracked independently per session in `chief-state.json`
|
|
164
|
+
* `ws.flippedPrecipitatedIds`), this function returns `[]` IMMEDIATELY without touching disk at all!
|
|
165
|
+
*/
|
|
166
|
+
export function getFlippedOutPrecipitatedForSession(projectKey = 'proj-default', rootSessionId = '', hasExplicitFlipOutFlag = false, targetOpt) {
|
|
167
|
+
if (!hasExplicitFlipOutFlag || !str(rootSessionId, '').trim()) {
|
|
168
|
+
return []
|
|
169
|
+
}
|
|
170
|
+
const sid = str(rootSessionId, '').trim()
|
|
171
|
+
const store = loadPrecipitatedStoreRaw(projectKey, targetOpt)
|
|
172
|
+
return store.items.filter(it => Array.isArray(it.flippedOutSessions) && it.flippedOutSessions.includes(sid))
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Losslessly copy a precipitated cold-vault entry into `session` or `global` vault WITHOUT deleting the precipitated item.
|
|
177
|
+
* Reads & releases precipitated lock BEFORE calling `mutateArchive` to strictly prevent lock-order inversion.
|
|
178
|
+
*/
|
|
179
|
+
export function copyPrecipitatedToVault(projectKey = 'proj-default', payload = {}, targetOpt) {
|
|
180
|
+
const pid = str(payload.id || payload.precipitatedId, '').trim()
|
|
181
|
+
if (!pid) return { ok: false, error: 'missing precipitated id' }
|
|
182
|
+
const targetScope = normalizeVaultScope(payload.targetScope || payload.toScope || 'session') === 'global' ? 'global' : 'session'
|
|
183
|
+
const rootSid = cleanRootSessionId(payload.rootSessionId || payload.session || (targetOpt && targetOpt.rootSessionId))
|
|
184
|
+
if (targetScope === 'session' && !rootSid) {
|
|
185
|
+
return { ok: false, error: '当前为空白新对话,请先发送一条消息建立会话后再调入本局档案库' }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const base = resolveTargetOpts(targetOpt)
|
|
189
|
+
const file = globalPrecipitatedFilePath(base)
|
|
190
|
+
const release = acquireFileLock(file)
|
|
191
|
+
let snapshot = null
|
|
192
|
+
try {
|
|
193
|
+
const pStore = loadPrecipitatedStoreRaw(projectKey, base)
|
|
194
|
+
const hit = pStore.items.find(it => it.id === pid)
|
|
195
|
+
if (!hit) return { ok: false, error: '未找到沉淀条目:' + pid }
|
|
196
|
+
snapshot = {
|
|
197
|
+
id: hit.id,
|
|
198
|
+
title: hit.title,
|
|
199
|
+
category: hit.category || '经验',
|
|
200
|
+
summary: hit.summary || '',
|
|
201
|
+
points: Array.isArray(hit.points) ? hit.points.map(p => ({ ...p })) : [],
|
|
202
|
+
body: hit.body || '',
|
|
203
|
+
tags: Array.isArray(hit.tags) ? hit.tags.slice() : [],
|
|
204
|
+
}
|
|
205
|
+
} finally {
|
|
206
|
+
release()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const catFolderMap = {
|
|
210
|
+
contracts: 'contracts',
|
|
211
|
+
architecture: 'architecture',
|
|
212
|
+
patterns: 'ops',
|
|
213
|
+
pitfalls: 'pitfalls',
|
|
214
|
+
toolchain: 'ops',
|
|
215
|
+
}
|
|
216
|
+
const folder = catFolderMap[snapshot.category] || 'ops'
|
|
217
|
+
const pointText = snapshot.points.map(pt => `- ${pt.title}:${pt.detail || pt.cue || ''}`).join('\n')
|
|
218
|
+
const fullBody = [
|
|
219
|
+
snapshot.body || snapshot.summary,
|
|
220
|
+
pointText ? '\n### 精炼要点\n' + pointText : '',
|
|
221
|
+
].filter(Boolean).join('\n')
|
|
222
|
+
|
|
223
|
+
const saveRes = invokeArchiveMutatorHook({
|
|
224
|
+
op: 'save',
|
|
225
|
+
vaultScope: targetScope,
|
|
226
|
+
rootSessionId: rootSid,
|
|
227
|
+
folder,
|
|
228
|
+
paperclip: 'silver',
|
|
229
|
+
title: snapshot.title,
|
|
230
|
+
summary: snapshot.summary,
|
|
231
|
+
points: snapshot.points,
|
|
232
|
+
body: fullBody || snapshot.title,
|
|
233
|
+
tags: [...new Set(['沉淀调入', snapshot.category, ...snapshot.tags].filter(Boolean))],
|
|
234
|
+
by: 'user',
|
|
235
|
+
takeOutNow: Boolean(payload.takeOutNow && targetScope === 'session'),
|
|
236
|
+
}, {
|
|
237
|
+
...base,
|
|
238
|
+
vaultScope: targetScope,
|
|
239
|
+
rootSessionId: targetScope === 'session' ? rootSid : '',
|
|
240
|
+
cabinetId: '',
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
if (!saveRes || !saveRes.ok) return saveRes || { ok: false, error: '调入目标档案库失败' }
|
|
244
|
+
return {
|
|
245
|
+
...saveRes,
|
|
246
|
+
copiedFromPrecipitatedId: snapshot.id,
|
|
247
|
+
targetScope,
|
|
248
|
+
lossless: true,
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Listed ONLY for Chief background sealed-archive watch (never injected into Lead prompt unless placed on desk). */
|
|
253
|
+
export function collectSealedWatchSummary(rootSessionId = '', projectKey = 'proj-default', targetOpt, opts = {}) {
|
|
254
|
+
const sid = cleanRootSessionId(rootSessionId)
|
|
255
|
+
const base = resolveTargetOpts(targetOpt)
|
|
256
|
+
const capPerVault = Math.max(2, Math.min(Number(opts.limitPerVault) || 8, 16))
|
|
257
|
+
|
|
258
|
+
let sessionSealed = []
|
|
259
|
+
let globalSealed = []
|
|
260
|
+
let precipSealed = []
|
|
261
|
+
|
|
262
|
+
if (sid) {
|
|
263
|
+
try {
|
|
264
|
+
const sStore = loadArchiveStore({
|
|
265
|
+
customSharedBoardFile: base.customSharedBoardFile,
|
|
266
|
+
cwd: base.cwd,
|
|
267
|
+
projectKey: base.projectKey,
|
|
268
|
+
cabinetId: base.cabinetId,
|
|
269
|
+
rootSessionId: sid,
|
|
270
|
+
vaultScope: 'session',
|
|
271
|
+
})
|
|
272
|
+
if (!sStore.unboundSession) {
|
|
273
|
+
sessionSealed = (sStore.items || [])
|
|
274
|
+
.filter(it => !Array.isArray(it.takenOutSessions) || !it.takenOutSessions.includes(sid))
|
|
275
|
+
.slice(0, capPerVault)
|
|
276
|
+
.map(it => ({ id: it.id, scope: 'session', title: it.title, folder: it.folder, summary: it.summary }))
|
|
277
|
+
}
|
|
278
|
+
} catch {}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
const gStore = loadGlobalVaultRaw(base)
|
|
283
|
+
globalSealed = (gStore.items || [])
|
|
284
|
+
.filter(it => !sid || !Array.isArray(it.takenOutSessions) || !it.takenOutSessions.includes(sid))
|
|
285
|
+
.slice(0, capPerVault)
|
|
286
|
+
.map(it => ({ id: it.id, scope: 'global', title: it.title, folder: it.folder, summary: it.summary, chiefMounted: Boolean(sid && Array.isArray(it.chiefMountedSessions) && it.chiefMountedSessions.includes(sid)) }))
|
|
287
|
+
} catch {}
|
|
288
|
+
|
|
289
|
+
if (hasAnyPrecipitatedFilesOnDisk(base)) {
|
|
290
|
+
try {
|
|
291
|
+
const pStore = loadPrecipitatedStoreRaw(projectKey || base.projectKey || 'proj-default', base)
|
|
292
|
+
precipSealed = (pStore.items || [])
|
|
293
|
+
.filter(it => !sid || !Array.isArray(it.flippedOutSessions) || !it.flippedOutSessions.includes(sid))
|
|
294
|
+
.slice(0, capPerVault)
|
|
295
|
+
.map(it => ({ id: it.id, scope: 'precipitated', title: it.title, category: it.category, summary: it.summary }))
|
|
296
|
+
} catch {}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return { sessionSealed, globalSealed, precipSealed }
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Listed ONLY when user opens the `档案` tab in Web UI (`GET /api/dsh-chief/archives`). */
|
|
303
|
+
export function listPrecipitatedHeaders(projectKey = 'proj-default', rootSessionId = '', targetOpt) {
|
|
304
|
+
const file = ensureGlobalPrecipitatedVault(targetOpt)
|
|
305
|
+
if (!fs.existsSync(file)) return []
|
|
306
|
+
const sid = str(rootSessionId, '').trim()
|
|
307
|
+
const store = loadPrecipitatedStoreRaw(projectKey, targetOpt)
|
|
308
|
+
return store.items.map(it => ({
|
|
309
|
+
id: it.id,
|
|
310
|
+
title: it.title,
|
|
311
|
+
category: it.category,
|
|
312
|
+
summary: it.summary,
|
|
313
|
+
purified: it.purified,
|
|
314
|
+
sourceCabinetId: it.sourceCabinetId,
|
|
315
|
+
sourceDrawer: it.sourceDrawer,
|
|
316
|
+
sourceTitles: it.sourceTitles,
|
|
317
|
+
tags: it.tags,
|
|
318
|
+
pointCount: Array.isArray(it.points) ? it.points.length : 0,
|
|
319
|
+
flippedOut: Boolean(sid && Array.isArray(it.flippedOutSessions) && it.flippedOutSessions.includes(sid)),
|
|
320
|
+
points: (it.points || []).map(p => ({ id: p.id, title: p.title, cue: p.cue, detail: p.detail })),
|
|
321
|
+
createdAt: it.createdAt,
|
|
322
|
+
updatedAt: it.updatedAt,
|
|
323
|
+
}))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function getPrecipitatedDetail(projectKey = 'proj-default', id = '', rootSessionId = '', targetOpt) {
|
|
327
|
+
const target = str(id, '').trim()
|
|
328
|
+
if (!target) return { ok: false, error: 'missing precipitated id' }
|
|
329
|
+
const store = loadPrecipitatedStoreRaw(projectKey, targetOpt)
|
|
330
|
+
const hit = store.items.find(it => it.id === target)
|
|
331
|
+
if (!hit) return { ok: false, error: '未找到沉淀经验条目:' + target }
|
|
332
|
+
const sid = str(rootSessionId, '').trim()
|
|
333
|
+
return {
|
|
334
|
+
ok: true,
|
|
335
|
+
item: {
|
|
336
|
+
...hit,
|
|
337
|
+
flippedOut: Boolean(sid && hit.flippedOutSessions.includes(sid)),
|
|
338
|
+
},
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function savePrecipitatedEntry(projectKey = 'proj-default', entry = {}, targetOpt) {
|
|
343
|
+
const resolvedKey = resolveProjectKey(projectKey)
|
|
344
|
+
const file = globalPrecipitatedFilePath(targetOpt)
|
|
345
|
+
const release = acquireFileLock(file)
|
|
346
|
+
try {
|
|
347
|
+
const store = loadPrecipitatedStoreRaw(resolvedKey, targetOpt)
|
|
348
|
+
const bodyCheck = cleanMultiline(entry.rawBody || entry.body || '', 30000)
|
|
349
|
+
if (SECRET_PATTERN.test(bodyCheck)) {
|
|
350
|
+
return { ok: false, error: '沉淀内容疑似包含敏感密钥或私密凭据,已拒绝写入冷库' }
|
|
351
|
+
}
|
|
352
|
+
const existing = entry.id ? store.items.find(it => it.id === entry.id) : null
|
|
353
|
+
if (existing) {
|
|
354
|
+
const updated = normalizePrecipitatedEntry({ ...existing, ...entry, id: existing.id, updatedAt: Date.now() })
|
|
355
|
+
Object.assign(existing, updated)
|
|
356
|
+
savePrecipitatedStoreRaw(resolvedKey, store, targetOpt)
|
|
357
|
+
return { ok: true, item: existing, entry: existing, file }
|
|
358
|
+
}
|
|
359
|
+
const created = normalizePrecipitatedEntry({
|
|
360
|
+
...entry,
|
|
361
|
+
id: str(entry.id, '').trim() || nextId('prec'),
|
|
362
|
+
flippedOutSessions: [], // Strictly cold-isolated by default!
|
|
363
|
+
createdAt: Date.now(),
|
|
364
|
+
updatedAt: Date.now(),
|
|
365
|
+
})
|
|
366
|
+
if (!created) return { ok: false, error: '无法写入空的沉淀经验' }
|
|
367
|
+
store.items.unshift(created)
|
|
368
|
+
savePrecipitatedStoreRaw(resolvedKey, store, targetOpt)
|
|
369
|
+
return { ok: true, item: created, entry: created, file }
|
|
370
|
+
} finally {
|
|
371
|
+
release()
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function mutatePrecipitated(projectKey = 'proj-default', payload = {}, targetOpt) {
|
|
376
|
+
const resolvedKey = resolveProjectKey(projectKey || payload.projectKey || payload.cwd || 'proj-default')
|
|
377
|
+
const file = globalPrecipitatedFilePath(targetOpt)
|
|
378
|
+
const op = str(payload.op, '').trim()
|
|
379
|
+
const sid = str(payload.rootSessionId || payload.session, '').trim()
|
|
380
|
+
const id = str(payload.precipitatedId || payload.id, '').trim()
|
|
381
|
+
|
|
382
|
+
const release = acquireFileLock(file)
|
|
383
|
+
try {
|
|
384
|
+
const store = loadPrecipitatedStoreRaw(resolvedKey, targetOpt)
|
|
385
|
+
|
|
386
|
+
if (op === 'flipOut' || op === 'sealBack') {
|
|
387
|
+
if (!sid) return { ok: false, error: '当前未绑定对话会话' }
|
|
388
|
+
const item = store.items.find(it => it.id === id)
|
|
389
|
+
if (!item) return { ok: false, error: '未找到沉淀经验:' + id }
|
|
390
|
+
const set = new Set(item.flippedOutSessions || [])
|
|
391
|
+
if (op === 'flipOut') set.add(sid)
|
|
392
|
+
else set.delete(sid)
|
|
393
|
+
item.flippedOutSessions = [...set]
|
|
394
|
+
item.updatedAt = Date.now()
|
|
395
|
+
savePrecipitatedStoreRaw(resolvedKey, store, targetOpt)
|
|
396
|
+
return {
|
|
397
|
+
ok: true,
|
|
398
|
+
id: item.id,
|
|
399
|
+
title: item.title,
|
|
400
|
+
flippedOut: op === 'flipOut',
|
|
401
|
+
flippedIdsForSession: store.items
|
|
402
|
+
.filter(it => it.flippedOutSessions.includes(sid))
|
|
403
|
+
.map(it => it.id),
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (op === 'deletePrecipitated') {
|
|
408
|
+
const idx = store.items.findIndex(it => it.id === id)
|
|
409
|
+
if (idx < 0) return { ok: false, error: '未找到沉淀经验:' + id }
|
|
410
|
+
const [removed] = store.items.splice(idx, 1)
|
|
411
|
+
savePrecipitatedStoreRaw(resolvedKey, store, targetOpt)
|
|
412
|
+
return { ok: true, removedId: removed.id, title: removed.title }
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (op === 'updatePrecipitated') {
|
|
416
|
+
const item = store.items.find(it => it.id === id)
|
|
417
|
+
if (!item) return { ok: false, error: '未找到沉淀经验:' + id }
|
|
418
|
+
const next = normalizePrecipitatedEntry({ ...item, ...payload, id: item.id, updatedAt: Date.now() })
|
|
419
|
+
Object.assign(item, next)
|
|
420
|
+
savePrecipitatedStoreRaw(resolvedKey, store, targetOpt)
|
|
421
|
+
return { ok: true, item }
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return { ok: false, error: '未知的沉淀库操作:' + op }
|
|
425
|
+
} finally {
|
|
426
|
+
release()
|
|
427
|
+
}
|
|
428
|
+
}
|