dsh-fish-soul 0.19.28 → 0.19.29
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 +1 -1
- package/lib/data-dir.js +68 -15
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/data-dir.js
CHANGED
|
@@ -11,12 +11,40 @@
|
|
|
11
11
|
* 通过才改名成目标,旧目录改名 `fish-moved-<时间>` 留档;
|
|
12
12
|
* - 任何一步失败 → **回退旧目录继续跑,绝不空库启动**(半份数据比没有更糟)。
|
|
13
13
|
*/
|
|
14
|
-
import { cp, mkdir, readdir, rename, rm, stat } from 'node:fs/promises';
|
|
15
|
-
import { existsSync } from 'node:fs';
|
|
14
|
+
import { cp, mkdir, readdir, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
15
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
16
16
|
import { dirname, isAbsolute, join, resolve, sep } from 'node:path';
|
|
17
|
-
import { dshHome, configuredDataDir, setActiveDataDir, getKv, setKv } from './db.js';
|
|
17
|
+
import { dshHome, configPath, configuredDataDir, setActiveDataDir, getKv, setKv } from './db.js';
|
|
18
18
|
import { stampLocal } from './time-util.js';
|
|
19
19
|
|
|
20
|
+
/** 配置文件里「上次生效的数据目录」的键名(搬完家写回,下次启动据此判断从哪搬)。 */
|
|
21
|
+
const ACTIVE_KEY = 'dataDirActive';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 读配置文件原文(JSON 对象;读不到 / 不是对象 → `{}`)。
|
|
25
|
+
* 写回时必须保留用户其它键,不能整份覆盖。
|
|
26
|
+
*/
|
|
27
|
+
function readConfig() {
|
|
28
|
+
try {
|
|
29
|
+
const v = JSON.parse(readFileSync(configPath(), 'utf8').replace(/^\uFEFF/, ''));
|
|
30
|
+
return v && typeof v === 'object' && !Array.isArray(v) ? v : {};
|
|
31
|
+
} catch { return {}; }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 把配置写回去(保留其它键;写不进去只影响下次启动从哪搬,不影响本次)。 */
|
|
35
|
+
async function writeConfig(patch) {
|
|
36
|
+
const next = { ...readConfig(), ...patch };
|
|
37
|
+
try { await writeFile(configPath(), JSON.stringify(next, null, '\t'), 'utf8'); } catch { /* 同上 */ }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 上次生效的数据目录(配置文件里记的;没记 / 记的目录已不在 → `''`)。 */
|
|
41
|
+
function lastActiveDir() {
|
|
42
|
+
const v = String(readConfig()[ACTIVE_KEY] ?? '').trim();
|
|
43
|
+
if (v === '') return '';
|
|
44
|
+
// 目录已不在:当作没记(用户手删了留档、或换了机器),否则会试图从空处搬家
|
|
45
|
+
return existsSync(v) ? resolve(v) : '';
|
|
46
|
+
}
|
|
47
|
+
|
|
20
48
|
/** 目录里有东西吗(跳过点开头的残留文件)。 */
|
|
21
49
|
async function hasContent(dir) {
|
|
22
50
|
try {
|
|
@@ -57,29 +85,51 @@ export async function verifyCopy(from, to) {
|
|
|
57
85
|
}
|
|
58
86
|
|
|
59
87
|
/**
|
|
60
|
-
*
|
|
88
|
+
* 启动时决定数据放哪,必要时把数据搬到新目录。
|
|
61
89
|
*
|
|
62
90
|
* 必须在 `openDatabase()` 之前调用 —— 库路径就是这里钉下去的。
|
|
91
|
+
*
|
|
92
|
+
* 「从哪搬」不再只看默认目录:配置文件里记着上次生效的目录(`dataDirActive`),
|
|
93
|
+
* 所以 自定义→默认、自定义A→自定义B 都能搬。没记(老安装)就按默认目录算。
|
|
63
94
|
* @returns {Promise<{dir: string, moved: boolean, archived: string}>} archived = 旧目录改成的留档路径(没搬为空)
|
|
64
95
|
*/
|
|
65
96
|
export async function ensureDataDir({ log = () => {}, copy = cp } = {}) {
|
|
66
|
-
const
|
|
97
|
+
const last = lastActiveDir();
|
|
98
|
+
const from = last || join(dshHome(), 'fish-soul');
|
|
67
99
|
const want = configuredDataDir();
|
|
100
|
+
if (want === null && last !== '' && resolve(last) !== resolve(join(dshHome(), 'fish-soul'))) {
|
|
101
|
+
// 用户清空了配置 = 「搬回默认目录」。默认目录就是目标,走下面同一条搬家流程。
|
|
102
|
+
return moveTo(join(dshHome(), 'fish-soul'), { from, log, copy, back: true });
|
|
103
|
+
}
|
|
68
104
|
if (want === null || resolve(want) === resolve(from)) {
|
|
69
|
-
//
|
|
105
|
+
// 没配(或配成上次在用的目录)时**不缓存**:让 fishSoulDir() 每次现拼 dshHome(),
|
|
70
106
|
// 否则测试里换 DSH_HOME 会全部串到第一个 home 上(smoke 第 20 组就是这么挂的)。
|
|
71
|
-
|
|
72
|
-
|
|
107
|
+
// ⚠️ 只有「回到默认」才清缓存;配的正好是上次在用的自定义目录时要钉住它。
|
|
108
|
+
if (want === null) setActiveDataDir(null);
|
|
109
|
+
else setActiveDataDir(want);
|
|
110
|
+
return { dir: want ?? from, moved: false, archived: '' };
|
|
73
111
|
}
|
|
112
|
+
return moveTo(want, { from, log, copy });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 把数据从 `from` 搬到 `want`(目标已确认是空的或不存在)。
|
|
117
|
+
* `back: true` 表示搬回默认目录 —— 成功后不写 dataDirActive(默认目录不需要记)。
|
|
118
|
+
*/
|
|
119
|
+
async function moveTo(want, { from, log, copy, back = false }) {
|
|
74
120
|
if (await hasContent(want)) {
|
|
75
|
-
|
|
76
|
-
|
|
121
|
+
// 目标已经有东西(上次搬过 / 用户自己搬过)→ 直接用。顺手把「上次生效」钉过去,
|
|
122
|
+
// 这样下次再改目录才知道从这搬。
|
|
123
|
+
if (!back) await writeConfig({ [ACTIVE_KEY]: resolve(want) });
|
|
124
|
+
setActiveDataDir(back ? null : want);
|
|
125
|
+
return { dir: want, moved: false, archived: '' };
|
|
77
126
|
}
|
|
78
127
|
if (!(await hasContent(from))) {
|
|
79
128
|
// 全新安装:直接在新位置起,没有可搬的东西
|
|
80
129
|
await mkdir(want, { recursive: true });
|
|
81
|
-
|
|
82
|
-
|
|
130
|
+
if (!back) await writeConfig({ [ACTIVE_KEY]: resolve(want) });
|
|
131
|
+
setActiveDataDir(back ? null : want);
|
|
132
|
+
return { dir: want, moved: false, archived: '' };
|
|
83
133
|
}
|
|
84
134
|
|
|
85
135
|
log('info', `[fish-soul] 数据目录搬家:${from} → ${want}`);
|
|
@@ -96,7 +146,8 @@ export async function ensureDataDir({ log = () => {}, copy = cp } = {}) {
|
|
|
96
146
|
} catch (error) {
|
|
97
147
|
// 半份数据不能留下 —— 下次启动会被当成「已经有内容」直接用
|
|
98
148
|
try { await rm(incoming, { recursive: true, force: true }); } catch { /* 删不掉也不影响回退 */ }
|
|
99
|
-
|
|
149
|
+
// 回退到「上次在用的目录」:可能是默认目录(老安装),也可能是上一个自定义目录(A→B 失败)
|
|
150
|
+
setActiveDataDir(lastActiveDir() === '' ? null : from);
|
|
100
151
|
const msg = error instanceof Error ? error.message : String(error);
|
|
101
152
|
log('error', `[fish-soul] 数据目录搬家失败,继续用 ${from}:${msg}`);
|
|
102
153
|
throw error;
|
|
@@ -112,7 +163,9 @@ export async function ensureDataDir({ log = () => {}, copy = cp } = {}) {
|
|
|
112
163
|
// 改名失败不影响使用:数据已经在新位置了,旧目录当成多余的副本
|
|
113
164
|
log('warn', `[fish-soul] 旧数据目录改名失败(不影响使用):${error instanceof Error ? error.message : String(error)}`);
|
|
114
165
|
}
|
|
115
|
-
|
|
166
|
+
if (!back) await writeConfig({ [ACTIVE_KEY]: resolve(want) });
|
|
167
|
+
else await writeConfig({ [ACTIVE_KEY]: '' });
|
|
168
|
+
setActiveDataDir(back ? null : want);
|
|
116
169
|
// 留档路径同时记进新库(openDatabase 之后才调 importArchives),这样面板能列出来删
|
|
117
170
|
return { dir: want, moved: true, archived };
|
|
118
171
|
}
|
|
@@ -175,7 +228,7 @@ export async function removeArchived(db, dir) {
|
|
|
175
228
|
export function checkDataDir(value, current) {
|
|
176
229
|
const raw = String(value ?? '').trim();
|
|
177
230
|
if (raw === '') return ''; // 空 = 用默认,永远合法
|
|
178
|
-
if (!isAbsolute(raw)) return '请填完整路径,比如 D
|
|
231
|
+
if (!isAbsolute(raw)) return '请填完整路径,比如 D:\\fish-soul-data';
|
|
179
232
|
const next = resolve(raw);
|
|
180
233
|
const cur = resolve(String(current ?? ''));
|
|
181
234
|
if (next === cur) return ''; // 没变,允许(保存等于什么都没做)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-fish-soul",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.29",
|
|
4
4
|
"description": "鱼 · 人格、按人记忆(可纠正、有印象、能导入旧聊天记录)、日程自走与主动开口,直连 SnowLuma 收发 QQ(群里会自己插话),B站 / 抖音小号私信与自己刷视频;按功能选模型并统计用量花费,能听会说 QQ 语音、收藏表情包、学群里的梗,可选上网查;心情影响说话、去 AI 味、每个群的设定、按聊天选模型、每晚写日记、每周记忆存档与可选周记(DSH 插件,单角色 feiyu)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|