- Schema 扩展: CorpConfigSchema 增加 env ('prod' | 'test' | 'demo') 与 isTest 字段,未知企业保守兜底为 prod
- 智能初判与归一化: inferCorpEnvFromName 依据名称初判环境,normalizeCorpConfig 确保配置持久化时环境对齐
- CLI 视图升级: corps list 与 search 增加「环境」列 (🏭 正式 / 🧪 测试 / 🎪 演示),支持 --test/--prod/--env 筛选
- 环境打标与迁移: 新增 hose-use corps tag 命令,支持单个企业标记与 --migrate-all 批量显式升级持久化
- 生产安全阻断: workflow test 等敏感调试在正式生产环境 (prod) 阻断执行,需显式附加 --force 确认生产风险
- 上下文与探针增强: context、probe、corps current 全面输出企业环境属性
- 测试与文档补全: 覆盖 489 项全量测试并通过 tsc 类型检查
165 lines
7.3 KiB
TypeScript
165 lines
7.3 KiB
TypeScript
import { getActiveAccount } from "../../core/auth/account-store.js";
|
|
import { canRefreshCorpAccessToken, hasOpenApiPersistentCredentials } from "../../core/auth/login.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, formatCorpEnvLabel, 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.persistent || !c.expiresAt || c.expiresAt > now);
|
|
const expiredCorps = allCorps.filter((c) => !c.persistent && c.expiresAt && c.expiresAt <= now);
|
|
const currentPersistent = currentCorp ? canRefreshCorpAccessToken(currentCorp) : false;
|
|
|
|
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,
|
|
env: currentCorp.env,
|
|
isTest: currentCorp.isTest,
|
|
staffId: currentCorp.credentials?.staffId || currentCorp.staffId,
|
|
userName: currentCorp.staffName || currentCorp.session?.staffName || null,
|
|
source: currentCorp.source,
|
|
persistent: currentPersistent,
|
|
expiresAt: currentCorp.auth?.expiresAt || currentCorp.session?.expTime,
|
|
expired: currentPersistent
|
|
? false
|
|
: (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,
|
|
prodCount: allCorps.filter((c) => c.env === "prod").length,
|
|
testCount: allCorps.filter((c) => c.env === "test").length,
|
|
demoCount: allCorps.filter((c) => c.env === "demo").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 prodCount = allCorps.filter((c) => c.env === "prod").length;
|
|
const testCount = allCorps.filter((c) => c.env === "test").length;
|
|
const demoCount = allCorps.filter((c) => c.env === "demo").length;
|
|
|
|
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}) [${formatCorpEnvLabel(currentCorp.env)}]` : "⚠️ 未激活任何企业"}`,
|
|
` 当前操作员工 : ${corpStaffId}`,
|
|
` 员工真实姓名 : ${corpStaffName}`,
|
|
` 凭证来源类型 : ${currentCorp ? formatSourceLabel(currentCorp.source, hasOpenApiPersistentCredentials(currentCorp)) : "-"}`,
|
|
` 凭证有效状态 : ${currentCorp ? formatExpiresAt(corpExpiresAt, currentPersistent, hasOpenApiPersistentCredentials(currentCorp) ? "openapi" : currentCorp.source) : "-"}`,
|
|
` 主站 BaseURL : ${currentCorp?.baseUrl || "-"}`,
|
|
` 已配置企业总数: ${allCorps.length} 家 (🏭 正式: ${prodCount} 家, 🧪 测试: ${testCount} 家, 🎪 演示: ${demoCount} 家)`,
|
|
` 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 };
|
|
}
|