(req: Request, isInternal: boolean = false)
| 236 | * @param {boolean} isInternal |
| 237 | */ |
| 238 | export const upsertVector = async (req: Request, isInternal: boolean = false) => { |
| 239 | const appServer = getRunningExpressApp() |
| 240 | |
| 241 | try { |
| 242 | const chatflowid = req.params.id |
| 243 | |
| 244 | // Check if chatflow exists |
| 245 | const chatflow = await appServer.AppDataSource.getRepository(ChatFlow).findOneBy({ |
| 246 | id: chatflowid |
| 247 | }) |
| 248 | if (!chatflow) { |
| 249 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`) |
| 250 | } |
| 251 | |
| 252 | const httpProtocol = req.get('x-forwarded-proto') || req.protocol |
| 253 | const baseURL = `${httpProtocol}://${req.get('host')}` |
| 254 | const incomingInput: IncomingInput = req.body |
| 255 | const chatId = incomingInput.chatId ?? incomingInput.overrideConfig?.sessionId ?? uuidv4() |
| 256 | const files = (req.files as Express.Multer.File[]) || [] |
| 257 | |
| 258 | if (!isInternal) { |
| 259 | const isKeyValidated = await validateFlowAPIKey(req, chatflow) |
| 260 | if (!isKeyValidated) { |
| 261 | throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | const chatflowWorkspaceId = chatflow.workspaceId |
| 266 | const workspace = await appServer.AppDataSource.getRepository(Workspace).findOneBy({ |
| 267 | id: chatflowWorkspaceId |
| 268 | }) |
| 269 | if (!workspace) { |
| 270 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Workspace ${chatflowWorkspaceId} not found`) |
| 271 | } |
| 272 | const workspaceId = workspace.id |
| 273 | |
| 274 | if (workspaceId !== req.user?.activeWorkspaceId) throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, 'Unauthorized') |
| 275 | |
| 276 | const org = await appServer.AppDataSource.getRepository(Organization).findOneBy({ |
| 277 | id: workspace.organizationId |
| 278 | }) |
| 279 | if (!org) { |
| 280 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Organization ${workspace.organizationId} not found`) |
| 281 | } |
| 282 | |
| 283 | const orgId = org.id |
| 284 | const subscriptionId = org.subscriptionId as string |
| 285 | const productId = await appServer.identityManager.getProductIdFromSubscription(subscriptionId) |
| 286 | |
| 287 | const executeData: IExecuteFlowParams = { |
| 288 | componentNodes: appServer.nodesPool.componentNodes, |
| 289 | incomingInput, |
| 290 | chatflow, |
| 291 | chatId, |
| 292 | appDataSource: appServer.AppDataSource, |
| 293 | telemetry: appServer.telemetry, |
| 294 | cachePool: appServer.cachePool, |
| 295 | sseStreamer: appServer.sseStreamer, |
no test coverage detected