181 lines
5.1 KiB
TypeScript
181 lines
5.1 KiB
TypeScript
import {
|
|
CorpConfig,
|
|
getCorpExpiresAt,
|
|
getCorpPhysicalId,
|
|
getCorpStaffId,
|
|
getCorpStaffName,
|
|
} from "./types.js";
|
|
import { NotFoundError, NotLoggedInError, ValidationError } from "../errors.js";
|
|
import { saveGlobalConfig } from "./loader.js";
|
|
|
|
export interface CorpSummary {
|
|
id: string;
|
|
name: string;
|
|
corpId?: string;
|
|
staffName?: string;
|
|
source?: string;
|
|
accountId?: string;
|
|
staffId: string;
|
|
baseUrl?: string;
|
|
current: boolean;
|
|
expiresAt?: number;
|
|
persistent?: boolean;
|
|
hasOpenApi?: boolean;
|
|
}
|
|
|
|
export function resolveCorpConfig(
|
|
corps: CorpConfig[],
|
|
options: {
|
|
stateful: boolean;
|
|
defaultCorpId?: string;
|
|
corpId?: string;
|
|
},
|
|
candidateDirs?: 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) => getCorpPhysicalId(item) === selectedCorpId);
|
|
}
|
|
if (corp) return corp;
|
|
throw new NotFoundError(`未找到企业或身份标识为 [${selectedCorpId}] 的企业配置`);
|
|
}
|
|
|
|
if (!options.stateful) {
|
|
throw new ValidationError(
|
|
"当前处于无状态模式 (Stateless Mode)。执行企业级操作必须显式指定 --corp <corpId>。\n" +
|
|
"如需设置默认企业,可执行 `hose-use config mode stateful` 切换为有状态模式。"
|
|
);
|
|
}
|
|
|
|
const firstCorp = corps[0];
|
|
if (firstCorp) return firstCorp;
|
|
|
|
const hintDirs = candidateDirs?.length ? ` (已检索路径: ${candidateDirs.join(", ")})` : "";
|
|
throw new NotLoggedInError(`未找到任何企业配置。请先运行 'hose-use auth login' 登录或添加企业配置${hintDirs}。`);
|
|
}
|
|
|
|
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, candidateDirs?: string[]): CorpConfig {
|
|
return resolveCorpConfig(
|
|
Array.from(this.corps.values()),
|
|
{
|
|
stateful: this.stateful,
|
|
defaultCorpId: this.defaultCorpId,
|
|
corpId,
|
|
},
|
|
candidateDirs
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
getAllCorps(): CorpConfig[] {
|
|
return Array.from(this.corps.values());
|
|
}
|
|
|
|
listCorps(): CorpSummary[] {
|
|
return Array.from(this.corps.values()).map((corp) => {
|
|
const hasOpenApiKeys = Boolean(
|
|
(corp.credentials?.appKey || corp.auth?.appKey) &&
|
|
(corp.credentials?.appSecurity || corp.auth?.appSecurity)
|
|
);
|
|
const hasAccountBinding = corp.source === "account" && !corp.session?.isShortTermToken && Boolean(corp.accountId);
|
|
return {
|
|
id: corp.id,
|
|
name: corp.name,
|
|
corpId: getCorpPhysicalId(corp),
|
|
staffId: getCorpStaffId(corp),
|
|
staffName: getCorpStaffName(corp),
|
|
source: corp.source || "manual",
|
|
accountId: corp.accountId,
|
|
baseUrl: corp.baseUrl,
|
|
current: corp.id === this.current?.id,
|
|
expiresAt: getCorpExpiresAt(corp),
|
|
persistent: hasOpenApiKeys || hasAccountBinding,
|
|
hasOpenApi: hasOpenApiKeys,
|
|
};
|
|
});
|
|
}
|
|
}
|