dsh-zcode-memory 0.0.0-stage → 1.1.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/LICENSE +21 -0
- package/README.md +251 -2
- package/client.js +573 -0
- package/cordis.patch.yml +6 -0
- package/index.js +7143 -0
- package/package.json +49 -4
- package/tests/abort-loop.mjs +70 -0
- package/tests/ancestor-lookup.mjs +169 -0
- package/tests/append-race.mjs +90 -0
- package/tests/backlog-state.mjs +274 -0
- package/tests/client-bundle.mjs +256 -0
- package/tests/context-memo.mjs +85 -0
- package/tests/extract-cost.mjs +312 -0
- package/tests/extract-retry.mjs +88 -0
- package/tests/extract-system.mjs +138 -0
- package/tests/extract-window.mjs +236 -0
- package/tests/extract.mjs +126 -0
- package/tests/func.mjs +217 -0
- package/tests/index-budget.mjs +182 -0
- package/tests/index-comments.mjs +92 -0
- package/tests/index-truncate.mjs +237 -0
- package/tests/memory-notes.mjs +90 -0
- package/tests/native-tools-edge.mjs +297 -0
- package/tests/native-tools.mjs +182 -0
- package/tests/prompt-split.mjs +253 -0
- package/tests/recall-score.mjs +225 -0
- package/tests/resume-recovery.mjs +245 -0
- package/tests/sandbox-grants.mjs +494 -0
- package/tests/save-append-edges.mjs +190 -0
- package/tests/sensitive-path.mjs +227 -0
- package/tests/settings-api.mjs +399 -0
- package/tests/settings-disable.mjs +280 -0
- package/tests/smoke.mjs +111 -0
package/client.js
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-zcode-memory —— Client 半边(设置页)。
|
|
3
|
+
*
|
|
4
|
+
* 只做两件事:
|
|
5
|
+
* 1. 一个总开关:决定记忆功能是否注入上下文 / 自动抽取 / 写入 / 工具可用;
|
|
6
|
+
* 2. 一份只读清单:展示 memoriesRoot 下所有工作区与全局的记忆(分页),点开看正文。
|
|
7
|
+
*
|
|
8
|
+
* 状态存在宿主侧(<memoriesRoot>/_settings.json),通过本插件的
|
|
9
|
+
* `/dsh-zcode-memory-api` 同源路由读写 —— 与 dsh-advisors / dsh-memory-switch 同款做法:
|
|
10
|
+
* 不走 Session 事件,因为这是界面偏好而不是对话内容。
|
|
11
|
+
*
|
|
12
|
+
* 注意:本文件是手写的 `__ModuleLoader__` bundle,**不能** require 宿主的
|
|
13
|
+
* `@deepseek-ai/dsh-client-ui-primitives`(那些包会变,抛错会让整个 slot entry 崩掉)。
|
|
14
|
+
* 控件全部自己写,样式只用 `--dsw-alias-*` 主题 token,类名一律 `dsh-zmem-` 前缀。
|
|
15
|
+
*/
|
|
16
|
+
window.__ModuleLoader__.load({
|
|
17
|
+
id: 'dsh-zcode-memory',
|
|
18
|
+
factory(require) {
|
|
19
|
+
const React = require('react')
|
|
20
|
+
const h = React.createElement
|
|
21
|
+
|
|
22
|
+
/** 从加载本 bundle 的 <script src> 反推子路径部署前缀(同 dsh-advisors)。 */
|
|
23
|
+
const detectAppBase = (doc) => {
|
|
24
|
+
if (!doc) return ''
|
|
25
|
+
const fromPath = (p) => {
|
|
26
|
+
const m = /^(.*?)\/plugins\//.exec(String(p || ''))
|
|
27
|
+
return m ? String(m[1]).replace(/\/+$/, '') : null
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const src = doc.currentScript && doc.currentScript.src
|
|
31
|
+
if (src) {
|
|
32
|
+
const base = fromPath(new URL(src, doc.baseURI || location.href).pathname)
|
|
33
|
+
if (base !== null) return base
|
|
34
|
+
}
|
|
35
|
+
} catch (e) { /* 非浏览器 */ }
|
|
36
|
+
try {
|
|
37
|
+
for (const el of doc.querySelectorAll('script[src]')) {
|
|
38
|
+
const base = fromPath(new URL(el.src, doc.baseURI || location.href).pathname)
|
|
39
|
+
if (base !== null) return base
|
|
40
|
+
}
|
|
41
|
+
} catch (e) { /* 极简 DOM */ }
|
|
42
|
+
return ''
|
|
43
|
+
}
|
|
44
|
+
const APP_BASE = detectAppBase(typeof document === 'undefined' ? null : document)
|
|
45
|
+
const absAppPath = (u) => {
|
|
46
|
+
const s = String(u == null ? '' : u)
|
|
47
|
+
if (!s || s[0] !== '/' || s[1] === '/') return s
|
|
48
|
+
if (!APP_BASE || s === APP_BASE || s.indexOf(APP_BASE + '/') === 0) return s
|
|
49
|
+
return APP_BASE + s
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const NS = 'dsh-zcode-memory'
|
|
53
|
+
const API = '/dsh-zcode-memory-api'
|
|
54
|
+
const SECTION_ID = 'dsh-zcode-memory'
|
|
55
|
+
const PAGE_SIZE = 200
|
|
56
|
+
|
|
57
|
+
const zh = {
|
|
58
|
+
'section.label': 'ZCode 记忆',
|
|
59
|
+
'page.title': 'ZCode 记忆',
|
|
60
|
+
'page.intro': '这里展示 ZCode / WorkBuddy 的文件式长期记忆:索引常驻上下文,正文按需读取,回合结束后自动抽取。下面的开关控制整套记忆功能。',
|
|
61
|
+
'page.loading': '读取记忆…',
|
|
62
|
+
'page.retry': '重试',
|
|
63
|
+
'page.failed': '读取失败:{message}',
|
|
64
|
+
'page.saveFailed': '保存失败,请重试:{message}',
|
|
65
|
+
|
|
66
|
+
'toggle.label': '启用记忆功能',
|
|
67
|
+
'toggle.hint': '开启时:记忆索引注入上下文、回合结束自动抽取、记忆工具可用。关闭后不再注入、不再抽取,记忆工具与直写一律拒绝;记忆文件原样保留,随时可以重新打开。关闭期间产生的对话不会被补抽。',
|
|
68
|
+
'toggle.configDisabled': 'profile 配置里写的是 enabled: false,设置页无法开启记忆功能。要恢复请改 DSH profile 配置。',
|
|
69
|
+
'toggle.pending': '保存中…',
|
|
70
|
+
'toggle.saved': '已保存',
|
|
71
|
+
'toggle.stateOn': '当前:已开启',
|
|
72
|
+
'toggle.stateOff': '当前:已关闭',
|
|
73
|
+
'toggle.preferenceOnBlocked': '偏好:开;但 profile 配置把记忆功能硬关闭了,改回 profile 后生效。',
|
|
74
|
+
|
|
75
|
+
'list.title': '记忆清单',
|
|
76
|
+
'list.hint': '这里列出所有工作区和全局的记忆,按更新时间倒序;模型注入只覆盖当前工作区(以及按配置开启的全局),所以页面条数可能多于模型可见条数。默认显示最新 200 条,可「加载更多」;点一行展开正文。',
|
|
77
|
+
'list.refresh': '刷新',
|
|
78
|
+
'list.search': '搜索已加载条目的标题、摘要、正文…',
|
|
79
|
+
'list.count': '{total} 条 · 项目 {project} / 全局 {global}',
|
|
80
|
+
'list.empty': '还没有任何记忆。开启功能后,对话里的长期事实会自动抽取到这里。',
|
|
81
|
+
'list.noMatch': '没有匹配的记忆。',
|
|
82
|
+
'list.truncated': '正文已截断(全文 {chars} 字符)。',
|
|
83
|
+
'list.root': '记忆目录:{path}',
|
|
84
|
+
'list.loadMore': '加载更多',
|
|
85
|
+
'list.loadingMore': '加载中…',
|
|
86
|
+
'list.pageNote': '已显示最新 {shown} / 共 {total} 条',
|
|
87
|
+
'list.searchNote': '搜索只覆盖已加载的 {shown} 条;更多条目请先“加载更多”。',
|
|
88
|
+
'list.status': '状态文件异常({code}),当前按 profile 配置运行。',
|
|
89
|
+
'state.symlink': '符号链接',
|
|
90
|
+
'state.not-file': '不是普通文件',
|
|
91
|
+
'state.too-large': '超过 64KB',
|
|
92
|
+
'state.invalid': 'JSON 无效',
|
|
93
|
+
'state.read': '不可读',
|
|
94
|
+
|
|
95
|
+
'err.forbidden': '只接受本机同源请求',
|
|
96
|
+
'err.unsupported_media_type': '请求类型不受支持',
|
|
97
|
+
'err.bad_request': '请求参数无效',
|
|
98
|
+
'err.too_large': '请求体过大',
|
|
99
|
+
'err.method_not_allowed': '请求方法不受支持',
|
|
100
|
+
'err.read_failed': '读取记忆失败',
|
|
101
|
+
'err.write_failed': '保存开关失败',
|
|
102
|
+
'err.network': '无法连接本地接口',
|
|
103
|
+
|
|
104
|
+
'scope.global': '全局',
|
|
105
|
+
'scope.project': '项目',
|
|
106
|
+
'type.user': '用户',
|
|
107
|
+
'type.feedback': '反馈',
|
|
108
|
+
'type.project': '项目',
|
|
109
|
+
'type.reference': '参考',
|
|
110
|
+
}
|
|
111
|
+
const en = {
|
|
112
|
+
'section.label': 'ZCode Memory',
|
|
113
|
+
'page.title': 'ZCode Memory',
|
|
114
|
+
'page.intro': 'File-based long-term memory shared with ZCode / WorkBuddy: the index stays in context, bodies are read on demand, and facts are extracted after a turn. The switch below controls the whole feature.',
|
|
115
|
+
'page.loading': 'Reading memories…',
|
|
116
|
+
'page.retry': 'Retry',
|
|
117
|
+
'page.failed': 'Could not load: {message}',
|
|
118
|
+
'page.saveFailed': 'Could not save. Try again: {message}',
|
|
119
|
+
|
|
120
|
+
'toggle.label': 'Enable memory',
|
|
121
|
+
'toggle.hint': 'When on: the memory index is injected, facts are auto-extracted after turns, and memory tools are available. When off, nothing is injected or extracted, and memory tools plus direct writes are refused; memory files stay intact and can be re-enabled at any time. Conversations from the off period are not extracted later.',
|
|
122
|
+
'toggle.configDisabled': 'The profile config sets enabled: false, so this page cannot turn memory on. Edit the DSH profile config to restore it.',
|
|
123
|
+
'toggle.pending': 'Saving…',
|
|
124
|
+
'toggle.saved': 'Saved',
|
|
125
|
+
'toggle.stateOn': 'Now: on',
|
|
126
|
+
'toggle.stateOff': 'Now: off',
|
|
127
|
+
'toggle.preferenceOnBlocked': 'Preference: on; the profile config hard-disables memory. It takes effect once the profile is restored.',
|
|
128
|
+
|
|
129
|
+
'list.title': 'Memories',
|
|
130
|
+
'list.hint': 'Lists workspace and global memories across all workspaces, newest first. Model injection only covers the current workspace (and global when enabled), so this page can show more than the model sees. The newest 200 load first; use “Load more” for older ones. Click a row to read the body.',
|
|
131
|
+
'list.refresh': 'Refresh',
|
|
132
|
+
'list.search': 'Search loaded titles, summaries, bodies…',
|
|
133
|
+
'list.count': '{total} memories · {project} project / {global} global',
|
|
134
|
+
'list.empty': 'No memories yet. Once the feature is on, durable facts from conversations are extracted here.',
|
|
135
|
+
'list.noMatch': 'No matching memories.',
|
|
136
|
+
'list.truncated': 'Body truncated ({chars} characters in full).',
|
|
137
|
+
'list.root': 'Memory root: {path}',
|
|
138
|
+
'list.loadMore': 'Load more',
|
|
139
|
+
'list.loadingMore': 'Loading…',
|
|
140
|
+
'list.pageNote': 'Showing the newest {shown} of {total}',
|
|
141
|
+
'list.searchNote': 'Search covers only the loaded {shown} memories; use “Load more” for older ones.',
|
|
142
|
+
'list.status': 'Settings state file is unreadable ({code}); running from the profile config.',
|
|
143
|
+
'state.symlink': 'symlink',
|
|
144
|
+
'state.not-file': 'not a regular file',
|
|
145
|
+
'state.too-large': 'larger than 64KB',
|
|
146
|
+
'state.invalid': 'invalid JSON',
|
|
147
|
+
'state.read': 'unreadable',
|
|
148
|
+
|
|
149
|
+
'err.forbidden': 'Only local same-origin requests are accepted',
|
|
150
|
+
'err.unsupported_media_type': 'Unsupported request content type',
|
|
151
|
+
'err.bad_request': 'Invalid request',
|
|
152
|
+
'err.too_large': 'Request body too large',
|
|
153
|
+
'err.method_not_allowed': 'Method not allowed',
|
|
154
|
+
'err.read_failed': 'Could not read memories',
|
|
155
|
+
'err.write_failed': 'Could not save the switch',
|
|
156
|
+
'err.network': 'Cannot reach the local API',
|
|
157
|
+
|
|
158
|
+
'scope.global': 'Global',
|
|
159
|
+
'scope.project': 'Project',
|
|
160
|
+
'type.user': 'User',
|
|
161
|
+
'type.feedback': 'Feedback',
|
|
162
|
+
'type.project': 'Project',
|
|
163
|
+
'type.reference': 'Reference',
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const format = (template, values) => String(template).replace(/\{(\w+)\}/g,
|
|
167
|
+
(whole, key) => (values && Object.prototype.hasOwnProperty.call(values, key) ? String(values[key]) : whole))
|
|
168
|
+
|
|
169
|
+
const STATE_CODES = new Set(['symlink', 'not-file', 'too-large', 'invalid', 'read'])
|
|
170
|
+
const stateText = (code, t) => (STATE_CODES.has(code) ? t('state.' + code) : String(code))
|
|
171
|
+
|
|
172
|
+
const ERROR_CODES = new Set([
|
|
173
|
+
'forbidden', 'unsupported_media_type', 'bad_request', 'too_large',
|
|
174
|
+
'method_not_allowed', 'read_failed', 'write_failed',
|
|
175
|
+
])
|
|
176
|
+
|
|
177
|
+
const errorText = (error, t) => {
|
|
178
|
+
const code = error && error.code
|
|
179
|
+
if (code && ERROR_CODES.has(code)) return t('err.' + code)
|
|
180
|
+
if (error && error.name === 'TypeError') return t('err.network')
|
|
181
|
+
return error && error.message ? error.message : String(error)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const CSS = `
|
|
185
|
+
.dsh-zmem-page{display:flex;flex-direction:column;gap:20px;padding:4px 0 8px;color:var(--dsw-alias-label-primary);font-size:13px;line-height:20px}
|
|
186
|
+
.dsh-zmem-page h2{margin:0;font-size:16px;line-height:24px;font-weight:600}
|
|
187
|
+
.dsh-zmem-intro{margin:0;color:var(--dsw-alias-label-secondary);font-size:13px;line-height:20px;max-width:64ch}
|
|
188
|
+
.dsh-zmem-card{display:flex;flex-direction:column;gap:12px;padding:14px 16px;border:1px solid var(--dsw-alias-settings-card-stroke,var(--dsw-alias-border-l2));border-radius:10px;background:var(--dsw-alias-settings-card-fill,var(--dsw-alias-bg-l1))}
|
|
189
|
+
.dsh-zmem-cardHead{display:flex;align-items:flex-start;justify-content:space-between;gap:16px}
|
|
190
|
+
.dsh-zmem-cardTitle{font-weight:600}
|
|
191
|
+
.dsh-zmem-cardHint{margin:0;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px;max-width:64ch}
|
|
192
|
+
.dsh-zmem-switch{position:relative;flex:0 0 auto;box-sizing:border-box;width:36px;height:20px;padding:2px;border:0;border-radius:999px;corner-shape:round;background:var(--dsw-alias-border-l3);cursor:pointer}
|
|
193
|
+
.dsh-zmem-switch[aria-checked=true]{background:var(--dsw-alias-brand-primary)}
|
|
194
|
+
.dsh-zmem-switch:disabled{cursor:default;opacity:.5}
|
|
195
|
+
.dsh-zmem-switch:focus-visible{outline:var(--dsw-focus-ring-width) solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:2px}
|
|
196
|
+
.dsh-zmem-switch[aria-checked=false] .dsh-zmem-thumb{background:var(--dsw-alias-switch-thumb)}
|
|
197
|
+
.dsh-zmem-switch[aria-checked=true] .dsh-zmem-thumb{transform:translate(16px)}
|
|
198
|
+
.dsh-zmem-thumb{display:block;width:16px;height:16px;border-radius:50%;corner-shape:round;background:var(--dsw-alias-label-primary-foreground);transition:transform .12s ease}
|
|
199
|
+
|
|
200
|
+
.dsh-zmem-toolbar{display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap}
|
|
201
|
+
.dsh-zmem-count{color:var(--dsw-alias-label-secondary);font-size:12px;font-variant-numeric:tabular-nums}
|
|
202
|
+
.dsh-zmem-refresh{padding:3px 10px;border:1px solid var(--dsw-alias-border-l2);border-radius:6px;background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;cursor:pointer}
|
|
203
|
+
.dsh-zmem-refresh:hover:not(:disabled){border-color:var(--dsw-alias-border-l3)}
|
|
204
|
+
.dsh-zmem-refresh:disabled{opacity:.6;cursor:default}
|
|
205
|
+
.dsh-zmem-refresh:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:1px}
|
|
206
|
+
.dsh-zmem-search{box-sizing:border-box;width:100%;padding:7px 10px;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;background:var(--dsw-alias-bg-l1);color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;outline:none}
|
|
207
|
+
.dsh-zmem-search::placeholder{color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary))}
|
|
208
|
+
.dsh-zmem-search:focus-visible{border-color:var(--dsw-alias-brand-primary)}
|
|
209
|
+
|
|
210
|
+
.dsh-zmem-list{display:flex;flex-direction:column;gap:2px;max-height:min(52vh,520px);overflow-y:auto;padding:2px;border-top:1px solid var(--dsw-alias-separator-primary,var(--dsw-alias-border-l1))}
|
|
211
|
+
.dsh-zmem-item{display:flex;flex-direction:column;border:1px solid transparent;border-radius:8px}
|
|
212
|
+
.dsh-zmem-item:has(.dsh-zmem-itemHead:hover){background:var(--dsw-alias-interactive-bg-hover,rgba(127,127,127,.10))}
|
|
213
|
+
.dsh-zmem-item:has(.dsh-zmem-itemHead:focus-visible){outline:2px solid var(--dsw-alias-brand-primary);outline-offset:-1px}
|
|
214
|
+
.dsh-zmem-itemHead{display:flex;flex-direction:column;gap:4px;padding:9px 10px;border-radius:8px;cursor:pointer}
|
|
215
|
+
.dsh-zmem-itemHead:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:-1px}
|
|
216
|
+
.dsh-zmem-itemTop{display:flex;align-items:baseline;gap:8px;min-width:0}
|
|
217
|
+
.dsh-zmem-itemTitle{font-weight:600;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
218
|
+
.dsh-zmem-badge{flex:none;padding:0 6px;border-radius:999px;border:1px solid var(--dsw-alias-border-l2);color:var(--dsw-alias-label-secondary);font-size:10px;line-height:16px;font-weight:400}
|
|
219
|
+
.dsh-zmem-badgeGlobal{color:var(--dsw-alias-state-business-primary,var(--dsw-alias-label-primary))}
|
|
220
|
+
.dsh-zmem-date{flex:none;margin-left:auto;color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary));font-size:11px;font-variant-numeric:tabular-nums}
|
|
221
|
+
.dsh-zmem-desc{color:var(--dsw-alias-label-secondary);font-size:12px;line-height:18px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
|
|
222
|
+
.dsh-zmem-rel{color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary));font-size:11px;line-height:16px}
|
|
223
|
+
.dsh-zmem-bodyWrap{display:flex;flex-direction:column;gap:4px;padding:0 10px 10px}
|
|
224
|
+
.dsh-zmem-body{margin:0;padding:10px 12px;border-radius:8px;background:var(--dsw-alias-bg-l2,var(--dsw-alias-bg-l1));color:var(--dsw-alias-label-primary);font:12px/1.6 "Cascadia Code",Consolas,monospace;white-space:pre-wrap;word-break:break-word;max-height:280px;overflow:auto}
|
|
225
|
+
.dsh-zmem-body:focus-visible{outline:2px solid var(--dsw-alias-brand-primary);outline-offset:1px}
|
|
226
|
+
.dsh-zmem-trunc{margin:0;color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary));font-size:11px}
|
|
227
|
+
.dsh-zmem-empty{padding:18px 10px;color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary));font-size:12px}
|
|
228
|
+
.dsh-zmem-root{margin:0;color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary));font-size:11px;overflow-wrap:anywhere}
|
|
229
|
+
.dsh-zmem-more{display:flex;align-items:center;justify-content:center;gap:8px;padding:10px 0 2px}
|
|
230
|
+
.dsh-zmem-moreBtn{padding:4px 14px;border:1px solid var(--dsw-alias-border-l2);border-radius:8px;background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;cursor:pointer}
|
|
231
|
+
.dsh-zmem-moreBtn:hover:not(:disabled){border-color:var(--dsw-alias-border-l3)}
|
|
232
|
+
.dsh-zmem-moreBtn:disabled{opacity:.6;cursor:default}
|
|
233
|
+
.dsh-zmem-note{color:var(--dsw-alias-label-tertiary,var(--dsw-alias-label-secondary));font-size:11px}
|
|
234
|
+
|
|
235
|
+
.dsh-zmem-status{font-size:11px;color:var(--dsw-alias-label-secondary);min-height:16px}
|
|
236
|
+
.dsh-zmem-error{color:var(--dsw-alias-state-error-primary)}
|
|
237
|
+
.dsh-zmem-banner{display:flex;align-items:center;gap:10px;flex-wrap:wrap;padding:10px 12px;border-radius:8px;background:var(--dsw-alias-bg-l2);color:var(--dsw-alias-label-secondary);font-size:12px}
|
|
238
|
+
.dsh-zmem-retry{padding:2px 10px;border:1px solid var(--dsw-alias-border-l2);border-radius:6px;background:transparent;color:var(--dsw-alias-label-primary);font:inherit;font-size:12px;cursor:pointer}
|
|
239
|
+
`
|
|
240
|
+
|
|
241
|
+
function installStyle() {
|
|
242
|
+
const el = document.createElement('style')
|
|
243
|
+
el.setAttribute('data-dsh-zcode-memory', '')
|
|
244
|
+
el.textContent = CSS
|
|
245
|
+
document.head.appendChild(el)
|
|
246
|
+
return el
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function fetchState(offset, limit) {
|
|
250
|
+
const res = await fetch(`${absAppPath(API)}?offset=${offset}&limit=${limit}`, {
|
|
251
|
+
headers: { Accept: 'application/json' },
|
|
252
|
+
})
|
|
253
|
+
const body = await res.json().catch(() => null)
|
|
254
|
+
if (!res.ok || !body || body.ok !== true) {
|
|
255
|
+
const error = new Error((body && body.error) || `HTTP ${res.status}`)
|
|
256
|
+
error.code = body && body.code
|
|
257
|
+
throw error
|
|
258
|
+
}
|
|
259
|
+
return body
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function postState(patch) {
|
|
263
|
+
const res = await fetch(absAppPath(API), {
|
|
264
|
+
method: 'POST',
|
|
265
|
+
headers: { 'Content-Type': 'application/json' },
|
|
266
|
+
body: JSON.stringify(patch),
|
|
267
|
+
})
|
|
268
|
+
const body = await res.json().catch(() => null)
|
|
269
|
+
if (!res.ok || !body || body.ok !== true) {
|
|
270
|
+
const error = new Error((body && body.error) || `HTTP ${res.status}`)
|
|
271
|
+
error.code = body && body.code
|
|
272
|
+
throw error
|
|
273
|
+
}
|
|
274
|
+
return body
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const itemKey = (it) => `${it.scope}\u0000${it.workspaceId}\u0000${it.rel || it.slug}`
|
|
278
|
+
const hashKey = (s) => {
|
|
279
|
+
let n = 5381
|
|
280
|
+
for (let i = 0; i < s.length; i += 1) n = ((n << 5) + n + s.charCodeAt(i)) >>> 0
|
|
281
|
+
return n.toString(16)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function MemoryItem(props) {
|
|
285
|
+
const { it, open, onToggle, t } = props
|
|
286
|
+
const key = itemKey(it)
|
|
287
|
+
const bodyId = `dsh-zmem-body-${hashKey(key)}`
|
|
288
|
+
const scopeLabel = it.scope === 'global'
|
|
289
|
+
? t('scope.global')
|
|
290
|
+
: (it.workspace || t('scope.project'))
|
|
291
|
+
const typeLabel = it.type
|
|
292
|
+
? (zh['type.' + it.type] !== undefined ? t('type.' + it.type) : String(it.type))
|
|
293
|
+
: ''
|
|
294
|
+
const nested = String(it.rel || '').includes('/')
|
|
295
|
+
return h('div', { className: 'dsh-zmem-item' }, [
|
|
296
|
+
h('div', {
|
|
297
|
+
key: 'head',
|
|
298
|
+
role: 'button',
|
|
299
|
+
tabIndex: 0,
|
|
300
|
+
className: 'dsh-zmem-itemHead',
|
|
301
|
+
'aria-expanded': open ? 'true' : 'false',
|
|
302
|
+
// 折叠时不渲染正文节点,aria-controls 只能在展开时指向真实存在的 id。
|
|
303
|
+
'aria-controls': open ? bodyId : undefined,
|
|
304
|
+
title: it.name ? `${it.title} (${it.name})` : it.title,
|
|
305
|
+
onClick: onToggle,
|
|
306
|
+
onKeyDown: (event) => {
|
|
307
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
308
|
+
event.preventDefault()
|
|
309
|
+
onToggle()
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
}, [
|
|
313
|
+
h('div', { key: 'top', className: 'dsh-zmem-itemTop' }, [
|
|
314
|
+
h('span', { key: 't', className: 'dsh-zmem-itemTitle' }, it.title || it.name || it.slug),
|
|
315
|
+
h('span', {
|
|
316
|
+
key: 's',
|
|
317
|
+
className: it.scope === 'global' ? 'dsh-zmem-badge dsh-zmem-badgeGlobal' : 'dsh-zmem-badge',
|
|
318
|
+
}, scopeLabel),
|
|
319
|
+
typeLabel ? h('span', { key: 'ty', className: 'dsh-zmem-badge' }, typeLabel) : null,
|
|
320
|
+
h('span', { key: 'd', className: 'dsh-zmem-date' }, it.updated || ''),
|
|
321
|
+
]),
|
|
322
|
+
it.description
|
|
323
|
+
? h('div', { key: 'desc', className: 'dsh-zmem-desc' }, it.description)
|
|
324
|
+
: null,
|
|
325
|
+
nested ? h('div', { key: 'rel', className: 'dsh-zmem-rel' }, it.rel) : null,
|
|
326
|
+
]),
|
|
327
|
+
open
|
|
328
|
+
? h('div', { key: 'body', id: bodyId, className: 'dsh-zmem-bodyWrap' }, [
|
|
329
|
+
h('pre', { key: 'pre', className: 'dsh-zmem-body', tabIndex: 0 }, it.body || ''),
|
|
330
|
+
it.truncated
|
|
331
|
+
? h('div', { key: 'tr', className: 'dsh-zmem-trunc' }, t('list.truncated', { chars: it.chars }))
|
|
332
|
+
: null,
|
|
333
|
+
])
|
|
334
|
+
: null,
|
|
335
|
+
])
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function MemorySettingsPage(props) {
|
|
339
|
+
const { t } = props
|
|
340
|
+
const [view, setView] = React.useState(null)
|
|
341
|
+
const [failure, setFailure] = React.useState('')
|
|
342
|
+
const [pending, setPending] = React.useState(false)
|
|
343
|
+
const [saved, setSaved] = React.useState(false)
|
|
344
|
+
const [query, setQuery] = React.useState('')
|
|
345
|
+
const [openKey, setOpenKey] = React.useState('')
|
|
346
|
+
const [loadingMore, setLoadingMore] = React.useState(false)
|
|
347
|
+
const reqRef = React.useRef(0)
|
|
348
|
+
const mounted = React.useRef(true)
|
|
349
|
+
const viewRef = React.useRef(null)
|
|
350
|
+
viewRef.current = view
|
|
351
|
+
|
|
352
|
+
React.useEffect(() => {
|
|
353
|
+
mounted.current = true
|
|
354
|
+
return () => { mounted.current = false }
|
|
355
|
+
}, [])
|
|
356
|
+
|
|
357
|
+
const load = React.useCallback((mode) => {
|
|
358
|
+
const id = (reqRef.current += 1)
|
|
359
|
+
const more = mode === 'more'
|
|
360
|
+
if (more) setLoadingMore(true)
|
|
361
|
+
else {
|
|
362
|
+
setLoadingMore(false)
|
|
363
|
+
setFailure('')
|
|
364
|
+
setSaved(false)
|
|
365
|
+
}
|
|
366
|
+
const offset = more && viewRef.current
|
|
367
|
+
? (Number(viewRef.current.page?.nextOffset) || viewRef.current.items.length)
|
|
368
|
+
: 0
|
|
369
|
+
fetchState(offset, PAGE_SIZE).then((body) => {
|
|
370
|
+
if (!mounted.current || id !== reqRef.current) return
|
|
371
|
+
setView((prev) => (more && prev
|
|
372
|
+
// 追加页只合并列表字段:开关状态以最后一次 GET/POST 为准,
|
|
373
|
+
// 不让慢到的分页响应把 enabled/stateError 改回旧值。
|
|
374
|
+
? {
|
|
375
|
+
...prev,
|
|
376
|
+
counts: body.counts,
|
|
377
|
+
page: body.page,
|
|
378
|
+
items: [...prev.items, ...body.items],
|
|
379
|
+
}
|
|
380
|
+
: body))
|
|
381
|
+
setFailure('')
|
|
382
|
+
}, (error) => {
|
|
383
|
+
if (!mounted.current || id !== reqRef.current) return
|
|
384
|
+
setFailure(t('page.failed', { message: errorText(error, t) }))
|
|
385
|
+
}).finally(() => {
|
|
386
|
+
if (mounted.current && id === reqRef.current) setLoadingMore(false)
|
|
387
|
+
})
|
|
388
|
+
}, [t])
|
|
389
|
+
|
|
390
|
+
React.useEffect(() => { load('reset') }, [])
|
|
391
|
+
|
|
392
|
+
const toggle = () => {
|
|
393
|
+
if (pending || view === null || view.configEnabled === false) return
|
|
394
|
+
// 开关也占用同一个请求代际:在途的刷新/加载更多返回后不能再覆盖开关状态。
|
|
395
|
+
const id = (reqRef.current += 1)
|
|
396
|
+
const next = view.enabled !== true
|
|
397
|
+
setPending(true)
|
|
398
|
+
setSaved(false)
|
|
399
|
+
setFailure('')
|
|
400
|
+
// 不做乐观更新:接口在本地,等真实有效值回填;被 profile 硬关闭时也能如实显示。
|
|
401
|
+
postState({ enabled: next }).then((body) => {
|
|
402
|
+
if (!mounted.current || id !== reqRef.current) return
|
|
403
|
+
setView((prev) => (prev ? { ...prev, ...body } : prev))
|
|
404
|
+
setSaved(true)
|
|
405
|
+
}, (error) => {
|
|
406
|
+
if (!mounted.current || id !== reqRef.current) return
|
|
407
|
+
setFailure(t('page.saveFailed', { message: errorText(error, t) }))
|
|
408
|
+
}).finally(() => {
|
|
409
|
+
if (mounted.current) {
|
|
410
|
+
setPending(false)
|
|
411
|
+
// toggle 递增了 reqRef,可能让在途 load-more 的 finally 变成 stale;
|
|
412
|
+
// 这里兜底复位,否则「加载更多」会永久停在“加载中…”。
|
|
413
|
+
setLoadingMore(false)
|
|
414
|
+
}
|
|
415
|
+
})
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (failure !== '' && view === null) {
|
|
419
|
+
return h('div', { className: 'dsh-zmem-page' }, [
|
|
420
|
+
h('div', { key: 'banner', className: 'dsh-zmem-banner' }, [
|
|
421
|
+
h('span', { key: 'text', className: 'dsh-zmem-error' }, failure),
|
|
422
|
+
h('button', { key: 'retry', type: 'button', className: 'dsh-zmem-retry', onClick: () => load('reset') },
|
|
423
|
+
t('page.retry')),
|
|
424
|
+
]),
|
|
425
|
+
])
|
|
426
|
+
}
|
|
427
|
+
if (view === null) {
|
|
428
|
+
return h('div', { className: 'dsh-zmem-page' }, [
|
|
429
|
+
h('p', { key: 'loading', className: 'dsh-zmem-intro' }, t('page.loading')),
|
|
430
|
+
])
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const items = Array.isArray(view.items) ? view.items : []
|
|
434
|
+
const total = Number(view.counts?.total) || items.length
|
|
435
|
+
const needle = query.trim().toLowerCase()
|
|
436
|
+
const shown = needle === ''
|
|
437
|
+
? items
|
|
438
|
+
: items.filter((it) => [it.title, it.name, it.description, it.body, it.workspace, it.type, it.rel]
|
|
439
|
+
.some((v) => String(v || '').toLowerCase().indexOf(needle) >= 0))
|
|
440
|
+
const hasMore = view.page?.hasMore === true
|
|
441
|
+
const status = failure !== '' ? failure : saved ? t('toggle.saved') : ''
|
|
442
|
+
const stateLine = pending
|
|
443
|
+
? t('toggle.pending')
|
|
444
|
+
: view.enabled === true
|
|
445
|
+
? t('toggle.stateOn')
|
|
446
|
+
: view.configEnabled === false && view.stateEnabled === true
|
|
447
|
+
? t('toggle.preferenceOnBlocked')
|
|
448
|
+
: t('toggle.stateOff')
|
|
449
|
+
|
|
450
|
+
return h('div', { className: 'dsh-zmem-page' }, [
|
|
451
|
+
h('h2', { key: 'title' }, t('page.title')),
|
|
452
|
+
h('p', { key: 'intro', className: 'dsh-zmem-intro' }, t('page.intro')),
|
|
453
|
+
|
|
454
|
+
// ---- 总开关 ----
|
|
455
|
+
h('section', { key: 'toggle', className: 'dsh-zmem-card' }, [
|
|
456
|
+
h('div', { key: 'head', className: 'dsh-zmem-cardHead' }, [
|
|
457
|
+
h('div', { key: 'copy' }, [
|
|
458
|
+
h('div', { key: 'title', className: 'dsh-zmem-cardTitle' }, t('toggle.label')),
|
|
459
|
+
h('p', { key: 'hint', className: 'dsh-zmem-cardHint' }, t('toggle.hint')),
|
|
460
|
+
view.configEnabled === false
|
|
461
|
+
? h('p', { key: 'cfg', className: 'dsh-zmem-cardHint dsh-zmem-error' }, t('toggle.configDisabled'))
|
|
462
|
+
: null,
|
|
463
|
+
view.stateError
|
|
464
|
+
? h('p', { key: 'stateError', className: 'dsh-zmem-cardHint dsh-zmem-error' },
|
|
465
|
+
t('list.status', { code: stateText(view.stateError, t) }))
|
|
466
|
+
: null,
|
|
467
|
+
]),
|
|
468
|
+
h('button', {
|
|
469
|
+
key: 'switch',
|
|
470
|
+
type: 'button',
|
|
471
|
+
role: 'switch',
|
|
472
|
+
'aria-checked': view.enabled === true ? 'true' : 'false',
|
|
473
|
+
'aria-label': t('toggle.label'),
|
|
474
|
+
className: 'dsh-zmem-switch',
|
|
475
|
+
disabled: pending || view.configEnabled === false,
|
|
476
|
+
onClick: toggle,
|
|
477
|
+
}, h('span', { key: 'thumb', className: 'dsh-zmem-thumb', 'aria-hidden': 'true' })),
|
|
478
|
+
]),
|
|
479
|
+
h('p', { key: 'state', className: 'dsh-zmem-cardHint' }, stateLine),
|
|
480
|
+
]),
|
|
481
|
+
|
|
482
|
+
// ---- 记忆清单 ----
|
|
483
|
+
h('section', { key: 'list', className: 'dsh-zmem-card' }, [
|
|
484
|
+
h('div', { key: 'head', className: 'dsh-zmem-cardHead' }, [
|
|
485
|
+
h('div', { key: 'copy' }, [
|
|
486
|
+
h('div', { key: 'title', className: 'dsh-zmem-cardTitle' }, t('list.title')),
|
|
487
|
+
h('p', { key: 'hint', className: 'dsh-zmem-cardHint' }, t('list.hint')),
|
|
488
|
+
]),
|
|
489
|
+
h('button', {
|
|
490
|
+
key: 'refresh',
|
|
491
|
+
type: 'button',
|
|
492
|
+
className: 'dsh-zmem-refresh',
|
|
493
|
+
disabled: pending,
|
|
494
|
+
onClick: () => load('reset'),
|
|
495
|
+
}, t('list.refresh')),
|
|
496
|
+
]),
|
|
497
|
+
h('div', { key: 'bar', className: 'dsh-zmem-toolbar' }, [
|
|
498
|
+
h('span', { key: 'count', className: 'dsh-zmem-count' }, t('list.count', {
|
|
499
|
+
total,
|
|
500
|
+
project: view.counts?.project ?? '0',
|
|
501
|
+
global: view.counts?.global ?? '0',
|
|
502
|
+
})),
|
|
503
|
+
h('p', { key: 'root', className: 'dsh-zmem-root' }, t('list.root', { path: view.memoriesRoot || '' })),
|
|
504
|
+
]),
|
|
505
|
+
h('input', {
|
|
506
|
+
key: 'search',
|
|
507
|
+
type: 'search',
|
|
508
|
+
className: 'dsh-zmem-search',
|
|
509
|
+
placeholder: t('list.search'),
|
|
510
|
+
'aria-label': t('list.search'),
|
|
511
|
+
value: query,
|
|
512
|
+
onChange: (event) => setQuery(event.target.value),
|
|
513
|
+
}),
|
|
514
|
+
h('div', { key: 'items', className: 'dsh-zmem-list' },
|
|
515
|
+
shown.length === 0
|
|
516
|
+
? h('div', { key: 'empty', className: 'dsh-zmem-empty' },
|
|
517
|
+
items.length === 0 ? t('list.empty') : t('list.noMatch'))
|
|
518
|
+
: shown.map((it) => {
|
|
519
|
+
const key = itemKey(it)
|
|
520
|
+
return h(MemoryItem, {
|
|
521
|
+
key,
|
|
522
|
+
it,
|
|
523
|
+
open: openKey === key,
|
|
524
|
+
onToggle: () => setOpenKey(openKey === key ? '' : key),
|
|
525
|
+
t,
|
|
526
|
+
})
|
|
527
|
+
})),
|
|
528
|
+
h('div', { key: 'more', className: 'dsh-zmem-more' }, [
|
|
529
|
+
hasMore
|
|
530
|
+
? h('button', {
|
|
531
|
+
key: 'moreBtn',
|
|
532
|
+
type: 'button',
|
|
533
|
+
className: 'dsh-zmem-moreBtn',
|
|
534
|
+
disabled: loadingMore || pending,
|
|
535
|
+
onClick: () => load('more'),
|
|
536
|
+
}, loadingMore ? t('list.loadingMore') : t('list.loadMore'))
|
|
537
|
+
: null,
|
|
538
|
+
hasMore
|
|
539
|
+
? h('span', { key: 'note', className: 'dsh-zmem-note' },
|
|
540
|
+
needle === ''
|
|
541
|
+
? t('list.pageNote', { shown: items.length, total })
|
|
542
|
+
: t('list.searchNote', { shown: items.length }))
|
|
543
|
+
: null,
|
|
544
|
+
]),
|
|
545
|
+
]),
|
|
546
|
+
|
|
547
|
+
h('p', {
|
|
548
|
+
key: 'status',
|
|
549
|
+
className: failure === '' ? 'dsh-zmem-status' : 'dsh-zmem-status dsh-zmem-error',
|
|
550
|
+
role: failure === '' ? 'status' : 'alert',
|
|
551
|
+
}, status),
|
|
552
|
+
])
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function apply(ctx) {
|
|
556
|
+
const styleEl = installStyle()
|
|
557
|
+
ctx.effect(() => () => { styleEl.remove() }, 'dsh-zcode-memory: styles')
|
|
558
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'dsh-zcode-memory: dictionaries')
|
|
559
|
+
|
|
560
|
+
const t = (key, values) => format(ctx.locale.bind(NS)(key), values)
|
|
561
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
562
|
+
name: 'settings.section',
|
|
563
|
+
id: SECTION_ID,
|
|
564
|
+
// 排在「顾问」(82) 之后,与其它插件页连成一块。
|
|
565
|
+
order: 83,
|
|
566
|
+
label: () => t('section.label'),
|
|
567
|
+
locale: NS,
|
|
568
|
+
}, MemorySettingsPage))
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return { inject: ['slots', 'locale'], apply }
|
|
572
|
+
},
|
|
573
|
+
})
|