Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] feat: add user agent override and internalize BotAdapter #1142

Merged
merged 32 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c45aada
internalize `BotAdapter` initialization and add user agent
aacebo Jan 9, 2024
ed24e09
Update Application.ts
aacebo Jan 9, 2024
1eaa6a6
Merge branch 'main' into aacebo/1130
aacebo Jan 9, 2024
431a461
don't publish source code, only built lib
aacebo Jan 9, 2024
f14e41e
Merge branch 'main' into aacebo/1130
aacebo Jan 9, 2024
c035ca0
Update Application.ts
aacebo Jan 9, 2024
9b8f21d
Update Application.spec.ts
aacebo Jan 9, 2024
9087870
Merge branch 'main' into aacebo/1130
aacebo Jan 9, 2024
dfd46f9
Update js/packages/teams-ai/src/Application.ts
aacebo Jan 9, 2024
590cc1c
move `ApplicationBuilder` to its own file
aacebo Jan 9, 2024
952fe43
lint
aacebo Jan 9, 2024
186d6d5
Update index.ts
aacebo Jan 9, 2024
787b99d
refactor for custom bot authentication implementation
aacebo Jan 10, 2024
9b5bf88
Update index.ts
aacebo Jan 10, 2024
5812647
Merge branch 'main' into aacebo/1130
aacebo Jan 10, 2024
a338c3d
update version to avoid sample conflicts
aacebo Jan 10, 2024
e1f828f
add bot auth telemetry header test
aacebo Jan 10, 2024
861ff18
Update BotAdapterOptions.ts
aacebo Jan 10, 2024
ba795f1
update jsdoc
aacebo Jan 10, 2024
80f2167
Update TeamsBotFrameworkAuthentication.ts
aacebo Jan 11, 2024
b2269f5
Update TeamsBotFrameworkAuthentication.ts
aacebo Jan 11, 2024
d8cc105
Update TeamsBotFrameworkAuthentication.ts
aacebo Jan 11, 2024
0dbe50f
Update TeamsBotFrameworkAuthentication.ts
aacebo Jan 11, 2024
80c9c05
Update JS.md
aacebo Jan 11, 2024
db3bfcb
Merge branch 'main' into aacebo/1130
aacebo Jan 11, 2024
23fdaf0
Merge branch 'main' into aacebo/1130
aacebo Jan 11, 2024
e44482d
Merge branch 'main' into aacebo/1130
aacebo Jan 11, 2024
367f5f8
fix backwards compatibility
aacebo Jan 11, 2024
3fa4552
Merge branch 'main' into aacebo/1130
aacebo Jan 11, 2024
6ac6c3d
add sandbox
aacebo Jan 12, 2024
c7266b9
Update Application.spec.ts
aacebo Jan 12, 2024
0098aa1
update user agent
aacebo Jan 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions js/packages/teams-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
},
"files": [
"_ts3.4",
"lib",
"src"
"lib"
]
}
13 changes: 12 additions & 1 deletion js/packages/teams-ai/src/Application.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'assert';
import { Activity, ActivityTypes, Channels, MemoryStorage, MessageReactionTypes, TestAdapter } from 'botbuilder';
import { Activity, ActivityTypes, Channels, CloudAdapter, MemoryStorage, MessageReactionTypes, TestAdapter } from 'botbuilder';
import {
Application,
ApplicationBuilder,
Expand Down Expand Up @@ -123,6 +123,17 @@ describe('Application', () => {
});
});

describe('botAuthentication', () => {
const app = new Application({
botAuthentication: { }
});

it('should initialize `CloudAdapter`', () => {
assert.doesNotThrow(() => app.adapter);
assert.equal(app.adapter instanceof CloudAdapter, true);
});
});

describe('adaptiveCards', () => {
it('should return the adaptiveCards property', () => {
const app = new Application();
Expand Down
78 changes: 64 additions & 14 deletions js/packages/teams-ai/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ import {
Activity,
ActivityTypes,
BotAdapter,
CloudAdapter,
ConfigurationBotFrameworkAuthentication,
ConfigurationBotFrameworkAuthenticationOptions,
ConversationReference,
FileConsentCardResponse,
O365ConnectorCardActionQuery,
ResourceResponse,
Storage,
TurnContext
} from 'botbuilder';
import { ReadReceiptInfo } from 'botframework-connector';

import {
AuthenticationConfiguration,
ConnectorClientOptions,
ReadReceiptInfo,
ServiceClientCredentialsFactory
} from 'botframework-connector';

import packageInfo from '../package.json';
import { AdaptiveCards, AdaptiveCardsOptions } from './AdaptiveCards';
import { AI, AIOptions } from './AI';
import { Meetings } from './Meetings';
Expand All @@ -40,6 +50,11 @@ import {
*/
const TYPING_TIMER_DELAY = 1000;

/**
* @private
*/
const USER_AGENT = `${packageInfo.name}/${packageInfo.version}`;

/**
* Query arguments for a search-based message extension.
* @template TParams Type of the query parameters.
Expand Down Expand Up @@ -68,6 +83,8 @@ export interface Query<TParams extends Record<string, any>> {
export interface ApplicationOptions<TState extends TurnState> {
/**
* Optional. Bot adapter being used.
* @deprecated
* since version 1.0.2, use `botAuthentication` instead
* @remarks
* If using the `longRunningMessages` option or calling the continueConversationAsync() method,
* this property is required.
Expand All @@ -87,6 +104,17 @@ export interface ApplicationOptions<TState extends TurnState> {
*/
botAppId?: string;

/**
* Optional. Bot authentication configuration
*/
botAuthentication?: {
botFrameworkAuthConfig?: ConfigurationBotFrameworkAuthenticationOptions;
credentialsFactory?: ServiceClientCredentialsFactory;
authConfiguration?: AuthenticationConfiguration;
botFrameworkClientFetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
connectorClientOptions?: ConnectorClientOptions;
};

/**
* Optional. Storage provider to use for the application.
*/
Expand Down Expand Up @@ -236,22 +264,31 @@ export class Application<TState extends TurnState = TurnState> {
* @param {ApplicationOptions<TState>} options Optional. Options used to configure the application.
*/
public constructor(options?: Partial<ApplicationOptions<TState>>) {
this._options = Object.assign(
{
removeRecipientMention: true,
startTypingTimer: true,
longRunningMessages: false
} as ApplicationOptions<TState>,
options
) as ApplicationOptions<TState>;

this._adapter = this._options.adapter;
this._options = {
...options,
turnStateFactory: options?.turnStateFactory || (() => new TurnState() as TState),
removeRecipientMention:
options?.removeRecipientMention !== undefined ? options.removeRecipientMention : true,
startTypingTimer: options?.startTypingTimer !== undefined ? options.startTypingTimer : true,
longRunningMessages: options?.longRunningMessages !== undefined ? options.longRunningMessages : false
};

this._adapter = this._options.adapter;

// Create turn state factory
if (!this._options.turnStateFactory) {
this._options.turnStateFactory = () => new TurnState() as TState;
if (!this._adapter && this._options.botAuthentication) {
this._adapter = new CloudAdapter(
new ConfigurationBotFrameworkAuthentication(
this._options.botAuthentication.botFrameworkAuthConfig || {},
this._options.botAuthentication.credentialsFactory,
this._options.botAuthentication.authConfiguration,
this._options.botAuthentication.botFrameworkClientFetch,
{
...(this._options.botAuthentication.connectorClientOptions || {}),
userAgent: USER_AGENT,
userAgentHeaderName: undefined
}
)
);
}

// Create AI component if configured with a planner
Expand Down Expand Up @@ -366,6 +403,19 @@ export class Application<TState extends TurnState = TurnState> {
return this._taskModules;
}

/**
* Sets the bots error handler
* @param handler Function to call when an error is encountered.
* @returns {this} The application instance for chaining purposes.
*/
public error(handler: (context: TurnContext, error: Error) => Promise<void>): this {
if (this._adapter) {
this._adapter.onTurnError = handler;
}

return this;
}

/**
* Adds a new route to the application.
* @remarks
Expand Down
Loading