Stream the report to the terminal as the writer generates it #5
1 changed files with 3 additions and 7 deletions
|
|
@ -5,7 +5,6 @@ use clap::Parser;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use rig::agent::MultiTurnStreamItem;
|
use rig::agent::MultiTurnStreamItem;
|
||||||
use rig::client::{AgentClientExt, Nothing};
|
use rig::client::{AgentClientExt, Nothing};
|
||||||
use rig::message::Text;
|
|
||||||
use rig::providers::ollama;
|
use rig::providers::ollama;
|
||||||
use rig::streaming::{StreamedAssistantContent, StreamingPrompt};
|
use rig::streaming::{StreamedAssistantContent, StreamingPrompt};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
@ -186,15 +185,12 @@ async fn write_report(
|
||||||
|
|
||||||
while let Some(chunk) = response_stream.next().await {
|
while let Some(chunk) = response_stream.next().await {
|
||||||
match chunk? {
|
match chunk? {
|
||||||
MultiTurnStreamItem::StreamAssistantItem(StreamedAssistantContent::Text(Text {
|
MultiTurnStreamItem::StreamAssistantItem(StreamedAssistantContent::Text(text)) => {
|
||||||
text,
|
report.push_str(&text.text);
|
||||||
..
|
|
||||||
})) => {
|
|
||||||
report.push_str(&text);
|
|
||||||
// Terminal stdout is line-buffered, so a flush is needed here —
|
// Terminal stdout is line-buffered, so a flush is needed here —
|
||||||
// otherwise a chunk without a trailing newline sits in the
|
// otherwise a chunk without a trailing newline sits in the
|
||||||
// buffer instead of appearing as it streams in.
|
// buffer instead of appearing as it streams in.
|
||||||
write!(handle, "{text}")?;
|
write!(handle, "{}", text.text)?;
|
||||||
handle.flush()?;
|
handle.flush()?;
|
||||||
}
|
}
|
||||||
_ => continue,
|
_ => continue,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue