feat(platform): 强化工作流节点 JSON schema 约束并修复 MCP inputSchema 生成
- 将 tasks 从 z.array(z.any()) 改为递归 discriminated union - 覆盖 webhook/condition/every/stop/http/flow-detail/code/code-js2/ekuaibao-datalink-filter/create/data-convert/flow-create-task 及兜底 unknown - 使用 Zod v4 内置 toJSONSchema() 替换不兼容的 zod-to-json-schema - 移除 zod-to-json-schema 依赖
This commit is contained in:
1 parent
58c8a92d8e
commit
e583a5faab
4 files changed
+242
-44
No files matched your search
@@ -8,7 +8,6 @@
|
|||||||
"@modelcontextprotocol/sdk": "^1.29.0",
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
||||||
"ky": "^2.0.2",
|
"ky": "^2.0.2",
|
||||||
"zod": "^4.4.3",
|
"zod": "^4.4.3",
|
||||||
"zod-to-json-schema": "^3.25.2",
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "^1.3.14",
|
"@types/bun": "^1.3.14",
|
||||||
|
|||||||
+4
-3
@@ -5,13 +5,14 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "bun run --watch src/server.ts",
|
"dev": "bun run --watch src/server.ts",
|
||||||
"start": "bun run src/server.ts"
|
"start": "bun run src/server.ts",
|
||||||
|
"typecheck": "tsc --noEmit",
|
||||||
|
"export:workflow-schema": "bun run scripts/export-workflow-schema.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@modelcontextprotocol/sdk": "^1.29.0",
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
||||||
"ky": "^2.0.2",
|
"ky": "^2.0.2",
|
||||||
"zod": "^4.4.3",
|
"zod": "^4.4.3"
|
||||||
"zod-to-json-schema": "^3.25.2"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "^1.3.14",
|
"@types/bun": "^1.3.14",
|
||||||
|
|||||||
+229
-11
@@ -30,17 +30,208 @@ export const PlatformSchema = z.object({
|
|||||||
updateTime: z.number().default(0),
|
updateTime: z.number().default(0),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── 工作流任务节点(递归、按类型区分)─────────────────────────────────────────
|
||||||
|
|
||||||
|
export const BaseTaskSchema = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
type: z.string(),
|
||||||
|
label: z.string(),
|
||||||
|
desc: z.string().optional(),
|
||||||
|
groupName: z.string(),
|
||||||
|
orderIndex: z.number(),
|
||||||
|
levelIndex: z.array(z.number()),
|
||||||
|
status: z.string(),
|
||||||
|
displayName: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// 任务节点运行时类型(用于递归结构中的类型标注)
|
||||||
|
export interface WorkflowTask {
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
label?: string;
|
||||||
|
desc?: string;
|
||||||
|
groupName: string;
|
||||||
|
orderIndex: number;
|
||||||
|
levelIndex: number[];
|
||||||
|
status: string;
|
||||||
|
displayName?: string;
|
||||||
|
tasks?: WorkflowTask[];
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 条件/过滤的递归结构:condition / combineFilters
|
||||||
|
export const FilterConditionSchema = z.object({
|
||||||
|
key: z.string(),
|
||||||
|
value: z.any().optional(),
|
||||||
|
compare: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const FilterGroupSchema: z.ZodType<any> = z.lazy(() =>
|
||||||
|
z.object({
|
||||||
|
filters: z.array(z.union([FilterConditionSchema, FilterGroupSchema])),
|
||||||
|
condition: z.string(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 字段映射条目的两种形态:只读 key 或 key-value 映射
|
||||||
|
export const FieldMappingSchema = z.union([
|
||||||
|
z.object({ key: z.string() }),
|
||||||
|
z.object({ key: z.string(), value: z.string() }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 错误处理配置
|
||||||
|
export const OnErrorSchema = z.object({
|
||||||
|
defaultAction: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── 控制节点 ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export const ConditionTaskSchema: z.ZodType<WorkflowTask> = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("condition"),
|
||||||
|
tasksMaxLevel: z.number(),
|
||||||
|
combineFilters: FilterGroupSchema,
|
||||||
|
tasks: z.lazy(() => z.array(WorkflowTaskSchema)),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const EveryTaskSchema: z.ZodType<WorkflowTask> = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("every"),
|
||||||
|
batch: z.boolean(),
|
||||||
|
batchSize: z.number().optional(),
|
||||||
|
mode: z.string(),
|
||||||
|
list: z.string(),
|
||||||
|
filters: z.array(z.any()),
|
||||||
|
tasksMaxLevel: z.number(),
|
||||||
|
tasks: z.lazy(() => z.array(WorkflowTaskSchema)),
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── 触发器节点 ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export const WebhookTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("webhook"),
|
||||||
|
sampleId: z.string(),
|
||||||
|
params: z.array(z.object({ key: z.string(), value: z.string() })),
|
||||||
|
urlPath: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── 动作节点 ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export const StopTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("stop"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const HttpTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("http"),
|
||||||
|
method: z.string(),
|
||||||
|
url: z.string(),
|
||||||
|
bodyType: z.string().optional(),
|
||||||
|
bodyRaw: z.record(z.string(), z.any()).optional(),
|
||||||
|
header: z.array(z.any()),
|
||||||
|
params: z.array(z.object({ key: z.string(), value: z.string() })),
|
||||||
|
successCode: z.array(z.string()),
|
||||||
|
sampleData: z.record(z.string(), z.any()).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const FlowDetailTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("flow-detail"),
|
||||||
|
specificationId: z.string(),
|
||||||
|
mapping: z.union([
|
||||||
|
z.boolean(),
|
||||||
|
z.array(z.object({ key: z.string(), value: z.string() })),
|
||||||
|
]),
|
||||||
|
flowCode: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const CodeTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("code"),
|
||||||
|
code: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const CodeJs2TaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("code-js2"),
|
||||||
|
code: z.string(),
|
||||||
|
mapping: z.array(z.object({ key: z.string(), value: z.string() })).optional(),
|
||||||
|
onError: OnErrorSchema.optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const EkuaibaoDatalinkFilterTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("ekuaibao-datalink-filter"),
|
||||||
|
mode: z.string(),
|
||||||
|
entityId: z.string(),
|
||||||
|
fields: z.array(FieldMappingSchema),
|
||||||
|
filters: z.array(z.any()),
|
||||||
|
selectFields: z.array(z.any()),
|
||||||
|
combineFilters: FilterGroupSchema,
|
||||||
|
orderKey: z.string(),
|
||||||
|
orderMode: z.string(),
|
||||||
|
limitCount: z.string(),
|
||||||
|
onError: OnErrorSchema.optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const EkuaibaoDatalinkCreateTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("ekuaibao-datalink-create"),
|
||||||
|
mode: z.string(),
|
||||||
|
entityId: z.string(),
|
||||||
|
fields: z.array(FieldMappingSchema),
|
||||||
|
filters: z.array(z.any()),
|
||||||
|
combineFilters: FilterGroupSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const DataConvertTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("data-convert"),
|
||||||
|
mode: z.string(),
|
||||||
|
path: z.array(z.string()),
|
||||||
|
fields: z.array(z.object({ key: z.string(), value: z.string() })),
|
||||||
|
onError: OnErrorSchema.optional(),
|
||||||
|
subType: z.string(),
|
||||||
|
itemType: z.string(),
|
||||||
|
targetType: z.string(),
|
||||||
|
feeFormType: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const FlowCreateTaskSchema = BaseTaskSchema.extend({
|
||||||
|
type: z.literal("flow-create-task"),
|
||||||
|
specificationId: z.string(),
|
||||||
|
formState: z.string(),
|
||||||
|
form: z.record(z.string(), z.string()),
|
||||||
|
details: z.record(z.string(), z.any()),
|
||||||
|
onError: OnErrorSchema.optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// 兜底节点:遇到未收录类型时仍保留全部字段
|
||||||
|
export const UnknownTaskSchema = z
|
||||||
|
.object({
|
||||||
|
name: z.string(),
|
||||||
|
type: z.string(),
|
||||||
|
label: z.string().optional(),
|
||||||
|
desc: z.string().optional(),
|
||||||
|
groupName: z.string(),
|
||||||
|
orderIndex: z.number(),
|
||||||
|
levelIndex: z.array(z.number()),
|
||||||
|
status: z.string(),
|
||||||
|
displayName: z.string().optional(),
|
||||||
|
})
|
||||||
|
.passthrough();
|
||||||
|
|
||||||
|
// 统一任务节点类型
|
||||||
|
export const WorkflowTaskSchema: z.ZodType<WorkflowTask> = z.union([
|
||||||
|
WebhookTaskSchema,
|
||||||
|
ConditionTaskSchema,
|
||||||
|
EveryTaskSchema,
|
||||||
|
StopTaskSchema,
|
||||||
|
HttpTaskSchema,
|
||||||
|
FlowDetailTaskSchema,
|
||||||
|
CodeTaskSchema,
|
||||||
|
CodeJs2TaskSchema,
|
||||||
|
EkuaibaoDatalinkFilterTaskSchema,
|
||||||
|
EkuaibaoDatalinkCreateTaskSchema,
|
||||||
|
DataConvertTaskSchema,
|
||||||
|
FlowCreateTaskSchema,
|
||||||
|
UnknownTaskSchema,
|
||||||
|
]);
|
||||||
|
|
||||||
// ── 工作流(EASO Pipeline)───────────────────────────────────
|
// ── 工作流(EASO Pipeline)───────────────────────────────────
|
||||||
|
|
||||||
export const WorkflowTaskSchema = z.object({
|
export const WorkflowTaskSummarySchema = BaseTaskSchema;
|
||||||
name: z.string().default(""),
|
|
||||||
type: z.string().default(""),
|
|
||||||
label: z.string().default(""),
|
|
||||||
desc: z.string().optional(),
|
|
||||||
groupName: z.string().default(""),
|
|
||||||
orderIndex: z.number().default(0),
|
|
||||||
status: z.string().default(""),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const WorkflowSchema = z.object({
|
export const WorkflowSchema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
@@ -50,7 +241,7 @@ export const WorkflowSchema = z.object({
|
|||||||
errorCount: z.number().default(0),
|
errorCount: z.number().default(0),
|
||||||
updateTime: z.string().default(""),
|
updateTime: z.string().default(""),
|
||||||
updateUser: z.string().nullable().optional(),
|
updateUser: z.string().nullable().optional(),
|
||||||
tasks: z.array(z.any()).default([]),
|
tasks: z.array(WorkflowTaskSchema).default([]),
|
||||||
desc: z.string().nullable().optional(),
|
desc: z.string().nullable().optional(),
|
||||||
customDesc: z.string().nullable().optional(),
|
customDesc: z.string().nullable().optional(),
|
||||||
});
|
});
|
||||||
@@ -152,10 +343,33 @@ export const WebhookInfoSchema = z.object({
|
|||||||
active: z.boolean().default(true),
|
active: z.boolean().default(true),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── 跨应用聚合结果类型 ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export const WorkflowSummarySchema = WorkflowSchema.extend({
|
||||||
|
platformId: z.string(),
|
||||||
|
platformName: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const WorkflowListAllErrorSchema = z.object({
|
||||||
|
platformId: z.string(),
|
||||||
|
platformName: z.string(),
|
||||||
|
error: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const TaskMatchSchema = z.object({
|
||||||
|
taskName: z.string(),
|
||||||
|
taskType: z.string(),
|
||||||
|
levelIndex: z.array(z.number()),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const WorkflowWithMatchesSchema = WorkflowSummarySchema.extend({
|
||||||
|
matchedTasks: z.array(TaskMatchSchema),
|
||||||
|
});
|
||||||
|
|
||||||
// ── 类型导出 ──────────────────────────────────────────────────────────────────
|
// ── 类型导出 ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
export type Platform = z.infer<typeof PlatformSchema>;
|
export type Platform = z.infer<typeof PlatformSchema>;
|
||||||
export type WorkflowTask = z.infer<typeof WorkflowTaskSchema>;
|
export type FilterGroup = z.infer<typeof FilterGroupSchema>;
|
||||||
export type Workflow = z.infer<typeof WorkflowSchema>;
|
export type Workflow = z.infer<typeof WorkflowSchema>;
|
||||||
export type WorkflowPage = z.infer<typeof WorkflowPageSchema>;
|
export type WorkflowPage = z.infer<typeof WorkflowPageSchema>;
|
||||||
export type WorkflowStats = z.infer<typeof WorkflowStatsSchema>;
|
export type WorkflowStats = z.infer<typeof WorkflowStatsSchema>;
|
||||||
@@ -166,3 +380,7 @@ export type WorkflowJob = z.infer<typeof WorkflowJobSchema>;
|
|||||||
export type WorkflowDsl = z.infer<typeof WorkflowDslSchema>;
|
export type WorkflowDsl = z.infer<typeof WorkflowDslSchema>;
|
||||||
export type Staff = z.infer<typeof StaffSchema>;
|
export type Staff = z.infer<typeof StaffSchema>;
|
||||||
export type WebhookInfo = z.infer<typeof WebhookInfoSchema>;
|
export type WebhookInfo = z.infer<typeof WebhookInfoSchema>;
|
||||||
|
export type WorkflowSummary = z.infer<typeof WorkflowSummarySchema>;
|
||||||
|
export type WorkflowListAllError = z.infer<typeof WorkflowListAllErrorSchema>;
|
||||||
|
export type TaskMatch = z.infer<typeof TaskMatchSchema>;
|
||||||
|
export type WorkflowWithMatches = z.infer<typeof WorkflowWithMatchesSchema>;
|
||||||
+9
-29
@@ -4,7 +4,6 @@ import {
|
|||||||
CallToolRequestSchema,
|
CallToolRequestSchema,
|
||||||
ListToolsRequestSchema,
|
ListToolsRequestSchema,
|
||||||
} from "@modelcontextprotocol/sdk/types.js";
|
} from "@modelcontextprotocol/sdk/types.js";
|
||||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
||||||
import { loadCorpConfigs } from "./core/config/loader.js";
|
import { loadCorpConfigs } from "./core/config/loader.js";
|
||||||
import { CorpContext } from "./core/config/manager.js";
|
import { CorpContext } from "./core/config/manager.js";
|
||||||
import { createClient } from "./core/http/factory.js";
|
import { createClient } from "./core/http/factory.js";
|
||||||
@@ -12,7 +11,6 @@ import { getEasoClient } from "./core/auth/easo.js";
|
|||||||
import { getPiperClient } from "./core/auth/piper.js";
|
import { getPiperClient } from "./core/auth/piper.js";
|
||||||
import { sealaiTools } from "./domains/sealai/tools.js";
|
import { sealaiTools } from "./domains/sealai/tools.js";
|
||||||
import { platformTools } from "./domains/platform/tools.js";
|
import { platformTools } from "./domains/platform/tools.js";
|
||||||
import * as platformApi from "./domains/platform/api.js";
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
// 加载企业配置
|
// 加载企业配置
|
||||||
@@ -54,18 +52,13 @@ async function main() {
|
|||||||
...sealaiTools.map((t) => ({
|
...sealaiTools.map((t) => ({
|
||||||
name: t.name,
|
name: t.name,
|
||||||
description: t.description,
|
description: t.description,
|
||||||
inputSchema: zodToJsonSchema(t.parameters as any),
|
inputSchema: (t.parameters as any).toJSONSchema(),
|
||||||
})),
|
})),
|
||||||
// Platform 工具
|
// Platform 工具
|
||||||
{
|
|
||||||
name: "platform_list",
|
|
||||||
description: "列出企业下所有启用的自建应用",
|
|
||||||
inputSchema: { type: "object", properties: {} },
|
|
||||||
},
|
|
||||||
...platformTools.map((t) => ({
|
...platformTools.map((t) => ({
|
||||||
name: t.name,
|
name: t.name,
|
||||||
description: t.description,
|
description: t.description,
|
||||||
inputSchema: zodToJsonSchema(t.parameters as any),
|
inputSchema: (t.parameters as any).toJSONSchema(),
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
@@ -122,11 +115,13 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Platform 工具
|
// Platform 工具
|
||||||
if (name === "platform_list") {
|
const platformTool = platformTools.find((t) => t.name === name);
|
||||||
const client = await createClient(currentCorp, "ekb");
|
if (platformTool) {
|
||||||
const result = await platformApi.listPlatforms(
|
const ekbClient = await createClient(currentCorp, "ekb");
|
||||||
client,
|
const { client: easoClient } = await getEasoClient(currentCorp);
|
||||||
currentCorp.credentials.corpId
|
const result = await platformTool.handler(
|
||||||
|
{ corp: currentCorp, ekbClient, easoClient },
|
||||||
|
args as never
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
content: [
|
content: [
|
||||||
@@ -138,21 +133,6 @@ async function main() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Platform 工作流工具(EASO)
|
|
||||||
const easoTool = platformTools.find((t) => t.name === name);
|
|
||||||
if (easoTool) {
|
|
||||||
const { client } = await getEasoClient(currentCorp);
|
|
||||||
const result = await easoTool.handler(client, args as never);
|
|
||||||
return {
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: JSON.stringify(result, null, 2),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content: [
|
content: [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user