dsh-colophon 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +149 -2
- package/client.js +557 -0
- package/cordis.patch.yml +5 -0
- package/icon.svg +8 -0
- package/index.js +66 -0
- package/lib/api.js +190 -0
- package/lib/app.js +700 -0
- package/lib/collect.js +271 -0
- package/lib/digest.js +226 -0
- package/lib/files.js +113 -0
- package/lib/store.js +255 -0
- package/lib/text.js +207 -0
- package/lib/usage.js +133 -0
- package/package.json +63 -4
package/client.js
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of Colophon: the 卷末 (digest) chip docked above the composer,
|
|
3
|
+
* the digest panel, the crash-recovery banner and the settings section with
|
|
4
|
+
* token / CNY usage cards.
|
|
5
|
+
*
|
|
6
|
+
* Plain JS on purpose — DSH serves this file directly through the client
|
|
7
|
+
* module loader, so no build step is involved.
|
|
8
|
+
* @module dsh-colophon/client
|
|
9
|
+
*/
|
|
10
|
+
window.__ModuleLoader__.load({
|
|
11
|
+
id: 'dsh-colophon',
|
|
12
|
+
factory(require) {
|
|
13
|
+
const React = require('react')
|
|
14
|
+
const h = React.createElement
|
|
15
|
+
const { useCallback, useEffect, useState } = React
|
|
16
|
+
|
|
17
|
+
const NS = 'colophon'
|
|
18
|
+
const COMPOSER_BAR = 'conversation.input.dock'
|
|
19
|
+
const SETTINGS_SECTION = 'settings.section'
|
|
20
|
+
const CRASH_ACK_KEY = 'colophon:crash-ack'
|
|
21
|
+
|
|
22
|
+
const zh = (navigator.language || 'zh').toLowerCase().startsWith('zh')
|
|
23
|
+
const DICT = {
|
|
24
|
+
zh: {
|
|
25
|
+
chip: '卷末',
|
|
26
|
+
chipReady: '卷末 · 已就绪',
|
|
27
|
+
chipEmpty: '卷末 · 待生成',
|
|
28
|
+
panelTitle: '卷末',
|
|
29
|
+
empty: '这条会话还没有卷末:对话太短,或者还没生成。',
|
|
30
|
+
generate: '立即生成',
|
|
31
|
+
regenerate: '重新生成',
|
|
32
|
+
copy: '复制 Markdown',
|
|
33
|
+
copied: '已复制',
|
|
34
|
+
download: '下载 Markdown',
|
|
35
|
+
close: '关闭',
|
|
36
|
+
loading: '读取中…',
|
|
37
|
+
failed: '读取失败:{message}',
|
|
38
|
+
crash: '上次 DSH 没有正常退出,卷末已在本地恢复(0 token)。',
|
|
39
|
+
crashAck: '知道了',
|
|
40
|
+
settingsLabel: '卷末 · Colophon',
|
|
41
|
+
settingsIntro: '规则引擎只读本地日志,不调用模型、不花 token;API 摘要为可选项。',
|
|
42
|
+
engine: '摘要引擎',
|
|
43
|
+
engineRules: '本地规则(0 token)',
|
|
44
|
+
engineApi: 'API 摘要(可选的增强)',
|
|
45
|
+
enabled: '启用卷末',
|
|
46
|
+
triggers: '触发时机',
|
|
47
|
+
onIdle: '会话空闲时',
|
|
48
|
+
onSwitch: '切换到其他会话时',
|
|
49
|
+
closeBatch: '关闭 DSH 时批量生成全部会话',
|
|
50
|
+
closeBatchWarn: '批量生成会在退出前集中调用模型;未完成的任务会进入待确认队列。',
|
|
51
|
+
closeBatchMax: '批量上限(会话数)',
|
|
52
|
+
apiSection: 'API 摘要设置(可选)',
|
|
53
|
+
apiNote: 'API 摘要默认关闭;开启后每次调用先检查单次与每日预算。',
|
|
54
|
+
provider: 'Provider',
|
|
55
|
+
model: '模型',
|
|
56
|
+
priceIn: '输入价(元/百万 token)',
|
|
57
|
+
priceOut: '输出价(元/百万 token)',
|
|
58
|
+
perSummaryCap: '单次摘要上限(元)',
|
|
59
|
+
dailyBudget: '每日 token 预算',
|
|
60
|
+
overCap: '超预算时',
|
|
61
|
+
overCapQueue: '排队,下次启动时确认',
|
|
62
|
+
overCapSkip: '直接跳过',
|
|
63
|
+
filter: '会话筛选',
|
|
64
|
+
minMessages: '最少消息数',
|
|
65
|
+
minTokens: '最少 token 数',
|
|
66
|
+
usage: '用量',
|
|
67
|
+
usageHour: '本小时',
|
|
68
|
+
usageDay: '今天',
|
|
69
|
+
usageWeek: '本周',
|
|
70
|
+
usageMonth: '本月',
|
|
71
|
+
usageCustom: '自定义区间',
|
|
72
|
+
from: '起',
|
|
73
|
+
to: '止',
|
|
74
|
+
calls: '{count} 次调用',
|
|
75
|
+
tokensLabel: '{tokens} token',
|
|
76
|
+
localZero: '规则模式 · 0 token',
|
|
77
|
+
save: '保存设置',
|
|
78
|
+
saved: '设置已保存',
|
|
79
|
+
saveFailed: '保存失败:{message}',
|
|
80
|
+
dataRoot: '数据目录',
|
|
81
|
+
queue: '待确认队列',
|
|
82
|
+
queueTitle: '待确认队列',
|
|
83
|
+
queueEmpty: '没有等待确认的会话',
|
|
84
|
+
queueProcess: '处理队列',
|
|
85
|
+
queueRun: '队列已处理',
|
|
86
|
+
apiUnavailable: '当前环境没有可用的模型服务(llm):API 引擎会回退到本地 rules 摘要,并把它放进待确认队列。',
|
|
87
|
+
statusFailed: '状态读取失败',
|
|
88
|
+
},
|
|
89
|
+
en: {
|
|
90
|
+
chip: 'Colophon',
|
|
91
|
+
chipReady: 'Colophon · ready',
|
|
92
|
+
chipEmpty: 'Colophon · pending',
|
|
93
|
+
panelTitle: 'Colophon',
|
|
94
|
+
empty: 'No digest yet for this session: the conversation is short or nothing was generated.',
|
|
95
|
+
generate: 'Generate now',
|
|
96
|
+
regenerate: 'Regenerate',
|
|
97
|
+
copy: 'Copy Markdown',
|
|
98
|
+
copied: 'Copied',
|
|
99
|
+
download: 'Download',
|
|
100
|
+
close: 'Close',
|
|
101
|
+
loading: 'Loading…',
|
|
102
|
+
failed: 'Failed: {message}',
|
|
103
|
+
crash: 'Last DSH exit was not clean. Digests were rebuilt locally (0 tokens).',
|
|
104
|
+
crashAck: 'Dismiss',
|
|
105
|
+
settingsLabel: 'Colophon',
|
|
106
|
+
settingsIntro: 'The rules engine only reads local logs — no model call, no token spend. API summaries are optional.',
|
|
107
|
+
engine: 'Summary engine',
|
|
108
|
+
engineRules: 'Local rules (0 tokens)',
|
|
109
|
+
engineApi: 'API summary (optional)',
|
|
110
|
+
enabled: 'Enable Colophon',
|
|
111
|
+
triggers: 'Triggers',
|
|
112
|
+
onIdle: 'When a session goes idle',
|
|
113
|
+
onSwitch: 'When switching to another session',
|
|
114
|
+
closeBatch: 'Batch all sessions when DSH closes',
|
|
115
|
+
closeBatchWarn: 'Batching calls the model right before exit; unfinished work is queued for confirmation.',
|
|
116
|
+
closeBatchMax: 'Batch limit (sessions)',
|
|
117
|
+
apiSection: 'API summary settings (optional)',
|
|
118
|
+
apiNote: 'API summaries are off by default; every call is checked against the per-summary and daily budgets.',
|
|
119
|
+
provider: 'Provider',
|
|
120
|
+
model: 'Model',
|
|
121
|
+
priceIn: 'Input price (CNY / 1M tokens)',
|
|
122
|
+
priceOut: 'Output price (CNY / 1M tokens)',
|
|
123
|
+
perSummaryCap: 'Per-summary cap (CNY)',
|
|
124
|
+
dailyBudget: 'Daily token budget',
|
|
125
|
+
overCap: 'When over budget',
|
|
126
|
+
overCapQueue: 'Queue for next-start confirmation',
|
|
127
|
+
overCapSkip: 'Skip',
|
|
128
|
+
filter: 'Session filter',
|
|
129
|
+
minMessages: 'Minimum messages',
|
|
130
|
+
minTokens: 'Minimum tokens',
|
|
131
|
+
usage: 'Usage',
|
|
132
|
+
usageHour: 'This hour',
|
|
133
|
+
usageDay: 'Today',
|
|
134
|
+
usageWeek: 'This week',
|
|
135
|
+
usageMonth: 'This month',
|
|
136
|
+
usageCustom: 'Custom range',
|
|
137
|
+
from: 'From',
|
|
138
|
+
to: 'To',
|
|
139
|
+
calls: '{count} calls',
|
|
140
|
+
tokensLabel: '{tokens} tokens',
|
|
141
|
+
localZero: 'Rules mode · 0 tokens',
|
|
142
|
+
save: 'Save',
|
|
143
|
+
saved: 'Settings saved',
|
|
144
|
+
saveFailed: 'Save failed: {message}',
|
|
145
|
+
dataRoot: 'Data dir',
|
|
146
|
+
queue: 'Pending queue',
|
|
147
|
+
queueTitle: 'Pending queue',
|
|
148
|
+
queueEmpty: 'Nothing is waiting for confirmation',
|
|
149
|
+
queueProcess: 'Process queue',
|
|
150
|
+
queueRun: 'Queue processed',
|
|
151
|
+
apiUnavailable: 'No model service (llm) is available here: the API engine falls back to local rules digests and queues the session for confirmation.',
|
|
152
|
+
statusFailed: 'Status unavailable',
|
|
153
|
+
},
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Translate a key, interpolating `{name}` placeholders. */
|
|
157
|
+
function tr(key, params) {
|
|
158
|
+
const text = (DICT[zh ? 'zh' : 'en'][key] ?? key).replace(/\{(\w+)\}/gu, (_, name) => String(params?.[name] ?? ''))
|
|
159
|
+
return text
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** One authenticated Host call; unwraps `{ ok, value }`. */
|
|
163
|
+
async function callHost(path, options = {}) {
|
|
164
|
+
const response = await fetch(`api/colophon/${path}`, {
|
|
165
|
+
method: options.method ?? 'GET',
|
|
166
|
+
headers: options.body === undefined ? undefined : { 'content-type': 'application/json' },
|
|
167
|
+
credentials: 'same-origin',
|
|
168
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
169
|
+
signal: options.signal,
|
|
170
|
+
})
|
|
171
|
+
const payload = await response.json().catch(() => undefined)
|
|
172
|
+
if (payload === undefined) throw new Error(`HTTP ${response.status}`)
|
|
173
|
+
if (payload.ok !== true) throw new Error(payload.error?.message ?? `HTTP ${response.status}`)
|
|
174
|
+
return payload.value
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const money = value => `¥${(Number(value) || 0).toFixed(4)}`
|
|
178
|
+
const integer = value => (Number(value) || 0).toLocaleString(zh ? 'zh-CN' : 'en-US')
|
|
179
|
+
|
|
180
|
+
const styles = {
|
|
181
|
+
chip: {
|
|
182
|
+
display: 'inline-flex', alignItems: 'center', gap: 6, padding: '2px 10px',
|
|
183
|
+
border: '1px solid rgba(255,255,255,0.18)', borderRadius: 999, background: 'transparent',
|
|
184
|
+
color: 'inherit', cursor: 'pointer', fontSize: 12, lineHeight: '18px', height: 24,
|
|
185
|
+
},
|
|
186
|
+
dot: { width: 7, height: 7, borderRadius: 999, background: '#4cc38a', display: 'inline-block' },
|
|
187
|
+
dotEmpty: { width: 7, height: 7, borderRadius: 999, background: 'rgba(255,255,255,0.35)', display: 'inline-block' },
|
|
188
|
+
panel: {
|
|
189
|
+
position: 'fixed', right: 16, bottom: 96, width: 380, maxWidth: 'calc(100vw - 32px)',
|
|
190
|
+
maxHeight: '62vh', overflow: 'auto', zIndex: 60, padding: 14,
|
|
191
|
+
background: 'rgba(28,30,36,0.98)', color: '#e8e8e8',
|
|
192
|
+
border: '1px solid rgba(255,255,255,0.14)', borderRadius: 12,
|
|
193
|
+
boxShadow: '0 12px 40px rgba(0,0,0,0.45)', fontSize: 12, lineHeight: 1.55,
|
|
194
|
+
},
|
|
195
|
+
panelHead: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 8 },
|
|
196
|
+
title: { fontSize: 13, fontWeight: 600 },
|
|
197
|
+
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 },
|
|
198
|
+
actions: { display: 'flex', gap: 8, marginTop: 10, flexWrap: 'wrap' },
|
|
199
|
+
button: {
|
|
200
|
+
padding: '4px 10px', borderRadius: 8, border: '1px solid rgba(255,255,255,0.2)',
|
|
201
|
+
background: 'rgba(255,255,255,0.08)', color: 'inherit', cursor: 'pointer', fontSize: 12,
|
|
202
|
+
},
|
|
203
|
+
muted: { opacity: 0.65 },
|
|
204
|
+
banner: {
|
|
205
|
+
position: 'fixed', top: 12, left: '50%', transform: 'translateX(-50%)', zIndex: 70,
|
|
206
|
+
display: 'flex', alignItems: 'center', gap: 10, padding: '8px 12px',
|
|
207
|
+
background: 'rgba(84,64,16,0.98)', color: '#ffe9b0', border: '1px solid rgba(255,220,140,0.35)',
|
|
208
|
+
borderRadius: 10, fontSize: 12, boxShadow: '0 8px 24px rgba(0,0,0,0.35)',
|
|
209
|
+
},
|
|
210
|
+
field: { display: 'flex', flexDirection: 'column', gap: 3, minWidth: 120 },
|
|
211
|
+
label: { fontSize: 11, opacity: 0.7 },
|
|
212
|
+
input: {
|
|
213
|
+
background: 'rgba(0,0,0,0.28)', color: 'inherit', border: '1px solid rgba(255,255,255,0.18)',
|
|
214
|
+
borderRadius: 6, padding: '3px 6px', fontSize: 12, width: 110,
|
|
215
|
+
},
|
|
216
|
+
row: { display: 'flex', gap: 14, flexWrap: 'wrap', alignItems: 'flex-end' },
|
|
217
|
+
section: { borderTop: '1px solid rgba(255,255,255,0.1)', paddingTop: 12, display: 'flex', flexDirection: 'column', gap: 10 },
|
|
218
|
+
sectionTitle: { fontSize: 12, fontWeight: 600, opacity: 0.9 },
|
|
219
|
+
check: { display: 'flex', alignItems: 'center', gap: 6, fontSize: 12 },
|
|
220
|
+
cards: { display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(140px, 1fr))', gap: 8 },
|
|
221
|
+
card: { border: '1px solid rgba(255,255,255,0.12)', borderRadius: 8, padding: '8px 10px' },
|
|
222
|
+
cardValue: { fontSize: 15, fontWeight: 600, marginTop: 2 },
|
|
223
|
+
warn: { color: '#ffd27a', fontSize: 11 },
|
|
224
|
+
ok: { color: '#4cc38a', fontSize: 11 },
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Read/persist the crash banner acknowledgement. */
|
|
228
|
+
function readAck() {
|
|
229
|
+
try { return localStorage.getItem(CRASH_ACK_KEY) } catch { return null }
|
|
230
|
+
}
|
|
231
|
+
function writeAck(value) {
|
|
232
|
+
try { localStorage.setItem(CRASH_ACK_KEY, value) } catch { /* storage disabled */ }
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Banner shown when the previous shutdown was unclean. */
|
|
236
|
+
function CrashBanner() {
|
|
237
|
+
const [status, setStatus] = useState(null)
|
|
238
|
+
const [ack, setAck] = useState(readAck)
|
|
239
|
+
useEffect(() => {
|
|
240
|
+
let alive = true
|
|
241
|
+
callHost('status').then(value => { if (alive) setStatus(value) }).catch(() => {})
|
|
242
|
+
return () => { alive = false }
|
|
243
|
+
}, [])
|
|
244
|
+
const crashAt = status?.recovery?.crashAt
|
|
245
|
+
if (status?.recovery?.crashDetected !== true || !crashAt || ack === String(crashAt)) return null
|
|
246
|
+
return h('div', { style: styles.banner, role: 'status' },
|
|
247
|
+
h('span', null, tr('crash')),
|
|
248
|
+
h('button', {
|
|
249
|
+
style: styles.button,
|
|
250
|
+
onClick: () => { writeAck(String(crashAt)); setAck(String(crashAt)) },
|
|
251
|
+
}, tr('crashAck')))
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The chip + floating digest panel above the composer. */
|
|
255
|
+
function ColophonDock(props) {
|
|
256
|
+
const sessionId = props.sessionId
|
|
257
|
+
const variant = props.variant
|
|
258
|
+
const [open, setOpen] = useState(false)
|
|
259
|
+
const [loading, setLoading] = useState(false)
|
|
260
|
+
const [digest, setDigest] = useState(null)
|
|
261
|
+
const [error, setError] = useState('')
|
|
262
|
+
const [copied, setCopied] = useState(false)
|
|
263
|
+
|
|
264
|
+
const load = useCallback(async () => {
|
|
265
|
+
if (!sessionId) return
|
|
266
|
+
setLoading(true)
|
|
267
|
+
setError('')
|
|
268
|
+
try {
|
|
269
|
+
const value = await callHost(`summary?sessionId=${encodeURIComponent(sessionId)}`)
|
|
270
|
+
setDigest(value?.digest ?? null)
|
|
271
|
+
} catch (cause) {
|
|
272
|
+
setError(String(cause?.message ?? cause))
|
|
273
|
+
} finally {
|
|
274
|
+
setLoading(false)
|
|
275
|
+
}
|
|
276
|
+
}, [sessionId])
|
|
277
|
+
|
|
278
|
+
useEffect(() => { if (open) void load() }, [open, load])
|
|
279
|
+
|
|
280
|
+
const generate = useCallback(async () => {
|
|
281
|
+
if (!sessionId) return
|
|
282
|
+
setLoading(true)
|
|
283
|
+
setError('')
|
|
284
|
+
try {
|
|
285
|
+
const value = await callHost('summarize', { method: 'POST', body: { sessionId } })
|
|
286
|
+
setDigest(value?.digest ?? null)
|
|
287
|
+
} catch (cause) {
|
|
288
|
+
setError(String(cause?.message ?? cause))
|
|
289
|
+
} finally {
|
|
290
|
+
setLoading(false)
|
|
291
|
+
}
|
|
292
|
+
}, [sessionId])
|
|
293
|
+
|
|
294
|
+
const copy = useCallback(async () => {
|
|
295
|
+
if (!digest?.markdown) return
|
|
296
|
+
try {
|
|
297
|
+
await navigator.clipboard.writeText(digest.markdown)
|
|
298
|
+
setCopied(true)
|
|
299
|
+
setTimeout(() => setCopied(false), 1500)
|
|
300
|
+
} catch { /* clipboard denied */ }
|
|
301
|
+
}, [digest])
|
|
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
|
+
|
|
316
|
+
// The hero (new session) has no history yet; keep it clean.
|
|
317
|
+
if (variant === 'hero' || !sessionId) return null
|
|
318
|
+
|
|
319
|
+
const chip = h('button', {
|
|
320
|
+
style: styles.chip,
|
|
321
|
+
title: tr('panelTitle'),
|
|
322
|
+
onClick: () => setOpen(current => !current),
|
|
323
|
+
},
|
|
324
|
+
h('span', { style: digest ? styles.dot : styles.dotEmpty }),
|
|
325
|
+
h('span', null, digest ? tr('chipReady') : tr('chipEmpty')))
|
|
326
|
+
|
|
327
|
+
const panel = open ? h('div', { style: styles.panel, role: 'dialog', 'aria-label': tr('panelTitle') },
|
|
328
|
+
h('div', { style: styles.panelHead },
|
|
329
|
+
h('span', { style: styles.title }, tr('panelTitle')),
|
|
330
|
+
h('span', { style: styles.muted }, digest?.updatedAt ? new Date(digest.updatedAt).toLocaleString() : '')),
|
|
331
|
+
loading ? h('div', { style: styles.muted }, tr('loading'))
|
|
332
|
+
: error ? h('div', { style: styles.warn }, tr('failed', { message: error }))
|
|
333
|
+
: digest?.markdown ? h('pre', { style: styles.pre }, digest.markdown)
|
|
334
|
+
: h('div', { style: styles.muted }, tr('empty')),
|
|
335
|
+
h('div', { style: styles.actions },
|
|
336
|
+
h('button', { style: styles.button, onClick: () => void generate(), disabled: loading },
|
|
337
|
+
digest ? tr('regenerate') : tr('generate')),
|
|
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,
|
|
340
|
+
h('button', { style: styles.button, onClick: () => setOpen(false) }, tr('close')))) : null
|
|
341
|
+
|
|
342
|
+
return h(React.Fragment, null, h(CrashBanner), h('div', null, chip), panel)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** One labelled number input bound to a form field. */
|
|
346
|
+
function NumberField({ label, value, onChange, step }) {
|
|
347
|
+
return h('label', { style: styles.field },
|
|
348
|
+
h('span', { style: styles.label }, label),
|
|
349
|
+
h('input', {
|
|
350
|
+
style: styles.input,
|
|
351
|
+
type: 'number',
|
|
352
|
+
step: step ?? 1,
|
|
353
|
+
value: value ?? '',
|
|
354
|
+
onChange: event => onChange(Number(event.target.value)),
|
|
355
|
+
}))
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** One checkbox row. */
|
|
359
|
+
function Check({ label, checked, onChange }) {
|
|
360
|
+
return h('label', { style: styles.check },
|
|
361
|
+
h('input', { type: 'checkbox', checked: checked === true, onChange: event => onChange(event.target.checked) }),
|
|
362
|
+
h('span', null, label))
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** Settings page section: engine, triggers, API budget and usage cards. */
|
|
366
|
+
function ColophonSettings() {
|
|
367
|
+
const [form, setForm] = useState(null)
|
|
368
|
+
const [status, setStatus] = useState(null)
|
|
369
|
+
const [custom, setCustom] = useState(null)
|
|
370
|
+
const [message, setMessage] = useState('')
|
|
371
|
+
const [failed, setFailed] = useState('')
|
|
372
|
+
|
|
373
|
+
useEffect(() => {
|
|
374
|
+
let alive = true
|
|
375
|
+
void (async () => {
|
|
376
|
+
try {
|
|
377
|
+
const [config, statistics] = await Promise.all([callHost('config'), callHost('status')])
|
|
378
|
+
if (!alive) return
|
|
379
|
+
setForm(config.config)
|
|
380
|
+
setStatus(statistics)
|
|
381
|
+
} catch (cause) {
|
|
382
|
+
if (alive) setFailed(String(cause?.message ?? cause))
|
|
383
|
+
}
|
|
384
|
+
})()
|
|
385
|
+
return () => { alive = false }
|
|
386
|
+
}, [])
|
|
387
|
+
|
|
388
|
+
const update = (section, key, value) => {
|
|
389
|
+
setForm(current => {
|
|
390
|
+
if (!current) return current
|
|
391
|
+
const next = { ...current, [section]: { ...current[section], [key]: value } }
|
|
392
|
+
return next
|
|
393
|
+
})
|
|
394
|
+
setMessage('')
|
|
395
|
+
}
|
|
396
|
+
const updateTop = (key, value) => {
|
|
397
|
+
setForm(current => (current ? { ...current, [key]: value } : current))
|
|
398
|
+
setMessage('')
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const save = async () => {
|
|
402
|
+
if (!form) return
|
|
403
|
+
setFailed('')
|
|
404
|
+
try {
|
|
405
|
+
const patch = {
|
|
406
|
+
enabled: form.enabled,
|
|
407
|
+
engine: form.engine,
|
|
408
|
+
api: form.api,
|
|
409
|
+
triggers: form.triggers,
|
|
410
|
+
filter: form.filter,
|
|
411
|
+
}
|
|
412
|
+
const value = await callHost('config', { method: 'POST', body: { patch } })
|
|
413
|
+
setForm(value.config)
|
|
414
|
+
setStatus(await callHost('status'))
|
|
415
|
+
setMessage(tr('saved'))
|
|
416
|
+
} catch (cause) {
|
|
417
|
+
setFailed(tr('saveFailed', { message: String(cause?.message ?? cause) }))
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const loadCustom = async (from, to) => {
|
|
422
|
+
if (!Number.isFinite(from) || !Number.isFinite(to) || from > to) { setCustom(null); return }
|
|
423
|
+
try {
|
|
424
|
+
const value = await callHost(`usage?from=${from}&to=${to}`)
|
|
425
|
+
setCustom(value?.custom ?? null)
|
|
426
|
+
} catch { setCustom(null) }
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (!form) {
|
|
430
|
+
return h('div', { style: styles.section },
|
|
431
|
+
h(CrashBanner),
|
|
432
|
+
h('div', { style: styles.muted }, failed || tr('loading')))
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const usage = status?.usage ?? {}
|
|
436
|
+
const card = (label, range) => h('div', { style: styles.card },
|
|
437
|
+
h('div', { style: styles.label }, label),
|
|
438
|
+
h('div', { style: styles.cardValue }, money(range?.costCNY)),
|
|
439
|
+
h('div', { style: styles.muted }, tr('tokensLabel', { tokens: integer(range?.tokens) }), ' · ', tr('calls', { count: range?.calls ?? 0 })))
|
|
440
|
+
|
|
441
|
+
return h('div', { style: { display: 'flex', flexDirection: 'column', gap: 12, fontSize: 13 } },
|
|
442
|
+
h(CrashBanner),
|
|
443
|
+
h('div', { style: styles.muted }, tr('settingsIntro')),
|
|
444
|
+
h('div', { style: styles.section },
|
|
445
|
+
h(Check, { label: tr('enabled'), checked: form.enabled, onChange: value => updateTop('enabled', value) }),
|
|
446
|
+
h('label', { style: styles.field },
|
|
447
|
+
h('span', { style: styles.label }, tr('engine')),
|
|
448
|
+
h('select', { style: styles.input, value: form.engine, onChange: event => updateTop('engine', event.target.value) },
|
|
449
|
+
h('option', { value: 'rules' }, tr('engineRules')),
|
|
450
|
+
h('option', { value: 'api' }, tr('engineApi'))))),
|
|
451
|
+
h('div', { style: styles.section },
|
|
452
|
+
h('div', { style: styles.sectionTitle }, tr('triggers')),
|
|
453
|
+
h(Check, { label: tr('onIdle'), checked: form.triggers.onIdle, onChange: value => update('triggers', 'onIdle', value) }),
|
|
454
|
+
h(Check, { label: tr('onSwitch'), checked: form.triggers.onSwitch, onChange: value => update('triggers', 'onSwitch', value) }),
|
|
455
|
+
h(Check, { label: tr('closeBatch'), checked: form.triggers.closeBatch, onChange: value => update('triggers', 'closeBatch', value) }),
|
|
456
|
+
h('div', { style: styles.warn }, tr('closeBatchWarn')),
|
|
457
|
+
h(NumberField, {
|
|
458
|
+
label: tr('closeBatchMax'), value: form.triggers.closeBatchMaxSessions, step: 1,
|
|
459
|
+
onChange: value => update('triggers', 'closeBatchMaxSessions', value),
|
|
460
|
+
})),
|
|
461
|
+
h('div', { style: styles.section },
|
|
462
|
+
h('div', { style: styles.sectionTitle }, tr('apiSection')),
|
|
463
|
+
h('div', { style: styles.muted }, tr('apiNote')),
|
|
464
|
+
h('div', { style: styles.row },
|
|
465
|
+
h('label', { style: styles.field, key: 'provider' },
|
|
466
|
+
h('span', { style: styles.label }, tr('provider')),
|
|
467
|
+
h('input', { style: styles.input, value: form.api.provider, placeholder: 'deepseek', onChange: event => update('api', 'provider', event.target.value) })),
|
|
468
|
+
h('label', { style: styles.field, key: 'model' },
|
|
469
|
+
h('span', { style: styles.label }, tr('model')),
|
|
470
|
+
h('input', { style: styles.input, value: form.api.model, onChange: event => update('api', 'model', event.target.value) }))),
|
|
471
|
+
h('div', { style: styles.row },
|
|
472
|
+
h(NumberField, { label: tr('priceIn'), value: form.api.priceInCNYPerM, step: 0.1, onChange: value => update('api', 'priceInCNYPerM', value) }),
|
|
473
|
+
h(NumberField, { label: tr('priceOut'), value: form.api.priceOutCNYPerM, step: 0.1, onChange: value => update('api', 'priceOutCNYPerM', value) })),
|
|
474
|
+
h('div', { style: styles.row },
|
|
475
|
+
h(NumberField, { label: tr('perSummaryCap'), value: form.api.maxCostPerSummaryCNY, step: 0.01, onChange: value => update('api', 'maxCostPerSummaryCNY', value) }),
|
|
476
|
+
h(NumberField, { label: tr('dailyBudget'), value: form.api.dailyTokenBudget, step: 1000, onChange: value => update('api', 'dailyTokenBudget', value) }),
|
|
477
|
+
h('label', { style: styles.field, key: 'overCap' },
|
|
478
|
+
h('span', { style: styles.label }, tr('overCap')),
|
|
479
|
+
h('select', { style: styles.input, value: form.api.overCap, onChange: event => update('api', 'overCap', event.target.value) },
|
|
480
|
+
h('option', { value: 'queue' }, tr('overCapQueue')),
|
|
481
|
+
h('option', { value: 'skip' }, tr('overCapSkip')))))),
|
|
482
|
+
h('div', { style: styles.section },
|
|
483
|
+
h('div', { style: styles.sectionTitle }, tr('filter')),
|
|
484
|
+
h('div', { style: styles.row },
|
|
485
|
+
h(NumberField, { label: tr('minMessages'), value: form.filter.minMessages, onChange: value => update('filter', 'minMessages', value) }),
|
|
486
|
+
h(NumberField, { label: tr('minTokens'), value: form.filter.minTokens, step: 500, onChange: value => update('filter', 'minTokens', value) }))),
|
|
487
|
+
form.engine === 'api' && status && status.apiAvailable === false
|
|
488
|
+
? h('div', { style: styles.warn }, tr('apiUnavailable'))
|
|
489
|
+
: null,
|
|
490
|
+
h('div', { style: styles.section },
|
|
491
|
+
h('div', { style: styles.sectionTitle }, tr('queueTitle'), ' · ',
|
|
492
|
+
h('span', { style: styles.muted }, String(status?.queueCount ?? status?.queue ?? 0))),
|
|
493
|
+
(status?.queueItems ?? []).length === 0
|
|
494
|
+
? h('div', { style: styles.muted }, tr('queueEmpty'))
|
|
495
|
+
: h('div', { style: { display: 'flex', flexDirection: 'column', gap: 4 } },
|
|
496
|
+
status.queueItems.map(item => h('div', { key: item.sessionId, style: styles.muted },
|
|
497
|
+
item.sessionId, ' · ', item.reason, ' · ', new Date(item.at).toLocaleString()))),
|
|
498
|
+
h('div', { style: styles.actions },
|
|
499
|
+
h('button', {
|
|
500
|
+
style: styles.button,
|
|
501
|
+
disabled: (status?.queueCount ?? 0) === 0,
|
|
502
|
+
onClick: async () => {
|
|
503
|
+
setFailed('')
|
|
504
|
+
try {
|
|
505
|
+
await callHost('queue/run', { method: 'POST', body: {} })
|
|
506
|
+
setStatus(await callHost('status'))
|
|
507
|
+
setMessage(tr('queueRun'))
|
|
508
|
+
} catch (cause) {
|
|
509
|
+
setFailed(String(cause?.message ?? cause))
|
|
510
|
+
}
|
|
511
|
+
},
|
|
512
|
+
}, tr('queueProcess')))),
|
|
513
|
+
h('div', { style: styles.section },
|
|
514
|
+
h('div', { style: styles.sectionTitle }, tr('usage'), ' · ', h('span', { style: styles.ok }, tr('localZero'))),
|
|
515
|
+
h('div', { style: styles.cards },
|
|
516
|
+
card(tr('usageHour'), usage.hour),
|
|
517
|
+
card(tr('usageDay'), usage.day),
|
|
518
|
+
card(tr('usageWeek'), usage.week),
|
|
519
|
+
card(tr('usageMonth'), usage.month),
|
|
520
|
+
custom ? card(tr('usageCustom'), custom) : null),
|
|
521
|
+
h('div', { style: styles.row },
|
|
522
|
+
h('label', { style: styles.field },
|
|
523
|
+
h('span', { style: styles.label }, tr('from')),
|
|
524
|
+
h('input', { style: styles.input, type: 'datetime-local', onChange: event => {
|
|
525
|
+
const from = Date.parse(event.target.value)
|
|
526
|
+
const to = Date.now()
|
|
527
|
+
setCustom(null)
|
|
528
|
+
void loadCustom(from, to)
|
|
529
|
+
} }))),
|
|
530
|
+
status ? h('div', { style: styles.muted },
|
|
531
|
+
tr('dataRoot'), ': ', status.dataRoot, ' · ', tr('queue'), ': ', status.queue ?? 0) : null),
|
|
532
|
+
failed ? h('div', { style: styles.warn }, failed) : null,
|
|
533
|
+
message ? h('div', { style: styles.ok }, message) : null,
|
|
534
|
+
h('div', { style: styles.actions },
|
|
535
|
+
h('button', { style: styles.button, onClick: () => void save() }, tr('save'))))
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return {
|
|
539
|
+
inject: ['slots', 'locale', 'sessions'],
|
|
540
|
+
apply(ctx) {
|
|
541
|
+
ctx.slots.inject(COMPOSER_BAR, () => ctx.slots.register({
|
|
542
|
+
name: COMPOSER_BAR,
|
|
543
|
+
id: 'colophon',
|
|
544
|
+
order: 15,
|
|
545
|
+
locale: NS,
|
|
546
|
+
inject: sessionId => ({ sessionId }),
|
|
547
|
+
}, ColophonDock))
|
|
548
|
+
ctx.slots.inject(SETTINGS_SECTION, () => ctx.slots.register({
|
|
549
|
+
name: SETTINGS_SECTION,
|
|
550
|
+
id: 'colophon',
|
|
551
|
+
order: 30,
|
|
552
|
+
label: () => tr('settingsLabel'),
|
|
553
|
+
}, ColophonSettings))
|
|
554
|
+
},
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
})
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64">
|
|
2
|
+
<!-- Colophon mark: a closed manuscript with a curled lower corner and ink drop. -->
|
|
3
|
+
<rect x="8" y="8" width="48" height="48" rx="12" fill="#1f2128"/>
|
|
4
|
+
<path d="M20 18h19a7 7 0 0 1 7 7v21H27a7 7 0 0 1-7-7V18z" fill="none" stroke="#e8d6a8" stroke-width="2.5" stroke-linejoin="round"/>
|
|
5
|
+
<path d="M46 46c4 0 6-2 6-6" fill="none" stroke="#e8d6a8" stroke-width="2.5" stroke-linecap="round"/>
|
|
6
|
+
<path d="M26 27h13M26 33h10" stroke="#e8d6a8" stroke-width="2" stroke-linecap="round" opacity="0.75"/>
|
|
7
|
+
<circle cx="40.5" cy="40.5" r="3" fill="#e8d6a8"/>
|
|
8
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colophon(卷末)— a Host plugin that keeps a local, token-free conclusion
|
|
3
|
+
* digest for every conversation, so reopening an old session can start from
|
|
4
|
+
* its final state instead of replaying its full history.
|
|
5
|
+
*
|
|
6
|
+
* The Client half (composer card, settings page, recovery banner) is P2; this
|
|
7
|
+
* entry only mounts the host runtime and its read-only `/api/colophon/*`
|
|
8
|
+
* surface.
|
|
9
|
+
* @module dsh-colophon
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import Schema from '@deepseek-ai/schemastery'
|
|
13
|
+
import { install } from './lib/app.js'
|
|
14
|
+
|
|
15
|
+
/** Cordis plugin name. */
|
|
16
|
+
export const name = 'colophon'
|
|
17
|
+
|
|
18
|
+
/** Required Host services. */
|
|
19
|
+
export const inject = ['sessions', 'sessionPersistence', 'connection', 'llm']
|
|
20
|
+
|
|
21
|
+
/** Plugin configuration. */
|
|
22
|
+
export const Config = Schema.object({
|
|
23
|
+
/** Master switch. */
|
|
24
|
+
enabled: Schema.boolean().default(true),
|
|
25
|
+
/** Summary engine: `rules` is local and free; `api` spends model tokens. */
|
|
26
|
+
engine: Schema.union([Schema.const('rules'), Schema.const('api')]).default('rules'),
|
|
27
|
+
api: Schema.object({
|
|
28
|
+
provider: Schema.string().default('deepseek-official'),
|
|
29
|
+
model: Schema.string().default('deepseek-chat'),
|
|
30
|
+
maxInputTokens: Schema.number().default(32_000),
|
|
31
|
+
maxOutputTokens: Schema.number().default(1_024),
|
|
32
|
+
temperature: Schema.number().default(0.2),
|
|
33
|
+
priceInCNYPerM: Schema.number().default(2),
|
|
34
|
+
priceOutCNYPerM: Schema.number().default(8),
|
|
35
|
+
maxCostPerSummaryCNY: Schema.number().default(0.05),
|
|
36
|
+
dailyTokenBudget: Schema.number().default(200_000),
|
|
37
|
+
overCap: Schema.union([Schema.const('queue'), Schema.const('skip')]).default('queue'),
|
|
38
|
+
}),
|
|
39
|
+
triggers: Schema.object({
|
|
40
|
+
onIdle: Schema.boolean().default(true),
|
|
41
|
+
onSwitch: Schema.boolean().default(true),
|
|
42
|
+
closeBatch: Schema.boolean().default(false),
|
|
43
|
+
closeBatchMaxSessions: Schema.number().default(10),
|
|
44
|
+
}),
|
|
45
|
+
filter: Schema.object({
|
|
46
|
+
minMessages: Schema.number().default(20),
|
|
47
|
+
minTokens: Schema.number().default(4_000),
|
|
48
|
+
maxAgeDays: Schema.number().default(30),
|
|
49
|
+
sessionTypes: Schema.array(Schema.string()).default(['root']),
|
|
50
|
+
}),
|
|
51
|
+
retention: Schema.object({
|
|
52
|
+
digests: Schema.number().default(500),
|
|
53
|
+
usageDays: Schema.number().default(180),
|
|
54
|
+
}),
|
|
55
|
+
debug: Schema.boolean().default(false),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Mount the Colophon runtime.
|
|
60
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx Host plugin context.
|
|
61
|
+
* @param {any} config validated Config value.
|
|
62
|
+
*/
|
|
63
|
+
export function apply(ctx, config) {
|
|
64
|
+
const runtime = install(ctx, config)
|
|
65
|
+
ctx.effect(() => () => { void runtime.dispose() }, 'colophon: runtime')
|
|
66
|
+
}
|