Skip to content

[Feature Request]: Clearing stream context #2721

@MMarciniak103

Description

@MMarciniak103

Hey,
I'm wondering if it is possible to have option for clearing current stream context so that new message stream is displayed in place of old one. As an example of this functionality we can think about multi agent system, where agents are called in sequential order, for example: agent_1 -> agent_2 -> agent_1 -> user

In such case I would like to stream agent_1 response and once agent_2 starts his run I would like to display his stream over the previous one so that from user perspective it looks like the system is producing new updates.

Taking this code as example:

STREAM_MESSAGES = ["Hello", "from", "stream1"]
STREAM_MESSAGES_2 = ["Hello", "from", "stream2"]

@app.on_message
async def handle_message(ctx: ActivityContext[MessageActivity]):
    ctx.stream.update("Stream starting...")
    await asyncio.sleep(1)

    # Stream messages with delays using ctx.stream.emit
    for message in STREAM_MESSAGES:
        # Add some randomness to timing
        await asyncio.sleep(random())

        ctx.stream.emit(message)
     
    # I would like to clear steam context so that it overwrites previous stream messages
    # Currently I was only able to send them as new message by calling ctx.stream.close()
    await ctx.stream.close() # This makes STREAM_MESSAGES_2 to be send as new message instead of appending to previous stream

    for message in STREAM_MESSAGES_2:
        await asyncio.sleep(random())

        ctx.stream.emit(message)

Output without ctx.stream.close():
msg_1: Hellofromstream1Hellofromstream2

Output with ctx.stream.close():
msg_1: Hellofromstream1
msg_2: Helofromstream2

What I want to achieve:
display "Hello from stream1" in streaming mode, then overwrite/update the message by streaming "Hello from stream2"

Is it possible to do? Maybe there could be some method being exposed in ctx.stream that would allow to clear previous stream content

Thanks for your help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions