🐛 fix(client): 后台判定改为现读可见性,补投递诊断
This commit is contained in:
@@ -73,7 +73,8 @@ window.__ModuleLoader__.load({
|
||||
'settings.permission.hint.mac': '系统设置 → 通知 → DeepSeek Harness',
|
||||
'settings.permission.hint.linux': '在桌面环境的通知设置(GNOME / KDE)中允许 DeepSeek Harness',
|
||||
'settings.action.allow': '允许通知',
|
||||
'settings.action.test': '测试通知',
|
||||
'settings.action.testSystem': '测试系统通知',
|
||||
'settings.action.testAny': '测试提醒',
|
||||
'settings.action.unsupported': '不可用',
|
||||
'settings.permission.promptFailed': '无法请求通知权限:{message}',
|
||||
'settings.health.watching': '正在监听会话状态',
|
||||
@@ -89,6 +90,17 @@ window.__ModuleLoader__.load({
|
||||
'config.section.status': '运行状态',
|
||||
'config.intro': '提醒都从当前页面发出:窗口不在前台时是系统通知,窗口在前台时是这里的轻弹窗。',
|
||||
'config.saveNote': '开关保存在浏览器本地,重装插件不会丢失。',
|
||||
'config.testHint': '这里的按钮直接走系统通知通道,用来验证系统通知本身是否可用(窗口在前台也照发)。',
|
||||
'config.testAny': '测试提醒',
|
||||
'config.testSystem': '测试系统通知',
|
||||
'config.diag.window': '窗口状态',
|
||||
'config.diag.inFront': '在前台(会走轻弹窗)',
|
||||
'config.diag.away': '不在前台(会走系统通知)',
|
||||
'config.diag.last': '最近一次投递',
|
||||
'config.diag.none': '还没有投递过',
|
||||
'config.diag.system': '系统通知',
|
||||
'config.diag.popup': '应用内轻弹窗',
|
||||
'config.diag.refused': '系统通知被拒绝,改用了轻弹窗',
|
||||
}
|
||||
const en = {
|
||||
'notification.completion': 'Conversation finished',
|
||||
@@ -113,7 +125,8 @@ window.__ModuleLoader__.load({
|
||||
'settings.permission.hint.mac': 'System Settings → Notifications → DeepSeek Harness',
|
||||
'settings.permission.hint.linux': 'Allow DeepSeek Harness in your desktop notification settings (GNOME / KDE)',
|
||||
'settings.action.allow': 'Allow notifications',
|
||||
'settings.action.test': 'Test notification',
|
||||
'settings.action.testSystem': 'Test system notification',
|
||||
'settings.action.testAny': 'Test alert',
|
||||
'settings.action.unsupported': 'Unavailable',
|
||||
'settings.permission.promptFailed': 'Could not request notification permission: {message}',
|
||||
'settings.health.watching': 'Watching session status',
|
||||
@@ -129,6 +142,17 @@ window.__ModuleLoader__.load({
|
||||
'config.section.status': 'Status',
|
||||
'config.intro': 'Every alert comes from this page: a system notification while the window is in the background, the light popup here while it is in the foreground.',
|
||||
'config.saveNote': 'The switches are stored in this browser and survive reinstalling the plugin.',
|
||||
'config.testHint': 'The buttons here go straight to the system channel, so you can verify it on its own (they fire even while the window is in front).',
|
||||
'config.testAny': 'Test alert',
|
||||
'config.testSystem': 'Test system notification',
|
||||
'config.diag.window': 'Window',
|
||||
'config.diag.inFront': 'in front (popup is used)',
|
||||
'config.diag.away': 'not in front (system notification is used)',
|
||||
'config.diag.last': 'Last delivery',
|
||||
'config.diag.none': 'nothing delivered yet',
|
||||
'config.diag.system': 'system notification',
|
||||
'config.diag.popup': 'in-app popup',
|
||||
'config.diag.refused': 'system channel refused, popup was used instead',
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,13 +333,33 @@ window.__ModuleLoader__.load({
|
||||
return 'default'
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the Harness window is somewhere the user cannot see the app.
|
||||
*
|
||||
* Two independent browser facts are consulted, and ANY of them counts as "not
|
||||
* in the foreground": the page's visibility and the document's focus. They are
|
||||
* read at delivery time rather than remembered from an event, because a missed
|
||||
* blur in the desktop shell made this plugin believe the window was in front
|
||||
* and swallow the alert.
|
||||
* @returns whether the alert must go to the system notification channel.
|
||||
*/
|
||||
function windowIsAway() {
|
||||
try {
|
||||
if (document.visibilityState === 'hidden') return true
|
||||
} catch {
|
||||
/* an unreadable visibility state leaves the focus fact */
|
||||
}
|
||||
try {
|
||||
if (typeof document.hasFocus === 'function') return !document.hasFocus()
|
||||
} catch {
|
||||
/* an unreadable focus state leaves the default below */
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** Read the window's live focus state rather than a cached value. */
|
||||
function currentFocus() {
|
||||
try {
|
||||
return typeof document.hasFocus === 'function' ? document.hasFocus() : true
|
||||
} catch {
|
||||
return true
|
||||
}
|
||||
return !windowIsAway()
|
||||
}
|
||||
|
||||
/** Focus the window without letting a refusal stop the navigation. */
|
||||
@@ -504,9 +548,10 @@ window.__ModuleLoader__.load({
|
||||
toasts: [],
|
||||
promptError: '',
|
||||
health: { state: 'starting', message: '' },
|
||||
lastDelivery: null,
|
||||
})
|
||||
const notify = (message) => {
|
||||
report(`session-notify: ${message}`)
|
||||
report(message)
|
||||
}
|
||||
/** Every component translates through this: the slot's own seat when the owner supplies one, else the locale service. */
|
||||
const seatFor = (props) => (typeof props.t === 'function' ? props.t : t)
|
||||
@@ -521,7 +566,6 @@ window.__ModuleLoader__.load({
|
||||
live: {
|
||||
list: undefined,
|
||||
status: undefined,
|
||||
focus: true,
|
||||
},
|
||||
}
|
||||
const timers = new Map()
|
||||
@@ -632,12 +676,15 @@ window.__ModuleLoader__.load({
|
||||
}
|
||||
}
|
||||
|
||||
/** Raise one system notification, tolerating every refusal a browser may give. */
|
||||
/**
|
||||
* Raise one system notification, tolerating every refusal a browser may give.
|
||||
* @returns the outcome the config page reports, so a refusal is never silent.
|
||||
*/
|
||||
const systemNotify = (candidate, force) => {
|
||||
const Ctor = globalThis.Notification
|
||||
const permission = notificationPermission()
|
||||
if (permission === 'unsupported') return false
|
||||
if (!force && permission !== 'granted') return false
|
||||
if (permission === 'unsupported') return 'unsupported'
|
||||
if (!force && permission !== 'granted') return 'permission'
|
||||
try {
|
||||
const notice = copyFor(candidate)
|
||||
const options = {
|
||||
@@ -656,10 +703,10 @@ window.__ModuleLoader__.load({
|
||||
/* close is best effort on every platform */
|
||||
}
|
||||
}
|
||||
return true
|
||||
return 'raised'
|
||||
} catch (error) {
|
||||
notify(`system notification failed: ${text(error)}`)
|
||||
return false
|
||||
return 'threw'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -675,6 +722,25 @@ window.__ModuleLoader__.load({
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Record what actually happened to the newest alert.
|
||||
*
|
||||
* Everything on the delivery path used to fail invisibly: the alert simply
|
||||
* never appeared, with no way to tell a wrong channel decision from a
|
||||
* refused constructor. The config page shows this record, so the next
|
||||
* question ("did it even try?") has an answer.
|
||||
*/
|
||||
const recordDelivery = (outcome, candidate) => {
|
||||
store.set({
|
||||
...store.getSnapshot(),
|
||||
lastDelivery: {
|
||||
outcome,
|
||||
at: new Date().toISOString().slice(11, 19),
|
||||
title: copyFor(candidate).title,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a queued candidate is still worth delivering. Read from the LIVE
|
||||
* snapshot rather than the one captured when the transition was seen: a
|
||||
@@ -694,9 +760,12 @@ window.__ModuleLoader__.load({
|
||||
|
||||
/**
|
||||
* Deliver one candidate through exactly one channel: the system
|
||||
* notification while the window is in the background, the light popup while
|
||||
* it is in the foreground, and nothing at all for the conversation on
|
||||
* screen.
|
||||
* notification while the window is away, the light popup while it is in
|
||||
* front, and nothing at all for the conversation on screen.
|
||||
*
|
||||
* The channel decision reads the window state right here, at delivery time,
|
||||
* instead of trusting the state the observer last remembered — that is what
|
||||
* an unreported blur used to defeat.
|
||||
*/
|
||||
const deliver = (candidate, ignoreThrottle) => {
|
||||
if (ignoreThrottle !== true) {
|
||||
@@ -704,12 +773,21 @@ window.__ModuleLoader__.load({
|
||||
if (now - state.lastDelivery < THROTTLE_MS) return
|
||||
state.lastDelivery = now
|
||||
}
|
||||
if (state.live.focus) {
|
||||
if (!isOnScreen(candidate.sessionId, state.live.list)) showToast(candidate)
|
||||
if (!windowIsAway()) {
|
||||
if (isOnScreen(candidate.sessionId, state.live.list)) return
|
||||
showToast(candidate)
|
||||
recordDelivery('popup', candidate)
|
||||
return
|
||||
}
|
||||
if (systemNotify(candidate, candidate.test === true)) return
|
||||
if (candidate.test === true) showToast(candidate)
|
||||
const outcome = systemNotify(candidate, candidate.test === true)
|
||||
if (outcome === 'raised') {
|
||||
recordDelivery('system', candidate)
|
||||
return
|
||||
}
|
||||
// A test alert still has to reach the user, and so does a real one when
|
||||
// the system channel refuses: the popup is the channel that remains.
|
||||
showToast(candidate)
|
||||
recordDelivery(`system-refused-${outcome}`, candidate)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -736,11 +814,13 @@ window.__ModuleLoader__.load({
|
||||
* One derivation pass over the client's own session state: remember the
|
||||
* freshest snapshot for the settle tick, compare it against what this page
|
||||
* already observed, emit at most one candidate per transition, and let the
|
||||
* settle tick decide delivery.
|
||||
* settle tick decide delivery. The window state is deliberately NOT captured
|
||||
* here — the delivery reads it fresh, because it can change inside the
|
||||
* settle window.
|
||||
*/
|
||||
const observe = (focus, list, status) => {
|
||||
const observe = (list, status) => {
|
||||
if (list === undefined || status === undefined) return
|
||||
state.live = { list, status, focus }
|
||||
state.live = { list, status }
|
||||
const kinds = store.getSnapshot().kinds
|
||||
for (const sessionId of Object.keys(list.byId ?? {})) {
|
||||
const summary = list.byId[sessionId]
|
||||
@@ -800,18 +880,36 @@ window.__ModuleLoader__.load({
|
||||
|
||||
// --------------------------------------------------------------- actions
|
||||
|
||||
/** Raise one test notification through the channel the current state selects. */
|
||||
/** Build one test alert that skips the throttle and the "on screen" rule. */
|
||||
const testCandidate = (title) => ({
|
||||
kind: 'completion',
|
||||
sessionId: 'test',
|
||||
title,
|
||||
detail: '',
|
||||
pendingKind: '',
|
||||
test: true,
|
||||
})
|
||||
|
||||
/**
|
||||
* Raise one test alert through whichever channel the window state selects.
|
||||
* Verified from the config page, where the user is looking at the app, so it
|
||||
* normally lands in the popup — the way to prove the system channel is to
|
||||
* press the button in a channel-specific test instead.
|
||||
*/
|
||||
const sendTest = () => {
|
||||
const candidate = {
|
||||
kind: 'completion',
|
||||
sessionId: 'test',
|
||||
title: t('settings.title'),
|
||||
detail: '',
|
||||
pendingKind: '',
|
||||
test: true,
|
||||
deliver(testCandidate(t('config.testAny')), true)
|
||||
}
|
||||
|
||||
/** Raise the test alert on the system channel specifically, whatever the focus is. */
|
||||
const sendTestSystem = () => {
|
||||
const candidate = testCandidate(t('config.testSystem'))
|
||||
const outcome = systemNotify(candidate, true)
|
||||
if (outcome === 'raised') {
|
||||
recordDelivery('system', candidate)
|
||||
return
|
||||
}
|
||||
if (!currentFocus() && notificationPermission() === 'granted' && systemNotify(candidate, true)) return
|
||||
showToast(candidate)
|
||||
recordDelivery(`system-refused-${outcome}`, candidate)
|
||||
}
|
||||
|
||||
/** Request notification permission inside a user gesture, then report the outcome. */
|
||||
@@ -854,21 +952,27 @@ window.__ModuleLoader__.load({
|
||||
return React.useSyncExternalStore(source.subscribe, source.getSnapshot, source.getSnapshot)
|
||||
}
|
||||
|
||||
/** The window's focus state as a React value, kept current by the DOM's own events. */
|
||||
function useFocus() {
|
||||
/**
|
||||
* Subscribe to every browser signal that says whether the app is visible:
|
||||
* the window's focus events and the document's visibility change. The value
|
||||
* itself is a boolean, so the observer re-renders once per transition.
|
||||
*/
|
||||
function useWindowState() {
|
||||
const subscribe = React.useCallback((listener) => {
|
||||
try {
|
||||
window.addEventListener('focus', listener)
|
||||
window.addEventListener('blur', listener)
|
||||
document.addEventListener('visibilitychange', listener)
|
||||
} catch {
|
||||
return () => {}
|
||||
}
|
||||
return () => {
|
||||
window.removeEventListener('focus', listener)
|
||||
window.removeEventListener('blur', listener)
|
||||
document.removeEventListener('visibilitychange', listener)
|
||||
}
|
||||
}, [])
|
||||
return React.useSyncExternalStore(subscribe, currentFocus, () => true)
|
||||
return React.useSyncExternalStore(subscribe, () => !windowIsAway(), () => true)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -876,9 +980,13 @@ window.__ModuleLoader__.load({
|
||||
* how a plugin reaches the session hooks without occupying visible UI. It
|
||||
* renders `null`, and any failure in its own derivation is contained here so
|
||||
* a slot entry can never crash.
|
||||
*
|
||||
* It subscribes to the window state only so a foreground/background change
|
||||
* re-derives promptly; the delivery itself re-reads that state, so a missed
|
||||
* event can no longer pick the wrong channel.
|
||||
*/
|
||||
function NotifyObserver(props) {
|
||||
const focus = useFocus()
|
||||
useWindowState()
|
||||
const useSessions = props.useSessions
|
||||
const useSessionStatus = props.useSessionStatus
|
||||
const select = React.useCallback((snapshot) => snapshot, [])
|
||||
@@ -894,7 +1002,7 @@ window.__ModuleLoader__.load({
|
||||
applyHealth(store, { state: 'starting', message: '' })
|
||||
return
|
||||
}
|
||||
observe(focus, list, status)
|
||||
observe(list, status)
|
||||
applyHealth(store, { state: 'watching', message: '' })
|
||||
} catch (error) {
|
||||
applyHealth(store, { state: 'error', message: text(error) })
|
||||
@@ -1037,18 +1145,38 @@ window.__ModuleLoader__.load({
|
||||
type: 'button',
|
||||
className: 'dsn-button',
|
||||
disabled: !supported,
|
||||
onClick: permission === 'granted' ? props.test : props.ask,
|
||||
onClick: permission === 'granted' ? props.testSystem : props.ask,
|
||||
}, tr(!supported
|
||||
? 'settings.action.unsupported'
|
||||
: permission === 'granted' ? 'settings.action.test' : 'settings.action.allow')))),
|
||||
: permission === 'granted' ? 'settings.action.testSystem' : 'settings.action.allow'))),
|
||||
h('p', { className: 'dsn-section-desc' }, tr('config.testHint'))),
|
||||
h('section', { className: 'dsn-section' },
|
||||
h('h3', { className: 'dsn-section-title' }, tr('config.section.status')),
|
||||
h('div', { className: 'dsn-row' },
|
||||
h('div', { className: 'dsn-row-text' },
|
||||
h('p', { className: 'dsn-status' }, `${tr('config.diag.window')}: ${tr(windowIsAway() ? 'config.diag.away' : 'config.diag.inFront')}`),
|
||||
h('p', {
|
||||
className: `dsn-status${healthFailed ? ' dsn-status-error' : ''}`,
|
||||
role: healthFailed ? 'alert' : 'status',
|
||||
}, healthText))))))
|
||||
}, healthText),
|
||||
h('p', { className: 'dsn-status' }, deliveryText(tr))),
|
||||
h('button', {
|
||||
type: 'button',
|
||||
className: 'dsn-button',
|
||||
onClick: props.testAny,
|
||||
}, tr('settings.action.testAny'))))))
|
||||
}
|
||||
|
||||
/** Describe the newest alert's fate in one line. */
|
||||
function deliveryText(tr) {
|
||||
const last = store.getSnapshot().lastDelivery
|
||||
if (last === null || last === undefined) return tr('config.diag.none')
|
||||
const outcome = last.outcome.startsWith('system-refused')
|
||||
? tr('config.diag.refused')
|
||||
: last.outcome === 'system'
|
||||
? tr('config.diag.system')
|
||||
: tr('config.diag.popup')
|
||||
return `${tr('config.diag.last')}: ${last.at} · ${outcome} · ${last.title}`
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------- registration
|
||||
@@ -1116,7 +1244,8 @@ window.__ModuleLoader__.load({
|
||||
store,
|
||||
platform: platformKey(),
|
||||
ask: requestPermission,
|
||||
test: sendTest,
|
||||
testAny: sendTest,
|
||||
testSystem: sendTestSystem,
|
||||
setKind: setKindEnabled,
|
||||
}),
|
||||
}, ConfigSection))
|
||||
|
||||
Reference in New Issue
Block a user