Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions apps/maple-agent/app/locales/de/app.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1569,8 +1569,6 @@ chat-folder-path = Ordnerpfad
chat-folder-shown = Angezeigte Ordner: { $path }
chat-full-output = Vollständige Ausgabe anzeigen
chat-conversation = Gespräch
chat-history-loading = Verlauf wird geladen…
chat-history-older = Ältere Nachrichten laden
chat-history-newer = Neuere Nachrichten laden
chat-activity-outside = Weitere laufende Aktivitäten liegen außerhalb dieser Ansicht
chat-reload-latest = Neuesten Stand laden
Expand Down
2 changes: 0 additions & 2 deletions apps/maple-agent/app/locales/en/app.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1569,8 +1569,6 @@ chat-folder-path = Folder path
chat-folder-shown = Folders shown: { $path }
chat-full-output = Show full output
chat-conversation = Conversation
chat-history-loading = Loading history…
chat-history-older = Load older messages
chat-history-newer = Load newer messages
chat-activity-outside = Additional live activity is outside this view
chat-reload-latest = Reload latest
Expand Down
2 changes: 0 additions & 2 deletions apps/maple-agent/app/locales/es/app.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1456,8 +1456,6 @@ chat-folder-path = Ruta de carpeta
chat-folder-shown = Carpetas mostradas: { $path }
chat-full-output = Mostrar salida completa
chat-conversation = Conversación
chat-history-loading = Cargando historial…
chat-history-older = Cargar mensajes anteriores
chat-history-newer = Cargar mensajes más recientes
chat-activity-outside = Hay más actividad en vivo fuera de esta vista
chat-reload-latest = Recargar lo más reciente
Expand Down
2 changes: 0 additions & 2 deletions apps/maple-agent/app/locales/ko/app.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,6 @@ chat-folder-path = 폴더 경로
chat-folder-shown = 표시된 폴더: { $path }
chat-full-output = 전체 출력 보기
chat-conversation = 대화
chat-history-loading = 기록 불러오는 중…
chat-history-older = 이전 메시지 불러오기
chat-history-newer = 최신 메시지 불러오기
chat-activity-outside = 이 보기 밖에 진행 중인 활동이 더 있어요
chat-reload-latest = 최신 내용 다시 불러오기
Expand Down
158 changes: 122 additions & 36 deletions apps/maple-agent/app/src/ui/chat/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub(super) struct TranscriptPages {
pub after: bool,
pub epoch: Option<i64>,
pub loading: bool,
/// The row count older history was last requested for near the top.
/// Leaving the top rearms it, so a failure is not retried every frame.
pub top_load: Option<usize>,
pub live_has_more: bool,
pub generation: u64,
pub expanding: HashSet<String>,
Expand Down Expand Up @@ -155,7 +158,11 @@ impl ChatScreen {
}

pub(super) fn load_history_page(&mut self, older: bool, cx: &mut Context<Self>) {
if self.pages.loading || (older && !self.pages.before) || (!older && !self.pages.after) {
if self.pages.loading
|| (older && !self.pages.before)
|| (!older && !self.pages.after)
|| (older && self.list_state.is_scrollbar_dragging())
{
return;
}
let Some(task) = self.selected_session.clone() else {
Expand Down Expand Up @@ -196,41 +203,7 @@ impl ChatScreen {
return;
}
match result {
Ok(page) => {
if this.pages.epoch != Some(page.rows_epoch) {
this.reload_timeline(&target, cx);
return;
}
let anchor = this.scroll_anchor();
let mut items = Vec::with_capacity(page.rows.len());
for row in page.rows {
let key =
format!("stored:{}:{}:{}", target, row.rows_epoch, row.row_seq);
if let Some(summary) = row.summary.clone() {
this.tool_summaries.insert(key, summary.into());
}
items.push(normalize(crate::backend::persisted_item(&target, row)));
}
if older {
this.pages.before = page.next.is_some();
} else {
this.pages.after = page.next.is_some();
}
let known: HashSet<_> =
this.timeline.iter().map(|item| item.id.as_str()).collect();
items.retain(|item| !known.contains(item.id.as_str()));
if older {
items.append(&mut this.timeline);
this.timeline = items;
} else {
this.timeline.extend(items);
}
this.bound_transcript_window(!older);
this.rebuild_window(anchor);
this.warm_markdown(cx);
this.load_attachment_images(cx);
cx.notify();
}
Ok(page) => this.apply_history_page(older, &target, page, cx),
Err(error) => {
if matches!(&error, UiFailure::Host(failure) if matches!(failure.code.as_str(), "paging-changed" | "storage-epoch"))
|| error.raw_message().is_some_and(|raw| raw.contains("epoch") || raw.contains("changed")) {
Expand All @@ -245,6 +218,54 @@ impl ChatScreen {
);
}

/// Apply a reply from the currently selected task and page generation.
fn apply_history_page(
&mut self,
older: bool,
target: &str,
page: maple_agent::store::HistoryPage,
cx: &mut Context<Self>,
) {
// A reset clears GPUI's frozen drag height while the scrollbar
// still holds its original thumb coordinates. Drop the reply and
// rearm prefetch instead; mouse-up renders and fetches it again.
if older && self.list_state.is_scrollbar_dragging() {
self.pages.top_load = None;
return;
}
if self.pages.epoch != Some(page.rows_epoch) {
self.reload_timeline(target, cx);
return;
}
let anchor = self.scroll_anchor();
let mut items = Vec::with_capacity(page.rows.len());
for row in page.rows {
let key = format!("stored:{}:{}:{}", target, row.rows_epoch, row.row_seq);
if let Some(summary) = row.summary.clone() {
self.tool_summaries.insert(key, summary.into());
}
items.push(normalize(crate::backend::persisted_item(target, row)));
}
if older {
self.pages.before = page.next.is_some();
} else {
self.pages.after = page.next.is_some();
}
let known: HashSet<_> = self.timeline.iter().map(|item| item.id.as_str()).collect();
items.retain(|item| !known.contains(item.id.as_str()));
if older {
items.append(&mut self.timeline);
self.timeline = items;
} else {
self.timeline.extend(items);
}
self.bound_transcript_window(!older);
self.rebuild_window(anchor);
self.warm_markdown(cx);
self.load_attachment_images(cx);
cx.notify();
}

pub(super) fn scroll_anchor(&self) -> Option<(String, ListOffset)> {
let offset = self.list_state.logical_scroll_top();
self.timeline
Expand Down Expand Up @@ -500,6 +521,71 @@ mod tests {
}
}

fn history_page(row_seq: i64, next: Option<&str>) -> maple_agent::store::HistoryPage {
maple_agent::store::HistoryPage {
rows: vec![maple_agent::store::TimelineRow {
rows_epoch: 0,
row_seq,
update_seq: row_seq,
item: message("Older"),
summary: None,
summary_truncated: None,
}],
next: next.map(str::to_string),
rows_epoch: 0,
}
}

#[gpui::test]
fn an_older_page_keeps_the_anchor_when_it_prepends(cx: &mut gpui::TestAppContext) {
let chat = chat_with_task(cx);
chat.update(cx, |chat, cx| {
chat.replace_timeline(vec![stored(100, "Current")]);
chat.pages.epoch = Some(0);
chat.pages.before = true;
chat.list_state.scroll_to(ListOffset {
item_ix: 0,
offset_in_item: gpui::px(23.),
});
let anchored = chat.timeline[0].id.clone();

chat.apply_history_page(true, "task", history_page(99, None), cx);

assert_eq!(chat.timeline.len(), 2);
assert_eq!(chat.timeline[1].id, anchored);
assert!(!chat.pages.before);
let offset = chat.list_state.logical_scroll_top();
assert_eq!(offset.item_ix, 1);
assert_eq!(offset.offset_in_item, gpui::px(23.));
});
}

#[gpui::test]
fn an_older_page_landing_mid_drag_is_retried_after_release(cx: &mut gpui::TestAppContext) {
let chat = chat_with_task(cx);
chat.update(cx, |chat, cx| {
chat.replace_timeline(vec![stored(100, "Current")]);
chat.pages.epoch = Some(0);
chat.pages.before = true;
chat.pages.top_load = Some(1);
chat.list_state.scrollbar_drag_started();

chat.apply_history_page(true, "task", history_page(99, Some("more")), cx);

assert_eq!(chat.timeline.len(), 1, "the drag's list is untouched");
assert!(chat.pages.before);
assert_eq!(chat.pages.top_load, None);
assert!(chat.list_state.is_scrollbar_dragging());

chat.list_state.scrollbar_drag_ended();
chat.load_history_page(true, cx);
assert!(
chat.pages.loading,
"mouse-up allows the replacement request"
);
});
}

/// The running total must match a fresh sum over the loaded rows.
#[track_caller]
fn assert_weights_exact(chat: &ChatScreen) {
Expand Down
87 changes: 87 additions & 0 deletions apps/maple-agent/app/src/ui/chat/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12117,6 +12117,11 @@ mod state_tests {
cx.update(|_window, app| chat.read(app).list_state.is_following_tail())
}

fn redraw_chat(chat: &Entity<ChatScreen>, cx: &mut gpui::VisualTestContext) {
chat.update(cx, |_, cx| cx.notify());
cx.run_until_parked();
}

/// In the 1200 px test window the transcript and the composer share
/// the middle 900 px column. A wheel that nothing under the pointer
/// takes scrolls the transcript exactly as far as a wheel over the
Expand Down Expand Up @@ -12221,6 +12226,88 @@ mod state_tests {
);
}

/// Reaching the top during a scrollbar drag waits for mouse-up, then
/// loads older history without leaving a button above the transcript.
#[gpui::test]
fn test_scrollbar_drag_defers_older_history_until_release(cx: &mut TestAppContext) {
let (chat, cx) = chat_window(cx, |chat, _cx| {
chat.sidebar_collapsed = true;
let rows = long_timeline()
.into_iter()
.enumerate()
.map(|(seq, mut item)| {
item.persisted = Some(maple_agent::agent::AgentPersistedRowIdentity {
task_id: "s1".to_string(),
rows_epoch: 1,
row_seq: seq as i64 + 100,
update_seq: 0,
});
item
})
.collect();
chat.replace_timeline(rows);
chat.pages.before = true;
});
redraw_chat(&chat, cx);
chat.update(cx, |chat, _| {
assert!(!chat.pages.loading && chat.pages.top_load.is_none());
assert!(chat.notice.is_none());
});
assert!(cx.debug_bounds("history-older").is_none());

// A scrollbar drag moves the list without a wheel event.
chat.update(cx, |chat, _| {
chat.list_state.scrollbar_drag_started();
chat.list_state.scroll_to(gpui::ListOffset {
item_ix: 0,
offset_in_item: px(0.),
});
});
redraw_chat(&chat, cx);
chat.update(cx, |chat, _| {
assert!(!chat.pages.loading && chat.pages.top_load.is_none());
assert!(chat.notice.is_none());
});

// The scrollbar's mouse-up releases the drag and notifies the view.
chat.update(cx, |chat, _| chat.list_state.scrollbar_drag_ended());
redraw_chat(&chat, cx);
redraw_chat(&chat, cx);
chat.update(cx, |chat, _| {
// Still in flight, or already refused by the test backend.
assert!(
chat.pages.loading || chat.notice.is_some(),
"releasing the drag asks for the older page"
);
});
}

/// A short loaded page is already at the top and cannot scroll to ask.
#[gpui::test]
fn test_unscrollable_transcript_loads_older_history(cx: &mut TestAppContext) {
let (chat, cx) = chat_window(cx, |chat, _cx| {
chat.sidebar_collapsed = true;
let mut row = item("m0", "message", Some("Latest loaded message"));
row.persisted = Some(maple_agent::agent::AgentPersistedRowIdentity {
task_id: "s1".to_string(),
rows_epoch: 1,
row_seq: 100,
update_seq: 0,
});
chat.replace_timeline(vec![row]);
chat.pages.before = true;
});
redraw_chat(&chat, cx);
redraw_chat(&chat, cx);
chat.update(cx, |chat, _| {
assert!(
chat.pages.loading || chat.notice.is_some(),
"an unscrollable page asks for older history"
);
});
assert!(cx.debug_bounds("history-older").is_none());
}

/// At the bottom the list's scroll top names no row. A wheel beside
/// the composer still reports the rows on screen, the same rows the
/// list's own wheel reports, so images in view keep loading.
Expand Down
36 changes: 21 additions & 15 deletions apps/maple-agent/app/src/ui/chat/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ impl ChatScreen {
self.list_state
.reset_with_uniform_height(count, super::TRANSCRIPT_ROW_ESTIMATE);
}
let following = self.list_state.is_following_tail();
// Load just before the reader reaches the top, however they got
// there (wheel, scrollbar, keys), or when the loaded rows all fit
// and cannot scroll to ask. Once per visit, so a failure does not
// retry on every frame.
let laid_out = self.list_state.viewport_bounds().size.height > px(0.);
let near_top = laid_out
&& (self.list_state.max_offset_for_scrollbar().y <= px(0.)
|| (!following && self.list_state.logical_scroll_top().item_ix < 5));
if !near_top {
self.pages.top_load = None;
} else if self.pages.before
&& !self.pages.loading
&& !self.list_state.is_scrollbar_dragging()
&& count > 0
&& self.pages.top_load != Some(count)
{
self.pages.top_load = Some(count);
self.load_history_page(true, cx);
}
// Entrances read one clock for the whole frame and ask for the next
// frame only while one is still running.
let now = motion::now();
Expand All @@ -52,7 +72,7 @@ impl ChatScreen {
window.request_animation_frame();
}
let switch_reveal = self.row_motion.switch_progress(now, reduced);
let show_jump = count > 0 && !self.list_state.is_following_tail();
let show_jump = count > 0 && !following;
let tool_details = self.tool_details;
let entity = cx.entity().downgrade();
let selection = self.selection.clone();
Expand Down Expand Up @@ -235,20 +255,6 @@ impl ChatScreen {
}),
)
.children(self.render_transcript_menu(window, cx))
.when(self.pages.before, |container| {
container.child(
widgets::secondary_button("history-older")
.mx_6()
.my_1()
.text_xs()
.child(gpui::text!(if self.pages.loading {
tr("chat-history-loading")
} else {
tr("chat-history-older")
}))
.on_click(cx.listener(|chat, _, _, cx| chat.load_history_page(true, cx))),
)
})
.child(
// The list element does not apply padding itself, so the
// gutter lives here. Same column width as the composer. On a
Expand Down
2 changes: 0 additions & 2 deletions apps/maple-agent/app/translation-revisions/de.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ chat-group-connection 09d63de97c6b2af9
chat-group-list 5daca6e6c8b45508
chat-group-project 4ba8a89f51963833
chat-group-status f494c2b5fca99715
chat-history-loading 1737c0bffae15abe
chat-history-newer a83b2253a8cc1acc
chat-history-older 7203b7f6a0e8f096
chat-host-received a8309854280b240e
chat-host-reconnecting-detail 2a59c5a9aa782dea
chat-host-refreshing b9b36962bfae375e
Expand Down
Loading
Loading