dsh-mnemon-provider-weknora 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 +29 -7
  2. package/lib/index.js +60 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -34,18 +34,40 @@ pnpm add dsh-mnemon-provider-weknora
34
34
 
35
35
  ## 配置
36
36
 
37
- 在 DSH 记忆系统侧边栏的「记忆空间」里保存,或直接写 `~/.mnemon/state/memory-providers.json`:
37
+ > **日常不建议改这里。** 这些项都有默认值,要改就改统一配置文件 `~/.mnemon/state/weknora-suite.json` 里 `packages.<包名>.values` —— 那份文件带中文说明、带出厂值可回滚,AI 也能直接改。写在 `cordis.patch.yml` 里会盖住它,只在「这个 profile 要跟别人不一样」时才用。
38
38
 
39
- | 字段 key | 说明 | 默认 |
39
+ 分两处:**连接**属于每个记忆空间,**行为参数**属于这个 provider 行。
40
+
41
+ ### 连接(写在 body 的 connection 里)
42
+
43
+ 在记忆空间页保存,或直接写 `~/.mnemon/state/memory-providers.json`:
44
+
45
+ | 字段 | 说明 | 必需 |
40
46
  |---|---|---|
41
- | `endpoint` | WeKnora **API** 地址(不是前端地址),例如 `http://localhost:8080` | 无 |
42
- | `apiKey` | 请求头 `X-API-Key` 的值 | 无 |
43
- | `knowledgeBaseId` | 一个知识库 ID,或用逗号分隔多个。每个库变成一个记忆空间 | 无 |
44
- | `wikiPages` | 每次召回带几篇 wiki 结论页 | 3 |
45
- | `memoryItems` | 每次召回带几条一句话结论 | 0 |
47
+ | `endpoint` | WeKnora **API** 地址(不是前端地址),如 `http://127.0.0.1:8080` | 是 |
48
+ | `apiKey` | 请求头 `X-API-Key` 的值 | 是 |
49
+ | `knowledgeBaseId` | 库 uuid;逗号分隔可写多个 | 是 |
50
+ | `wikiPages` | 每次召回带几篇 wiki 结论页 | 否,默认 `"2"` |
51
+ | `memoryItems` | 每次召回带几条一句话结论 | 否,默认 `"0"` |
46
52
 
47
53
  三个知识库的分工建议:一个存结论(开 wiki)、一个存过程原文(关 wiki)、一个存外部素材。
48
54
 
55
+ ### 行为参数(写在 patch 里)
56
+
57
+ ```yaml
58
+ - use: dsh-mnemon-provider-weknora
59
+ instanceId: weknora
60
+ config:
61
+ requestTimeoutMs: 20000 # 到维克纳的请求超时(也接受 timeoutMs)
62
+ searchDefaultLimit: 50 # 检索默认条数
63
+ searchMaxLimit: 200 # 检索条数上限
64
+ graphLimit: 500 # 图谱一次拉多少
65
+ maxWriteChars: 2000 # 单条记忆写入上限
66
+ discoverWikiPages: "2" # 新发现的库默认每次取几页 wiki
67
+ discoverMemoryItems: "0" # 同上,记忆条目
68
+ ```
69
+
70
+
49
71
  ## 能力声明
50
72
 
51
73
  这个 provider 对 mnemon 声明的是:
package/lib/index.js CHANGED
@@ -9,8 +9,22 @@ import {
9
9
  jsonObject,
10
10
  jsonString,
11
11
  } from "dsh-mnemon-source-memory-spaces/provider-sdk";
12
+ import { readFileSync } from "node:fs";
13
+ import { homedir } from "node:os";
14
+ import { join } from "node:path";
12
15
  import { descriptor } from "./descriptor.js";
13
16
 
17
+ /** 统一配置文件 ~/.mnemon/state/weknora-suite.json 里本包那一段;没有或读坏就返回空对象。 */
18
+ function suiteSection(pkgName) {
19
+ try {
20
+ const raw = readFileSync(join(homedir(), ".mnemon", "state", "weknora-suite.json"), "utf8");
21
+ const values = JSON.parse(raw)?.packages?.[pkgName]?.values;
22
+ return values && typeof values === "object" ? values : {};
23
+ } catch {
24
+ return {};
25
+ }
26
+ }
27
+
14
28
  const API_PREFIX = "/api/v1";
15
29
 
16
30
  /** WeKnora answers `{success, data}`; older routes sometimes answer the array itself. */
@@ -168,6 +182,43 @@ function connectionOf(connection) {
168
182
  };
169
183
  }
170
184
 
185
+ /** 默认值:都能被 row 的 config 覆盖。 */
186
+ const DEFAULTS = {
187
+ /** 单次 HTTP 请求到维克纳的超时。 */
188
+ requestTimeoutMs: 20000,
189
+ /** 检索时没传 limit 的默认条数。 */
190
+ searchDefaultLimit: 50,
191
+ /** 检索条数上限(服务端一次最多拉这么多)。 */
192
+ searchMaxLimit: 200,
193
+ /** 图谱一次拉多少节点/边。 */
194
+ graphLimit: 500,
195
+ /** remember 写入内容的长度上限。 */
196
+ maxWriteChars: 2000,
197
+ /** discover 时给每个库填的默认「每次召回取几页 wiki」。 */
198
+ discoverWikiPages: "2",
199
+ /** discover 时给每个库填的默认「每次召回取几条记忆条目」。 */
200
+ discoverMemoryItems: "0",
201
+ };
202
+
203
+ const numOpt = (value, fallback) => {
204
+ const parsed = typeof value === "string" ? Number(value.trim()) : value;
205
+ return typeof parsed === "number" && Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
206
+ };
207
+
208
+ function resolveOptions(options = {}) {
209
+ const o = { ...suiteSection("dsh-mnemon-provider-weknora"), ...(options ?? {}) };
210
+ return {
211
+ ...o,
212
+ requestTimeoutMs: numOpt(o.requestTimeoutMs ?? o.timeoutMs, DEFAULTS.requestTimeoutMs),
213
+ searchDefaultLimit: numOpt(o.searchDefaultLimit, DEFAULTS.searchDefaultLimit),
214
+ searchMaxLimit: numOpt(o.searchMaxLimit, DEFAULTS.searchMaxLimit),
215
+ graphLimit: numOpt(o.graphLimit, DEFAULTS.graphLimit),
216
+ maxWriteChars: numOpt(o.maxWriteChars, DEFAULTS.maxWriteChars),
217
+ discoverWikiPages: typeof o.discoverWikiPages === "string" ? o.discoverWikiPages : DEFAULTS.discoverWikiPages,
218
+ discoverMemoryItems: typeof o.discoverMemoryItems === "string" ? o.discoverMemoryItems : DEFAULTS.discoverMemoryItems,
219
+ };
220
+ }
221
+
171
222
  var WeKnoraProvider = class extends HttpMemoryProvider {
172
223
  id = "weknora";
173
224
  scoreSemantics = Object.freeze({ kind: "provider-native" });
@@ -175,7 +226,9 @@ var WeKnoraProvider = class extends HttpMemoryProvider {
175
226
  conclusionKbId = "";
176
227
 
177
228
  constructor(memorySpaces, options = {}) {
178
- super(memorySpaces, { label: descriptor.label, ...options });
229
+ const resolved = resolveOptions(options);
230
+ super(memorySpaces, { label: descriptor.label, ...resolved });
231
+ this.opt = resolved;
179
232
  }
180
233
 
181
234
  async discover(connection, signal) {
@@ -192,7 +245,7 @@ var WeKnoraProvider = class extends HttpMemoryProvider {
192
245
  const name = textOf(item.name) || id;
193
246
  const description = textOf(item.description) || `WeKnora 知识库 ${name}`;
194
247
  const wiki = jsonObject(item.capabilities)?.wiki === true;
195
- return [{ externalId: id, name, description, wiki, connection: { knowledgeBaseId: id, wikiPages: "2", memoryItems: "0" } }];
248
+ return [{ externalId: id, name, description, wiki, connection: { knowledgeBaseId: id, wikiPages: this.opt.discoverWikiPages, memoryItems: this.opt.discoverMemoryItems } }];
196
249
  });
197
250
  if (this.conclusionKbId === "") {
198
251
  const owner = rows.find((row) => row.wiki === true) ?? rows[0];
@@ -256,7 +309,7 @@ var WeKnoraProvider = class extends HttpMemoryProvider {
256
309
  if (connection.memoryItems > 0 && isConclusionKb) {
257
310
  legNames.push("memory");
258
311
  legs.push(
259
- this.request(body, `${API_PREFIX}/memory/items?limit=200`, { headers: connection.headers, signal }).then(
312
+ this.request(body, `${API_PREFIX}/memory/items?limit=${this.opt.searchMaxLimit}`, { headers: connection.headers, signal }).then(
260
313
  (payload) => {
261
314
  const scored = payloadOf(payload).flatMap((value) => {
262
315
  const item = jsonObject(value);
@@ -357,7 +410,7 @@ var WeKnoraProvider = class extends HttpMemoryProvider {
357
410
  const selfKb = String(raw?.knowledgeBaseId ?? "").trim();
358
411
  const isConclusionKb = selfKb === "" || selfKb === (this.conclusionKbId || connection.kbIds[0]);
359
412
  if (!isConclusionKb) return [];
360
- const limit = Math.min(Math.max(request.limit ?? 50, 1), 200);
413
+ const limit = Math.min(Math.max(request.limit ?? this.opt.searchDefaultLimit, 1), this.opt.searchMaxLimit);
361
414
  const payload = await this.request(body, `${API_PREFIX}/memory/items?limit=${limit}`, {
362
415
  headers: connection.headers,
363
416
  signal,
@@ -387,7 +440,7 @@ var WeKnoraProvider = class extends HttpMemoryProvider {
387
440
  payload = jsonObject(
388
441
  await this.request(
389
442
  body,
390
- `${API_PREFIX}/knowledgebase/${encodeURIComponent(connection.kbIds[0])}/wiki/graph?limit=500`,
443
+ `${API_PREFIX}/knowledgebase/${encodeURIComponent(connection.kbIds[0])}/wiki/graph?limit=${this.opt.graphLimit}`,
391
444
  { headers: connection.headers, signal },
392
445
  ),
393
446
  );
@@ -440,7 +493,7 @@ var WeKnoraProvider = class extends HttpMemoryProvider {
440
493
 
441
494
  async remember(body, request, signal) {
442
495
  const connection = connectionOf(ownConnection(this.connection(body), body));
443
- const content = String(request.content ?? "").trim().slice(0, 2000);
496
+ const content = String(request.content ?? "").trim().slice(0, this.opt.maxWriteChars);
444
497
  if (content === "") return { action: "skipped", provider: this.id, summary: "内容为空,未写入" };
445
498
  const kind = textOf(request.category) === "" ? "fact" : textOf(request.category);
446
499
  const importance =
@@ -494,7 +547,7 @@ const definition = defineMemorySpaceProviderDefinition({
494
547
  },
495
548
  create: (context) =>
496
549
  new WeKnoraProvider(context.memorySpaces ?? context.memoryBodies, {
497
- requestTimeoutMs: context.config.timeoutMs,
550
+ ...(context.config ?? {}),
498
551
  }),
499
552
  });
500
553
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mnemon-provider-weknora",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "WeKnora (self-hosted knowledge base) as a mnemon Memory Space provider: hybrid search, wiki pages and native memory items in one store.",
5
5
  "type": "module",
6
6
  "license": "MIT",