Skip to content

Commit 56345a1

Browse files
rido-minRido
andauthored
Expose StreamingResponse delay in API (#556)
* refactor: expose StreamingResponse delay (#541) * refactor: expose StreamingResponse delay * upd API * Remove duplicate method declarations in API report files * streamingResponse get/set delay * upd api-docs * citation url --------- Co-authored-by: Rido <nouser@gh.com>
1 parent ef996e8 commit 56345a1

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

compat/baseline/agents-activity.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export const activityZodSchema: z.ZodObject<{
279279
value?: any;
280280
image?: string | undefined;
281281
text?: string | undefined;
282+
text?: string | undefined;
282283
displayText?: string | undefined;
283284
channelData?: unknown;
284285
imageAltText?: string | undefined;

compat/baseline/agents-hosting.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,19 +987,20 @@ export interface StoreItems {
987987
export class StreamingResponse {
988988
constructor(context: TurnContext);
989989
get citations(): ClientCitation[] | undefined;
990+
get delayInMs(): number;
990991
endStream(): Promise<void>;
991992
getMessage(): string;
992993
queueInformativeUpdate(text: string): void;
993994
queueTextChunk(text: string, citations?: Citation[]): void;
994995
setAttachments(attachments: Attachment[]): void;
995996
setCitations(citations: Citation[]): void;
997+
setDelayInMs(delayInMs: number): void;
996998
setFeedbackLoop(enableFeedbackLoop: boolean): void;
997999
setFeedbackLoopType(feedbackLoopType: 'default' | 'custom'): void;
9981000
setGeneratedByAILabel(enableGeneratedByAILabel: boolean): void;
9991001
setSensitivityLabel(sensitivityLabel: SensitivityUsageInfo): void;
10001002
get streamId(): string | undefined;
10011003
get updatesSent(): number;
1002-
waitForQueue(): Promise<void>;
10031004
}
10041005

10051006
// @public

packages/agents-hosting/src/app/streaming/streamingResponse.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class StreamingResponse {
2929
private _message: string = ''
3030
private _attachments?: Attachment[]
3131
private _ended = false
32+
private _delayInMs = 1500
3233

3334
// Queue for outgoing activities
3435
private _queue: Array<() => Activity> = []
@@ -80,6 +81,13 @@ export class StreamingResponse {
8081
return this._nextSequence - 1
8182
}
8283

84+
/**
85+
* Gets the delay in milliseconds between chunks.
86+
*/
87+
public get delayInMs (): number {
88+
return this._delayInMs
89+
}
90+
8391
/**
8492
* Queues an informative update to be sent to the client.
8593
*
@@ -183,7 +191,8 @@ export class StreamingResponse {
183191
appearance: {
184192
'@type': 'DigitalDocument',
185193
name: citation.title || `Document #${currPos + 1}`,
186-
abstract: CitationUtil.snippet(citation.content, 477)
194+
abstract: CitationUtil.snippet(citation.content, 477),
195+
url: citation.url!
187196
}
188197
}
189198
currPos++
@@ -222,6 +231,14 @@ export class StreamingResponse {
222231
this._enableGeneratedByAILabel = enableGeneratedByAILabel
223232
}
224233

234+
/**
235+
* Sets the delay in milliseconds between chunks.
236+
* @param delayInMs The delay in milliseconds.
237+
*/
238+
public setDelayInMs (delayInMs: number): void {
239+
this._delayInMs = delayInMs
240+
}
241+
225242
/**
226243
* Returns the most recently streamed message.
227244
*
@@ -236,7 +253,7 @@ export class StreamingResponse {
236253
*
237254
* @returns {Promise<void>} - A promise representing the async operation.
238255
*/
239-
public waitForQueue (): Promise<void> {
256+
private waitForQueue (): Promise<void> {
240257
return this._queueSync || Promise.resolve()
241258
}
242259

@@ -370,7 +387,7 @@ export class StreamingResponse {
370387

371388
// Send activity
372389
const response = await this._context.sendActivity(activity)
373-
await new Promise((resolve) => setTimeout(resolve, 1500))
390+
await new Promise((resolve) => setTimeout(resolve, this.delayInMs))
374391

375392
// Save assigned stream ID
376393
if (!this._streamId) {

samples/basic/streamingSample.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ agent.onActivity('invoke', async (context: TurnContext, state: TurnState) => {
2222
})
2323

2424
agent.onActivity('message', async (context: TurnContext, state: TurnState) => {
25+
context.streamingResponse.setDelayInMs(500)
2526
context.streamingResponse.setFeedbackLoop(true)
2627
context.streamingResponse.setSensitivityLabel({ type: 'https://schema.org/Message', '@type': 'CreativeWork', name: 'Internal' })
2728
context.streamingResponse.setGeneratedByAILabel(true)

0 commit comments

Comments
 (0)