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}] 的自建应用或业务对象`); }