Count AI cooldown from response time

This commit is contained in:
2026-04-07 16:26:03 +02:00
parent a265160624
commit 2cba15e85a

View File

@@ -1296,13 +1296,13 @@ function listAIAutoCommentRateLimitStatuses(settings = getAIAutoCommentRateLimit
));
}
function reserveAIAutoCommentAction(profileNumber, settings = getAIAutoCommentRateLimitSettings()) {
function checkAIAutoCommentActionAvailability(profileNumber, settings = getAIAutoCommentRateLimitSettings()) {
const normalizedProfileNumber = sanitizeProfileNumber(profileNumber);
if (!normalizedProfileNumber) {
return { ok: false, status: null };
}
const reserve = db.transaction(() => {
const check = db.transaction(() => {
purgeOldAIAutoCommentRateLimitEvents();
const currentStatus = buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, new Date());
if (!currentStatus || currentStatus.blocked) {
@@ -1318,20 +1318,32 @@ function reserveAIAutoCommentAction(profileNumber, settings = getAIAutoCommentRa
status: currentStatus
};
}
});
const nowIso = new Date().toISOString();
return check();
}
function recordAIAutoCommentAction(profileNumber, settings = getAIAutoCommentRateLimitSettings(), occurredAt = new Date()) {
const normalizedProfileNumber = sanitizeProfileNumber(profileNumber);
if (!normalizedProfileNumber) {
return null;
}
const eventDate = occurredAt instanceof Date && !Number.isNaN(occurredAt.getTime())
? occurredAt
: new Date();
const record = db.transaction(() => {
purgeOldAIAutoCommentRateLimitEvents(eventDate);
db.prepare(`
INSERT INTO ai_auto_comment_rate_limit_events (profile_number, created_at)
VALUES (?, ?)
`).run(normalizedProfileNumber, nowIso);
`).run(normalizedProfileNumber, eventDate.toISOString());
return {
ok: true,
status: buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, new Date())
};
return buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, eventDate);
});
return reserve();
return record();
}
function normalizeCreatorName(value) {
@@ -7940,9 +7952,9 @@ app.post('/api/ai/generate-comment', async (req, res) => {
}
const limitCheckStartedMs = timingStart();
const reservation = reserveAIAutoCommentAction(normalizedProfileNumber, autoCommentRateLimitSettings);
autoCommentRateLimitStatus = reservation.status || null;
if (!reservation.ok && autoCommentRateLimitStatus && autoCommentRateLimitStatus.blocked) {
const availability = checkAIAutoCommentActionAvailability(normalizedProfileNumber, autoCommentRateLimitSettings);
autoCommentRateLimitStatus = availability.status || null;
if (!availability.ok && autoCommentRateLimitStatus && autoCommentRateLimitStatus.blocked) {
timingEnd('profileLimitCheckMs', limitCheckStartedMs);
const blockedUntilText = autoCommentRateLimitStatus.blocked_until
? ` Freigabe ab ${new Date(autoCommentRateLimitStatus.blocked_until).toLocaleString('de-DE')}.`
@@ -8040,6 +8052,14 @@ app.post('/api/ai/generate-comment', async (req, res) => {
rateLimitResetAt: rateInfo?.rateLimitResetAt ?? null
});
if (traceSource === AI_AUTO_COMMENT_SOURCE && normalizedProfileNumber && autoCommentRateLimitSettings.enabled) {
autoCommentRateLimitStatus = recordAIAutoCommentAction(
normalizedProfileNumber,
autoCommentRateLimitSettings,
new Date()
);
}
backendTimings.credentialLoopMs = roundTiming(Date.now() - credentialLoopStartedMs);
backendTimings.totalMs = roundTiming(Date.now() - requestStartedMs);