botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Feature Request] Use Task-based Asynchronous Pattern (TAP) consistently across SDK especially PromptDialog

Open LTsLlama opened this issue 9 years ago • 10 comments

PromptDialog use asynchronous callback pattern instead of TAP (async and await). This complicates logic implementation and is inconsistent with the rest of the SDK.

LTsLlama avatar Mar 22 '17 00:03 LTsLlama

@DL-LiveTiles are you referring to the C# or Node SDK?

nwhitmont avatar Apr 10 '17 17:04 nwhitmont

@nwhitmont C#

LTsLlama avatar Apr 10 '17 23:04 LTsLlama

Hi @DL-LiveTiles,

The async methods in PromptDialog.cs seem to me to be consistent with the rest of the SDK. Would please elaborate on what you mean exactly?

Thanks.

EricDahlvang avatar Apr 21 '17 17:04 EricDahlvang

Hi @EricDahlvang,

Implementing a separate call-back method for each prompt makes the bot code messy and long. Currently the code would be: ... PromptDialog.Text(context, TextProvided, "Enter text value."); } async Task TextProvided(IDialogContext context, IAwaitable<string> argument) { string value = await argument; }

I would simply like to do: string value = await PromptDialog.Text(context, "Enter text value.");

This would make the bot code much easier to follow and simpler to implement. I believe with some changes to the SDK this would be possible using TaskCompletionSource

Similar to what is suggested here: Best way to convert callback-based async method to awaitable task.

Regards,

Douw

LTsLlama avatar Apr 23 '17 09:04 LTsLlama

Thanks for your suggestion. I agree, it would be more simple for devs to be able use async/await. I'm not knowledgeable regarding what the implications would be if we made this change. I've discussed it with the engineers and:

async/await is for the “task augmented value”, not the “wait for user input augmented value”.

Nonetheless, if you feel up to it, you can make the change and submit a pull request. Microsoft.BotBuilder is open source.

Again, thanks for your suggestion.

EricDahlvang avatar May 02 '17 17:05 EricDahlvang

@DL-LiveTiles I tried to do like you say but message doesn't send Did you have success with it?

var text = await PromptDialogExt.TextAsync(context, "Enter some text");
......

public static class PromptDialogExt
    {
        public static Task<string> TextAsync(IDialogContext context, string text)
        {
            var tcs = new TaskCompletionSource<string>();
            PromptDialog.Text(context, async (c, r) =>
            {
                try
                {
                    var result = await r;
                    tcs.TrySetResult(result);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }
            , text);
            return tcs.Task;
        }
    }

ma3yta avatar Aug 10 '17 12:08 ma3yta

@willportnoy Could you please check if async dialog prompts can be achieved with current codebase? Thanks.

ma3yta avatar Aug 10 '17 12:08 ma3yta

@Ma3yTa that code won't work because the closure is capturing the TaskCompletionSource and that TaskCompletionSource won't useful be persisted until the next turn of conversation (e.g. which could run on another machine entirely).

willportnoy avatar Aug 10 '17 16:08 willportnoy

@willportnoy Thanks for your answer. Does it mean that Task-based Asynchronous Pattern can't be implemented for PromptDialog methods?

I think it can be possible if IDialogStack.Call method return IAwaitable Is it possible to add new method CallAsync with signature IWaitable<R> CallAsync<R>(IDialog<R> child) ?

ma3yta avatar Aug 10 '17 17:08 ma3yta

@Ma3yTa the difficulty is that when the next message in the conversation is sent to the bot, that code that is [efficiently, asynchronously] waiting on the next message may not be the machine that receives the message because it is behind a load balancer.

willportnoy avatar Aug 11 '17 21:08 willportnoy