Skip to main content

adk_payments/journal/
memory_index.rs

1use std::sync::Arc;
2
3use adk_core::identity::AdkIdentity;
4use adk_core::{Content, Result};
5use adk_memory::{MemoryEntry, MemoryService};
6
7use crate::domain::SafeTransactionSummary;
8use crate::guardrail::redact_payment_content;
9
10/// Indexes safe payment summaries into semantic memory.
11pub struct PaymentMemoryIndex {
12    memory_service: Arc<dyn MemoryService>,
13}
14
15impl PaymentMemoryIndex {
16    /// Creates a new safe-summary memory indexer.
17    #[must_use]
18    pub fn new(memory_service: Arc<dyn MemoryService>) -> Self {
19        Self { memory_service }
20    }
21
22    /// Indexes one safe transaction summary under a transaction-scoped memory session key.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the underlying memory service rejects the write.
27    pub async fn index_summary(
28        &self,
29        identity: &AdkIdentity,
30        summary: &SafeTransactionSummary,
31    ) -> Result<()> {
32        let session_key = format!(
33            "payments:{}:{}",
34            identity.session_id.as_ref(),
35            summary.transaction_id.as_str()
36        );
37        let entry = MemoryEntry {
38            content: redact_payment_content(
39                &Content::new("system").with_text(summary.transcript_text()),
40            ),
41            author: "adk-payments".to_string(),
42            timestamp: summary.updated_at,
43        };
44
45        self.memory_service
46            .add_session(
47                identity.app_name.as_ref(),
48                identity.user_id.as_ref(),
49                &session_key,
50                vec![entry],
51            )
52            .await
53    }
54}