Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion packages/server/src/controllers/openai-realtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const getAgentTools = async (req: Request, res: Response, next: NextFunction) =>
`Error: openaiRealTimeController.getAgentTools - id not provided!`
)
}
const apiResponse = await openaiRealTimeService.getAgentTools(req.params.id)
const workspaceId = req.user?.activeWorkspaceId
const apiResponse = await openaiRealTimeService.getAgentTools(req.params.id, workspaceId)
return res.json(apiResponse)
} catch (error) {
next(error)
Expand Down Expand Up @@ -50,8 +51,10 @@ const executeAgentTool = async (req: Request, res: Response, next: NextFunction)
`Error: openaiRealTimeController.executeAgentTool - body inputArgs not provided!`
)
}
const workspaceId = req.user?.activeWorkspaceId
const apiResponse = await openaiRealTimeService.executeAgentTool(
req.params.id,
workspaceId,
req.body.chatId,
req.body.toolName,
req.body.inputArgs,
Expand Down
13 changes: 11 additions & 2 deletions packages/server/src/routes/openai-realtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import express from 'express'
import openaiRealTimeController from '../../controllers/openai-realtime'
import { checkAnyPermission } from '../../enterprise/rbac/PermissionCheck'

const router = express.Router()

// GET
router.get(['/', '/:id'], openaiRealTimeController.getAgentTools)
router.get(
['/', '/:id'],
checkAnyPermission('chatflows:view,chatflows:create,chatflows:update,chatflows:delete'),
openaiRealTimeController.getAgentTools
)

// EXECUTE
router.post(['/', '/:id'], openaiRealTimeController.executeAgentTool)
router.post(
['/', '/:id'],
checkAnyPermission('chatflows:view,chatflows:create,chatflows:update,chatflows:delete'),
openaiRealTimeController.executeAgentTool
)

export default router
3 changes: 2 additions & 1 deletion packages/server/src/services/export-import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ const exportData = async (exportInput: ExportInput, activeWorkspaceId: string):

let Variable: Variable[] | { data: Variable[]; total: number } =
exportInput.variable === true ? await variableService.getAllVariables(activeWorkspaceId) : []
Variable = 'data' in Variable ? Variable.data : Variable
const variables = 'data' in Variable ? Variable.data : Variable
Variable = variables.map((v) => ({ ...v, value: '' }))

return {
FileDefaultName,
Expand Down
24 changes: 13 additions & 11 deletions packages/server/src/services/openai-realtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../utils'
import { checkStorage, updateStorageUsage } from '../../utils/quotaUsage'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { ChatFlow } from '../../database/entities/ChatFlow'
import chatflowsService from '../chatflows'
import { IDepthQueue, IReactFlowNode } from '../../Interface'
import { ICommonObject, INodeData } from 'flowise-components'
import { convertToOpenAIFunction } from '@langchain/core/utils/function_calling'
Expand All @@ -26,14 +26,9 @@ const SOURCE_DOCUMENTS_PREFIX = '\n\n----FLOWISE_SOURCE_DOCUMENTS----\n\n'
const ARTIFACTS_PREFIX = '\n\n----FLOWISE_ARTIFACTS----\n\n'
const TOOL_ARGS_PREFIX = '\n\n----FLOWISE_TOOL_ARGS----\n\n'

const buildAndInitTool = async (chatflowid: string, _chatId?: string, _apiMessageId?: string) => {
const buildAndInitTool = async (chatflowid: string, reqWorkspaceId?: string, _chatId?: string, _apiMessageId?: string) => {
const appServer = getRunningExpressApp()
const chatflow = await appServer.AppDataSource.getRepository(ChatFlow).findOneBy({
id: chatflowid
})
if (!chatflow) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`)
}
const chatflow = await chatflowsService.getChatflowByIdForWorkspace(chatflowid, reqWorkspaceId)

const chatId = _chatId || uuidv4()
const apiMessageId = _apiMessageId || uuidv4()
Expand Down Expand Up @@ -156,12 +151,15 @@ const buildAndInitTool = async (chatflowid: string, _chatId?: string, _apiMessag
return agent
}

const getAgentTools = async (chatflowid: string): Promise<any> => {
const getAgentTools = async (chatflowid: string, workspaceId?: string): Promise<any> => {
try {
const agent = await buildAndInitTool(chatflowid)
const agent = await buildAndInitTool(chatflowid, workspaceId)
const tools = agent.tools
return tools.map(convertToOpenAIFunction)
} catch (error) {
if (error instanceof InternalFlowiseError) {
throw error
}
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: openaiRealTimeService.getAgentTools - ${getErrorMessage(error)}`
Expand All @@ -171,13 +169,14 @@ const getAgentTools = async (chatflowid: string): Promise<any> => {

const executeAgentTool = async (
chatflowid: string,
workspaceId: string | undefined,
chatId: string,
toolName: string,
inputArgs: string,
apiMessageId?: string
): Promise<any> => {
try {
const agent = await buildAndInitTool(chatflowid, chatId, apiMessageId)
const agent = await buildAndInitTool(chatflowid, workspaceId, chatId, apiMessageId)
const tools = agent.tools
const tool = tools.find((tool: any) => tool.name === toolName)

Expand Down Expand Up @@ -228,6 +227,9 @@ const executeAgentTool = async (
artifacts
}
} catch (error) {
if (error instanceof InternalFlowiseError) {
throw error
}
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: openaiRealTimeService.executeAgentTool - ${getErrorMessage(error)}`
Expand Down
Loading