dsh-mnemon-source-journal 0.1.0 → 0.1.1

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.
Files changed (3) hide show
  1. package/README.md +18 -0
  2. package/lib/index.js +58 -11
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -49,3 +49,21 @@ pnpm add dsh-mnemon-source-journal
49
49
  ## 许可
50
50
 
51
51
  MIT
52
+
53
+ ## 配置
54
+
55
+ > **日常不建议改这里。** 这些项都有默认值,要改就改统一配置文件 `~/.mnemon/state/weknora-suite.json` 里 `packages.<包名>.values` —— 那份文件带中文说明、带出厂值可回滚,AI 也能直接改。写在 `cordis.patch.yml` 里会盖住它,只在「这个 profile 要跟别人不一样」时才用。
56
+
57
+ ```yaml
58
+ - id: mnemon-source-journal
59
+ name: "dsh-mnemon-source-journal"
60
+ config:
61
+ maxItemChars: 2600 # 一条检索结果最多多少字
62
+ maxFiles: 4000 # 最多扫多少本账
63
+ maxRouteChars: 16000 # 单次检索路线预算
64
+ searchDefaultLimit: 10 # 没传 limit 时返回几条
65
+ searchMaxLimit: 20 # 传了 limit 时的上限
66
+ transcriptDir: "" # 留空 = ~/.mnemon/transcripts
67
+ ```
68
+
69
+ 这个包声明的是 `episodic` 角色:`default-three-tier` 策略不接收它,要走 `general` 策略才会被投影。这是上游设计,不是故障。
package/lib/index.js CHANGED
@@ -18,13 +18,59 @@ import { join } from "node:path";
18
18
  import { COMPOSABLE_MEMORY_API_VERSION } from "dsh-mnemon/contracts";
19
19
  import { defineMemoryPlugin, defineMemorySource, installMemory, memoryConfigurationDigest, truncateMemoryText } from "dsh-mnemon/extension-sdk";
20
20
 
21
+ /** 统一配置文件 ~/.mnemon/state/weknora-suite.json 里本包那一段;没有或读坏就返回空对象。 */
22
+ function suiteSection(pkgName) {
23
+ try {
24
+ const raw = readFileSync(join(homedir(), ".mnemon", "state", "weknora-suite.json"), "utf8");
25
+ const values = JSON.parse(raw)?.packages?.[pkgName]?.values;
26
+ return values && typeof values === "object" ? values : {};
27
+ } catch {
28
+ return {};
29
+ }
30
+ }
31
+
21
32
  const name = "dsh-mnemon-source-journal";
22
33
  const inject = ["mnemonMemory"];
23
34
 
24
35
  const DEFAULT_DIR = join(homedir(), ".mnemon", "transcripts");
25
36
  const FILE_PATTERN = /^\d{4}-\d{2}-\d{2}(-[0-9a-zA-Z]{1,16})?\.md$/u;
26
- const MAX_ITEM_CHARS = 2600;
27
- const MAX_FILES = 4000;
37
+ /** 默认值:每一项都能被 row 的 config 覆盖。 */
38
+ const DEFAULTS = {
39
+ /** 一条检索结果最多给多少字。 */
40
+ maxItemChars: 2600,
41
+ /** 最多扫描多少本账(新的优先)。 */
42
+ maxFiles: 4000,
43
+ /** 单次检索路线最多返回多少字。 */
44
+ maxRouteChars: 16000,
45
+ /** 没传 limit 时默认返回几条。 */
46
+ searchDefaultLimit: 10,
47
+ /** 传了 limit 时的上限。 */
48
+ searchMaxLimit: 20,
49
+ /** 账本目录。留空 = ~/.mnemon/transcripts。 */
50
+ transcriptDir: "",
51
+ };
52
+
53
+ const num = (value, fallback) => {
54
+ const parsed = typeof value === "string" ? Number(value.trim()) : value;
55
+ return typeof parsed === "number" && Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
56
+ };
57
+
58
+ function resolveConfig(config = {}) {
59
+ const c = { ...suiteSection("dsh-mnemon-source-journal"), ...(config ?? {}) };
60
+ return {
61
+ maxItemChars: num(c.maxItemChars, DEFAULTS.maxItemChars),
62
+ maxFiles: num(c.maxFiles, DEFAULTS.maxFiles),
63
+ maxRouteChars: num(c.maxRouteChars, DEFAULTS.maxRouteChars),
64
+ searchDefaultLimit: num(c.searchDefaultLimit, DEFAULTS.searchDefaultLimit),
65
+ searchMaxLimit: num(c.searchMaxLimit, DEFAULTS.searchMaxLimit),
66
+ transcriptDir:
67
+ typeof c.transcriptDir === "string" && c.transcriptDir.trim() !== ""
68
+ ? c.transcriptDir.trim()
69
+ : typeof c.dir === "string" && c.dir.trim() !== ""
70
+ ? c.dir.trim()
71
+ : DEFAULT_DIR,
72
+ };
73
+ }
28
74
  const LOAD_LOG = join(homedir(), ".mnemon", "state", "journal-source.log");
29
75
  let loggedOnce = false;
30
76
 
@@ -69,7 +115,7 @@ function tokensOf(text) {
69
115
  return [...new Set(tokens)];
70
116
  }
71
117
 
72
- function readDir(dir) {
118
+ function readDir(dir, maxFiles) {
73
119
  let names = [];
74
120
  try {
75
121
  names = readdirSync(dir);
@@ -89,7 +135,7 @@ function readDir(dir) {
89
135
  }
90
136
  }
91
137
  files.sort((left, right) => right.id.localeCompare(left.id));
92
- return files.slice(0, MAX_FILES);
138
+ return files.slice(0, maxFiles > 0 ? maxFiles : 4000);
93
139
  }
94
140
 
95
141
  function revisionOf(files) {
@@ -126,7 +172,8 @@ function excerptAround(text, tokens, limit) {
126
172
  }
127
173
 
128
174
  function createJournalSource(config = {}) {
129
- const dir = typeof config.dir === "string" && config.dir !== "" ? config.dir : DEFAULT_DIR;
175
+ const cfg = resolveConfig(config);
176
+ const dir = cfg.transcriptDir;
130
177
 
131
178
  return defineMemorySource({
132
179
  manifest: {
@@ -153,13 +200,13 @@ function createJournalSource(config = {}) {
153
200
  },
154
201
  maxCalls: 4,
155
202
  maxResults: 20,
156
- maxCharacters: 16000,
203
+ maxCharacters: cfg.maxRouteChars,
157
204
  },
158
205
  ],
159
206
  actions: [],
160
207
  },
161
208
  create(context) {
162
- const files = () => readDir(dir);
209
+ const files = () => readDir(dir, cfg.maxFiles);
163
210
 
164
211
  return {
165
212
  facts() {
@@ -211,7 +258,7 @@ function createJournalSource(config = {}) {
211
258
  const input = request.input ?? {};
212
259
  const query = typeof input.query === "string" ? input.query.trim() : "";
213
260
  const limitValue = Number(input.limit);
214
- const limit = Number.isFinite(limitValue) && limitValue > 0 ? Math.min(20, Math.floor(limitValue)) : 10;
261
+ const limit = Number.isFinite(limitValue) && limitValue > 0 ? Math.min(cfg.searchMaxLimit, Math.floor(limitValue)) : cfg.searchDefaultLimit;
215
262
  const tokens = tokensOf(query);
216
263
  const current = files();
217
264
 
@@ -238,7 +285,7 @@ function createJournalSource(config = {}) {
238
285
  .sort((left, right) => right.score - left.score || right.file.id.localeCompare(left.file.id))
239
286
  .slice(0, limit);
240
287
 
241
- let remaining = request.route.maxCharacters ?? 16000;
288
+ let remaining = request.route.maxCharacters ?? cfg.maxRouteChars;
242
289
  const items = [];
243
290
  let truncated = scored.length > limit;
244
291
  for (const row of ranked) {
@@ -247,7 +294,7 @@ function createJournalSource(config = {}) {
247
294
  break;
248
295
  }
249
296
  const raw = bodyOf(row.file);
250
- const budget = Math.min(MAX_ITEM_CHARS, remaining);
297
+ const budget = Math.min(cfg.maxItemChars, remaining);
251
298
  const text = excerptAround(raw, tokens, budget);
252
299
  remaining -= text.length;
253
300
  items.push({
@@ -317,4 +364,4 @@ function apply(ctx, config = {}) {
317
364
  );
318
365
  }
319
366
 
320
- export { apply, createJournalSource, inject, memoryPlugin, name };
367
+ export { apply, createJournalSource, resolveConfig, inject, memoryPlugin, name };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mnemon-source-journal",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Event-driven conversation journal for mnemon. Appends every turn to a dated transcript so nothing is lost between compactions.",
5
5
  "type": "module",
6
6
  "engines": {