import type { TaskHost, WorkflowHost } from "../abstracts/index.js"; import type { BaseCtx, TaskMarker } from "../execution/context/types.js"; /** * Converts a union to an intersection. Used for detecting if a type is a union. */ type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; /** * Checks if a type is a union (more than one member). */ type IsUnion = [T] extends [UnionToIntersection] ? false : true; /** * Represents any host instance type. */ export type AnyHost = WorkflowHost | TaskHost; /** * Represents any host constructor type. */ export type AnyHostCtor = WorkflowHostCtor | TaskHostCtor; /** * Represents a workflow host constructor type. * * @internal Used in public signatures but not intended for direct external use. * @template I - The input type that the workflow accepts. */ export type WorkflowHostCtor = abstract new (...args: any[]) => WorkflowHost; /** * Represents a task host constructor type. * * @internal Used in public signatures but not intended for direct external use. * @template I - The input type that the task accepts. */ export type TaskHostCtor = abstract new (...args: any[]) => TaskHost; /** * Checks if a function signature is a "runnable" task method. * * A method is considered runnable if its first parameter is a context type * that includes the TaskMarker brand. This distinguishes actual task methods * (decorated with `@Task()` or `@WorkflowTask()`) from helper methods that * use `HelperCtx` which lacks the marker. * * @template F - The function type to check. * @template C - The expected context type (defaults to any BaseCtx). * @returns `true` if F is a task method, `false` otherwise. */ export type IsTaskRunnableSignature any, C extends BaseCtx = any> = F extends (...args: any[]) => any ? Parameters extends [infer CI, ...any[]] ? CI extends { [K in TaskMarker]: true; } ? CI extends C ? true : false : false : false : false; /** * Extracts method keys from a host that are "runnable" task methods. * * Uses {@link IsTaskRunnableSignature} to filter only methods whose first * parameter has the TaskMarker brand (i.e., uses `TaskCtx` or `WorkflowCtx`). * * For a TaskHost, this should return exactly one key. * For a WorkflowHost, this returns all workflow task method keys. * * @internal Used by WorkflowTaskOpts but not intended for direct external use. * @template T - The host instance type. * @template C - Optional context type constraint. */ export type ContextMethodKeys = any> = { [K in keyof T]: T[K] extends (...args: any) => any ? IsTaskRunnableSignature extends true ? K : never : never; }[keyof T]; /** * Extracts the output (return) type of a specific task method. * * @template T - The host constructor type. * @template M - The method key (must be a valid task method key). * @returns The awaited return type of the method. */ export type AnyTaskOutput>> = InstanceType[M] extends (...args: any) => any ? Awaited[M]>> : never; /** * Represents any task function signature. * * A task function takes a context as its first parameter and returns * a value (sync or async). * * @template C - The context type. * @template O - The output type. */ export type AnyTaskFn, O> = (ctx: C) => Promise | O; /** * Extracts the output type from a task function, but only if it's a valid * task method (has TaskMarker in its context parameter). * * Returns `never` for helper methods that use `HelperCtx`. * This is used by the `parent()` method in workflows to get type-safe * access to parent task outputs. * * @template T - The task function type. */ export type OutputOfTaskFn> = IsTaskRunnableSignature extends true ? Awaited> : never; /** * Extracts the single task method key from a TaskHost. * Since a TaskHost must have exactly one task method, this resolves to that method's key. * * @template T - The task host constructor type. */ export type TaskMethodKey> = ContextMethodKeys, BaseCtx>; /** * Extracts the input type from a TaskHost. * * @template T - The task host constructor type. */ export type TaskInput> = InstanceType extends TaskHost ? I : never; /** * Extracts the output type from the single task method of a TaskHost. * * @template T - The task host constructor type. */ export type TaskOutput> = AnyTaskOutput>; /** * Extracts the input type from a WorkflowHost constructor. * * @template T - The workflow host constructor type. */ export type WorkflowInput> = InstanceType extends WorkflowHost ? I : never; /** * Maps each workflow task method to its output type. * * @template C - The workflow host instance type. * @example * ```ts * // For a workflow with step1() returning { a: string } and step2() returning { b: number } * type Output = WorkflowTasksOutputMap; * // Result: { step1: { a: string }; step2: { b: number } } * ``` */ export type WorkflowTasksOutputMap> = { [K in ContextMethodKeys]: C[K] extends (...a: any[]) => infer R ? Awaited : never; }; /** * Extracts the complete output type of a workflow (map of all task outputs). * * @template T - The workflow host constructor type. */ export type WorkflowOutput> = WorkflowTasksOutputMap>; /** * Error type for invalid TaskHost - shows as a readable string literal in error messages. * * @internal Type-level error message, not intended for direct external use. * @template Reason - The error message to display. */ export type InvalidTaskHost = `Error: ${Reason}`; /** * Error type for invalid WorkflowHost - shows as a readable string literal in error messages. * * @internal Type-level error message, not intended for direct external use. * @template Reason - The error message to display. */ export type InvalidWorkflowHost = `Error: ${Reason}`; /** * Validates that a TaskHost has exactly one task method. * Returns the host type if valid, or an error type with a descriptive message. * * Used by {@link taskRef} to provide compile-time validation. * * @internal Used in taskRef() signature but not intended for direct external use. * @template C - The task host constructor type to validate. */ export type ValidTaskHost> = ContextMethodKeys, BaseCtx> extends infer K ? [K] extends [never] ? InvalidTaskHost<"TaskHost must have exactly one method with TaskCtx parameter"> : IsUnion extends true ? InvalidTaskHost<"TaskHost has multiple task methods - only one is allowed"> : C : never; /** * Validates that a WorkflowHost has at least one workflow task method. * Returns the host type if valid, or an error type with a descriptive message. * * Used by {@link workflowRef} to provide compile-time validation. * * @internal Used in workflowRef() signature but not intended for direct external use. * @template C - The workflow host constructor type to validate. */ export type ValidWorkflowHost> = ContextMethodKeys, BaseCtx> extends infer K ? [K] extends [never] ? InvalidWorkflowHost<"WorkflowHost must have at least one method with WorkflowCtx parameter"> : C : never; export {};