dsh-plugin-zotero 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,7 +21,7 @@
21
21
  - **整理文库**:新建、重命名、移动、删除分类,批量把条目加入或移出分类。你说一句「把这几篇归到『待读』里」,它就照做。
22
22
  - **帮你入库**:把本地下载好的论文、PDF 交给 DSH 导入 Zotero;也可以让它读完外文文献后,把译文作为附件挂回条目。
23
23
  - **带进对话**:右侧面板单条目引用、全文划词摘录或整分类引用,一键写进当前会话草稿(不自动发送),你再接着说要求。
24
- - **安全可控**:浏览面板严格只读;模型能用的每一项工具都能单独开关,想彻底只读就把写操作关掉。
24
+ - **安全可控**:浏览面板严格只读;模型能用的每一项工具都能单独开关,想彻底只读就把写操作关掉;写操作优先走集成 MCP,它在 Zotero 进程内直接执行,不触发本地 API 的授权弹窗。
25
25
 
26
26
  面板分三块:顶部状态栏显示连接状态并提供刷新;左侧分类树按分类浏览与搜索;右侧详情区展示正文与「参考文献 / 笔记 / 标注 / 附件」标签页。
27
27
 
@@ -78,7 +78,7 @@ curl http://127.0.0.1:23119/api/ # 期望输出:Zotero is running
78
78
  - **右侧栏面板「Zotero 文献库」**:顶部状态栏(Zotero / 模型工具探活)+ 左侧分类树(按需拉取、标题摘要搜索)+ 右侧条目详情或分类概要。
79
79
  - **详情区上下分区**:上方正文区展示标题、元数据、摘要与 PDF 全文;下方用 tab 切换「参考文献 / 笔记 / 标注 / 附件」。两个区域之间的分隔条可拖动,正文占比默认 85% 并按会话记住;没有次级内容的条目正文独占整栏。
80
80
  - **摘要默认折叠**:标题行右侧的箭头按钮展开 / 收起摘要,默认把空间留给正文与全文。
81
- - **笔记可读**:笔记以折叠列表呈现(默认收起,点击标题展开),正文保留 Zotero 的引用块、标注高亮、表格与外链图片;Host 侧已清洗脚本与事件属性,避免注入。「引用」把该条笔记转成纯文本写进会话草稿。
81
+ - **笔记可读**:独立笔记(文库里的全局笔记)打开后正文直接占据上方正文区,跟普通条目一样阅读;条目下的子笔记仍进「笔记」tab,以折叠列表呈现(默认收起,点击标题展开),正文保留 Zotero 的引用块、标注高亮、表格与外链图片。Host 侧已清洗脚本与事件属性,避免注入。「引用」把该条笔记转成纯文本写进会话草稿。
82
82
  - **标注可读**:PDF 上的高亮与划线汇总进「标注」tab(带颜色、页码、原文与评论,可按类型 / 颜色筛选);全文块里原位标出底色与下划线,点列表条目可滚动定位并闪烁,「引用」把该条标注写进会话草稿。
83
83
  - **五种插入方式**:单条目引用、全文划词摘录、单条标注、单条笔记、整目录引用,均只写进会话草稿(不自动发送、不抢焦点),文本携带 `itemKey` / `collectionKey` 供模型接续操作。
84
84
  - **面板严格只读**:不提供任何新建 / 改名 / 删除分类的按钮。
@@ -234,11 +234,13 @@ dsh --profile demo # 启动
234
234
 
235
235
  ### 模型侧 MCP 与权限边界
236
236
 
237
- 模型侧工具由**插件自己**桥接:[src/host/mcp-bridge.ts](src/host/mcp-bridge.ts) 直连本机 Zotero 集成 MCP(本机实测 `zotero-integrated-mcp v1.1.0`,暴露 20 个工具),按配置里的 `disabledMcpTools` 过滤后以 `mcp__zotero__*` 注册给模型。[cordis.patch.yml](cordis.patch.yml) **不再**插入 `@deepseek-ai/dsh-mcp-client` —— 它不支持按工具开关,且同 `serverName` 会与自注册工具重名冲突。
237
+ 模型侧工具由**插件自己**桥接:[src/host/mcp-bridge.ts](src/host/mcp-bridge.ts) 直连本机 Zotero 集成 MCP(本机实测暴露 25 个工具),按配置里的 `disabledMcpTools` 过滤后以 `mcp__zotero__*` 注册给模型。[cordis.patch.yml](cordis.patch.yml) **不再**插入 `@deepseek-ai/dsh-mcp-client` —— 它不支持按工具开关,且同 `serverName` 会与自注册工具重名冲突。
238
238
 
239
239
  ⚠️ 前提是 [Zotero 里已装好集成 MCP 插件](#1-安装-zotero-mcp-插件)(或改用其他 MCP 插件并在配置卡填对 `mcpUrl`),并且开启了「允许写操作」。插件未安装或服务未起时,模型侧工具全部不可用;插件装了但没开写操作时,读类工具正常,`create_collection` 等写工具会被插件拒绝(返回「写操作已禁用」),表现为让模型帮忙新建分类直接报错——此时到 MCP 插件设置里打开写操作并重试即可,无需重启 dsh。
240
240
 
241
- ⚠️ 面板只读,**但模型侧工具不是**:20 个工具中含 5 个改变文库结构的写工具(`create_collection` / `update_collection` / `delete_collection` / `add_items_to_collection` / `remove_items_from_collection`)。想收紧为纯只读,直接在插件页配置卡的「MCP 工具」里关掉它们并保存即可;面板功能不受影响。
241
+ ⚠️ 面板只读,**但模型侧工具不是**:25 个工具中含 10 个写工具(`write_item` / `write_note` / `write_tag` / `write_metadata` / `add_by_identifier` / `create_collection` / `update_collection` / `delete_collection` / `add_items_to_collection` / `remove_items_from_collection`)。想收紧为纯只读,直接在插件页配置卡的「MCP 工具」里关掉它们并保存即可;面板功能不受影响。
242
+
243
+ **写操作为什么优先走 MCP**:这些工具在 Zotero 进程内直接改库,不经过本地 API,因此**不会触发 Zotero 的「本地 API 授权」弹窗**。反过来,若让模型用 shell/curl 直接打 `127.0.0.1:23119` 的写接口,就必须先用 `POST /api/local/authorize` 换 key —— 该端点每次调用都会弹一次授权框,用户点「允许」拿到的 key 还是一次性的(首个写请求即被 Zotero 删除),这就是「每写一次就要点一次允许」的根源。为此插件在 [mcp-bridge.ts](src/host/mcp-bridge.ts) 里给所有写工具的描述统一注入了「首选写通道」引导语;「导入本地文件并挂到某条目下」用 `write_item` 的 `action: "import"` + `filePath` + `parentItemKey`,一条工具调用即可。
242
244
 
243
245
  ## 相关文档
244
246
 
package/client/client.js CHANGED
@@ -31,6 +31,7 @@ const dicts = {
31
31
  "action.insertExcerpt": "添加摘录",
32
32
  "action.insertToChat": "引用到会话",
33
33
  "action.insertCollection": "添加整目录引用",
34
+ "action.retry": "重试",
34
35
  "status.zoteroOn": "Zotero 已连接",
35
36
  "status.zoteroOff": "Zotero 未连接",
36
37
  "status.mcpOn": "模型工具可用",
@@ -43,7 +44,9 @@ const dicts = {
43
44
  "tree.searchPlaceholder": "搜索标题 / 摘要",
44
45
  "tree.count": "{n} 条",
45
46
  "tree.empty": "这个分类下没有条目",
47
+ "tree.loadFailed": "条目加载失败",
46
48
  "detail.empty": "从左侧选择分类或条目",
49
+ "detail.loadFailed": "条目加载失败:{message}",
47
50
  "detail.metadata": "文献信息",
48
51
  "detail.metadataExpand": "展开文献信息",
49
52
  "detail.metadataCollapse": "收起文献信息",
@@ -71,12 +74,15 @@ const dicts = {
71
74
  "detail.annotationNotAnchoredTruncated": "全文超出索引上限,这段原文可能在未返回的部分",
72
75
  "detail.annotationNotAnchoredNoFulltext": "该附件没有全文索引,无法定位",
73
76
  "detail.fulltext": "全文",
74
- "detail.noFulltext": "该附件没有可用的全文索引",
77
+ "detail.noteBody": "笔记正文",
78
+ "detail.noFulltext": "该条目的附件都没有全文索引;可在 Zotero 中右键附件,选择「重新索引条目」",
79
+ "detail.fulltextFallback": "首选附件没有全文索引,已显示同条目其他附件的全文",
75
80
  "detail.fulltextTruncated": "(已按长度截断)",
76
81
  "detail.collectionSummary": "分类:{name}",
77
82
  "detail.collectionCount": "共 {n} 条条目",
78
83
  "detail.bibTitle": "参考文献(APA)",
79
- "detail.noBib": "该分类没有可导出的引用条目(裸 PDF 附件不产生引用)",
84
+ "detail.noBib": "该分类没有可导出的引用条目(笔记与裸 PDF 附件不产生引用)",
85
+ "detail.items": "条目",
80
86
  "error.noEditor": "当前面板不在会话上下文中,无法插入到输入框",
81
87
  "error.draftChanged": "输入框草稿已变化,未插入;请避免在插入期间编辑草稿",
82
88
  "error.empty": "没有可插入的内容",
@@ -149,6 +155,7 @@ const dicts = {
149
155
  "action.insertExcerpt": "Add excerpt",
150
156
  "action.insertToChat": "Add to chat",
151
157
  "action.insertCollection": "Add collection citations",
158
+ "action.retry": "Retry",
152
159
  "status.zoteroOn": "Zotero connected",
153
160
  "status.zoteroOff": "Zotero not connected",
154
161
  "status.mcpOn": "Model tools available",
@@ -161,7 +168,9 @@ const dicts = {
161
168
  "tree.searchPlaceholder": "Search title / abstract",
162
169
  "tree.count": "{n} items",
163
170
  "tree.empty": "No items in this collection",
171
+ "tree.loadFailed": "Failed to load items",
164
172
  "detail.empty": "Select a collection or item on the left",
173
+ "detail.loadFailed": "Failed to load the item: {message}",
165
174
  "detail.metadata": "Metadata",
166
175
  "detail.metadataExpand": "Expand metadata",
167
176
  "detail.metadataCollapse": "Collapse metadata",
@@ -189,12 +198,15 @@ const dicts = {
189
198
  "detail.annotationNotAnchoredTruncated": "The full text was truncated; this passage may be in the omitted part",
190
199
  "detail.annotationNotAnchoredNoFulltext": "No full-text index for this attachment, so it cannot be located",
191
200
  "detail.fulltext": "Full text",
192
- "detail.noFulltext": "No full-text index available for this attachment",
201
+ "detail.noteBody": "Note",
202
+ "detail.noFulltext": "None of this item’s attachments have a full-text index. Right-click the attachment in Zotero and choose “Reindex Item”.",
203
+ "detail.fulltextFallback": "The preferred attachment has no full-text index; showing another attachment from this item",
193
204
  "detail.fulltextTruncated": "(truncated by length)",
194
205
  "detail.collectionSummary": "Collection: {name}",
195
206
  "detail.collectionCount": "{n} items",
196
207
  "detail.bibTitle": "Bibliography (APA)",
197
- "detail.noBib": "Nothing citable here (bare PDF attachments produce no citation)",
208
+ "detail.noBib": "Nothing citable here (notes and bare PDF attachments produce no citation)",
209
+ "detail.items": "Items",
198
210
  "error.noEditor": "This panel is not in a conversation context; cannot insert into the composer",
199
211
  "error.draftChanged": "The draft changed; nothing was inserted. Avoid editing while inserting.",
200
212
  "error.empty": "Nothing to insert",
@@ -342,12 +354,14 @@ function initialState() {
342
354
  expanded: /* @__PURE__ */ new Set([TREE_ROOT_ID]),
343
355
  nodeItems: {},
344
356
  nodeTotals: {},
357
+ nodeErrors: {},
345
358
  query: "",
346
359
  results: null,
347
360
  selectedItemKey: null,
348
361
  selectedCollection: null,
349
362
  detail: null,
350
363
  detailLoading: false,
364
+ detailError: null,
351
365
  collectionBib: null,
352
366
  toast: null
353
367
  };
@@ -359,6 +373,8 @@ let state = initialState();
359
373
  const listeners = /* @__PURE__ */ new Set();
360
374
  let treeInflight = null;
361
375
  const nodeInflight = /* @__PURE__ */ new Set();
376
+ /** 各节点最近一次加载用的定位信息;刷新时用来重拉已展开的分支。 */
377
+ const nodeTargets = /* @__PURE__ */ new Map();
362
378
  let searchTimer = null;
363
379
  let toastTimer = null;
364
380
  function patch(next) {
@@ -368,6 +384,12 @@ function patch(next) {
368
384
  };
369
385
  for (const listener of listeners) listener();
370
386
  }
387
+ /** 返回去掉某个 key 的记录;没有该 key 时原样返回,避免无意义的新对象。 */
388
+ function withoutKey(record, key) {
389
+ if (!(key in record)) return record;
390
+ const { [key]: _removed, ...rest } = record;
391
+ return rest;
392
+ }
371
393
  /** 订阅整份面板状态(任一字段变化都会触发重渲染)。 */
372
394
  function usePaneState() {
373
395
  const [snapshot, setSnapshot] = (0, react.useState)(getPaneState);
@@ -397,6 +419,21 @@ function loadTree(force = false) {
397
419
  if (treeInflight !== null) return treeInflight;
398
420
  if (!force && state.loaded) return Promise.resolve();
399
421
  patch({ treeLoading: true });
422
+ if (force) {
423
+ const expanded = [...state.expanded];
424
+ patch({
425
+ nodeItems: {},
426
+ nodeTotals: {},
427
+ nodeErrors: {},
428
+ collectionBib: null
429
+ });
430
+ for (const nodeId of expanded) {
431
+ const target = nodeTargets.get(nodeId);
432
+ if (target !== void 0) loadNodeItems(nodeId, target, true);
433
+ }
434
+ if (state.selectedItemKey !== null) openItem(state.selectedItemKey);
435
+ else if (state.selectedCollection !== null) selectCollection(state.selectedCollection);
436
+ }
400
437
  const run = (async () => {
401
438
  try {
402
439
  const [healthData, tree] = await Promise.all([fetchHealth(), fetchTree()]);
@@ -417,10 +454,17 @@ function loadTree(force = false) {
417
454
  treeInflight = run;
418
455
  return run;
419
456
  }
420
- /** 按需拉取某节点的条目;已拉取或正在拉取时直接返回。 */
421
- function loadNodeItems(nodeId, target) {
422
- if (state.nodeItems[nodeId] !== void 0 || nodeInflight.has(nodeId)) return;
457
+ /** 按需拉取某节点的条目;已拉取或正在拉取时直接返回(force 时清缓存重拉)。 */
458
+ function loadNodeItems(nodeId, target, force = false) {
459
+ nodeTargets.set(nodeId, target);
460
+ if (nodeInflight.has(nodeId)) return;
461
+ if (!force && state.nodeItems[nodeId] !== void 0) return;
423
462
  nodeInflight.add(nodeId);
463
+ if (force) patch({
464
+ nodeItems: withoutKey(state.nodeItems, nodeId),
465
+ nodeTotals: withoutKey(state.nodeTotals, nodeId)
466
+ });
467
+ patch({ nodeErrors: withoutKey(state.nodeErrors, nodeId) });
424
468
  fetchItems({
425
469
  ...target,
426
470
  limit: NODE_LIMIT
@@ -435,21 +479,24 @@ function loadNodeItems(nodeId, target) {
435
479
  [nodeId]: data.total
436
480
  }
437
481
  });
438
- }).catch(() => {
482
+ }).catch((error) => {
439
483
  patch({
440
- nodeItems: {
441
- ...state.nodeItems,
442
- [nodeId]: []
443
- },
444
- nodeTotals: {
445
- ...state.nodeTotals,
446
- [nodeId]: 0
484
+ nodeItems: withoutKey(state.nodeItems, nodeId),
485
+ nodeTotals: withoutKey(state.nodeTotals, nodeId),
486
+ nodeErrors: {
487
+ ...state.nodeErrors,
488
+ [nodeId]: errorMessage(error)
447
489
  }
448
490
  });
449
491
  }).finally(() => {
450
492
  nodeInflight.delete(nodeId);
451
493
  });
452
494
  }
495
+ /** 重试某个加载失败的节点(树行内「重试」)。 */
496
+ function retryNode(nodeId) {
497
+ const target = nodeTargets.get(nodeId);
498
+ if (target !== void 0) loadNodeItems(nodeId, target, true);
499
+ }
453
500
  /** 三角箭头:只开合节点,展开时按需拉取条目(根节点没有条目,不传 target)。 */
454
501
  function toggleTreeNode(nodeId, target) {
455
502
  const willOpen = !state.expanded.has(nodeId);
@@ -482,14 +529,15 @@ function setQuery(next) {
482
529
  }).then((data) => patch({ results: data.items })).catch(() => patch({ results: [] }));
483
530
  }, SEARCH_DEBOUNCE_MS);
484
531
  }
485
- /** 打开条目详情。 */
532
+ /** 打开条目详情。失败时记录原因,由详情区就地显示「重试」。 */
486
533
  function openItem(key) {
487
534
  patch({
488
535
  selectedItemKey: key,
489
536
  detail: null,
490
- detailLoading: true
537
+ detailLoading: true,
538
+ detailError: null
491
539
  });
492
- fetchItem(key).then((data) => patch({ detail: data })).catch((error) => showToast(errorMessage(error))).finally(() => patch({ detailLoading: false }));
540
+ fetchItem(key).then((data) => patch({ detail: data })).catch((error) => patch({ detailError: errorMessage(error) })).finally(() => patch({ detailLoading: false }));
493
541
  }
494
542
  /** 选中分类并拉取该目录的引用(APA);虚拟分类(出版物/未分类/回收站)不导出引用。 */
495
543
  function selectCollection(collection) {
@@ -518,6 +566,7 @@ function resetPaneStore() {
518
566
  toastTimer = null;
519
567
  treeInflight = null;
520
568
  nodeInflight.clear();
569
+ nodeTargets.clear();
521
570
  state = initialState();
522
571
  for (const listener of listeners) listener();
523
572
  }
@@ -975,6 +1024,17 @@ function hintStyle$1(depth) {
975
1024
  paddingLeft: BASE_INDENT + depth * INDENT + 22
976
1025
  };
977
1026
  }
1027
+ /** 行内「重试」按钮:失败提示旁的小按钮。 */
1028
+ const retryButtonStyle = {
1029
+ padding: "0 6px",
1030
+ fontSize: 11.5,
1031
+ lineHeight: "18px",
1032
+ color: "inherit",
1033
+ background: "transparent",
1034
+ border: "1px solid rgba(127,127,127,0.35)",
1035
+ borderRadius: 5,
1036
+ cursor: "pointer"
1037
+ };
978
1038
  function Chevron({ open, onToggle }) {
979
1039
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
980
1040
  onClick: (event) => {
@@ -1046,15 +1106,29 @@ function ItemRow({ ctx, item, depth }) {
1046
1106
  });
1047
1107
  }
1048
1108
  function renderItems(ctx, nodeId, depth) {
1109
+ if (ctx.nodeErrors[nodeId] !== void 0) return [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
1110
+ style: {
1111
+ ...hintStyle$1(depth),
1112
+ display: "flex",
1113
+ alignItems: "center",
1114
+ gap: 8
1115
+ },
1116
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1117
+ style: { color: "#d29922" },
1118
+ children: ctx.t("tree.loadFailed")
1119
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
1120
+ type: "button",
1121
+ onClick: () => ctx.retry(nodeId),
1122
+ style: retryButtonStyle,
1123
+ children: ctx.t("action.retry")
1124
+ })]
1125
+ }, "error")];
1049
1126
  const items = ctx.itemsByNode[nodeId];
1050
1127
  if (items === void 0) return [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1051
1128
  style: hintStyle$1(depth),
1052
1129
  children: ctx.t("status.loading")
1053
1130
  }, "loading")];
1054
- if (items.length === 0) return [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
1055
- style: hintStyle$1(depth),
1056
- children: ctx.t("tree.empty")
1057
- }, "empty")];
1131
+ if (items.length === 0) return [];
1058
1132
  return items.map((item) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ItemRow, {
1059
1133
  ctx,
1060
1134
  item,
@@ -1183,7 +1257,7 @@ function VirtualRows({ ctx, node, depth }) {
1183
1257
  function CollectionTree(props) {
1184
1258
  const { t, collections, selectedItemKey, selectedCollectionKey, onSelectItem, onSelectCollection, onInsertItem, onInsertCollection } = props;
1185
1259
  /** 展开集合与各节点条目都在 store 里(默认展开根节点),重挂面板后原样恢复。 */
1186
- const { expanded, nodeItems, nodeTotals } = usePaneState();
1260
+ const { expanded, nodeItems, nodeTotals, nodeErrors } = usePaneState();
1187
1261
  /** 按需拉取:条目一律由 toggle / select 触发,根节点不再预取全库条目。 */
1188
1262
  function open(nodeId, collection) {
1189
1263
  expandTreeNode(nodeId, loadOpts(collection));
@@ -1200,10 +1274,12 @@ function CollectionTree(props) {
1200
1274
  expanded,
1201
1275
  itemsByNode: nodeItems,
1202
1276
  totalsByNode: nodeTotals,
1277
+ nodeErrors,
1203
1278
  selectedItemKey,
1204
1279
  selectedCollectionKey,
1205
1280
  toggle,
1206
1281
  select,
1282
+ retry: retryNode,
1207
1283
  onSelectItem,
1208
1284
  onInsertItem,
1209
1285
  onInsertCollection
@@ -2028,7 +2104,7 @@ function NoteList({ t, notes, onInsert }) {
2028
2104
  }), open && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2029
2105
  className: "dsh-zotero-note",
2030
2106
  dangerouslySetInnerHTML: { __html: note.html },
2031
- style: noteBodyStyle
2107
+ style: noteBodyStyle$1
2032
2108
  })]
2033
2109
  }, note.key);
2034
2110
  })] });
@@ -2038,6 +2114,9 @@ function NoteList({ t, notes, onInsert }) {
2038
2114
  *
2039
2115
  * 笔记 HTML 由 Zotero 生成,段落/引用/高亮标记都是默认样式,这里补一套
2040
2116
  * 贴合右栏密度的排版(引用块、标注高亮、图片、表格)。
2117
+ *
2118
+ * 独立笔记(条目本身就是 note)在详情「全文」位置渲染,那里也引这份样式,
2119
+ * 因此导出。
2041
2120
  */
2042
2121
  const NOTE_CSS = `
2043
2122
  .dsh-zotero-note p { margin: 0 0 6px; }
@@ -2087,7 +2166,7 @@ const insertButtonStyle$1 = {
2087
2166
  lineHeight: 1.2,
2088
2167
  whiteSpace: "nowrap"
2089
2168
  };
2090
- const noteBodyStyle = {
2169
+ const noteBodyStyle$1 = {
2091
2170
  marginTop: 6,
2092
2171
  padding: "6px 8px",
2093
2172
  border: "1px solid rgba(127,127,127,0.25)",
@@ -2114,6 +2193,9 @@ const noteBodyStyle = {
2114
2193
  *
2115
2194
  * 「添加到会话」两处入口:头部按钮(整条目元数据)、全文块里的选区浮出按钮
2116
2195
  * (摘录)。全文块内用鼠标选中文本后浮出小按钮,位置取选区 rect。
2196
+ *
2197
+ * 独立笔记(条目自身 `itemType: "note"`)的正文直接作为全文块内容渲染,
2198
+ * 与打开普通条目一样占据正文区,不再只能去底部「笔记」tab 里翻。
2117
2199
  */
2118
2200
  /** 正文区占比的存储键与取值范围(%)。 */
2119
2201
  const BODY_PERCENT_KEY = "dsh-plugin-zotero.detailBodyPercent";
@@ -2150,6 +2232,34 @@ const insertButtonStyle = {
2150
2232
  lineHeight: 1.2,
2151
2233
  whiteSpace: "nowrap"
2152
2234
  };
2235
+ /** 正文块外框:PDF 全文与独立笔记共用,仅排版细节不同。 */
2236
+ const bodyBoxStyle = {
2237
+ position: "relative",
2238
+ flex: 1,
2239
+ minHeight: 60,
2240
+ overflow: "auto",
2241
+ padding: "8px 10px",
2242
+ border: "1px solid rgba(127,127,127,0.25)",
2243
+ borderRadius: 6,
2244
+ fontSize: 12.5,
2245
+ opacity: .9
2246
+ };
2247
+ /** PDF 全文:纯文本,保留换行。 */
2248
+ const textBodyStyle = {
2249
+ ...bodyBoxStyle,
2250
+ whiteSpace: "pre-wrap",
2251
+ lineHeight: 1.7
2252
+ };
2253
+ /** 独立笔记:HTML 自带块级换行,不能再叠 pre-wrap,否则标签间空白会被放大。 */
2254
+ const noteBodyStyle = {
2255
+ ...bodyBoxStyle,
2256
+ lineHeight: 1.75,
2257
+ overflowWrap: "break-word"
2258
+ };
2259
+ const truncatedHintStyle = {
2260
+ marginTop: 6,
2261
+ opacity: .6
2262
+ };
2153
2263
  /** 去掉 CSL HTML 标签,只留可见文本(zotero 导出的是便携 HTML)。 */
2154
2264
  function stripTags$1(html) {
2155
2265
  return html.replace(/<[^>]*>/gu, "").replace(/\s+/gu, " ").trim();
@@ -2260,6 +2370,11 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2260
2370
  }
2261
2371
  const bib = detail.bib;
2262
2372
  const fulltext = detail.fulltext;
2373
+ /** 条目本身就是独立笔记时,正文即自身,直接放到全文位置展示。 */
2374
+ const selfNote = detail.itemType === "note" ? detail.notes.find((note) => note.key === detail.key) ?? null : null;
2375
+ /** 正文区是否有可展示的内容(PDF 全文或独立笔记正文)。 */
2376
+ const hasBody = fulltext !== null || selfNote !== null;
2377
+ const bodyKey = selfNote !== null ? selfNote.key : fulltext?.attachmentKey ?? "";
2263
2378
  const visibleAnnotations = (0, react.useMemo)(() => filterAnnotations(detail.annotations, annotationFilter), [detail.annotations, annotationFilter]);
2264
2379
  const segments = (0, react.useMemo)(() => buildSegments(fulltext?.content ?? "", visibleAnnotations), [fulltext, visibleAnnotations]);
2265
2380
  (0, react.useEffect)(() => () => {
@@ -2280,7 +2395,7 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2280
2395
  children: stripTags$1(bib)
2281
2396
  })
2282
2397
  });
2283
- if (detail.notes.length > 0) tabs.push({
2398
+ if (detail.notes.length > 0 && selfNote === null) tabs.push({
2284
2399
  id: "notes",
2285
2400
  label: t("detail.notes"),
2286
2401
  hint: String(detail.notes.length),
@@ -2436,7 +2551,7 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2436
2551
  minHeight: 0
2437
2552
  },
2438
2553
  children: [
2439
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { children: ANNOTATION_CSS }),
2554
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { children: ANNOTATION_CSS + NOTE_CSS }),
2440
2555
  /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2441
2556
  style: {
2442
2557
  flex: hasBottom ? `0 0 ${bodyPercent}%` : "1 1 auto",
@@ -2447,7 +2562,7 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2447
2562
  },
2448
2563
  children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2449
2564
  style: {
2450
- flex: fulltext !== null ? "0 1 auto" : "1 1 auto",
2565
+ flex: hasBody ? "0 1 auto" : "1 1 auto",
2451
2566
  minHeight: 44,
2452
2567
  overflow: "auto",
2453
2568
  padding: "12px 14px 0",
@@ -2470,7 +2585,7 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2470
2585
  lineHeight: 1.4
2471
2586
  },
2472
2587
  children: detail.title
2473
- }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
2588
+ }), selfNote === null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
2474
2589
  variant: "outline",
2475
2590
  size: "sm",
2476
2591
  onClick: onInsertItem,
@@ -2514,54 +2629,74 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2514
2629
  ]
2515
2630
  }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
2516
2631
  style: {
2517
- flex: fulltext !== null ? "1 1 0" : "0 0 auto",
2632
+ flex: hasBody ? "1 1 0" : "0 0 auto",
2518
2633
  minHeight: 0,
2519
2634
  display: "flex",
2520
2635
  flexDirection: "column",
2521
2636
  padding: "2px 14px 14px"
2522
2637
  },
2523
- children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("h3", {
2524
- style: sectionTitleStyle,
2525
- children: [t("detail.fulltext"), fulltext !== null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
2638
+ children: [
2639
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2526
2640
  style: {
2527
- opacity: .55,
2528
- fontWeight: 400,
2529
- marginLeft: 8
2641
+ display: "flex",
2642
+ alignItems: "center",
2643
+ gap: 6,
2644
+ marginBottom: 6
2530
2645
  },
2531
- children: fulltext.attachmentKey
2532
- })]
2533
- }), fulltext === null ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
2534
- style: {
2535
- margin: 0,
2536
- opacity: .6
2537
- },
2538
- children: t("detail.noFulltext")
2539
- }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2540
- ref: boxRef,
2541
- onMouseUp: handleMouseUp,
2542
- style: {
2543
- position: "relative",
2544
- flex: 1,
2545
- minHeight: 60,
2546
- overflow: "auto",
2547
- padding: "8px 10px",
2548
- border: "1px solid rgba(127,127,127,0.25)",
2549
- borderRadius: 6,
2550
- whiteSpace: "pre-wrap",
2551
- fontSize: 12.5,
2552
- lineHeight: 1.7,
2553
- opacity: .9
2554
- },
2555
- children: [
2556
- /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AnnotatedText, { segments }),
2557
- fulltext.truncated && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2558
- style: {
2559
- marginTop: 6,
2560
- opacity: .6
2561
- },
2646
+ children: [
2647
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("h3", {
2648
+ style: {
2649
+ ...sectionTitleStyle,
2650
+ margin: 0
2651
+ },
2652
+ children: [t(selfNote !== null ? "detail.noteBody" : "detail.fulltext"), bodyKey !== "" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
2653
+ style: {
2654
+ opacity: .55,
2655
+ fontWeight: 400,
2656
+ marginLeft: 8
2657
+ },
2658
+ children: bodyKey
2659
+ })]
2660
+ }),
2661
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { style: { flex: 1 } }),
2662
+ selfNote !== null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
2663
+ variant: "outline",
2664
+ size: "sm",
2665
+ onClick: () => onInsertNote(selfNote),
2666
+ title: t("action.insertToChat"),
2667
+ style: insertButtonStyle,
2668
+ children: t("action.insertItemShort")
2669
+ })
2670
+ ]
2671
+ }),
2672
+ fulltext?.fallback === true && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
2673
+ style: {
2674
+ margin: "0 0 6px",
2675
+ opacity: .6,
2676
+ fontSize: 12
2677
+ },
2678
+ children: t("detail.fulltextFallback")
2679
+ }),
2680
+ !hasBody ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
2681
+ style: {
2682
+ margin: 0,
2683
+ opacity: .6
2684
+ },
2685
+ children: t("detail.noFulltext")
2686
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2687
+ ref: boxRef,
2688
+ onMouseUp: handleMouseUp,
2689
+ style: selfNote === null ? textBodyStyle : noteBodyStyle,
2690
+ children: [selfNote !== null ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2691
+ className: "dsh-zotero-note",
2692
+ dangerouslySetInnerHTML: { __html: selfNote.html }
2693
+ }), selfNote.truncated && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2694
+ style: truncatedHintStyle,
2695
+ children: t("detail.noteTruncated")
2696
+ })] }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(AnnotatedText, { segments }), fulltext?.truncated === true && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2697
+ style: truncatedHintStyle,
2562
2698
  children: t("detail.fulltextTruncated")
2563
- }),
2564
- selection !== null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
2699
+ })] }), selection !== null && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
2565
2700
  type: "button",
2566
2701
  onMouseDown: (event) => event.preventDefault(),
2567
2702
  onClick: () => {
@@ -2584,9 +2719,9 @@ function ItemDetail({ t, detail, onInsertItem, onInsertExcerpt, onInsertAnnotati
2584
2719
  zIndex: 5
2585
2720
  },
2586
2721
  children: t("action.insertExcerpt")
2587
- })
2588
- ]
2589
- })]
2722
+ })]
2723
+ })
2724
+ ]
2590
2725
  })]
2591
2726
  }),
2592
2727
  hasBottom && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
@@ -2685,7 +2820,7 @@ function StatusDot({ ok, label }) {
2685
2820
  });
2686
2821
  }
2687
2822
  function ZoteroPane({ t, inputActions }) {
2688
- const { health, libraryName, collections, treeError, treeLoading, query, results, selectedItemKey, selectedCollection, detail, detailLoading, collectionBib, toast } = usePaneState();
2823
+ const { health, libraryName, collections, treeError, treeLoading, query, results, selectedItemKey, selectedCollection, detail, detailLoading, detailError, collectionBib, nodeItems, nodeErrors, toast } = usePaneState();
2689
2824
  (0, react.useEffect)(() => {
2690
2825
  loadTree();
2691
2826
  }, []);
@@ -2770,6 +2905,8 @@ function ZoteroPane({ t, inputActions }) {
2770
2905
  report,
2771
2906
  t
2772
2907
  ]);
2908
+ /** 分类详情里的条目列表:与左侧树共用同一份缓存(选中分类时树已按需拉取)。 */
2909
+ const collectionItems = selectedCollection === null ? void 0 : nodeItems[selectedCollection.key];
2773
2910
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2774
2911
  style: {
2775
2912
  position: "relative",
@@ -2915,9 +3052,24 @@ function ZoteroPane({ t, inputActions }) {
2915
3052
  overflow: "auto",
2916
3053
  minHeight: 0
2917
3054
  },
2918
- children: selectedItemKey !== null ? detailLoading || detail === null ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
3055
+ children: selectedItemKey !== null ? detailLoading ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2919
3056
  style: hintStyle,
2920
3057
  children: t("status.loading")
3058
+ }) : detail === null ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3059
+ style: {
3060
+ padding: "12px 14px",
3061
+ fontSize: 12.5,
3062
+ color: "#d29922"
3063
+ },
3064
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
3065
+ style: { margin: "0 0 8px" },
3066
+ children: tpl(t("detail.loadFailed"), { message: detailError ?? "" })
3067
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
3068
+ variant: "outline",
3069
+ size: "sm",
3070
+ onClick: () => openItem(selectedItemKey),
3071
+ children: t("action.retry")
3072
+ })]
2921
3073
  }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ItemDetail, {
2922
3074
  t,
2923
3075
  detail,
@@ -2975,7 +3127,25 @@ function ZoteroPane({ t, inputActions }) {
2975
3127
  opacity: .85
2976
3128
  },
2977
3129
  children: stripTags(collectionBib)
2978
- })] })
3130
+ })] }),
3131
+ collectionItems !== void 0 && collectionItems.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h3", {
3132
+ style: {
3133
+ ...sectionTitleStyle,
3134
+ margin: "16px 0 6px"
3135
+ },
3136
+ children: t("detail.items")
3137
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { children: collectionItems.map((item) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3138
+ onClick: () => openItem(item.key),
3139
+ style: resultRowStyle,
3140
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
3141
+ style: resultTitleStyle,
3142
+ title: item.title,
3143
+ children: item.title
3144
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
3145
+ style: resultMetaStyle,
3146
+ children: [item.creators, item.year].filter((part) => part !== "").join(" · ")
3147
+ })]
3148
+ }, item.key)) })] })
2979
3149
  ]
2980
3150
  }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
2981
3151
  style: hintStyle,
@@ -32,11 +32,53 @@ function blocksToText(content) {
32
32
  })
33
33
  .join('\n');
34
34
  }
35
+ /**
36
+ * 写工具判定:集成 MCP 里所有会改库的工具名都以这些动词开头
37
+ * (`write_item` / `create_collection` / `add_by_identifier` / `remove_items_from_collection` …)。
38
+ */
39
+ const WRITE_TOOL_RE = /^(write|add|create|update|delete|remove)_/i;
40
+ /** 破坏性写工具:删除/移除类,需要额外的确认提示。 */
41
+ const DESTRUCTIVE_TOOL_RE = /^(delete|remove)_/i;
42
+ /**
43
+ * 注入到写工具描述前面的引导语。
44
+ *
45
+ * 背景:Zotero 的本地 API(23119)写接口必须先用 `POST /api/local/authorize`
46
+ * 换一个 key,而该端点**每次调用都会弹授权框**、且不像应用名复用;模型若绕开
47
+ * MCP 手写 curl,就会每次写都让用户点一次「允许」。集成 MCP 的写工具在 Zotero
48
+ * 进程内直接改库,完全不触发这套授权,因此这里明确把 MCP 标成首选通道。
49
+ *
50
+ * 注意措辞是「优先」而不是「禁止」:集成 MCP 某些场景确实可能失败,必须给模型
51
+ * 留下降级空间,否则它会在报错后继续硬试或改用更糟的方式。
52
+ */
53
+ const WRITE_PREFIX = '[首选写通道] 本工具在 Zotero 进程内执行,不会触发本地 API 授权弹窗。写 Zotero 优先用它。' +
54
+ '仅当本工具不可用或调用失败时再考虑其他方式;不要直接用 shell/curl 访问 http://127.0.0.1:23119,' +
55
+ '那会每次都弹出「本地 API 授权」框让用户点允许。';
56
+ /** 针对「导入本地文件挂到条目下」这一高频场景,直接点明参数组合,省得模型去猜。 */
57
+ const WRITE_ITEM_HINT = '「导入 PDF/文件并挂到某个条目下」用本工具:action=import、filePath=<文件绝对路径>、parentItemKey=<目标条目 key>,title 可选。';
58
+ /** 按标识符入库优先走 Zotero 自带解析器,元数据比模型手填准确。 */
59
+ const IDENTIFIER_HINT = '「按 DOI/arXiv/ISBN/PMID 入库」优先用本工具,它调用 Zotero 自带的元数据解析器,比手填字段准确。';
60
+ const DESTRUCTIVE_HINT = '[破坏性写操作] 执行前必须先与用户确认要操作的具体条目/分类,不要凭推断批量删除或移除。';
61
+ /** 在不改动上游对象的前提下,给写工具的描述拼上引导语。 */
62
+ function descriptionFor(tool) {
63
+ const parts = [];
64
+ if (DESTRUCTIVE_TOOL_RE.test(tool.name))
65
+ parts.push(DESTRUCTIVE_HINT);
66
+ if (WRITE_TOOL_RE.test(tool.name))
67
+ parts.push(WRITE_PREFIX);
68
+ if (tool.name === 'write_item')
69
+ parts.push(WRITE_ITEM_HINT);
70
+ if (tool.name === 'add_by_identifier')
71
+ parts.push(IDENTIFIER_HINT);
72
+ const base = tool.description ?? '';
73
+ if (parts.length === 0)
74
+ return base;
75
+ return `${parts.join('\n\n')}\n\n${base}`;
76
+ }
35
77
  /** 由 MCP 工具造一条 dsh 工具定义(参数直接沿用上游 JSON Schema)。 */
36
78
  function definitionFor(tool) {
37
79
  return {
38
80
  name: publicName(tool.name),
39
- description: tool.description,
81
+ description: descriptionFor(tool),
40
82
  parameters: tool.parameters,
41
83
  output: {
42
84
  schema: {
@@ -450,8 +450,24 @@ export async function getItemFulltext(attachmentKey) {
450
450
  attachmentKey,
451
451
  content: truncated ? content.slice(0, maxBytes) : content,
452
452
  truncated,
453
+ fallback: false,
453
454
  };
454
455
  }
456
+ /**
457
+ * 依次尝试候选附件的全文,返回第一份可用的。
458
+ *
459
+ * 条目常同时挂原文与中文译本:译本若尚未建索引(`/fulltext` 404),
460
+ * 不能直接判定整条无全文,应回退到原文等其他附件。
461
+ */
462
+ async function getFirstFulltext(candidates) {
463
+ const preferredKey = candidates[0]?.key ?? '';
464
+ for (const attachment of candidates) {
465
+ const fulltext = await getItemFulltext(attachment.key);
466
+ if (fulltext !== null)
467
+ return { ...fulltext, fallback: attachment.key !== preferredKey };
468
+ }
469
+ return null;
470
+ }
455
471
  /** 单条条目的 CSL HTML(`format=bib`);失败返回 null。 */
456
472
  export async function getItemBib(key, style) {
457
473
  try {
@@ -478,11 +494,15 @@ export async function getItemDetail(key, style) {
478
494
  // 笔记是子条目(itemType=note);条目本身就是笔记时(独立笔记),正文即自身。
479
495
  const childNotes = children.filter((c) => c.data?.itemType === 'note');
480
496
  const notes = (itemType === 'note' ? [raw] : childNotes).map(toNote);
481
- const readable = isAttachment
482
- ? raw
483
- : childAttachments.find((c) => c.data?.contentType === 'application/pdf') ?? childAttachments[0];
497
+ // 候选顺序:PDF 优先,其余附件随后;逐份尝试直到拿到全文。
498
+ const candidates = isAttachment
499
+ ? [raw]
500
+ : [
501
+ ...childAttachments.filter((c) => c.data?.contentType === 'application/pdf'),
502
+ ...childAttachments.filter((c) => c.data?.contentType !== 'application/pdf'),
503
+ ];
484
504
  const [fulltext, bib] = await Promise.all([
485
- readable ? getItemFulltext(readable.key) : Promise.resolve(null),
505
+ getFirstFulltext(candidates),
486
506
  getItemBib(key, style),
487
507
  ]);
488
508
  // 展示顺序:当前附件的标注排前,同附件内按 PDF 位置(annotationSortIndex 零填充,可字典序比较)。
@@ -105,6 +105,8 @@ export interface ZoteroFulltext {
105
105
  attachmentKey: string;
106
106
  content: string;
107
107
  truncated: boolean;
108
+ /** 首选附件没有全文索引,正文取自同条目的其他附件。 */
109
+ fallback: boolean;
108
110
  }
109
111
  /** 探活结果。 */
110
112
  export interface ZoteroHealth {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-zotero",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Install this plugin and DSH can operate your Zotero for you: search the library, read PDF full text, summarize highlights and notes, organize collections by topic, import your downloaded papers, and attach a translated version back to an item. The right sidebar also gains a Zotero library panel that brings an item, an excerpt or a whole collection into the conversation with one click.",
5
5
  "keywords": [
6
6
  "dsh",
@@ -47,8 +47,8 @@
47
47
  },
48
48
  "devDependencies": {
49
49
  "@deepseek-ai/cordis": "^4.0.5",
50
- "@deepseek-ai/dsh": "0.2.1-rc.3",
51
- "@deepseek-ai/dsh-tools": "0.2.1-rc.3",
50
+ "@deepseek-ai/dsh": "0.2.1-rc.5",
51
+ "@deepseek-ai/dsh-tools": "0.2.1-rc.5",
52
52
  "@deepseek-ai/schemastery": "3.18.5",
53
53
  "@types/node": "^24.19.2",
54
54
  "@types/react": "~18.3.1",