Skip to content

Commit 51d3fd6

Browse files
cherrypick: settings memory scope fix, orchestrator runtime compatible package (#3607)
* fix: export default orchestrator bot component (#3599) * port: settings memory scope (#3549) * fix: reorganize dialog test structure * fix: refactor dialog memory tests * port: settings memory scope initial settings Fixes #3461 * fix: schema drift
1 parent 7eff262 commit 51d3fd6

File tree

11 files changed

+990
-1551
lines changed

11 files changed

+990
-1551
lines changed

libraries/botbuilder-ai-orchestrator/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
*/
88

99
export { LabelType, OrchestratorRecognizer } from './orchestratorRecognizer';
10-
export { OrchestratorBotComponent } from './orchestratorBotComponent';
10+
11+
import { OrchestratorBotComponent } from './orchestratorBotComponent';
12+
export { OrchestratorBotComponent };
13+
14+
// This export ensures that the botbuilder-ai-orchestrator package works as a component in the runtime
15+
export default OrchestratorBotComponent;

libraries/botbuilder-dialogs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"botframework-connector": "4.1.6",
3838
"globalize": "^1.4.2",
3939
"lodash": "^4.17.21",
40+
"runtypes": "~5.1.0",
4041
"uuid": "^8.3.2"
4142
},
4243
"devDependencies": {
@@ -51,7 +52,7 @@
5152
"lint": "eslint . --ext .js,.ts",
5253
"postbuild": "downlevel-dts lib _ts3.4/lib --checksum",
5354
"test": "npm-run-all build test:mocha",
54-
"test:mocha": "nyc mocha tests",
55+
"test:mocha": "nyc mocha --recursive --require source-map-support/register tests",
5556
"test:compat": "api-extractor run --verbose"
5657
},
5758
"files": [

libraries/botbuilder-dialogs/src/dialogsBotComponent.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
import * as t from 'runtypes';
45
import { BotComponent } from 'botbuilder-core';
56
import { Configuration, ServiceCollection } from 'botbuilder-dialogs-adaptive-runtime-core';
67
import { MemoryScope, PathResolver } from './memory';
@@ -25,12 +26,17 @@ import {
2526
PercentPathResolver,
2627
} from './memory/pathResolvers';
2728

29+
const InitialSettings = t.Dictionary(t.Unknown, t.String);
30+
2831
export class DialogsBotComponent extends BotComponent {
29-
configureServices(services: ServiceCollection, _configuration: Configuration): void {
32+
configureServices(services: ServiceCollection, configuration: Configuration): void {
3033
services.composeFactory<MemoryScope[]>('memoryScopes', (memoryScopes) => {
34+
const rootConfiguration = configuration.get([]);
35+
const initialSettings = InitialSettings.guard(rootConfiguration) ? rootConfiguration : undefined;
36+
3137
return memoryScopes.concat(
3238
new TurnMemoryScope(),
33-
new SettingsMemoryScope(),
39+
new SettingsMemoryScope(initialSettings),
3440
new DialogMemoryScope(),
3541
new DialogContextMemoryScope(),
3642
new DialogClassMemoryScope(),

libraries/botbuilder-dialogs/src/memory/scopes/settingsMemoryScope.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class Node {
4343
export class SettingsMemoryScope extends MemoryScope {
4444
/**
4545
* Initializes a new instance of the [SettingsMemoryScope](xref:botbuilder-dialogs.SettingsMemoryScope) class.
46+
*
47+
* @param initialSettings initial set of settings to supply
4648
*/
47-
public constructor() {
49+
public constructor(private readonly initialSettings?: Record<string, unknown>) {
4850
super(ScopePath.settings, false);
4951
}
5052

@@ -57,19 +59,27 @@ export class SettingsMemoryScope extends MemoryScope {
5759
public getMemory(dc: DialogContext): Record<string, unknown> {
5860
if (dc.context.turnState.has(ScopePath.settings)) {
5961
return dc.context.turnState.get(ScopePath.settings) ?? {};
60-
} else {
61-
const configuration = dc.context.turnState.get(DialogTurnStateConstants.configuration) ?? {};
62+
}
63+
64+
const configuration = dc.context.turnState.get(DialogTurnStateConstants.configuration) ?? {};
65+
66+
Object.entries(process.env).reduce((result, [key, value]) => {
67+
result[`${key}`] = value;
68+
return result;
69+
}, configuration);
6270

63-
Object.entries(process.env).reduce((result, [key, value]) => {
64-
result[`${key}`] = value;
65-
return result;
66-
}, configuration);
71+
const settings = SettingsMemoryScope.loadSettings(configuration);
72+
dc.context.turnState.set(ScopePath.settings, settings);
6773

68-
const settings = SettingsMemoryScope.loadSettings(configuration);
69-
dc.context.turnState.set(ScopePath.settings, settings);
74+
return settings;
75+
}
7076

71-
return settings;
77+
public async load(dc: DialogContext): Promise<void> {
78+
if (this.initialSettings) {
79+
dc.context.turnState.set(ScopePath.settings, this.initialSettings);
7280
}
81+
82+
await super.load(dc);
7383
}
7484

7585
/**
@@ -79,15 +89,15 @@ export class SettingsMemoryScope extends MemoryScope {
7989
* @returns {Record<string, ?>} Projected dictionary for settings.
8090
*/
8191
protected static loadSettings(configuration: Record<string, string>): Record<string, unknown> {
82-
const settings = {};
92+
let settings = {};
8393

8494
if (configuration) {
8595
// load configuration into settings
8696
const root = this.convertFlattenSettingToNode(Object.entries(configuration));
87-
root.children.reduce((result, child) => {
88-
result[child.value] = this.convertNodeToObject(child);
89-
return result;
90-
}, settings);
97+
settings = root.children.reduce(
98+
(acc, child) => ({ ...acc, [child.value]: this.convertNodeToObject(child) }),
99+
settings
100+
);
91101
}
92102

93103
return settings;

0 commit comments

Comments
 (0)