use super::*;
impl CurrentSessionCapability {
pub(in crate::runtime::session_manager) async fn current_snapshot_for_store_write(
&self,
) -> SessionSnapshot {
let mut state = self.snapshot.to_snapshot();
if let Some(store) = &self.store
&& let Err(err) =
crate::store::refresh_persisted_session_state(store.as_ref(), &mut state).await
{
tracing::warn!("failed to refresh persisted session state: {err}");
}
super::normalize_session_graph(&mut state);
state
}
pub(in crate::runtime::session_manager) async fn snapshot_by_id(
&self,
managed: &ManagedSessionCapability,
session_id: &str,
) -> Result<SessionSnapshot, crate::PluginError> {
if session_id == self.session_id {
let mut snapshot = self.snapshot.to_snapshot();
super::normalize_session_graph(&mut snapshot);
self.enrich_current_snapshot(&mut snapshot);
return Ok(snapshot);
}
let runtime = {
let registry = managed.registry.lock().await;
registry.get(session_id).cloned()
}
.ok_or_else(|| crate::PluginError::Session(format!("unknown session `{session_id}`")))?;
Ok(runtime.observe().persisted_state.clone())
}
pub(in crate::runtime::session_manager) async fn tool_catalog_by_id(
&self,
managed: &ManagedSessionCapability,
session_id: &str,
) -> Result<Vec<serde_json::Value>, crate::PluginError> {
if session_id == self.session_id {
if let Some(runtime) = managed.registry.lock().await.get(session_id).cloned() {
let runtime = runtime.runtime.lock().await;
return Ok(runtime.active_tool_catalog());
}
return Ok(self
.plugins
.tool_catalog(session_id, self.policy.execution_mode.clone()));
}
let runtime = {
let registry = managed.registry.lock().await;
registry.get(session_id).cloned()
}
.ok_or_else(|| crate::PluginError::Session(format!("unknown session `{session_id}`")))?;
let observation = runtime.observe();
Ok(observation.tool_catalog.as_ref().clone())
}
pub(in crate::runtime::session_manager) fn enrich_current_snapshot(
&self,
snapshot: &mut SessionSnapshot,
) {
let tool_state = self.plugins.tool_registry().export_state();
snapshot.tool_state_generation = Some(tool_state.generation());
snapshot.tool_state_snapshot = Some(tool_state);
snapshot.plugin_snapshot = self.plugins.snapshot().ok();
snapshot.plugin_snapshot_revision = Some(self.plugins.snapshot_revision_fingerprint());
}
pub(in crate::runtime::session_manager) fn current_tool_registry(
&self,
) -> Result<Arc<crate::ToolRegistry>, crate::PluginError> {
Ok(self.plugins.tool_registry())
}
pub(in crate::runtime::session_manager) async fn snapshot_current(
&self,
) -> Result<SessionSnapshot, crate::PluginError> {
let mut snapshot = self.snapshot.to_snapshot();
super::normalize_session_graph(&mut snapshot);
self.enrich_current_snapshot(&mut snapshot);
Ok(snapshot)
}
pub(in crate::runtime::session_manager) async fn snapshot_session(
&self,
managed: &ManagedSessionCapability,
session_id: &str,
) -> Result<SessionSnapshot, crate::PluginError> {
self.snapshot_by_id(managed, session_id).await
}
pub(in crate::runtime::session_manager) async fn tool_catalog(
&self,
managed: &ManagedSessionCapability,
session_id: &str,
) -> Result<Vec<serde_json::Value>, crate::PluginError> {
self.tool_catalog_by_id(managed, session_id).await
}
pub(in crate::runtime::session_manager) async fn tool_state(
&self,
managed: &ManagedSessionCapability,
session_id: &str,
) -> Result<crate::ToolState, crate::PluginError> {
if session_id == self.session_id {
if let Some(runtime) = managed.registry.lock().await.get(session_id).cloned() {
return runtime.observe().tool_state.clone().ok_or_else(|| {
crate::PluginError::Session("runtime session not available".to_string())
});
}
return Ok(self.current_tool_registry()?.export_state());
}
let runtime = {
let registry = managed.registry.lock().await;
registry.get(session_id).cloned()
}
.ok_or_else(|| crate::PluginError::Session(format!("unknown session `{session_id}`")))?;
runtime
.observe()
.tool_state
.clone()
.ok_or_else(|| crate::PluginError::Session("runtime session not available".to_string()))
}
pub(in crate::runtime::session_manager) async fn apply_tool_state(
&self,
managed: &ManagedSessionCapability,
session_id: &str,
snapshot: crate::ToolState,
) -> Result<u64, crate::PluginError> {
if session_id == self.session_id {
if let Some(runtime) = managed.registry.lock().await.get(session_id).cloned() {
let mut writer = runtime.runtime.lock().await;
let generation = writer
.apply_tool_state(snapshot)
.await
.map_err(|err| crate::PluginError::Session(err.to_string()))?;
runtime.publish_from(&writer);
return Ok(generation);
}
let tool_registry = self.current_tool_registry()?;
return tool_registry
.apply_state(snapshot)
.map_err(|err| crate::PluginError::Session(err.to_string()));
}
let runtime = {
let registry = managed.registry.lock().await;
registry.get(session_id).cloned()
}
.ok_or_else(|| crate::PluginError::Session(format!("unknown session `{session_id}`")))?;
let mut writer = runtime.runtime.lock().await;
let generation = writer
.apply_tool_state(snapshot)
.await
.map_err(|err| crate::PluginError::Session(err.to_string()))?;
runtime.publish_from(&writer);
Ok(generation)
}
pub(in crate::runtime::session_manager) async fn emit_trace_event(
&self,
context: lash_trace::TraceContext,
event: lash_trace::TraceEvent,
) -> Result<(), crate::PluginError> {
crate::trace::emit_trace(
&self.host.core.trace_sink,
&self.host.core.trace_context,
context.for_session(self.session_id.clone()),
event,
);
Ok(())
}
}