refactor(platform): 拆分服务层到 services.ts,并强化状态与响应 schema

This commit is contained in:
gitea committed 2026-07-17 17:33:08 +08:00
1 parent c9e0fba265
commit a4e23263aa
3 files changed
+158 -158

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
import { KyInstance } from "ky";
import { CorpConfig } from "../../core/config/types.js";
import * as api from "./api.js";
import {
Platform,
WorkflowSummary,
WorkflowListAllError,
WorkflowTask,
WorkflowWithMatches,
TaskMatch,
} from "./types.js";
export interface PlatformClients {
corp: CorpConfig;
ekbClient: KyInstance;
easoClient: KyInstance;
}
// ── 服务层:跨应用聚合 ───────────────────────────────────────────────────────
export interface ListAllWorkflowsOptions {
page?: number;
pageSize?: number;
}
export interface ListAllWorkflowsResult {
workflows: WorkflowSummary[];
errors: WorkflowListAllError[];
}
export async function listAllWorkflows(
clients: PlatformClients,
options: ListAllWorkflowsOptions = {}
): Promise<ListAllWorkflowsResult> {
const { ekbClient, easoClient, corp } = clients;
const page = options.page ?? 1;
const pageSize = options.pageSize ?? 20;
const platforms = await api.listPlatforms(ekbClient, corp.credentials.corpId);
const enabledPlatforms = platforms.filter((p) => p.active !== false);
const workflows: WorkflowSummary[] = [];
const errors: WorkflowListAllError[] = [];
await Promise.all(
enabledPlatforms.map(async (platform) => {
try {
const pageResult = await api.listWorkflows(
easoClient,
platform.id,
page,
pageSize
);
for (const workflow of pageResult.pipelineList) {
workflows.push({
...workflow,
platformId: platform.id,
platformName: platform.name,
});
}
} catch (err) {
errors.push({
platformId: platform.id,
platformName: platform.name,
error: err instanceof Error ? err.message : String(err),
});
}
})
);
return { workflows, errors };
}
function collectTasksByType(
tasks: WorkflowTask[],
taskType: string,
matches: TaskMatch[] = []
): TaskMatch[] {
for (const task of tasks) {
if (task.type === taskType) {
matches.push({
taskName: task.name,
taskType: task.type,
levelIndex: task.levelIndex,
});
}
if ("tasks" in task && Array.isArray(task.tasks)) {
collectTasksByType(task.tasks as WorkflowTask[], taskType, matches);
}
}
return matches;
}
export interface FindWorkflowsByTaskTypeOptions extends ListAllWorkflowsOptions {
includeDetail?: boolean;
}
export interface FindWorkflowsByTaskTypeResult {
workflows: WorkflowWithMatches[];
errors: WorkflowListAllError[];
}
export async function findWorkflowsByTaskType(
clients: PlatformClients,
taskType: string,
options: FindWorkflowsByTaskTypeOptions = {}
): Promise<FindWorkflowsByTaskTypeResult> {
const { easoClient } = clients;
const { includeDetail, ...listOptions } = options;
const all = await listAllWorkflows(clients, listOptions);
const matched: WorkflowWithMatches[] = [];
const detailErrors: WorkflowListAllError[] = [];
for (const workflow of all.workflows) {
let tasks = workflow.tasks;
if (includeDetail) {
try {
const detail = await api.getWorkflowDetail(easoClient, workflow.id);
tasks = detail.tasks;
} catch (err) {
detailErrors.push({
platformId: workflow.platformId,
platformName: workflow.platformName,
error: err instanceof Error ? err.message : String(err),
});
continue;
}
}
const matchedTasks = collectTasksByType(tasks, taskType);
if (matchedTasks.length > 0) {
matched.push({ ...workflow, matchedTasks });
}
}
return { workflows: matched, errors: [...all.errors, ...detailErrors] };
}