dsh-ustc-quota 1.1.20 → 1.1.22

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 (4) hide show
  1. package/README.md +5 -0
  2. package/client.js +226 -15
  3. package/index.js +1 -1
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -105,6 +105,11 @@ key-c 8.7/30 29% 8.7/70 12% 11.1/100 11%
105
105
  - 单把 Key 失败只影响该行;有 5 分钟内旧数据时保留 dimmed 数据并标记错误;
106
106
  - TokenWorks 的官方服务返回错误时,保留上一次有效窗口并显示官方错误文案。
107
107
 
108
+ ## 面板位置
109
+
110
+ - 卡片默认在左侧边栏底部。**鼠标移到卡片上时,标题左边的品牌图标会变成方向图标**(`→` 移到右侧、`←` 移回左侧),点一下即可换边;移开鼠标图标复原。选择会记住,重启后保持。
111
+ - 右侧位置停在**对话列里、聊天内容与轮次导航条之间的空白**:左右各留 24px,宽度随空白自适应,不遮挡右侧原生按钮,也不侵入轮次导航条。拖动对话边界或开关任一侧边栏时,卡片跟着走。
112
+ - 换边不重新加载:两处卡片共用同一份已取到的数据,切换时直接显示,之后在后台刷新。
108
113
  ## 更新
109
114
 
110
115
  - 标题「USTC 词元额度」右侧显示灰色小字版本号;点击版本号可手动检查更新;
package/client.js CHANGED
@@ -11,11 +11,12 @@ window.__ModuleLoader__.load({
11
11
  const UPDATE_ENDPOINT = 'plugins/dsh-ustc-quota/update';
12
12
  const UPDATE_APPLY_ENDPOINT = 'plugins/dsh-ustc-quota/update/apply';
13
13
  const WINDOW_IDS = ['3h', '12h', '24h'];
14
- const CLIENT_VERSION = '1.1.20';
14
+ const CLIENT_VERSION = '1.1.22';
15
15
  const UPDATE_INTERVAL_MS = 24 * 60 * 60 * 1000;
16
16
  const CLIENT_VERSION_KEY = `${NS}:client-version`;
17
17
  const CLIENT_RELOAD_KEY = `${NS}:client-reload`;
18
18
  const HOST_VERSION_KEY = `${NS}:host-version-reload`;
19
+ const POSITION_KEY = `${NS}:position`;
19
20
 
20
21
  function readDshVersion() {
21
22
  try {
@@ -203,6 +204,8 @@ window.__ModuleLoader__.load({
203
204
  title: 'USTC 词元额度',
204
205
  updatedLabel: '更新于',
205
206
  refresh: '刷新',
207
+ moveToRight: '移到右侧',
208
+ moveToLeft: '移到左侧',
206
209
  loading: '加载中…',
207
210
  noProviders: '未发现 USTC 模型厂商',
208
211
  noProvidersHint: '请检查模型设置中 baseURL 为 api.llm.ustc.edu.cn',
@@ -274,6 +277,8 @@ window.__ModuleLoader__.load({
274
277
  title: 'USTC token quota',
275
278
  updatedLabel: 'Updated',
276
279
  refresh: 'Refresh',
280
+ moveToRight: 'Move to the right',
281
+ moveToLeft: 'Move to the left',
277
282
  loading: 'Loading…',
278
283
  noProviders: 'No USTC providers found',
279
284
  noProvidersHint: 'Check that a model provider uses baseURL api.llm.ustc.edu.cn',
@@ -376,6 +381,19 @@ window.__ModuleLoader__.load({
376
381
  letterSpacing: '-0.01em',
377
382
  whiteSpace: 'nowrap',
378
383
  },
384
+ brandButton: {
385
+ display: 'inline-flex',
386
+ alignItems: 'center',
387
+ justifyContent: 'center',
388
+ margin: 0,
389
+ padding: 0,
390
+ border: 0,
391
+ background: 'transparent',
392
+ color: 'var(--dsw-alias-label-primary)',
393
+ cursor: 'pointer',
394
+ lineHeight: 0,
395
+ flex: '0 0 auto',
396
+ },
379
397
  brandMark: {
380
398
  display: 'inline-flex',
381
399
  alignItems: 'center',
@@ -1006,9 +1024,12 @@ window.__ModuleLoader__.load({
1006
1024
  return provider;
1007
1025
  }
1008
1026
 
1009
- function TokenWorksMark() {
1010
- // Redrawn from the TokenWorks brand mark (five rounded tiles forming a T),
1011
- // as a 24-viewBox monochrome line icon so it inherits currentColor.
1027
+ // Resting state: the TokenWorks brand mark (five rounded tiles forming a T),
1028
+ // redrawn as a 24-viewBox monochrome line icon. Hovering the card swaps it for
1029
+ // the dock switch glyph so the header keeps a single 13 px seat.
1030
+ function TokenWorksMark(props) {
1031
+ const showSwitch = props && props.showSwitch === true;
1032
+ const targetRight = !props || props.target !== 'left';
1012
1033
  const tile = (x, y) => h('rect', {
1013
1034
  key: x + '-' + y,
1014
1035
  x,
@@ -1019,19 +1040,177 @@ window.__ModuleLoader__.load({
1019
1040
  stroke: 'currentColor',
1020
1041
  strokeWidth: 1.6,
1021
1042
  });
1043
+ const frame = (children) => h('svg', {
1044
+ width: 13,
1045
+ height: 13,
1046
+ viewBox: '0 0 24 24',
1047
+ fill: 'none',
1048
+ style: { display: 'block' },
1049
+ 'data-ustc-mark-glyph': showSwitch ? 'switch' : 'brand',
1050
+ }, children);
1051
+ const switchGlyph = targetRight
1052
+ ? [
1053
+ h('rect', { key: 'f', x: 2.4, y: 4.6, width: 19.2, height: 14.8, rx: 2.6, stroke: 'currentColor', strokeWidth: 1.5 }),
1054
+ h('path', { key: 'a', d: 'M7.4 12h7.4', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round' }),
1055
+ h('path', { key: 'b', d: 'M12.2 9.1 15.4 12l-3.2 2.9', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round' }),
1056
+ ]
1057
+ : [
1058
+ h('rect', { key: 'f', x: 2.4, y: 4.6, width: 19.2, height: 14.8, rx: 2.6, stroke: 'currentColor', strokeWidth: 1.5 }),
1059
+ h('path', { key: 'a', d: 'M16.6 12H9.2', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round' }),
1060
+ h('path', { key: 'b', d: 'M11.8 9.1 8.6 12l3.2 2.9', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round' }),
1061
+ ];
1022
1062
  return h('span', { style: STYLE.brandMark, 'aria-hidden': true, 'data-ustc-mark': '' },
1023
- h('svg', { width: 13, height: 13, viewBox: '0 0 24 24', fill: 'none', style: { display: 'block' } },
1024
- tile(2.25, 3.5),
1025
- tile(9.25, 3.5),
1026
- tile(16.25, 3.5),
1027
- tile(9.25, 10.5),
1028
- tile(9.25, 17.5)
1029
- )
1063
+ frame(showSwitch
1064
+ ? switchGlyph
1065
+ : [tile(2.25, 3.5), tile(9.25, 3.5), tile(16.25, 3.5), tile(9.25, 10.5), tile(9.25, 17.5)])
1030
1066
  );
1031
1067
  }
1032
1068
 
1069
+ const panelPositionListeners = new Set();
1070
+
1071
+ function readPanelPosition() {
1072
+ try {
1073
+ return window.localStorage.getItem(POSITION_KEY) === 'right' ? 'right' : 'left';
1074
+ } catch {
1075
+ return 'left';
1076
+ }
1077
+ }
1078
+
1079
+ function writePanelPosition(next) {
1080
+ try {
1081
+ window.localStorage.setItem(POSITION_KEY, next);
1082
+ } catch {
1083
+ // Storage unavailable: the position stays for this session only.
1084
+ }
1085
+ for (const listener of panelPositionListeners) listener();
1086
+ }
1087
+
1088
+ function subscribePanelPosition(listener) {
1089
+ panelPositionListeners.add(listener);
1090
+ return () => { panelPositionListeners.delete(listener); };
1091
+ }
1092
+
1093
+ function usePanelPosition() {
1094
+ return React.useSyncExternalStore(subscribePanelPosition, readPanelPosition, readPanelPosition);
1095
+ }
1096
+
1097
+ // Both seats stay mounted so switching dock position keeps the fetched data:
1098
+ // the hidden seat only stops polling, it never unmounts.
1099
+ function LeftDock(props) {
1100
+ const position = usePanelPosition();
1101
+ const active = position === 'left';
1102
+ // No wrapper element: DSH's footer layout normalisation rewrites display on
1103
+ // ancestors, which would unhide a wrapper we hid ourselves. The card stays
1104
+ // mounted (state preserved) and returns null while the other seat is active.
1105
+ return h(QuotaStrip, Object.assign({}, props, { placement: 'left', active, visible: active }));
1106
+ }
1107
+
1108
+ // DSH exposes no plugin seat for the conversation's right column: 'rightbar'
1109
+ // is a single seat owned by the shipped right sidebar. The dock therefore
1110
+ // measures the conversation column and pins itself into the blank strip
1111
+ // between the chat content and the turn navigator, so it follows the
1112
+ // conversation when either side is dragged or toggled.
1113
+ const DOCK_GAP = 24;
1114
+ const DOCK_MIN_WIDTH = 220;
1115
+ const DOCK_MAX_WIDTH = 420;
1116
+
1117
+ function measureRightDock() {
1118
+ const holder = document.querySelector('[data-slot="main.conversation"]');
1119
+ const column = holder ? (holder.firstElementChild || holder) : null;
1120
+ if (!column) return null;
1121
+ const columnRect = column.getBoundingClientRect();
1122
+ if (columnRect.width < 240 || columnRect.height < 160) return null;
1123
+
1124
+ const flowHolder = document.querySelector('[data-slot="conversation.chat.flow"]');
1125
+ const flow = flowHolder ? (flowHolder.firstElementChild || flowHolder) : null;
1126
+ const flowRect = flow ? flow.getBoundingClientRect() : null;
1127
+
1128
+ // The turn navigator is a rail of per-turn buttons at the column's right edge.
1129
+ let navLeft = null;
1130
+ for (const button of document.querySelectorAll('button[aria-label]')) {
1131
+ const label = button.getAttribute('aria-label') || '';
1132
+ if (!/turnNavigation|轮次|跳转/i.test(label)) continue;
1133
+ const rect = button.getBoundingClientRect();
1134
+ if (rect.width > 0 && rect.height > 0) navLeft = navLeft === null ? rect.left : Math.min(navLeft, rect.left);
1135
+ }
1136
+ if (navLeft === null) navLeft = columnRect.right - 26;
1137
+
1138
+ let width = DOCK_MAX_WIDTH;
1139
+ if (flowRect && flowRect.width > 0 && flowRect.right < navLeft) {
1140
+ const available = navLeft - flowRect.right - DOCK_GAP * 2;
1141
+ width = Math.min(DOCK_MAX_WIDTH, Math.max(DOCK_MIN_WIDTH, Math.round(available)));
1142
+ }
1143
+ width = Math.max(DOCK_MIN_WIDTH, Math.min(width, Math.round(columnRect.width - DOCK_GAP * 2)));
1144
+
1145
+ return {
1146
+ top: Math.max(8, Math.round(columnRect.top) + 16),
1147
+ right: Math.max(8, Math.round(window.innerWidth - navLeft) + DOCK_GAP),
1148
+ width,
1149
+ maxHeight: Math.max(160, Math.round(columnRect.height) - 32),
1150
+ };
1151
+ }
1152
+
1153
+ function RightDock(props) {
1154
+ const position = usePanelPosition();
1155
+ const active = position === 'right';
1156
+ const [box, setBox] = React.useState(null);
1157
+ React.useEffect(() => {
1158
+ let frame = 0;
1159
+ let observer = null;
1160
+ let observed = null;
1161
+ const measure = () => {
1162
+ frame = 0;
1163
+ const holder = document.querySelector('[data-slot="main.conversation"]');
1164
+ const column = holder ? (holder.firstElementChild || holder) : null;
1165
+ if (column && column !== observed && typeof ResizeObserver === 'function') {
1166
+ if (observer) observer.disconnect();
1167
+ observed = column;
1168
+ observer = new ResizeObserver(schedule);
1169
+ observer.observe(column);
1170
+ }
1171
+ setBox(measureRightDock());
1172
+ };
1173
+ function schedule() {
1174
+ if (!frame) frame = window.requestAnimationFrame(measure);
1175
+ }
1176
+ measure();
1177
+ const timer = window.setInterval(measure, 2000);
1178
+ window.addEventListener('resize', schedule);
1179
+ return () => {
1180
+ window.removeEventListener('resize', schedule);
1181
+ window.clearInterval(timer);
1182
+ if (observer) observer.disconnect();
1183
+ if (frame) window.cancelAnimationFrame(frame);
1184
+ };
1185
+ }, []);
1186
+ const shown = active && box !== null;
1187
+ return h('div', {
1188
+ 'data-ustc-quota-dock': 'right',
1189
+ style: {
1190
+ position: 'fixed',
1191
+ top: box ? box.top : 0,
1192
+ right: box ? box.right : 0,
1193
+ width: box ? box.width : DOCK_MAX_WIDTH,
1194
+ maxHeight: box ? box.maxHeight : 0,
1195
+ overflowY: 'auto',
1196
+ display: shown ? 'flex' : 'none',
1197
+ flexDirection: 'column',
1198
+ gap: 6,
1199
+ pointerEvents: 'auto',
1200
+ zIndex: 5,
1201
+ },
1202
+ }, h(QuotaStrip, Object.assign({}, props, { placement: 'right', wide: true, active, visible: active })));
1203
+ }
1204
+
1033
1205
  function QuotaStrip(props) {
1034
1206
  const t = typeof props.t === 'function' ? props.t : fallbackT;
1207
+ const placement = props.placement === 'right' ? 'right' : 'left';
1208
+ const position = usePanelPosition();
1209
+ const active = props.active !== false;
1210
+ const [cardHovered, setCardHovered] = React.useState(false);
1211
+ const [markHovered, setMarkHovered] = React.useState(false);
1212
+ const activeRef = React.useRef(active);
1213
+ activeRef.current = active;
1035
1214
  const [snapshot, setSnapshot] = React.useState(null);
1036
1215
  const [error, setError] = React.useState(null);
1037
1216
  const [loading, setLoading] = React.useState(true);
@@ -1172,6 +1351,12 @@ window.__ModuleLoader__.load({
1172
1351
  }
1173
1352
 
1174
1353
  async function run() {
1354
+ if (!activeRef.current) {
1355
+ // The card is mounted in the other seat right now: keep the last data,
1356
+ // skip the request, and try again on the next tick.
1357
+ schedule(15000);
1358
+ return;
1359
+ }
1175
1360
  const body = await load(false);
1176
1361
  if (disposed) return;
1177
1362
  schedule(body && Number.isFinite(body.pollAfterMs) ? body.pollAfterMs : 15000);
@@ -1210,9 +1395,12 @@ window.__ModuleLoader__.load({
1210
1395
 
1211
1396
  React.useLayoutEffect(() => {
1212
1397
  const root = rootRef.current;
1213
- if (!root) return;
1398
+ if (!root) return undefined;
1399
+ // The right dock is not inside DSH's footer row; rewriting ancestors there
1400
+ // would fight the dock container instead of the sidebar layout.
1401
+ if (placement === 'right' || props.visible === false) return undefined;
1214
1402
  return installFooterLayout(root);
1215
- }, [props.wide]);
1403
+ }, [props.wide, placement]);
1216
1404
 
1217
1405
  const keys = snapshot && Array.isArray(snapshot.keys) ? snapshot.keys : [];
1218
1406
  const warnings = snapshot && Array.isArray(snapshot.warnings) ? snapshot.warnings : [];
@@ -1221,6 +1409,8 @@ window.__ModuleLoader__.load({
1221
1409
  const tokenworksNotReady = warnings.includes('tokenworks-not-ready');
1222
1410
 
1223
1411
  if (props.wide === false) return null;
1412
+ // Hidden seat: keep the component (and its data) mounted, render no DOM.
1413
+ if (props.visible === false) return null;
1224
1414
 
1225
1415
  function handleVersionClick() {
1226
1416
  if (updateInfo && updateInfo.updateAvailable) {
@@ -1545,7 +1735,18 @@ window.__ModuleLoader__.load({
1545
1735
  ? t('updateChecking')
1546
1736
  : t('versionCheck');
1547
1737
  const header = h('div', { style: STYLE.header },
1548
- h(TokenWorksMark),
1738
+ h('button', {
1739
+ type: 'button',
1740
+ 'data-ustc-position-toggle': '',
1741
+ 'aria-label': position === 'right' ? t('moveToLeft') : t('moveToRight'),
1742
+ title: position === 'right' ? t('moveToLeft') : t('moveToRight'),
1743
+ onClick: () => { writePanelPosition(position === 'right' ? 'left' : 'right'); },
1744
+ onMouseEnter: () => { setMarkHovered(true); },
1745
+ onMouseLeave: () => { setMarkHovered(false); },
1746
+ onFocus: () => { setMarkHovered(true); },
1747
+ onBlur: () => { setMarkHovered(false); },
1748
+ style: STYLE.brandButton,
1749
+ }, h(TokenWorksMark, { showSwitch: markHovered || cardHovered, target: position === 'right' ? 'left' : 'right' })),
1549
1750
  h('span', { style: STYLE.title, 'data-ustc-title': '' }, t('title')),
1550
1751
  h('button', {
1551
1752
  type: 'button',
@@ -1566,6 +1767,7 @@ window.__ModuleLoader__.load({
1566
1767
  : null,
1567
1768
  error && snapshot ? h('span', { style: STYLE.warn, title: error }, '!') : null,
1568
1769
  h('span', { style: STYLE.meta }, updated ? `${t('updatedLabel')} ${updated}` : ''),
1770
+
1569
1771
  h('button', {
1570
1772
  type: 'button',
1571
1773
  'aria-label': t('refresh'),
@@ -1707,6 +1909,8 @@ window.__ModuleLoader__.load({
1707
1909
  role: 'group',
1708
1910
  'aria-label': t('title'),
1709
1911
  'data-ustc-quota-root': '',
1912
+ onMouseEnter: () => { setCardHovered(true); },
1913
+ onMouseLeave: () => { setCardHovered(false); setMarkHovered(false); },
1710
1914
  },
1711
1915
  header,
1712
1916
  restartBanner,
@@ -1839,7 +2043,14 @@ window.__ModuleLoader__.load({
1839
2043
  order: 0,
1840
2044
  locale: NS,
1841
2045
  inject: () => ({ t }),
1842
- }, QuotaStrip));
2046
+ }, LeftDock));
2047
+ ctx.slots.inject('shell.overlay', () => ctx.slots.register({
2048
+ name: 'shell.overlay',
2049
+ id: 'ustc-quota-right',
2050
+ order: 100,
2051
+ locale: NS,
2052
+ inject: () => ({ t }),
2053
+ }, RightDock));
1843
2054
  },
1844
2055
  };
1845
2056
  },
package/index.js CHANGED
@@ -22,7 +22,7 @@ const ROUTE_PATH = '/plugins/dsh-ustc-quota/snapshot';
22
22
  const UPDATE_PATH = '/plugins/dsh-ustc-quota/update';
23
23
  const UPDATE_APPLY_PATH = '/plugins/dsh-ustc-quota/update/apply';
24
24
  const UPDATE_TOKEN_TTL_MS = 30 * 60 * 1000;
25
- const PLUGIN_VERSION = '1.1.20';
25
+ const PLUGIN_VERSION = '1.1.22';
26
26
  const KEY_INFO_PATH = 'key/info';
27
27
  const USER_INFO_PATH = 'user/info';
28
28
  const DISCOVERY_TTL_MS = 30000;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ustc-quota",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Live 3h/12h/24h USTC token quota card in the DSH sidebar (works with the USTC TokenWorks distribution).",