grammyjs / conversations

Conversational interfaces for grammY.
https://grammy.dev/plugins/conversations
MIT License
48 stars 17 forks source link

Infinitely increasing amount of data in session during a loop in conversation #100

Closed mozharov closed 6 months ago

mozharov commented 6 months ago

Node.js v20.11.0 grammyjs v1.21.1 @grammyjs/conversations v1.2.0 typescript v5.4.2

I used the example code with loops from the plugin documentation, and found that with each loop the data in the session increases in volume. It turns out that if the user calls for a long time actions that do not lead to the completion of the cycle, then the session can increase to unacceptable sizes.

export const bot = new Bot<Context>(botToken)

const composer = new Composer<Context>()

composer.use(sessions)
composer.use(conversations)

composer.use(createConversation(test))
composer.command('test').use(ctx => ctx.conversation.enter('test', {overwrite: true}))

async function test(conversation: Conversation, ctx: Context): Promise<void> {
  do {
    await ctx.reply('Send me a photo!')
    ctx = await conversation.wait()

    if (ctx.message?.text === '/cancel') {
      await ctx.reply('Cancelled, leaving!')
      return
    }
  } while (!ctx.message?.photo)
}

bot.errorBoundary(errorsHandler).use(composer)

This is what a session looks like in the database:

{"conversation": [["", 1, 2], "test", [3], ["", 4, 5, 274, 275], "log", ["", 6, 7], "u", [8, 74, 93, 112, 130, 148, 165, 184, 203, 221, 239, 257], ["", 6, 9, 48, 49, 54, -1, 55, 56], ["", 10, 11, 12, 13], "update_id", 381016006, "message", ["", 14, 15, 16, 17, 32, 33, 36, 37, 38, 39, 40, 41], "message_id", 6531, "from", ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], "id", 1033924450, "is_bot", false, "first_name", "Test", "last_name", "Test", "username", "test", "language_code", "en", "is_premium", false, "chat", ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], "type", "private", "date", 1711629928, "text", "/help", "entities", [42], ["", 43, 44, 45, 46, 34, 47], "offset", 0, "length", 5, "bot_command", "x", ["", 50, 51, 52, 53], "session", [""], "match", "", "f", "a", ["", 57, 58], "sendMessage", [59], ["", 60, 44, 61, 62], "i", "r", ["", 63, 31, 64, 65], "ok", "result", ["", 14, 66, 16, 67, 32, 71, 36, 72, 38, 73], 6532, ["", 18, 68, 20, 31, 22, 69, 26, 70], 5560197707, "Test_local_bot", "Test_local_robot", ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629929, "Send me a photo!", ["", 6, 75, 48, 83, 54, -1, 55, 85], ["", 10, 76, 12, 77], 381016007, ["", 14, 78, 16, 79, 32, 80, 36, 81, 38, 82], 6533, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629936, "asd", ["", 50, 84], [""], ["", 57, 86], [87], ["", 60, 44, 61, 88], ["", 63, 31, 64, 89], ["", 14, 90, 16, 91, 32, 92, 36, 81, 38, 73], 6534, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 94, 48, 102, 54, -1, 55, 104], ["", 10, 95, 12, 96], 381016008, ["", 14, 97, 16, 98, 32, 99, 36, 100, 38, 101], 6535, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629945, "sd", ["", 50, 103], [""], ["", 57, 105], [106], ["", 60, 44, 61, 107], ["", 63, 31, 64, 108], ["", 14, 109, 16, 110, 32, 111, 36, 100, 38, 73], 6536, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 113, 48, 120, 54, -1, 55, 122], ["", 10, 114, 12, 115], 381016009, ["", 14, 116, 16, 117, 32, 118, 36, 119, 38, 101], 6537, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629946, ["", 50, 121], [""], ["", 57, 123], [124], ["", 60, 44, 61, 125], ["", 63, 31, 64, 126], ["", 14, 127, 16, 128, 32, 129, 36, 119, 38, 73], 6538, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 131, 48, 137, 54, -1, 55, 139], ["", 10, 132, 12, 133], 381016010, ["", 14, 134, 16, 135, 32, 136, 36, 119, 38, 101], 6539, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 50, 138], [""], ["", 57, 140], [141], ["", 60, 44, 61, 142], ["", 63, 31, 64, 143], ["", 14, 144, 16, 145, 32, 146, 36, 147, 38, 73], 6540, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629947, ["", 6, 149, 48, 155, 54, -1, 55, 157], ["", 10, 150, 12, 151], 381016011, ["", 14, 152, 16, 153, 32, 154, 36, 147, 38, 101], 6541, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 50, 156], [""], ["", 57, 158], [159], ["", 60, 44, 61, 160], ["", 63, 31, 64, 161], ["", 14, 162, 16, 163, 32, 164, 36, 147, 38, 73], 6542, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 166, 48, 174, 54, -1, 55, 176], ["", 10, 167, 12, 168], 381016012, ["", 14, 169, 16, 170, 32, 171, 36, 172, 38, 173], 6543, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629960, "dsfsad", ["", 50, 175], [""], ["", 57, 177], [178], ["", 60, 44, 61, 179], ["", 63, 31, 64, 180], ["", 14, 181, 16, 182, 32, 183, 36, 172, 38, 73], 6544, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 185, 48, 193, 54, -1, 55, 195], ["", 10, 186, 12, 187], 381016013, ["", 14, 188, 16, 189, 32, 190, 36, 191, 38, 192], 6545, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629961, "fsadf", ["", 50, 194], [""], ["", 57, 196], [197], ["", 60, 44, 61, 198], ["", 63, 31, 64, 199], ["", 14, 200, 16, 201, 32, 202, 36, 191, 38, 73], 6546, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 204, 48, 211, 54, -1, 55, 213], ["", 10, 205, 12, 206], 381016014, ["", 14, 207, 16, 208, 32, 209, 36, 191, 38, 210], 6547, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], "asdas", ["", 50, 212], [""], ["", 57, 214], [215], ["", 60, 44, 61, 216], ["", 63, 31, 64, 217], ["", 14, 218, 16, 219, 32, 220, 36, 191, 38, 73], 6549, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 222, 48, 229, 54, -1, 55, 231], ["", 10, 223, 12, 224], 381016015, ["", 14, 225, 16, 226, 32, 227, 36, 191, 38, 228], 6548, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], "das", ["", 50, 230], [""], ["", 57, 232], [233], ["", 60, 44, 61, 234], ["", 63, 31, 64, 235], ["", 14, 236, 16, 237, 32, 238, 36, 191, 38, 73], 6551, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 6, 240, 48, 246, 54, -1, 55, 248], ["", 10, 241, 12, 242], 381016016, ["", 14, 243, 16, 244, 32, 245, 36, 191, 38, 228], 6550, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 50, 247], [""], ["", 57, 249], [250], ["", 60, 44, 61, 251], ["", 63, 31, 64, 252], ["", 14, 253, 16, 254, 32, 255, 36, 256, 38, 73], 6553, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], 1711629962, ["", 6, 258, 48, 264, 54, -1, 55, 266], ["", 10, 259, 12, 260], 381016017, ["", 14, 261, 16, 262, 32, 263, 36, 256, 38, 228], 6552, ["", 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], ["", 50, 265], [""], ["", 57, 267], [268], ["", 60, 44, 61, 269], ["", 63, 31, 64, 270], ["", 14, 271, 16, 272, 32, 273, 36, 256, 38, 73], 6554, ["", 18, 68, 20, 31, 22, 69, 26, 70], ["", 18, 19, 22, 23, 24, 25, 26, 27, 34, 35], "last", 1711629929128]}

KnorpelSenf commented 6 months ago

That is why it makes sense to finish the conversation after it is done. It is not a good idea to let it run indefinitely.

mozharov commented 6 months ago

That is why it makes sense to finish the conversation after it is done. It is not a good idea to let it run indefinitely.

Perhaps then the plugin author should mention this in the plugin documentation. It seems to me that many may not be aware of this problem.

KnorpelSenf commented 6 months ago

It is documented that all received update objects are stored in the session. This implies that the more update objects you handle, the more data needs to be stored.