From ba55ce3f3aed6d942854aac1636b2641cf8ac272 Mon Sep 17 00:00:00 2001 From: hex2077 Date: Fri, 10 Apr 2026 16:24:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(api-potluck):=20=E4=BF=AE=E5=A4=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E7=BB=9F=E8=AE=A1=E5=9C=A8=E5=A4=9A=E8=AF=B7?= =?UTF-8?q?=E6=B1=82ID=E5=9C=BA=E6=99=AF=E4=B8=8B=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构获取待处理使用量的逻辑,提取公共函数避免重复代码。修复当同时存在插件请求ID和监控请求ID时,使用量统计可能被错误删除的问题。 --- VERSION | 2 +- src/plugins/api-potluck/index.js | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/VERSION b/VERSION index 965a689..a9f871b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.13.4 +2.13.4.1 diff --git a/src/plugins/api-potluck/index.js b/src/plugins/api-potluck/index.js index 4839cfd..e56dd58 100644 --- a/src/plugins/api-potluck/index.js +++ b/src/plugins/api-potluck/index.js @@ -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); } }