use aidaemon_core::ChannelVisibility;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TurnRole {
User,
Assistant,
Tool,
}
impl TurnRole {
pub fn as_str(self) -> &'static str {
match self {
Self::User => "user",
Self::Assistant => "assistant",
Self::Tool => "tool",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct HistoryTurn {
pub id: i64,
pub role: TurnRole,
pub content: Option<String>,
pub created_at: DateTime<Utc>,
}
#[async_trait]
pub trait ConversationSource: Send + Sync {
async fn sessions_with_turns_before(
&self,
cutoff: DateTime<Utc>,
) -> anyhow::Result<Vec<String>>;
async fn is_memory_isolated(&self, session_id: &str) -> anyhow::Result<bool>;
async fn owner_only_since(
&self,
session_id: &str,
after_id: i64,
cutoff: DateTime<Utc>,
) -> anyhow::Result<bool>;
async fn last_turn_id(
&self,
session_id: &str,
after_id: i64,
cutoff: DateTime<Utc>,
) -> anyhow::Result<Option<i64>>;
async fn turns(
&self,
session_id: &str,
after_id: i64,
cutoff: DateTime<Utc>,
limit: usize,
) -> anyhow::Result<Vec<HistoryTurn>>;
async fn visibility(&self, session_id: &str) -> Option<ChannelVisibility>;
fn channel_id(&self, session_id: &str) -> Option<String>;
async fn project(&self, _session_id: &str) -> Option<aidaemon_core::ProjectRef> {
None
}
}
#[async_trait]
pub trait ConsolidationCursors: Send + Sync {
async fn cursor(&self, session_id: &str) -> i64;
async fn set_cursor(&self, session_id: &str, turn_id: i64) -> anyhow::Result<()>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoggedMessage {
pub id: i64,
pub session_id: String,
pub content: String,
pub created_at: String,
}
#[async_trait]
pub trait MessageLog: Send + Sync {
async fn message_bounds(
&self,
session_id: &str,
start: DateTime<Utc>,
end: DateTime<Utc>,
) -> anyhow::Result<(Option<i64>, Option<i64>)>;
async fn retained(&self, ids: &[i64]) -> anyhow::Result<std::collections::HashSet<i64>>;
}
#[derive(Debug, Clone, PartialEq)]
pub struct LoggedTurn {
pub role: TurnRole,
pub content: Option<String>,
pub tool_calls: Vec<String>,
pub tool_name: Option<String>,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UncapturedSessions {
pub min_turns: usize,
pub idle_before: Option<DateTime<Utc>>,
}
#[async_trait]
pub trait ActivityLog: ConversationSource + ConsolidationCursors {
async fn uncaptured_sessions(
&self,
episode_ends: &[(String, String)],
which: UncapturedSessions,
) -> anyhow::Result<Vec<String>>;
async fn owner_only_after(
&self,
session_id: &str,
since: Option<&crate::records::EpisodeBound>,
) -> anyhow::Result<bool>;
async fn turns_after(
&self,
session_id: &str,
since: Option<&crate::records::EpisodeBound>,
limit: usize,
) -> anyhow::Result<Vec<LoggedTurn>>;
async fn recent_turns(
&self,
since: DateTime<Utc>,
limit: usize,
) -> anyhow::Result<Vec<LoggedTurn>>;
async fn recent_owner_messages(
&self,
since: DateTime<Utc>,
limit: usize,
) -> anyhow::Result<Vec<LoggedTurn>>;
}