Skip to content

Commit

Permalink
fix: runtime parity (#3412)
Browse files Browse the repository at this point in the history
* fix: use __ env separator

* fix: add blobs/cosmos storage options
  • Loading branch information
joshgummersall authored Mar 18, 2021
1 parent f3664b5 commit 352e54c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 14 deletions.
1 change: 1 addition & 0 deletions libraries/botbuilder-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"botbuilder": "4.1.6",
"botbuilder-ai": "4.1.6",
"botbuilder-applicationinsights": "4.1.6",
"botbuilder-azure": "4.1.6",
"botbuilder-azure-blobs": "4.1.6",
"botbuilder-dialogs": "4.1.6",
"botbuilder-dialogs-adaptive": "4.1.6",
Expand Down
5 changes: 3 additions & 2 deletions libraries/botbuilder-runtime/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export class Configuration implements IConfiguration {
/**
* Load environment variables as a configuration source.
*
* @param separator value used to indicate nesting
* @returns this for chaining
*/
env(): this {
this.provider.env();
env(separator = '__'): this {
this.provider.env(separator);
return this;
}

Expand Down
65 changes: 55 additions & 10 deletions libraries/botbuilder-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { ok } from 'assert';
import { AdaptiveComponentRegistration } from 'botbuilder-dialogs-adaptive';
import { ApplicationInsightsTelemetryClient, TelemetryInitializerMiddleware } from 'botbuilder-applicationinsights';
import { AuthenticationConfiguration, SimpleCredentialProvider } from 'botframework-connector';
import { BlobsTranscriptStore } from 'botbuilder-azure-blobs';
import { BlobsStorage, BlobsTranscriptStore } from 'botbuilder-azure-blobs';
import { ComponentRegistration } from 'botbuilder';
import { CoreBot } from './coreBot';
import { CoreBotAdapter } from './coreBotAdapter';
import { CosmosDbPartitionedStorage } from 'botbuilder-azure';
import { IServices, ServiceCollection, TPlugin } from 'botbuilder-runtime-core';
import { LuisComponentRegistration, QnAMakerComponentRegistration } from 'botbuilder-ai';
import { ResourceExplorer } from 'botbuilder-dialogs-declarative';
Expand Down Expand Up @@ -46,15 +47,18 @@ function addFeatures(services: ServiceCollection<IServices>, configuration: Conf
}

if (await configuration.bool(['traceTranscript'])) {
const [connectionString, container] = await Promise.all([
configuration.string(['blobTranscript', 'connectionString']),
configuration.string(['blobTranscript', 'connectionString']),
]);
const blobsTranscript = await configuration.type(
['blobTranscript'],
t.Record({
connectionString: t.String,
containerName: t.String,
})
);

middlewareSet.use(
new TranscriptLoggerMiddleware(
connectionString && container
? new BlobsTranscriptStore(connectionString, container)
blobsTranscript
? new BlobsTranscriptStore(blobsTranscript.connectionString, blobsTranscript.containerName)
: new ConsoleTranscriptLogger()
)
);
Expand Down Expand Up @@ -90,10 +94,51 @@ function addTelemetry(services: ServiceCollection<IServices>, configuration: Con
);
}

function addState(services: ServiceCollection<IServices>): void {
services.addInstance('storage', new MemoryStorage());
function addStorage(services: ServiceCollection<IServices>, configuration: Configuration): void {
services.addFactory('conversationState', ['storage'], ({ storage }) => new ConversationState(storage));
services.addFactory('userState', ['storage'], ({ storage }) => new UserState(storage));

services.addFactory('storage', async () => {
const storage = await configuration.string(['runtimeSettings', 'storage']);

switch (storage) {
case 'BlobsStorage': {
const blobsStorage = await configuration.type(
['BlobsStorage'],
t.Record({
connectionString: t.String,
containerName: t.String,
})
);

ok(blobsStorage);

return new BlobsStorage(blobsStorage.connectionString, blobsStorage.containerName);
}

case 'CosmosDbPartitionedStorage': {
const cosmosOptions = await configuration.type(
['CosmosDbPartitionedStorage'],
t.Record({
authKey: t.String.Or(t.Undefined),
compatibilityMode: t.Boolean.Or(t.Undefined),
containerId: t.String,
containerThroughput: t.Number.Or(t.Undefined),
cosmosDbEndpoint: t.String.Or(t.Undefined),
databaseId: t.String,
keySuffix: t.String.Or(t.Undefined),
})
);

ok(cosmosOptions);

return new CosmosDbPartitionedStorage(cosmosOptions);
}

default:
return new MemoryStorage();
}
});
}

function addSkills(services: ServiceCollection<IServices>, configuration: Configuration): void {
Expand Down Expand Up @@ -298,7 +343,7 @@ export async function getRuntimeServices(
addCoreBot(services, configuration);
addFeatures(services, runtimeSettings.bind(['features']));
addSkills(services, runtimeSettings.bind(['skills']));
addState(services);
addStorage(services, configuration);
addTelemetry(services, runtimeSettings.bind(['telemetry']));
await addPlugins(services, configuration);

Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-runtime/test/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Configuration', () => {

configuration.argv(['--strings.argv', 'argv']);

process.env['strings:env'] = 'env';
process.env['strings__env'] = 'env';
configuration.env();

return configuration;
Expand Down
50 changes: 49 additions & 1 deletion libraries/botbuilder-runtime/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { BotFrameworkAdapter } from 'botbuilder';
import { BlobsStorage } from 'botbuilder-azure-blobs';
import { BotFrameworkAdapter, MemoryStorage } from 'botbuilder';
import { Configuration, getRuntimeServices } from '../src';
import { CosmosDbPartitionedStorage } from 'botbuilder-azure';
import { ok } from 'assert';
import { plugin } from 'botbuilder-runtime-core';

Expand Down Expand Up @@ -40,4 +42,50 @@ describe('getRuntimeServices', () => {
const customAdapter = await services.mustMakeInstance('customAdapters');
ok(customAdapter.get('foo'));
});

describe('storage', () => {
it('defaults to memory storage', async () => {
const [services] = await getRuntimeServices(__dirname, __dirname);
ok(services);

const storage = await services.mustMakeInstance('storage');
ok(storage instanceof MemoryStorage);
});

it('supports blobs storage', async () => {
const configuration = new Configuration().argv().env();

configuration.set(['runtimeSettings', 'storage'], 'BlobsStorage');

configuration.set(['BlobsStorage'], {
connectionString: 'UseDevelopmentStorage=true',
containerName: 'containerName',
});

const [services] = await getRuntimeServices(__dirname, configuration);
ok(services);

const storage = await services.mustMakeInstance('storage');
ok(storage instanceof BlobsStorage);
});

it('supports cosmos storage', async () => {
const configuration = new Configuration().argv().env();

configuration.set(['runtimeSettings', 'storage'], 'CosmosDbPartitionedStorage');

configuration.set(['CosmosDbPartitionedStorage'], {
authKey: 'authKey',
cosmosDbEndpoint: 'cosmosDbEndpoint',
containerId: 'containerId',
databaseId: 'databaseId',
});

const [services] = await getRuntimeServices(__dirname, configuration);
ok(services);

const storage = await services.mustMakeInstance('storage');
ok(storage instanceof CosmosDbPartitionedStorage);
});
});
});

0 comments on commit 352e54c

Please sign in to comment.