| const FormData = require('form-data'); |
| const { getCodeBaseURL } = require('@librechat/agents'); |
| const { createAxiosInstance, logAxiosError } = require('@librechat/api'); |
|
|
| const axios = createAxiosInstance(); |
|
|
| const MAX_FILE_SIZE = 150 * 1024 * 1024; |
|
|
| |
| |
| |
| |
| |
| |
| |
| async function getCodeOutputDownloadStream(fileIdentifier, apiKey) { |
| try { |
| const baseURL = getCodeBaseURL(); |
| |
| const options = { |
| method: 'get', |
| url: `${baseURL}/download/${fileIdentifier}`, |
| responseType: 'stream', |
| headers: { |
| 'User-Agent': 'LibreChat/1.0', |
| 'X-API-Key': apiKey, |
| }, |
| timeout: 15000, |
| }; |
|
|
| const response = await axios(options); |
| return response; |
| } catch (error) { |
| throw new Error( |
| logAxiosError({ |
| message: `Error downloading code environment file stream: ${error.message}`, |
| error, |
| }), |
| ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function uploadCodeEnvFile({ req, stream, filename, apiKey, entity_id = '' }) { |
| try { |
| const form = new FormData(); |
| if (entity_id.length > 0) { |
| form.append('entity_id', entity_id); |
| } |
| form.append('file', stream, filename); |
|
|
| const baseURL = getCodeBaseURL(); |
| |
| const options = { |
| headers: { |
| ...form.getHeaders(), |
| 'Content-Type': 'multipart/form-data', |
| 'User-Agent': 'LibreChat/1.0', |
| 'User-Id': req.user.id, |
| 'X-API-Key': apiKey, |
| }, |
| maxContentLength: MAX_FILE_SIZE, |
| maxBodyLength: MAX_FILE_SIZE, |
| }; |
|
|
| const response = await axios.post(`${baseURL}/upload`, form, options); |
|
|
| |
| const result = response.data; |
| if (result.message !== 'success') { |
| throw new Error(`Error uploading file: ${result.message}`); |
| } |
|
|
| const fileIdentifier = `${result.session_id}/${result.files[0].fileId}`; |
| if (entity_id.length === 0) { |
| return fileIdentifier; |
| } |
|
|
| return `${fileIdentifier}?entity_id=${entity_id}`; |
| } catch (error) { |
| throw new Error( |
| logAxiosError({ |
| message: `Error uploading code environment file: ${error.message}`, |
| error, |
| }), |
| ); |
| } |
| } |
|
|
| module.exports = { getCodeOutputDownloadStream, uploadCodeEnvFile }; |
|
|