Flowise
264 строки · 10.0 Кб
1import { NextFunction, Request, Response } from 'express'
2import { StatusCodes } from 'http-status-codes'
3import documentStoreService from '../../services/documentstore'
4import { DocumentStore } from '../../database/entities/DocumentStore'
5import { InternalFlowiseError } from '../../errors/internalFlowiseError'
6import { DocumentStoreDTO } from '../../Interface'
7
8const createDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
9try {
10if (typeof req.body === 'undefined') {
11throw new InternalFlowiseError(
12StatusCodes.PRECONDITION_FAILED,
13`Error: documentStoreController.createDocumentStore - body not provided!`
14)
15}
16const body = req.body
17const docStore = DocumentStoreDTO.toEntity(body)
18const apiResponse = await documentStoreService.createDocumentStore(docStore)
19return res.json(apiResponse)
20} catch (error) {
21next(error)
22}
23}
24
25const getAllDocumentStores = async (req: Request, res: Response, next: NextFunction) => {
26try {
27const apiResponse = await documentStoreService.getAllDocumentStores()
28return res.json(DocumentStoreDTO.fromEntities(apiResponse))
29} catch (error) {
30next(error)
31}
32}
33
34const deleteLoaderFromDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
35try {
36const storeId = req.params.id
37const loaderId = req.params.loaderId
38
39if (!storeId || !loaderId) {
40throw new InternalFlowiseError(
41StatusCodes.PRECONDITION_FAILED,
42`Error: documentStoreController.deleteLoaderFromDocumentStore - missing storeId or loaderId.`
43)
44}
45const apiResponse = await documentStoreService.deleteLoaderFromDocumentStore(storeId, loaderId)
46return res.json(DocumentStoreDTO.fromEntity(apiResponse))
47} catch (error) {
48next(error)
49}
50}
51
52const getDocumentStoreById = async (req: Request, res: Response, next: NextFunction) => {
53try {
54if (typeof req.params.id === 'undefined' || req.params.id === '') {
55throw new InternalFlowiseError(
56StatusCodes.PRECONDITION_FAILED,
57`Error: documentStoreController.getDocumentStoreById - id not provided!`
58)
59}
60const apiResponse = await documentStoreService.getDocumentStoreById(req.params.id)
61if (apiResponse && apiResponse.whereUsed) {
62apiResponse.whereUsed = JSON.stringify(await documentStoreService.getUsedChatflowNames(apiResponse))
63}
64return res.json(DocumentStoreDTO.fromEntity(apiResponse))
65} catch (error) {
66next(error)
67}
68}
69
70const getDocumentStoreFileChunks = async (req: Request, res: Response, next: NextFunction) => {
71try {
72if (typeof req.params.storeId === 'undefined' || req.params.storeId === '') {
73throw new InternalFlowiseError(
74StatusCodes.PRECONDITION_FAILED,
75`Error: documentStoreController.getDocumentStoreFileChunks - storeId not provided!`
76)
77}
78if (typeof req.params.fileId === 'undefined' || req.params.fileId === '') {
79throw new InternalFlowiseError(
80StatusCodes.PRECONDITION_FAILED,
81`Error: documentStoreController.getDocumentStoreFileChunks - fileId not provided!`
82)
83}
84const page = req.params.pageNo ? parseInt(req.params.pageNo) : 1
85const apiResponse = await documentStoreService.getDocumentStoreFileChunks(req.params.storeId, req.params.fileId, page)
86return res.json(apiResponse)
87} catch (error) {
88next(error)
89}
90}
91
92const deleteDocumentStoreFileChunk = async (req: Request, res: Response, next: NextFunction) => {
93try {
94if (typeof req.params.storeId === 'undefined' || req.params.storeId === '') {
95throw new InternalFlowiseError(
96StatusCodes.PRECONDITION_FAILED,
97`Error: documentStoreController.deleteDocumentStoreFileChunk - storeId not provided!`
98)
99}
100if (typeof req.params.loaderId === 'undefined' || req.params.loaderId === '') {
101throw new InternalFlowiseError(
102StatusCodes.PRECONDITION_FAILED,
103`Error: documentStoreController.deleteDocumentStoreFileChunk - loaderId not provided!`
104)
105}
106if (typeof req.params.chunkId === 'undefined' || req.params.chunkId === '') {
107throw new InternalFlowiseError(
108StatusCodes.PRECONDITION_FAILED,
109`Error: documentStoreController.deleteDocumentStoreFileChunk - chunkId not provided!`
110)
111}
112const apiResponse = await documentStoreService.deleteDocumentStoreFileChunk(
113req.params.storeId,
114req.params.loaderId,
115req.params.chunkId
116)
117return res.json(apiResponse)
118} catch (error) {
119next(error)
120}
121}
122
123const editDocumentStoreFileChunk = async (req: Request, res: Response, next: NextFunction) => {
124try {
125if (typeof req.params.storeId === 'undefined' || req.params.storeId === '') {
126throw new InternalFlowiseError(
127StatusCodes.PRECONDITION_FAILED,
128`Error: documentStoreController.editDocumentStoreFileChunk - storeId not provided!`
129)
130}
131if (typeof req.params.loaderId === 'undefined' || req.params.loaderId === '') {
132throw new InternalFlowiseError(
133StatusCodes.PRECONDITION_FAILED,
134`Error: documentStoreController.editDocumentStoreFileChunk - loaderId not provided!`
135)
136}
137if (typeof req.params.chunkId === 'undefined' || req.params.chunkId === '') {
138throw new InternalFlowiseError(
139StatusCodes.PRECONDITION_FAILED,
140`Error: documentStoreController.editDocumentStoreFileChunk - chunkId not provided!`
141)
142}
143const body = req.body
144if (typeof body === 'undefined') {
145throw new InternalFlowiseError(
146StatusCodes.PRECONDITION_FAILED,
147`Error: documentStoreController.editDocumentStoreFileChunk - body not provided!`
148)
149}
150const apiResponse = await documentStoreService.editDocumentStoreFileChunk(
151req.params.storeId,
152req.params.loaderId,
153req.params.chunkId,
154body.pageContent,
155body.metadata
156)
157return res.json(apiResponse)
158} catch (error) {
159next(error)
160}
161}
162
163const processFileChunks = async (req: Request, res: Response, next: NextFunction) => {
164try {
165if (typeof req.body === 'undefined') {
166throw new InternalFlowiseError(
167StatusCodes.PRECONDITION_FAILED,
168`Error: documentStoreController.processFileChunks - body not provided!`
169)
170}
171const body = req.body
172const apiResponse = await documentStoreService.processAndSaveChunks(body)
173return res.json(apiResponse)
174} catch (error) {
175next(error)
176}
177}
178
179const updateDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
180try {
181if (typeof req.params.id === 'undefined' || req.params.id === '') {
182throw new InternalFlowiseError(
183StatusCodes.PRECONDITION_FAILED,
184`Error: documentStoreController.updateDocumentStore - storeId not provided!`
185)
186}
187if (typeof req.body === 'undefined') {
188throw new InternalFlowiseError(
189StatusCodes.PRECONDITION_FAILED,
190`Error: documentStoreController.updateDocumentStore - body not provided!`
191)
192}
193const store = await documentStoreService.getDocumentStoreById(req.params.id)
194if (!store) {
195throw new InternalFlowiseError(
196StatusCodes.NOT_FOUND,
197`Error: documentStoreController.updateDocumentStore - DocumentStore ${req.params.id} not found in the database`
198)
199}
200const body = req.body
201const updateDocStore = new DocumentStore()
202Object.assign(updateDocStore, body)
203const apiResponse = await documentStoreService.updateDocumentStore(store, updateDocStore)
204return res.json(DocumentStoreDTO.fromEntity(apiResponse))
205} catch (error) {
206next(error)
207}
208}
209
210const deleteDocumentStore = async (req: Request, res: Response, next: NextFunction) => {
211try {
212if (typeof req.params.id === 'undefined' || req.params.id === '') {
213throw new InternalFlowiseError(
214StatusCodes.PRECONDITION_FAILED,
215`Error: documentStoreController.deleteDocumentStore - storeId not provided!`
216)
217}
218const apiResponse = await documentStoreService.deleteDocumentStore(req.params.id)
219return res.json(apiResponse)
220} catch (error) {
221next(error)
222}
223}
224
225const previewFileChunks = async (req: Request, res: Response, next: NextFunction) => {
226try {
227if (typeof req.body === 'undefined') {
228throw new InternalFlowiseError(
229StatusCodes.PRECONDITION_FAILED,
230`Error: documentStoreController.previewFileChunks - body not provided!`
231)
232}
233const body = req.body
234body.preview = true
235const apiResponse = await documentStoreService.previewChunks(body)
236return res.json(apiResponse)
237} catch (error) {
238next(error)
239}
240}
241
242const getDocumentLoaders = async (req: Request, res: Response, next: NextFunction) => {
243try {
244const apiResponse = await documentStoreService.getDocumentLoaders()
245return res.json(apiResponse)
246} catch (error) {
247next(error)
248}
249}
250
251export default {
252deleteDocumentStore,
253createDocumentStore,
254getAllDocumentStores,
255deleteLoaderFromDocumentStore,
256getDocumentStoreById,
257getDocumentStoreFileChunks,
258updateDocumentStore,
259processFileChunks,
260previewFileChunks,
261getDocumentLoaders,
262deleteDocumentStoreFileChunk,
263editDocumentStoreFileChunk
264}
265