Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
chore(ai): idiomatic memory config (#2400)
Eric Hayes4c120602344d
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex 0b69b5653c..242c356bd2 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -6349,6 +6349,7 @@ dependencies = ["macro_db_migrator","macro_entrypoint","macro_env",+ "macro_env_var","macro_user_id","macro_uuid","model_user",diff --git a/rust/cloud-storage/memory/Cargo.toml b/rust/cloud-storage/memory/Cargo.tomlindex 3a3c597df0..fb5fd9809d 100644--- a/rust/cloud-storage/memory/Cargo.toml+++ b/rust/cloud-storage/memory/Cargo.toml@@ -26,6 +26,7 @@ tracing = { workspace = true }utoipa = { workspace = true }macro_entrypoint = { path = "../macro_entrypoint" }macro_env = { path = "../macro_env" }+macro_env_var = { path = "../macro_env_var" }# For building ToolServiceContextcomms = { path = "../comms" }diff --git a/rust/cloud-storage/memory/src/config.rs b/rust/cloud-storage/memory/src/config.rsnew file mode 100644index 0000000000..9e91843f17--- /dev/null+++ b/rust/cloud-storage/memory/src/config.rs@@ -0,0 +1,98 @@+pub use macro_env::Environment;+use macro_env_var::{env_var, maybe_env_var};++pub struct Config {+ pub environment: Environment,+ pub database_url: String,+ pub user_id: String,+ pub internal_api_secret_key: String,+ pub document_storage_service_url: String,+ pub search_service_url: String,+ pub email_service_url: String,+ pub sync_service_url: String,+ pub static_file_service_url: String,+ pub lexical_service_url: String,+ pub document_storage_bucket: String,+ pub docx_document_upload_bucket: String,+ pub email_scheduled_queue: String,+ pub document_storage_service_cloudfront_distribution_url: String,+ pub document_storage_service_cloudfront_signer_public_key_id: String,+ pub document_storage_service_cloudfront_signer_private_key_secret_name: String,+}++env_var!(+ pub struct EnvVars {+ pub DatabaseUrl,+ pub UserId,+ pub DocumentStorageServiceUrl,+ pub SearchServiceUrl,+ pub EmailServiceUrl,+ pub SyncServiceUrl,+ pub StaticFileServiceUrl,+ pub DocumentStorageBucket,+ pub DocxDocumentUploadBucket,+ pub EmailScheduledQueue,+ pub DocumentStorageServiceCloudfrontDistributionUrl,+ pub DocumentStorageServiceCloudfrontSignerPublicKeyId,+ pub DocumentStorageServiceCloudfrontSignerPrivateKeySecretName,+ }+);++maybe_env_var!(+ pub struct MaybeEnvVars {+ pub InternalApiSecretKey,+ pub LexicalServiceUrl,+ }+);++impl Config {+ pub fn from_env() -> anyhow::Result<Self> {+ let environment = Environment::new_or_prod();+ let env_vars = EnvVars::new()?;+ let maybe_env_vars = MaybeEnvVars::new();++ let EnvVars {+ database_url,+ user_id,+ document_storage_service_url,+ search_service_url,+ email_service_url,+ sync_service_url,+ static_file_service_url,+ document_storage_bucket,+ docx_document_upload_bucket,+ email_scheduled_queue,+ document_storage_service_cloudfront_distribution_url,+ document_storage_service_cloudfront_signer_public_key_id,+ document_storage_service_cloudfront_signer_private_key_secret_name,+ } = env_vars;++ Ok(Self {+ environment,+ database_url: database_url.to_string(),+ user_id: user_id.to_string(),+ internal_api_secret_key: maybe_env_vars+ .internal_api_secret_key+ .map(|v| v.to_string())+ .unwrap_or_else(|| "local".to_string()),+ document_storage_service_url: document_storage_service_url.to_string(),+ search_service_url: search_service_url.to_string(),+ email_service_url: email_service_url.to_string(),+ sync_service_url: sync_service_url.to_string(),+ static_file_service_url: static_file_service_url.to_string(),+ lexical_service_url: maybe_env_vars+ .lexical_service_url+ .map(|v| v.to_string())+ .unwrap_or_else(|| "http://localhost:8096".to_string()),+ document_storage_bucket: document_storage_bucket.to_string(),+ docx_document_upload_bucket: docx_document_upload_bucket.to_string(),+ email_scheduled_queue: email_scheduled_queue.to_string(),+ document_storage_service_cloudfront_distribution_url:+ document_storage_service_cloudfront_distribution_url.to_string(),+ document_storage_service_cloudfront_signer_public_key_id:+ document_storage_service_cloudfront_signer_public_key_id.to_string(),+ document_storage_service_cloudfront_signer_private_key_secret_name:+ document_storage_service_cloudfront_signer_private_key_secret_name.to_string(),+ })+ }+}diff --git a/rust/cloud-storage/memory/src/context.rs b/rust/cloud-storage/memory/src/context.rsindex 2253de4ae1..fddc8cb3fb 100644--- a/rust/cloud-storage/memory/src/context.rs+++ b/rust/cloud-storage/memory/src/context.rs@@ -27,6 +27,8 @@ use static_file_service_client::StaticFileServiceClient;use std::sync::Arc;use sync_service_client::SyncServiceClient;+use crate::config::Config;+/// Builds a [`ToolServiceContext`] from environment variables and a database pool.////// Required env vars: `INTERNAL_API_SECRET_KEY`, `DOCUMENT_STORAGE_SERVICE_URL`,@@ -37,48 +39,43 @@ use sync_service_client::SyncServiceClient;/// `DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_DISTRIBUTION_URL`,/// `DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PUBLIC_KEY_ID`,/// `DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PRIVATE_KEY_SECRET_NAME`-#[tracing::instrument(skip(pool), err)]-pub async fn build_tool_service_context(pool: sqlx::PgPool) -> anyhow::Result<ToolServiceContext> {- let internal_auth_key =- std::env::var("INTERNAL_API_SECRET_KEY").unwrap_or_else(|_| "local".into());- let dss_url = std::env::var("DOCUMENT_STORAGE_SERVICE_URL")?;- let search_url = std::env::var("SEARCH_SERVICE_URL")?;- let email_url = std::env::var("EMAIL_SERVICE_URL")?;- let sync_url = std::env::var("SYNC_SERVICE_URL")?;- let sfs_url = std::env::var("STATIC_FILE_SERVICE_URL")?;- let lexical_url =- std::env::var("LEXICAL_SERVICE_URL").unwrap_or_else(|_| "http://localhost:8096".into());- let doc_bucket = std::env::var("DOCUMENT_STORAGE_BUCKET")?;- let docx_bucket = std::env::var("DOCX_DOCUMENT_UPLOAD_BUCKET")?;- let email_scheduled_queue = std::env::var("EMAIL_SCHEDULED_QUEUE")?;- let cf_dist_url = std::env::var("DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_DISTRIBUTION_URL")?;- let cf_key_id = std::env::var("DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PUBLIC_KEY_ID")?;- let cf_private_key =- std::env::var("DOCUMENT_STORAGE_SERVICE_CLOUDFRONT_SIGNER_PRIVATE_KEY_SECRET_NAME")?;+#[tracing::instrument(skip(pool, config), err)]+pub async fn build_tool_service_context(+ pool: sqlx::PgPool,+ config: &Config,+) -> anyhow::Result<ToolServiceContext> {let aws_config = macro_aws_config::get_macro_aws_config().await;let sqs_client = sqs_client::SQS::new(aws_sdk_sqs::Client::new(&aws_config))- .email_scheduled_queue(&email_scheduled_queue);+ .email_scheduled_queue(&config.email_scheduled_queue);// Service clientslet dss_client = Arc::new(DocumentStorageServiceClient::new(- internal_auth_key.clone(),- dss_url,+ config.internal_api_secret_key.clone(),+ config.document_storage_service_url.clone(),));let search_client = Arc::new(SearchServiceClient::new(- internal_auth_key.clone(),- search_url,+ config.internal_api_secret_key.clone(),+ config.search_service_url.clone(),+ ));+ let sync_client = Arc::new(SyncServiceClient::new(+ config.internal_api_secret_key.clone(),+ config.sync_service_url.clone(),));- let sync_client = Arc::new(SyncServiceClient::new(internal_auth_key.clone(), sync_url));let email_client = Arc::new(EmailServiceClient::new(- internal_auth_key.clone(),- email_url.clone(),+ config.internal_api_secret_key.clone(),+ config.email_service_url.clone(),));let sfs_client = Arc::new(StaticFileServiceClient::new(- internal_auth_key.clone(),- sfs_url,+ config.internal_api_secret_key.clone(),+ config.static_file_service_url.clone(),+ ));+ let email_ext_client = Arc::new(EmailServiceClientExternal::new(+ config.email_service_url.clone(),));- let email_ext_client = Arc::new(EmailServiceClientExternal::new(email_url));- let lexical_client = LexicalClient::new(internal_auth_key, lexical_url);+ let lexical_client = LexicalClient::new(+ config.internal_api_secret_key.clone(),+ config.lexical_service_url.clone(),+ );// Scribelet scribe = Arc::new(@@ -121,12 +118,22 @@ pub async fn build_tool_service_context(pool: sqlx::PgPool) -> anyhow::Result<To// Document tool contextlet s3_client = macro_aws_config::s3_client().await;- let s3_upload_adapter = S3UploadUrlAdapter::new(s3_client, &doc_bucket, &docx_bucket);+ let s3_upload_adapter = S3UploadUrlAdapter::new(+ s3_client,+ &config.document_storage_bucket,+ &config.docx_document_upload_bucket,+ );let document_repo = PgDocumentRepo::new(pool.clone());let cloudfront_config = CloudFrontConfig {- distribution_url: cf_dist_url,- signer_public_key_id: cf_key_id,- signer_private_key: cf_private_key,+ distribution_url: config+ .document_storage_service_cloudfront_distribution_url+ .clone(),+ signer_public_key_id: config+ .document_storage_service_cloudfront_signer_public_key_id+ .clone(),+ signer_private_key: config+ .document_storage_service_cloudfront_signer_private_key_secret_name+ .clone(),presigned_url_expiry_seconds: 3600,browser_cache_expiry_seconds: 86400,};diff --git a/rust/cloud-storage/memory/src/lib.rs b/rust/cloud-storage/memory/src/lib.rsindex 75ced455b8..3ca0751e31 100644--- a/rust/cloud-storage/memory/src/lib.rs+++ b/rust/cloud-storage/memory/src/lib.rs@@ -1,3 +1,4 @@+pub mod config;pub mod context;pub mod domain;pub mod inbound;diff --git a/rust/cloud-storage/memory/src/main.rs b/rust/cloud-storage/memory/src/main.rsindex 1791da482e..d7b67ce89a 100644--- a/rust/cloud-storage/memory/src/main.rs+++ b/rust/cloud-storage/memory/src/main.rs@@ -1,5 +1,7 @@use ai_tools::all_tools;+use anyhow::Context;use macro_user_id::user_id::MacroUserIdStr;+use memory::config::Config;use memory::context::build_tool_service_context;use memory::domain::{MemoryService, service::MemoryServiceImpl};use memory::outbound::pg_memory_repo::PgMemoryRepo;@@ -8,22 +10,21 @@ use sqlx::postgres::PgPoolOptions;#[tokio::main]#[tracing::instrument(err)]async fn main() -> anyhow::Result<()> {- macro_entrypoint::MacroEntrypoint::default().init();-- let database_url = std::env::var("DATABASE_URL")?;+ let config = Config::from_env().context("failed to load memory configuration")?;+ macro_entrypoint::MacroEntrypoint::new(config.environment).init();let pool = PgPoolOptions::new().max_connections(5)- .connect(&database_url)+ .connect(&config.database_url).await?;- let tool_context = build_tool_service_context(pool.clone()).await?;+ let tool_context = build_tool_service_context(pool.clone(), &config).await?;let tools = all_tools();let memory_repo = PgMemoryRepo::new(pool.clone());let memory_service = MemoryServiceImpl::new(pool, memory_repo, tool_context, tools);- let user = MacroUserIdStr::try_from(std::env::var("USER_ID").expect("USER_ID"))- .expect("parse user id");+ let user = MacroUserIdStr::try_from(config.user_id.clone())+ .context("USER_ID must be a valid Macro user id")?;tracing::info!("Generating memory for {user}...");match memory_service.get_or_generate_memory(user).await? {