Count AI cooldown from response time
This commit is contained in:
@@ -1296,13 +1296,13 @@ function listAIAutoCommentRateLimitStatuses(settings = getAIAutoCommentRateLimit
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
function reserveAIAutoCommentAction(profileNumber, settings = getAIAutoCommentRateLimitSettings()) {
|
function checkAIAutoCommentActionAvailability(profileNumber, settings = getAIAutoCommentRateLimitSettings()) {
|
||||||
const normalizedProfileNumber = sanitizeProfileNumber(profileNumber);
|
const normalizedProfileNumber = sanitizeProfileNumber(profileNumber);
|
||||||
if (!normalizedProfileNumber) {
|
if (!normalizedProfileNumber) {
|
||||||
return { ok: false, status: null };
|
return { ok: false, status: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
const reserve = db.transaction(() => {
|
const check = db.transaction(() => {
|
||||||
purgeOldAIAutoCommentRateLimitEvents();
|
purgeOldAIAutoCommentRateLimitEvents();
|
||||||
const currentStatus = buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, new Date());
|
const currentStatus = buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, new Date());
|
||||||
if (!currentStatus || currentStatus.blocked) {
|
if (!currentStatus || currentStatus.blocked) {
|
||||||
@@ -1318,20 +1318,32 @@ function reserveAIAutoCommentAction(profileNumber, settings = getAIAutoCommentRa
|
|||||||
status: currentStatus
|
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(`
|
db.prepare(`
|
||||||
INSERT INTO ai_auto_comment_rate_limit_events (profile_number, created_at)
|
INSERT INTO ai_auto_comment_rate_limit_events (profile_number, created_at)
|
||||||
VALUES (?, ?)
|
VALUES (?, ?)
|
||||||
`).run(normalizedProfileNumber, nowIso);
|
`).run(normalizedProfileNumber, eventDate.toISOString());
|
||||||
|
|
||||||
return {
|
return buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, eventDate);
|
||||||
ok: true,
|
|
||||||
status: buildAIAutoCommentRateLimitStatus(normalizedProfileNumber, settings, new Date())
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return reserve();
|
return record();
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeCreatorName(value) {
|
function normalizeCreatorName(value) {
|
||||||
@@ -7940,9 +7952,9 @@ app.post('/api/ai/generate-comment', async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const limitCheckStartedMs = timingStart();
|
const limitCheckStartedMs = timingStart();
|
||||||
const reservation = reserveAIAutoCommentAction(normalizedProfileNumber, autoCommentRateLimitSettings);
|
const availability = checkAIAutoCommentActionAvailability(normalizedProfileNumber, autoCommentRateLimitSettings);
|
||||||
autoCommentRateLimitStatus = reservation.status || null;
|
autoCommentRateLimitStatus = availability.status || null;
|
||||||
if (!reservation.ok && autoCommentRateLimitStatus && autoCommentRateLimitStatus.blocked) {
|
if (!availability.ok && autoCommentRateLimitStatus && autoCommentRateLimitStatus.blocked) {
|
||||||
timingEnd('profileLimitCheckMs', limitCheckStartedMs);
|
timingEnd('profileLimitCheckMs', limitCheckStartedMs);
|
||||||
const blockedUntilText = autoCommentRateLimitStatus.blocked_until
|
const blockedUntilText = autoCommentRateLimitStatus.blocked_until
|
||||||
? ` Freigabe ab ${new Date(autoCommentRateLimitStatus.blocked_until).toLocaleString('de-DE')}.`
|
? ` 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
|
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.credentialLoopMs = roundTiming(Date.now() - credentialLoopStartedMs);
|
||||||
backendTimings.totalMs = roundTiming(Date.now() - requestStartedMs);
|
backendTimings.totalMs = roundTiming(Date.now() - requestStartedMs);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user