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,
};
}
+30 -8
View File
@@ -1,23 +1,45 @@
import readline from "readline";
import { loadCorpConfigs } from "../core/config/loader.js";
import { CorpContext } from "../core/config/manager.js";
import { loadCorpConfigs, loadGlobalConfig } from "../core/config/loader.js";
import { CorpContext, resolveCorpConfig } from "../core/config/manager.js";
import { CorpConfig } from "../core/config/types.js";
import { createEkbClient } from "../core/auth/ekb.js";
import { getEasoClient } from "../core/auth/easo.js";
import { CliRuntime } from "./types.js";
export function initCorpRuntime(corpIdOption?: string, jsonMode: boolean = false): CliRuntime {
export function initCorpRuntime(
options: {
corpId?: string;
stateless?: boolean;
stateful?: boolean;
jsonMode?: boolean;
} = {}
): CliRuntime {
const corps = loadCorpConfigs();
const corpContext = new CorpContext(corps);
const globalConfig = loadGlobalConfig();
if (corpIdOption) {
corpContext.switchCorp(corpIdOption);
// 命令行参数 --stateless / --stateful 具有最高优先级
const isStateful = options.stateless
? false
: options.stateful
? true
: globalConfig.stateful;
const corpContext = new CorpContext(corps, {
stateful: isStateful,
defaultCorpId: globalConfig.defaultCorpId,
});
let selectedCorp: CorpConfig | null = null;
try {
selectedCorp = corpContext.resolve(options.corpId);
} catch {
selectedCorp = null;
}
return {
corpContext,
selectedCorp: corpContext.getCurrent(),
jsonMode,
selectedCorp,
jsonMode: options.jsonMode ?? false,
};
}
+12 -1
View File
@@ -4,6 +4,7 @@ import { printHelp, printJson } from "./printer.js";
import { initCorpRuntime } from "./helpers.js";
import { handleAuthCommand } from "./commands/auth.js";
import { handleCorpsCommand } from "./commands/corps.js";
import { handleConfigCommand } from "./commands/config.js";
import { handleWorkflowCommand } from "./commands/workflow.js";
import { handlePlatformCommand } from "./commands/platform.js";
import { handleProbeCommand } from "./commands/probe.js";
@@ -26,8 +27,15 @@ export async function runCli(
const jsonMode = booleanOption(parsed.options, ["json", "j"]);
const corpIdOption = stringOption(parsed.options, ["corp", "c"]);
const statelessOption = booleanOption(parsed.options, ["stateless"]);
const statefulOption = booleanOption(parsed.options, ["stateful"]);
const runtime = initCorpRuntime(corpIdOption, jsonMode);
const runtime = initCorpRuntime({
corpId: corpIdOption,
stateless: statelessOption,
stateful: statefulOption,
jsonMode,
});
try {
switch (parsed.area) {
@@ -37,6 +45,9 @@ export async function runCli(
case "corps":
case "corp":
return await handleCorpsCommand(parsed, runtime);
case "config":
case "mode":
return await handleConfigCommand(parsed, runtime);
case "workflow":
case "workflows":
case "wf":