diff --git a/console-ui/src/pages/AmbientGroupPicker.tsx b/console-ui/src/pages/AmbientGroupPicker.tsx index 6c5b871..0555776 100644 --- a/console-ui/src/pages/AmbientGroupPicker.tsx +++ b/console-ui/src/pages/AmbientGroupPicker.tsx @@ -98,6 +98,7 @@ export function AmbientGroupPickerView({ groups, onChange, read }: { groups: str const outside = new Set(read.catalog ? groupsOutsideCatalog(groups, read.catalog) : groups) const listUsable = view.kind === 'ready' || view.kind === 'no-match' const groupListLabel = i18n.t("群列表") + const groupListError = (failure: { text: string; detail: string }) => failure.detail ? i18n.t("{0}读取失败:{1}", [groupListLabel, failure.detail]) : failure.text const addManual = () => { const result = addManualGroups(groups, manualText) @@ -140,7 +141,8 @@ export function AmbientGroupPickerView({ groups, onChange, read }: { groups: str /> {view.kind === 'loading' ? : null} {view.kind === 'denied' ?

{describeReadFailure(view.error, groupListLabel, i18n.locale).text} {i18n.t("可以在下面手动填写群号。")}

: null} - {view.kind === 'error' ? : null} + {/* The group picker keeps the reason inline: ErrorAlert has no 技术详情 (#64 ambient-read-error-raw covers status / settings only). */} + {view.kind === 'error' ? : null} {view.kind === 'empty' ?

{view.noOnlineAccount ? i18n.t("没有在线的账号,读不到群列表。可以在下面手动填写群号。") : i18n.t("在线账号里没有读到群。可以在下面手动填写群号。")}

: null} {view.kind === 'no-match' ?

{i18n.t("没有群名、群号或账号包含「{0}」的群。", [query.trim()])}

: null} {view.kind === 'ready' ?
diff --git a/console-ui/src/pages/ambient-engagement-model.d.mts b/console-ui/src/pages/ambient-engagement-model.d.mts index 9831b4e..2c70704 100644 --- a/console-ui/src/pages/ambient-engagement-model.d.mts +++ b/console-ui/src/pages/ambient-engagement-model.d.mts @@ -93,6 +93,8 @@ export function validateAmbientSettings(settings: AmbientSettings, locale?: 'zh- export function buildAmbientPatches(settings: AmbientSettings, options?: { mergedJudge?: boolean }): AmbientPatch[] export function describeSaveResult(result: unknown, locale?: 'zh-CN' | 'en'): SaveFeedback export function isPermissionDenied(error: unknown): boolean +/** 无权限:旧 Host 的拒绝文字,或 statusFromError 认出的 forbidden(状态、设置、群列表共用)。 */ +export function isReadDenied(error: unknown): boolean export function describeReadFailure(error: unknown, resource: string, locale?: 'zh-CN' | 'en'): { denied: boolean; text: string; detail: string } export interface AmbientGroupSummary { group: string; count: number; lastAt: number; lastText: string; outcomes: Array<{ outcome: AmbientOutcome; label: string; count: number }> } export function summarizeRecentByGroup(recent: AmbientStatusView['recent'], locale?: 'zh-CN' | 'en'): AmbientGroupSummary[] diff --git a/console-ui/src/pages/ambient-engagement-model.mjs b/console-ui/src/pages/ambient-engagement-model.mjs index a54d09d..2e7bc43 100644 --- a/console-ui/src/pages/ambient-engagement-model.mjs +++ b/console-ui/src/pages/ambient-engagement-model.mjs @@ -1,6 +1,7 @@ import { translate } from '../i18n/locale.mjs' import { configurationRuntimeStatus } from '../components/PluginConfigForm/config-live-status.mjs' import { outcomeFromConfigSave, readBackMismatch } from '../components/apply-outcome-model.mjs' +import { statusFromError } from '../components/status-state-model.mjs' /** * 群聊插话页(zhinjs/zhin#970)的纯逻辑:校验 Host 状态、把设置翻成大白话、生成 config:patch。 * 页面面向普通用户,这里不输出判官、决策模型、置信度等内部细节。 @@ -378,11 +379,23 @@ export function isPermissionDenied(error) { return PERMISSION_DENIED_TEXT.test(text.trim()) } -/** 读取失败时给用户看的提示:只有权限问题才说权限,其余带上真实原因。 */ +/** + * 群聊插话页的「无权限」判断(#64 `ambient-read-error-raw`):旧 Host 的拒绝文字(`isPermissionDenied`), + * 或 `statusFromError` 认出的无权限(Host 的 `forbidden` 码、401/403)。状态、设置和群列表都用它。 + */ +export function isReadDenied(error) { + return isPermissionDenied(error) || statusFromError(error).kind === 'forbidden' +} + +/** + * 读取失败时给用户看的提示(#64 `ambient-read-error-raw`):无权限按 `statusFromError` 判断(Host 的 + * `forbidden` 码、401/403、拒绝文字),与页面上 StatusState 的判断一致;其余只说哪一部分没读到, + * 不把 Host 原文拼进来——原因由 `StatusState error=` 按类型说明,原文只在 `detail`(技术详情)里。 + */ export function describeReadFailure(error, resource, locale = 'zh-CN') { const reason = (typeof error === 'string' ? error : error instanceof Error ? error.message : String(error ?? '')).trim() - if (isPermissionDenied(error)) return { denied: true, text: translate(locale, "当前账号没有读取{0}的权限,请联系管理员开通。", [resource]), detail: reason } - return { denied: false, text: reason ? translate(locale, "{0}读取失败:{1}", [resource, reason]) : translate(locale, "{0}读取失败,请稍后重试。", [resource]), detail: reason } + if (isReadDenied(error)) return { denied: true, text: translate(locale, "当前账号没有读取{0}的权限,请联系管理员开通。", [resource]), detail: reason } + return { denied: false, text: translate(locale, "{0}读取失败,请稍后重试。", [resource]), detail: reason } } const SHORT_OUTCOME_LABELS = Object.freeze({ spoke: '接了话', watched: '想接话', stayed_quiet: '不接话', skipped: '未判断' }) diff --git a/console-ui/src/pages/ambient-engagement.tsx b/console-ui/src/pages/ambient-engagement.tsx index 7e44633..3485e64 100644 --- a/console-ui/src/pages/ambient-engagement.tsx +++ b/console-ui/src/pages/ambient-engagement.tsx @@ -38,7 +38,7 @@ import { describeSaveResult, ambientSaveOutcome, ambientRestartOutcome, - isPermissionDenied, + isReadDenied, AMBIENT_STATUS_PERMISSION, readAmbientSettings, summarizeRecentByGroup, @@ -72,7 +72,7 @@ async function readAmbientStatus(locale: 'zh-CN' | 'en' = 'zh-CN'): Promise} /> const failure = read.error ? describeReadFailure(read.error, i18n.t("群聊插话状态"), i18n.locale) : null - if (failure && !read.loaded && !failure.denied) return + if (failure && !read.loaded && !failure.denied) return if (!read.data && !failure?.denied) return null if (!read.data || read.data.denied) { return @@ -167,7 +167,7 @@ function AmbientStatusSection({ read, onRetry }: { read: StatusReadState; onRetr {i18n.t("缓冲内观察:最近 24 小时、实例这次启动以来的记录,重启后重新计数。")} - {failure &&

{i18n.t("刷新失败,下面是上次读到的数据。")} {failure.text}

} + {failure && }
{view.counts.map((count) =>
{count.value}
@@ -298,7 +298,7 @@ function AmbientSettingsSection({ read, onReload, currentReadEpoch, focusGroup } {failure.denied ?

{i18n.t("当前账号不能查看或修改群聊插话设置,只有管理员账号可以修改。")}

- : { void onReload().catch(() => {}) }} />} + : { void onReload().catch(() => {}) }} />}
} diff --git a/console-ui/src/pages/ambient-group-picker-model.mjs b/console-ui/src/pages/ambient-group-picker-model.mjs index 9188c73..75b662c 100644 --- a/console-ui/src/pages/ambient-group-picker-model.mjs +++ b/console-ui/src/pages/ambient-group-picker-model.mjs @@ -1,5 +1,5 @@ import { translate } from '../i18n/locale.mjs' -import { isPermissionDenied, parseGroupList } from './ambient-engagement-model.mjs' +import { isReadDenied, parseGroupList } from './ambient-engagement-model.mjs' /** * 群聊插话白名单的群选择器:把各在线账号的群列表(endpoint.groups)整理成按平台/账号分组的目录, @@ -150,11 +150,11 @@ export function addManualGroups(selected, text) { * 读取失败(包括所有在线账号都失败)绝不显示成「没有群」。 */ export function groupPickerView({ loading, error, catalog, query }) { - if (error) return { kind: isPermissionDenied(error) ? 'denied' : 'error', error: String(error) } + if (error) return { kind: isReadDenied(error) ? 'denied' : 'error', error: String(error) } if (loading || !catalog) return { kind: 'loading' } const hasGroups = catalog.sections.some((section) => section.groups.length > 0) if (!hasGroups && catalog.failures.length > 0) { - const denied = catalog.failures.every((failure) => isPermissionDenied(failure.error)) + const denied = catalog.failures.every((failure) => isReadDenied(failure.error)) return { kind: denied ? 'denied' : 'error', error: catalog.failures.map((failure) => failure.error).join('\n') } } if (!hasGroups) return { kind: 'empty', noOnlineAccount: catalog.sections.length === 0 } diff --git a/tests/ambient-group-picker-ui.test.mjs b/tests/ambient-group-picker-ui.test.mjs index b4bd79b..3a9fb1d 100644 --- a/tests/ambient-group-picker-ui.test.mjs +++ b/tests/ambient-group-picker-ui.test.mjs @@ -164,7 +164,8 @@ test('a status read failure shows the real reason and retry', async () => { const tree = module.AmbientStatusSection({ read: { loading: false, loaded: false, error: 'HTTP 500', data: null }, onRetry: () => { retried++ } }) assert.equal(tree.type, 'status-state') assert.equal(tree.props.error, 'HTTP 500') - assert.equal(tree.props.description, '群聊插话状态读取失败:HTTP 500') + // #64 ambient-read-error-raw: StatusState explains the cause; the raw text stays in its 技术详情. + assert.equal(tree.props.description, undefined) tree.props.onRetry(); assert.equal(retried, 1) }) diff --git a/tests/ambient-group-picker.test.mjs b/tests/ambient-group-picker.test.mjs index 34ba498..4ac8bc2 100644 --- a/tests/ambient-group-picker.test.mjs +++ b/tests/ambient-group-picker.test.mjs @@ -67,7 +67,9 @@ test('only 401/403 count as a permission problem; other failures keep their real } const failure = describeReadFailure('HTTP 502', '群聊插话设置') assert.equal(failure.denied, false) - assert.equal(failure.text, '群聊插话设置读取失败:HTTP 502') + // #64 ambient-read-error-raw: the Host text stays in detail (技术详情), not in the summary. + assert.equal(failure.text, '群聊插话设置读取失败,请稍后重试。') + assert.equal(failure.detail, 'HTTP 502') assert.doesNotMatch(failure.text, /权限|管理员/) assert.match(describeReadFailure('Token scope does not allow this route', '群列表').text, /没有读取群列表的权限/) assert.equal(describeReadFailure(new Error('boom'), '群列表').detail, 'boom') diff --git a/tests/ambient-read-error-raw.test.mjs b/tests/ambient-read-error-raw.test.mjs new file mode 100644 index 0000000..be3332c --- /dev/null +++ b/tests/ambient-read-error-raw.test.mjs @@ -0,0 +1,42 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import { describeReadFailure, isReadDenied } from '../console-ui/src/pages/ambient-engagement-model.mjs' +import { groupPickerView } from '../console-ui/src/pages/ambient-group-picker-model.mjs' + +// #64 `ambient-read-error-raw`: 群聊插话 status / settings read failures pasted the Host's raw text into the +// StatusState description (「群聊插话状态读取失败:Unknown RPC type: …」), overriding the cause-based title and +// text, and a 403 `{ code: 'forbidden' }` on settings was not treated as 无权限. +const errorWith = (message, details) => Object.assign(new Error(message), details ? { details } : {}) + +test('the summary never carries the Host text; it stays in detail for 技术详情', () => { + for (const raw of ['TypeError: Cannot read properties of undefined', 'Unknown RPC type: plugin.root.ambient', 'HTTP 502']) { + const failure = describeReadFailure(new Error(raw), '群聊插话状态') + assert.equal(failure.denied, false, raw) + assert.doesNotMatch(failure.text, new RegExp(raw.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&'), 'u'), raw) + assert.equal(failure.detail, raw) + } +}) + +test('无权限 follows statusFromError, so a Host `forbidden` code or plain 「Forbidden」 is denied in both sections', () => { + assert.equal(describeReadFailure(errorWith('Forbidden', { serverCode: 'forbidden' }), '群聊插话设置').denied, true) + assert.equal(describeReadFailure('Forbidden', '群聊插话设置').denied, true) + assert.equal(describeReadFailure(errorWith('request refused', { httpStatus: 403, transport: 'refused' }), '群聊插话状态').denied, true) + assert.equal(describeReadFailure('Token scope does not allow this route', '群聊插话状态').denied, true, 'the old Host texts still count') + assert.equal(describeReadFailure(new Error('Unknown RPC type: plugin.root.ambient'), '群聊插话状态').denied, false) +}) + +test('both sections hand the error to StatusState without a hand-made description', () => { + const page = readFileSync(new URL('../console-ui/src/pages/ambient-engagement.tsx', import.meta.url), 'utf8') + assert.doesNotMatch(page, /description=\{failure\.text\}/u) + assert.equal((page.match(/ { + const forbidden = errorWith('Forbidden', { serverCode: 'forbidden' }) + assert.equal(isReadDenied(forbidden), true) + assert.equal(groupPickerView({ loading: false, error: forbidden, catalog: null, query: '' }).kind, 'denied') + assert.equal(groupPickerView({ loading: false, error: new Error('REST request failed'), catalog: null, query: '' }).kind, 'error') + const page = readFileSync(new URL('../console-ui/src/pages/ambient-engagement.tsx', import.meta.url), 'utf8') + assert.match(page, /if \(isReadDenied\(error\)\) return \{ denied: true \}/u, 'the status read treats a forbidden code as denied too') +})