|
| 1 | +const { default: axios } = require('axios') |
| 2 | + |
| 3 | +module.exports = { |
| 4 | + /** |
| 5 | + * Invokes a Language Learning Model (LLM) service with the specified method and payload. |
| 6 | + * |
| 7 | + * @async |
| 8 | + * @function |
| 9 | + * @param {Object} app @ignore - The application instance containing configuration details. |
| 10 | + * @param {string} method - The API endpoint method to call on the LLM service. |
| 11 | + * @param {Object} payload - The request payload to be sent to the LLM. |
| 12 | + * @param {Object} options - An optional object containing configurations for the request. |
| 13 | + * @param {string} options.teamHashId - The unique identifier for the team. |
| 14 | + * @param {Object} [options.additionalHeaders={}] - Additional headers to be included in the request. |
| 15 | + * @param {string} options.instanceType - The type of instance making the request. |
| 16 | + * @param {string} options.instanceId - The unique identifier for the instance. |
| 17 | + * @param {boolean} [options.isTeamOnTrial] - Indicator if the team is currently on trial. |
| 18 | + * @returns {Promise<Object>} Resolves with the response data from the LLM service. |
| 19 | + * @throws {Error} Throws an error if there is a transaction ID mismatch in the response. |
| 20 | + */ |
| 21 | + invokeLLM: async (app, method, payload, { |
| 22 | + teamHashId, |
| 23 | + additionalHeaders = { }, |
| 24 | + instanceType, |
| 25 | + instanceId, |
| 26 | + isTeamOnTrial = undefined |
| 27 | + }) => { |
| 28 | + const timeout = app.config.assistant?.service?.requestTimeout || 60000 |
| 29 | + const serviceUrl = app.config.assistant?.service?.url |
| 30 | + const url = `${serviceUrl.replace(/\/+$/, '')}/${method.replace(/^\/+/, '')}` |
| 31 | + |
| 32 | + const headers = await module.exports.buildRequestHeaders(app, additionalHeaders, { |
| 33 | + isTeamOnTrial, |
| 34 | + instanceType, |
| 35 | + instanceId, |
| 36 | + teamHashId |
| 37 | + }) |
| 38 | + |
| 39 | + const response = await axios.post(url, payload, { headers, timeout }) |
| 40 | + |
| 41 | + if (payload.transactionId !== response.data.transactionId) { |
| 42 | + throw new Error('Transaction ID mismatch') // Ensure we are responding to the correct transaction |
| 43 | + } |
| 44 | + return response.data |
| 45 | + }, |
| 46 | + |
| 47 | + /** |
| 48 | + * Builds and returns the request headers for HTTP requests, combining application configuration, license information, |
| 49 | + * and additional headers as needed. |
| 50 | + * |
| 51 | + * @param {Object} app - The application object containing configuration and license details. |
| 52 | + * @param {Object} additionalHeaders - An object containing additional headers to include in the request. |
| 53 | + * @param {Object} options - Options for customizing the headers. |
| 54 | + * @param {string} options.instanceType - The type of owner for the request (e.g., user, team). |
| 55 | + * @param {string} options.instanceId - The instance ID associated with the request. |
| 56 | + * @param {string} options.teamHashId - The hashed team ID associated with the request. |
| 57 | + * @param {boolean} [options.isTeamOnTrial] - Indicates if the team is currently in a trial period. |
| 58 | + * @returns {Promise<Object>} A Promise */ |
| 59 | + buildRequestHeaders: async (app, additionalHeaders, { instanceType, instanceId, teamHashId, isTeamOnTrial }) => { |
| 60 | + const config = app.config |
| 61 | + const serviceToken = config.assistant?.service?.token |
| 62 | + |
| 63 | + const requestHeaders = { |
| 64 | + 'ff-owner-type': instanceType, |
| 65 | + 'ff-owner-id': instanceId, |
| 66 | + 'ff-team-id': teamHashId |
| 67 | + } |
| 68 | + // include license information, team id and trial status so that we can make decisions in the assistant service |
| 69 | + const isLicensed = app.license?.active() || false |
| 70 | + const licenseType = isLicensed ? (app.license.get('dev') ? 'DEV' : 'EE') : 'CE' |
| 71 | + const tier = isLicensed ? app.license.get('tier') : null |
| 72 | + |
| 73 | + requestHeaders['ff-license-active'] = isLicensed |
| 74 | + requestHeaders['ff-license-type'] = licenseType |
| 75 | + requestHeaders['ff-license-tier'] = tier |
| 76 | + |
| 77 | + if (isTeamOnTrial !== undefined) { |
| 78 | + requestHeaders['ff-team-trial'] = isTeamOnTrial |
| 79 | + } |
| 80 | + if (serviceToken) { |
| 81 | + requestHeaders.Authorization = `Bearer ${serviceToken}` |
| 82 | + } |
| 83 | + if (additionalHeaders.accept) { |
| 84 | + requestHeaders.Accept = additionalHeaders.accept |
| 85 | + } |
| 86 | + if (additionalHeaders['user-agent']) { |
| 87 | + requestHeaders['User-Agent'] = additionalHeaders['user-agent'] |
| 88 | + } |
| 89 | + |
| 90 | + return requestHeaders |
| 91 | + } |
| 92 | +} |
0 commit comments