Skip to main content

cognee_session/
types.rs

1use std::collections::HashMap;
2
3use chrono::{DateTime, Utc};
4use cognee_llm::Message;
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8/// Graph element IDs that were used to produce the answer for a Q&A entry.
9///
10/// Matches the Python `used_graph_element_ids` dict: `{"node_ids": [...], "edge_ids": [...]}`.
11#[derive(Debug, Clone, Serialize, Deserialize, Default)]
12pub struct UsedGraphElementIds {
13    #[serde(default)]
14    pub node_ids: Vec<String>,
15    #[serde(default)]
16    pub edge_ids: Vec<String>,
17}
18
19/// A single question-answer entry stored in a session.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct SessionQAEntry {
22    pub id: Uuid,
23    pub session_id: String,
24    pub user_id: Option<String>,
25    pub question: String,
26    pub answer: String,
27    pub context: Option<String>,
28    pub created_at: DateTime<Utc>,
29    /// User-provided feedback text for this Q&A entry.
30    #[serde(default)]
31    pub feedback_text: Option<String>,
32    /// User-provided feedback score (1-5 rating, validated on write).
33    #[serde(default)]
34    pub feedback_score: Option<i32>,
35    /// Graph node/edge IDs that were used to produce the answer.
36    #[serde(default)]
37    pub used_graph_element_ids: Option<UsedGraphElementIds>,
38    /// Metadata tracking for the memify pipeline (e.g. `{"feedback_weights_applied": true}`).
39    #[serde(default)]
40    pub memify_metadata: Option<HashMap<String, bool>>,
41}
42
43/// Session context passed to retrievers: the session ID and any loaded
44/// conversation history (as LLM messages).
45#[derive(Debug, Clone, Default)]
46pub struct SessionContext {
47    pub session_id: Option<String>,
48    pub history: Vec<Message>,
49    pub formatted_history: String,
50    /// Stored knowledge-graph snapshot to prepend to history.
51    ///
52    /// Set by `improve()` stage 4 (`sync_graph_to_session`) and loaded by the
53    /// search orchestrator before retrieval, so follow-up questions benefit from
54    /// prior graph knowledge. Matches Python's `get_graph_context` / prepend
55    /// logic in `session_manager.py:435-450`.
56    pub graph_context: Option<String>,
57}
58
59/// One agent-trace step persisted in a session — mirrors Python's
60/// `SessionAgentTraceEntry` (no `created_at`; ordering is positional).
61///
62/// Library-internal type; kept `snake_case` to match Python's persisted JSON
63/// shape so cross-SDK reads stay byte-equal. This is **not** an HTTP DTO —
64/// the wire-side camelCase rule (Decision 10) does not apply here.
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct SessionTraceStep {
67    /// Server-generated UUID4 — returned by `SessionManager::add_agent_trace_step`.
68    pub trace_id: String,
69    pub origin_function: String,
70    /// Free-form per Python validator; typically `"success"` / `"error"`.
71    pub status: String,
72    #[serde(default)]
73    pub memory_query: String,
74    #[serde(default)]
75    pub memory_context: String,
76    /// Default `{}` — matches Python's `default_factory=dict`.
77    #[serde(default = "empty_object")]
78    pub method_params: serde_json::Value,
79    #[serde(default)]
80    pub method_return_value: Option<serde_json::Value>,
81    #[serde(default)]
82    pub error_message: String,
83    /// LLM-resolved feedback string — generation owned by `SessionManager`
84    /// (LIB-01); callers in this task pass `""` until LIB-01 lands.
85    #[serde(default)]
86    pub session_feedback: String,
87}
88
89fn empty_object() -> serde_json::Value {
90    serde_json::Value::Object(serde_json::Map::new())
91}