@@ -734,13 +734,19 @@ type Prettify<T> = {
734
734
[ P in keyof T ] : T [ P ] ;
735
735
} & { } ;
736
736
737
+ export type PromptAwaitedReturn < T > = Exclude < Awaited < T > , symbol > ;
738
+
737
739
export type PromptGroupAwaitedReturn < T > = Prettify < {
738
- [ P in keyof T ] : Exclude < Awaited < T [ P ] > , symbol > ;
740
+ [ P in keyof T ] : PromptAwaitedReturn < T [ P ] > ;
739
741
} > ;
740
742
741
- export type PromptWithOptions < TResults , TReturn > = ( opts : {
742
- results : PromptGroupAwaitedReturn < TResults > ;
743
- } ) => TReturn ;
743
+ export type PromptWithOptions < TResults , TResult , TOptions extends Record < string , unknown > = { } > = (
744
+ opts : Prettify <
745
+ {
746
+ results : PromptGroupAwaitedReturn < TResults > ;
747
+ } & TOptions
748
+ >
749
+ ) => TResult ;
744
750
745
751
export type PromptGroup < T > = {
746
752
[ P in keyof T ] : PromptWithOptions < Partial < Omit < T , P > > , void | Promise < T [ P ] | void > > ;
@@ -821,42 +827,108 @@ type NextWorkflowBuilder<
821
827
TKey extends string ,
822
828
TResult ,
823
829
> = WorkflowBuilder <
824
- {
825
- [ Key in keyof TResults ] : Key extends TKey ? TResult : TResults [ Key ] ;
826
- } & {
827
- [ Key in TKey ] : TResult ;
828
- }
830
+ Prettify <
831
+ {
832
+ [ Key in keyof TResults ] : Key extends TKey ? TResult : TResults [ Key ] ;
833
+ } & {
834
+ [ Key in TKey as undefined extends TResult ? never : TKey ] : TResult ;
835
+ } & {
836
+ [ Key in TKey as undefined extends TResult ? TKey : never ] ?: TResult ;
837
+ }
838
+ >
829
839
> ;
830
840
841
+ type WorkflowStep < TName extends string , TResults , TResult = unknown > = {
842
+ name : TName ;
843
+ prompt : PromptWithOptions < TResults , TResult > ;
844
+ setResult : boolean ;
845
+ condition ?: PromptWithOptions < TResults , boolean > ;
846
+ } ;
847
+
831
848
class WorkflowBuilder < TResults extends Record < string , unknown > = { } > {
832
849
private results : TResults = { } as TResults ;
833
- private prompts : Record < string , PromptWithOptions < TResults , unknown > > = { } ;
850
+ private steps : WorkflowStep < string , TResults > [ ] = [ ] ;
834
851
private cancelCallback : PromptWithOptions < Partial < TResults > , void > | undefined ;
835
852
836
- public step < TKey extends string , TResult > (
837
- key : TKey extends keyof TResults ? never : TKey ,
853
+ public step < TName extends string , TResult > (
854
+ name : TName extends keyof TResults ? never : TName ,
838
855
prompt : PromptWithOptions < TResults , TResult >
839
- ) : NextWorkflowBuilder < TResults , TKey , TResult > {
840
- this . prompts [ key ] = prompt ;
841
- return this as NextWorkflowBuilder < TResults , TKey , TResult > ;
856
+ ) : NextWorkflowBuilder < TResults , TName , PromptAwaitedReturn < TResult > > {
857
+ this . steps . push ( { name, prompt, setResult : true } ) ;
858
+ return this as any ;
859
+ }
860
+
861
+ public conditionalStep < TName extends string , TResult > (
862
+ name : TName ,
863
+ condition : PromptWithOptions < TResults , boolean > ,
864
+ prompt : PromptWithOptions < TResults , TResult >
865
+ ) : NextWorkflowBuilder <
866
+ TResults ,
867
+ TName ,
868
+ | ( TName extends keyof TResults ? TResults [ TName ] : never )
869
+ | PromptAwaitedReturn < TResult >
870
+ | undefined
871
+ > {
872
+ this . steps . push ( { name, prompt, condition, setResult : true } ) ;
873
+ return this as any ;
874
+ }
875
+
876
+ public forkStep < TName extends string , TResult extends Record < string , unknown > > (
877
+ name : TName ,
878
+ condition : PromptWithOptions < TResults , boolean > ,
879
+ subWorkflow : PromptWithOptions < TResults , WorkflowBuilder < TResult > >
880
+ ) : NextWorkflowBuilder <
881
+ TResults ,
882
+ TName ,
883
+ ( TName extends keyof TResults ? TResults [ TName ] : never ) | TResult | undefined
884
+ > {
885
+ this . steps . push ( {
886
+ name,
887
+ prompt : ( { results } ) => {
888
+ return subWorkflow ( { results } ) . run ( ) ;
889
+ } ,
890
+ condition,
891
+ setResult : true ,
892
+ } ) ;
893
+ return this as any ;
894
+ }
895
+
896
+ public logStep (
897
+ name : string ,
898
+ prompt : PromptWithOptions < TResults , void >
899
+ ) : WorkflowBuilder < TResults > {
900
+ this . steps . push ( { name, prompt, setResult : false } ) ;
901
+ return this ;
902
+ }
903
+
904
+ public customStep < TName extends string , TResult > (
905
+ step : WorkflowStep < TName , TResults , TResult >
906
+ ) : NextWorkflowBuilder < TResults , TName , PromptAwaitedReturn < TResult > > {
907
+ this . steps . push ( step ) ;
908
+ return this as any ;
842
909
}
843
910
844
911
public onCancel ( cb : PromptWithOptions < Partial < TResults > , void > ) : WorkflowBuilder < TResults > {
845
912
this . cancelCallback = cb ;
846
913
return this ;
847
914
}
848
915
849
- public async run ( ) : Promise < PromptGroupAwaitedReturn < TResults > > {
850
- for ( const [ key , prompt ] of Object . entries ( this . prompts ) ) {
851
- const result = await prompt ( { results : this . results as any } ) ;
916
+ public async run ( ) : Promise < TResults > {
917
+ for ( const step of this . steps ) {
918
+ if ( step . condition && ! step . condition ( { results : this . results as any } ) ) {
919
+ continue ;
920
+ }
921
+ const result = await step . prompt ( { results : this . results as any } ) ;
852
922
if ( isCancel ( result ) ) {
853
923
this . cancelCallback ?.( { results : this . results as any } ) ;
854
924
continue ;
855
925
}
856
- //@ts -ignore
857
- this . results [ key ] = result ;
926
+ if ( step . setResult ) {
927
+ //@ts -ignore
928
+ this . results [ step . name ] = result ;
929
+ }
858
930
}
859
- return this . results as PromptGroupAwaitedReturn < TResults > ;
931
+ return this . results ;
860
932
}
861
933
}
862
934
0 commit comments