dsh-lifecard 0.2.0 → 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.
- package/cordis.patch.yml +171 -2
- package/lib/client.js +1191 -132
- package/lib/codec.js +287 -1
- package/lib/core.js +87 -1
- package/lib/index.js +812 -26
- package/lib/novel.js +10 -2
- package/lib/presets.js +35 -0
- package/lib/prose.js +253 -0
- package/lib/store.js +584 -15
- package/lifecard-tools.js +805 -0
- package/package.json +9 -3
- package/skills/blind-review-loop/SKILL.md +205 -0
- package/skills/human-writing/LICENSE +21 -0
- package/skills/human-writing/SKILL.md +306 -0
- package/skills/human-writing/VERSION +1 -0
- package/skills/human-writing/agents/openai.yaml +4 -0
- package/skills/human-writing/dist/human-writing-lite.md +29 -0
- package/skills/human-writing/references/essay-or-fiction.md +79 -0
- package/skills/human-writing/references/fiction.md +115 -0
- package/skills/human-writing/references/formats.md +99 -0
- package/skills/human-writing/references/forum-prose.md +349 -0
- package/skills/human-writing/references/reality.md +119 -0
- package/skills/human-writing/references/revision.md +228 -0
- package/skills/human-writing/references/story-diagnosis.md +131 -0
- package/skills/human-writing/references/writing-ceiling.md +151 -0
- package/skills/human-writing/scripts/check_prose.py +864 -0
- package/skills/lifecard-novel-writing/SKILL.md +88 -0
- package/skills/visual-novel/SKILL.md +139 -0
- package/skills/visual-novel/references/script-and-flow.md +89 -0
package/lib/codec.js
CHANGED
|
@@ -481,6 +481,259 @@ export function encodeCardJson(card, asV3 = false) {
|
|
|
481
481
|
* @param {string} [fileName]
|
|
482
482
|
* @returns {{ json: any, kind: string, source: string, rawFiles?: Record<string, Uint8Array> }}
|
|
483
483
|
*/
|
|
484
|
+
/**
|
|
485
|
+
* 文本角色卡里常见的字段写法。
|
|
486
|
+
* 中文社区的 txt 卡八成是「名字:…/性格:…」这种一行一个字段,
|
|
487
|
+
* 也有用 Markdown 小标题的,所以两种都认。
|
|
488
|
+
*/
|
|
489
|
+
const TEXT_FIELDS = [
|
|
490
|
+
['name', ['名字', '姓名', '名称', '角色名', '人名', '角色', '人物', '主角', 'name', 'char_name', 'character name']],
|
|
491
|
+
['description', ['描述', '设定', '人物设定', '人设', '简介', '介绍', '背景', '人物背景', '背景设定', '角色设定', 'profile', 'description', 'desc', 'persona']],
|
|
492
|
+
['personality', ['性格', '个性', 'personality']],
|
|
493
|
+
['scenario', ['场景', '情景', 'scenario']],
|
|
494
|
+
['first_mes', ['开场白', '开场', '第一句话', '问候语', '初次见面', 'first_mes', 'first message', 'greeting', 'first_message']],
|
|
495
|
+
['mes_example', ['对话示例', '示例对话', '对话范例', '例句', '示例', 'mes_example', 'example dialogue', 'examples']],
|
|
496
|
+
['creator_notes', ['创作者备注', '备注', '说明', 'creator_notes', 'notes', 'creator comment']],
|
|
497
|
+
['appearance', ['外貌', '长相', '外观', '穿着', 'appearance', 'looks']],
|
|
498
|
+
['speech', ['说话方式', '口癖', '语气', '语癖', '说话风格', 'speech', 'voice', 'tone']],
|
|
499
|
+
['relationship', ['关系', '与我的关系', '身份关系', 'relationship']],
|
|
500
|
+
['world', ['世界观', '世界', 'world', 'worldview']],
|
|
501
|
+
]
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* 提示词式的卡(网上下的「破限/人设」txt 常见)没有「名字:性格:」这种字段,
|
|
505
|
+
* 整个文件被包在一两个外层标签里。这些标签名当作「整段放哪儿」的意思来认。
|
|
506
|
+
*/
|
|
507
|
+
const TEXT_WRAPPER_FIELDS = [
|
|
508
|
+
['description', ['project_instructions', 'projectinstructions', 'system', 'system_prompt', 'persona', 'character', 'char', 'instruction', 'instructions', 'prompt', 'main', 'profile', 'context']],
|
|
509
|
+
['creator_notes', ['user_style', 'userstyle', 'style', 'style_notes', 'stylesheet', 'notes', 'author_notes', 'postscript', 'appendix']],
|
|
510
|
+
['mes_example', ['examples', 'dialogues', 'sample_dialogue', 'sample_messages']],
|
|
511
|
+
['first_mes', ['greeting', 'opening', 'first_message']],
|
|
512
|
+
]
|
|
513
|
+
|
|
514
|
+
/** 外层容器标签对到「整段放哪个字段」;认不出返回 null。 */
|
|
515
|
+
function textWrapperOf(rawKey) {
|
|
516
|
+
const key = String(rawKey ?? '').replace(/[\s\u3000]+/g, '').replace(/[-_]+/g, '_').toLowerCase()
|
|
517
|
+
if (key === '') return null
|
|
518
|
+
for (const [field, aliases] of TEXT_WRAPPER_FIELDS) {
|
|
519
|
+
if (aliases.includes(key)) return field
|
|
520
|
+
}
|
|
521
|
+
return null
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** 「外貌」「口癖」这些没有独立小节的,拼进自述里,加一个标签。 */
|
|
525
|
+
const TEXT_EXTRA_FIELDS = new Set(['appearance', 'speech', 'relationship', 'world', 'extra'])
|
|
526
|
+
|
|
527
|
+
/** 把一个标题/键名对到字段名上;认不出返回 null。 */
|
|
528
|
+
function textFieldOf(rawKey) {
|
|
529
|
+
const key = String(rawKey ?? '')
|
|
530
|
+
.replace(/\{\{\s*(char|user|bot|character)\s*\}\}/gi, '')
|
|
531
|
+
.replace(/^(角色|人物|主角|TA|他|她)的/, '')
|
|
532
|
+
.replace(/[\s\u3000]+/g, '')
|
|
533
|
+
.replace(/^[#*>\-\s]+/, '')
|
|
534
|
+
.replace(/[::]+$/, '')
|
|
535
|
+
.toLowerCase()
|
|
536
|
+
if (key === '') return null
|
|
537
|
+
for (const [field, aliases] of TEXT_FIELDS) {
|
|
538
|
+
if (aliases.includes(key)) return field
|
|
539
|
+
}
|
|
540
|
+
return null
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* 把一份纯文本角色卡读成 CC v2 的 JSON 形状。
|
|
545
|
+
*
|
|
546
|
+
* 认三种写法:
|
|
547
|
+
* 1) 整份就是 JSON(可能在 ``` 里包着,常见于把 .json 改名成 .txt)
|
|
548
|
+
* 2) 「名字:张三 / 性格:沉默」一行一个字段(也认 `【名字】` 单独占一行的写法)
|
|
549
|
+
* 3) Markdown 小标题(`## 性格`)
|
|
550
|
+
* 4) `<名字>…</名字>` 这种带尖括号标签的写法
|
|
551
|
+
* 认不出的键不丢,按「标签:内容」拼进自述;一行都没有的散文返回 null,
|
|
552
|
+
* 由调用方给出「先当材料导进来」的提示。
|
|
553
|
+
* @param {string} raw
|
|
554
|
+
* @returns {{ json: any, source: string } | null}
|
|
555
|
+
*/
|
|
556
|
+
export function parseTextCard(raw, options = {}) {
|
|
557
|
+
const text = String(raw ?? '').replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n').trim()
|
|
558
|
+
if (text === '') return null
|
|
559
|
+
|
|
560
|
+
// 1) 整份就是 JSON
|
|
561
|
+
const fenced = text.match(/^```(?:json|javascript)?\s*\n([\s\S]*?)\n```$/i)
|
|
562
|
+
const jsonBody = (fenced !== null ? fenced[1] : text).trim()
|
|
563
|
+
if (jsonBody.startsWith('{') || jsonBody.startsWith('[')) {
|
|
564
|
+
try {
|
|
565
|
+
const parsed = JSON.parse(jsonBody)
|
|
566
|
+
return { json: parsed, source: 'text-json' }
|
|
567
|
+
} catch {
|
|
568
|
+
/* 不是合法 JSON,继续按字段读 */
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// 顶上单独一行的一级标题(`# 林小雨`)当名字兜底:
|
|
573
|
+
// 后面再接「## 描述」这种小标题时,它本身对不上任何字段,但显然是名字。
|
|
574
|
+
const topHeading = /^\s*#\s+(\S.*)$/m.exec(text.split('\n').filter((l) => l.trim() !== '')[0] ?? '')
|
|
575
|
+
const fileStem = String(options?.fileName ?? '').replace(/\.[^.]+$/, '').trim()
|
|
576
|
+
const nameFallback = topHeading !== null && textFieldOf(topHeading[1]) === null ? topHeading[1].trim() : fileStem
|
|
577
|
+
|
|
578
|
+
// 2) 一行一个字段 / Markdown 小标题
|
|
579
|
+
const entries = []
|
|
580
|
+
let current = null
|
|
581
|
+
let sawKnown = false
|
|
582
|
+
for (const rawLine of text.split('\n')) {
|
|
583
|
+
const line = rawLine.replace(/\u3000/g, ' ').trimEnd()
|
|
584
|
+
const trimmed = line.trim()
|
|
585
|
+
if (trimmed === '') {
|
|
586
|
+
if (current !== null) current.lines.push('')
|
|
587
|
+
continue
|
|
588
|
+
}
|
|
589
|
+
const heading = /^#{1,4}\s*(.+?)\s*#*$/.exec(trimmed)
|
|
590
|
+
const bracket = /^[【\[](.{1,12}?)[】\]]\s*[::]?\s*(.*)$/.exec(trimmed)
|
|
591
|
+
const colon = /^(.{1,12}?)\s*[::]\s*(.*)$/.exec(trimmed)
|
|
592
|
+
let key = null
|
|
593
|
+
let inline = ''
|
|
594
|
+
if (heading !== null) {
|
|
595
|
+
key = heading[1]
|
|
596
|
+
} else if (bracket !== null) {
|
|
597
|
+
key = bracket[1]
|
|
598
|
+
inline = bracket[2]
|
|
599
|
+
} else if (colon !== null) {
|
|
600
|
+
key = colon[1]
|
|
601
|
+
inline = colon[2]
|
|
602
|
+
}
|
|
603
|
+
// 对话行(`{{user}}:…`、`<旁白>:…`)里的冒号不是字段分隔符
|
|
604
|
+
const keyLooksLikeLabel = key !== null
|
|
605
|
+
&& !/[{}<>\[\]"'“”‘’「」《》]/.test(key)
|
|
606
|
+
&& !/\s/.test(String(key).trim())
|
|
607
|
+
if (key !== null && !keyLooksLikeLabel) key = null
|
|
608
|
+
if (key !== null) {
|
|
609
|
+
const field = textFieldOf(key)
|
|
610
|
+
if (field !== null) {
|
|
611
|
+
sawKnown = true
|
|
612
|
+
current = { field, key: String(key).trim(), lines: inline.trim() === '' ? [] : [inline] }
|
|
613
|
+
entries.push(current)
|
|
614
|
+
continue
|
|
615
|
+
}
|
|
616
|
+
// 认不出的键:只有在已经确认这是张卡(见过已知字段)之后才当标签,否则当成正文
|
|
617
|
+
if (sawKnown && !/^[\s"'“”‘’《》()(),。!?、;]/.test(key)) {
|
|
618
|
+
current = { field: 'extra', key: String(key).trim(), lines: inline.trim() === '' ? [] : [inline] }
|
|
619
|
+
entries.push(current)
|
|
620
|
+
continue
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (current !== null) current.lines.push(line)
|
|
624
|
+
else if (trimmed !== '') {
|
|
625
|
+
current = { field: 'extra', key: '', lines: [line] }
|
|
626
|
+
entries.push(current)
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
// 2) <标签>…</标签> 的写法(不少 txt 卡其实是当 XML 写的,中英标签都认)
|
|
630
|
+
const tagEntries = []
|
|
631
|
+
// `<名字>…</名字>`,以及带属性的 `<char name="张三">…</char>`
|
|
632
|
+
const tagRe = /<([A-Za-z\u4e00-\u9fff][\w\u4e00-\u9fff-]{0,30})([^>]*)>([\s\S]*?)<\/\1>/g
|
|
633
|
+
let tagHit = tagRe.exec(text)
|
|
634
|
+
while (tagHit !== null) {
|
|
635
|
+
const tagName = tagHit[1]
|
|
636
|
+
const attrText = tagHit[2]
|
|
637
|
+
// 标签里紧跟着写了个冒号(`<名字>:阿澜`)也认——那只是在抄「字段:值」的习惯
|
|
638
|
+
const body = tagHit[3].replace(/^\s*[::]?\s*/, '').trimEnd()
|
|
639
|
+
// `<char name="沈九">正文</char>`:属性给名字,这一段正文算自述
|
|
640
|
+
const attrName = /name\s*=\s*["']?([^"'>]+)["']?/i.exec(attrText)
|
|
641
|
+
const attrIsName = attrName !== null && /^(char|character|character_card|bot|persona|card)$/i.test(tagName)
|
|
642
|
+
if (attrIsName) tagEntries.push({ field: 'name', key: tagName, lines: [attrName[1].trim()] })
|
|
643
|
+
let field = textFieldOf(tagName)
|
|
644
|
+
if (field === null) field = textWrapperOf(tagName)
|
|
645
|
+
if (field === null && attrIsName) field = 'description'
|
|
646
|
+
if (field !== null) {
|
|
647
|
+
tagEntries.push({ field, key: tagName, lines: body === '' ? [] : body.split('\n') })
|
|
648
|
+
} else if (body !== '') {
|
|
649
|
+
tagEntries.push({ field: 'extra', key: tagName, lines: body.split('\n') })
|
|
650
|
+
}
|
|
651
|
+
tagHit = tagRe.exec(text)
|
|
652
|
+
}
|
|
653
|
+
const tagHasKnown = tagEntries.some((e) => e.field !== 'extra')
|
|
654
|
+
if (tagHasKnown) {
|
|
655
|
+
const made = assemble(tagEntries, nameFallback)
|
|
656
|
+
if (made !== null) return made
|
|
657
|
+
}
|
|
658
|
+
// 有 <标签>…</标签> 的结构、但一个字段都对不上:整份当自述收进来,别让人卡在「读不进去」。
|
|
659
|
+
// 名字取文件名(用户挑一份叫「张三.txt」的卡时,这几乎总是对的)。
|
|
660
|
+
if (tagEntries.length > 0) {
|
|
661
|
+
const stem = String(options?.fileName ?? '').replace(/\.[^.]+$/, '').trim()
|
|
662
|
+
const name = tagEntries.find((e) => e.key !== '')?.lines?.[0]?.trim() ?? ''
|
|
663
|
+
const body = text.trim()
|
|
664
|
+
if (body !== '') {
|
|
665
|
+
return {
|
|
666
|
+
json: {
|
|
667
|
+
spec: 'chara_card_v2',
|
|
668
|
+
spec_version: '2.0',
|
|
669
|
+
data: {
|
|
670
|
+
name: stem === '' ? (name === '' ? '未命名' : name) : stem,
|
|
671
|
+
description: body,
|
|
672
|
+
personality: '',
|
|
673
|
+
scenario: '',
|
|
674
|
+
first_mes: '',
|
|
675
|
+
mes_example: '',
|
|
676
|
+
creator_notes: '这份是提示词式的卡:里面没有「名字:」「性格:」这种分字段的写法,所以原文整份收在自述里,名字用的是文件名。想拆成小节可以手动改。',
|
|
677
|
+
tags: [],
|
|
678
|
+
system_prompt: '',
|
|
679
|
+
post_history_instructions: '',
|
|
680
|
+
alternate_greetings: [],
|
|
681
|
+
},
|
|
682
|
+
},
|
|
683
|
+
source: 'text-raw',
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (!sawKnown || entries.length === 0) return null
|
|
689
|
+
|
|
690
|
+
return assemble(entries, nameFallback)
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* 把读到的字段整理成 CC v2 的 JSON 形状。
|
|
695
|
+
* 两种入口(`<标签>` 与「一行一个字段」)都走这里,省得两套拼装逻辑慢慢走岔。
|
|
696
|
+
*/
|
|
697
|
+
function assemble(entries, nameFallback) {
|
|
698
|
+
const take = (field) => {
|
|
699
|
+
const found = entries.filter((e) => e.field === field)
|
|
700
|
+
if (found.length === 0) return ''
|
|
701
|
+
return found.map((e) => e.lines.join('\n').trim()).filter((v) => v !== '').join('\n\n').trim()
|
|
702
|
+
}
|
|
703
|
+
const extraBlocks = []
|
|
704
|
+
for (const entry of entries) {
|
|
705
|
+
if (!TEXT_EXTRA_FIELDS.has(entry.field)) continue
|
|
706
|
+
const value = entry.lines.join('\n').trim()
|
|
707
|
+
if (value === '') continue
|
|
708
|
+
if (entry.key === '') extraBlocks.push(value)
|
|
709
|
+
else if (value.includes('\n')) extraBlocks.push(`【${entry.key}】\n${value}`)
|
|
710
|
+
else extraBlocks.push(`${entry.key}:${value}`)
|
|
711
|
+
}
|
|
712
|
+
const rawName = take('name').split('\n')[0].trim() === '' ? nameFallback : take('name').split('\n')[0].trim()
|
|
713
|
+
const descriptionParts = [take('description'), ...extraBlocks].map((v) => String(v ?? '').trim()).filter((v) => v !== '')
|
|
714
|
+
const description = descriptionParts.join('\n\n').trim()
|
|
715
|
+
if (rawName === '' && description === '') return null
|
|
716
|
+
|
|
717
|
+
const json = {
|
|
718
|
+
spec: 'chara_card_v2',
|
|
719
|
+
spec_version: '2.0',
|
|
720
|
+
data: {
|
|
721
|
+
name: rawName === '' ? '未命名' : rawName,
|
|
722
|
+
description,
|
|
723
|
+
personality: take('personality'),
|
|
724
|
+
scenario: take('scenario'),
|
|
725
|
+
first_mes: take('first_mes'),
|
|
726
|
+
mes_example: take('mes_example'),
|
|
727
|
+
creator_notes: take('creator_notes'),
|
|
728
|
+
tags: [],
|
|
729
|
+
system_prompt: '',
|
|
730
|
+
post_history_instructions: '',
|
|
731
|
+
alternate_greetings: [],
|
|
732
|
+
},
|
|
733
|
+
}
|
|
734
|
+
return { json, source: 'text' }
|
|
735
|
+
}
|
|
736
|
+
|
|
484
737
|
export async function extractCard(bytes, fileName = '') {
|
|
485
738
|
const lower = fileName.toLowerCase()
|
|
486
739
|
// 1) PNG
|
|
@@ -517,7 +770,40 @@ export async function extractCard(bytes, fileName = '') {
|
|
|
517
770
|
try {
|
|
518
771
|
json = JSON.parse(text)
|
|
519
772
|
} catch {
|
|
520
|
-
|
|
773
|
+
// 4) 文本角色卡(一行一个字段 / Markdown 小标题 / 改名成 .txt 的 JSON)
|
|
774
|
+
const textCard = parseTextCard(text, { fileName })
|
|
775
|
+
if (textCard !== null) {
|
|
776
|
+
const kind = detectJsonKind(textCard.json)
|
|
777
|
+
if (kind !== 'unknown') return { json: textCard.json, kind, source: textCard.source }
|
|
778
|
+
}
|
|
779
|
+
const head = text.replace(/\s+/g, ' ').slice(0, 60)
|
|
780
|
+
// 扩展名就是 .txt / .md 的时候,宁可先收进来让人自己改,也不要留一句「读不进去」。
|
|
781
|
+
// 名字取文件名,整份原文进自述,备注里写明是怎么收的——删掉重来也比卡住强。
|
|
782
|
+
const stem = String(fileName).replace(/\.[^.]+$/, '').trim()
|
|
783
|
+
if (/\.(txt|md|markdown)$/i.test(fileName)) {
|
|
784
|
+
return {
|
|
785
|
+
json: {
|
|
786
|
+
spec: 'chara_card_v2',
|
|
787
|
+
spec_version: '2.0',
|
|
788
|
+
data: {
|
|
789
|
+
name: stem === '' ? '未命名' : stem,
|
|
790
|
+
description: text,
|
|
791
|
+
personality: '',
|
|
792
|
+
scenario: '',
|
|
793
|
+
first_mes: '',
|
|
794
|
+
mes_example: '',
|
|
795
|
+
creator_notes: '这份是提示词式的卡:里面没有「名字:」「性格:」这种分字段的写法,所以原文整份收在自述里,名字用的是文件名。想拆成小节可以手动改。',
|
|
796
|
+
tags: [],
|
|
797
|
+
system_prompt: '',
|
|
798
|
+
post_history_instructions: '',
|
|
799
|
+
alternate_greetings: [],
|
|
800
|
+
},
|
|
801
|
+
},
|
|
802
|
+
kind: 'v2',
|
|
803
|
+
source: 'text-raw',
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
throw new Error(`这个文件读不出角色卡(开头是「${head}」)。如果是散文式的人物介绍,先当材料导进来,用「让 AI 读一读」或提炼功能建卡。`)
|
|
521
807
|
}
|
|
522
808
|
return { json, kind: detectJsonKind(json), source: 'json' }
|
|
523
809
|
}
|
package/lib/core.js
CHANGED
|
@@ -83,6 +83,47 @@ export const PROPOSAL_FIELDS = [
|
|
|
83
83
|
'关系史', '不会说的话', '不会做的事', '外貌', '口癖', '备注',
|
|
84
84
|
]
|
|
85
85
|
|
|
86
|
+
/**
|
|
87
|
+
* 正文类小节。这些字段的内容是成段的话,同一件事很容易被模型写成
|
|
88
|
+
* 一句短的又说成一句长的,所以除完全重复外还做包含式去重。
|
|
89
|
+
*
|
|
90
|
+
* 结构字段(name、tags、birth_year…)不做包含式:`家` 是 `家人` 的子串,
|
|
91
|
+
* 但它们是两个 tag;把短的那个当重复删掉,等于悄悄丢数据。
|
|
92
|
+
*/
|
|
93
|
+
const LONG_TEXT_FIELDS = new Set([
|
|
94
|
+
'自述', '性格', '场景', '开场白', '备选开场', '对话示例', '创作者备注',
|
|
95
|
+
'关系史', '不会说的话', '不会做的事', '外貌', '口癖', '备注',
|
|
96
|
+
'knowledge', 'life', 'address',
|
|
97
|
+
])
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 去重时用的正文形态:去掉空白与标点、统一小写。
|
|
101
|
+
*
|
|
102
|
+
* 「他喜欢下棋。」和「他 喜欢下棋」是同一句话,只差标点和空格。
|
|
103
|
+
* 模型重新整理同一份材料时,这种差别几乎必然出现——按原样比就漏了。
|
|
104
|
+
*/
|
|
105
|
+
function proposalTextKey(text) {
|
|
106
|
+
return String(text ?? '')
|
|
107
|
+
.toLowerCase()
|
|
108
|
+
.replace(/[\s\p{P}\p{S}]+/gu, '')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 一条提案的值做成去重键。数组按元素摊平,对象按稳定序列化。 */
|
|
112
|
+
function proposalValueKey(item) {
|
|
113
|
+
const v = item?.value
|
|
114
|
+
if (typeof v === 'string') return proposalTextKey(v)
|
|
115
|
+
if (Array.isArray(v)) {
|
|
116
|
+
return v
|
|
117
|
+
.map((x) => (typeof x === 'string' ? proposalTextKey(x) : proposalTextKey(JSON.stringify(x))))
|
|
118
|
+
.filter((s) => s !== '')
|
|
119
|
+
.sort()
|
|
120
|
+
.join('|')
|
|
121
|
+
}
|
|
122
|
+
if (v === null || v === undefined) return ''
|
|
123
|
+
if (typeof v === 'object') return proposalTextKey(JSON.stringify(v))
|
|
124
|
+
return proposalTextKey(String(v))
|
|
125
|
+
}
|
|
126
|
+
|
|
86
127
|
/**
|
|
87
128
|
* 模型自报的 line_start 与实际匹配位置允许相差几行。
|
|
88
129
|
*
|
|
@@ -896,6 +937,16 @@ export function verifyQuote(item, materials) {
|
|
|
896
937
|
|
|
897
938
|
/**
|
|
898
939
|
* 校验整个提案:逐条过引文门,不通过的移进 rejected。
|
|
940
|
+
*
|
|
941
|
+
* 还要去重。同一条事实在同一份提案里出现两遍(模型把材料里一句话拆成
|
|
942
|
+
* 两个「自述」条目,或者换个标点再说一遍),以前两条都会过闸门、被勾选、
|
|
943
|
+
* 写进卡——用户整理一次材料,卡里就多一段重复的正文。
|
|
944
|
+
* 这一步只在**同一 field 内**比:`name` 和 `自述` 里出现同一句话不是重复,
|
|
945
|
+
* 那是两个字段各说各的。比对前先去掉空白和标点、统一大小写。
|
|
946
|
+
*
|
|
947
|
+
* 正文类小节(自述、场景、对话示例…)再做包含式去重:短的被长的吸收。
|
|
948
|
+
* 结构字段不做,"家" 是 "家人" 的子串,但那是两个 tag。
|
|
949
|
+
*
|
|
899
950
|
* @param {object} proposal
|
|
900
951
|
* @param {Record<string, string>} materials
|
|
901
952
|
*/
|
|
@@ -903,6 +954,9 @@ export function verifyProposal(proposal, materials) {
|
|
|
903
954
|
const items = asArray(proposal?.items)
|
|
904
955
|
const accepted = []
|
|
905
956
|
const rejected = []
|
|
957
|
+
/** field → 已经收下的条目(记下键和对象,长句吸收短句时要回撤)。 */
|
|
958
|
+
const seen = new Map()
|
|
959
|
+
|
|
906
960
|
for (const item of items) {
|
|
907
961
|
if (item === null || typeof item !== 'object') {
|
|
908
962
|
rejected.push({ item, reason: '条目不是对象' })
|
|
@@ -927,7 +981,39 @@ export function verifyProposal(proposal, materials) {
|
|
|
927
981
|
rejected.push({ item, reason: q.reason })
|
|
928
982
|
continue
|
|
929
983
|
}
|
|
930
|
-
|
|
984
|
+
|
|
985
|
+
const field = item.field.trim()
|
|
986
|
+
const key = proposalValueKey(item)
|
|
987
|
+
const prior = seen.get(field) ?? []
|
|
988
|
+
let dup
|
|
989
|
+
if (key !== '') {
|
|
990
|
+
dup = prior.find((p) => p.key === key)
|
|
991
|
+
if (dup === undefined && LONG_TEXT_FIELDS.has(field)) {
|
|
992
|
+
dup = prior.find((p) => p.key !== '' && (key.includes(p.key) || p.key.includes(key)))
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
if (dup !== undefined) {
|
|
996
|
+
if (key.length > dup.key.length && key.includes(dup.key)) {
|
|
997
|
+
// 新的这句更长、把前面那句整个包含进来了:留长的,把短的撤下来。
|
|
998
|
+
// 「他喜欢下棋」和「他喜欢下棋,每天下午都要下一盘」是同一件事,
|
|
999
|
+
// 留两句会让卡读起来像复读。
|
|
1000
|
+
const at = accepted.indexOf(dup.entry)
|
|
1001
|
+
if (at >= 0) accepted.splice(at, 1)
|
|
1002
|
+
seen.set(field, prior.filter((p) => p !== dup))
|
|
1003
|
+
rejected.push({ item: dup.item, reason: '和后面那条重复,只留了长的' })
|
|
1004
|
+
} else {
|
|
1005
|
+
rejected.push({ item, reason: '和前面那条重复' })
|
|
1006
|
+
continue
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
const entry = { ...item, verified_line: q.line }
|
|
1011
|
+
accepted.push(entry)
|
|
1012
|
+
if (key !== '') {
|
|
1013
|
+
const list = seen.get(field) ?? []
|
|
1014
|
+
list.push({ key, item, entry })
|
|
1015
|
+
seen.set(field, list)
|
|
1016
|
+
}
|
|
931
1017
|
}
|
|
932
1018
|
return { accepted, rejected, ok: rejected.length === 0 }
|
|
933
1019
|
}
|