-
-
Notifications
You must be signed in to change notification settings - Fork 24.1k
Fix Mass Assignment in Assistants Endpoints #6052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { Assistant } from '../../database/entities/Assistant' | |
| import { Credential } from '../../database/entities/Credential' | ||
| import { DocumentStore } from '../../database/entities/DocumentStore' | ||
| import { Workspace } from '../../enterprise/database/entities/workspace.entity' | ||
| import { WorkspaceService } from '../../enterprise/services/workspace.service' | ||
| import { getWorkspaceSearchOptions } from '../../enterprise/utils/ControllerServiceUtils' | ||
| import { InternalFlowiseError } from '../../errors/internalFlowiseError' | ||
| import { getErrorMessage } from '../../errors/utils' | ||
|
|
@@ -20,7 +21,37 @@ import { ASSISTANT_PROMPT_GENERATOR } from '../../utils/prompt' | |
| import { checkUsageLimit } from '../../utils/quotaUsage' | ||
| import nodesService from '../nodes' | ||
|
|
||
| const createAssistant = async (requestBody: any, orgId: string): Promise<Assistant> => { | ||
| const findAccessibleCredential = async (credentialId: string, workspaceId: string): Promise<Credential | null> => { | ||
| const appServer = getRunningExpressApp() | ||
| const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({ | ||
| id: credentialId, | ||
| workspaceId | ||
| }) | ||
| if (credential) return credential | ||
|
|
||
| const workspaceService = new WorkspaceService() | ||
| const sharedItems = (await workspaceService.getSharedItemsForWorkspace(workspaceId, 'credential')) as Credential[] | ||
| if (sharedItems.some((item) => item.id === credentialId)) { | ||
| return await appServer.AppDataSource.getRepository(Credential).findOneBy({ id: credentialId }) | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| function applyAssistantCreateFields(entity: Assistant, body: any, workspaceId: string): void { | ||
| entity.details = body.details | ||
| entity.credential = body.credential | ||
| entity.iconSrc = body.iconSrc | ||
| entity.type = body.type | ||
| entity.workspaceId = workspaceId | ||
| } | ||
|
|
||
| function applyAssistantUpdateFields(entity: Assistant, details: string, body: any): void { | ||
| entity.details = details | ||
| entity.credential = body.credential | ||
| entity.iconSrc = body.iconSrc | ||
| } | ||
|
|
||
| const createAssistant = async (requestBody: any, orgId: string, workspaceId: string): Promise<Assistant> => { | ||
| try { | ||
| const appServer = getRunningExpressApp() | ||
| if (!requestBody.details) { | ||
|
|
@@ -30,7 +61,7 @@ const createAssistant = async (requestBody: any, orgId: string): Promise<Assista | |
|
|
||
| if (requestBody.type === 'CUSTOM') { | ||
| const newAssistant = new Assistant() | ||
| Object.assign(newAssistant, requestBody) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we just want to overwrite Object.assign(newAssistant, requestBody, {
workspaceId,
})
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, there are other fields as well that we don't want to allow update for through the request, e.g. updatedTime. I think it could get more complicated with this?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the |
||
| applyAssistantCreateFields(newAssistant, requestBody, workspaceId) | ||
|
|
||
| const assistant = appServer.AppDataSource.getRepository(Assistant).create(newAssistant) | ||
| const dbResponse = await appServer.AppDataSource.getRepository(Assistant).save(assistant) | ||
|
|
@@ -50,9 +81,7 @@ const createAssistant = async (requestBody: any, orgId: string): Promise<Assista | |
| } | ||
|
|
||
| try { | ||
| const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({ | ||
| id: requestBody.credential | ||
| }) | ||
| const credential = await findAccessibleCredential(requestBody.credential, workspaceId) | ||
|
|
||
| if (!credential) { | ||
| throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${requestBody.credential} not found`) | ||
|
|
@@ -135,7 +164,7 @@ const createAssistant = async (requestBody: any, orgId: string): Promise<Assista | |
| throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error creating new assistant - ${getErrorMessage(error)}`) | ||
| } | ||
| const newAssistant = new Assistant() | ||
| Object.assign(newAssistant, requestBody) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the above comment |
||
| applyAssistantCreateFields(newAssistant, requestBody, workspaceId) | ||
|
|
||
| const assistant = appServer.AppDataSource.getRepository(Assistant).create(newAssistant) | ||
| const dbResponse = await appServer.AppDataSource.getRepository(Assistant).save(assistant) | ||
|
|
@@ -298,11 +327,7 @@ const updateAssistant = async (assistantId: string, requestBody: any, workspaceI | |
| } | ||
|
|
||
| if (assistant.type === 'CUSTOM') { | ||
| const body = requestBody | ||
| const updateAssistant = new Assistant() | ||
| Object.assign(updateAssistant, body) | ||
|
|
||
| appServer.AppDataSource.getRepository(Assistant).merge(assistant, updateAssistant) | ||
| applyAssistantUpdateFields(assistant, requestBody.details, requestBody) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what's happening here. The |
||
| const dbResponse = await appServer.AppDataSource.getRepository(Assistant).save(assistant) | ||
| return dbResponse | ||
| } | ||
|
|
@@ -311,9 +336,7 @@ const updateAssistant = async (assistantId: string, requestBody: any, workspaceI | |
| const openAIAssistantId = JSON.parse(assistant.details)?.id | ||
| const body = requestBody | ||
| const assistantDetails = JSON.parse(body.details) | ||
| const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({ | ||
| id: body.credential | ||
| }) | ||
| const credential = await findAccessibleCredential(body.credential, workspaceId) | ||
|
|
||
| if (!credential) { | ||
| throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${body.credential} not found`) | ||
|
|
@@ -376,11 +399,7 @@ const updateAssistant = async (assistantId: string, requestBody: any, workspaceI | |
| } | ||
| if (savedToolResources) newAssistantDetails.tool_resources = savedToolResources | ||
|
|
||
| const updateAssistant = new Assistant() | ||
| body.details = JSON.stringify(newAssistantDetails) | ||
| Object.assign(updateAssistant, body) | ||
|
|
||
| appServer.AppDataSource.getRepository(Assistant).merge(assistant, updateAssistant) | ||
| applyAssistantUpdateFields(assistant, JSON.stringify(newAssistantDetails), body) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the above comment |
||
| const dbResponse = await appServer.AppDataSource.getRepository(Assistant).save(assistant) | ||
| return dbResponse | ||
| } catch (error) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.