Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

perf(agent): scan tool results in reverse to find last typed payload

Steven Enamakelb4c47af09fb9
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs b/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs
index 680f4d1500..79167a8efd 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs
@@ -4,13 +4,13 @@ use serde_json::Value;
/// Return the last tool-result JSON object whose `type` matches `expected_type`.
/// Callers adapt their transcript representation to an iterator of result text.
-pub(crate) fn extract_last_typed_payload<'a>(
- contents: impl IntoIterator<Item = &'a str>,
- expected_type: &str,
-) -> Option<Value> {
- contents
- .into_iter()
- .filter_map(|content| serde_json::from_str::<Value>(content.trim()).ok())
- .filter(|value| value.get("type").and_then(Value::as_str) == Some(expected_type))
- .last()
+pub(crate) fn extract_last_typed_payload<'a, I>(contents: I, expected_type: &str) -> Option<Value>
+where
+ I: IntoIterator<Item = &'a str>,
+ I::IntoIter: DoubleEndedIterator,
+{
+ contents.into_iter().rev().find_map(|content| {
+ let value = serde_json::from_str::<Value>(content.trim()).ok()?;
+ (value.get("type").and_then(Value::as_str) == Some(expected_type)).then_some(value)
+ })
}