@ququ-jiaxk/dsh-groupchat 0.1.4 → 0.1.6

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 (3) hide show
  1. package/client.js +253 -5
  2. package/index.js +30 -21
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -350,6 +350,82 @@ window.__ModuleLoader__.load({
350
350
  }
351
351
  })()
352
352
 
353
+ // ---- 版本检查 store ----
354
+ // 面板挂载(切到群聊)时自动查一次;60 秒内重复切换不重复请求。
355
+ // 结果缓存起来,切走再回来不用重新查。
356
+ const versionStore = (() => {
357
+ let value = null // { current, latest, status, reason, checkedAt }
358
+ let lastCheck = 0
359
+ let inflight = null
360
+ const listeners = new Set()
361
+ const publish = (next) => {
362
+ value = next
363
+ for (const l of listeners) l()
364
+ }
365
+ return {
366
+ get: () => value,
367
+ subscribe: (listener) => {
368
+ listeners.add(listener)
369
+ return () => listeners.delete(listener)
370
+ },
371
+ async check(force) {
372
+ const now = Date.now()
373
+ if (!force && value && now - lastCheck < 60 * 1000) return value
374
+ if (inflight) return inflight
375
+ lastCheck = now
376
+ inflight = (async () => {
377
+ try {
378
+ const r = await api('/version')
379
+ publish(r)
380
+ return r
381
+ } catch (e) {
382
+ publish({ status: 'unavailable', reason: (e && e.message) || '检查失败', checkedAt: Date.now() })
383
+ return value
384
+ } finally {
385
+ inflight = null
386
+ }
387
+ })()
388
+ return inflight
389
+ },
390
+ async update(version) {
391
+ try {
392
+ const r = await api('/version/update', {
393
+ method: 'POST',
394
+ headers: { 'content-type': 'application/json' },
395
+ body: JSON.stringify({ version }),
396
+ })
397
+ if (r && r.ok) {
398
+ publish({ ...(value || {}), status: 'updated', updatedTo: version })
399
+ return { ok: true, version }
400
+ }
401
+ return { ok: false, error: (r && r.error) || '更新失败' }
402
+ } catch (e) {
403
+ return { ok: false, error: (e && e.message) || '更新失败' }
404
+ }
405
+ },
406
+ }
407
+ })()
408
+
409
+ // ---- 群聊面板是否处于激活态 ----
410
+ // DSH 的 main 插槽只挂载当前面板:切到别的面板/会话时 GroupchatPanel 会卸载。
411
+ // 所以用「面板挂载 = 激活」来驱动侧栏入口的高亮,不依赖内部 layout store。
412
+ const panelActiveStore = (() => {
413
+ let value = false
414
+ const listeners = new Set()
415
+ return {
416
+ get: () => value,
417
+ set: (next) => {
418
+ if (next === value) return
419
+ value = next
420
+ for (const l of listeners) l()
421
+ },
422
+ subscribe: (listener) => {
423
+ listeners.add(listener)
424
+ return () => listeners.delete(listener)
425
+ },
426
+ }
427
+ })()
428
+
353
429
  // ---- 面板记忆(跨卸载保留 UI 状态) ----
354
430
  // 用户切到别的界面/会话时 GroupchatPanel 会卸载,React state 全部丢失,
355
431
  // 再回来就"重新加载"(回到列表、草稿没了、滚动回顶部)。
@@ -1054,7 +1130,13 @@ window.__ModuleLoader__.load({
1054
1130
 
1055
1131
  // ---- 主面板 ----
1056
1132
  function GroupchatPanel() {
1057
- // 数据来自全局 stateStore(由 globalPoll 驱动),面板不再自己轮询
1133
+ // 挂载 = 群聊面板被选中(DSH 的 main 插槽只挂载当前面板),用于侧栏入口高亮
1134
+ useEffect(() => {
1135
+ panelActiveStore.set(true)
1136
+ return () => panelActiveStore.set(false)
1137
+ }, [])
1138
+
1139
+ // 数据来自全局 stateStore(由全局轮询驱动),面板不再自己轮询
1058
1140
  const [state, setState] = useState(() => stateStore.get())
1059
1141
  // 视图从模块级记忆恢复:切走再切回仍停留在原来的群/标签页
1060
1142
  const [view, setViewRaw] = useState(() => panelMemo.view || 'list') // 'list' | 'create' | 'roster' | 'createProfile' | groupId
@@ -1130,7 +1212,10 @@ window.__ModuleLoader__.load({
1130
1212
  tabLink(lang.panel, view === 'list', () => setView('list')),
1131
1213
  tabLink(lang.roster, view === 'roster', () => setView('roster')),
1132
1214
  ),
1133
- btn(lang.newGroup, () => setView('create')),
1215
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: 10 } },
1216
+ h(VersionBadge, null),
1217
+ btn(lang.newGroup, () => setView('create')),
1218
+ ),
1134
1219
  ),
1135
1220
  // 群卡片列表
1136
1221
  h('div', { className: 'gc-fade', style: { flex: 1, overflow: 'auto', padding: '16px 20px' } },
@@ -2528,7 +2613,10 @@ window.__ModuleLoader__.load({
2528
2613
  tabLink(lang.panel, false, () => setView('list')),
2529
2614
  tabLink(lang.roster, true, () => setView('roster')),
2530
2615
  ),
2531
- btn(lang.newProfile, () => setView('createProfile')),
2616
+ h('div', { style: { display: 'flex', alignItems: 'center', gap: 10 } },
2617
+ h(VersionBadge, null),
2618
+ btn(lang.newProfile, () => setView('createProfile')),
2619
+ ),
2532
2620
  ),
2533
2621
  // 卡片网格
2534
2622
  h('div', { className: 'gc-fade', style: { flex: 1, overflow: 'auto', padding: '24px' } },
@@ -3312,6 +3400,164 @@ window.__ModuleLoader__.load({
3312
3400
  }
3313
3401
 
3314
3402
  // ---- 侧栏图标(带未读角标) ----
3403
+ /**
3404
+ * 版本徽标:显示当前版本,点击检查更新;有新版本时可直接更新。
3405
+ * 面板每次挂载(切到群聊)自动查一次,60 秒内不重复请求。
3406
+ */
3407
+ function VersionBadge() {
3408
+ const [info, setInfo] = useState(() => versionStore.get())
3409
+ const [busy, setBusy] = useState(false)
3410
+ const [error, setError] = useState('')
3411
+
3412
+ useEffect(() => {
3413
+ const unsubscribe = versionStore.subscribe(() => setInfo(versionStore.get()))
3414
+ setInfo(versionStore.get())
3415
+ // 挂载即自动检查(内部有节流)
3416
+ versionStore.check(false)
3417
+ return unsubscribe
3418
+ }, [])
3419
+
3420
+ const current = (info && info.current) || ''
3421
+ const latest = (info && info.latest) || ''
3422
+ const status = (info && info.status) || 'unknown'
3423
+ const hasUpdate = status === 'available' && latest && latest !== current
3424
+
3425
+ const onClick = async () => {
3426
+ if (busy) return
3427
+ setError('')
3428
+ if (hasUpdate) {
3429
+ setBusy(true)
3430
+ const r = await versionStore.update(latest)
3431
+ setBusy(false)
3432
+ if (!r.ok) setError(r.error || '更新失败')
3433
+ return
3434
+ }
3435
+ setBusy(true)
3436
+ await versionStore.check(true)
3437
+ setBusy(false)
3438
+ }
3439
+
3440
+ // 文案与配色按状态走
3441
+ let label = current ? 'v' + current : '版本'
3442
+ let fg = T.textMuted
3443
+ let bg = 'transparent'
3444
+ let border = T.border
3445
+ if (busy) label = hasUpdate ? '更新中…' : '检查中…'
3446
+ else if (status === 'updated') { label = '已更新,重启生效'; fg = T.ok; bg = T.okBg; border = 'transparent' }
3447
+ else if (hasUpdate) { label = '↑ v' + latest; fg = '#fff'; bg = T.accent; border = 'transparent' }
3448
+ else if (error || status === 'unavailable') { fg = T.danger }
3449
+
3450
+ return h('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } },
3451
+ h('button', {
3452
+ type: 'button',
3453
+ onClick,
3454
+ disabled: busy,
3455
+ title: error || (hasUpdate
3456
+ ? `发现新版本 ${latest},点击更新(当前 ${current})`
3457
+ : status === 'unavailable'
3458
+ ? `检查更新不可用:${(info && info.reason) || ''}`
3459
+ : '点击检查更新'),
3460
+ style: {
3461
+ display: 'inline-flex', alignItems: 'center', gap: 4,
3462
+ padding: '3px 9px', borderRadius: 999, cursor: busy ? 'default' : 'pointer',
3463
+ border: `1px solid ${border}`, background: bg, color: fg,
3464
+ fontSize: 11.5, fontWeight: hasUpdate ? 600 : 500, fontFamily: 'inherit',
3465
+ transition: 'all .15s ease', whiteSpace: 'nowrap',
3466
+ },
3467
+ onMouseEnter: (e) => { if (!busy && !hasUpdate) e.currentTarget.style.background = T.bg },
3468
+ onMouseLeave: (e) => { if (!busy && !hasUpdate) e.currentTarget.style.background = 'transparent' },
3469
+ }, label),
3470
+ error ? h('span', { style: { fontSize: 11, color: T.danger } }, error) : null,
3471
+ )
3472
+ }
3473
+
3474
+ /**
3475
+ * 侧栏「群聊」入口按钮:与「新会话」并排(`sidebar.session.action` 槽位)。
3476
+ *
3477
+ * 两个约束(都踩过):
3478
+ * 1. 该槽位会把默认的「新会话」按钮作为 children 传进来,必须原样渲染,否则新会话消失。
3479
+ * 2. 那一行是横向 flex,新会话靠 flex:1 占位;我们的按钮**不能设 width:100%**,
3480
+ * 否则会把新会话挤成 0 宽。用自适应宽度 + flexShrink:0,新会话会自然收窄,两者并排。
3481
+ */
3482
+ function GroupchatEntryButton(props) {
3483
+ const wide = !!(props && props.wide)
3484
+ const children = props ? props.children : null
3485
+ const [unread, setUnread] = useState(0)
3486
+ const [active, setActive] = useState(() => panelActiveStore.get())
3487
+ useEffect(() => {
3488
+ const unsubscribe = unreadStore.subscribe(() => setUnread(unreadStore.get()))
3489
+ setUnread(unreadStore.get())
3490
+ return unsubscribe
3491
+ }, [])
3492
+ useEffect(() => {
3493
+ const unsubscribe = panelActiveStore.subscribe(() => setActive(panelActiveStore.get()))
3494
+ setActive(panelActiveStore.get())
3495
+ return unsubscribe
3496
+ }, [])
3497
+ const open = () => {
3498
+ const layout = layoutRef.current
3499
+ if (layout && typeof layout.selectPanel === 'function') layout.selectPanel(PANEL_ID)
3500
+ }
3501
+ // 选中态:主色底 + 主色文字(和其他面板选中时一致的感觉)
3502
+ // 默认态:和「新会话」完全一致(同一套变量、同圆角、同高度、同字号)
3503
+ const idleBg = 'var(--dsw-alias-fill-secondary, rgba(16,41,66,0.05))'
3504
+ const idleColor = 'var(--dsw-alias-label-primary, #102942)'
3505
+ // 激活态:主色实底 + 白字,一眼能看出当前就在群聊
3506
+ const activeBg = 'var(--dsw-alias-accent, #2E6BE6)'
3507
+ const activeColor = '#fff'
3508
+ return h(React.Fragment, null,
3509
+ children,
3510
+ h('button', {
3511
+ type: 'button',
3512
+ 'aria-label': '群聊',
3513
+ 'aria-current': active ? 'page' : undefined,
3514
+ title: '群聊',
3515
+ onClick: open,
3516
+ style: {
3517
+ display: 'flex', alignItems: 'center', justifyContent: 'center',
3518
+ gap: wide ? 6 : 0,
3519
+ height: 34, boxSizing: 'border-box',
3520
+ padding: wide ? '0 6px' : 0,
3521
+ // 高度/圆角/配色与新会话一致;宽度按内容,避免把新会话压成两行
3522
+ flex: '0 0 auto', minWidth: 0, whiteSpace: 'nowrap',
3523
+ border: 'none', cursor: 'pointer',
3524
+ borderRadius: 'var(--dsw-radius-md, 10px)',
3525
+ background: active ? activeBg : idleBg,
3526
+ color: active ? activeColor : idleColor,
3527
+ fontSize: 13, fontWeight: active ? 600 : 500, fontFamily: 'inherit',
3528
+ transition: 'background .15s ease, color .15s ease',
3529
+ },
3530
+ onMouseEnter: (e) => { if (!active) e.currentTarget.style.background = 'var(--dsw-alias-fill-tertiary, rgba(16,41,66,0.09))' },
3531
+ onMouseLeave: (e) => { e.currentTarget.style.background = active ? activeBg : idleBg },
3532
+ onMouseDown: (e) => { if (active) e.currentTarget.style.filter = 'brightness(0.94)' },
3533
+ onMouseUp: (e) => { e.currentTarget.style.filter = 'none' },
3534
+ },
3535
+ h('span', { style: { position: 'relative', display: 'flex', flexShrink: 0 } },
3536
+ h('svg', { width: 16, height: 16, viewBox: '0 0 18 18', fill: 'none', stroke: 'currentColor', strokeWidth: 1.7, strokeLinecap: 'round', strokeLinejoin: 'round' },
3537
+ h('circle', { cx: 6, cy: 7.5, r: 2.2 }),
3538
+ h('circle', { cx: 12, cy: 7.5, r: 2.2 }),
3539
+ h('path', { d: 'M2.5 14.5c0-1.9 1.3-3.2 3.3-3.2h1.4M15.5 14.5c0-1.9-1.3-3.2-3.3-3.2h-1.4' }),
3540
+ ),
3541
+ unread > 0 && !wide ? h('span', {
3542
+ style: {
3543
+ position: 'absolute', top: -3, right: -5, minWidth: 13, height: 13, borderRadius: 999,
3544
+ background: '#C0392B', color: '#fff', fontSize: 8, fontWeight: 700,
3545
+ display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '0 3px',
3546
+ },
3547
+ }, unread > 99 ? '99+' : String(unread)) : null,
3548
+ ),
3549
+ wide ? h('span', { style: { whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' } }, '群聊') : null,
3550
+ unread > 0 && wide ? h('span', {
3551
+ style: {
3552
+ minWidth: 16, height: 16, borderRadius: 999, background: '#C0392B', color: '#fff',
3553
+ fontSize: 9, fontWeight: 700, display: 'flex', alignItems: 'center',
3554
+ justifyContent: 'center', padding: '0 4px', flexShrink: 0,
3555
+ },
3556
+ }, unread > 99 ? '99+' : String(unread)) : null,
3557
+ ),
3558
+ )
3559
+ }
3560
+
3315
3561
  function GroupchatIcon() {
3316
3562
  const [unread, setUnread] = useState(0)
3317
3563
  useEffect(() => {
@@ -3419,8 +3665,10 @@ window.__ModuleLoader__.load({
3419
3665
  return () => { clearInterval(globalPollTimer); globalPollTimer = null }
3420
3666
  }, 'groupchat: global poll')
3421
3667
 
3422
- ctx.slots.inject('sidebar.panellist', () =>
3423
- ctx.slots.register({ name: 'sidebar.panellist', id: PANEL_ID, order: 30, label: () => '群聊', locale: NS }, GroupchatIcon),
3668
+ // 侧栏入口:与「新会话」并排(sidebar.session.action 槽位)。
3669
+ // 该槽位是横向 flex 行,我们注册一个自适应宽度的按钮,新会话会随之收窄,两者并排。
3670
+ ctx.slots.inject('sidebar.session.action', () =>
3671
+ ctx.slots.register({ name: 'sidebar.session.action', id: PANEL_ID, locale: NS }, GroupchatEntryButton),
3424
3672
  )
3425
3673
  ctx.slots.inject('main', function* () {
3426
3674
  yield ctx.slots.register({ name: 'main', key: PANEL_ID }, GroupchatPanel)
package/index.js CHANGED
@@ -2001,34 +2001,35 @@ export function apply(ctx) {
2001
2001
  // POST /groupchat/api/version/update 安装指定版本(需重启 DSH 生效)
2002
2002
  if (pathname === '/groupchat/api/version' && method === 'GET') {
2003
2003
  const own = readOwnPackage()
2004
- const manager = (() => { try { return ctx.get('pluginManager') } catch (_e) { return null } })()
2005
2004
  const base = { current: own.version, packageName: PUBLISHED_PACKAGE, localName: own.name, checkedAt: Date.now() }
2006
- if (!manager || typeof manager.listVersions !== 'function') {
2007
- return sendJson(res, 200, { ...base, status: 'unavailable', reason: '当前 DSH 不支持插件版本查询' })
2008
- }
2009
- let list
2005
+ // 不用 pluginManager.listVersions:对 `link:` 安装(插件中心装法)它会直接拒绝
2006
+ // ("installed neither from a registry under its own name nor from a Git commit")。
2007
+ // 直接查 npm registry 的 dist-tags,公开包不需要鉴权。
2008
+ let latest = null
2010
2009
  try {
2011
- list = await manager.listVersions(PUBLISHED_PACKAGE)
2010
+ const res = await fetch('https://registry.npmjs.org/' + PUBLISHED_PACKAGE.replace('/', '%2F'), {
2011
+ headers: { accept: 'application/vnd.npm.install-v1+json' },
2012
+ signal: AbortSignal.timeout(8000),
2013
+ })
2014
+ if (!res.ok) throw new Error('registry ' + res.status)
2015
+ const doc = await res.json()
2016
+ const tag = doc && doc['dist-tags'] && doc['dist-tags'].latest
2017
+ latest = typeof tag === 'string' ? tag : null
2012
2018
  } catch (e) {
2013
- return sendJson(res, 200, { ...base, status: 'unavailable', reason: String(e && e.message || e) })
2014
- }
2015
- if (!list || list.status !== 'listed') {
2016
- return sendJson(res, 200, { ...base, status: 'unavailable', reason: (list && (list.reason || list.problem)) || '未找到已发布版本' })
2019
+ return sendJson(res, 200, {
2020
+ ...base, status: 'unavailable',
2021
+ reason: '查询 npm 失败:' + String((e && e.message) || e),
2022
+ })
2017
2023
  }
2018
- const versions = Array.isArray(list.versions) ? list.versions : []
2019
- // 只认正式版:跳过预发布/已废弃/与本机不兼容的
2020
- const candidates = versions.filter((v) => v && !v.prerelease && !v.deprecated && !v.incompatible)
2021
- let latest = null
2022
- for (const v of candidates) {
2023
- if (!latest || compareVersions(v.version, latest.version) > 0) latest = v
2024
+ if (!latest) {
2025
+ return sendJson(res, 200, { ...base, status: 'unavailable', reason: 'npm 上没有 latest 标签' })
2024
2026
  }
2025
- const latestVersion = latest ? latest.version : null
2026
- const hasUpdate = !!latestVersion && compareVersions(latestVersion, own.version) > 0
2027
+ const hasUpdate = compareVersions(latest, own.version) > 0
2027
2028
  return sendJson(res, 200, {
2028
2029
  ...base,
2029
2030
  status: hasUpdate ? 'available' : 'current',
2030
- latest: latestVersion,
2031
- registry: list.registry || null,
2031
+ latest,
2032
+ registry: 'https://registry.npmjs.org',
2032
2033
  })
2033
2034
  }
2034
2035
 
@@ -2044,7 +2045,15 @@ export function apply(ctx) {
2044
2045
  await manager.updateBundle(PUBLISHED_PACKAGE, target)
2045
2046
  return sendJson(res, 200, { ok: true, version: target, restartRequired: true })
2046
2047
  } catch (e) {
2047
- return sendJson(res, 200, { ok: false, error: String(e && e.message || e) })
2048
+ const msg = String((e && e.message) || e)
2049
+ // link: 安装(插件中心装法)不支持原地更新,给出可操作路径而不是干巴巴报错
2050
+ return sendJson(res, 200, {
2051
+ ok: false,
2052
+ version: target,
2053
+ error: '当前是本地链接安装,无法原地更新',
2054
+ hint: '打开「小趣插件中心」→ 本插件 → 更新到 v' + target + ',然后重启 DSH',
2055
+ detail: msg,
2056
+ })
2048
2057
  }
2049
2058
  }
2050
2059
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ququ-jiaxk/dsh-groupchat",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "description": "DSH 群聊插件:把已有会话作为固定角色的智能体拉进项目群,由项目经理 agent(sAgent)聚合、推进、排人、汇报。",
6
6
  "exports": {