fix: update imports to use librechat-data-provider for token functions

Move matchModelName, findMatchingPattern, getModelMaxTokens, and
maxTokensMap imports from @librechat/api to librechat-data-provider
in CJS backend files, matching where these functions now live.
This commit is contained in:
Marco Beretta 2026-02-08 18:35:46 +01:00
parent cb6ce42815
commit ff9549e79c
No known key found for this signature in database
GPG key ID: D918033D8E74CC11
5 changed files with 19 additions and 28 deletions

View file

@ -1,4 +1,4 @@
const { getModelMaxTokens } = require('@librechat/api');
const { getModelMaxTokens } = require('librechat-data-provider');
const BaseClient = require('../BaseClient');
class FakeClient extends BaseClient {

View file

@ -1,34 +1,24 @@
const { matchModelName, findMatchingPattern } = require('@librechat/api');
const { matchModelName, findMatchingPattern } = require('librechat-data-provider');
const defaultRate = 6;
/**
* Token Pricing Configuration
*
* IMPORTANT: Key Ordering for Pattern Matching
* ============================================
* The `findMatchingPattern` function iterates through object keys in REVERSE order
* (last-defined keys are checked first) and uses `modelName.includes(key)` for matching.
* Pattern Matching Strategy
* ========================
* The `findMatchingPattern` function uses a longest-match strategy: it checks all keys
* and returns the longest key that is a substring of the model name. This ensures
* specific patterns always take priority over generic ones regardless of definition order.
*
* This means:
* 1. BASE PATTERNS must be defined FIRST (e.g., "kimi", "moonshot")
* 2. SPECIFIC PATTERNS must be defined AFTER their base patterns (e.g., "kimi-k2", "kimi-k2.5")
* Example for Kimi models:
* kimi: { prompt: 0.6, completion: 2.5 }, // Base pattern (3 chars)
* 'kimi-k2': { prompt: 0.6, completion: 2.5 }, // More specific (7 chars)
* 'kimi-k2.5': { prompt: 0.6, completion: 3.0 }, // Most specific (9 chars)
*
* Example ordering for Kimi models:
* kimi: { prompt: 0.6, completion: 2.5 }, // Base pattern - checked last
* 'kimi-k2': { prompt: 0.6, completion: 2.5 }, // More specific - checked before "kimi"
* 'kimi-k2.5': { prompt: 0.6, completion: 3.0 }, // Most specific - checked first
* For model name "kimi-k2.5", all three keys match, but "kimi-k2.5" wins (longest).
*
* Why this matters:
* - Model name "kimi-k2.5" contains both "kimi" and "kimi-k2" as substrings
* - If "kimi" were checked first, it would incorrectly match and return wrong pricing
* - By defining specific patterns AFTER base patterns, they're checked first in reverse iteration
*
* This applies to BOTH `tokenValues` and `cacheTokenValues` objects.
*
* When adding new model families:
* 1. Define the base/generic pattern first
* 2. Define increasingly specific patterns after
* 3. Ensure no pattern is a substring of another that should match differently
* Keys are ordered base-to-specific for readability, but this is not required
* by the algorithm. This applies to BOTH `tokenValues` and `cacheTokenValues` objects.
*/
/**

View file

@ -1,6 +1,5 @@
/** Note: No hard-coded values should be used in this file. */
const { maxTokensMap } = require('@librechat/api');
const { EModelEndpoint } = require('librechat-data-provider');
const { EModelEndpoint, maxTokensMap } = require('librechat-data-provider');
const {
defaultRate,
tokenValues,

View file

@ -1,7 +1,7 @@
const { v4 } = require('uuid');
const { sleep } = require('@librechat/agents');
const { logger } = require('@librechat/data-schemas');
const { sendEvent, getBalanceConfig, getModelMaxTokens, countTokens } = require('@librechat/api');
const { sendEvent, getBalanceConfig, countTokens } = require('@librechat/api');
const {
Time,
Constants,
@ -12,6 +12,7 @@ const {
EModelEndpoint,
ViolationTypes,
ImageVisionTool,
getModelMaxTokens,
checkOpenAIStorage,
AssistantStreamEvents,
} = require('librechat-data-provider');

View file

@ -1,7 +1,7 @@
const { v4 } = require('uuid');
const { sleep } = require('@librechat/agents');
const { logger } = require('@librechat/data-schemas');
const { sendEvent, getBalanceConfig, getModelMaxTokens, countTokens } = require('@librechat/api');
const { sendEvent, getBalanceConfig, countTokens } = require('@librechat/api');
const {
Time,
Constants,
@ -10,6 +10,7 @@ const {
ContentTypes,
ToolCallTypes,
EModelEndpoint,
getModelMaxTokens,
retrievalMimeTypes,
AssistantStreamEvents,
} = require('librechat-data-provider');