dsh-lifecard 0.3.1 → 0.4.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/lib/store.js CHANGED
@@ -75,7 +75,10 @@ export function defaultConfig() {
75
75
  locale: 'zh',
76
76
  // 三扇门各自「当前用哪张卡」。写小说、酒馆那两扇门里演谁 / 写谁,
77
77
  // 不用每次都现挑;「真实人物记录」那扇门用它来决定首页先给谁。
78
- default_cards: { real: '', tavern: '', novel: '' },
78
+ default_cards: { real: '', tavern: '', novel: '', diary: '' },
79
+ // 日记放在哪儿。**不在这个工作区里**:用户第一次点「写日记」时自己挑一个文件夹,
80
+ // 之后每天那篇就落在那里。空串=还没挑,界面会先问一句。
81
+ diary_dir: '',
79
82
  // 界面上被收起来的门(用户自己删的)。随时能装回来,数据一点没动。
80
83
  hidden_doors: [],
81
84
  created_at: new Date().toISOString(),
@@ -125,6 +128,27 @@ export async function readConfig(root) {
125
128
  return (await readConfigState(root)).config
126
129
  }
127
130
 
131
+ // ── 写入排队 ──────────────────────────────────────────────────────
132
+ //
133
+ // 所有「读一遍、改一点、整份写回去」的动作都是读-改-写。两个窗口同时点,
134
+ // 后写的那个是照着旧内容改的,前一次改的那一项就被整份盖掉——日记那边
135
+ // 早就按路径排队了,卡和配置一直裸着。这里补上同一把锁。
136
+ //
137
+ // 键是「这次要改的是哪个文件」(绝对路径),排队粒度够细:改 A 卡不会挡住改 B 卡。
138
+ const writeLocks = new Map()
139
+
140
+ function withWriteLock(key, run) {
141
+ const prev = writeLocks.get(key) ?? Promise.resolve()
142
+ const next = prev.then(run, run)
143
+ const tail = next.then(() => {}, () => {})
144
+ writeLocks.set(key, tail)
145
+ tail.then(() => {
146
+ // 队列排空就把这一格删掉,不然改过多少张卡就攒多少个 Promise。
147
+ if (writeLocks.get(key) === tail) writeLocks.delete(key)
148
+ })
149
+ return next
150
+ }
151
+
128
152
  /**
129
153
  * 读配置,并且**告诉调用方「是没有,还是读不出来」**。
130
154
  *
@@ -154,13 +178,24 @@ export async function readConfigState(root) {
154
178
  *
155
179
  * 先建目录:还没选文件夹(或选的那个被删了)时直接写文件会抛 ENOENT,
156
180
  * 界面拿到的是一句英文报错,人看不出「你还没选文件夹」。
181
+ *
182
+ * 返回值一定带 `ok`。以前成功和失败回的是同一个对象形状,调用方判
183
+ * `saved?.ok === false` 永远不成立,「位置没记住」也就永远报不出来——
184
+ * 用户以为存上了,重启一次才发现又要重挑一遍文件夹。
157
185
  */
158
186
  export async function writeConfig(root, patch) {
187
+ const abs = path.join(root, CONFIG_NAME)
188
+ // 排到同一份配置的队尾:两个人同时改设置,后写的那个不该把前一个刚改的盖回去。
189
+ return withWriteLock(abs, () => writeConfigNow(root, patch, abs))
190
+ }
191
+
192
+ async function writeConfigNow(root, patch, abs) {
159
193
  const state = await readConfigState(root)
160
194
  // 「读不出来」不等于「还没有」:这时候照写会把整份设置按默认值重刷一遍。
161
- if (state.ok !== true) return state.config
195
+ if (state.ok !== true) {
196
+ return { ok: false, config: state.config, error: state.error ?? '这个文件夹里的设置读不出来,为了不按默认值重刷一遍,这次没写。' }
197
+ }
162
198
  const next = { ...state.config, ...patch }
163
- const abs = path.join(root, CONFIG_NAME)
164
199
  const tmp = `${abs}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 8)}`
165
200
  try {
166
201
  await fs.mkdir(root, { recursive: true })
@@ -168,8 +203,9 @@ export async function writeConfig(root, patch) {
168
203
  await fs.rename(tmp, abs)
169
204
  } catch {
170
205
  await fs.rm(tmp, { force: true }).catch(() => {})
206
+ return { ok: false, config: state.config, error: '设置没能写进磁盘。检查一下那个文件夹是不是满了、有没有写的权限。' }
171
207
  }
172
- return next
208
+ return { ok: true, config: next }
173
209
  }
174
210
 
175
211
  /**
@@ -263,11 +299,28 @@ export async function writeConcept(root, concept) {
263
299
  const want = clean(concept?.want)
264
300
  const avoid = clean(concept?.avoid)
265
301
  if (idea === '' && want.length === 0 && avoid.length === 0) {
266
- const current = await readConfig(root)
267
- const next = { ...current }
268
- delete next.concept
269
- await fs.writeFile(path.join(root, CONFIG_NAME), JSON.stringify(next, null, 2) + '\n', 'utf8')
270
- return { ok: true, concept: null }
302
+ // 「清空」这条路以前是裸的 fs.writeFile:配置读不出来(坏了、没权限、同步盘
303
+ // 没挂上)时它照样把一份按默认值拼出来的设置写回去,用户挑的文件夹、
304
+ // 认过的人都归零。现在和 writeConfig 走同一道门。
305
+ const abs = path.join(root, CONFIG_NAME)
306
+ return withWriteLock(abs, async () => {
307
+ const state = await readConfigState(root)
308
+ if (state.ok !== true) {
309
+ return { ok: false, concept: null, error: state.error ?? '这个文件夹里的设置读不出来,为了不按默认值重刷一遍,这次没写。' }
310
+ }
311
+ const next = { ...state.config }
312
+ delete next.concept
313
+ const tmp = `${abs}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 8)}`
314
+ try {
315
+ await fs.mkdir(root, { recursive: true })
316
+ await fs.writeFile(tmp, JSON.stringify(next, null, 2) + '\n', 'utf8')
317
+ await fs.rename(tmp, abs)
318
+ } catch {
319
+ await fs.rm(tmp, { force: true }).catch(() => {})
320
+ return { ok: false, concept: null, error: '设置没能写进磁盘。检查一下那个文件夹是不是满了、有没有写的权限。' }
321
+ }
322
+ return { ok: true, concept: null }
323
+ })
271
324
  }
272
325
  const value = { idea, want, avoid, updated_at: new Date().toISOString() }
273
326
  await writeConfig(root, { concept: value })
@@ -743,13 +796,43 @@ async function freeStamp(dir, when) {
743
796
 
744
797
  /** 写一张卡,并在写前留一份全文快照。 */
745
798
  export async function writeCard(root, relPath, data, sections, options = {}) {
799
+ const at = await resolveIn(root, relPath)
800
+ if (!at.ok) return { ok: false, path: relPath, error: at.error }
801
+ // 同一张卡排成一队:两个窗口同时补同一张卡时,「读旧内容→拼新的→写回」
802
+ // 会互相踩着,后写的那次是照着**它读到的旧内容**改的,前一次补的就没了,
803
+ // 而且备份里也没有——这是唯一一种丢了就找不回来的情况。
804
+ return withWriteLock(at.abs, () => writeCardNow(root, relPath, at.abs, data, sections, options))
805
+ }
806
+
807
+ /**
808
+ * 读一张卡 → 改 → 写回,整段排在同一队列里。
809
+ *
810
+ * 只给 `writeCard` 加锁是不够的:一次「补信息」在别处是「读出旧内容、拼上新的、
811
+ * 再写回」,中间跨了好几个 await。两个窗口同时补同一张卡时,两边读到的是同一份
812
+ * 旧内容,后写的那一份照着旧底稿改,前一次补的就没了——而且快照里也没有,
813
+ * 因为两次写前的旧内容是一样的。所以读也得在队列里。
814
+ *
815
+ * `mutate(read)` 拿到 `{data, sections, text}`(readCard 的返回),要么回
816
+ * `{ ok: true, data, sections }`,要么回 `{ ok: false, error }` 表示这次不写。
817
+ */
818
+ export async function updateCard(root, relPath, mutate, options = {}) {
819
+ const at = await resolveIn(root, relPath)
820
+ if (!at.ok) return { ok: false, path: relPath, error: at.error }
821
+ return withWriteLock(at.abs, async () => {
822
+ const read = await readCard(root, relPath)
823
+ if (read.ok !== true) return read
824
+ const next = typeof mutate === 'function' ? await mutate(read) : null
825
+ if (next === null || next === undefined) return { ok: false, path: relPath, error: '这一版没能算出来,这次没写。' }
826
+ if (next.ok === false) return { ...next, path: next.path ?? relPath }
827
+ return writeCardNow(root, relPath, at.abs, next.data, next.sections, options)
828
+ })
829
+ }
830
+
831
+ async function writeCardNow(root, relPath, abs, data, sections, options = {}) {
746
832
  const validation = core.validateCard(data)
747
833
  if (!validation.ok && options.force !== true) {
748
834
  return { ok: false, error: '校验未通过', validation }
749
835
  }
750
- const at = await resolveIn(root, relPath)
751
- if (!at.ok) return { ok: false, path: relPath, error: at.error }
752
- const abs = at.abs
753
836
  await fs.mkdir(path.dirname(abs), { recursive: true })
754
837
  // 快照:旧内容存全文(快照是审计依据,不能只存 diff)
755
838
  let old = null
@@ -926,6 +1009,8 @@ export async function listAll(root) {
926
1009
  kind: String(r.data?.kind ?? 'real'),
927
1010
  world: String(r.data?.world ?? 'reality'),
928
1011
  relation: r.data?.relation,
1012
+ // 补充关系(「也是伴侣」这类)。关系网要用它排先后,卡背面也读它。
1013
+ relation_extra: Array.isArray(r.data?.relation_extra) ? r.data.relation_extra.filter((x) => typeof x === 'string') : [],
929
1014
  status: r.data?.status,
930
1015
  tags: Array.isArray(r.data?.tags) ? r.data.tags : [],
931
1016
  // 卡背面要显示一段「简介」:取自述的头一段,够长又不至于把整卡铺出来。
@@ -2245,6 +2330,7 @@ export default {
2245
2330
  cardRelPath,
2246
2331
  readCard,
2247
2332
  writeCard,
2333
+ updateCard,
2248
2334
  listAll,
2249
2335
  isBackupName,
2250
2336
  listWithdrawn,
package/lib/tavern.js CHANGED
@@ -373,6 +373,9 @@ export function buildRoleplaySystem({ cardName, persona, worldNotes, memory, pre
373
373
  '- 不跳出角色解释自己,不写「(作为 AI)」「(注)」「(旁白)」这类东西。',
374
374
  '- 卡上写了「不会说的话」「不会做的事」,就真的别说、别做。',
375
375
  '- 不知道的事说不知道;没发生过的事不要编成回忆。',
376
+ '- 你只写自己,但场面要有人:屋里还有别人时,给其中一两个一个具体的动作或一句自己的话(谁在做什么、谁往这边看一眼),不要让他们全站着当背景。',
377
+ '- 你的每一句都得是你这个人才会说的。同一句话换个人说也成立,那就是还没说好。',
378
+ '- 不拿一句总结代替过程:不写「聊了一会儿」「沉默了半晌」「都明白了」。写这一刻发生了什么——手上的动作、说出口的半句话、谁先别开眼睛。',
376
379
  '',
377
380
  '直接写你要说的话,不要写「' + name + ':」这样的前缀。',
378
381
  )