dsh-colophon 0.2.3 → 0.2.4
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 +8 -0
- package/client.js +104 -17
- package/index.js +4 -2
- package/lib/api.js +42 -3
- package/lib/app.js +9 -5
- package/lib/digest.js +73 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## 0.2.4(摘要区块可配置 + 明亮摘要面板)
|
|
2
|
+
|
|
3
|
+
- 摘要内容改为**区块制**,可在设置页逐项开关:目标 / 结论 / 决策 / 涉及文件 / 完成情况 / 下一步 / 风险与注意 / 关键词 / 待确认问题(默认只开目标、结论、决策、下一步、风险与注意,不再输出中间的已完成动作)。
|
|
4
|
+
- 摘要面板改用主题变量配色(浅色主题下为明亮白底),按区块显示带标题的内容,不再是整段黑色等宽文本。
|
|
5
|
+
- 对话输入框上方改为明确的「生成卷末摘要」按钮,面板标题栏内直接提供「立即生成 / 重新生成」主按钮。
|
|
6
|
+
- 短对话也支持总结:默认门槛降为 2 条消息 / 100 token;阈值仍可在设置页调整。
|
|
7
|
+
- 本地规则引擎补上关键词与待确认问题的抽取;API 引擎按所选区块生成对应小节。
|
|
8
|
+
|
|
1
9
|
# Colophon(卷末)
|
|
2
10
|
|
|
3
11
|
> 旧书卷末的那一笔,记录这本书由谁写成、在哪一页收束。
|
package/client.js
CHANGED
|
@@ -23,8 +23,8 @@ window.__ModuleLoader__.load({
|
|
|
23
23
|
const DICT = {
|
|
24
24
|
zh: {
|
|
25
25
|
chip: '卷末',
|
|
26
|
-
chipReady: '
|
|
27
|
-
chipEmpty: '
|
|
26
|
+
chipReady: '卷末摘要',
|
|
27
|
+
chipEmpty: '生成卷末摘要',
|
|
28
28
|
panelTitle: '卷末',
|
|
29
29
|
empty: '这条会话还没有卷末:对话太短,或者还没生成。',
|
|
30
30
|
generate: '立即生成',
|
|
@@ -87,6 +87,13 @@ window.__ModuleLoader__.load({
|
|
|
87
87
|
queueRun: '队列已处理',
|
|
88
88
|
apiUnavailable: '当前环境没有可用的模型服务(llm):API 引擎会回退到本地 rules 摘要,并把它放进待确认队列。',
|
|
89
89
|
statusFailed: '状态读取失败',
|
|
90
|
+
sectionsTitle: '摘要区块',
|
|
91
|
+
sectionsHint: '总结里会出现哪些区块,按需勾选;区块越多,内容越长。',
|
|
92
|
+
secGoal: '目标', secConclusion: '结论', secDecisions: '决策', secFiles: '涉及文件',
|
|
93
|
+
secProgress: '完成情况', secNextSteps: '下一步', secRisks: '风险与注意', secKeywords: '关键词', secQuestions: '待确认问题',
|
|
94
|
+
secGoalHint: '用户最终想要什么', secConclusionHint: '当前状态与结论', secDecisionsHint: '关键决定及理由',
|
|
95
|
+
secFilesHint: '被修改的文件', secProgressHint: '已确认完成的事(中间动作)', secNextStepsHint: '还没做完的事',
|
|
96
|
+
secRisksHint: '报错、限制与未验证假设', secKeywordsHint: '便于检索的短词', secQuestionsHint: '仍需确认的问题',
|
|
90
97
|
basicTitle: '基本设置',
|
|
91
98
|
statusOn: '已启用',
|
|
92
99
|
statusOff: '已暂停',
|
|
@@ -118,8 +125,8 @@ window.__ModuleLoader__.load({
|
|
|
118
125
|
},
|
|
119
126
|
en: {
|
|
120
127
|
chip: 'Colophon',
|
|
121
|
-
chipReady: 'Colophon
|
|
122
|
-
chipEmpty: '
|
|
128
|
+
chipReady: 'Colophon summary',
|
|
129
|
+
chipEmpty: 'Generate summary',
|
|
123
130
|
panelTitle: 'Colophon',
|
|
124
131
|
empty: 'No digest yet for this session: the conversation is short or nothing was generated.',
|
|
125
132
|
generate: 'Generate now',
|
|
@@ -182,6 +189,13 @@ window.__ModuleLoader__.load({
|
|
|
182
189
|
queueRun: 'Queue processed',
|
|
183
190
|
apiUnavailable: 'No model service (llm) is available here: the API engine falls back to local rules digests and queues the session for confirmation.',
|
|
184
191
|
statusFailed: 'Status unavailable',
|
|
192
|
+
sectionsTitle: 'Summary blocks',
|
|
193
|
+
sectionsHint: 'Pick the blocks that appear in the digest; more blocks make it longer.',
|
|
194
|
+
secGoal: 'Goal', secConclusion: 'Conclusion', secDecisions: 'Decisions', secFiles: 'Files',
|
|
195
|
+
secProgress: 'Progress', secNextSteps: 'Next steps', secRisks: 'Risks', secKeywords: 'Keywords', secQuestions: 'Open questions',
|
|
196
|
+
secGoalHint: 'what the user ultimately wants', secConclusionHint: 'current state and result', secDecisionsHint: 'key decisions and why',
|
|
197
|
+
secFilesHint: 'files that were changed', secProgressHint: 'confirmed completed work', secNextStepsHint: 'what is left to do',
|
|
198
|
+
secRisksHint: 'errors, limits, unverified guesses', secKeywordsHint: 'short search terms', secQuestionsHint: 'questions needing confirmation',
|
|
185
199
|
basicTitle: 'Basics',
|
|
186
200
|
statusOn: 'Enabled',
|
|
187
201
|
statusOff: 'Paused',
|
|
@@ -239,21 +253,33 @@ window.__ModuleLoader__.load({
|
|
|
239
253
|
|
|
240
254
|
const styles = {
|
|
241
255
|
chip: {
|
|
242
|
-
display: 'inline-flex', alignItems: 'center', gap: 6, padding: '
|
|
243
|
-
border: '1px solid rgba(
|
|
244
|
-
|
|
256
|
+
display: 'inline-flex', alignItems: 'center', gap: 6, padding: '0 12px', height: 28,
|
|
257
|
+
border: '1px solid var(--dsw-alias-border-l2, rgba(0,0,0,0.14))', borderRadius: 8,
|
|
258
|
+
background: 'var(--dsw-alias-bg-layer-2, #f2f3f5)',
|
|
259
|
+
color: 'var(--dsw-alias-label-primary, #1f2329)', cursor: 'pointer', fontSize: 13, fontWeight: 500,
|
|
260
|
+
},
|
|
261
|
+
chipOn: {
|
|
262
|
+
border: '1px solid var(--dsw-alias-button-primary-fill, #4d6bfe)',
|
|
263
|
+
background: 'color-mix(in srgb, var(--dsw-alias-button-primary-fill, #4d6bfe) 12%, transparent)',
|
|
245
264
|
},
|
|
246
265
|
dot: { width: 7, height: 7, borderRadius: 999, background: '#4cc38a', display: 'inline-block' },
|
|
247
266
|
dotEmpty: { width: 7, height: 7, borderRadius: 999, background: 'rgba(255,255,255,0.35)', display: 'inline-block' },
|
|
248
267
|
panel: {
|
|
249
268
|
position: 'fixed', right: 16, bottom: 96, width: 380, maxWidth: 'calc(100vw - 32px)',
|
|
250
269
|
maxHeight: '62vh', overflow: 'auto', zIndex: 60, padding: 14,
|
|
251
|
-
background: '
|
|
252
|
-
|
|
253
|
-
|
|
270
|
+
background: 'var(--dsw-alias-bg-layer-2, #ffffff)',
|
|
271
|
+
color: 'var(--dsw-alias-label-primary, #1f2329)',
|
|
272
|
+
border: '1px solid var(--dsw-alias-border-l2, rgba(0,0,0,0.12))', borderRadius: 12,
|
|
273
|
+
boxShadow: '0 12px 40px rgba(0,0,0,0.22)', fontSize: 13, lineHeight: 1.55,
|
|
274
|
+
},
|
|
275
|
+
panelHead: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 },
|
|
276
|
+
title: { fontSize: 15, fontWeight: 600, color: 'var(--dsw-alias-label-primary, #1f2329)' },
|
|
277
|
+
digestTitle: { fontSize: 13, fontWeight: 600, color: 'var(--dsw-alias-label-secondary, #4e5969)', marginBottom: 4 },
|
|
278
|
+
digestBlock: { marginTop: 4, paddingTop: 12, borderTop: '1px solid var(--dsw-alias-border-l1, rgba(0,0,0,0.07))' },
|
|
279
|
+
digestBody: {
|
|
280
|
+
whiteSpace: 'pre-wrap', wordBreak: 'break-word', margin: 0, fontFamily: 'inherit',
|
|
281
|
+
fontSize: 13, lineHeight: 1.65, color: 'var(--dsw-alias-label-primary, #1f2329)',
|
|
254
282
|
},
|
|
255
|
-
panelHead: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 8 },
|
|
256
|
-
title: { fontSize: 13, fontWeight: 600 },
|
|
257
283
|
pre: { whiteSpace: 'pre-wrap', wordBreak: 'break-word', margin: 0, fontFamily: 'var(--dsh-font-mono, ui-monospace, SFMono-Regular, Menlo, monospace)', fontSize: 11, lineHeight: 1.6 },
|
|
258
284
|
actions: { display: 'flex', gap: 8, marginTop: 10, flexWrap: 'wrap' },
|
|
259
285
|
button: {
|
|
@@ -417,6 +443,26 @@ window.__ModuleLoader__.load({
|
|
|
417
443
|
}, tr('crashAck')))
|
|
418
444
|
}
|
|
419
445
|
|
|
446
|
+
/** Split a rendered digest Markdown into titled blocks. */
|
|
447
|
+
function parseDigestBlocks(markdown) {
|
|
448
|
+
const out = []
|
|
449
|
+
let current = null
|
|
450
|
+
for (const raw of String(markdown ?? '').split('\n')) {
|
|
451
|
+
const line = raw.trimEnd()
|
|
452
|
+
if (line.startsWith('#')) {
|
|
453
|
+
const heading = line.replace(/^#+\s*/, '').trim()
|
|
454
|
+
if (!heading) continue
|
|
455
|
+
current = { heading, body: [] }
|
|
456
|
+
out.push(current)
|
|
457
|
+
continue
|
|
458
|
+
}
|
|
459
|
+
if (current) current.body.push(line)
|
|
460
|
+
}
|
|
461
|
+
return out
|
|
462
|
+
.map(block => ({ heading: block.heading, body: block.body.join('\n').trim() }))
|
|
463
|
+
.filter(block => block.body !== '' || /^(卷末|Colophon)/i.test(block.heading) === false)
|
|
464
|
+
}
|
|
465
|
+
|
|
420
466
|
/** The chip + floating digest panel above the composer. */
|
|
421
467
|
function ColophonDock(props) {
|
|
422
468
|
const sessionId = props.sessionId
|
|
@@ -483,24 +529,29 @@ window.__ModuleLoader__.load({
|
|
|
483
529
|
if (variant === 'hero' || !sessionId) return null
|
|
484
530
|
|
|
485
531
|
const chip = h('button', {
|
|
486
|
-
style: styles.chip,
|
|
532
|
+
style: digest ? { ...styles.chip, ...styles.chipOn } : styles.chip,
|
|
487
533
|
title: tr('panelTitle'),
|
|
488
534
|
onClick: () => setOpen(current => !current),
|
|
489
535
|
},
|
|
490
536
|
h('span', { style: digest ? styles.dot : styles.dotEmpty }),
|
|
491
537
|
h('span', null, digest ? tr('chipReady') : tr('chipEmpty')))
|
|
492
538
|
|
|
539
|
+
const blocks = digest?.markdown ? parseDigestBlocks(digest.markdown) : []
|
|
540
|
+
|
|
493
541
|
const panel = open ? h('div', { style: styles.panel, role: 'dialog', 'aria-label': tr('panelTitle') },
|
|
494
542
|
h('div', { style: styles.panelHead },
|
|
495
543
|
h('span', { style: styles.title }, tr('panelTitle')),
|
|
496
|
-
h('span', { style: styles.muted }, digest?.updatedAt ? new Date(digest.updatedAt).toLocaleString() : '')
|
|
544
|
+
h('span', { style: { ...styles.muted, marginRight: 'auto' } }, digest?.updatedAt ? new Date(digest.updatedAt).toLocaleString() : ''),
|
|
545
|
+
h('button', { style: { ...styles.buttonPrimary, minWidth: 92 }, onClick: () => void generate(), disabled: loading },
|
|
546
|
+
digest ? tr('regenerate') : tr('generate'))),
|
|
497
547
|
loading ? h('div', { style: styles.muted }, tr('loading'))
|
|
498
548
|
: error ? h('div', { style: styles.warn }, tr('failed', { message: error }))
|
|
499
|
-
:
|
|
549
|
+
: blocks.length > 0
|
|
550
|
+
? h('div', null, blocks.map((block, index) => h('div', { key: index, style: index === 0 ? null : styles.digestBlock },
|
|
551
|
+
h('div', { style: styles.digestTitle }, block.heading),
|
|
552
|
+
h('div', { style: styles.digestBody }, block.body))))
|
|
500
553
|
: h('div', { style: styles.muted }, tr('empty')),
|
|
501
554
|
h('div', { style: styles.actions },
|
|
502
|
-
h('button', { style: styles.buttonPrimary, onClick: () => void generate(), disabled: loading },
|
|
503
|
-
digest ? tr('regenerate') : tr('generate')),
|
|
504
555
|
digest?.markdown ? h('button', { style: styles.buttonSecondary, onClick: () => void copy() }, copied ? tr('copied') : tr('copy')) : null,
|
|
505
556
|
digest?.markdown ? h('button', { style: styles.buttonSecondary, onClick: () => void download() }, tr('download')) : null,
|
|
506
557
|
h('button', { style: styles.buttonSecondary, onClick: () => setOpen(false) }, tr('close')))) : null
|
|
@@ -733,6 +784,19 @@ window.__ModuleLoader__.load({
|
|
|
733
784
|
h('span', { style: { ...ui.pillDot, background: 'currentColor', opacity: on || count ? 1 : 0.6 } }),
|
|
734
785
|
count ? onText : on ? onText : offText)
|
|
735
786
|
}
|
|
787
|
+
/** Digest blocks the user can include, in presentation order. */
|
|
788
|
+
const SECTION_META = [
|
|
789
|
+
['goal', 'secGoal', 'secGoalHint'],
|
|
790
|
+
['conclusion', 'secConclusion', 'secConclusionHint'],
|
|
791
|
+
['decisions', 'secDecisions', 'secDecisionsHint'],
|
|
792
|
+
['files', 'secFiles', 'secFilesHint'],
|
|
793
|
+
['progress', 'secProgress', 'secProgressHint'],
|
|
794
|
+
['nextSteps', 'secNextSteps', 'secNextStepsHint'],
|
|
795
|
+
['risks', 'secRisks', 'secRisksHint'],
|
|
796
|
+
['keywords', 'secKeywords', 'secKeywordsHint'],
|
|
797
|
+
['questions', 'secQuestions', 'secQuestionsHint'],
|
|
798
|
+
]
|
|
799
|
+
|
|
736
800
|
/** Settings page: native DSH layout (rows + hairlines) and token colors. */
|
|
737
801
|
function ColophonSettings() {
|
|
738
802
|
const [form, setForm] = useState(null)
|
|
@@ -770,6 +834,16 @@ window.__ModuleLoader__.load({
|
|
|
770
834
|
setMessage('')
|
|
771
835
|
}
|
|
772
836
|
|
|
837
|
+
const toggleSection = (id) => {
|
|
838
|
+
setForm(current => {
|
|
839
|
+
if (!current) return current
|
|
840
|
+
const list = Array.isArray(current.sections) ? current.sections : []
|
|
841
|
+
const next = list.includes(id) ? list.filter(item => item !== id) : [...list, id]
|
|
842
|
+
return { ...current, sections: next }
|
|
843
|
+
})
|
|
844
|
+
setMessage('')
|
|
845
|
+
}
|
|
846
|
+
|
|
773
847
|
const save = async () => {
|
|
774
848
|
if (!form) return
|
|
775
849
|
setSaving(true)
|
|
@@ -777,6 +851,7 @@ window.__ModuleLoader__.load({
|
|
|
777
851
|
try {
|
|
778
852
|
const patch = {
|
|
779
853
|
enabled: form.enabled, engine: form.engine,
|
|
854
|
+
sections: form.sections,
|
|
780
855
|
api: form.api, triggers: form.triggers, filter: form.filter,
|
|
781
856
|
}
|
|
782
857
|
const value = await callHost('config', { method: 'POST', body: { patch } })
|
|
@@ -853,6 +928,18 @@ window.__ModuleLoader__.load({
|
|
|
853
928
|
],
|
|
854
929
|
})))),
|
|
855
930
|
|
|
931
|
+
h(Group, { title: tr('sectionsTitle'), meta: `${(form.sections ?? []).length}/${SECTION_META.length}` },
|
|
932
|
+
h('div', { style: { paddingBottom: 4 } },
|
|
933
|
+
h('div', null,
|
|
934
|
+
SECTION_META.map(([id, labelKey, hintKey], index) => h(SwitchRow, {
|
|
935
|
+
key: id,
|
|
936
|
+
label: tr(labelKey),
|
|
937
|
+
hint: tr(hintKey),
|
|
938
|
+
checked: (form.sections ?? []).includes(id),
|
|
939
|
+
last: index === SECTION_META.length - 1,
|
|
940
|
+
onChange: () => toggleSection(id),
|
|
941
|
+
}))))),
|
|
942
|
+
|
|
856
943
|
h(Group, { title: tr('triggers') },
|
|
857
944
|
h(SwitchRow, {
|
|
858
945
|
label: tr('onIdle'), hint: tr('onIdleHint'), checked: form.triggers.onIdle === true,
|
package/index.js
CHANGED
|
@@ -42,9 +42,11 @@ export const Config = Schema.object({
|
|
|
42
42
|
closeBatch: Schema.boolean().default(false),
|
|
43
43
|
closeBatchMaxSessions: Schema.number().default(10),
|
|
44
44
|
}),
|
|
45
|
+
/** Blocks shown in the digest, in the order the settings page lists them. */
|
|
46
|
+
sections: Schema.array(Schema.string()).default(['goal', 'conclusion', 'decisions', 'nextSteps', 'risks']),
|
|
45
47
|
filter: Schema.object({
|
|
46
|
-
minMessages: Schema.number().default(
|
|
47
|
-
minTokens: Schema.number().default(
|
|
48
|
+
minMessages: Schema.number().default(2),
|
|
49
|
+
minTokens: Schema.number().default(100),
|
|
48
50
|
maxAgeDays: Schema.number().default(30),
|
|
49
51
|
sessionTypes: Schema.array(Schema.string()).default(['root']),
|
|
50
52
|
}),
|
package/lib/api.js
CHANGED
|
@@ -9,6 +9,19 @@ import { bullets, clip, estimateTokens, lastParagraph, redactSecrets, section, w
|
|
|
9
9
|
import { costCNY } from './usage.js'
|
|
10
10
|
import { renderDigest } from './digest.js'
|
|
11
11
|
|
|
12
|
+
/** Per-block instructions; only the user-selected blocks are requested. */
|
|
13
|
+
export const SECTION_PROMPTS = {
|
|
14
|
+
goal: ['## 目标', '一段话,说明用户最终想要什么。'],
|
|
15
|
+
conclusion: ['## 结论', '一段话,说明现在处于什么状态、结论是什么;没有结论就写“尚无结论”。'],
|
|
16
|
+
decisions: ['## 决策', '- 条目列表(最多 4 条),列出关键决定及一句话理由;没有就省略该小节。'],
|
|
17
|
+
files: ['## 涉及文件', '- 条目列表(最多 8 条),列出被修改的文件路径;没有就省略该小节。'],
|
|
18
|
+
progress: ['## 完成情况', '- 条目列表(最多 8 条),只列已确认完成的事。'],
|
|
19
|
+
nextSteps: ['## 下一步', '- 条目列表(最多 5 条),来自未完成项;没有就省略该小节。'],
|
|
20
|
+
risks: ['## 风险与注意', '- 条目列表(最多 4 条),来自报错、限制、未验证的假设;没有就省略该小节。'],
|
|
21
|
+
keywords: ['## 关键词', '- 3 到 6 个短词,便于检索;没有就省略该小节。'],
|
|
22
|
+
questions: ['## 待确认问题', '- 条目列表(最多 4 条),列出仍需用户确认的问题;没有就省略该小节。'],
|
|
23
|
+
}
|
|
24
|
+
|
|
12
25
|
/** Instruction sent to the summarizer. Short by design: the tail carries the facts. */
|
|
13
26
|
export const API_PROMPT = [
|
|
14
27
|
'你是「卷末」撰写器:为一个即将结束的编码会话写一份最终交接摘要。',
|
|
@@ -29,6 +42,23 @@ export const API_PROMPT = [
|
|
|
29
42
|
'保持中文、简洁、可执行;不要复述整段对话。',
|
|
30
43
|
].join('\n')
|
|
31
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Build the instruction for the blocks the user enabled.
|
|
47
|
+
* @param {string[]} [sections] enabled block ids.
|
|
48
|
+
* @returns {string}
|
|
49
|
+
*/
|
|
50
|
+
export function buildApiPrompt(sections) {
|
|
51
|
+
const ids = Array.isArray(sections) && sections.length > 0 ? sections.filter(id => SECTION_PROMPTS[id]) : ['goal', 'conclusion', 'decisions', 'nextSteps', 'risks']
|
|
52
|
+
const lines = [
|
|
53
|
+
'你是「卷末」撰写器:为一个即将结束的编码会话写一份最终交接摘要。',
|
|
54
|
+
'只依据用户消息、最后一条助手回复和工具足迹,不要编造未发生的事。',
|
|
55
|
+
'输出严格使用以下小节(不要输出未列出的小节,也不要在小节前加任何前言):',
|
|
56
|
+
]
|
|
57
|
+
for (const id of ids) lines.push(...SECTION_PROMPTS[id])
|
|
58
|
+
lines.push('保持中文、简洁、可执行;不要复述整段对话。')
|
|
59
|
+
return lines.join('\n')
|
|
60
|
+
}
|
|
61
|
+
|
|
32
62
|
/**
|
|
33
63
|
* Compose the summarizer messages for one checkpoint.
|
|
34
64
|
* @param {ReturnType<import('./collect.js').SessionTail['snapshot']>} checkpoint session tail.
|
|
@@ -63,12 +93,13 @@ export function buildApiMessages(checkpoint, config) {
|
|
|
63
93
|
parts.push(`工具错误:\n${errors.slice(0, 6).map(error => `- ${clip(error?.name ?? 'tool', 60)}${error?.reason ? `:${clip(error.reason, 120)}` : ''}`).join('\n')}`)
|
|
64
94
|
}
|
|
65
95
|
const userText = parts.join('\n\n')
|
|
96
|
+
const system = buildApiPrompt(Array.isArray(config?.sections) ? config.sections : undefined)
|
|
66
97
|
const messages = [
|
|
67
|
-
{ role: /** @type {const} */ ('system'), content:
|
|
98
|
+
{ role: /** @type {const} */ ('system'), content: system },
|
|
68
99
|
{ role: /** @type {const} */ ('user'), content: userText },
|
|
69
100
|
]
|
|
70
|
-
const inputTokens = estimateTokens(
|
|
71
|
-
return { system
|
|
101
|
+
const inputTokens = estimateTokens(system) + estimateTokens(userText)
|
|
102
|
+
return { system, messages, inputTokens, userText }
|
|
72
103
|
}
|
|
73
104
|
|
|
74
105
|
/**
|
|
@@ -121,6 +152,11 @@ export function parseApiDigest(text, checkpoint, meta = {}) {
|
|
|
121
152
|
}
|
|
122
153
|
}
|
|
123
154
|
}
|
|
155
|
+
const keywords = toList(fromJson?.keywords, 6, 40)
|
|
156
|
+
if (keywords.length === 0) keywords.push(...bullets(reply, /(关键词|keywords)/iu, 6, 40))
|
|
157
|
+
const questions = toList(fromJson?.questions, 4, 140)
|
|
158
|
+
if (questions.length === 0) questions.push(...bullets(reply, /(待确认|问题|questions)/iu, 4, 140))
|
|
159
|
+
const sections = Array.isArray(meta.sections) && meta.sections.length > 0 ? meta.sections : ['goal', 'conclusion', 'decisions', 'nextSteps', 'risks']
|
|
124
160
|
const usage = meta.usage ?? {}
|
|
125
161
|
const digest = {
|
|
126
162
|
v: 1,
|
|
@@ -141,6 +177,9 @@ export function parseApiDigest(text, checkpoint, meta = {}) {
|
|
|
141
177
|
done,
|
|
142
178
|
nextSteps,
|
|
143
179
|
risks,
|
|
180
|
+
keywords,
|
|
181
|
+
questions,
|
|
182
|
+
sections,
|
|
144
183
|
tokens: {
|
|
145
184
|
input: Number(usage.inputTokens) || 0,
|
|
146
185
|
output: Number(usage.outputTokens) || 0,
|
package/lib/app.js
CHANGED
|
@@ -9,7 +9,7 @@ import { ensureDir } from './files.js'
|
|
|
9
9
|
import { dataRoot } from './files.js'
|
|
10
10
|
import { Collector, SessionTail } from './collect.js'
|
|
11
11
|
import { buildApiMessages, estimateCost, parseApiDigest } from './api.js'
|
|
12
|
-
import { RULES_VERSION, buildDigest, shouldSummarize } from './digest.js'
|
|
12
|
+
import { DEFAULT_SECTIONS, RULES_VERSION, SECTION_IDS, buildDigest, shouldSummarize } from './digest.js'
|
|
13
13
|
import { Store } from './store.js'
|
|
14
14
|
import { UsageLedger, costCNY } from './usage.js'
|
|
15
15
|
import { estimateTokens } from './text.js'
|
|
@@ -99,6 +99,10 @@ export function sanitizeSettings(patch) {
|
|
|
99
99
|
if (max !== undefined) triggers.closeBatchMaxSessions = Math.max(1, Math.min(50, Math.round(max)))
|
|
100
100
|
if (Object.keys(triggers).length > 0) out.triggers = triggers
|
|
101
101
|
}
|
|
102
|
+
if (Array.isArray(patch.sections)) {
|
|
103
|
+
const sections = [...new Set(patch.sections.filter(id => typeof id === 'string' && SECTION_IDS.includes(id)))]
|
|
104
|
+
out.sections = sections.length > 0 ? sections : [...DEFAULT_SECTIONS]
|
|
105
|
+
}
|
|
102
106
|
if (patch.filter && typeof patch.filter === 'object') {
|
|
103
107
|
const filter = {}
|
|
104
108
|
for (const key of ['minMessages', 'minTokens', 'maxAgeDays']) {
|
|
@@ -292,7 +296,7 @@ export function install(ctx, rawConfig) {
|
|
|
292
296
|
const checkpoint = tail.snapshot()
|
|
293
297
|
if (!shouldSummarize(checkpoint, config.filter).ok) { recovery.skipped += 1; continue }
|
|
294
298
|
await store.saveCheckpoint(checkpoint)
|
|
295
|
-
await store.saveDigest(buildDigest(checkpoint, { engine: 'rules', now: Date.now() }))
|
|
299
|
+
await store.saveDigest(buildDigest(checkpoint, { engine: 'rules', now: Date.now(), sections: config.sections }))
|
|
296
300
|
recovery.created += 1
|
|
297
301
|
log(`recovered digest for ${sessionId}`)
|
|
298
302
|
} catch (error) {
|
|
@@ -320,7 +324,7 @@ export function install(ctx, rawConfig) {
|
|
|
320
324
|
const digest = await tryApiDigest(sessionId, checkpoint, trigger)
|
|
321
325
|
if (digest) return digest
|
|
322
326
|
}
|
|
323
|
-
const digest = buildDigest(checkpoint, { engine: 'rules', now: Date.now() })
|
|
327
|
+
const digest = buildDigest(checkpoint, { engine: 'rules', now: Date.now(), sections: config.sections })
|
|
324
328
|
await store.saveDigest(digest)
|
|
325
329
|
return digest
|
|
326
330
|
}
|
|
@@ -388,7 +392,7 @@ export function install(ctx, rawConfig) {
|
|
|
388
392
|
} catch (error) {
|
|
389
393
|
warn(`usage record failed for ${sessionId}: ${String(error)}`)
|
|
390
394
|
}
|
|
391
|
-
const digest = parseApiDigest(text, checkpoint, { model: api.model, usage: real, now: Date.now() })
|
|
395
|
+
const digest = parseApiDigest(text, checkpoint, { model: api.model, usage: real, now: Date.now(), sections: config.sections })
|
|
392
396
|
await store.saveDigest(digest)
|
|
393
397
|
store.dequeueJob(sessionId)
|
|
394
398
|
await store.persist()
|
|
@@ -442,7 +446,7 @@ export function install(ctx, rawConfig) {
|
|
|
442
446
|
continue
|
|
443
447
|
}
|
|
444
448
|
if (config.engine !== 'api') {
|
|
445
|
-
await store.saveDigest(buildDigest(checkpoint, { engine: 'rules', now: Date.now() }))
|
|
449
|
+
await store.saveDigest(buildDigest(checkpoint, { engine: 'rules', now: Date.now(), sections: config.sections }))
|
|
446
450
|
store.dequeueJob(sessionId)
|
|
447
451
|
await store.persist()
|
|
448
452
|
results.push({ sessionId, ok: true, engine: 'rules' })
|
package/lib/digest.js
CHANGED
|
@@ -7,10 +7,18 @@
|
|
|
7
7
|
import { bullets, clip, lastParagraph, redactSecrets, section, withoutTrailingFence } from './text.js'
|
|
8
8
|
|
|
9
9
|
/** Bump when summary extraction rules change; startup backfill rebuilds stale rules digests. */
|
|
10
|
-
export const RULES_VERSION =
|
|
10
|
+
export const RULES_VERSION = 4
|
|
11
11
|
|
|
12
12
|
/** Defaults for the length filter (also mirrored in the Config schema). */
|
|
13
|
-
export const FILTER_DEFAULTS = Object.freeze({ minMessages:
|
|
13
|
+
export const FILTER_DEFAULTS = Object.freeze({ minMessages: 2, minTokens: 100 })
|
|
14
|
+
|
|
15
|
+
/** Every block a digest can contain; the settings page lets users pick. */
|
|
16
|
+
export const SECTION_IDS = Object.freeze(['goal', 'conclusion', 'decisions', 'files', 'progress', 'nextSteps', 'risks', 'keywords', 'questions'])
|
|
17
|
+
|
|
18
|
+
/** Blocks shown when the user has not chosen anything (no intermediate actions). */
|
|
19
|
+
export const DEFAULT_SECTIONS = Object.freeze(['goal', 'conclusion', 'decisions', 'nextSteps', 'risks'])
|
|
20
|
+
|
|
21
|
+
const STOPWORDS = new Set(['这个', '那个', '我们', '你们', '他们', '可以', '需要', '现在', '就是', '因为', '所以', '如果', '然后', '并且', '或者', '一个', '没有', '已经', '还是', '这样', '那样', '这里', '那里', '什么', '怎么', '如何', 'the', 'and', 'for', 'with', 'that', 'this', 'from', 'into', 'will', 'should', 'would', 'could'])
|
|
14
22
|
|
|
15
23
|
const CONCLUSION_HEADING = /(结论|总结|完成情况|conclusion|summary|outcome)/iu
|
|
16
24
|
const NEXT_HEADING = /(下一步|后续|待办|next step|next steps|todo|follow[- ]?up)/iu
|
|
@@ -141,6 +149,20 @@ export function buildDigest(checkpoint, options = {}) {
|
|
|
141
149
|
const goal = redactSecrets(typeof checkpoint.goal === 'string' && checkpoint.goal !== '' ? checkpoint.goal : '(未捕捉到目标)')
|
|
142
150
|
const conclusion = conclusionOf(assistantText) || (checkpoint.messageCount === 0 ? '(空会话)' : '(本次对话没有留下文字结论)')
|
|
143
151
|
|
|
152
|
+
const keywords = extractKeywords(`${checkpoint.title ?? ''} ${goal} ${conclusion}`)
|
|
153
|
+
const questions = []
|
|
154
|
+
for (const line of assistantText.split('\n')) {
|
|
155
|
+
const item = line.replace(/^[#>*\-\s`]+/u, '').trim()
|
|
156
|
+
if (item === '' || item.length > 160) continue
|
|
157
|
+
if (!/[??]|是否|要不要|待确认/u.test(item)) continue
|
|
158
|
+
const question = clip(redactSecrets(item), 140)
|
|
159
|
+
if (!questions.includes(question)) questions.push(question)
|
|
160
|
+
if (questions.length >= 4) break
|
|
161
|
+
}
|
|
162
|
+
const sections = Array.isArray(options.sections) && options.sections.length > 0
|
|
163
|
+
? options.sections.filter(id => SECTION_IDS.includes(id))
|
|
164
|
+
: [...DEFAULT_SECTIONS]
|
|
165
|
+
|
|
144
166
|
const digest = {
|
|
145
167
|
v: 1,
|
|
146
168
|
rulesVersion: RULES_VERSION,
|
|
@@ -161,66 +183,75 @@ export function buildDigest(checkpoint, options = {}) {
|
|
|
161
183
|
done,
|
|
162
184
|
nextSteps,
|
|
163
185
|
risks,
|
|
186
|
+
keywords,
|
|
187
|
+
questions,
|
|
188
|
+
sections,
|
|
164
189
|
params: { minMessages: FILTER_DEFAULTS.minMessages, minTokens: FILTER_DEFAULTS.minTokens },
|
|
165
190
|
}
|
|
166
191
|
digest.markdown = renderDigest(digest)
|
|
167
192
|
return digest
|
|
168
193
|
}
|
|
169
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Pick up to six salient terms for the keyword block.
|
|
197
|
+
* @param {string} text combined title/goal/conclusion.
|
|
198
|
+
* @returns {string[]}
|
|
199
|
+
*/
|
|
200
|
+
export function extractKeywords(text) {
|
|
201
|
+
const seen = new Set()
|
|
202
|
+
const out = []
|
|
203
|
+
for (const raw of String(text ?? '').split(/[^\p{L}\p{N}_\-+.]+/u)) {
|
|
204
|
+
const token = raw.trim()
|
|
205
|
+
if (token.length < 2 || token.length > 32) continue
|
|
206
|
+
const key = token.toLowerCase()
|
|
207
|
+
if (STOPWORDS.has(key) || seen.has(key)) continue
|
|
208
|
+
seen.add(key)
|
|
209
|
+
out.push(token)
|
|
210
|
+
if (out.length >= 6) break
|
|
211
|
+
}
|
|
212
|
+
return out
|
|
213
|
+
}
|
|
214
|
+
|
|
170
215
|
/**
|
|
171
216
|
* Render a digest as the Markdown block a user reads (and can paste).
|
|
217
|
+
* Only the sections selected in the config are rendered; intermediate tool
|
|
218
|
+
* actions are never part of the default set.
|
|
172
219
|
* @param {object} digest a digest record.
|
|
173
220
|
* @returns {string}
|
|
174
221
|
*/
|
|
175
222
|
export function renderDigest(digest) {
|
|
223
|
+
const enabled = Array.isArray(digest.sections) && digest.sections.length > 0
|
|
224
|
+
? digest.sections.filter(id => SECTION_IDS.includes(id))
|
|
225
|
+
: [...DEFAULT_SECTIONS]
|
|
226
|
+
const has = id => enabled.includes(id)
|
|
176
227
|
const lines = []
|
|
177
228
|
lines.push(`# 卷末 · ${digest.title ?? digest.sessionId}`)
|
|
178
229
|
lines.push('')
|
|
179
|
-
const meta = [
|
|
180
|
-
`会话 \`${digest.sessionId}\``,
|
|
181
|
-
`引擎 ${digest.engine}`,
|
|
182
|
-
`更新 ${new Date(digest.updatedAt).toISOString().replace('T', ' ').slice(0, 19)}`,
|
|
183
|
-
]
|
|
230
|
+
const meta = [digest.engine === 'api' ? 'API 摘要' : '本地规则', `更新 ${new Date(digest.updatedAt).toISOString().replace('T', ' ').slice(0, 16)}`]
|
|
184
231
|
if (digest.messageCount) meta.push(`${digest.messageCount} 条消息`)
|
|
185
232
|
lines.push(`> ${meta.join(' · ')}`)
|
|
186
233
|
lines.push('')
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
lines.push('## 结论')
|
|
192
|
-
lines.push('')
|
|
193
|
-
lines.push(digest.conclusion)
|
|
194
|
-
if (Array.isArray(digest.decisions) && digest.decisions.length > 0) {
|
|
195
|
-
lines.push('')
|
|
196
|
-
lines.push('## 决策')
|
|
197
|
-
lines.push('')
|
|
198
|
-
for (const item of digest.decisions) lines.push(`- ${item}`)
|
|
199
|
-
}
|
|
200
|
-
if (Array.isArray(digest.writes) && digest.writes.length > 0) {
|
|
201
|
-
lines.push('')
|
|
202
|
-
lines.push('## 涉及文件')
|
|
203
|
-
lines.push('')
|
|
204
|
-
for (const item of digest.writes) lines.push(`- ${item}`)
|
|
205
|
-
}
|
|
206
|
-
if (Array.isArray(digest.done) && digest.done.length > 0) {
|
|
207
|
-
lines.push('')
|
|
208
|
-
lines.push('## 已完成')
|
|
209
|
-
lines.push('')
|
|
210
|
-
for (const item of digest.done) lines.push(`- ${item}`)
|
|
211
|
-
}
|
|
212
|
-
if (digest.nextSteps.length > 0) {
|
|
213
|
-
lines.push('')
|
|
214
|
-
lines.push('## 下一步')
|
|
215
|
-
lines.push('')
|
|
216
|
-
for (const item of digest.nextSteps) lines.push(`- ${item}`)
|
|
217
|
-
}
|
|
218
|
-
if (digest.risks.length > 0) {
|
|
234
|
+
|
|
235
|
+
const block = (heading, body) => {
|
|
236
|
+
if (body === '' || body === undefined || body === null) return
|
|
237
|
+
lines.push(`## ${heading}`)
|
|
219
238
|
lines.push('')
|
|
220
|
-
|
|
239
|
+
if (Array.isArray(body)) {
|
|
240
|
+
for (const item of body) lines.push(`- ${item}`)
|
|
241
|
+
} else {
|
|
242
|
+
lines.push(body)
|
|
243
|
+
}
|
|
221
244
|
lines.push('')
|
|
222
|
-
for (const item of digest.risks) lines.push(`- ${item}`)
|
|
223
245
|
}
|
|
224
|
-
|
|
246
|
+
|
|
247
|
+
if (has('goal')) block('目标', digest.goal)
|
|
248
|
+
if (has('conclusion')) block('结论', digest.conclusion)
|
|
249
|
+
if (has('decisions')) block('决策', Array.isArray(digest.decisions) && digest.decisions.length > 0 ? digest.decisions : '')
|
|
250
|
+
if (has('files')) block('涉及文件', Array.isArray(digest.writes) && digest.writes.length > 0 ? digest.writes : '')
|
|
251
|
+
if (has('progress')) block('完成情况', Array.isArray(digest.done) && digest.done.length > 0 ? digest.done : '')
|
|
252
|
+
if (has('nextSteps')) block('下一步', Array.isArray(digest.nextSteps) && digest.nextSteps.length > 0 ? digest.nextSteps : '')
|
|
253
|
+
if (has('risks')) block('风险与注意', Array.isArray(digest.risks) && digest.risks.length > 0 ? digest.risks : '')
|
|
254
|
+
if (has('keywords')) block('关键词', Array.isArray(digest.keywords) && digest.keywords.length > 0 ? digest.keywords : '')
|
|
255
|
+
if (has('questions')) block('待确认问题', Array.isArray(digest.questions) && digest.questions.length > 0 ? digest.questions : '')
|
|
225
256
|
return `${lines.join('\n')}\n`
|
|
226
257
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-colophon",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Colophon(卷末)— keep a local, token-free conclusion digest for every conversation, so reopening an old session starts from its final state instead of its full history.",
|
|
5
5
|
"documentation": "README.md",
|
|
6
6
|
"license": "MIT",
|