1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Paging back through a session's saved conversation
//! (`rho/session/history`), for a client whose attach a window bounded
//! ([`rho_protocol::ReplayWindow`]). A loaded session's page comes from its
//! feed's transcript; one the daemon has not loaded is read from its file as
//! an observe reads it, without loading it, once per page: about 75 ms for
//! an 18 MB session file, against 11 ms for the same page loaded. A session
//! an agent runtime ran has no saved entries to page through.
use rho_protocol::{ErrorCode, RpcError, SessionHistoryParams, SessionHistoryResponse};
use super::{locate::Located, reopen::parse_session, unloaded::Unloaded, Sessions};
impl Sessions {
/// A page of the session's saved conversation through `params.through`
/// ([`super::Feed::history`]), rendered off the async workers.
pub(in crate::app::daemon_host) async fn history(
&self,
params: SessionHistoryParams,
) -> Result<SessionHistoryResponse, RpcError> {
self.refuse_when_stopping()?;
let id = parse_session(¶ms.session)?;
let page = match self.locate(&id).await {
Located::Loaded(session) => {
session
.feed
.history(¶ms.through, params.max_bytes, params.defer_tool_output)?
}
// A run's stream keeps no saved entries to page through, so a
// session a run writes is read from its file, once the run
// lets it be.
Located::Unloaded { run: _, reopening } => {
match self.read_unloaded(&reopening).await? {
Unloaded::Engine { .. } => {
return Err(RpcError::new(
ErrorCode::ConversationMoved,
format!(
"session {id} is run by an agent, whose journal names no saved entries"
),
));
}
Unloaded::Saved { feed, .. } => {
feed.history(¶ms.through, params.max_bytes, params.defer_tool_output)?
}
}
}
};
tokio::task::spawn_blocking(move || page.render())
.await
.map_err(|error| {
RpcError::new(
ErrorCode::InternalError,
format!("could not render the history page: {error}"),
)
})
}
}