From f5bb7b95569169f3b4b0f53f86345a0e5d817fbd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 07:41:46 +0000 Subject: [PATCH 1/3] Initial plan From 769f6e9d4517021a7579ed4bac5cedb632418ae9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 07:49:18 +0000 Subject: [PATCH 2/3] fix: update Zod v4 async function schema to use z.custom for proper type inference Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com> --- packages/spec/src/system/events.zod.ts | 14 +++-- packages/spec/src/system/job.zod.ts | 14 +++-- packages/spec/src/system/plugin.zod.ts | 75 ++++++++++++++++---------- 3 files changed, 67 insertions(+), 36 deletions(-) diff --git a/packages/spec/src/system/events.zod.ts b/packages/spec/src/system/events.zod.ts index 6adda635a9c..afe3c1bf42d 100644 --- a/packages/spec/src/system/events.zod.ts +++ b/packages/spec/src/system/events.zod.ts @@ -1,6 +1,10 @@ import { z } from 'zod'; import { EventNameSchema } from '../shared/identifiers.zod'; +// Helper to create async function schema compatible with Zod v4 +const createAsyncFunctionSchema = >(_schema: T) => + z.custom[0]>((fn) => typeof fn === 'function'); + // ========================================== // Event Priority // ========================================== @@ -136,10 +140,12 @@ export const EventHandlerSchema = z.object({ /** * Handler function */ - handler: z.function({ - input: z.tuple([EventSchema]), - output: z.promise(z.void()) - }).describe('Handler function'), + handler: createAsyncFunctionSchema( + z.function({ + input: z.tuple([EventSchema]), + output: z.promise(z.void()) + }) + ).describe('Handler function'), /** * Execution priority diff --git a/packages/spec/src/system/job.zod.ts b/packages/spec/src/system/job.zod.ts index d0e52fe2528..26f5c3405b7 100644 --- a/packages/spec/src/system/job.zod.ts +++ b/packages/spec/src/system/job.zod.ts @@ -1,5 +1,9 @@ import { z } from 'zod'; +// Helper to create async function schema compatible with Zod v4 +const createAsyncFunctionSchema = >(_schema: T) => + z.custom[0]>((fn) => typeof fn === 'function'); + /** * Cron Schedule Schema * Schedule jobs using cron expressions @@ -64,10 +68,12 @@ export const JobSchema = z.object({ id: z.string().describe('Unique job identifier'), name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Job name (snake_case)'), schedule: ScheduleSchema.describe('Job schedule configuration'), - handler: z.function({ - input: z.tuple([]), - output: z.promise(z.void()) - }).describe('Job handler function'), + handler: createAsyncFunctionSchema( + z.function({ + input: z.tuple([]), + output: z.promise(z.void()) + }) + ).describe('Job handler function'), retryPolicy: RetryPolicySchema.optional().describe('Retry policy configuration'), timeout: z.number().int().positive().optional().describe('Timeout in milliseconds'), enabled: z.boolean().default(true).describe('Whether the job is enabled'), diff --git a/packages/spec/src/system/plugin.zod.ts b/packages/spec/src/system/plugin.zod.ts index 28273fdeaa4..40bc776b978 100644 --- a/packages/spec/src/system/plugin.zod.ts +++ b/packages/spec/src/system/plugin.zod.ts @@ -1,5 +1,10 @@ import { z } from 'zod'; +// Helper to create async function schema compatible with Zod v4 +// In Zod v4, z.function with z.promise doesn't infer correctly, so we use z.custom for proper typing +const createAsyncFunctionSchema = >(_schema: T) => + z.custom[0]>((fn) => typeof fn === 'function'); + // We use z.any() for services that are interfaces with methods, // as Zod cannot easily validate function signatures at runtime. export const PluginContextSchema = z.object({ @@ -49,14 +54,18 @@ export const PluginContextSchema = z.object({ input: z.tuple([]).rest(z.any()), output: z.any() }), - set: z.function({ - input: z.tuple([]).rest(z.any()), - output: z.promise(z.void()) - }), - delete: z.function({ - input: z.tuple([]).rest(z.any()), - output: z.promise(z.void()) - }), + set: createAsyncFunctionSchema( + z.function({ + input: z.tuple([]).rest(z.any()), + output: z.promise(z.void()) + }) + ), + delete: createAsyncFunctionSchema( + z.function({ + input: z.tuple([]).rest(z.any()), + output: z.promise(z.void()) + }) + ), }).passthrough().describe('Storage Interface'), i18n: z.object({ @@ -101,30 +110,40 @@ export const PluginContextSchema = z.object({ export type PluginContextData = z.infer; export const PluginLifecycleSchema = z.object({ - onInstall: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onInstall: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onEnable: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onEnable: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onDisable: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onDisable: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onUninstall: z.function({ - input: z.tuple([PluginContextSchema]), - output: z.promise(z.void()) - }).optional(), + onUninstall: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema]), + output: z.promise(z.void()) + }) + ).optional(), - onUpgrade: z.function({ - input: z.tuple([PluginContextSchema, z.string(), z.string()]), - output: z.promise(z.void()) - }).optional(), + onUpgrade: createAsyncFunctionSchema( + z.function({ + input: z.tuple([PluginContextSchema, z.string(), z.string()]), + output: z.promise(z.void()) + }) + ).optional(), }); export type PluginLifecycleHooks = z.infer; From ad25ab9e6ad896371ff20cc843bdde72ed8b883a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 07:50:49 +0000 Subject: [PATCH 3/3] refactor: extract createAsyncFunctionSchema to shared utility module Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com> --- packages/spec/src/shared/index.ts | 1 + packages/spec/src/shared/zod-helpers.ts | 24 ++++++++++++++++++++++++ packages/spec/src/system/events.zod.ts | 5 +---- packages/spec/src/system/job.zod.ts | 5 +---- packages/spec/src/system/plugin.zod.ts | 6 +----- 5 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 packages/spec/src/shared/zod-helpers.ts diff --git a/packages/spec/src/shared/index.ts b/packages/spec/src/shared/index.ts index efa0e42b508..ffc74e35473 100644 --- a/packages/spec/src/shared/index.ts +++ b/packages/spec/src/shared/index.ts @@ -6,3 +6,4 @@ export * from './identifiers.zod'; export * from './mapping.zod'; export * from './http.zod'; +export * from './zod-helpers'; diff --git a/packages/spec/src/shared/zod-helpers.ts b/packages/spec/src/shared/zod-helpers.ts new file mode 100644 index 00000000000..877a5c45e39 --- /dev/null +++ b/packages/spec/src/shared/zod-helpers.ts @@ -0,0 +1,24 @@ +import { z } from 'zod'; + +/** + * Helper to create async function schema compatible with Zod v4. + * + * In Zod v4, z.function with z.promise doesn't correctly infer async function types. + * This helper wraps the function schema with z.custom to ensure proper TypeScript + * type inference for async functions. + * + * @example + * ```ts + * const asyncFn = createAsyncFunctionSchema( + * z.function({ + * input: z.tuple([z.string()]), + * output: z.promise(z.number()) + * }) + * ); + * // Type is inferred as: (arg: string) => Promise + * ``` + * + * @see https://github.com/colinhacks/zod/issues/4143 + */ +export const createAsyncFunctionSchema = >(_schema: T) => + z.custom[0]>((fn) => typeof fn === 'function'); diff --git a/packages/spec/src/system/events.zod.ts b/packages/spec/src/system/events.zod.ts index afe3c1bf42d..21b182629b4 100644 --- a/packages/spec/src/system/events.zod.ts +++ b/packages/spec/src/system/events.zod.ts @@ -1,9 +1,6 @@ import { z } from 'zod'; import { EventNameSchema } from '../shared/identifiers.zod'; - -// Helper to create async function schema compatible with Zod v4 -const createAsyncFunctionSchema = >(_schema: T) => - z.custom[0]>((fn) => typeof fn === 'function'); +import { createAsyncFunctionSchema } from '../shared/zod-helpers'; // ========================================== // Event Priority diff --git a/packages/spec/src/system/job.zod.ts b/packages/spec/src/system/job.zod.ts index 26f5c3405b7..ade743531f4 100644 --- a/packages/spec/src/system/job.zod.ts +++ b/packages/spec/src/system/job.zod.ts @@ -1,8 +1,5 @@ import { z } from 'zod'; - -// Helper to create async function schema compatible with Zod v4 -const createAsyncFunctionSchema = >(_schema: T) => - z.custom[0]>((fn) => typeof fn === 'function'); +import { createAsyncFunctionSchema } from '../shared/zod-helpers'; /** * Cron Schedule Schema diff --git a/packages/spec/src/system/plugin.zod.ts b/packages/spec/src/system/plugin.zod.ts index 40bc776b978..05745c28c4f 100644 --- a/packages/spec/src/system/plugin.zod.ts +++ b/packages/spec/src/system/plugin.zod.ts @@ -1,9 +1,5 @@ import { z } from 'zod'; - -// Helper to create async function schema compatible with Zod v4 -// In Zod v4, z.function with z.promise doesn't infer correctly, so we use z.custom for proper typing -const createAsyncFunctionSchema = >(_schema: T) => - z.custom[0]>((fn) => typeof fn === 'function'); +import { createAsyncFunctionSchema } from '../shared/zod-helpers'; // We use z.any() for services that are interfaces with methods, // as Zod cannot easily validate function signatures at runtime.