Flowise

Форк
0
88 строк · 3.3 Кб
1
import { StatusCodes } from 'http-status-codes'
2
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
3
import { Tool } from '../../database/entities/Tool'
4
import { getAppVersion } from '../../utils'
5
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
6
import { getErrorMessage } from '../../errors/utils'
7

8
const createTool = async (requestBody: any): Promise<any> => {
9
    try {
10
        const appServer = getRunningExpressApp()
11
        const newTool = new Tool()
12
        Object.assign(newTool, requestBody)
13
        const tool = await appServer.AppDataSource.getRepository(Tool).create(newTool)
14
        const dbResponse = await appServer.AppDataSource.getRepository(Tool).save(tool)
15
        await appServer.telemetry.sendTelemetry('tool_created', {
16
            version: await getAppVersion(),
17
            toolId: dbResponse.id,
18
            toolName: dbResponse.name
19
        })
20
        return dbResponse
21
    } catch (error) {
22
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.createTool - ${getErrorMessage(error)}`)
23
    }
24
}
25

26
const deleteTool = async (toolId: string): Promise<any> => {
27
    try {
28
        const appServer = getRunningExpressApp()
29
        const dbResponse = await appServer.AppDataSource.getRepository(Tool).delete({
30
            id: toolId
31
        })
32
        return dbResponse
33
    } catch (error) {
34
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.deleteTool - ${getErrorMessage(error)}`)
35
    }
36
}
37

38
const getAllTools = async (): Promise<any> => {
39
    try {
40
        const appServer = getRunningExpressApp()
41
        const dbResponse = await appServer.AppDataSource.getRepository(Tool).find()
42
        return dbResponse
43
    } catch (error) {
44
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.getAllTools - ${getErrorMessage(error)}`)
45
    }
46
}
47

48
const getToolById = async (toolId: string): Promise<any> => {
49
    try {
50
        const appServer = getRunningExpressApp()
51
        const dbResponse = await appServer.AppDataSource.getRepository(Tool).findOneBy({
52
            id: toolId
53
        })
54
        if (!dbResponse) {
55
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Tool ${toolId} not found`)
56
        }
57
        return dbResponse
58
    } catch (error) {
59
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.getToolById - ${getErrorMessage(error)}`)
60
    }
61
}
62

63
const updateTool = async (toolId: string, toolBody: any): Promise<any> => {
64
    try {
65
        const appServer = getRunningExpressApp()
66
        const tool = await appServer.AppDataSource.getRepository(Tool).findOneBy({
67
            id: toolId
68
        })
69
        if (!tool) {
70
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Tool ${toolId} not found`)
71
        }
72
        const updateTool = new Tool()
73
        Object.assign(updateTool, toolBody)
74
        await appServer.AppDataSource.getRepository(Tool).merge(tool, updateTool)
75
        const dbResponse = await appServer.AppDataSource.getRepository(Tool).save(tool)
76
        return dbResponse
77
    } catch (error) {
78
        throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: toolsService.updateTool - ${getErrorMessage(error)}`)
79
    }
80
}
81

82
export default {
83
    createTool,
84
    deleteTool,
85
    getAllTools,
86
    getToolById,
87
    updateTool
88
}
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.