use async_trait::async_trait;
use systemprompt_identifiers::{AgentRunId, ContextId, TaskId, UserId};
#[derive(Debug, Clone)]
pub struct AgentRunLaunch {
pub user_id: UserId,
pub task_id: TaskId,
pub context_id: ContextId,
pub request: serde_json::Value,
pub origin_kind: String,
pub origin_id: String,
}
#[derive(Debug, thiserror::Error)]
pub enum AgentRunLaunchError {
#[error("invalid run request")]
InvalidRequest(#[source] serde_json::Error),
#[error("agent run {0} not found")]
NotFound(AgentRunId),
#[error("agent run launcher unavailable")]
Unavailable(#[source] Box<dyn std::error::Error + Send + Sync>),
}
impl AgentRunLaunchError {
pub fn unavailable(source: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Unavailable(Box::new(source))
}
}
#[derive(Debug, Clone, Default)]
pub struct AgentRunUpdates {
pub frames: Vec<serde_json::Value>,
pub last_seq: i64,
pub finished: bool,
pub final_status: Option<serde_json::Value>,
}
#[async_trait]
pub trait AgentRunLauncher: Send + Sync {
async fn enqueue(&self, request: AgentRunLaunch) -> Result<AgentRunId, AgentRunLaunchError>;
async fn cancel(&self, run_id: &AgentRunId) -> Result<(), AgentRunLaunchError>;
async fn run_for_task(
&self,
task_id: &TaskId,
) -> Result<Option<AgentRunId>, AgentRunLaunchError>;
async fn updates_after(
&self,
_run_id: &AgentRunId,
after_seq: i64,
) -> Result<AgentRunUpdates, AgentRunLaunchError> {
Ok(AgentRunUpdates {
last_seq: after_seq,
finished: true,
..AgentRunUpdates::default()
})
}
}
pub type DynAgentRunLauncher = std::sync::Arc<dyn AgentRunLauncher>;