use super::super::inbox::{self, Epitaph};
use super::super::{Deps, Error};
use super::step_commit::read_branch_tip;
use brazen::Content;
use std::path::Path;
pub(super) fn terminal_text(blocks: &[Content]) -> Option<String> {
let mut out = String::new();
for block in blocks {
if let Content::Text(text) = block {
out.push_str(text);
}
}
(!out.is_empty()).then_some(out)
}
pub(super) fn deposit_terminal(
repo: &Path,
conv_id: &str,
worktree: &Path,
epitaph: Epitaph,
response: Option<&str>,
deps: &Deps<'_>,
) -> Result<(), Error> {
let terminal_ref = read_branch_tip(worktree, deps)?;
inbox::deposit_child_result(repo, conv_id, epitaph, &terminal_ref, response, deps.clock)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn terminal_text_joins_text_blocks_and_skips_non_text() {
let blocks = vec![
Content::Text("hello ".into()),
Content::Thinking {
text: "ignored".into(),
signature: None,
id: None,
encrypted_content: None,
},
Content::ToolUse {
id: "t1".into(),
name: "bash".into(),
input: json!({}),
signature: None,
},
Content::Text("world".into()),
];
assert_eq!(terminal_text(&blocks).as_deref(), Some("hello world"));
}
#[test]
fn terminal_text_is_none_when_agent_did_not_speak() {
let blocks = vec![Content::ToolUse {
id: "t1".into(),
name: "bash".into(),
input: json!({}),
signature: None,
}];
assert_eq!(terminal_text(&blocks), None);
assert_eq!(terminal_text(&[]), None);
}
}