areev 0.2.0

Rust SDK for the Areev knowledge database — gRPC and HTTP transports
Documentation
//! `Sessions` resource — agent-session context: bootstrap, state,
//! actions, goals, context compilation, and consolidation.
//!
//! Mirrors the Python SDK's `client.sessions.*` surface.

use serde_json::{json, Value};

use crate::error::Result;
use crate::http::HttpClient;

/// Session context operations.
///
/// Access via [`crate::Areev::sessions`].
pub struct Sessions<'a> {
    http: &'a HttpClient,
    memory_id: String,
}

impl<'a> Sessions<'a> {
    /// Internal constructor — use [`crate::Areev::sessions`].
    pub(crate) fn new(http: &'a HttpClient, memory_id: String) -> Self {
        Self { http, memory_id }
    }

    /// Compile the initial context bundle for a fresh agent session.
    /// `opts` is added as query parameters.
    pub async fn bootstrap(&self, session_id: &str, opts: Option<&Value>) -> Result<Value> {
        let path = format!(
            "/memories/{}/sessions/{}/bootstrap",
            self.memory_id, session_id
        );
        self.http._get(&path, opts).await
    }

    /// Latest State grain for the session.
    pub async fn state(&self, session_id: &str) -> Result<Value> {
        let path = format!("/memories/{}/sessions/{}/state", self.memory_id, session_id);
        self.http._get(&path, None).await
    }

    /// Diff two State grains by hash.
    pub async fn diff_states(&self, old_hash: &str, new_hash: &str) -> Result<Value> {
        let body = json!({ "old_hash": old_hash, "new_hash": new_hash });
        let path = format!("/memories/{}/states/diff", self.memory_id);
        self.http._post(&path, Some(&body)).await
    }

    /// Recent Tool grains for the session. `opts` is added as query
    /// parameters.
    pub async fn recent_tools(&self, session_id: &str, opts: Option<&Value>) -> Result<Value> {
        let path = format!(
            "/memories/{}/sessions/{}/tools",
            self.memory_id, session_id
        );
        self.http._get(&path, opts).await
    }

    /// Failed Tool grains for the session. `opts` is added as query
    /// parameters.
    pub async fn failed_tools(&self, session_id: &str, opts: Option<&Value>) -> Result<Value> {
        let path = format!(
            "/memories/{}/sessions/{}/tools/failed",
            self.memory_id, session_id
        );
        self.http._get(&path, opts).await
    }

    /// Aggregate action statistics for the session. `opts` is added as
    /// query parameters.
    pub async fn tool_summary(&self, session_id: &str, opts: Option<&Value>) -> Result<Value> {
        let path = format!(
            "/memories/{}/sessions/{}/tools/summary",
            self.memory_id, session_id
        );
        self.http._get(&path, opts).await
    }

    /// Causal action chain for `task_id` within the session. `opts` is
    /// added as query parameters.
    pub async fn tool_chain(
        &self,
        session_id: &str,
        task_id: &str,
        opts: Option<&Value>,
    ) -> Result<Value> {
        let path = format!(
            "/memories/{}/sessions/{}/tools/chain/{}",
            self.memory_id, session_id, task_id
        );
        self.http._get(&path, opts).await
    }

    /// Active (non-terminal) goals for the session.
    pub async fn active_goals(&self, session_id: &str) -> Result<Value> {
        let path = format!("/memories/{}/sessions/{}/goals", self.memory_id, session_id);
        self.http._get(&path, None).await
    }

    /// Compile a recall-ready context window for the session. `opts` is
    /// added as query parameters.
    pub async fn context(&self, session_id: &str, opts: Option<&Value>) -> Result<Value> {
        let path = format!(
            "/memories/{}/sessions/{}/context",
            self.memory_id, session_id
        );
        self.http._get(&path, opts).await
    }

    /// Consolidate the session's grains (dedup / summarize). Emits an
    /// audit event.
    pub async fn consolidate(&self, session_id: &str, opts: Option<Value>) -> Result<Value> {
        let body = opts.unwrap_or_else(|| json!({}));
        let path = format!(
            "/memories/{}/sessions/{}/consolidate",
            self.memory_id, session_id
        );
        self.http._post(&path, Some(&body)).await
    }
}