@@ -139,45 +139,11 @@ const getAllChatflowsbyUserId = async (userid: string, type?: ChatflowType): Pro
139139 const appServer = getRunningExpressApp ( )
140140
141141 // Use find with a where condition to filter by userid
142- let dbResponse = await appServer . AppDataSource . getRepository ( ChatFlow ) . find ( {
142+ const dbResponse = await appServer . AppDataSource . getRepository ( ChatFlow ) . find ( {
143143 where : {
144144 userid : userid , // Filter by the specific userid
145145 } ,
146146 } )
147-
148- // If no chatflows are found for the user, create a new one based from sample workflow
149- if ( dbResponse . length === 0 ) {
150- // URL to fetch the sample workflow
151- const url = 'https://raw.githubusercontent.com/opea-project/GenAIStudio/refs/heads/main/sample-workflows/sample_workflow_chatqna.json' ;
152-
153- try {
154- // Fetch and parse the JSON data from the URL
155- const response = await axios . get ( url ) ;
156- const parsedFlowData = response . data ;
157-
158- // Create a new chatflow with the flowData from the URL
159- const newChatflow : Partial < ChatFlow > = {
160- userid : userid ,
161- name : 'sample-chatqna' ,
162- flowData : JSON . stringify ( parsedFlowData ) ,
163- type : 'OPEA' ,
164- deployed : false ,
165- isPublic : false
166- } ;
167-
168- // Call the importChatflows function to insert the new chatflow
169- await importChatflows ( [ newChatflow ] ) ;
170- } catch ( error ) {
171- throw new Error ( 'Failed to import sample chatflow' ) ;
172- }
173-
174- // Rerun the find query to fetch the latest state of chatflows after insertion
175- dbResponse = await appServer . AppDataSource . getRepository ( ChatFlow ) . find ( {
176- where : {
177- userid : userid ,
178- } ,
179- } ) ;
180- }
181147
182148 // Filter further by type if needed
183149 if ( type ) {
@@ -193,6 +159,39 @@ const getAllChatflowsbyUserId = async (userid: string, type?: ChatflowType): Pro
193159 }
194160}
195161
162+ const importSampleChatflowsbyUserId = async ( userid : string , type ?: ChatflowType ) : Promise < ChatFlow [ ] > => {
163+ try {
164+ const response = await axios . get ( 'https://api.github.com/repos/opea-project/GenAIStudio/contents/sample-workflows' ) ;
165+ const files = response . data . filter ( ( item : any ) => item . type === 'file' ) ;
166+ console . log ( `Number of files: ${ files . length } ` ) ;
167+
168+ const chatflows : Partial < ChatFlow > [ ] = [ ] ;
169+
170+ for ( const file of files ) {
171+ console . log ( `Download URL: ${ file . download_url } ` ) ;
172+ const fileResponse = await axios . get ( file . download_url ) ;
173+ const parsedFlowData = fileResponse . data ;
174+
175+ const newChatflow : Partial < ChatFlow > = {
176+ userid : userid ,
177+ name : file . name . replace ( '.json' , '' ) ,
178+ flowData : JSON . stringify ( parsedFlowData ) ,
179+ type : 'OPEA' ,
180+ deployed : false ,
181+ isPublic : false
182+ } ;
183+
184+ chatflows . push ( newChatflow ) ;
185+ }
186+ const insertResponse = await importChatflows ( chatflows ) ;
187+ return insertResponse ;
188+ } catch ( error ) {
189+ throw new InternalFlowiseError (
190+ StatusCodes . INTERNAL_SERVER_ERROR ,
191+ `Error: chatflowsService.importSampleChatflowsbyUserId - ${ getErrorMessage ( error ) } `
192+ ) ;
193+ }
194+ }
196195
197196const getChatflowByApiKey = async ( apiKeyId : string , keyonly ?: unknown ) : Promise < any > => {
198197 try {
@@ -516,6 +515,7 @@ export default {
516515 deleteChatflow,
517516 getAllChatflows,
518517 getAllChatflowsbyUserId,
518+ importSampleChatflowsbyUserId,
519519 getChatflowByApiKey,
520520 getChatflowById,
521521 saveChatflow,
0 commit comments