Files
hose-use/src/core/config/manager.ts
T
gitea 1cd8901f7a feat(cli): add corps search command and intuitive aliases
- Implement hose-use corps search <keyword> with case-insensitive fuzzy matching across id, name, corpId, staffId, staffName, and accountId
- Format terminal table output with matched cell brackets, hit labels, active corp markers, and friendly no-match fallback
- Support --json structured output conforming to CliResult specification
- Add intuitive aliases for frequently guessed commands: auth list -> auth accounts, corps find -> corps search, config show -> config paths
- Enhance CorpSummary and CorpContext with corpId, staffName, and getAllCorps
- Add comprehensive unit tests in corps.test.ts, auth.test.ts, and config.test.ts
2026-09-09 14:42:58 +08:00

167 lines
4.6 KiB
TypeScript

import { CorpConfig, GlobalConfig } from "./types.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;
}
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) =>
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-use config mode stateful` 切换为有状态模式。"
);
}
const firstCorp = corps[0];
if (firstCorp) return firstCorp;
const hintDirs = candidateDirs?.length ? ` (已检索路径: ${candidateDirs.join(", ")})` : "";
throw new Error(`未找到任何企业配置。请先运行 '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) => ({
id: corp.id,
name: corp.name,
corpId: corp.corpId || corp.credentials.corpId,
staffId: corp.credentials.staffId,
staffName: corp.staffName || corp.session?.staffName || undefined,
source: corp.source || "manual",
accountId: corp.accountId,
baseUrl: corp.baseUrl,
current: corp.id === this.current?.id,
expiresAt: corp.auth?.expiresAt || (corp.session?.expTime ?? undefined),
}));
}
}