use std::collections::HashMap;
use pretty_assertions::assert_eq;
use rho_protocol::{
display::{
NotificationDelivery, Presentation, ToolCard, ToolCardStatus, ToolFact, ToolFamily,
ToolHeader,
},
CardTone, ContentBlock, MessageId, PlanEntry, PlanStatus, SessionEvent as WireEvent,
ToolCallId, ToolCallUpdate, ToolStatus,
};
use rho_sdk::model::ContextUsage;
use super::ExternalEvents;
use crate::{presentation::parent_message_card, run_artifacts::AttachmentEvent};
fn mapped(recorded: Vec<AttachmentEvent>) -> Vec<WireEvent> {
let mut external = ExternalEvents::default();
let mut names: HashMap<MessageId, MessageId> = HashMap::new();
let mut name = |id: &mut MessageId| {
let count = names.len();
*id = names
.entry(id.clone())
.or_insert_with(|| MessageId::new(format!("m{count}")))
.clone();
};
recorded
.iter()
.flat_map(|event| external.map(event))
.map(|mut event| {
match &mut event {
WireEvent::UserMessage { message, .. }
| WireEvent::AgentMessageChunk { message, .. }
| WireEvent::ThoughtChunk { message, .. } => name(message),
WireEvent::Card { display, .. } => event = shown_card(display.take()),
_ => {}
}
event
})
.collect()
}
fn shown_card(display: Option<Box<rho_protocol::display::NotificationCard>>) -> WireEvent {
WireEvent::Card {
title: String::new(),
markdown: String::new(),
plain: String::new(),
tone: CardTone::Neutral,
display,
}
}
fn card(status: ToolCardStatus) -> ToolCard {
ToolCard::new(
status,
ToolFamily::FileCommand,
ToolHeader::shell("$", Some("cargo test".into())),
)
}
fn call(id: &str, status: ToolStatus, title: Option<&str>, display: Presentation) -> WireEvent {
WireEvent::tool_call(ToolCallUpdate {
id: ToolCallId::new(id),
title: title.map(str::to_owned),
kind: None,
status: Some(status),
raw_input: None,
content: None,
locations: None,
display: Some(display),
})
}
#[test]
fn an_external_runs_record_shows_as_its_sessions_events() {
let finished = Presentation::Card(card(ToolCardStatus::Ok));
let text = |text: &str| AttachmentEvent::AssistantTextDelta(text.into());
let message = parent_message_card(
"Also lint.".into(),
NotificationDelivery::Received,
"steered into the running turn".into(),
);
let recorded = vec![
AttachmentEvent::Prompt("Run the tests.".into()),
AttachmentEvent::StepStarted,
AttachmentEvent::ReasoningDelta("Plan.".into()),
text("Running "),
text("them."),
AttachmentEvent::ToolStarted {
key: Some("run-1".into()),
card: card(ToolCardStatus::Running),
},
AttachmentEvent::ToolFinished {
key: Some("run-1".into()),
presentation: finished.clone(),
},
text("All pass."),
AttachmentEvent::Plan(vec![
PlanEntry {
content: "Test".into(),
status: PlanStatus::Completed,
},
PlanEntry {
content: "Lint".into(),
status: PlanStatus::InProgress,
},
]),
AttachmentEvent::Message(Box::new(message.clone())),
text("Linting."),
AttachmentEvent::ToolFinished {
key: None,
presentation: finished.clone(),
},
AttachmentEvent::ContextUsage(ContextUsage::provider_reported(1200, Some(200_000))),
AttachmentEvent::Completed,
];
let shown = mapped(recorded);
let plan_card = |status: ToolCardStatus| {
Presentation::Card(
ToolCard::new(status, ToolFamily::Form, ToolHeader::call("Plan", None)).with_facts(
vec![
ToolFact::Text {
text: "✓ Test".into(),
},
ToolFact::Text {
text: "▸ Lint".into(),
},
],
),
)
};
let chunk = |id: &str, text: &str| WireEvent::AgentMessageChunk {
message: MessageId::new(id),
text: text.into(),
};
assert_eq!(
shown,
vec![
WireEvent::UserMessage {
message: MessageId::new("m0"),
content: vec![ContentBlock::Text {
text: "Run the tests.".into()
}],
steer: None,
},
WireEvent::ThoughtChunk {
message: MessageId::new("m1"),
text: "Plan.".into()
},
chunk("m2", "Running "),
chunk("m2", "them."),
call(
"run-1",
ToolStatus::InProgress,
Some("cargo test"),
Presentation::Card(card(ToolCardStatus::Running)),
),
call("run-1", ToolStatus::Completed, None, finished.clone()),
chunk("m3", "All pass."),
WireEvent::Plan {
entries: vec![
PlanEntry {
content: "Test".into(),
status: PlanStatus::Completed,
},
PlanEntry {
content: "Lint".into(),
status: PlanStatus::InProgress,
},
],
},
call(
"acp-plan",
ToolStatus::InProgress,
Some("Plan"),
plan_card(ToolCardStatus::Running),
),
shown_card(Some(Box::new(message))),
chunk("m4", "Linting."),
call("unnamed-call-1", ToolStatus::Completed, None, finished),
WireEvent::Usage {
used_tokens: 1200,
window_tokens: Some(200_000),
cost_usd_micros: None,
},
call(
"acp-plan",
ToolStatus::Completed,
None,
plan_card(ToolCardStatus::Ok),
),
]
);
}
#[test]
fn a_runs_end_closes_the_calls_it_left_open() {
let progress = || {
card(ToolCardStatus::Running).with_facts(vec![rho_protocol::display::ToolFact::Text {
text: "2 of 3".into(),
}])
};
let closed = |end: AttachmentEvent| {
let mut external = ExternalEvents::default();
for event in [
AttachmentEvent::ToolStarted {
key: Some("plan".into()),
card: card(ToolCardStatus::Running),
},
AttachmentEvent::ToolStarted {
key: Some("run-1".into()),
card: card(ToolCardStatus::Running),
},
AttachmentEvent::ToolFinished {
key: Some("run-1".into()),
presentation: Presentation::Card(card(ToolCardStatus::Ok)),
},
AttachmentEvent::ToolUpdated {
key: Some("plan".into()),
card: progress(),
},
] {
external.map(&event);
}
external.map(&end)
};
let plan = |status: ToolStatus, shows: ToolCardStatus| {
let mut card = progress();
card.status = shows;
vec![call("plan", status, None, Presentation::Card(card))]
};
assert_eq!(
[
closed(AttachmentEvent::Completed),
closed(AttachmentEvent::Cancelled),
closed(AttachmentEvent::Failed("boom".into())),
],
[
plan(ToolStatus::Completed, ToolCardStatus::Ok),
plan(ToolStatus::Cancelled, ToolCardStatus::Interrupted),
plan(ToolStatus::Failed, ToolCardStatus::Error),
]
);
}