(requestBody: any, orgId: string, workspaceId: string)
| 22 | import nodesService from '../nodes' |
| 23 | |
| 24 | const createAssistant = async (requestBody: any, orgId: string, workspaceId: string): Promise<Assistant> => { |
| 25 | try { |
| 26 | const appServer = getRunningExpressApp() |
| 27 | if (!requestBody.details) { |
| 28 | throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Invalid request body`) |
| 29 | } |
| 30 | const assistantDetails = JSON.parse(requestBody.details) |
| 31 | |
| 32 | if (requestBody.type === 'CUSTOM') { |
| 33 | // For CUSTOM assistants the credential field is a client-generated UUID used as an |
| 34 | // internal identifier, not a reference to the Credential entity, so no lookup is needed. |
| 35 | const newAssistant = new Assistant() |
| 36 | Object.assign(newAssistant, stripProtectedFields(requestBody)) |
| 37 | newAssistant.workspaceId = workspaceId |
| 38 | |
| 39 | const assistant = appServer.AppDataSource.getRepository(Assistant).create(newAssistant) |
| 40 | const dbResponse = await appServer.AppDataSource.getRepository(Assistant).save(assistant) |
| 41 | |
| 42 | await appServer.telemetry.sendTelemetry( |
| 43 | 'assistant_created', |
| 44 | { |
| 45 | version: await getAppVersion(), |
| 46 | assistantId: dbResponse.id |
| 47 | }, |
| 48 | orgId |
| 49 | ) |
| 50 | appServer.metricsProvider?.incrementCounter(FLOWISE_METRIC_COUNTERS.ASSISTANT_CREATED, { |
| 51 | status: FLOWISE_COUNTER_STATUS.SUCCESS |
| 52 | }) |
| 53 | return dbResponse |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({ |
| 58 | id: requestBody.credential, |
| 59 | workspaceId: workspaceId |
| 60 | }) |
| 61 | |
| 62 | if (!credential) { |
| 63 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${requestBody.credential} not found`) |
| 64 | } |
| 65 | |
| 66 | // Decrpyt credentialData |
| 67 | const decryptedCredentialData = await decryptCredentialData(credential.encryptedData) |
| 68 | const openAIApiKey = decryptedCredentialData['openAIApiKey'] |
| 69 | if (!openAIApiKey) { |
| 70 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`) |
| 71 | } |
| 72 | const openai = new OpenAI({ apiKey: openAIApiKey }) |
| 73 | |
| 74 | // Prepare tools |
| 75 | let tools = [] |
| 76 | if (assistantDetails.tools) { |
| 77 | for (const tool of assistantDetails.tools ?? []) { |
| 78 | tools.push({ |
| 79 | type: tool |
| 80 | }) |
| 81 | } |
nothing calls this directly
no test coverage detected