fix(telegram-bot): OOM when handling loop (#106)
Co-authored-by: 藍+85CD <50108258+kwaa@users.noreply.github.com> Co-authored-by: Chino Moka <31543482+QiroNT@users.noreply.github.com>
This commit is contained in:
co-authored by
藍+85CD
Chino Moka
parent
f47d794b8e
commit
a9207a7364
@@ -22,7 +22,7 @@ async function isChatIdBotAdmin(chatId: number) {
|
||||
return admins.includes(chatId.toString())
|
||||
}
|
||||
|
||||
async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) {
|
||||
async function handleLoopStep(state: BotSelf, msgs?: LLMMessage[], chatId?: string): Promise<() => Promise<any> | undefined> {
|
||||
// Set the start time when beginning new processing
|
||||
state.currentProcessingStartTime = Date.now()
|
||||
|
||||
@@ -96,7 +96,7 @@ async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string)
|
||||
|
||||
if (shouldInterrupt) {
|
||||
state.logger.log(`Interrupting message processing for chat ${action.chatId} - new messages deemed more important`)
|
||||
return handleLoop(state)
|
||||
return () => handleLoopStep(state)
|
||||
}
|
||||
else {
|
||||
state.logger.log(`Continuing current processing despite new messages in chat ${action.chatId}`)
|
||||
@@ -123,18 +123,16 @@ async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string)
|
||||
// }
|
||||
|
||||
await readMessage(state, state.bot.botInfo.id.toString(), chatId, action, unreadMessagesForThisChat, currentController)
|
||||
break
|
||||
return
|
||||
case 'listChats':
|
||||
msgs.push(message.user(`List of chats:${(await listJoinedChats()).map(chat => `ID:${chat.chat_id}, Name:${chat.chat_name}`).join('\n')}`))
|
||||
await handleLoop(state, msgs, chatId)
|
||||
break
|
||||
return () => handleLoopStep(state, msgs, chatId)
|
||||
case 'sendMessage':
|
||||
await sendMayStructuredMessage(state, action.content, action.groupId)
|
||||
break
|
||||
return
|
||||
default:
|
||||
msgs.push(message.user(`The action you sent ${action.action} haven't implemented yet by developer.`))
|
||||
await handleLoop(state, msgs, chatId)
|
||||
break
|
||||
return () => handleLoopStep(state, msgs, chatId)
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
@@ -154,6 +152,16 @@ async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string)
|
||||
}
|
||||
}
|
||||
|
||||
async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) {
|
||||
let result = await handleLoopStep(state, msgs, chatId)
|
||||
|
||||
while (typeof result === 'function') {
|
||||
result = await result()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function loop(state: BotSelf) {
|
||||
setTimeout(() => {
|
||||
handleLoop(state)
|
||||
|
||||
@@ -10,7 +10,7 @@ import { message } from '@xsai/utils-chat'
|
||||
import { findLastNMessages, findRelevantMessages } from '../../../models'
|
||||
import { recordChatCompletions } from '../../../models/chat-completions-history'
|
||||
import { chatMessageToOneLine, telegramMessageToOneLine } from '../../../models/common'
|
||||
import { systemPrompt } from '../../../prompts/system-v1'
|
||||
import { personality } from '../../../prompts/system-v1'
|
||||
import { sendMayStructuredMessage } from '../utils/message'
|
||||
|
||||
export async function readMessage(
|
||||
@@ -57,20 +57,23 @@ export async function readMessage(
|
||||
state.unreadMessages[action.chatId] = []
|
||||
|
||||
const messages = message.messages(
|
||||
systemPrompt(),
|
||||
personality(),
|
||||
message.user(''
|
||||
+ `Currently, it\'s ${new Date()} on the server that hosts you.`
|
||||
+ 'The others in the group may live in a different timezone, so please be aware of the time difference.'
|
||||
+ '\n'
|
||||
+ 'Last 30 messages:\n'
|
||||
+ `${lastNMessagesOneliner || 'No messages'}`
|
||||
+ 'You choose to read the messages from the group (perhaps you are already engaging the topics in the group).'
|
||||
+ 'Imaging you are using Telegram app on the mobile phone, and you are reading the messages from the group chat.'
|
||||
+ '\n'
|
||||
+ 'I helped you searched these relevant chat messages may help you recall the memories:\n'
|
||||
+ `${relevantChatMessagesOneliner || 'No relevant messages'}`
|
||||
+ 'Previous 30 messages (including what you said):\n'
|
||||
+ `${lastNMessagesOneliner || 'No messages'}`
|
||||
+ '\n'
|
||||
+ 'All the messages you requested to read:\n'
|
||||
+ `${unreadHistoryMessageOneliner || 'No messages'}`
|
||||
+ '\n'
|
||||
+ 'Relevant chat messages may help you recall the memories:\n'
|
||||
+ `${relevantChatMessagesOneliner || 'No relevant messages'}`
|
||||
+ '\n'
|
||||
+ 'Based on your personalities, imaging you have your own choice and interest over different topics, '
|
||||
+ 'giving the above context and chat history, would you like to participate in the conversation '
|
||||
+ 'about the topic? Or will you aggressively diss or piss off about the opinions of others?\n'
|
||||
|
||||
@@ -9,7 +9,7 @@ import { message } from '@xsai/utils-chat'
|
||||
import { parse } from 'best-effort-json-parser'
|
||||
|
||||
import { recordChatCompletions } from '../models/chat-completions-history'
|
||||
import { systemPrompt } from '../prompts/system-v1'
|
||||
import { personality } from '../prompts/system-v1'
|
||||
|
||||
export async function imagineAnAction(
|
||||
_botId: string,
|
||||
@@ -26,7 +26,7 @@ export async function imagineAnAction(
|
||||
|
||||
agentMessages.push(
|
||||
message.system(''
|
||||
+ `${systemPrompt().content}`
|
||||
+ `${personality().content}`
|
||||
+ '\n'
|
||||
+ 'I am one of your system component, called Ticking system, which is responsible to keep track of the time, and '
|
||||
+ 'help you schedule, retain focus, and keep eyes on different tasks, and ideas you have.'
|
||||
|
||||
Reference in New Issue
Block a user