Flowise

Форк
0
264 строки · 10.0 Кб
1
import { NextFunction, Request, Response } from 'express'
2
import { StatusCodes } from 'http-status-codes'
3
import documentStoreService from '../../services/documentstore'
4
import { DocumentStore } from '../../database/entities/DocumentStore'
5
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
6
import { DocumentStoreDTO } from '../../Interface'
7

8
const createDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
9
    try {
10
        if (typeof req.body === 'undefined') {
11
            throw new InternalFlowiseError(
12
                StatusCodes.PRECONDITION_FAILED,
13
                `Error: documentStoreController.createDocumentStore - body not provided!`
14
            )
15
        }
16
        const body = req.body
17
        const docStore = DocumentStoreDTO.toEntity(body)
18
        const apiResponse = await documentStoreService.createDocumentStore(docStore)
19
        return res.json(apiResponse)
20
    } catch (error) {
21
        next(error)
22
    }
23
}
24

25
const getAllDocumentStores = async (req: Request, res: Response, next: NextFunction) => {
26
    try {
27
        const apiResponse = await documentStoreService.getAllDocumentStores()
28
        return res.json(DocumentStoreDTO.fromEntities(apiResponse))
29
    } catch (error) {
30
        next(error)
31
    }
32
}
33

34
const deleteLoaderFromDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
35
    try {
36
        const storeId = req.params.id
37
        const loaderId = req.params.loaderId
38

39
        if (!storeId || !loaderId) {
40
            throw new InternalFlowiseError(
41
                StatusCodes.PRECONDITION_FAILED,
42
                `Error: documentStoreController.deleteLoaderFromDocumentStore - missing storeId or loaderId.`
43
            )
44
        }
45
        const apiResponse = await documentStoreService.deleteLoaderFromDocumentStore(storeId, loaderId)
46
        return res.json(DocumentStoreDTO.fromEntity(apiResponse))
47
    } catch (error) {
48
        next(error)
49
    }
50
}
51

52
const getDocumentStoreById = async (req: Request, res: Response, next: NextFunction) => {
53
    try {
54
        if (typeof req.params.id === 'undefined' || req.params.id === '') {
55
            throw new InternalFlowiseError(
56
                StatusCodes.PRECONDITION_FAILED,
57
                `Error: documentStoreController.getDocumentStoreById - id not provided!`
58
            )
59
        }
60
        const apiResponse = await documentStoreService.getDocumentStoreById(req.params.id)
61
        if (apiResponse && apiResponse.whereUsed) {
62
            apiResponse.whereUsed = JSON.stringify(await documentStoreService.getUsedChatflowNames(apiResponse))
63
        }
64
        return res.json(DocumentStoreDTO.fromEntity(apiResponse))
65
    } catch (error) {
66
        next(error)
67
    }
68
}
69

70
const getDocumentStoreFileChunks = async (req: Request, res: Response, next: NextFunction) => {
71
    try {
72
        if (typeof req.params.storeId === 'undefined' || req.params.storeId === '') {
73
            throw new InternalFlowiseError(
74
                StatusCodes.PRECONDITION_FAILED,
75
                `Error: documentStoreController.getDocumentStoreFileChunks - storeId not provided!`
76
            )
77
        }
78
        if (typeof req.params.fileId === 'undefined' || req.params.fileId === '') {
79
            throw new InternalFlowiseError(
80
                StatusCodes.PRECONDITION_FAILED,
81
                `Error: documentStoreController.getDocumentStoreFileChunks - fileId not provided!`
82
            )
83
        }
84
        const page = req.params.pageNo ? parseInt(req.params.pageNo) : 1
85
        const apiResponse = await documentStoreService.getDocumentStoreFileChunks(req.params.storeId, req.params.fileId, page)
86
        return res.json(apiResponse)
87
    } catch (error) {
88
        next(error)
89
    }
90
}
91

92
const deleteDocumentStoreFileChunk = async (req: Request, res: Response, next: NextFunction) => {
93
    try {
94
        if (typeof req.params.storeId === 'undefined' || req.params.storeId === '') {
95
            throw new InternalFlowiseError(
96
                StatusCodes.PRECONDITION_FAILED,
97
                `Error: documentStoreController.deleteDocumentStoreFileChunk - storeId not provided!`
98
            )
99
        }
100
        if (typeof req.params.loaderId === 'undefined' || req.params.loaderId === '') {
101
            throw new InternalFlowiseError(
102
                StatusCodes.PRECONDITION_FAILED,
103
                `Error: documentStoreController.deleteDocumentStoreFileChunk - loaderId not provided!`
104
            )
105
        }
106
        if (typeof req.params.chunkId === 'undefined' || req.params.chunkId === '') {
107
            throw new InternalFlowiseError(
108
                StatusCodes.PRECONDITION_FAILED,
109
                `Error: documentStoreController.deleteDocumentStoreFileChunk - chunkId not provided!`
110
            )
111
        }
112
        const apiResponse = await documentStoreService.deleteDocumentStoreFileChunk(
113
            req.params.storeId,
114
            req.params.loaderId,
115
            req.params.chunkId
116
        )
117
        return res.json(apiResponse)
118
    } catch (error) {
119
        next(error)
120
    }
121
}
122

123
const editDocumentStoreFileChunk = async (req: Request, res: Response, next: NextFunction) => {
124
    try {
125
        if (typeof req.params.storeId === 'undefined' || req.params.storeId === '') {
126
            throw new InternalFlowiseError(
127
                StatusCodes.PRECONDITION_FAILED,
128
                `Error: documentStoreController.editDocumentStoreFileChunk - storeId not provided!`
129
            )
130
        }
131
        if (typeof req.params.loaderId === 'undefined' || req.params.loaderId === '') {
132
            throw new InternalFlowiseError(
133
                StatusCodes.PRECONDITION_FAILED,
134
                `Error: documentStoreController.editDocumentStoreFileChunk - loaderId not provided!`
135
            )
136
        }
137
        if (typeof req.params.chunkId === 'undefined' || req.params.chunkId === '') {
138
            throw new InternalFlowiseError(
139
                StatusCodes.PRECONDITION_FAILED,
140
                `Error: documentStoreController.editDocumentStoreFileChunk - chunkId not provided!`
141
            )
142
        }
143
        const body = req.body
144
        if (typeof body === 'undefined') {
145
            throw new InternalFlowiseError(
146
                StatusCodes.PRECONDITION_FAILED,
147
                `Error: documentStoreController.editDocumentStoreFileChunk - body not provided!`
148
            )
149
        }
150
        const apiResponse = await documentStoreService.editDocumentStoreFileChunk(
151
            req.params.storeId,
152
            req.params.loaderId,
153
            req.params.chunkId,
154
            body.pageContent,
155
            body.metadata
156
        )
157
        return res.json(apiResponse)
158
    } catch (error) {
159
        next(error)
160
    }
161
}
162

163
const processFileChunks = async (req: Request, res: Response, next: NextFunction) => {
164
    try {
165
        if (typeof req.body === 'undefined') {
166
            throw new InternalFlowiseError(
167
                StatusCodes.PRECONDITION_FAILED,
168
                `Error: documentStoreController.processFileChunks - body not provided!`
169
            )
170
        }
171
        const body = req.body
172
        const apiResponse = await documentStoreService.processAndSaveChunks(body)
173
        return res.json(apiResponse)
174
    } catch (error) {
175
        next(error)
176
    }
177
}
178

179
const updateDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
180
    try {
181
        if (typeof req.params.id === 'undefined' || req.params.id === '') {
182
            throw new InternalFlowiseError(
183
                StatusCodes.PRECONDITION_FAILED,
184
                `Error: documentStoreController.updateDocumentStore - storeId not provided!`
185
            )
186
        }
187
        if (typeof req.body === 'undefined') {
188
            throw new InternalFlowiseError(
189
                StatusCodes.PRECONDITION_FAILED,
190
                `Error: documentStoreController.updateDocumentStore - body not provided!`
191
            )
192
        }
193
        const store = await documentStoreService.getDocumentStoreById(req.params.id)
194
        if (!store) {
195
            throw new InternalFlowiseError(
196
                StatusCodes.NOT_FOUND,
197
                `Error: documentStoreController.updateDocumentStore - DocumentStore ${req.params.id} not found in the database`
198
            )
199
        }
200
        const body = req.body
201
        const updateDocStore = new DocumentStore()
202
        Object.assign(updateDocStore, body)
203
        const apiResponse = await documentStoreService.updateDocumentStore(store, updateDocStore)
204
        return res.json(DocumentStoreDTO.fromEntity(apiResponse))
205
    } catch (error) {
206
        next(error)
207
    }
208
}
209

210
const deleteDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
211
    try {
212
        if (typeof req.params.id === 'undefined' || req.params.id === '') {
213
            throw new InternalFlowiseError(
214
                StatusCodes.PRECONDITION_FAILED,
215
                `Error: documentStoreController.deleteDocumentStore - storeId not provided!`
216
            )
217
        }
218
        const apiResponse = await documentStoreService.deleteDocumentStore(req.params.id)
219
        return res.json(apiResponse)
220
    } catch (error) {
221
        next(error)
222
    }
223
}
224

225
const previewFileChunks = async (req: Request, res: Response, next: NextFunction) => {
226
    try {
227
        if (typeof req.body === 'undefined') {
228
            throw new InternalFlowiseError(
229
                StatusCodes.PRECONDITION_FAILED,
230
                `Error: documentStoreController.previewFileChunks - body not provided!`
231
            )
232
        }
233
        const body = req.body
234
        body.preview = true
235
        const apiResponse = await documentStoreService.previewChunks(body)
236
        return res.json(apiResponse)
237
    } catch (error) {
238
        next(error)
239
    }
240
}
241

242
const getDocumentLoaders = async (req: Request, res: Response, next: NextFunction) => {
243
    try {
244
        const apiResponse = await documentStoreService.getDocumentLoaders()
245
        return res.json(apiResponse)
246
    } catch (error) {
247
        next(error)
248
    }
249
}
250

251
export default {
252
    deleteDocumentStore,
253
    createDocumentStore,
254
    getAllDocumentStores,
255
    deleteLoaderFromDocumentStore,
256
    getDocumentStoreById,
257
    getDocumentStoreFileChunks,
258
    updateDocumentStore,
259
    processFileChunks,
260
    previewFileChunks,
261
    getDocumentLoaders,
262
    deleteDocumentStoreFileChunk,
263
    editDocumentStoreFileChunk
264
}
265

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

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

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

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