@microsoft/agents-hosting 0.2.10-g3ac88ff25e → 0.3.5
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.
- package/README.md +17 -1
- package/dist/src/activityHandler.d.ts +304 -46
- package/dist/src/activityHandler.js +298 -45
- package/dist/src/activityHandler.js.map +1 -1
- package/dist/src/agent-client/agentClient.d.ts +50 -0
- package/dist/src/agent-client/agentClient.js +28 -0
- package/dist/src/agent-client/agentClient.js.map +1 -1
- package/dist/src/app/agentApplication.d.ts +272 -7
- package/dist/src/app/agentApplication.js +278 -12
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationBuilder.d.ts +34 -3
- package/dist/src/app/agentApplicationBuilder.js +38 -6
- package/dist/src/app/agentApplicationBuilder.js.map +1 -1
- package/dist/src/app/agentApplicationOptions.d.ts +26 -2
- package/dist/src/app/appMemory.d.ts +34 -0
- package/dist/src/app/{memory.js → appMemory.js} +1 -1
- package/dist/src/app/appMemory.js.map +1 -0
- package/dist/src/app/appRoute.d.ts +6 -0
- package/dist/src/app/attachmentDownloader.d.ts +18 -0
- package/dist/src/app/attachmentDownloader.js +18 -0
- package/dist/src/app/attachmentDownloader.js.map +1 -1
- package/dist/src/app/conversationUpdateEvents.d.ts +6 -0
- package/dist/src/app/index.d.ts +4 -1
- package/dist/src/app/index.js +4 -1
- package/dist/src/app/index.js.map +1 -1
- package/dist/src/app/inputFileDownloader.d.ts +22 -0
- package/dist/src/app/oauth/authorization.d.ts +87 -0
- package/dist/src/app/oauth/authorization.js +135 -0
- package/dist/src/app/oauth/authorization.js.map +1 -0
- package/dist/src/app/routeHandler.d.ts +8 -0
- package/dist/src/app/routeSelector.d.ts +9 -0
- package/dist/src/app/turnEvents.d.ts +6 -0
- package/dist/src/app/turnState.d.ts +152 -2
- package/dist/src/app/turnState.js +125 -0
- package/dist/src/app/turnState.js.map +1 -1
- package/dist/src/app/turnStateEntry.d.ts +32 -0
- package/dist/src/app/turnStateEntry.js +32 -0
- package/dist/src/app/turnStateEntry.js.map +1 -1
- package/dist/src/auth/authConfiguration.d.ts +24 -0
- package/dist/src/auth/authConfiguration.js.map +1 -1
- package/dist/src/auth/request.d.ts +12 -0
- package/dist/src/baseAdapter.d.ts +17 -0
- package/dist/src/baseAdapter.js +17 -0
- package/dist/src/baseAdapter.js.map +1 -1
- package/dist/src/cards/cardFactory.d.ts +3 -0
- package/dist/src/cards/cardFactory.js +3 -0
- package/dist/src/cards/cardFactory.js.map +1 -1
- package/dist/src/cards/index.d.ts +1 -0
- package/dist/src/cards/index.js +1 -0
- package/dist/src/cards/index.js.map +1 -1
- package/dist/src/cards/o365ConnectorCardActionBase.d.ts +8 -0
- package/dist/src/cloudAdapter.d.ts +25 -3
- package/dist/src/cloudAdapter.js +25 -3
- package/dist/src/cloudAdapter.js.map +1 -1
- package/dist/src/getProductInfo.d.ts +6 -0
- package/dist/src/getProductInfo.js +6 -0
- package/dist/src/getProductInfo.js.map +1 -1
- package/dist/src/logger.d.ts +34 -2
- package/dist/src/logger.js +35 -0
- package/dist/src/logger.js.map +1 -1
- package/dist/src/oauth/oAuthFlow.d.ts +32 -3
- package/dist/src/oauth/oAuthFlow.js +38 -14
- package/dist/src/oauth/oAuthFlow.js.map +1 -1
- package/dist/src/oauth/userTokenClient.d.ts +2 -2
- package/dist/src/oauth/userTokenClient.js +3 -3
- package/dist/src/oauth/userTokenClient.js.map +1 -1
- package/dist/src/state/agentState.d.ts +79 -27
- package/dist/src/state/agentState.js +58 -27
- package/dist/src/state/agentState.js.map +1 -1
- package/dist/src/state/agentStatePropertyAccesor.d.ts +67 -11
- package/dist/src/state/agentStatePropertyAccesor.js +58 -11
- package/dist/src/state/agentStatePropertyAccesor.js.map +1 -1
- package/dist/src/statusCodes.d.ts +39 -0
- package/dist/src/statusCodes.js +39 -0
- package/dist/src/statusCodes.js.map +1 -1
- package/dist/src/storage/memoryStorage.d.ts +48 -14
- package/dist/src/storage/memoryStorage.js +48 -14
- package/dist/src/storage/memoryStorage.js.map +1 -1
- package/dist/src/storage/storage.d.ts +43 -13
- package/dist/src/tokenResponseEventName.d.ts +3 -0
- package/dist/src/tokenResponseEventName.js +3 -0
- package/dist/src/tokenResponseEventName.js.map +1 -1
- package/dist/src/turnContext.d.ts +142 -56
- package/dist/src/turnContext.js +123 -53
- package/dist/src/turnContext.js.map +1 -1
- package/package.json +6 -6
- package/src/activityHandler.ts +304 -46
- package/src/agent-client/agentClient.ts +55 -5
- package/src/app/agentApplication.ts +283 -15
- package/src/app/agentApplicationBuilder.ts +39 -7
- package/src/app/agentApplicationOptions.ts +33 -2
- package/src/app/appMemory.ts +38 -0
- package/src/app/appRoute.ts +7 -0
- package/src/app/attachmentDownloader.ts +18 -0
- package/src/app/conversationUpdateEvents.ts +6 -0
- package/src/app/index.ts +4 -1
- package/src/app/inputFileDownloader.ts +24 -0
- package/src/app/oauth/authorization.ts +162 -0
- package/src/app/routeHandler.ts +8 -0
- package/src/app/routeSelector.ts +9 -0
- package/src/app/turnEvents.ts +6 -0
- package/src/app/turnState.ts +153 -3
- package/src/app/turnStateEntry.ts +32 -0
- package/src/auth/authConfiguration.ts +32 -1
- package/src/auth/request.ts +15 -0
- package/src/baseAdapter.ts +18 -0
- package/src/cards/cardFactory.ts +3 -0
- package/src/cards/index.ts +1 -0
- package/src/cards/o365ConnectorCardActionBase.ts +8 -0
- package/src/cloudAdapter.ts +28 -3
- package/src/getProductInfo.ts +7 -0
- package/src/logger.ts +34 -1
- package/src/oauth/oAuthFlow.ts +59 -18
- package/src/oauth/userTokenClient.ts +4 -4
- package/src/state/agentState.ts +81 -29
- package/src/state/agentStatePropertyAccesor.ts +67 -11
- package/src/statusCodes.ts +51 -0
- package/src/storage/memoryStorage.ts +48 -14
- package/src/storage/storage.ts +51 -18
- package/src/tokenResponseEventName.ts +3 -0
- package/src/turnContext.ts +142 -56
- package/dist/src/app/memory.d.ts +0 -10
- package/dist/src/app/memory.js.map +0 -1
- package/dist/src/app/oauth/userIdentity.d.ts +0 -43
- package/dist/src/app/oauth/userIdentity.js +0 -54
- package/dist/src/app/oauth/userIdentity.js.map +0 -1
- package/src/app/memory.ts +0 -14
- package/src/app/oauth/userIdentity.ts +0 -78
|
@@ -15,28 +15,44 @@ import { AppRoute } from './appRoute'
|
|
|
15
15
|
import { TurnContext } from '../turnContext'
|
|
16
16
|
import { ResourceResponse } from '../connector-client'
|
|
17
17
|
import { debug } from '../logger'
|
|
18
|
-
import {
|
|
19
|
-
import { MemoryStorage } from '../storage'
|
|
18
|
+
import { Authorization } from './oauth/authorization'
|
|
20
19
|
|
|
21
20
|
const logger = debug('agents:agent-application')
|
|
22
21
|
|
|
23
22
|
const TYPING_TIMER_DELAY = 1000
|
|
24
|
-
type ApplicationEventHandler<TState extends TurnState> = (context: TurnContext, state: TState) => Promise<boolean>
|
|
23
|
+
export type ApplicationEventHandler<TState extends TurnState> = (context: TurnContext, state: TState) => Promise<boolean>
|
|
25
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Executes the application logic for a given turn context.
|
|
27
|
+
*
|
|
28
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
29
|
+
* @returns A promise that resolves when the application logic has completed.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* This method is the entry point for processing a turn in the conversation.
|
|
33
|
+
* It delegates the actual processing to the `runInternal` method, which handles
|
|
34
|
+
* the core logic for routing and executing handlers.
|
|
35
|
+
*
|
|
36
|
+
* Example usage:
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const app = new AgentApplication();
|
|
39
|
+
* await app.run(turnContext);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
26
42
|
export class AgentApplication<TState extends TurnState> {
|
|
27
43
|
protected readonly _options: AgentApplicationOptions<TState>
|
|
28
44
|
protected readonly _routes: AppRoute<TState>[] = []
|
|
29
45
|
protected readonly _beforeTurn: ApplicationEventHandler<TState>[] = []
|
|
30
46
|
protected readonly _afterTurn: ApplicationEventHandler<TState>[] = []
|
|
31
47
|
private readonly _adapter?: BaseAdapter
|
|
48
|
+
private readonly _authorization?: Authorization
|
|
32
49
|
private _typingTimer: any
|
|
33
|
-
private readonly _userIdentity?: UserIdentity
|
|
34
50
|
|
|
35
51
|
public constructor (options?: Partial<AgentApplicationOptions<TState>>) {
|
|
36
52
|
this._options = {
|
|
37
53
|
...options,
|
|
38
54
|
turnStateFactory: options?.turnStateFactory || (() => new TurnState() as TState),
|
|
39
|
-
startTypingTimer: options?.startTypingTimer !== undefined ? options.startTypingTimer :
|
|
55
|
+
startTypingTimer: options?.startTypingTimer !== undefined ? options.startTypingTimer : false,
|
|
40
56
|
longRunningMessages: options?.longRunningMessages !== undefined ? options.longRunningMessages : false
|
|
41
57
|
}
|
|
42
58
|
|
|
@@ -44,8 +60,8 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
44
60
|
this._adapter = this._options.adapter
|
|
45
61
|
}
|
|
46
62
|
|
|
47
|
-
if (this._options.
|
|
48
|
-
this.
|
|
63
|
+
if (this._options.authorization) {
|
|
64
|
+
this._authorization = new Authorization(this._options.storage!, this._options.authorization)
|
|
49
65
|
}
|
|
50
66
|
|
|
51
67
|
if (this._options.longRunningMessages && !this._adapter && !this._options.agentAppId) {
|
|
@@ -55,6 +71,10 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
55
71
|
}
|
|
56
72
|
}
|
|
57
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Gets the adapter associated with the application.
|
|
76
|
+
* @throws Error if the adapter is not configured.
|
|
77
|
+
*/
|
|
58
78
|
public get adapter (): BaseAdapter {
|
|
59
79
|
if (!this._adapter) {
|
|
60
80
|
throw new Error(
|
|
@@ -65,20 +85,46 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
65
85
|
return this._adapter
|
|
66
86
|
}
|
|
67
87
|
|
|
68
|
-
|
|
69
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Gets the authorization instance for the application.
|
|
90
|
+
* @throws Error if no authentication options were configured.
|
|
91
|
+
*/
|
|
92
|
+
public get authorization (): Authorization {
|
|
93
|
+
if (!this._authorization) {
|
|
70
94
|
throw new Error(
|
|
71
|
-
'The Application.
|
|
95
|
+
'The Application.authorization property is unavailable because no authentication options were configured.'
|
|
72
96
|
)
|
|
73
97
|
}
|
|
74
98
|
|
|
75
|
-
return this.
|
|
99
|
+
return this._authorization
|
|
76
100
|
}
|
|
77
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Gets the options used to configure the application.
|
|
104
|
+
* @returns The application options.
|
|
105
|
+
*/
|
|
78
106
|
public get options (): AgentApplicationOptions<TState> {
|
|
79
107
|
return this._options
|
|
80
108
|
}
|
|
81
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Sets an error handler for the application.
|
|
112
|
+
*
|
|
113
|
+
* @param handler - The error handler function to be called when an error occurs.
|
|
114
|
+
* @returns The current instance of the application.
|
|
115
|
+
*
|
|
116
|
+
* @remarks
|
|
117
|
+
* This method allows you to handle any errors that occur during turn processing.
|
|
118
|
+
* The handler will receive the turn context and the error that occurred.
|
|
119
|
+
*
|
|
120
|
+
* Example usage:
|
|
121
|
+
* ```typescript
|
|
122
|
+
* app.error(async (context, error) => {
|
|
123
|
+
* console.error(`An error occurred: ${error.message}`);
|
|
124
|
+
* await context.sendActivity('Sorry, something went wrong!');
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
82
128
|
public error (handler: (context: TurnContext, error: Error) => Promise<void>): this {
|
|
83
129
|
if (this._adapter) {
|
|
84
130
|
this._adapter.onTurnError = handler
|
|
@@ -87,11 +133,49 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
87
133
|
return this
|
|
88
134
|
}
|
|
89
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Adds a new route to the application for handling activities.
|
|
138
|
+
*
|
|
139
|
+
* @param selector - The selector function that determines if a route should handle the current activity.
|
|
140
|
+
* @param handler - The handler function that will be called if the selector returns true.
|
|
141
|
+
* @returns The current instance of the application.
|
|
142
|
+
*
|
|
143
|
+
* @remarks
|
|
144
|
+
* Routes are evaluated in the order they are added. The first route with a selector that returns true will be used.
|
|
145
|
+
*
|
|
146
|
+
* Example usage:
|
|
147
|
+
* ```typescript
|
|
148
|
+
* app.addRoute(
|
|
149
|
+
* async (context) => context.activity.type === ActivityTypes.Message,
|
|
150
|
+
* async (context, state) => {
|
|
151
|
+
* await context.sendActivity('I received your message');
|
|
152
|
+
* }
|
|
153
|
+
* );
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
90
156
|
public addRoute (selector: RouteSelector, handler: RouteHandler<TState>): this {
|
|
91
157
|
this._routes.push({ selector, handler })
|
|
92
158
|
return this
|
|
93
159
|
}
|
|
94
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Adds a handler for specific activity types.
|
|
163
|
+
*
|
|
164
|
+
* @param type - The activity type(s) to handle. Can be a string, RegExp, RouteSelector, or array of these types.
|
|
165
|
+
* @param handler - The handler function that will be called when the specified activity type is received.
|
|
166
|
+
* @returns The current instance of the application.
|
|
167
|
+
*
|
|
168
|
+
* @remarks
|
|
169
|
+
* This method allows you to register handlers for specific activity types such as 'message', 'conversationUpdate', etc.
|
|
170
|
+
* You can specify multiple activity types by passing an array.
|
|
171
|
+
*
|
|
172
|
+
* Example usage:
|
|
173
|
+
* ```typescript
|
|
174
|
+
* app.activity(ActivityTypes.Message, async (context, state) => {
|
|
175
|
+
* await context.sendActivity('I received your message');
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
95
179
|
public activity (
|
|
96
180
|
type: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
|
|
97
181
|
handler: (context: TurnContext, state: TState) => Promise<void>
|
|
@@ -103,6 +187,29 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
103
187
|
return this
|
|
104
188
|
}
|
|
105
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Adds a handler for conversation update events.
|
|
192
|
+
*
|
|
193
|
+
* @param event - The conversation update event to handle (e.g., 'membersAdded', 'membersRemoved').
|
|
194
|
+
* @param handler - The handler function that will be called when the specified event occurs.
|
|
195
|
+
* @returns The current instance of the application.
|
|
196
|
+
* @throws Error if the handler is not a function.
|
|
197
|
+
*
|
|
198
|
+
* @remarks
|
|
199
|
+
* Conversation update events occur when the state of a conversation changes, such as when members join or leave.
|
|
200
|
+
*
|
|
201
|
+
* Example usage:
|
|
202
|
+
* ```typescript
|
|
203
|
+
* app.conversationUpdate('membersAdded', async (context, state) => {
|
|
204
|
+
* const membersAdded = context.activity.membersAdded;
|
|
205
|
+
* for (const member of membersAdded) {
|
|
206
|
+
* if (member.id !== context.activity.recipient.id) {
|
|
207
|
+
* await context.sendActivity('Hello and welcome!');
|
|
208
|
+
* }
|
|
209
|
+
* }
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
106
213
|
public conversationUpdate (
|
|
107
214
|
event: ConversationUpdateEvents,
|
|
108
215
|
handler: (context: TurnContext, state: TState) => Promise<void>
|
|
@@ -118,6 +225,13 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
118
225
|
return this
|
|
119
226
|
}
|
|
120
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Continues a conversation asynchronously.
|
|
230
|
+
* @param conversationReferenceOrContext - The conversation reference or turn context.
|
|
231
|
+
* @param logic - The logic to execute during the conversation.
|
|
232
|
+
* @returns A promise that resolves when the conversation logic has completed.
|
|
233
|
+
* @throws Error if the adapter is not configured.
|
|
234
|
+
*/
|
|
121
235
|
protected async continueConversationAsync (
|
|
122
236
|
conversationReferenceOrContext: ConversationReference | TurnContext,
|
|
123
237
|
logic: (context: TurnContext) => Promise<void>
|
|
@@ -143,6 +257,31 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
143
257
|
await this._adapter.continueConversation(reference, logic)
|
|
144
258
|
}
|
|
145
259
|
|
|
260
|
+
/**
|
|
261
|
+
* Adds a handler for message activities that match the specified keyword or pattern.
|
|
262
|
+
*
|
|
263
|
+
* @param keyword - The keyword, pattern, or selector function to match against message text.
|
|
264
|
+
* Can be a string, RegExp, RouteSelector, or array of these types.
|
|
265
|
+
* @param handler - The handler function that will be called when a matching message is received.
|
|
266
|
+
* @returns The current instance of the application.
|
|
267
|
+
*
|
|
268
|
+
* @remarks
|
|
269
|
+
* This method allows you to register handlers for specific message patterns.
|
|
270
|
+
* If keyword is a string, it matches messages containing that string.
|
|
271
|
+
* If keyword is a RegExp, it tests the message text against the regular expression.
|
|
272
|
+
* If keyword is a function, it calls the function with the context to determine if the message matches.
|
|
273
|
+
*
|
|
274
|
+
* Example usage:
|
|
275
|
+
* ```typescript
|
|
276
|
+
* app.message('hello', async (context, state) => {
|
|
277
|
+
* await context.sendActivity('Hello there!');
|
|
278
|
+
* });
|
|
279
|
+
*
|
|
280
|
+
* app.message(/help., async (context, state) => {
|
|
281
|
+
* await context.sendActivity('How can I help you?');
|
|
282
|
+
* });
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
146
285
|
public message (
|
|
147
286
|
keyword: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[],
|
|
148
287
|
handler: (context: TurnContext, state: TState) => Promise<void>
|
|
@@ -154,9 +293,27 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
154
293
|
return this
|
|
155
294
|
}
|
|
156
295
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
296
|
+
/**
|
|
297
|
+
* Sets a handler to be called when a user successfully signs in.
|
|
298
|
+
*
|
|
299
|
+
* @param handler - The handler function to be called after successful sign-in.
|
|
300
|
+
* @returns The current instance of the application.
|
|
301
|
+
* @throws Error if authentication options were not configured.
|
|
302
|
+
*
|
|
303
|
+
* @remarks
|
|
304
|
+
* This method allows you to perform actions after a user has successfully authenticated.
|
|
305
|
+
* The handler will receive the turn context and state.
|
|
306
|
+
*
|
|
307
|
+
* Example usage:
|
|
308
|
+
* ```typescript
|
|
309
|
+
* app.onSignInSuccess(async (context, state) => {
|
|
310
|
+
* await context.sendActivity('You have successfully signed in!');
|
|
311
|
+
* });
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
public onSignInSuccess (handler: (context: TurnContext, state: TurnState, id?: string) => void): this {
|
|
315
|
+
if (this.options.authorization) {
|
|
316
|
+
this.authorization.onSignInSuccess(handler)
|
|
160
317
|
} else {
|
|
161
318
|
throw new Error(
|
|
162
319
|
'The Application.authentication property is unavailable because no authentication options were configured.'
|
|
@@ -165,7 +322,38 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
165
322
|
return this
|
|
166
323
|
}
|
|
167
324
|
|
|
168
|
-
|
|
325
|
+
/**
|
|
326
|
+
* Executes the application logic for a given turn context.
|
|
327
|
+
*
|
|
328
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
329
|
+
* @returns A promise that resolves when the application logic has completed.
|
|
330
|
+
*
|
|
331
|
+
* @remarks
|
|
332
|
+
* This method is the entry point for processing a turn in the conversation.
|
|
333
|
+
* It delegates the actual processing to the `runInternal` method, which handles
|
|
334
|
+
* the core logic for routing and executing handlers.
|
|
335
|
+
*
|
|
336
|
+
* Example usage:
|
|
337
|
+
* ```typescript
|
|
338
|
+
* const app = new AgentApplication();
|
|
339
|
+
* await app.run(turnContext);
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
public async run (turnContext:TurnContext): Promise<void> {
|
|
343
|
+
await this.runInternal(turnContext)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Executes the application logic for a given turn context.
|
|
348
|
+
* @private
|
|
349
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
350
|
+
* @returns A promise that resolves to true if a handler was executed, false otherwise.
|
|
351
|
+
*
|
|
352
|
+
* @remarks
|
|
353
|
+
* This method is the core logic for processing a turn in the conversation.
|
|
354
|
+
* It handles routing and executing handlers based on the activity type and content.
|
|
355
|
+
*/
|
|
356
|
+
public async runInternal (turnContext: TurnContext): Promise<boolean> {
|
|
169
357
|
return await this.startLongRunningCall(turnContext, async (context) => {
|
|
170
358
|
this.startTypingTimer(context)
|
|
171
359
|
try {
|
|
@@ -222,6 +410,27 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
222
410
|
})
|
|
223
411
|
}
|
|
224
412
|
|
|
413
|
+
/**
|
|
414
|
+
* Sends a proactive message to a conversation.
|
|
415
|
+
*
|
|
416
|
+
* @param context - The turn context or conversation reference to use.
|
|
417
|
+
* @param activityOrText - The activity or text to send.
|
|
418
|
+
* @param speak - Optional text to be spoken by the bot on a speech-enabled channel.
|
|
419
|
+
* @param inputHint - Optional input hint for the activity.
|
|
420
|
+
* @returns A promise that resolves to the resource response from sending the activity.
|
|
421
|
+
*
|
|
422
|
+
* @remarks
|
|
423
|
+
* This method allows you to send messages proactively to a conversation, outside the normal turn flow.
|
|
424
|
+
*
|
|
425
|
+
* Example usage:
|
|
426
|
+
* ```typescript
|
|
427
|
+
* // With conversation reference
|
|
428
|
+
* await app.sendProactiveActivity(conversationReference, 'Important notification!');
|
|
429
|
+
*
|
|
430
|
+
* // From an existing context
|
|
431
|
+
* await app.sendProactiveActivity(turnContext, 'Important notification!');
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
225
434
|
public async sendProactiveActivity (
|
|
226
435
|
context: TurnContext | ConversationReference,
|
|
227
436
|
activityOrText: string | Activity,
|
|
@@ -236,6 +445,28 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
236
445
|
return response
|
|
237
446
|
}
|
|
238
447
|
|
|
448
|
+
/**
|
|
449
|
+
* Starts a typing indicator timer for the current turn context.
|
|
450
|
+
*
|
|
451
|
+
* @param context - The turn context for the current conversation.
|
|
452
|
+
* @returns void
|
|
453
|
+
*
|
|
454
|
+
* @remarks
|
|
455
|
+
* This method starts a timer that sends typing activity indicators to the user
|
|
456
|
+
* at regular intervals. The typing indicator continues until a message is sent
|
|
457
|
+
* or the timer is explicitly stopped.
|
|
458
|
+
*
|
|
459
|
+
* The typing indicator helps provide feedback to users that the agent is processing
|
|
460
|
+
* their message, especially when responses might take time to generate.
|
|
461
|
+
*
|
|
462
|
+
* Example usage:
|
|
463
|
+
* ```typescript
|
|
464
|
+
* app.startTypingTimer(turnContext);
|
|
465
|
+
* // Do some processing...
|
|
466
|
+
* await turnContext.sendActivity('Response after processing');
|
|
467
|
+
* // Typing timer automatically stops when sending a message
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
239
470
|
public startTypingTimer (context: TurnContext): void {
|
|
240
471
|
if (context.activity.type === ActivityTypes.Message && !this._typingTimer) {
|
|
241
472
|
let timerRunning = true
|
|
@@ -274,6 +505,23 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
274
505
|
}
|
|
275
506
|
}
|
|
276
507
|
|
|
508
|
+
/**
|
|
509
|
+
* Stops the typing indicator timer if it's currently running.
|
|
510
|
+
*
|
|
511
|
+
* @returns void
|
|
512
|
+
*
|
|
513
|
+
* @remarks
|
|
514
|
+
* This method clears the typing indicator timer to prevent further typing indicators
|
|
515
|
+
* from being sent. It's typically called automatically when a message is sent, but
|
|
516
|
+
* can also be called manually to stop the typing indicator.
|
|
517
|
+
*
|
|
518
|
+
* Example usage:
|
|
519
|
+
* ```typescript
|
|
520
|
+
* app.startTypingTimer(turnContext);
|
|
521
|
+
* // Do some processing...
|
|
522
|
+
* app.stopTypingTimer(); // Manually stop the typing indicator
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
277
525
|
public stopTypingTimer (): void {
|
|
278
526
|
if (this._typingTimer) {
|
|
279
527
|
clearTimeout(this._typingTimer)
|
|
@@ -281,6 +529,26 @@ export class AgentApplication<TState extends TurnState> {
|
|
|
281
529
|
}
|
|
282
530
|
}
|
|
283
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Adds an event handler for specified turn events.
|
|
534
|
+
*
|
|
535
|
+
* @param event - The turn event(s) to handle. Can be 'beforeTurn', 'afterTurn', or other custom events.
|
|
536
|
+
* @param handler - The handler function that will be called when the event occurs.
|
|
537
|
+
* @returns The current instance of the application.
|
|
538
|
+
*
|
|
539
|
+
* @remarks
|
|
540
|
+
* Turn events allow you to execute logic before or after the main turn processing.
|
|
541
|
+
* Handlers added for 'beforeTurn' are executed before routing logic.
|
|
542
|
+
* Handlers added for 'afterTurn' are executed after routing logic.
|
|
543
|
+
*
|
|
544
|
+
* Example usage:
|
|
545
|
+
* ```typescript
|
|
546
|
+
* app.turn('beforeTurn', async (context, state) => {
|
|
547
|
+
* console.log('Processing before turn');
|
|
548
|
+
* return true; // Continue execution
|
|
549
|
+
* });
|
|
550
|
+
* ```
|
|
551
|
+
*/
|
|
284
552
|
public turn (
|
|
285
553
|
event: TurnEvents | TurnEvents[],
|
|
286
554
|
handler: (context: TurnContext, state: TState) => Promise<boolean>
|
|
@@ -7,35 +7,67 @@ import { AgentApplication } from './agentApplication'
|
|
|
7
7
|
import { AgentApplicationOptions } from './agentApplicationOptions'
|
|
8
8
|
import { TurnState } from './turnState'
|
|
9
9
|
import { Storage } from '../storage'
|
|
10
|
-
import {
|
|
10
|
+
import { AuthorizationHandlers } from './oauth/authorization'
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Builder class for creating and configuring AgentApplication instances.
|
|
14
|
+
* @template TState Type extending TurnState that will be used by the application
|
|
15
|
+
*/
|
|
12
16
|
export class AgentApplicationBuilder<TState extends TurnState = TurnState> {
|
|
13
17
|
protected _options: Partial<AgentApplicationOptions<TState>> = {}
|
|
14
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Gets the current options for the AgentApplication being built.
|
|
21
|
+
* @returns The current options object
|
|
22
|
+
*/
|
|
15
23
|
protected get options () {
|
|
16
24
|
return this._options
|
|
17
25
|
}
|
|
18
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Sets the storage provider for the AgentApplication.
|
|
29
|
+
* @param storage The storage implementation to use
|
|
30
|
+
* @returns This builder instance for chaining
|
|
31
|
+
*/
|
|
19
32
|
public withStorage (storage: Storage): this {
|
|
20
33
|
this._options.storage = storage
|
|
21
34
|
return this
|
|
22
35
|
}
|
|
23
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Sets the factory function to create new TurnState instances.
|
|
39
|
+
* @param turnStateFactory Function that creates a new TurnState
|
|
40
|
+
* @returns This builder instance for chaining
|
|
41
|
+
*/
|
|
24
42
|
public withTurnStateFactory (turnStateFactory: () => TState): this {
|
|
25
43
|
this._options.turnStateFactory = turnStateFactory
|
|
26
44
|
return this
|
|
27
45
|
}
|
|
28
46
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Configures whether the agent should display typing indicators.
|
|
49
|
+
* @param startTypingTimer Whether to show typing indicators
|
|
50
|
+
* @returns This builder instance for chaining
|
|
51
|
+
*/
|
|
52
|
+
// public setStartTypingTimer (startTypingTimer: boolean): this {
|
|
53
|
+
// this._options.startTypingTimer = startTypingTimer
|
|
54
|
+
// return this
|
|
55
|
+
// }
|
|
33
56
|
|
|
34
|
-
|
|
35
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Sets authentication options for the AgentApplication.
|
|
59
|
+
* @param authHandlers The user identity authentication options
|
|
60
|
+
* @returns This builder instance for chaining
|
|
61
|
+
*/
|
|
62
|
+
public withAuthorization (authHandlers: AuthorizationHandlers): this {
|
|
63
|
+
this._options.authorization = authHandlers
|
|
36
64
|
return this
|
|
37
65
|
}
|
|
38
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Builds and returns a new AgentApplication instance configured with the provided options.
|
|
69
|
+
* @returns A new AgentApplication instance
|
|
70
|
+
*/
|
|
39
71
|
public build (): AgentApplication<TState> {
|
|
40
72
|
return new AgentApplication(this._options)
|
|
41
73
|
}
|
|
@@ -7,15 +7,46 @@ import { CloudAdapter } from '../cloudAdapter'
|
|
|
7
7
|
import { InputFileDownloader } from './inputFileDownloader'
|
|
8
8
|
import { TurnState } from './turnState'
|
|
9
9
|
import { Storage } from '../storage'
|
|
10
|
-
import {
|
|
10
|
+
import { AuthorizationHandlers } from './oauth/authorization'
|
|
11
11
|
|
|
12
12
|
export interface AgentApplicationOptions<TState extends TurnState> {
|
|
13
|
+
/**
|
|
14
|
+
* The adapter used for handling bot interactions.
|
|
15
|
+
*/
|
|
13
16
|
adapter?: CloudAdapter;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The application ID of the agent.
|
|
20
|
+
*/
|
|
14
21
|
agentAppId?: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The storage mechanism for persisting state.
|
|
25
|
+
*/
|
|
15
26
|
storage?: Storage;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Whether to start a typing timer for the bot.
|
|
30
|
+
*/
|
|
16
31
|
startTypingTimer: boolean;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether to enable long-running messages.
|
|
35
|
+
*/
|
|
17
36
|
longRunningMessages: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A factory function to create the turn state.
|
|
40
|
+
*/
|
|
18
41
|
turnStateFactory: () => TState;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* An array of file downloaders for handling input files.
|
|
45
|
+
*/
|
|
19
46
|
fileDownloaders?: InputFileDownloader<TState>[];
|
|
20
|
-
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Handlers for managing authorization.
|
|
50
|
+
*/
|
|
51
|
+
authorization?: AuthorizationHandlers;
|
|
21
52
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Interface for memory operations that provides a way to store and retrieve values by path.
|
|
8
|
+
* Allows components to persist state data during a conversation.
|
|
9
|
+
*/
|
|
10
|
+
export interface AppMemory {
|
|
11
|
+
/**
|
|
12
|
+
* Deletes a value at the specified path.
|
|
13
|
+
* @param path The path to the value to delete
|
|
14
|
+
*/
|
|
15
|
+
deleteValue(path: string): void;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a value exists at the specified path.
|
|
19
|
+
* @param path The path to check
|
|
20
|
+
* @returns True if a value exists at the path, false otherwise
|
|
21
|
+
*/
|
|
22
|
+
hasValue(path: string): boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Gets a value from the specified path.
|
|
26
|
+
* @template TValue The expected type of the value
|
|
27
|
+
* @param path The path to get the value from
|
|
28
|
+
* @returns The value at the specified path cast to type TValue
|
|
29
|
+
*/
|
|
30
|
+
getValue<TValue = unknown>(path: string): TValue;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sets a value at the specified path.
|
|
34
|
+
* @param path The path where the value should be stored
|
|
35
|
+
* @param value The value to store
|
|
36
|
+
*/
|
|
37
|
+
setValue(path: string, value: unknown): void;
|
|
38
|
+
}
|
package/src/app/appRoute.ts
CHANGED
|
@@ -8,6 +8,13 @@ import { RouteSelector } from './routeSelector'
|
|
|
8
8
|
import { TurnState } from './turnState'
|
|
9
9
|
|
|
10
10
|
export interface AppRoute<TState extends TurnState> {
|
|
11
|
+
/**
|
|
12
|
+
* The selector function used to determine if this route should handle the current activity.
|
|
13
|
+
*/
|
|
11
14
|
selector: RouteSelector;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The handler function that processes the activity if the selector matches.
|
|
18
|
+
*/
|
|
12
19
|
handler: RouteHandler<TState>;
|
|
13
20
|
}
|
|
@@ -14,13 +14,31 @@ import { loadAuthConfigFromEnv, MsalTokenProvider } from '../auth'
|
|
|
14
14
|
|
|
15
15
|
const logger = debug('agents:attachmentDownloader')
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* A utility class for downloading input files from activity attachments.
|
|
19
|
+
*
|
|
20
|
+
* This class provides functionality to filter and download attachments from a turn context,
|
|
21
|
+
* supporting various content types and handling authentication for secure URLs.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam TState - The type of the turn state used in the application.
|
|
24
|
+
*/
|
|
17
25
|
export class AttachmentDownloader<TState extends TurnState = TurnState> implements InputFileDownloader<TState> {
|
|
18
26
|
private _httpClient: AxiosInstance
|
|
19
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Creates an instance of AttachmentDownloader.
|
|
30
|
+
* This class is responsible for downloading input files from attachments.
|
|
31
|
+
*/
|
|
20
32
|
public constructor () {
|
|
21
33
|
this._httpClient = axios.create()
|
|
22
34
|
}
|
|
23
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Downloads files from the attachments in the current turn context.
|
|
38
|
+
* @param context The turn context containing the activity with attachments.
|
|
39
|
+
* @param state The turn state for the current conversation.
|
|
40
|
+
* @returns A promise that resolves to an array of downloaded input files.
|
|
41
|
+
*/
|
|
24
42
|
public async downloadFiles (context: TurnContext, state: TState): Promise<InputFile[]> {
|
|
25
43
|
const attachments = context.activity.attachments?.filter((a) => !a.contentType.startsWith('text/html'))
|
|
26
44
|
if (!attachments || attachments.length === 0) {
|
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents the types of conversation update events that can occur.
|
|
8
|
+
*
|
|
9
|
+
* - `membersAdded`: Triggered when new members are added to the conversation.
|
|
10
|
+
* - `membersRemoved`: Triggered when members are removed from the conversation.
|
|
11
|
+
*/
|
|
6
12
|
export type ConversationUpdateEvents =
|
|
7
13
|
| 'membersAdded'
|
|
8
14
|
| 'membersRemoved'
|
package/src/app/index.ts
CHANGED
|
@@ -3,9 +3,12 @@ export * from './agentApplicationBuilder'
|
|
|
3
3
|
export * from './agentApplicationOptions'
|
|
4
4
|
export * from './appRoute'
|
|
5
5
|
export * from './attachmentDownloader'
|
|
6
|
-
export * from './oauth/
|
|
6
|
+
export * from './oauth/authorization'
|
|
7
7
|
export * from './conversationUpdateEvents'
|
|
8
8
|
export * from './routeHandler'
|
|
9
9
|
export * from './routeSelector'
|
|
10
10
|
export * from './turnState'
|
|
11
|
+
export * from './turnEvents'
|
|
12
|
+
export * from './turnStateEntry'
|
|
11
13
|
export * from './inputFileDownloader'
|
|
14
|
+
export * from './appMemory'
|