Flowise

Форк
0
64 строки · 2.7 Кб
1
import path from 'path'
2
import * as fs from 'fs'
3
import { StatusCodes } from 'http-status-codes'
4
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
5
import { getErrorMessage } from '../../errors/utils'
6

7
// Get all templates for marketplaces
8
const getAllTemplates = async () => {
9
    try {
10
        let marketplaceDir = path.join(__dirname, '..', '..', '..', 'marketplaces', 'chatflows')
11
        let jsonsInDir = fs.readdirSync(marketplaceDir).filter((file) => path.extname(file) === '.json')
12
        let templates: any[] = []
13
        jsonsInDir.forEach((file, index) => {
14
            const filePath = path.join(__dirname, '..', '..', '..', 'marketplaces', 'chatflows', file)
15
            const fileData = fs.readFileSync(filePath)
16
            const fileDataObj = JSON.parse(fileData.toString())
17
            const template = {
18
                id: index,
19
                templateName: file.split('.json')[0],
20
                flowData: fileData.toString(),
21
                badge: fileDataObj?.badge,
22
                framework: fileDataObj?.framework,
23
                categories: fileDataObj?.categories,
24
                type: 'Chatflow',
25
                description: fileDataObj?.description || ''
26
            }
27
            templates.push(template)
28
        })
29

30
        marketplaceDir = path.join(__dirname, '..', '..', '..', 'marketplaces', 'tools')
31
        jsonsInDir = fs.readdirSync(marketplaceDir).filter((file) => path.extname(file) === '.json')
32
        jsonsInDir.forEach((file, index) => {
33
            const filePath = path.join(__dirname, '..', '..', '..', 'marketplaces', 'tools', file)
34
            const fileData = fs.readFileSync(filePath)
35
            const fileDataObj = JSON.parse(fileData.toString())
36
            const template = {
37
                ...fileDataObj,
38
                id: index,
39
                type: 'Tool',
40
                framework: fileDataObj?.framework,
41
                badge: fileDataObj?.badge,
42
                categories: '',
43
                templateName: file.split('.json')[0]
44
            }
45
            templates.push(template)
46
        })
47
        const sortedTemplates = templates.sort((a, b) => a.templateName.localeCompare(b.templateName))
48
        const FlowiseDocsQnAIndex = sortedTemplates.findIndex((tmp) => tmp.templateName === 'Flowise Docs QnA')
49
        if (FlowiseDocsQnAIndex > 0) {
50
            sortedTemplates.unshift(sortedTemplates.splice(FlowiseDocsQnAIndex, 1)[0])
51
        }
52
        const dbResponse = sortedTemplates
53
        return dbResponse
54
    } catch (error) {
55
        throw new InternalFlowiseError(
56
            StatusCodes.INTERNAL_SERVER_ERROR,
57
            `Error: marketplacesService.getAllTemplates - ${getErrorMessage(error)}`
58
        )
59
    }
60
}
61

62
export default {
63
    getAllTemplates
64
}
65

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

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

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

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