diff --git a/package-lock.json b/package-lock.json index 0c610c2f26..d9c3ea5f42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48787,7 +48787,7 @@ }, "packages/data-schemas": { "name": "@librechat/data-schemas", - "version": "0.0.12", + "version": "0.0.13", "license": "MIT", "devDependencies": { "@rollup/plugin-alias": "^5.1.0", diff --git a/packages/data-schemas/package.json b/packages/data-schemas/package.json index 6f299e3f09..651f82e22c 100644 --- a/packages/data-schemas/package.json +++ b/packages/data-schemas/package.json @@ -1,6 +1,6 @@ { "name": "@librechat/data-schemas", - "version": "0.0.12", + "version": "0.0.13", "description": "Mongoose schemas and models for LibreChat", "type": "module", "main": "dist/index.cjs", diff --git a/packages/data-schemas/src/config/meiliLogger.ts b/packages/data-schemas/src/config/meiliLogger.ts index 0d4d39475b..572e877071 100644 --- a/packages/data-schemas/src/config/meiliLogger.ts +++ b/packages/data-schemas/src/config/meiliLogger.ts @@ -1,8 +1,8 @@ -import path from 'path'; import winston from 'winston'; import 'winston-daily-rotate-file'; +import { getLogDirectory } from './utils'; -const logDir = path.join(__dirname, '..', '..', '..', 'api', 'logs'); +const logDir = getLogDirectory(); const { NODE_ENV, DEBUG_LOGGING = 'false' } = process.env; diff --git a/packages/data-schemas/src/config/utils.ts b/packages/data-schemas/src/config/utils.ts new file mode 100644 index 0000000000..323334207b --- /dev/null +++ b/packages/data-schemas/src/config/utils.ts @@ -0,0 +1,37 @@ +import path from 'path'; + +/** + * Determine the log directory in a cross-compatible way. + * Priority: + * 1. LIBRECHAT_LOG_DIR environment variable + * 2. If running within LibreChat monorepo (when cwd ends with /api), use api/logs + * 3. If api/logs exists relative to cwd, use that (for running from project root) + * 4. Otherwise, use logs directory relative to process.cwd() + * + * This avoids using __dirname which is not available in ESM modules + */ +export const getLogDirectory = (): string => { + if (process.env.LIBRECHAT_LOG_DIR) { + return process.env.LIBRECHAT_LOG_DIR; + } + + const cwd = process.cwd(); + + // Check if we're running from within the api directory + if (cwd.endsWith('/api') || cwd.endsWith('\\api')) { + return path.join(cwd, 'logs'); + } + + // Check if api/logs exists relative to current directory (running from project root) + // We'll just use the path and let the file system create it if needed + const apiLogsPath = path.join(cwd, 'api', 'logs'); + + // For LibreChat project structure, use api/logs + // For external consumers, they should set LIBRECHAT_LOG_DIR + if (cwd.includes('LibreChat')) { + return apiLogsPath; + } + + // Default to logs directory relative to current working directory + return path.join(cwd, 'logs'); +}; diff --git a/packages/data-schemas/src/config/winston.ts b/packages/data-schemas/src/config/winston.ts index 7e52872962..0141094bdc 100644 --- a/packages/data-schemas/src/config/winston.ts +++ b/packages/data-schemas/src/config/winston.ts @@ -1,9 +1,9 @@ -import path from 'path'; import winston from 'winston'; import 'winston-daily-rotate-file'; import { redactFormat, redactMessage, debugTraverse, jsonTruncateFormat } from './parsers'; +import { getLogDirectory } from './utils'; -const logDir = path.join(__dirname, '..', '..', '..', 'api', 'logs'); +const logDir = getLogDirectory(); const { NODE_ENV, DEBUG_LOGGING, CONSOLE_JSON, DEBUG_CONSOLE } = process.env;