cleanup, logging of ratelimit

This commit is contained in:
Aidan 2025-06-28 15:19:51 -04:00
parent a4233c5cff
commit 276d053ed5

View file

@ -45,54 +45,21 @@ class RateLimiter {
return `${chatId}:${messageId}` return `${chatId}:${messageId}`
} }
private async waitForRateLimit(): Promise<void> { private async waitForRateLimit(chatId: number, messageId: number): Promise<void> {
if (this.isRateLimited) { if (!this.isRateLimited) return
console.log(`[✨ AI | RATELIMIT] [${chatId}:${messageId}] Ratelimited, waiting for end of ${this.rateLimitEndTime - Date.now()}ms`)
const now = Date.now() const now = Date.now()
if (now < this.rateLimitEndTime) { if (now < this.rateLimitEndTime) {
const waitTime = this.rateLimitEndTime - now await new Promise(resolve => setTimeout(resolve, this.rateLimitEndTime - now))
await new Promise(resolve => setTimeout(resolve, waitTime))
} }
this.isRateLimited = false this.isRateLimited = false
} }
}
private async processUpdate( private chunkText(text: string): string[] {
ctx: Context,
chatId: number,
messageId: number,
options: any
): Promise<void> {
const messageKey = this.getMessageKey(chatId, messageId)
const latestText = this.pendingUpdates.get(messageKey)
if (!latestText) return
const now = Date.now()
const timeSinceLastEdit = now - this.lastEditTime
await this.waitForRateLimit()
if (timeSinceLastEdit < this.minInterval) {
const existingTimeout = this.updateQueue.get(messageKey)
if (existingTimeout) {
clearTimeout(existingTimeout)
}
const timeout = setTimeout(() => {
this.processUpdate(ctx, chatId, messageId, options)
}, this.minInterval - timeSinceLastEdit)
this.updateQueue.set(messageKey, timeout)
return
}
try {
if (latestText.length > this.max_msg_length) {
const chunks: string[] = [] const chunks: string[] = []
let currentChunk = '' let currentChunk = ''
let currentLength = 0 let currentLength = 0
const lines = text.split('\n')
// Split text into chunks while preserving markdown formatting
const lines = latestText.split('\n')
for (const line of lines) { for (const line of lines) {
if (currentLength + line.length + 1 > this.max_msg_length) { if (currentLength + line.length + 1 > this.max_msg_length) {
if (currentChunk) { if (currentChunk) {
@ -100,7 +67,6 @@ class RateLimiter {
currentChunk = '' currentChunk = ''
currentLength = 0 currentLength = 0
} }
// if a single line is too long, split
if (line.length > this.max_msg_length) { if (line.length > this.max_msg_length) {
for (let i = 0; i < line.length; i += this.max_msg_length) { for (let i = 0; i < line.length; i += this.max_msg_length) {
chunks.push(line.substring(i, i + this.max_msg_length)) chunks.push(line.substring(i, i + this.max_msg_length))
@ -121,28 +87,96 @@ class RateLimiter {
if (currentChunk) { if (currentChunk) {
chunks.push(currentChunk) chunks.push(currentChunk)
} }
return chunks
}
const firstChunk = chunks[0] private handleTelegramError(error: unknown, messageKey: string, options: any, ctx: Context, chatId: number, messageId: number): boolean {
logger.logChunk(chatId, messageId, firstChunk) if (!isTelegramError(error)) return false
if (error.response.error_code === 429) {
const retryAfter = error.response.parameters?.retry_after || 1
this.isRateLimited = true
this.rateLimitEndTime = Date.now() + (retryAfter * 1000)
const existingTimeout = this.updateQueue.get(messageKey)
if (existingTimeout) clearTimeout(existingTimeout)
const timeout = setTimeout(() => {
this.processUpdate(ctx, chatId, messageId, options)
}, retryAfter * 1000)
this.updateQueue.set(messageKey, timeout)
return true
}
if (error.response.error_code === 400) {
if (error.response.description?.includes("can't parse entities") || error.response.description?.includes("MESSAGE_TOO_LONG")) {
const plainOptions = { ...options, parse_mode: undefined }
this.processUpdate(ctx, chatId, messageId, plainOptions)
return true
}
if (error.response.description?.includes("message is not modified")) {
this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey)
return true
}
logger.logError(error)
this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey)
return true
}
logger.logError(error)
this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey)
return true
}
private async processUpdate(
ctx: Context,
chatId: number,
messageId: number,
options: any
): Promise<void> {
const messageKey = this.getMessageKey(chatId, messageId)
const latestText = this.pendingUpdates.get(messageKey)
if (!latestText) return
const now = Date.now()
const timeSinceLastEdit = now - this.lastEditTime
await this.waitForRateLimit(chatId, messageId)
if (timeSinceLastEdit < this.minInterval) {
const existingTimeout = this.updateQueue.get(messageKey)
if (existingTimeout) clearTimeout(existingTimeout)
const timeout = setTimeout(() => {
this.processUpdate(ctx, chatId, messageId, options)
}, this.minInterval - timeSinceLastEdit)
this.updateQueue.set(messageKey, timeout)
return
}
try {
if (latestText.length > this.max_msg_length) {
const chunks = this.chunkText(latestText)
const firstChunk = chunks[0]
logger.logChunk(chatId, messageId, firstChunk)
try { try {
await ctx.telegram.editMessageText(chatId, messageId, undefined, firstChunk, options) await ctx.telegram.editMessageText(chatId, messageId, undefined, firstChunk, options)
} catch (error: any) { } catch (error: unknown) {
if (!error.response?.description?.includes("message is not modified")) { if (
isTelegramError(error) &&
!error.response.description?.includes("message is not modified")
) {
throw error throw error
} }
} }
for (let i = 1; i < chunks.length; i++) { for (let i = 1; i < chunks.length; i++) {
const chunk = chunks[i] const chunk = chunks[i]
const overflowMessageId = this.overflowMessages.get(messageKey) const overflowMessageId = this.overflowMessages.get(messageKey)
if (overflowMessageId) { if (overflowMessageId) {
try { try {
await ctx.telegram.editMessageText(chatId, overflowMessageId, undefined, chunk, options) await ctx.telegram.editMessageText(chatId, overflowMessageId, undefined, chunk, options)
logger.logChunk(chatId, overflowMessageId, chunk, true) logger.logChunk(chatId, overflowMessageId, chunk, true)
} catch (error: any) { } catch (error: unknown) {
if (!error.response?.description?.includes("message is not modified")) { if (
isTelegramError(error) &&
!error.response.description?.includes("message is not modified")
) {
throw error throw error
} }
} }
@ -155,7 +189,6 @@ class RateLimiter {
this.overflowMessages.set(messageKey, newMessage.message_id) this.overflowMessages.set(messageKey, newMessage.message_id)
} }
} }
this.pendingUpdates.set(messageKey, firstChunk) this.pendingUpdates.set(messageKey, firstChunk)
if (chunks.length > 1) { if (chunks.length > 1) {
this.pendingUpdates.set( this.pendingUpdates.set(
@ -164,54 +197,23 @@ class RateLimiter {
) )
} }
} else { } else {
const sanitizedText = latestText logger.logChunk(chatId, messageId, latestText)
logger.logChunk(chatId, messageId, sanitizedText)
try { try {
await ctx.telegram.editMessageText(chatId, messageId, undefined, sanitizedText, options) await ctx.telegram.editMessageText(chatId, messageId, undefined, latestText, options)
} catch (error: any) { } catch (error: unknown) {
if (!error.response?.description?.includes("message is not modified")) { if (
isTelegramError(error) &&
!error.response.description?.includes("message is not modified")
) {
throw error throw error
} }
} }
this.pendingUpdates.delete(messageKey) this.pendingUpdates.delete(messageKey)
} }
this.lastEditTime = Date.now() this.lastEditTime = Date.now()
this.updateQueue.delete(messageKey) this.updateQueue.delete(messageKey)
} catch (error: any) { } catch (error: unknown) {
if (error.response?.error_code === 429) { if (!this.handleTelegramError(error, messageKey, options, ctx, chatId, messageId)) {
const retryAfter = error.response.parameters?.retry_after || 1
this.isRateLimited = true
this.rateLimitEndTime = Date.now() + (retryAfter * 1000)
const existingTimeout = this.updateQueue.get(messageKey)
if (existingTimeout) {
clearTimeout(existingTimeout)
}
const timeout = setTimeout(() => {
this.processUpdate(ctx, chatId, messageId, options)
}, retryAfter * 1000)
this.updateQueue.set(messageKey, timeout)
} else if (error.response?.error_code === 400) {
if (error.response?.description?.includes("can't parse entities")) {
// try again with plain text
const plainOptions = { ...options, parse_mode: undefined }
await this.processUpdate(ctx, chatId, messageId, plainOptions)
} else if (error.response?.description?.includes("MESSAGE_TOO_LONG")) {
const plainOptions = { ...options, parse_mode: undefined }
await this.processUpdate(ctx, chatId, messageId, plainOptions)
} else if (error.response?.description?.includes("message is not modified")) {
this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey)
} else {
logger.logError(error)
this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey)
}
} else {
logger.logError(error) logger.logError(error)
this.pendingUpdates.delete(messageKey) this.pendingUpdates.delete(messageKey)
this.updateQueue.delete(messageKey) this.updateQueue.delete(messageKey)
@ -233,3 +235,12 @@ class RateLimiter {
} }
export const rateLimiter = new RateLimiter() export const rateLimiter = new RateLimiter()
function isTelegramError(error: unknown): error is { response: { description?: string, error_code?: number, parameters?: { retry_after?: number } } } {
return (
typeof error === "object" &&
error !== null &&
"response" in error &&
typeof (error as any).response === "object"
)
}