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
// biohazard
use crate::cognition::api::context::ApiContext;
use crate::cognition::model::chats::ChatResponse;
use anyhow::Context;
use macro_db_client::dcs::get_chat::{get_chat_db, get_messages, get_web_citations};
use unfurl_service::GetUnfurlResponse;
#[tracing::instrument(err, skip(ctx))]
pub async fn get_chat(
ctx: &ApiContext,
chat_id: &str,
current_user_id: &str,
) -> anyhow::Result<ChatResponse> {
let chat = get_chat_db(&ctx.db, chat_id)
.await
.context("Failed to get chat from database")?;
let messages = get_messages(&ctx.db, chat_id)
.await
.context("Failed to get messages")?;
let web_citations = get_web_citations(&ctx.db, chat_id)
.await
.unwrap_or_default()
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.map(|v| GetUnfurlResponse {
description: v.description,
favicon_url: v.favicon_url,
image_url: v.image_url,
title: v.title,
url: v.url,
embed_type: Default::default(),
embed_url: None,
})
.collect(),
)
})
.collect();
#[allow(deprecated)]
Ok(ChatResponse {
id: chat.id,
user_id: chat.user_id.principal_id(),
name: chat.name,
model: chat.model,
messages,
project_id: chat.project_id,
created_at: chat.created_at,
updated_at: chat.updated_at,
attachments: vec![],
token_count: chat.token_count,
web_citations,
is_persistent: chat.is_persistent,
})
}
#[cfg(test)]
mod tests {
use super::*;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::{Pool, Postgres};
#[ignore]
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../fixtures", scripts("chat_example"))
)]
/// chat three has 2 messages,
/// it has 0 active attachments
/// but message-one has 3 message attachments
async fn test_get_chat(pool: Pool<Postgres>) {
let ctx = crate::cognition::api::context::test_api_context(pool.clone()).await;
let chat = get_chat(&ctx, "chat-three", "user").await.unwrap();
assert_eq!(chat.id, "chat-three".to_string());
assert_eq!(chat.user_id, "macro|[email protected]".to_string());
assert_eq!(chat.name, "test-chat 3".to_string());
assert!(chat.model.is_some(), "some model");
assert_eq!(chat.messages.len(), 2);
assert_eq!(chat.messages[0].attachments.len(), 3);
}
}