fix(api-potluck): 修复使用量统计在多请求ID场景下的问题
重构获取待处理使用量的逻辑,提取公共函数避免重复代码。修复当同时存在插件请求ID和监控请求ID时,使用量统计可能被错误删除的问题。
This commit is contained in:
parent
8afcb479fa
commit
ba55ce3f3a
2 changed files with 22 additions and 6 deletions
2
VERSION
2
VERSION
|
|
@ -1 +1 @@
|
|||
2.13.4
|
||||
2.13.4.1
|
||||
|
|
|
|||
|
|
@ -91,6 +91,24 @@ function extractUsage(...candidates) {
|
|||
});
|
||||
}
|
||||
|
||||
function getTrackedRequestIds(hookContext = {}) {
|
||||
return [...new Set([
|
||||
hookContext._monitorRequestId,
|
||||
hookContext._pluginRequestId
|
||||
].filter(Boolean))];
|
||||
}
|
||||
|
||||
function getPendingUsageForHookContext(hookContext = {}) {
|
||||
for (const requestId of getTrackedRequestIds(hookContext)) {
|
||||
const usage = pendingUsage.get(requestId);
|
||||
if (usage) {
|
||||
return usage;
|
||||
}
|
||||
}
|
||||
|
||||
return { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件定义
|
||||
*/
|
||||
|
|
@ -224,13 +242,11 @@ const apiPotluckPlugin = {
|
|||
* @param {Object} hookContext - 钩子上下文,包含请求和模型信息
|
||||
*/
|
||||
async onContentGenerated(hookContext) {
|
||||
const requestId = hookContext._pluginRequestId || hookContext._monitorRequestId;
|
||||
const trackedRequestIds = getTrackedRequestIds(hookContext);
|
||||
|
||||
if (hookContext.potluckApiKey) {
|
||||
try {
|
||||
const usage = requestId
|
||||
? (pendingUsage.get(requestId) || { promptTokens: 0, completionTokens: 0, totalTokens: 0 })
|
||||
: { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
||||
const usage = getPendingUsageForHookContext(hookContext);
|
||||
|
||||
// 传入提供商和模型信息
|
||||
await incrementUsage(
|
||||
|
|
@ -245,7 +261,7 @@ const apiPotluckPlugin = {
|
|||
}
|
||||
}
|
||||
|
||||
if (requestId) {
|
||||
for (const requestId of trackedRequestIds) {
|
||||
pendingUsage.delete(requestId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue