dsh-palace 1.2.1-walker.0 → 1.3.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/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/host/index.ts
2
+ import { existsSync as existsSync5 } from "node:fs";
2
3
  import { homedir as homedir3 } from "node:os";
3
4
  import { join as join6, resolve } from "node:path";
4
5
 
@@ -8,7 +9,7 @@ import { join as join2 } from "node:path";
8
9
 
9
10
  // src/core/store.ts
10
11
  import { randomBytes } from "node:crypto";
11
- import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
12
+ import { cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
12
13
  import { join } from "node:path";
13
14
 
14
15
  // src/shared/palace.ts
@@ -113,8 +114,10 @@ var KINDS = /* @__PURE__ */ new Set(["book", "note", "group"]);
113
114
  function assertCellId(id) {
114
115
  if (typeof id !== "string" || !CELL_ID.test(id)) throw new ShelfError("\u683C\u5B50 id \u65E0\u6548");
115
116
  }
116
- function parsePalace(raw) {
117
- const data = JSON.parse(raw);
117
+ function schemaOf(data) {
118
+ return typeof data?.schema === "number" && data.schema > SCHEMA ? data.schema : SCHEMA;
119
+ }
120
+ function parsePalace(data) {
118
121
  if (data === null || typeof data !== "object" || !Array.isArray(data.cells)) throw new Error("\u7F3A\u5C11 cells \u6570\u7EC4");
119
122
  for (const c of data.cells) {
120
123
  if (typeof c?.id !== "string" || !KINDS.has(c.kind) || !Number.isSafeInteger(c.col) || !Number.isSafeInteger(c.row) || typeof c.title !== "string") {
@@ -122,10 +125,9 @@ function parsePalace(raw) {
122
125
  }
123
126
  if (c.parentId === void 0) c.parentId = null;
124
127
  }
125
- return { ...data, schema: SCHEMA, cells: data.cells };
128
+ return { ...data, schema: schemaOf(data), cells: data.cells };
126
129
  }
127
- function parseItem(raw, id) {
128
- const data = JSON.parse(raw);
130
+ function parseItem(data, id) {
129
131
  if (data === null || typeof data !== "object") throw new Error("\u4E0D\u662F JSON \u5BF9\u8C61");
130
132
  for (const key of ["chapters", "cards", "inspirations"]) {
131
133
  if (data[key] !== void 0 && !Array.isArray(data[key])) throw new Error(`${key} \u5E94\u4E3A\u6570\u7EC4`);
@@ -140,7 +142,7 @@ function parseItem(raw, id) {
140
142
  return {
141
143
  ...emptyItem(id),
142
144
  ...data,
143
- schema: SCHEMA,
145
+ schema: schemaOf(data),
144
146
  id,
145
147
  chapters: data.chapters ?? [],
146
148
  cards: data.cards ?? [],
@@ -166,14 +168,17 @@ var PalaceStore = class {
166
168
  items = /* @__PURE__ */ new Map();
167
169
  /** 每次写入 +1,前端轮询它判断是否需要刷新。 */
168
170
  rev = 1;
169
- constructor(dir) {
171
+ constructor(dir, { create = true } = {}) {
170
172
  this.dir = dir;
171
173
  this.itemsDir = join(dir, "items");
172
174
  this.imagesDir = join(dir, "images");
173
175
  this.palaceFile = join(dir, "palace.json");
174
- mkdirSync(this.itemsDir, { recursive: true });
175
- mkdirSync(this.imagesDir, { recursive: true });
176
- this.refreshPalace();
176
+ if (create) {
177
+ mkdirSync(this.itemsDir, { recursive: true });
178
+ mkdirSync(this.imagesDir, { recursive: true });
179
+ }
180
+ const state = this.refreshPalace();
181
+ if (!create && (state.stamp === "" || state.broken)) throw new ShelfError(`\u65E0\u6CD5\u8BFB\u53D6 ${this.palaceFile}`);
177
182
  }
178
183
  // ---------- 读 ----------
179
184
  refreshPalace() {
@@ -186,11 +191,14 @@ var PalaceStore = class {
186
191
  }
187
192
  const mtime = stamp(this.palaceFile);
188
193
  if (mtime === this.palace.stamp) return this.palace;
194
+ let schema = this.palace.schema;
189
195
  try {
190
- this.palace = { value: parsePalace(readFileSync(this.palaceFile, "utf8")), stamp: mtime, broken: false };
196
+ const data = JSON.parse(readFileSync(this.palaceFile, "utf8"));
197
+ schema = schemaOf(data);
198
+ this.palace = { value: parsePalace(data), stamp: mtime, broken: false, schema };
191
199
  } catch (error) {
192
200
  console.warn("[dsh-palace] palace.json \u89E3\u6790\u5931\u8D25\uFF0C\u7EE7\u7EED\u4F7F\u7528\u5185\u5B58\u4E2D\u7684\u6570\u636E", error);
193
- this.palace = { ...this.palace, stamp: mtime, broken: true };
201
+ this.palace = { ...this.palace, stamp: mtime, broken: true, schema };
194
202
  }
195
203
  this.rev += 1;
196
204
  return this.palace;
@@ -209,11 +217,14 @@ var PalaceStore = class {
209
217
  const mtime = stamp(file);
210
218
  if (cached !== void 0 && cached.stamp === mtime) return cached;
211
219
  let next;
220
+ let schema = cached?.schema;
212
221
  try {
213
- next = { value: parseItem(readFileSync(file, "utf8"), id), stamp: mtime, broken: false };
222
+ const data = JSON.parse(readFileSync(file, "utf8"));
223
+ schema = schemaOf(data);
224
+ next = { value: parseItem(data, id), stamp: mtime, broken: false, schema };
214
225
  } catch (error) {
215
226
  console.warn(`[dsh-palace] items/${id}.json \u89E3\u6790\u5931\u8D25`, error);
216
- next = { value: cached?.value ?? emptyItem(id), stamp: mtime, broken: true };
227
+ next = { value: cached?.value ?? emptyItem(id), stamp: mtime, broken: true, schema };
217
228
  }
218
229
  if (cached !== void 0) this.rev += 1;
219
230
  this.items.set(id, next);
@@ -240,6 +251,41 @@ var PalaceStore = class {
240
251
  return out;
241
252
  }
242
253
  // ---------- 写 ----------
254
+ snapshotBeforeWrite() {
255
+ const name2 = ".snapshots";
256
+ const root = join(this.dir, name2), target = join(root, today());
257
+ try {
258
+ const state = lstatSync(root, { throwIfNoEntry: false });
259
+ if (state && !state.isDirectory()) throw new Error("\u5FEB\u7167\u8DEF\u5F84\u4E0D\u662F\u666E\u901A\u76EE\u5F55");
260
+ if (existsSync(target)) return;
261
+ mkdirSync(root, { recursive: true });
262
+ const temp = mkdtempSync(join(root, ".partial-"));
263
+ try {
264
+ for (const file of readdirSync(this.dir)) {
265
+ if (file !== name2) cpSync(join(this.dir, file), join(temp, file), { recursive: true, verbatimSymlinks: true });
266
+ }
267
+ if (!existsSync(target)) renameSync(temp, target);
268
+ } finally {
269
+ rmSync(temp, { recursive: true, force: true });
270
+ }
271
+ const days = readdirSync(root, { withFileTypes: true }).filter((entry) => entry.isDirectory() && /^\d{4}-\d{2}-\d{2}$/.test(entry.name)).map((entry) => entry.name);
272
+ for (const old of days.sort().slice(0, -3)) rmSync(join(root, old), { recursive: true, force: true });
273
+ } catch (error) {
274
+ console.warn("[dsh-palace] \u6BCF\u65E5\u5FEB\u7167\u5931\u8D25\uFF0C\u7EE7\u7EED\u5199\u5165", error);
275
+ }
276
+ }
277
+ /** 格子、记忆和图片共用同一条只读边界。 */
278
+ assertWritable() {
279
+ const palace = this.refreshPalace();
280
+ this.assertSupportedSchema(palace, "palace.json");
281
+ for (const cell of palace.value.cells) {
282
+ if (cell.kind === "group" || !CELL_ID.test(cell.id)) continue;
283
+ this.assertSupportedSchema(this.refreshItem(cell.id), `items/${cell.id}.json`);
284
+ }
285
+ }
286
+ assertSupportedSchema(state, file) {
287
+ if ((state.schema ?? SCHEMA) > SCHEMA) throw new ShelfError(`${file} \u7684\u6570\u636E\u7248\u672C\u8F83\u65B0\uFF0C\u8BF7\u5347\u7EA7\u63D2\u4EF6`);
288
+ }
243
289
  /**
244
290
  * 执行一次修改。fn 抛错则不落盘。
245
291
  *
@@ -256,7 +302,8 @@ var PalaceStore = class {
256
302
  }
257
303
  }
258
304
  commit(fn) {
259
- const palaceState = this.refreshPalace();
305
+ this.assertWritable();
306
+ const palaceState = this.palace;
260
307
  if (palaceState.broken) throw new ShelfError("palace.json \u683C\u5F0F\u6709\u8BEF\uFF1A\u8BF7\u5148\u4FEE\u597D\u6216\u5220\u6389\u8FD9\u4E2A\u6587\u4EF6\uFF0C\u518D\u7EE7\u7EED\u64CD\u4F5C");
261
308
  const palace = structuredClone(palaceState.value);
262
309
  const touched = /* @__PURE__ */ new Map();
@@ -270,17 +317,18 @@ var PalaceStore = class {
270
317
  return hit.item;
271
318
  }
272
319
  const state = this.refreshItem(id);
320
+ this.assertSupportedSchema(state, `items/${id}.json`);
273
321
  if (state.broken) throw new ShelfError(`items/${id}.json \u683C\u5F0F\u6709\u8BEF\uFF1A\u8BF7\u5148\u4FEE\u597D\u6216\u5220\u6389\u8FD9\u4E2A\u6587\u4EF6`);
274
322
  const item = structuredClone(state.value);
275
323
  touched.set(id, { item, before: JSON.stringify(state.value), stamp: state.stamp });
276
324
  return item;
277
325
  },
278
326
  createItem: (item) => {
279
- assertCellId(item.id);
327
+ this.assertSupportedSchema(this.refreshItem(item.id), `items/${item.id}.json`);
280
328
  touched.set(item.id, { item, before: "", stamp: "" });
281
329
  },
282
330
  deleteItem: (id) => {
283
- assertCellId(id);
331
+ this.assertSupportedSchema(this.refreshItem(id), `items/${id}.json`);
284
332
  touched.set(id, { item: null, before: "", stamp: "" });
285
333
  },
286
334
  deleteImage: (name2) => {
@@ -289,14 +337,17 @@ var PalaceStore = class {
289
337
  };
290
338
  const result = fn(tx);
291
339
  if (result instanceof Promise) throw new Error("mutate \u7684\u56DE\u8C03\u5FC5\u987B\u662F\u540C\u6B65\u7684");
292
- const changed = (file, was) => (existsSync(file) ? stamp(file) : "") !== was;
293
340
  const palaceChanged = JSON.stringify(palace) !== JSON.stringify(palaceState.value);
294
- if (palaceChanged && changed(this.palaceFile, palaceState.stamp)) throw new ShelfError("palace.json \u521A\u88AB\u5176\u4ED6\u7A0B\u5E8F\u4FEE\u6539\u8FC7\uFF0C\u8BF7\u91CD\u8BD5");
341
+ const writes = [...touched].filter(([, { item, before }]) => item === null || JSON.stringify(item) !== before);
342
+ if (palaceChanged || writes.length > 0 || images.length > 0) this.snapshotBeforeWrite();
343
+ const changed = (file, was) => (existsSync(file) ? stamp(file) : "") !== was;
344
+ if (changed(this.palaceFile, palaceState.stamp)) throw new ShelfError("palace.json \u521A\u88AB\u5176\u4ED6\u7A0B\u5E8F\u4FEE\u6539\u8FC7\uFF0C\u8BF7\u91CD\u8BD5");
295
345
  for (const [id, t] of touched) {
296
346
  if (t.before !== "" && changed(join(this.itemsDir, `${id}.json`), t.stamp)) throw new ShelfError(`items/${id}.json \u521A\u88AB\u5176\u4ED6\u7A0B\u5E8F\u4FEE\u6539\u8FC7\uFF0C\u8BF7\u91CD\u8BD5`);
297
347
  }
298
- for (const [id, { item, before }] of touched) {
299
- if (item === null || JSON.stringify(item) === before) continue;
348
+ for (const [id, { item }] of writes) {
349
+ if (item === null) continue;
350
+ if (!existsSync(this.itemsDir)) mkdirSync(this.itemsDir, { recursive: true });
300
351
  const st = writeAtomic(join(this.itemsDir, `${id}.json`), item);
301
352
  this.items.set(id, { value: item, stamp: st, broken: false });
302
353
  }
@@ -399,6 +450,7 @@ var Memory = class {
399
450
  }
400
451
  /** 整页覆写。target 是 'me' 或书 / 笔记格子的 id。 */
401
452
  writePage(target, input) {
453
+ this.store.assertWritable();
402
454
  if (target !== "me") this.cellOf(target, false);
403
455
  if (typeof input.body !== "string" || input.body.trim() === "") throw new ShelfError("\u6B63\u6587\u4E0D\u80FD\u4E3A\u7A7A");
404
456
  const body = input.body.trim();
@@ -419,6 +471,7 @@ var Memory = class {
419
471
  writtenAt: nowIso()
420
472
  };
421
473
  const file = this.pageFile(target);
474
+ this.store.snapshotBeforeWrite();
422
475
  mkdirSync2(this.cellsDir, { recursive: true });
423
476
  const tmp = `${file}.${process.pid}.tmp`;
424
477
  writeFileSync2(tmp, renderPage(page), "utf8");
@@ -451,6 +504,8 @@ var Memory = class {
451
504
  return records;
452
505
  }
453
506
  append(record) {
507
+ this.store.assertWritable();
508
+ this.store.snapshotBeforeWrite();
454
509
  const size = existsSync2(this.linksFile) ? statSync2(this.linksFile).size : 0;
455
510
  const broken = size > 0 && readFileSync2(this.linksFile, "utf8").at(-1) !== "\n";
456
511
  appendFileSync(this.linksFile, (broken ? "\n" : "") + JSON.stringify(record) + "\n", "utf8");
@@ -612,14 +667,6 @@ ${mine.map((a) => `- \u2194 ${title(a.from === current.id ? a.to : a.from)}\uFF0
612
667
  }
613
668
  };
614
669
 
615
- // src/host/routes.ts
616
- import { execFile as execFile2, spawn } from "node:child_process";
617
- import { createReadStream, existsSync as existsSync4, statSync as statSync4 } from "node:fs";
618
- import { homedir as homedir2 } from "node:os";
619
- import { basename as basename2, extname as extname2, join as join5, normalize, sep as sep2 } from "node:path";
620
- import { Readable } from "node:stream";
621
- import { fileURLToPath } from "node:url";
622
-
623
670
  // src/core/ops.ts
624
671
  function text(value, what) {
625
672
  if (typeof value !== "string" || value.trim() === "") throw new ShelfError(`${what}\u4E0D\u80FD\u4E3A\u7A7A`);
@@ -690,6 +737,15 @@ function pruneGroup(palace, groupId) {
690
737
  if (groupId === null || palace.cells.some((c) => c.parentId === groupId)) return;
691
738
  palace.cells = palace.cells.filter((c) => c.id !== groupId);
692
739
  }
740
+ var WELCOME_URL = "https://dsh-palace.pages.dev";
741
+ async function seedWelcome(store) {
742
+ return store.mutate((tx) => {
743
+ const cell = { id: newId("c"), kind: "note", parentId: null, ...firstEmptySlot(tx.palace, null), title: "Memory Palace \u2197", subtitle: "\u4EC0\u4E48\u662F", icon: "open-book", href: WELCOME_URL, createdAt: nowIso() };
744
+ tx.palace.cells.push(cell);
745
+ tx.createItem(emptyItem(cell.id, null));
746
+ return cell;
747
+ });
748
+ }
693
749
  async function addCell(store, args) {
694
750
  const kind = args.kind;
695
751
  if (kind !== "book" && kind !== "note" && kind !== "group") throw new ShelfError("\u683C\u5B50\u7C7B\u578B\u5E94\u4E3A book / note / group");
@@ -993,8 +1049,16 @@ async function deleteInspiration(store, args) {
993
1049
  });
994
1050
  }
995
1051
 
1052
+ // src/host/routes.ts
1053
+ import { execFile as execFile2, spawn } from "node:child_process";
1054
+ import { createReadStream, existsSync as existsSync4, statSync as statSync4 } from "node:fs";
1055
+ import { homedir as homedir2 } from "node:os";
1056
+ import { basename as basename2, extname as extname2, join as join5, normalize, sep as sep2 } from "node:path";
1057
+ import { Readable } from "node:stream";
1058
+ import { fileURLToPath } from "node:url";
1059
+
996
1060
  // src/host/books.ts
997
- import { closeSync, copyFileSync, existsSync as existsSync3, openSync, readdirSync, readSync, statSync as statSync3 } from "node:fs";
1061
+ import { closeSync, copyFileSync, existsSync as existsSync3, mkdirSync as mkdirSync3, openSync, readdirSync as readdirSync2, readSync, statSync as statSync3 } from "node:fs";
998
1062
  import { homedir } from "node:os";
999
1063
  import { basename, extname, isAbsolute, join as join4 } from "node:path";
1000
1064
 
@@ -1162,7 +1226,7 @@ var SCANNED_THRESHOLD = 20;
1162
1226
  function expandHome(path) {
1163
1227
  return path.startsWith("~/") ? join4(homedir(), path.slice(2)) : path;
1164
1228
  }
1165
- async function addBook(store, args, signal) {
1229
+ async function addBook(store, args, signal, { readVideo = true } = {}) {
1166
1230
  const cancelled = () => {
1167
1231
  if (signal?.aborted) throw new ShelfError("\u5DF2\u53D6\u6D88");
1168
1232
  };
@@ -1172,9 +1236,9 @@ async function addBook(store, args, signal) {
1172
1236
  let outlineSource;
1173
1237
  if (typeof args.url === "string" && args.url.trim() !== "") {
1174
1238
  const url = args.url.trim();
1175
- if (!isBilibili(url)) {
1239
+ if (!readVideo || !isBilibili(url)) {
1176
1240
  if (!/^https?:\/\/\S+$/.test(url)) throw new ShelfError("\u89C6\u9891\u94FE\u63A5\u8981\u4EE5 http:// \u6216 https:// \u5F00\u5934");
1177
- if (typeof args.title !== "string" || args.title.trim() === "") throw new ShelfError("\u4E0D\u662F B \u7AD9\u94FE\u63A5\u65F6\u8981\u586B\u4F5C\u54C1\u540D");
1241
+ if (typeof args.title !== "string" || args.title.trim() === "") throw new ShelfError(readVideo ? "\u4E0D\u662F B \u7AD9\u94FE\u63A5\u65F6\u8981\u586B\u4F5C\u54C1\u540D" : "\u89C6\u9891\u94FE\u63A5\u8981\u586B\u4F5C\u54C1\u540D");
1178
1242
  cancelled();
1179
1243
  return addCell(store, { kind: "book", title: args.title.trim(), subtitle: args.subtitle, source: { type: "video", url }, parentId: args.parentId, targetDate: args.targetDate });
1180
1244
  }
@@ -1233,7 +1297,7 @@ function findAttachment(ref) {
1233
1297
  const home = process.env.DSH_HOME ?? join4(homedir(), ".dsh");
1234
1298
  const dir = join4(home, "attachments", "v1", "objects", hex.slice(0, 2));
1235
1299
  if (!existsSync3(dir)) return void 0;
1236
- const name2 = readdirSync(dir).find((f) => f.startsWith(hex));
1300
+ const name2 = readdirSync2(dir).find((f) => f.startsWith(hex));
1237
1301
  return name2 === void 0 ? void 0 : join4(dir, name2);
1238
1302
  }
1239
1303
  var IMAGE_EXTS = /* @__PURE__ */ new Set([".png", ".jpg", ".jpeg", ".webp", ".gif"]);
@@ -1244,6 +1308,9 @@ function saveImage(store, ref) {
1244
1308
  if (source === void 0) return void 0;
1245
1309
  const ext = IMAGE_EXTS.has(extname(source).toLowerCase()) ? extname(source).toLowerCase() : ".png";
1246
1310
  const name2 = `${newId("img")}${ext}`;
1311
+ store.assertWritable();
1312
+ store.snapshotBeforeWrite();
1313
+ mkdirSync3(store.imagesDir, { recursive: true });
1247
1314
  copyFileSync(source, join4(store.imagesDir, name2));
1248
1315
  return name2;
1249
1316
  }
@@ -1477,10 +1544,19 @@ function createRoutes(store, active, memory = new Memory(store)) {
1477
1544
  route("POST", "pick-file", async () => json({ ok: true, path: await pickBook() })),
1478
1545
  // 只打开格子里登记过的东西,不接受任意路径
1479
1546
  route("POST", "open", ({ body }) => {
1547
+ if (body.target === "welcome") {
1548
+ openExternal(WELCOME_URL);
1549
+ return json({ ok: true });
1550
+ }
1480
1551
  if (body.target === "dataDir") {
1481
1552
  openExternal(store.dir);
1482
1553
  return json({ ok: true });
1483
1554
  }
1555
+ const linked = store.getPalace().cells.find((c) => c.id === body.id)?.href;
1556
+ if (linked !== void 0 && /^https?:\/\//.test(linked)) {
1557
+ openExternal(linked);
1558
+ return json({ ok: true });
1559
+ }
1484
1560
  const item = itemOf2(store, body.id);
1485
1561
  const section = findSection(item, typeof body.sectionId === "string" ? body.sectionId : void 0)?.section;
1486
1562
  const source = item.source;
@@ -1506,10 +1582,8 @@ function createRoutes(store, active, memory = new Memory(store)) {
1506
1582
 
1507
1583
  // src/host/tools.ts
1508
1584
  import { defineTool } from "@deepseek-ai/dsh-tools";
1509
- var stringOut = {
1510
- schema: { type: "string" },
1511
- render: (_args, value) => [{ type: "text", text: value }]
1512
- };
1585
+
1586
+ // src/core/tools.ts
1513
1587
  var KIND_LABEL = { book: "\u4E66", note: "\u7B14\u8BB0", group: "\u5206\u7EC4" };
1514
1588
  var CARD_LABEL = { excerpt: "\u6458\u5F55", thought: "\u60F3\u6CD5", mistake: "\u9519\u9898" };
1515
1589
  function cellLine(store, c, active) {
@@ -1570,40 +1644,22 @@ async function guard(run) {
1570
1644
  throw error;
1571
1645
  }
1572
1646
  }
1573
- function createTools(store, active, memory = new Memory(store)) {
1574
- const nudged = /* @__PURE__ */ new Set();
1575
- const nudge = (exec, id, kind) => {
1576
- const key = `${exec.agent?.id ?? ""}:${id}:${kind}`;
1577
- if (nudged.has(key)) return "";
1578
- if (kind === "wiki") {
1579
- const debt = memory.debt(id);
1580
- if (debt < 3) return "";
1581
- nudged.add(key);
1582
- return `
1583
- \uFF08\u63D0\u793A\uFF1A\u8FD9\u4E00\u683C\u7684 wiki \u4E4B\u540E\u65B0\u589E\u4E86 ${debt} \u6761\u5361\u7247 / \u7075\u611F\uFF0C\u804A\u5B8C\u65B9\u4FBF\u65F6\u7528 palace_wiki_write \u66F4\u65B0\uFF1B\u4E0D\u7528\u6253\u65AD\u7528\u6237\u3002\uFF09`;
1647
+ function createOverviewTool(store) {
1648
+ return {
1649
+ name: "palace_overview",
1650
+ description: "\u67E5\u770B\u7528\u6237\u7684 Memory Palace\uFF08\u8BB0\u5FC6\u5BAB\u6BBF\uFF09\uFF1A\u6240\u6709\u5206\u7EC4\u3001\u4E66\u3001\u7B14\u8BB0\u7684 id\u3001\u8FDB\u5EA6\u3001\u5361\u7247\u6570\uFF0C\u4EE5\u53CA\u7528\u6237\u5F53\u524D\u6253\u5F00\u7684\u662F\u54EA\u4E2A\u683C\u5B50\u3002\u6D89\u53CA\u5BAB\u6BBF\u7684\u4EFB\u4F55\u64CD\u4F5C\u524D\u5148\u8C03\u7528\u5B83\u62FF\u5230 id\u3002\u4F20 item_id \u53EF\u67E5\u770B\u8FD9\u4E2A\u683C\u5B50\u7684\u5B8C\u6574\u76EE\u5F55\uFF08\u542B section_id\uFF09\u3001\u6700\u8FD1\u7684\u5361\u7247\u548C\u7075\u611F\u3002",
1651
+ parameters: {
1652
+ item_id: { type: "string", description: "\u53EF\u9009\uFF1A\u67E5\u770B\u8FD9\u4E2A\u683C\u5B50\u7684\u8BE6\u60C5" }
1653
+ },
1654
+ async execute(args, options) {
1655
+ return guard(async () => args.item_id === void 0 ? describe(store, options.highlightId) : itemDetail(store, args.item_id));
1584
1656
  }
1585
- nudged.add(key);
1586
- return "\n\uFF08\u63D0\u793A\uFF1A\u5982\u679C\u8FD9\u6761\u548C\u5BAB\u6BBF\u91CC\u522B\u7684\u683C\u5B50\u6709\u5173\u2014\u2014\u540C\u4E00\u4E2A\u5FF5\u5934\u3001\u4E3B\u9898\u6216\u7591\u95EE\u2014\u2014\u53EF\u4EE5\u7528 palace_link \u8FDE\u4E00\u6761\u7535\u5F27\u3002\uFF09";
1587
- };
1588
- const activeOf = (exec) => exec.agent ? active.get(exec.agent.id) : void 0;
1589
- const targetOf = (given, exec) => {
1590
- const id = typeof given === "string" && given !== "" ? given : activeOf(exec);
1591
- if (id === void 0) throw new ShelfError("\u4E0D\u77E5\u9053\u8BB0\u5230\u54EA\u4E2A\u683C\u5B50\uFF1A\u7528\u6237\u6CA1\u6709\u6253\u5F00\u683C\u5B50\u3002\u8BF7\u5148\u7528 palace_overview \u67E5\u770B\u540E\u6307\u5B9A item_id\uFF0C\u6216\u8BF7\u7528\u6237\u5728\u53F3\u4FA7 Memory Palace \u6253\u5F00\u5BF9\u5E94\u7684\u4E66\u3002");
1592
- return id;
1593
1657
  };
1658
+ }
1659
+ function createPalaceTools(store, memory, sources) {
1594
1660
  return [
1595
- defineTool({
1596
- name: "palace_overview",
1597
- description: "\u67E5\u770B\u7528\u6237\u7684 Memory Palace\uFF08\u8BB0\u5FC6\u5BAB\u6BBF\uFF09\uFF1A\u6240\u6709\u5206\u7EC4\u3001\u4E66\u3001\u7B14\u8BB0\u7684 id\u3001\u8FDB\u5EA6\u3001\u5361\u7247\u6570\uFF0C\u4EE5\u53CA\u7528\u6237\u5F53\u524D\u6253\u5F00\u7684\u662F\u54EA\u4E2A\u683C\u5B50\u3002\u6D89\u53CA\u5BAB\u6BBF\u7684\u4EFB\u4F55\u64CD\u4F5C\u524D\u5148\u8C03\u7528\u5B83\u62FF\u5230 id\u3002\u4F20 item_id \u53EF\u67E5\u770B\u8FD9\u4E2A\u683C\u5B50\u7684\u5B8C\u6574\u76EE\u5F55\uFF08\u542B section_id\uFF09\u3001\u6700\u8FD1\u7684\u5361\u7247\u548C\u7075\u611F\u3002",
1598
- parameters: {
1599
- item_id: { type: "string", description: "\u53EF\u9009\uFF1A\u67E5\u770B\u8FD9\u4E2A\u683C\u5B50\u7684\u8BE6\u60C5" }
1600
- },
1601
- output: stringOut,
1602
- async execute(args, exec) {
1603
- return guard(async () => args.item_id === void 0 ? describe(store, activeOf(exec)) : itemDetail(store, args.item_id));
1604
- }
1605
- }),
1606
- defineTool({
1661
+ createOverviewTool(store),
1662
+ {
1607
1663
  name: "palace_add",
1608
1664
  description: "\u5F80\u5BAB\u6BBF\u91CC\u653E\u4E1C\u897F\u3002kind=group \u65B0\u5EFA\u5206\u7EC4\uFF08title\uFF09\uFF1Bkind=note \u65B0\u5EFA\u7B14\u8BB0\u683C\u5B50\uFF08title\uFF0C\u7528\u4E8E\u6CA1\u6709\u539F\u6587\u7684\u4E1C\u897F\uFF1A\u65E5\u8BB0\u3001\u5FC3\u60C5\u3001\u8FFD\u5267\uFF09\uFF1Bkind=book \u6DFB\u52A0 PDF / EPUB\uFF08path \u4E3A\u7EDD\u5BF9\u8DEF\u5F84\uFF09\u6216\u89C6\u9891\uFF08url\uFF0C\u5267\u3001\u7EFC\u827A\u3001\u8BFE\u90FD\u7B97\uFF09\uFF0C\u53EF\u9009 title\u3001subtitle\uFF08\u5907\u6CE8\uFF1A\u683C\u5B50\u4E0A\u6807\u9898\u4E0A\u65B9\u7684\u4E00\u884C\u5C0F\u5B57\uFF0C\u7528\u6237\u60F3\u586B\u4EC0\u4E48\u90FD\u884C\uFF0C\u5E38\u89C1\u662F\u4F5C\u8005\u6216\u53E6\u4E00\u79CD\u8BED\u8A00\u7684\u4E66\u540D\uFF09\u3002title \u7528\u4E66\u7684\u539F\u540D\uFF0C\u5927\u5C0F\u5199\u7167\u539F\u4E66\uFF08\u5982 Invention of Nature\uFF09\uFF0C\u4E0D\u5E26\u6587\u4EF6\u540D\u91CC\u7684 - _ \u300A\u300B \u7B49\u7B26\u53F7\uFF1B\u5916\u6587\u4E66\u53EF\u628A\u4E2D\u6587\u8BD1\u540D\u653E subtitle\u3001target_date\uFF08YYYY-MM-DD\uFF09\u3002parent_id \u586B\u5206\u7EC4 id \u53EF\u4EE5\u76F4\u63A5\u653E\u8FDB\u5206\u7EC4\uFF08\u5206\u7EC4\u91CC\u4E0D\u80FD\u518D\u653E\u5206\u7EC4\uFF09\u3002\u89C6\u9891\u94FE\u63A5\u6309\u5E73\u53F0\u5904\u7406\uFF1AB \u7AD9\uFF08\u5E26 BV \u53F7\u6216 b23.tv \u77ED\u94FE\uFF09\u76F4\u63A5\u4F20 url\uFF0C\u4F1A\u81EA\u52A8\u8BFB\u51FA\u5206 P / \u5408\u96C6\u548C\u6BCF\u4E00\u96C6\u7684\u94FE\u63A5\uFF0Ctitle \u53EF\u4E0D\u586B\uFF1BYouTube\uFF1A\u4F20 url \u548C\u89C6\u9891\u6216\u64AD\u653E\u5217\u8868\u7684\u6807\u9898\uFF1B\u5176\u4ED6\u5E73\u53F0\uFF08Netflix\u3001\u7231\u5947\u827A\u3001\u8292\u679C TV\u3001\u817E\u8BAF\u89C6\u9891\u3001\u8BFE\u7A0B\u7F51\u7AD9\u2026\u2026\uFF09\uFF1A\u4ECE\u94FE\u63A5\u548C\u7528\u6237\u7684\u8BDD\u8BA4\u51FA\u4F5C\u54C1\uFF0Ctitle \u7528\u4F5C\u54C1\u7684\u6B63\u5F0F\u540D\uFF0C\u4F20 url\u3002\u8FD9\u4E24\u7C7B\u5EFA\u597D\u540E\u5148\u505C\u4E0B\uFF1A\u544A\u8BC9\u7528\u6237\u5DF2\u7ECF\u653E\u8FDB\u6765\u4E86\uFF0C\u95EE\u8981\u4E0D\u8981\u4E0A\u7F51\u67E5\u5206\u96C6\u548C\u6BCF\u4E00\u96C6\u7684\u94FE\u63A5\uFF08\u8981\u82B1\u4E00\u4E24\u5206\u949F\uFF09\uFF1B\u7528\u6237\u5728\u539F\u8BDD\u91CC\u5DF2\u7ECF\u8981\u4E86\u76EE\u5F55\u5C31\u76F4\u63A5\u67E5\u3002\u67E5\u7684\u65F6\u5019\u5206\u5B63\u5C31\u4E00\u5B63\u4E00\u7AE0\uFF0C\u7528 palace_set_outline \u5199\u5165\uFF08\u6BCF\u96C6\u5C3D\u91CF\u5E26 url\uFF09\uFF1B\u8FDE\u8F7D\u4E2D\u7684\u53EA\u5199\u5DF2\u7ECF\u67E5\u5B9E\u7684\u96C6\u6570\uFF0C\u67E5\u4E0D\u5230\u94FE\u63A5\u5C31\u53EA\u5199\u6807\u9898\u3002\u540C\u6837\u7684\u89C4\u77E9\u9002\u7528\u4E8E\u4EFB\u4F55\u8981\u82B1\u65F6\u95F4\u7684\u8865\u5168\uFF08\u4E0A\u7F51\u67E5\u76EE\u5F55\u3001\u8BFB\u5F88\u591A\u9875\u2026\u2026\uFF09\uFF1A\u5148\u5EFA\u683C\u5B50\uFF0C\u518D\u95EE\u7528\u6237\uFF1B\u51E0\u79D2\u5185\u5C31\u80FD\u5B8C\u6210\u7684\uFF08B \u7AD9\u63A5\u53E3\u3001PDF \u4E66\u7B7E\u3001EPUB \u76EE\u5F55\uFF09\u7167\u65E7\u81EA\u52A8\u505A\u3002\u6CA1\u6709\u539F\u6587\u3001\u4E5F\u4E0D\u9700\u8981\u6309\u96C6\u6253\u52FE\u7684\uFF08\u6BD4\u5982\u53EA\u662F\u60F3\u804A\u4E00\u90E8\u5267\uFF09\uFF0C\u7528 kind=note\u3002",
1609
1665
  parameters: {
@@ -1615,14 +1671,13 @@ function createTools(store, active, memory = new Memory(store)) {
1615
1671
  url: { type: "string", description: "book\uFF1A\u89C6\u9891\u94FE\u63A5\uFF08http / https\uFF09" },
1616
1672
  target_date: { type: "string" }
1617
1673
  },
1618
- output: stringOut,
1619
- async execute(args, exec) {
1674
+ async execute(args, options) {
1620
1675
  return guard(async () => {
1621
1676
  if (args.kind !== "book") {
1622
1677
  const c2 = await addCell(store, { kind: args.kind, title: args.title, subtitle: args.subtitle, parentId: args.parent_id });
1623
1678
  return `\u5DF2\u65B0\u5EFA${KIND_LABEL[c2.kind]}\u300C${c2.title}\u300D id=${c2.id}`;
1624
1679
  }
1625
- const c = await addBook(store, { file: args.path, url: args.url, title: args.title, subtitle: args.subtitle, parentId: args.parent_id, targetDate: args.target_date }, exec.signal);
1680
+ const c = await sources.addBook({ file: args.path, url: args.url, title: args.title, subtitle: args.subtitle, parentId: args.parent_id, targetDate: args.target_date }, options.signal);
1626
1681
  const item = store.getItem(c.id);
1627
1682
  const total = progressOf(item).total;
1628
1683
  if (item.source?.type === "video") {
@@ -1632,14 +1687,13 @@ function createTools(store, active, memory = new Memory(store)) {
1632
1687
  return `\u5DF2\u6DFB\u52A0\u300A${c.title}\u300B id=${c.id}\uFF0C\u5171 ${item.source?.type === "pdf" ? item.source.pageCount : "?"} \u9875\u3002` + (total > 0 ? `\u5DF2\u4ECE PDF \u4E66\u7B7E\u8BFB\u51FA\u76EE\u5F55\uFF1A${item.chapters.length} \u7AE0 ${total} \u8282\u3002` : "\u8FD9\u672C PDF \u6CA1\u6709\u4E66\u7B7E\uFF0C\u9700\u8981\u751F\u6210\u76EE\u5F55\uFF1A\u8BF7\u7528\u6237\u5728\u53F3\u4FA7\u7B14\u8BB0\u672C\u7684\u300C\u8FDB\u5EA6\u300D\u91CC\u52FE\u9009\u76EE\u5F55\u9875\uFF0C\u6216\u544A\u8BC9\u4F60\u76EE\u5F55\u5728\u7B2C\u51E0\u9875\uFF0C\u7136\u540E\u7528 palace_read \u8BFB\u53D6\u3002");
1633
1688
  });
1634
1689
  }
1635
- }),
1636
- defineTool({
1690
+ },
1691
+ {
1637
1692
  name: "palace_remove",
1638
1693
  description: "\u4ECE\u5BAB\u6BBF\u91CC\u5220\u6389\u4E00\u4E2A\u683C\u5B50\uFF08\u4E66\u3001\u7B14\u8BB0\u6216\u7A7A\u5206\u7EC4\uFF09\uFF0C\u8FDE\u540C\u5B83\u7684\u8FDB\u5EA6\u3001\u5361\u7247\u548C\u7075\u611F\uFF0C\u4E0D\u5220\u7535\u8111\u4E0A\u7684\u539F\u6587\u4EF6\u3002\u5206\u7EC4\u91CC\u6700\u540E\u4E00\u683C\u5220\u6389\u6216\u79FB\u51FA\u540E\uFF0C\u5206\u7EC4\u4F1A\u81EA\u52A8\u6D88\u5931\uFF0C\u4E0D\u7528\u518D\u5220\u3002\u4E0D\u53EF\u64A4\u9500\uFF1A\u8C03\u7528\u524D\u5FC5\u987B\u5148\u8BF4\u6E05\u8981\u5220\u7684\u662F\u54EA\u4E00\u4E2A\u3001\u4F1A\u4E22\u6389\u591A\u5C11\u5361\u7247\u548C\u7075\u611F\uFF0C\u7B49\u7528\u6237\u660E\u786E\u540C\u610F\u540E\u518D\u8C03\u7528\u3002\u5206\u7EC4\u91CC\u8FD8\u6709\u4E1C\u897F\u65F6\u4E0D\u80FD\u5220\u3002",
1639
1694
  parameters: {
1640
1695
  item_id: { type: "string", required: true }
1641
1696
  },
1642
- output: stringOut,
1643
1697
  async execute(args) {
1644
1698
  return guard(async () => {
1645
1699
  const cell = store.getPalace().cells.find((c) => c.id === args.item_id);
@@ -1649,22 +1703,21 @@ function createTools(store, active, memory = new Memory(store)) {
1649
1703
  return `\u5DF2\u5220\u9664${KIND_LABEL[cell.kind]}\u300C${cell.title}\u300D` + (item ? `\uFF08\u5361\u7247 ${item.cards.length} \u5F20\u3001\u7075\u611F ${item.inspirations.length} \u6761\uFF09` : "");
1650
1704
  });
1651
1705
  }
1652
- }),
1653
- defineTool({
1706
+ },
1707
+ {
1654
1708
  name: "palace_read",
1655
1709
  description: "\u8BFB\u53D6\u67D0\u672C PDF \u6307\u5B9A\u9875\u7684\u6587\u5B57\uFF08\u9875\u7801\u4E3A PDF \u7684\u9875\u5E8F\u53F7\uFF0C\u4ECE 1 \u5F00\u59CB\uFF0C\u4E0D\u662F\u4E66\u4E0A\u5370\u7684\u9875\u7801\uFF09\u3002\u7528\u4E8E\u8BFB\u53D6\u76EE\u5F55\u9875\u6765\u751F\u6210\u7AE0\u8282\u7ED3\u6784\u3002\u6BCF\u6B21\u6700\u591A 30 \u9875\u3002",
1656
1710
  parameters: {
1657
1711
  item_id: { type: "string", required: true },
1658
1712
  pages: { type: "array", items: { type: "integer" }, required: true, description: "PDF \u9875\u5E8F\u53F7\u5217\u8868" }
1659
1713
  },
1660
- output: stringOut,
1661
1714
  async execute(args) {
1662
1715
  return guard(async () => {
1663
1716
  const source = store.getItem(args.item_id).source;
1664
1717
  if (source?.type !== "pdf") throw new ShelfError("\u8FD9\u4E2A\u683C\u5B50\u4E0D\u662F PDF");
1665
1718
  const pages = [...new Set(args.pages)].slice(0, 30);
1666
- const texts = await readPageTexts(source.path, pages);
1667
- const scanned = texts.filter((t) => t.text.length < SCANNED_THRESHOLD).map((t) => t.page);
1719
+ const texts = await sources.readPageTexts(source.path, pages);
1720
+ const scanned = texts.filter((t) => t.text.length < sources.scannedThreshold).map((t) => t.page);
1668
1721
  let out = texts.map((t) => `=== \u7B2C ${t.page} \u9875 ===
1669
1722
  ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
1670
1723
  if (scanned.length > 0) {
@@ -1675,8 +1728,8 @@ ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
1675
1728
  return out;
1676
1729
  });
1677
1730
  }
1678
- }),
1679
- defineTool({
1731
+ },
1732
+ {
1680
1733
  name: "palace_set_outline",
1681
1734
  description: "\u5199\u5165\uFF08\u8986\u76D6\uFF09\u4E66\u7684\u76EE\u5F55\u7ED3\u6784\uFF0C\u53EA\u8981\u4E24\u7EA7\uFF1A\u7AE0 \u203A \u8282\u3002\u6CA1\u6709\u5206\u8282\u7684\u7AE0\uFF0Csections \u7559\u7A7A\u5373\u53EF\uFF08\u4F1A\u81EA\u52A8\u7B97\u4F5C\u4E00\u8282\uFF09\u3002\u6807\u9898\u4FDD\u6301\u539F\u4E66\u5199\u6CD5\uFF0C\u53BB\u6389\u9875\u7801\u548C\u7701\u7565\u53F7\u3002\u91CD\u65B0\u751F\u6210\u76EE\u5F55\u65F6\uFF0C\u6807\u9898\u4E0D\u53D8\u7684\u8282\u4F1A\u4FDD\u7559\u5B8C\u6210\u72B6\u6001\u3002\u9875\u7801 page \u53EF\u9009\uFF0C\u586B PDF \u9875\u5E8F\u53F7\uFF08\u4E66\u4E0A\u5370\u7684\u9875\u7801 + \u504F\u79FB\u91CF\uFF09\uFF1B\u4E0D\u786E\u5B9A\u5C31\u4E0D\u586B\u3002\u89C6\u9891\u7684\u6BCF\u4E00\u96C6\u53EF\u4EE5\u586B url\uFF08\u8FD9\u4E00\u96C6\u7684\u64AD\u653E\u94FE\u63A5\uFF0Chttp / https\uFF09\uFF0C\u7528\u6237\u70B9\u300C\u64AD\u653E\u300D\u76F4\u63A5\u6253\u5F00\uFF1B\u4E0D\u586B\u5C31\u6253\u5F00\u4F5C\u54C1\u94FE\u63A5\u3002",
1682
1735
  parameters: {
@@ -1705,7 +1758,6 @@ ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
1705
1758
  }
1706
1759
  }
1707
1760
  },
1708
- output: stringOut,
1709
1761
  async execute(args) {
1710
1762
  return guard(async () => {
1711
1763
  let video = false;
@@ -1722,8 +1774,8 @@ ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
1722
1774
  return `\u5DF2\u5199\u5165\u76EE\u5F55\uFF1A${item.chapters.length} \u7AE0 ${progressOf(item).total} \u8282\u3002\u7528\u6237\u53EF\u4EE5\u5728\u53F3\u4FA7\u7B14\u8BB0\u672C\u91CC\u67E5\u770B\u548C\u6253\u52FE\u3002`;
1723
1775
  });
1724
1776
  }
1725
- }),
1726
- defineTool({
1777
+ },
1778
+ {
1727
1779
  name: "palace_set_target_dates",
1728
1780
  description: "\u8BBE\u7F6E\u4E66\u7684\u76EE\u6807\u5B8C\u6210\u65E5\u671F\uFF08YYYY-MM-DD\uFF09\uFF0C\u7528\u4E8E\u89C4\u5212\u8FDB\u5EA6\u3002\u53EF\u4E00\u6B21\u8BBE\u7F6E\u591A\u672C\u3002\u89C4\u5212\u65F6\u53C2\u8003\u5404\u4E66\u7684\u8282\u6570\uFF0C\u5E76\u7ED9\u590D\u4E60\u7559\u51FA\u65F6\u95F4\u3002\u8BBE\u7F6E\u524D\u5148\u548C\u7528\u6237\u786E\u8BA4\u65B9\u6848\u3002",
1729
1781
  parameters: {
@@ -1740,7 +1792,6 @@ ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
1740
1792
  }
1741
1793
  }
1742
1794
  },
1743
- output: stringOut,
1744
1795
  async execute(args) {
1745
1796
  return guard(async () => {
1746
1797
  const lines = [];
@@ -1754,8 +1805,8 @@ ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
1754
1805
  ${lines.join("\n")}`;
1755
1806
  });
1756
1807
  }
1757
- }),
1758
- defineTool({
1808
+ },
1809
+ {
1759
1810
  name: "palace_add_card",
1760
1811
  description: "\u8BB0\u4E00\u5F20\u5361\u7247\u3002type=mistake \u9519\u9898\uFF1A\u7528\u6237\u53D1\u6765\u9519\u9898\u622A\u56FE\u5E76\u8BF4\u300C\u8BB0\u9519\u9898\u300D\u65F6\u8C03\u7528\uFF0C\u5148\u770B\u56FE\u628A\u9898\u76EE\u5B8C\u6574\u8F6C\u6210\u6587\u5B57\u5199\u8FDB quote\uFF08\u542B\u9009\u9879\uFF09\uFF0C\u80FD\u5224\u65AD\u7684\u8BDD\u628A\u7B54\u6848\u548C\u89E3\u6790\u5199\u8FDB answer\uFF1Btype=excerpt \u6458\u5F55\uFF1Aquote \u662F\u539F\u6587\uFF0Cnote \u662F\u7528\u6237\u7684\u60F3\u6CD5\uFF1Btype=thought \u60F3\u6CD5\uFF1A\u53EA\u6709 note\u3002image \u586B\u5BF9\u8BDD\u91CC\u56FE\u7247\u7684\u672C\u5730\u53EA\u8BFB\u8DEF\u5F84\uFF08\u56FE\u7247\u8BF4\u660E\u4E2D\u7684 read-only path\uFF09\uFF0C\u6CA1\u6709\u8DEF\u5F84\u5C31\u586B\u9644\u4EF6 id\uFF08sha256:...\uFF09\u3002item_id \u4E0D\u586B\u65F6\u8BB0\u5230\u7528\u6237\u5F53\u524D\u6253\u5F00\u7684\u683C\u5B50\uFF1Bsection_id \u4E0D\u586B\u65F6\uFF0C\u6458\u5F55\u548C\u9519\u9898\u9ED8\u8BA4\u6302\u5230\u5F53\u524D\u5728\u5B66\u7684\u90A3\u4E00\u8282\u3002",
1761
1812
  parameters: {
@@ -1767,20 +1818,19 @@ ${lines.join("\n")}`;
1767
1818
  item_id: { type: "string" },
1768
1819
  section_id: { type: "string" }
1769
1820
  },
1770
- output: stringOut,
1771
- async execute(args, exec) {
1821
+ async execute(args, options) {
1772
1822
  return guard(async () => {
1773
- const id = targetOf(args.item_id, exec);
1823
+ const id = args.item_id;
1774
1824
  const item = store.getItem(id);
1775
1825
  const sectionId = args.section_id ?? (args.type === "thought" ? void 0 : progressOf(item).current?.section.id);
1776
- const image = saveImage(store, args.image);
1826
+ const image = sources.saveImage(args.image);
1777
1827
  const card = await addCard(store, { id, type: args.type, quote: args.quote, note: args.note, answer: args.answer, image, sectionId });
1778
1828
  const title = store.getPalace().cells.find((c) => c.id === id)?.title;
1779
- return `\u5DF2\u8BB0\u5165\u300A${title}\u300B${where(store.getItem(id), card)}\u7684${CARD_LABEL[card.type]}` + (args.image ? image ? "\uFF0C\u539F\u56FE\u5DF2\u4FDD\u5B58\u3002" : "\u3002\uFF08\u6CA1\u6709\u627E\u5230\u539F\u56FE\uFF09" : "\u3002") + nudge(exec, id, "wiki");
1829
+ return `\u5DF2\u8BB0\u5165\u300A${title}\u300B${where(store.getItem(id), card)}\u7684${CARD_LABEL[card.type]}` + (args.image ? image ? "\uFF0C\u539F\u56FE\u5DF2\u4FDD\u5B58\u3002" : "\u3002\uFF08\u6CA1\u6709\u627E\u5230\u539F\u56FE\uFF09" : "\u3002") + (options.nudge?.(id, "wiki") ?? "");
1780
1830
  });
1781
1831
  }
1782
- }),
1783
- defineTool({
1832
+ },
1833
+ {
1784
1834
  name: "palace_add_inspiration",
1785
1835
  description: "\u628A\u8FD9\u6B21\u5BF9\u8BDD\u5B58\u6210\u4E00\u6761\u300C\u7075\u611F\u300D\uFF1A\u7528\u6237\u8BF4\u300C\u5B58\u6210\u7075\u611F\u300D\u300C\u8BB0\u4E0B\u6765\u300D\u4E4B\u7C7B\u65F6\u8C03\u7528\u3002\u7528\u7528\u6237\u7684\u53E3\u543B\u3001\u7B80\u6D01\u5730\u603B\u7ED3\u8FD9\u6B21\u5BF9\u8BDD\u91CC\u7528\u6237\u7684\u60F3\u6CD5\u548C\u5F97\u51FA\u7684\u7ED3\u8BBA\uFF08\u4E0D\u662F\u590D\u8FF0 AI \u8BF4\u4E86\u4EC0\u4E48\uFF09\uFF0Cbody \u7528 Markdown\uFF0C\u51E0\u6BB5\u5373\u53EF\uFF1Btitle \u4E00\u53E5\u8BDD\u70B9\u9898\u3002item_id \u4E0D\u586B\u65F6\u5B58\u5230\u7528\u6237\u5F53\u524D\u6253\u5F00\u7684\u683C\u5B50\u3002",
1786
1836
  parameters: {
@@ -1789,23 +1839,21 @@ ${lines.join("\n")}`;
1789
1839
  item_id: { type: "string" },
1790
1840
  card_ids: { type: "array", items: { type: "string" }, description: "\u53EF\u9009\uFF1A\u8FD9\u6B21\u5BF9\u8BDD\u6D89\u53CA\u7684\u5361\u7247" }
1791
1841
  },
1792
- output: stringOut,
1793
- async execute(args, exec) {
1842
+ async execute(args, options) {
1794
1843
  return guard(async () => {
1795
- const id = targetOf(args.item_id, exec);
1796
- const insp = await addInspiration(store, { id, title: args.title, body: args.body, cardIds: args.card_ids, sessionId: exec.agent?.id });
1844
+ const id = args.item_id;
1845
+ const insp = await addInspiration(store, { id, title: args.title, body: args.body, cardIds: args.card_ids, ...options.attribution });
1797
1846
  const title = store.getPalace().cells.find((c) => c.id === id)?.title;
1798
- return `\u5DF2\u5B58\u5230\u300A${title}\u300B\u7684\u7075\u611F\uFF1A\u300C${insp.title}\u300D id=${insp.id}` + nudge(exec, id, "link") + nudge(exec, id, "wiki");
1847
+ return `\u5DF2\u5B58\u5230\u300A${title}\u300B\u7684\u7075\u611F\uFF1A\u300C${insp.title}\u300D id=${insp.id}` + (options.nudge?.(id, "link") ?? "") + (options.nudge?.(id, "wiki") ?? "");
1799
1848
  });
1800
1849
  }
1801
- }),
1802
- defineTool({
1850
+ },
1851
+ {
1803
1852
  name: "palace_wiki_read",
1804
1853
  description: "\u8BFB\u4E00\u9875\u8BB0\u5FC6\uFF1Atarget \u586B\u683C\u5B50 id \u8BFB\u8FD9\u4E00\u683C\u7684 wiki\uFF08\u4E00\u53E5\u8BDD\u3001\u4E3B\u9898\u8BCD\u3001\u6B63\u6587\uFF09\uFF0C\u586B me \u8BFB\u300C\u5173\u4E8E\u7528\u6237\u300D\u3002\u5F53\u524D\u683C\u5B50\u548C me \u5DF2\u7ECF\u5728\u5BF9\u8BDD\u80CC\u666F\u91CC\uFF0C\u8BFB\u522B\u7684\u683C\u5B50\u65F6\u624D\u9700\u8981\u8C03\u7528\u3002",
1805
1854
  parameters: {
1806
1855
  target: { type: "string", required: true, description: "\u683C\u5B50 id \u6216 me" }
1807
1856
  },
1808
- output: stringOut,
1809
1857
  async execute(args) {
1810
1858
  return guard(async () => {
1811
1859
  const page = memory.readPage(args.target);
@@ -1826,8 +1874,8 @@ ${page.body}` + (debt > 0 ? `
1826
1874
  ${page.body}`;
1827
1875
  });
1828
1876
  }
1829
- }),
1830
- defineTool({
1877
+ },
1878
+ {
1831
1879
  name: "palace_wiki_write",
1832
1880
  description: `\u6574\u9875\u91CD\u5199\u4E00\u9875\u8BB0\u5FC6\uFF08\u4F1A\u8986\u76D6\u65E7\u5185\u5BB9\uFF0C\u5148\u5728\u65E7\u5185\u5BB9\u57FA\u7840\u4E0A\u6539\uFF09\u3002\u6B63\u6587\u6700\u591A ${PAGE_LIMIT} \u5B57\uFF0C\u8D85\u4E86\u4F1A\u88AB\u62D2\u7EDD\uFF1A\u5408\u5E76\u76F8\u8FD1\u7684\u3001\u5220\u6389\u8FC7\u65F6\u7684\u518D\u5199\u3002target=me\uFF1A\u5173\u4E8E\u7528\u6237\u7684\u957F\u671F\u8BB0\u5FC6\uFF0C\u5206\u300C\u957F\u671F\u300D\u300C\u6700\u8FD1\u300D\u4E24\u6BB5\uFF0C\u53EA\u8BB0\u7528\u6237\u8BF4\u8FC7\u6216\u505A\u8FC7\u7684\u4E8B\u5B9E\uFF08\u559C\u597D\u3001\u6B63\u5728\u7ECF\u5386\u7684\u4E8B\u3001\u6DF1\u5165\u804A\u8FC7\u7684\u8BDD\u9898\uFF09\uFF0C\u4E0D\u8D34\u6027\u683C\u6807\u7B7E\u3002target=\u683C\u5B50 id\uFF1A\u8FD9\u4E00\u683C\u7684 wiki\uFF0C\u53EA\u5199\u5361\u7247\u548C\u7075\u611F\u91CC\u6CA1\u6709\u7684\u4E1C\u897F\uFF08\u5BF9\u7528\u6237\u610F\u5473\u7740\u4EC0\u4E48\u3001\u5361\u5728\u54EA\u3001\u53CD\u590D\u51FA\u73B0\u7684\u4E3B\u9898\u3001\u6CA1\u89E3\u51B3\u7684\u7591\u95EE\uFF09\uFF0C\u53E5\u672B\u7528 [^\u5361\u7247\u6216\u7075\u611F id] \u6807\u51FA\u4F9D\u636E\uFF1Bsummary \u4E00\u53E5\u8BDD\uFF08\u226440 \u5B57\uFF09\uFF0Ctopics 3\u20135 \u4E2A\u4E3B\u9898\u8BCD\uFF08\u81EA\u7531\u586B\u5199\uFF0C\u8DE8\u683C\u5B50\u76F8\u901A\u7684\u8BCD\u66F4\u597D\uFF0C\u6BD4\u5982\u300C\u7B49\u5F85\u300D\u300C\u5B64\u72EC\u300D\uFF09\u3002`,
1833
1881
  parameters: {
@@ -1836,7 +1884,6 @@ ${page.body}`;
1836
1884
  summary: { type: "string" },
1837
1885
  topics: { type: "array", items: { type: "string" } }
1838
1886
  },
1839
- output: stringOut,
1840
1887
  async execute(args) {
1841
1888
  return guard(async () => {
1842
1889
  const page = memory.writePage(args.target, { body: args.body, summary: args.summary, topics: args.topics });
@@ -1845,8 +1892,8 @@ ${page.body}`;
1845
1892
  return `\u5DF2\u66F4\u65B0\u300A${title}\u300B\u7684 wiki` + (page.topics?.length ? `\uFF08\u4E3B\u9898\u8BCD\uFF1A${page.topics.join("\u3001")}\uFF09` : "") + "\u3002";
1846
1893
  });
1847
1894
  }
1848
- }),
1849
- defineTool({
1895
+ },
1896
+ {
1850
1897
  name: "palace_link",
1851
1898
  description: "\u5728\u4E24\u683C\u4E4B\u95F4\u8FDE\u4E00\u6761\u7535\u5F27\uFF08\u8054\u60F3\uFF09\uFF1A\u7528\u6237\u7684\u67D0\u4E2A\u60F3\u6CD5\u3001\u4E3B\u9898\u6216\u7591\u95EE\u628A\u8FD9\u4E24\u683C\u8FDE\u5728\u4E00\u8D77\u65F6\u8C03\u7528\u3002\u4E0D\u9700\u8981\u4E24\u672C\u4E66\u4E92\u76F8\u63D0\u53CA\uFF0C\u8FDE\u7684\u662F\u7528\u6237\u7684\u5FF5\u5934\u3002why \u4E00\u53E5\u8BDD\u8BF4\u6E05\u8FDE\u5728\u54EA\uFF08\u226440 \u5B57\uFF0C\u4F8B\u5982\u300C\u90FD\u5728\u5199\u7B49\u5F85\uFF1A\u76D6\u8328\u6BD4\u7B49\u7EFF\u706F\uFF0C\u4F60\u5728\u7B49 offer\u300D\uFF09\uFF1Bevidence \u586B\u4E24\u7AEF\u5404\u81F3\u5C11\u4E00\u5F20\u5361\u7247\u6216\u4E00\u6761\u7075\u611F\u7684 id\u3002\u540C\u6837\u7684\u8BC1\u636E\u4E0D\u80FD\u91CD\u590D\u8FDE\uFF1B\u6709\u65B0\u8BC1\u636E\u5C31\u518D\u8FDE\u4E00\u6761\uFF0C\u7535\u5F27\u4F1A\u53D8\u7C97\u3002\u7528\u6237\u526A\u65AD\u8FC7\u7684\u4E24\u683C\uFF0C\u9664\u975E\u7528\u6237\u660E\u786E\u8981\u6C42\uFF0C\u5426\u5219\u4E0D\u8981\u518D\u8FDE\u3002",
1852
1899
  parameters: {
@@ -1856,40 +1903,86 @@ ${page.body}`;
1856
1903
  evidence: { type: "array", items: { type: "string" }, required: true, description: "\u5361\u7247 / \u7075\u611F id" },
1857
1904
  user_asked: { type: "boolean", description: "\u7528\u6237\u660E\u786E\u8981\u6C42\u8FDE\uFF08\u526A\u65AD\u8FC7\u7684\u4E5F\u80FD\u91CD\u8FDE\uFF09" }
1858
1905
  },
1859
- output: stringOut,
1860
- async execute(args, exec) {
1906
+ async execute(args, options) {
1861
1907
  return guard(async () => {
1862
- const link = memory.addLink({ from: args.from, to: args.to, why: args.why, evidence: args.evidence, sessionId: exec.agent?.id, userAsked: args.user_asked === true });
1908
+ const link = memory.addLink({ from: args.from, to: args.to, why: args.why, evidence: args.evidence, ...options.attribution, userAsked: args.user_asked === true });
1863
1909
  const cells = store.getPalace().cells;
1864
1910
  const arc = memory.arcs().find((a) => a.links.some((l) => l.id === link.id));
1865
1911
  return `\u5DF2\u8FDE\u4E0A\u300A${cells.find((c) => c.id === link.from)?.title}\u300B\u2194\u300A${cells.find((c) => c.id === link.to)?.title}\u300B\uFF1A${link.why}\uFF08\u73B0\u5728 ${arc?.strands ?? 1} \u80A1\uFF09`;
1866
1912
  });
1867
1913
  }
1868
- }),
1869
- defineTool({
1914
+ },
1915
+ {
1870
1916
  name: "palace_unlink",
1871
1917
  description: "\u526A\u65AD\u4E24\u683C\u4E4B\u95F4\u7684\u7535\u5F27\uFF08\u7528\u6237\u8BF4\u8FD9\u4E24\u4E2A\u6CA1\u5173\u7CFB\u3001\u8FDE\u9519\u4E86\u65F6\u8C03\u7528\uFF09\u3002\u4E4B\u524D\u7684\u8054\u60F3\u90FD\u4E0D\u518D\u663E\u793A\uFF0C\u4EE5\u540E\u4E5F\u4E0D\u4F1A\u81EA\u52A8\u518D\u8FDE\u3002",
1872
1918
  parameters: {
1873
1919
  from: { type: "string", required: true },
1874
1920
  to: { type: "string", required: true }
1875
1921
  },
1876
- output: stringOut,
1877
1922
  async execute(args) {
1878
1923
  return guard(async () => {
1879
1924
  memory.cut({ from: args.from, to: args.to });
1880
1925
  return "\u5DF2\u526A\u65AD\u3002";
1881
1926
  });
1882
1927
  }
1883
- })
1928
+ }
1884
1929
  ];
1885
1930
  }
1886
1931
 
1932
+ // src/host/tools.ts
1933
+ var stringOut = {
1934
+ schema: { type: "string" },
1935
+ render: (_args, value) => [{ type: "text", text: value }]
1936
+ };
1937
+ function createTools(store, active, memory = new Memory(store)) {
1938
+ const nudged = /* @__PURE__ */ new Set();
1939
+ const nudge = (exec, id, kind) => {
1940
+ const key = `${exec.agent?.id ?? ""}:${id}:${kind}`;
1941
+ if (nudged.has(key)) return "";
1942
+ if (kind === "wiki") {
1943
+ const debt = memory.debt(id);
1944
+ if (debt < 3) return "";
1945
+ nudged.add(key);
1946
+ return `
1947
+ \uFF08\u63D0\u793A\uFF1A\u8FD9\u4E00\u683C\u7684 wiki \u4E4B\u540E\u65B0\u589E\u4E86 ${debt} \u6761\u5361\u7247 / \u7075\u611F\uFF0C\u804A\u5B8C\u65B9\u4FBF\u65F6\u7528 palace_wiki_write \u66F4\u65B0\uFF1B\u4E0D\u7528\u6253\u65AD\u7528\u6237\u3002\uFF09`;
1948
+ }
1949
+ nudged.add(key);
1950
+ return "\n\uFF08\u63D0\u793A\uFF1A\u5982\u679C\u8FD9\u6761\u548C\u5BAB\u6BBF\u91CC\u522B\u7684\u683C\u5B50\u6709\u5173\u2014\u2014\u540C\u4E00\u4E2A\u5FF5\u5934\u3001\u4E3B\u9898\u6216\u7591\u95EE\u2014\u2014\u53EF\u4EE5\u7528 palace_link \u8FDE\u4E00\u6761\u7535\u5F27\u3002\uFF09";
1951
+ };
1952
+ const activeOf = (exec) => exec.agent ? active.get(exec.agent.id) : void 0;
1953
+ return createPalaceTools(store, memory, {
1954
+ addBook: (args, signal) => addBook(store, args, signal),
1955
+ readPageTexts,
1956
+ scannedThreshold: SCANNED_THRESHOLD,
1957
+ saveImage: (ref) => saveImage(store, ref)
1958
+ }).map(({ execute, ...definition }) => defineTool({
1959
+ ...definition,
1960
+ output: stringOut,
1961
+ async execute(args, exec) {
1962
+ const highlightId = activeOf(exec);
1963
+ if (definition.name === "palace_add_card" || definition.name === "palace_add_inspiration") {
1964
+ const id = typeof args.item_id === "string" && args.item_id !== "" ? args.item_id : highlightId;
1965
+ if (id === void 0) return "\u64CD\u4F5C\u5931\u8D25\uFF1A\u4E0D\u77E5\u9053\u8BB0\u5230\u54EA\u4E2A\u683C\u5B50\uFF1A\u7528\u6237\u6CA1\u6709\u6253\u5F00\u683C\u5B50\u3002\u8BF7\u5148\u7528 palace_overview \u67E5\u770B\u540E\u6307\u5B9A item_id\uFF0C\u6216\u8BF7\u7528\u6237\u5728\u53F3\u4FA7 Memory Palace \u6253\u5F00\u5BF9\u5E94\u7684\u4E66\u3002";
1966
+ args = { ...args, item_id: id };
1967
+ }
1968
+ return execute(args, {
1969
+ signal: exec.signal,
1970
+ highlightId,
1971
+ attribution: { sessionId: exec.agent?.id },
1972
+ nudge: (id, kind) => nudge(exec, id, kind)
1973
+ });
1974
+ }
1975
+ }));
1976
+ }
1977
+
1887
1978
  // src/host/index.ts
1888
1979
  var name = "dsh-palace";
1889
1980
  var defaultDataDir = () => join6(homedir3(), "Documents", "dsh-palace-data");
1890
1981
  function apply(ctx, config = {}) {
1891
1982
  const dir = config.dataDir ?? defaultDataDir();
1983
+ const firstRun = !existsSync5(join6(dir, "palace.json"));
1892
1984
  const store = new PalaceStore(dir);
1985
+ if (firstRun) void seedWelcome(store).catch((error) => console.warn("[dsh-palace] \u653E\u5165\u4ECB\u7ECD\u683C\u5B50\u5931\u8D25", error));
1893
1986
  const active = /* @__PURE__ */ new Map();
1894
1987
  const memory = new Memory(store);
1895
1988
  ctx.inject(["tools"], (scope) => {