use std::path::Path;
use std::sync::Arc;
use agent_client_protocol_schema::SessionId;
use defect_agent::session::{AgentCore, Frontend, Session, new_session_id};
use defect_tools::{LocalFsBackend, LocalShellBackend};
#[derive(Debug, Clone)]
pub struct LocalSessionOpts {
pub resume: Option<SessionId>,
pub shell_output_max_bytes: usize,
}
pub async fn open_local_session(
agent: &Arc<dyn AgentCore>,
cwd: &Path,
opts: LocalSessionOpts,
) -> anyhow::Result<Arc<dyn Session>> {
let fs = Arc::new(LocalFsBackend::new(cwd.to_path_buf()));
let shell = Arc::new(LocalShellBackend::with_max_output_bytes(
opts.shell_output_max_bytes,
));
match opts.resume {
Some(id) => agent
.load_session(id, fs, shell, Frontend::Cli)
.await
.map_err(|e| anyhow::anyhow!("load_session failed: {e}")),
None => {
let session_id = SessionId::new(new_session_id());
agent
.create_session(
session_id,
cwd.to_path_buf(),
Vec::new(),
fs,
shell,
Frontend::Cli,
)
.await
.map_err(|e| anyhow::anyhow!("create_session failed: {e}"))
}
}
}