feat(config): 实现有状态与无状态 (stateful/stateless) 运行模式无缝切换与企业解析
This commit is contained in:
1 parent
e7aa8a86cc
commit
189415cdac
6 files changed
+293
-16
No files matched your search
@@ -1,13 +1,43 @@
|
||||
import { readFileSync, readdirSync, writeFileSync, mkdirSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types.js";
|
||||
import { join, dirname } from "path";
|
||||
import { homedir } from "os";
|
||||
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
|
||||
|
||||
const ENTERPRISES_DIR = "./enterprises";
|
||||
const GLOBAL_CONFIG_PATH = join(homedir(), ".cache", "ekb-mcp", "config.json");
|
||||
|
||||
function ensureDir(dir: string): void {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
export function resolveGlobalConfigFile(): string {
|
||||
return GLOBAL_CONFIG_PATH;
|
||||
}
|
||||
|
||||
export function loadGlobalConfig(): GlobalConfig {
|
||||
try {
|
||||
const content = readFileSync(GLOBAL_CONFIG_PATH, "utf-8");
|
||||
return GlobalConfigSchema.parse(JSON.parse(content));
|
||||
} catch {
|
||||
return {
|
||||
stateful: true,
|
||||
defaultCorpId: undefined,
|
||||
defaultAccountId: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function saveGlobalConfig(config: Partial<GlobalConfig>): GlobalConfig {
|
||||
const current = loadGlobalConfig();
|
||||
const updated = GlobalConfigSchema.parse({
|
||||
...current,
|
||||
...config,
|
||||
});
|
||||
ensureDir(dirname(GLOBAL_CONFIG_PATH));
|
||||
writeFileSync(GLOBAL_CONFIG_PATH, JSON.stringify(updated, null, 2), "utf-8");
|
||||
return updated;
|
||||
}
|
||||
|
||||
export function loadCorpConfigs(dir: string = ENTERPRISES_DIR): CorpConfig[] {
|
||||
let files: string[];
|
||||
try {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CorpConfig } from "./types.js";
|
||||
import { CorpConfig, GlobalConfig } from "./types.js";
|
||||
import { saveGlobalConfig } from "./loader.js";
|
||||
|
||||
export interface CorpSummary {
|
||||
id: string;
|
||||
@@ -10,11 +11,51 @@ export interface CorpSummary {
|
||||
current: boolean;
|
||||
}
|
||||
|
||||
export function resolveCorpConfig(
|
||||
corps: CorpConfig[],
|
||||
options: {
|
||||
stateful: boolean;
|
||||
defaultCorpId?: string;
|
||||
corpId?: string;
|
||||
}
|
||||
): CorpConfig {
|
||||
const requestedCorpId = options.corpId?.trim() || undefined;
|
||||
const configuredCorpId = options.stateful
|
||||
? options.defaultCorpId?.trim() || undefined
|
||||
: undefined;
|
||||
const selectedCorpId = requestedCorpId ?? configuredCorpId;
|
||||
|
||||
if (selectedCorpId) {
|
||||
const corp = corps.find((item) => item.id === selectedCorpId);
|
||||
if (corp) return corp;
|
||||
throw new Error(`未找到企业 ID 为 ${selectedCorpId} 的企业配置`);
|
||||
}
|
||||
|
||||
if (!options.stateful) {
|
||||
throw new Error(
|
||||
"当前处于无状态模式 (Stateless Mode)。执行企业级操作必须显式指定 --corp <corpId>。\n" +
|
||||
"如需设置默认企业,可执行 `hose config mode stateful` 切换为有状态模式。"
|
||||
);
|
||||
}
|
||||
|
||||
const firstCorp = corps[0];
|
||||
if (firstCorp) return firstCorp;
|
||||
|
||||
throw new Error("未找到任何企业配置。请先运行 `hose auth login` 或在 enterprises/ 目录下添加配置。");
|
||||
}
|
||||
|
||||
export class CorpContext {
|
||||
private current: CorpConfig | null = null;
|
||||
private corps: Map<string, CorpConfig> = new Map();
|
||||
private stateful: boolean = true;
|
||||
private defaultCorpId: string | undefined;
|
||||
|
||||
constructor(corps: CorpConfig[] = []) {
|
||||
constructor(
|
||||
corps: CorpConfig[] = [],
|
||||
options: { stateful?: boolean; defaultCorpId?: string } = {}
|
||||
) {
|
||||
this.stateful = options.stateful ?? true;
|
||||
this.defaultCorpId = options.defaultCorpId;
|
||||
this.reload(corps);
|
||||
}
|
||||
|
||||
@@ -23,7 +64,30 @@ export class CorpContext {
|
||||
for (const corp of corps) {
|
||||
this.corps.set(corp.id, corp);
|
||||
}
|
||||
if (this.corps.size > 0 && (!this.current || !this.corps.has(this.current.id))) {
|
||||
|
||||
if (!this.stateful) {
|
||||
this.current = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.defaultCorpId && this.corps.has(this.defaultCorpId)) {
|
||||
this.current = this.corps.get(this.defaultCorpId)!;
|
||||
} else if (this.corps.size > 0) {
|
||||
this.current = Array.from(this.corps.values())[0];
|
||||
} else {
|
||||
this.current = null;
|
||||
}
|
||||
}
|
||||
|
||||
isStateful(): boolean {
|
||||
return this.stateful;
|
||||
}
|
||||
|
||||
setStateful(stateful: boolean): void {
|
||||
this.stateful = stateful;
|
||||
if (!stateful) {
|
||||
this.current = null;
|
||||
} else if (!this.current && this.corps.size > 0) {
|
||||
this.current = Array.from(this.corps.values())[0];
|
||||
}
|
||||
}
|
||||
@@ -36,17 +100,30 @@ export class CorpContext {
|
||||
return this.corps.get(corpId);
|
||||
}
|
||||
|
||||
resolve(corpId?: string): CorpConfig {
|
||||
return resolveCorpConfig(Array.from(this.corps.values()), {
|
||||
stateful: this.stateful,
|
||||
defaultCorpId: this.defaultCorpId,
|
||||
corpId,
|
||||
});
|
||||
}
|
||||
|
||||
upsertCorp(corp: CorpConfig, setCurrent: boolean = false): void {
|
||||
this.corps.set(corp.id, corp);
|
||||
if (setCurrent || !this.current) {
|
||||
if (this.stateful && (setCurrent || !this.current)) {
|
||||
this.current = corp;
|
||||
this.defaultCorpId = corp.id;
|
||||
}
|
||||
}
|
||||
|
||||
switchCorp(corpId: string): boolean {
|
||||
switchCorp(corpId: string, persist: boolean = true): boolean {
|
||||
const corp = this.corps.get(corpId);
|
||||
if (!corp) return false;
|
||||
this.current = corp;
|
||||
this.defaultCorpId = corp.id;
|
||||
if (persist && this.stateful) {
|
||||
saveGlobalConfig({ defaultCorpId: corp.id });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,3 +28,13 @@ export const CorpConfigSchema = z.object({
|
||||
export type CorpCredentials = z.infer<typeof CorpCredentialsSchema>;
|
||||
export type CorpAuthConfig = z.infer<typeof CorpAuthConfigSchema>;
|
||||
export type CorpConfig = z.infer<typeof CorpConfigSchema>;
|
||||
|
||||
// ── 全局运行配置(有状态/无状态模式)─────────────────────────────────────────
|
||||
|
||||
export const GlobalConfigSchema = z.object({
|
||||
stateful: z.boolean().default(true).describe("是否为有状态模式 (true 记忆默认企业,false 无状态需显式传 --corp)"),
|
||||
defaultCorpId: z.string().optional().describe("默认工作企业 ID"),
|
||||
defaultAccountId: z.string().optional().describe("默认主账号"),
|
||||
});
|
||||
|
||||
export type GlobalConfig = z.infer<typeof GlobalConfigSchema>;
|
||||
Reference in new issue
Block a user