153 lines
6.3 KiB
TypeScript
153 lines
6.3 KiB
TypeScript
import { getActiveAccount } from "../../core/auth/account-store.js";
|
|
import {
|
|
resolveGlobalConfigFile,
|
|
resolvePrimaryEnterprisesDir,
|
|
resolveWritableEnterprisesDir,
|
|
getEnterprisesDirCandidates,
|
|
} from "../../core/config/loader.js";
|
|
import { ApprovalRepository } from "../../domains/approval/index.js";
|
|
import { VERSION } from "../../version.js";
|
|
import { formatExpiresAt, formatSourceLabel, printJson } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
|
|
export async function handleContextCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const currentCorp = runtime.selectedCorp;
|
|
const allCorps = runtime.corpContext.listCorps();
|
|
const activeAccount = getActiveAccount();
|
|
const isStateful = runtime.corpContext.isStateful();
|
|
const configFile = resolveGlobalConfigFile();
|
|
const enterprisesDir = resolvePrimaryEnterprisesDir() || resolveWritableEnterprisesDir();
|
|
|
|
let scopesCount = 0;
|
|
let rulesCount = 0;
|
|
let scopes: Array<{ index: number; scope: string; ruleCount: number }> = [];
|
|
let totalRunsCount = 0;
|
|
let pendingRunsCount = 0;
|
|
|
|
try {
|
|
const repo = new ApprovalRepository();
|
|
const loadedScopes = repo.listScopes("active");
|
|
scopesCount = loadedScopes.length;
|
|
scopes = loadedScopes.map((s) => ({
|
|
index: s.index,
|
|
scope: s.scope,
|
|
ruleCount: s.ruleCount,
|
|
}));
|
|
const loadedRules = repo.listRules();
|
|
rulesCount = loadedRules.length;
|
|
|
|
const runs = currentCorp?.id ? repo.listRuns({ corpId: currentCorp.id }) : repo.listRuns();
|
|
totalRunsCount = runs.length;
|
|
pendingRunsCount = runs.filter((r) => r.decision === "PENDING").length;
|
|
} catch {
|
|
// 容错处理(如 SQLite 尚未初始化等场景)
|
|
}
|
|
|
|
const now = Date.now();
|
|
const validCorps = allCorps.filter((c) => !c.expiresAt || c.expiresAt > now);
|
|
const expiredCorps = allCorps.filter((c) => c.expiresAt && c.expiresAt <= now);
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(
|
|
{
|
|
tool: "hose-use",
|
|
version: VERSION,
|
|
account: activeAccount
|
|
? {
|
|
accountId: activeAccount.accountId,
|
|
userId: activeAccount.userId || null,
|
|
corporationCount: activeAccount.corporations.length,
|
|
}
|
|
: currentCorp?.accountId
|
|
? {
|
|
accountId: currentCorp.accountId,
|
|
userId: null,
|
|
corporationCount: allCorps.filter((c) => c.accountId === currentCorp.accountId).length,
|
|
}
|
|
: null,
|
|
activeCorporation: currentCorp
|
|
? {
|
|
id: currentCorp.id,
|
|
name: currentCorp.name,
|
|
staffId: currentCorp.credentials?.staffId || currentCorp.staffId,
|
|
userName: currentCorp.staffName || currentCorp.session?.staffName || null,
|
|
source: currentCorp.source,
|
|
expiresAt: currentCorp.auth?.expiresAt || currentCorp.session?.expTime,
|
|
expired:
|
|
(currentCorp.auth?.expiresAt || currentCorp.session?.expTime)
|
|
? (currentCorp.auth?.expiresAt || currentCorp.session?.expTime)! <= now
|
|
: false,
|
|
baseUrl: currentCorp.baseUrl,
|
|
accountId: currentCorp.accountId || null,
|
|
}
|
|
: null,
|
|
corporationsSummary: {
|
|
total: allCorps.length,
|
|
validCount: validCorps.length,
|
|
expiredCount: expiredCorps.length,
|
|
activeCorpId: currentCorp?.id || null,
|
|
},
|
|
aiProGateway: {
|
|
connectedCorpId: currentCorp?.id || null,
|
|
scopesCount,
|
|
rulesCount,
|
|
scopes,
|
|
pendingRunsCount,
|
|
totalRunsCount,
|
|
},
|
|
runtime: {
|
|
mode: isStateful ? "stateful" : "stateless",
|
|
configFile,
|
|
enterprisesDir,
|
|
candidates: getEnterprisesDirCandidates(),
|
|
},
|
|
},
|
|
runtime.outputOptions
|
|
),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const accountStr = activeAccount?.accountId || currentCorp?.accountId || "-";
|
|
const userIdStr = activeAccount?.userId ? ` (用户 ID: ${activeAccount.userId})` : "";
|
|
const corpStaffId = currentCorp?.credentials?.staffId || currentCorp?.staffId || "-";
|
|
const corpStaffName = currentCorp?.staffName || currentCorp?.session?.staffName || "-";
|
|
const corpExpiresAt = currentCorp?.auth?.expiresAt || currentCorp?.session?.expTime;
|
|
|
|
const scopesStr =
|
|
scopes.length > 0
|
|
? `已加载 ${scopesCount} 个场景 (${scopes.map((s) => `#${s.index} ${s.scope}`).join(", ")}),共 ${rulesCount} 条仲裁规则`
|
|
: "暂未配置场景规则 (可使用 hose-use rule add 添加)";
|
|
|
|
const lines = [
|
|
`====== 全局运行上下文与身份诊断 (hose-use v${VERSION}) ======`,
|
|
` 当前活跃账号 : ${accountStr}${userIdStr}`,
|
|
` 当前激活企业 : ${currentCorp ? `${currentCorp.name} (${currentCorp.id})` : "⚠️ 未激活任何企业"}`,
|
|
` 当前操作员工 : ${corpStaffId}`,
|
|
` 员工真实姓名 : ${corpStaffName}`,
|
|
` 凭证来源类型 : ${currentCorp ? formatSourceLabel(currentCorp.source) : "-"}`,
|
|
` 凭证有效状态 : ${currentCorp ? formatExpiresAt(corpExpiresAt) : "-"}`,
|
|
` 主站 BaseURL : ${currentCorp?.baseUrl || "-"}`,
|
|
` 已配置企业总数: ${allCorps.length} 家 (有效: ${validCorps.length} 家, 已过期: ${expiredCorps.length} 家)`,
|
|
` AI PRO 规约库 : ${scopesStr}`,
|
|
` AI PRO 待决断 : ${pendingRunsCount} 个待决断会话 (当前企业历史总会话: ${totalRunsCount} 个)`,
|
|
` 运行模式设置 : ${isStateful ? "有状态模式 (Stateful - 自动记忆当前企业)" : "无状态模式 (Stateless)"}`,
|
|
` 全局配置文件 : ${configFile}`,
|
|
` 企业凭证目录 : ${enterprisesDir}`,
|
|
"",
|
|
"提示:",
|
|
" - hose-use 与 hose-aipro 默认共享同一套企业与身份凭证上下文,无须重复配置。",
|
|
" - 切换全局默认企业: hose-use corps switch <企业ID>",
|
|
" - 单次临时指定企业: hose-use --corp=<企业ID> <命令> 或 hose-aipro --corp=<企业ID> <子命令>",
|
|
" - AI PRO 专属上下文: hose-aipro status",
|
|
"",
|
|
];
|
|
|
|
return { stdout: lines.join("\n"), stderr: "", exitCode: 0 };
|
|
}
|