dsh-memory-trigger 0.2.1 → 0.2.2

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.
Files changed (2) hide show
  1. package/lib/backfill.mjs +34 -1
  2. package/package.json +1 -1
package/lib/backfill.mjs CHANGED
@@ -13,6 +13,39 @@ import { readdirSync, readFileSync, writeFileSync, mkdirSync, statSync } from 'n
13
13
  import { basename, join } from 'node:path';
14
14
  import { homedir } from 'node:os';
15
15
  import { zstdDecompressSync } from 'node:zlib';
16
+ import { spawnSync } from 'node:child_process';
17
+
18
+ const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
19
+
20
+ /**
21
+ * DSH 的会话文件是「追加写入的多帧 zstd」,而 zlib.zstdDecompressSync 只解第一帧。
22
+ * 这里按帧魔数切开逐帧解压;如果系统里有 `zstd` 命令,优先用它(最稳)。
23
+ * @param {string} file 会话文件路径
24
+ * @returns {Buffer} 解压后的全部内容
25
+ */
26
+ function readZstdFile(file) {
27
+ // 1) 系统 zstd(多帧支持最好)
28
+ const cli = spawnSync('zstd', ['-dc', file], { maxBuffer: 1024 * 1024 * 1024 });
29
+ if (!cli.error && cli.status === 0 && cli.stdout && cli.stdout.length > 0) return cli.stdout;
30
+
31
+ // 2) 逐帧解压
32
+ const buf = readFileSync(file);
33
+ const starts = [];
34
+ for (let i = 0; (i = buf.indexOf(ZSTD_MAGIC, i)) !== -1; i += 4) starts.push(i);
35
+ if (starts.length <= 1) return zstdDecompressSync(buf);
36
+ const chunks = [];
37
+ for (let k = 0; k < starts.length; k += 1) {
38
+ const start = starts[k];
39
+ const end = k + 1 < starts.length ? starts[k + 1] : buf.length;
40
+ try {
41
+ chunks.push(zstdDecompressSync(buf.subarray(start, end)));
42
+ } catch {
43
+ // 帧边界误判时跳过这一段,继续后面的
44
+ }
45
+ }
46
+ if (chunks.length === 0) return zstdDecompressSync(buf);
47
+ return Buffer.concat(chunks);
48
+ }
16
49
 
17
50
  const args = process.argv.slice(2);
18
51
  function argOf(name, fallback = '') {
@@ -63,7 +96,7 @@ const summary = [];
63
96
  for (const f of files) {
64
97
  let lines;
65
98
  try {
66
- lines = zstdDecompressSync(readFileSync(f)).toString('utf8').split('\n');
99
+ lines = readZstdFile(f).toString('utf8').split('\n');
67
100
  } catch (error) {
68
101
  summary.push(`${f}: 解压失败 ${String(error)}`);
69
102
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-memory-trigger",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "DSH 记忆触发插件:上下文压缩后,用户引用旧记忆时自动检索会话历史并注入带时间戳的片段。",
5
5
  "license": "MIT",
6
6
  "type": "module",