@jacklika/dsh-memory-curator 0.1.0-alpha.4 → 0.1.0-alpha.6

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
@@ -4,7 +4,15 @@ Curator tools for the DSH memory vault. This package is a member of the `@jackli
4
4
 
5
5
  ## Tools
6
6
 
7
- - `memory_recall(query)` — Search the vault for prior notes before starting a task.
7
+ - `memory_recall(query)` — Search the vault for prior notes before starting a task. Returns `{ query, hits, total }`, where `total` is the vault-wide match count passed through from `wiki_search` and can exceed `hits.length` when the search layer caps its results — so a caller can tell "these are all the matches" from "there are more".
8
8
  - `memory_capture(title, summary, ...)` — Persist durable knowledge after significant work, with conflict detection and optional user approval.
9
9
 
10
+ ## Note ids
11
+
12
+ When `id` is omitted, `memory_capture` derives it from the title: Unicode letters and digits are kept (NFC-normalized, lowercased) and every other run of characters becomes a single `-`, so CJK and other non-Latin titles stay readable and distinct. A title with no letters or digits at all falls back to `note-<12 hex chars of its sha256>`, so symbol-only captures never collapse into one shared file.
13
+
14
+ ## Overwriting a capture
15
+
16
+ `mode: 'overwrite'` replaces the note body but merges its frontmatter: fields another writer recorded and the original `created` survive, `title` and `tags` come from this call, and `updated` is stamped. A first capture writes `created` only.
17
+
10
18
  See the Bundle documentation in `docs/usage.md` for configuration and workflow.
package/README.zh.md CHANGED
@@ -4,7 +4,15 @@ DSH 记忆库的策展工具。本包是 `@jacklika/dsh-memory` Bundle 的成员
4
4
 
5
5
  ## 工具
6
6
 
7
- - `memory_recall(query)` — 在任务开始前搜索知识库中的相关笔记。
7
+ - `memory_recall(query)` — 在任务开始前搜索知识库中的相关笔记。返回 `{ query, hits, total }`,其中 `total` 是从 `wiki_search` 透传的全库命中数,在搜索层截断结果时可能大于 `hits.length`——调用方因此能区分“这就是全部命中”与“还有更多”。
8
8
  - `memory_capture(title, summary, ...)` — 在有意义的任务结束后持久化知识,支持冲突检测与可选的用户审批。
9
9
 
10
+ ## 笔记 id
11
+
12
+ 省略 `id` 时,`memory_capture` 由标题推导笔记 id:保留 Unicode 字母与数字(先做 NFC 归一化并转小写),其余连续字符统一折叠为单个 `-`,因此中日韩等非拉丁标题依然可读且互不相同。完全不含字母与数字的标题退化为 `note-<标题 sha256 的前 12 位十六进制>`,纯符号标题的写入不会坍缩到同一个文件。
13
+
14
+ ## 覆盖式入库
15
+
16
+ `mode: 'overwrite'` 替换笔记正文,但会合并既有 frontmatter:其他写入方记录的字段与原始 `created` 都保留,`title` 与 `tags` 以本次调用为准,并写入 `updated`。首次入库只写 `created`。
17
+
10
18
  配置与工作流请参阅 Bundle 文档 `docs/usage.zh.md`。
package/lib/index.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * shared curated zone.
8
8
  * @module @jacklika/dsh-memory-curator
9
9
  */
10
- import { randomUUID } from 'node:crypto';
10
+ import { createHash, randomUUID } from 'node:crypto';
11
11
  import z from '@deepseek-ai/schemastery';
12
12
  import { ToolCallId } from '@deepseek-ai/dsh-llm';
13
13
  import { defineTool } from '@deepseek-ai/dsh-tools';
@@ -23,18 +23,49 @@ export const Config = z.object({
23
23
  defaultScope: z.union(['shared', 'private']).default('shared'),
24
24
  notesDir: z.string().default('shared/notes'),
25
25
  });
26
- function normalizeTitle(title) {
27
- return title.toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();
26
+ /**
27
+ * Fold a title to its comparison form: NFC, case-insensitive, every run of
28
+ * characters that are neither Unicode letters nor digits collapsed to one
29
+ * space. Keeping `\p{L}` means CJK and other non-Latin titles stay distinct
30
+ * instead of folding to the empty string they would all share.
31
+ */
32
+ function foldTitle(title) {
33
+ return title.normalize('NFC').toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim();
34
+ }
35
+ const MAX_SLUG_LENGTH = 100;
36
+ function hashTail(title, length = 12) {
37
+ return createHash('sha256').update(title.normalize('NFC')).digest('hex').slice(0, length);
28
38
  }
29
39
  function slugify(title) {
30
- return title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
40
+ let slug = foldTitle(title).replace(/ /g, '-');
41
+ // Emoji-only and punctuation-only titles fold to nothing; a stable content
42
+ // hash keeps them addressable and separate rather than sharing a bare ".md".
43
+ if (slug === '')
44
+ return `note-${hashTail(title)}`;
45
+ // Keep generated filenames bounded on all platforms. A long CJK title would
46
+ // otherwise create a very long filename; truncate and append a hash so the
47
+ // id remains deterministic and collision-resistant.
48
+ if (slug.length > MAX_SLUG_LENGTH) {
49
+ slug = `${slug.slice(0, 64)}-${hashTail(title)}`;
50
+ }
51
+ return slug;
31
52
  }
32
53
  function ensureMdExtension(id) {
33
54
  return id.endsWith('.md') ? id : `${id}.md`;
34
55
  }
56
+ function isSafeNoteId(id) {
57
+ if (id === '' || id.startsWith('/') || id.startsWith('\\') || /^[A-Za-z]:/.test(id))
58
+ return false;
59
+ if (id.includes('\\'))
60
+ return false;
61
+ return !id.split('/').some(segment => segment === '..' || segment === '.');
62
+ }
35
63
  function resolveNoteId(id, title, scope, notesDir) {
36
64
  if (id !== undefined && id !== '') {
37
65
  const withExt = ensureMdExtension(id);
66
+ if (!isSafeNoteId(withExt)) {
67
+ throw new Error(`memory-curator: invalid note id ${JSON.stringify(withExt)}: ids must be vault-relative paths without '..', '.', backslashes, leading slashes, or drive letters`);
68
+ }
38
69
  if (scope === 'private')
39
70
  return withExt;
40
71
  return withExt.startsWith('shared/') ? withExt : `${notesDir}/${withExt.replace(/^\//, '')}`;
@@ -66,16 +97,50 @@ async function callTool(ctx, exec, toolName, args) {
66
97
  return JSON.parse(textFromContent(result.content) || 'null');
67
98
  }
68
99
  function findConflicts(targetId, title, hits) {
69
- const normalized = normalizeTitle(title);
100
+ const normalized = foldTitle(title);
101
+ const target = targetId.normalize('NFC').toLowerCase();
70
102
  return hits.filter(hit => {
71
- if (hit.id.toLowerCase() === targetId.toLowerCase())
103
+ if (hit.id.normalize('NFC').toLowerCase() === target)
72
104
  return true;
73
- return normalizeTitle(hit.title) === normalized;
105
+ // A title that folds to nothing carries no identity; matching it would make
106
+ // every untitled note a conflict with every other one.
107
+ return normalized !== '' && foldTitle(hit.title) === normalized;
74
108
  });
75
109
  }
76
110
  function dedupeHits(hits) {
77
111
  return [...new Map(hits.map(hit => [hit.id, hit])).values()];
78
112
  }
113
+ /**
114
+ * Frontmatter for a capture that overwrites `id`. `wiki_write` overwrite
115
+ * replaces the whole file, so the note's existing fields have to be carried
116
+ * over here or an update silently drops whatever another writer recorded and
117
+ * resets `created` to the update time.
118
+ * @param ctx - registrant context carrying the tool registry.
119
+ * @param exec - the run context the nested read is dispatched under.
120
+ * @param id - vault-relative note id about to be overwritten.
121
+ * @param title - title from this capture; it wins over any stored one.
122
+ * @param tags - tags from this capture; they win over any stored ones.
123
+ * @param now - Beijing-time timestamp used for `created`/`updated`.
124
+ * @returns the merged frontmatter to serialize.
125
+ */
126
+ async function overwriteFrontmatter(ctx, exec, id, title, tags, now) {
127
+ let existing;
128
+ try {
129
+ const note = await callTool(ctx, exec, 'wiki_read', { id });
130
+ if (note !== null && typeof note === 'object' && note.frontmatter !== null && typeof note.frontmatter === 'object') {
131
+ existing = note.frontmatter;
132
+ }
133
+ }
134
+ catch {
135
+ // No readable note at this id: the ordinary first capture of a new note.
136
+ }
137
+ const frontmatter = { ...existing, title, tags };
138
+ const created = existing?.created;
139
+ frontmatter.created = typeof created === 'string' && created !== '' ? created : now;
140
+ if (existing !== undefined)
141
+ frontmatter.updated = now;
142
+ return frontmatter;
143
+ }
79
144
  /**
80
145
  * Register `memory_recall` and `memory_capture` on the tool registry.
81
146
  * @param ctx - registrant context carrying the tool registry.
@@ -91,7 +156,7 @@ export function apply(ctx, config) {
91
156
  const approval = ctx.get('approval');
92
157
  ctx.effect(() => ctx.tools.register(defineTool({
93
158
  name: 'memory_recall',
94
- description: 'Search the memory vault for prior notes related to the current task or topic. Call this at the start of a development task to avoid asking the user to repeat context that is already captured. Query terms are OR-matched and ranked by field-weighted relevance, so partial matches still return. Returns matching note ids, titles, scores, and backlink counts.',
159
+ description: 'Search the memory vault for prior notes related to the current task or topic. Call this at the start of a development task to avoid asking the user to repeat context that is already captured. Query terms are OR-matched and ranked by field-weighted relevance, so partial matches still return. Returns the matching note ids, titles, scores, and backlinks, plus total — how many notes matched before the result cap, so a total above the returned hit count means the list was truncated.',
95
160
  parameters: {
96
161
  query: {
97
162
  type: 'string',
@@ -104,11 +169,19 @@ export function apply(ctx, config) {
104
169
  render: (_args, value) => [{ type: 'text', text: JSON.stringify(value, null, 2) }],
105
170
  },
106
171
  async execute(args, exec) {
107
- const hits = await callTool(ctx, exec, 'wiki_search', { query: args.query });
172
+ const search = await callTool(ctx, exec, 'wiki_search', { query: args.query });
173
+ // Mechanical per-turn summaries are useful for session replay, but they
174
+ // are not curated knowledge. Keep them out of proactive recall unless a
175
+ // caller explicitly searches the wiki.
176
+ const curated = search.hits.filter(hit => !hit.id.startsWith('shared/notes/auto-capture/'));
177
+ // `search.total` is the vault-wide count before the search-layer cap. If
178
+ // any auto-capture hits made it into the capped `hits` list, remove them
179
+ // from the total so curated numbers stay truthful.
180
+ const total = search.total - (search.hits.length - curated.length);
108
181
  return {
109
182
  query: args.query,
110
- hits: hits.slice(0, 20),
111
- total: hits.length,
183
+ hits: curated.slice(0, 20),
184
+ total,
112
185
  };
113
186
  },
114
187
  })));
@@ -165,11 +238,11 @@ export function apply(ctx, config) {
165
238
  const targetId = resolveNoteId(args.id, args.title, scope, notesDir);
166
239
  const mode = args.mode ?? 'append';
167
240
  const tags = Array.isArray(args.tags) ? args.tags.map(String) : [];
168
- const [titleHits, idHits] = await Promise.all([
241
+ const [titleSearch, idSearch] = await Promise.all([
169
242
  callTool(ctx, exec, 'wiki_search', { query: args.title }),
170
243
  callTool(ctx, exec, 'wiki_search', { query: targetId }),
171
244
  ]);
172
- const conflicts = dedupeHits(findConflicts(targetId, args.title, [...titleHits, ...idHits]));
245
+ const conflicts = dedupeHits(findConflicts(targetId, args.title, [...titleSearch.hits, ...idSearch.hits]));
173
246
  const conflictExists = conflicts.length > 0;
174
247
  const finalId = conflicts[0]?.id ?? targetId;
175
248
  if (conflictExists) {
@@ -214,15 +287,12 @@ export function apply(ctx, config) {
214
287
  const now = formatBeijingTime(new Date());
215
288
  const detailsText = args.details ? `\n\n## Details\n\n${args.details}` : '';
216
289
  const appendContent = `${args.summary}${detailsText}\n`;
217
- const frontmatter = {
218
- title: args.title,
219
- tags,
220
- created: now,
221
- };
222
- const newNoteContent = `---\n${yaml.dump(frontmatter).trim()}\n---\n\n# ${args.title}\n\n${appendContent}`;
223
290
  const appending = conflictExists && mode === 'append';
224
- const content = appending ? appendContent : newNoteContent;
225
291
  const writeMode = appending ? 'append' : 'overwrite';
292
+ const frontmatter = appending ? undefined : await overwriteFrontmatter(ctx, exec, finalId, args.title, tags, now);
293
+ const content = appending
294
+ ? appendContent
295
+ : `---\n${yaml.dump(frontmatter).trim()}\n---\n\n# ${args.title}\n\n${appendContent}`;
226
296
  await callTool(ctx, exec, 'wiki_write', {
227
297
  id: finalId,
228
298
  content,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jacklika/dsh-memory-curator",
3
3
  "description": "Curator tools for the DSH memory vault: memory_recall before acting and memory_capture after significant work, with conflict detection and optional user approval.",
4
- "version": "0.1.0-alpha.4",
4
+ "version": "0.1.0-alpha.6",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -37,13 +37,13 @@
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@deepseek-ai/cordis": "4.0.4",
40
- "@deepseek-ai/dsh-agent": ">=0.1.7-rc.1 <0.3.0-0",
41
- "@deepseek-ai/dsh-llm": ">=0.1.7-rc.1 <0.3.0-0",
42
- "@deepseek-ai/dsh-tools": ">=0.1.7-rc.1 <0.3.0-0",
43
- "@deepseek-ai/dsh-user-approval": ">=0.1.7-rc.1 <0.3.0-0",
44
- "@deepseek-ai/dsh-util-values": ">=0.1.7-rc.1 <0.3.0-0"
40
+ "@deepseek-ai/dsh-agent": ">=0.1.7-rc.1 <0.3.0-0 || >=0.2.0-rc.1 <0.3.0-0",
41
+ "@deepseek-ai/dsh-llm": ">=0.1.7-rc.1 <0.3.0-0 || >=0.2.0-rc.1 <0.3.0-0",
42
+ "@deepseek-ai/dsh-tools": ">=0.1.7-rc.1 <0.3.0-0 || >=0.2.0-rc.1 <0.3.0-0",
43
+ "@deepseek-ai/dsh-user-approval": ">=0.1.7-rc.1 <0.3.0-0 || >=0.2.0-rc.1 <0.3.0-0",
44
+ "@deepseek-ai/dsh-util-values": ">=0.1.7-rc.1 <0.3.0-0 || >=0.2.0-rc.1 <0.3.0-0"
45
45
  },
46
46
  "devDependencies": {
47
- "@jacklika/dsh-tool-memory-filesystem": "^0.1.3-alpha.5"
47
+ "@jacklika/dsh-tool-memory-filesystem": "^0.1.3-alpha.9"
48
48
  }
49
49
  }