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
//! Outbound port for ai projection persistence.
use macro_user_id::user_id::MacroUserIdStr;
use crate::domain::model::{
AiProjection, AiProjectionError, Expiry, RefreshCadence, TargetType, UserAiProjection,
};
/// The AiProjectionRepository defines the persistence actions for ai projections.
pub trait AiProjectionRepository: Clone + Send + Sync + 'static {
/// Gets a projection definition by id, creating it if absent. An existing
/// definition whose `prompt_hash` (or cadence/expiry) differs from the
/// request is updated in place, so re-upserting with a changed prompt,
/// model, or output schema revises the definition.
#[allow(clippy::too_many_arguments)]
fn upsert_projection_definition(
&self,
id: &str,
prompt: &str,
prompt_hash: &str,
target_type: TargetType,
refresh_cadence: RefreshCadence,
expiry: Expiry,
model: Option<&str>,
output_schema: Option<&serde_json::Value>,
) -> impl Future<Output = Result<AiProjection, AiProjectionError>> + Send;
/// Gets the target's instance of a projection, or creates a cold instance
/// if one does not exist. An existing instance whose `prompt_hash` differs
/// from the definition's is reset to cold (keeping its previous result
/// visible until regeneration overwrites it). Loading instances without a
/// live processing claim are also recovered as cold,
/// so a later read can restart abandoned generation. Active claims retain
/// their state and existing results remain available. Refreshing instances
/// can still be queued without a claim and are left intact.
fn get_or_create_target_projection(
&self,
ai_projection_id: &str,
target_id: &str,
prompt_hash: &str,
) -> impl Future<Output = Result<UserAiProjection, AiProjectionError>> + Send;
/// Gets the target's instance of a projection. Returns
/// [`AiProjectionError::NotFound`] if no instance exists.
fn get_target_projection(
&self,
ai_projection_id: &str,
target_id: &str,
) -> impl Future<Output = Result<UserAiProjection, AiProjectionError>> + Send;
/// Gets an existing projection definition by id. Returns
/// [`AiProjectionError::NotFound`] if no definition exists.
fn get_projection(
&self,
id: &str,
) -> impl Future<Output = Result<AiProjection, AiProjectionError>> + Send;
/// Attempts to claim the `(ai_projection_id, target_id)` pair for
/// processing. Stale claims (older than the implementation's threshold) are
/// reclaimed first so crashed workers do not block reprocessing. Returns
/// `true` if the claim was acquired, or `false` if another worker is already
/// processing the pair.
fn try_start_processing(
&self,
ai_projection_id: &str,
target_id: &str,
) -> impl Future<Output = Result<bool, AiProjectionError>> + Send;
/// Releases a processing claim for the `(ai_projection_id, target_id)` pair,
/// allowing it to be reprocessed.
fn finish_processing(
&self,
ai_projection_id: &str,
target_id: &str,
) -> impl Future<Output = Result<(), AiProjectionError>> + Send;
/// Marks the target's projection instance as loading. A no-op if the
/// instance's `prompt_hash` no longer matches (a newer version exists).
fn set_projection_loading(
&self,
ai_projection_id: &str,
target_id: &str,
prompt_hash: &str,
) -> impl Future<Output = Result<(), AiProjectionError>> + Send;
/// Marks the target's projection instance as refreshing, signalling that a
/// regeneration is in flight while the previous result remains available.
fn set_projection_refreshing(
&self,
ai_projection_id: &str,
target_id: &str,
) -> impl Future<Output = Result<(), AiProjectionError>> + Send;
/// Stores a materialized result on the target's projection instance and
/// marks it ready. A no-op if the instance's `prompt_hash` no longer
/// matches (the result belongs to an outdated version).
#[allow(clippy::too_many_arguments)]
fn set_projection_result(
&self,
ai_projection_id: &str,
target_id: &str,
prompt_hash: &str,
result: &str,
generated_at: chrono::DateTime<chrono::Utc>,
stale_at: chrono::DateTime<chrono::Utc>,
) -> impl Future<Output = Result<(), AiProjectionError>> + Send;
/// Records a materialization error on the target's projection instance and
/// marks it errored. A no-op if the instance's `prompt_hash` no longer
/// matches.
fn set_projection_error(
&self,
ai_projection_id: &str,
target_id: &str,
prompt_hash: &str,
error: &str,
) -> impl Future<Output = Result<(), AiProjectionError>> + Send;
/// Returns whether the user has the given permission id
/// (e.g. `read:professional_features`).
fn user_has_permission(
&self,
user_id: &MacroUserIdStr<'_>,
permission: &str,
) -> impl Future<Output = Result<bool, AiProjectionError>> + Send;
/// Returns the ids of the teams the user belongs to.
fn get_user_team_ids(
&self,
user_id: &MacroUserIdStr<'_>,
) -> impl Future<Output = Result<Vec<uuid::Uuid>, AiProjectionError>> + Send;
}