LibreChat/api/server/controllers/TokenConfigController.js
Danny Avila a5f9ae3351 🩹 fix: Move Token-Config Resolution to TS; Key Live Usage by Created Convo
- extract the token-config resolution (override gathering + cache lookup +
  buildTokenConfigMap) into resolveTokenConfigMap in packages/api, leaving
  the /api controller a thin request-scoped wrapper (CLAUDE.md TS rule)
- getConvoKey prefers the user message's real conversationId once the
  `created` event stamps it, so a new chat's first-response live gauge and
  totals land under the id TokenUsage subscribes to instead of NEW_CONVO
2026-06-13 13:52:30 -04:00

27 lines
1.1 KiB
JavaScript

const { logger } = require('@librechat/data-schemas');
const { resolveTokenConfigMap } = require('@librechat/api');
const { getModelsConfig } = require('~/server/controllers/ModelController');
const { getValueKey, getMultiplier, getCacheMultiplier } = require('~/models');
/**
* Returns server-resolved context windows (and pricing when
* `interface.contextCost` is enabled) for every configured model. Resolution
* lives in `@librechat/api`; this controller only supplies request-scoped deps.
* @param {ServerRequest} req
* @param {ServerResponse} res
*/
async function tokenConfigController(req, res) {
try {
const modelsConfig = await getModelsConfig(req);
const tokenConfigMap = await resolveTokenConfigMap(
{ appConfig: req.config, modelsConfig, userId: req.user.id },
{ getValueKey, getMultiplier, getCacheMultiplier },
);
res.json(tokenConfigMap);
} catch (error) {
logger.error('[tokenConfigController]', error);
res.status(500).json({ error: 'Failed to resolve token config' });
}
}
module.exports = tokenConfigController;