diff --git a/console-ui/src/i18n/messages.en.mjs b/console-ui/src/i18n/messages.en.mjs index 35108035..e2e62f03 100644 --- a/console-ui/src/i18n/messages.en.mjs +++ b/console-ui/src/i18n/messages.en.mjs @@ -1127,6 +1127,8 @@ export const englishMessages = Object.freeze({ "聊天记录": "Chat history", "按聊天账号和会话查看机器人和大家聊过什么(只读)。": "View conversations by account and chat (read-only).", "按聊天账号和会话查看机器人和大家聊过什么,也可以接着聊。": "View conversations by account and chat, and pick one up again.", + "按聊天账号和会话查看机器人和大家聊过什么,可以接着聊,也可以从其他消息继续。": "View conversations by account and chat, pick one up again, or continue from another message.", + "按聊天账号和会话查看机器人和大家聊过什么,也可以从其他消息继续。": "View conversations by account and chat, and continue from another message.", "活跃叶节点:": "Active leaf:", "默认(最后一条)": "Default (latest message)", "当前活跃": "Currently active", diff --git a/console-ui/src/pages/agent-sessions-description.d.mts b/console-ui/src/pages/agent-sessions-description.d.mts new file mode 100644 index 00000000..e18d6dcf --- /dev/null +++ b/console-ui/src/pages/agent-sessions-description.d.mts @@ -0,0 +1,2 @@ +/** The 聊天记录 page description for what this identity can do (Chinese source text; translate it). */ +export function sessionsPageDescription(abilities: { canChat: boolean; canBranch: boolean }): string diff --git a/console-ui/src/pages/agent-sessions-description.mjs b/console-ui/src/pages/agent-sessions-description.mjs new file mode 100644 index 00000000..2be1ac7e --- /dev/null +++ b/console-ui/src/pages/agent-sessions-description.mjs @@ -0,0 +1,11 @@ +/** + * 聊天记录页的页面说明(#64 `sessions-description-permission-mismatch`)。两种能力分开说: + * `canChat` 能回到会话里接着聊(endpoint.operate),`canBranch` 能从其他消息继续(POST + * /agent/sessions//leaf,按协议表查)。两样都没有时才说「只读」。返回中文原文,由页面翻译。 + */ +export function sessionsPageDescription({ canChat, canBranch }) { + if (canChat && canBranch) return '按聊天账号和会话查看机器人和大家聊过什么,可以接着聊,也可以从其他消息继续。' + if (canChat) return '按聊天账号和会话查看机器人和大家聊过什么,也可以接着聊。' + if (canBranch) return '按聊天账号和会话查看机器人和大家聊过什么,也可以从其他消息继续。' + return '按聊天账号和会话查看机器人和大家聊过什么(只读)。' +} diff --git a/console-ui/src/pages/agent-sessions.tsx b/console-ui/src/pages/agent-sessions.tsx index 2851e118..7103069a 100644 --- a/console-ui/src/pages/agent-sessions.tsx +++ b/console-ui/src/pages/agent-sessions.tsx @@ -22,6 +22,7 @@ import { AgentSessionPicker } from '../components/AgentSessionPicker' import { CONSOLE_REST } from '../contracts/zhin-console' import { hasConsolePermission } from '../utils/console-access.mjs' import { httpRouteAccess } from '../utils/http-permission.mjs' +import { sessionsPageDescription } from './agent-sessions-description.mjs' import { permissionLabel } from './access/access-model.mjs' import { readRecentSessions, sessionsNeedingGroupNames, summarizeRecentSessions, withGroupNames, type RecentSession, type RecentSessionSummary } from './agent-session-recent.mjs' import { useAmbientGroupCatalog } from './AmbientGroupPicker' @@ -293,7 +294,7 @@ export default function AgentSessionsPage() {
{!personal && recentResult ? ( diff --git a/tests/conversation-page.test.mjs b/tests/conversation-page.test.mjs index 75d3528a..711594c5 100644 --- a/tests/conversation-page.test.mjs +++ b/tests/conversation-page.test.mjs @@ -6,6 +6,7 @@ import ts from 'typescript' import { localeModule, isLocaleImport } from './locale-fixture.mjs' import * as personalModels from '../console-ui/src/pages/personal-session-model.mjs' import * as httpPermission from '../console-ui/src/utils/http-permission.mjs' +import * as sessionsDescription from '../console-ui/src/pages/agent-sessions-description.mjs' import * as accessModel from '../console-ui/src/pages/access/access-model.mjs' import { readRecentSessions, sessionsNeedingGroupNames, summarizeRecentSessions, withGroupNames } from '../console-ui/src/pages/agent-session-recent.mjs' import * as recordsModel from '../console-ui/src/pages/scope/records-model.mjs' @@ -52,7 +53,7 @@ function fixture(initialKey = 'icqq:bot:group:room', granted = null) { './SessionMessageDetails':{SessionMessageDetails:'message-details'}, './ConversationHistory':{ConversationHistory:'conversation-history'}, './ConversationTrace':{ConversationTrace:'conversation-trace'}, './useConversationTraces':{useConversationTraces:(...args)=>{ traceReads.push(args); return {turns:[],loading:false,error:null,refresh(){}} }}, '@zhin.js/client':{cn:(...parts)=>parts.filter(Boolean).join(' ')}, '../contracts/zhin-console':{CONSOLE_REST:{AGENT_SESSIONS:'/api/agent/sessions'}}, - '../utils/console-access.mjs':{hasConsolePermission:name=>!granted||granted.has(name)}, '../utils/http-permission.mjs':httpPermission, './access/access-model.mjs':accessModel, './agent-session-recent.mjs':{readRecentSessions,summarizeRecentSessions,sessionsNeedingGroupNames,withGroupNames}, './AmbientGroupPicker':{useAmbientGroupCatalog:()=>({loading:false,error:null,catalog:null,retry(){}})}, './ambient-group-picker-model.mjs':{groupNamesFromCatalog:()=>new Map()}, }) + '../utils/console-access.mjs':{hasConsolePermission:name=>!granted||granted.has(name)}, '../utils/http-permission.mjs':httpPermission, './agent-sessions-description.mjs':sessionsDescription, './access/access-model.mjs':accessModel, './agent-session-recent.mjs':{readRecentSessions,summarizeRecentSessions,sessionsNeedingGroupNames,withGroupNames}, './AmbientGroupPicker':{useAmbientGroupCatalog:()=>({loading:false,error:null,catalog:null,retry(){}})}, './ambient-group-picker-model.mjs':{groupNamesFromCatalog:()=>new Map()}, }) const f = {calls,traceReads,render(){cursor=0;tree=module.default(); const run=effects;effects=[];run.forEach(fn=>fn());return tree},get tree(){return tree},setScope(value){scope=value},navigate(key){params=new URLSearchParams({sessionKey:key})},close(){slots.forEach(slot=>slot?.cleanup?.())}} f.render();return f } @@ -300,3 +301,12 @@ test('「从这里继续」 follows the protocol (agent.configure), not endpoint assert.doesNotMatch(text(configurer.tree), /需要「修改 AI 与 Agent 设置」权限/) configurer.close() }) + +test('the page description follows both abilities: chatting (endpoint.operate) and continuing from another message (the leaf route)', () => { + // #64 sessions-description-permission-mismatch + const description = granted => { const f = fixture('icqq:bot:group:room', granted); const value = find(f, 'page-header').props.description; f.close(); return value } + assert.equal(description(new Set()), '按聊天账号和会话查看机器人和大家聊过什么(只读)。') + assert.equal(description(new Set(['endpoint.operate'])), '按聊天账号和会话查看机器人和大家聊过什么,也可以接着聊。') + assert.equal(description(new Set(['agent.configure'])), '按聊天账号和会话查看机器人和大家聊过什么,也可以从其他消息继续。') + assert.equal(description(new Set(['endpoint.operate', 'agent.configure'])), '按聊天账号和会话查看机器人和大家聊过什么,可以接着聊,也可以从其他消息继续。') +}) diff --git a/tests/sessions-description-permission-mismatch.test.mjs b/tests/sessions-description-permission-mismatch.test.mjs new file mode 100644 index 00000000..5ac511d6 --- /dev/null +++ b/tests/sessions-description-permission-mismatch.test.mjs @@ -0,0 +1,32 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import { httpRouteAccess } from '../console-ui/src/utils/http-permission.mjs' +import { sessionsPageDescription } from '../console-ui/src/pages/agent-sessions-description.mjs' +import { translate } from '../console-ui/src/i18n/locale.mjs' + +// #64 `sessions-description-permission-mismatch`: the 聊天记录 description followed endpoint.operate only, +// while 「从这里继续」 follows POST /agent/sessions//leaf (agent.configure in the protocol, #215). +const canBranch = permissions => httpRouteAccess('POST', '/agent/sessions/_/leaf', name => permissions.includes(name)).allowed +const describe = permissions => sessionsPageDescription({ canChat: permissions.includes('endpoint.operate'), canBranch: canBranch(permissions) }) + +test('the description names each ability the identity has, and says read-only only when it has neither', () => { + assert.equal(canBranch(['agent.configure']), true, 'the protocol table maps the leaf route to agent.configure') + const none = describe([]) + const chat = describe(['endpoint.operate']) + const branch = describe(['agent.configure']) + const both = describe(['endpoint.operate', 'agent.configure']) + assert.match(none, /只读/u) + for (const text of [chat, branch, both]) assert.doesNotMatch(text, /只读/u) + assert.match(chat, /接着聊/u); assert.doesNotMatch(chat, /其他消息继续/u, 'an operator has no 「从这里继续」') + assert.match(branch, /其他消息继续/u); assert.doesNotMatch(branch, /接着聊/u, 'agent.configure alone cannot chat') + assert.match(both, /接着聊/u); assert.match(both, /其他消息继续/u) + assert.equal(new Set([none, chat, branch, both]).size, 4) + for (const text of [none, chat, branch, both]) assert.notEqual(translate('en', text), text, `English for ${text}`) +}) + +test('the page builds its description from both abilities and keeps readOnly for the trace read', () => { + const page = readFileSync(new URL('../console-ui/src/pages/agent-sessions.tsx', import.meta.url), 'utf8') + assert.match(page, /sessionsPageDescription\(\{ canChat: !readOnly, canBranch: canSwitchLeaf \}\)/u) + assert.match(page, /useConversationTraces\(readOnly \? '' : selectedKey/u) +})