import type { AxAIService } from '../ai/types.js'; import type { AxGen } from '../dsp/generate.js'; import type { AxFieldValue, AxProgramForwardOptions, AxProgrammable, ParseSignature, } from '../dsp/types.js'; // Type for state object that flows through the pipeline export type AxFlowState = Record; // Type for node definitions in the flow export interface AxFlowNodeDefinition { inputs: Record; outputs: Record; } // Type for flow step functions export type AxFlowStepFunction = ( state: AxFlowState, context: Readonly<{ mainAi: AxAIService; mainOptions?: AxProgramForwardOptions; }> ) => Promise | AxFlowState; // Type for dynamic context overrides export interface AxFlowDynamicContext> { ai?: T; options?: AxProgramForwardOptions< NonNullable>[number]['key'] >; } // Helper type to extract input type from an AxProgrammable instance (including AxGen) export type GetGenIn> = T extends AxProgrammable ? IN : never; // Helper type to extract output type from an AxProgrammable instance (including AxGen) export type GetGenOut> = T extends AxProgrammable ? OUT : never; // Helper type to create an AxGen type from a signature string // Uses ParseSignature to extract proper input/output types export type InferAxGen = AxGen< ParseSignature['inputs'], ParseSignature['outputs'] >; // Helper type to create result key name from node name export type NodeResultKey = `${TNodeName}Result`; // Helper type to add node result to state export type AddNodeResult< TState extends AxFlowState, TNodeName extends string, TNodeOut, > = TState & { [K in NodeResultKey]: TNodeOut }; /** * Interface for flows that can be tuned, executed, and used in compositions. * Provides methods for building and executing complex AI workflows. */ export interface AxFlowable extends AxProgrammable {} // Type for parallel branch functions with typed context // NOTE: The `any` here is necessary because we need to support AxProgrammable with any input/output types export type AxFlowTypedParallelBranch< TNodes extends Record>, TState extends AxFlowState, > = ( subFlow: AxFlowTypedSubContext ) => AxFlowTypedSubContext; // Type for typed sub-flow context used in parallel execution // NOTE: The `any` here is necessary for the same reason as above export interface AxFlowTypedSubContext< TNodes extends Record>, TState extends AxFlowState, > { execute< TNodeName extends keyof TNodes & string, TAI extends Readonly, >( nodeName: TNodeName, mapping: (state: TState) => GetGenIn, dynamicContext?: AxFlowDynamicContext ): AxFlowTypedSubContext< TNodes, AddNodeResult> >; map( transform: (state: TState) => TNewState ): AxFlowTypedSubContext; executeSteps( initialState: TState, context: Readonly<{ mainAi: AxAIService; mainOptions?: AxProgramForwardOptions; }> ): Promise; } // Legacy untyped interfaces for backward compatibility export type AxFlowParallelBranch = ( subFlow: AxFlowSubContext ) => AxFlowSubContext; export interface AxFlowSubContext { execute>( nodeName: string, mapping: (state: AxFlowState) => Record, dynamicContext?: AxFlowDynamicContext ): this; map(transform: (state: AxFlowState) => AxFlowState): this; executeSteps>( initialState: AxFlowState, context: Readonly<{ mainAi: TAI; mainOptions?: AxProgramForwardOptions< NonNullable>[number]['key'] >; }> ): Promise; } // Type for branch context export interface AxFlowBranchContext { predicate: (state: AxFlowState) => unknown; branches: Map; currentBranchValue?: unknown; } // Type for execution step metadata export interface AxFlowExecutionStep { type: 'execute' | 'map' | 'merge' | 'parallel-map' | 'parallel' | 'derive'; nodeName?: string; dependencies: string[]; produces: string[]; stepFunction: AxFlowStepFunction; stepIndex: number; } // Type for parallel execution groups export interface AxFlowParallelGroup { level: number; steps: AxFlowExecutionStep[]; } // Configuration for automatic parallelization export interface AxFlowAutoParallelConfig { enabled: boolean; batchSize?: number; }