|
| 1 | +import { StatusCodes } from 'http-status-codes' |
| 2 | +import { ChatFlow } from '../../database/entities/ChatFlow' |
| 3 | +import { Tool } from '../../database/entities/Tool' |
| 4 | +import { Variable } from '../../database/entities/Variable' |
| 5 | +import { Assistant } from '../../database/entities/Assistant' |
| 6 | +import { InternalFlowiseError } from '../../errors/internalFlowiseError' |
| 7 | +import { getErrorMessage } from '../../errors/utils' |
| 8 | +import { getRunningExpressApp } from '../../utils/getRunningExpressApp' |
| 9 | +import chatflowService from '../chatflows' |
| 10 | +import toolsService from '../tools' |
| 11 | +import variableService from '../variables' |
| 12 | +import assistantService from '../assistants' |
| 13 | + |
| 14 | +type ExportInput = { |
| 15 | + tool: boolean |
| 16 | + chatflow: boolean |
| 17 | + agentflow: boolean |
| 18 | + variable: boolean |
| 19 | + assistant: boolean |
| 20 | +} |
| 21 | + |
| 22 | +type ExportData = { |
| 23 | + Tool: Tool[] |
| 24 | + ChatFlow: ChatFlow[] |
| 25 | + AgentFlow: ChatFlow[] |
| 26 | + Variable: Variable[] |
| 27 | + Assistant: Assistant[] |
| 28 | +} |
| 29 | + |
| 30 | +const convertExportInput = (body: any): ExportInput => { |
| 31 | + try { |
| 32 | + if (!body || typeof body !== 'object') throw new Error('Invalid ExportInput object in request body') |
| 33 | + if (body.tool && typeof body.tool !== 'boolean') throw new Error('Invalid tool property in ExportInput object') |
| 34 | + if (body.chatflow && typeof body.chatflow !== 'boolean') throw new Error('Invalid chatflow property in ExportInput object') |
| 35 | + if (body.agentflow && typeof body.agentflow !== 'boolean') throw new Error('Invalid agentflow property in ExportInput object') |
| 36 | + if (body.variable && typeof body.variable !== 'boolean') throw new Error('Invalid variable property in ExportInput object') |
| 37 | + if (body.assistant && typeof body.assistant !== 'boolean') throw new Error('Invalid assistant property in ExportInput object') |
| 38 | + return body as ExportInput |
| 39 | + } catch (error) { |
| 40 | + throw new InternalFlowiseError( |
| 41 | + StatusCodes.INTERNAL_SERVER_ERROR, |
| 42 | + `Error: exportImportService.convertExportInput - ${getErrorMessage(error)}` |
| 43 | + ) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +const FileDefaultName = 'ExportData.json' |
| 48 | +const exportData = async (exportInput: ExportInput): Promise<{ FileDefaultName: string } & ExportData> => { |
| 49 | + try { |
| 50 | + // step 1 - get all Tool |
| 51 | + let allTool: Tool[] = [] |
| 52 | + if (exportInput.tool === true) allTool = await toolsService.getAllTools() |
| 53 | + |
| 54 | + // step 2 - get all ChatFlow |
| 55 | + let allChatflow: ChatFlow[] = [] |
| 56 | + if (exportInput.chatflow === true) allChatflow = await chatflowService.getAllChatflows('CHATFLOW') |
| 57 | + |
| 58 | + // step 3 - get all MultiAgent |
| 59 | + let allMultiAgent: ChatFlow[] = [] |
| 60 | + if (exportInput.agentflow === true) allMultiAgent = await chatflowService.getAllChatflows('MULTIAGENT') |
| 61 | + |
| 62 | + let allVars: Variable[] = [] |
| 63 | + if (exportInput.variable === true) allVars = await variableService.getAllVariables() |
| 64 | + |
| 65 | + let allAssistants: Assistant[] = [] |
| 66 | + if (exportInput.assistant === true) allAssistants = await assistantService.getAllAssistants() |
| 67 | + |
| 68 | + return { |
| 69 | + FileDefaultName, |
| 70 | + Tool: allTool, |
| 71 | + ChatFlow: allChatflow, |
| 72 | + AgentFlow: allMultiAgent, |
| 73 | + Variable: allVars, |
| 74 | + Assistant: allAssistants |
| 75 | + } |
| 76 | + } catch (error) { |
| 77 | + throw new InternalFlowiseError( |
| 78 | + StatusCodes.INTERNAL_SERVER_ERROR, |
| 79 | + `Error: exportImportService.exportData - ${getErrorMessage(error)}` |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +const importData = async (importData: ExportData) => { |
| 85 | + try { |
| 86 | + const appServer = getRunningExpressApp() |
| 87 | + const queryRunner = appServer.AppDataSource.createQueryRunner() |
| 88 | + |
| 89 | + try { |
| 90 | + queryRunner.startTransaction() |
| 91 | + |
| 92 | + // step 1 - importTools |
| 93 | + if (importData.Tool.length > 0) await toolsService.importTools(importData.Tool) |
| 94 | + // step 2 - importChatflows |
| 95 | + if (importData.ChatFlow.length > 0) await chatflowService.importChatflows(importData.ChatFlow) |
| 96 | + // step 3 - importAgentlows |
| 97 | + if (importData.AgentFlow.length > 0) await chatflowService.importChatflows(importData.AgentFlow) |
| 98 | + if (importData.Variable.length > 0) await variableService.importVariables(importData.Variable) |
| 99 | + if (importData.Assistant.length > 0) await assistantService.importAssistants(importData.Assistant) |
| 100 | + queryRunner.commitTransaction() |
| 101 | + } catch (error) { |
| 102 | + queryRunner.rollbackTransaction() |
| 103 | + throw error |
| 104 | + } finally { |
| 105 | + queryRunner.release() |
| 106 | + } |
| 107 | + } catch (error) { |
| 108 | + throw new InternalFlowiseError( |
| 109 | + StatusCodes.INTERNAL_SERVER_ERROR, |
| 110 | + `Error: exportImportService.importAll - ${getErrorMessage(error)}` |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export default { |
| 116 | + convertExportInput, |
| 117 | + exportData, |
| 118 | + importData |
| 119 | +} |
0 commit comments