@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,179 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { ModelResult } from './modelResult'
6
+ import { defaultTokenizer, Token, TokenizerFunction } from './tokenizer'
7
+
8
+ /**
9
+ * Basic search options used to control how choices are recognized in a users utterance.
10
+ */
11
+ export interface FindValuesOptions {
12
+ allowPartialMatches?: boolean;
13
+ locale?: string;
14
+ maxTokenDistance?: number;
15
+ tokenizer?: TokenizerFunction;
16
+ }
17
+
18
+ export interface FoundValue {
19
+ value: string;
20
+ index: number;
21
+ score: number;
22
+ }
23
+
24
+ export interface SortedValue {
25
+ value: string;
26
+ index: number;
27
+ }
28
+
29
+ /**
30
+ * INTERNAL: Low-level function that searches for a set of values within an utterance. Higher level
31
+ * functions like `findChoices()` and `recognizeChoices()` are layered above this function. In most
32
+ * cases its easier to just call one of the higher level functions instead but this function contains
33
+ * the fuzzy search algorithm that drives choice recognition.
34
+ *
35
+ * @param utterance The text or user utterance to search over.
36
+ * @param values List of values to search over.
37
+ * @param options (Optional) options used to tweak the search that's performed.
38
+ * @returns A list of found values.
39
+ */
40
+ export function findValues (
41
+ utterance: string,
42
+ values: SortedValue[],
43
+ options?: FindValuesOptions
44
+ ): ModelResult<FoundValue>[] {
45
+ function indexOfToken (token: Token, startPos: number): number {
46
+ for (let i: number = startPos; i < tokens.length; i++) {
47
+ if (tokens[i].normalized === token.normalized) {
48
+ return i
49
+ }
50
+ }
51
+
52
+ return -1
53
+ }
54
+
55
+ function findExactMatch (utterance: string, values: SortedValue[]): ModelResult<FoundValue> | null {
56
+ const entry = values.find(({ value }) => value.toLowerCase() === utterance.toLowerCase())
57
+ if (!entry) {
58
+ return null
59
+ }
60
+ return {
61
+ text: utterance,
62
+ start: 0,
63
+ end: utterance.length - 1,
64
+ typeName: 'value',
65
+ resolution: {
66
+ value: entry.value,
67
+ index: entry.index,
68
+ score: 1,
69
+ },
70
+ }
71
+ }
72
+
73
+ const exactMatch = findExactMatch(utterance, values)
74
+ if (exactMatch) {
75
+ return [exactMatch]
76
+ }
77
+
78
+ function matchValue (
79
+ index: number,
80
+ value: string,
81
+ vTokens: Token[],
82
+ startPos: number
83
+ ): ModelResult<FoundValue> | undefined {
84
+ let matched = 0
85
+ let totalDeviation = 0
86
+ let start = -1
87
+ let end = -1
88
+ vTokens.forEach((token: Token) => {
89
+ const pos: number = indexOfToken(token, startPos)
90
+ if (pos >= 0) {
91
+ const distance: number = matched > 0 ? pos - startPos : 0
92
+ if (distance <= maxDistance) {
93
+ matched++
94
+ totalDeviation += distance
95
+ startPos = pos + 1
96
+
97
+ if (start < 0) {
98
+ start = pos
99
+ }
100
+ end = pos
101
+ }
102
+ }
103
+ })
104
+
105
+ let result: ModelResult<FoundValue> | undefined
106
+ if (matched > 0 && (matched === vTokens.length || opt.allowPartialMatches)) {
107
+ const completeness: number = matched / vTokens.length
108
+
109
+ const accuracy: number = matched / (matched + totalDeviation)
110
+
111
+ const score: number = completeness * accuracy
112
+
113
+ result = {
114
+ start,
115
+ end,
116
+ typeName: 'value',
117
+ resolution: {
118
+ value,
119
+ index,
120
+ score,
121
+ },
122
+ } as ModelResult<FoundValue>
123
+ }
124
+
125
+ return result
126
+ }
127
+
128
+ const list: SortedValue[] = values.sort((a: SortedValue, b: SortedValue) => b.value.length - a.value.length)
129
+
130
+ let matches: ModelResult<FoundValue>[] = []
131
+ const opt: FindValuesOptions = options || {}
132
+ const tokenizer: TokenizerFunction = opt.tokenizer || defaultTokenizer
133
+ const tokens: Token[] = tokenizer(utterance, opt.locale)
134
+ const maxDistance: number = opt.maxTokenDistance !== undefined ? opt.maxTokenDistance : 2
135
+ list.forEach((entry: SortedValue) => {
136
+ let startPos = 0
137
+ const vTokens: Token[] = tokenizer(entry.value.trim(), opt.locale)
138
+ while (startPos < tokens.length) {
139
+ const match = matchValue(entry.index, entry.value, vTokens, startPos)
140
+ if (match) {
141
+ startPos = match.end + 1
142
+ matches.push(match)
143
+ } else {
144
+ break
145
+ }
146
+ }
147
+ })
148
+
149
+ matches = matches.sort(
150
+ (a: ModelResult<FoundValue>, b: ModelResult<FoundValue>) => b.resolution.score - a.resolution.score
151
+ )
152
+
153
+ const results: ModelResult<FoundValue>[] = []
154
+ const foundIndexes: { [index: number]: boolean } = {}
155
+ const usedTokens: { [index: number]: boolean } = {}
156
+ matches.forEach((match: ModelResult<FoundValue>) => {
157
+ let add = !Object.prototype.hasOwnProperty.call(foundIndexes, match.resolution.index)
158
+ for (let i: number = match.start; i <= match.end; i++) {
159
+ if (usedTokens[i]) {
160
+ add = false
161
+ break
162
+ }
163
+ }
164
+
165
+ if (add) {
166
+ foundIndexes[match.resolution.index] = true
167
+ for (let i: number = match.start; i <= match.end; i++) {
168
+ usedTokens[i] = true
169
+ }
170
+
171
+ match.start = tokens[match.start].start
172
+ match.end = tokens[match.end].end
173
+ match.text = utterance.substring(match.start, match.end + 1)
174
+ results.push(match)
175
+ }
176
+ })
177
+
178
+ return results.sort((a: ModelResult<FoundValue>, b: ModelResult<FoundValue>) => a.start - b.start)
179
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export * from './choice'
6
+ export * from './choiceFactory'
7
+ export * from './findChoices'
8
+ export * from './findValues'
9
+ export * from './modelResult'
10
+ export * from './recognizeChoices'
11
+ export * from './tokenizer'
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export interface ModelResult<T extends Record<string, any> = {}> {
6
+ text: string;
7
+ start: number;
8
+ end: number;
9
+ typeName: string;
10
+ resolution: T;
11
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { findChoices, FindChoicesOptions, FoundChoice } from './findChoices'
6
+ import { ModelResult } from './modelResult'
7
+ import { Choice } from './choice'
8
+ import { debug } from '@microsoft/agents-hosting'
9
+
10
+ const logger = debug('dialogs:recognizeChoices')
11
+
12
+ /**
13
+ * High level function for recognizing a choice in a users utterance.
14
+ *
15
+ * @param utterance The text or user utterance to search over. For an incoming 'message' activity you can simply use `context.activity.text`.
16
+ * @param choices List of choices to search over.
17
+ * @param options (Optional) options used to tweak the search that's performed.
18
+ * @returns A list of found choices, sorted by most relevant first.
19
+ */
20
+ export function recognizeChoices (
21
+ utterance: string,
22
+ choices: (string | Choice)[],
23
+ options?: FindChoicesOptions
24
+ ): ModelResult<FoundChoice>[] {
25
+ function matchChoiceByIndex (match: ModelResult<any>): void {
26
+ try {
27
+ const index: number = parseInt(match.resolution.value, 10) - 1
28
+ if (index >= 0 && index < list.length) {
29
+ const choice: Choice = list[index]
30
+ matched.push({
31
+ start: match.start,
32
+ end: match.end,
33
+ typeName: 'choice',
34
+ text: match.text,
35
+ resolution: {
36
+ value: choice.value,
37
+ index,
38
+ score: 1,
39
+ },
40
+ })
41
+ }
42
+ } catch (error: any) {
43
+ logger.error('Error: ', error)
44
+ throw error
45
+ }
46
+ }
47
+
48
+ // Initialize options
49
+ options = Object.assign(
50
+ {
51
+ locale: 'en-us',
52
+ recognizeNumbers: true,
53
+ recognizeOrdinals: true,
54
+ } as FindChoicesOptions,
55
+ options
56
+ )
57
+
58
+ // Normalize choices
59
+ const list: Choice[] = (choices || [])
60
+ .map((choice) => (typeof choice === 'string' ? { value: choice } : choice))
61
+ .filter(
62
+ (choice: Choice) => choice // TODO: does this do anything?
63
+ )
64
+
65
+ // Try finding choices by text search first
66
+ // - We only want to use a single strategy for returning results to avoid issues where utterances
67
+ // like the "the third one" or "the red one" or "the first division book" would miss-recognize as
68
+ // a numerical index or ordinal as well.
69
+ let matched: ModelResult<FoundChoice>[] = findChoices(utterance, list, options)
70
+ if (matched.length === 0) {
71
+ // Next try finding by ordinal
72
+ if (options.recognizeOrdinals) {
73
+ // TODO: Review recognizeOrdinal function
74
+ // const ordinals: ModelResult[] = Recognizers.recognizeOrdinal(utterance, options.locale ?? '')
75
+ const ordinals: ModelResult[] = []
76
+ ordinals.forEach(matchChoiceByIndex)
77
+ }
78
+
79
+ // Finally try by numerical index
80
+ if (matched.length === 0 && options.recognizeNumbers) {
81
+ // TODO: Review recognizeNumber function
82
+ // const numbers: ModelResult[] = Recognizers.recognizeNumber(utterance, options.locale ?? '').forEach(matchChoiceByIndex)
83
+ const numbers: ModelResult[] = []
84
+ numbers.forEach(matchChoiceByIndex)
85
+ }
86
+
87
+ // Sort any found matches by their position within the utterance.
88
+ // - The results from findChoices() are already properly sorted so we just need this
89
+ // for ordinal & numerical lookups.
90
+ matched = matched.sort((a: ModelResult<FoundChoice>, b: ModelResult<FoundChoice>) => a.start - b.start)
91
+ }
92
+
93
+ return matched
94
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export interface Token {
6
+ start: number;
7
+ end: number;
8
+ text: string;
9
+ normalized: string;
10
+ }
11
+
12
+ /**
13
+ * Signature for an alternate word breaker.
14
+ *
15
+ * @param TokenizerFunction.text The text to be tokenized.
16
+ * @param TokenizerFunction.locale (Optional) locale of the text if known.
17
+ */
18
+ export type TokenizerFunction = (text: string, locale?: string) => Token[]
19
+
20
+ /**
21
+ * Simple tokenizer that breaks on spaces and punctuation.
22
+ *
23
+ * @param text The input text.
24
+ * @param _locale Optional, identifies the locale of the input text.
25
+ * @returns A list of tokens.
26
+ */
27
+ export function defaultTokenizer (text: string, _locale?: string): Token[] {
28
+ const tokens: Token[] = []
29
+ let token: Token | undefined
30
+
31
+ function appendToken (end: number): void {
32
+ if (token) {
33
+ token.end = end
34
+ token.normalized = token.text.toLowerCase()
35
+ tokens.push(token)
36
+ token = undefined
37
+ }
38
+ }
39
+
40
+ // Parse text
41
+ const length: number = text ? text.length : 0
42
+ let i = 0
43
+ while (i < length) {
44
+ const codePoint: number = text.codePointAt(i) || text.charCodeAt(i)
45
+ const chr: string = String.fromCodePoint(codePoint)
46
+
47
+ if (isBreakingChar(codePoint)) {
48
+ appendToken(i - 1)
49
+ } else if (codePoint > 0xffff) {
50
+ appendToken(i - 1)
51
+ tokens.push({
52
+ start: i,
53
+ end: i + (chr.length - 1),
54
+ text: chr,
55
+ normalized: chr,
56
+ })
57
+ } else if (!token) {
58
+ token = { start: i, text: chr } as Token
59
+ } else {
60
+ token.text += chr
61
+ }
62
+ i += chr.length
63
+ }
64
+ appendToken(length - 1)
65
+
66
+ return tokens
67
+ }
68
+
69
+ /**
70
+ * @private
71
+ * @param codePoint number of character
72
+ */
73
+ function isBreakingChar (codePoint: number): boolean {
74
+ return (
75
+ isBetween(codePoint, 0x0000, 0x002f) ||
76
+ isBetween(codePoint, 0x003a, 0x0040) ||
77
+ isBetween(codePoint, 0x005b, 0x0060) ||
78
+ isBetween(codePoint, 0x007b, 0x00bf) ||
79
+ isBetween(codePoint, 0x02b9, 0x036f) ||
80
+ isBetween(codePoint, 0x2000, 0x2bff) ||
81
+ isBetween(codePoint, 0x2e00, 0x2e7f)
82
+ )
83
+ }
84
+
85
+ /**
86
+ * @private
87
+ * @param value number value
88
+ * @param from low range
89
+ * @param to high range
90
+ */
91
+ function isBetween (value: number, from: number, to: number): boolean {
92
+ return value >= from && value <= to
93
+ }
@@ -0,0 +1,306 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { TurnContext } from '@microsoft/agents-hosting'
6
+ import { Dialog } from './dialog'
7
+ import { DialogContext } from './dialogContext'
8
+ import { DialogContainer } from './dialogContainer'
9
+ import { DialogTurnResult } from './dialogTurnResult'
10
+ import { DialogReason } from './dialogReason'
11
+ import { DialogInstance } from './dialogInstance'
12
+ import { DialogTurnStatus } from './dialogTurnStatus'
13
+
14
+ const PERSISTED_DIALOG_STATE = 'dialogs'
15
+
16
+ /**
17
+ * Base class for a dialog that contains other child dialogs.
18
+ *
19
+ * @remarks
20
+ * Component dialogs let you break your agent's logic up into components that can themselves be added
21
+ * as a dialog to another `ComponentDialog` or `DialogSet`. Components can also be exported as part
22
+ * of a node package and used within other agents.
23
+ *
24
+ * @param O (Optional) options that can be passed into the `DialogContext.beginDialog()` method.
25
+ */
26
+ export class ComponentDialog<O extends object = {}> extends DialogContainer<O> {
27
+ /**
28
+ * ID of the child dialog that should be started anytime the component is started.
29
+ *
30
+ * @remarks
31
+ * This defaults to the ID of the first child dialog added using [addDialog()](#adddialog).
32
+ */
33
+ protected initialDialogId: string
34
+
35
+ /**
36
+ * Called when the dialog is started and pushed onto the parent's dialog stack.
37
+ * By default, this calls the
38
+ * Dialog.BeginDialogAsync(DialogContext, object, CancellationToken) method
39
+ * of the component dialog's initial dialog, as defined by InitialDialogId.
40
+ * Override this method in a derived class to implement interrupt logic.
41
+ *
42
+ * @param outerDialogContext The parent DialogContext for the current turn of conversation.
43
+ * @param options Optional, initial information to pass to the dialog.
44
+ * @returns A Promise representing the asynchronous operation.
45
+ * @remarks
46
+ * If the task is successful, the result indicates whether the dialog is still
47
+ * active after the turn has been processed by the dialog.
48
+ */
49
+ async beginDialog (outerDialogContext: DialogContext, options?: O): Promise<DialogTurnResult> {
50
+ await this.checkForVersionChange(outerDialogContext)
51
+
52
+ // Start the inner dialog.
53
+ const innerDC: DialogContext = this.createChildContext(outerDialogContext)
54
+ const turnResult: DialogTurnResult<any> = await this.onBeginDialog(innerDC, options)
55
+
56
+ // Check for end of inner dialog
57
+ if (turnResult.status !== DialogTurnStatus.waiting) {
58
+ if (turnResult.status === DialogTurnStatus.cancelled) {
59
+ await this.endComponent(outerDialogContext, turnResult.result)
60
+ const cancelledTurnResult: DialogTurnResult = {
61
+ status: DialogTurnStatus.cancelled,
62
+ result: turnResult.result,
63
+ }
64
+ return cancelledTurnResult
65
+ }
66
+ // Return result to calling dialog
67
+ return await this.endComponent(outerDialogContext, turnResult.result)
68
+ }
69
+ // Just signal end of turn
70
+ return Dialog.EndOfTurn
71
+ }
72
+
73
+ /**
74
+ * Called when the dialog is _continued_, where it is the active dialog and the
75
+ * user replies with a new Activity.
76
+ * If this method is *not* overridden, the dialog automatically ends when the user replies.
77
+ *
78
+ * @param outerDialogContext The parent DialogContext for the current turn of conversation.
79
+ * @returns A Promise representing the asynchronous operation.
80
+ * @remarks
81
+ * If the task is successful, the result indicates whether the dialog is still
82
+ * active after the turn has been processed by the dialog. The result may also contain a
83
+ * return value.
84
+ */
85
+ async continueDialog (outerDialogContext: DialogContext): Promise<DialogTurnResult> {
86
+ await this.checkForVersionChange(outerDialogContext)
87
+
88
+ // Continue execution of inner dialog.
89
+ const innerDC: DialogContext = this.createChildContext(outerDialogContext)
90
+ const turnResult: DialogTurnResult<any> = await this.onContinueDialog(innerDC)
91
+
92
+ // Check for end of inner dialog
93
+ if (turnResult.status !== DialogTurnStatus.waiting) {
94
+ // Return result to calling dialog
95
+ return await this.endComponent(outerDialogContext, turnResult.result)
96
+ }
97
+
98
+ // Just signal end of turn
99
+ return Dialog.EndOfTurn
100
+ }
101
+
102
+ /**
103
+ * Called when a child dialog on the parent's dialog stack completed this turn, returning
104
+ * control to this dialog component.
105
+ *
106
+ * @param outerDialogContext The DialogContext for the current turn of conversation.
107
+ * @param _reason Reason why the dialog resumed.
108
+ * @param _result Optional, value returned from the dialog that was called. The type
109
+ * of the value returned is dependent on the child dialog.
110
+ * @returns A Promise representing the asynchronous operation.
111
+ * @remarks
112
+ * If the task is successful, the result indicates whether this dialog is still
113
+ * active after this dialog turn has been processed.
114
+ * Generally, the child dialog was started with a call to
115
+ * beginDialog(DialogContext, object) in the parent's
116
+ * context. However, if the DialogContext.replaceDialog(string, object) method
117
+ * is called, the logical child dialog may be different than the original.
118
+ * If this method is *not* overridden, the dialog automatically calls its
119
+ * RepromptDialog(ITurnContext, DialogInstance) when the user replies.
120
+ */
121
+ async resumeDialog (outerDialogContext: DialogContext, _reason: DialogReason, _result?: any): Promise<DialogTurnResult> {
122
+ await this.checkForVersionChange(outerDialogContext)
123
+
124
+ // Containers are typically leaf nodes on the stack but the dev is free to push other dialogs
125
+ // on top of the stack which will result in the container receiving an unexpected call to
126
+ // resumeDialog() when the pushed on dialog ends.
127
+ // To avoid the container prematurely ending we need to implement this method and simply
128
+ // ask our inner dialog stack to re-prompt.
129
+ await this.repromptDialog(outerDialogContext.context, outerDialogContext.activeDialog)
130
+
131
+ return Dialog.EndOfTurn
132
+ }
133
+
134
+ /**
135
+ * Called when the dialog should re-prompt the user for input.
136
+ *
137
+ * @param context The TurnContext object for this turn.
138
+ * @param instance State information for this dialog.
139
+ * @returns A Promise representing the asynchronous operation.
140
+ */
141
+ async repromptDialog (context: TurnContext, instance: DialogInstance): Promise<void> {
142
+ // Forward to inner dialogs
143
+ const innerDC: DialogContext = this.createInnerDC(context, instance)
144
+ await innerDC.repromptDialog()
145
+
146
+ // Notify component.
147
+ await this.onRepromptDialog(context, instance)
148
+ }
149
+
150
+ /**
151
+ * Called when the Dialog is ending.
152
+ *
153
+ * @param context The TurnContext object for this turn.
154
+ * @param instance State information associated with the instance of this component Dialog on its parent's dialog stack.
155
+ * @param reason Reason why the Dialog ended.
156
+ * @returns A Promise representing the asynchronous operation.
157
+ * @remarks When this method is called from the parent dialog's context, the component Dialog
158
+ * cancels all of the dialogs on its inner dialog stack before ending.
159
+ */
160
+ async endDialog (context: TurnContext, instance: DialogInstance, reason: DialogReason): Promise<void> {
161
+ // Forward cancel to inner dialogs
162
+ if (reason === DialogReason.cancelCalled) {
163
+ const innerDC: DialogContext = this.createInnerDC(context, instance)
164
+ await innerDC.cancelAllDialogs()
165
+ }
166
+
167
+ // Notify component
168
+ await this.onEndDialog(context, instance, reason)
169
+ }
170
+
171
+ /**
172
+ * Adds a child Dialog or prompt to the components internal DialogSet.
173
+ *
174
+ * @param dialog The child Dialog or prompt to add.
175
+ * @returns The ComponentDialog after the operation is complete.
176
+ * @remarks
177
+ * The Dialog.id of the first child added to the component will be assigned to the initialDialogId property.
178
+ */
179
+ addDialog (dialog: Dialog): this {
180
+ this.dialogs.add(dialog)
181
+ if (this.initialDialogId === undefined) {
182
+ this.initialDialogId = dialog.id
183
+ }
184
+
185
+ return this
186
+ }
187
+
188
+ /**
189
+ * Creates the inner dialog context
190
+ *
191
+ * @param outerDialogContext the outer dialog context
192
+ * @returns The created Dialog Context.
193
+ */
194
+ createChildContext (outerDialogContext: DialogContext): DialogContext {
195
+ return this.createInnerDC(outerDialogContext, outerDialogContext.activeDialog)
196
+ }
197
+
198
+ /**
199
+ * Called anytime an instance of the component has been started.
200
+ *
201
+ * @remarks
202
+ * SHOULD be overridden by components that wish to perform custom interruption logic. The
203
+ * default implementation calls `innerDC.beginDialog()` with the dialog assigned to
204
+ * initialdialogid.
205
+ * @param innerDialogContext Dialog context for the components internal `DialogSet`.
206
+ * @param options (Optional) options that were passed to the component by its parent.
207
+ * @returns {Promise<DialogTurnResult>} A promise resolving to the dialog turn result.
208
+ */
209
+ protected onBeginDialog (innerDialogContext: DialogContext, options?: O): Promise<DialogTurnResult> {
210
+ return innerDialogContext.beginDialog(this.initialDialogId, options)
211
+ }
212
+
213
+ /**
214
+ * Called anytime a multi-turn component receives additional activities.
215
+ *
216
+ * @remarks
217
+ * SHOULD be overridden by components that wish to perform custom interruption logic. The
218
+ * default implementation calls `innerDC.continueDialog()`.
219
+ * @param innerDC Dialog context for the components internal `DialogSet`.
220
+ * @returns {Promise<DialogTurnResult>} A promise resolving to the dialog turn result.
221
+ */
222
+ protected onContinueDialog (innerDC: DialogContext): Promise<DialogTurnResult> {
223
+ return innerDC.continueDialog()
224
+ }
225
+
226
+ /**
227
+ * Called when the component is ending.
228
+ *
229
+ * @remarks
230
+ * If the `reason` code is equal to `DialogReason.cancelCalled`, then any active child dialogs
231
+ * will be cancelled before this method is called.
232
+ * @param _context Context for the current turn of conversation.
233
+ * @param _instance The components instance data within its parents dialog stack.
234
+ * @param _reason The reason the component is ending.
235
+ * @returns A promise representing the asynchronous operation.
236
+ */
237
+ protected onEndDialog (_context: TurnContext, _instance: DialogInstance, _reason: DialogReason): Promise<void> {
238
+ return Promise.resolve()
239
+ }
240
+
241
+ /**
242
+ * Called when the component has been requested to re-prompt the user for input.
243
+ *
244
+ * @remarks
245
+ * The active child dialog will have already been asked to reprompt before this method is called.
246
+ * @param _context Context for the current turn of conversation.
247
+ * @param _instance The instance of the current dialog.
248
+ * @returns A promise representing the asynchronous operation.
249
+ */
250
+ protected onRepromptDialog (_context: TurnContext, _instance: DialogInstance): Promise<void> {
251
+ return Promise.resolve()
252
+ }
253
+
254
+ /**
255
+ * Called when the components last active child dialog ends and the component is ending.
256
+ *
257
+ * @remarks
258
+ * SHOULD be overridden by components that wish to perform custom logic before the component
259
+ * ends. The default implementation calls `outerDC.endDialog()` with the `result` returned
260
+ * from the last active child dialog.
261
+ * @param outerDC Dialog context for the parents `DialogSet`.
262
+ * @param result Result returned by the last active child dialog. Can be a value of `undefined`.
263
+ * @returns {Promise<DialogTurnResult>} A promise resolving to the dialog turn result.
264
+ */
265
+ protected endComponent (outerDC: DialogContext, result: any): Promise<DialogTurnResult> {
266
+ return outerDC.endDialog(result)
267
+ }
268
+
269
+ /**
270
+ * @private
271
+ * @param context DialogContext for the current turn of conversation with the user.
272
+ * @param instance DialogInstance which contains the current state information for this dialog.
273
+ * @returns A new DialogContext instance.
274
+ * @remarks
275
+ * You should only call this if you don't have a dialogContext to work with (such as OnResume())
276
+ */
277
+ private createInnerDC (context: DialogContext, instance: DialogInstance): DialogContext
278
+ /**
279
+ * @private
280
+ * @param context TurnContext for the current turn of conversation with the user.
281
+ * @param instance DialogInstance which contains the current state information for this dialog.
282
+ * @returns A new DialogContext instance.
283
+ * @remarks
284
+ * You should only call this if you don't have a dc to work with (such as OnResume())
285
+ */
286
+ private createInnerDC (context: TurnContext, instance: DialogInstance): DialogContext
287
+ /**
288
+ * @private
289
+ * @param context TurnContext or DialogContext for the current turn of conversation with the user.
290
+ * @param instance DialogInstance which contains the current state information for this dialog.
291
+ * @returns A new DialogContext instance.
292
+ * @remarks
293
+ * You should only call this if you don't have a dc to work with (such as OnResume())
294
+ */
295
+ private createInnerDC (context: TurnContext | DialogContext, instance: DialogInstance): DialogContext {
296
+ if (!instance) {
297
+ const dialogInstance = { state: {} }
298
+ instance = dialogInstance as DialogInstance
299
+ }
300
+
301
+ const dialogState = instance.state[PERSISTED_DIALOG_STATE] || { dialogStack: [] }
302
+ instance.state[PERSISTED_DIALOG_STATE] = dialogState
303
+
304
+ return new DialogContext(this.dialogs, context as TurnContext, dialogState)
305
+ }
306
+ }