klieo_memory_pgvector/factory.rs
1//! `MemoryPgvector` — convenience factory wrapping the `LongTermMemory`
2//! handle in an `Arc<dyn …>` ready for `AgentContext`.
3
4use crate::client::{PgvectorConfig, PgvectorHandle};
5use crate::embedder::{DummyEmbedder, Embedder};
6use crate::long_term::PgvectorLongTerm;
7use klieo_core::error::MemoryError;
8use klieo_core::memory::LongTermMemory;
9use std::sync::Arc;
10
11/// Factory bundle returned by [`MemoryPgvector::new`].
12#[non_exhaustive]
13pub struct MemoryPgvector {
14 /// Trait-object view for `AgentContext`; the same instance as
15 /// [`Self::pgvector_long_term`] (use this unless you need the concrete type).
16 pub long_term: Arc<dyn LongTermMemory>,
17 /// Concrete [`PgvectorLongTerm`] handle. `FilterableLongTermMemory` lives
18 /// in `klieo-memory-graph` and cannot ride the `LongTermMemory` trait
19 /// object, so the GraphRAG composer needs the concrete type. Both fields
20 /// point at the same instance.
21 pub pgvector_long_term: Arc<PgvectorLongTerm>,
22}
23
24impl MemoryPgvector {
25 /// Capability-shaped default — connect with [`DummyEmbedder`] and default
26 /// [`PgvectorConfig`] (table prefix `klieo_facts`, embedder id `default`).
27 ///
28 /// Use [`Self::new`] for a real embedder or a non-default config.
29 pub async fn connect(url: impl Into<String>) -> Result<Self, MemoryError> {
30 Self::new(PgvectorConfig::new(url), Arc::new(DummyEmbedder)).await
31 }
32
33 /// Connect to Postgres and return a ready-to-use handle. The table is
34 /// provisioned lazily on the first `remember`, so concurrent process
35 /// starts don't race on DDL.
36 pub async fn new(
37 cfg: PgvectorConfig,
38 embedder: Arc<dyn Embedder>,
39 ) -> Result<Self, MemoryError> {
40 let embedder_id = cfg.embedder_id().to_string();
41 let handle = PgvectorHandle::connect(&cfg).await?;
42 let pgvector_long_term = Arc::new(PgvectorLongTerm::new(handle, embedder, embedder_id));
43 let long_term: Arc<dyn LongTermMemory> = pgvector_long_term.clone();
44 Ok(Self {
45 long_term,
46 pgvector_long_term,
47 })
48 }
49}