-
-
Notifications
You must be signed in to change notification settings - Fork 15.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/export import stage 2 #3063
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5585f9e
add export all function
chungyau97 5c42780
modify exportAll to reuse existing code from other services
chungyau97 4503ae8
modify routes of export-import
chungyau97 62d11cd
add exportAll function into UI
chungyau97 c0a50e5
add errorhandler
chungyau97 d9d5c1a
add importAll Function into UI for ChatFlow
chungyau97 53bbf81
modify importAll Function to import tools
chungyau97 0909f0c
remove appServer variable
chungyau97 83ad90b
modify exportAll to exportData for new requirement in backend
chungyau97 5459546
chore modify type camelCase to PascalCase in exportImportService
chungyau97 3aba6ab
add import export for variables, assistants, and checkboxes for UI
HenryHengZJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@ | ||
import { NextFunction, Request, Response } from 'express' | ||
import { ChatFlow } from '../../database/entities/ChatFlow' | ||
import { Tool } from '../../database/entities/Tool' | ||
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 importAll = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const importData: { Tool: Partial<Tool>[]; ChatFlow: Partial<ChatFlow>[] } = req.body | ||
await exportImportService.importAll(importData) | ||
return res.json({ message: 'success' }) | ||
} catch (error) { | ||
next(error) | ||
} | ||
} | ||
|
||
export default { | ||
exportData, | ||
importAll | ||
} |
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/all', exportImportController.importAll) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { StatusCodes } from 'http-status-codes' | ||
import { ChatFlow } from '../../database/entities/ChatFlow' | ||
import { Tool } from '../../database/entities/Tool' | ||
import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
import { getErrorMessage } from '../../errors/utils' | ||
import { getRunningExpressApp } from '../../utils/getRunningExpressApp' | ||
import chatflowService from '../chatflows' | ||
import toolsService from '../tools' | ||
|
||
type ExportInput = { tool: boolean; chatflow: boolean; multiagent: boolean } | ||
|
||
const convertExportInput = (body: any): ExportInput => { | ||
try { | ||
if (typeof body.tool !== 'boolean' || typeof body.chatflow !== 'boolean' || typeof body.multiagent !== 'boolean') | ||
throw new Error('Invalid ExportInput object in request body') | ||
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; Tool: Tool[]; ChatFlow: ChatFlow[] }> => { | ||
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.multiagent === true) allMultiAgent = await chatflowService.getAllChatflows('MULTIAGENT') | ||
|
||
return { FileDefaultName, Tool: allTool, ChatFlow: [...allChatflow, ...allMultiAgent] } | ||
} catch (error) { | ||
throw new InternalFlowiseError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`Error: exportImportService.exportData - ${getErrorMessage(error)}` | ||
) | ||
} | ||
} | ||
|
||
const importAll = async (importData: { Tool: Partial<Tool>[]; ChatFlow: Partial<ChatFlow>[] }) => { | ||
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) | ||
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, | ||
importAll | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { StatusCodes } from 'http-status-codes' | ||
import { getRunningExpressApp } from '../../utils/getRunningExpressApp' | ||
import { Tool } from '../../database/entities/Tool' | ||
import { getAppVersion } from '../../utils' | ||
import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
import { getErrorMessage } from '../../errors/utils' | ||
import { getAppVersion } from '../../utils' | ||
import { getRunningExpressApp } from '../../utils/getRunningExpressApp' | ||
|
||
const createTool = async (requestBody: any): Promise<any> => { | ||
try { | ||
|
@@ -35,7 +35,7 @@ const deleteTool = async (toolId: string): Promise<any> => { | |
} | ||
} | ||
|
||
const getAllTools = async (): Promise<any> => { | ||
const getAllTools = async (): Promise<Tool[]> => { | ||
try { | ||
const appServer = getRunningExpressApp() | ||
const dbResponse = await appServer.AppDataSource.getRepository(Tool).find() | ||
|
@@ -79,10 +79,58 @@ const updateTool = async (toolId: string, toolBody: any): Promise<any> => { | |
} | ||
} | ||
|
||
const importTools = async (newTools: Partial<Tool>[]) => { | ||
try { | ||
const appServer = getRunningExpressApp() | ||
|
||
// step 1 - check whether file tools array is zero | ||
if (newTools.length == 0) throw new Error('No tools in this file.') | ||
|
||
// step 2 - check whether ids are duplicate in database | ||
let ids = '(' | ||
let count: number = 0 | ||
const lastCount = newTools.length - 1 | ||
newTools.forEach((newTools) => { | ||
ids += `'${newTools.id}'` | ||
if (lastCount != count) ids += ',' | ||
if (lastCount == count) ids += ')' | ||
count += 1 | ||
}) | ||
|
||
const selectResponse = await appServer.AppDataSource.getRepository(Tool) | ||
.createQueryBuilder('t') | ||
.select('t.id') | ||
.where(`t.id IN ${ids}`) | ||
.getMany() | ||
const foundIds = selectResponse.map((response) => { | ||
return response.id | ||
}) | ||
|
||
// step 3 - remove ids that are only duplicate | ||
const prepTools: Partial<Tool>[] = newTools.map((newTool) => { | ||
let id: string = '' | ||
if (newTool.id) id = newTool.id | ||
if (foundIds.includes(id)) { | ||
newTool.id = undefined | ||
newTool.name += ' with new id' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
return newTool | ||
}) | ||
|
||
// step 4 - transactional insert array of entities | ||
const insertResponse = await appServer.AppDataSource.getRepository(Tool).insert(prepTools) | ||
|
||
return insertResponse | ||
} catch (error) { | ||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.importTools - ${getErrorMessage(error)}`) | ||
} | ||
} | ||
|
||
export default { | ||
createTool, | ||
deleteTool, | ||
getAllTools, | ||
getToolById, | ||
updateTool | ||
updateTool, | ||
importTools | ||
} |
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,10 @@ | ||
import client from './client' | ||
|
||
const exportAll = () => client.get('/export-import/export/all') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to modify this part to const exportData = (body) => client.post('/export-import/export', body) Body example: {
"tool": true,
"chatflow": true,
"multiagent": true
} |
||
|
||
const importAll = (body) => client.post('/export-import/import/all', body) | ||
|
||
export default { | ||
exportAll, | ||
importAll | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with new id