feat(config): 实现有状态与无状态 (stateful/stateless) 运行模式无缝切换与企业解析

This commit is contained in:
gitea committed 2026-08-27 10:59:08 +08:00
1 parent e7aa8a86cc
commit 189415cdac
6 files changed
+293 -16

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
import {
loadGlobalConfig,
saveGlobalConfig,
resolveGlobalConfigFile,
} from "../../core/config/loader.js";
import { printJson, printKeyValue } from "../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
export async function handleConfigCommand(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const action = parsed.action || "show";
switch (action) {
case "show":
case "get":
case "list":
return handleShow(parsed, runtime);
case "mode":
return handleMode(parsed, runtime);
case "set":
return handleSet(parsed, runtime);
default:
return {
stdout: "",
stderr: `未知 config 子命令: ${action}。支持: show, mode <stateful|stateless>, set <key> <val>\n`,
exitCode: 1,
};
}
}
async function handleShow(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const globalConfig = loadGlobalConfig();
const currentCorp = runtime.selectedCorp;
const configPath = resolveGlobalConfigFile();
const summaryObj = {
mode: globalConfig.stateful ? "stateful" : "stateless",
stateful: globalConfig.stateful,
defaultCorpId: globalConfig.defaultCorpId || null,
defaultAccountId: globalConfig.defaultAccountId || null,
activeCorp: currentCorp ? { id: currentCorp.id, name: currentCorp.name } : null,
configFile: configPath,
};
if (runtime.jsonMode) {
return { stdout: printJson(summaryObj), stderr: "", exitCode: 0 };
}
const output = printKeyValue({
"运行模式 (Mode)": globalConfig.stateful ? "有状态模式 (Stateful - 自动记忆默认企业)" : "无状态模式 (Stateless - 每次需显式传 --corp)",
"默认工作企业": globalConfig.defaultCorpId || "未设置 (使用首个可用企业)",
"当前解析企业": currentCorp ? `${currentCorp.name} (${currentCorp.id})` : "无",
"配置文件路径": configPath,
});
return {
stdout: `====== 全局运行配置 ======\n${output}\n\n提示:\n 切换模式: hose config mode <stateful|stateless>\n 指定企业: hose corps switch <corpId>\n`,
stderr: "",
exitCode: 0,
};
}
async function handleMode(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const targetMode = parsed.args[0]?.toLowerCase();
if (!targetMode || (targetMode !== "stateful" && targetMode !== "stateless")) {
return {
stdout: "",
stderr: "用法: hose config mode <stateful|stateless>\n",
exitCode: 1,
};
}
const isStateful = targetMode === "stateful";
const updated = saveGlobalConfig({ stateful: isStateful });
if (runtime.jsonMode) {
return { stdout: printJson(updated), stderr: "", exitCode: 0 };
}
return {
stdout: `[SUCCESS] 全局运行模式已切换为: ${isStateful ? "有状态模式 (Stateful)" : "无状态模式 (Stateless)"}\n`,
stderr: "",
exitCode: 0,
};
}
async function handleSet(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const key = parsed.args[0];
const val = parsed.args[1];
if (!key || val === undefined) {
return {
stdout: "",
stderr: "用法: hose config set <key> <val> (如 hose config set defaultCorpId uzU7pcWfScck00)\n",
exitCode: 1,
};
}
const partial: Record<string, unknown> = {};
if (key === "stateful") {
partial.stateful = val === "true" || val === "1";
} else if (key === "defaultCorpId") {
partial.defaultCorpId = val;
} else if (key === "defaultAccountId") {
partial.defaultAccountId = val;
} else {
return {
stdout: "",
stderr: `不支持配置键: ${key}。支持: stateful, defaultCorpId, defaultAccountId\n`,
exitCode: 1,
};
}
const updated = saveGlobalConfig(partial);
if (runtime.jsonMode) {
return { stdout: printJson(updated), stderr: "", exitCode: 0 };
}
return {
stdout: `[SUCCESS] 配置已更新: ${key} = ${val}\n`,
stderr: "",
exitCode: 0,
};
}