Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/server/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ export interface IChatFlow {
workspaceId: string
}

export interface IChatFlowMaster {
id: string
name: string
type?: ChatflowType
workspaceId: string
category?: string
isPublic?: boolean
createdDate: Date
updatedDate: Date
}

export interface IChatFlowVersion {
id: string
masterId: string
version: number
isActive: boolean
flowData: string
apikeyid?: string
chatbotConfig?: string
apiConfig?: string
analytic?: string
speechToText?: string
textToSpeech?: string
followUpPrompts?: string
changeDescription?: string
sourceVersion?: number
createdBy?: string
createdDate: Date
updatedDate: Date
}

export interface IChatMessage {
id: string
role: MessageType
Expand Down
218 changes: 218 additions & 0 deletions packages/server/src/controllers/chatflow-versions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import { NextFunction, Request, Response } from 'express'
import { StatusCodes } from 'http-status-codes'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import chatflowVersionsService from '../../services/chatflow-versions'

/**
* Get all versions for a chatflow
*/
const getAllVersions = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.getAllVersions - id not provided!`
)
}

const workspaceId = req.user?.activeWorkspaceId
if (!workspaceId) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Error: chatflowVersionsController.getAllVersions - workspace not found!`)
}

const apiResponse = await chatflowVersionsService.getAllVersions(req.params.id, workspaceId)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
Comment thread
AhmedRaafat14 marked this conversation as resolved.

/**
* Get a specific version
*/
const getVersion = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.params.id || !req.params.version) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.getVersion - id or version not provided!`
)
}

const workspaceId = req.user?.activeWorkspaceId
if (!workspaceId) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Error: chatflowVersionsController.getVersion - workspace not found!`)
}

const versionNumber = parseInt(req.params.version, 10)
if (isNaN(versionNumber)) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Error: chatflowVersionsController.getVersion - invalid version number!`
)
}

const apiResponse = await chatflowVersionsService.getVersion(req.params.id, versionNumber, workspaceId)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

/**
* Create a new version
*/
const createVersion = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.createVersion - id not provided!`
)
}

if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.createVersion - body not provided!`
)
}

const workspaceId = req.user?.activeWorkspaceId
if (!workspaceId) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Error: chatflowVersionsController.createVersion - workspace not found!`)
}

const versionData = {
...req.body,
createdBy: req.user?.email || req.user?.name
}

const apiResponse = await chatflowVersionsService.createVersion(req.params.id, workspaceId, versionData)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

/**
* Update a version
*/
const updateVersion = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.params.id || !req.params.version) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.updateVersion - id or version not provided!`
)
}

if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.updateVersion - body not provided!`
)
}

const workspaceId = req.user?.activeWorkspaceId
if (!workspaceId) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Error: chatflowVersionsController.updateVersion - workspace not found!`)
}

const versionNumber = parseInt(req.params.version, 10)
if (isNaN(versionNumber)) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Error: chatflowVersionsController.updateVersion - invalid version number!`
)
}

const apiResponse = await chatflowVersionsService.updateVersion(req.params.id, versionNumber, workspaceId, req.body)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

/**
* Set active version
*/
const setActiveVersion = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.setActiveVersion - id not provided!`
)
}

if (!req.body || req.body.version === undefined) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.setActiveVersion - version not provided!`
)
}

const workspaceId = req.user?.activeWorkspaceId
if (!workspaceId) {
throw new InternalFlowiseError(
StatusCodes.NOT_FOUND,
`Error: chatflowVersionsController.setActiveVersion - workspace not found!`
)
}

const versionNumber = parseInt(req.body.version, 10)
if (isNaN(versionNumber)) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Error: chatflowVersionsController.setActiveVersion - invalid version number!`
)
}

await chatflowVersionsService.setActiveVersion(req.params.id, versionNumber, workspaceId)
return res.json({ success: true, message: `Version ${versionNumber} is now active` })
} catch (error) {
next(error)
}
}

/**
* Delete a version
*/
const deleteVersion = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.params.id || !req.params.version) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: chatflowVersionsController.deleteVersion - id or version not provided!`
)
}

const workspaceId = req.user?.activeWorkspaceId
if (!workspaceId) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Error: chatflowVersionsController.deleteVersion - workspace not found!`)
}

const versionNumber = parseInt(req.params.version, 10)
if (isNaN(versionNumber)) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Error: chatflowVersionsController.deleteVersion - invalid version number!`
)
}

await chatflowVersionsService.deleteVersion(req.params.id, versionNumber, workspaceId)
return res.json({ success: true, message: `Version ${versionNumber} deleted successfully` })
} catch (error) {
next(error)
}
}

export default {
getAllVersions,
getVersion,
createVersion,
updateVersion,
setActiveVersion,
deleteVersion
}
Loading