grammyjs / conversations

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

Local counter in loop as side effect #67

Closed Morb0 closed 4 weeks ago

Morb0 commented 1 year ago

The code below doesn't work correctly, most likely it breaks the side effect system, but based on the documentation, this behavior is not obvious. I managed to fix it by wrapping counter in external:counter = await conv.external(counter - 1).

async function counter(conv: Conversation, ctx: Context) {
    await ctx.reply('Please send me a message');
    let counter = 0;
    while (true) {
      conv.log(`Loop begin: Counter: ${counter}`);
      const text = await conv.form.text();
      conv.log(`After wait: Counter: ${counter}`);

      const idx = conv.session.tags.indexOf(text);
      if (idx !== -1) {
        conv.log(`Remove "${text}" at ${idx} index`);
        conv.session.tags.splice(idx, 1);
        counter--;
      } else {
        conv.log(`Add "${text}"`);
        conv.session.tags.push(text);
        counter++;
      }
      await ctx.reply(`Counter: ${counter}`);

      if (counter === 3) {
        await ctx.reply('Done');
        return;
      }

      conv.log(`Loop end: Counter: ${counter}`);
    }
  }

Logs:

Loop begin: Counter: 0
After wait: Counter: 0
Add "1"
Loop end: Counter: 1

Loop begin: Counter: 1
After wait: Counter: 1
Remove "1" at 0 index
Loop end: Counter: 0

Loop begin: Counter: 0
After wait: Counter: -2 <--- Unexpected after wait
Add "1"
Loop end: Counter: -1

Loop begin: Counter: -1
[Stuck, no more interactions work]
KnorpelSenf commented 1 year ago

This seems to be a bug, most likely with conversation.session. This needs to be investigated