1use std::path::PathBuf;
2use std::sync::Arc;
3
4use crate::error::{Error, Result};
5use crate::watch::WatchBuilder;
6use agtrace_index::SessionSummary;
7
8pub struct Client {
9 inner: Arc<agtrace_runtime::AgTrace>,
10}
11
12impl Client {
13 pub fn connect(path: impl Into<PathBuf>) -> Result<Self> {
14 let path = path.into();
15 let runtime = agtrace_runtime::AgTrace::open(path).map_err(Error::Internal)?;
16 Ok(Self {
17 inner: Arc::new(runtime),
18 })
19 }
20
21 pub fn watch(&self) -> WatchBuilder {
22 WatchBuilder::new(self.inner.clone())
23 }
24
25 pub fn session(&self, id: &str) -> SessionHandle {
26 SessionHandle {
27 client_inner: self.inner.clone(),
28 id: id.to_string(),
29 }
30 }
31
32 pub fn list_sessions(&self) -> Result<Vec<SessionSummary>> {
33 let filter = agtrace_runtime::SessionFilter::new().limit(100);
34 self.inner.sessions().list(filter).map_err(Error::Internal)
35 }
36}
37
38pub struct SessionHandle {
39 client_inner: Arc<agtrace_runtime::AgTrace>,
40 id: String,
41}
42
43impl SessionHandle {
44 pub fn events(&self) -> Result<Vec<agtrace_types::event::AgentEvent>> {
45 let session_handle = self
46 .client_inner
47 .sessions()
48 .find(&self.id)
49 .map_err(|e| Error::NotFound(format!("Session {}: {}", self.id, e)))?;
50
51 session_handle.events().map_err(Error::Internal)
52 }
53
54 pub fn summary(&self) -> Result<agtrace_engine::SessionSummary> {
55 let events = self.events()?;
56 let session = agtrace_engine::assemble_session(&events)
57 .ok_or_else(|| Error::Internal(anyhow::anyhow!("Failed to assemble session")))?;
58 Ok(agtrace_engine::session::summarize(&session))
59 }
60}