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
use crate::domain::models::{
ChatResponse, CopyChatArgs, CreateChatArgs, GetChatResponse, PatchChatArgs,
PatchChatMessageArgs, PatchChatRepoArgs, Result,
};
use agent::types::ChatMessageContent;
use ai_toolset::tool_object::UserToolResponse;
use attachment::FormattedParts;
use entity_access::domain::models::{
EditAccessLevel, EntityAccessReceipt, OwnerAccessLevel, ViewAccessLevel,
};
use macro_user_id::user_id::MacroUserIdStr;
use model::chat::Chat;
use model_owner::Owner;
use models_permissions::share_permission::access_level::AccessLevel;
use models_permissions::share_permission::team_share::TeamShareFacts;
use models_permissions::share_permission::{SharePermissionV2, TeamLinkShareDefault};
/// Repository trait for low-level chat data access.
pub trait ChatRepo: Send + Sync + 'static {
/// Create a new chat, returning the chat ID.
///
/// `share_permission` is the pre-resolved initial share permission — the
/// repository persists it verbatim and carries no share-policy of its own.
fn create(
&self,
owner: Owner,
args: CreateChatArgs,
share_permission: SharePermissionV2,
) -> impl std::future::Future<Output = Result<String>> + Send;
/// Get the link-share preference of the owner's team, or `None` when the
/// owner has no team. The team is the one [`model_owner::team::owner_team`]
/// resolves, so a bot owner gets its team's default.
fn get_team_default_link_share(
&self,
owner: &Owner,
) -> impl std::future::Future<Output = Result<Option<TeamLinkShareDefault>>> + Send;
/// Get the full chat response (metadata, messages, web citations).
fn get_chat(
&self,
chat_id: &str,
) -> impl std::future::Future<Output = Result<ChatResponse>> + Send;
/// Get a chat metadata by its ID.
fn get_metadata(&self, chat_id: &str)
-> impl std::future::Future<Output = Result<Chat>> + Send;
/// Get the requesting user's access level on a chat.
fn get_access_level(
&self,
user_id: MacroUserIdStr<'_>,
chat_id: &str,
) -> impl std::future::Future<Output = Result<AccessLevel>> + Send;
/// Copy a chat (create a new chat and duplicate its messages), returning the new chat ID.
///
/// `share_permission` is the pre-resolved initial share permission for the
/// copy — the repository persists it verbatim.
fn copy_chat(
&self,
owner: Owner,
source_chat_id: &str,
args: CopyChatArgs,
share_permission: SharePermissionV2,
) -> impl std::future::Future<Output = Result<String>> + Send;
/// Revert a soft-deleted chat (clears `deleted_at`, restores history).
fn revert_delete(
&self,
chat_id: &str,
project_id: Option<&str>,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Get the share permissions for a chat.
fn get_permissions(
&self,
chat_id: &str,
) -> impl std::future::Future<Output = Result<SharePermissionV2>> + Send;
/// Load the canonical team-share facts (persisted owner, owner's team,
/// current explicit grant, revision) the owner policy authorizes against.
fn get_team_share_facts(
&self,
chat_id: &str,
) -> impl std::future::Future<Output = Result<TeamShareFacts>> + Send;
/// Soft-delete a chat (sets `deleted_at`, removes pins and history).
fn delete(&self, chat_id: &str) -> impl std::future::Future<Output = Result<()>> + Send;
/// Permanently delete a chat and all associated data.
fn permanently_delete(
&self,
chat_id: &str,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Patch a chat's metadata (name, project, share permissions).
///
/// `args.team_share` is the owner-authorized command for an explicit
/// `teamShareAccessLevel`; the repository applies it atomically with the
/// rest of the patch and rejects a team level that arrives without one.
fn patch(
&self,
user_id: MacroUserIdStr<'static>,
chat_id: &str,
args: PatchChatRepoArgs,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Update a project's `updatedAt` timestamp.
fn update_project_modified(
&self,
project_id: &str,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Patch a message's content.
fn patch_message(
&self,
chat_id: &str,
args: PatchChatMessageArgs,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Get the content of a single message by ID
fn get_message_content(
&self,
chat_id: &str,
message_id: &str,
) -> impl std::future::Future<Output = Result<ChatMessageContent>> + Send;
/// Update a specific message's final content and chat recency.
fn update_message_content(
&self,
chat_id: &str,
message_id: &str,
content: &ChatMessageContent,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Persist interim message content without updating chat recency.
fn update_interim_message_content(
&self,
chat_id: &str,
message_id: &str,
content: &ChatMessageContent,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Store the resolved (AI-facing) representation of a user message.
fn store_resolved_message(
&self,
message_id: &str,
parts: FormattedParts,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Get the resolved representation of a user message, if it exists.
fn get_resolved_message(
&self,
message_id: &str,
) -> impl std::future::Future<Output = Result<FormattedParts>> + Send;
}
/// Service trait for chat business logic.
///
/// Handlers depend on this trait rather than [`ChatRepo`] directly.
/// The default implementation is [`super::service::ChatServiceImpl`].
pub trait ChatService: Send + Sync + 'static {
/// Create a new chat, returning the chat ID.
fn create(
&self,
owner: Owner,
args: CreateChatArgs,
) -> impl std::future::Future<Output = Result<String>> + Send;
/// Get a chat with messages, web citations, and the user's access level.
fn get_chat(
&self,
entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,
) -> impl std::future::Future<Output = Result<GetChatResponse>> + Send;
/// Get chat metadata without loading messages.
fn get_metadata(
&self,
entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,
) -> impl std::future::Future<Output = Result<model::chat::Chat>> + Send;
/// Copy a chat and its messages, returning the new chat ID.
fn copy_chat(
&self,
entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,
) -> impl std::future::Future<Output = Result<String>> + Send;
/// Soft-delete a chat.
fn delete(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Permanently delete a chat and all associated data.
fn permanently_delete(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Patch a chat's metadata (name, project, share permissions).
fn patch(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
args: PatchChatArgs,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Revert a soft-deleted chat.
fn revert_delete(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Get the share permissions for a chat.
fn get_permissions(
&self,
entity_access_receipt: EntityAccessReceipt<EditAccessLevel>,
) -> impl std::future::Future<Output = Result<SharePermissionV2>> + Send;
/// Update a tool call's arguments after validation.
fn update_tool_call(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
message_id: &str,
tool_call_id: &str,
new_args: serde_json::Value,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Update a tool response.
fn update_tool_response(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
message_id: &str,
tool_call_id: &str,
response: UserToolResponse<serde_json::Value>,
) -> impl std::future::Future<Output = Result<()>> + Send;
/// Execute a pending tool call. Optionally update its arguments first.
fn call_tool(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
message_id: &str,
tool_call_id: &str,
args: Option<serde_json::Value>,
) -> impl std::future::Future<Output = Result<serde_json::Value>> + Send;
/// Reject a pending tool call.
fn reject_tool_call(
&self,
entity_access_receipt: EntityAccessReceipt<OwnerAccessLevel>,
message_id: &str,
tool_call_id: &str,
) -> impl std::future::Future<Output = Result<()>> + Send;
}