LibreChat/api/server/controllers/ContextProjectionController.js
Danny Avila e2310c9433
🪙 feat: Context-projection backend endpoint
POST /api/endpoints/context-projection → resolveContextProjection (packages/api):
reconstructs the viewed branch (parent-chain walk from messageId), resolves the
agent config (instructions/provider/model/maxContextTokens), reuses LibreChat's
stored per-message tokenCounts as the index map (no re-tokenizing), and calls
the agents SDK projectAgentContextUsage — no model call. Thin controller injects
db.getMessages/db.getAgent; route mirrors /token-config.

First cut targets message-windowing accuracy; tool-schema tokens are deferred to
a follow-up that reuses the full initializeAgent path.
2026-06-16 16:52:01 -04:00

31 lines
1.1 KiB
JavaScript

const { logger } = require('@librechat/data-schemas');
const { resolveContextProjection } = require('@librechat/api');
const db = require('~/models');
/**
* Returns a server-side context-usage projection for the viewed branch + config
* (agents SDK, no model call) — powers the gauge for snapshot-less branches and
* after a model/window switch. Resolution lives in `@librechat/api`; this
* controller only injects request-scoped model accessors.
* @param {ServerRequest} req
* @param {ServerResponse} res
*/
async function contextProjectionController(req, res) {
try {
const params = req.body ?? {};
if (!params.conversationId || !params.messageId) {
res.json(null);
return;
}
const projection = await resolveContextProjection(
{ getMessages: db.getMessages, getAgent: db.getAgent },
params,
);
res.json(projection ?? null);
} catch (error) {
logger.error('[contextProjectionController]', error);
res.status(500).json({ error: 'Failed to resolve context projection' });
}
}
module.exports = contextProjectionController;