use std::collections::HashMap;
use std::future::{Future as _, poll_fn};
use std::pin::Pin;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::task::Poll;
use tea::{AgentRuntime, RuntimeCommandOutcome, RuntimeSessionState, SessionStats};
use tea_coding_tools::WorkspaceRoot;
use tea_mcp::{McpError, McpManager, McpServerHealth, McpServerId};
use tea_model::{ModelCapabilities, ModelSpec};
use tea_policy::WorkspaceId;
use tea_protocol::{
AgentCommand, ApprovalDecision, ApprovalId, BranchId, CanonicalMessage, CommandEnvelope,
CommandId, CommandText, ContentBlock, MessageId, ModelRef, ProfileId, ProtocolMetadata,
ProtocolTimestamp, ReasoningEffort, SessionId,
};
use tea_session::{SessionCatalogEntry, SessionName};
use tea_tools::ToolName;
use crate::config::CodingSettings;
use crate::mcp::{self, McpServiceSnapshot};
use crate::resources::ResourceCatalog;
use crate::{CodingError, CodingErrorCode};
const MAX_OWNED_RUNS: usize = 64;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommandAcceptance {
command_id: CommandId,
session_id: SessionId,
}
impl CommandAcceptance {
#[must_use]
pub const fn command_id(self) -> CommandId {
self.command_id
}
#[must_use]
pub const fn session_id(self) -> SessionId {
self.session_id
}
}
#[derive(Debug)]
pub struct CodingAgentService {
runtime: Arc<AgentRuntime>,
workspace: WorkspaceRoot,
resources: ResourceCatalog,
settings: CodingSettings,
workspace_id: WorkspaceId,
runs: Mutex<
HashMap<SessionId, tokio::task::JoinHandle<Result<RuntimeCommandOutcome, CodingError>>>,
>,
mcp_manager: Mutex<Option<Arc<McpManager>>>,
}
#[allow(clippy::missing_errors_doc)] impl CodingAgentService {
pub(crate) fn new(
runtime: AgentRuntime,
workspace: WorkspaceRoot,
resources: ResourceCatalog,
settings: CodingSettings,
workspace_id: WorkspaceId,
mcp_manager: Option<Arc<McpManager>>,
) -> Self {
Self {
runtime: Arc::new(runtime),
workspace,
resources,
settings,
workspace_id,
runs: Mutex::new(HashMap::new()),
mcp_manager: Mutex::new(mcp_manager),
}
}
#[must_use]
pub const fn workspace(&self) -> &WorkspaceRoot {
&self.workspace
}
#[must_use]
pub const fn workspace_id(&self) -> &WorkspaceId {
&self.workspace_id
}
#[must_use]
pub const fn resources(&self) -> &ResourceCatalog {
&self.resources
}
#[must_use]
pub const fn settings(&self) -> &CodingSettings {
&self.settings
}
#[must_use]
pub fn models(&self) -> Vec<ModelRef> {
self.runtime
.models()
.iter()
.map(|model| model.model_ref().clone())
.collect()
}
#[must_use]
pub fn model_capabilities(&self, model_ref: &ModelRef) -> Option<ModelCapabilities> {
self.runtime
.models()
.iter()
.find(|model| model.model_ref() == model_ref)
.map(tea_model::ModelSpec::capabilities)
}
#[must_use]
pub fn model_spec(&self, model_ref: &ModelRef) -> Option<&ModelSpec> {
self.runtime
.models()
.iter()
.find(|model| model.model_ref() == model_ref)
}
pub fn mcp_snapshot(&self) -> Result<McpServiceSnapshot, McpError> {
let manager = self
.mcp_manager
.lock()
.map_err(|_| tea_mcp::McpError::new(tea_mcp::McpErrorCode::Unavailable))?
.clone();
mcp::snapshot(manager.as_deref(), now_mcp()?)
}
pub async fn reconnect_mcp(
&self,
server_id: &McpServerId,
) -> Result<McpServerHealth, McpError> {
let manager = self
.mcp_manager
.lock()
.map_err(|_| tea_mcp::McpError::new(tea_mcp::McpErrorCode::Unavailable))?
.clone()
.ok_or_else(|| tea_mcp::McpError::new(tea_mcp::McpErrorCode::Unavailable))?;
manager.reconnect(server_id, now_mcp()?).await
}
pub fn subscribe(
&self,
session_id: SessionId,
) -> Result<tokio::sync::mpsc::Receiver<tea_protocol::EventEnvelope>, CodingError> {
self.runtime
.subscribe(session_id)
.map_err(CodingError::from)
}
pub async fn create_session(&self) -> Result<SessionId, CodingError> {
let outcome = self
.runtime
.send(envelope(
None,
AgentCommand::CreateSession {
profile_id: ProfileId::from_str("coding-agent").map_err(|_| invalid())?,
metadata: ProtocolMetadata::default(),
},
)?)
.await
.map_err(CodingError::from)?;
match outcome {
RuntimeCommandOutcome::Created { session_id } => Ok(session_id),
_ => Err(runtime_error()),
}
}
pub async fn open_session(
&self,
session_id: SessionId,
) -> Result<RuntimeSessionState, CodingError> {
self.runtime
.attach_session(session_id)
.await
.map_err(CodingError::from)
}
pub async fn list_sessions(&self) -> Result<Vec<SessionCatalogEntry>, CodingError> {
self.runtime
.list_sessions()
.await
.map_err(CodingError::from)
}
pub async fn name_session(
&self,
session_id: SessionId,
name: Option<SessionName>,
) -> Result<(), CodingError> {
self.runtime
.set_session_name(session_id, name)
.await
.map_err(CodingError::from)
}
pub async fn snapshot(
&self,
session_id: SessionId,
) -> Result<RuntimeSessionState, CodingError> {
self.runtime
.session_state(session_id)
.await
.map_err(CodingError::from)
}
pub async fn session_snapshot(
&self,
session_id: SessionId,
) -> Result<tea_session::SessionSnapshot, CodingError> {
self.runtime
.snapshot(session_id)
.await
.map_err(CodingError::from)
}
pub async fn stats(&self, session_id: SessionId) -> Result<SessionStats, CodingError> {
self.runtime
.session_stats(session_id)
.await
.map_err(CodingError::from)
}
pub fn prompt(
&self,
session_id: SessionId,
text: impl Into<String>,
) -> Result<CommandAcceptance, CodingError> {
let content = vec![ContentBlock::text(text.into()).map_err(|_| invalid())?];
self.prompt_content(session_id, content)
}
pub fn prompt_content(
&self,
session_id: SessionId,
content: Vec<ContentBlock>,
) -> Result<CommandAcceptance, CodingError> {
let timestamp = now()?;
let message = CanonicalMessage::user(new_id::<MessageId>()?, content, timestamp)
.map_err(|_| invalid())?;
let envelope = envelope_at(
Some(session_id),
AgentCommand::Prompt { message },
timestamp,
)?;
self.start_owned(session_id, envelope)
}
pub async fn wait(&self, session_id: SessionId) -> Result<RuntimeCommandOutcome, CodingError> {
poll_fn(|context| {
let Ok(mut runs) = self.runs.lock() else {
return Poll::Ready(Err(runtime_error()));
};
let Some(handle) = runs.get_mut(&session_id) else {
return Poll::Ready(Err(CodingError::new(
CodingErrorCode::Runtime,
"session has no owned run",
)));
};
match Pin::new(handle).poll(context) {
Poll::Pending => Poll::Pending,
Poll::Ready(joined) => {
runs.remove(&session_id);
Poll::Ready(
joined
.map_err(|_| runtime_error())
.and_then(|result| result),
)
}
}
})
.await
}
pub async fn steer(
&self,
session_id: SessionId,
text: impl Into<String>,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(
session_id,
AgentCommand::Steer {
text: CommandText::new(text).map_err(|_| invalid())?,
},
)
.await
}
pub async fn follow_up(
&self,
session_id: SessionId,
text: impl Into<String>,
) -> Result<RuntimeCommandOutcome, CodingError> {
let timestamp = now()?;
let message = CanonicalMessage::user(
new_id::<MessageId>()?,
vec![ContentBlock::text(text.into()).map_err(|_| invalid())?],
timestamp,
)
.map_err(|_| invalid())?;
self.send_at(session_id, AgentCommand::FollowUp { message }, timestamp)
.await
}
pub async fn abort(&self, session_id: SessionId) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(session_id, AgentCommand::Abort {}).await
}
pub fn approve(
&self,
session_id: SessionId,
approval_id: ApprovalId,
decision: ApprovalDecision,
) -> Result<CommandAcceptance, CodingError> {
let command = envelope(
Some(session_id),
AgentCommand::ResolveApproval {
approval_id,
decision,
},
)?;
self.start_owned(session_id, command)
}
pub async fn set_model(
&self,
session_id: SessionId,
model: ModelRef,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(session_id, AgentCommand::SetModel { model })
.await
}
pub async fn set_reasoning_effort(
&self,
session_id: SessionId,
reasoning_effort: ReasoningEffort,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(
session_id,
AgentCommand::SetReasoningEffort { reasoning_effort },
)
.await
}
pub async fn set_profile(
&self,
session_id: SessionId,
profile_id: ProfileId,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(session_id, AgentCommand::SetProfile { profile_id })
.await
}
pub async fn set_active_tools(
&self,
session_id: SessionId,
names: Vec<ToolName>,
) -> Result<(), CodingError> {
self.runtime
.set_active_tools(session_id, names)
.await
.map_err(CodingError::from)
}
pub async fn compact(
&self,
session_id: SessionId,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(
session_id,
AgentCommand::CompactSession { instruction: None },
)
.await
}
pub async fn fork(
&self,
session_id: SessionId,
from_message_id: MessageId,
branch_id: BranchId,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.send(
session_id,
AgentCommand::ForkSession {
from_message_id,
branch_id,
},
)
.await
}
pub async fn shutdown(&self) {
let session_ids = self
.runs
.lock()
.map(|runs| runs.keys().copied().collect::<Vec<_>>())
.unwrap_or_default();
for session_id in session_ids {
if let Ok(command) = envelope(Some(session_id), AgentCommand::Abort {}) {
let _ = self.runtime.send(command).await;
}
}
let handles = self
.runs
.lock()
.map(|mut runs| runs.drain().map(|(_, handle)| handle).collect::<Vec<_>>())
.unwrap_or_default();
for handle in &handles {
handle.abort();
}
for handle in handles {
let _ = handle.await;
}
let manager = self
.mcp_manager
.lock()
.ok()
.and_then(|mut manager| manager.take());
if let Some(manager) = manager {
let _ = manager.shutdown().await;
}
}
fn start_owned(
&self,
session_id: SessionId,
command: CommandEnvelope,
) -> Result<CommandAcceptance, CodingError> {
let command_id = command.command_id();
let runtime = Arc::clone(&self.runtime);
let mut runs = self.runs.lock().map_err(|_| runtime_error())?;
if runs.contains_key(&session_id) || runs.len() >= MAX_OWNED_RUNS {
return Err(CodingError::new(
CodingErrorCode::Runtime,
"session already has an owned run",
));
}
let handle =
tokio::spawn(async move { runtime.send(command).await.map_err(CodingError::from) });
runs.insert(session_id, handle);
Ok(CommandAcceptance {
command_id,
session_id,
})
}
async fn send(
&self,
session_id: SessionId,
command: AgentCommand,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.runtime
.send(envelope(Some(session_id), command)?)
.await
.map_err(CodingError::from)
}
async fn send_at(
&self,
session_id: SessionId,
command: AgentCommand,
timestamp: ProtocolTimestamp,
) -> Result<RuntimeCommandOutcome, CodingError> {
self.runtime
.send(envelope_at(Some(session_id), command, timestamp)?)
.await
.map_err(CodingError::from)
}
}
impl Drop for CodingAgentService {
fn drop(&mut self) {
if let Ok(runs) = self.runs.get_mut() {
for handle in runs.values() {
handle.abort();
}
}
let Some(manager) = self.mcp_manager.get_mut().ok().and_then(Option::take) else {
return;
};
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime.spawn(async move {
let _ = manager.shutdown().await;
});
return;
}
if let Ok(runtime) = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
{
let _ = runtime.block_on(manager.shutdown());
}
}
}
fn envelope(
session_id: Option<SessionId>,
command: AgentCommand,
) -> Result<CommandEnvelope, CodingError> {
envelope_at(session_id, command, now()?)
}
fn envelope_at(
session_id: Option<SessionId>,
command: AgentCommand,
timestamp: ProtocolTimestamp,
) -> Result<CommandEnvelope, CodingError> {
CommandEnvelope::new(new_id::<CommandId>()?, session_id, timestamp, command)
.map_err(|_| invalid())
}
fn new_id<T: FromStr>() -> Result<T, CodingError> {
UuidString::new().0.parse().map_err(|_| invalid())
}
struct UuidString(String);
impl UuidString {
fn new() -> Self {
Self(uuid::Uuid::now_v7().hyphenated().to_string())
}
}
fn now() -> Result<ProtocolTimestamp, CodingError> {
chrono::Utc::now()
.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
.parse()
.map_err(|_| invalid())
}
fn now_mcp() -> Result<ProtocolTimestamp, McpError> {
chrono::Utc::now()
.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
.parse()
.map_err(|_| tea_mcp::McpError::new(tea_mcp::McpErrorCode::Configuration))
}
fn invalid() -> CodingError {
CodingError::new(
CodingErrorCode::InvalidInput,
"canonical coding command is invalid",
)
}
fn runtime_error() -> CodingError {
CodingError::new(CodingErrorCode::Runtime, "coding runtime operation failed")
}