fix: harden bridge session matching and error handling
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+14
-7
@@ -23,7 +23,7 @@ export interface TelegramBridgeOptions {
|
|||||||
interface ChatState {
|
interface ChatState {
|
||||||
chatId: number
|
chatId: number
|
||||||
handle: AgentHandle
|
handle: AgentHandle
|
||||||
sessionId: ReturnType<typeof SessionId>
|
sessionId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SessionLike {
|
interface SessionLike {
|
||||||
@@ -74,7 +74,9 @@ export class TelegramBridge {
|
|||||||
start(): void {
|
start(): void {
|
||||||
this.disposeSessionListener?.()
|
this.disposeSessionListener?.()
|
||||||
this.disposeSessionListener = this.ctx.on('session/event', (session, event) => {
|
this.disposeSessionListener = this.ctx.on('session/event', (session, event) => {
|
||||||
void this.onSessionEvent(session, event)
|
void this.onSessionEvent(session, event).catch((err) => {
|
||||||
|
this.ctx.logger.error(this.redact(err))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
if (!this.polling) {
|
if (!this.polling) {
|
||||||
this.polling = true
|
this.polling = true
|
||||||
@@ -201,8 +203,9 @@ export class TelegramBridge {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private findChatBySessionId(sessionId: ReturnType<typeof SessionId>): ChatState | undefined {
|
private findChatBySessionId(sessionId: ReturnType<typeof SessionId>): ChatState | undefined {
|
||||||
|
const id = String(sessionId)
|
||||||
for (const chat of this.chats.values()) {
|
for (const chat of this.chats.values()) {
|
||||||
if (chat.sessionId === sessionId) return chat
|
if (chat.sessionId === id) return chat
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
@@ -212,8 +215,8 @@ export class TelegramBridge {
|
|||||||
const existing = this.chats.get(key)
|
const existing = this.chats.get(key)
|
||||||
if (existing) return existing
|
if (existing) return existing
|
||||||
|
|
||||||
const sessionId = SessionId(`telegram:${chatId}`)
|
const sessionId = String(SessionId(`telegram:${chatId}`))
|
||||||
const handle = await this.createAgent(sessionId)
|
const handle = await this.createAgent(SessionId(sessionId))
|
||||||
const state: ChatState = { chatId, handle, sessionId }
|
const state: ChatState = { chatId, handle, sessionId }
|
||||||
this.chats.set(key, state)
|
this.chats.set(key, state)
|
||||||
return state
|
return state
|
||||||
@@ -226,8 +229,8 @@ export class TelegramBridge {
|
|||||||
await existing.handle.dispose()
|
await existing.handle.dispose()
|
||||||
this.chats.delete(key)
|
this.chats.delete(key)
|
||||||
}
|
}
|
||||||
const sessionId = SessionId(`telegram:${chatId}:${Date.now()}`)
|
const sessionId = String(SessionId(`telegram:${chatId}:${Date.now()}`))
|
||||||
const handle = await this.createAgent(sessionId)
|
const handle = await this.createAgent(SessionId(sessionId))
|
||||||
this.chats.set(key, { chatId, handle, sessionId })
|
this.chats.set(key, { chatId, handle, sessionId })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +261,11 @@ export class TelegramBridge {
|
|||||||
try {
|
try {
|
||||||
await this.client.sendMessage(chatId, html, 'HTML')
|
await this.client.sendMessage(chatId, html, 'HTML')
|
||||||
} catch {
|
} catch {
|
||||||
|
try {
|
||||||
await this.client.sendMessage(chatId, chunk)
|
await this.client.sendMessage(chatId, chunk)
|
||||||
|
} catch (err) {
|
||||||
|
this.ctx.logger.error(this.redact(err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user