dsh-colophon 0.1.0 → 0.2.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 CHANGED
@@ -12,21 +12,39 @@
12
12
  - 历史会话在启动时**本地回填**一次(读本地事件日志,无模型调用)。
13
13
  - 崩溃 / 断电 / 强制结束:每轮落盘 checkpoint + `lifecycle.json` 标记;下次启动检测到"非正常关闭"后自动本地重建,与 Office 自动恢复的预期一致。
14
14
 
15
- ## 安装(本地开发)
15
+ ## 截图
16
+
17
+ 设置页(引擎 / 触发 / API 预算 / 用量):
18
+
19
+ ![settings](docs/screenshots/settings.png)
20
+
21
+ 输入框上方的卷末卡片 + 崩溃恢复条:
22
+
23
+ ![composer](docs/screenshots/composer.png)
24
+
25
+ ## 安装
26
+
27
+ **npm(推荐)**
28
+
29
+ ```bash
30
+ dsh plugin --profile <你的 profile> add dsh-colophon
31
+ ```
32
+
33
+ > `dsh-colophon@0.1.0` 已发布到 npm;市场条目见 [awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)。与 billion-context 可共存——colophon 不接管上下文压缩,只做摘要沉淀与读取。
34
+
35
+ **本地开发 / 手动**
16
36
 
17
37
  ```bash
18
38
  dsh plugin --profile desktop add /Users/shibuya/Desktop/DeepSeek/colophon
19
39
  ```
20
40
 
21
- 或手动把本目录加进 profile `package.json` 的 `dsh.profile.bundles`:
41
+ 或把本目录加进 profile `package.json` 的 `dsh.profile.bundles`:
22
42
 
23
43
  ```json
24
44
  "dsh-colophon": "link:/Users/shibuya/Desktop/DeepSeek/colophon"
25
45
  ```
26
46
 
27
- > 注意:与 billion-context 可共存——colophon 不接管上下文压缩,只做摘要沉淀与读取。
28
-
29
- 没有 pnpm 的环境(本机)可手动安装,改两处:
47
+ 没有 pnpm 的环境可手动安装,改两处:
30
48
 
31
49
  ```bash
32
50
  mkdir -p ~/.dsh/profiles/desktop/node_modules
@@ -73,10 +91,18 @@ usage.jsonl API 调用账本(rules 引擎不写)
73
91
 
74
92
  - `session/event` 采集 → 每会话尾部状态(用户原文、最新回复、文件足迹、命令、工具错误)
75
93
  - 每轮 checkpoint(原子写)+ 30s 周期落盘 + `process.on('exit')` 同步落盘
76
- - `rules` 摘要引擎(Markdown,含 目标/结论/已完成/下一步/风险)
94
+ - `rules` 摘要引擎(Markdown,含 目标/结论/决策/涉及文件/已完成/下一步/风险)
77
95
  - 崩溃检测(`lifecycle.json`)与启动时本地回填(`sessionPersistence.open(id,'read')`)
78
96
  - 用量账本(token + 人民币,支持 时/日/周/月/自定义 窗口)
79
97
 
98
+ ## 0.2.0(摘要质量与配置安全)
99
+
100
+ - `rules` 提取升级:`## 决策` 小节、最后一条「已完成…」段落优先作结论、`- [ ]` 待办自动进下一步、写入文件单列「涉及文件」、规则版本号(旧摘要启动时自动重建)
101
+ - 凭据脱敏:`sk-*`、`ghp_*`、`AKIA*`、`Bearer …`、`password=…` 写入摘要前替换为 `[redacted]`
102
+ - API 引擎更稳:兼容 JSON 对象回复(字段 goal/conclusion/decisions/done/nextSteps/risks/writes),Markdown 小节继续可用,缺失字段回退本地事实
103
+ - 摘要面板新增「下载 Markdown」按钮(直接保存为 `.md` 文件)
104
+ - 版本号 0.2.0;测试 38 项全绿
105
+
80
106
  ## 已实现(P2 · 客户端)
81
107
 
82
108
  - 输入框上方「卷末」胶囊:点击展开摘要面板(Markdown 预览、复制、重新生成)
package/client.js CHANGED
@@ -31,6 +31,7 @@ window.__ModuleLoader__.load({
31
31
  regenerate: '重新生成',
32
32
  copy: '复制 Markdown',
33
33
  copied: '已复制',
34
+ download: '下载 Markdown',
34
35
  close: '关闭',
35
36
  loading: '读取中…',
36
37
  failed: '读取失败:{message}',
@@ -95,6 +96,7 @@ window.__ModuleLoader__.load({
95
96
  regenerate: 'Regenerate',
96
97
  copy: 'Copy Markdown',
97
98
  copied: 'Copied',
99
+ download: 'Download',
98
100
  close: 'Close',
99
101
  loading: 'Loading…',
100
102
  failed: 'Failed: {message}',
@@ -298,6 +300,19 @@ window.__ModuleLoader__.load({
298
300
  } catch { /* clipboard denied */ }
299
301
  }, [digest])
300
302
 
303
+ const download = useCallback(() => {
304
+ if (!digest?.markdown) return
305
+ try {
306
+ const blob = new Blob([digest.markdown], { type: 'text/markdown;charset=utf-8' })
307
+ const url = URL.createObjectURL(blob)
308
+ const anchor = document.createElement('a')
309
+ anchor.href = url
310
+ anchor.download = `colophon-${String(sessionId).slice(0, 8)}.md`
311
+ anchor.click()
312
+ setTimeout(() => URL.revokeObjectURL(url), 1000)
313
+ } catch { /* download blocked */ }
314
+ }, [digest, sessionId])
315
+
301
316
  // The hero (new session) has no history yet; keep it clean.
302
317
  if (variant === 'hero' || !sessionId) return null
303
318
 
@@ -321,6 +336,7 @@ window.__ModuleLoader__.load({
321
336
  h('button', { style: styles.button, onClick: () => void generate(), disabled: loading },
322
337
  digest ? tr('regenerate') : tr('generate')),
323
338
  digest?.markdown ? h('button', { style: styles.button, onClick: () => void copy() }, copied ? tr('copied') : tr('copy')) : null,
339
+ digest?.markdown ? h('button', { style: styles.button, onClick: () => void download() }, tr('download')) : null,
324
340
  h('button', { style: styles.button, onClick: () => setOpen(false) }, tr('close')))) : null
325
341
 
326
342
  return h(React.Fragment, null, h(CrashBanner), h('div', null, chip), panel)
package/lib/api.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * @module dsh-colophon/lib/api
6
6
  */
7
7
 
8
- import { bullets, clip, estimateTokens, lastParagraph, section } from './text.js'
8
+ import { bullets, clip, estimateTokens, lastParagraph, redactSecrets, section, withoutTrailingFence } from './text.js'
9
9
  import { costCNY } from './usage.js'
10
10
  import { renderDigest } from './digest.js'
11
11
 
@@ -18,6 +18,8 @@ export const API_PROMPT = [
18
18
  '一段话,说明用户最终想要什么。',
19
19
  '## 结论',
20
20
  '一段话,说明现在处于什么状态、结论是什么;没有结论就写“尚无结论”。',
21
+ '## 决策',
22
+ '- 条目列表(最多 4 条),列出会话中做出的关键决定及一句话理由;没有就省略该小节。',
21
23
  '## 已完成',
22
24
  '- 条目列表(最多 8 条),只列已确认完成的事。',
23
25
  '## 下一步',
@@ -96,13 +98,29 @@ export function estimateCost(built, config) {
96
98
  * @returns {object}
97
99
  */
98
100
  export function parseApiDigest(text, checkpoint, meta = {}) {
99
- const reply = String(text ?? '').trim()
100
101
  const now = meta.now ?? Date.now()
101
- const goal = section(reply, /(目标|goal)/iu) || (typeof checkpoint?.goal === 'string' ? checkpoint.goal : '')
102
- const conclusion = section(reply, /(结论|总结|conclusion|summary)/iu) || lastParagraph(reply)
103
- const done = bullets(reply, /(已完成|完成情况|done)/iu, 8, 140)
104
- const nextSteps = bullets(reply, /(下一步|后续|待办|next)/iu, 5, 140)
105
- const risks = bullets(reply, /(风险|注意|警告|warning|risk)/iu, 4, 140)
102
+ const reply = withoutTrailingFence(String(text ?? '').trim())
103
+ const fromJson = tryParseJsonDigest(reply)
104
+ const goal = fromJson?.goal ?? (section(reply, /(目标|goal)/iu) || (typeof checkpoint?.goal === 'string' ? checkpoint.goal : ''))
105
+ const conclusion = fromJson?.conclusion ?? (section(reply, /(结论|总结|conclusion|summary)/iu) || lastParagraph(reply))
106
+ const decisions = toList(fromJson?.decisions, 4, 140)
107
+ if (decisions.length === 0) decisions.push(...bullets(reply, /(决策|方案|决定|decision)/iu, 4, 140))
108
+ const done = toList(fromJson?.done, 8, 140)
109
+ if (done.length === 0) done.push(...bullets(reply, /(已完成|完成情况|done)/iu, 8, 140))
110
+ const nextSteps = toList(fromJson?.nextSteps, 5, 140)
111
+ if (nextSteps.length === 0) nextSteps.push(...bullets(reply, /(下一步|后续|待办|next)/iu, 5, 140))
112
+ const risks = toList(fromJson?.risks, 4, 140)
113
+ if (risks.length === 0) risks.push(...bullets(reply, /(风险|注意|警告|warning|risk)/iu, 4, 140))
114
+ const writes = toList(fromJson?.writes ?? fromJson?.files, 8, 160)
115
+ if (writes.length === 0) {
116
+ writes.push(...bullets(reply, /(涉及文件|相关文件|files|changed)/iu, 8, 160))
117
+ if (writes.length === 0) {
118
+ for (const file of Array.isArray(checkpoint?.files) ? checkpoint.files : []) {
119
+ if (file?.op === 'write' && typeof file.path === 'string' && !writes.includes(clip(redactSecrets(file.path), 160))) writes.push(clip(redactSecrets(file.path), 160))
120
+ if (writes.length >= 8) break
121
+ }
122
+ }
123
+ }
106
124
  const usage = meta.usage ?? {}
107
125
  const digest = {
108
126
  v: 1,
@@ -116,8 +134,10 @@ export function parseApiDigest(text, checkpoint, meta = {}) {
116
134
  messageCount: checkpoint?.messageCount ?? 0,
117
135
  estTokens: checkpoint?.estTokens ?? 0,
118
136
  seq: checkpoint?.seq ?? 0,
119
- goal: goal === '' ? '(未捕捉到目标)' : clip(goal, 900),
120
- conclusion: conclusion === '' ? '(本次对话没有留下文字结论)' : clip(conclusion, 900),
137
+ goal: goal === '' ? '(未捕捉到目标)' : clip(redactSecrets(String(goal)), 900),
138
+ conclusion: conclusion === '' ? '(本次对话没有留下文字结论)' : clip(redactSecrets(String(conclusion)), 900),
139
+ decisions,
140
+ writes,
121
141
  done,
122
142
  nextSteps,
123
143
  risks,
@@ -125,8 +145,46 @@ export function parseApiDigest(text, checkpoint, meta = {}) {
125
145
  input: Number(usage.inputTokens) || 0,
126
146
  output: Number(usage.outputTokens) || 0,
127
147
  },
128
- params: {},
148
+ params: { rulesVersion: 3 },
129
149
  }
130
150
  digest.markdown = renderDigest(digest)
131
151
  return digest
132
152
  }
153
+
154
+ /**
155
+ * Accept either the requested Markdown sections or a JSON object when the
156
+ * model answers with one (both are common; neither must break the digest).
157
+ * @param {string} reply trimmed reply text.
158
+ * @returns {any}
159
+ */
160
+ function tryParseJsonDigest(reply) {
161
+ const start = reply.indexOf('{')
162
+ if (start < 0) return null
163
+ const end = reply.lastIndexOf('}')
164
+ if (end <= start) return null
165
+ try {
166
+ const parsed = JSON.parse(reply.slice(start, end + 1))
167
+ return parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null
168
+ } catch {
169
+ return null
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Coerce a JSON field into a bounded list of redacted strings.
175
+ * @param {any} value JSON field (array of strings or objects with `text`).
176
+ * @param {number} limit maximum items.
177
+ * @param {number} maxChars per-item character cap.
178
+ * @returns {string[]}
179
+ */
180
+ function toList(value, limit, maxChars) {
181
+ if (!Array.isArray(value)) return []
182
+ const items = []
183
+ for (const entry of value) {
184
+ const text = typeof entry === 'string' ? entry : entry && typeof entry.text === 'string' ? entry.text : ''
185
+ const trimmed = text.trim()
186
+ if (trimmed !== '') items.push(clip(redactSecrets(trimmed), maxChars))
187
+ if (items.length >= limit) break
188
+ }
189
+ return items
190
+ }
package/lib/digest.js CHANGED
@@ -4,18 +4,21 @@
4
4
  * @module dsh-colophon/lib/digest
5
5
  */
6
6
 
7
- import { bullets, clip, lastParagraph, section, withoutTrailingFence } from './text.js'
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 = 2
10
+ export const RULES_VERSION = 3
11
11
 
12
12
  /** Defaults for the length filter (also mirrored in the Config schema). */
13
13
  export const FILTER_DEFAULTS = Object.freeze({ minMessages: 20, minTokens: 4000 })
14
14
 
15
- const CONCLUSION_HEADING = /(结论|总结|conclusion|summary|完成情况)/iu
15
+ const CONCLUSION_HEADING = /(结论|总结|完成情况|conclusion|summary|outcome)/iu
16
16
  const NEXT_HEADING = /(下一步|后续|待办|next step|next steps|todo|follow[- ]?up)/iu
17
17
  const RISK_HEADING = /(注意|风险|警告|warning|caution|limitation|限制|blocker)/iu
18
18
  const RISK_LINE = /(注意|风险|警告|warning|caution|blocker|无法|失败|cannot|can't)/iu
19
+ const DECISION_HEADING = /(决策|方案|决定|取舍|design|decision)/iu
20
+ const DONE_WORDS = /(完成|已完成|已实现|已修复|已更新|已通过|fixed|done|implemented|shipped)/iu
21
+ const CHECKBOX = /^\s*[-*]\s*\[ \]\s+\S/u
19
22
 
20
23
  /**
21
24
  * Decide whether a session is long enough to deserve a digest.
@@ -33,15 +36,39 @@ export function shouldSummarize(checkpoint, filter = {}) {
33
36
 
34
37
  /**
35
38
  * The conclusion of a session tail: an explicit conclusion section wins, then
36
- * the last paragraph of the last reply.
39
+ * the last paragraph that reports completion, then the last paragraph.
37
40
  * @param {string} assistantText last assistant reply.
38
41
  * @returns {string}
39
42
  */
40
43
  export function conclusionOf(assistantText) {
41
44
  const explicit = section(assistantText, CONCLUSION_HEADING)
42
- if (explicit !== '') return clip(explicit, 700)
45
+ if (explicit !== '') return clip(redactSecrets(explicit), 700)
46
+ const paragraphs = String(assistantText ?? '')
47
+ .replace(/\r\n/gu, '\n')
48
+ .split(/\n{2,}/u)
49
+ .map(part => part.trim())
50
+ .filter(part => part !== '')
51
+ const finished = [...paragraphs].reverse().find(part => DONE_WORDS.test(part))
52
+ if (finished !== undefined) return clip(redactSecrets(withoutTrailingFence(finished)), 700)
43
53
  const paragraph = withoutTrailingFence(lastParagraph(assistantText))
44
- return paragraph === '' ? '' : clip(paragraph, 700)
54
+ return paragraph === '' ? '' : clip(redactSecrets(paragraph), 700)
55
+ }
56
+
57
+ /**
58
+ * Unchecked markdown checkboxes of a reply, i.e. the author's own todo list.
59
+ * @param {string} text assistant reply.
60
+ * @param {number} [limit] maximum items.
61
+ * @returns {string[]}
62
+ */
63
+ export function uncheckedTodos(text, limit = 4) {
64
+ const items = []
65
+ for (const line of String(text ?? '').split('\n')) {
66
+ if (!CHECKBOX.test(line)) continue
67
+ const item = line.replace(/^\s*[-*]\s*\[ \]\s+/u, '').trim()
68
+ if (item !== '') items.push(clip(redactSecrets(item), 140))
69
+ if (items.length >= limit) break
70
+ }
71
+ return items
45
72
  }
46
73
 
47
74
  /**
@@ -58,43 +85,60 @@ export function buildDigest(checkpoint, options = {}) {
58
85
  const errors = Array.isArray(checkpoint.toolErrors) ? checkpoint.toolErrors : []
59
86
  const assistantText = String(checkpoint.assistantText ?? '')
60
87
 
88
+ const writes = []
61
89
  const done = []
62
90
  for (const file of files) {
63
- if (done.length >= 8) break
64
- if (file && typeof file.path === 'string') done.push(`${file.op === 'write' ? '写入' : '读取'} ${clip(file.path, 120)}`)
91
+ if (!file || typeof file.path !== 'string') continue
92
+ const path = clip(file.path, 120)
93
+ if (file.op === 'write' && !writes.includes(path)) writes.push(path)
94
+ if (done.length >= 8) continue
95
+ done.push(`${file.op === 'write' ? '修改' : '读取'} ${path}`)
65
96
  }
66
97
  for (const command of commands) {
67
98
  if (done.length >= 8) break
68
99
  done.push(`运行 ${clip(command, 120)}`)
69
100
  }
70
101
 
71
- let nextSteps = bullets(assistantText, NEXT_HEADING, 5, 140)
102
+ const nextSteps = uncheckedTodos(assistantText)
103
+ if (nextSteps.length === 0) nextSteps.push(...bullets(assistantText, NEXT_HEADING, 5, 140))
72
104
  if (nextSteps.length === 0) {
73
105
  for (const line of assistantText.split('\n')) {
74
106
  const item = line.replace(/^\s*(?:[-*+]|\d+[.)])\s+/u, '').trim()
75
- if (item !== '' && /^(?:下一步|待办|todo|next)[::]/iu.test(item)) nextSteps.push(clip(item, 140))
107
+ if (item !== '' && /^(?:下一步|待办|todo|next)[::]/iu.test(item)) nextSteps.push(clip(redactSecrets(item), 140))
76
108
  if (nextSteps.length >= 5) break
77
109
  }
78
110
  }
79
111
 
112
+ const decisions = bullets(assistantText, DECISION_HEADING, 4, 140)
113
+ if (decisions.length === 0) {
114
+ for (const line of assistantText.split('\n')) {
115
+ const item = line.trim()
116
+ if (item !== '' && item.length <= 160 && /(决定|采用|选择|取舍|改用)/u.test(item)) {
117
+ decisions.push(clip(redactSecrets(item.replace(/^[#>\-*\s`]+/u, '')), 140))
118
+ }
119
+ if (decisions.length >= 3) break
120
+ }
121
+ }
122
+
80
123
  const risks = []
81
124
  const seenErrors = new Set()
82
125
  for (const error of errors) {
83
126
  const name = typeof error?.name === 'string' ? error.name : 'tool'
84
127
  if (seenErrors.has(name)) continue
85
128
  seenErrors.add(name)
86
- risks.push(`工具 ${clip(name, 60)} 报错${typeof error?.reason === 'string' ? `:${clip(error.reason, 80)}` : ''}`)
129
+ risks.push(`工具 ${clip(name, 60)} 报错${typeof error?.reason === 'string' ? `:${clip(redactSecrets(error.reason), 80)}` : ''}`)
87
130
  if (risks.length >= 3) break
88
131
  }
89
132
  for (const line of assistantText.split('\n')) {
90
133
  if (risks.length >= 4) break
91
134
  const item = line.replace(/^\s*(?:[-*+]|\d+[.)])\s+/u, '').trim()
92
- if (item !== '' && item.length <= 160 && RISK_LINE.test(item) && !risks.includes(clip(item, 140))) {
93
- risks.push(clip(item, 140))
135
+ if (item !== '' && item.length <= 160 && RISK_LINE.test(item)) {
136
+ const risk = clip(redactSecrets(item), 140)
137
+ if (!risks.includes(risk)) risks.push(risk)
94
138
  }
95
139
  }
96
140
 
97
- const goal = typeof checkpoint.goal === 'string' && checkpoint.goal !== '' ? checkpoint.goal : '(未捕捉到目标)'
141
+ const goal = redactSecrets(typeof checkpoint.goal === 'string' && checkpoint.goal !== '' ? checkpoint.goal : '(未捕捉到目标)')
98
142
  const conclusion = conclusionOf(assistantText) || (checkpoint.messageCount === 0 ? '(空会话)' : '(本次对话没有留下文字结论)')
99
143
 
100
144
  const digest = {
@@ -112,6 +156,8 @@ export function buildDigest(checkpoint, options = {}) {
112
156
  seq: checkpoint.seq ?? 0,
113
157
  goal,
114
158
  conclusion,
159
+ decisions,
160
+ writes,
115
161
  done,
116
162
  nextSteps,
117
163
  risks,
@@ -145,7 +191,19 @@ export function renderDigest(digest) {
145
191
  lines.push('## 结论')
146
192
  lines.push('')
147
193
  lines.push(digest.conclusion)
148
- if (digest.done.length > 0) {
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) {
149
207
  lines.push('')
150
208
  lines.push('## 已完成')
151
209
  lines.push('')
package/lib/text.js CHANGED
@@ -157,6 +157,32 @@ export function bullets(text, heading, limit = 5, maxChars = 120) {
157
157
  return items
158
158
  }
159
159
 
160
+ /**
161
+ * Redact credential-shaped strings before a digest is written to disk or
162
+ * pasted into another conversation.
163
+ * @param {string} text source text.
164
+ * @returns {string}
165
+ */
166
+ export function redactSecrets(text) {
167
+ return String(text ?? '')
168
+ .replace(/\b(?:sk|rk|pk)-[A-Za-z0-9_-]{12,}\b/gu, '[redacted-key]')
169
+ .replace(/\bgh[pousr]_[A-Za-z0-9]{16,}\b/gu, '[redacted-token]')
170
+ .replace(/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/gu, '[redacted-aws-key]')
171
+ .replace(/\b(Bearer)\s+[A-Za-z0-9._-]{12,}/giu, '$1 [redacted]')
172
+ .replace(/\b(password|passwd|secret|token|api[_-]?key)\s*[=:]\s*['"]?[^\s'"]{6,}/giu, '$1=[redacted]')
173
+ }
174
+
175
+ /**
176
+ * Whether a text is predominantly Chinese (used to pick the digest wording).
177
+ * @param {string} text source text.
178
+ * @returns {boolean}
179
+ */
180
+ export function isChinese(text) {
181
+ const value = String(text ?? '')
182
+ const cjk = (value.match(/[\u3400-\u9fff]/gu) ?? []).length
183
+ return cjk >= Math.max(2, value.length / 8)
184
+ }
185
+
160
186
  /**
161
187
  * Rough token estimate for a string (the 4-chars-per-token heuristic used by
162
188
  * the context dashboards).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-colophon",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
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",