@microsoft/agents-hosting-dialogs 0.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (325) hide show
  1. package/dist/index.js +20 -0
  2. package/dist/src/agentComponent.d.ts +18 -0
  3. package/dist/src/agentComponent.js +55 -0
  4. package/dist/src/agentComponent.js.map +1 -0
  5. package/dist/src/agentStateSet.d.ts +48 -0
  6. package/dist/src/agentStateSet.js +66 -0
  7. package/dist/src/agentStateSet.js.map +1 -0
  8. package/dist/src/choices/choice.d.ts +14 -0
  9. package/dist/src/choices/choice.js +7 -0
  10. package/dist/src/choices/choice.js.map +1 -0
  11. package/dist/src/choices/choiceFactory.d.ts +81 -0
  12. package/dist/src/choices/choiceFactory.js +133 -0
  13. package/dist/src/choices/choiceFactory.js.map +1 -0
  14. package/dist/src/choices/findChoices.d.ts +28 -0
  15. package/dist/src/choices/findChoices.js +46 -0
  16. package/dist/src/choices/findChoices.js.map +1 -0
  17. package/dist/src/choices/findValues.d.ts +36 -0
  18. package/dist/src/choices/findValues.js +129 -0
  19. package/dist/src/choices/findValues.js.map +1 -0
  20. package/dist/src/choices/index.d.ts +11 -0
  21. package/dist/src/choices/index.js +28 -0
  22. package/dist/src/choices/index.js.map +1 -0
  23. package/dist/src/choices/modelResult.d.ts +11 -0
  24. package/dist/src/choices/modelResult.js +3 -0
  25. package/dist/src/choices/modelResult.js.map +1 -0
  26. package/dist/src/choices/recognizeChoices.d.ts +16 -0
  27. package/dist/src/choices/recognizeChoices.js +81 -0
  28. package/dist/src/choices/recognizeChoices.js.map +1 -0
  29. package/dist/src/choices/tokenizer.d.ts +25 -0
  30. package/dist/src/choices/tokenizer.js +73 -0
  31. package/dist/src/choices/tokenizer.js.map +1 -0
  32. package/dist/src/componentDialog.d.ts +178 -0
  33. package/dist/src/componentDialog.js +252 -0
  34. package/dist/src/componentDialog.js.map +1 -0
  35. package/dist/src/componentRegistration.d.ts +22 -0
  36. package/dist/src/componentRegistration.js +32 -0
  37. package/dist/src/componentRegistration.js.map +1 -0
  38. package/dist/src/configurable.d.ts +22 -0
  39. package/dist/src/configurable.js +60 -0
  40. package/dist/src/configurable.js.map +1 -0
  41. package/dist/src/configuration.d.ts +11 -0
  42. package/dist/src/configuration.js +17 -0
  43. package/dist/src/configuration.js.map +1 -0
  44. package/dist/src/converter.d.ts +13 -0
  45. package/dist/src/converter.js +7 -0
  46. package/dist/src/converter.js.map +1 -0
  47. package/dist/src/dialog.d.ts +184 -0
  48. package/dist/src/dialog.js +201 -0
  49. package/dist/src/dialog.js.map +1 -0
  50. package/dist/src/dialogContainer.d.ts +61 -0
  51. package/dist/src/dialogContainer.js +82 -0
  52. package/dist/src/dialogContainer.js.map +1 -0
  53. package/dist/src/dialogContext.d.ts +272 -0
  54. package/dist/src/dialogContext.js +444 -0
  55. package/dist/src/dialogContext.js.map +1 -0
  56. package/dist/src/dialogContextError.d.ts +27 -0
  57. package/dist/src/dialogContextError.js +39 -0
  58. package/dist/src/dialogContextError.js.map +1 -0
  59. package/dist/src/dialogEvent.d.ts +20 -0
  60. package/dist/src/dialogEvent.js +3 -0
  61. package/dist/src/dialogEvent.js.map +1 -0
  62. package/dist/src/dialogEvents.d.ts +15 -0
  63. package/dist/src/dialogEvents.js +20 -0
  64. package/dist/src/dialogEvents.js.map +1 -0
  65. package/dist/src/dialogHelper.d.ts +32 -0
  66. package/dist/src/dialogHelper.js +116 -0
  67. package/dist/src/dialogHelper.js.map +1 -0
  68. package/dist/src/dialogInstance.d.ts +30 -0
  69. package/dist/src/dialogInstance.js +3 -0
  70. package/dist/src/dialogInstance.js.map +1 -0
  71. package/dist/src/dialogManager.d.ts +73 -0
  72. package/dist/src/dialogManager.js +156 -0
  73. package/dist/src/dialogManager.js.map +1 -0
  74. package/dist/src/dialogReason.d.ts +35 -0
  75. package/dist/src/dialogReason.js +40 -0
  76. package/dist/src/dialogReason.js.map +1 -0
  77. package/dist/src/dialogSet.d.ts +85 -0
  78. package/dist/src/dialogSet.js +138 -0
  79. package/dist/src/dialogSet.js.map +1 -0
  80. package/dist/src/dialogTurnResult.d.ts +34 -0
  81. package/dist/src/dialogTurnResult.js +3 -0
  82. package/dist/src/dialogTurnResult.js.map +1 -0
  83. package/dist/src/dialogTurnStateConstants.d.ts +12 -0
  84. package/dist/src/dialogTurnStateConstants.js +17 -0
  85. package/dist/src/dialogTurnStateConstants.js.map +1 -0
  86. package/dist/src/dialogTurnStatus.d.ts +34 -0
  87. package/dist/src/dialogTurnStatus.js +39 -0
  88. package/dist/src/dialogTurnStatus.js.map +1 -0
  89. package/dist/src/dialogsAgentComponent.d.ts +17 -0
  90. package/dist/src/dialogsAgentComponent.js +65 -0
  91. package/dist/src/dialogsAgentComponent.js.map +1 -0
  92. package/dist/src/dialogsComponentRegistration.d.ts +28 -0
  93. package/dist/src/dialogsComponentRegistration.js +45 -0
  94. package/dist/src/dialogsComponentRegistration.js.map +1 -0
  95. package/dist/src/i18n.d.ts +15 -0
  96. package/dist/src/i18n.js +28 -0
  97. package/dist/src/i18n.js.map +1 -0
  98. package/dist/src/index.d.ts +29 -0
  99. package/dist/src/index.js +48 -0
  100. package/dist/src/index.js.map +1 -0
  101. package/dist/src/intentScore.d.ts +11 -0
  102. package/dist/src/intentScore.js +3 -0
  103. package/dist/src/intentScore.js.map +1 -0
  104. package/dist/src/memory/componentMemoryScopes.d.ts +18 -0
  105. package/dist/src/memory/componentMemoryScopes.js +54 -0
  106. package/dist/src/memory/componentMemoryScopes.js.map +1 -0
  107. package/dist/src/memory/componentPathResolvers.d.ts +18 -0
  108. package/dist/src/memory/componentPathResolvers.js +52 -0
  109. package/dist/src/memory/componentPathResolvers.js.map +1 -0
  110. package/dist/src/memory/dialogPath.d.ts +17 -0
  111. package/dist/src/memory/dialogPath.js +30 -0
  112. package/dist/src/memory/dialogPath.js.map +1 -0
  113. package/dist/src/memory/dialogStateManager.d.ts +156 -0
  114. package/dist/src/memory/dialogStateManager.js +598 -0
  115. package/dist/src/memory/dialogStateManager.js.map +1 -0
  116. package/dist/src/memory/index.d.ts +12 -0
  117. package/dist/src/memory/index.js +29 -0
  118. package/dist/src/memory/index.js.map +1 -0
  119. package/dist/src/memory/pathResolvers/aliasPathResolver.d.ts +25 -0
  120. package/dist/src/memory/pathResolvers/aliasPathResolver.js +37 -0
  121. package/dist/src/memory/pathResolvers/aliasPathResolver.js.map +1 -0
  122. package/dist/src/memory/pathResolvers/atAtPathResolver.d.ts +11 -0
  123. package/dist/src/memory/pathResolvers/atAtPathResolver.js +18 -0
  124. package/dist/src/memory/pathResolvers/atAtPathResolver.js.map +1 -0
  125. package/dist/src/memory/pathResolvers/atPathResolver.d.ts +20 -0
  126. package/dist/src/memory/pathResolvers/atPathResolver.js +46 -0
  127. package/dist/src/memory/pathResolvers/atPathResolver.js.map +1 -0
  128. package/dist/src/memory/pathResolvers/dollarPathResolver.d.ts +11 -0
  129. package/dist/src/memory/pathResolvers/dollarPathResolver.js +18 -0
  130. package/dist/src/memory/pathResolvers/dollarPathResolver.js.map +1 -0
  131. package/dist/src/memory/pathResolvers/hashPathResolver.d.ts +11 -0
  132. package/dist/src/memory/pathResolvers/hashPathResolver.js +18 -0
  133. package/dist/src/memory/pathResolvers/hashPathResolver.js.map +1 -0
  134. package/dist/src/memory/pathResolvers/index.d.ts +11 -0
  135. package/dist/src/memory/pathResolvers/index.js +28 -0
  136. package/dist/src/memory/pathResolvers/index.js.map +1 -0
  137. package/dist/src/memory/pathResolvers/pathResolver.d.ts +13 -0
  138. package/dist/src/memory/pathResolvers/pathResolver.js +7 -0
  139. package/dist/src/memory/pathResolvers/pathResolver.js.map +1 -0
  140. package/dist/src/memory/pathResolvers/percentPathResolver.d.ts +11 -0
  141. package/dist/src/memory/pathResolvers/percentPathResolver.js +18 -0
  142. package/dist/src/memory/pathResolvers/percentPathResolver.js.map +1 -0
  143. package/dist/src/memory/scopePath.d.ts +18 -0
  144. package/dist/src/memory/scopePath.js +23 -0
  145. package/dist/src/memory/scopePath.js.map +1 -0
  146. package/dist/src/memory/scopes/agentStateMemoryScope.d.ts +53 -0
  147. package/dist/src/memory/scopes/agentStateMemoryScope.js +82 -0
  148. package/dist/src/memory/scopes/agentStateMemoryScope.js.map +1 -0
  149. package/dist/src/memory/scopes/classMemoryScope.d.ts +29 -0
  150. package/dist/src/memory/scopes/classMemoryScope.js +65 -0
  151. package/dist/src/memory/scopes/classMemoryScope.js.map +1 -0
  152. package/dist/src/memory/scopes/conversationMemoryScope.d.ts +15 -0
  153. package/dist/src/memory/scopes/conversationMemoryScope.js +23 -0
  154. package/dist/src/memory/scopes/conversationMemoryScope.js.map +1 -0
  155. package/dist/src/memory/scopes/dialogClassMemoryScope.d.ts +19 -0
  156. package/dist/src/memory/scopes/dialogClassMemoryScope.js +42 -0
  157. package/dist/src/memory/scopes/dialogClassMemoryScope.js.map +1 -0
  158. package/dist/src/memory/scopes/dialogContextMemoryScope.d.ts +19 -0
  159. package/dist/src/memory/scopes/dialogContextMemoryScope.js +48 -0
  160. package/dist/src/memory/scopes/dialogContextMemoryScope.js.map +1 -0
  161. package/dist/src/memory/scopes/dialogMemoryScope.d.ts +32 -0
  162. package/dist/src/memory/scopes/dialogMemoryScope.js +74 -0
  163. package/dist/src/memory/scopes/dialogMemoryScope.js.map +1 -0
  164. package/dist/src/memory/scopes/index.d.ts +15 -0
  165. package/dist/src/memory/scopes/index.js +32 -0
  166. package/dist/src/memory/scopes/index.js.map +1 -0
  167. package/dist/src/memory/scopes/memoryScope.d.ts +52 -0
  168. package/dist/src/memory/scopes/memoryScope.js +54 -0
  169. package/dist/src/memory/scopes/memoryScope.js.map +1 -0
  170. package/dist/src/memory/scopes/settingsMemoryScope.d.ts +68 -0
  171. package/dist/src/memory/scopes/settingsMemoryScope.js +216 -0
  172. package/dist/src/memory/scopes/settingsMemoryScope.js.map +1 -0
  173. package/dist/src/memory/scopes/thisMemoryScope.d.ts +26 -0
  174. package/dist/src/memory/scopes/thisMemoryScope.js +43 -0
  175. package/dist/src/memory/scopes/thisMemoryScope.js.map +1 -0
  176. package/dist/src/memory/scopes/turnMemoryScope.d.ts +29 -0
  177. package/dist/src/memory/scopes/turnMemoryScope.js +52 -0
  178. package/dist/src/memory/scopes/turnMemoryScope.js.map +1 -0
  179. package/dist/src/memory/scopes/userMemoryScope.d.ts +15 -0
  180. package/dist/src/memory/scopes/userMemoryScope.js +23 -0
  181. package/dist/src/memory/scopes/userMemoryScope.js.map +1 -0
  182. package/dist/src/memory/turnPath.d.ts +21 -0
  183. package/dist/src/memory/turnPath.js +38 -0
  184. package/dist/src/memory/turnPath.js.map +1 -0
  185. package/dist/src/prompts/activityPrompt.d.ts +101 -0
  186. package/dist/src/prompts/activityPrompt.js +166 -0
  187. package/dist/src/prompts/activityPrompt.js.map +1 -0
  188. package/dist/src/prompts/attachmentPrompt.d.ts +37 -0
  189. package/dist/src/prompts/attachmentPrompt.js +55 -0
  190. package/dist/src/prompts/attachmentPrompt.js.map +1 -0
  191. package/dist/src/prompts/choicePrompt.d.ts +83 -0
  192. package/dist/src/prompts/choicePrompt.js +116 -0
  193. package/dist/src/prompts/choicePrompt.js.map +1 -0
  194. package/dist/src/prompts/confirmPrompt.d.ts +82 -0
  195. package/dist/src/prompts/confirmPrompt.js +163 -0
  196. package/dist/src/prompts/confirmPrompt.js.map +1 -0
  197. package/dist/src/prompts/datetimePrompt.d.ts +65 -0
  198. package/dist/src/prompts/datetimePrompt.js +109 -0
  199. package/dist/src/prompts/datetimePrompt.js.map +1 -0
  200. package/dist/src/prompts/index.d.ts +13 -0
  201. package/dist/src/prompts/index.js +30 -0
  202. package/dist/src/prompts/index.js.map +1 -0
  203. package/dist/src/prompts/numberPrompt.d.ts +55 -0
  204. package/dist/src/prompts/numberPrompt.js +134 -0
  205. package/dist/src/prompts/numberPrompt.js.map +1 -0
  206. package/dist/src/prompts/prompt.d.ts +256 -0
  207. package/dist/src/prompts/prompt.js +260 -0
  208. package/dist/src/prompts/prompt.js.map +1 -0
  209. package/dist/src/prompts/promptCultureModels.d.ts +72 -0
  210. package/dist/src/prompts/promptCultureModels.js +123 -0
  211. package/dist/src/prompts/promptCultureModels.js.map +1 -0
  212. package/dist/src/prompts/textPrompt.d.ts +54 -0
  213. package/dist/src/prompts/textPrompt.js +72 -0
  214. package/dist/src/prompts/textPrompt.js.map +1 -0
  215. package/dist/src/recognizer.d.ts +43 -0
  216. package/dist/src/recognizer.js +104 -0
  217. package/dist/src/recognizer.js.map +1 -0
  218. package/dist/src/recognizerResult.d.ts +19 -0
  219. package/dist/src/recognizerResult.js +24 -0
  220. package/dist/src/recognizerResult.js.map +1 -0
  221. package/dist/src/serviceCollection.d.ts +112 -0
  222. package/dist/src/serviceCollection.js +209 -0
  223. package/dist/src/serviceCollection.js.map +1 -0
  224. package/dist/src/stringUtils.d.ts +36 -0
  225. package/dist/src/stringUtils.js +62 -0
  226. package/dist/src/stringUtils.js.map +1 -0
  227. package/dist/src/template.d.ts +18 -0
  228. package/dist/src/template.js +7 -0
  229. package/dist/src/template.js.map +1 -0
  230. package/dist/src/waterfallDialog.d.ts +101 -0
  231. package/dist/src/waterfallDialog.js +179 -0
  232. package/dist/src/waterfallDialog.js.map +1 -0
  233. package/dist/src/waterfallStepContext.d.ts +71 -0
  234. package/dist/src/waterfallStepContext.js +78 -0
  235. package/dist/src/waterfallStepContext.js.map +1 -0
  236. package/package.json +49 -0
  237. package/src/agentComponent.ts +23 -0
  238. package/src/agentStateSet.ts +75 -0
  239. package/src/choices/choice.ts +16 -0
  240. package/src/choices/choiceFactory.ts +189 -0
  241. package/src/choices/findChoices.ts +70 -0
  242. package/src/choices/findValues.ts +179 -0
  243. package/src/choices/index.ts +11 -0
  244. package/src/choices/modelResult.ts +11 -0
  245. package/src/choices/recognizeChoices.ts +94 -0
  246. package/src/choices/tokenizer.ts +93 -0
  247. package/src/componentDialog.ts +306 -0
  248. package/src/componentRegistration.ts +30 -0
  249. package/src/configurable.ts +55 -0
  250. package/src/configuration.ts +22 -0
  251. package/src/converter.ts +15 -0
  252. package/src/dialog.ts +243 -0
  253. package/src/dialogContainer.ts +90 -0
  254. package/src/dialogContext.ts +607 -0
  255. package/src/dialogContextError.ts +50 -0
  256. package/src/dialogEvent.ts +23 -0
  257. package/src/dialogEvents.ts +16 -0
  258. package/src/dialogHelper.ts +154 -0
  259. package/src/dialogInstance.ts +32 -0
  260. package/src/dialogManager.ts +202 -0
  261. package/src/dialogReason.ts +40 -0
  262. package/src/dialogSet.ts +168 -0
  263. package/src/dialogTurnResult.ts +37 -0
  264. package/src/dialogTurnStateConstants.ts +13 -0
  265. package/src/dialogTurnStatus.ts +38 -0
  266. package/src/dialogsAgentComponent.ts +71 -0
  267. package/src/dialogsComponentRegistration.ts +49 -0
  268. package/src/i18n.ts +17 -0
  269. package/src/index.ts +31 -0
  270. package/src/intentScore.ts +11 -0
  271. package/src/memory/componentMemoryScopes.ts +28 -0
  272. package/src/memory/componentPathResolvers.ts +29 -0
  273. package/src/memory/dialogPath.ts +33 -0
  274. package/src/memory/dialogStateManager.ts +661 -0
  275. package/src/memory/index.ts +12 -0
  276. package/src/memory/pathResolvers/aliasPathResolver.ts +44 -0
  277. package/src/memory/pathResolvers/atAtPathResolver.ts +14 -0
  278. package/src/memory/pathResolvers/atPathResolver.ts +45 -0
  279. package/src/memory/pathResolvers/dollarPathResolver.ts +14 -0
  280. package/src/memory/pathResolvers/hashPathResolver.ts +14 -0
  281. package/src/memory/pathResolvers/index.ts +11 -0
  282. package/src/memory/pathResolvers/pathResolver.ts +14 -0
  283. package/src/memory/pathResolvers/percentPathResolver.ts +14 -0
  284. package/src/memory/scopePath.ts +19 -0
  285. package/src/memory/scopes/agentStateMemoryScope.ts +92 -0
  286. package/src/memory/scopes/classMemoryScope.ts +71 -0
  287. package/src/memory/scopes/conversationMemoryScope.ts +19 -0
  288. package/src/memory/scopes/dialogClassMemoryScope.ts +43 -0
  289. package/src/memory/scopes/dialogContextMemoryScope.ts +51 -0
  290. package/src/memory/scopes/dialogMemoryScope.ts +79 -0
  291. package/src/memory/scopes/index.ts +15 -0
  292. package/src/memory/scopes/memoryScope.ts +70 -0
  293. package/src/memory/scopes/settingsMemoryScope.ts +239 -0
  294. package/src/memory/scopes/thisMemoryScope.ts +44 -0
  295. package/src/memory/scopes/turnMemoryScope.ts +54 -0
  296. package/src/memory/scopes/userMemoryScope.ts +19 -0
  297. package/src/memory/turnPath.ts +45 -0
  298. package/src/prompts/activityPrompt.ts +202 -0
  299. package/src/prompts/attachmentPrompt.ts +62 -0
  300. package/src/prompts/choicePrompt.ts +176 -0
  301. package/src/prompts/confirmPrompt.ts +182 -0
  302. package/src/prompts/datetimePrompt.ts +110 -0
  303. package/src/prompts/index.ts +13 -0
  304. package/src/prompts/numberPrompt.ts +117 -0
  305. package/src/prompts/prompt.ts +459 -0
  306. package/src/prompts/promptCultureModels.ts +178 -0
  307. package/src/prompts/textPrompt.ts +82 -0
  308. package/src/recognizer.ts +127 -0
  309. package/src/recognizerResult.ts +37 -0
  310. package/src/serviceCollection.ts +305 -0
  311. package/src/stringUtils.ts +62 -0
  312. package/src/template.ts +20 -0
  313. package/src/waterfallDialog.ts +215 -0
  314. package/src/waterfallStepContext.ts +96 -0
  315. package/vendor/cldr-data/README.md +5 -0
  316. package/vendor/cldr-data/main/de/numbers.json +156 -0
  317. package/vendor/cldr-data/main/en/numbers.json +159 -0
  318. package/vendor/cldr-data/main/es/numbers.json +156 -0
  319. package/vendor/cldr-data/main/fr/numbers.json +157 -0
  320. package/vendor/cldr-data/main/ja/numbers.json +120 -0
  321. package/vendor/cldr-data/main/nl/numbers.json +156 -0
  322. package/vendor/cldr-data/main/pt/numbers.json +156 -0
  323. package/vendor/cldr-data/main/zh/numbers.json +214 -0
  324. package/vendor/cldr-data/supplemental/likelySubtags.json +1844 -0
  325. package/vendor/cldr-data/supplemental/numberingSystems.json +343 -0
@@ -0,0 +1,607 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { Activity, TurnContext, TurnContextStateCollection } from '@microsoft/agents-hosting'
6
+ import { Dialog } from './dialog'
7
+ import { DialogSet } from './dialogSet'
8
+ import { PromptOptions } from './prompts'
9
+ import { DialogStateManager, TurnPath } from './memory'
10
+ import { DialogContainer } from './dialogContainer'
11
+ import { DialogEvents } from './dialogEvents'
12
+ import { DialogManager } from './dialogManager'
13
+ import { DialogTurnStateConstants } from './dialogTurnStateConstants'
14
+ import { DialogContextError } from './dialogContextError'
15
+ import { DialogInstance } from './dialogInstance'
16
+ import { DialogReason } from './dialogReason'
17
+ import { DialogEvent } from './dialogEvent'
18
+ import { DialogTurnResult } from './dialogTurnResult'
19
+ import { DialogTurnStatus } from './dialogTurnStatus'
20
+ import { Choice } from './choices'
21
+
22
+ /**
23
+ * Wraps a promise in a try-catch that automatically enriches errors with extra dialog context.
24
+ *
25
+ * @param dialogContext source dialog context from which enriched error properties are sourced
26
+ * @param promise a promise to await inside a try-catch for error enrichment
27
+ * @returns A promise representing the asynchronous operation.
28
+ */
29
+ const wrapErrors = async <T>(dialogContext: DialogContext, promise: Promise<T>): Promise<T> => {
30
+ try {
31
+ return await promise
32
+ } catch (err: any) {
33
+ if (err instanceof DialogContextError) {
34
+ throw err
35
+ } else {
36
+ throw new DialogContextError(err as Error, dialogContext)
37
+ }
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @private
43
+ */
44
+ const ACTIVITY_RECEIVED_EMITTED = Symbol('ActivityReceivedEmitted')
45
+
46
+ /**
47
+ * Contains dialog state, information about the state of the dialog stack, for a specific DialogSet.
48
+ *
49
+ * @remarks
50
+ * State is read from and saved to storage each turn, and state cache for the turn is managed through the TurnContext.
51
+ *
52
+ * For more information, see the articles on
53
+ * [Managing state](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-state) and
54
+ * [Dialogs library](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog).
55
+ */
56
+ export interface DialogState {
57
+ /**
58
+ * Contains state information for each Dialog on the stack.
59
+ */
60
+ dialogStack: DialogInstance[];
61
+ }
62
+
63
+ /**
64
+ * The context for the current dialog turn with respect to a specific DialogSet.
65
+ *
66
+ * @remarks
67
+ * This includes the turn context, information about the dialog set, and the state of the dialog stack.
68
+ *
69
+ * From code outside of a dialog in the set, use DialogSet.createContext
70
+ * to create the dialog context. Then use the methods of the dialog context to manage the progression of dialogs in the set.
71
+ *
72
+ * When you implement a dialog, the dialog context is a parameter available to the various methods you override or implement.
73
+ *
74
+ */
75
+ export class DialogContext {
76
+ /**
77
+ * Creates an new instance of the DialogContext class.
78
+ *
79
+ * @param dialogs The DialogSet for which to create the dialog context.
80
+ * @param contextOrDialogContext The TurnContext object for the current turn.
81
+ * @param state The state object to use to read and write DialogState to storage.
82
+ * @remarks
83
+ * Passing in a DialogContext instance will clone the dialog context.
84
+ */
85
+ constructor (dialogs: DialogSet, contextOrDialogContext: TurnContext, state: DialogState)
86
+
87
+ /**
88
+ * Creates an new instance of the DialogContext class.
89
+ *
90
+ * @param dialogs The DialogSet for which to create the dialog context.
91
+ * @param contextOrDialogContext The DialogContext object for the current turn.
92
+ * @param state The state object to use to read and write DialogState to storage.
93
+ * @remarks
94
+ * Passing in a DialogContext instance will clone the dialog context.
95
+ */
96
+ constructor (dialogs: DialogSet, contextOrDialogContext: DialogContext, state: DialogState)
97
+
98
+ /**
99
+ * Creates an new instance of the DialogContext class.
100
+ *
101
+ * @param dialogs The DialogSet for which to create the dialog context.
102
+ * @param contextOrDialogContext The TurnContext or DialogContext for the current turn.
103
+ * @param state The state object to use to read and write DialogState to storage.
104
+ * @remarks Passing in a DialogContext instance will clone the dialog context.
105
+ */
106
+ constructor (dialogs: DialogSet, contextOrDialogContext: TurnContext | DialogContext, state: DialogState) {
107
+ this.dialogs = dialogs
108
+ if (contextOrDialogContext instanceof DialogContext) {
109
+ this.context = contextOrDialogContext.context
110
+ this.parent = contextOrDialogContext
111
+ if (this.parent.services) {
112
+ this.parent.services.forEach((value, key): void => {
113
+ this.services.set(key, value)
114
+ })
115
+ }
116
+ } else {
117
+ this.context = contextOrDialogContext
118
+ }
119
+ if (!Array.isArray(state.dialogStack)) {
120
+ state.dialogStack = []
121
+ }
122
+ this.stack = state.dialogStack
123
+ this.state = new DialogStateManager(this)
124
+ this.state.setValue(TurnPath.activity, this.context.activity)
125
+ }
126
+
127
+ /**
128
+ * Gets the dialogs that can be called directly from this context.
129
+ */
130
+ dialogs: DialogSet
131
+
132
+ /**
133
+ * Gets the context object for the turn.
134
+ */
135
+ context: TurnContext
136
+
137
+ /**
138
+ * Gets the current dialog stack.
139
+ */
140
+ stack: DialogInstance[]
141
+
142
+ /**
143
+ * The parent dialog context for this dialog context, or `undefined` if this context doesn't have a parent.
144
+ *
145
+ * @remarks
146
+ * When it attempts to start a dialog, the dialog context searches for the Dialog.id
147
+ * in its DialogContext.dialogs. If the dialog to start is not found
148
+ * in this dialog context, it searches in its parent dialog context, and so on.
149
+ */
150
+ parent: DialogContext | undefined
151
+
152
+ /**
153
+ * @returns Dialog context for child if the active dialog is a container.
154
+ */
155
+ get child (): DialogContext | undefined {
156
+ const instance = this.activeDialog
157
+ if (instance !== undefined) {
158
+ // Is active dialog a container?
159
+ const dialog = this.findDialog(instance.id)
160
+ if (dialog instanceof DialogContainer) {
161
+ return dialog.createChildContext(this)
162
+ }
163
+ }
164
+
165
+ return undefined
166
+ }
167
+
168
+ /**
169
+ * @returns The state information for the dialog on the top of the dialog stack, or `undefined` if
170
+ * the stack is empty.
171
+ */
172
+ get activeDialog (): DialogInstance | undefined {
173
+ return this.stack.length > 0 ? this.stack[this.stack.length - 1] : undefined
174
+ }
175
+
176
+ /**
177
+ * Gets the DialogStateManager which manages view of all memory scopes.
178
+ */
179
+ state: DialogStateManager
180
+
181
+ /**
182
+ * Gets the services collection which is contextual to this dialog context.
183
+ */
184
+ services: TurnContextStateCollection = new TurnContextStateCollection()
185
+
186
+ /**
187
+ * @deprecated This property serves no function.
188
+ * @returns The current dialog manager instance. This property is deprecated.
189
+ */
190
+ get dialogManager (): DialogManager {
191
+ return this.context.turnState.get(DialogTurnStateConstants.dialogManager)
192
+ }
193
+
194
+ /**
195
+ * Obtain the CultureInfo in DialogContext.
196
+ *
197
+ * @returns a locale string.
198
+ */
199
+ getLocale (): string {
200
+ const _turnLocaleProperty = 'turn.locale'
201
+
202
+ const turnLocaleValue = this.state.getValue(_turnLocaleProperty)
203
+ if (turnLocaleValue) {
204
+ return turnLocaleValue
205
+ }
206
+
207
+ const locale = this.context.activity?.locale
208
+ if (locale !== undefined) {
209
+ return locale
210
+ }
211
+
212
+ return Intl.DateTimeFormat().resolvedOptions().locale
213
+ }
214
+
215
+ /**
216
+ * Starts a dialog instance and pushes it onto the dialog stack.
217
+ * Creates a new instance of the dialog and pushes it onto the stack.
218
+ *
219
+ * @param dialogId ID of the dialog to start.
220
+ * @param options Optional. Arguments to pass into the dialog when it starts.
221
+ * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result.
222
+ * @remarks
223
+ * If there's already an active dialog on the stack, that dialog will be paused until
224
+ * it is again the top dialog on the stack.
225
+ *
226
+ * The DialogTurnResult.status of returned object describes
227
+ * the status of the dialog stack after this method completes.
228
+ *
229
+ * This method throws an exception if the requested dialog can't be found in this dialog context
230
+ * or any of its ancestors.
231
+ *
232
+ */
233
+ async beginDialog (dialogId: string, options?: object): Promise<DialogTurnResult> {
234
+ // Lookup dialog
235
+ const dialog: Dialog<{}> = this.findDialog(dialogId)
236
+ if (!dialog) {
237
+ throw new DialogContextError(
238
+ `DialogContext.beginDialog(): A dialog with an id of '${dialogId}' wasn't found.`,
239
+ this
240
+ )
241
+ }
242
+
243
+ // Push new instance onto stack.
244
+ const instance: DialogInstance<any> = {
245
+ id: dialogId,
246
+ state: {},
247
+ }
248
+ this.stack.push(instance)
249
+
250
+ // Call dialogs begin() method.
251
+ return wrapErrors(this, dialog.beginDialog(this, options))
252
+ }
253
+
254
+ /**
255
+ * Cancels all dialogs on the dialog stack, and clears stack.
256
+ *
257
+ * @param cancelParents Optional. If `true` all parent dialogs will be cancelled as well.
258
+ * @param eventName Optional. Name of a custom event to raise as dialogs are cancelled. This defaults to DialogEvents.cancelDialog.
259
+ * @param eventValue Optional. Value to pass along with custom cancellation event.
260
+ * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result.
261
+ * @remarks
262
+ * This calls each dialog's .Dialog.endDialog method before
263
+ * removing the dialog from the stack.
264
+ *
265
+ * If there were any dialogs on the stack initially, the DialogTurnResult.status
266
+ * of the return value is DialogTurnStatus.cancelled; otherwise, it's
267
+ * DialogTurnStatus.empty.
268
+ *
269
+ */
270
+ async cancelAllDialogs (cancelParents = false, eventName?: string, eventValue?: any): Promise<DialogTurnResult> {
271
+ eventName = eventName || DialogEvents.cancelDialog
272
+ if (this.stack.length > 0 || this.parent !== undefined) {
273
+ // Cancel all local and parent dialogs while checking for interception
274
+ let notify = false
275
+
276
+ let dialogContext: DialogContext = this
277
+ while (dialogContext !== undefined) {
278
+ if (dialogContext.stack.length > 0) {
279
+ // Check to see if the dialog wants to handle the event
280
+ // - We skip notifying the first dialog which actually called cancelAllDialogs()
281
+ if (notify) {
282
+ const handled = await dialogContext.emitEvent(eventName, eventValue, false, false)
283
+ if (handled) {
284
+ break
285
+ }
286
+ }
287
+
288
+ // End the active dialog
289
+ await dialogContext.endActiveDialog(DialogReason.cancelCalled)
290
+ } else {
291
+ dialogContext = cancelParents ? dialogContext.parent : undefined
292
+ }
293
+
294
+ notify = true
295
+ }
296
+
297
+ return { status: DialogTurnStatus.cancelled }
298
+ } else {
299
+ return { status: DialogTurnStatus.empty }
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Searches for a dialog with a given ID.
305
+ *
306
+ * @param dialogId ID of the dialog to search for.
307
+ * @returns The dialog for the provided ID.
308
+ * @remarks
309
+ * If the dialog to start is not found in the DialogSet associated
310
+ * with this dialog context, it attempts to find the dialog in its parent dialog context.
311
+ *
312
+ */
313
+ findDialog (dialogId: string): Dialog | undefined {
314
+ let dialog = this.dialogs.find(dialogId)
315
+ if (!dialog && this.parent) {
316
+ dialog = this.parent.findDialog(dialogId)
317
+ }
318
+ return dialog
319
+ }
320
+
321
+ /**
322
+ * Helper function to simplify formatting the options for calling a prompt dialog.
323
+ *
324
+ * @param dialogId ID of the prompt dialog to start.
325
+ * @param promptOrOptions The text of the initial prompt to send the user,
326
+ * the activity to send as the initial prompt, or
327
+ * the object with which to format the prompt dialog.
328
+ * @param choices Optional. Array of choices for the user to choose from,
329
+ * for use with a ChoicePrompt.
330
+ *
331
+ * @remarks
332
+ * This helper method formats the object to use as the `options` parameter, and then calls
333
+ * DialogContext.beginDialog to start the specified prompt dialog.
334
+ *
335
+ */
336
+ async prompt (
337
+ dialogId: string,
338
+ promptOrOptions: string | Partial<Activity> | PromptOptions,
339
+ ): Promise<DialogTurnResult>
340
+
341
+ /**
342
+ * Helper function to simplify formatting the options for calling a prompt dialog.
343
+ *
344
+ * @param dialogId ID of the prompt dialog to start.
345
+ * @param promptOrOptions The text of the initial prompt to send the user,
346
+ * the Activity to send as the initial prompt, or
347
+ * the object with which to format the prompt dialog.
348
+ * @param choices Optional. Array of choices for the user to choose from,
349
+ * for use with a ChoicePrompt.
350
+ * @remarks
351
+ * This helper method formats the object to use as the `options` parameter, and then calls
352
+ * DialogContext.beginDialog to start the specified prompt dialog.
353
+ *
354
+ */
355
+ async prompt (
356
+ dialogId: string,
357
+ promptOrOptions: string | Partial<Activity> | PromptOptions,
358
+ choices: (string | Choice)[],
359
+ ): Promise<DialogTurnResult>
360
+
361
+ /**
362
+ * Helper function to simplify formatting the options for calling a prompt dialog.
363
+ *
364
+ * @param dialogId ID of the prompt dialog to start.
365
+ * @param promptOrOptions The text of the initial prompt to send the user,
366
+ * or the Activity to send as the initial prompt.
367
+ * @param choices Optional. Array of choices for the user to choose from,
368
+ * for use with a ChoicePrompt.
369
+ * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result.
370
+ * @remarks This helper method formats the object to use as the `options` parameter, and then calls
371
+ * beginDialog to start the specified prompt dialog.
372
+ *
373
+ */
374
+ async prompt (
375
+ dialogId: string,
376
+ promptOrOptions: string | Activity,
377
+ choices?: (string | Choice)[]
378
+ ): Promise<DialogTurnResult> {
379
+ let options: PromptOptions
380
+ if (
381
+ (typeof promptOrOptions === 'object' && (promptOrOptions as Activity).type !== undefined) ||
382
+ typeof promptOrOptions === 'string'
383
+ ) {
384
+ options = { prompt: promptOrOptions as string | Activity }
385
+ } else {
386
+ options = { ...(promptOrOptions as PromptOptions) }
387
+ }
388
+
389
+ if (choices) {
390
+ options.choices = choices
391
+ }
392
+
393
+ return wrapErrors(this, this.beginDialog(dialogId, options))
394
+ }
395
+
396
+ /**
397
+ * Continues execution of the active dialog, if there is one, by passing this dialog context to its
398
+ * Dialog.continueDialog method.
399
+ *
400
+ * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result.
401
+ * @remarks
402
+ * After the call completes, you can check the turn context's TurnContext.responded
403
+ * property to determine if the dialog sent a reply to the user.
404
+ *
405
+ * The DialogTurnResult.status of returned object describes
406
+ * the status of the dialog stack after this method completes.
407
+ *
408
+ * Typically, you would call this from within your agent's turn handler.
409
+ *
410
+ */
411
+ async continueDialog (): Promise<DialogTurnResult> {
412
+ // if we are continuing and haven't emitted the activityReceived event, emit it
413
+ // NOTE: This is backward compatible way for activity received to be fired even if you have legacy dialog loop
414
+ if (!this.context.turnState.has(ACTIVITY_RECEIVED_EMITTED)) {
415
+ this.context.turnState.set(ACTIVITY_RECEIVED_EMITTED, true)
416
+
417
+ // Dispatch "activityReceived" event
418
+ // - This fired from teh leaf and will queue up any interruptions.
419
+ await this.emitEvent(DialogEvents.activityReceived, this.context.activity, true, true)
420
+ }
421
+
422
+ // Check for a dialog on the stack
423
+ const instance: DialogInstance<any> = this.activeDialog
424
+ if (instance) {
425
+ // Lookup dialog
426
+ const dialog: Dialog<{}> = this.findDialog(instance.id)
427
+ if (!dialog) {
428
+ throw new DialogContextError(
429
+ `DialogContext.continueDialog(): Can't continue dialog. A dialog with an id of '${instance.id}' wasn't found.`,
430
+ this
431
+ )
432
+ }
433
+
434
+ // Continue execution of dialog
435
+ return wrapErrors(this, dialog.continueDialog(this))
436
+ } else {
437
+ return { status: DialogTurnStatus.empty }
438
+ }
439
+ }
440
+
441
+ /**
442
+ * Ends a dialog and pops it off the stack. Returns an optional result to the dialog's parent.
443
+ *
444
+ * @param result Optional. A result to pass to the parent logic. This might be the next dialog
445
+ * on the stack, or if this was the last dialog on the stack, a parent dialog context or
446
+ * the agent's turn handler.
447
+ * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result.
448
+ * @remarks
449
+ * The _parent_ dialog is the next dialog on the dialog stack, if there is one. This method
450
+ * calls the parent's Dialog.resumeDialog method,
451
+ * passing the result returned by the ending dialog. If there is no parent dialog, the turn ends
452
+ * and the result is available to the agent through the returned object's
453
+ * DialogTurnResult.result property.
454
+ *
455
+ * The DialogTurnResult.status of returned object describes
456
+ * the status of the dialog stack after this method completes.
457
+ *
458
+ * Typically, you would call this from within the logic for a specific dialog to signal back to
459
+ * the dialog context that the dialog has completed, the dialog should be removed from the stack,
460
+ * and the parent dialog should resume.
461
+ *
462
+ */
463
+ async endDialog (result?: any): Promise<DialogTurnResult> {
464
+ // End the active dialog
465
+ await this.endActiveDialog(DialogReason.endCalled, result)
466
+
467
+ // Resume parent dialog
468
+ const instance: DialogInstance<any> = this.activeDialog
469
+ if (instance) {
470
+ // Lookup dialog
471
+ const dialog: Dialog<{}> = this.findDialog(instance.id)
472
+ if (!dialog) {
473
+ throw new DialogContextError(
474
+ `DialogContext.endDialog(): Can't resume previous dialog. A dialog with an id of '${instance.id}' wasn't found.`,
475
+ this
476
+ )
477
+ }
478
+
479
+ // Return result to previous dialog
480
+ return wrapErrors(this, dialog.resumeDialog(this, DialogReason.endCalled, result))
481
+ } else {
482
+ // Signal completion
483
+ return { status: DialogTurnStatus.complete, result }
484
+ }
485
+ }
486
+
487
+ /**
488
+ * Ends the active dialog and starts a new dialog in its place.
489
+ *
490
+ * @param dialogId ID of the dialog to start.
491
+ * @param options Optional. Arguments to pass into the new dialog when it starts.
492
+ * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result.
493
+ * @remarks
494
+ * This is particularly useful for creating a loop or redirecting to another dialog.
495
+ *
496
+ * The DialogTurnResult.status of returned object describes
497
+ * the status of the dialog stack after this method completes.
498
+ *
499
+ * This method is similar to ending the current dialog and immediately beginning the new one.
500
+ * However, the parent dialog is neither resumed nor otherwise notified.
501
+ *
502
+ */
503
+ async replaceDialog (dialogId: string, options?: object): Promise<DialogTurnResult> {
504
+ // End the active dialog
505
+ await this.endActiveDialog(DialogReason.replaceCalled)
506
+
507
+ // Start replacement dialog
508
+ return this.beginDialog(dialogId, options)
509
+ }
510
+
511
+ /**
512
+ * Requests the active dialog to re-prompt the user for input.
513
+ *
514
+ * @remarks
515
+ * This calls the active dialog's Dialog.repromptDialog method.
516
+ *
517
+ */
518
+ async repromptDialog (): Promise<void> {
519
+ // Try raising event first
520
+ const handled = await this.emitEvent(DialogEvents.repromptDialog, undefined, false, false)
521
+ if (!handled) {
522
+ // Check for a dialog on the stack
523
+ const instance: DialogInstance<any> = this.activeDialog
524
+ if (instance) {
525
+ // Lookup dialog
526
+ const dialog: Dialog<{}> = this.findDialog(instance.id)
527
+ if (!dialog) {
528
+ throw new DialogContextError(
529
+ `DialogContext.repromptDialog(): Can't find a dialog with an id of '${instance.id}'.`,
530
+ this
531
+ )
532
+ }
533
+
534
+ // Ask dialog to re-prompt if supported
535
+ await wrapErrors(this, dialog.repromptDialog(this.context, instance))
536
+ }
537
+ }
538
+ }
539
+
540
+ /**
541
+ * Searches for a dialog with a given ID.
542
+ *
543
+ * @remarks
544
+ * Emits a named event for the current dialog, or someone who started it, to handle.
545
+ * @param name Name of the event to raise.
546
+ * @param value Optional. Value to send along with the event.
547
+ * @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`.
548
+ * @param fromLeaf Optional. Whether the event is emitted from a leaf node.
549
+ * @returns `true` if the event was handled.
550
+ */
551
+ async emitEvent (name: string, value?: any, bubble = true, fromLeaf = false): Promise<boolean> {
552
+ // Initialize event
553
+ const dialogEvent: DialogEvent = {
554
+ bubble,
555
+ name,
556
+ value,
557
+ }
558
+
559
+ // Find starting dialog
560
+
561
+ let dialogContext: DialogContext = this
562
+ if (fromLeaf) {
563
+ while (true) {
564
+ const childDialogContext = dialogContext.child
565
+ if (childDialogContext !== undefined) {
566
+ dialogContext = childDialogContext
567
+ } else {
568
+ break
569
+ }
570
+ }
571
+ }
572
+
573
+ // Dispatch to active dialog first
574
+ // - The active dialog will decide if it should bubble the event to its parent.
575
+ const instance = dialogContext.activeDialog
576
+ if (instance !== undefined) {
577
+ const dialog = dialogContext.findDialog(instance.id)
578
+ if (dialog !== undefined) {
579
+ return wrapErrors(this, dialog.onDialogEvent(dialogContext, dialogEvent))
580
+ }
581
+ }
582
+
583
+ return false
584
+ }
585
+
586
+ /**
587
+ * @private
588
+ * @param reason
589
+ * @param result
590
+ */
591
+ private async endActiveDialog (reason: DialogReason, result?: any): Promise<void> {
592
+ const instance: DialogInstance<any> = this.activeDialog
593
+ if (instance) {
594
+ // Lookup dialog
595
+ const dialog: Dialog<{}> = this.findDialog(instance.id)
596
+ if (dialog) {
597
+ // Notify dialog of end
598
+ await wrapErrors(this, dialog.endDialog(this.context, instance, reason))
599
+ }
600
+
601
+ // Pop dialog off stack
602
+ this.stack.pop()
603
+
604
+ this.state.setValue(TurnPath.lastResult, result)
605
+ }
606
+ }
607
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { DialogContext } from './dialogContext'
6
+ import { DialogInstance } from './dialogInstance'
7
+
8
+ /**
9
+ * An Error that includes extra dialog context, including the dialog stack.
10
+ */
11
+ export class DialogContextError extends Error {
12
+ /**
13
+ * Represents the state of a dialog when an error occurred.
14
+ */
15
+ readonly dialogContext: {
16
+ activeDialog?: string;
17
+ parent?: string;
18
+ stack: DialogInstance[];
19
+ }
20
+
21
+ /**
22
+ * Construct a DialogError.
23
+ *
24
+ * @param {Error | string} error Source error or error message.
25
+ * @param {DialogContext} dialogContext Dialog context that is the source of the error.
26
+ */
27
+ constructor (
28
+ readonly error: Error | string,
29
+ dialogContext: DialogContext
30
+ ) {
31
+ super()
32
+
33
+ if (!(error instanceof Error) && typeof error !== 'string') {
34
+ throw new Error('`error` argument must be an Error or a string')
35
+ }
36
+
37
+ if (!(dialogContext instanceof DialogContext)) {
38
+ throw new Error('`dialogContext` argument must be of type DialogContext')
39
+ }
40
+
41
+ this.name = 'DialogContextError'
42
+ this.message = error instanceof Error ? error.message : error
43
+
44
+ this.dialogContext = {
45
+ activeDialog: dialogContext.activeDialog?.id,
46
+ parent: dialogContext.parent?.activeDialog?.id,
47
+ stack: dialogContext.stack,
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,23 @@
1
+ export interface DialogEvent {
2
+ /**
3
+ * Flag indicating whether the event will be bubbled to the parent `DialogContext`.
4
+ */
5
+ bubble: boolean;
6
+
7
+ /**
8
+ * Name of the event being raised.
9
+ */
10
+ name: string;
11
+
12
+ /**
13
+ * Optional. Value associated with the event.
14
+ */
15
+ value?: any;
16
+ }
17
+
18
+ export interface DialogConfiguration {
19
+ /**
20
+ * Static id of the dialog.
21
+ */
22
+ id?: string;
23
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Represents the events related to the "lifecycle" of the dialog.
8
+ */
9
+ export class DialogEvents {
10
+ static readonly beginDialog = 'beginDialog'
11
+ static readonly repromptDialog = 'repromptDialog'
12
+ static readonly cancelDialog = 'cancelDialog'
13
+ static readonly activityReceived = 'activityReceived'
14
+ static readonly versionChanged = 'versionChanged'
15
+ static readonly error = 'error'
16
+ }