diff --git a/src/domains/platform/services.ts b/src/domains/platform/services.ts new file mode 100644 index 0000000..7524d92 --- /dev/null +++ b/src/domains/platform/services.ts @@ -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 { + 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 { + 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] }; +} diff --git a/src/domains/platform/tools.ts b/src/domains/platform/tools.ts index 0e58b7e..6e6ae58 100644 --- a/src/domains/platform/tools.ts +++ b/src/domains/platform/tools.ts @@ -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 { - 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 { - 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); }, }, { diff --git a/src/domains/platform/types.ts b/src/domains/platform/types.ts index 754ac84..cdee22a 100644 --- a/src/domains/platform/types.ts +++ b/src/domains/platform/types.ts @@ -1,14 +1,5 @@ import { z } from "zod"; -// ── 通用响应包装 ────────────────────────────────────────────────────────────── - -export const EkbResponseSchema = (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 = 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; export type FilterGroup = z.infer; +export type WorkflowState = z.infer; export type Workflow = z.infer; export type WorkflowPage = z.infer; export type WorkflowStats = z.infer; @@ -380,6 +378,7 @@ export type WorkflowJob = z.infer; export type WorkflowDsl = z.infer; export type Staff = z.infer; export type WebhookInfo = z.infer; +export type WorkflowSampleData = z.infer; export type WorkflowSummary = z.infer; export type WorkflowListAllError = z.infer; export type TaskMatch = z.infer;