@tangjs/dsh-memory 0.0.0-stage → 1.0.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/locale/zh.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "meta": {
3
+ "title": "记忆笔记",
4
+ "description": "按工作区隔离的记忆笔记:捕获、提取、召回,并在侧边栏页面浏览。",
5
+ "detail": "docs/DETAIL.md"
6
+ },
7
+ "page": {
8
+ "tab.title": "记忆",
9
+ "guide.title": "记忆",
10
+ "guide.description": "浏览各目录空间的记忆笔记",
11
+ "spaces.title": "空间",
12
+ "spaces.current": "当前",
13
+ "spaces.global": "全局",
14
+ "spaces.locked": "后续开放",
15
+ "spaces.empty": "暂无空间",
16
+ "spaces.count": "{count} 条",
17
+ "spaces.none": "—",
18
+ "notes.title": "记忆笔记",
19
+ "notes.empty": "这个目录还没有记忆",
20
+ "notes.emptyHint": "在对话里让助手记住某件事,笔记就会出现在这里。",
21
+ "notes.count": "{count} 条笔记",
22
+ "notes.refresh": "刷新",
23
+ "note.updatedAt": "更新于 {time}",
24
+ "note.session": "会话 {id}",
25
+ "note.noSession": "无来源会话",
26
+ "note.noDescription": "(无描述)",
27
+ "state.loading": "加载中…",
28
+ "state.error.title": "加载失败",
29
+ "state.error.retry": "重试",
30
+ "state.error.network": "无法连接记忆服务,请确认插件仍在运行。",
31
+ "state.error.http": "接口返回 {status}。",
32
+ "state.error.body": "接口返回了无法解析的内容。",
33
+ "state.error.timeout": "请求超时,请重试。",
34
+ "detail.back": "返回",
35
+ "detail.file": "文件",
36
+ "detail.notFound": "这条笔记不存在,可能已被合并或删除。",
37
+ "detail.bytes": "{bytes} 字节",
38
+ "badge.type.user": "用户",
39
+ "badge.type.feedback": "反馈",
40
+ "badge.type.project": "项目",
41
+ "badge.type.reference": "参考",
42
+ "badge.origin.agent": "工具写入",
43
+ "badge.origin.extract": "自动提取",
44
+ "badge.origin.dream": "整理",
45
+ "index.badge": "索引",
46
+ "index.title": "MEMORY.md 索引",
47
+ "index.subtitle": "目录 · {count} 条笔记",
48
+ "index.open": "查看索引"
49
+ }
50
+ }
package/package.json CHANGED
@@ -1,6 +1,79 @@
1
1
  {
2
2
  "name": "@tangjs/dsh-memory",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Hierarchical, workspace-scoped memory notes for DeepSeek Harness: capture, consolidate, recall, and browse.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "author": "tangzhicheng",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/zixingtangmouren/dsh-memory.git"
11
+ },
12
+ "homepage": "https://github.com/zixingtangmouren/dsh-memory#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/zixingtangmouren/dsh-memory/issues"
15
+ },
16
+ "keywords": [
17
+ "dsh",
18
+ "dsh-plugin",
19
+ "deepseek-harness",
20
+ "memory",
21
+ "notes",
22
+ "agent-memory",
23
+ "cordis",
24
+ "llm"
25
+ ],
26
+ "engines": {
27
+ "node": "^22.19.0 || >=24.9.0",
28
+ "dsh": ">=0.2.2-0 <0.3.0"
29
+ },
30
+ "peerDependencies": {
31
+ "@deepseek-ai/dsh-llm": ">=0.2.2-0 <0.3.0",
32
+ "@deepseek-ai/dsh-tools": ">=0.2.2-0 <0.3.0",
33
+ "@deepseek-ai/schemastery": "^3.18.3"
34
+ },
35
+ "exports": {
36
+ ".": "./index.js",
37
+ "./client": "./client/client.js",
38
+ "./cordis.patch.yml": "./cordis.patch.yml",
39
+ "./locale/*.json": "./locale/*.json",
40
+ "./icon": "./icon.svg",
41
+ "./package.json": "./package.json"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "files": [
47
+ "index.js",
48
+ "src",
49
+ "client",
50
+ "locale",
51
+ "docs",
52
+ "screenshots",
53
+ "cordis.patch.yml",
54
+ "icon.svg",
55
+ "README.md",
56
+ "README.en.md",
57
+ "CHANGELOG.md",
58
+ "LICENSE"
59
+ ],
60
+ "dsh": {
61
+ "manifestVersion": 1,
62
+ "bundle": {
63
+ "patch": "./cordis.patch.yml"
64
+ },
65
+ "client": {
66
+ "platform": "web",
67
+ "immediately": true
68
+ },
69
+ "capabilities": [
70
+ "tools:register"
71
+ ],
72
+ "labels": []
73
+ },
74
+ "scripts": {
75
+ "check": "node --check index.js && for f in client/client.js src/host/*.mjs; do node --check \"$f\" || exit 1; done",
76
+ "test": "node --test",
77
+ "ci": "npm run check && npm run test"
78
+ }
79
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,234 @@
1
+ // Read-only JSON routes for the client page.
2
+ //
3
+ // The webserver binds to loopback for browser clients; these routes expose only
4
+ // notes under the configured memory root and never accept a caller-supplied
5
+ // path. Kept free of @deepseek-ai imports so it can be unit tested in CI.
6
+
7
+ import { parseMemoryIndex } from './memoryIndex.mjs'
8
+ import { GLOBAL_SPACE, isSafeFile, isSafeSpaceKey } from './store.mjs'
9
+
10
+ export const API_PREFIX = '/plugin/dsh-memory/api'
11
+
12
+ const JSON_HEADERS = Object.freeze({
13
+ 'content-type': 'application/json; charset=utf-8',
14
+ 'cache-control': 'no-store',
15
+ })
16
+
17
+ function send(res, status, value) {
18
+ const body = JSON.stringify(value)
19
+ if (typeof res.writeHead === 'function') {
20
+ res.writeHead(status, { ...JSON_HEADERS, 'content-length': Buffer.byteLength(body) })
21
+ }
22
+ res.end(body)
23
+ }
24
+
25
+ function fail(res, status, message) {
26
+ send(res, status, { error: message })
27
+ }
28
+
29
+ /**
30
+ * @param {object} options
31
+ * @param {object} options.store createStore() result
32
+ * @param {(spaceKey: string) => Promise<object|null>} [options.describeSpace]
33
+ * @param {() => Promise<Array<{key: string, title?: string, path?: string}>>} [options.listKnownSpaces]
34
+ * @param {string|(() => string)} [options.currentSpaceKey]
35
+ * @param {boolean} [options.globalEnabled]
36
+ */
37
+ export function createApiHandler({
38
+ store,
39
+ describeSpace,
40
+ listKnownSpaces,
41
+ currentSpaceKey = '',
42
+ globalEnabled = false,
43
+ } = {}) {
44
+ if (store === undefined) throw new Error('createApiHandler requires a store')
45
+ const globalOn = globalEnabled === true
46
+
47
+ /**
48
+ * A workspace key usable as "current". `_global` is never a current
49
+ * workspace: it is a fixed row appended to the space list.
50
+ */
51
+ function safeCurrent(value) {
52
+ return isSafeSpaceKey(value ?? '') && value !== GLOBAL_SPACE ? String(value) : ''
53
+ }
54
+
55
+ function resolveCurrent() {
56
+ const value = typeof currentSpaceKey === 'function' ? currentSpaceKey() : currentSpaceKey
57
+ return safeCurrent(value)
58
+ }
59
+
60
+ /** Whether a space may be read: any safe key, plus `_global` when enabled. */
61
+ function isReadableSpace(space) {
62
+ if (!isSafeSpaceKey(space)) return false
63
+ return space === GLOBAL_SPACE ? globalOn : true
64
+ }
65
+
66
+ /**
67
+ * `refresh=1` drops the store's derived cache so a note edited by hand (on
68
+ * disk, outside the plugin) shows its new content without a restart. When the
69
+ * parameter is absent the behaviour is exactly as before.
70
+ */
71
+ function refreshCache(space, url) {
72
+ const value = url.searchParams.get('refresh')
73
+ if (value !== '1' && value !== 'true') return
74
+ if (typeof store.invalidate === 'function') store.invalidate(space)
75
+ }
76
+
77
+ async function spacesPayload(current) {
78
+ const onDisk = await store.listSpaces()
79
+ const byKey = new Map(onDisk.map(space => [space.key, space]))
80
+ // The global row below is always appended last (so it never sorts into the
81
+ // recent-workspace list), but its count comes from what is really on disk.
82
+ const globalSpace = byKey.get(GLOBAL_SPACE)
83
+ byKey.delete(GLOBAL_SPACE)
84
+ const known = typeof listKnownSpaces === 'function' ? await listKnownSpaces().catch(() => []) : []
85
+ const rows = []
86
+ for (const space of known) {
87
+ if (!isSafeSpaceKey(space.key) || space.key === GLOBAL_SPACE) continue
88
+ const disk = byKey.get(space.key)
89
+ rows.push({
90
+ key: space.key,
91
+ title: space.title ?? space.key,
92
+ path: space.path ?? '',
93
+ count: disk?.count ?? 0,
94
+ updatedAt: disk?.updatedAt ?? '',
95
+ current: space.key === current,
96
+ enabled: true,
97
+ })
98
+ byKey.delete(space.key)
99
+ }
100
+ for (const disk of byKey.values()) {
101
+ rows.push({
102
+ key: disk.key,
103
+ title: disk.key,
104
+ path: '',
105
+ count: disk.count,
106
+ updatedAt: disk.updatedAt,
107
+ current: disk.key === current,
108
+ enabled: true,
109
+ })
110
+ }
111
+ rows.sort((a, b) => {
112
+ if (a.current !== b.current) return a.current ? -1 : 1
113
+ return String(b.updatedAt).localeCompare(String(a.updatedAt))
114
+ })
115
+ rows.push({
116
+ key: GLOBAL_SPACE,
117
+ title: 'Global',
118
+ path: '',
119
+ count: globalSpace?.count ?? 0,
120
+ updatedAt: globalSpace?.updatedAt ?? '',
121
+ current: false,
122
+ enabled: globalOn,
123
+ })
124
+ return { spaces: rows, current }
125
+ }
126
+
127
+ return async function handle(req, res) {
128
+ const method = String(req.method ?? 'GET').toUpperCase()
129
+ if (method !== 'GET' && method !== 'HEAD') {
130
+ if (typeof res.writeHead === 'function') res.writeHead(405, { allow: 'GET, HEAD' })
131
+ res.end()
132
+ return
133
+ }
134
+ let url
135
+ try {
136
+ url = new URL(String(req.url ?? '/'), 'http://localhost')
137
+ } catch {
138
+ fail(res, 400, 'invalid request URL')
139
+ return
140
+ }
141
+ const path = url.pathname
142
+ try {
143
+ if (path === `${API_PREFIX}/spaces`) {
144
+ const requested = safeCurrent(url.searchParams.get('current'))
145
+ const current = requested !== '' ? requested : resolveCurrent()
146
+ const payload = await spacesPayload(current)
147
+ if (describeSpace !== undefined && current !== '') {
148
+ payload.space = await describeSpace(current).catch(() => null)
149
+ }
150
+ send(res, 200, payload)
151
+ return
152
+ }
153
+ if (path === `${API_PREFIX}/notes`) {
154
+ const space = url.searchParams.get('space') ?? resolveCurrent()
155
+ if (!isReadableSpace(space)) {
156
+ fail(res, 400, 'unknown space')
157
+ return
158
+ }
159
+ refreshCache(space, url)
160
+ const notes = await store.listNotes(space)
161
+ const indexMarkdown = typeof store.readIndex === 'function' ? await store.readIndex(space).catch(() => '') : ''
162
+ const index =
163
+ indexMarkdown === ''
164
+ ? null
165
+ : {
166
+ file: 'MEMORY.md',
167
+ entries: parseMemoryIndex(indexMarkdown).length,
168
+ chars: indexMarkdown.length,
169
+ bytes: Buffer.byteLength(indexMarkdown),
170
+ }
171
+ send(res, 200, { space, notes, index })
172
+ return
173
+ }
174
+ if (path === `${API_PREFIX}/index`) {
175
+ const space = url.searchParams.get('space') ?? resolveCurrent()
176
+ if (!isReadableSpace(space)) {
177
+ fail(res, 400, 'unknown space')
178
+ return
179
+ }
180
+ refreshCache(space, url)
181
+ // Reading the notes first lets the store repair a missing index.
182
+ await store.listNotes(space)
183
+ const markdown = typeof store.readIndex === 'function' ? await store.readIndex(space).catch(() => '') : ''
184
+ if (markdown === '') {
185
+ fail(res, 404, 'index not found')
186
+ return
187
+ }
188
+ send(res, 200, {
189
+ space,
190
+ file: 'MEMORY.md',
191
+ kind: 'index',
192
+ markdown,
193
+ entries: parseMemoryIndex(markdown).length,
194
+ bytes: Buffer.byteLength(markdown),
195
+ })
196
+ return
197
+ }
198
+ if (path === `${API_PREFIX}/note`) {
199
+ const space = url.searchParams.get('space') ?? resolveCurrent()
200
+ const file = url.searchParams.get('file') ?? ''
201
+ if (!isReadableSpace(space)) {
202
+ fail(res, 400, 'unknown space')
203
+ return
204
+ }
205
+ if (!isSafeFile(file)) {
206
+ fail(res, 400, 'invalid note file')
207
+ return
208
+ }
209
+ refreshCache(space, url)
210
+ const note = await store.readNote(space, file)
211
+ send(res, 200, {
212
+ space,
213
+ file: note.file,
214
+ frontmatter: note.frontmatter,
215
+ body: note.body,
216
+ bytes: note.bytes,
217
+ })
218
+ return
219
+ }
220
+ fail(res, 404, 'no such route')
221
+ } catch (error) {
222
+ const code = error?.code
223
+ if (code === 'NOT_FOUND') {
224
+ fail(res, 404, 'note not found')
225
+ return
226
+ }
227
+ if (code === 'INVALID_FILE' || code === 'INVALID_SPACE' || code === 'NOTE_TOO_LARGE') {
228
+ fail(res, 400, String(error.message ?? 'invalid request'))
229
+ return
230
+ }
231
+ fail(res, 500, 'memory route failed')
232
+ }
233
+ }
234
+ }