@microsoft/agents-hosting 0.2.10-g3ac88ff25e → 0.2.14
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/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 +256 -3
- package/dist/src/app/agentApplication.js +256 -0
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationBuilder.d.ts +32 -0
- package/dist/src/app/agentApplicationBuilder.js +32 -0
- package/dist/src/app/agentApplicationBuilder.js.map +1 -1
- 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/index.d.ts +3 -0
- package/dist/src/app/index.js +3 -0
- package/dist/src/app/index.js.map +1 -1
- package/dist/src/app/turnEvents.d.ts +6 -0
- package/dist/src/app/turnState.d.ts +2 -2
- 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/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/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/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/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/turnContext.d.ts +142 -56
- package/dist/src/turnContext.js +123 -53
- package/dist/src/turnContext.js.map +1 -1
- package/package.json +5 -5
- package/src/activityHandler.ts +304 -46
- package/src/agent-client/agentClient.ts +55 -5
- package/src/app/agentApplication.ts +259 -2
- package/src/app/agentApplicationBuilder.ts +32 -0
- package/src/app/appMemory.ts +38 -0
- package/src/app/index.ts +3 -0
- package/src/app/turnEvents.ts +6 -0
- package/src/app/turnState.ts +2 -2
- package/src/app/turnStateEntry.ts +32 -0
- package/src/cards/index.ts +1 -0
- package/src/cloudAdapter.ts +28 -3
- package/src/getProductInfo.ts +7 -0
- package/src/logger.ts +34 -1
- package/src/state/agentState.ts +81 -29
- package/src/state/agentStatePropertyAccesor.ts +67 -11
- package/src/storage/memoryStorage.ts +48 -14
- package/src/storage/storage.ts +51 -18
- 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/src/app/memory.ts +0 -14
|
@@ -6,10 +6,30 @@ const agents_activity_1 = require("@microsoft/agents-activity");
|
|
|
6
6
|
const uuid_1 = require("uuid");
|
|
7
7
|
const logger_1 = require("../logger");
|
|
8
8
|
const logger = (0, logger_1.debug)('agents:agent-client');
|
|
9
|
+
/**
|
|
10
|
+
* Client for communicating with other agents through HTTP requests.
|
|
11
|
+
* Manages configuration, authentication, and activity exchange with target agents.
|
|
12
|
+
*/
|
|
9
13
|
class AgentClient {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new instance of the AgentClient class.
|
|
16
|
+
*
|
|
17
|
+
* @param agentConfigKey The name of the agent, used to locate configuration in environment variables
|
|
18
|
+
* @throws Error if required configuration is missing
|
|
19
|
+
*/
|
|
10
20
|
constructor(agentConfigKey) {
|
|
11
21
|
this.agentClientConfig = this.loadAgentClientConfig(agentConfigKey);
|
|
12
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Sends an activity to another agent and handles the conversation state.
|
|
25
|
+
*
|
|
26
|
+
* @param activity The activity to send to the target agent
|
|
27
|
+
* @param authConfig Authentication configuration used to obtain access tokens
|
|
28
|
+
* @param conversationState State manager to store conversation data
|
|
29
|
+
* @param context The current turn context
|
|
30
|
+
* @returns A promise that resolves to the HTTP status text of the agent response
|
|
31
|
+
* @throws Error if the request to the agent endpoint fails
|
|
32
|
+
*/
|
|
13
33
|
async postActivity(activity, authConfig, conversationState, context) {
|
|
14
34
|
const activityCopy = activity.clone();
|
|
15
35
|
activityCopy.serviceUrl = this.agentClientConfig.serviceUrl;
|
|
@@ -48,6 +68,14 @@ class AgentClient {
|
|
|
48
68
|
}
|
|
49
69
|
return response.statusText;
|
|
50
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Loads agent configuration from environment variables based on the agent name.
|
|
73
|
+
*
|
|
74
|
+
* @param agentName The name of the agent to load configuration for
|
|
75
|
+
* @returns The agent client configuration
|
|
76
|
+
* @throws Error if any required configuration is missing
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
51
79
|
loadAgentClientConfig(agentName) {
|
|
52
80
|
if (agentName) {
|
|
53
81
|
if (process.env[`${agentName}_endpoint`] !== undefined &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentClient.js","sourceRoot":"","sources":["../../../src/agent-client/agentClient.ts"],"names":[],"mappings":";;;AAAA,kCAA8D;AAC9D,gEAAuF;AACvF,+BAAyB;AACzB,sCAAiC;AAIjC,MAAM,MAAM,GAAG,IAAA,cAAK,EAAC,qBAAqB,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"agentClient.js","sourceRoot":"","sources":["../../../src/ag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AE,CAAC,CAAA;QAC7E,CAAC;QACD,OAAO,QAAQ,CAAC,UAAU,CAAA;IAC5B,CAAC;IAED;;;;;;;OAOG;IACK,qBAAqB,CAAE,SAAiB;QAC9C,IAAI,SAAS,EAAE,CAAC;YACd,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,WAAW,CAAC,KAAK,SAAS;gBACpD,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,WAAW,CAAC,KAAK,SAAS;gBAClD,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,aAAa,CAAC,KAAK,SAAS,EAAE,CAAC;gBACvD,OAAO;oBACL,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,WAAW,CAAE;oBAC/C,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,WAAW,CAAE;oBAC/C,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,SAAS,aAAa,CAAE;iBACpD,CAAA;YACH,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,KAAK,CAAC,yCAAyC,SAAS,EAAE,CAAC,CAAA;YACvE,CAAC;QACH,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAA;QAC3C,CAAC;IACH,CAAC;CACF;AAhGD,kCAgGC"}
|
|
@@ -14,7 +14,24 @@ import { AppRoute } from './appRoute';
|
|
|
14
14
|
import { TurnContext } from '../turnContext';
|
|
15
15
|
import { ResourceResponse } from '../connector-client';
|
|
16
16
|
import { UserIdentity } from './oauth/userIdentity';
|
|
17
|
-
type ApplicationEventHandler<TState extends TurnState> = (context: TurnContext, state: TState) => Promise<boolean>;
|
|
17
|
+
export type ApplicationEventHandler<TState extends TurnState> = (context: TurnContext, state: TState) => Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Executes the application logic for a given turn context.
|
|
20
|
+
*
|
|
21
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
22
|
+
* @returns A promise that resolves when the application logic has completed.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* This method is the entry point for processing a turn in the conversation.
|
|
26
|
+
* It delegates the actual processing to the `runInternal` method, which handles
|
|
27
|
+
* the core logic for routing and executing handlers.
|
|
28
|
+
*
|
|
29
|
+
* Example usage:
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const app = new AgentApplication();
|
|
32
|
+
* await app.run(turnContext);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
18
35
|
export declare class AgentApplication<TState extends TurnState> {
|
|
19
36
|
protected readonly _options: AgentApplicationOptions<TState>;
|
|
20
37
|
protected readonly _routes: AppRoute<TState>[];
|
|
@@ -27,17 +44,254 @@ export declare class AgentApplication<TState extends TurnState> {
|
|
|
27
44
|
get adapter(): BaseAdapter;
|
|
28
45
|
get userIdentity(): UserIdentity;
|
|
29
46
|
get options(): AgentApplicationOptions<TState>;
|
|
47
|
+
/**
|
|
48
|
+
* Sets an error handler for the application.
|
|
49
|
+
*
|
|
50
|
+
* @param handler - The error handler function to be called when an error occurs.
|
|
51
|
+
* @returns The current instance of the application.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* This method allows you to handle any errors that occur during turn processing.
|
|
55
|
+
* The handler will receive the turn context and the error that occurred.
|
|
56
|
+
*
|
|
57
|
+
* Example usage:
|
|
58
|
+
* ```typescript
|
|
59
|
+
* app.error(async (context, error) => {
|
|
60
|
+
* console.error(`An error occurred: ${error.message}`);
|
|
61
|
+
* await context.sendActivity('Sorry, something went wrong!');
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
30
65
|
error(handler: (context: TurnContext, error: Error) => Promise<void>): this;
|
|
66
|
+
/**
|
|
67
|
+
* Adds a new route to the application for handling activities.
|
|
68
|
+
*
|
|
69
|
+
* @param selector - The selector function that determines if a route should handle the current activity.
|
|
70
|
+
* @param handler - The handler function that will be called if the selector returns true.
|
|
71
|
+
* @returns The current instance of the application.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* Routes are evaluated in the order they are added. The first route with a selector that returns true will be used.
|
|
75
|
+
*
|
|
76
|
+
* Example usage:
|
|
77
|
+
* ```typescript
|
|
78
|
+
* app.addRoute(
|
|
79
|
+
* async (context) => context.activity.type === ActivityTypes.Message,
|
|
80
|
+
* async (context, state) => {
|
|
81
|
+
* await context.sendActivity('I received your message');
|
|
82
|
+
* }
|
|
83
|
+
* );
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
31
86
|
addRoute(selector: RouteSelector, handler: RouteHandler<TState>): this;
|
|
87
|
+
/**
|
|
88
|
+
* Adds a handler for specific activity types.
|
|
89
|
+
*
|
|
90
|
+
* @param type - The activity type(s) to handle. Can be a string, RegExp, RouteSelector, or array of these types.
|
|
91
|
+
* @param handler - The handler function that will be called when the specified activity type is received.
|
|
92
|
+
* @returns The current instance of the application.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* This method allows you to register handlers for specific activity types such as 'message', 'conversationUpdate', etc.
|
|
96
|
+
* You can specify multiple activity types by passing an array.
|
|
97
|
+
*
|
|
98
|
+
* Example usage:
|
|
99
|
+
* ```typescript
|
|
100
|
+
* app.activity(ActivityTypes.Message, async (context, state) => {
|
|
101
|
+
* await context.sendActivity('I received your message');
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
32
105
|
activity(type: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[], handler: (context: TurnContext, state: TState) => Promise<void>): this;
|
|
106
|
+
/**
|
|
107
|
+
* Adds a handler for conversation update events.
|
|
108
|
+
*
|
|
109
|
+
* @param event - The conversation update event to handle (e.g., 'membersAdded', 'membersRemoved').
|
|
110
|
+
* @param handler - The handler function that will be called when the specified event occurs.
|
|
111
|
+
* @returns The current instance of the application.
|
|
112
|
+
* @throws Error if the handler is not a function.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* Conversation update events occur when the state of a conversation changes, such as when members join or leave.
|
|
116
|
+
*
|
|
117
|
+
* Example usage:
|
|
118
|
+
* ```typescript
|
|
119
|
+
* app.conversationUpdate('membersAdded', async (context, state) => {
|
|
120
|
+
* const membersAdded = context.activity.membersAdded;
|
|
121
|
+
* for (const member of membersAdded) {
|
|
122
|
+
* if (member.id !== context.activity.recipient.id) {
|
|
123
|
+
* await context.sendActivity('Hello and welcome!');
|
|
124
|
+
* }
|
|
125
|
+
* }
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
33
129
|
conversationUpdate(event: ConversationUpdateEvents, handler: (context: TurnContext, state: TState) => Promise<void>): this;
|
|
130
|
+
/**
|
|
131
|
+
* Continues a conversation asynchronously.
|
|
132
|
+
* @param conversationReferenceOrContext - The conversation reference or turn context.
|
|
133
|
+
* @param logic - The logic to execute during the conversation.
|
|
134
|
+
* @returns A promise that resolves when the conversation logic has completed.
|
|
135
|
+
* @throws Error if the adapter is not configured.
|
|
136
|
+
*/
|
|
34
137
|
protected continueConversationAsync(conversationReferenceOrContext: ConversationReference | TurnContext, logic: (context: TurnContext) => Promise<void>): Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* Adds a handler for message activities that match the specified keyword or pattern.
|
|
140
|
+
*
|
|
141
|
+
* @param keyword - The keyword, pattern, or selector function to match against message text.
|
|
142
|
+
* Can be a string, RegExp, RouteSelector, or array of these types.
|
|
143
|
+
* @param handler - The handler function that will be called when a matching message is received.
|
|
144
|
+
* @returns The current instance of the application.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* This method allows you to register handlers for specific message patterns.
|
|
148
|
+
* If keyword is a string, it matches messages containing that string.
|
|
149
|
+
* If keyword is a RegExp, it tests the message text against the regular expression.
|
|
150
|
+
* If keyword is a function, it calls the function with the context to determine if the message matches.
|
|
151
|
+
*
|
|
152
|
+
* Example usage:
|
|
153
|
+
* ```typescript
|
|
154
|
+
* app.message('hello', async (context, state) => {
|
|
155
|
+
* await context.sendActivity('Hello there!');
|
|
156
|
+
* });
|
|
157
|
+
*
|
|
158
|
+
* app.message(/help., async (context, state) => {
|
|
159
|
+
* await context.sendActivity('How can I help you?');
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
35
163
|
message(keyword: string | RegExp | RouteSelector | (string | RegExp | RouteSelector)[], handler: (context: TurnContext, state: TState) => Promise<void>): this;
|
|
164
|
+
/**
|
|
165
|
+
* Sets a handler to be called when a user successfully signs in.
|
|
166
|
+
*
|
|
167
|
+
* @param handler - The handler function to be called after successful sign-in.
|
|
168
|
+
* @returns The current instance of the application.
|
|
169
|
+
* @throws Error if authentication options were not configured.
|
|
170
|
+
*
|
|
171
|
+
* @remarks
|
|
172
|
+
* This method allows you to perform actions after a user has successfully authenticated.
|
|
173
|
+
* The handler will receive the turn context and state.
|
|
174
|
+
*
|
|
175
|
+
* Example usage:
|
|
176
|
+
* ```typescript
|
|
177
|
+
* app.onSignInSuccess(async (context, state) => {
|
|
178
|
+
* await context.sendActivity('You have successfully signed in!');
|
|
179
|
+
* });
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
36
182
|
onSignInSuccess(handler: (context: TurnContext, state: TurnState) => void): this;
|
|
37
|
-
|
|
183
|
+
/**
|
|
184
|
+
* Executes the application logic for a given turn context.
|
|
185
|
+
*
|
|
186
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
187
|
+
* @returns A promise that resolves when the application logic has completed.
|
|
188
|
+
*
|
|
189
|
+
* @remarks
|
|
190
|
+
* This method is the entry point for processing a turn in the conversation.
|
|
191
|
+
* It delegates the actual processing to the `runInternal` method, which handles
|
|
192
|
+
* the core logic for routing and executing handlers.
|
|
193
|
+
*
|
|
194
|
+
* Example usage:
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const app = new AgentApplication();
|
|
197
|
+
* await app.run(turnContext);
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
run(turnContext: TurnContext): Promise<void>;
|
|
201
|
+
/**
|
|
202
|
+
* Executes the application logic for a given turn context.
|
|
203
|
+
* @private
|
|
204
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
205
|
+
* @returns A promise that resolves to true if a handler was executed, false otherwise.
|
|
206
|
+
*
|
|
207
|
+
* @remarks
|
|
208
|
+
* This method is the core logic for processing a turn in the conversation.
|
|
209
|
+
* It handles routing and executing handlers based on the activity type and content.
|
|
210
|
+
*/
|
|
211
|
+
runInternal(turnContext: TurnContext): Promise<boolean>;
|
|
212
|
+
/**
|
|
213
|
+
* Sends a proactive message to a conversation.
|
|
214
|
+
*
|
|
215
|
+
* @param context - The turn context or conversation reference to use.
|
|
216
|
+
* @param activityOrText - The activity or text to send.
|
|
217
|
+
* @param speak - Optional text to be spoken by the bot on a speech-enabled channel.
|
|
218
|
+
* @param inputHint - Optional input hint for the activity.
|
|
219
|
+
* @returns A promise that resolves to the resource response from sending the activity.
|
|
220
|
+
*
|
|
221
|
+
* @remarks
|
|
222
|
+
* This method allows you to send messages proactively to a conversation, outside the normal turn flow.
|
|
223
|
+
*
|
|
224
|
+
* Example usage:
|
|
225
|
+
* ```typescript
|
|
226
|
+
* // With conversation reference
|
|
227
|
+
* await app.sendProactiveActivity(conversationReference, 'Important notification!');
|
|
228
|
+
*
|
|
229
|
+
* // From an existing context
|
|
230
|
+
* await app.sendProactiveActivity(turnContext, 'Important notification!');
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
38
233
|
sendProactiveActivity(context: TurnContext | ConversationReference, activityOrText: string | Activity, speak?: string, inputHint?: string): Promise<ResourceResponse | undefined>;
|
|
234
|
+
/**
|
|
235
|
+
* Starts a typing indicator timer for the current turn context.
|
|
236
|
+
*
|
|
237
|
+
* @param context - The turn context for the current conversation.
|
|
238
|
+
* @returns void
|
|
239
|
+
*
|
|
240
|
+
* @remarks
|
|
241
|
+
* This method starts a timer that sends typing activity indicators to the user
|
|
242
|
+
* at regular intervals. The typing indicator continues until a message is sent
|
|
243
|
+
* or the timer is explicitly stopped.
|
|
244
|
+
*
|
|
245
|
+
* The typing indicator helps provide feedback to users that the agent is processing
|
|
246
|
+
* their message, especially when responses might take time to generate.
|
|
247
|
+
*
|
|
248
|
+
* Example usage:
|
|
249
|
+
* ```typescript
|
|
250
|
+
* app.startTypingTimer(turnContext);
|
|
251
|
+
* // Do some processing...
|
|
252
|
+
* await turnContext.sendActivity('Response after processing');
|
|
253
|
+
* // Typing timer automatically stops when sending a message
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
39
256
|
startTypingTimer(context: TurnContext): void;
|
|
257
|
+
/**
|
|
258
|
+
* Stops the typing indicator timer if it's currently running.
|
|
259
|
+
*
|
|
260
|
+
* @returns void
|
|
261
|
+
*
|
|
262
|
+
* @remarks
|
|
263
|
+
* This method clears the typing indicator timer to prevent further typing indicators
|
|
264
|
+
* from being sent. It's typically called automatically when a message is sent, but
|
|
265
|
+
* can also be called manually to stop the typing indicator.
|
|
266
|
+
*
|
|
267
|
+
* Example usage:
|
|
268
|
+
* ```typescript
|
|
269
|
+
* app.startTypingTimer(turnContext);
|
|
270
|
+
* // Do some processing...
|
|
271
|
+
* app.stopTypingTimer(); // Manually stop the typing indicator
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
40
274
|
stopTypingTimer(): void;
|
|
275
|
+
/**
|
|
276
|
+
* Adds an event handler for specified turn events.
|
|
277
|
+
*
|
|
278
|
+
* @param event - The turn event(s) to handle. Can be 'beforeTurn', 'afterTurn', or other custom events.
|
|
279
|
+
* @param handler - The handler function that will be called when the event occurs.
|
|
280
|
+
* @returns The current instance of the application.
|
|
281
|
+
*
|
|
282
|
+
* @remarks
|
|
283
|
+
* Turn events allow you to execute logic before or after the main turn processing.
|
|
284
|
+
* Handlers added for 'beforeTurn' are executed before routing logic.
|
|
285
|
+
* Handlers added for 'afterTurn' are executed after routing logic.
|
|
286
|
+
*
|
|
287
|
+
* Example usage:
|
|
288
|
+
* ```typescript
|
|
289
|
+
* app.turn('beforeTurn', async (context, state) => {
|
|
290
|
+
* console.log('Processing before turn');
|
|
291
|
+
* return true; // Continue execution
|
|
292
|
+
* });
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
41
295
|
turn(event: TurnEvents | TurnEvents[], handler: (context: TurnContext, state: TState) => Promise<boolean>): this;
|
|
42
296
|
protected callEventHandlers(context: TurnContext, state: TState, handlers: ApplicationEventHandler<TState>[]): Promise<boolean>;
|
|
43
297
|
protected startLongRunningCall(context: TurnContext, handler: (context: TurnContext) => Promise<boolean>): Promise<boolean>;
|
|
@@ -45,4 +299,3 @@ export declare class AgentApplication<TState extends TurnState> {
|
|
|
45
299
|
private createConversationUpdateSelector;
|
|
46
300
|
private createMessageSelector;
|
|
47
301
|
}
|
|
48
|
-
export {};
|
|
@@ -12,6 +12,23 @@ const userIdentity_1 = require("./oauth/userIdentity");
|
|
|
12
12
|
const storage_1 = require("../storage");
|
|
13
13
|
const logger = (0, logger_1.debug)('agents:agent-application');
|
|
14
14
|
const TYPING_TIMER_DELAY = 1000;
|
|
15
|
+
/**
|
|
16
|
+
* Executes the application logic for a given turn context.
|
|
17
|
+
*
|
|
18
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
19
|
+
* @returns A promise that resolves when the application logic has completed.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* This method is the entry point for processing a turn in the conversation.
|
|
23
|
+
* It delegates the actual processing to the `runInternal` method, which handles
|
|
24
|
+
* the core logic for routing and executing handlers.
|
|
25
|
+
*
|
|
26
|
+
* Example usage:
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const app = new AgentApplication();
|
|
29
|
+
* await app.run(turnContext);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
15
32
|
class AgentApplication {
|
|
16
33
|
constructor(options) {
|
|
17
34
|
var _a;
|
|
@@ -49,16 +66,72 @@ class AgentApplication {
|
|
|
49
66
|
get options() {
|
|
50
67
|
return this._options;
|
|
51
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Sets an error handler for the application.
|
|
71
|
+
*
|
|
72
|
+
* @param handler - The error handler function to be called when an error occurs.
|
|
73
|
+
* @returns The current instance of the application.
|
|
74
|
+
*
|
|
75
|
+
* @remarks
|
|
76
|
+
* This method allows you to handle any errors that occur during turn processing.
|
|
77
|
+
* The handler will receive the turn context and the error that occurred.
|
|
78
|
+
*
|
|
79
|
+
* Example usage:
|
|
80
|
+
* ```typescript
|
|
81
|
+
* app.error(async (context, error) => {
|
|
82
|
+
* console.error(`An error occurred: ${error.message}`);
|
|
83
|
+
* await context.sendActivity('Sorry, something went wrong!');
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
52
87
|
error(handler) {
|
|
53
88
|
if (this._adapter) {
|
|
54
89
|
this._adapter.onTurnError = handler;
|
|
55
90
|
}
|
|
56
91
|
return this;
|
|
57
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Adds a new route to the application for handling activities.
|
|
95
|
+
*
|
|
96
|
+
* @param selector - The selector function that determines if a route should handle the current activity.
|
|
97
|
+
* @param handler - The handler function that will be called if the selector returns true.
|
|
98
|
+
* @returns The current instance of the application.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Routes are evaluated in the order they are added. The first route with a selector that returns true will be used.
|
|
102
|
+
*
|
|
103
|
+
* Example usage:
|
|
104
|
+
* ```typescript
|
|
105
|
+
* app.addRoute(
|
|
106
|
+
* async (context) => context.activity.type === ActivityTypes.Message,
|
|
107
|
+
* async (context, state) => {
|
|
108
|
+
* await context.sendActivity('I received your message');
|
|
109
|
+
* }
|
|
110
|
+
* );
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
58
113
|
addRoute(selector, handler) {
|
|
59
114
|
this._routes.push({ selector, handler });
|
|
60
115
|
return this;
|
|
61
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds a handler for specific activity types.
|
|
119
|
+
*
|
|
120
|
+
* @param type - The activity type(s) to handle. Can be a string, RegExp, RouteSelector, or array of these types.
|
|
121
|
+
* @param handler - The handler function that will be called when the specified activity type is received.
|
|
122
|
+
* @returns The current instance of the application.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* This method allows you to register handlers for specific activity types such as 'message', 'conversationUpdate', etc.
|
|
126
|
+
* You can specify multiple activity types by passing an array.
|
|
127
|
+
*
|
|
128
|
+
* Example usage:
|
|
129
|
+
* ```typescript
|
|
130
|
+
* app.activity(ActivityTypes.Message, async (context, state) => {
|
|
131
|
+
* await context.sendActivity('I received your message');
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
62
135
|
activity(type, handler) {
|
|
63
136
|
(Array.isArray(type) ? type : [type]).forEach((t) => {
|
|
64
137
|
const selector = this.createActivitySelector(t);
|
|
@@ -66,6 +139,29 @@ class AgentApplication {
|
|
|
66
139
|
});
|
|
67
140
|
return this;
|
|
68
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Adds a handler for conversation update events.
|
|
144
|
+
*
|
|
145
|
+
* @param event - The conversation update event to handle (e.g., 'membersAdded', 'membersRemoved').
|
|
146
|
+
* @param handler - The handler function that will be called when the specified event occurs.
|
|
147
|
+
* @returns The current instance of the application.
|
|
148
|
+
* @throws Error if the handler is not a function.
|
|
149
|
+
*
|
|
150
|
+
* @remarks
|
|
151
|
+
* Conversation update events occur when the state of a conversation changes, such as when members join or leave.
|
|
152
|
+
*
|
|
153
|
+
* Example usage:
|
|
154
|
+
* ```typescript
|
|
155
|
+
* app.conversationUpdate('membersAdded', async (context, state) => {
|
|
156
|
+
* const membersAdded = context.activity.membersAdded;
|
|
157
|
+
* for (const member of membersAdded) {
|
|
158
|
+
* if (member.id !== context.activity.recipient.id) {
|
|
159
|
+
* await context.sendActivity('Hello and welcome!');
|
|
160
|
+
* }
|
|
161
|
+
* }
|
|
162
|
+
* });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
69
165
|
conversationUpdate(event, handler) {
|
|
70
166
|
if (typeof handler !== 'function') {
|
|
71
167
|
throw new Error(`ConversationUpdate 'handler' for ${event} is ${typeof handler}. Type of 'handler' must be a function.`);
|
|
@@ -74,6 +170,13 @@ class AgentApplication {
|
|
|
74
170
|
this.addRoute(selector, handler);
|
|
75
171
|
return this;
|
|
76
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Continues a conversation asynchronously.
|
|
175
|
+
* @param conversationReferenceOrContext - The conversation reference or turn context.
|
|
176
|
+
* @param logic - The logic to execute during the conversation.
|
|
177
|
+
* @returns A promise that resolves when the conversation logic has completed.
|
|
178
|
+
* @throws Error if the adapter is not configured.
|
|
179
|
+
*/
|
|
77
180
|
async continueConversationAsync(conversationReferenceOrContext, logic) {
|
|
78
181
|
if (!this._adapter) {
|
|
79
182
|
throw new Error("You must configure the Application with an 'adapter' before calling Application.continueConversationAsync()");
|
|
@@ -90,6 +193,31 @@ class AgentApplication {
|
|
|
90
193
|
}
|
|
91
194
|
await this._adapter.continueConversation(reference, logic);
|
|
92
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Adds a handler for message activities that match the specified keyword or pattern.
|
|
198
|
+
*
|
|
199
|
+
* @param keyword - The keyword, pattern, or selector function to match against message text.
|
|
200
|
+
* Can be a string, RegExp, RouteSelector, or array of these types.
|
|
201
|
+
* @param handler - The handler function that will be called when a matching message is received.
|
|
202
|
+
* @returns The current instance of the application.
|
|
203
|
+
*
|
|
204
|
+
* @remarks
|
|
205
|
+
* This method allows you to register handlers for specific message patterns.
|
|
206
|
+
* If keyword is a string, it matches messages containing that string.
|
|
207
|
+
* If keyword is a RegExp, it tests the message text against the regular expression.
|
|
208
|
+
* If keyword is a function, it calls the function with the context to determine if the message matches.
|
|
209
|
+
*
|
|
210
|
+
* Example usage:
|
|
211
|
+
* ```typescript
|
|
212
|
+
* app.message('hello', async (context, state) => {
|
|
213
|
+
* await context.sendActivity('Hello there!');
|
|
214
|
+
* });
|
|
215
|
+
*
|
|
216
|
+
* app.message(/help., async (context, state) => {
|
|
217
|
+
* await context.sendActivity('How can I help you?');
|
|
218
|
+
* });
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
93
221
|
message(keyword, handler) {
|
|
94
222
|
(Array.isArray(keyword) ? keyword : [keyword]).forEach((k) => {
|
|
95
223
|
const selector = this.createMessageSelector(k);
|
|
@@ -97,6 +225,24 @@ class AgentApplication {
|
|
|
97
225
|
});
|
|
98
226
|
return this;
|
|
99
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Sets a handler to be called when a user successfully signs in.
|
|
230
|
+
*
|
|
231
|
+
* @param handler - The handler function to be called after successful sign-in.
|
|
232
|
+
* @returns The current instance of the application.
|
|
233
|
+
* @throws Error if authentication options were not configured.
|
|
234
|
+
*
|
|
235
|
+
* @remarks
|
|
236
|
+
* This method allows you to perform actions after a user has successfully authenticated.
|
|
237
|
+
* The handler will receive the turn context and state.
|
|
238
|
+
*
|
|
239
|
+
* Example usage:
|
|
240
|
+
* ```typescript
|
|
241
|
+
* app.onSignInSuccess(async (context, state) => {
|
|
242
|
+
* await context.sendActivity('You have successfully signed in!');
|
|
243
|
+
* });
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
100
246
|
onSignInSuccess(handler) {
|
|
101
247
|
if (this._userIdentity) {
|
|
102
248
|
this._userIdentity.onSignInSuccess(handler);
|
|
@@ -106,7 +252,37 @@ class AgentApplication {
|
|
|
106
252
|
}
|
|
107
253
|
return this;
|
|
108
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* Executes the application logic for a given turn context.
|
|
257
|
+
*
|
|
258
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
259
|
+
* @returns A promise that resolves when the application logic has completed.
|
|
260
|
+
*
|
|
261
|
+
* @remarks
|
|
262
|
+
* This method is the entry point for processing a turn in the conversation.
|
|
263
|
+
* It delegates the actual processing to the `runInternal` method, which handles
|
|
264
|
+
* the core logic for routing and executing handlers.
|
|
265
|
+
*
|
|
266
|
+
* Example usage:
|
|
267
|
+
* ```typescript
|
|
268
|
+
* const app = new AgentApplication();
|
|
269
|
+
* await app.run(turnContext);
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
109
272
|
async run(turnContext) {
|
|
273
|
+
await this.runInternal(turnContext);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Executes the application logic for a given turn context.
|
|
277
|
+
* @private
|
|
278
|
+
* @param turnContext - The context for the current turn of the conversation.
|
|
279
|
+
* @returns A promise that resolves to true if a handler was executed, false otherwise.
|
|
280
|
+
*
|
|
281
|
+
* @remarks
|
|
282
|
+
* This method is the core logic for processing a turn in the conversation.
|
|
283
|
+
* It handles routing and executing handlers based on the activity type and content.
|
|
284
|
+
*/
|
|
285
|
+
async runInternal(turnContext) {
|
|
110
286
|
return await this.startLongRunningCall(turnContext, async (context) => {
|
|
111
287
|
var _a, _b;
|
|
112
288
|
this.startTypingTimer(context);
|
|
@@ -156,6 +332,27 @@ class AgentApplication {
|
|
|
156
332
|
}
|
|
157
333
|
});
|
|
158
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* Sends a proactive message to a conversation.
|
|
337
|
+
*
|
|
338
|
+
* @param context - The turn context or conversation reference to use.
|
|
339
|
+
* @param activityOrText - The activity or text to send.
|
|
340
|
+
* @param speak - Optional text to be spoken by the bot on a speech-enabled channel.
|
|
341
|
+
* @param inputHint - Optional input hint for the activity.
|
|
342
|
+
* @returns A promise that resolves to the resource response from sending the activity.
|
|
343
|
+
*
|
|
344
|
+
* @remarks
|
|
345
|
+
* This method allows you to send messages proactively to a conversation, outside the normal turn flow.
|
|
346
|
+
*
|
|
347
|
+
* Example usage:
|
|
348
|
+
* ```typescript
|
|
349
|
+
* // With conversation reference
|
|
350
|
+
* await app.sendProactiveActivity(conversationReference, 'Important notification!');
|
|
351
|
+
*
|
|
352
|
+
* // From an existing context
|
|
353
|
+
* await app.sendProactiveActivity(turnContext, 'Important notification!');
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
159
356
|
async sendProactiveActivity(context, activityOrText, speak, inputHint) {
|
|
160
357
|
let response;
|
|
161
358
|
await this.continueConversationAsync(context, async (ctx) => {
|
|
@@ -163,6 +360,28 @@ class AgentApplication {
|
|
|
163
360
|
});
|
|
164
361
|
return response;
|
|
165
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Starts a typing indicator timer for the current turn context.
|
|
365
|
+
*
|
|
366
|
+
* @param context - The turn context for the current conversation.
|
|
367
|
+
* @returns void
|
|
368
|
+
*
|
|
369
|
+
* @remarks
|
|
370
|
+
* This method starts a timer that sends typing activity indicators to the user
|
|
371
|
+
* at regular intervals. The typing indicator continues until a message is sent
|
|
372
|
+
* or the timer is explicitly stopped.
|
|
373
|
+
*
|
|
374
|
+
* The typing indicator helps provide feedback to users that the agent is processing
|
|
375
|
+
* their message, especially when responses might take time to generate.
|
|
376
|
+
*
|
|
377
|
+
* Example usage:
|
|
378
|
+
* ```typescript
|
|
379
|
+
* app.startTypingTimer(turnContext);
|
|
380
|
+
* // Do some processing...
|
|
381
|
+
* await turnContext.sendActivity('Response after processing');
|
|
382
|
+
* // Typing timer automatically stops when sending a message
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
166
385
|
startTypingTimer(context) {
|
|
167
386
|
if (context.activity.type === agents_activity_1.ActivityTypes.Message && !this._typingTimer) {
|
|
168
387
|
let timerRunning = true;
|
|
@@ -199,12 +418,49 @@ class AgentApplication {
|
|
|
199
418
|
this._typingTimer = setTimeout(onTimeout, TYPING_TIMER_DELAY);
|
|
200
419
|
}
|
|
201
420
|
}
|
|
421
|
+
/**
|
|
422
|
+
* Stops the typing indicator timer if it's currently running.
|
|
423
|
+
*
|
|
424
|
+
* @returns void
|
|
425
|
+
*
|
|
426
|
+
* @remarks
|
|
427
|
+
* This method clears the typing indicator timer to prevent further typing indicators
|
|
428
|
+
* from being sent. It's typically called automatically when a message is sent, but
|
|
429
|
+
* can also be called manually to stop the typing indicator.
|
|
430
|
+
*
|
|
431
|
+
* Example usage:
|
|
432
|
+
* ```typescript
|
|
433
|
+
* app.startTypingTimer(turnContext);
|
|
434
|
+
* // Do some processing...
|
|
435
|
+
* app.stopTypingTimer(); // Manually stop the typing indicator
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
202
438
|
stopTypingTimer() {
|
|
203
439
|
if (this._typingTimer) {
|
|
204
440
|
clearTimeout(this._typingTimer);
|
|
205
441
|
this._typingTimer = undefined;
|
|
206
442
|
}
|
|
207
443
|
}
|
|
444
|
+
/**
|
|
445
|
+
* Adds an event handler for specified turn events.
|
|
446
|
+
*
|
|
447
|
+
* @param event - The turn event(s) to handle. Can be 'beforeTurn', 'afterTurn', or other custom events.
|
|
448
|
+
* @param handler - The handler function that will be called when the event occurs.
|
|
449
|
+
* @returns The current instance of the application.
|
|
450
|
+
*
|
|
451
|
+
* @remarks
|
|
452
|
+
* Turn events allow you to execute logic before or after the main turn processing.
|
|
453
|
+
* Handlers added for 'beforeTurn' are executed before routing logic.
|
|
454
|
+
* Handlers added for 'afterTurn' are executed after routing logic.
|
|
455
|
+
*
|
|
456
|
+
* Example usage:
|
|
457
|
+
* ```typescript
|
|
458
|
+
* app.turn('beforeTurn', async (context, state) => {
|
|
459
|
+
* console.log('Processing before turn');
|
|
460
|
+
* return true; // Continue execution
|
|
461
|
+
* });
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
208
464
|
turn(event, handler) {
|
|
209
465
|
(Array.isArray(event) ? event : [event]).forEach((e) => {
|
|
210
466
|
switch (e) {
|