use std::collections::HashSet;
use agent_client_protocol::v1::schema::{
AvailableCommandsUpdate, CompactionId, CompactionStatus, CompactionUpdate, ConfigOptionUpdate,
ContentBlock, ContentChunk, Cost, CurrentModeUpdate, Diff, EmbeddedResource,
EmbeddedResourceResource, ImageContent, MessageId, Notice, NoticeSeverity, Plan, PlanEntry,
PlanEntryPriority, PlanEntryStatus, ResourceLink, SessionId, SessionInfoUpdate,
SessionNotification, SessionUpdate, TextResourceContents, ToolCall, ToolCallContent,
ToolCallId, ToolCallLocation, ToolCallStatus, ToolCallUpdate, ToolCallUpdateFields, ToolKind,
UsageUpdate,
};
use rho_protocol::{self as wire, SessionEvent};
use serde_json::Value;
use super::{host_features::HostFeatures, options, permission};
use crate::permission::PermissionMode;
pub(super) const DELETED_FILE_META_KEY: &str = "rho/fileDeleted";
const REPLAY_META_KEY: &str = "isReplay";
const RESPONSE_DISCARDED_NOTICE: &str = "[provider response discarded; retrying]";
const UNNAMED_TOOL_TITLE: &str = "tool";
pub(super) struct Updates {
features: HostFeatures,
seen_tools: HashSet<String>,
nonce: String,
minted: u64,
}
impl Updates {
pub(super) fn new(features: HostFeatures) -> Self {
Self::with_nonce(features, uuid::Uuid::new_v4().to_string())
}
fn with_nonce(features: HostFeatures, nonce: String) -> Self {
Self {
features,
seen_tools: HashSet::new(),
nonce,
minted: 0,
}
}
pub(super) fn map(&mut self, event: &SessionEvent) -> Vec<SessionUpdate> {
match event {
SessionEvent::UserMessage {
message,
content,
steer: _,
} => content
.iter()
.filter_map(content_block)
.map(|block| SessionUpdate::UserMessageChunk(chunk(block, message)))
.collect(),
SessionEvent::AgentMessageChunk { message, text } => {
vec![SessionUpdate::AgentMessageChunk(chunk(
ContentBlock::from(text.as_str()),
message,
))]
}
SessionEvent::ThoughtChunk { message, text } => {
vec![SessionUpdate::AgentThoughtChunk(chunk(
ContentBlock::from(text.as_str()),
message,
))]
}
SessionEvent::ToolCall(update) => self.tool_call(update).into_iter().collect(),
SessionEvent::Plan { entries } => vec![SessionUpdate::Plan(Plan::new(
entries.iter().map(plan_entry).collect(),
))],
SessionEvent::Usage {
used_tokens,
window_tokens,
cost_usd_micros,
} => vec![usage(*used_tokens, *window_tokens, *cost_usd_micros)],
SessionEvent::TitleChanged { title } => vec![SessionUpdate::SessionInfoUpdate(
SessionInfoUpdate::new().title(title.clone()),
)],
SessionEvent::Compaction { id, status } if self.features.compaction => {
Vec::from_iter(compaction_update(id, status))
}
SessionEvent::Compaction { .. } => Vec::new(),
SessionEvent::ResponseDiscarded => {
let id = self.mint("rho-msg");
vec![SessionUpdate::AgentThoughtChunk(
ContentChunk::new(ContentBlock::from(RESPONSE_DISCARDED_NOTICE))
.message_id(MessageId::new(id)),
)]
}
SessionEvent::Card {
title,
markdown,
plain,
tone,
display: _,
} => vec![self.card(title, markdown, plain, *tone)],
SessionEvent::Notice { text } => vec![self.notice(text)],
SessionEvent::State { .. }
| SessionEvent::InputRequested { .. }
| SessionEvent::InputResolved { .. }
| SessionEvent::InputHeld { .. }
| SessionEvent::TurnEnded { .. }
| SessionEvent::OptionsChanged { .. }
| SessionEvent::Background { .. }
| SessionEvent::Activity { .. }
| SessionEvent::ProviderRequest
| SessionEvent::ProviderRetry { .. }
| SessionEvent::SteeringDelivered { .. } => Vec::new(),
_ => {
tracing::debug!("skipping a session event this rho does not know");
Vec::new()
}
}
}
fn notice(&mut self, text: &str) -> SessionUpdate {
if self.features.notices {
return SessionUpdate::Notice(Notice::new(NoticeSeverity::Info, text));
}
let id = self.mint("rho-msg");
SessionUpdate::AgentMessageChunk(
ContentChunk::new(ContentBlock::from(text)).message_id(MessageId::new(id)),
)
}
fn tool_call(&mut self, update: &wire::ToolCallUpdate) -> Option<SessionUpdate> {
let id = ToolCallId::new(update.id.as_str());
let content = update
.content
.as_ref()
.map(|content| content.iter().filter_map(tool_content).collect::<Vec<_>>());
let locations = update
.locations
.as_ref()
.map(|locations| locations.iter().map(location).collect::<Vec<_>>());
if self.seen_tools.insert(update.id.as_str().to_owned()) {
let title = update.title.as_deref().unwrap_or(UNNAMED_TOOL_TITLE);
let mut call = ToolCall::new(id, title)
.kind(update.kind.map_or(ToolKind::Other, tool_kind))
.status(update.status.map_or(ToolCallStatus::Pending, tool_status))
.raw_input(update.raw_input.clone());
if let Some(content) = content {
call = call.content(content);
}
if let Some(locations) = locations {
call = call.locations(locations);
}
return Some(SessionUpdate::ToolCall(call));
}
if update.title.is_none()
&& update.kind.is_none()
&& update.status.is_none()
&& update.raw_input.is_none()
&& content.is_none()
&& locations.is_none()
{
return None;
}
let fields = ToolCallUpdateFields::new()
.title(update.title.clone())
.kind(update.kind.map(tool_kind))
.status(update.status.map(tool_status))
.raw_input(update.raw_input.clone())
.content(content)
.locations(locations);
Some(SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(
id, fields,
)))
}
fn card(
&mut self,
title: &str,
markdown: &str,
plain: &str,
tone: wire::CardTone,
) -> SessionUpdate {
let status = match tone {
wire::CardTone::Error => ToolCallStatus::Failed,
wire::CardTone::Neutral
| wire::CardTone::Accent
| wire::CardTone::Success
| wire::CardTone::Warning => ToolCallStatus::Completed,
_ => ToolCallStatus::Completed,
};
let id = self.mint("rho-notice");
let mut call = ToolCall::new(ToolCallId::new(id), title)
.kind(ToolKind::Other)
.status(status);
if !plain.trim().is_empty() {
call = call
.content(vec![ToolCallContent::from(markdown)])
.raw_output(Value::String(plain.to_owned()));
}
SessionUpdate::ToolCall(call)
}
fn mint(&mut self, kind: &str) -> String {
self.minted += 1;
format!("{kind}-{}-{}", self.nonce, self.minted)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Delivery {
Live,
Replay,
}
pub(super) fn notification(
session: &SessionId,
update: SessionUpdate,
delivery: Delivery,
) -> SessionNotification {
let notification = SessionNotification::new(session.clone(), update);
match delivery {
Delivery::Live => notification,
Delivery::Replay => notification.meta(serde_json::Map::from_iter([(
REPLAY_META_KEY.to_owned(),
Value::Bool(true),
)])),
}
}
pub(super) fn options_changed(
old: &wire::SessionOptions,
new: &wire::SessionOptions,
features: HostFeatures,
) -> Vec<SessionUpdate> {
let mut updates = Vec::new();
let config = options::config_options(new, features.toggles);
if config != options::config_options(old, features.toggles) {
updates.push(SessionUpdate::ConfigOptionUpdate(ConfigOptionUpdate::new(
config,
)));
}
if new.permission_mode.current != old.permission_mode.current {
if let Ok(mode) = PermissionMode::try_from(new.permission_mode.current) {
updates.push(SessionUpdate::CurrentModeUpdate(CurrentModeUpdate::new(
permission::mode_id(mode),
)));
}
}
if new.commands != old.commands {
updates.push(commands_update(&new.commands));
}
updates
}
fn compaction_update(
id: &wire::CompactionId,
status: &wire::CompactionStatus,
) -> Option<SessionUpdate> {
let id = CompactionId::new(id.as_str());
let update = match status {
wire::CompactionStatus::Started { .. } => {
CompactionUpdate::new(id, CompactionStatus::InProgress)
}
wire::CompactionStatus::Completed { .. } => {
CompactionUpdate::new(id, CompactionStatus::Completed)
}
wire::CompactionStatus::Cancelled => CompactionUpdate::new(id, CompactionStatus::Cancelled),
wire::CompactionStatus::Failed { error } => {
CompactionUpdate::new(id, CompactionStatus::Failed).error(error.clone())
}
_ => return None,
};
Some(SessionUpdate::CompactionUpdate(update))
}
pub(super) fn commands_update(commands: &[wire::SessionCommand]) -> SessionUpdate {
SessionUpdate::AvailableCommandsUpdate(AvailableCommandsUpdate::new(
options::available_commands(commands),
))
}
fn chunk(block: ContentBlock, message: &wire::MessageId) -> ContentChunk {
ContentChunk::new(block).message_id(MessageId::new(message.as_str()))
}
fn content_block(block: &wire::ContentBlock) -> Option<ContentBlock> {
Some(match block {
wire::ContentBlock::Text { text } => ContentBlock::from(text.as_str()),
wire::ContentBlock::Image { mime_type, data } => {
ContentBlock::Image(ImageContent::new(data.as_str(), mime_type.as_str()))
}
wire::ContentBlock::Resource { uri, text } => ContentBlock::Resource(
EmbeddedResource::new(EmbeddedResourceResource::TextResourceContents(
TextResourceContents::new(text.as_str(), uri.as_str()),
)),
),
wire::ContentBlock::ResourceLink {
uri,
name,
description,
} => ContentBlock::ResourceLink(
ResourceLink::new(name.as_str(), uri.as_str()).description(description.clone()),
),
wire::ContentBlock::Context { .. } => return None,
_ => return None,
})
}
fn tool_kind(kind: wire::ToolKind) -> ToolKind {
match kind {
wire::ToolKind::Read => ToolKind::Read,
wire::ToolKind::Edit => ToolKind::Edit,
wire::ToolKind::Delete => ToolKind::Delete,
wire::ToolKind::Move => ToolKind::Move,
wire::ToolKind::Search => ToolKind::Search,
wire::ToolKind::Execute => ToolKind::Execute,
wire::ToolKind::Think => ToolKind::Think,
wire::ToolKind::Fetch => ToolKind::Fetch,
wire::ToolKind::SwitchMode => ToolKind::SwitchMode,
wire::ToolKind::Other => ToolKind::Other,
_ => ToolKind::Other,
}
}
fn tool_status(status: wire::ToolStatus) -> ToolCallStatus {
match status {
wire::ToolStatus::Pending => ToolCallStatus::Pending,
wire::ToolStatus::InProgress => ToolCallStatus::InProgress,
wire::ToolStatus::Completed => ToolCallStatus::Completed,
wire::ToolStatus::Failed => ToolCallStatus::Failed,
_ => ToolCallStatus::InProgress,
}
}
fn tool_content(content: &wire::ToolContent) -> Option<ToolCallContent> {
Some(match content {
wire::ToolContent::Text { text } => ToolCallContent::from(text.as_str()),
wire::ToolContent::Image { mime_type, data } => ToolCallContent::from(ContentBlock::Image(
ImageContent::new(data.as_str(), mime_type.as_str()),
)),
wire::ToolContent::Diff {
path,
old_text,
new_text,
deleted,
} => {
let diff = Diff::new(path.clone(), new_text.as_str()).old_text(old_text.clone());
ToolCallContent::Diff(if *deleted {
diff.meta(serde_json::Map::from_iter([(
DELETED_FILE_META_KEY.to_owned(),
Value::Bool(true),
)]))
} else {
diff
})
}
_ => return None,
})
}
fn location(location: &wire::Location) -> ToolCallLocation {
ToolCallLocation::new(location.path.clone())
.line(location.line.map(|row| row.saturating_add(1)))
}
fn plan_entry(entry: &wire::PlanEntry) -> PlanEntry {
let status = match entry.status {
wire::PlanStatus::Pending => PlanEntryStatus::Pending,
wire::PlanStatus::InProgress => PlanEntryStatus::InProgress,
wire::PlanStatus::Completed => PlanEntryStatus::Completed,
_ => PlanEntryStatus::Pending,
};
PlanEntry::new(entry.content.clone(), PlanEntryPriority::Medium, status)
}
fn usage(used: u64, window: Option<u64>, cost_usd_micros: Option<u64>) -> SessionUpdate {
let cost = cost_usd_micros.map(|micros| Cost::new(micros as f64 / 1_000_000.0, "USD"));
SessionUpdate::UsageUpdate(UsageUpdate::new(used, window.unwrap_or(0)).cost(cost))
}
#[cfg(test)]
#[path = "updates_tests.rs"]
mod tests;