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] };
}
+9 -147
View File
@@ -1,145 +1,11 @@
import { z } from "zod";
import { KyInstance } from "ky";
import { CorpConfig } from "../../core/config/types.js";
import * as api from "./api";
import * as api from "./api.js";
import {
Platform,
WorkflowSummary,
WorkflowListAllError,
WorkflowTask,
WorkflowWithMatches,
TaskMatch,
} from "./types";
// Platform 域需要的客户端集合。部分工具需要同时访问 EKB 主站(自建应用列表)
// 和 EASO(工作流接口),因此不再只传单个 client。
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] };
}
PlatformClients,
listAllWorkflows,
findWorkflowsByTaskType,
} from "./services.js";
import { WorkflowStateSchema } from "./types.js";
// ── Tool 定义 ─────────────────────────────────────────────────────────────────
@@ -211,17 +77,13 @@ export const platformTools = [
description: "启用或停用工作流(Piper)。state=1 启用,state=0 停用",
parameters: z.object({
pipelineId: z.string().describe("工作流 ID"),
state: z.number().describe("状态:1 启用,0 停用"),
state: WorkflowStateSchema.describe("状态:1 启用,0 停用"),
}),
handler: async (
{ easoClient }: PlatformClients,
params: { pipelineId: string; state: number }
params: { pipelineId: string; state: 0 | 1 }
) => {
return await api.toggleWorkflow(
easoClient,
params.pipelineId,
params.state as 0 | 1
);
return await api.toggleWorkflow(easoClient, params.pipelineId, params.state);
},
},
{
+10 -11
View File
@@ -1,14 +1,5 @@
import { z } from "zod";
// ── 通用响应包装 ──────────────────────────────────────────────────────────────
export const EkbResponseSchema = <T extends z.ZodType>(dataSchema: T) =>
z.object({
code: z.number(),
msg: z.string().default(""),
data: dataSchema.optional(),
});
// ── 自建应用(DataLink Platform)─────────────────────────────
export const PlatformSchema = z.object({
@@ -231,8 +222,6 @@ export const WorkflowTaskSchema: z.ZodType<WorkflowTask> = z.union([
// ── 工作流(EASO Pipeline)───────────────────────────────────
export const WorkflowTaskSummarySchema = BaseTaskSchema;
export const WorkflowSchema = z.object({
id: z.string(),
pipelineName: z.string().default(""),
@@ -343,6 +332,14 @@ export const WebhookInfoSchema = z.object({
active: z.boolean().default(true),
});
// ── 工作流状态 ────────────────────────────────────────────────────────────────
export const WorkflowStateSchema = z.union([z.literal(0), z.literal(1)]);
// ── 测试/采样数据 ─────────────────────────────────────────────────────────────
export const WorkflowSampleDataSchema = z.record(z.string(), z.any());
// ── 跨应用聚合结果类型 ───────────────────────────────────────────────────────
export const WorkflowSummarySchema = WorkflowSchema.extend({
@@ -370,6 +367,7 @@ export const WorkflowWithMatchesSchema = WorkflowSummarySchema.extend({
export type Platform = z.infer<typeof PlatformSchema>;
export type FilterGroup = z.infer<typeof FilterGroupSchema>;
export type WorkflowState = z.infer<typeof WorkflowStateSchema>;
export type Workflow = z.infer<typeof WorkflowSchema>;
export type WorkflowPage = z.infer<typeof WorkflowPageSchema>;
export type WorkflowStats = z.infer<typeof WorkflowStatsSchema>;
@@ -380,6 +378,7 @@ export type WorkflowJob = z.infer<typeof WorkflowJobSchema>;
export type WorkflowDsl = z.infer<typeof WorkflowDslSchema>;
export type Staff = z.infer<typeof StaffSchema>;
export type WebhookInfo = z.infer<typeof WebhookInfoSchema>;
export type WorkflowSampleData = z.infer<typeof WorkflowSampleDataSchema>;
export type WorkflowSummary = z.infer<typeof WorkflowSummarySchema>;
export type WorkflowListAllError = z.infer<typeof WorkflowListAllErrorSchema>;
export type TaskMatch = z.infer<typeof TaskMatchSchema>;