dsh-mega-chat-nav 0.1.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.
Files changed (192) hide show
  1. package/LICENSE +201 -0
  2. package/README.en.md +149 -0
  3. package/README.md +149 -0
  4. package/cordis.patch.yml +4 -0
  5. package/lib/client/NavOverlay.js +35 -0
  6. package/lib/client/NavRail.js +12 -0
  7. package/lib/client/QuestionNavSettingsTab.js +26 -0
  8. package/lib/client/QuestionNavStrip.js +555 -0
  9. package/lib/client/components/ConfigPanel.js +14 -0
  10. package/lib/client/components/DotRail.js +62 -0
  11. package/lib/client/components/NavOverlay.js +121 -0
  12. package/lib/client/components/NavRail.js +97 -0
  13. package/lib/client/components/RailDispatch.js +22 -0
  14. package/lib/client/components/RailHost.js +79 -0
  15. package/lib/client/components/SearchBox.js +66 -0
  16. package/lib/client/components/SettingsPage.js +46 -0
  17. package/lib/client/components/StandaloneSettings.js +16 -0
  18. package/lib/client/components/codex/CodexRail.js +139 -0
  19. package/lib/client/components/deepseek/DeepseekRail.js +264 -0
  20. package/lib/client/components/minimal/MinimalRail.js +135 -0
  21. package/lib/client/components/minimal/cards.js +32 -0
  22. package/lib/client/components/minimal/dot.js +11 -0
  23. package/lib/client/components/minimal/hint.js +30 -0
  24. package/lib/client/components/minimal/paging.js +5 -0
  25. package/lib/client/components/minimal/quick-settings.js +28 -0
  26. package/lib/client/components/minimal/search.js +67 -0
  27. package/lib/client/components/minimal/useRail.js +716 -0
  28. package/lib/client/components/rail/cards.js +32 -0
  29. package/lib/client/components/rail/hint.js +30 -0
  30. package/lib/client/components/rail/paging.js +5 -0
  31. package/lib/client/components/rail/quick-settings.js +28 -0
  32. package/lib/client/components/rail/search.js +76 -0
  33. package/lib/client/components/rail/useRail.js +845 -0
  34. package/lib/client/components/settings/SettingsPage.js +46 -0
  35. package/lib/client/components/settings/SettingsTab.js +17 -0
  36. package/lib/client/components/settings/StandaloneSettings.js +16 -0
  37. package/lib/client/components/shared/Cascade.js +26 -0
  38. package/lib/client/components/shared/ConfigPanel.js +27 -0
  39. package/lib/client/components/shared/Dot.js +8 -0
  40. package/lib/client/components/shared/Hint.js +5 -0
  41. package/lib/client/components/shared/PagingButton.js +5 -0
  42. package/lib/client/components/shared/RailShell.js +78 -0
  43. package/lib/client/components/shared/SearchBox.js +63 -0
  44. package/lib/client/components/shared/hooks.js +427 -0
  45. package/lib/client/components/shared/types.js +1 -0
  46. package/lib/client/config.js +1 -0
  47. package/lib/client/engines/jump.js +34 -0
  48. package/lib/client/engines/useAtBottom.js +25 -0
  49. package/lib/client/engines/useJump.js +40 -0
  50. package/lib/client/engines/useScrollSpy.js +88 -0
  51. package/lib/client/hooks.js +11 -0
  52. package/lib/client/index.js +312 -0
  53. package/lib/client/jump.js +255 -0
  54. package/lib/client/locales.js +142 -0
  55. package/lib/client/pure.js +61 -0
  56. package/lib/client/settings.js +369 -0
  57. package/lib/client/virtual/VirtualList.js +21 -0
  58. package/lib/client.js +3213 -0
  59. package/lib/compat.js +145 -0
  60. package/lib/config.js +37 -0
  61. package/lib/core/active-dot.js +17 -0
  62. package/lib/core/align.js +9 -0
  63. package/lib/core/config.js +37 -0
  64. package/lib/core/focus.js +38 -0
  65. package/lib/core/jump.js +98 -0
  66. package/lib/core/logic/active-dot.js +16 -0
  67. package/lib/core/logic/focus.js +34 -0
  68. package/lib/core/logic/jump.js +188 -0
  69. package/lib/core/logic/nodes.js +59 -0
  70. package/lib/core/model/nav-item.js +1 -0
  71. package/lib/core/model/turns.js +73 -0
  72. package/lib/core/nav-item.js +1 -0
  73. package/lib/core/nodes.js +57 -0
  74. package/lib/core/page-size.js +7 -0
  75. package/lib/core/question-entry.js +1 -0
  76. package/lib/core/time.js +23 -0
  77. package/lib/core/turn-dots.js +85 -0
  78. package/lib/core/utils/measure.js +24 -0
  79. package/lib/core/utils/time.js +26 -0
  80. package/lib/index.js +64 -0
  81. package/lib/nav-item.js +1 -0
  82. package/lib/nodes.js +59 -0
  83. package/lib/projection.js +126 -0
  84. package/lib/schema.js +26 -0
  85. package/lib/search.js +285 -0
  86. package/lib/settings.js +89 -0
  87. package/lib/types/client/NavOverlay.d.ts +18 -0
  88. package/lib/types/client/NavRail.d.ts +7 -0
  89. package/lib/types/client/QuestionNavSettingsTab.d.ts +16 -0
  90. package/lib/types/client/QuestionNavStrip.d.ts +48 -0
  91. package/lib/types/client/components/ConfigPanel.d.ts +14 -0
  92. package/lib/types/client/components/DotRail.d.ts +15 -0
  93. package/lib/types/client/components/NavOverlay.d.ts +30 -0
  94. package/lib/types/client/components/NavRail.d.ts +16 -0
  95. package/lib/types/client/components/RailDispatch.d.ts +8 -0
  96. package/lib/types/client/components/RailHost.d.ts +27 -0
  97. package/lib/types/client/components/SearchBox.d.ts +9 -0
  98. package/lib/types/client/components/SettingsPage.d.ts +9 -0
  99. package/lib/types/client/components/StandaloneSettings.d.ts +13 -0
  100. package/lib/types/client/components/codex/CodexRail.d.ts +3 -0
  101. package/lib/types/client/components/deepseek/DeepseekRail.d.ts +3 -0
  102. package/lib/types/client/components/minimal/MinimalRail.d.ts +8 -0
  103. package/lib/types/client/components/minimal/cards.d.ts +19 -0
  104. package/lib/types/client/components/minimal/dot.d.ts +14 -0
  105. package/lib/types/client/components/minimal/hint.d.ts +11 -0
  106. package/lib/types/client/components/minimal/paging.d.ts +10 -0
  107. package/lib/types/client/components/minimal/quick-settings.d.ts +8 -0
  108. package/lib/types/client/components/minimal/search.d.ts +27 -0
  109. package/lib/types/client/components/minimal/useRail.d.ts +90 -0
  110. package/lib/types/client/components/rail/cards.d.ts +19 -0
  111. package/lib/types/client/components/rail/hint.d.ts +11 -0
  112. package/lib/types/client/components/rail/paging.d.ts +10 -0
  113. package/lib/types/client/components/rail/quick-settings.d.ts +8 -0
  114. package/lib/types/client/components/rail/search.d.ts +34 -0
  115. package/lib/types/client/components/rail/useRail.d.ts +132 -0
  116. package/lib/types/client/components/settings/SettingsPage.d.ts +9 -0
  117. package/lib/types/client/components/settings/SettingsTab.d.ts +6 -0
  118. package/lib/types/client/components/settings/StandaloneSettings.d.ts +13 -0
  119. package/lib/types/client/components/shared/Cascade.d.ts +14 -0
  120. package/lib/types/client/components/shared/ConfigPanel.d.ts +8 -0
  121. package/lib/types/client/components/shared/Dot.d.ts +17 -0
  122. package/lib/types/client/components/shared/Hint.d.ts +6 -0
  123. package/lib/types/client/components/shared/PagingButton.d.ts +10 -0
  124. package/lib/types/client/components/shared/RailShell.d.ts +43 -0
  125. package/lib/types/client/components/shared/SearchBox.d.ts +18 -0
  126. package/lib/types/client/components/shared/hooks.d.ts +64 -0
  127. package/lib/types/client/components/shared/types.d.ts +101 -0
  128. package/lib/types/client/config.d.ts +9 -0
  129. package/lib/types/client/engines/jump.d.ts +17 -0
  130. package/lib/types/client/engines/useAtBottom.d.ts +1 -0
  131. package/lib/types/client/engines/useJump.d.ts +16 -0
  132. package/lib/types/client/engines/useScrollSpy.d.ts +12 -0
  133. package/lib/types/client/hooks.d.ts +7 -0
  134. package/lib/types/client/index.d.ts +31 -0
  135. package/lib/types/client/jump.d.ts +47 -0
  136. package/lib/types/client/locales.d.ts +142 -0
  137. package/lib/types/client/pure.d.ts +21 -0
  138. package/lib/types/client/settings.d.ts +173 -0
  139. package/lib/types/client/virtual/VirtualList.d.ts +15 -0
  140. package/lib/types/compat.d.ts +44 -0
  141. package/lib/types/config.d.ts +44 -0
  142. package/lib/types/core/active-dot.d.ts +15 -0
  143. package/lib/types/core/align.d.ts +10 -0
  144. package/lib/types/core/config.d.ts +44 -0
  145. package/lib/types/core/focus.d.ts +24 -0
  146. package/lib/types/core/jump.d.ts +39 -0
  147. package/lib/types/core/logic/active-dot.d.ts +15 -0
  148. package/lib/types/core/logic/focus.d.ts +22 -0
  149. package/lib/types/core/logic/jump.d.ts +39 -0
  150. package/lib/types/core/logic/nodes.d.ts +48 -0
  151. package/lib/types/core/model/nav-item.d.ts +12 -0
  152. package/lib/types/core/model/turns.d.ts +30 -0
  153. package/lib/types/core/nav-item.d.ts +12 -0
  154. package/lib/types/core/nodes.d.ts +43 -0
  155. package/lib/types/core/page-size.d.ts +8 -0
  156. package/lib/types/core/question-entry.d.ts +12 -0
  157. package/lib/types/core/time.d.ts +4 -0
  158. package/lib/types/core/turn-dots.d.ts +29 -0
  159. package/lib/types/core/utils/measure.d.ts +12 -0
  160. package/lib/types/core/utils/time.d.ts +4 -0
  161. package/lib/types/index.d.ts +4 -0
  162. package/lib/types/nav-item.d.ts +12 -0
  163. package/lib/types/nodes.d.ts +48 -0
  164. package/lib/types/projection.d.ts +71 -0
  165. package/lib/types/schema.d.ts +37 -0
  166. package/lib/types/search.d.ts +42 -0
  167. package/lib/types/settings.d.ts +99 -0
  168. package/package.json +71 -0
  169. package/src/client/components/RailDispatch.tsx +27 -0
  170. package/src/client/components/codex/CodexRail.tsx +348 -0
  171. package/src/client/components/deepseek/DeepseekRail.tsx +522 -0
  172. package/src/client/components/minimal/MinimalRail.tsx +324 -0
  173. package/src/client/components/minimal/dot.tsx +32 -0
  174. package/src/client/components/rail/cards.tsx +102 -0
  175. package/src/client/components/rail/hint.tsx +50 -0
  176. package/src/client/components/rail/paging.tsx +26 -0
  177. package/src/client/components/rail/quick-settings.tsx +130 -0
  178. package/src/client/components/rail/search.tsx +147 -0
  179. package/src/client/components/rail/useRail.ts +906 -0
  180. package/src/client/components/settings/SettingsPage.tsx +198 -0
  181. package/src/client/components/settings/StandaloneSettings.tsx +28 -0
  182. package/src/client/components/shared/types.ts +99 -0
  183. package/src/client/index.ts +388 -0
  184. package/src/client/jump.ts +293 -0
  185. package/src/client/locales.ts +143 -0
  186. package/src/client/settings.ts +482 -0
  187. package/src/client/styles.css +1580 -0
  188. package/src/compat.ts +161 -0
  189. package/src/index.ts +66 -0
  190. package/src/projection.ts +174 -0
  191. package/src/search.ts +291 -0
  192. package/src/settings.ts +169 -0
@@ -0,0 +1,32 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { createPortal } from 'react-dom';
3
+ import { cardMetrics, metricsLine, clockText } from "./useRail.js";
4
+ function favoriteButton(favorited, onClick) {
5
+ return (_jsx("button", { type: "button", className: favorited ? 'mgcn-cardFav mgcn-cardFavOn' : 'mgcn-cardFav', "aria-label": favorited ? 'unfavorite' : 'favorite', onClick: (e) => { e.stopPropagation(); onClick(); }, children: _jsx("svg", { viewBox: "0 0 16 16", width: "11", height: "11", "aria-hidden": "true", children: _jsx("path", { d: "M8 2.2l1.76 3.56 3.93.57-2.84 2.77.67 3.91L8 11.4l-3.52 1.85.67-3.91-2.84-2.77 3.93-.57z", fill: favorited ? 'currentColor' : 'none', stroke: "currentColor", strokeWidth: "1.3", strokeLinejoin: "round" }) }) }));
6
+ }
7
+ export function Cards(props) {
8
+ const { focus, align, cardItems, favorites, onToggleFavorite, onJump, onEnter, onLeave } = props;
9
+ const showTurn = cardItems.includes('turn');
10
+ const showDuration = cardItems.includes('duration');
11
+ const showFavorite = cardItems.includes('favorite');
12
+ return createPortal(_jsx("div", { className: align === 'right' ? 'mgcn-cascade mgcn-cascadeRight' : 'mgcn-cascade', style: {
13
+ ...(focus.left !== undefined ? { left: focus.left } : {}),
14
+ ...(focus.right !== undefined ? { right: focus.right } : {}),
15
+ top: focus.centerY,
16
+ transform: 'translateY(-50%)',
17
+ }, onMouseEnter: onEnter, onMouseLeave: onLeave, children: focus.items.map((item) => {
18
+ const metrics = cardMetrics(item.distance);
19
+ if (metrics === null)
20
+ return null;
21
+ const isSelected = item.distance === 0;
22
+ const dot = item.dot;
23
+ const text = dot.texts.join(' · ');
24
+ const cls = isSelected
25
+ ? 'mgcn-card mgcn-cardSelected' + (align === 'right' ? ' mgcn-cardSelectedRight' : '')
26
+ : 'mgcn-card';
27
+ const favorited = favorites.has(dot.key);
28
+ return (_jsxs("div", { className: cls, role: "button", tabIndex: -1, style: { width: metrics.widthPx }, onClick: () => onJump(dot), children: [_jsxs("div", { className: "mgcn-cardHead", children: [showFavorite ? favoriteButton(favorited, () => onToggleFavorite(dot.key)) : null, showTurn && isSelected && dot.turn !== null ? (_jsxs("div", { className: "mgcn-cardTitle", children: ["Turn ", dot.turn] })) : null, _jsx("div", { className: "mgcn-cardTime", children: clockText(dot.time, Date.now()) })] }), showDuration && dot.metrics !== undefined && isSelected ? (_jsx("div", { className: "mgcn-cardMetrics", children: metricsLine(dot.metrics) })) : null, _jsx("div", { className: isSelected ? 'mgcn-cardBody' : 'mgcn-cardClamp', style: isSelected
29
+ ? undefined
30
+ : { WebkitLineClamp: metrics.maxLines, fontSize: metrics.fontSize }, children: text })] }, dot.key));
31
+ }) }), document.body);
32
+ }
@@ -0,0 +1,30 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // 跳转失败 / 加载提示(portal 到 body):
3
+ // - 失败提示:贴 rail 侧顶部(rail 左右对齐)
4
+ // - 加载提示:对话输入框上方细条,水平对齐相对**对话区域**左右边缘(跟随 rail 侧)
5
+ import { useEffect, useState } from 'react';
6
+ import { createPortal } from 'react-dom';
7
+ /** 对话区域左/右边缘到视口的距离(fixed 定位用) */
8
+ function conversationEdge(align) {
9
+ const root = document.querySelector('[data-slot="conversation"] > div[data-phase]');
10
+ if (root === null)
11
+ return null;
12
+ const r = root.getBoundingClientRect();
13
+ return align === 'right' ? window.innerWidth - r.right : r.left;
14
+ }
15
+ export function Hint({ text, align, position = 'rail', loading = false }) {
16
+ const [edge, setEdge] = useState(() => conversationEdge(align));
17
+ useEffect(() => {
18
+ const measure = () => setEdge(conversationEdge(align));
19
+ measure();
20
+ window.addEventListener('resize', measure);
21
+ return () => window.removeEventListener('resize', measure);
22
+ }, [align]);
23
+ const cls = position === 'bottom' ? 'mgcn-hint mgcn-hint--bottom' : 'mgcn-hint';
24
+ const style = position === 'bottom'
25
+ ? align === 'right'
26
+ ? { right: (edge ?? 0) + 20, bottom: 150 }
27
+ : { left: (edge ?? 0) + 20, bottom: 150 }
28
+ : align === 'right' ? { right: 52, top: 12 } : { left: 52, top: 12 };
29
+ return createPortal(_jsxs("div", { className: cls, style: style, children: [loading ? (_jsxs("span", { className: "mgcn-hint-dots", "aria-hidden": "true", children: [_jsx("i", {}), _jsx("i", {}), _jsx("i", {})] })) : null, _jsx("span", { children: text })] }), document.body);
30
+ }
@@ -0,0 +1,5 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export function PagingButton(props) {
3
+ const cls = props.dir === -1 ? 'mgcn-navBtn mgcn-navUp' : 'mgcn-navBtn mgcn-navDown';
4
+ return (_jsx("button", { type: "button", className: cls, "aria-label": props.label, style: { visibility: props.visible ? 'visible' : 'hidden' }, onMouseEnter: props.onEnter, onMouseLeave: props.onLeave, onClick: props.onClick }));
5
+ }
@@ -0,0 +1,28 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // 快捷设置面板:对齐/条带高度/卡片数(能力感知)+ 显示模式
3
+ import { useEffect, useState } from 'react';
4
+ import { ALIGN_OPTIONS, BAND_OPTIONS, CARD_OPTIONS, MARK_TONE_OPTIONS, SHOW_OPTIONS, STYLE_CAPABILITIES } from "../../settings.js";
5
+ function Seg(props) {
6
+ return (_jsx("div", { className: "mgcn-segmented", role: "radiogroup", children: props.options.map((o) => (_jsx("button", { type: "button", role: "radio", "aria-checked": props.value === o, className: props.value === o ? 'mgcn-segment mgcn-segmentActive' : 'mgcn-segment', onClick: () => props.onPick(o), children: props.labels[String(o)] ?? String(o) }, o))) }));
7
+ }
8
+ function ModeSeg({ value, onChange, t }) {
9
+ return (_jsx(Seg, { options: SHOW_OPTIONS, value: value, labels: {
10
+ show: t('config.showMode.show'),
11
+ peek: t('config.showMode.peek'),
12
+ hide: t('config.showMode.hide'),
13
+ }, onPick: (v) => onChange(v) }));
14
+ }
15
+ function useSettingsTick(subscribe) {
16
+ const [, bump] = useState(0);
17
+ useEffect(() => subscribe(() => bump((n) => n + 1)), [subscribe]);
18
+ }
19
+ export function QuickSettings(props) {
20
+ const { injected, t, onClose } = props;
21
+ useSettingsTick(injected.subscribeSettings);
22
+ const caps = STYLE_CAPABILITIES[injected.style()];
23
+ return (_jsxs("div", { className: "mgcn-config-panel", role: "dialog", "aria-label": t('config.title'), children: [_jsxs("div", { className: "mgcn-config-head", children: [_jsxs("span", { className: "mgcn-config-title", children: [t('config.style.' + injected.style()), " \u00B7 ", t('config.settings')] }), _jsx("button", { type: "button", className: "mgcn-config-close", "aria-label": t('config.close'), onClick: onClose, children: "\u2715" })] }), _jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.align') }), _jsx(Seg, { options: ALIGN_OPTIONS, value: injected.align(), labels: { left: t('settings.align.left'), right: t('settings.align.right') }, onPick: (v) => injected.setAlign(v) })] }), _jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.bandHeight') }), _jsx(Seg, { options: BAND_OPTIONS, value: injected.bandHeight(), labels: {
24
+ compact: t('config.bandHeight.compact'),
25
+ standard: t('config.bandHeight.standard'),
26
+ tall: t('config.bandHeight.tall'),
27
+ }, onPick: (v) => injected.setBandHeight(v) })] }), caps.hoverCards ? (_jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.cardCount') }), _jsx(Seg, { options: CARD_OPTIONS, value: injected.cardCount(), labels: { 1: '1', 3: '3', 5: '5' }, onPick: (v) => injected.setCardCount(v) })] })) : null, injected.style() === 'codex' ? (_jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.markTone') }), _jsx(Seg, { options: MARK_TONE_OPTIONS, value: injected.markTone(), labels: { soft: t('config.markTone.soft'), deep: t('config.markTone.deep') }, onPick: (v) => injected.setMarkTone(v) })] })) : null, _jsx("div", { className: "mgcn-config-sep" }), _jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.showQuickSettings') }), _jsx(ModeSeg, { value: injected.showQuickSettings(), onChange: (v) => injected.setShowQuickSettings(v), t: t })] }), _jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.showSearch') }), _jsx(ModeSeg, { value: injected.showSearch(), onChange: (v) => injected.setShowSearch(v), t: t })] }), _jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.showFavorites') }), _jsx(ModeSeg, { value: injected.showFavorites(), onChange: (v) => injected.setShowFavorites(v), t: t })] }), _jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.showCount') }), _jsx(ModeSeg, { value: injected.showCount(), onChange: (v) => injected.setShowCount(v), t: t })] }), STYLE_CAPABILITIES[injected.style()].paging ? (_jsxs("div", { className: "mgcn-config-row", children: [_jsx("span", { className: "mgcn-config-label", children: t('config.showPaging') }), _jsx(ModeSeg, { value: injected.showPaging(), onChange: (v) => injected.setShowPaging(v), t: t })] })) : null] }));
28
+ }
@@ -0,0 +1,76 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ // 搜索浮层:输入防抖 → host 检索 → 结果(tag 用户/AI + 轮次标题 + 两行预览 + 历史提示)
3
+ import { useCallback, useEffect, useRef, useState } from 'react';
4
+ import { clockText } from "./useRail.js";
5
+ /** 检索:携带 AbortController 的 host 请求(scopes 过滤由 host 执行) */
6
+ /**
7
+ * 检索 + 统一排序:host 按日志顺序返回,此处统一按轮次倒序(最新轮在上;
8
+ * 同轮按 seq 倒序——最新消息在前)。minimal/codex 浮层、deepseek 面板与移动抽屉共用,
9
+ * 保证三种风格与搜索抽屉的命中顺序一致。
10
+ */
11
+ export function fetchHits(sessionId, query, scopes, signal) {
12
+ const url = '/mega-chat-nav/search?session=' + encodeURIComponent(sessionId ?? '')
13
+ + '&q=' + encodeURIComponent(query)
14
+ + '&scopes=' + encodeURIComponent(scopes.join(',')); // 不传 limit → host 返回全部命中
15
+ return fetch(url, { signal })
16
+ .then((resp) => resp.json())
17
+ .then((data) => {
18
+ const hits = data.ok === true ? (data.hits ?? []) : [];
19
+ return hits.sort((x, y) => (y.turn - x.turn) || (y.seq - x.seq));
20
+ });
21
+ }
22
+ /** 角色 → 标签文本(用户/助手/工具;steering 归用户侧) */
23
+ function roleTag(role, t) {
24
+ if (role === 'assistant')
25
+ return t('search.tag.assistant');
26
+ if (role === 'tool')
27
+ return t('search.tag.tool');
28
+ return t('search.tag.user');
29
+ }
30
+ export function SearchBox(props) {
31
+ const [query, setQuery] = useState('');
32
+ const [results, setResults] = useState([]);
33
+ const [busy, setBusy] = useState(false);
34
+ const abortRef = useRef(null);
35
+ const debounceRef = useRef(null);
36
+ const inputRef = useRef(null);
37
+ const run = useCallback((raw) => {
38
+ abortRef.current?.abort();
39
+ const ac = new AbortController();
40
+ abortRef.current = ac;
41
+ const q = raw.trim();
42
+ if (q.length < 1) {
43
+ setResults([]);
44
+ setBusy(false);
45
+ return;
46
+ }
47
+ setBusy(true);
48
+ fetchHits(props.sessionId, q, props.scopes, ac.signal)
49
+ .then((hits) => {
50
+ setResults(hits); // fetchHits 已按轮次倒序返回
51
+ })
52
+ .catch((e) => {
53
+ if (e.name !== 'AbortError')
54
+ setResults([]);
55
+ })
56
+ .finally(() => setBusy(false));
57
+ }, [props.sessionId, props.scopes]);
58
+ const onType = (value) => {
59
+ setQuery(value);
60
+ if (debounceRef.current !== null)
61
+ window.clearTimeout(debounceRef.current);
62
+ // 250ms 防抖:连续输入只触发一次检索
63
+ debounceRef.current = window.setTimeout(() => run(value), 250);
64
+ };
65
+ useEffect(() => {
66
+ inputRef.current?.focus();
67
+ return () => {
68
+ if (debounceRef.current !== null)
69
+ window.clearTimeout(debounceRef.current);
70
+ abortRef.current?.abort();
71
+ };
72
+ }, []);
73
+ const historicCount = results.filter((hit) => hit.historic).length;
74
+ return (_jsxs("div", { className: "mgcn-search", role: "dialog", "aria-label": props.t('search.placeholder'), children: [_jsx("input", { ref: inputRef, className: "mgcn-search-input", value: query, placeholder: props.t('search.placeholder'), onChange: (e) => onType(e.target.value), onKeyDown: (e) => { if (e.key === 'Escape')
75
+ props.onClose(); } }), busy ? _jsx("div", { className: "mgcn-search-busy", children: "\u2026" }) : null, !busy && query.trim() === '' && props.emptyContent !== undefined ? props.emptyContent : null, !busy && results.length > 0 && historicCount > 0 ? (_jsx("div", { className: "mgcn-search-historic", children: props.t('search.historic', { n: historicCount }) })) : null, results.length > 0 ? (_jsx("div", { className: "mgcn-search-results", children: results.map((hit) => (_jsxs("button", { type: "button", className: "mgcn-search-hit", onClick: () => props.onJump(hit.key, hit.seq), children: [_jsxs("span", { className: "mgcn-search-title", children: [_jsxs("span", { className: "mgcn-search-titleLeft", children: [hit.turn > 0 ? _jsx("span", { className: "mgcn-search-turn", children: props.t('search.turnTitle', { turn: hit.turn }) }) : null, _jsx("span", { className: 'mgcn-search-tag mgcn-search-tag--' + hit.role, children: roleTag(hit.role, props.t) })] }), _jsx("span", { className: "mgcn-search-titleTime", children: clockText(hit.time, Date.now()) })] }), _jsx("span", { className: "mgcn-search-body", children: _jsx("span", { className: "mgcn-search-text", children: hit.hitStart >= 0 && hit.hitLen > 0 ? (_jsxs(_Fragment, { children: [hit.snippet.slice(0, hit.hitStart), _jsx("mark", { className: "mgcn-search-mark", children: hit.snippet.slice(hit.hitStart, hit.hitStart + hit.hitLen) }), hit.snippet.slice(hit.hitStart + hit.hitLen)] })) : hit.snippet }) })] }, hit.key + ':' + hit.seq))) })) : null] }));
76
+ }