Skip to main content

prismer_sdk/
memory.rs

1use crate::{PrismerClient, types::*};
2use serde_json::json;
3
4pub struct MemoryClient<'a> {
5    pub(crate) client: &'a PrismerClient,
6}
7
8impl<'a> MemoryClient<'a> {
9    /// Create a memory file.
10    pub async fn create_file(&self, path: &str, content: &str, scope: Option<&str>) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
11        let mut body = json!({ "path": path, "content": content });
12        if let Some(s) = scope { body["scope"] = json!(s); }
13        self.client.request(reqwest::Method::POST, "/api/im/memory/files", Some(body)).await
14    }
15
16    /// List memory files.
17    pub async fn list_files(&self, scope: Option<&str>, path: Option<&str>) -> Result<ApiResponse<Vec<serde_json::Value>>, PrismerError> {
18        let mut params = vec![];
19        if let Some(s) = scope { params.push(format!("scope={}", s)); }
20        if let Some(p) = path { params.push(format!("path={}", p)); }
21        let qs = if params.is_empty() { String::new() } else { format!("?{}", params.join("&")) };
22        self.client.request(reqwest::Method::GET, &format!("/api/im/memory/files{}", qs), None).await
23    }
24
25    /// Get a memory file by ID.
26    pub async fn get_file(&self, file_id: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
27        self.client.request(reqwest::Method::GET, &format!("/api/im/memory/files/{}", file_id), None).await
28    }
29
30    /// Update a memory file.
31    pub async fn update_file(&self, file_id: &str, operation: &str, content: &str, section: Option<&str>, version: Option<i32>) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
32        let mut body = json!({ "operation": operation, "content": content });
33        if let Some(s) = section { body["section"] = json!(s); }
34        if let Some(v) = version { body["version"] = json!(v); }
35        self.client.request(reqwest::Method::PATCH, &format!("/api/im/memory/files/{}", file_id), Some(body)).await
36    }
37
38    /// Delete a memory file.
39    pub async fn delete_file(&self, file_id: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
40        self.client.request(reqwest::Method::DELETE, &format!("/api/im/memory/files/{}", file_id), None).await
41    }
42
43    /// Create a compaction summary.
44    pub async fn compact(&self, conversation_id: &str, summary: &str) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
45        self.client.request(
46            reqwest::Method::POST,
47            "/api/im/memory/compact",
48            Some(json!({ "conversationId": conversation_id, "summary": summary })),
49        ).await
50    }
51
52    /// Load auto-memory (MEMORY.md).
53    pub async fn load(&self, scope: Option<&str>) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
54        let mut params = vec![];
55        if let Some(s) = scope { params.push(format!("scope={}", s)); }
56        let qs = if params.is_empty() { String::new() } else { format!("?{}", params.join("&")) };
57        self.client.request(reqwest::Method::GET, &format!("/api/im/memory/load{}", qs), None).await
58    }
59
60    /// Get memory-gene knowledge links for the authenticated user's memory files (v1.8.0).
61    pub async fn get_knowledge_links(&self) -> Result<ApiResponse<serde_json::Value>, PrismerError> {
62        self.client.request(reqwest::Method::GET, "/api/im/memory/links", None).await
63    }
64}