Files
hose-use/src/cli/commands/platform/shared.ts
T
gitea 0c9eb27f85 feat(platform): 支持工作流单号本地缓存秒级定位与自建应用业务对象智能反查
- 数据库迁移与本地缓存:在 `core/db/migrations.ts` 新增 version 5 迁移 (`005_workflow_job_cache`),创建 `workflow_job_index` 缓存表及高效复合索引;实现 `core/db/workflow-job-cache.ts`,支持按企业+工作流+单号快速检索与幂等增量缓存;
- 流式早停探查与业务编排:在 `domains/platform/workflow/execution/search-by-flow.ts` 实现 `searchWorkflowJobByFlowCode`,采用「本地 SQLite 缓存 0ms 直达 ➔ 远端流式探查早停熔断并自动建立本地索引」机制,彻底解决云端 Piper API 无单号索引导致的排查低效问题;
- CLI 命令层升级:
  - `workflow logs <pipelineId> --flow=<单号>`:直接秒级定位单据关联 Job 实例;
  - `platform records <业务对象>`:支持单参数业务对象名称,自动在企业全量自建应用中全局反查与智能寻址,若跨应用重名则抛出精准候选;
- 命令行元数据对齐:在 `command-metadata.ts` 为 `workflow` 补齐 `--max-pages`、`--maxPages` 与 `--page-limit` 选项注册,满足严格参数词汇表静态检查;
- 测试与文档:新增并更新 `workflow-job-cache.test.ts`、`search-by-flow.test.ts`、`platform.test.ts` 与 `db.test.ts`,更新核心与域层文档,全量 1126 个测试与类型检查全部通过。
2026-11-13 13:17:14 +08:00

103 lines
4.3 KiB
TypeScript

import type { KyInstance } from "ky";
import { listPlatformEntities, resolveEntity } from "../../../domains/platform/datalink/services.js";
import { getPlatform, listPlatforms, resolvePlatform } from "../../../domains/platform/applications/services.js";
import type { DatalinkEntity } from "../../../domains/platform/datalink/types.js";
import type { DatalinkPlatformDetail } from "../../../domains/platform/applications/types.js";
import { createClientsForCorp } from "../../helpers.js";
import type { CliParsedArgs, CliRuntime } from "../../types.js";
export function requiredTarget(parsed: CliParsedArgs, index: number, usage: string): string {
const target = parsed.args[index]?.trim();
if (!target) throw new Error(`请提供目标参数: ${usage}`);
return target;
}
export function splitFields(value?: string): string[] {
return value ? value.split(",").map((field) => field.trim()).filter(Boolean) : [];
}
export function cell(value: unknown): string {
if (value === undefined || value === null) return "";
if (typeof value === "object") return JSON.stringify(value);
return String(value);
}
/**
* 在企业全量自建应用中智能反查指定业务对象,避免强制用户/Agent 手动查询并记忆 platformId
*/
async function resolveEntityGlobally(
client: KyInstance,
corpId: string,
target: string
): Promise<{ platform: DatalinkPlatformDetail; entity: DatalinkEntity } | undefined> {
const platforms = await listPlatforms(client, corpId);
const matches: Array<{ platform: any; entity: DatalinkEntity }> = [];
const results = await Promise.allSettled(
platforms.map(async (p) => {
const entities = await listPlatformEntities(client, corpId, p.id);
const found = entities.find((e) =>
[e.id, e.name, e.code].some((val) => typeof val === "string" && val.toLowerCase() === target.toLowerCase())
);
if (found) {
return { platform: p, entity: found };
}
return null;
})
);
for (const r of results) {
if (r.status === "fulfilled" && r.value) {
matches.push(r.value);
}
}
if (matches.length === 1) {
const fullPlatform = await getPlatform(client, corpId, matches[0].platform.id);
return { platform: fullPlatform || matches[0].platform, entity: matches[0].entity };
}
if (matches.length > 1) {
const names = matches.map((m) => `[${m.platform.name}] (${m.platform.id})`).join(", ");
throw new Error(`在多个自建应用中找到同名业务对象 [${target}],请显式指定应用名称: ${names}`);
}
return undefined;
}
export async function resolveAppAndEntity(parsed: CliParsedArgs, runtime: CliRuntime, appIndex = 0, entityIndex = 1) {
const targetA = parsed.args[appIndex]?.trim();
const targetB = parsed.args[entityIndex]?.trim();
if (!targetA) {
throw new Error("请提供自建应用或业务对象参数: hose-use platform <子命令> [应用] <业务对象>");
}
const clients = await createClientsForCorp(runtime.selectedCorp!);
const corpId = runtime.selectedCorp!.credentials.corpId;
// 1. 若提供了两个位置参数,严格按「应用 ➔ 对象」标准两级解析
if (targetB) {
const platform = await resolvePlatform(clients.ekbClient, corpId, targetA);
if (!platform) throw new Error(`[NOT FOUND] 当前企业下未找到自建应用: ${targetA}`);
const entity = await resolveEntity(clients.ekbClient, corpId, platform, targetB);
if (!entity) throw new Error(`[NOT FOUND] 应用 [${platform.name}] 下未找到业务对象: ${targetB}`);
return { clients, corpId, platform, entity };
}
// 2. 若仅提供了一个位置参数:优先尝试按「应用」解析(兼容查看应用级信息,如 entities/audit 等)
const platform = await resolvePlatform(clients.ekbClient, corpId, targetA);
if (platform) {
return { clients, corpId, platform, entity: undefined };
}
// 3. 若按应用未找到,自动降维为「业务对象」在企业全量自建应用中智能反查 (Zero-Config Search)
const globalMatch = await resolveEntityGlobally(clients.ekbClient, corpId, targetA);
if (globalMatch) {
return { clients, corpId, platform: globalMatch.platform, entity: globalMatch.entity };
}
throw new Error(`[NOT FOUND] 当前企业下未找到名为 [${targetA}] 的自建应用或业务对象`);
}