dsh-lifecard 0.3.1 → 0.5.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/README.md +32 -5
- package/cordis.patch.yml +64 -0
- package/lib/career.js +98 -0
- package/lib/client.js +792 -18
- package/lib/core.js +30 -5
- package/lib/diary.js +838 -0
- package/lib/index.js +521 -9
- package/lib/novel.js +4 -0
- package/lib/presets.js +30 -0
- package/lib/store.js +217 -12
- package/lib/tavern.js +3 -0
- package/lifecard-tools.js +534 -59
- package/package.json +2 -1
- package/skills/lifecard-diary/SKILL.md +131 -0
package/lifecard-tools.js
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 为什么单独一个文件:酒馆和写小说原来是在插件自己的界面里说话。用户要的是
|
|
5
5
|
* 「和正常对话一样,在新会话里打开」——那就不能自建聊天框,得让 DSH 的普通
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* 会话能读到这些卡。这个文件就是那条通路:模型想演谁、想写谁,先来这儿把人和
|
|
7
|
+
* 世界取走;聊出来的东西(日记)也从这儿写回去。
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* 默认只读。会写盘的只有四把工具,而且都有一道门:改卡走 approved + 原句核对,
|
|
10
|
+
* 写日记只写用户这一回亲口讲的那一天、绝不静默覆盖。其余工具只看不改,
|
|
11
|
+
* 免得模型手一滑把用户写的东西洗掉。
|
|
10
12
|
*
|
|
11
13
|
* 挂法(见 cordis.patch.yml):宿主那一层插一行本文件,预设里的
|
|
12
14
|
* plugins 再引用一次,于是「LC 酒馆 / LC 小说」这些门里也能用。
|
|
@@ -17,6 +19,7 @@ import { readdir } from 'node:fs/promises'
|
|
|
17
19
|
|
|
18
20
|
import * as core from './lib/core.js'
|
|
19
21
|
import * as store from './lib/store.js'
|
|
22
|
+
import { DIARY_SECTIONS, diaryRootFrom, readDiary, listDiaries, writeDiary, diaryBlock } from './lib/diary.js'
|
|
20
23
|
|
|
21
24
|
// 这几个和 lib/index.js 里是同一套约定。故意在这里重写一遍、不从 index.js
|
|
22
25
|
// import:工具是被主插件在 apply 里调起来的,反过来 import index.js 会绕成
|
|
@@ -81,6 +84,15 @@ async function resolveRoot(config) {
|
|
|
81
84
|
|
|
82
85
|
const KIND_LABEL = { real: '现实里的人', fictional: '虚构角色', self: '自己', hybrid: '半真半虚构' }
|
|
83
86
|
|
|
87
|
+
/**
|
|
88
|
+
* 一句引文至少要有几个字才算「原文里有这一句」。
|
|
89
|
+
*
|
|
90
|
+
* 以前只要求非空,于是 `quote: '今天'` 就能过——那两个字在哪个人的材料里
|
|
91
|
+
* 都找得到,「每条都要有原文」这道门等于没有。五个字是个折中:
|
|
92
|
+
* 「睡得很沉」这种短句还在,纯口头禅类的两三个字拦掉。
|
|
93
|
+
*/
|
|
94
|
+
const MIN_QUOTE_CHARS = 5
|
|
95
|
+
|
|
84
96
|
function kindOf(card) {
|
|
85
97
|
const kind = String(card?.kind ?? '')
|
|
86
98
|
return KIND_LABEL[kind] ?? (kind === '' ? '没写是哪种' : kind)
|
|
@@ -131,10 +143,10 @@ function entriesToText(entries) {
|
|
|
131
143
|
}
|
|
132
144
|
|
|
133
145
|
/**
|
|
134
|
-
*
|
|
146
|
+
* 把这一组工具挂到 ctx.tools 上。
|
|
135
147
|
*
|
|
136
148
|
* 由 lib/index.js 的 apply 在 tools 服务就绪后调用(宿主那一层注册一次),
|
|
137
|
-
* 所以不只是 LC
|
|
149
|
+
* 所以不只是 LC 几个预设,任何预设里都能调。
|
|
138
150
|
*/
|
|
139
151
|
export function registerTools(ctx, config) {
|
|
140
152
|
if (ctx?.tools?.register === undefined) return false
|
|
@@ -443,34 +455,41 @@ export function registerTools(ctx, config) {
|
|
|
443
455
|
|
|
444
456
|
const existingRel = await findExisting(root, rel, id)
|
|
445
457
|
if (existingRel !== null) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
458
|
+
if (mode === 'replace' && args.confirm_replace !== true) {
|
|
459
|
+
return [
|
|
460
|
+
`已经有一张同名的卡:${existingRel}`,
|
|
461
|
+
'整张重写会连身份、关系、原来写过的小节一起换掉,换完就找不回来了。',
|
|
462
|
+
'确定要整张重写,就再调一次,带 mode: "replace" 和 confirm_replace: true。',
|
|
463
|
+
'只想往里补,就不用改参数——默认只补空缺,原来的字一个不动。',
|
|
464
|
+
].join('\n')
|
|
450
465
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
|
|
466
|
+
// 读旧内容 → 合并 → 写回,三件事必须在同一个队列里(`store.updateCard`)。
|
|
467
|
+
// 拆开做的话,两个窗口同时补同一张卡时,后写的那份照着它读到的旧底稿改,
|
|
468
|
+
// 前一次补的就没了——连快照里都没有,因为两次写前的旧内容一模一样。
|
|
469
|
+
let added = []
|
|
470
|
+
let filled = []
|
|
471
|
+
let nothing = false
|
|
472
|
+
const saved = await store.updateCard(root, existingRel, (already) => {
|
|
473
|
+
if (mode === 'replace') return { ok: true, data: incoming, sections }
|
|
474
|
+
const mergedData = mergeData(already.data, incoming)
|
|
475
|
+
const mergedSections = mergeSections(already.sections, sections)
|
|
476
|
+
added = mergedSections.added
|
|
477
|
+
filled = mergedData.filled
|
|
478
|
+
if (added.length === 0 && filled.length === 0) { nothing = true; return { ok: false, error: '没有可补的' } }
|
|
479
|
+
return { ok: true, data: mergedData.data, sections: mergedSections.sections }
|
|
480
|
+
})
|
|
481
|
+
if (nothing) return `这张卡已经有了,这次没有可补的:${existingRel}\n原有的内容一个字没动。\nmerged: true`
|
|
482
|
+
if (saved.ok !== true) {
|
|
483
|
+
if (saved.withdrawn === true || isWithdrawnAt(root, existingRel)) return withdrawnNote(existingRel, '卡')
|
|
484
|
+
if (saved.error === '文件不存在' || saved.code !== undefined) return `已经有一张同名的卡(${existingRel}),可这会儿读不出来:${String(saved.error ?? '读不出来')}。先把它弄好,再来写。`
|
|
485
|
+
return `没能写成:${String(saved.error ?? '存盘失败')}`
|
|
463
486
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
if (mergedSections.added.length === 0 && mergedData.filled.length === 0) {
|
|
467
|
-
return `这张卡已经有了,这次没有可补的:${existingRel}\n原有的内容一个字没动。\nmerged: true`
|
|
487
|
+
if (mode === 'replace') {
|
|
488
|
+
return `整张换掉了:${saved.path}\n${sections.length} 段,每段都对着材料里的原句核过。这次是按用户第二次确认整张覆盖写的。`
|
|
468
489
|
}
|
|
469
|
-
const saved = await store.writeCard(root, existingRel, mergedData.data, mergedSections.sections)
|
|
470
|
-
if (saved.ok !== true) return `没能写成:${String(saved.error ?? '存盘失败')}`
|
|
471
490
|
const bits = []
|
|
472
|
-
if (
|
|
473
|
-
if (
|
|
491
|
+
if (added.length > 0) bits.push(`补了 ${added.length} 节:${added.join('、')}`)
|
|
492
|
+
if (filled.length > 0) bits.push(`补了 ${filled.length} 项:${filled.join('、')}`)
|
|
474
493
|
return [
|
|
475
494
|
`补好了(没有重写):${saved.path}`,
|
|
476
495
|
...bits,
|
|
@@ -572,39 +591,46 @@ export function registerTools(ctx, config) {
|
|
|
572
591
|
|
|
573
592
|
const existingRel = await findWorld(root, rel, name)
|
|
574
593
|
if (existingRel !== null) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
594
|
+
if (mode === 'replace' && args.confirm_replace !== true) {
|
|
595
|
+
return [
|
|
596
|
+
`已经有一本同名的世界书:${existingRel}`,
|
|
597
|
+
'整份重写会把原来的条目全换掉,换完就找不回来了。',
|
|
598
|
+
'确定要整份重写,就再调一次,带 mode: "replace" 和 confirm_replace: true。',
|
|
599
|
+
'只想往里加,就不用改参数——默认只补空缺,原来的条目一个不动。',
|
|
600
|
+
].join('\n')
|
|
579
601
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
].join('\n')
|
|
602
|
+
// 同上:读旧条目 → 并新的 → 写回,得在同一个队列里。
|
|
603
|
+
let merged = null
|
|
604
|
+
let filled = []
|
|
605
|
+
const saved = await store.updateCard(root, existingRel, (already) => {
|
|
606
|
+
if (mode === 'replace') {
|
|
607
|
+
const body = entries.map((e) => renderWorldEntry(e)).join('\n')
|
|
608
|
+
return { ok: true, data: incoming, sections: [{ title: '世界书条目', body }] }
|
|
588
609
|
}
|
|
589
|
-
const
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
610
|
+
const oldBody = core.sectionBody(already.sections ?? [], '世界书条目')
|
|
611
|
+
const mergedBlocks = mergeWorldBlocks(oldBody, entries)
|
|
612
|
+
merged = mergedBlocks
|
|
613
|
+
const mergedData = mergeData(already.data, { ...incoming, entry_count: 0 })
|
|
614
|
+
mergedData.data.entry_count = mergedBlocks.total
|
|
615
|
+
filled = mergedData.filled
|
|
616
|
+
const hasSection = (already.sections ?? []).some((one) => one?.title === '世界书条目')
|
|
617
|
+
const sections = hasSection
|
|
618
|
+
? (already.sections ?? []).map((one) => (one?.title === '世界书条目' ? { title: '世界书条目', body: mergedBlocks.body } : one))
|
|
619
|
+
: [...(already.sections ?? []), { title: '世界书条目', body: mergedBlocks.body }]
|
|
620
|
+
return { ok: true, data: mergedData.data, sections }
|
|
621
|
+
})
|
|
622
|
+
if (saved.ok !== true) {
|
|
623
|
+
if (saved.withdrawn === true || isWithdrawnAt(root, existingRel)) return withdrawnNote(existingRel, '世界书')
|
|
624
|
+
if (saved.error === '文件不存在' || saved.code !== undefined) return `已经有一本同名的世界书(${existingRel}),可这会儿读不出来:${String(saved.error ?? '读不出来')}。先把它弄好,再来写。`
|
|
625
|
+
return `没能写成:${String(saved.error ?? '存盘失败')}`
|
|
626
|
+
}
|
|
627
|
+
if (mode === 'replace') {
|
|
628
|
+
return `整份换掉了:${saved.path}\n${entries.length} 条,每条都对着材料里的原句核过。这次是按用户第二次确认整份覆盖写的。`
|
|
593
629
|
}
|
|
594
|
-
const oldBody = core.sectionBody(already.sections ?? [], '世界书条目')
|
|
595
|
-
const merged = mergeWorldBlocks(oldBody, entries)
|
|
596
|
-
const mergedData = mergeData(already.data, { ...incoming, entry_count: 0 })
|
|
597
|
-
mergedData.data.entry_count = merged.total
|
|
598
|
-
const hasSection = (already.sections ?? []).some((one) => one?.title === '世界书条目')
|
|
599
|
-
const sections = hasSection
|
|
600
|
-
? (already.sections ?? []).map((one) => (one?.title === '世界书条目' ? { title: '世界书条目', body: merged.body } : one))
|
|
601
|
-
: [...(already.sections ?? []), { title: '世界书条目', body: merged.body }]
|
|
602
|
-
const saved = await store.writeCard(root, existingRel, mergedData.data, sections)
|
|
603
|
-
if (saved.ok !== true) return `没能写成:${String(saved.error ?? '存盘失败')}`
|
|
604
630
|
const bits = []
|
|
605
|
-
if (merged.added.length > 0) bits.push(`新加 ${merged.added.length} 条:${merged.added.join('、')}`)
|
|
606
|
-
if (merged.joined.length > 0) bits.push(`并进已有条目 ${merged.joined.length} 条:${merged.joined.join('、')}`)
|
|
607
|
-
if (
|
|
631
|
+
if (merged !== null && merged.added.length > 0) bits.push(`新加 ${merged.added.length} 条:${merged.added.join('、')}`)
|
|
632
|
+
if (merged !== null && merged.joined.length > 0) bits.push(`并进已有条目 ${merged.joined.length} 条:${merged.joined.join('、')}`)
|
|
633
|
+
if (filled.length > 0) bits.push(`补了 ${filled.length} 项:${filled.join('、')}`)
|
|
608
634
|
if (bits.length === 0) bits.push('这次的条目都已经在里面了')
|
|
609
635
|
return [
|
|
610
636
|
`补好了(没有重写):${saved.path}`,
|
|
@@ -625,9 +651,379 @@ export function registerTools(ctx, config) {
|
|
|
625
651
|
},
|
|
626
652
|
)
|
|
627
653
|
|
|
654
|
+
// ── 写日记顺手养卡:改「我」那张,和陪你写日记的那个助手 ────────────
|
|
655
|
+
//
|
|
656
|
+
// 日记写得越久,越会攒下卡上没有的东西:新认识的人、新的底线、一个说法
|
|
657
|
+
// 被推翻了、一件小事反复出现。这里只做两件事——把「我」那张卡补准,
|
|
658
|
+
// 把日记助手的性子按用户的原话调准。规矩和提炼成卡那组一样:没同意不写、
|
|
659
|
+
// 每一条都要指得出是哪一天哪句话,对不上整次拒绝。
|
|
660
|
+
|
|
661
|
+
register(
|
|
662
|
+
'lifecard_refine_card',
|
|
663
|
+
[
|
|
664
|
+
'写日记时顺手把两张卡养准:用户在真实人物记录里那**张「我」自己的卡**,和**陪你写日记的那个助手**。',
|
|
665
|
+
'**只有用户明确同意之后才调用。**先把打算改什么列给他看(改哪一节、改成什么、依据哪一天哪句话),',
|
|
666
|
+
'等他说「可以」,再带 approved: true 调这一次。',
|
|
667
|
+
'每一条都要附一句依据原话(quote),写之前逐字比对;对不上整次不写。',
|
|
668
|
+
'不给 approved: true 时不写任何东西,只告诉你还差哪一步。',
|
|
669
|
+
'默认只补空缺:原来写过的小节和字段一个字都不动,返回里会说补了哪几节。',
|
|
670
|
+
'只有用户明确要求整节重写时,才带 mode: "replace" 与 confirm_replace: true。',
|
|
671
|
+
].join('\n'),
|
|
672
|
+
{
|
|
673
|
+
target: {
|
|
674
|
+
type: 'string',
|
|
675
|
+
enum: ['self', 'diary'],
|
|
676
|
+
description: '改哪张卡:self 是用户自己那张(「我」),diary 是陪你写日记的那个助手。',
|
|
677
|
+
},
|
|
678
|
+
approved: { type: 'boolean', description: '用户是否已经明确同意写入。必须为 true 才会真的改卡。' },
|
|
679
|
+
from: {
|
|
680
|
+
type: 'string',
|
|
681
|
+
enum: ['diary', 'material'],
|
|
682
|
+
description: '依据从哪来:diary(默认,最近写的日记)还是 material(导入的材料)。',
|
|
683
|
+
},
|
|
684
|
+
date: { type: 'string', description: '只拿这一天的日记做依据,例如 2026-10-31。不填就看最近写的几篇。' },
|
|
685
|
+
material: { type: 'string', description: 'from 是 material 时必填:材料的路径或文件名。' },
|
|
686
|
+
name: { type: 'string', description: '这张卡还没有的时候,用它起名字。改已有的卡不用填。' },
|
|
687
|
+
fields: {
|
|
688
|
+
type: 'array',
|
|
689
|
+
description: '结构字段。每项 { key, value, quote }:key 是字段名(如 relation / tags / competency),value 是新值,quote 是依据原话。',
|
|
690
|
+
},
|
|
691
|
+
sections: {
|
|
692
|
+
type: 'array',
|
|
693
|
+
description: '正文小节。每项 { title, body, quote }:title 用已有的小节名(自述/性格/关系史/口癖/外貌/不会说的话…),body 是要写进去的话,quote 是依据原话。',
|
|
694
|
+
},
|
|
695
|
+
mode: {
|
|
696
|
+
type: 'string',
|
|
697
|
+
enum: ['merge', 'replace'],
|
|
698
|
+
description: '已有内容怎么办:merge(默认)只补空缺;replace 整份重写,必须同时带 confirm_replace: true。',
|
|
699
|
+
},
|
|
700
|
+
confirm_replace: { type: 'boolean', description: '整份重写时的第二次确认。' },
|
|
701
|
+
},
|
|
702
|
+
['target', 'approved'],
|
|
703
|
+
async (args) => {
|
|
704
|
+
const root = await resolveRoot(config)
|
|
705
|
+
const target = String(args.target ?? '').trim()
|
|
706
|
+
if (target !== 'self' && target !== 'diary') {
|
|
707
|
+
return '要说清改哪一张:target 写 self(用户自己那张)或 diary(陪你写日记的那个助手)。'
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const rowsSections = Array.isArray(args.sections) ? args.sections : []
|
|
711
|
+
const rowsFields = Array.isArray(args.fields) ? args.fields : []
|
|
712
|
+
if (rowsSections.length === 0 && rowsFields.length === 0) {
|
|
713
|
+
return '一处要改的都没有,那就没东西可写。先定下要补哪几节、哪几个字段。'
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const got = await refineSource(root, args)
|
|
717
|
+
if (got.ok !== true) return got.error
|
|
718
|
+
const source = got.text
|
|
719
|
+
|
|
720
|
+
const bad = []
|
|
721
|
+
const sections = []
|
|
722
|
+
for (const row of rowsSections) {
|
|
723
|
+
const title = String(row?.title ?? '').trim()
|
|
724
|
+
const body = String(row?.body ?? '').trim()
|
|
725
|
+
const quote = String(row?.quote ?? '').trim()
|
|
726
|
+
if (title === '' || body === '') { bad.push('(有一节没写小节名或内容是空的)'); continue }
|
|
727
|
+
if (quote === '') { bad.push(`「${title}」这一节没给原句`); continue }
|
|
728
|
+
if (!hasQuote(source, quote)) { bad.push(`「${title}」引的原句在${got.where}里找不到:${quote.slice(0, 40)}…`); continue }
|
|
729
|
+
sections.push({ title, body })
|
|
730
|
+
}
|
|
731
|
+
const fields = []
|
|
732
|
+
for (const row of rowsFields) {
|
|
733
|
+
const key = String(row?.key ?? '').trim()
|
|
734
|
+
const quote = String(row?.quote ?? '').trim()
|
|
735
|
+
if (key === '') { bad.push('(有一个字段没写字段名)'); continue }
|
|
736
|
+
if (row?.value === undefined || row?.value === null) { bad.push(`字段 ${key} 没给新值`); continue }
|
|
737
|
+
if (quote === '') { bad.push(`字段 ${key} 没给原句`); continue }
|
|
738
|
+
if (!hasQuote(source, quote)) { bad.push(`字段 ${key} 引的原句在${got.where}里找不到:${quote.slice(0, 40)}…`); continue }
|
|
739
|
+
if (!core.PROPOSAL_FIELDS.includes(key)) { bad.push(`字段 ${key} 不是卡上允许写的名字`); continue }
|
|
740
|
+
fields.push({ key, value: row.value })
|
|
741
|
+
}
|
|
742
|
+
if (bad.length > 0) {
|
|
743
|
+
return `这一版没写。下面这些对不上:\n${bad.map((b) => `- ${b}`).join('\n')}\n\n改好之后再来一次。`
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const held = target === 'self' ? await findSelfCard(root) : await findDiaryAssistant(root)
|
|
747
|
+
if (args.approved !== true) {
|
|
748
|
+
const lines = [
|
|
749
|
+
'还没写。先把这一版列给用户看,等他明确说要,再带 approved: true 重调一次。',
|
|
750
|
+
'',
|
|
751
|
+
`改哪张:${target === 'self' ? '「我」那张卡' : '日记助手那张卡'}`,
|
|
752
|
+
held === null ? '这张卡现在还没有,等你说要,会新建一张。' : `现在是:${held.card?.path ?? held.path}`,
|
|
753
|
+
`依据:${got.where}`,
|
|
754
|
+
]
|
|
755
|
+
if (sections.length > 0) lines.push('', '补这几节:', ...sections.map((x) => `- ${x.title}:${x.body.slice(0, 60)}${x.body.length > 60 ? '…' : ''}`))
|
|
756
|
+
if (fields.length > 0) lines.push('', '补这几个字段:', ...fields.map((x) => `- ${x.key}:${String(x.value).slice(0, 60)}`))
|
|
757
|
+
return lines.join('\n')
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
const mode = args.mode === 'replace' ? 'replace' : 'merge'
|
|
761
|
+
const existing = held === null ? null : held.card ?? held
|
|
762
|
+
const existingRel = existing === null ? null : (existing.path ?? held.path ?? null)
|
|
763
|
+
|
|
764
|
+
// 这里只「看一眼」有没有这张卡,真正读旧内容、合并、写回都在下面的
|
|
765
|
+
// `store.updateCard` 里一次做完——中间另起的 await 就是并发丢卡的缝。
|
|
766
|
+
let card = null
|
|
767
|
+
if (existingRel !== null) {
|
|
768
|
+
card = { path: existingRel }
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
if (card === null) {
|
|
772
|
+
const fallbackName = target === 'self' ? '我' : '日记助手'
|
|
773
|
+
const name = String(args.name ?? '').trim() === '' ? fallbackName : String(args.name).trim()
|
|
774
|
+
const id = core.safeId(name)
|
|
775
|
+
const incoming = {
|
|
776
|
+
lifecard: core.DATA_VERSION,
|
|
777
|
+
id,
|
|
778
|
+
kind: target === 'self' ? 'self' : 'fictional',
|
|
779
|
+
name,
|
|
780
|
+
world: 'reality',
|
|
781
|
+
}
|
|
782
|
+
if (target === 'self') incoming.relation = '我'
|
|
783
|
+
const rel = store.cardRelPath(id, incoming.world)
|
|
784
|
+
const heldName = await store.withdrawnNameHit(root, rel, [id, name])
|
|
785
|
+
if (isWithdrawnAt(root, rel) || heldName !== null) return withdrawnNote(heldName?.path ?? rel, '卡')
|
|
786
|
+
const mergedData = mergeData(incoming, Object.fromEntries(fields.map((f) => [f.key, f.value])))
|
|
787
|
+
const saved = await store.writeCard(root, rel, mergedData.data, sections)
|
|
788
|
+
if (saved.ok !== true) return `没能写成:${String(saved.error ?? '存盘失败')}`
|
|
789
|
+
let pinNote = ''
|
|
790
|
+
if (target === 'diary') {
|
|
791
|
+
const state = await store.readConfigState(root)
|
|
792
|
+
const pins = { ...(state.config?.default_cards ?? {}), diary: id }
|
|
793
|
+
await store.writeConfig(root, { default_cards: pins })
|
|
794
|
+
pinNote = '\n写日记已经默认用这张卡了。'
|
|
795
|
+
}
|
|
796
|
+
return [
|
|
797
|
+
`建好了一张新卡:${saved.path}`,
|
|
798
|
+
sections.length > 0 ? `写了 ${sections.length} 节:${sections.map((x) => x.title).join('、')}` : '',
|
|
799
|
+
fields.length > 0 ? `写了 ${fields.length} 个字段:${fields.map((x) => x.key).join('、')}` : '',
|
|
800
|
+
`依据:${got.where}`,
|
|
801
|
+
'每一条都对着依据原句核过。',
|
|
802
|
+
].filter((x) => x !== '').join('\n') + pinNote
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
if (mode === 'replace' && args.confirm_replace !== true) {
|
|
806
|
+
return [
|
|
807
|
+
`这张卡现在是这样:${existingRel}`,
|
|
808
|
+
'整份重写会把原来写过的内容一起换掉,换完就找不回来了。',
|
|
809
|
+
'确定要整份重写,就再调一次,带 mode: "replace" 和 confirm_replace: true。',
|
|
810
|
+
'只想往里补,就不用改参数——默认只补空缺,原来的字一个不动。',
|
|
811
|
+
].join('\n')
|
|
812
|
+
}
|
|
813
|
+
let added = []
|
|
814
|
+
let filled = []
|
|
815
|
+
let nothing = false
|
|
816
|
+
const saved = await store.updateCard(root, existingRel, (already) => {
|
|
817
|
+
if (mode === 'replace') {
|
|
818
|
+
const incoming = { ...already.data }
|
|
819
|
+
for (const f of fields) incoming[f.key] = f.value
|
|
820
|
+
return { ok: true, data: incoming, sections }
|
|
821
|
+
}
|
|
822
|
+
const incoming = Object.fromEntries(fields.map((f) => [f.key, f.value]))
|
|
823
|
+
const mergedData = mergeData(already.data, incoming)
|
|
824
|
+
const mergedSections = mergeSections(already.sections, sections)
|
|
825
|
+
added = mergedSections.added
|
|
826
|
+
filled = mergedData.filled
|
|
827
|
+
if (added.length === 0 && filled.length === 0) { nothing = true; return { ok: false, error: '没有可补的' } }
|
|
828
|
+
return { ok: true, data: mergedData.data, sections: mergedSections.sections }
|
|
829
|
+
})
|
|
830
|
+
if (nothing) return `这张卡已经有了,这次没有可补的:${existingRel}\n原有的内容一个字没动。\nmerged: true`
|
|
831
|
+
if (saved.ok !== true) {
|
|
832
|
+
if (saved.withdrawn === true || isWithdrawnAt(root, existingRel)) return withdrawnNote(existingRel, '卡')
|
|
833
|
+
if (saved.error === '文件不存在' || saved.code !== undefined) return `这张卡这会儿读不出来:${String(saved.error ?? '读不出来')}。先把它弄好,再来改。`
|
|
834
|
+
return `没能写成:${String(saved.error ?? '存盘失败')}`
|
|
835
|
+
}
|
|
836
|
+
if (mode === 'replace') return `整份换掉了:${saved.path}\n这是按用户第二次确认整份覆盖写的。`
|
|
837
|
+
const bits = []
|
|
838
|
+
if (added.length > 0) bits.push(`补了 ${added.length} 节:${added.join('、')}`)
|
|
839
|
+
if (filled.length > 0) bits.push(`补了 ${filled.length} 项:${filled.join('、')}`)
|
|
840
|
+
return [
|
|
841
|
+
`补好了(没有重写):${saved.path}`,
|
|
842
|
+
...bits,
|
|
843
|
+
`依据:${got.where}`,
|
|
844
|
+
'原来写过的内容一个字没动。',
|
|
845
|
+
'merged: true',
|
|
846
|
+
].join('\n')
|
|
847
|
+
},
|
|
848
|
+
)
|
|
849
|
+
|
|
850
|
+
// ── 写今天这一篇 ──────────────────────────────────────────────────
|
|
851
|
+
//
|
|
852
|
+
// 为什么必须有个工具:日记是在**新会话**里聊的,聊天框里的字不会自己变成
|
|
853
|
+
// 文件。没有这个工具时,用户陪着 AI 聊完一整篇,那一篇只活在对话记录里,
|
|
854
|
+
// 磁盘上一个字都没有——「每天一篇存在自己文件夹里」这句承诺是空的。
|
|
855
|
+
//
|
|
856
|
+
// 它和上面几把工具不是一类:不改别人的卡、不改已经定下来的东西,只把用户
|
|
857
|
+
// 这一回亲口讲的话落到他指定的那一天。所以不要 approved,但**绝不静默覆盖**:
|
|
858
|
+
// 这一天已经有内容时,凡是「原来有、这次要写空的」小节都拦下来报给模型。
|
|
859
|
+
register(
|
|
860
|
+
'lifecard_diary_save',
|
|
861
|
+
[
|
|
862
|
+
'把今天这一篇日记存进用户自己的文件夹(一天一篇,文件名就是日期)。',
|
|
863
|
+
'**用户讲完、你把这五节收好之后**再调;不要边聊边存,也不要替他写。',
|
|
864
|
+
'section 的 title 只认这五个:今天最值得讲的一件事 / 当时在想什么 / 情绪往下三层 / 我做了什么决定 / 下一步。',
|
|
865
|
+
'「下一步」一条一行,写成他能勾掉的事。',
|
|
866
|
+
'这一天原来写过东西时,只会更新;凡是要把原来写的东西弄空的,会拦下来让你先问用户。',
|
|
867
|
+
].join('\n'),
|
|
868
|
+
{
|
|
869
|
+
date: { type: 'string', description: '哪一天,例如 2026-10-31。不填就是今天。' },
|
|
870
|
+
sections: {
|
|
871
|
+
type: 'array',
|
|
872
|
+
description: '五节正文。每项 { title, body }:title 用那五个小节名之一,body 是用户自己说的话(不要替他润色成书面语)。',
|
|
873
|
+
},
|
|
874
|
+
scores: {
|
|
875
|
+
type: 'object',
|
|
876
|
+
description: '可选:健康 / 工作 / 关系 / 玩 各一个 0 到 10 的整数。用户不想打就别传。',
|
|
877
|
+
},
|
|
878
|
+
confirm_overwrite: {
|
|
879
|
+
type: 'boolean',
|
|
880
|
+
description: '这一天原来有内容、这次要少写几节时,问过用户、他确认要删,才传 true。',
|
|
881
|
+
},
|
|
882
|
+
},
|
|
883
|
+
['sections'],
|
|
884
|
+
async (args) => {
|
|
885
|
+
const root = await resolveRoot(config)
|
|
886
|
+
const state = await store.readConfigState(root)
|
|
887
|
+
if (state.ok !== true) return '读不出来日记放在哪儿。'
|
|
888
|
+
const dir = diaryRootFrom(state.config)
|
|
889
|
+
if (dir === '') {
|
|
890
|
+
return '还没有挑日记放在哪个文件夹。让用户在首页点一次「写日记」,挑好位置之后再存。'
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
const date = String(args.date ?? '').trim() === '' ? store.localDate() : String(args.date).trim()
|
|
894
|
+
const rows = Array.isArray(args.sections) ? args.sections : []
|
|
895
|
+
if (rows.length === 0) return '一节都没有,那就没东西可存。'
|
|
896
|
+
|
|
897
|
+
const wanted = {}
|
|
898
|
+
const unknown = []
|
|
899
|
+
for (const row of rows) {
|
|
900
|
+
const title = matchSection(String(row?.title ?? '').trim())
|
|
901
|
+
const body = Array.isArray(row?.body)
|
|
902
|
+
? row.body.map((x) => String(x ?? '').trim()).filter((x) => x !== '').join('\n')
|
|
903
|
+
: String(row?.body ?? '').trim()
|
|
904
|
+
if (title === '') { unknown.push(String(row?.title ?? '')); continue }
|
|
905
|
+
// 同一节写了两遍就接起来,别让后一段悄悄盖掉前一段。
|
|
906
|
+
wanted[title] = wanted[title] === undefined || wanted[title] === '' ? body : `${wanted[title]}\n${body}`
|
|
907
|
+
}
|
|
908
|
+
if (unknown.length > 0) {
|
|
909
|
+
return [
|
|
910
|
+
`这几节的标题我没认出来:${unknown.join('、')}`,
|
|
911
|
+
`只认这五个:${DIARY_SECTIONS.join(' / ')}。`,
|
|
912
|
+
'翻成上面的说法再来一次;实在有第六件事要说,就并进「今天最值得讲的一件事」。',
|
|
913
|
+
].join('\n')
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
const scores = {}
|
|
917
|
+
const rawScores = args.scores !== null && typeof args.scores === 'object' ? args.scores : {}
|
|
918
|
+
for (const [key, value] of Object.entries(rawScores)) {
|
|
919
|
+
const n = Number(value)
|
|
920
|
+
if (!Number.isFinite(n)) continue
|
|
921
|
+
scores[String(key).trim()] = Math.max(0, Math.min(10, Math.round(n)))
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
const existing = await readDiary(dir, date)
|
|
925
|
+
if (existing.ok !== true) {
|
|
926
|
+
return `这一天读不出来,为了不把原来的东西弄丢,这次没写:${String(existing.error ?? '')}`
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
const before = existing.data === null ? {} : existing.data
|
|
930
|
+
// 用户手改过这篇:多出来的小节、自己在 frontmatter 里加的键,写回去都会原样
|
|
931
|
+
// 带着走(这一层在 writeDiary 里),所以不再为这个拦人,只在结果里说一句。
|
|
932
|
+
const known = new Set([...DIARY_SECTIONS, '今天这四样'])
|
|
933
|
+
const extra = (existing.sections ?? [])
|
|
934
|
+
.filter((s) => s?.title !== '_intro' && !known.has(String(s?.title ?? '')) && String(s?.body ?? '').trim() !== '')
|
|
935
|
+
.map((s) => String(s.title))
|
|
936
|
+
|
|
937
|
+
// 会动到旧内容的三件事都得先问用户:
|
|
938
|
+
// ① 把一节弄空 ② 一节这次写了另一段(旧那段会被换掉)③ 改掉原来打的分
|
|
939
|
+
const losing = []
|
|
940
|
+
const replacing = []
|
|
941
|
+
for (const title of DIARY_SECTIONS) {
|
|
942
|
+
if (!Object.hasOwn(wanted, title)) continue
|
|
943
|
+
const old = before[title]
|
|
944
|
+
const oldText = Array.isArray(old) ? old.join('\n') : String(old ?? '')
|
|
945
|
+
const newText = Array.isArray(wanted[title]) ? wanted[title].join('\n') : String(wanted[title] ?? '')
|
|
946
|
+
if (oldText.trim() === '') continue
|
|
947
|
+
if (newText.trim() === '') { losing.push(title); continue }
|
|
948
|
+
// 接着往下写(新内容把原来那段原样带在开头、后面又加了几句)不算换掉,
|
|
949
|
+
// 那正是「一天里接着写」该有的样子;整段重写才拦。
|
|
950
|
+
if (squash(newText).startsWith(squash(oldText))) continue
|
|
951
|
+
replacing.push(title)
|
|
952
|
+
}
|
|
953
|
+
const oldScores = before.scores !== null && typeof before.scores === 'object' ? before.scores : {}
|
|
954
|
+
const rescoring = Object.keys(scores).filter((name) => oldScores[name] !== undefined && Number(oldScores[name]) !== Number(scores[name]))
|
|
955
|
+
|
|
956
|
+
if ((losing.length > 0 || replacing.length > 0 || rescoring.length > 0) && args.confirm_overwrite !== true) {
|
|
957
|
+
const lines = [`${date} 这一天已经有内容了,这次没写:`]
|
|
958
|
+
if (losing.length > 0) lines.push(`会弄空这几节:${losing.join('、')}`)
|
|
959
|
+
if (replacing.length > 0) lines.push(`会换掉这几节原来写的:${replacing.join('、')}`)
|
|
960
|
+
if (rescoring.length > 0) lines.push(`会改掉这几样原来的分:${rescoring.join('、')}`)
|
|
961
|
+
lines.push('先问一句用户:是改这一天,还是另起一天。他确认要改,再带 confirm_overwrite: true 重调一次。')
|
|
962
|
+
lines.push('想保留原来那段就接着写、把新内容接在后面,别整段重写。')
|
|
963
|
+
return lines.join('\n')
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
const payload = { ...wanted, date }
|
|
967
|
+
// 分数一格一格地算:这次只说了两个格子,另外两个不该被抹掉。
|
|
968
|
+
const mergedScores = { ...oldScores, ...scores }
|
|
969
|
+
if (Object.keys(mergedScores).length > 0) payload.scores = mergedScores
|
|
970
|
+
// 已经写过、这次没提到的节,把原来的留着——「接着写」不该把上午写的删掉。
|
|
971
|
+
for (const title of DIARY_SECTIONS) {
|
|
972
|
+
if (payload[title] !== undefined) continue
|
|
973
|
+
const old = before[title]
|
|
974
|
+
if (Array.isArray(old) ? old.length > 0 : String(old ?? '').trim() !== '') payload[title] = old
|
|
975
|
+
}
|
|
976
|
+
// 心情那行工具不管,也不该因为这次没提到就没了。
|
|
977
|
+
if (payload.mood === undefined && String(before.mood ?? '').trim() !== '') payload.mood = before.mood
|
|
978
|
+
|
|
979
|
+
// 用户点了「就改这一天」,那就照他说的改:写盘那一层默认会把「这次没写的节」
|
|
980
|
+
// 从旧文件里补回来(对「接着写」是对的),补回来就清不掉了。所以确认过之后
|
|
981
|
+
// 关掉那一层——要留的节上面已经一条条补进 payload 了。
|
|
982
|
+
const mergeOld = args.confirm_overwrite !== true
|
|
983
|
+
const saved = await writeDiary(dir, payload, new Date(), { expect: { text: existing.text }, merge: mergeOld })
|
|
984
|
+
if (saved.ok !== true) {
|
|
985
|
+
if (saved.conflict === true) {
|
|
986
|
+
return [
|
|
987
|
+
'这一天在我们说话的时候被改过了(可能是你自己在访达里动的)。',
|
|
988
|
+
'为了不把你外面改的内容盖掉,这次没存。重新打开这一篇、把要补的那几节再说一遍就行。',
|
|
989
|
+
].join('\n')
|
|
990
|
+
}
|
|
991
|
+
return `没能存下来:${String(saved.error ?? '写盘失败')}`
|
|
992
|
+
}
|
|
993
|
+
const wrote = DIARY_SECTIONS.filter((title) => {
|
|
994
|
+
const value = payload[title]
|
|
995
|
+
return Array.isArray(value) ? value.length > 0 : String(value ?? '').trim() !== ''
|
|
996
|
+
})
|
|
997
|
+
return [
|
|
998
|
+
`${existing.data === null ? '存好了' : '这一篇更新好了'}:${path.join(dir, saved.path)}`,
|
|
999
|
+
`写了 ${wrote.length} 节:${wrote.join('、')}`,
|
|
1000
|
+
existing.data === null ? '' : '这一篇原来写的内容,这次没提到的都留着。',
|
|
1001
|
+
extra.length > 0 ? `你自己在这一篇里加的那几节(${extra.join('、')})原样留着,没动。` : '',
|
|
1002
|
+
saved.snapshot !== '' ? '改之前的旧版本另存了一份,在同一个文件夹的「.lifecard/旧版本」里。' : '',
|
|
1003
|
+
'用户自己用访达就能打开改;下次再点「写日记」会接着这一篇。',
|
|
1004
|
+
].filter((x) => x !== '').join('\n')
|
|
1005
|
+
},
|
|
1006
|
+
)
|
|
1007
|
+
|
|
628
1008
|
return true
|
|
629
1009
|
}
|
|
630
1010
|
|
|
1011
|
+
/** 把模型写的小节名对上那五个固定的说法(多写几个字、少写几个字都认)。 */
|
|
1012
|
+
function matchSection(title) {
|
|
1013
|
+
const want = String(title ?? '').trim()
|
|
1014
|
+
if (want === '') return ''
|
|
1015
|
+
if (DIARY_SECTIONS.includes(want)) return want
|
|
1016
|
+
const rules = [
|
|
1017
|
+
['今天最值得讲的一件事', /最值得讲|那件事|今天的事/],
|
|
1018
|
+
['当时在想什么', /在想什么|当时想|心里想/],
|
|
1019
|
+
['情绪往下三层', /三层|情绪/],
|
|
1020
|
+
['我做了什么决定', /决定|怎么办/],
|
|
1021
|
+
['下一步', /下一步|接下来|要做的事/],
|
|
1022
|
+
]
|
|
1023
|
+
for (const [canon, pattern] of rules) if (pattern.test(want)) return canon
|
|
1024
|
+
return ''
|
|
1025
|
+
}
|
|
1026
|
+
|
|
631
1027
|
/** 比对原句时先把空白和换行抹平:材料里的一句常被折行,逐字比会假失败。 */
|
|
632
1028
|
function squash(text) {
|
|
633
1029
|
return String(text ?? '').replace(/\s+/g, '')
|
|
@@ -636,10 +1032,89 @@ function squash(text) {
|
|
|
636
1032
|
/** 材料里到底有没有这句话。宁可放过折行,绝不放过「差不多」。 */
|
|
637
1033
|
function hasQuote(source, quote) {
|
|
638
1034
|
const q = squash(quote)
|
|
639
|
-
|
|
1035
|
+
// 两三个字太容易碰上了:`quote: '今天'` 能对上任何一段日记,
|
|
1036
|
+
// 于是「每条都要有原文引文」这道门就成了摆设。太短的一律不算。
|
|
1037
|
+
if (q.length < MIN_QUOTE_CHARS) return false
|
|
640
1038
|
return squash(source).includes(q)
|
|
641
1039
|
}
|
|
642
1040
|
|
|
1041
|
+
/**
|
|
1042
|
+
* 反哺卡时用的「依据」:一串能逐字比对的原文,外加一句「这是从哪儿来的」。
|
|
1043
|
+
*
|
|
1044
|
+
* 用户没挑日子就看最近写的三篇(和日记助手看到的一样);挑了日子就只认那一天,
|
|
1045
|
+
* 那一天的日记读不出来时宁可报错,也不退回「看最近几篇」——退回去就会
|
|
1046
|
+
* 拿着一篇不相干的日记改卡,而且改完看不出来。
|
|
1047
|
+
*/
|
|
1048
|
+
async function refineSource(root, args) {
|
|
1049
|
+
const from = String(args.from ?? 'diary').trim()
|
|
1050
|
+
if (from === 'material') {
|
|
1051
|
+
const materials = await store.readMaterials(root)
|
|
1052
|
+
const picked = pickMaterial(materials, args.material)
|
|
1053
|
+
if (picked === null) return { ok: false, error: `没找到「${String(args.material ?? '')}」这份材料,先确认材料名。` }
|
|
1054
|
+
return { ok: true, text: String(materials[picked] ?? ''), where: `材料「${picked}」` }
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
const state = await store.readConfigState(root)
|
|
1058
|
+
if (state.ok !== true) return { ok: false, error: '读不出来日记放在哪儿。' }
|
|
1059
|
+
const dir = diaryRootFrom(state.config)
|
|
1060
|
+
if (dir === '') return { ok: false, error: '还没有挑日记放在哪个文件夹,先让用户在首页点一次「写日记」。' }
|
|
1061
|
+
|
|
1062
|
+
const want = String(args.date ?? '').trim()
|
|
1063
|
+
if (want !== '') {
|
|
1064
|
+
const got = await readDiary(dir, want)
|
|
1065
|
+
if (got.ok !== true) return { ok: false, error: String(got.error ?? '这一天的日记读不出来。') }
|
|
1066
|
+
if (got.data === null) return { ok: false, error: `没有 ${want} 这一天写的日记。` }
|
|
1067
|
+
return { ok: true, text: diaryBlock(got.data), where: `${want} 这一篇日记` }
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
const listed = await listDiaries(dir, { limit: 3 })
|
|
1071
|
+
if (listed.ok !== true) return { ok: false, error: String(listed.error ?? '日记列不出来。') }
|
|
1072
|
+
if (listed.diaries.length === 0) return { ok: false, error: '还一篇日记都没写过,没有可以当依据的东西。' }
|
|
1073
|
+
const parts = []
|
|
1074
|
+
const dates = []
|
|
1075
|
+
for (const item of listed.diaries) {
|
|
1076
|
+
const got = await readDiary(dir, item.date)
|
|
1077
|
+
if (got.ok !== true || got.data === null) continue
|
|
1078
|
+
parts.push(`【${item.date}】\n${diaryBlock(got.data)}`)
|
|
1079
|
+
dates.push(item.date)
|
|
1080
|
+
}
|
|
1081
|
+
if (parts.length === 0) return { ok: false, error: '最近这几篇日记都读不出来。' }
|
|
1082
|
+
return { ok: true, text: parts.join('\n\n'), where: `日记(${dates.join('、')})` }
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/** 用户自己那张卡(kind 是 self)。没有回 null。 */
|
|
1086
|
+
async function findSelfCard(root) {
|
|
1087
|
+
const all = await store.listAll(root)
|
|
1088
|
+
const cards = Array.isArray(all.cards) ? all.cards : []
|
|
1089
|
+
const hit = cards.find((c) => String(c.kind ?? '') === 'self')
|
|
1090
|
+
if (hit === undefined) {
|
|
1091
|
+
// 老档案里「我」常常是 kind:real + 某个关系词(用户自己就是这么建的)。
|
|
1092
|
+
// 认它,但只在没有真正的 self 卡时认。`relation` 这一格有人写「我」,
|
|
1093
|
+
// 也有人写「本人」「自己」「我自己」——只认第一个的话,他会建出
|
|
1094
|
+
// 第二张「我」,然后两张卡各写一半。
|
|
1095
|
+
const SELF_WORDS = new Set(['我', '本人', '自己', '我自己', '本人自己'])
|
|
1096
|
+
const looksSelf = (c) => SELF_WORDS.has(String(c.relation ?? '').trim()) && String(c.kind ?? '') === 'real'
|
|
1097
|
+
const byRelation = cards.find(looksSelf)
|
|
1098
|
+
if (byRelation === undefined) return null
|
|
1099
|
+
const read = await store.readCard(root, byRelation.path)
|
|
1100
|
+
return read.ok === true ? read : null
|
|
1101
|
+
}
|
|
1102
|
+
const read = await store.readCard(root, hit.path)
|
|
1103
|
+
return read.ok === true ? read : null
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/** 现在陪用户写日记的那个助手。没挑过就按「日记助手」这个名字找。回 null 表示还没有。 */
|
|
1107
|
+
async function findDiaryAssistant(root) {
|
|
1108
|
+
const state = await store.readConfigState(root)
|
|
1109
|
+
const pin = String(state.config?.default_cards?.diary ?? '').trim()
|
|
1110
|
+
if (pin !== '') {
|
|
1111
|
+
const card = await findCard(root, pin)
|
|
1112
|
+
if (card !== null) return card
|
|
1113
|
+
}
|
|
1114
|
+
const named = await findCard(root, '日记助手')
|
|
1115
|
+
return named === null ? null : named
|
|
1116
|
+
}
|
|
1117
|
+
|
|
643
1118
|
/** 按路径、文件名、或独一无二的片段找那份材料。找不到回 null。 */
|
|
644
1119
|
function pickMaterial(materials, want) {
|
|
645
1120
|
const key = String(want ?? '').trim()
|