272 lines
13 KiB
JavaScript
272 lines
13 KiB
JavaScript
window.__ModuleLoader__.load({
|
||
id: 'dsh-sansheng-sansheng',
|
||
factory: (require) => {
|
||
var module = { exports: {} }
|
||
var exports = module.exports
|
||
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
||
|
||
const React = require('react')
|
||
const h = React.createElement
|
||
|
||
const TAB_ID = 'sansheng-roles'
|
||
const TAB_KIND = 'sansheng-roles'
|
||
const CSS_ID = 'dsh-sansheng-roles'
|
||
const POLL_MS = 4000
|
||
const ACTIVE_KEY = 'dsh.sansheng.activeProject'
|
||
|
||
const ROLE_MARK = { lead: '丞', shangshu: '尚', gongbu: '工' }
|
||
|
||
const CSS = `
|
||
.ss-root{display:flex;flex-direction:column;height:100%;min-height:0;background:var(--dsw-alias-bg-layer-1,#fff);color:var(--dsw-alias-label-primary,#1f2329);font-size:13px;line-height:20px}
|
||
.ss-bar{display:flex;align-items:center;gap:8px;padding:10px 12px;border-bottom:1px solid var(--dsw-alias-border-l1,rgba(127,127,127,.14));flex:none}
|
||
.ss-bar select{appearance:none;border:1px solid var(--dsw-alias-border-l2,rgba(127,127,127,.24));background:var(--dsw-alias-bg-layer-2,rgba(127,127,127,.06));color:inherit;font:inherit;font-size:12px;padding:2px 22px 2px 8px;border-radius:7px;min-height:26px;max-width:100%}
|
||
.ss-bar-note{margin-left:auto;font-size:11px;color:var(--dsw-alias-label-tertiary,#8a8f98);white-space:nowrap}
|
||
.ss-list{flex:1;min-height:0;overflow:auto;display:flex;flex-direction:column;gap:10px;padding:12px}
|
||
.ss-card{position:relative;display:flex;flex-direction:column;gap:6px;padding:11px 12px;border:1px solid var(--dsw-alias-border-l2,rgba(127,127,127,.2));border-radius:12px;background:var(--dsw-alias-bg-layer-2,rgba(127,127,127,.04));text-align:left;color:inherit;font:inherit;width:100%;min-width:0}
|
||
.ss-card[data-role="lead"]{--ss-role:#b8860b}
|
||
.ss-card[data-role="shangshu"]{--ss-role:#2f6fed}
|
||
.ss-card[data-role="gongbu"]{--ss-role:#12805c}
|
||
.ss-card[data-tone="warn"]{border-color:color-mix(in srgb,var(--dsw-alias-state-error-primary,#dc2626) 45%,transparent)}
|
||
.ss-card-head{display:flex;align-items:center;gap:8px;min-width:0}
|
||
.ss-mark{flex:none;width:22px;height:22px;border-radius:7px;display:inline-flex;align-items:center;justify-content:center;font-size:11px;font-weight:600;color:#fff;background:var(--ss-role,#666)}
|
||
.ss-card-head strong{font-size:13px;font-weight:600;letter-spacing:.02em}
|
||
.ss-dot{flex:none;margin-left:auto;width:7px;height:7px;border-radius:50%;background:var(--dsw-alias-label-tertiary,#9ca3af)}
|
||
.ss-card[data-tone="active"] .ss-dot{background:var(--dsw-alias-state-business-primary,#3b82f6)}
|
||
.ss-card[data-tone="ok"] .ss-dot{background:#1a9d6a}
|
||
.ss-card[data-tone="warn"] .ss-dot{background:var(--dsw-alias-state-error-primary,#dc2626)}
|
||
.ss-card[data-tone="attention"] .ss-dot{background:#c9871f}
|
||
.ss-duty{font-size:11px;color:var(--dsw-alias-label-tertiary,#8a8f98);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||
.ss-main{font-size:12.5px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||
.ss-sub{font-size:11px;line-height:17px;color:var(--dsw-alias-label-secondary,#5b616b);display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;word-break:break-word}
|
||
.ss-foot{display:flex;align-items:center;gap:6px;font-size:10.5px;color:var(--dsw-alias-label-tertiary,#8a8f98);min-width:0}
|
||
.ss-foot span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||
.ss-chip{flex:none;border:1px solid var(--dsw-alias-border-l2,rgba(127,127,127,.24));border-radius:999px;padding:0 7px;font-size:10px;line-height:16px}
|
||
.ss-empty{display:flex;flex-direction:column;gap:8px;align-items:flex-start;padding:18px 4px;color:var(--dsw-alias-label-secondary,#5b616b);font-size:12px}
|
||
.ss-empty code{font-size:11px;background:var(--dsw-alias-bg-layer-3,rgba(127,127,127,.14));padding:1px 5px;border-radius:5px}
|
||
.ss-error{color:var(--dsw-alias-state-error-primary,#dc2626);font-size:11px;line-height:17px}
|
||
.ss-tab-title{display:inline-flex;align-items:center;gap:6px;min-width:0}
|
||
.ss-tab-mark{width:18px;height:18px;border-radius:6px;display:inline-flex;align-items:center;justify-content:center;font-size:9px;font-weight:600;color:#fff;background:linear-gradient(135deg,#b8860b,#2f6fed 60%,#12805c)}
|
||
.ss-tab-count{border-radius:999px;padding:0 6px;font-size:10px;line-height:15px;background:var(--dsw-alias-state-business-primary,#3b82f6);color:#fff}
|
||
.ss-open{appearance:none;border:0;background:0 0;color:var(--dsw-alias-label-tertiary,#8a8f98);font:inherit;font-size:12px;display:inline-flex;align-items:center;gap:5px;padding:3px 7px;border-radius:6px;cursor:pointer;min-height:26px}
|
||
.ss-open:hover,.ss-open:focus-visible{color:var(--dsw-alias-label-primary,#1f2329);background:var(--dsw-alias-bg-layer-2,rgba(127,127,127,.08))}
|
||
`
|
||
|
||
function ensureCss() {
|
||
if (typeof document === 'undefined') return
|
||
try {
|
||
const selector = 'style[data-plugin-css=' + JSON.stringify(CSS_ID) + ']'
|
||
if (document.querySelector(selector)) return
|
||
const style = document.createElement('style')
|
||
style.setAttribute('data-plugin-css', CSS_ID)
|
||
style.textContent = CSS
|
||
document.head.appendChild(style)
|
||
} catch (error) {
|
||
/* 样式注入失败不影响功能 */
|
||
}
|
||
}
|
||
|
||
function serviceOf(ctx, name) {
|
||
try {
|
||
if (typeof ctx.get === 'function') {
|
||
const service = ctx.get(name, false)
|
||
if (service) return service
|
||
}
|
||
} catch (error) { /* 老宿主没有该服务 */ }
|
||
return undefined
|
||
}
|
||
|
||
function betterSidebarOf(ctx) {
|
||
const service = serviceOf(ctx, 'betterSidebar')
|
||
return service && typeof service.openTab === 'function' ? service : undefined
|
||
}
|
||
|
||
/** 打开右栏「三省六部」页签:优先 better-sidebar,其次宿主原生右栏。 */
|
||
function openRolesTab(ctx, sessionId) {
|
||
if (!sessionId) return { ok: false, error: '先打开一个会话,右侧卡片是会话级的。' }
|
||
const better = betterSidebarOf(ctx)
|
||
if (better) {
|
||
try {
|
||
better.openTab({ type: TAB_ID }, { sessionId })
|
||
return { ok: true }
|
||
} catch (error) { /* 退回原生右栏 */ }
|
||
}
|
||
const nav = serviceOf(ctx, 'sidebarRight')
|
||
if (nav) {
|
||
try {
|
||
if (typeof nav.openTabIn === 'function') {
|
||
nav.openTabIn(sessionId, TAB_KIND)
|
||
return { ok: true }
|
||
}
|
||
if (typeof nav.openTab === 'function') {
|
||
nav.openTab(TAB_KIND)
|
||
return { ok: true }
|
||
}
|
||
} catch (error) { /* 统一报错 */ }
|
||
}
|
||
return { ok: false, error: '宿主右栏不可用。' }
|
||
}
|
||
|
||
function useSanshengState() {
|
||
const [state, setState] = React.useState({ loading: true, error: null, data: null })
|
||
React.useEffect(() => {
|
||
let alive = true
|
||
let timer
|
||
const tick = async () => {
|
||
try {
|
||
const response = await fetch('/sansheng/state', { cache: 'no-cache' })
|
||
if (!response.ok) throw new Error('HTTP ' + response.status)
|
||
const data = await response.json()
|
||
if (alive) setState({ loading: false, error: null, data })
|
||
} catch (error) {
|
||
if (alive) setState(prev => ({ loading: false, error: String((error && error.message) || error), data: prev.data }))
|
||
} finally {
|
||
if (alive) timer = setTimeout(tick, POLL_MS)
|
||
}
|
||
}
|
||
tick()
|
||
return () => {
|
||
alive = false
|
||
if (timer) clearTimeout(timer)
|
||
}
|
||
}, [])
|
||
return state
|
||
}
|
||
|
||
function RoleCard({ project, card }) {
|
||
const model = card.model ? String(card.model) : '默认模型'
|
||
return h('div', {
|
||
className: 'ss-card',
|
||
'data-role': card.role,
|
||
'data-tone': card.tone,
|
||
title: [card.duty, card.detail].filter(Boolean).join('\n'),
|
||
}, [
|
||
h('div', { key: 'head', className: 'ss-card-head' }, [
|
||
h('span', { key: 'mark', className: 'ss-mark', 'aria-hidden': 'true', children: ROLE_MARK[card.role] }),
|
||
h('strong', { key: 'label', children: card.label }),
|
||
h('span', { key: 'dot', className: 'ss-dot', 'aria-hidden': 'true' }),
|
||
]),
|
||
h('div', { key: 'duty', className: 'ss-duty', children: `${card.name || '未就绪'} · ${model}` }),
|
||
h('div', { key: 'main', className: 'ss-main', children: card.line }),
|
||
card.detail ? h('div', { key: 'sub', className: 'ss-sub', children: card.detail }) : null,
|
||
h('div', { key: 'foot', className: 'ss-foot' }, [
|
||
h('span', { key: 'duty', children: card.duty }),
|
||
h('span', { key: 'proj', className: 'ss-chip', children: project.name }),
|
||
]),
|
||
])
|
||
}
|
||
|
||
function EmptyHint({ error }) {
|
||
return h('div', { className: 'ss-empty' }, [
|
||
error ? h('div', { key: 'error', className: 'ss-error', children: `读不到项目状态:${error}` }) : null,
|
||
h('div', { key: 'text', children: '还没有三省六部项目。在对话里让助手调用 sansheng_provision 创建项目(三 Bot + 项目群)。' }),
|
||
h('div', { key: 'tip', children: '已有的群用 sansheng_bind 绑定;创建后可在这里看到丞相、尚书、工部三张卡片。' }),
|
||
])
|
||
}
|
||
|
||
function RolesColumn() {
|
||
const { loading, error, data } = useSanshengState()
|
||
const projects = (data && data.projects) || []
|
||
const [activeId, setActiveId] = React.useState(() => {
|
||
try { return window.localStorage.getItem(ACTIVE_KEY) || '' } catch (e) { return '' }
|
||
})
|
||
const project = projects.find(item => item.id === activeId) || projects[0]
|
||
React.useEffect(() => {
|
||
if (project && project.id !== activeId) {
|
||
setActiveId(project.id)
|
||
try { window.localStorage.setItem(ACTIVE_KEY, project.id) } catch (e) { /* 忽略 */ }
|
||
}
|
||
}, [project && project.id])
|
||
const attention = projects.reduce((sum, item) => sum + item.roles.filter(card => card.tone === 'warn' || card.tone === 'attention').length, 0)
|
||
const onPick = event => {
|
||
const next = event.target.value
|
||
setActiveId(next)
|
||
try { window.localStorage.setItem(ACTIVE_KEY, next) } catch (e) { /* 忽略 */ }
|
||
}
|
||
return h('div', { className: 'ss-root', 'data-sansheng-roles': '1' }, [
|
||
h('div', { key: 'bar', className: 'ss-bar' }, [
|
||
projects.length > 1
|
||
? h('select', { key: 'select', value: project ? project.id : '', onChange: onPick, 'aria-label': '选择项目' },
|
||
projects.map(item => h('option', { key: item.id, value: item.id, children: item.name })))
|
||
: h('strong', { key: 'name', children: project ? project.name : (loading ? '载入中…' : '三省六部') }),
|
||
h('span', { key: 'note', className: 'ss-bar-note', children: attention > 0 ? `待处理 ${attention}` : '就绪' }),
|
||
]),
|
||
h('div', { key: 'list', className: 'ss-list' },
|
||
project
|
||
? [
|
||
...project.roles.map(card => h(RoleCard, { key: card.role, project, card })),
|
||
error ? h('div', { key: 'err', className: 'ss-error', children: `状态刷新失败:${error}` }) : null,
|
||
]
|
||
: [h(EmptyHint, { key: 'empty', error: error })]),
|
||
])
|
||
}
|
||
|
||
function RolesTabTitle() {
|
||
const { data } = useSanshengState()
|
||
const projects = (data && data.projects) || []
|
||
const attention = projects.reduce((sum, item) => sum + item.roles.filter(card => card.tone === 'warn').length, 0)
|
||
return h('div', { className: 'ss-tab-title' }, [
|
||
h('span', { key: 'mark', className: 'ss-tab-mark', children: '三省' }),
|
||
h('span', { key: 'label', children: '角色' }),
|
||
attention > 0 ? h('span', { key: 'count', className: 'ss-tab-count', children: String(attention) }) : null,
|
||
])
|
||
}
|
||
|
||
function HeaderButton(props) {
|
||
const ctx = props.__ctx
|
||
const sessionId = props && props.sessionId ? props.sessionId : undefined
|
||
const [notice, setNotice] = React.useState(null)
|
||
return h('button', {
|
||
className: 'ss-open',
|
||
type: 'button',
|
||
title: notice || '打开右侧三省六部角色卡片',
|
||
onClick: () => {
|
||
const result = openRolesTab(ctx, sessionId)
|
||
setNotice(result.ok ? null : result.error)
|
||
},
|
||
}, [h('span', { key: 'mark', children: '三省' }), h('span', { key: 'text', children: notice ? '右栏不可用' : '角色' })])
|
||
}
|
||
|
||
function apply(ctx) {
|
||
ensureCss()
|
||
const wrap = component => props => h(component, { ...props, __ctx: ctx })
|
||
|
||
ctx.effect(() => {
|
||
try {
|
||
return ctx.sidebarRightTabs.register({
|
||
id: TAB_ID,
|
||
kind: TAB_KIND,
|
||
priority: 'builtin',
|
||
title: () => '三省六部',
|
||
keepMounted: true,
|
||
})
|
||
} catch (error) {
|
||
console.warn('[三省六部] 右栏页签注册失败:', error)
|
||
return () => {}
|
||
}
|
||
}, '三省六部:右栏角色页签')
|
||
|
||
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register({
|
||
name: 'sidebar.right.pane.tab',
|
||
key: TAB_ID,
|
||
}, wrap(RolesColumn)))
|
||
|
||
ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register({
|
||
name: 'sidebar.right.pane.tab.title',
|
||
key: TAB_ID,
|
||
}, () => h(RolesTabTitle, null)))
|
||
|
||
ctx.slots.inject('conversation.session.header.actions', () => ctx.slots.register({
|
||
name: 'conversation.session.header.actions',
|
||
id: TAB_ID + ':open',
|
||
order: 60,
|
||
}, wrap(HeaderButton)))
|
||
}
|
||
|
||
exports.apply = apply
|
||
exports.inject = ['slots', 'sidebarRightTabs', 'sidebarRight']
|
||
exports.TAB_ID = TAB_ID
|
||
exports.TAB_KIND = TAB_KIND
|
||
return module.exports
|
||
},
|
||
})
|