Files
hose-use/src/core/config/manager.ts
T

151 lines
4.0 KiB
TypeScript

import { CorpConfig, GlobalConfig } from "./types.js";
import { saveGlobalConfig } from "./loader.js";
export interface CorpSummary {
id: string;
name: string;
source?: string;
accountId?: string;
staffId: string;
baseUrl?: string;
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) {
// 1. 优先按身份/配置 ID 精确匹配 (如 uzU7pcWfScck00@17718556446)
let corp = corps.find((item) => item.id === selectedCorpId);
// 2. 其次按企业物理 ID 匹配 (如 uzU7pcWfScck00)
if (!corp) {
corp = corps.find(
(item) =>
item.corpId === selectedCorpId ||
item.credentials.corpId === selectedCorpId
);
}
if (corp) return corp;
throw new Error(`未找到企业或身份标识为 [${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[] = [],
options: { stateful?: boolean; defaultCorpId?: string } = {}
) {
this.stateful = options.stateful ?? true;
this.defaultCorpId = options.defaultCorpId;
this.reload(corps);
}
reload(corps: CorpConfig[]): void {
this.corps.clear();
for (const corp of corps) {
this.corps.set(corp.id, corp);
}
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];
}
}
getCurrent(): CorpConfig | null {
return this.current;
}
getCorp(corpId: string): CorpConfig | undefined {
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 (this.stateful && (setCurrent || !this.current)) {
this.current = corp;
this.defaultCorpId = corp.id;
}
}
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;
}
listCorps(): CorpSummary[] {
return Array.from(this.corps.values()).map((corp) => ({
id: corp.id,
name: corp.name,
source: corp.source || "manual",
accountId: corp.accountId,
staffId: corp.credentials.staffId,
baseUrl: corp.baseUrl,
current: corp.id === this.current?.id,
}));
}
}