dsh-memsearch-community 0.4.1 → 0.5.1
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/README.md +27 -0
- package/lib/index.js +4 -1
- package/lib/tree.js +253 -106
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,33 @@ computed locally with ONNX.
|
|
|
15
15
|
|
|
16
16
|
## Changes from upstream
|
|
17
17
|
|
|
18
|
+
**0.5.1 — existing archives are renumbered automatically**
|
|
19
|
+
|
|
20
|
+
Archives written before 0.5.0 named every day's file after the title, so a
|
|
21
|
+
session spanning three days had three identical names. On first start the plugin
|
|
22
|
+
now discovers the days of each recorded session, renames the files to the
|
|
23
|
+
day-numbered scheme (`标题`, `标题(2)`, `标题(3)`), and adds the retrieval hint
|
|
24
|
+
to the continuations. It runs once, never overwrites an existing file, and is a
|
|
25
|
+
no-op afterwards.
|
|
26
|
+
|
|
27
|
+
**0.5.0 — day numbering, overflow volumes and retrieval hints**
|
|
28
|
+
|
|
29
|
+
A session that spans several days keeps counting instead of restarting:
|
|
30
|
+
|
|
31
|
+
| File | Meaning |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `标题.txt` | day 1 |
|
|
34
|
+
| `标题(2).txt` | day 2 |
|
|
35
|
+
| `标题(3).txt` | day 3 |
|
|
36
|
+
|
|
37
|
+
Inside one day, a file that reaches `maxFileSizeKB` opens an overflow volume
|
|
38
|
+
numbered from 1 within that day: `标题(1.1).txt`, `标题(1.2).txt` for day 1 and
|
|
39
|
+
`标题(2.1).txt` for day 2.
|
|
40
|
+
|
|
41
|
+
Every continuation file now starts with a retrieval hint: read the session's
|
|
42
|
+
first file, then follow the date/volume order. Renaming a session renames its
|
|
43
|
+
folder on **every** day it spans, and a rename never lands on an occupied folder.
|
|
44
|
+
|
|
18
45
|
**0.4.1 — sessions never share a folder**
|
|
19
46
|
|
|
20
47
|
Two sessions with the same title used to share one folder, so renaming one moved
|
package/lib/index.js
CHANGED
|
@@ -465,10 +465,13 @@ function apply(ctx, config) {
|
|
|
465
465
|
const log = (message) => console.error(message);
|
|
466
466
|
|
|
467
467
|
// Upgrading from the flat layout (`<day>/<title>.txt`) moves those files into
|
|
468
|
-
// their session folder once; later starts are no-ops.
|
|
468
|
+
// their session folder once; later starts are no-ops. The second call renumbers
|
|
469
|
+
// archives written before the day/volume scheme existed.
|
|
469
470
|
try {
|
|
470
471
|
const moved = archive.migrateLayout();
|
|
471
472
|
if (moved) log(`[memsearch] 归档结构迁移: ${moved} 个文件移入会话文件夹`);
|
|
473
|
+
const renumbered = archive.migrateDayNaming();
|
|
474
|
+
if (renumbered) log(`[memsearch] 归档编号迁移: ${renumbered} 个文件按天数重新编号`);
|
|
472
475
|
} catch (err) {
|
|
473
476
|
log(`[memsearch] 归档迁移失败: ${err && err.message ? err.message : String(err)}`);
|
|
474
477
|
}
|
package/lib/tree.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The human-facing conversation archive.
|
|
3
3
|
*
|
|
4
|
-
* Layout: `<root>/<YYYY.MM.DD>/<session title>/<
|
|
4
|
+
* Layout: `<root>/<YYYY.MM.DD>/<session title>/<file>.txt`
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* numbered continuation, `<title>(2).txt`, then `(3)`, and so on. Readers walk
|
|
9
|
-
* the day folder, then the title folder, then every `.txt` in it.
|
|
6
|
+
* A session that keeps going on later days continues numbering instead of
|
|
7
|
+
* restarting:
|
|
10
8
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* day 1: 标题.txt
|
|
10
|
+
* day 2: 标题(2).txt
|
|
11
|
+
* day 3: 标题(3).txt
|
|
12
|
+
*
|
|
13
|
+
* Inside one day, a file that reaches `maxBytes` opens an overflow volume, which
|
|
14
|
+
* is numbered from 1 within that day:
|
|
15
|
+
*
|
|
16
|
+
* 标题(1.1).txt, 标题(1.2).txt … (day 1 overflow)
|
|
17
|
+
* 标题(2.1).txt, 标题(2.2).txt … (day 2 overflow)
|
|
18
|
+
*
|
|
19
|
+
* Every continuation file starts with a retrieval hint telling a reader to begin
|
|
20
|
+
* at the session's first file and follow the date/volume order.
|
|
13
21
|
*/
|
|
14
22
|
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
15
23
|
import { basename, join } from "node:path";
|
|
@@ -36,16 +44,24 @@ export function safeFileBase(title, fallback) {
|
|
|
36
44
|
return base.length > 80 ? base.slice(0, 80).trim() : base;
|
|
37
45
|
}
|
|
38
46
|
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
/**
|
|
48
|
+
* File name for one day/volume slot of a session.
|
|
49
|
+
*
|
|
50
|
+
* Volume 0 is the day's first file: the bare title on day 1, `标题(2)` on day 2.
|
|
51
|
+
* Overflow files of that day are numbered from 1: `标题(2.1)`, `标题(2.2)` …
|
|
52
|
+
*/
|
|
53
|
+
export function volumeName(base, dayIndex, volume = 0) {
|
|
54
|
+
if (volume <= 0) return dayIndex <= 1 ? `${base}.txt` : `${base}(${dayIndex}).txt`;
|
|
55
|
+
return `${base}(${dayIndex}.${volume}).txt`;
|
|
42
56
|
}
|
|
43
57
|
|
|
44
|
-
/**
|
|
45
|
-
export function
|
|
46
|
-
if (fileName
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
/** Parse `base.txt`, `base(n).txt` or `base(n.m).txt`. Returns null otherwise. */
|
|
59
|
+
export function parseVolume(fileName, base) {
|
|
60
|
+
if (!fileName.endsWith(".txt")) return null;
|
|
61
|
+
if (fileName === `${base}.txt`) return { day: 1, volume: 0 };
|
|
62
|
+
const m = fileName.match(new RegExp(`^${escapeRegExp(base)}((\\d+)(?:\\.(\\d+))?)\\.txt$`));
|
|
63
|
+
if (!m) return null;
|
|
64
|
+
return { day: Number(m[1]), volume: m[2] === undefined ? 0 : Number(m[2]) };
|
|
49
65
|
}
|
|
50
66
|
|
|
51
67
|
function escapeRegExp(text) {
|
|
@@ -53,16 +69,25 @@ function escapeRegExp(text) {
|
|
|
53
69
|
}
|
|
54
70
|
|
|
55
71
|
/**
|
|
56
|
-
* Escape timestamp-looking lines inside captured text
|
|
57
|
-
*
|
|
58
|
-
* `appendTurn` separates turns with a line that starts with `[YYYY.MM.DD HH:MM]`.
|
|
59
|
-
* A reply that quotes such a line — an example, another archive, a log — would
|
|
60
|
-
* read as a turn boundary and split one exchange into several.
|
|
72
|
+
* Escape timestamp-looking lines inside captured text, so a quoted example such
|
|
73
|
+
* as `[2026.12.04 09:00]` is not mistaken for a turn boundary.
|
|
61
74
|
*/
|
|
62
75
|
export function defuseStamps(text) {
|
|
63
76
|
return String(text).replace(/^(\[\d{4}\.\d{2}\.\d{2} \d{2}:\d{2}\])/gm, "\\$1");
|
|
64
77
|
}
|
|
65
78
|
|
|
79
|
+
/** Header written at the top of every continuation file. */
|
|
80
|
+
export function continuationHint(dayIndex, volume) {
|
|
81
|
+
const slot = volume > 0 ? `第 ${dayIndex} 天第 ${volume} 卷` : `第 ${dayIndex} 天`;
|
|
82
|
+
return [
|
|
83
|
+
"【检索指引】",
|
|
84
|
+
`本文件是该会话的延续内容(${slot}),不是开头。`,
|
|
85
|
+
"检索、总结或补充本会话时,请先读同目录下的初始文件,再按日期与卷号顺序依次读取:",
|
|
86
|
+
"初始文件 → (2) → (2.1) → (2.2) → (3) → …",
|
|
87
|
+
"",
|
|
88
|
+
].join("\n");
|
|
89
|
+
}
|
|
90
|
+
|
|
66
91
|
export class Archive {
|
|
67
92
|
constructor(root, { maxBytes = 512 * 1024 } = {}) {
|
|
68
93
|
this.root = root;
|
|
@@ -92,15 +117,12 @@ export class Archive {
|
|
|
92
117
|
}
|
|
93
118
|
}
|
|
94
119
|
|
|
95
|
-
/** Point the archive at another root, keeping the flags already recorded. */
|
|
96
120
|
relocate(root) {
|
|
97
121
|
const previous = this.state;
|
|
98
122
|
this.root = root;
|
|
99
123
|
this.statePath = join(root, ".dsh-memsearch-state.json");
|
|
100
124
|
this.state = this.#load();
|
|
101
|
-
if (previous && previous.flags) {
|
|
102
|
-
this.state.flags = { ...previous.flags, ...(this.state.flags || {}) };
|
|
103
|
-
}
|
|
125
|
+
if (previous && previous.flags) this.state.flags = { ...previous.flags, ...(this.state.flags || {}) };
|
|
104
126
|
this.ensureRoot();
|
|
105
127
|
this.save();
|
|
106
128
|
return this.root;
|
|
@@ -126,7 +148,6 @@ export class Archive {
|
|
|
126
148
|
return dir;
|
|
127
149
|
}
|
|
128
150
|
|
|
129
|
-
/** Day folders currently on disk, oldest first. */
|
|
130
151
|
listDays() {
|
|
131
152
|
try {
|
|
132
153
|
return readdirSync(this.root)
|
|
@@ -137,14 +158,26 @@ export class Archive {
|
|
|
137
158
|
}
|
|
138
159
|
}
|
|
139
160
|
|
|
140
|
-
/**
|
|
161
|
+
/** Session folders of one day. */
|
|
162
|
+
sessionsOfDay(day) {
|
|
163
|
+
try {
|
|
164
|
+
return readdirSync(join(this.root, day))
|
|
165
|
+
.filter((name) => statSync(join(this.root, day, name)).isDirectory())
|
|
166
|
+
.sort();
|
|
167
|
+
} catch {
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Every text file of a session folder, ordered by day then volume. */
|
|
141
173
|
volumes(day, base) {
|
|
142
174
|
const dir = join(this.root, day, base);
|
|
143
175
|
try {
|
|
144
176
|
return readdirSync(dir)
|
|
145
|
-
.
|
|
146
|
-
.
|
|
147
|
-
.
|
|
177
|
+
.map((name) => ({ name, slot: parseVolume(name, base) }))
|
|
178
|
+
.filter((entry) => entry.slot)
|
|
179
|
+
.sort((a, b) => a.slot.day - b.slot.day || a.slot.volume - b.slot.volume)
|
|
180
|
+
.map((entry) => join(dir, entry.name));
|
|
148
181
|
} catch {
|
|
149
182
|
return [];
|
|
150
183
|
}
|
|
@@ -158,20 +191,19 @@ export class Archive {
|
|
|
158
191
|
}
|
|
159
192
|
}
|
|
160
193
|
|
|
161
|
-
/**
|
|
162
|
-
* Pick the folder a session owns inside a day.
|
|
163
|
-
*
|
|
164
|
-
* Folders are named after the session title, but two sessions can share a
|
|
165
|
-
* title: the second one gets `标题(2)`. `selfFolder` is the caller's current
|
|
166
|
-
* folder and counts as free because the caller is moving away from it.
|
|
167
|
-
*/
|
|
194
|
+
/** Folders are claimed per session so a rename never moves another transcript. */
|
|
168
195
|
#claimFolder(day, base, sessionId, selfFolder) {
|
|
169
196
|
const owner = new Map();
|
|
170
197
|
for (const [id, record] of Object.entries(this.state.sessions)) {
|
|
171
198
|
if (!record) continue;
|
|
172
|
-
|
|
173
|
-
const folder
|
|
174
|
-
|
|
199
|
+
if (record.folder) owner.set(record.folder, id);
|
|
200
|
+
for (const folder of Object.values(record.folders || {})) {
|
|
201
|
+
if (folder) owner.set(folder, id);
|
|
202
|
+
}
|
|
203
|
+
if (!record.folder && record.path) {
|
|
204
|
+
const parent = record.path.slice(0, Math.max(record.path.lastIndexOf("\\"), record.path.lastIndexOf("/")));
|
|
205
|
+
if (parent) owner.set(parent, id);
|
|
206
|
+
}
|
|
175
207
|
}
|
|
176
208
|
const dayDir = this.dayDir(day);
|
|
177
209
|
for (let n = 1; n < 1000; n++) {
|
|
@@ -184,34 +216,34 @@ export class Archive {
|
|
|
184
216
|
return join(dayDir, `${base}(${Date.now()})`);
|
|
185
217
|
}
|
|
186
218
|
|
|
187
|
-
/**
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
219
|
+
/** Which day of this session `day` is (1-based), registering new days in order. */
|
|
220
|
+
#dayIndexOf(record, day) {
|
|
221
|
+
const days = Array.isArray(record && record.days) ? [...record.days] : [];
|
|
222
|
+
if (!days.includes(day)) days.push(day);
|
|
223
|
+
return { days, index: days.indexOf(day) + 1 };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Highest volume already used for `dayIndex` in this folder (-1 when none). */
|
|
227
|
+
#maxVolume(folder, base, dayIndex) {
|
|
228
|
+
let max = -1;
|
|
229
|
+
try {
|
|
230
|
+
for (const name of readdirSync(folder)) {
|
|
231
|
+
const slot = parseVolume(name, base);
|
|
232
|
+
if (slot && slot.day === dayIndex) max = Math.max(max, slot.volume);
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
/* empty folder */
|
|
199
236
|
}
|
|
200
|
-
|
|
201
|
-
const last = files[files.length - 1];
|
|
202
|
-
if (this.#sizeOf(last) + blockBytes <= this.maxBytes) return last;
|
|
203
|
-
const nextIndex = volumeIndexOf(basename(last), base) + 1;
|
|
204
|
-
return join(folder, volumeName(base, nextIndex));
|
|
237
|
+
return max;
|
|
205
238
|
}
|
|
206
239
|
|
|
207
240
|
/**
|
|
208
241
|
* Append one exchange and return the file it went into.
|
|
209
|
-
* Renames the session folder and its volumes when the title changed.
|
|
210
242
|
*/
|
|
211
243
|
appendTurn({ sessionId, title, date = new Date(), userText, assistantText }) {
|
|
212
244
|
this.ensureRoot();
|
|
213
245
|
const day = dayName(date);
|
|
214
|
-
const
|
|
246
|
+
const wanted = safeFileBase(title, `会话-${String(sessionId).slice(-6)}`);
|
|
215
247
|
|
|
216
248
|
const block = [
|
|
217
249
|
`[${dayStamp(date)}]`,
|
|
@@ -220,86 +252,200 @@ export class Archive {
|
|
|
220
252
|
"",
|
|
221
253
|
].join("\n");
|
|
222
254
|
|
|
223
|
-
|
|
255
|
+
let record = this.state.sessions[sessionId];
|
|
224
256
|
let folder;
|
|
225
|
-
let currentPath;
|
|
257
|
+
let currentPath = null;
|
|
258
|
+
|
|
259
|
+
const sameDayFolder = record && record.day === day && record.folder && existsSync(record.folder);
|
|
226
260
|
|
|
227
|
-
if (
|
|
261
|
+
if (sameDayFolder && record.title === wanted) {
|
|
262
|
+
folder = record.folder;
|
|
263
|
+
currentPath = record.path;
|
|
264
|
+
} else if (sameDayFolder) {
|
|
265
|
+
folder = this.#renameAllDays(sessionId, record, wanted);
|
|
266
|
+
record = this.state.sessions[sessionId];
|
|
228
267
|
folder = record.folder;
|
|
229
268
|
currentPath = record.path;
|
|
230
|
-
if (record.title !== base) {
|
|
231
|
-
folder = this.#renameFolder(sessionId, record, day, base);
|
|
232
|
-
currentPath = this.state.sessions[sessionId].path;
|
|
233
|
-
}
|
|
234
269
|
} else {
|
|
235
|
-
|
|
270
|
+
// New session, or the same session on another day: one folder per day.
|
|
271
|
+
folder = this.#claimFolder(day, wanted, sessionId);
|
|
236
272
|
mkdirSync(folder, { recursive: true });
|
|
237
|
-
currentPath = null;
|
|
238
273
|
}
|
|
239
274
|
|
|
240
|
-
const
|
|
241
|
-
const
|
|
275
|
+
const base = basename(folder);
|
|
276
|
+
const { days, index: dayIndex } = this.#dayIndexOf(record, day);
|
|
277
|
+
const folders = { ...(record && record.folders ? record.folders : {}), [day]: folder };
|
|
278
|
+
|
|
279
|
+
// Continue the current file while it has room.
|
|
280
|
+
if (currentPath && existsSync(currentPath)) {
|
|
281
|
+
const slot = parseVolume(basename(currentPath), base);
|
|
282
|
+
const room = this.#sizeOf(currentPath) + Buffer.byteLength(block, "utf8") <= this.maxBytes;
|
|
283
|
+
if (slot && slot.day === dayIndex && room) {
|
|
284
|
+
appendFileSync(currentPath, block, "utf8");
|
|
285
|
+
this.state.sessions[sessionId] = { ...record, path: currentPath, day, title: base, folder, days, folders };
|
|
286
|
+
this.save();
|
|
287
|
+
return currentPath;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Otherwise open the next volume of this day.
|
|
292
|
+
const nextVolume = this.#maxVolume(folder, base, dayIndex) + 1;
|
|
293
|
+
const target = join(folder, volumeName(base, dayIndex, nextVolume));
|
|
294
|
+
if (!(dayIndex === 1 && nextVolume === 0)) {
|
|
295
|
+
appendFileSync(target, continuationHint(dayIndex, nextVolume), "utf8");
|
|
296
|
+
}
|
|
242
297
|
appendFileSync(target, block, "utf8");
|
|
243
|
-
this.state.sessions[sessionId] = { path: target, day, title:
|
|
298
|
+
this.state.sessions[sessionId] = { ...record, path: target, day, title: base, folder, days, folders };
|
|
244
299
|
this.save();
|
|
245
300
|
return target;
|
|
246
301
|
}
|
|
247
302
|
|
|
248
|
-
/**
|
|
303
|
+
/** Public rename entry used by the session/title event. */
|
|
249
304
|
renameForTitle(sessionId, title) {
|
|
250
305
|
const record = this.state.sessions[sessionId];
|
|
251
306
|
if (!record || !record.folder || !existsSync(record.folder)) return null;
|
|
252
|
-
const
|
|
253
|
-
if (
|
|
254
|
-
|
|
307
|
+
const next = safeFileBase(title, record.title || basename(record.folder));
|
|
308
|
+
if (next === record.title) return record.path;
|
|
309
|
+
this.#renameAllDays(sessionId, record, next);
|
|
310
|
+
return this.state.sessions[sessionId].path;
|
|
255
311
|
}
|
|
256
312
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Rename every day folder a session owns, keeping each file's day/volume slot.
|
|
315
|
+
*
|
|
316
|
+
* A session may already have per-day folder names that differ (one day the
|
|
317
|
+
* folder was disambiguated as `周报(2)`), so the recorded folder map is the
|
|
318
|
+
* source of truth rather than a single derived name.
|
|
319
|
+
*/
|
|
320
|
+
#renameAllDays(sessionId, record, nextBase) {
|
|
321
|
+
const days = Array.isArray(record.days) && record.days.length ? record.days : [record.day];
|
|
322
|
+
const folders = { ...(record.folders || {}) };
|
|
323
|
+
let currentFolder = record.folder;
|
|
324
|
+
let currentPath = record.path;
|
|
325
|
+
|
|
326
|
+
for (const day of days) {
|
|
327
|
+
const fromDir = folders[day] || join(this.root, day, record.title || nextBase);
|
|
328
|
+
if (!existsSync(fromDir)) continue;
|
|
329
|
+
const previousBase = basename(fromDir);
|
|
330
|
+
const toDir = this.#claimFolder(day, nextBase, sessionId, fromDir);
|
|
331
|
+
const nextFolderBase = basename(toDir);
|
|
332
|
+
|
|
333
|
+
if (toDir !== fromDir) {
|
|
334
|
+
// Never rename onto an existing folder: Windows would replace it.
|
|
335
|
+
if (existsSync(toDir)) continue;
|
|
336
|
+
try {
|
|
337
|
+
renameSync(fromDir, toDir);
|
|
338
|
+
} catch {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
262
342
|
|
|
263
|
-
if (targetFolder !== previousFolder) {
|
|
264
343
|
try {
|
|
265
|
-
|
|
266
|
-
|
|
344
|
+
for (const name of readdirSync(toDir)) {
|
|
345
|
+
const slot = parseVolume(name, previousBase);
|
|
346
|
+
if (!slot) continue;
|
|
347
|
+
const next = volumeName(nextFolderBase, slot.day, slot.volume);
|
|
348
|
+
if (next === name) continue;
|
|
349
|
+
const target = join(toDir, next);
|
|
350
|
+
if (existsSync(target)) continue;
|
|
351
|
+
renameSync(join(toDir, name), target);
|
|
267
352
|
}
|
|
268
|
-
renameSync(previousFolder, targetFolder);
|
|
269
353
|
} catch {
|
|
270
|
-
|
|
354
|
+
/* best effort */
|
|
271
355
|
}
|
|
272
|
-
}
|
|
273
356
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
const next = volumeName(targetBase, index);
|
|
280
|
-
if (next === file) continue;
|
|
281
|
-
const target = join(targetFolder, next);
|
|
282
|
-
if (existsSync(target)) continue;
|
|
283
|
-
renameSync(join(targetFolder, file), target);
|
|
357
|
+
folders[day] = toDir;
|
|
358
|
+
if (day === record.day) {
|
|
359
|
+
currentFolder = toDir;
|
|
360
|
+
const files = this.volumes(day, nextFolderBase);
|
|
361
|
+
currentPath = files.length ? files[0] : join(toDir, volumeName(nextFolderBase, 1, 0));
|
|
284
362
|
}
|
|
285
|
-
} catch {
|
|
286
|
-
/* leave whatever was renamed renamed */
|
|
287
363
|
}
|
|
288
364
|
|
|
289
|
-
|
|
290
|
-
.filter((f) => volumeIndexOf(f, targetBase) > 0)
|
|
291
|
-
.sort((a, b) => volumeIndexOf(a, targetBase) - volumeIndexOf(b, targetBase));
|
|
292
|
-
|
|
293
|
-
const path = files.length ? join(targetFolder, files[0]) : join(targetFolder, volumeName(targetBase, 1));
|
|
294
|
-
this.state.sessions[sessionId] = { ...record, path, title: targetBase, folder: targetFolder, day };
|
|
365
|
+
this.state.sessions[sessionId] = { ...record, title: nextBase, folder: currentFolder, path: currentPath, folders };
|
|
295
366
|
this.save();
|
|
296
|
-
return
|
|
367
|
+
return currentPath;
|
|
297
368
|
}
|
|
298
369
|
|
|
299
370
|
/**
|
|
300
|
-
*
|
|
301
|
-
*
|
|
371
|
+
* Bring archives written before 0.5.0 up to the day-numbering scheme.
|
|
372
|
+
*
|
|
373
|
+
* Sessions recorded by older versions have no `days` map, so the day folders
|
|
374
|
+
* are discovered by title: every day that holds a folder with that name is one
|
|
375
|
+
* day of the session, in date order. Files already named for a later day are
|
|
376
|
+
* left alone, and nothing is overwritten.
|
|
302
377
|
*/
|
|
378
|
+
migrateDayNaming() {
|
|
379
|
+
if (this.countsAsFlag("dayNamingMigrated")) return 0;
|
|
380
|
+
const days = this.listDays();
|
|
381
|
+
let changed = 0;
|
|
382
|
+
|
|
383
|
+
for (const [sessionId, record] of Object.entries(this.state.sessions)) {
|
|
384
|
+
const base = record.title || (record.folder ? basename(record.folder) : null);
|
|
385
|
+
if (!base) continue;
|
|
386
|
+
const present = days.filter((day) => existsSync(join(this.root, day, base)));
|
|
387
|
+
if (!present.length) continue;
|
|
388
|
+
|
|
389
|
+
present.forEach((day, index) => {
|
|
390
|
+
const dayIndex = index + 1;
|
|
391
|
+
const dir = join(this.root, day, base);
|
|
392
|
+
let names;
|
|
393
|
+
try {
|
|
394
|
+
names = readdirSync(dir);
|
|
395
|
+
} catch {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
for (const name of names) {
|
|
399
|
+
const slot = parseVolume(name, base);
|
|
400
|
+
if (!slot) continue;
|
|
401
|
+
const target = join(dir, volumeName(base, dayIndex, slot.volume));
|
|
402
|
+
if (target === join(dir, name)) {
|
|
403
|
+
// already correct; only make sure a continuation carries its hint
|
|
404
|
+
if (dayIndex > 1 || slot.volume > 0) this.#ensureHint(target, dayIndex, slot.volume);
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (existsSync(target)) continue;
|
|
408
|
+
try {
|
|
409
|
+
renameSync(join(dir, name), target);
|
|
410
|
+
changed++;
|
|
411
|
+
if (dayIndex > 1 || slot.volume > 0) this.#ensureHint(target, dayIndex, slot.volume);
|
|
412
|
+
} catch {
|
|
413
|
+
/* leave it in place */
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
const folders = {};
|
|
419
|
+
for (const day of present) folders[day] = join(this.root, day, base);
|
|
420
|
+
const lastDay = present[present.length - 1];
|
|
421
|
+
const volumes = this.volumes(lastDay, base);
|
|
422
|
+
this.state.sessions[sessionId] = {
|
|
423
|
+
...record,
|
|
424
|
+
title: base,
|
|
425
|
+
day: lastDay,
|
|
426
|
+
days: present,
|
|
427
|
+
folder: folders[lastDay],
|
|
428
|
+
path: volumes.length ? volumes[volumes.length - 1] : join(folders[lastDay], volumeName(base, present.length, 0)),
|
|
429
|
+
folders,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
this.setFlag("dayNamingMigrated", true);
|
|
434
|
+
this.save();
|
|
435
|
+
return changed;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
#ensureHint(file, dayIndex, volume) {
|
|
439
|
+
try {
|
|
440
|
+
const text = readFileSync(file, "utf8");
|
|
441
|
+
if (text.startsWith("【检索指引】")) return;
|
|
442
|
+
writeFileSync(file, continuationHint(dayIndex, volume) + text, "utf8");
|
|
443
|
+
} catch {
|
|
444
|
+
/* best effort */
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Move files written by the flat layout into the folder layout. */
|
|
303
449
|
migrateLayout() {
|
|
304
450
|
if (this.countsAsFlag("layoutMigrated")) return 0;
|
|
305
451
|
let moved = 0;
|
|
@@ -334,3 +480,4 @@ export class Archive {
|
|
|
334
480
|
return moved;
|
|
335
481
|
}
|
|
336
482
|
}
|
|
483
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-memsearch-community",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Automatic local memory for DeepSeek Harness: per-day, per-session conversation archive with numbered volumes, semantic search index, first-run setup prompts, optional history import.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|