From 67a898ea33b22a8ffd2a799001e6e149bdacc3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Dub=C3=A9?= Date: Mon, 24 Mar 2025 13:40:01 -0400 Subject: [PATCH] fix: update check_usage_ to only consider past month usage (#1209) This change modifies the SQL query in check_usage_ to only consider AI usage from the past month when checking against limits, rather than all historical usage. ai: true --- src/backend/src/modules/puterai/AIChatService.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/backend/src/modules/puterai/AIChatService.js b/src/backend/src/modules/puterai/AIChatService.js index f2ff4e7b4..67e57b47c 100644 --- a/src/backend/src/modules/puterai/AIChatService.js +++ b/src/backend/src/modules/puterai/AIChatService.js @@ -662,11 +662,15 @@ class AIChatService extends BaseService { const reading = await svc_permission.scan(actor, `paid-services:ai-chat`); const options = PermissionUtil.reading_to_options(reading); - // Query current ai usage in terms of cost + // Query current ai usage in terms of cost (only from the past month) + const oneMonthAgo = new Date(); + oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1); + const oneMonthAgoStr = oneMonthAgo.toISOString().slice(0, 19).replace('T', ' '); + const [row] = await this.db.read( 'SELECT SUM(`cost`) AS sum FROM `ai_usage` ' + - 'WHERE `user_id` = ?', - [actor.type.user.id] + 'WHERE `user_id` = ? AND `created_at` >= ?', + [actor.type.user.id, oneMonthAgoStr] ); const cost_used = row?.sum || 0;