dsh-palace 1.3.0 → 1.3.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.
- package/README.md +17 -2
- package/client/client.js +31 -23
- package/lib/index.js +652 -58
- package/lib/mcp.js +70 -66
- package/lib/palace-web.js +33 -25
- package/package.json +9 -4
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// src/host/index.ts
|
|
1
|
+
// src/host/harness/index.ts
|
|
2
2
|
import { existsSync as existsSync5 } from "node:fs";
|
|
3
|
-
import { homedir as
|
|
4
|
-
import { join as
|
|
3
|
+
import { homedir as homedir4 } from "node:os";
|
|
4
|
+
import { join as join8, resolve } from "node:path";
|
|
5
5
|
|
|
6
6
|
// src/core/memory.ts
|
|
7
7
|
import { appendFileSync, existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, renameSync as renameSync2, statSync as statSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
@@ -381,6 +381,12 @@ var SUMMARY_LIMIT = 40;
|
|
|
381
381
|
var WHY_LIMIT = 40;
|
|
382
382
|
var TOPIC_LIMIT = 5;
|
|
383
383
|
var charCount = (s) => [...s].length;
|
|
384
|
+
function linkWhy(value) {
|
|
385
|
+
if (typeof value !== "string" || value.trim() === "") throw new ShelfError("\u8981\u5199\u4E00\u53E5\u7406\u7531");
|
|
386
|
+
const why = value.trim().replace(/\n/g, " ");
|
|
387
|
+
if (charCount(why) > WHY_LIMIT) throw new ShelfError(`\u7406\u7531\u6700\u591A ${WHY_LIMIT} \u5B57`);
|
|
388
|
+
return why;
|
|
389
|
+
}
|
|
384
390
|
var pairKey = (a, b) => a < b ? `${a}|${b}` : `${b}|${a}`;
|
|
385
391
|
function recordsOf(item) {
|
|
386
392
|
const out = /* @__PURE__ */ new Map();
|
|
@@ -496,19 +502,20 @@ var Memory = class {
|
|
|
496
502
|
if (line.trim() === "") continue;
|
|
497
503
|
try {
|
|
498
504
|
const r = JSON.parse(line);
|
|
499
|
-
if ((r.type === "link" || r.type === "cut") && typeof r.from === "string" && typeof r.to === "string") records.push(r);
|
|
505
|
+
if ((r.type === "link" || r.type === "cut" || r.type === "retitle" && typeof r.why === "string") && typeof r.from === "string" && typeof r.to === "string") records.push(r);
|
|
500
506
|
} catch {
|
|
501
507
|
}
|
|
502
508
|
}
|
|
503
509
|
this.cache = { stamp: stamp2, records };
|
|
504
510
|
return records;
|
|
505
511
|
}
|
|
506
|
-
append(
|
|
512
|
+
append(records) {
|
|
513
|
+
if (records.length === 0) return;
|
|
507
514
|
this.store.assertWritable();
|
|
508
515
|
this.store.snapshotBeforeWrite();
|
|
509
516
|
const size = existsSync2(this.linksFile) ? statSync2(this.linksFile).size : 0;
|
|
510
517
|
const broken = size > 0 && readFileSync2(this.linksFile, "utf8").at(-1) !== "\n";
|
|
511
|
-
appendFileSync(this.linksFile, (broken ? "\n" : "") + JSON.stringify(record) + "\n", "utf8");
|
|
518
|
+
appendFileSync(this.linksFile, (broken ? "\n" : "") + records.map((record) => JSON.stringify(record)).join("\n") + "\n", "utf8");
|
|
512
519
|
this.store.rev += 1;
|
|
513
520
|
}
|
|
514
521
|
cellOf(id, allowGroup) {
|
|
@@ -527,9 +534,7 @@ var Memory = class {
|
|
|
527
534
|
const from = this.cellOf(args.from, false);
|
|
528
535
|
const to = this.cellOf(args.to, false);
|
|
529
536
|
if (from.id === to.id) throw new ShelfError("\u4E0D\u80FD\u8FDE\u5230\u81EA\u5DF1");
|
|
530
|
-
|
|
531
|
-
const why = args.why.trim().replace(/\n/g, " ");
|
|
532
|
-
if (charCount(why) > WHY_LIMIT) throw new ShelfError(`\u7406\u7531\u6700\u591A ${WHY_LIMIT} \u5B57`);
|
|
537
|
+
const why = linkWhy(args.why);
|
|
533
538
|
if (!Array.isArray(args.evidence)) throw new ShelfError("evidence \u5E94\u4E3A\u5361\u7247 / \u7075\u611F id \u7684\u6570\u7EC4");
|
|
534
539
|
const evidence = [...new Set(args.evidence.filter((e) => typeof e === "string"))];
|
|
535
540
|
const a = recordsOf(this.store.getItem(from.id));
|
|
@@ -541,14 +546,43 @@ var Memory = class {
|
|
|
541
546
|
const used = new Set(this.validLinks().filter((l) => pairKey(l.from, l.to) === pairKey(from.id, to.id)).flatMap((l) => l.evidence));
|
|
542
547
|
if (evidence.every((e) => used.has(e))) throw new ShelfError("\u8FD9\u4E9B\u8BC1\u636E\u5DF2\u7ECF\u8FDE\u8FC7\u4E86\uFF1B\u6709\u65B0\u7684\u5361\u7247\u6216\u7075\u611F\u518D\u8FDE");
|
|
543
548
|
const record = { type: "link", id: newId("l"), from: from.id, to: to.id, why, evidence, ...args.sessionId ? { sessionId: args.sessionId } : {}, createdAt: nowIso() };
|
|
544
|
-
this.append(record);
|
|
549
|
+
this.append([record]);
|
|
545
550
|
return record;
|
|
546
551
|
}
|
|
552
|
+
/** 导入双链按来源键去重。用户剪过的格子对继续保留断开状态。 */
|
|
553
|
+
previewImportedLinks(links) {
|
|
554
|
+
const records = this.records();
|
|
555
|
+
const cuts = new Set(records.filter((r) => r.type === "cut").map((r) => pairKey(r.from, r.to)));
|
|
556
|
+
const known = new Set(records.flatMap((r) => r.type === "link" && r.sourceKey ? [r.sourceKey] : []));
|
|
557
|
+
const active = this.validLinks();
|
|
558
|
+
const activeKeys = new Set(active.map((r) => r.sourceKey));
|
|
559
|
+
const activePairs = new Set(active.map((r) => pairKey(r.from, r.to)));
|
|
560
|
+
const eligible = links.filter((link) => !cuts.has(pairKey(link.from, link.to)));
|
|
561
|
+
const added = eligible.filter((link) => !activeKeys.has(link.sourceKey));
|
|
562
|
+
return {
|
|
563
|
+
pending: eligible.filter((link) => !known.has(link.sourceKey)),
|
|
564
|
+
arcs: new Set(added.map((link) => pairKey(link.from, link.to)).filter((pair) => !activePairs.has(pair))).size,
|
|
565
|
+
strands: added.length
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
addImportedLinks(links) {
|
|
569
|
+
if (links.length === 0) return;
|
|
570
|
+
const { pending } = this.previewImportedLinks(links);
|
|
571
|
+
this.append(pending.map((link) => ({ ...link, type: "link", id: newId("l"), why: linkWhy(link.why), createdAt: nowIso() })));
|
|
572
|
+
}
|
|
573
|
+
retitleLink(args) {
|
|
574
|
+
const from = this.cellOf(args.from, false).id;
|
|
575
|
+
const to = this.cellOf(args.to, false).id;
|
|
576
|
+
const why = linkWhy(args.why);
|
|
577
|
+
const arc = this.arcs().find((arc2) => pairKey(arc2.from, arc2.to) === pairKey(from, to));
|
|
578
|
+
if (!arc) throw new ShelfError("\u8FD9\u4E24\u683C\u4E4B\u95F4\u6CA1\u6709\u6709\u6548\u7535\u5F27");
|
|
579
|
+
if (arc.why !== why) this.append([{ type: "retitle", from, to, why }]);
|
|
580
|
+
}
|
|
547
581
|
/** 用户剪断两格之间的电弧(追加记录,不删历史)。 */
|
|
548
582
|
cut(args) {
|
|
549
583
|
const from = this.cellOf(args.from, true);
|
|
550
584
|
const to = this.cellOf(args.to, true);
|
|
551
|
-
this.append({ type: "cut", from: from.id, to: to.id, createdAt: nowIso() });
|
|
585
|
+
this.append([{ type: "cut", from: from.id, to: to.id, createdAt: nowIso() }]);
|
|
552
586
|
}
|
|
553
587
|
/** 还有效的联想:两端格子都在、在最近一次剪断之后、两端各至少还剩一条证据。 */
|
|
554
588
|
validLinks() {
|
|
@@ -562,12 +596,15 @@ var Memory = class {
|
|
|
562
596
|
}
|
|
563
597
|
return r;
|
|
564
598
|
};
|
|
599
|
+
const records = this.records();
|
|
565
600
|
const cuts = /* @__PURE__ */ new Map();
|
|
566
|
-
for (const r of
|
|
567
|
-
|
|
568
|
-
|
|
601
|
+
for (const r of records) {
|
|
602
|
+
if (r.type === "cut") {
|
|
603
|
+
const key = pairKey(r.from, r.to);
|
|
604
|
+
if (r.createdAt > (cuts.get(key) ?? "")) cuts.set(key, r.createdAt);
|
|
605
|
+
}
|
|
569
606
|
}
|
|
570
|
-
return
|
|
607
|
+
return records.filter((r) => {
|
|
571
608
|
if (r.type !== "link" || !cells.has(r.from) || !cells.has(r.to)) return false;
|
|
572
609
|
if (r.createdAt <= (cuts.get(pairKey(r.from, r.to)) ?? "")) return false;
|
|
573
610
|
const ev = Array.isArray(r.evidence) ? r.evidence : [];
|
|
@@ -576,14 +613,21 @@ var Memory = class {
|
|
|
576
613
|
}
|
|
577
614
|
/** 电弧:按格子对聚合有效联想。 */
|
|
578
615
|
arcs() {
|
|
616
|
+
const titles = /* @__PURE__ */ new Map();
|
|
617
|
+
for (const r of this.records()) {
|
|
618
|
+
if (r.type === "cut") titles.delete(pairKey(r.from, r.to));
|
|
619
|
+
if (r.type === "retitle") titles.set(pairKey(r.from, r.to), r.why);
|
|
620
|
+
}
|
|
579
621
|
const byPair = /* @__PURE__ */ new Map();
|
|
580
622
|
for (const l of this.validLinks()) {
|
|
581
623
|
const k = pairKey(l.from, l.to);
|
|
582
|
-
|
|
624
|
+
const links = byPair.get(k);
|
|
625
|
+
if (links) links.push(l);
|
|
626
|
+
else byPair.set(k, [l]);
|
|
583
627
|
}
|
|
584
628
|
return [...byPair.values()].map((links) => {
|
|
585
629
|
const latest = links.reduce((x, y) => y.createdAt >= x.createdAt ? y : x);
|
|
586
|
-
return { from: latest.from, to: latest.to, strands: links.length, why: latest.why, lastAt: latest.createdAt, links };
|
|
630
|
+
return { from: latest.from, to: latest.to, strands: links.length, why: titles.get(pairKey(latest.from, latest.to)) ?? latest.why, lastAt: latest.createdAt, links };
|
|
587
631
|
}).sort((x, y) => y.strands - x.strands || (y.lastAt > x.lastAt ? 1 : -1));
|
|
588
632
|
}
|
|
589
633
|
// ---------- 对话背景 ----------
|
|
@@ -1052,8 +1096,8 @@ async function deleteInspiration(store, args) {
|
|
|
1052
1096
|
// src/host/routes.ts
|
|
1053
1097
|
import { execFile as execFile2, spawn } from "node:child_process";
|
|
1054
1098
|
import { createReadStream, existsSync as existsSync4, statSync as statSync4 } from "node:fs";
|
|
1055
|
-
import { homedir as
|
|
1056
|
-
import { basename as
|
|
1099
|
+
import { homedir as homedir3 } from "node:os";
|
|
1100
|
+
import { basename as basename3, extname as extname2, join as join6, normalize, sep as sep3 } from "node:path";
|
|
1057
1101
|
import { Readable } from "node:stream";
|
|
1058
1102
|
import { fileURLToPath } from "node:url";
|
|
1059
1103
|
|
|
@@ -1176,7 +1220,7 @@ async function readPdfInfo(file) {
|
|
|
1176
1220
|
try {
|
|
1177
1221
|
let items = await doc.getOutline() ?? [];
|
|
1178
1222
|
while (items.length === 1 && items[0].items.length > 0) items = items[0].items;
|
|
1179
|
-
const
|
|
1223
|
+
const outline2 = [];
|
|
1180
1224
|
for (const item of items) {
|
|
1181
1225
|
const sections = [];
|
|
1182
1226
|
for (const child of item.items) {
|
|
@@ -1184,7 +1228,7 @@ async function readPdfInfo(file) {
|
|
|
1184
1228
|
if (title3 !== "") sections.push({ title: title3, page: await pageOf(doc, child.dest) });
|
|
1185
1229
|
}
|
|
1186
1230
|
const title2 = clean(item.title);
|
|
1187
|
-
if (title2 !== "")
|
|
1231
|
+
if (title2 !== "") outline2.push({ title: title2, page: await pageOf(doc, item.dest), sections });
|
|
1188
1232
|
}
|
|
1189
1233
|
let title;
|
|
1190
1234
|
try {
|
|
@@ -1193,7 +1237,7 @@ async function readPdfInfo(file) {
|
|
|
1193
1237
|
if (typeof info?.Title === "string" && info.Title.trim() !== "") title = info.Title.trim();
|
|
1194
1238
|
} catch {
|
|
1195
1239
|
}
|
|
1196
|
-
return { pageCount: doc.numPages, title, outline };
|
|
1240
|
+
return { pageCount: doc.numPages, title, outline: outline2 };
|
|
1197
1241
|
} finally {
|
|
1198
1242
|
await close();
|
|
1199
1243
|
}
|
|
@@ -1232,7 +1276,7 @@ async function addBook(store, args, signal, { readVideo = true } = {}) {
|
|
|
1232
1276
|
};
|
|
1233
1277
|
let source;
|
|
1234
1278
|
let title;
|
|
1235
|
-
let
|
|
1279
|
+
let outline2 = [];
|
|
1236
1280
|
let outlineSource;
|
|
1237
1281
|
if (typeof args.url === "string" && args.url.trim() !== "") {
|
|
1238
1282
|
const url = args.url.trim();
|
|
@@ -1248,7 +1292,7 @@ async function addBook(store, args, signal, { readVideo = true } = {}) {
|
|
|
1248
1292
|
});
|
|
1249
1293
|
source = { type: "video", url: course.url };
|
|
1250
1294
|
title = course.title;
|
|
1251
|
-
|
|
1295
|
+
outline2 = course.chapters.map((c) => ({ title: c.title, sections: c.episodes.map((e) => ({ title: e.title, loc: e.url })) }));
|
|
1252
1296
|
outlineSource = "video";
|
|
1253
1297
|
} else if (typeof args.file === "string" && args.file.trim() !== "") {
|
|
1254
1298
|
const file = expandHome(args.file.trim().replace(/^['"]|['"]$/g, ""));
|
|
@@ -1269,7 +1313,7 @@ async function addBook(store, args, signal, { readVideo = true } = {}) {
|
|
|
1269
1313
|
});
|
|
1270
1314
|
source = { type: "pdf", path: file, pageCount: info.pageCount };
|
|
1271
1315
|
title = basename(file, extname(file));
|
|
1272
|
-
|
|
1316
|
+
outline2 = info.outline.map((c) => ({
|
|
1273
1317
|
title: c.title,
|
|
1274
1318
|
...c.page !== void 0 ? { loc: String(c.page) } : {},
|
|
1275
1319
|
sections: c.sections.map((s) => ({ title: s.title, ...s.page !== void 0 ? { loc: String(s.page) } : {} }))
|
|
@@ -1280,7 +1324,7 @@ async function addBook(store, args, signal, { readVideo = true } = {}) {
|
|
|
1280
1324
|
}
|
|
1281
1325
|
if (typeof args.title === "string" && args.title.trim() !== "") title = args.title.trim();
|
|
1282
1326
|
cancelled();
|
|
1283
|
-
return addCell(store, { kind: "book", title, subtitle: args.subtitle, source, parentId: args.parentId, targetDate: args.targetDate, outline, outlineSource });
|
|
1327
|
+
return addCell(store, { kind: "book", title, subtitle: args.subtitle, source, parentId: args.parentId, targetDate: args.targetDate, outline: outline2, outlineSource });
|
|
1284
1328
|
}
|
|
1285
1329
|
function isZip(file) {
|
|
1286
1330
|
const fd = openSync(file, "r");
|
|
@@ -1315,6 +1359,214 @@ function saveImage(store, ref) {
|
|
|
1315
1359
|
return name2;
|
|
1316
1360
|
}
|
|
1317
1361
|
|
|
1362
|
+
// src/host/weread.ts
|
|
1363
|
+
import { randomUUID } from "node:crypto";
|
|
1364
|
+
import { mkdirSync as mkdirSync4, readFileSync as readFileSync3, realpathSync, renameSync as renameSync3, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
|
|
1365
|
+
import { homedir as homedir2 } from "node:os";
|
|
1366
|
+
import { basename as basename2, dirname as dirname2, isAbsolute as isAbsolute2, join as join5, relative, sep as sep2 } from "node:path";
|
|
1367
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
1368
|
+
var GATEWAY = "https://i.weread.qq.com/api/agent/gateway";
|
|
1369
|
+
var SKILL_VERSION = "1.0.6";
|
|
1370
|
+
var validKey = (key) => typeof key === "string" && /^wrk-[A-Za-z0-9_-]+$/.test(key);
|
|
1371
|
+
var keyFile = join5(homedir2(), ".dsh-palace", "weread.json");
|
|
1372
|
+
function readKey(file = keyFile) {
|
|
1373
|
+
try {
|
|
1374
|
+
const { key } = JSON.parse(readFileSync3(file, "utf8"));
|
|
1375
|
+
if (validKey(key)) return key;
|
|
1376
|
+
} catch (error) {
|
|
1377
|
+
if (error.code === "ENOENT") return void 0;
|
|
1378
|
+
}
|
|
1379
|
+
throw new ShelfError("\u65E0\u6CD5\u8BFB\u53D6\u5FAE\u4FE1\u8BFB\u4E66 Key\uFF0C\u8BF7\u5728\u5BAB\u6BBF\u91CD\u65B0\u4FDD\u5B58");
|
|
1380
|
+
}
|
|
1381
|
+
function wereadKey(key, file = keyFile, dataDir) {
|
|
1382
|
+
if (key === void 0) return readKey(file) !== void 0;
|
|
1383
|
+
if (key !== null && !validKey(key)) throw new ShelfError("\u8BF7\u8F93\u5165\u6709\u6548\u7684\u5FAE\u4FE1\u8BFB\u4E66 API Key");
|
|
1384
|
+
const stage = file + "." + randomUUID();
|
|
1385
|
+
try {
|
|
1386
|
+
if (key === null) {
|
|
1387
|
+
rmSync2(file, { force: true });
|
|
1388
|
+
return false;
|
|
1389
|
+
}
|
|
1390
|
+
mkdirSync4(dirname2(file), { recursive: true, mode: 448 });
|
|
1391
|
+
if (dataDir !== void 0) {
|
|
1392
|
+
const location = relative(realpathSync(dataDir), join5(realpathSync(dirname2(file)), basename2(file)));
|
|
1393
|
+
if (!isAbsolute2(location) && location !== ".." && !location.startsWith(".." + sep2)) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66 Key \u5FC5\u987B\u4FDD\u5B58\u5728\u5BAB\u6BBF\u6570\u636E\u76EE\u5F55\u5916");
|
|
1394
|
+
}
|
|
1395
|
+
writeFileSync3(stage, JSON.stringify({ key }), { mode: 384, flag: "wx" });
|
|
1396
|
+
renameSync3(stage, file);
|
|
1397
|
+
return true;
|
|
1398
|
+
} catch (error) {
|
|
1399
|
+
if (error instanceof ShelfError) throw error;
|
|
1400
|
+
throw new ShelfError("\u65E0\u6CD5\u4FDD\u5B58\u6216\u5220\u9664\u5FAE\u4FE1\u8BFB\u4E66 Key");
|
|
1401
|
+
} finally {
|
|
1402
|
+
try {
|
|
1403
|
+
rmSync2(stage, { force: true });
|
|
1404
|
+
} catch {
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
var positionKey = ({ chapterUid, range }) => chapterUid != null && range?.trim() ? JSON.stringify([String(chapterUid), range]) : void 0;
|
|
1409
|
+
var responseList = {
|
|
1410
|
+
"/shelf/sync": "books",
|
|
1411
|
+
"/user/notebooks": "books",
|
|
1412
|
+
"/review/list/mine": "reviews",
|
|
1413
|
+
"/book/chapterinfo": "chapters",
|
|
1414
|
+
"/book/bookmarklist": "updated"
|
|
1415
|
+
};
|
|
1416
|
+
function outline(chapters, cards) {
|
|
1417
|
+
const result = [];
|
|
1418
|
+
for (const chapter of chapters) {
|
|
1419
|
+
const title = chapter.title?.trim();
|
|
1420
|
+
if (!title) continue;
|
|
1421
|
+
const level = chapter.level ?? 1;
|
|
1422
|
+
const row = { title, loc: String(chapter.chapterUid), sections: [] };
|
|
1423
|
+
if (level === 1 || level === 2 && !result.length) result.push(row);
|
|
1424
|
+
else if (level === 2) result.at(-1).sections.push({ title: row.title, loc: row.loc });
|
|
1425
|
+
}
|
|
1426
|
+
const marked = new Set(cards.map((card) => card.sectionKey));
|
|
1427
|
+
for (const row of result) {
|
|
1428
|
+
if (row.sections?.length && row.loc && marked.has(row.loc)) row.sections.unshift({ title: row.title, loc: row.loc });
|
|
1429
|
+
}
|
|
1430
|
+
return result;
|
|
1431
|
+
}
|
|
1432
|
+
function createWeRead(fetcher = fetch, keyReader = readKey) {
|
|
1433
|
+
const inFlight = /* @__PURE__ */ new Set();
|
|
1434
|
+
async function request(key, api_name, params, signal, retried = false) {
|
|
1435
|
+
async function send() {
|
|
1436
|
+
const deadline = AbortSignal.timeout(12e3);
|
|
1437
|
+
const requestSignal = signal ? AbortSignal.any([signal, deadline]) : deadline;
|
|
1438
|
+
const response = await fetcher(GATEWAY, {
|
|
1439
|
+
method: "POST",
|
|
1440
|
+
redirect: "error",
|
|
1441
|
+
signal: requestSignal,
|
|
1442
|
+
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
|
|
1443
|
+
body: JSON.stringify({ api_name, skill_version: SKILL_VERSION, ...params })
|
|
1444
|
+
});
|
|
1445
|
+
return { response, data: response.ok ? await response.json() : {} };
|
|
1446
|
+
}
|
|
1447
|
+
try {
|
|
1448
|
+
while (inFlight.size >= 3) {
|
|
1449
|
+
signal?.throwIfAborted();
|
|
1450
|
+
await Promise.race(inFlight).catch(() => void 0);
|
|
1451
|
+
}
|
|
1452
|
+
signal?.throwIfAborted();
|
|
1453
|
+
const task = send();
|
|
1454
|
+
inFlight.add(task);
|
|
1455
|
+
const { response, data } = await task.finally(() => inFlight.delete(task));
|
|
1456
|
+
const code = Number(data?.errcode || response.status);
|
|
1457
|
+
if (code === 429) {
|
|
1458
|
+
const seconds = Number(response.headers.get("retry-after") ?? 1);
|
|
1459
|
+
if (seconds >= 0 && seconds <= 3 && !retried) {
|
|
1460
|
+
await delay(seconds * 1e3, void 0, { signal });
|
|
1461
|
+
return request(key, api_name, params, signal, true);
|
|
1462
|
+
}
|
|
1463
|
+
throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u8BF7\u6C42\u8FC7\u4E8E\u9891\u7E41\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5");
|
|
1464
|
+
}
|
|
1465
|
+
if (code === 401 || code === 403) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u9274\u6743\u5931\u8D25\uFF0C\u8BF7\u66F4\u6362 API Key");
|
|
1466
|
+
if (!response.ok || !data || Number(data.errcode ?? 0) !== 0) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u63A5\u53E3\u6682\u65F6\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5");
|
|
1467
|
+
if (!Array.isArray(data[responseList[api_name]])) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u56DE\u5305\u4E0D\u5B8C\u6574\uFF0C\u8BF7\u91CD\u8BD5");
|
|
1468
|
+
return data;
|
|
1469
|
+
} catch (error) {
|
|
1470
|
+
if (error instanceof ShelfError) throw error;
|
|
1471
|
+
if (signal?.aborted) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u5BFC\u5165\u5DF2\u53D6\u6D88");
|
|
1472
|
+
throw new ShelfError("\u8FDE\u63A5\u5FAE\u4FE1\u8BFB\u4E66\u5931\u8D25\u6216\u8D85\u65F6\uFF0C\u8BF7\u91CD\u8BD5");
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
function currentKey() {
|
|
1476
|
+
const key = keyReader();
|
|
1477
|
+
if (!key) throw new ShelfError("\u8BF7\u5148\u5728\u5BAB\u6BBF\u300C\u4ECE\u5FAE\u4FE1\u8BFB\u4E66\u5BFC\u5165\u300D\u91CC\u4FDD\u5B58 API Key");
|
|
1478
|
+
return key;
|
|
1479
|
+
}
|
|
1480
|
+
async function shelf(key, signal) {
|
|
1481
|
+
const main = await request(key, "/shelf/sync", {}, signal);
|
|
1482
|
+
const books = new Map(main.books.map((book) => [book.bookId, { ...book, hasNotes: false }]));
|
|
1483
|
+
let lastSort;
|
|
1484
|
+
while (true) {
|
|
1485
|
+
const page = await request(key, "/user/notebooks", { count: 100, lastSort }, signal);
|
|
1486
|
+
for (const entry of page.books) {
|
|
1487
|
+
const book = books.get(entry.bookId) ?? entry.book;
|
|
1488
|
+
if (book) books.set(entry.bookId, { ...book, bookId: entry.bookId, hasNotes: Boolean(entry.noteCount || entry.reviewCount) });
|
|
1489
|
+
}
|
|
1490
|
+
if (!page.hasMore) break;
|
|
1491
|
+
const next = page.books.at(-1)?.sort;
|
|
1492
|
+
if (next === void 0 || next === lastSort) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u7B14\u8BB0\u5217\u8868\u5206\u9875\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5");
|
|
1493
|
+
lastSort = next;
|
|
1494
|
+
}
|
|
1495
|
+
return { books, total: main.books.length + (main.albums?.length ?? 0) + Number(Object.keys(main.mp ?? {}).length > 0) };
|
|
1496
|
+
}
|
|
1497
|
+
let listing;
|
|
1498
|
+
let prepared;
|
|
1499
|
+
return {
|
|
1500
|
+
async scan(signal) {
|
|
1501
|
+
const key = currentKey();
|
|
1502
|
+
listing = { key, data: await shelf(key, signal) };
|
|
1503
|
+
prepared = void 0;
|
|
1504
|
+
const { books, total } = listing.data;
|
|
1505
|
+
return { total, withNotes: [...books.values()].filter((book) => book.hasNotes).length, books: [...books.values()].map(
|
|
1506
|
+
({ bookId, title, author, category, finishReading, readUpdateTime, publishTime, hasNotes }) => ({ bookId, title, author, category, finished: finishReading === 1, readUpdateTime, publishTime, hasNotes })
|
|
1507
|
+
) };
|
|
1508
|
+
},
|
|
1509
|
+
async prepare(plan, confirming, signal) {
|
|
1510
|
+
if (!Array.isArray(plan) || plan.length === 0 || plan.some((id) => typeof id !== "string" || !id)) throw new ShelfError("\u8BF7\u63D0\u4F9B\u8981\u5BFC\u5165\u7684 bookId \u5217\u8868");
|
|
1511
|
+
const key = currentKey(), ids = [...new Set(plan)], fingerprint = JSON.stringify(ids);
|
|
1512
|
+
if (confirming && prepared?.key === key && prepared.plan === fingerprint) return prepared.batch;
|
|
1513
|
+
if (confirming) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u5BFC\u5165\u9884\u89C8\u5DF2\u53D8\u5316\uFF0C\u8BF7\u91CD\u65B0\u9884\u89C8\u5E76\u786E\u8BA4");
|
|
1514
|
+
const { books } = listing?.key === key ? listing.data : await shelf(key, signal);
|
|
1515
|
+
const groups = [];
|
|
1516
|
+
for (const id of ids) {
|
|
1517
|
+
const book = books.get(id);
|
|
1518
|
+
if (!book) throw new ShelfError("\u9009\u4E2D\u7684\u4E66\u4E0D\u5728\u4E66\u67B6\u6216\u7B14\u8BB0\u672C\u91CC\uFF0C\u8BF7\u91CD\u65B0\u626B\u63CF");
|
|
1519
|
+
const href = [book.readerLink, book.deepLink].find((link) => typeof link === "string" && /^https?:\/\//.test(link));
|
|
1520
|
+
const group = { key: id, kind: "book", title: book.title, href, cards: [] };
|
|
1521
|
+
groups.push(group);
|
|
1522
|
+
if (!book.hasNotes) continue;
|
|
1523
|
+
const reviews = async () => {
|
|
1524
|
+
const rows = [];
|
|
1525
|
+
let synckey = 0;
|
|
1526
|
+
while (true) {
|
|
1527
|
+
const page = await request(key, "/review/list/mine", { bookid: id, synckey, count: 100 }, signal);
|
|
1528
|
+
rows.push(...page.reviews);
|
|
1529
|
+
if (!page.hasMore) return rows;
|
|
1530
|
+
if (!page.synckey || page.synckey === synckey) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u60F3\u6CD5\u5217\u8868\u5206\u9875\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5");
|
|
1531
|
+
synckey = page.synckey;
|
|
1532
|
+
}
|
|
1533
|
+
};
|
|
1534
|
+
const [toc, marks, notes] = await Promise.all([
|
|
1535
|
+
request(key, "/book/chapterinfo", { bookId: id }, signal),
|
|
1536
|
+
request(key, "/book/bookmarklist", { bookId: id }, signal),
|
|
1537
|
+
reviews()
|
|
1538
|
+
]);
|
|
1539
|
+
const cards = /* @__PURE__ */ new Map();
|
|
1540
|
+
const highlights = /* @__PURE__ */ new Map();
|
|
1541
|
+
for (const row of marks.updated) if (row.markText?.trim()) {
|
|
1542
|
+
const key2 = JSON.stringify([id, "bookmark", row.bookmarkId]);
|
|
1543
|
+
const card = { key: key2, type: "excerpt", quote: row.markText, sectionKey: row.chapterUid?.toString() };
|
|
1544
|
+
cards.set(key2, card);
|
|
1545
|
+
const position = positionKey(row);
|
|
1546
|
+
if (position !== void 0) highlights.set(position, card);
|
|
1547
|
+
}
|
|
1548
|
+
const reviewsById = new Map(notes.map(({ review }) => [review.reviewId, review]));
|
|
1549
|
+
for (const row of reviewsById.values()) if (row.abstract?.trim() || row.content?.trim()) {
|
|
1550
|
+
const position = positionKey(row);
|
|
1551
|
+
const highlight = position === void 0 ? void 0 : highlights.get(position);
|
|
1552
|
+
if (highlight) {
|
|
1553
|
+
if (row.content?.trim()) highlight.note = highlight.note ? highlight.note + "\n\n" + row.content : row.content;
|
|
1554
|
+
continue;
|
|
1555
|
+
}
|
|
1556
|
+
const key2 = JSON.stringify([id, "review", row.reviewId]);
|
|
1557
|
+
cards.set(key2, { key: key2, type: row.abstract?.trim() ? "excerpt" : "thought", quote: row.abstract || void 0, note: row.content, sectionKey: row.chapterUid?.toString() });
|
|
1558
|
+
}
|
|
1559
|
+
group.cards = [...cards.values()];
|
|
1560
|
+
group.chapters = outline(toc.chapters, group.cards);
|
|
1561
|
+
}
|
|
1562
|
+
const batch = { source: "weread", groups, links: [] };
|
|
1563
|
+
prepared = { key, plan: fingerprint, batch };
|
|
1564
|
+
return batch;
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
var weread = createWeRead();
|
|
1569
|
+
|
|
1318
1570
|
// src/host/dictionary.ts
|
|
1319
1571
|
import { execFile } from "node:child_process";
|
|
1320
1572
|
var SCRIPT = `ObjC.import('CoreServices')
|
|
@@ -1390,8 +1642,8 @@ function streamFile(file, type) {
|
|
|
1390
1642
|
return new Response(body, { headers: { "content-type": type, "content-length": String(size), "cache-control": "no-store" } });
|
|
1391
1643
|
}
|
|
1392
1644
|
function displayPath(path) {
|
|
1393
|
-
const home =
|
|
1394
|
-
return path === home || path.startsWith(home +
|
|
1645
|
+
const home = homedir3();
|
|
1646
|
+
return path === home || path.startsWith(home + sep3) ? "~" + path.slice(home.length) : path;
|
|
1395
1647
|
}
|
|
1396
1648
|
function openExternal(target) {
|
|
1397
1649
|
const child = process.platform === "darwin" ? spawn("open", [target], { detached: true, stdio: "ignore" }) : process.platform === "win32" ? spawn("cmd", ["/c", "start", "", target], { detached: true, stdio: "ignore", windowsHide: true }) : spawn("xdg-open", [target], { detached: true, stdio: "ignore" });
|
|
@@ -1452,6 +1704,7 @@ function createRoutes(store, active, memory = new Memory(store)) {
|
|
|
1452
1704
|
}
|
|
1453
1705
|
});
|
|
1454
1706
|
return [
|
|
1707
|
+
route("POST", "weread-key", ({ body }) => json({ ok: true, saved: wereadKey(body.key, void 0, store.dir) })),
|
|
1455
1708
|
route("GET", "palace", () => json({
|
|
1456
1709
|
ok: true,
|
|
1457
1710
|
rev: store.rev,
|
|
@@ -1501,8 +1754,8 @@ function createRoutes(store, active, memory = new Memory(store)) {
|
|
|
1501
1754
|
return json({ ok: true, pages: texts, scanned });
|
|
1502
1755
|
}),
|
|
1503
1756
|
route("GET", "image", ({ url }) => {
|
|
1504
|
-
const name2 =
|
|
1505
|
-
const file =
|
|
1757
|
+
const name2 = basename3(url.searchParams.get("name") ?? "");
|
|
1758
|
+
const file = join6(store.imagesDir, name2);
|
|
1506
1759
|
const type = IMAGE_TYPES[extname2(name2).toLowerCase()];
|
|
1507
1760
|
if (name2 === "" || type === void 0 || !existsSync4(file)) return new Response("not found", { status: 404 });
|
|
1508
1761
|
return streamFile(file, type);
|
|
@@ -1513,13 +1766,13 @@ function createRoutes(store, active, memory = new Memory(store)) {
|
|
|
1513
1766
|
if (rel.startsWith("reader/")) {
|
|
1514
1767
|
const name2 = rel.slice("reader/".length);
|
|
1515
1768
|
if (!READER_SCRIPTS.includes(name2)) return new Response("not found", { status: 404 });
|
|
1516
|
-
const response2 = streamFile(
|
|
1769
|
+
const response2 = streamFile(join6(vendorDir(), name2), "text/javascript; charset=utf-8");
|
|
1517
1770
|
response2.headers.set("cache-control", "public, max-age=86400");
|
|
1518
1771
|
return response2;
|
|
1519
1772
|
}
|
|
1520
|
-
const theme = /^themes\/[a-z][a-z0-9-]{0,30}\/(?:[a-z]+\/)?[A-Za-z0-9][A-Za-z0-9-]{0,60}\.(webp|png|svg|ttf|woff2)$/.exec(rel.split(
|
|
1773
|
+
const theme = /^themes\/[a-z][a-z0-9-]{0,30}\/(?:[a-z]+\/)?[A-Za-z0-9][A-Za-z0-9-]{0,60}\.(webp|png|svg|ttf|woff2)$/.exec(rel.split(sep3).join("/"));
|
|
1521
1774
|
if (theme !== null) {
|
|
1522
|
-
const file2 =
|
|
1775
|
+
const file2 = join6(vendorDir(), rel);
|
|
1523
1776
|
if (!existsSync4(file2)) return new Response("not found", { status: 404 });
|
|
1524
1777
|
const response2 = streamFile(file2, THEME_TYPES[theme[1]]);
|
|
1525
1778
|
response2.headers.set("cache-control", "public, max-age=86400");
|
|
@@ -1527,15 +1780,15 @@ function createRoutes(store, active, memory = new Memory(store)) {
|
|
|
1527
1780
|
}
|
|
1528
1781
|
const font = /^fonts\/([a-z0-9][a-z0-9-]{0,60})\.woff2$/.exec(rel)?.[1];
|
|
1529
1782
|
if (font !== void 0) {
|
|
1530
|
-
const file2 =
|
|
1783
|
+
const file2 = join6(vendorDir(), "fonts", `${font}.woff2`);
|
|
1531
1784
|
if (!existsSync4(file2)) return new Response("not found", { status: 404 });
|
|
1532
1785
|
const response2 = streamFile(file2, "font/woff2");
|
|
1533
1786
|
response2.headers.set("cache-control", "public, max-age=31536000, immutable");
|
|
1534
1787
|
return response2;
|
|
1535
1788
|
}
|
|
1536
1789
|
const allowed = ["cmaps", "standard_fonts", "wasm", "iccs"];
|
|
1537
|
-
if (!allowed.some((dir) => rel.startsWith(dir +
|
|
1538
|
-
const file =
|
|
1790
|
+
if (!allowed.some((dir) => rel.startsWith(dir + sep3)) || rel.includes("..")) return new Response("not found", { status: 404 });
|
|
1791
|
+
const file = join6(pdfjsDistDir(), rel);
|
|
1539
1792
|
if (!existsSync4(file)) return new Response("not found", { status: 404 });
|
|
1540
1793
|
const response = streamFile(file, file.endsWith(".wasm") ? "application/wasm" : "application/octet-stream");
|
|
1541
1794
|
response.headers.set("cache-control", "public, max-age=31536000, immutable");
|
|
@@ -1580,9 +1833,103 @@ function createRoutes(store, active, memory = new Memory(store)) {
|
|
|
1580
1833
|
];
|
|
1581
1834
|
}
|
|
1582
1835
|
|
|
1583
|
-
// src/host/tools.ts
|
|
1836
|
+
// src/host/harness/tools.ts
|
|
1584
1837
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
1585
1838
|
|
|
1839
|
+
// src/core/import.ts
|
|
1840
|
+
import { createHash } from "node:crypto";
|
|
1841
|
+
var digest = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
1842
|
+
var stableId = (prefix, ...key) => `${prefix}_${digest(key).slice(0, 10)}`;
|
|
1843
|
+
async function importBatch(store, memory, batch, confirmation, onlyNew = false) {
|
|
1844
|
+
const palace = store.getPalace();
|
|
1845
|
+
const cells = new Map(palace.cells.map((cell) => [cell.id, cell]));
|
|
1846
|
+
const placed = /* @__PURE__ */ new Map();
|
|
1847
|
+
for (const cell of palace.cells) {
|
|
1848
|
+
if (cell.kind === "group") continue;
|
|
1849
|
+
for (const card of store.getItem(cell.id).cards) {
|
|
1850
|
+
if (card.importSource?.source === batch.source) placed.set(card.importSource.key, { cell, card });
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
const newCells = /* @__PURE__ */ new Map();
|
|
1854
|
+
const outlines = /* @__PURE__ */ new Map();
|
|
1855
|
+
const selected = /* @__PURE__ */ new Map();
|
|
1856
|
+
const at = nowIso();
|
|
1857
|
+
for (const group of batch.groups) {
|
|
1858
|
+
const id = group.itemId ?? stableId("c", batch.source, group.key);
|
|
1859
|
+
let target = cells.get(id) ?? newCells.get(id);
|
|
1860
|
+
if (group.itemId !== void 0 && (target === void 0 || target.kind === "group")) throw new ShelfError("\u5BFC\u5165\u76EE\u6807\u5FC5\u987B\u662F\u5DF2\u6709\u7684\u4E66\u6216\u7B14\u8BB0\u683C\u5B50");
|
|
1861
|
+
if (target === void 0 && (group.kind === "book" || group.cards.some((card) => !placed.has(card.key)))) {
|
|
1862
|
+
target = { id, kind: group.kind ?? "note", title: group.title, href: group.href, parentId: null, col: 0, row: 0, createdAt: at };
|
|
1863
|
+
newCells.set(id, target);
|
|
1864
|
+
}
|
|
1865
|
+
if (target && group.chapters?.length && (newCells.has(id) || store.getItem(id).chapters.length === 0)) outlines.set(id, group.chapters);
|
|
1866
|
+
for (const input of group.cards) {
|
|
1867
|
+
if (selected.has(input.key)) throw new ShelfError(`\u65B9\u6848\u91CC\u91CD\u590D\u51FA\u73B0\uFF1A${input.key}`);
|
|
1868
|
+
const previous = placed.get(input.key);
|
|
1869
|
+
if (previous && group.itemId !== void 0 && previous.cell.id !== group.itemId) throw new ShelfError(`\u5DF2\u5BFC\u5165\u7684\u7B14\u8BB0\u7559\u5728\u539F\u683C\u5B50\uFF1A${input.key}`);
|
|
1870
|
+
const content = digest(input.type === void 0 ? [input.note, input.frontmatter] : [input.type, input.note, input.quote, input.sectionKey]);
|
|
1871
|
+
const card = {
|
|
1872
|
+
...previous?.card ?? { id: stableId("k", batch.source, input.key), type: input.type ?? "thought", quote: input.quote, createdAt: at },
|
|
1873
|
+
note: previous?.card.importSource?.digest === content ? previous.card.note : input.note,
|
|
1874
|
+
importSource: { source: batch.source, key: input.key, digest: content, frontmatter: input.frontmatter }
|
|
1875
|
+
};
|
|
1876
|
+
selected.set(input.key, { cell: previous?.cell ?? target, card, previous: previous?.card, sectionKey: input.sectionKey });
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
const links = [];
|
|
1880
|
+
const occurrences = /* @__PURE__ */ new Map();
|
|
1881
|
+
for (const link of batch.links) {
|
|
1882
|
+
const from = selected.get(link.from);
|
|
1883
|
+
const to = selected.get(link.to) ?? placed.get(link.to);
|
|
1884
|
+
if (!from || !to || from.cell.id === to.cell.id) continue;
|
|
1885
|
+
const pair = JSON.stringify([link.from, link.to]);
|
|
1886
|
+
const occurrence = (occurrences.get(pair) ?? 0) + 1;
|
|
1887
|
+
occurrences.set(pair, occurrence);
|
|
1888
|
+
const sourceKey = digest([batch.source, pair, occurrence, from.cell.id, to.cell.id]);
|
|
1889
|
+
links.push({ sourceKey, from: from.cell.id, to: to.cell.id, why: link.why, evidence: [from.card.id, to.card.id], fromPath: link.from, toPath: link.to });
|
|
1890
|
+
}
|
|
1891
|
+
const changed = [...selected.values()].filter(({ previous, card }) => previous === void 0 || !onlyNew && previous.importSource?.digest !== card.importSource?.digest);
|
|
1892
|
+
const { pending, arcs, strands } = memory.previewImportedLinks(
|
|
1893
|
+
links.map(({ sourceKey, from, to, why, evidence }) => ({ sourceKey, from, to, why, evidence }))
|
|
1894
|
+
);
|
|
1895
|
+
const summary = {
|
|
1896
|
+
cells: newCells.size,
|
|
1897
|
+
cards: changed.filter((x) => !x.previous).length,
|
|
1898
|
+
updatedCards: changed.filter((x) => x.previous).length,
|
|
1899
|
+
arcs,
|
|
1900
|
+
strands
|
|
1901
|
+
};
|
|
1902
|
+
const token = digest([batch, [...selected].map(([key, x]) => [key, x.cell.id, x.previous?.id]), links, pending, [...outlines.keys()], summary]);
|
|
1903
|
+
if (confirmation === void 0) return { preview: true, ...summary, token };
|
|
1904
|
+
if (confirmation !== token) throw new ShelfError("\u7B14\u8BB0\u3001\u65B9\u6848\u6216\u5BAB\u6BBF\u5DF2\u53D8\u5316\uFF0C\u8BF7\u91CD\u65B0\u9884\u89C8\u5E76\u786E\u8BA4");
|
|
1905
|
+
if (changed.length || newCells.size || outlines.size) {
|
|
1906
|
+
await store.mutate((tx) => {
|
|
1907
|
+
for (const cell of newCells.values()) {
|
|
1908
|
+
Object.assign(cell, firstEmptySlot(tx.palace, null));
|
|
1909
|
+
tx.palace.cells.push(cell);
|
|
1910
|
+
tx.createItem(emptyItem(cell.id, null));
|
|
1911
|
+
}
|
|
1912
|
+
for (const [id, chapters] of outlines) tx.item(id).chapters = buildChapters(chapters);
|
|
1913
|
+
for (const { cell, card, previous, sectionKey } of changed) {
|
|
1914
|
+
const item = tx.item(cell.id);
|
|
1915
|
+
if (sectionKey !== void 0) card.sectionId = item.chapters.flatMap((chapter) => chapter.sections).find((section) => section.loc === sectionKey)?.id;
|
|
1916
|
+
const { cards } = item;
|
|
1917
|
+
const index = previous === void 0 ? -1 : cards.findIndex((value) => value.id === previous.id);
|
|
1918
|
+
if (index === -1) cards.push(card);
|
|
1919
|
+
else cards[index] = card;
|
|
1920
|
+
}
|
|
1921
|
+
});
|
|
1922
|
+
}
|
|
1923
|
+
memory.addImportedLinks(pending);
|
|
1924
|
+
const active = new Set(memory.validLinks().map((link) => link.sourceKey));
|
|
1925
|
+
const pairs = /* @__PURE__ */ new Map();
|
|
1926
|
+
for (const { sourceKey, from, to, fromPath, toPath, evidence } of links) {
|
|
1927
|
+
if (active.has(sourceKey)) pairs.set([from, to].sort().join(":"), { from, to, fromPath, toPath, evidence });
|
|
1928
|
+
if (pairs.size === 20) break;
|
|
1929
|
+
}
|
|
1930
|
+
return { preview: false, ...summary, links: [...pairs.values()] };
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1586
1933
|
// src/core/tools.ts
|
|
1587
1934
|
var KIND_LABEL = { book: "\u4E66", note: "\u7B14\u8BB0", group: "\u5206\u7EC4" };
|
|
1588
1935
|
var CARD_LABEL = { excerpt: "\u6458\u5F55", thought: "\u60F3\u6CD5", mistake: "\u9519\u9898" };
|
|
@@ -1619,12 +1966,12 @@ function itemDetail(store, id) {
|
|
|
1619
1966
|
const cell = store.getPalace().cells.find((c) => c.id === id);
|
|
1620
1967
|
if (cell === void 0 || cell.kind === "group") throw new ShelfError("\u683C\u5B50\u4E0D\u5B58\u5728");
|
|
1621
1968
|
const item = store.getItem(id);
|
|
1622
|
-
const
|
|
1969
|
+
const outline2 = item.chapters.length === 0 ? "\uFF08\u8FD8\u6CA1\u6709\u76EE\u5F55\uFF09" : item.chapters.map((c, i) => `${i + 1}. ${c.title}
|
|
1623
1970
|
${c.sections.map((s) => ` - ${s.done ? "[x]" : "[ ]"} ${s.title} (section_id=${s.id})`).join("\n")}`).join("\n");
|
|
1624
1971
|
const cards = item.cards.slice(-20).map((k) => `- ${CARD_LABEL[k.type]} ${k.id}\uFF1A${(k.quote ?? k.note ?? "").slice(0, 60)}`).join("\n");
|
|
1625
1972
|
const insps = item.inspirations.slice(-10).map((i) => `- ${i.id} ${i.createdAt.slice(0, 10)} ${i.title}`).join("\n");
|
|
1626
1973
|
return `\u300A${cell.title}\u300B\u76EE\u5F55\uFF1A
|
|
1627
|
-
${
|
|
1974
|
+
${outline2}` + (cards ? `
|
|
1628
1975
|
|
|
1629
1976
|
\u6700\u8FD1\u7684\u5361\u7247\uFF1A
|
|
1630
1977
|
${cards}` : "") + (insps ? `
|
|
@@ -1644,10 +1991,10 @@ async function guard(run) {
|
|
|
1644
1991
|
throw error;
|
|
1645
1992
|
}
|
|
1646
1993
|
}
|
|
1647
|
-
function createOverviewTool(store) {
|
|
1994
|
+
function createOverviewTool(store, currentItem) {
|
|
1648
1995
|
return {
|
|
1649
1996
|
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",
|
|
1997
|
+
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" + (currentItem ? "\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
1998
|
parameters: {
|
|
1652
1999
|
item_id: { type: "string", description: "\u53EF\u9009\uFF1A\u67E5\u770B\u8FD9\u4E2A\u683C\u5B50\u7684\u8BE6\u60C5" }
|
|
1653
2000
|
},
|
|
@@ -1657,11 +2004,12 @@ function createOverviewTool(store) {
|
|
|
1657
2004
|
};
|
|
1658
2005
|
}
|
|
1659
2006
|
function createPalaceTools(store, memory, sources) {
|
|
2007
|
+
const itemId = sources.currentItem ? { type: "string" } : { type: "string", minLength: 1, required: true, description: "\u76EE\u6807\u683C\u5B50 id" };
|
|
1660
2008
|
return [
|
|
1661
|
-
createOverviewTool(store),
|
|
2009
|
+
createOverviewTool(store, sources.currentItem),
|
|
1662
2010
|
{
|
|
1663
2011
|
name: "palace_add",
|
|
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
|
|
2012
|
+
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" + sources.videoHelp,
|
|
1665
2013
|
parameters: {
|
|
1666
2014
|
kind: { type: "string", enum: ["group", "note", "book"], required: true },
|
|
1667
2015
|
title: { type: "string" },
|
|
@@ -1723,7 +2071,7 @@ ${t.text || "\uFF08\u65E0\u6587\u5B57\uFF09"}`).join("\n\n");
|
|
|
1723
2071
|
if (scanned.length > 0) {
|
|
1724
2072
|
out += `
|
|
1725
2073
|
|
|
1726
|
-
\u6CE8\u610F\uFF1A\u7B2C ${scanned.join("\u3001")} \u9875\u6CA1\u6709\u6587\u5B57\u5C42\uFF08\u53EF\u80FD\u662F\u626B\u63CF\u4EF6\uFF09\u3002\u5982\u679C\u5BF9\u8BDD\u91CC\u9644\u6709\u8FD9\u4E9B\u9875\u7684\u56FE\u7247\uFF0C\u8BF7\u76F4\u63A5\u770B\u56FE\u8BC6\u522B\uFF1B
|
|
2074
|
+
\u6CE8\u610F\uFF1A\u7B2C ${scanned.join("\u3001")} \u9875\u6CA1\u6709\u6587\u5B57\u5C42\uFF08\u53EF\u80FD\u662F\u626B\u63CF\u4EF6\uFF09\u3002\u5982\u679C\u5BF9\u8BDD\u91CC\u9644\u6709\u8FD9\u4E9B\u9875\u7684\u56FE\u7247\uFF0C\u8BF7\u76F4\u63A5\u770B\u56FE\u8BC6\u522B\uFF1B` + sources.scannedPageHelp;
|
|
1727
2075
|
}
|
|
1728
2076
|
return out;
|
|
1729
2077
|
});
|
|
@@ -1808,14 +2156,14 @@ ${lines.join("\n")}`;
|
|
|
1808
2156
|
},
|
|
1809
2157
|
{
|
|
1810
2158
|
name: "palace_add_card",
|
|
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\
|
|
2159
|
+
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\u3002" + sources.imageHelp + (sources.currentItem ? "item_id \u4E0D\u586B\u65F6\u8BB0\u5230\u7528\u6237\u5F53\u524D\u6253\u5F00\u7684\u683C\u5B50\uFF1B" : "item_id \u586B\u76EE\u6807\u683C\u5B50 id\uFF1B") + "section_id \u4E0D\u586B\u65F6\uFF0C\u6458\u5F55\u548C\u9519\u9898\u9ED8\u8BA4\u6302\u5230\u5F53\u524D\u5728\u5B66\u7684\u90A3\u4E00\u8282\u3002",
|
|
1812
2160
|
parameters: {
|
|
1813
2161
|
type: { type: "string", enum: ["mistake", "excerpt", "thought"], required: true },
|
|
1814
2162
|
quote: { type: "string", description: "\u539F\u6587\u6216\u9898\u76EE" },
|
|
1815
2163
|
note: { type: "string", description: "\u60F3\u6CD5\u3001\u6279\u6CE8\u6216\u9519\u56E0\uFF08\u7528\u6237\u8BF4\u4E86\u624D\u586B\uFF09" },
|
|
1816
2164
|
answer: { type: "string", description: "\u9519\u9898\u7684\u7B54\u6848\u4E0E\u89E3\u6790" },
|
|
1817
|
-
image: { type: "string", description:
|
|
1818
|
-
item_id:
|
|
2165
|
+
image: { type: "string", description: sources.imageDescription },
|
|
2166
|
+
item_id: itemId,
|
|
1819
2167
|
section_id: { type: "string" }
|
|
1820
2168
|
},
|
|
1821
2169
|
async execute(args, options) {
|
|
@@ -1832,11 +2180,11 @@ ${lines.join("\n")}`;
|
|
|
1832
2180
|
},
|
|
1833
2181
|
{
|
|
1834
2182
|
name: "palace_add_inspiration",
|
|
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\
|
|
2183
|
+
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\u3002" + (sources.currentItem ? "item_id \u4E0D\u586B\u65F6\u5B58\u5230\u7528\u6237\u5F53\u524D\u6253\u5F00\u7684\u683C\u5B50\u3002" : "item_id \u5FC5\u987B\u586B\u76EE\u6807\u683C\u5B50 id\u3002"),
|
|
1836
2184
|
parameters: {
|
|
1837
2185
|
title: { type: "string", required: true },
|
|
1838
2186
|
body: { type: "string", required: true },
|
|
1839
|
-
item_id:
|
|
2187
|
+
item_id: itemId,
|
|
1840
2188
|
card_ids: { type: "array", items: { type: "string" }, description: "\u53EF\u9009\uFF1A\u8FD9\u6B21\u5BF9\u8BDD\u6D89\u53CA\u7684\u5361\u7247" }
|
|
1841
2189
|
},
|
|
1842
2190
|
async execute(args, options) {
|
|
@@ -1850,7 +2198,7 @@ ${lines.join("\n")}`;
|
|
|
1850
2198
|
},
|
|
1851
2199
|
{
|
|
1852
2200
|
name: "palace_wiki_read",
|
|
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
|
|
2201
|
+
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" + sources.wikiReadHelp,
|
|
1854
2202
|
parameters: {
|
|
1855
2203
|
target: { type: "string", required: true, description: "\u683C\u5B50 id \u6216 me" }
|
|
1856
2204
|
},
|
|
@@ -1895,16 +2243,21 @@ ${page.body}`;
|
|
|
1895
2243
|
},
|
|
1896
2244
|
{
|
|
1897
2245
|
name: "palace_link",
|
|
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",
|
|
2246
|
+
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\u53EA\u6539\u5DF2\u6709\u7535\u5F27\u7684\u9898\u8BCD\u65F6\u586B update_why=true\uFF0Cwhy \u4ECE\u4E24\u7AEF\u539F\u6587\u6458\u53D6\uFF0C\u7701\u7565 evidence\u3002",
|
|
1899
2247
|
parameters: {
|
|
1900
2248
|
from: { type: "string", required: true, description: "\u683C\u5B50 id" },
|
|
1901
2249
|
to: { type: "string", required: true, description: "\u683C\u5B50 id" },
|
|
1902
2250
|
why: { type: "string", required: true },
|
|
1903
|
-
evidence: { type: "array", items: { type: "string" },
|
|
2251
|
+
evidence: { type: "array", items: { type: "string" }, description: "\u8FDE\u7EBF\u5FC5\u586B\uFF1A\u5361\u7247 / \u7075\u611F id" },
|
|
2252
|
+
update_why: { type: "boolean", description: "\u53EA\u66F4\u65B0\u73B0\u6709\u7535\u5F27\u7684\u9898\u8BCD" },
|
|
1904
2253
|
user_asked: { type: "boolean", description: "\u7528\u6237\u660E\u786E\u8981\u6C42\u8FDE\uFF08\u526A\u65AD\u8FC7\u7684\u4E5F\u80FD\u91CD\u8FDE\uFF09" }
|
|
1905
2254
|
},
|
|
1906
2255
|
async execute(args, options) {
|
|
1907
2256
|
return guard(async () => {
|
|
2257
|
+
if (args.update_why === true) {
|
|
2258
|
+
memory.retitleLink({ from: args.from, to: args.to, why: args.why });
|
|
2259
|
+
return "\u5DF2\u66F4\u65B0\u7535\u5F27\u9898\u8BCD\u3002";
|
|
2260
|
+
}
|
|
1908
2261
|
const link = memory.addLink({ from: args.from, to: args.to, why: args.why, evidence: args.evidence, ...options.attribution, userAsked: args.user_asked === true });
|
|
1909
2262
|
const cells = store.getPalace().cells;
|
|
1910
2263
|
const arc = memory.arcs().find((a) => a.links.some((l) => l.id === link.id));
|
|
@@ -1928,8 +2281,242 @@ ${page.body}`;
|
|
|
1928
2281
|
}
|
|
1929
2282
|
];
|
|
1930
2283
|
}
|
|
2284
|
+
function createObsidianTools(store, memory, source) {
|
|
2285
|
+
const path = { type: "string", required: true, description: "\u7528\u6237\u63D0\u4F9B\u7684\u7B14\u8BB0\u5E93\u6587\u4EF6\u5939\u7EDD\u5BF9\u8DEF\u5F84" };
|
|
2286
|
+
return [
|
|
2287
|
+
{
|
|
2288
|
+
name: "palace_scan_obsidian",
|
|
2289
|
+
description: "\u53EA\u8BFB\u626B\u63CF Obsidian \u7B14\u8BB0\u5E93\u3002\u5411\u7528\u6237\u8BE2\u95EE\u8DEF\u5F84\u540E\u8C03\u7528\uFF0C\u6309\u6587\u4EF6\u5939\u7EDF\u8BA1\u7BC7\u6570\u3001\u6807\u7B7E\u548C\u53EF\u89E3\u6790\u53CC\u94FE\uFF0C\u6BCF\u9875\u5217\u51FA\u6700\u591A 100 \u7BC7\u3002\u548C\u7528\u6237\u5546\u91CF\u5206\u683C\u65B9\u6848\u540E\u518D\u9884\u89C8\u3002",
|
|
2290
|
+
parameters: { path, offset: { type: "integer", description: "\u4E0B\u4E00\u9875\u8D77\u70B9\uFF0C\u4F7F\u7528\u4E0A\u6B21\u8FD4\u56DE\u7684 nextOffset" } },
|
|
2291
|
+
async execute(args, options) {
|
|
2292
|
+
return guard(async () => JSON.stringify(await source.scan(args.path, args.offset, options.signal)));
|
|
2293
|
+
}
|
|
2294
|
+
},
|
|
2295
|
+
{
|
|
2296
|
+
name: "palace_import_obsidian",
|
|
2297
|
+
description: "\u5BFC\u5165 Obsidian \u7B14\u8BB0\u3002plan \u7701\u7565\u65F6\u6309\u6587\u4EF6\u5939\u5EFA\u683C\uFF1B\u6241\u5E73\u7B14\u8BB0\u5E93\u6309\u6807\u7B7E\u548C\u53CC\u94FE\u5546\u91CF\u5206\u683C\u3002plan \u6BCF\u4E2A\u76EE\u6807\u7528 paths \u6216 tags \u9009\u7B14\u8BB0\uFF0C\u4E8C\u9009\u4E00\uFF0C\u518D\u6307\u5B9A\u65B0\u683C\u5B50\u6807\u9898\u6216\u5DF2\u6709\u683C\u5B50 id\u3002\u5148\u7701\u7565 confirmation \u83B7\u53D6\u65B0\u589E\u683C\u5B50\u3001\u5361\u7247\u548C\u7535\u5F27\u7684\u9884\u89C8\uFF0C\u5411\u7528\u6237\u590D\u8FF0\u5E76\u7B49\u786E\u8BA4\uFF0C\u518D\u7528\u76F8\u540C\u65B9\u6848\u548C\u9884\u89C8\u7684 token \u8C03\u7528\u3002\u53CC\u94FE\u81EA\u52A8\u8FDE\u7EBF\u3002\u5BFC\u5165\u540E\u6309\u7ED3\u679C\u91CC\u7684\u4E24\u7AEF\u7B14\u8BB0\u8DEF\u5F84\u8BFB\u539F\u6587\uFF0C\u5FC5\u8981\u65F6\u7528 palace_link \u7684 update_why \u6539\u987A\u9898\u8BCD\u3002",
|
|
2298
|
+
parameters: {
|
|
2299
|
+
path,
|
|
2300
|
+
plan: {
|
|
2301
|
+
type: "array",
|
|
2302
|
+
items: { type: "object", additionalProperties: false, properties: {
|
|
2303
|
+
title: { type: "string", description: "\u65B0\u7B14\u8BB0\u683C\u5B50\u6807\u9898" },
|
|
2304
|
+
item_id: { type: "string", description: "\u5DF2\u6709\u4E66\u6216\u7B14\u8BB0\u683C\u5B50 id" },
|
|
2305
|
+
paths: { type: "array", items: { type: "string" }, description: "\u626B\u63CF\u8FD4\u56DE\u7684\u7B14\u8BB0\u76F8\u5BF9\u8DEF\u5F84" },
|
|
2306
|
+
tags: { type: "array", items: { type: "string" }, description: "\u626B\u63CF\u8FD4\u56DE\u7684\u6807\u7B7E\uFF0C\u5339\u914D\u5176\u4E2D\u4EFB\u610F\u4E00\u4E2A\u5C31\u9009\u5165" }
|
|
2307
|
+
} }
|
|
2308
|
+
},
|
|
2309
|
+
confirmation: { type: "string", description: "\u4EC5\u5728\u7528\u6237\u786E\u8BA4\u540E\u4F20\u5165\u9884\u89C8\u8FD4\u56DE\u7684 token" }
|
|
2310
|
+
},
|
|
2311
|
+
async execute(args, options) {
|
|
2312
|
+
return guard(async () => {
|
|
2313
|
+
if (args.confirmation !== void 0 && typeof args.confirmation !== "string") throw new ShelfError("\u786E\u8BA4\u503C\u5FC5\u987B\u662F\u9884\u89C8\u8FD4\u56DE\u7684 token");
|
|
2314
|
+
const batch = await source.prepare(args.path, args.plan, options.signal);
|
|
2315
|
+
options.signal?.throwIfAborted();
|
|
2316
|
+
return JSON.stringify(await importBatch(store, memory, batch, args.confirmation));
|
|
2317
|
+
});
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
];
|
|
2321
|
+
}
|
|
2322
|
+
function createWeReadTools(store, memory, source) {
|
|
2323
|
+
return [
|
|
2324
|
+
{
|
|
2325
|
+
name: "palace_scan_weread",
|
|
2326
|
+
description: "\u9884\u89C8\u5FAE\u4FE1\u8BFB\u4E66\u4E66\u67B6\u548C\u6709\u5212\u7EBF\u6216\u60F3\u6CD5\u7684\u4E66\u3002\u7528\u6237\u63D0\u51FA\u5BFC\u5165\u65F6\uFF0C\u5148\u7528\u8FD9\u4E9B\u4FE1\u606F\u5546\u91CF\u8981\u5BFC\u5165\u54EA\u4E9B\u4E66\u3002",
|
|
2327
|
+
parameters: {},
|
|
2328
|
+
async execute(_args, { signal }) {
|
|
2329
|
+
return guard(async () => JSON.stringify(await source.scan(signal)));
|
|
2330
|
+
}
|
|
2331
|
+
},
|
|
2332
|
+
{
|
|
2333
|
+
name: "palace_import_weread",
|
|
2334
|
+
description: "\u628A\u9009\u4E2D\u7684\u5FAE\u4FE1\u8BFB\u4E66\u4E66\u548C\u7B14\u8BB0\u5BFC\u5165\u5BAB\u6BBF\u3002plan \u586B\u626B\u63CF\u5F97\u5230\u7684 bookId \u5217\u8868\u3002\u5148\u7701\u7565 confirmation \u8FDB\u884C\u9884\u89C8\uFF0C\u7B49\u7528\u6237\u786E\u8BA4\u540E\u7528\u540C\u4E00\u65B9\u6848\u548C\u9884\u89C8\u7684 token \u5199\u5165\u3002",
|
|
2335
|
+
parameters: {
|
|
2336
|
+
plan: { type: "array", required: true, items: { type: "string" } },
|
|
2337
|
+
confirmation: { type: "string" }
|
|
2338
|
+
},
|
|
2339
|
+
async execute({ plan, confirmation }, { signal }) {
|
|
2340
|
+
return guard(async () => {
|
|
2341
|
+
if (confirmation !== void 0 && typeof confirmation !== "string") throw new ShelfError("\u8BF7\u4F7F\u7528\u9884\u89C8\u8FD4\u56DE\u7684 token");
|
|
2342
|
+
const batch = await source.prepare(plan, confirmation !== void 0, signal);
|
|
2343
|
+
if (signal?.aborted) throw new ShelfError("\u5FAE\u4FE1\u8BFB\u4E66\u5BFC\u5165\u5DF2\u53D6\u6D88");
|
|
2344
|
+
return JSON.stringify(await importBatch(store, memory, batch, confirmation, true));
|
|
2345
|
+
});
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
];
|
|
2349
|
+
}
|
|
1931
2350
|
|
|
1932
|
-
// src/host/
|
|
2351
|
+
// src/host/obsidian.ts
|
|
2352
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
2353
|
+
import { constants } from "node:fs";
|
|
2354
|
+
import { open as open2, readdir, realpath, stat } from "node:fs/promises";
|
|
2355
|
+
import { isAbsolute as isAbsolute3, join as join7, posix } from "node:path";
|
|
2356
|
+
function withTitle(note, filename, raw) {
|
|
2357
|
+
const opening = note.replace(/^(?:\uFEFF)?(?:[ \t]*\r?\n)*/, "");
|
|
2358
|
+
if (/^ {0,3}#{1,6}(?:[ \t]|\r?\n|$)/.test(opening) || /^ {0,3}\S[^\r\n]*\r?\n {0,3}(?:=+|-+)[ \t]*(?:\r?\n|$)/.test(opening)) return note;
|
|
2359
|
+
const title = posix.basename(filename).replace(/\.md$/i, "").replace(/[\r\n]+/g, " ");
|
|
2360
|
+
const newline = raw.match(/\r?\n/)?.[0] ?? "\n";
|
|
2361
|
+
return `# ${title}${newline}${newline}${note}`;
|
|
2362
|
+
}
|
|
2363
|
+
async function readVault(path, signal) {
|
|
2364
|
+
if (typeof path !== "string" || !isAbsolute3(expandHome(path))) throw new ShelfError("\u8BF7\u63D0\u4F9B Obsidian \u7B14\u8BB0\u5E93\u7684\u7EDD\u5BF9\u8DEF\u5F84");
|
|
2365
|
+
const notes = [];
|
|
2366
|
+
const references = [];
|
|
2367
|
+
let root;
|
|
2368
|
+
try {
|
|
2369
|
+
root = await realpath(expandHome(path));
|
|
2370
|
+
if (!(await stat(root)).isDirectory()) throw new ShelfError("\u8BF7\u9009\u62E9 Obsidian \u7B14\u8BB0\u5E93\u6587\u4EF6\u5939");
|
|
2371
|
+
async function walk(folder) {
|
|
2372
|
+
signal?.throwIfAborted();
|
|
2373
|
+
const entries = await readdir(join7(root, folder), { withFileTypes: true });
|
|
2374
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
2375
|
+
signal?.throwIfAborted();
|
|
2376
|
+
if (entry.name.startsWith(".")) continue;
|
|
2377
|
+
const key = posix.join(folder, entry.name);
|
|
2378
|
+
if (entry.isDirectory()) {
|
|
2379
|
+
await walk(key);
|
|
2380
|
+
continue;
|
|
2381
|
+
}
|
|
2382
|
+
if (!entry.isFile() || !/\.md$/i.test(entry.name)) continue;
|
|
2383
|
+
const file = await open2(join7(root, key), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
2384
|
+
let raw;
|
|
2385
|
+
try {
|
|
2386
|
+
raw = await file.readFile({ encoding: "utf8", signal });
|
|
2387
|
+
} finally {
|
|
2388
|
+
await file.close();
|
|
2389
|
+
}
|
|
2390
|
+
const header = /^(?:\uFEFF)?---[ \t]*\r?\n[\s\S]*?^---[ \t]*(?:\r?\n|$)/m.exec(raw);
|
|
2391
|
+
const frontmatter = header?.index === 0 ? header[0] : void 0;
|
|
2392
|
+
const note = raw.slice(frontmatter?.length ?? 0);
|
|
2393
|
+
const metadataTags = /^tags:[ \t]*(.*)((?:\r?\n[ \t]*-[ \t]+[^\r\n]*)*)/m.exec(frontmatter ?? "");
|
|
2394
|
+
const tagValues = metadataTags ? metadataTags[1] + metadataTags[2].replace(/^[ \t]*-[ \t]+/gm, " ") : "";
|
|
2395
|
+
const tags = new Set(tagValues.match(/[\p{L}\p{N}_/-]+/gu) ?? []);
|
|
2396
|
+
const markup = note.replace(/<!--[^]*?(?:-->|$)|%%[^]*?(?:%%|$)/g, (comment) => comment.replace(/[^\r\n]/g, " "));
|
|
2397
|
+
let fence;
|
|
2398
|
+
for (const line of markup.split(/\r?\n/)) {
|
|
2399
|
+
const marker = /^ {0,3}(`{3,}|~{3,})/.exec(line)?.[1];
|
|
2400
|
+
if (marker && !fence) {
|
|
2401
|
+
fence = marker;
|
|
2402
|
+
continue;
|
|
2403
|
+
}
|
|
2404
|
+
if (fence) {
|
|
2405
|
+
if (marker?.startsWith(fence) && line.trim() === marker) fence = void 0;
|
|
2406
|
+
continue;
|
|
2407
|
+
}
|
|
2408
|
+
const text2 = line.replace(/(`+)[^`]*\1/g, (code) => " ".repeat(code.length));
|
|
2409
|
+
for (const match of text2.matchAll(/(?:^|[\s((,。!?、;:])#([\p{L}\p{N}_/-]+)/gu)) tags.add(match[1]);
|
|
2410
|
+
for (const match of text2.matchAll(/(!|\\)?\[\[([^\]\n]+)\]\]/g)) {
|
|
2411
|
+
if (match[1]) continue;
|
|
2412
|
+
const target = match[2].split(/[|#]/, 1)[0].trim();
|
|
2413
|
+
if (!target) continue;
|
|
2414
|
+
const start = match.index;
|
|
2415
|
+
const end = start + match[0].length;
|
|
2416
|
+
const before = line.slice(0, start).split(/[。!?!?]/).at(-1) ?? "";
|
|
2417
|
+
const after = line.slice(end).split(/[。!?!?]/, 1)[0];
|
|
2418
|
+
const sentence = [...(before + match[0] + after).trim()];
|
|
2419
|
+
const why = sentence.length <= 40 ? sentence.join("") : [...before.trimEnd()].slice(-8).concat([...match[0] + after]).slice(0, 40).join("").trim();
|
|
2420
|
+
references.push({ from: key, to: target, why });
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
notes.push({ key, note: withTitle(note, key, raw), frontmatter, tags: [...tags].sort() });
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
await walk("");
|
|
2427
|
+
} catch (error) {
|
|
2428
|
+
if (signal?.aborted || error instanceof ShelfError) throw error;
|
|
2429
|
+
throw new ShelfError("\u65E0\u6CD5\u8BFB\u53D6\u8FD9\u4E2A Obsidian \u7B14\u8BB0\u5E93\uFF0C\u8BF7\u786E\u8BA4\u8DEF\u5F84\u548C\u8BFB\u53D6\u6743\u9650");
|
|
2430
|
+
}
|
|
2431
|
+
const paths = new Set(notes.map((note) => note.key));
|
|
2432
|
+
const byName = /* @__PURE__ */ new Map();
|
|
2433
|
+
for (const { key } of notes) {
|
|
2434
|
+
const name2 = posix.basename(key).replace(/\.md$/i, "");
|
|
2435
|
+
const matches = byName.get(name2);
|
|
2436
|
+
if (matches) matches.push(key);
|
|
2437
|
+
else byName.set(name2, [key]);
|
|
2438
|
+
}
|
|
2439
|
+
const links = references.flatMap((link) => {
|
|
2440
|
+
const target = /\.md$/i.test(link.to) ? link.to : link.to + ".md";
|
|
2441
|
+
const relative2 = posix.normalize(posix.join(posix.dirname(link.from), target));
|
|
2442
|
+
const matches = byName.get(link.to.replace(/\.md$/i, ""));
|
|
2443
|
+
const to = target.startsWith(".") ? paths.has(relative2) ? relative2 : void 0 : target.includes("/") ? paths.has(target) ? target : paths.has(relative2) ? relative2 : void 0 : paths.has(relative2) ? relative2 : matches?.length === 1 ? matches[0] : void 0;
|
|
2444
|
+
return to && to !== link.from ? [{ ...link, to }] : [];
|
|
2445
|
+
});
|
|
2446
|
+
return { root, notes, links, unresolved: references.length - links.length };
|
|
2447
|
+
}
|
|
2448
|
+
var obsidian = {
|
|
2449
|
+
async scan(path, offset, signal) {
|
|
2450
|
+
if (offset !== void 0 && (typeof offset !== "number" || !Number.isSafeInteger(offset) || offset < 0)) throw new ShelfError("offset \u5E94\u4E3A\u975E\u8D1F\u6574\u6570");
|
|
2451
|
+
const { notes, links, unresolved } = await readVault(path, signal);
|
|
2452
|
+
const start = Number(offset ?? 0);
|
|
2453
|
+
const folders = /* @__PURE__ */ new Map();
|
|
2454
|
+
const targets = /* @__PURE__ */ new Map();
|
|
2455
|
+
for (const link of links) {
|
|
2456
|
+
const ids = targets.get(link.from);
|
|
2457
|
+
if (ids) ids.push(link.to);
|
|
2458
|
+
else targets.set(link.from, [link.to]);
|
|
2459
|
+
}
|
|
2460
|
+
const listing = notes.map(({ key, tags }) => {
|
|
2461
|
+
const path2 = posix.dirname(key);
|
|
2462
|
+
const folder = folders.get(path2) ?? { path: path2, count: 0, tags: /* @__PURE__ */ new Set(), links: 0 };
|
|
2463
|
+
folders.set(path2, folder);
|
|
2464
|
+
const linked = targets.get(key) ?? [];
|
|
2465
|
+
folder.count += 1;
|
|
2466
|
+
for (const tag of tags) folder.tags.add(tag);
|
|
2467
|
+
folder.links += linked.length;
|
|
2468
|
+
return { path: key, tags, links: linked };
|
|
2469
|
+
});
|
|
2470
|
+
return {
|
|
2471
|
+
count: notes.length,
|
|
2472
|
+
links: links.length,
|
|
2473
|
+
unresolved,
|
|
2474
|
+
folders: [...folders.values()].map((folder) => ({ ...folder, tags: [...folder.tags].sort() })),
|
|
2475
|
+
notes: listing.slice(start, start + 100),
|
|
2476
|
+
nextOffset: start + 100 < listing.length ? start + 100 : void 0
|
|
2477
|
+
};
|
|
2478
|
+
},
|
|
2479
|
+
async prepare(path, plan, signal) {
|
|
2480
|
+
const { root, notes, links } = await readVault(path, signal);
|
|
2481
|
+
if (notes.length === 0) throw new ShelfError("\u8FD9\u4E2A\u6587\u4EF6\u5939\u6CA1\u6709 Markdown \u7B14\u8BB0");
|
|
2482
|
+
const source = "obsidian:" + createHash2("sha256").update(root).digest("hex");
|
|
2483
|
+
const groups = [];
|
|
2484
|
+
if (plan === void 0) {
|
|
2485
|
+
const folders = /* @__PURE__ */ new Map();
|
|
2486
|
+
for (const note of notes) {
|
|
2487
|
+
const folder = posix.dirname(note.key);
|
|
2488
|
+
const group = folders.get(folder) ?? { key: "folder:" + folder, title: posix.basename(folder === "." ? root : folder) || "Obsidian", cards: [] };
|
|
2489
|
+
folders.set(folder, group);
|
|
2490
|
+
group.cards.push(note);
|
|
2491
|
+
}
|
|
2492
|
+
groups.push(...folders.values());
|
|
2493
|
+
} else {
|
|
2494
|
+
if (!Array.isArray(plan) || plan.length === 0) throw new ShelfError("\u5206\u683C\u65B9\u6848\u81F3\u5C11\u9700\u8981\u4E00\u4E2A\u76EE\u6807");
|
|
2495
|
+
const byPath = new Map(notes.map((note) => [note.key, note]));
|
|
2496
|
+
for (const group of plan) {
|
|
2497
|
+
if (!group || typeof group !== "object") throw new ShelfError("\u5206\u683C\u65B9\u6848\u65E0\u6548");
|
|
2498
|
+
const { title, item_id: itemId, paths, tags } = group;
|
|
2499
|
+
if (paths === void 0 === (tags === void 0)) throw new ShelfError("\u6BCF\u4E2A\u76EE\u6807\u8BF7\u5728 paths \u548C tags \u4E2D\u9009\u4E00\u9879");
|
|
2500
|
+
const selection = paths ?? tags;
|
|
2501
|
+
if (!Array.isArray(selection) || selection.length === 0 || selection.some((value) => typeof value !== "string" || !value.trim())) {
|
|
2502
|
+
throw new ShelfError("paths \u6216 tags \u5FC5\u987B\u662F\u975E\u7A7A\u7684\u6587\u5B57\u5217\u8868");
|
|
2503
|
+
}
|
|
2504
|
+
const cards = paths === void 0 ? notes.filter((note) => note.tags.some((tag) => selection.includes(tag))) : selection.map((key) => {
|
|
2505
|
+
const note = byPath.get(key);
|
|
2506
|
+
if (!note) throw new ShelfError(`\u65B9\u6848\u4E2D\u7684\u7B14\u8BB0\u4E0D\u5B58\u5728\uFF1A${key}`);
|
|
2507
|
+
return note;
|
|
2508
|
+
});
|
|
2509
|
+
if (cards.length === 0) throw new ShelfError("\u6240\u9009\u6807\u7B7E\u6CA1\u6709\u5339\u914D\u7684\u7B14\u8BB0");
|
|
2510
|
+
if (typeof itemId === "string" && itemId) groups.push({ key: itemId, itemId, title: "", cards });
|
|
2511
|
+
else if (itemId === void 0 && typeof title === "string" && title.trim()) groups.push({ key: "title:" + title.trim(), title: title.trim(), cards });
|
|
2512
|
+
else throw new ShelfError("\u6BCF\u4E2A\u76EE\u6807\u8BF7\u586B\u5DF2\u6709\u683C\u5B50 item_id \u6216\u65B0\u683C\u5B50 title");
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
return { source, groups, links };
|
|
2516
|
+
}
|
|
2517
|
+
};
|
|
2518
|
+
|
|
2519
|
+
// src/host/harness/tools.ts
|
|
1933
2520
|
var stringOut = {
|
|
1934
2521
|
schema: { type: "string" },
|
|
1935
2522
|
render: (_args, value) => [{ type: "text", text: value }]
|
|
@@ -1950,12 +2537,19 @@ function createTools(store, active, memory = new Memory(store)) {
|
|
|
1950
2537
|
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
2538
|
};
|
|
1952
2539
|
const activeOf = (exec) => exec.agent ? active.get(exec.agent.id) : void 0;
|
|
1953
|
-
|
|
2540
|
+
const shared = createPalaceTools(store, memory, {
|
|
1954
2541
|
addBook: (args, signal) => addBook(store, args, signal),
|
|
1955
2542
|
readPageTexts,
|
|
1956
2543
|
scannedThreshold: SCANNED_THRESHOLD,
|
|
2544
|
+
scannedPageHelp: "\u5426\u5219\u8BF7\u7528\u6237\u5728\u53F3\u4FA7\u7B14\u8BB0\u672C\u91CC\u91CD\u65B0\u52FE\u9009\u8FD9\u4E9B\u9875\uFF08\u4F1A\u628A\u9875\u9762\u56FE\u7247\u53D1\u5230\u5BF9\u8BDD\uFF09\u3002",
|
|
2545
|
+
videoHelp: "\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",
|
|
2546
|
+
imageHelp: "image \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\u3002",
|
|
2547
|
+
imageDescription: "\u622A\u56FE\u7684\u672C\u5730\u8DEF\u5F84\u6216\u9644\u4EF6 id",
|
|
2548
|
+
currentItem: true,
|
|
2549
|
+
wikiReadHelp: "\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",
|
|
1957
2550
|
saveImage: (ref) => saveImage(store, ref)
|
|
1958
|
-
})
|
|
2551
|
+
});
|
|
2552
|
+
return [...shared, ...createObsidianTools(store, memory, obsidian), ...createWeReadTools(store, memory, weread)].map(({ execute, ...definition }) => defineTool({
|
|
1959
2553
|
...definition,
|
|
1960
2554
|
output: stringOut,
|
|
1961
2555
|
async execute(args, exec) {
|
|
@@ -1975,12 +2569,12 @@ function createTools(store, active, memory = new Memory(store)) {
|
|
|
1975
2569
|
}));
|
|
1976
2570
|
}
|
|
1977
2571
|
|
|
1978
|
-
// src/host/index.ts
|
|
2572
|
+
// src/host/harness/index.ts
|
|
1979
2573
|
var name = "dsh-palace";
|
|
1980
|
-
var defaultDataDir = () =>
|
|
2574
|
+
var defaultDataDir = () => join8(homedir4(), "Documents", "dsh-palace-data");
|
|
1981
2575
|
function apply(ctx, config = {}) {
|
|
1982
2576
|
const dir = config.dataDir ?? defaultDataDir();
|
|
1983
|
-
const firstRun = !existsSync5(
|
|
2577
|
+
const firstRun = !existsSync5(join8(dir, "palace.json"));
|
|
1984
2578
|
const store = new PalaceStore(dir);
|
|
1985
2579
|
if (firstRun) void seedWelcome(store).catch((error) => console.warn("[dsh-palace] \u653E\u5165\u4ECB\u7ECD\u683C\u5B50\u5931\u8D25", error));
|
|
1986
2580
|
const active = /* @__PURE__ */ new Map();
|