@alexpeng/dsh-custom-plugin 0.4.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -1570,6 +1570,12 @@ window.__ModuleLoader__.load({
1570
1570
  * over `/api/custom-plugin` fetch routes.
1571
1571
  * @module @alexpeng/dsh-custom-plugin/client/custom
1572
1572
  */
1573
+ /**
1574
+ * dsh's shipped web profile configures the session index with `openAt: never`,
1575
+ * so cross-session full-text search can legitimately be unavailable. Say what
1576
+ * still works instead of leaving a bare error that reads as a broken palette.
1577
+ */
1578
+ const CROSS_SEARCH_UNAVAILABLE = "跨会话全文搜索不可用;下方结果仍按标题匹配会话与工作区。";
1573
1579
  function fmtClock(time) {
1574
1580
  const d = new Date(time);
1575
1581
  const pad = (n) => String(n).padStart(2, "0");
@@ -1829,6 +1835,57 @@ window.__ModuleLoader__.load({
1829
1835
  return null;
1830
1836
  }
1831
1837
  }
1838
+ /**
1839
+ * Follow the Session the conversation is showing. The 0.1.7 session list
1840
+ * snapshot dropped its `current` field — view selection moved to the
1841
+ * Workspace browser — so the plugin reads the identity off the standard
1842
+ * `sessionId` prop that every session-scoped slot entry receives.
1843
+ *
1844
+ * Several entries carry it at once (two header actions, the utilities entry,
1845
+ * the quote dock, and one tail entry per turn), and a turn-level entry can
1846
+ * unmount while the same Session stays open. Only the LAST mounted holder may
1847
+ * clear the identity, otherwise the rail would go blank under the surviving
1848
+ * entries, whose props never change and so never re-run their effect.
1849
+ *
1850
+ * A holder that reports no session id claims nothing at all: it must not
1851
+ * count toward the holders, because one entry flipping to null while the
1852
+ * others still carry the session would otherwise wipe the identity they are
1853
+ * still rendering — and their effects never re-run to restore it.
1854
+ */
1855
+ let sessionScopeHolders = 0;
1856
+ function useViewedSession(props) {
1857
+ const id = typeof props.sessionId === "string" && props.sessionId !== "" ? props.sessionId : null;
1858
+ react.useEffect(() => {
1859
+ if (id === null) return;
1860
+ sessionScopeHolders++;
1861
+ if (S.sessionId !== id) {
1862
+ S.anchors.clear();
1863
+ S.seqAnchor.clear();
1864
+ setS({
1865
+ sessionId: id,
1866
+ turns: null,
1867
+ railPositions: [],
1868
+ railSig: "",
1869
+ railHover: null
1870
+ });
1871
+ fetchTurns(id);
1872
+ }
1873
+ return () => {
1874
+ sessionScopeHolders--;
1875
+ if (sessionScopeHolders === 0 && S.sessionId === id) {
1876
+ S.anchors.clear();
1877
+ S.seqAnchor.clear();
1878
+ setS({
1879
+ sessionId: null,
1880
+ turns: null,
1881
+ railPositions: [],
1882
+ railSig: "",
1883
+ railHover: null
1884
+ });
1885
+ }
1886
+ };
1887
+ }, [id]);
1888
+ }
1832
1889
  const ICONS = {
1833
1890
  sliders: [
1834
1891
  ["line", {
@@ -2835,11 +2892,13 @@ window.__ModuleLoader__.load({
2835
2892
  let lastRefetchAt = 0;
2836
2893
  let turnsRequestSerial = 0;
2837
2894
  let railRefetchRows = -1;
2838
- let lastDiagAt = 0;
2895
+ const diagAtByKey = /* @__PURE__ */ new Map();
2839
2896
  function diagThrottled(message) {
2897
+ const key = message.slice(0, 24);
2840
2898
  const now = Date.now();
2841
- if (now - lastDiagAt < 5e3) return;
2842
- lastDiagAt = now;
2899
+ const at = diagAtByKey.get(key);
2900
+ if (at !== void 0 && now - at < 5e3) return;
2901
+ diagAtByKey.set(key, now);
2843
2902
  reportDiag(message);
2844
2903
  }
2845
2904
  function scheduleTurnsRefetch() {
@@ -3058,20 +3117,24 @@ window.__ModuleLoader__.load({
3058
3117
  updateRailPositions();
3059
3118
  }
3060
3119
  async function forkAt(seq) {
3061
- const sessions = C.get("sessions");
3062
- if (sessions === void 0) return;
3120
+ const remote = sessionRemote();
3121
+ if (remote === void 0) {
3122
+ toast("分支不可用:会话 Remote 服务缺失", "error");
3123
+ return;
3124
+ }
3063
3125
  try {
3064
- const childId = await sessions.fork({
3126
+ const result = await remote.fork({
3065
3127
  sessionId: S.sessionId,
3066
3128
  atSeq: seq
3067
3129
  });
3130
+ if (!result.ok) {
3131
+ toast("创建分支失败: " + remoteErrorMessage(result.error, "未知错误"), "error");
3132
+ return;
3133
+ }
3134
+ const childId = result.value.sessionId;
3068
3135
  toast("分支会话已创建", "info", {
3069
3136
  label: "打开分支",
3070
- run: () => {
3071
- try {
3072
- sessions.open(childId);
3073
- } catch {}
3074
- }
3137
+ run: () => openSessionItem(childId)
3075
3138
  });
3076
3139
  } catch (error) {
3077
3140
  toast("创建分支失败: " + String(error?.message ?? error), "error");
@@ -3494,29 +3557,56 @@ window.__ModuleLoader__.load({
3494
3557
  for (const child of node.children ?? []) n += folderCount(child);
3495
3558
  return n;
3496
3559
  }
3560
+ /**
3561
+ * The `ctx.remote` namespaces this plugin calls, read fail-soft so a profile
3562
+ * without the Session Remote degrades to a toast instead of throwing.
3563
+ */
3564
+ function sessionRemote() {
3565
+ const session = C.get("remote")?.session;
3566
+ if (session === void 0) return void 0;
3567
+ if (typeof session.fork !== "function" || typeof session.search !== "function") return void 0;
3568
+ return session;
3569
+ }
3570
+ /** Render a Remote failure envelope as one readable line. */
3571
+ function remoteErrorMessage(error, fallback) {
3572
+ if (typeof error === "string") return error;
3573
+ if (error !== null && typeof error === "object") {
3574
+ const joined = [String(error.code ?? ""), String(error.message ?? "")].filter((part) => part !== "").join(": ");
3575
+ if (joined !== "") return joined;
3576
+ }
3577
+ return fallback;
3578
+ }
3579
+ /**
3580
+ * Navigation moved to the Workspace browser's `uiWorkspace` service: the
3581
+ * Session Controller no longer opens or selects a Session for a caller.
3582
+ */
3583
+ function workspaceNav() {
3584
+ const nav = C.get("uiWorkspace");
3585
+ if (nav === void 0 || typeof nav.openSession !== "function" || typeof nav.connectWorkspace !== "function") return void 0;
3586
+ return nav;
3587
+ }
3497
3588
  async function openWorkspaceItem(id) {
3498
- const workspaces = C.get("workspaces");
3499
- const sessions = C.get("sessions");
3500
- if (workspaces === void 0 || sessions === void 0) {
3501
- toast("工作区服务不可用", "error");
3589
+ const nav = workspaceNav();
3590
+ if (nav === void 0) {
3591
+ toast("工作区导航服务不可用", "error");
3502
3592
  return;
3503
3593
  }
3504
3594
  try {
3505
- const sid = await workspaces.connectWorkspace(id);
3506
- sessions.open(sid);
3595
+ const sid = await nav.connectWorkspace(id);
3596
+ nav.openSession(sid);
3507
3597
  toast("已打开项目", "info");
3508
3598
  } catch (error) {
3509
3599
  toast("打开项目失败: " + String(error?.message ?? error), "error");
3510
3600
  }
3511
3601
  }
3512
3602
  function openSessionItem(id) {
3513
- const sessions = C.get("sessions");
3514
- if (sessions === void 0) {
3515
- toast("会话服务不可用", "error");
3603
+ const nav = workspaceNav();
3604
+ if (nav === void 0) {
3605
+ toast("会话导航服务不可用", "error");
3516
3606
  return;
3517
3607
  }
3518
3608
  try {
3519
- sessions.open(id);
3609
+ nav.openSession(id);
3520
3610
  } catch (error) {
3521
3611
  toast("打开会话失败: " + String(error?.message ?? error), "error");
3522
3612
  }
@@ -3531,7 +3621,11 @@ window.__ModuleLoader__.load({
3531
3621
  }
3532
3622
  }), props.hint !== void 0 ? react.createElement("span", { className: "vx-muted" }, props.hint) : null);
3533
3623
  }
3534
- function FolderSidebarButton() {
3624
+ /** `sidebar.footer.action` hands the column state down. In the 56px rail the
3625
+ * two-character label wrapped into a vertical stack beside the icon, so the
3626
+ * text renders only while the sidebar is wide; a host that passes no `wide`
3627
+ * keeps the label. */
3628
+ function FolderSidebarButton(props) {
3535
3629
  return react.createElement("button", {
3536
3630
  className: "vx-foot-btn",
3537
3631
  title: "项目文件夹",
@@ -3542,9 +3636,10 @@ window.__ModuleLoader__.load({
3542
3636
  }, react.createElement(Icon, {
3543
3637
  n: "folder",
3544
3638
  size: 15
3545
- }), react.createElement("span", { className: "vx-foot-label" }, "项目"));
3639
+ }), props.wide === false ? null : react.createElement("span", { className: "vx-foot-label" }, "项目"));
3546
3640
  }
3547
- function HeaderPanelButton() {
3641
+ function HeaderPanelButton(props) {
3642
+ useViewedSession(props);
3548
3643
  return react.createElement("button", {
3549
3644
  className: "vx-header-btn",
3550
3645
  title: "个性化中心",
@@ -3557,7 +3652,8 @@ window.__ModuleLoader__.load({
3557
3652
  size: 13
3558
3653
  }), react.createElement("span", null, "个性化"));
3559
3654
  }
3560
- function PromptQuickButton() {
3655
+ function PromptQuickButton(props) {
3656
+ useViewedSession(props);
3561
3657
  return react.createElement("button", {
3562
3658
  className: "vx-header-btn",
3563
3659
  title: "快速调用提示词",
@@ -3610,7 +3706,8 @@ window.__ModuleLoader__.load({
3610
3706
  onClick: () => insertPrompt(x)
3611
3707
  }, react.createElement("div", { className: "vx-prompt-title" }, x.name), react.createElement("div", { className: "vx-prompt-body" }, x.text))) : react.createElement("div", { className: "vx-muted vx-pad-sm" }, "暂无匹配提示词")));
3612
3708
  }
3613
- function HeaderBalance() {
3709
+ function HeaderBalance(props) {
3710
+ useViewedSession(props);
3614
3711
  const s = useS();
3615
3712
  const b = s.balance;
3616
3713
  const [hover, setHover] = react.useState(false);
@@ -3807,6 +3904,7 @@ window.__ModuleLoader__.load({
3807
3904
  }), " 扫描今日会话日志更新用量"));
3808
3905
  }
3809
3906
  function QuoteDock(props) {
3907
+ useViewedSession(props);
3810
3908
  const s = useS();
3811
3909
  const inputRef = react.useRef(props.input);
3812
3910
  inputRef.current = props.input;
@@ -3901,7 +3999,8 @@ window.__ModuleLoader__.load({
3901
3999
  }), react.createElement("span", null, "引用回复"));
3902
4000
  }
3903
4001
  function TurnTailEntry(props) {
3904
- const seq = props.matched !== void 0 && props.matched !== null ? props.matched : typeof props.seq === "number" ? props.seq : null;
4002
+ useViewedSession(props);
4003
+ const seq = typeof props.seq === "number" ? props.seq : null;
3905
4004
  const turn = props.turn;
3906
4005
  const s = useS();
3907
4006
  let registered = null;
@@ -4026,33 +4125,8 @@ window.__ModuleLoader__.load({
4026
4125
  function TimelineRail(props) {
4027
4126
  const s = useS();
4028
4127
  const sessions = useSessionsSafe(props);
4029
- const current = sessions !== null ? sessions.current ?? null : null;
4030
- const summary = current !== null ? sessions.byId[current] : null;
4128
+ const summary = sessions !== null && s.sessionId !== null ? sessions.byId[s.sessionId] : null;
4031
4129
  const trackRef = react.useRef(null);
4032
- react.useEffect(() => {
4033
- if (current !== null && current !== S.sessionId) {
4034
- S.anchors.clear();
4035
- S.seqAnchor.clear();
4036
- setS({
4037
- sessionId: current,
4038
- turns: null,
4039
- railPositions: [],
4040
- railSig: "",
4041
- railHover: null
4042
- });
4043
- fetchTurns(String(current));
4044
- } else if (current === null && S.sessionId !== null) {
4045
- S.anchors.clear();
4046
- S.seqAnchor.clear();
4047
- setS({
4048
- sessionId: null,
4049
- turns: null,
4050
- railPositions: [],
4051
- railSig: "",
4052
- railHover: null
4053
- });
4054
- }
4055
- }, [current]);
4056
4130
  react.useEffect(() => {
4057
4131
  if (s.cfg.timeline !== true || S.sessionId === null) return;
4058
4132
  const running = summary?.running === true;
@@ -4062,7 +4136,7 @@ window.__ModuleLoader__.load({
4062
4136
  return () => clearInterval(handle);
4063
4137
  }, [
4064
4138
  s.cfg.timeline,
4065
- current,
4139
+ s.sessionId,
4066
4140
  summary?.running
4067
4141
  ]);
4068
4142
  react.useEffect(() => {
@@ -4848,11 +4922,13 @@ window.__ModuleLoader__.load({
4848
4922
  const [items, setItems] = react.useState([]);
4849
4923
  const [status, setStatus] = react.useState("");
4850
4924
  const [hasMore, setHasMore] = react.useState(false);
4925
+ const [scanned, setScanned] = react.useState(false);
4851
4926
  react.useEffect(() => {
4852
4927
  if (query.trim() === "" || S.sessionId === null) {
4853
4928
  setItems([]);
4854
4929
  setStatus("");
4855
4930
  setHasMore(false);
4931
+ setScanned(false);
4856
4932
  return;
4857
4933
  }
4858
4934
  const controller = new AbortController();
@@ -4867,6 +4943,7 @@ window.__ModuleLoader__.load({
4867
4943
  }
4868
4944
  setItems(result.items);
4869
4945
  setHasMore(result.hasMore);
4946
+ setScanned(result.source === "scan");
4870
4947
  setStatus(result.items.length === 0 ? "没有匹配结果" : "");
4871
4948
  });
4872
4949
  }, 250);
@@ -4885,7 +4962,7 @@ window.__ModuleLoader__.load({
4885
4962
  assistant: "助手",
4886
4963
  tool: "工具"
4887
4964
  };
4888
- return react.createElement("div", { className: "vx-col" }, react.createElement("div", { className: "vx-muted" }, "搜索当前会话的索引内容;结果只在本次请求中使用。"), react.createElement("input", {
4965
+ return react.createElement("div", { className: "vx-col" }, react.createElement("div", { className: "vx-muted" }, "搜索当前会话的消息与工具记录;结果只在本次请求中使用。"), scanned ? react.createElement("div", { className: "vx-muted" }, "本部署未开启 dsh 的事件全文索引,已改为直接扫描会话日志(结果按出现顺序,最多 100 条)。") : null, react.createElement("input", {
4889
4966
  className: "vx-input",
4890
4967
  autoFocus: true,
4891
4968
  placeholder: "输入搜索词…",
@@ -5291,34 +5368,32 @@ window.__ModuleLoader__.load({
5291
5368
  }
5292
5369
  const controller = new AbortController();
5293
5370
  const timer = setTimeout(() => {
5294
- const sessions = C.get("sessions");
5295
- if (sessions === void 0 || typeof sessions.search !== "function") {
5296
- setRemoteStatus("跨会话搜索不可用");
5371
+ const sessionSearch = sessionRemote();
5372
+ if (sessionSearch === void 0) {
5373
+ setRemoteStatus(CROSS_SEARCH_UNAVAILABLE);
5297
5374
  return;
5298
5375
  }
5299
5376
  setRemoteStatus("跨会话搜索中…");
5300
5377
  try {
5301
- sessions.search(query.trim(), controller.signal).then((result) => {
5378
+ sessionSearch.search({ query: query.trim() }, controller.signal).then((result) => {
5302
5379
  if (controller.signal.aborted) return;
5303
5380
  if (result.ok) {
5304
5381
  setRemote(result.value.items);
5305
5382
  setRemoteStatus("");
5306
5383
  } else {
5307
- const error = result.error;
5308
- const message = typeof error === "string" ? error : error !== null && typeof error === "object" ? [String(error.code ?? ""), String(error.message ?? "")].filter((part) => part !== "").join(": ") || "跨会话搜索不可用" : "跨会话搜索不可用";
5309
5384
  setRemote([]);
5310
- setRemoteStatus(message);
5385
+ setRemoteStatus(remoteErrorMessage(result.error, CROSS_SEARCH_UNAVAILABLE));
5311
5386
  }
5312
5387
  }).catch(() => {
5313
5388
  if (!controller.signal.aborted) {
5314
5389
  setRemote([]);
5315
- setRemoteStatus("跨会话搜索不可用");
5390
+ setRemoteStatus(CROSS_SEARCH_UNAVAILABLE);
5316
5391
  }
5317
5392
  });
5318
5393
  } catch {
5319
5394
  if (!controller.signal.aborted) {
5320
5395
  setRemote([]);
5321
- setRemoteStatus("跨会话搜索不可用");
5396
+ setRemoteStatus(CROSS_SEARCH_UNAVAILABLE);
5322
5397
  }
5323
5398
  }
5324
5399
  }, 250);
@@ -5981,23 +6056,30 @@ window.__ModuleLoader__.load({
5981
6056
  return () => {};
5982
6057
  }
5983
6058
  const unregisterAll = [];
6059
+ const injectedKeys = /* @__PURE__ */ new Set();
6060
+ let injectedCount = 0;
5984
6061
  const injectOne = (key, id, options, comp) => {
5985
6062
  try {
5986
6063
  slots.inject(key, () => {
5987
- const unregister = slots.register({
5988
- name: key,
5989
- ...options
5990
- }, comp);
5991
- unregisterAll.push(unregister);
5992
- return unregister;
6064
+ try {
6065
+ const unregister = slots.register({
6066
+ name: key,
6067
+ ...options
6068
+ }, comp);
6069
+ unregisterAll.push(unregister);
6070
+ return unregister;
6071
+ } catch (error) {
6072
+ reportDiag("register " + key + " (" + id + "): " + String(error?.message ?? error));
6073
+ return () => {};
6074
+ }
5993
6075
  });
5994
6076
  reportDiag("ok " + key + " (" + id + ")");
5995
- return true;
6077
+ injectedCount++;
6078
+ injectedKeys.add(key);
5996
6079
  } catch (error) {
5997
6080
  const message = "inject " + key + " (" + id + "): " + String(error?.message ?? error);
5998
6081
  console.error("[custom-plugin] " + message);
5999
6082
  reportDiag(message);
6000
- return false;
6001
6083
  }
6002
6084
  };
6003
6085
  injectOne("sidebar.footer.action", "custom-plugin-folders", {
@@ -6015,7 +6097,10 @@ window.__ModuleLoader__.load({
6015
6097
  order: 30,
6016
6098
  label: "引用回复"
6017
6099
  }, QuoteDock);
6018
- injectOne("conversation.chat.turnTail", "custom-plugin-turn-tail", { select: (o) => o !== null && typeof o.seq === "number" ? o.seq : null }, TurnTailEntry);
6100
+ injectOne("conversation.chat.turnTail", "custom-plugin-turn-tail", {
6101
+ id: "custom-plugin-turn-tail",
6102
+ order: 10
6103
+ }, TurnTailEntry);
6019
6104
  injectOne("settings.section", "custom-plugin-appearance", {
6020
6105
  id: "custom-plugin-appearance",
6021
6106
  order: 30,
@@ -6036,7 +6121,7 @@ window.__ModuleLoader__.load({
6036
6121
  order: -5,
6037
6122
  label: "额度"
6038
6123
  }, HeaderBalance);
6039
- reportDiag("client registered: 8 injections / 7 slots");
6124
+ reportDiag(`client installed: ${injectedCount} injection requests / ${injectedKeys.size} slots`);
6040
6125
  return () => {
6041
6126
  for (const unregister of unregisterAll.splice(0)) try {
6042
6127
  unregister();