fix: clarify session index binding scope

This commit is contained in:
xmanrui 2026-08-18 22:17:39 +08:00
parent 7b369187f8
commit 33d1cd43a7
10 changed files with 204 additions and 124 deletions

View file

@ -13,7 +13,7 @@ const MAX_COMMAND_MESSAGE_LENGTH = 1_800;
const MAX_SESSION_ID_LENGTH = 256;
const UNSAFE_DISPLAY_TEXT = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
const UNSAFE_DISPLAY_TEXT_GLOBAL = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]+/gu;
const SESSION_BIND_USAGE = '用法:/session Session ID 或 序号(/session N)';
const SESSION_BIND_USAGE = '用法:/session Session ID 或当前工作区序号(/session N)';
const SESSION_LIST_USAGE = [
'用法:',
'/sessionlist 列出当前工作区会话',
@ -194,7 +194,7 @@ function formatSessionRelativeTime(value) {
return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
}
function sessionListMessage(workspace, sessions) {
function sessionListMessage(workspace, sessions, { currentWorkspace = false } = {}) {
const rows = sessions.map((session) => {
const sessionId = safeDisplayText(session?.sessionId);
if (!sessionId) throw new TypeError('Harness returned an invalid session id');
@ -212,10 +212,19 @@ function sessionListMessage(workspace, sessions) {
'',
...rows.map((row, index) => `${index + 1}. ${row}`),
'',
'绑定用法:/session Session ID 或 序号(/session N)',
currentWorkspace
? '绑定用法:/session Session ID 或当前工作区序号(/session N)'
: '绑定用法:/session Session ID\n提示:/session N 只按机器人当前工作区的序号绑定。',
].join('\n');
}
async function currentSessionListWorkspace(harness) {
if (typeof harness?.currentWorkspace !== 'function') return null;
const [current] = await existingWorkspacePaths([harness.currentWorkspace()]);
harness.assertWorkspaceScope?.();
return current ?? null;
}
async function runSessionListCommand(match, harness) {
if (typeof harness?.listWorkspaceSessions !== 'function') {
return commandResult('当前机器人暂不支持列出工作区会话。');
@ -230,7 +239,10 @@ async function runSessionListCommand(match, harness) {
}
harness.assertWorkspaceScope?.();
const workspace = normalizedWorkspacePath(listed.workspace) ?? resolved.workspace;
const message = sessionListMessage(workspace, listed.sessions);
const currentWorkspace = await currentSessionListWorkspace(harness);
const message = sessionListMessage(workspace, listed.sessions, {
currentWorkspace: workspace === currentWorkspace,
});
return commandResult(message, splitWorkspaceCommandMessage(message));
} catch (error) {
if (error?.code === 'workspace-bot-not-found') {
@ -275,21 +287,30 @@ async function runSessionBindCommand(command, harness, conversationKey) {
|| typeof harness?.currentWorkspace !== 'function') {
return commandResult('当前机器人暂不支持按序号绑定,请使用 /session Session ID。');
}
const selected = await selectedWorkspacePath(harness.currentWorkspace());
if (selected.error) return commandResult(selected.error);
let listed;
try {
listed = await harness.listWorkspaceSessions(selected.workspace);
const selected = await selectedWorkspacePath(harness.currentWorkspace());
if (selected.error) return commandResult(selected.error);
const listed = await harness.listWorkspaceSessions(selected.workspace);
if (!listed || !Array.isArray(listed.sessions)) {
throw new TypeError('Harness returned an invalid workspace session list');
}
harness.assertWorkspaceScope?.();
} catch {
const position = Number(sessionId);
if (!Number.isSafeInteger(position) || position < 1
|| position > listed.sessions.length) {
return commandResult('会话序号不存在,请先执行 /sessionlist 查看序号。');
}
const selectedSessionId = listed.sessions[position - 1]?.sessionId;
if (!validSessionId(selectedSessionId)) {
throw new TypeError('Harness returned an invalid session id');
}
sessionId = selectedSessionId;
} catch (error) {
if (error?.code === 'workspace-bot-not-found') {
return commandResult(sessionBindErrorMessage(error));
}
return commandResult('暂时无法获取会话列表,请稍后重试。');
}
const position = Number(sessionId);
if (!Number.isSafeInteger(position) || position < 1
|| position > (listed?.sessions?.length ?? 0)) {
return commandResult('会话序号不存在,请先执行 /sessionlist 查看序号。');
}
sessionId = listed.sessions[position - 1].sessionId;
}
if (!validSessionId(sessionId)) return commandResult(SESSION_BIND_USAGE);
if (typeof harness?.bindWorkspaceSession !== 'function') {