127 lines
4.7 KiB
Rust
127 lines
4.7 KiB
Rust
//! Transcript consolidation for finalized streaming agent messages.
|
|
//!
|
|
//! During streaming, the chat widget emits transient `AgentMessageCell`s so it
|
|
//! can animate stable lines into scrollback while keeping the active mutable
|
|
//! tail in the bottom pane. Once the answer finishes, the app replaces that
|
|
//! trailing run with a single source-backed `AgentMarkdownCell`. This makes the
|
|
//! transcript the canonical owner of the raw markdown source used for future
|
|
//! resize re-renders. The retained view preserves its displayed revision before that
|
|
//! replacement, then repaints without replaying terminal scrollback.
|
|
|
|
use std::ops::Range;
|
|
use std::sync::Arc;
|
|
|
|
use color_eyre::eyre::Result;
|
|
|
|
use super::App;
|
|
use super::resize_reflow::trailing_run_start;
|
|
use crate::app_event::ConsolidationScrollbackReflow;
|
|
use crate::history_cell;
|
|
use crate::history_cell::HistoryCell;
|
|
use crate::pager_overlay::Overlay;
|
|
use crate::tui;
|
|
|
|
impl App {
|
|
pub(super) fn handle_consolidate_agent_message(
|
|
&mut self,
|
|
tui: &mut tui::Tui,
|
|
source: history_cell::AgentMarkdownCell,
|
|
scrollback_reflow: ConsolidationScrollbackReflow,
|
|
deferred_history_cell: Option<Box<dyn HistoryCell>>,
|
|
) -> Result<()> {
|
|
// Some finalize paths must preserve a last provisional stream cell long
|
|
// enough for queue ordering, then fold it into the canonical
|
|
// source-backed cell during consolidation.
|
|
if let Some(cell) = deferred_history_cell {
|
|
let cell: Arc<dyn HistoryCell> = cell.into();
|
|
if let Some(Overlay::Transcript(t)) = &mut self.overlay {
|
|
t.insert_cell(cell.clone());
|
|
}
|
|
if !tui.is_owned_screen() {
|
|
self.native_history.defer(&cell);
|
|
}
|
|
self.transcript_cells.push(cell);
|
|
}
|
|
|
|
// Walk backward to find the contiguous run of streaming AgentMessageCells that
|
|
// belong to the just-finalized stream.
|
|
let end = self.transcript_cells.len();
|
|
tracing::debug!(
|
|
"ConsolidateAgentMessage: transcript_cells.len()={end}, source_len={}",
|
|
source.copy_source().map_or(/*default*/ 0, str::len)
|
|
);
|
|
let start = trailing_run_start::<history_cell::AgentMessageCell>(&self.transcript_cells);
|
|
self.replace_agent_message(tui, start..end, source, scrollback_reflow)
|
|
}
|
|
|
|
pub(super) fn replace_agent_message(
|
|
&mut self,
|
|
tui: &mut tui::Tui,
|
|
range: Range<usize>,
|
|
source: history_cell::AgentMarkdownCell,
|
|
scrollback_reflow: ConsolidationScrollbackReflow,
|
|
) -> Result<()> {
|
|
let Range { start, end } = range;
|
|
if start < end {
|
|
tracing::debug!(
|
|
"ConsolidateAgentMessage: replacing cells [{start}..{end}] with AgentMarkdownCell"
|
|
);
|
|
let consolidated: Arc<dyn HistoryCell> = Arc::new(source);
|
|
self.native_history
|
|
.consolidate(&self.transcript_cells[start..end], &consolidated);
|
|
if tui.is_owned_screen() {
|
|
self.transcript_view.replace_group(
|
|
&self.transcript_cells,
|
|
start..end,
|
|
&consolidated,
|
|
);
|
|
} else {
|
|
self.transcript_view.replace_range(
|
|
&self.transcript_cells,
|
|
start..end,
|
|
&consolidated,
|
|
);
|
|
}
|
|
self.transcript_cells
|
|
.splice(start..end, std::iter::once(consolidated.clone()));
|
|
|
|
if let Some(Overlay::Transcript(t)) = &mut self.overlay {
|
|
t.regroup_cells(start..end, consolidated.clone());
|
|
tui.frame_requester().schedule_frame();
|
|
}
|
|
|
|
self.finish_agent_message_consolidation(tui, scrollback_reflow)?;
|
|
} else {
|
|
tracing::debug!(
|
|
"ConsolidateAgentMessage: no cells to consolidate(start={start}, end={end})",
|
|
);
|
|
self.maybe_finish_stream_reflow(tui)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn finish_agent_message_consolidation(
|
|
&mut self,
|
|
tui: &mut tui::Tui,
|
|
scrollback_reflow: ConsolidationScrollbackReflow,
|
|
) -> Result<()> {
|
|
if tui.is_owned_screen() {
|
|
self.transcript_reflow.clear_pending_reflow();
|
|
self.transcript_reflow.clear_stream_flags();
|
|
tui.frame_requester().schedule_frame();
|
|
return Ok(());
|
|
}
|
|
match scrollback_reflow {
|
|
ConsolidationScrollbackReflow::IfResizeReflowRan => {
|
|
self.maybe_finish_stream_reflow(tui)?;
|
|
}
|
|
ConsolidationScrollbackReflow::Required => {
|
|
self.finish_required_stream_reflow(tui)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|