Skip to content

Add QueryAgent streaming #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Conversation

danmichaeljones
Copy link

@danmichaeljones danmichaeljones commented Jun 12, 2025

Adds an option to stream from the QueryAgent, by consuming the server-sent-events API on the backend. This new method yields objects providing progress messages (e.g., Analyzing Query...) as well as per-token streaming of the final answer, before the final state (like from .run()).

This new method can be used as follows:

const qa = new QueryAgent(...);
const query = "..."

for await (const event of qa.stream(query)) {
    if (event.output_type === "progress_message") {
        // The message is a human-readable string, structured info available in event.details
        console.log(event.message);
    } else if (event.output_type === "streamed_tokens") {
        // The delta is a string containing the next chunk of the final answer
        process.stdout.write(event.delta);
    } else {
        // This is the final response, as returned by QueryAgent.run()
        event.display();
    }
}

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

* @param init - The request init options.
* @returns An async generator of ServerSentEvent objects.
*/
export async function* fetchServerSentEvents(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I checked those docs first, and started implementing this using EventSource. The problem with it is that EventSource can only send GET requests (see e.g., here, here), whereas our API expects a POST request with a JSON body.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, that's a shame :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants