HttpStream Class

HTTP-based streaming implementation for Microsoft Teams activities.

Flow:

  1. emit() adds activities to a queue
  2. _flush() drains the entire queue under a lock.
  3. Informative typing updates are sent immediately if no message started.
  4. Message text are combined into a typing chunk.
  5. Another flush is scheduled if more items remain.
  6. close() waits for queue to empty, then sends final message with stream_type='stream_final'

The timeout cancellation ensures only one flush operation is scheduled at a time. The delays between flushes is to ensure we dont hit API rate limits with Microsoft Teams.

Initialize a new HttpStream instance.

Constructor

HttpStream(client: ApiClient, ref: ConversationReference)

Parameters

Name Description
client
Required
<xref:microsoft_teams.apps.ApiClient>

The API client used to send activities to Microsoft Teams.

ref
Required
<xref:microsoft_teams.apps.ConversationReference>

Reference to the Teams conversation.

Methods

clear_text

Discard everything accumulated so far so the caller can emit a fresh card-only final activity.

Clears the text buffer, drops queued message items so the next flush doesn't repopulate it, and discards the promoted final activity so any previously-flushed attachments/suggested actions aren't sent if the caller never emits a replacement. The stream id and channel data are kept intact so the new final activity still updates the stream in place.

close

Finalize the current streamed message.

Closing is idempotent until the next emit or update. Emitting or updating after close starts a new streamed message using the same stream instance.

emit

Emit a new activity to the stream.

on_chunk
on_close
update

Send status updates before emitting (ex. "Thinking...").

clear_text

Discard everything accumulated so far so the caller can emit a fresh card-only final activity.

Clears the text buffer, drops queued message items so the next flush doesn't repopulate it, and discards the promoted final activity so any previously-flushed attachments/suggested actions aren't sent if the caller never emits a replacement. The stream id and channel data are kept intact so the new final activity still updates the stream in place.

clear_text() -> None

close

Finalize the current streamed message.

Closing is idempotent until the next emit or update. Emitting or updating after close starts a new streamed message using the same stream instance.

async close() -> SentActivity | None

emit

Emit a new activity to the stream.

emit(activity: MessageActivityInput | TypingActivityInput | str) -> None

Parameters

Name Description
activity
Required

The activity to emit.

on_chunk

on_chunk(handler: Callable[[SentActivity], Awaitable[None]])

Parameters

Name Description
handler
Required

on_close

on_close(handler: Callable[[SentActivity], Awaitable[None]])

Parameters

Name Description
handler
Required

update

Send status updates before emitting (ex. "Thinking...").

update(text: str, text_format: Literal['markdown', 'plain', 'xml', 'extendedmarkdown'] | None = None) -> None

Parameters

Name Description
text
Required

The status text to send.

text_format

Format of text (ex. 'extendedmarkdown'). Omit or pass None to use the Teams default ('markdown').

Default value: None

Attributes

canceled

Whether the stream has been canceled. For example when the user pressed the Stop button.

closed

Whether the current streamed message has been finalized.

count

The total number of chunks queued to be sent.

sequence

The sequence number, representing the number of stream activities sent.

timed_out

Whether the stream has timed out. For example when the streaming has exceeded two minutes.