feat(platform): 实现跨应用工作流聚合与节点类型查找
- 新增 listAllWorkflows 服务函数:遍历所有启用自建应用汇总工作流 - 新增 findWorkflowsByTaskType 服务函数:递归扫描嵌套 tasks 按类型过滤 - 新增 MCP tools:platform_workflow_list_all、platform_workflow_find_by_task - 失败平台单独记录 errors,不阻断整体结果
This commit is contained in:
1 parent
e583a5faab
commit
5dd280bdc2
1 file changed
+223
-22
+223
-22
@@ -1,11 +1,157 @@
|
||||
import { z } from "zod";
|
||||
import { KyInstance } from "ky";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import * as api from "./api";
|
||||
import {
|
||||
Platform,
|
||||
WorkflowSummary,
|
||||
WorkflowListAllError,
|
||||
WorkflowTask,
|
||||
WorkflowWithMatches,
|
||||
TaskMatch,
|
||||
} from "./types";
|
||||
|
||||
// 注意:platform_list 在 server.ts 中直接处理,不在这里注册 handler
|
||||
// 其余工具需要 EASO/Piper 客户端,handler 签名统一为 (client, params)
|
||||
// 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] };
|
||||
}
|
||||
|
||||
// ── Tool 定义 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
export const platformTools = [
|
||||
{
|
||||
name: "platform_list",
|
||||
description: "列出企业下所有启用的自建应用",
|
||||
parameters: z.object({}),
|
||||
handler: async ({ ekbClient, corp }: PlatformClients) => {
|
||||
return await api.listPlatforms(ekbClient, corp.credentials.corpId);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "platform_workflow_list",
|
||||
description: "列出指定自建应用下的工作流(EASO)",
|
||||
@@ -15,11 +161,11 @@ export const platformTools = [
|
||||
pageSize: z.number().optional().describe("每页数量,默认 20"),
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { platformId: string; page?: number; pageSize?: number }
|
||||
) => {
|
||||
return await api.listWorkflows(
|
||||
client,
|
||||
easoClient,
|
||||
params.platformId,
|
||||
params.page,
|
||||
params.pageSize
|
||||
@@ -32,16 +178,19 @@ export const platformTools = [
|
||||
parameters: z.object({
|
||||
platformId: z.string().describe("自建应用 ID"),
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { platformId: string }) => {
|
||||
return await api.getWorkflowStats(client, params.platformId);
|
||||
handler: async (
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { platformId: string }
|
||||
) => {
|
||||
return await api.getWorkflowStats(easoClient, params.platformId);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "platform_workflow_charge",
|
||||
description: "查询企业工作流用量配额(EASO)",
|
||||
parameters: z.object({}),
|
||||
handler: async (client: KyInstance) => {
|
||||
return await api.getWorkflowCharge(client);
|
||||
handler: async ({ easoClient }: PlatformClients) => {
|
||||
return await api.getWorkflowCharge(easoClient);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -50,8 +199,11 @@ export const platformTools = [
|
||||
parameters: z.object({
|
||||
pipelineId: z.string().describe("工作流 ID"),
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { pipelineId: string }) => {
|
||||
return await api.getWorkflowDetail(client, params.pipelineId);
|
||||
handler: async (
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { pipelineId: string }
|
||||
) => {
|
||||
return await api.getWorkflowDetail(easoClient, params.pipelineId);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -62,11 +214,11 @@ export const platformTools = [
|
||||
state: z.number().describe("状态:1 启用,0 停用"),
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { pipelineId: string; state: number }
|
||||
) => {
|
||||
return await api.toggleWorkflow(
|
||||
client,
|
||||
easoClient,
|
||||
params.pipelineId,
|
||||
params.state as 0 | 1
|
||||
);
|
||||
@@ -80,18 +232,18 @@ export const platformTools = [
|
||||
runType: z.enum(["DEBUG", "PROD"]).optional().describe("运行类型,默认 DEBUG"),
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { pipelineId: string; runType?: "DEBUG" | "PROD" }
|
||||
) => {
|
||||
return await api.listRunningJobs(client, params.pipelineId, params.runType);
|
||||
return await api.listRunningJobs(easoClient, params.pipelineId, params.runType);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "platform_workflow_dsl",
|
||||
description: "获取工作流 DSL 配置列表(Piper)",
|
||||
parameters: z.object({}),
|
||||
handler: async (client: KyInstance) => {
|
||||
return await api.listDsl(client);
|
||||
handler: async ({ easoClient }: PlatformClients) => {
|
||||
return await api.listDsl(easoClient);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -100,8 +252,11 @@ export const platformTools = [
|
||||
parameters: z.object({
|
||||
external: z.boolean().optional().describe("是否包含外部人员,默认 false"),
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { external?: boolean }) => {
|
||||
return await api.listStaffs(client, params.external);
|
||||
handler: async (
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { external?: boolean }
|
||||
) => {
|
||||
return await api.listStaffs(easoClient, params.external);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -110,8 +265,11 @@ export const platformTools = [
|
||||
parameters: z.object({
|
||||
pipelineId: z.string().describe("工作流 ID"),
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { pipelineId: string }) => {
|
||||
return await api.getWebhookInfo(client, params.pipelineId);
|
||||
handler: async (
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { pipelineId: string }
|
||||
) => {
|
||||
return await api.getWebhookInfo(easoClient, params.pipelineId);
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -120,8 +278,51 @@ export const platformTools = [
|
||||
parameters: z.object({
|
||||
pipelineId: z.string().describe("工作流 ID"),
|
||||
}),
|
||||
handler: async (client: KyInstance, params: { pipelineId: string }) => {
|
||||
return await api.sampleWorkflowData(client, params.pipelineId);
|
||||
handler: async (
|
||||
{ easoClient }: PlatformClients,
|
||||
params: { pipelineId: string }
|
||||
) => {
|
||||
return await api.sampleWorkflowData(easoClient, params.pipelineId);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "platform_workflow_list_all",
|
||||
description:
|
||||
"跨所有启用自建应用汇总工作流列表,返回带 platformId/platformName 的扁平结果",
|
||||
parameters: z.object({
|
||||
page: z.number().optional().describe("页码,默认 1"),
|
||||
pageSize: z.number().optional().describe("每页数量,默认 20"),
|
||||
}),
|
||||
handler: async (
|
||||
clients: PlatformClients,
|
||||
params: { page?: number; pageSize?: number }
|
||||
) => {
|
||||
return await listAllWorkflows(clients, params);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "platform_workflow_find_by_task",
|
||||
description:
|
||||
"跨所有启用自建应用查找包含指定节点类型的工作流,递归扫描嵌套 tasks",
|
||||
parameters: z.object({
|
||||
taskType: z.string().describe("节点类型,如 http、code-js2、every、condition"),
|
||||
page: z.number().optional().describe("页码,默认 1"),
|
||||
pageSize: z.number().optional().describe("每页数量,默认 20"),
|
||||
includeDetail: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe("是否先调用详情接口获取完整节点后再匹配,默认 false"),
|
||||
}),
|
||||
handler: async (
|
||||
clients: PlatformClients,
|
||||
params: {
|
||||
taskType: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
includeDetail?: boolean;
|
||||
}
|
||||
) => {
|
||||
return await findWorkflowsByTaskType(clients, params.taskType, params);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
Reference in new issue
Block a user