use agentyk::{
ChatResponse, Message, Result, SessionId, ToolCall, ToolContext, ToolOutput, TurnAction,
TurnState, Usage,
};
use serde_json::json;
#[test]
fn a_batch_can_be_dispatched_concurrently_and_completed_out_of_order() -> Result<()> {
let session_id = SessionId::new();
let (mut state, _) = TurnState::start(session_id, 8, &Message::user("go"));
let response = ChatResponse {
message: Message::assistant_with_calls(
"",
vec![
ToolCall {
id: "call_a".into(),
name: "echo".into(),
arguments: json!({"n": 1}),
},
ToolCall {
id: "call_b".into(),
name: "echo".into(),
arguments: json!({"n": 2}),
},
ToolCall {
id: "call_c".into(),
name: "echo".into(),
arguments: json!({"n": 3}),
},
],
),
usage: Usage::default(),
};
state.on_reason_completed(&response);
let actions = state.pending_tool_actions();
assert_eq!(actions.len(), 3);
let calls: Vec<ToolCall> = actions
.into_iter()
.map(|a| {
let TurnAction::ExecuteTool { call } = a else {
unreachable!()
};
call
})
.collect();
for call in &calls {
let effects = state.on_tool_started(&call.id);
assert_eq!(effects.len(), 1);
}
assert!(
state.pending_tool_actions().is_empty(),
"every call in the batch has been started"
);
let _context = ToolContext {
session_id,
turn_id: state.turn_id,
..Default::default()
};
for call in [&calls[2], &calls[1], &calls[0]] {
let output = ToolOutput::text(format!("echoed {}", call.id));
let effects = state.on_tool_completed(&call.id, &output);
assert_eq!(effects.len(), 1);
}
assert_eq!(state.next_action(), TurnAction::Reason);
assert_eq!(state.tool_calls_executed, 3);
assert!(
state
.on_tool_completed(&calls[0].id, &ToolOutput::text("dup"))
.is_empty()
);
Ok(())
}