/** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import { TurnContext, TurnContextStateCollection } from '@microsoft/agents-hosting'; import { Dialog } from './dialog'; import { DialogSet } from './dialogSet'; import { PromptOptions } from './prompts'; import { DialogStateManager } from './memory'; import { DialogManager } from './dialogManager'; import { DialogInstance } from './dialogInstance'; import { DialogTurnResult } from './dialogTurnResult'; import { Choice } from './choices'; import { Activity } from '@microsoft/agents-activity'; /** * Contains dialog state, information about the state of the dialog stack, for a specific {@link DialogSet}. * * @remarks * State is read from and saved to storage each turn, and state cache for the turn is managed through the {@link TurnContext}. * * For more information, see the articles on * [Managing state](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-state) and * [Dialogs library](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog). */ export interface DialogState { /** * Contains state information for each {@link Dialog} on the stack. */ dialogStack: DialogInstance[]; } /** * The context for the current dialog turn with respect to a specific {@link DialogSet}. * * @remarks * This includes the turn context, information about the dialog set, and the state of the dialog stack. * * From code outside of a dialog in the set, use {@link DialogSet.createContext | DialogSet.createContext method} * to create the dialog context. Then use the methods of the dialog context to manage the progression of dialogs in the set. * * When you implement a dialog, the dialog context is a parameter available to the various methods you override or implement. * */ export declare class DialogContext { /** * Creates an new instance of the DialogContext class. * * @param dialogs The `DialogSet` for which to create the dialog context. * @param contextOrDialogContext The `TurnContext` object for the current turn. * @param state The state object to use to read and write `DialogState` to storage. * * @remarks * Passing in a `DialogContext` instance will clone the dialog context. * */ constructor(dialogs: DialogSet, contextOrDialogContext: TurnContext, state: DialogState); /** * Creates an new instance of the DialogContext class. * * @param dialogs The `DialogSet` for which to create the dialog context. * @param contextOrDialogContext The `DialogContext` object for the current turn. * @param state The state object to use to read and write `DialogState` to storage. * * @remarks * Passing in a `DialogContext` instance will clone the dialog context. * */ constructor(dialogs: DialogSet, contextOrDialogContext: DialogContext, state: DialogState); /** * Gets the dialogs that can be called directly from this context. */ dialogs: DialogSet; /** * Gets the context object for the turn. */ context: TurnContext; /** * Gets the current dialog stack. */ stack: DialogInstance[]; /** * The parent dialog context for this dialog context, or `undefined` if this context doesn't have a parent. * * @remarks * When it attempts to start a dialog, the dialog context searches for the {@link Dialog.id} * in its {@link DialogContext.dialogs}. If the dialog to start is not found * in this dialog context, it searches in its parent dialog context, and so on. * */ parent: DialogContext | undefined; /** * Gets the dialog context for the child if the active dialog is a container. * * @returns Dialog context for child if the active dialog is a container. */ get child(): DialogContext | undefined; /** * Gets the state information for the dialog on the top of the dialog stack, or `undefined` if * the stack is empty. * * @returns The state information for the dialog on the top of the dialog stack, or `undefined` if * the stack is empty. * */ get activeDialog(): DialogInstance | undefined; /** * Gets the {@link DialogStateManager} which manages view of all memory scopes. */ state: DialogStateManager; /** * Gets the services collection which is contextual to this dialog context. */ services: TurnContextStateCollection; /** * Gets the current dialog manager instance. * * @returns The current dialog manager instance. */ get dialogManager(): DialogManager; /** * Obtain the CultureInfo in DialogContext. * * @returns a locale string. */ getLocale(): string; /** * Starts a dialog instance and pushes it onto the dialog stack. * Creates a new instance of the dialog and pushes it onto the stack. * * @param dialogId ID of the dialog to start. * @param options Optional. Arguments to pass into the dialog when it starts. * @returns {Promise} a promise resolving to the dialog turn result. * * @remarks * If there's already an active dialog on the stack, that dialog will be paused until * it is again the top dialog on the stack. * * The {@link DialogTurnResult.status} of returned object describes * the status of the dialog stack after this method completes. * * This method throws an exception if the requested dialog can't be found in this dialog context * or any of its ancestors. * */ beginDialog(dialogId: string, options?: object): Promise; /** * Cancels all dialogs on the dialog stack, and clears stack. * * @param cancelParents Optional. If `true` all parent dialogs will be cancelled as well. * @param eventName Optional. Name of a custom event to raise as dialogs are cancelled. This defaults to DialogEvents.cancelDialog. * @param eventValue Optional. Value to pass along with custom cancellation event. * @returns {Promise} a promise resolving to the dialog turn result. * * @remarks * This calls each dialog's {@link Dialog.endDialog | endDialog method} before * removing the dialog from the stack. * * If there were any dialogs on the stack initially, the DialogTurnResult.status * of the return value is {@link DialogTurnStatus.cancelled}; otherwise, it's * {@link DialogTurnStatus.empty}. * */ cancelAllDialogs(cancelParents?: boolean, eventName?: string, eventValue?: any): Promise; /** * Searches for a dialog with a given ID. * * @param dialogId ID of the dialog to search for. * @returns The dialog for the provided ID. * * @remarks * If the dialog to start is not found in the {@link DialogSet} associated * with this dialog context, it attempts to find the dialog in its parent dialog context. * */ findDialog(dialogId: string): Dialog | undefined; /** * Helper function to simplify formatting the options for calling a prompt dialog. * * @param dialogId ID of the prompt dialog to start. * @param promptOrOptions The text of the initial prompt to send the user, * the activity to send as the initial prompt, or * the object with which to format the prompt dialog. * * @remarks * This helper method formats the object to use as the `options` parameter, and then calls * {@link DialogContext.beginDialog} to start the specified prompt dialog. * */ prompt(dialogId: string, promptOrOptions: string | Partial | PromptOptions): Promise; /** * Helper function to simplify formatting the options for calling a prompt dialog. * * @param dialogId ID of the prompt dialog to start. * @param promptOrOptions The text of the initial prompt to send the user, * the {@link Activity} to send as the initial prompt, or * the object with which to format the prompt dialog. * @param choices Optional. Array of choices for the user to choose from, * for use with a {@link ChoicePrompt}. * * @remarks * This helper method formats the object to use as the `options` parameter, and then calls * {@link DialogContext.beginDialog} to start the specified prompt dialog. * */ prompt(dialogId: string, promptOrOptions: string | Partial | PromptOptions, choices: (string | Choice)[]): Promise; /** * Continues execution of the active dialog, if there is one, by passing this dialog context to its * {@link DialogContext.continueDialog} method. * * @returns {Promise} a promise resolving to the dialog turn result. * * @remarks * After the call completes, you can check the turn context's {@link TurnContext.responded} * property to determine if the dialog sent a reply to the user. * * The {@link DialogTurnResult.status} of returned object describes * the status of the dialog stack after this method completes. * * Typically, you would call this from within your agent's turn handler. * */ continueDialog(): Promise; /** * Ends a dialog and pops it off the stack. Returns an optional result to the dialog's parent. * * @param result Optional. A result to pass to the parent logic. This might be the next dialog * on the stack, or if this was the last dialog on the stack, a parent dialog context or * the agent's turn handler. * @returns {Promise} a promise resolving to the dialog turn result. * * @remarks * The _parent_ dialog is the next dialog on the dialog stack, if there is one. This method * calls the parent's {@link Dialog.resumeDialog} method, * passing the result returned by the ending dialog. If there is no parent dialog, the turn ends * and the result is available to the agent through the returned object's * {@link DialogTurnResult.result} property. * * The {@link DialogTurnResult.status} of returned object describes * the status of the dialog stack after this method completes. * * Typically, you would call this from within the logic for a specific dialog to signal back to * the dialog context that the dialog has completed, the dialog should be removed from the stack, * and the parent dialog should resume. * */ endDialog(result?: any): Promise; /** * Ends the active dialog and starts a new dialog in its place. * * @param dialogId ID of the dialog to start. * @param options Optional. Arguments to pass into the new dialog when it starts. * @returns {Promise} a promise resolving to the dialog turn result. * * @remarks * This is particularly useful for creating a loop or redirecting to another dialog. * * The {@link DialogTurnResult.status} of returned object describes * the status of the dialog stack after this method completes. * * This method is similar to ending the current dialog and immediately beginning the new one. * However, the parent dialog is neither resumed nor otherwise notified. * */ replaceDialog(dialogId: string, options?: object): Promise; /** * Requests the active dialog to re-prompt the user for input. * * @remarks * This calls the active dialog's {@link Dialog.repromptDialog} method. * */ repromptDialog(): Promise; /** * Searches for a dialog with a given ID. * * @param name Name of the event to raise. * @param value Optional. Value to send along with the event. * @param bubble Optional. Flag to control whether the event should be bubbled to its parent if not handled locally. Defaults to a value of `true`. * @param fromLeaf Optional. Whether the event is emitted from a leaf node. * @returns `true` if the event was handled. * * @remarks * Emits a named event for the current dialog, or someone who started it, to handle. */ emitEvent(name: string, value?: any, bubble?: boolean, fromLeaf?: boolean): Promise; /** * @private * @param reason * @param result */ private endActiveDialog; }