Skip to content
169 changes: 104 additions & 65 deletions crates/data_connector/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ use url::Url;

use crate::{
config::{HistoryBackend, OracleConfig, PostgresConfig, RedisConfig},
core::{ConversationItemStorage, ConversationStorage, ResponseStorage},
core::{
ConversationItemStorage, ConversationMemoryWriter, ConversationStorage, ResponseStorage,
},
hooked::{HookedConversationItemStorage, HookedConversationStorage, HookedResponseStorage},
hooks::StorageHook,
memory::{MemoryConversationItemStorage, MemoryConversationStorage, MemoryResponseStorage},
noop::{NoOpConversationItemStorage, NoOpConversationStorage, NoOpResponseStorage},
memory::{
MemoryConversationItemStorage, MemoryConversationMemoryWriter, MemoryConversationStorage,
MemoryResponseStorage,
},
noop::{
NoOpConversationItemStorage, NoOpConversationMemoryWriter, NoOpConversationStorage,
NoOpResponseStorage,
},
oracle::{OracleConversationItemStorage, OracleConversationStorage, OracleResponseStorage},
postgres::{
PostgresConversationItemStorage, PostgresConversationStorage, PostgresResponseStorage,
Expand All @@ -22,13 +30,13 @@ use crate::{
},
};

/// Type alias for the storage tuple returned by factory functions.
/// This avoids clippy::type_complexity warnings while keeping Arc explicit.
pub type StorageTuple = (
Arc<dyn ResponseStorage>,
Arc<dyn ConversationStorage>,
Arc<dyn ConversationItemStorage>,
);
/// Complete storage handles returned by the factory, including conversation memory writer.
pub struct StorageBundle {
pub response_storage: Arc<dyn ResponseStorage>,
pub conversation_storage: Arc<dyn ConversationStorage>,
pub conversation_item_storage: Arc<dyn ConversationItemStorage>,
pub conversation_memory_writer: Arc<dyn ConversationMemoryWriter>,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny pattern alignment — the rest of this file uses NoOp* impls so callers can call unconditionally (NoOpResponseStorage, NoOpConversationStorage, etc). A NoOpConversationMemoryWriter would let this field be Arc<dyn ConversationMemoryWriter> (required) instead of Option<...>, and would remove the if let Some(writer) branches that'll otherwise show up in every consumer downstream. Same cost now, less friction later.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Simo I have implemented per your suggestion. Added NoOpConversationMemoryWriter and moved writer plumbing to required Arc in the core path, so callers can invoke unconditionally without Option branching.

This keeps behavior unchanged (NoOp on unsupported backends) while reducing downstream friction.


/// Configuration for creating storage backends
pub struct StorageFactoryConfig<'a> {
Expand All @@ -42,33 +50,34 @@ pub struct StorageFactoryConfig<'a> {
pub hook: Option<Arc<dyn StorageHook>>,
}

/// Create all three storage backends based on configuration.
///
/// # Arguments
/// * `config` - Storage factory configuration
///
/// # Returns
/// Tuple of (response_storage, conversation_storage, conversation_item_storage)
/// Returns whether a backend currently provides `ConversationMemoryWriter`.
pub const fn backend_supports_memory_writer(backend: &HistoryBackend) -> bool {
matches!(backend, HistoryBackend::Memory)
}

/// Create all configured storage handles, including conversation memory writer.
///
/// # Errors
/// Returns error string if required configuration is missing or initialization fails
pub async fn create_storage(config: StorageFactoryConfig<'_>) -> Result<StorageTuple, String> {
let (resp, conv, items): StorageTuple = match config.backend {
pub async fn create_storage(config: StorageFactoryConfig<'_>) -> Result<StorageBundle, String> {
let bundle = match config.backend {
HistoryBackend::Memory => {
info!("Initializing data connector: Memory");
(
Arc::new(MemoryResponseStorage::new()),
Arc::new(MemoryConversationStorage::new()),
Arc::new(MemoryConversationItemStorage::new()),
)
StorageBundle {
response_storage: Arc::new(MemoryResponseStorage::new()),
conversation_storage: Arc::new(MemoryConversationStorage::new()),
conversation_item_storage: Arc::new(MemoryConversationItemStorage::new()),
conversation_memory_writer: Arc::new(MemoryConversationMemoryWriter::new()),
}
}
HistoryBackend::None => {
info!("Initializing data connector: None (no persistence)");
(
Arc::new(NoOpResponseStorage::new()),
Arc::new(NoOpConversationStorage::new()),
Arc::new(NoOpConversationItemStorage::new()),
)
StorageBundle {
response_storage: Arc::new(NoOpResponseStorage::new()),
conversation_storage: Arc::new(NoOpConversationStorage::new()),
conversation_item_storage: Arc::new(NoOpConversationItemStorage::new()),
conversation_memory_writer: Arc::new(NoOpConversationMemoryWriter::new()),
}
}
HistoryBackend::Oracle => {
let oracle_cfg = config
Expand Down Expand Up @@ -140,18 +149,28 @@ pub async fn create_storage(config: StorageFactoryConfig<'_>) -> Result<StorageT
// Wrap backends in hooked storage when a hook is provided
if let Some(hook) = config.hook {
info!("Wrapping storage backends with hook");
Ok((
Arc::new(HookedResponseStorage::new(resp, hook.clone())),
Arc::new(HookedConversationStorage::new(conv, hook.clone())),
Arc::new(HookedConversationItemStorage::new(items, hook)),
))
Ok(StorageBundle {
response_storage: Arc::new(HookedResponseStorage::new(
bundle.response_storage,
hook.clone(),
)),
conversation_storage: Arc::new(HookedConversationStorage::new(
bundle.conversation_storage,
hook.clone(),
)),
conversation_item_storage: Arc::new(HookedConversationItemStorage::new(
bundle.conversation_item_storage,
hook,
)),
conversation_memory_writer: bundle.conversation_memory_writer,
})
} else {
Ok((resp, conv, items))
Ok(bundle)
}
}

/// Create Oracle storage backends with a single shared connection pool.
fn create_oracle_storage(oracle_cfg: &OracleConfig) -> Result<StorageTuple, String> {
fn create_oracle_storage(oracle_cfg: &OracleConfig) -> Result<StorageBundle, String> {
use crate::oracle::OracleStore;

let store = OracleStore::new(
Expand All @@ -163,14 +182,15 @@ fn create_oracle_storage(oracle_cfg: &OracleConfig) -> Result<StorageTuple, Stri
],
)?;

Ok((
Arc::new(OracleResponseStorage::new(store.clone())),
Arc::new(OracleConversationStorage::new(store.clone())),
Arc::new(OracleConversationItemStorage::new(store)),
))
Ok(StorageBundle {
response_storage: Arc::new(OracleResponseStorage::new(store.clone())),
conversation_storage: Arc::new(OracleConversationStorage::new(store.clone())),
conversation_item_storage: Arc::new(OracleConversationItemStorage::new(store)),
conversation_memory_writer: Arc::new(NoOpConversationMemoryWriter::new()),
})
}

async fn create_postgres_storage(postgres_cfg: &PostgresConfig) -> Result<StorageTuple, String> {
async fn create_postgres_storage(postgres_cfg: &PostgresConfig) -> Result<StorageBundle, String> {
let store = PostgresStore::new(postgres_cfg.clone())?;
let postgres_resp = PostgresResponseStorage::new(store.clone())
.await
Expand All @@ -191,24 +211,26 @@ async fn create_postgres_storage(postgres_cfg: &PostgresConfig) -> Result<Storag
store.ensure_response_indexes().await?;
}

Ok((
Arc::new(postgres_resp),
Arc::new(postgres_conv),
Arc::new(postgres_item),
))
Ok(StorageBundle {
response_storage: Arc::new(postgres_resp),
conversation_storage: Arc::new(postgres_conv),
conversation_item_storage: Arc::new(postgres_item),
conversation_memory_writer: Arc::new(NoOpConversationMemoryWriter::new()),
})
}

fn create_redis_storage(redis_cfg: &RedisConfig) -> Result<StorageTuple, String> {
fn create_redis_storage(redis_cfg: &RedisConfig) -> Result<StorageBundle, String> {
let store = RedisStore::new(redis_cfg.clone())?;
let redis_resp = RedisResponseStorage::new(store.clone());
let redis_conv = RedisConversationStorage::new(store.clone());
let redis_item = RedisConversationItemStorage::new(store);

Ok((
Arc::new(redis_resp),
Arc::new(redis_conv),
Arc::new(redis_item),
))
Ok(StorageBundle {
response_storage: Arc::new(redis_resp),
conversation_storage: Arc::new(redis_conv),
conversation_item_storage: Arc::new(redis_item),
conversation_memory_writer: Arc::new(NoOpConversationMemoryWriter::new()),
})
}

#[cfg(test)]
Expand All @@ -227,7 +249,12 @@ mod tests {
redis: None,
hook: None,
};
let (resp, conv, items) = create_storage(config).await.unwrap();
let bundle = create_storage(config).await.unwrap();
let (resp, conv, items) = (
bundle.response_storage,
bundle.conversation_storage,
bundle.conversation_item_storage,
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// Verify they work end-to-end
let mut response = StoredResponse::new(None);
Expand Down Expand Up @@ -271,7 +298,8 @@ mod tests {
redis: None,
hook: None,
};
let (resp, conv, _items) = create_storage(config).await.unwrap();
let bundle = create_storage(config).await.unwrap();
let (resp, conv) = (bundle.response_storage, bundle.conversation_storage);

// NoOp storage should accept writes but return nothing on reads
let mut response = StoredResponse::new(None);
Expand All @@ -287,40 +315,46 @@ mod tests {

#[tokio::test]
async fn test_create_storage_oracle_missing_config() {
let config = StorageFactoryConfig {
let err = create_storage(StorageFactoryConfig {
backend: &HistoryBackend::Oracle,
oracle: None,
postgres: None,
redis: None,
hook: None,
};
let err = create_storage(config).await.err().expect("should fail");
})
.await
.err()
.expect("should fail");
assert!(err.contains("oracle configuration is required"));
}

#[tokio::test]
async fn test_create_storage_postgres_missing_config() {
let config = StorageFactoryConfig {
let err = create_storage(StorageFactoryConfig {
backend: &HistoryBackend::Postgres,
oracle: None,
postgres: None,
redis: None,
hook: None,
};
let err = create_storage(config).await.err().expect("should fail");
})
.await
.err()
.expect("should fail");
assert!(err.contains("Postgres configuration is required"));
}

#[tokio::test]
async fn test_create_storage_redis_missing_config() {
let config = StorageFactoryConfig {
let err = create_storage(StorageFactoryConfig {
backend: &HistoryBackend::Redis,
oracle: None,
postgres: None,
redis: None,
hook: None,
};
let err = create_storage(config).await.err().expect("should fail");
})
.await
.err()
.expect("should fail");
assert!(err.contains("Redis configuration is required"));
}

Expand Down Expand Up @@ -367,7 +401,12 @@ mod tests {
redis: None,
hook: Some(Arc::new(NoOpHook)),
};
let (resp, conv, items) = create_storage(config).await.unwrap();
let bundle = create_storage(config).await.unwrap();
let (resp, conv, items) = (
bundle.response_storage,
bundle.conversation_storage,
bundle.conversation_item_storage,
);

// Verify hooked storage works end-to-end
let mut response = StoredResponse::new(None);
Expand Down
5 changes: 4 additions & 1 deletion crates/data_connector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ pub use context::{
RequestContext,
};
// Re-export factory
pub use factory::{create_storage, StorageFactoryConfig};
pub use factory::{
backend_supports_memory_writer, create_storage, StorageBundle, StorageFactoryConfig,
};
pub use hooks::{BeforeHookResult, ExtraColumns, HookError, StorageHook, StorageOperation};
// Re-export memory implementations for testing
pub use memory::{MemoryConversationItemStorage, MemoryConversationStorage, MemoryResponseStorage};
pub use noop::NoOpConversationMemoryWriter;
// Re-export schema config types
pub use schema::{ColumnDef, SchemaConfig, TableConfig};
64 changes: 62 additions & 2 deletions crates/data_connector/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
//! Structure:
//! 1. MemoryConversationStorage
//! 2. MemoryConversationItemStorage
//! 3. MemoryResponseStorage
//! 3. MemoryConversationMemoryWriter
//! 4. MemoryResponseStorage

use std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -274,7 +275,38 @@ impl ConversationItemStorage for MemoryConversationItemStorage {
}

// ============================================================================
// PART 3: MemoryResponseStorage
// PART 3: MemoryConversationMemoryWriter
// ============================================================================

/// In-memory conversation memory writer used only by `HistoryBackend::Memory`.
#[derive(Default, Clone)]
pub struct MemoryConversationMemoryWriter {
inner: Arc<RwLock<HashMap<ConversationMemoryId, NewConversationMemory>>>,
}

impl MemoryConversationMemoryWriter {
/// Create a new in-memory conversation memory writer.
pub fn new() -> Self {
Self {
inner: Arc::new(RwLock::new(HashMap::new())),
}
}
}

#[async_trait]
impl ConversationMemoryWriter for MemoryConversationMemoryWriter {
async fn create_memory(
&self,
input: NewConversationMemory,
) -> ConversationMemoryResult<ConversationMemoryId> {
let id = ConversationMemoryId(format!("mem_{}", ulid::Ulid::new()));
self.inner.write().insert(id.clone(), input);
Ok(id)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// ============================================================================
// PART 4: MemoryResponseStorage
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// ============================================================================

/// Internal store structure holding both maps together
Expand Down Expand Up @@ -590,6 +622,34 @@ mod tests {
assert_eq!(asc_after[0].id, i3.id);
}

#[tokio::test]
async fn test_memory_conversation_memory_writer_happy_path() {
let writer = MemoryConversationMemoryWriter::new();
let input = NewConversationMemory {
conversation_id: ConversationId::from("conv_mem_test"),
conversation_version: Some(1),
response_id: None,
memory_type: ConversationMemoryType::Ltm,
status: ConversationMemoryStatus::Ready,
attempt: 1,
owner_id: Some("owner_1".to_string()),
next_run_at: Utc::now(),
lease_until: None,
content: Some("memory content".to_string()),
memory_config: Some("{\"k\":\"v\"}".to_string()),
scope_id: Some("scope_1".to_string()),
error_msg: None,
};

let id = writer.create_memory(input.clone()).await.unwrap();
let _typed_id: ConversationMemoryId = id.clone();
assert!(id.0.starts_with("mem_"));

let store = writer.inner.read();
let stored = store.get(&id.clone()).expect("memory should be present");
assert_eq!(stored, &input);
}

// ========================================================================
// Response Tests
// ========================================================================
Expand Down
Loading
Loading