fix: preserve assist session lifecycle and identity certainty

This commit is contained in:
gitea committed 2026-10-21 16:04:26 +08:00
1 parent 4e53c88a51
commit 3597ec6694
8 files changed
+369 -38

No files matched your search

+38 -22
View File
@@ -1,3 +1,4 @@
import { BusinessError } from "../../../core/errors.js";
import {
generateAssistSession,
importAssistSessionToLocal,
@@ -29,6 +30,10 @@ function formatIdentityLines(ident?: AssistIdentityProfile): {
profilePairs["核心系统权限"] = `${permSummary}${ident.readablePermissions.length > 5 ? ` 等共 ${ident.readablePermissions.length} 项` : ""}`;
}
if (ident.profileComplete === false) {
profilePairs["身份画像状态"] = "信息不完整,无法排除更高权限风险";
}
if (ident.isSeniorExecutive && ident.riskWarning) {
const titles = ident.seniorTitles.length > 0 ? ident.seniorTitles.join(" / ") : "高级管理人员";
warningText =
@@ -47,7 +52,6 @@ export async function handleEnter(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const client = resolveAssistClient(parsed, runtime);
const corpId =
parsed.args[0] ||
stringOption(parsed.options, ["corp", "corpid", "id"]) ||
@@ -72,6 +76,28 @@ export async function handleEnter(
const overwrite = booleanOption(parsed.options, ["overwrite"]);
const saveName = stringOption(parsed.options, ["alias", "corp-name"]);
if (isDryRun(parsed, runtime)) {
return mutationPreview(
parsed,
runtime,
shouldSave || shouldSwitch
? "从协助平台换取免密凭证并落盘为本地企业配置"
: "从协助平台换取免密凭证并生成直通链接",
saveName || `企业 [${corpId}]`,
{
corpId,
isAdmin,
staffName: staffName || null,
ticketId: ticketId || "自动选择最新有效工单",
save: shouldSave || shouldSwitch,
switchCurrent: shouldSwitch,
overwrite,
}
);
}
const client = resolveAssistClient(parsed, runtime);
// 1. 若不要求保存落盘,仅生成直通免密链接输出
if (!shouldSave && !shouldSwitch) {
const sessionRes = await generateAssistSession(client, {
@@ -84,6 +110,7 @@ export async function handleEnter(
if (runtime.jsonMode) {
return {
stdout: printJson(sessionRes, runtime.outputOptions),
preserveSensitiveStdout: true,
stderr: "",
exitCode: 0,
};
@@ -101,6 +128,7 @@ export async function handleEnter(
Object.assign(pairs, profilePairs);
return {
preserveSensitiveStdout: true,
stdout:
`====== 协助平台免密会话生成成功 ======\n${printKeyValue(pairs)}\n` +
`${warningText}\n操作提示:\n` +
@@ -112,23 +140,6 @@ export async function handleEnter(
}
// 2. 要求持久化落盘 / 切换为工作企业
if (isDryRun(parsed, runtime)) {
return mutationPreview(
parsed,
runtime,
"从协助平台换取免密凭证并落盘为本地企业配置",
saveName || `企业 [${corpId}]`,
{
corpId,
isAdmin,
staffName: staffName || null,
ticketId: ticketId || "自动选择最新有效工单",
switchCurrent: shouldSwitch,
overwrite,
}
);
}
const nextConfirmCmd = `hose-use assist-platform enter ${corpId} ${isAdmin ? "--admin" : `--staff "${staffName}"`} --save ${shouldSwitch ? "--switch " : ""}--yes`;
requireMutationConfirmation(
parsed,
@@ -146,13 +157,18 @@ export async function handleEnter(
overwrite: overwrite ?? true,
});
// 导入文件后同步注册到当前运行态,确保新企业可立即切换
runtime.corpContext.upsertCorp(corpConfig);
// 若指定了 --switch,切换当前激活企业
if (shouldSwitch) {
runtime.corpContext.switchCorp(corpConfig.id, true);
const switched = shouldSwitch && runtime.corpContext.switchCorp(corpConfig.id, true);
if (shouldSwitch && !switched) {
throw new BusinessError(`企业 [${corpConfig.id}] 配置已保存,但切换当前工作企业失败,请运行 hose-use corps switch ${corpConfig.id} 重试`);
}
if (runtime.jsonMode) {
return {
preserveSensitiveStdout: true,
stdout: printJson({
success: true,
sessionResult,
@@ -161,7 +177,7 @@ export async function handleEnter(
name: corpConfig.name,
staffId: corpConfig.credentials.staffId,
hasOpenApi: Boolean(corpConfig.credentials.appKey),
switched: shouldSwitch,
switched,
},
}, runtime.outputOptions),
stderr: "",
@@ -176,7 +192,7 @@ export async function handleEnter(
授权工单: sessionResult.ticketId,
凭证状态: corpConfig.credentials.appKey ? `✅ 已提取 OpenAPI 持久化密钥 (${corpConfig.credentials.appKey})` : "⚪ 临时协助会话",
配置文件: `enterprises/${corpConfig.id}.json`,
激活状态: shouldSwitch ? "🎯 已设为当前激活工作企业" : "可随时运行 hose-use corps switch 进行切换",
激活状态: switched ? "🎯 已设为当前激活工作企业" : "可随时运行 hose-use corps switch 进行切换",
};
const { profilePairs, warningText } = formatIdentityLines(sessionResult.identity);