dsh-weknora-panel 0.1.1 → 0.1.3

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
@@ -34,23 +34,34 @@ pnpm add dsh-weknora-panel
34
34
 
35
35
  ## 配置
36
36
 
37
- 在 `cordis.patch.yml` 里:
37
+ > **日常不建议改这里。** 这些项都有默认值,要改就改统一配置文件 `~/.mnemon/state/weknora-suite.json` 里 `packages.<包名>.values` —— 那份文件带中文说明、带出厂值可回滚,AI 也能直接改。写在 `cordis.patch.yml` 里会盖住它,只在「这个 profile 要跟别人不一样」时才用。
38
38
 
39
39
  ```yaml
40
40
  - id: weknora-panel
41
+ name: "dsh-weknora-panel"
41
42
  config:
42
43
  weknoraProxy:
43
- port: 8099
44
- target: http://127.0.0.1:80
44
+ enabled: true
45
+ port: 8099 # 反代监听端口
46
+ host: "127.0.0.1"
47
+ target: "http://127.0.0.1:80" # 维克纳前端地址
45
48
  login:
46
- path: /api/v1/auth/login
47
- password: "改成你自己的"
49
+ path: "/api/v1/auth/login"
50
+ password: "" # 空 = 透明反代;填了就由服务端替你登录
51
+ transcriptDir: "" # 账本目录,留空 = ~/.mnemon/transcripts
52
+ maxReadBytes: 409600 # 账本读写上限
53
+ memoryListLimit: 200 # 记忆条目一次列多少
54
+ panelTitle: "记忆"
55
+ panelNote: "" # 留空自动显示「本地反代 host:port」
48
56
  ```
49
57
 
50
58
  - `port`:反代监听端口,默认 8099
51
59
  - `target`:要嵌的地址,默认 `http://127.0.0.1:80`(WeKnora 前端)
52
- - `login`:可不填。填了才做服务端登录注入;不填就是纯透明反代
53
- - `login.password`:目标站点的登录密码。**明文存在配置文件里**,本机用没问题,多机部署要考虑换成环境变量
60
+ - `login.password`:留空 = 纯透明反代(上游不需要密码,或让用户自己在上游登录)。**填了是明文存在配置文件里**
61
+ - `panelTitle` / `panelNote`:侧栏页签名与那行小字
62
+
63
+ 反代地址**没有写死在客户端**:页面启动时向 `/api/weknora-panel/config` 取一次,所以改 `port` 只要重启,iframe 会自己跟上。
64
+
54
65
 
55
66
  ## 安全
56
67
 
package/lib/client.js CHANGED
@@ -24,7 +24,9 @@ window.__ModuleLoader__.load({
24
24
  const React = require("react");
25
25
  const el = React.createElement;
26
26
 
27
- const FRAME_URL = "http://127.0.0.1:8099/";
27
+ /** 反代地址由服务端下发(/api/weknora-panel/config),这里只是它没答上来时的兜底。 */
28
+ const DEFAULT_FRAME_URL = "http://127.0.0.1:8099/";
29
+ const CONFIG_API = "/api/weknora-panel/config";
28
30
  const JOURNAL_API = "/api/weknora-panel/journal";
29
31
  const MEMORY_API = "/api/weknora-panel/memory";
30
32
 
@@ -76,18 +78,31 @@ window.__ModuleLoader__.load({
76
78
  class WeKnoraPage extends React.Component {
77
79
  constructor(props) {
78
80
  super(props);
79
- this.state = { nonce: 0 };
81
+ this.state = { nonce: 0, frameUrl: DEFAULT_FRAME_URL, note: "" };
80
82
  this.reload = this.reload.bind(this);
81
83
  this.openWindow = this.openWindow.bind(this);
82
84
  }
83
85
 
86
+ componentDidMount() {
87
+ fetch(CONFIG_API)
88
+ .then((response) => response.json())
89
+ .then((data) => {
90
+ if (!data || data.ok !== true) return;
91
+ this.setState({
92
+ frameUrl: typeof data.proxyUrl === "string" && data.proxyUrl !== "" ? data.proxyUrl : DEFAULT_FRAME_URL,
93
+ note: typeof data.note === "string" ? data.note : "",
94
+ });
95
+ })
96
+ .catch(() => {});
97
+ }
98
+
84
99
  reload() {
85
100
  this.setState({ nonce: this.state.nonce + 1 });
86
101
  }
87
102
 
88
103
  openWindow() {
89
104
  try {
90
- window.open(FRAME_URL, "_blank", "noopener");
105
+ window.open(this.state.frameUrl, "_blank", "noopener");
91
106
  } catch {}
92
107
  }
93
108
 
@@ -99,7 +114,7 @@ window.__ModuleLoader__.load({
99
114
  "div",
100
115
  { className: "wkp-bar" },
101
116
  el("span", { className: "wkp-title" }, "记忆"),
102
- el("span", { className: "wkp-note" }, "MemOS 本地 127.0.0.1:18801 · 世界模型 / 轨迹 / 策略都在这里"),
117
+ this.state.note === "" ? null : el("span", { className: "wkp-note" }, this.state.note),
103
118
  el(
104
119
  "span",
105
120
  { className: "wkp-tools" },
@@ -107,7 +122,7 @@ window.__ModuleLoader__.load({
107
122
  el("button", { className: "wkp-btn", onClick: this.openWindow }, "新窗口"),
108
123
  ),
109
124
  ),
110
- el("iframe", { key: this.state.nonce, className: "wkp-frame", src: FRAME_URL, title: "WeKnora" }),
125
+ el("iframe", { key: this.state.nonce, className: "wkp-frame", src: this.state.frameUrl, title: "WeKnora" }),
111
126
  );
112
127
  }
113
128
  }
package/lib/index.js CHANGED
@@ -11,11 +11,31 @@
11
11
  * lib/routes.js; the visible tabs live in lib/client.js on the client plane.
12
12
  */
13
13
  import { PROXY_DEFAULTS, startWeKnoraProxy } from "./proxy.js";
14
+ import { readFileSync } from "node:fs";
15
+ import { homedir } from "node:os";
16
+ import { join } from "node:path";
14
17
  import { panelRoutes } from "./routes.js";
15
18
 
19
+ /** 统一配置文件 ~/.mnemon/state/weknora-suite.json 里本包那一段;没有或读坏就返回空对象。 */
20
+ function suiteSection(pkgName) {
21
+ try {
22
+ const raw = readFileSync(join(homedir(), ".mnemon", "state", "weknora-suite.json"), "utf8");
23
+ const values = JSON.parse(raw)?.packages?.[pkgName]?.values;
24
+ return values && typeof values === "object" ? values : {};
25
+ } catch {
26
+ return {};
27
+ }
28
+ }
29
+
16
30
  export const name = "dsh-weknora-panel";
17
31
 
18
32
  export function apply(ctx, config = {}) {
33
+ const suite = suiteSection("dsh-weknora-panel");
34
+ config = {
35
+ ...suite,
36
+ ...config,
37
+ weknoraProxy: { ...(suite.weknoraProxy ?? {}), ...(config?.weknoraProxy ?? {}) },
38
+ };
19
39
  const log =
20
40
  typeof ctx?.logger === "function"
21
41
  ? (line) => {
@@ -41,7 +61,16 @@ export function apply(ctx, config = {}) {
41
61
  const disposers = [];
42
62
  const mount = (scope) => {
43
63
  try {
44
- for (const route of panelRoutes({})) disposers.push(scope.connection.fetch.register(route));
64
+ for (const route of panelRoutes({
65
+ transcriptDir: config?.transcriptDir,
66
+ maxReadBytes: config?.maxReadBytes,
67
+ memoryListLimit: config?.memoryListLimit,
68
+ proxyUrl: proxy?.port ? "http://" + (settings.weknoraProxy.host ?? "127.0.0.1") + ":" + proxy.port + "/" : "",
69
+ proxyTarget: settings.weknoraProxy.target,
70
+ panelTitle: config?.panelTitle,
71
+ panelNote: config?.panelNote,
72
+ }))
73
+ disposers.push(scope.connection.fetch.register(route));
45
74
  log?.("panel routes mounted (" + disposers.length + ")");
46
75
  } catch (error) {
47
76
  log?.("panel routes failed: " + String(error?.message ?? error));
package/lib/proxy.js CHANGED
@@ -21,17 +21,18 @@ export const PROXY_DEFAULTS = {
21
21
  enabled: true,
22
22
  port: 8099,
23
23
  host: "127.0.0.1",
24
- /** 目标:默认指向 MemOS 的 Memory Viewer(登录过的 WeKnora 前端也可以)。 */
24
+ /** 目标:维克纳的前端地址。默认本机 80,指向别处就改这里。 */
25
25
  target: "http://127.0.0.1:80",
26
26
  /**
27
27
  * 上游要登录时,由反代替浏览器持有会话。
28
28
  *
29
- * 为什么必须这样做:MemOS 的 Memory Viewer 登录后发的 cookie 是 `SameSite=Lax`,
30
- * 而侧边栏是 iframe —— 顶层是 GUI 的端口、iframe 是 8099,浏览器把这次写 cookie
31
- * 当成跨站请求直接丢掉,于是页面永远停在“解锁”按钮上转圈。把登录搬到服务端,
32
- * iframe 里就不再需要任何 cookie。密码留空则退化成透明反代(不许密码的上游)。
29
+ * 为什么要搬到服务端:上游登录后发的 cookie 是 `SameSite=Lax`,而侧边栏是 iframe
30
+ * (顶层是 GUI 的端口、iframe 是反代端口),浏览器把这次写 cookie 当成跨站请求丢掉,
31
+ * 页面就永远停在“解锁”按钮上转圈。登录放服务端,iframe 里不再需要任何 cookie。
32
+ *
33
+ * password 留空 = 透明反代(上游不需要密码,或由用户自己在上游页面登录)。
33
34
  */
34
- login: { path: "/api/v1/auth/login", password: "123456" },
35
+ login: { path: "/api/v1/auth/login", password: "" },
35
36
  };
36
37
 
37
38
  /** 这些响应头会让 iframe 被浏览器拒绝,转发时直接丢掉。 */
package/lib/routes.js CHANGED
@@ -14,12 +14,16 @@
14
14
  import { existsSync, readdirSync, readFileSync, statSync, unlinkSync, writeFileSync } from "node:fs";
15
15
  import { homedir } from "node:os";
16
16
  import { join, resolve, sep } from "node:path";
17
+ import { applyUpdates, describeSuite, ensureSuite, readSuite, resetPackage, suitePath, writeSuite } from "./suite.js";
17
18
 
18
19
  export const PANEL_API_PREFIX = "/api/weknora-panel";
19
20
  export const JOURNAL_ROUTE = PANEL_API_PREFIX + "/journal";
20
21
  export const MEMORY_ROUTE = PANEL_API_PREFIX + "/memory";
22
+ export const CONFIG_ROUTE = PANEL_API_PREFIX + "/config";
23
+ export const SETTINGS_ROUTE = PANEL_API_PREFIX + "/settings";
21
24
 
22
25
  const NAME_PATTERN = /^\d{4}-\d{2}-\d{2}(-[0-9a-zA-Z]{1,16})?\.md$/u;
26
+ /** 读写单个账本文件的默认字节上限,可由 config 覆盖。 */
23
27
  const MAX_READ_BYTES = 400 * 1024;
24
28
 
25
29
  function json(status, body) {
@@ -109,6 +113,8 @@ async function callWeKnora(service, method, path, body) {
109
113
 
110
114
  export function journalRoutes(options = {}) {
111
115
  const dir = options.transcriptDir ?? transcriptDirOf();
116
+ const maxReadBytes =
117
+ typeof options.maxReadBytes === "number" && options.maxReadBytes > 0 ? options.maxReadBytes : MAX_READ_BYTES;
112
118
 
113
119
  return [
114
120
  {
@@ -135,14 +141,14 @@ export function journalRoutes(options = {}) {
135
141
  if (op === "read") {
136
142
  if (!existsSync(full)) return json(404, { ok: false, error: "not_found" });
137
143
  const stat = statSync(full);
138
- if (stat.size > MAX_READ_BYTES) return json(413, { ok: false, error: "too_large" });
144
+ if (stat.size > maxReadBytes) return json(413, { ok: false, error: "too_large" });
139
145
  return json(200, { ok: true, name: body.name, content: readFileSync(full, "utf8") });
140
146
  }
141
147
 
142
148
  if (op === "save") {
143
149
  const content = typeof body?.content === "string" ? body.content : undefined;
144
150
  if (content === undefined) return json(400, { ok: false, error: "bad_content" });
145
- if (Buffer.byteLength(content) > MAX_READ_BYTES) return json(413, { ok: false, error: "too_large" });
151
+ if (Buffer.byteLength(content) > maxReadBytes) return json(413, { ok: false, error: "too_large" });
146
152
  writeFileSync(full, content, "utf8");
147
153
  return json(200, { ok: true, name: body.name, bytes: Buffer.byteLength(content) });
148
154
  }
@@ -159,7 +165,9 @@ export function journalRoutes(options = {}) {
159
165
  ];
160
166
  }
161
167
 
162
- export function memoryRoutes() {
168
+ export function memoryRoutes(options = {}) {
169
+ const listLimit =
170
+ typeof options.memoryListLimit === "number" && options.memoryListLimit > 0 ? options.memoryListLimit : 200;
163
171
  return [
164
172
  {
165
173
  path: MEMORY_ROUTE,
@@ -168,7 +176,7 @@ export function memoryRoutes() {
168
176
  fetch: async () => {
169
177
  const service = memoryService();
170
178
  if (service === undefined) return json(200, { ok: false, error: "no_provider", items: [] });
171
- const list = await callWeKnora(service, "GET", "/api/v1/memory/items?limit=200");
179
+ const list = await callWeKnora(service, "GET", `/api/v1/memory/items?limit=${listLimit}`);
172
180
  if (list.ok === false) return json(200, { ok: false, error: list.error, items: [] });
173
181
  const rows = Array.isArray(list.payload?.data) ? list.payload.data : [];
174
182
  const items = rows
@@ -221,6 +229,99 @@ export function memoryRoutes() {
221
229
  ];
222
230
  }
223
231
 
232
+ /**
233
+ * 设置页的后端:读一份统一配置文件给前端渲染表单,写回时只认已经存在的键。
234
+ * 不碰 cordis.patch.yml —— 那是 DSH 的启动配置,改错了整个 profile 起不来;
235
+ * 这里改的是插件自己的那一份,patch 里若也写了同名项,仍然是 patch 优先。
236
+ */
237
+ export function settingsRoutes() {
238
+ return [
239
+ {
240
+ path: SETTINGS_ROUTE,
241
+ methods: ["GET"],
242
+ requestBody: "buffered",
243
+ fetch: () => {
244
+ ensureSuite();
245
+ const file = suitePath();
246
+ const suite = readSuite();
247
+ if (!suite) {
248
+ const present = existsSync(file);
249
+ return Promise.resolve(
250
+ json(200, {
251
+ ok: false,
252
+ file,
253
+ exists: present,
254
+ error: present ? "unreadable" : "missing",
255
+ packages: [],
256
+ }),
257
+ );
258
+ }
259
+ return Promise.resolve(json(200, { ok: true, file, exists: true, packages: describeSuite(suite) }));
260
+ },
261
+ },
262
+ {
263
+ path: SETTINGS_ROUTE,
264
+ methods: ["POST"],
265
+ requestBody: "buffered",
266
+ fetch: async (request) => {
267
+ let body;
268
+ try {
269
+ body = await request.json();
270
+ } catch {
271
+ return json(400, { ok: false, error: "bad_json" });
272
+ }
273
+ const suite = readSuite();
274
+ if (!suite) return json(404, { ok: false, error: "suite_missing", file: suitePath() });
275
+
276
+ if (String(body?.op ?? "update") === "reset") {
277
+ const names = Array.isArray(body?.packages) ? body.packages : [body?.package];
278
+ const done = [];
279
+ for (const name of names) if (resetPackage(suite, String(name))) done.push(String(name));
280
+ const saved = writeSuite(suite);
281
+ if (!saved.ok) return json(500, { ok: false, error: saved.error });
282
+ return json(200, { ok: true, reset: done, packages: describeSuite(suite) });
283
+ }
284
+
285
+ const result = applyUpdates(suite, body?.updates ?? {});
286
+ if (result.applied.length === 0) {
287
+ return json(400, { ok: false, error: "nothing_applied", ignored: result.ignored });
288
+ }
289
+ const saved = writeSuite(suite);
290
+ if (!saved.ok) return json(500, { ok: false, error: saved.error });
291
+ return json(200, {
292
+ ok: true,
293
+ applied: result.applied,
294
+ ignored: result.ignored,
295
+ packages: describeSuite(suite),
296
+ });
297
+ },
298
+ },
299
+ ];
300
+ }
301
+
224
302
  export function panelRoutes(options = {}) {
225
- return [...journalRoutes(options), ...memoryRoutes()];
303
+ const configRoutes = [
304
+ {
305
+ path: CONFIG_ROUTE,
306
+ methods: ["GET"],
307
+ requestBody: "buffered",
308
+ fetch: () =>
309
+ Promise.resolve(
310
+ json(200, {
311
+ ok: true,
312
+ proxyUrl: typeof options.proxyUrl === "string" ? options.proxyUrl : "",
313
+ proxyTarget: typeof options.proxyTarget === "string" ? options.proxyTarget : "",
314
+ transcriptDir: options.transcriptDir ?? transcriptDirOf(),
315
+ title: typeof options.panelTitle === "string" ? options.panelTitle : "记忆",
316
+ note:
317
+ typeof options.panelNote === "string" && options.panelNote !== ""
318
+ ? options.panelNote
319
+ : typeof options.proxyUrl === "string" && options.proxyUrl !== ""
320
+ ? "本地反代 " + options.proxyUrl.replace(/^https?:\/\//u, "").replace(/\/$/u, "")
321
+ : "",
322
+ }),
323
+ ),
324
+ },
325
+ ];
326
+ return [...configRoutes, ...settingsRoutes(), ...journalRoutes(options), ...memoryRoutes(options)];
226
327
  }
package/lib/suite.js ADDED
@@ -0,0 +1,174 @@
1
+ /**
2
+ * 统一配置文件的读写层。
3
+ *
4
+ * 配置文件 `~/.mnemon/state/weknora-suite.json` 是这套插件的真相源:五个包启动时各读自己那一段,
5
+ * 面板上的「设置」页签只是它的图形编辑器。人在编辑器里改、AI 直接改文件、或者只留文件不用面板,
6
+ * 都走同一份数据,不会分叉。
7
+ *
8
+ * 这个模块只碰 `packages.<包名>.values` 与 `.defaults` 两段,别的一概原样保留,
9
+ * 免得把写在同一个文件里的说明文字改掉。
10
+ */
11
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
12
+ import { homedir } from "node:os";
13
+ import { dirname, join } from "node:path";
14
+
15
+ export function suitePath() {
16
+ return join(homedir(), ".mnemon", "state", "weknora-suite.json");
17
+ }
18
+
19
+ /**
20
+ * 第一次用的时候把这个包自带的模板复制过去,省得用户面对一个空文件。
21
+ * 模板就是一份填好默认值与说明的配置,和亲手建一个没有区别。
22
+ */
23
+ export function ensureSuite() {
24
+ const file = suitePath();
25
+ if (existsSync(file)) return true;
26
+ try {
27
+ const template = readFileSync(new URL("../weknora-suite.example.json", import.meta.url), "utf8");
28
+ mkdirSync(dirname(file), { recursive: true });
29
+ writeFileSync(file, template, "utf8");
30
+ return true;
31
+ } catch {
32
+ return false;
33
+ }
34
+ }
35
+
36
+ /** 读整份配置;不存在或解析失败返回 undefined(调用方据此提示"配置文件不在")。 */
37
+ export function readSuite() {
38
+ const file = suitePath();
39
+ if (!existsSync(file)) return undefined;
40
+ try {
41
+ const parsed = JSON.parse(readFileSync(file, "utf8"));
42
+ return parsed && typeof parsed === "object" ? parsed : undefined;
43
+ } catch {
44
+ return undefined;
45
+ }
46
+ }
47
+
48
+ /** 写回:先留一份 .bak-时间戳,再整体覆盖。只允许调用方传回 readSuite 的原结构。 */
49
+ export function writeSuite(suite) {
50
+ const file = suitePath();
51
+ try {
52
+ if (existsSync(file)) copyFileSync(file, file + ".bak-" + Date.now());
53
+ mkdirSync(dirname(file), { recursive: true });
54
+ writeFileSync(file, JSON.stringify(suite, null, 2) + "\n", "utf8");
55
+ return { ok: true, file };
56
+ } catch (error) {
57
+ return { ok: false, file, error: String(error?.message ?? error) };
58
+ }
59
+ }
60
+
61
+ function typeOf(value) {
62
+ if (typeof value === "boolean") return "boolean";
63
+ if (typeof value === "number") return "number";
64
+ return "string";
65
+ }
66
+
67
+ /** 把嵌套的 values 摊平成 a.b.c 形式的字段列表,交给前端一个通用表单渲染。 */
68
+ export function flattenValues(values, docs, prefix = "") {
69
+ const out = [];
70
+ for (const [key, value] of Object.entries(values ?? {})) {
71
+ const path = prefix === "" ? key : prefix + "." + key;
72
+ if (value !== null && typeof value === "object" && !Array.isArray(value)) {
73
+ out.push(...flattenValues(value, docs, path));
74
+ continue;
75
+ }
76
+ const doc = docs && typeof docs === "object" ? docs[path] : undefined;
77
+ out.push({ path, type: typeOf(value), value, doc: typeof doc === "string" ? doc : "" });
78
+ }
79
+ return out;
80
+ }
81
+
82
+ /** 只认已经存在的 key,杜绝前端随手加一个新字段把配置撑坏。 */
83
+ function hasPath(target, path) {
84
+ const parts = String(path).split(".");
85
+ let cursor = target;
86
+ for (const part of parts) {
87
+ if (cursor === null || typeof cursor !== "object" || !(part in cursor)) return false;
88
+ cursor = cursor[part];
89
+ }
90
+ return true;
91
+ }
92
+
93
+ /** 按 a.b.c 路径写值,中间层不存在就建。 */
94
+ function setPath(target, path, value) {
95
+ const parts = String(path).split(".");
96
+ let cursor = target;
97
+ for (let i = 0; i < parts.length - 1; i += 1) {
98
+ if (cursor[parts[i]] === null || typeof cursor[parts[i]] !== "object") cursor[parts[i]] = {};
99
+ cursor = cursor[parts[i]];
100
+ }
101
+ cursor[parts[parts.length - 1]] = value;
102
+ }
103
+
104
+ function coerce(current, incoming) {
105
+ if (typeof current === "boolean") return incoming === true || incoming === "true";
106
+ if (typeof current === "number") {
107
+ const n = Number(incoming);
108
+ return Number.isFinite(n) ? n : current;
109
+ }
110
+ return typeof incoming === "string" ? incoming : String(incoming ?? "");
111
+ }
112
+
113
+ /**
114
+ * 把一批修改应用到 suite 里。updates 形如 { "包名": { "maxChars": 1200, "weknoraProxy.port": 8100 } }。
115
+ * 返回 { applied: [...], ignored: [...] },ignored 里是无法识别的路径,方便前端提示。
116
+ */
117
+ export function applyUpdates(suite, updates) {
118
+ const applied = [];
119
+ const ignored = [];
120
+ for (const [pkgName, patch] of Object.entries(updates ?? {})) {
121
+ const section = suite?.packages?.[pkgName];
122
+ if (!section || typeof patch !== "object" || patch === null) {
123
+ ignored.push(pkgName);
124
+ continue;
125
+ }
126
+ section.values = section.values ?? {};
127
+ for (const [path, incoming] of Object.entries(patch)) {
128
+ const known = hasPath(section.values, path) || hasPath(section.defaults, path);
129
+ if (!known) {
130
+ ignored.push(pkgName + "." + path);
131
+ continue;
132
+ }
133
+ const current = hasPath(section.values, path) ? valueAt(section.values, path) : valueAt(section.defaults, path);
134
+ setPath(section.values, path, coerce(current, incoming));
135
+ applied.push(pkgName + "." + path);
136
+ }
137
+ }
138
+ return { applied, ignored };
139
+ }
140
+
141
+ function valueAt(target, path) {
142
+ const parts = String(path).split(".");
143
+ let cursor = target;
144
+ for (const part of parts) {
145
+ if (cursor === null || typeof cursor !== "object") return undefined;
146
+ cursor = cursor[part];
147
+ }
148
+ return cursor;
149
+ }
150
+
151
+ /** 把一个包的 values 整段还原成 defaults 里的出厂值。 */
152
+ export function resetPackage(suite, pkgName) {
153
+ const section = suite?.packages?.[pkgName];
154
+ if (!section || typeof section.defaults !== "object" || section.defaults === null) return false;
155
+ section.values = JSON.parse(JSON.stringify(section.defaults));
156
+ return true;
157
+ }
158
+
159
+ /** 给前端的一份完整视图:每个包的说明、字段(当前值 + 出厂值 + 一句话解释)。 */
160
+ export function describeSuite(suite) {
161
+ const packages = [];
162
+ for (const [name, section] of Object.entries(suite?.packages ?? {})) {
163
+ const fields = flattenValues(section.values, section.docs);
164
+ const defaults = flattenValues(section.defaults, section.docs);
165
+ const defaultMap = new Map(defaults.map((f) => [f.path, f.value]));
166
+ for (const field of fields) field.default = defaultMap.has(field.path) ? defaultMap.get(field.path) : null;
167
+ packages.push({
168
+ name,
169
+ doc: typeof section._doc === "string" ? section._doc : "",
170
+ fields,
171
+ });
172
+ }
173
+ return packages;
174
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-weknora-panel",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Sidebar panel for browsing and editing WeKnora knowledge bases without leaving DSH.",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -34,7 +34,8 @@
34
34
  "lib",
35
35
  "README.md",
36
36
  "cordis.patch.yml",
37
- "package.json"
37
+ "package.json",
38
+ "weknora-suite.example.json"
38
39
  ],
39
40
  "author": "chenge",
40
41
  "engines": {
@@ -0,0 +1,254 @@
1
+ {
2
+ "version": 1,
3
+ "_what": "维克纳记忆套件的统一配置。五个包启动时各读自己那一段;改完重启 DSH 生效。",
4
+ "_priority": "优先级:profile 的 cordis.patch.yml 里的同名配置 > 本文件 > 包内置默认值。也就是说这里改了但 patch 里也写了同一项,patch 赢。",
5
+ "_hidden": "statePath、documentIndexPath、transcriptDir 三个路径若留空字符串,会自动指向 ~/.mnemon 下的对应位置;只有把 mnemon 装在别处才需要填。",
6
+ "_safe": "两个库 id(archiveKbId / digestKbId)留空时不归档也不蒸馏,只在 journal.log 里记一句原因。填错不会丢数据,只会写不进去。",
7
+ "packages": {
8
+ "dsh-weknora-recall": {
9
+ "_doc": "每轮说话前检索维克纳,把相关的几条塞进上下文。",
10
+ "values": {
11
+ "enabled": true,
12
+ "maxChars": 900,
13
+ "perSpaceItems": 2,
14
+ "fetchFactor": 2,
15
+ "perItemChars": 220,
16
+ "timeoutMs": 1500,
17
+ "providerTimeoutMs": 1200,
18
+ "minQueryChars": 4,
19
+ "knowledgeBodyName": "知识",
20
+ "preamble": "",
21
+ "archiveHeading": "",
22
+ "stateCacheMs": 5000,
23
+ "archive": {
24
+ "enabled": true,
25
+ "maxDocs": 2,
26
+ "itemChars": 320,
27
+ "matchWindow": 900,
28
+ "minScore": 2
29
+ },
30
+ "statePath": "",
31
+ "documentIndexPath": ""
32
+ },
33
+ "docs": {
34
+ "enabled": "总开关。关掉就完全不注入。",
35
+ "maxChars": "一轮最多注入多少字。越大记得越多、越费上下文。900 是平衡点。",
36
+ "perSpaceItems": "每个记忆空间最多取几条。",
37
+ "fetchFactor": "先多取几倍再截断,提高命中率。2 = 取 2 倍。",
38
+ "perItemChars": "每条保留多少字,超出截断。",
39
+ "timeoutMs": "整轮召回硬超时。超了就不注入,宁可这轮没记忆也不卡住对话。",
40
+ "providerTimeoutMs": "单次检索请求的超时,要比 timeoutMs 略小。",
41
+ "minQueryChars": "你说的话短于这个字数就不查(像「好」「嗯」查了也是浪费)。",
42
+ "knowledgeBodyName": "只查这个空间。留空 = 三个库都查。默认只查「知识」,因为结论都在那儿,比翻原文省得多。",
43
+ "preamble": "注入块的开头那段话。留空用内置文案。",
44
+ "archiveHeading": "「自动归档」那一节的标题。留空用内置。",
45
+ "stateCacheMs": "记忆空间列表缓存多久。0 = 每轮重读(改配置后不用等)。",
46
+ "archive.enabled": "是否连带本地账本里自动归档的文档一起找。",
47
+ "archive.maxDocs": "最多带几篇归档文档。",
48
+ "archive.itemChars": "每篇留多少字。",
49
+ "archive.matchWindow": "在文档标题+正文前多少字里找关键词。",
50
+ "archive.minScore": "打分达到几才算命中。",
51
+ "statePath": "记忆空间配置文件路径。留空 = ~/.mnemon/state/memory-providers.json。",
52
+ "documentIndexPath": "归档文档索引路径。留空 = ~/.mnemon/documents/index.json。"
53
+ },
54
+ "defaults": {
55
+ "enabled": true,
56
+ "maxChars": 900,
57
+ "perSpaceItems": 2,
58
+ "fetchFactor": 2,
59
+ "perItemChars": 220,
60
+ "timeoutMs": 1500,
61
+ "providerTimeoutMs": 1200,
62
+ "minQueryChars": 4,
63
+ "knowledgeBodyName": "知识",
64
+ "preamble": "",
65
+ "archiveHeading": "",
66
+ "stateCacheMs": 5000,
67
+ "archive": {
68
+ "enabled": true,
69
+ "maxDocs": 2,
70
+ "itemChars": 320,
71
+ "matchWindow": 900,
72
+ "minScore": 2
73
+ },
74
+ "statePath": "",
75
+ "documentIndexPath": ""
76
+ }
77
+ },
78
+ "dsh-weknora-journal": {
79
+ "_doc": "每轮把对话记进本地账本;过零点后把昨天的账本归档进维克纳,再蒸馏成知识页。没有定时任务,全靠事件驱动。",
80
+ "values": {
81
+ "archiveKbId": "",
82
+ "digestKbId": "",
83
+ "distillEnabled": true,
84
+ "distillMaxChars": 1200,
85
+ "distillPrompt": "",
86
+ "minArchiveBytes": 400,
87
+ "maxArchiveChars": 180000,
88
+ "maxTurnChars": 12000,
89
+ "archiveTitlePrefix": "会话记录 ",
90
+ "archiveTitleMaxChars": 200,
91
+ "brainDir": "",
92
+ "logMaxBytes": 262144,
93
+ "timeouts": {
94
+ "discover": 15000,
95
+ "sessionCreate": 30000,
96
+ "knowledgeChat": 240000,
97
+ "pageRead": 15000,
98
+ "pageWrite": 30000,
99
+ "archive": 20000
100
+ }
101
+ },
102
+ "docs": {
103
+ "archiveKbId": "必填。归档到哪个库 —— 填你维克纳里「会话记忆」那个库的 uuid。留空则不归档。",
104
+ "digestKbId": "必填。结论写进哪个库 —— 填「知识」库的 uuid,且这个库要开了 wiki。留空则不蒸馏。",
105
+ "distillEnabled": "关掉就只归档原文、不提炼结论。",
106
+ "distillMaxChars": "一天最多沉淀多少字。太多会稀释重点。",
107
+ "distillPrompt": "换整套蒸馏提示词。可用占位符 {day} {pages} {content}。留空用内置的那套四类白名单。",
108
+ "minArchiveBytes": "账本小于这个字节数就不建文档(闲聊太短不值得)。",
109
+ "maxArchiveChars": "单篇正文上限,超出截断,防止一天写太多把接口撑爆。",
110
+ "maxTurnChars": "单轮最多记多少字。",
111
+ "archiveTitlePrefix": "归档文档标题前缀,后面接日期和当天的第一个话题。",
112
+ "archiveTitleMaxChars": "标题总长上限。",
113
+ "brainDir": "mnemon 数据根目录。留空 = ~/.mnemon。改它可以把账本放到别的盘。",
114
+ "logMaxBytes": "journal.log 超过这个大小就滚动截断。",
115
+ "timeouts.discover": "列已有主题页的超时。",
116
+ "timeouts.sessionCreate": "建临时会话的超时。",
117
+ "timeouts.knowledgeChat": "跑蒸馏的超时 —— 大模型处理长文,给足。",
118
+ "timeouts.pageRead": "读 wiki 页的超时。",
119
+ "timeouts.pageWrite": "写 wiki 页的超时。",
120
+ "timeouts.archive": "归档一篇文档的超时。"
121
+ },
122
+ "defaults": {
123
+ "archiveKbId": "",
124
+ "digestKbId": "",
125
+ "distillEnabled": true,
126
+ "distillMaxChars": 1200,
127
+ "distillPrompt": "",
128
+ "minArchiveBytes": 400,
129
+ "maxArchiveChars": 180000,
130
+ "maxTurnChars": 12000,
131
+ "archiveTitlePrefix": "会话记录 ",
132
+ "archiveTitleMaxChars": 200,
133
+ "brainDir": "",
134
+ "logMaxBytes": 262144,
135
+ "timeouts": {
136
+ "discover": 15000,
137
+ "sessionCreate": 30000,
138
+ "knowledgeChat": 240000,
139
+ "pageRead": 15000,
140
+ "pageWrite": 30000,
141
+ "archive": 20000
142
+ }
143
+ }
144
+ },
145
+ "dsh-mnemon-source-journal": {
146
+ "_doc": "本地账本的只读索引。注意:它声明的是 episodic 角色,mnemon 默认的 default-three-tier 策略不接收它 —— 要用 general 策略才生效。这是上游设计,不是故障。",
147
+ "values": {
148
+ "maxItemChars": 2600,
149
+ "maxFiles": 4000,
150
+ "maxRouteChars": 16000,
151
+ "searchDefaultLimit": 10,
152
+ "searchMaxLimit": 20,
153
+ "transcriptDir": ""
154
+ },
155
+ "docs": {
156
+ "maxItemChars": "一条检索结果最多多少字。",
157
+ "maxFiles": "最多扫多少本账,防止目录太大拖慢。",
158
+ "maxRouteChars": "单次检索路线的字数预算。",
159
+ "searchDefaultLimit": "调用方没给条数时返回几条。",
160
+ "searchMaxLimit": "调用方给了条数时的上限。",
161
+ "transcriptDir": "账本目录。留空 = ~/.mnemon/transcripts。"
162
+ },
163
+ "defaults": {
164
+ "maxItemChars": 2600,
165
+ "maxFiles": 4000,
166
+ "maxRouteChars": 16000,
167
+ "searchDefaultLimit": 10,
168
+ "searchMaxLimit": 20,
169
+ "transcriptDir": ""
170
+ }
171
+ },
172
+ "dsh-mnemon-provider-weknora": {
173
+ "_doc": "把维克纳当成 mnemon 的一个记忆空间。连接信息属于每个空间(在记忆空间页填),这里只配行为。",
174
+ "values": {
175
+ "requestTimeoutMs": 20000,
176
+ "searchDefaultLimit": 50,
177
+ "searchMaxLimit": 200,
178
+ "graphLimit": 500,
179
+ "maxWriteChars": 2000,
180
+ "discoverWikiPages": "2",
181
+ "discoverMemoryItems": "0"
182
+ },
183
+ "docs": {
184
+ "requestTimeoutMs": "到维克纳的请求超时。局域网 20 秒够。",
185
+ "searchDefaultLimit": "检索默认条数。",
186
+ "searchMaxLimit": "检索条数上限。",
187
+ "graphLimit": "图谱一次拉多少节点。",
188
+ "maxWriteChars": "单条记忆写入的字数上限。",
189
+ "discoverWikiPages": "新发现一个库时,默认每次带几页 wiki 结论。",
190
+ "discoverMemoryItems": "新发现一个库时,默认带几条一句话结论。0 = 不带。"
191
+ },
192
+ "defaults": {
193
+ "requestTimeoutMs": 20000,
194
+ "searchDefaultLimit": 50,
195
+ "searchMaxLimit": 200,
196
+ "graphLimit": 500,
197
+ "maxWriteChars": 2000,
198
+ "discoverWikiPages": "2",
199
+ "discoverMemoryItems": "0"
200
+ }
201
+ },
202
+ "dsh-weknora-panel": {
203
+ "_doc": "侧栏面板:把维克纳的网页嵌进来(一个反代 + 一个「记忆」页签)。",
204
+ "values": {
205
+ "weknoraProxy": {
206
+ "enabled": true,
207
+ "port": 8099,
208
+ "host": "127.0.0.1",
209
+ "target": "http://127.0.0.1:80",
210
+ "login": {
211
+ "path": "/api/v1/auth/login",
212
+ "password": ""
213
+ }
214
+ },
215
+ "transcriptDir": "",
216
+ "maxReadBytes": 409600,
217
+ "memoryListLimit": 200,
218
+ "panelTitle": "记忆",
219
+ "panelNote": ""
220
+ },
221
+ "docs": {
222
+ "weknoraProxy.enabled": "关掉就不起反代,只有账本和设置两个页签能用。",
223
+ "weknoraProxy.port": "反代监听端口。面板页面自己会向服务端问这个值,改完重启即可,不用改代码。",
224
+ "weknoraProxy.host": "只绑定本机。不要改成 0.0.0.0。",
225
+ "weknoraProxy.target": "维克纳前端地址。默认 80 端口。",
226
+ "weknoraProxy.login.path": "如果你想让服务端替你登录,填登录接口路径。",
227
+ "weknoraProxy.login.password": "留空 = 纯透明反代(推荐)。填了就是明文存在磁盘上。",
228
+ "transcriptDir": "账本目录。留空 = ~/.mnemon/transcripts。",
229
+ "maxReadBytes": "账本读写上限。",
230
+ "memoryListLimit": "记忆条目一次列多少。",
231
+ "panelTitle": "侧栏页签名。",
232
+ "panelNote": "页签下面那行小字。留空自动显示反代地址。"
233
+ },
234
+ "defaults": {
235
+ "weknoraProxy": {
236
+ "enabled": true,
237
+ "port": 8099,
238
+ "host": "127.0.0.1",
239
+ "target": "http://127.0.0.1:80",
240
+ "login": {
241
+ "path": "/api/v1/auth/login",
242
+ "password": ""
243
+ }
244
+ },
245
+ "transcriptDir": "",
246
+ "maxReadBytes": 409600,
247
+ "memoryListLimit": 200,
248
+ "panelTitle": "记忆",
249
+ "panelNote": ""
250
+ }
251
+ }
252
+ },
253
+ "_reset": "每个包的 defaults 是出厂值,面板上的「恢复默认」从这个段复制回 values。直接改 values 就是改生效值。"
254
+ }