mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* 🏗️ refactor: Improve spendTokens logic to handle zero completion tokens and enhance test coverage * 🏗️ test: Add tests to ensure balance does not go below zero when spending tokens * 🏗️ fix: Ensure proper continuation in AgentClient when handling errors * fix: spend token race conditions * 🏗️ test: Add test for handling multiple concurrent transactions with high balance * fix: Handle Omni models prompt prefix handling for user messages with array content in OpenAIClient * refactor: Update checkBalance import paths to use new balanceMethods module * refactor: Update checkBalance imports and implement updateBalance function for atomic balance updates * fix: import from replace method * feat: Add createAutoRefillTransaction method to handle non-balance updating transactions * refactor: Move auto-refill logic to balanceMethods and enhance checkBalance functionality * feat: Implement logging for auto-refill transactions in balance checks * refactor: Remove logRefill calls from multiple client and handler files * refactor: Move balance checking and auto-refill logic to balanceMethods for improved structure * refactor: Simplify balance check calls by removing unnecessary balanceRecord assignments * fix: Prevent negative rawAmount in spendTokens when promptTokens is zero * fix: Update balanceMethods to use Balance model for findOneAndUpdate * chore: import order * refactor: remove unused txMethods file to streamline codebase * feat: enhance updateBalance and createAutoRefillTransaction methods to support additional parameters for improved balance management
95 lines
2.6 KiB
JavaScript
95 lines
2.6 KiB
JavaScript
const { promptTokensEstimate } = require('openai-chat-tokens');
|
|
const { EModelEndpoint, supportsBalanceCheck } = require('librechat-data-provider');
|
|
const { formatFromLangChain } = require('~/app/clients/prompts');
|
|
const { getBalanceConfig } = require('~/server/services/Config');
|
|
const { checkBalance } = require('~/models/balanceMethods');
|
|
const { logger } = require('~/config');
|
|
|
|
const createStartHandler = ({
|
|
context,
|
|
conversationId,
|
|
tokenBuffer = 0,
|
|
initialMessageCount,
|
|
manager,
|
|
}) => {
|
|
return async (_llm, _messages, runId, parentRunId, extraParams) => {
|
|
const { invocation_params } = extraParams;
|
|
const { model, functions, function_call } = invocation_params;
|
|
const messages = _messages[0].map(formatFromLangChain);
|
|
|
|
logger.debug(`[createStartHandler] handleChatModelStart: ${context}`, {
|
|
model,
|
|
function_call,
|
|
});
|
|
|
|
if (context !== 'title') {
|
|
logger.debug(`[createStartHandler] handleChatModelStart: ${context}`, {
|
|
functions,
|
|
});
|
|
}
|
|
|
|
const payload = { messages };
|
|
let prelimPromptTokens = 1;
|
|
|
|
if (functions) {
|
|
payload.functions = functions;
|
|
prelimPromptTokens += 2;
|
|
}
|
|
|
|
if (function_call) {
|
|
payload.function_call = function_call;
|
|
prelimPromptTokens -= 5;
|
|
}
|
|
|
|
prelimPromptTokens += promptTokensEstimate(payload);
|
|
logger.debug('[createStartHandler]', {
|
|
prelimPromptTokens,
|
|
tokenBuffer,
|
|
});
|
|
prelimPromptTokens += tokenBuffer;
|
|
|
|
try {
|
|
const balance = await getBalanceConfig();
|
|
if (balance?.enabled && supportsBalanceCheck[EModelEndpoint.openAI]) {
|
|
const generations =
|
|
initialMessageCount && messages.length > initialMessageCount
|
|
? messages.slice(initialMessageCount)
|
|
: null;
|
|
await checkBalance({
|
|
req: manager.req,
|
|
res: manager.res,
|
|
txData: {
|
|
user: manager.user,
|
|
tokenType: 'prompt',
|
|
amount: prelimPromptTokens,
|
|
debug: manager.debug,
|
|
generations,
|
|
model,
|
|
endpoint: EModelEndpoint.openAI,
|
|
},
|
|
});
|
|
}
|
|
} catch (err) {
|
|
logger.error(`[createStartHandler][${context}] checkBalance error`, err);
|
|
manager.abortController.abort();
|
|
if (context === 'summary' || context === 'plugins') {
|
|
manager.addRun(runId, { conversationId, error: err.message });
|
|
throw new Error(err);
|
|
}
|
|
return;
|
|
}
|
|
|
|
manager.addRun(runId, {
|
|
model,
|
|
messages,
|
|
functions,
|
|
function_call,
|
|
runId,
|
|
parentRunId,
|
|
conversationId,
|
|
prelimPromptTokens,
|
|
});
|
|
};
|
|
};
|
|
|
|
module.exports = createStartHandler;
|