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 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()
}