use crate::data::{Database, EngineError};
use crate::error_messages::ERROR_DB_SETUP;
use serde::{Deserialize, Serialize};
#[cfg(feature = "dynamo")]
use self::dynamodb as dynamodb_connector;
#[cfg(feature = "mongo")]
use self::mongodb as mongodb_connector;
pub mod conversations;
pub mod interactions;
pub mod memories;
pub mod messages;
pub mod nodes;
pub mod state;
use crate::Client;
#[cfg(feature = "dynamo")]
mod dynamodb;
#[cfg(feature = "mongo")]
mod mongodb;
#[derive(Serialize, Deserialize, Debug)]
pub struct DbConversation {
#[serde(rename = "_id")] pub id: String,
pub client: Client,
pub flow_id: String,
pub step_id: String,
pub metadata: serde_json::Value,
pub status: String,
pub last_interaction_at: String,
pub updated_at: String,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbInteraction {
#[serde(rename = "_id")] pub id: String,
pub client: Client,
pub success: bool,
pub event: serde_json::Value,
pub updated_at: String,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbMemory {
#[serde(rename = "_id")] pub id: String,
pub client: Client,
pub interaction_id: String,
pub conversation_id: String,
pub flow_id: String,
pub step_id: String,
pub memory_order: i32,
pub interaction_order: i32,
pub key: String,
pub value: serde_json::Value,
pub expires_at: Option<String>,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbMessage {
#[serde(rename = "_id")] pub id: String,
pub client: Client,
pub interaction_id: String,
pub conversation_id: String,
pub flow_id: String,
pub step_id: String,
pub message_order: i32,
pub interaction_order: i32,
pub direction: String,
pub payload: serde_json::Value,
pub content_type: String,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbNode {
#[serde(rename = "_id")] pub id: String,
pub client: Client,
pub interaction_id: String,
pub conversation_id: String,
pub flow_id: String,
pub step_id: String,
pub next_step: Option<String>,
pub next_flow: Option<String>,
pub created_at: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DbState {
#[serde(rename = "_id")] pub id: String,
pub client: Client,
#[serde(rename = "type")]
pub _type: String,
pub value: serde_json::Value,
pub expires_at: Option<String>,
pub created_at: String,
}
#[cfg(feature = "mongo")]
pub fn is_mongodb() -> bool {
match std::env::var("ENGINE_DB_TYPE") {
Ok(val) => val == "mongodb".to_owned(),
Err(_) => true,
}
}
#[cfg(feature = "dynamo")]
pub fn is_dynamodb() -> bool {
match std::env::var("ENGINE_DB_TYPE") {
Ok(val) => val == "dynamodb".to_owned(),
Err(_) => false,
}
}
pub fn init_db() -> Result<Database, EngineError> {
#[cfg(feature = "mongo")]
if is_mongodb() {
return mongodb_connector::init();
}
#[cfg(feature = "dynamo")]
if is_dynamodb() {
return dynamodb_connector::init();
}
Err(EngineError::Manager(ERROR_DB_SETUP.to_owned()))
}