-
-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/export import stage 2 (#3063)
* add export all function * modify exportAll to reuse existing code from other services * modify routes of export-import * add exportAll function into UI * add errorhandler * add importAll Function into UI for ChatFlow * modify importAll Function to import tools * remove appServer variable * modify exportAll to exportData for new requirement in backend * chore modify type camelCase to PascalCase in exportImportService * add import export for variables, assistants, and checkboxes for UI --------- Co-authored-by: Henry <[email protected]>
- Loading branch information
1 parent
40a1064
commit 56f9208
Showing
18 changed files
with
644 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NextFunction, Request, Response } from 'express' | ||
import exportImportService from '../../services/export-import' | ||
|
||
const exportData = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const apiResponse = await exportImportService.exportData(exportImportService.convertExportInput(req.body)) | ||
return res.json(apiResponse) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
const importData = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const importData = req.body | ||
await exportImportService.importData(importData) | ||
return res.json({ message: 'success' }) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
export default { | ||
exportData, | ||
importData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import express from 'express' | ||
import exportImportController from '../../controllers/export-import' | ||
const router = express.Router() | ||
|
||
router.post('/export', exportImportController.exportData) | ||
|
||
router.post('/import', exportImportController.importData) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { StatusCodes } from 'http-status-codes' | ||
import { ChatFlow } from '../../database/entities/ChatFlow' | ||
import { Tool } from '../../database/entities/Tool' | ||
import { Variable } from '../../database/entities/Variable' | ||
import { Assistant } from '../../database/entities/Assistant' | ||
import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
import { getErrorMessage } from '../../errors/utils' | ||
import { getRunningExpressApp } from '../../utils/getRunningExpressApp' | ||
import chatflowService from '../chatflows' | ||
import toolsService from '../tools' | ||
import variableService from '../variables' | ||
import assistantService from '../assistants' | ||
|
||
type ExportInput = { | ||
tool: boolean | ||
chatflow: boolean | ||
agentflow: boolean | ||
variable: boolean | ||
assistant: boolean | ||
} | ||
|
||
type ExportData = { | ||
Tool: Tool[] | ||
ChatFlow: ChatFlow[] | ||
AgentFlow: ChatFlow[] | ||
Variable: Variable[] | ||
Assistant: Assistant[] | ||
} | ||
|
||
const convertExportInput = (body: any): ExportInput => { | ||
try { | ||
if (!body || typeof body !== 'object') throw new Error('Invalid ExportInput object in request body') | ||
if (body.tool && typeof body.tool !== 'boolean') throw new Error('Invalid tool property in ExportInput object') | ||
if (body.chatflow && typeof body.chatflow !== 'boolean') throw new Error('Invalid chatflow property in ExportInput object') | ||
if (body.agentflow && typeof body.agentflow !== 'boolean') throw new Error('Invalid agentflow property in ExportInput object') | ||
if (body.variable && typeof body.variable !== 'boolean') throw new Error('Invalid variable property in ExportInput object') | ||
if (body.assistant && typeof body.assistant !== 'boolean') throw new Error('Invalid assistant property in ExportInput object') | ||
return body as ExportInput | ||
} catch (error) { | ||
throw new InternalFlowiseError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`Error: exportImportService.convertExportInput - ${getErrorMessage(error)}` | ||
) | ||
} | ||
} | ||
|
||
const FileDefaultName = 'ExportData.json' | ||
const exportData = async (exportInput: ExportInput): Promise<{ FileDefaultName: string } & ExportData> => { | ||
try { | ||
// step 1 - get all Tool | ||
let allTool: Tool[] = [] | ||
if (exportInput.tool === true) allTool = await toolsService.getAllTools() | ||
|
||
// step 2 - get all ChatFlow | ||
let allChatflow: ChatFlow[] = [] | ||
if (exportInput.chatflow === true) allChatflow = await chatflowService.getAllChatflows('CHATFLOW') | ||
|
||
// step 3 - get all MultiAgent | ||
let allMultiAgent: ChatFlow[] = [] | ||
if (exportInput.agentflow === true) allMultiAgent = await chatflowService.getAllChatflows('MULTIAGENT') | ||
|
||
let allVars: Variable[] = [] | ||
if (exportInput.variable === true) allVars = await variableService.getAllVariables() | ||
|
||
let allAssistants: Assistant[] = [] | ||
if (exportInput.assistant === true) allAssistants = await assistantService.getAllAssistants() | ||
|
||
return { | ||
FileDefaultName, | ||
Tool: allTool, | ||
ChatFlow: allChatflow, | ||
AgentFlow: allMultiAgent, | ||
Variable: allVars, | ||
Assistant: allAssistants | ||
} | ||
} catch (error) { | ||
throw new InternalFlowiseError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`Error: exportImportService.exportData - ${getErrorMessage(error)}` | ||
) | ||
} | ||
} | ||
|
||
const importData = async (importData: ExportData) => { | ||
try { | ||
const appServer = getRunningExpressApp() | ||
const queryRunner = appServer.AppDataSource.createQueryRunner() | ||
|
||
try { | ||
queryRunner.startTransaction() | ||
|
||
// step 1 - importTools | ||
if (importData.Tool.length > 0) await toolsService.importTools(importData.Tool) | ||
// step 2 - importChatflows | ||
if (importData.ChatFlow.length > 0) await chatflowService.importChatflows(importData.ChatFlow) | ||
// step 3 - importAgentlows | ||
if (importData.AgentFlow.length > 0) await chatflowService.importChatflows(importData.AgentFlow) | ||
if (importData.Variable.length > 0) await variableService.importVariables(importData.Variable) | ||
if (importData.Assistant.length > 0) await assistantService.importAssistants(importData.Assistant) | ||
queryRunner.commitTransaction() | ||
} catch (error) { | ||
queryRunner.rollbackTransaction() | ||
throw error | ||
} finally { | ||
queryRunner.release() | ||
} | ||
} catch (error) { | ||
throw new InternalFlowiseError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`Error: exportImportService.importAll - ${getErrorMessage(error)}` | ||
) | ||
} | ||
} | ||
|
||
export default { | ||
convertExportInput, | ||
exportData, | ||
importData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.