44 Flow ,
55 FunctionDefinition ,
66 NodeFunction ,
7- NodeFunctionIdWrapper ,
7+ SubFlowValue ,
88 NodeParameter ,
9- ReferenceValue
9+ ReferenceValue , Maybe
1010} from "@code0-tech/sagittarius-graphql-types" ;
1111import ts from "typescript" ;
1212import { createSystem , createVirtualTypeScriptEnvironment , VirtualTypeScriptEnvironment } from "@typescript/vfs"
@@ -109,20 +109,20 @@ export function generateFlowSourceCode(
109109) : string {
110110 const nodes = flow ?. nodes ?. nodes || [ ] ;
111111 const funcMap = new Map ( functions ?. map ( f => [ f . identifier , f ] ) ) ;
112- const visited = new Set < string > ( ) ;
112+ const visited = new Set < NodeFunction [ 'id' ] > ( ) ;
113113
114- const generateNodeCode = ( nodeId : string , indent : string = "" ) : string => {
115- const node = nodes . find ( n => n ?. id === nodeId ) ;
114+ const generateNodeCode = ( id : NodeFunction [ 'id' ] | FunctionDefinition [ 'identifier' ] , indent : string = "" ) : string => {
115+ const node = nodes . find ( n => n ?. id === id ) ;
116116 if ( ! node || ! node . functionDefinition ) return "" ;
117- visited . add ( nodeId ) ;
117+ if ( id ?. includes ( "NodeFunction" ) ) visited . add ( id as NodeFunction [ 'id' ] ) ;
118118
119119 const funcDef = funcMap . get ( node . functionDefinition . identifier ) ;
120120 if ( ! funcDef ) return `${ indent } // Error: Function ${ node . functionDefinition . identifier } not found\n` ;
121121
122122 const params = ( node . parameters ?. nodes as NodeParameter [ ] ) || [ ] ;
123123 const args = params . map ( ( p , index ) => {
124124 const val = p . value ;
125- if ( ! val ) return isForInference ? `/* @pos ${ nodeId } ${ index } */ {}` : `/* @pos ${ nodeId } ${ index } */ undefined` ;
125+ if ( ! val ) return isForInference ? `/* @pos ${ id } ${ index } */ {}` : `/* @pos ${ id } ${ index } */ undefined` ;
126126 if ( val . __typename === "ReferenceValue" ) {
127127 const ref = val as ReferenceValue ;
128128 let refCode = typeof ref . inputIndex === "number"
@@ -131,19 +131,19 @@ export function generateFlowSourceCode(
131131 ref . referencePath ?. forEach ( pathObj => {
132132 refCode += `?.${ pathObj . path } ` ;
133133 } ) ;
134- return `/* @pos ${ nodeId } ${ index } */ ${ refCode } ` ;
134+ return `/* @pos ${ id } ${ index } */ ${ refCode } ` ;
135135 }
136136 if ( val . __typename === "LiteralValue" ) {
137137 const jsonString = stringify ( val ?. value )
138- return `/* @pos ${ nodeId } ${ index } */ ${ jsonString } ` ;
138+ return `/* @pos ${ id } ${ index } */ ${ jsonString } ` ;
139139 }
140- if ( val . __typename === "NodeFunctionIdWrapper " ) {
141- const wrapper = val as NodeFunctionIdWrapper ;
142- const lambdaArgName = `p_${ sanitizeId ( nodeId ) } _${ index } ` ;
143- const subTreeCode = generateNodeCode ( wrapper . id ! , indent + " " ) ;
144- return `/* @pos ${ nodeId } ${ index } */ (...${ lambdaArgName } ) => {\n${ subTreeCode } ${ indent } }` ;
140+ if ( val . __typename === "SubFlowValue " ) {
141+ const wrapper = val as SubFlowValue ;
142+ const lambdaArgName = `p_${ sanitizeId ( id as string ) } _${ index } ` ;
143+ const subTreeCode = generateNodeCode ( wrapper . startingNodeId || wrapper . functionDefinition ?. id ! , indent + " " ) ;
144+ return `/* @pos ${ id } ${ index } */ (...${ lambdaArgName } ) => {\n${ subTreeCode } ${ indent } }` ;
145145 }
146- return isForInference ? `/* @pos ${ nodeId } ${ index } */ {}` : `/* @pos ${ nodeId } ${ index } */ undefined` ;
146+ return isForInference ? `/* @pos ${ id } ${ index } */ {}` : `/* @pos ${ id } ${ index } */ undefined` ;
147147 } ) ;
148148
149149 const varName = `node_${ sanitizeId ( node . id ! ) } ` ;
@@ -155,21 +155,21 @@ export function generateFlowSourceCode(
155155 let code = `${ indent } ` ;
156156
157157 if ( node . functionDefinition . identifier === "std::control::return" ) {
158- code += `return /* @pos ${ nodeId } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
158+ code += `return /* @pos ${ id } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
159159 } else if ( node . functionDefinition . identifier === "std::control::if" ) {
160- code += `const ${ varName } = /* @pos ${ nodeId } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
160+ code += `const ${ varName } = /* @pos ${ id } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
161161 code += `if(${ args [ 0 ] } ) {
162- ${ generateNodeCode ( ( node . parameters ?. nodes ?. [ 1 ] ?. value as NodeFunctionIdWrapper ) ?. id ! , indent + " " ) }
162+ ${ generateNodeCode ( ( ( node . parameters ?. nodes ?. [ 1 ] ?. value as SubFlowValue ) ?. startingNodeId || ( node . parameters ?. nodes ?. [ 1 ] ?. value as SubFlowValue ) ?. functionDefinition ?. identifier ) , indent + " " ) }
163163 }`
164164 } else if ( node . functionDefinition . identifier === "std::control::if_else" ) {
165- code += `const ${ varName } = /* @pos ${ nodeId } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
165+ code += `const ${ varName } = /* @pos ${ id } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
166166 code += `if(${ args [ 0 ] } ) {
167- ${ generateNodeCode ( ( node . parameters ?. nodes ?. [ 1 ] ?. value as NodeFunctionIdWrapper ) ?. id ! , indent + " " ) }
167+ ${ generateNodeCode ( ( ( node . parameters ?. nodes ?. [ 1 ] ?. value as SubFlowValue ) ?. startingNodeId || ( node . parameters ?. nodes ?. [ 1 ] ?. value as SubFlowValue ) ?. functionDefinition ?. identifier ) , indent + " " ) }
168168 } else {
169- ${ generateNodeCode ( ( node . parameters ?. nodes ?. [ 2 ] ?. value as NodeFunctionIdWrapper ) ?. id ! , indent + " " ) }
169+ ${ generateNodeCode ( ( ( node . parameters ?. nodes ?. [ 2 ] ?. value as SubFlowValue ) ?. startingNodeId || ( node . parameters ?. nodes ?. [ 2 ] ?. value as SubFlowValue ) ?. functionDefinition ?. identifier ) , indent + " " ) }
170170 }`
171171 } else {
172- code += `const ${ varName } = /* @pos ${ nodeId } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
172+ code += `const ${ varName } = /* @pos ${ id } null */ ${ funcName } (${ args . join ( ", " ) } )${ needsAnyCast ? "" : "" } ;\n`
173173 }
174174
175175 if ( node . nextNodeId ) code += generateNodeCode ( node . nextNodeId , indent ) ;
@@ -181,9 +181,9 @@ export function generateFlowSourceCode(
181181 const funcDeclarations = functions ?. map ( f => `declare function fn_${ f . identifier ?. replace ( / : : / g, '_' ) } ${ f . signature } ` ) . join ( '\n' ) ;
182182
183183 const nextNodeIds = new Set ( nodes . map ( n => n ?. nextNodeId ) . filter ( id => ! ! id ) ) ;
184- const subTreeIds = new Set < string > ( ) ;
185- nodes . forEach ( n => n ?. parameters ?. nodes ?. forEach ( ( p : any ) => {
186- if ( p ?. value ?. __typename === "NodeFunctionIdWrapper " && p . value . id ) subTreeIds . add ( p . value . id ) ;
184+ const subTreeIds = new Set < NodeFunction [ 'id' ] | FunctionDefinition [ 'id' ] > ( ) ;
185+ nodes . forEach ( n => n ?. parameters ?. nodes ?. forEach ( ( p : Maybe < NodeParameter > ) => {
186+ if ( p ?. value ?. __typename === "SubFlowValue " && ( p . value . startingNodeId || p . value . functionDefinition ?. id ) ) subTreeIds . add ( p . value . startingNodeId || p . value . functionDefinition ? .id ) ;
187187 } ) ) ;
188188
189189 const flowCode = flow ? `const flow_${ sanitizeId ( flow . id ?? "" ) } = /* @pos null null */ flow(${ flow . settings ?. nodes ?. map ( ( setting , index ) => `/* @pos null ${ index } */ ${ stringify ( setting ?. value ) } ` ) . join ( ", " ) ?? "" } );` : ""
0 commit comments