Skip to main content

cognee_lib/
session.rs

1//! Public session management API — thin wrappers over `SessionManager`.
2//!
3//! Mirrors the Python `cognee/api/v1/session/session.py` functions:
4//! `get_session`, `add_feedback`, `delete_feedback`, `get_graph_context`,
5//! `set_graph_context`.
6
7pub use cognee_session::{
8    SessionContext, SessionError, SessionManager, SessionQAEntry, SessionQAUpdate, SessionStore,
9    UsedGraphElementIds,
10};
11
12/// Retrieve Q&A history from a session.
13///
14/// If `last_n` is `Some(n)`, only the most recent `n` entries are returned.
15/// Otherwise all entries are returned. Delegates to the underlying store
16/// via the manager, using the explicit `session_id` (not the manager's default).
17pub async fn get_session(
18    store: &dyn SessionStore,
19    session_id: &str,
20    user_id: Option<&str>,
21    last_n: Option<usize>,
22) -> Result<Vec<SessionQAEntry>, SessionError> {
23    if let Some(n) = last_n {
24        store.get_latest_qa_entries(session_id, user_id, n).await
25    } else {
26        store.get_all_qa_entries(session_id, user_id).await
27    }
28}
29
30/// Add feedback (text and/or score) to a Q&A entry.
31///
32/// Returns `true` if the entry was found and updated.
33pub async fn add_feedback(
34    manager: &SessionManager,
35    session_id: &str,
36    qa_id: &str,
37    user_id: Option<&str>,
38    feedback_text: Option<&str>,
39    feedback_score: Option<i32>,
40) -> Result<bool, SessionError> {
41    manager
42        .add_feedback(
43            Some(session_id),
44            user_id,
45            qa_id,
46            feedback_text,
47            feedback_score,
48        )
49        .await
50}
51
52/// Clear feedback from a Q&A entry.
53///
54/// Returns `true` if the entry was found and updated.
55pub async fn delete_feedback(
56    manager: &SessionManager,
57    session_id: &str,
58    qa_id: &str,
59    user_id: Option<&str>,
60) -> Result<bool, SessionError> {
61    manager
62        .delete_feedback(Some(session_id), user_id, qa_id)
63        .await
64}
65
66/// Retrieve the graph knowledge snapshot for a session.
67pub async fn get_graph_context(
68    manager: &SessionManager,
69    session_id: &str,
70    user_id: Option<&str>,
71) -> Result<Option<String>, SessionError> {
72    manager.get_graph_context(Some(session_id), user_id).await
73}
74
75/// Store (or overwrite) the graph knowledge snapshot for a session.
76pub async fn set_graph_context(
77    manager: &SessionManager,
78    session_id: &str,
79    user_id: Option<&str>,
80    context: &str,
81) -> Result<(), SessionError> {
82    manager
83        .set_graph_context(Some(session_id), user_id, context)
84        .await
85}