use std::collections::HashMap;
use rho_protocol::{
MessageId, SessionEvent as WireEvent, ToolCallId, ToolCallUpdate, ToolContent, ToolKind,
ToolStatus,
};
use rho_sdk::model;
use serde_json::json;
use crate::session::tree::{DisplayRowOrigin, NodeId};
pub(super) fn origin(row: usize) -> DisplayRowOrigin {
DisplayRowOrigin {
node: NodeId::from_string("node").unwrap(),
row,
}
}
pub(super) fn shell_call(id: &str, command: &str) -> model::ToolCall {
model::ToolCall::new(id.into(), "bash".into(), json!({ "command": command }))
}
pub(super) fn opened(id: &str, command: &str) -> WireEvent {
WireEvent::tool_call(ToolCallUpdate {
id: ToolCallId::new(id),
title: Some(command.to_owned()),
kind: Some(ToolKind::Other),
status: Some(ToolStatus::InProgress),
raw_input: Some(json!({ "command": command })),
content: None,
locations: None,
display: None,
})
}
pub(super) fn updated(id: &str, status: ToolStatus, content: Vec<ToolContent>) -> WireEvent {
WireEvent::tool_call(ToolCallUpdate {
status: Some(status),
content: Some(content),
..closed_update(id)
})
}
pub(super) fn finished(
id: &str,
command: &str,
status: ToolStatus,
content: Vec<ToolContent>,
) -> WireEvent {
let WireEvent::ToolCall(opened) = opened(id, command) else {
unreachable!("opened sends a tool call")
};
WireEvent::tool_call(ToolCallUpdate {
status: Some(status),
content: Some(content),
..*opened
})
}
pub(super) fn closed(id: &str) -> WireEvent {
WireEvent::tool_call(closed_update(id))
}
fn closed_update(id: &str) -> ToolCallUpdate {
ToolCallUpdate {
id: ToolCallId::new(id),
title: None,
kind: None,
status: Some(ToolStatus::Failed),
raw_input: None,
content: None,
locations: None,
display: None,
}
}
pub(super) fn text(text: &str) -> ToolContent {
ToolContent::Text { text: text.into() }
}
pub(super) fn agent(message: &str, text: &str) -> WireEvent {
WireEvent::AgentMessageChunk {
message: MessageId::new(message),
text: text.into(),
}
}
pub(super) fn canonical_messages(events: Vec<WireEvent>) -> Vec<WireEvent> {
let mut names: HashMap<String, MessageId> = HashMap::new();
let mut rename = |id: &mut MessageId| {
let next = MessageId::new(format!("m{}", names.len()));
*id = names.entry(id.as_str().to_owned()).or_insert(next).clone();
};
events
.into_iter()
.map(|mut event| {
match &mut event {
WireEvent::UserMessage { message, .. }
| WireEvent::AgentMessageChunk { message, .. }
| WireEvent::ThoughtChunk { message, .. } => rename(message),
_ => {}
}
event
})
.collect()
}
pub(super) fn without_display(events: Vec<WireEvent>) -> Vec<WireEvent> {
events
.into_iter()
.map(|mut event| {
if let WireEvent::ToolCall(update) = &mut event {
update.display = None;
}
event
})
.collect()
}
#[derive(Clone, Copy, Debug)]
pub(super) enum Placement {
FirstEvent,
Close,
}
pub(super) fn rendered(events: Vec<WireEvent>, placement: Placement) -> Vec<WireEvent> {
enum Slot {
Event(WireEvent),
Call(ToolCallId),
Moved,
}
let mut slots = Vec::new();
let mut calls: HashMap<ToolCallId, (usize, ToolCallUpdate)> = HashMap::new();
for event in events {
let WireEvent::ToolCall(update) = event else {
slots.push(Slot::Event(event));
continue;
};
let closes = matches!(
update.status,
Some(ToolStatus::Completed | ToolStatus::Failed | ToolStatus::Cancelled)
);
let Some((slot, card)) = calls.get_mut(&update.id) else {
slots.push(Slot::Call(update.id.clone()));
calls.insert(update.id.clone(), (slots.len() - 1, *update));
continue;
};
let ToolCallUpdate {
id: _,
title,
kind,
status,
raw_input,
content,
locations,
display,
} = *update;
card.title = title.or(card.title.take());
card.kind = kind.or(card.kind.take());
card.status = status.or(card.status.take());
card.raw_input = raw_input.or(card.raw_input.take());
card.content = content.or(card.content.take());
card.locations = locations.or(card.locations.take());
card.display = display.or(card.display.take());
if closes && matches!(placement, Placement::Close) {
slots[*slot] = Slot::Moved;
slots.push(Slot::Call(card.id.clone()));
*slot = slots.len() - 1;
}
}
slots
.into_iter()
.filter_map(|slot| match slot {
Slot::Event(event) => Some(event),
Slot::Call(id) => calls
.remove(&id)
.map(|(_, card)| WireEvent::tool_call(card)),
Slot::Moved => None,
})
.collect()
}