Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compat/baseline/agents-activity.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export const activityZodSchema: z.ZodObject<{
value?: any;
image?: string | undefined;
text?: string | undefined;
text?: string | undefined;
displayText?: string | undefined;
channelData?: unknown;
imageAltText?: string | undefined;
Expand Down
3 changes: 2 additions & 1 deletion compat/baseline/agents-hosting.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -987,19 +987,20 @@ export interface StoreItems {
export class StreamingResponse {
constructor(context: TurnContext);
get citations(): ClientCitation[] | undefined;
get delayInMs(): number;
endStream(): Promise<void>;
getMessage(): string;
queueInformativeUpdate(text: string): void;
queueTextChunk(text: string, citations?: Citation[]): void;
setAttachments(attachments: Attachment[]): void;
setCitations(citations: Citation[]): void;
setDelayInMs(delayInMs: number): void;
setFeedbackLoop(enableFeedbackLoop: boolean): void;
setFeedbackLoopType(feedbackLoopType: 'default' | 'custom'): void;
setGeneratedByAILabel(enableGeneratedByAILabel: boolean): void;
setSensitivityLabel(sensitivityLabel: SensitivityUsageInfo): void;
get streamId(): string | undefined;
get updatesSent(): number;
waitForQueue(): Promise<void>;
}

// @public
Expand Down
23 changes: 20 additions & 3 deletions packages/agents-hosting/src/app/streaming/streamingResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class StreamingResponse {
private _message: string = ''
private _attachments?: Attachment[]
private _ended = false
private _delayInMs = 1500

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

/**
* Gets the delay in milliseconds between chunks.
*/
public get delayInMs (): number {
return this._delayInMs
}

/**
* Queues an informative update to be sent to the client.
*
Expand Down Expand Up @@ -183,7 +191,8 @@ export class StreamingResponse {
appearance: {
'@type': 'DigitalDocument',
name: citation.title || `Document #${currPos + 1}`,
abstract: CitationUtil.snippet(citation.content, 477)
abstract: CitationUtil.snippet(citation.content, 477),
url: citation.url!
}
}
currPos++
Expand Down Expand Up @@ -222,6 +231,14 @@ export class StreamingResponse {
this._enableGeneratedByAILabel = enableGeneratedByAILabel
}

/**
* Sets the delay in milliseconds between chunks.
* @param delayInMs The delay in milliseconds.
*/
public setDelayInMs (delayInMs: number): void {
this._delayInMs = delayInMs
}

/**
* Returns the most recently streamed message.
*
Expand All @@ -236,7 +253,7 @@ export class StreamingResponse {
*
* @returns {Promise<void>} - A promise representing the async operation.
*/
public waitForQueue (): Promise<void> {
private waitForQueue (): Promise<void> {
return this._queueSync || Promise.resolve()
}

Expand Down Expand Up @@ -370,7 +387,7 @@ export class StreamingResponse {

// Send activity
const response = await this._context.sendActivity(activity)
await new Promise((resolve) => setTimeout(resolve, 1500))
await new Promise((resolve) => setTimeout(resolve, this.delayInMs))

// Save assigned stream ID
if (!this._streamId) {
Expand Down
1 change: 1 addition & 0 deletions samples/basic/streamingSample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ agent.onActivity('invoke', async (context: TurnContext, state: TurnState) => {
})

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