@microsoft/agents-hosting 0.2.9-g361635b71c → 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
package/src/turnContext.ts
CHANGED
|
@@ -8,22 +8,37 @@ import { AttachmentInfo } from './connector-client/attachmentInfo'
|
|
|
8
8
|
import { AttachmentData } from './connector-client/attachmentData'
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Defines a handler for sending activities.
|
|
11
|
+
* Defines a handler for processing and sending activities.
|
|
12
|
+
* Used for middleware that needs to intercept or modify activities being sent.
|
|
13
|
+
*
|
|
14
|
+
* @param context The current turn context
|
|
15
|
+
* @param activities The activities being sent
|
|
16
|
+
* @param next Function to call to continue the middleware chain
|
|
12
17
|
*/
|
|
13
18
|
export type SendActivitiesHandler = (context: TurnContext, activities: Activity[], next: () => Promise<ResourceResponse[]>) => Promise<ResourceResponse[]>
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
21
|
* Defines a handler for updating an activity.
|
|
22
|
+
* Used for middleware that needs to intercept or modify activity updates.
|
|
23
|
+
*
|
|
24
|
+
* @param context The current turn context
|
|
25
|
+
* @param activity The activity being updated
|
|
26
|
+
* @param next Function to call to continue the middleware chain
|
|
17
27
|
*/
|
|
18
28
|
export type UpdateActivityHandler = (context: TurnContext, activity: Activity, next: () => Promise<void>) => Promise<void>
|
|
19
29
|
|
|
20
30
|
/**
|
|
21
31
|
* Defines a handler for deleting an activity.
|
|
32
|
+
* Used for middleware that needs to intercept or handle activity deletions.
|
|
33
|
+
*
|
|
34
|
+
* @param context The current turn context
|
|
35
|
+
* @param reference Reference to the activity being deleted
|
|
36
|
+
* @param next Function to call to continue the middleware chain
|
|
22
37
|
*/
|
|
23
38
|
export type DeleteActivityHandler = (context: TurnContext, reference: ConversationReference, next: () => Promise<void>) => Promise<void>
|
|
24
39
|
|
|
25
40
|
/**
|
|
26
|
-
* Key for the agent callback handler.
|
|
41
|
+
* Key for the agent callback handler in TurnState collection.
|
|
27
42
|
*/
|
|
28
43
|
export const AgentCallbackHandlerKey = 'agentCallbackHandler'
|
|
29
44
|
|
|
@@ -33,7 +48,20 @@ export const AgentCallbackHandlerKey = 'agentCallbackHandler'
|
|
|
33
48
|
export interface TurnContext {}
|
|
34
49
|
|
|
35
50
|
/**
|
|
36
|
-
* Represents the context
|
|
51
|
+
* Represents the context for a single turn in a conversation between a user and an agent.
|
|
52
|
+
*
|
|
53
|
+
* TurnContext is a central concept in the Agents framework - it contains:
|
|
54
|
+
* - The incoming activity that started the turn
|
|
55
|
+
* - Access to the adapter that can be used to send responses
|
|
56
|
+
* - A state collection for storing information during the turn
|
|
57
|
+
* - Methods for sending, updating, and deleting activities
|
|
58
|
+
* - Middleware hooks for intercepting activity operations
|
|
59
|
+
*
|
|
60
|
+
* The TurnContext object is created by the adapter when an activity is received
|
|
61
|
+
* and is passed to the agent's logic to process the turn. It maintains information
|
|
62
|
+
* about the conversation and provides methods to send responses.
|
|
63
|
+
*
|
|
64
|
+
* This class follows the builder pattern for registering middleware handlers.
|
|
37
65
|
*/
|
|
38
66
|
export class TurnContext {
|
|
39
67
|
private readonly _adapter?: BaseAdapter
|
|
@@ -48,8 +76,9 @@ export class TurnContext {
|
|
|
48
76
|
|
|
49
77
|
/**
|
|
50
78
|
* Initializes a new instance of the TurnContext class.
|
|
51
|
-
*
|
|
52
|
-
* @param
|
|
79
|
+
*
|
|
80
|
+
* @param adapterOrContext The adapter that created this context, or another TurnContext to clone
|
|
81
|
+
* @param request The activity for the turn (required when first parameter is an adapter)
|
|
53
82
|
*/
|
|
54
83
|
constructor (adapterOrContext: BaseAdapter, request: Activity)
|
|
55
84
|
constructor (adapterOrContext: TurnContext)
|
|
@@ -63,17 +92,24 @@ export class TurnContext {
|
|
|
63
92
|
}
|
|
64
93
|
|
|
65
94
|
/**
|
|
66
|
-
* A list of buffered
|
|
95
|
+
* A list of reply activities that are buffered until the end of the turn.
|
|
96
|
+
*
|
|
97
|
+
* This is primarily used with the 'expectReplies' delivery mode where all
|
|
98
|
+
* activities during a turn are collected and returned as a single response.
|
|
67
99
|
*/
|
|
68
100
|
readonly bufferedReplyActivities: Activity[] = []
|
|
69
101
|
|
|
70
102
|
/**
|
|
71
|
-
* Sends a trace activity.
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* @
|
|
103
|
+
* Sends a trace activity for debugging purposes.
|
|
104
|
+
*
|
|
105
|
+
* Trace activities are typically used for debugging and are only visible in
|
|
106
|
+
* channels that support them, like the Bot Framework Emulator.
|
|
107
|
+
*
|
|
108
|
+
* @param name The name/category of the trace
|
|
109
|
+
* @param value The value/data to include in the trace
|
|
110
|
+
* @param valueType Optional type name for the value
|
|
111
|
+
* @param label Optional descriptive label for the trace
|
|
112
|
+
* @returns A promise that resolves to the resource response or undefined
|
|
77
113
|
*/
|
|
78
114
|
async sendTraceActivity (name: string, value?: any, valueType?: string, label?: string): Promise<ResourceResponse | undefined> {
|
|
79
115
|
const traceActivityObj = {
|
|
@@ -89,11 +125,16 @@ export class TurnContext {
|
|
|
89
125
|
}
|
|
90
126
|
|
|
91
127
|
/**
|
|
92
|
-
* Sends an activity.
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
128
|
+
* Sends an activity to the sender of the incoming activity.
|
|
129
|
+
*
|
|
130
|
+
* This is the primary method used to respond to the user. It automatically
|
|
131
|
+
* addresses the response to the correct conversation and recipient using
|
|
132
|
+
* information from the incoming activity.
|
|
133
|
+
*
|
|
134
|
+
* @param activityOrText The activity to send or a string for a simple message
|
|
135
|
+
* @param speak Optional text to be spoken by the agent
|
|
136
|
+
* @param inputHint Optional input hint to indicate if the agent is expecting input
|
|
137
|
+
* @returns A promise that resolves to the resource response or undefined
|
|
97
138
|
*/
|
|
98
139
|
async sendActivity (activityOrText: string | Activity, speak?: string, inputHint?: string): Promise<ResourceResponse | undefined> {
|
|
99
140
|
let activityObject: {}
|
|
@@ -112,9 +153,14 @@ export class TurnContext {
|
|
|
112
153
|
}
|
|
113
154
|
|
|
114
155
|
/**
|
|
115
|
-
* Sends multiple activities.
|
|
116
|
-
*
|
|
117
|
-
*
|
|
156
|
+
* Sends multiple activities to the sender of the incoming activity.
|
|
157
|
+
*
|
|
158
|
+
* This method applies conversation references to each activity and
|
|
159
|
+
* emits them through the middleware chain before sending them to
|
|
160
|
+
* the adapter.
|
|
161
|
+
*
|
|
162
|
+
* @param activities The array of activities to send
|
|
163
|
+
* @returns A promise that resolves to an array of resource responses
|
|
118
164
|
*/
|
|
119
165
|
async sendActivities (activities: Activity[]): Promise<ResourceResponse[]> {
|
|
120
166
|
let sentNonTraceActivity = false
|
|
@@ -164,9 +210,13 @@ export class TurnContext {
|
|
|
164
210
|
}
|
|
165
211
|
|
|
166
212
|
/**
|
|
167
|
-
* Updates an activity.
|
|
168
|
-
*
|
|
169
|
-
*
|
|
213
|
+
* Updates an existing activity in the conversation.
|
|
214
|
+
*
|
|
215
|
+
* This can be used to edit previously sent activities, for example to
|
|
216
|
+
* update the content of an adaptive card or change a message.
|
|
217
|
+
*
|
|
218
|
+
* @param activity The activity to update with its ID specified
|
|
219
|
+
* @returns A promise that resolves when the activity has been updated
|
|
170
220
|
*/
|
|
171
221
|
async updateActivity (activity: Activity): Promise<void> {
|
|
172
222
|
const ref: ConversationReference = this.activity.getConversationReference()
|
|
@@ -177,9 +227,10 @@ export class TurnContext {
|
|
|
177
227
|
}
|
|
178
228
|
|
|
179
229
|
/**
|
|
180
|
-
* Deletes an activity.
|
|
181
|
-
*
|
|
182
|
-
* @
|
|
230
|
+
* Deletes an activity from the conversation.
|
|
231
|
+
*
|
|
232
|
+
* @param idOrReference The ID of the activity to delete or a conversation reference
|
|
233
|
+
* @returns A promise that resolves when the activity has been deleted
|
|
183
234
|
*/
|
|
184
235
|
async deleteActivity (idOrReference: string | ConversationReference): Promise<void> {
|
|
185
236
|
let reference: ConversationReference
|
|
@@ -193,38 +244,45 @@ export class TurnContext {
|
|
|
193
244
|
}
|
|
194
245
|
|
|
195
246
|
/**
|
|
196
|
-
* Uploads an attachment.
|
|
197
|
-
*
|
|
198
|
-
* @param
|
|
199
|
-
* @
|
|
247
|
+
* Uploads an attachment to the conversation.
|
|
248
|
+
*
|
|
249
|
+
* @param conversationId The ID of the conversation
|
|
250
|
+
* @param attachmentData The attachment data to upload
|
|
251
|
+
* @returns A promise that resolves to the resource response
|
|
200
252
|
*/
|
|
201
253
|
async uploadAttachment (conversationId: string, attachmentData: AttachmentData): Promise<ResourceResponse> {
|
|
202
254
|
return await this.adapter.uploadAttachment(conversationId, attachmentData)
|
|
203
255
|
}
|
|
204
256
|
|
|
205
257
|
/**
|
|
206
|
-
* Gets attachment
|
|
207
|
-
*
|
|
208
|
-
* @
|
|
258
|
+
* Gets information about an attachment.
|
|
259
|
+
*
|
|
260
|
+
* @param attachmentId The ID of the attachment
|
|
261
|
+
* @returns A promise that resolves to the attachment information
|
|
209
262
|
*/
|
|
210
263
|
async getAttachmentInfo (attachmentId: string): Promise<AttachmentInfo> {
|
|
211
264
|
return await this.adapter.getAttachmentInfo(attachmentId)
|
|
212
265
|
}
|
|
213
266
|
|
|
214
267
|
/**
|
|
215
|
-
* Gets an attachment.
|
|
216
|
-
*
|
|
217
|
-
* @param
|
|
218
|
-
* @
|
|
268
|
+
* Gets the content of an attachment.
|
|
269
|
+
*
|
|
270
|
+
* @param attachmentId The ID of the attachment
|
|
271
|
+
* @param viewId The view to get
|
|
272
|
+
* @returns A promise that resolves to a readable stream of the attachment content
|
|
219
273
|
*/
|
|
220
274
|
async getAttachment (attachmentId: string, viewId: string): Promise<NodeJS.ReadableStream> {
|
|
221
275
|
return await this.adapter.getAttachment(attachmentId, viewId)
|
|
222
276
|
}
|
|
223
277
|
|
|
224
278
|
/**
|
|
225
|
-
* Registers a handler for
|
|
226
|
-
*
|
|
227
|
-
*
|
|
279
|
+
* Registers a handler for intercepting and processing activities being sent.
|
|
280
|
+
*
|
|
281
|
+
* This method follows a middleware pattern, allowing multiple handlers to
|
|
282
|
+
* be chained together. Handlers can modify activities or inject new ones.
|
|
283
|
+
*
|
|
284
|
+
* @param handler The handler to register
|
|
285
|
+
* @returns The current TurnContext instance for chaining
|
|
228
286
|
*/
|
|
229
287
|
onSendActivities (handler: SendActivitiesHandler): this {
|
|
230
288
|
this._onSendActivities.push(handler)
|
|
@@ -232,9 +290,10 @@ export class TurnContext {
|
|
|
232
290
|
}
|
|
233
291
|
|
|
234
292
|
/**
|
|
235
|
-
* Registers a handler for
|
|
236
|
-
*
|
|
237
|
-
* @
|
|
293
|
+
* Registers a handler for intercepting activity updates.
|
|
294
|
+
*
|
|
295
|
+
* @param handler The handler to register
|
|
296
|
+
* @returns The current TurnContext instance for chaining
|
|
238
297
|
*/
|
|
239
298
|
onUpdateActivity (handler: UpdateActivityHandler): this {
|
|
240
299
|
this._onUpdateActivity.push(handler)
|
|
@@ -242,9 +301,10 @@ export class TurnContext {
|
|
|
242
301
|
}
|
|
243
302
|
|
|
244
303
|
/**
|
|
245
|
-
* Registers a handler for
|
|
246
|
-
*
|
|
247
|
-
* @
|
|
304
|
+
* Registers a handler for intercepting activity deletions.
|
|
305
|
+
*
|
|
306
|
+
* @param handler The handler to register
|
|
307
|
+
* @returns The current TurnContext instance for chaining
|
|
248
308
|
*/
|
|
249
309
|
onDeleteActivity (handler: DeleteActivityHandler): this {
|
|
250
310
|
this._onDeleteActivity.push(handler)
|
|
@@ -253,28 +313,41 @@ export class TurnContext {
|
|
|
253
313
|
|
|
254
314
|
/**
|
|
255
315
|
* Copies the properties of this TurnContext to another TurnContext.
|
|
256
|
-
*
|
|
316
|
+
*
|
|
317
|
+
* Used internally when cloning contexts.
|
|
318
|
+
*
|
|
319
|
+
* @param context The context to copy to
|
|
320
|
+
* @protected
|
|
257
321
|
*/
|
|
258
322
|
protected copyTo (context: TurnContext): void {
|
|
259
323
|
['_adapter', '_activity', '_respondedRef', '_services', '_onSendActivities', '_onUpdateActivity', '_onDeleteActivity'].forEach((prop: string) => ((context as any)[prop] = (this as any)[prop]))
|
|
260
324
|
}
|
|
261
325
|
|
|
262
326
|
/**
|
|
263
|
-
* Gets the adapter
|
|
327
|
+
* Gets the adapter that created this context.
|
|
328
|
+
*
|
|
329
|
+
* The adapter is responsible for sending and receiving activities
|
|
330
|
+
* to and from the user's channel.
|
|
264
331
|
*/
|
|
265
332
|
get adapter (): BaseAdapter {
|
|
266
333
|
return this._adapter as BaseAdapter
|
|
267
334
|
}
|
|
268
335
|
|
|
269
336
|
/**
|
|
270
|
-
* Gets the activity
|
|
337
|
+
* Gets the incoming activity that started this turn.
|
|
338
|
+
*
|
|
339
|
+
* This is the activity that was received from the user or channel
|
|
340
|
+
* and triggered the creation of this context.
|
|
271
341
|
*/
|
|
272
342
|
get activity (): Activity {
|
|
273
343
|
return this._activity as Activity
|
|
274
344
|
}
|
|
275
345
|
|
|
276
346
|
/**
|
|
277
|
-
* Gets or sets whether the turn has
|
|
347
|
+
* Gets or sets whether the turn has sent a response to the user.
|
|
348
|
+
*
|
|
349
|
+
* This is used to track whether the agent has responded to the user's
|
|
350
|
+
* activity. Once set to true, it cannot be set back to false.
|
|
278
351
|
*/
|
|
279
352
|
get responded (): boolean {
|
|
280
353
|
return this._respondedRef.responded
|
|
@@ -289,6 +362,9 @@ export class TurnContext {
|
|
|
289
362
|
|
|
290
363
|
/**
|
|
291
364
|
* Gets or sets the locale for the turn.
|
|
365
|
+
*
|
|
366
|
+
* The locale affects language-dependent operations like
|
|
367
|
+
* formatting dates or numbers.
|
|
292
368
|
*/
|
|
293
369
|
get locale (): string | undefined {
|
|
294
370
|
const turnObj = this._turnState.get(this._turn)
|
|
@@ -309,18 +385,28 @@ export class TurnContext {
|
|
|
309
385
|
}
|
|
310
386
|
|
|
311
387
|
/**
|
|
312
|
-
* Gets the turn state collection.
|
|
388
|
+
* Gets the turn state collection for storing data during the turn.
|
|
389
|
+
*
|
|
390
|
+
* The turn state collection provides a dictionary-like interface
|
|
391
|
+
* for storing arbitrary data that needs to be accessible during
|
|
392
|
+
* the processing of the current turn.
|
|
313
393
|
*/
|
|
314
394
|
get turnState (): TurnContextStateCollection {
|
|
315
395
|
return this._turnState
|
|
316
396
|
}
|
|
317
397
|
|
|
318
398
|
/**
|
|
319
|
-
* Emits events to
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
399
|
+
* Emits events to registered middleware handlers.
|
|
400
|
+
*
|
|
401
|
+
* This internal method implements the middleware pattern, allowing
|
|
402
|
+
* handlers to be chained together with each having the option to
|
|
403
|
+
* short-circuit the chain.
|
|
404
|
+
*
|
|
405
|
+
* @param handlers Array of handlers to execute
|
|
406
|
+
* @param arg The argument to pass to each handler
|
|
407
|
+
* @param next The function to execute at the end of the middleware chain
|
|
408
|
+
* @returns A promise that resolves to the result from the handlers or next function
|
|
409
|
+
* @private
|
|
324
410
|
*/
|
|
325
411
|
private async emit<A, T>(handlers: Array<(context: TurnContext, arg: A, next: () => Promise<T>) => Promise<T>>, arg: A, next: () => Promise<T>): Promise<T> {
|
|
326
412
|
const runHandlers = async ([handler, ...remaining]: typeof handlers): Promise<T> => {
|
package/dist/src/app/memory.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
export interface Memory {
|
|
6
|
-
deleteValue(path: string): void;
|
|
7
|
-
hasValue(path: string): boolean;
|
|
8
|
-
getValue<TValue = unknown>(path: string): TValue;
|
|
9
|
-
setValue(path: string, value: unknown): void;
|
|
10
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../src/app/memory.ts"],"names":[],"mappings":";AAAA;;;GAGG"}
|
package/src/app/memory.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export interface Memory {
|
|
7
|
-
deleteValue(path: string): void;
|
|
8
|
-
|
|
9
|
-
hasValue(path: string): boolean;
|
|
10
|
-
|
|
11
|
-
getValue<TValue = unknown>(path: string): TValue;
|
|
12
|
-
|
|
13
|
-
setValue(path: string, value: unknown): void;
|
|
14
|
-
}
|