@microsoft/agents-hosting 0.5.1-g2e246ff274 → 0.5.4-ga4d0401645
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/app/streaming/AIEntity.d.ts +36 -0
- package/dist/src/app/streaming/AIEntity.js +7 -0
- package/dist/src/app/streaming/AIEntity.js.map +1 -0
- package/dist/src/app/streaming/actionCall.d.ts +33 -0
- package/dist/src/app/streaming/actionCall.js +7 -0
- package/dist/src/app/streaming/actionCall.js.map +1 -0
- package/dist/src/app/streaming/clientCitation.d.ts +68 -0
- package/dist/src/app/streaming/clientCitation.js +10 -0
- package/dist/src/app/streaming/clientCitation.js.map +1 -0
- package/dist/src/app/streaming/message.d.ts +106 -0
- package/dist/src/app/streaming/message.js +7 -0
- package/dist/src/app/streaming/message.js.map +1 -0
- package/dist/src/app/streaming/sensitivityUsageInfo.d.ts +40 -0
- package/dist/src/app/streaming/sensitivityUsageInfo.js +3 -0
- package/dist/src/app/streaming/sensitivityUsageInfo.js.map +1 -0
- package/dist/src/app/streaming/streamingResponse.d.ts +141 -0
- package/dist/src/app/streaming/streamingResponse.js +331 -0
- package/dist/src/app/streaming/streamingResponse.js.map +1 -0
- package/dist/src/app/streaming/utilities.d.ts +31 -0
- package/dist/src/app/streaming/utilities.js +101 -0
- package/dist/src/app/streaming/utilities.js.map +1 -0
- package/dist/src/turnContext.d.ts +3 -0
- package/dist/src/turnContext.js +5 -0
- package/dist/src/turnContext.js.map +1 -1
- package/package.json +2 -2
- package/src/app/streaming/AIEntity.ts +44 -0
- package/src/app/streaming/actionCall.ts +37 -0
- package/src/app/streaming/clientCitation.ts +102 -0
- package/src/app/streaming/message.ts +125 -0
- package/src/app/streaming/sensitivityUsageInfo.ts +48 -0
- package/src/app/streaming/streamingResponse.ts +406 -0
- package/src/app/streaming/utilities.ts +108 -0
- package/src/turnContext.ts +7 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module teams-ai
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
6
|
+
* Licensed under the MIT License.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { SensitivityUsageInfo } from './sensitivityUsageInfo'
|
|
10
|
+
|
|
11
|
+
export type ClientCitationIconName =
|
|
12
|
+
| 'Microsoft Word'
|
|
13
|
+
| 'Microsoft Excel'
|
|
14
|
+
| 'Microsoft PowerPoint'
|
|
15
|
+
| 'Microsoft OneNote'
|
|
16
|
+
| 'Microsoft SharePoint'
|
|
17
|
+
| 'Microsoft Visio'
|
|
18
|
+
| 'Microsoft Loop'
|
|
19
|
+
| 'Microsoft Whiteboard'
|
|
20
|
+
| 'Adobe Illustrator'
|
|
21
|
+
| 'Adobe Photoshop'
|
|
22
|
+
| 'Adobe InDesign'
|
|
23
|
+
| 'Adobe Flash'
|
|
24
|
+
| 'Sketch'
|
|
25
|
+
| 'Source Code'
|
|
26
|
+
| 'Image'
|
|
27
|
+
| 'GIF'
|
|
28
|
+
| 'Video'
|
|
29
|
+
| 'Sound'
|
|
30
|
+
| 'ZIP'
|
|
31
|
+
| 'Text'
|
|
32
|
+
| 'PDF'
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Represents a Teams client citation to be included in a message. See Bot messages with AI-generated content for more details.
|
|
36
|
+
* https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bot-messages-ai-generated-content?tabs=before%2Cbotmessage
|
|
37
|
+
*/
|
|
38
|
+
export interface ClientCitation {
|
|
39
|
+
/**
|
|
40
|
+
* Required; must be "Claim"
|
|
41
|
+
*/
|
|
42
|
+
'@type': 'Claim';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Required. Number and position of the citation.
|
|
46
|
+
*/
|
|
47
|
+
position: number;
|
|
48
|
+
appearance: {
|
|
49
|
+
/**
|
|
50
|
+
* Required; Must be 'DigitalDocument'
|
|
51
|
+
*/
|
|
52
|
+
'@type': 'DigitalDocument';
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Name of the document. (max length 80)
|
|
56
|
+
*/
|
|
57
|
+
name: string;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Stringified adaptive card with additional information about the citation.
|
|
61
|
+
* It is rendered within the modal.
|
|
62
|
+
*/
|
|
63
|
+
text?: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* URL of the document. This will make the name of the citation clickable and direct the user to the specified URL.
|
|
67
|
+
*/
|
|
68
|
+
url?: string;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Extract of the referenced content. (max length 160)
|
|
72
|
+
*/
|
|
73
|
+
abstract: string;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Encoding format of the `citation.appearance.text` field.
|
|
77
|
+
*/
|
|
78
|
+
encodingFormat?: 'application/vnd.microsoft.card.adaptive';
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Information about the citation’s icon.
|
|
82
|
+
*/
|
|
83
|
+
image?: {
|
|
84
|
+
'@type': 'ImageObject';
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The image/icon name
|
|
88
|
+
*/
|
|
89
|
+
name: ClientCitationIconName;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Optional; set by developer. (max length 3) (max keyword length 28)
|
|
94
|
+
*/
|
|
95
|
+
keywords?: string[];
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Optional sensitivity content information.
|
|
99
|
+
*/
|
|
100
|
+
usageInfo?: SensitivityUsageInfo;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ActionCall } from './actionCall'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A message object sent to or received from an LLM.
|
|
10
|
+
* @param TContent Optional. Type of the message content. Defaults to `string`
|
|
11
|
+
*/
|
|
12
|
+
export interface Message<TContent = string> {
|
|
13
|
+
/**
|
|
14
|
+
* The messages role. Typically 'system', 'user', 'assistant', 'tool', 'function'.
|
|
15
|
+
*/
|
|
16
|
+
role: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Text of the message.
|
|
20
|
+
*/
|
|
21
|
+
content: TContent | undefined;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Optional. A named function to call.
|
|
25
|
+
*/
|
|
26
|
+
function_call?: FunctionCall;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The context used for the message.
|
|
30
|
+
*/
|
|
31
|
+
context?: MessageContext;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Optional. Name of the function that was called.
|
|
35
|
+
*/
|
|
36
|
+
name?: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Optional. The action or tool to be called by the model when using 'tools' augmentation. In OpenAI, this is the tool_calls property returned from the model.
|
|
40
|
+
*/
|
|
41
|
+
action_calls?: ActionCall[];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Optional. The id of the action called.
|
|
45
|
+
*/
|
|
46
|
+
action_call_id?: string | undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A named function to call.
|
|
51
|
+
*/
|
|
52
|
+
export interface FunctionCall {
|
|
53
|
+
/**
|
|
54
|
+
* Name of the function to call.
|
|
55
|
+
*/
|
|
56
|
+
name?: string;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Optional. Arguments to pass to the function. Must be deserialized.
|
|
60
|
+
*/
|
|
61
|
+
arguments?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type MessageContentParts = TextContentPart | ImageContentPart
|
|
65
|
+
|
|
66
|
+
export interface TextContentPart {
|
|
67
|
+
/**
|
|
68
|
+
* Type of the message content. Should always be 'text'.
|
|
69
|
+
*/
|
|
70
|
+
type: 'text';
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The text of the message.
|
|
74
|
+
*/
|
|
75
|
+
text: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ImageContentPart {
|
|
79
|
+
/**
|
|
80
|
+
* Type of the message content. Should always be 'image_url'.
|
|
81
|
+
*/
|
|
82
|
+
type: 'image_url';
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The URL of the image.
|
|
86
|
+
*/
|
|
87
|
+
image_url: string | { url: string };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Citations returned by the model.
|
|
92
|
+
*/
|
|
93
|
+
export interface Citation {
|
|
94
|
+
/**
|
|
95
|
+
* The content of the citation.
|
|
96
|
+
*/
|
|
97
|
+
content: string;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The title of the citation.
|
|
101
|
+
*/
|
|
102
|
+
title: string | null;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The URL of the citation.
|
|
106
|
+
*/
|
|
107
|
+
url: string | null;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The filepath of the document.
|
|
111
|
+
*/
|
|
112
|
+
filepath: string | null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface MessageContext {
|
|
116
|
+
/**
|
|
117
|
+
* Citations used in the message.
|
|
118
|
+
*/
|
|
119
|
+
citations: Citation[];
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The intent of the message (what the user wrote).
|
|
123
|
+
*/
|
|
124
|
+
intent: string;
|
|
125
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sensitivity usage info for content sent to the user. This is used to provide information about the content to the user. See ClientCitation for more information.
|
|
3
|
+
*/
|
|
4
|
+
export interface SensitivityUsageInfo {
|
|
5
|
+
/**
|
|
6
|
+
* Must be "https://schema.org/Message"
|
|
7
|
+
*/
|
|
8
|
+
type: 'https://schema.org/Message';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Required; Set to CreativeWork;
|
|
12
|
+
*/
|
|
13
|
+
'@type': 'CreativeWork';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sensitivity description of the content
|
|
17
|
+
*/
|
|
18
|
+
description?: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sensitivity title of the content
|
|
22
|
+
*/
|
|
23
|
+
name: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Optional; ignored in Teams.
|
|
27
|
+
*/
|
|
28
|
+
position?: number;
|
|
29
|
+
|
|
30
|
+
pattern?: {
|
|
31
|
+
/**
|
|
32
|
+
* Set to DefinedTerm
|
|
33
|
+
*/
|
|
34
|
+
'@type': 'DefinedTerm';
|
|
35
|
+
|
|
36
|
+
inDefinedTermSet: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Color
|
|
40
|
+
*/
|
|
41
|
+
name: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* e.g. #454545
|
|
45
|
+
*/
|
|
46
|
+
termCode: string;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Activity, Attachment, Entity } from '@microsoft/agents-activity'
|
|
7
|
+
import { TurnContext } from '../../turnContext'
|
|
8
|
+
import { ClientCitation } from './clientCitation'
|
|
9
|
+
import { SensitivityUsageInfo } from './sensitivityUsageInfo'
|
|
10
|
+
import { Citation } from './message'
|
|
11
|
+
import { Utilities } from './utilities'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A helper class for streaming responses to the client.
|
|
15
|
+
* @remarks
|
|
16
|
+
* This class is used to send a series of updates to the client in a single response. The expected
|
|
17
|
+
* sequence of calls is:
|
|
18
|
+
*
|
|
19
|
+
* `sendInformativeUpdate()`, `sendTextChunk()`, `sendTextChunk()`, ..., `endStream()`.
|
|
20
|
+
*
|
|
21
|
+
* Once `endStream()` is called, the stream is considered ended and no further updates can be sent.
|
|
22
|
+
*/
|
|
23
|
+
export class StreamingResponse {
|
|
24
|
+
private readonly _context: TurnContext
|
|
25
|
+
private _nextSequence: number = 1
|
|
26
|
+
private _streamId?: string
|
|
27
|
+
private _message: string = ''
|
|
28
|
+
private _attachments?: Attachment[]
|
|
29
|
+
private _ended = false
|
|
30
|
+
|
|
31
|
+
// Queue for outgoing activities
|
|
32
|
+
private _queue: Array<() => Activity> = []
|
|
33
|
+
private _queueSync: Promise<void> | undefined
|
|
34
|
+
private _chunkQueued = false
|
|
35
|
+
|
|
36
|
+
// Powered by AI feature flags
|
|
37
|
+
private _enableFeedbackLoop = false
|
|
38
|
+
private _feedbackLoopType?: 'default' | 'custom'
|
|
39
|
+
private _enableGeneratedByAILabel = false
|
|
40
|
+
private _citations?: ClientCitation[] = []
|
|
41
|
+
private _sensitivityLabel?: SensitivityUsageInfo
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new StreamingResponse instance.
|
|
45
|
+
* @param {TurnContext} context - Context for the current turn of conversation with the user.
|
|
46
|
+
* @returns {TurnContext} - The context for the current turn of conversation with the user.
|
|
47
|
+
*/
|
|
48
|
+
public constructor (context: TurnContext) {
|
|
49
|
+
this._context = context
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets the stream ID of the current response.
|
|
54
|
+
* @returns {string | undefined} - The stream ID of the current response.
|
|
55
|
+
* @remarks
|
|
56
|
+
* Assigned after the initial update is sent.
|
|
57
|
+
*/
|
|
58
|
+
public get streamId (): string | undefined {
|
|
59
|
+
return this._streamId
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Gets the citations of the current response.
|
|
64
|
+
*/
|
|
65
|
+
public get citations (): ClientCitation[] | undefined {
|
|
66
|
+
return this._citations
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Gets the number of updates sent for the stream.
|
|
71
|
+
* @returns {number} - The number of updates sent for the stream.
|
|
72
|
+
*/
|
|
73
|
+
public get updatesSent (): number {
|
|
74
|
+
return this._nextSequence - 1
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Queues an informative update to be sent to the client.
|
|
79
|
+
* @param {string} text Text of the update to send.
|
|
80
|
+
*/
|
|
81
|
+
public queueInformativeUpdate (text: string): void {
|
|
82
|
+
if (this._ended) {
|
|
83
|
+
throw new Error('The stream has already ended.')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Queue a typing activity
|
|
87
|
+
this.queueActivity(() => Activity.fromObject({
|
|
88
|
+
type: 'typing',
|
|
89
|
+
text,
|
|
90
|
+
channelData: {
|
|
91
|
+
streamType: 'informative',
|
|
92
|
+
streamSequence: this._nextSequence++
|
|
93
|
+
} as StreamingChannelData
|
|
94
|
+
}))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Queues a chunk of partial message text to be sent to the client
|
|
99
|
+
* @remarks
|
|
100
|
+
* The text we be sent as quickly as possible to the client. Chunks may be combined before
|
|
101
|
+
* delivery to the client.
|
|
102
|
+
* @param {string} text Partial text of the message to send.
|
|
103
|
+
* @param {Citation[]} citations Citations to be included in the message.
|
|
104
|
+
*/
|
|
105
|
+
public queueTextChunk (text: string, citations?: Citation[]): void {
|
|
106
|
+
if (this._ended) {
|
|
107
|
+
throw new Error('The stream has already ended.')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Update full message text
|
|
111
|
+
this._message += text
|
|
112
|
+
|
|
113
|
+
// If there are citations, modify the content so that the sources are numbers instead of [doc1], [doc2], etc.
|
|
114
|
+
this._message = Utilities.formatCitationsResponse(this._message)
|
|
115
|
+
|
|
116
|
+
// Queue the next chunk
|
|
117
|
+
this.queueNextChunk()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Ends the stream by sending the final message to the client.
|
|
122
|
+
* @returns {Promise<void>} - A promise representing the async operation
|
|
123
|
+
*/
|
|
124
|
+
public endStream (): Promise<void> {
|
|
125
|
+
if (this._ended) {
|
|
126
|
+
throw new Error('The stream has already ended.')
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Queue final message
|
|
130
|
+
this._ended = true
|
|
131
|
+
this.queueNextChunk()
|
|
132
|
+
|
|
133
|
+
// Wait for the queue to drain
|
|
134
|
+
return this.waitForQueue()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Sets the attachments to attach to the final chunk.
|
|
139
|
+
* @param attachments List of attachments.
|
|
140
|
+
*/
|
|
141
|
+
public setAttachments (attachments: Attachment[]): void {
|
|
142
|
+
this._attachments = attachments
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Sets the sensitivity label to attach to the final chunk.
|
|
147
|
+
* @param sensitivityLabel The sensitivty label.
|
|
148
|
+
*/
|
|
149
|
+
public setSensitivityLabel (sensitivityLabel: SensitivityUsageInfo): void {
|
|
150
|
+
this._sensitivityLabel = sensitivityLabel
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Sets the citations for the full message.
|
|
155
|
+
* @param {Citation[]} citations Citations to be included in the message.
|
|
156
|
+
*/
|
|
157
|
+
public setCitations (citations: Citation[]): void {
|
|
158
|
+
if (citations.length > 0) {
|
|
159
|
+
if (!this._citations) {
|
|
160
|
+
this._citations = []
|
|
161
|
+
}
|
|
162
|
+
let currPos = this._citations.length
|
|
163
|
+
|
|
164
|
+
for (const citation of citations) {
|
|
165
|
+
const clientCitation: ClientCitation = {
|
|
166
|
+
'@type': 'Claim',
|
|
167
|
+
position: currPos + 1,
|
|
168
|
+
appearance: {
|
|
169
|
+
'@type': 'DigitalDocument',
|
|
170
|
+
name: citation.title || `Document #${currPos + 1}`,
|
|
171
|
+
abstract: Utilities.snippet(citation.content, 477)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
currPos++
|
|
175
|
+
this._citations.push(clientCitation)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Sets the Feedback Loop in Teams that allows a user to
|
|
182
|
+
* give thumbs up or down to a response.
|
|
183
|
+
* Default is `false`.
|
|
184
|
+
* @param enableFeedbackLoop If true, the feedback loop is enabled.
|
|
185
|
+
*/
|
|
186
|
+
public setFeedbackLoop (enableFeedbackLoop: boolean): void {
|
|
187
|
+
this._enableFeedbackLoop = enableFeedbackLoop
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Sets the type of UI to use for the feedback loop.
|
|
192
|
+
* @param feedbackLoopType The type of the feedback loop.
|
|
193
|
+
*/
|
|
194
|
+
public setFeedbackLoopType (feedbackLoopType: 'default' | 'custom'): void {
|
|
195
|
+
this._feedbackLoopType = feedbackLoopType
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Sets the the Generated by AI label in Teams
|
|
200
|
+
* Default is `false`.
|
|
201
|
+
* @param enableGeneratedByAILabel If true, the label is added.
|
|
202
|
+
*/
|
|
203
|
+
public setGeneratedByAILabel (enableGeneratedByAILabel: boolean): void {
|
|
204
|
+
this._enableGeneratedByAILabel = enableGeneratedByAILabel
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns the most recently streamed message.
|
|
209
|
+
* @returns The streamed message.
|
|
210
|
+
*/
|
|
211
|
+
public getMessage (): string {
|
|
212
|
+
return this._message
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Waits for the outgoing activity queue to be empty.
|
|
217
|
+
* @returns {Promise<void>} - A promise representing the async operation.
|
|
218
|
+
*/
|
|
219
|
+
public waitForQueue (): Promise<void> {
|
|
220
|
+
return this._queueSync || Promise.resolve()
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Queues the next chunk of text to be sent to the client.
|
|
225
|
+
* @private
|
|
226
|
+
*/
|
|
227
|
+
private queueNextChunk (): void {
|
|
228
|
+
// Are we already waiting to send a chunk?
|
|
229
|
+
if (this._chunkQueued) {
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Queue a chunk of text to be sent
|
|
234
|
+
this._chunkQueued = true
|
|
235
|
+
this.queueActivity(() => {
|
|
236
|
+
this._chunkQueued = false
|
|
237
|
+
if (this._ended) {
|
|
238
|
+
// Send final message
|
|
239
|
+
return Activity.fromObject({
|
|
240
|
+
type: 'message',
|
|
241
|
+
text: this._message,
|
|
242
|
+
attachments: this._attachments,
|
|
243
|
+
channelData: {
|
|
244
|
+
streamType: 'final'
|
|
245
|
+
} as StreamingChannelData
|
|
246
|
+
})
|
|
247
|
+
} else {
|
|
248
|
+
// Send typing activity
|
|
249
|
+
return Activity.fromObject({
|
|
250
|
+
type: 'typing',
|
|
251
|
+
text: this._message,
|
|
252
|
+
channelData: {
|
|
253
|
+
streamType: 'streaming',
|
|
254
|
+
streamSequence: this._nextSequence++
|
|
255
|
+
} as StreamingChannelData
|
|
256
|
+
})
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Queues an activity to be sent to the client.
|
|
263
|
+
*/
|
|
264
|
+
private queueActivity (factory: () => Activity): void {
|
|
265
|
+
this._queue.push(factory)
|
|
266
|
+
|
|
267
|
+
// If there's no sync in progress, start one
|
|
268
|
+
if (!this._queueSync) {
|
|
269
|
+
this._queueSync = this.drainQueue().catch((err) => {
|
|
270
|
+
console.error(`Error occured when sending activity while streaming: "${err}".`)
|
|
271
|
+
throw err
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Sends any queued activities to the client until the queue is empty.
|
|
278
|
+
* @returns {Promise<void>} - A promise that will be resolved once the queue is empty.
|
|
279
|
+
* @private
|
|
280
|
+
*/
|
|
281
|
+
private async drainQueue (): Promise<void> {
|
|
282
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
283
|
+
return new Promise<void>(async (resolve, reject) => {
|
|
284
|
+
try {
|
|
285
|
+
while (this._queue.length > 0) {
|
|
286
|
+
// Get next activity from queue
|
|
287
|
+
const factory = this._queue.shift()!
|
|
288
|
+
const activity = factory()
|
|
289
|
+
|
|
290
|
+
// Send activity
|
|
291
|
+
await this.sendActivity(activity)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
resolve()
|
|
295
|
+
} catch (err) {
|
|
296
|
+
reject(err)
|
|
297
|
+
} finally {
|
|
298
|
+
// Queue is empty, mark as idle
|
|
299
|
+
this._queueSync = undefined
|
|
300
|
+
}
|
|
301
|
+
})
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Sends an activity to the client and saves the stream ID returned.
|
|
306
|
+
* @param {Activity} activity - The activity to send.
|
|
307
|
+
* @returns {Promise<void>} - A promise representing the async operation.
|
|
308
|
+
* @private
|
|
309
|
+
*/
|
|
310
|
+
private async sendActivity (activity: Activity): Promise<void> {
|
|
311
|
+
// Set activity ID to the assigned stream ID
|
|
312
|
+
if (this._streamId) {
|
|
313
|
+
activity.id = this._streamId
|
|
314
|
+
activity.channelData = Object.assign({}, activity.channelData, { streamId: this._streamId })
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
activity.entities = [
|
|
318
|
+
{
|
|
319
|
+
type: 'streaminfo',
|
|
320
|
+
...activity.channelData
|
|
321
|
+
} as Entity
|
|
322
|
+
]
|
|
323
|
+
|
|
324
|
+
if (this._citations && this._citations.length > 0 && !this._ended) {
|
|
325
|
+
// Filter out the citations unused in content.
|
|
326
|
+
const currCitations = Utilities.getUsedCitations(this._message, this._citations) ?? undefined
|
|
327
|
+
activity.entities.push({
|
|
328
|
+
type: 'https://schema.org/Message',
|
|
329
|
+
'@type': 'Message',
|
|
330
|
+
'@context': 'https://schema.org',
|
|
331
|
+
'@id': '',
|
|
332
|
+
citation: currCitations
|
|
333
|
+
} as unknown as Entity)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Add in Powered by AI feature flags
|
|
337
|
+
if (this._ended) {
|
|
338
|
+
if (this._enableFeedbackLoop && this._feedbackLoopType) {
|
|
339
|
+
activity.channelData = Object.assign({}, activity.channelData, {
|
|
340
|
+
feedbackLoop: { type: this._feedbackLoopType }
|
|
341
|
+
})
|
|
342
|
+
} else {
|
|
343
|
+
activity.channelData = Object.assign({}, activity.channelData, {
|
|
344
|
+
feedbackLoopEnabled: this._enableFeedbackLoop
|
|
345
|
+
})
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Add in Generated by AI
|
|
349
|
+
if (this._enableGeneratedByAILabel) {
|
|
350
|
+
activity.entities.push({
|
|
351
|
+
type: 'https://schema.org/Message',
|
|
352
|
+
'@type': 'Message',
|
|
353
|
+
'@context': 'https://schema.org',
|
|
354
|
+
'@id': '',
|
|
355
|
+
additionalType: ['AIGeneratedContent'],
|
|
356
|
+
citation: this._citations && this._citations.length > 0 ? this._citations : [],
|
|
357
|
+
usageInfo: this._sensitivityLabel
|
|
358
|
+
} as unknown as Entity)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Send activity
|
|
363
|
+
const response = await this._context.sendActivity(activity)
|
|
364
|
+
// await new Promise((resolve) => setTimeout(resolve, 1500))
|
|
365
|
+
|
|
366
|
+
// Save assigned stream ID
|
|
367
|
+
if (!this._streamId) {
|
|
368
|
+
this._streamId = response?.id
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @private
|
|
375
|
+
* Structure of the outgoing channelData field for streaming responses.
|
|
376
|
+
* @remarks
|
|
377
|
+
* The expected sequence of streamTypes is:
|
|
378
|
+
*
|
|
379
|
+
* `informative`, `streaming`, `streaming`, ..., `final`.
|
|
380
|
+
*
|
|
381
|
+
* Once a `final` message is sent, the stream is considered ended.
|
|
382
|
+
*/
|
|
383
|
+
interface StreamingChannelData {
|
|
384
|
+
/**
|
|
385
|
+
* The type of message being sent.
|
|
386
|
+
* @remarks
|
|
387
|
+
* `informative` - An informative update.
|
|
388
|
+
* `streaming` - A chunk of partial message text.
|
|
389
|
+
* `final` - The final message.
|
|
390
|
+
*/
|
|
391
|
+
streamType: 'informative' | 'streaming' | 'final';
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Sequence number of the message in the stream.
|
|
395
|
+
* @remarks
|
|
396
|
+
* Starts at 1 for the first message and increments from there.
|
|
397
|
+
*/
|
|
398
|
+
streamSequence: number;
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* ID of the stream.
|
|
402
|
+
* @remarks
|
|
403
|
+
* Assigned after the initial update is sent.
|
|
404
|
+
*/
|
|
405
|
+
streamId?: string;
|
|
406
|
+
}
|