Skip to content

Commit

Permalink
cherrypick: settings memory scope fix, orchestrator runtime compatibl…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
joshgummersall authored Apr 22, 2021
1 parent 7eff262 commit 51d3fd6
Show file tree
Hide file tree
Showing 11 changed files with 990 additions and 1,551 deletions.
7 changes: 6 additions & 1 deletion libraries/botbuilder-ai-orchestrator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
*/

export { LabelType, OrchestratorRecognizer } from './orchestratorRecognizer';
export { OrchestratorBotComponent } from './orchestratorBotComponent';

import { OrchestratorBotComponent } from './orchestratorBotComponent';
export { OrchestratorBotComponent };

// This export ensures that the botbuilder-ai-orchestrator package works as a component in the runtime
export default OrchestratorBotComponent;
3 changes: 2 additions & 1 deletion libraries/botbuilder-dialogs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"botframework-connector": "4.1.6",
"globalize": "^1.4.2",
"lodash": "^4.17.21",
"runtypes": "~5.1.0",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand All @@ -51,7 +52,7 @@
"lint": "eslint . --ext .js,.ts",
"postbuild": "downlevel-dts lib _ts3.4/lib --checksum",
"test": "npm-run-all build test:mocha",
"test:mocha": "nyc mocha tests",
"test:mocha": "nyc mocha --recursive --require source-map-support/register tests",
"test:compat": "api-extractor run --verbose"
},
"files": [
Expand Down
10 changes: 8 additions & 2 deletions libraries/botbuilder-dialogs/src/dialogsBotComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import * as t from 'runtypes';
import { BotComponent } from 'botbuilder-core';
import { Configuration, ServiceCollection } from 'botbuilder-dialogs-adaptive-runtime-core';
import { MemoryScope, PathResolver } from './memory';
Expand All @@ -25,12 +26,17 @@ import {
PercentPathResolver,
} from './memory/pathResolvers';

const InitialSettings = t.Dictionary(t.Unknown, t.String);

export class DialogsBotComponent extends BotComponent {
configureServices(services: ServiceCollection, _configuration: Configuration): void {
configureServices(services: ServiceCollection, configuration: Configuration): void {
services.composeFactory<MemoryScope[]>('memoryScopes', (memoryScopes) => {
const rootConfiguration = configuration.get([]);
const initialSettings = InitialSettings.guard(rootConfiguration) ? rootConfiguration : undefined;

return memoryScopes.concat(
new TurnMemoryScope(),
new SettingsMemoryScope(),
new SettingsMemoryScope(initialSettings),
new DialogMemoryScope(),
new DialogContextMemoryScope(),
new DialogClassMemoryScope(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class Node {
export class SettingsMemoryScope extends MemoryScope {
/**
* Initializes a new instance of the [SettingsMemoryScope](xref:botbuilder-dialogs.SettingsMemoryScope) class.
*
* @param initialSettings initial set of settings to supply
*/
public constructor() {
public constructor(private readonly initialSettings?: Record<string, unknown>) {
super(ScopePath.settings, false);
}

Expand All @@ -57,19 +59,27 @@ export class SettingsMemoryScope extends MemoryScope {
public getMemory(dc: DialogContext): Record<string, unknown> {
if (dc.context.turnState.has(ScopePath.settings)) {
return dc.context.turnState.get(ScopePath.settings) ?? {};
} else {
const configuration = dc.context.turnState.get(DialogTurnStateConstants.configuration) ?? {};
}

const configuration = dc.context.turnState.get(DialogTurnStateConstants.configuration) ?? {};

Object.entries(process.env).reduce((result, [key, value]) => {
result[`${key}`] = value;
return result;
}, configuration);

Object.entries(process.env).reduce((result, [key, value]) => {
result[`${key}`] = value;
return result;
}, configuration);
const settings = SettingsMemoryScope.loadSettings(configuration);
dc.context.turnState.set(ScopePath.settings, settings);

const settings = SettingsMemoryScope.loadSettings(configuration);
dc.context.turnState.set(ScopePath.settings, settings);
return settings;
}

return settings;
public async load(dc: DialogContext): Promise<void> {
if (this.initialSettings) {
dc.context.turnState.set(ScopePath.settings, this.initialSettings);
}

await super.load(dc);
}

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

if (configuration) {
// load configuration into settings
const root = this.convertFlattenSettingToNode(Object.entries(configuration));
root.children.reduce((result, child) => {
result[child.value] = this.convertNodeToObject(child);
return result;
}, settings);
settings = root.children.reduce(
(acc, child) => ({ ...acc, [child.value]: this.convertNodeToObject(child) }),
settings
);
}

return settings;
Expand Down
Loading

0 comments on commit 51d3fd6

Please sign in to comment.