Files
hose-use/src/cli/helpers.ts
T
gitea 42bf0bfb0b refactor(core/http): 7大通信底座客户端语义化全量重构与合思开放文档客户端纳管
- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名:
  * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端)
  * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端)
  * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端)
  * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端)
- 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信
- 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转
- 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验
- 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
2026-11-16 17:15:29 +08:00

126 lines
3.5 KiB
TypeScript

import readline from "readline";
import { loadCorpConfigs, loadGlobalConfig, getEnterprisesDirCandidates } from "../core/config/loader.js";
import { CorpContext, resolveCorpConfig } from "../core/config/manager.js";
import { CorpConfig } from "../core/config/types.js";
import {
createSessionClient,
getEasoClient,
getUnityClient,
getScriptEngineClient,
} from "../core/http/index.js";
import { CliRuntime, OutputOptions } from "./types.js";
export function initCorpRuntime(
options: {
corpId?: string;
stateless?: boolean;
stateful?: boolean;
jsonMode?: boolean;
dryRun?: boolean;
confirmed?: boolean;
outputOptions?: OutputOptions;
appRoot?: string;
entryFile?: string;
launcher?: {
command: string;
args?: string[];
};
} = {}
): CliRuntime {
const corps = loadCorpConfigs();
const globalConfig = loadGlobalConfig();
// 命令行参数 --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, getEnterprisesDirCandidates());
} catch {
selectedCorp = null;
}
return {
corpContext,
selectedCorp,
jsonMode: options.jsonMode ?? false,
dryRun: options.dryRun ?? false,
confirmed: options.confirmed ?? false,
outputOptions: options.outputOptions,
appRoot: options.appRoot,
entryFile: options.entryFile,
launcher: options.launcher,
};
}
export async function createClientsForCorp(corp: CorpConfig) {
const sessionClient = await createSessionClient(corp);
let easoClient: Awaited<ReturnType<typeof getEasoClient>>["client"];
let unityClient: Awaited<ReturnType<typeof getUnityClient>>["client"];
try {
const easo = await getEasoClient(corp);
easoClient = easo.client;
unityClient = easo.client;
} catch {
const unity = await getUnityClient(corp);
unityClient = unity.client;
easoClient = unity.client;
}
return {
corp,
sessionClient,
ekbClient: sessionClient,
unityClient,
easoClient,
};
}
export async function createScriptEngineClientForCorp(corp: CorpConfig, platformId: string) {
return getScriptEngineClient(corp, platformId);
}
export const createPiperClientForCorp = createScriptEngineClientForCorp;
export const createPiperWorkflowClientForCorp = createPiperClientForCorp;
export function requireSelectedCorp(
runtime: CliRuntime,
message = "未指定或未找到有效企业。请通过 --corp=<id> 指定企业,或运行 hose-use auth login 登录。"
): CorpConfig {
if (!runtime.selectedCorp) {
throw new Error(message);
}
return runtime.selectedCorp;
}
export async function getSessionClientForRuntime(runtime: CliRuntime) {
const corp = requireSelectedCorp(runtime);
const corpId = corp.credentials?.corpId || corp.corpId || corp.id;
const client = await createSessionClient(corp);
return { corp, corpId, client };
}
export const getHoseWebSessionClientForRuntime = getSessionClientForRuntime;
export function prompt(question: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}