refactor(platform): 拆分服务层到 services.ts,并强化状态与响应 schema
This commit is contained in:
1 parent
c9e0fba265
commit
a4e23263aa
3 files changed
+158
-158
No files matched your search
@@ -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);
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user