Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/hive-agent/src/transcribe.rs

176 lines
6.5 KiB
Rust

//! `Request::Transcribe`: the text of an audio file, from the swarm's
//! speech-to-text service.
//!
//! The harness holds the swarm queue connection, so an agent's tool call
//! reaches the service through it: read the file, publish the bytes on this
//! agent's request subject (`swarm_wire::stt`), and wait for the service's
//! JSON answer.
use std::path::Path;
use async_nats::client::RequestErrorKind;
use hive_agent_sock::Response;
use swarm_wire::stt;
use tokio::io::AsyncReadExt as _;
/// The largest file sent. The queue carries 8 MiB a message; the service
/// refuses what it was configured not to take, so this only keeps the harness
/// from reading something huge into memory.
const MAX_BYTES: u64 = 6 * 1024 * 1024;
/// Ask the service for the text of the audio file at `path`.
pub async fn run(path: &str) -> Response {
let agent = crate::identity::label();
if agent.is_empty() {
return err("this agent has no label, so the service has no subject to answer on");
}
let bytes = match read_audio(Path::new(path)).await {
Ok(bytes) => bytes,
Err(message) => return err(message),
};
let client = crate::swarm_queue::client().await;
// `client.request` would wait the connection's default 10 seconds, long
// before a queued or long memo is done; this request names its own wait.
match client
.send_request(stt::request_subject(&agent), request(bytes))
.await
{
Ok(reply) => from_wire(&reply.payload),
Err(e) => err(request_failure(e.kind())),
}
}
/// How long a request waits for the answer: as long as the service may take,
/// not the connection's default, which `async-nats` sets to 10 seconds.
const fn reply_wait() -> std::time::Duration {
stt::REPLY_DEADLINE
}
/// The request for `bytes`, waiting [`reply_wait`] for the answer.
fn request(bytes: Vec<u8>) -> async_nats::Request {
async_nats::Request::new()
.payload(bytes.into())
.timeout(Some(reply_wait()))
}
/// The file's bytes, if it is a regular file small enough to send.
async fn read_audio(path: &Path) -> Result<Vec<u8>, String> {
let meta = tokio::fs::metadata(path)
.await
.map_err(|e| format!("cannot read {}: {e}", path.display()))?;
if !meta.is_file() {
return Err(format!("{} is not a regular file", path.display()));
}
if meta.len() > MAX_BYTES {
return Err(format!(
"{} is {} bytes, over the {MAX_BYTES} byte limit",
path.display(),
meta.len()
));
}
// The size check above can go stale (the file may grow, or be a
// pseudo-file that reports nothing), so the read is capped as well.
let cannot = |e: std::io::Error| format!("cannot read {}: {e}", path.display());
let file = tokio::fs::File::open(path).await.map_err(cannot)?;
let mut bytes = Vec::new();
file.take(MAX_BYTES + 1)
.read_to_end(&mut bytes)
.await
.map_err(cannot)?;
if bytes.len() as u64 > MAX_BYTES {
return Err(format!(
"{} is over the {MAX_BYTES} byte limit",
path.display()
));
}
Ok(bytes)
}
/// What a failed publish-and-wait means to the agent.
fn request_failure(kind: RequestErrorKind) -> &'static str {
match kind {
RequestErrorKind::NoResponders => "no speech-to-text service is running in this swarm",
RequestErrorKind::TimedOut => "the speech-to-text service did not answer in time",
RequestErrorKind::MaxPayloadExceeded => {
"the audio is too large for the swarm queue to carry"
}
RequestErrorKind::InvalidSubject | RequestErrorKind::Other => {
"the speech-to-text service could not be reached"
}
}
}
/// The service's JSON answer as the socket's response.
fn from_wire(payload: &[u8]) -> Response {
match serde_json::from_slice::<stt::Response>(payload) {
Ok(stt::Response::Ok { text, cached }) => Response::Transcript { text, cached },
Ok(stt::Response::Failed { message }) => Response::Err { message },
Err(_) => err("the speech-to-text service sent an answer this harness does not understand"),
}
}
fn err(message: impl Into<String>) -> Response {
Response::Err {
message: message.into(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_services_answers_map_to_socket_responses() {
let ok = br#"{"status":"ok","text":"hello","cached":true}"#;
assert!(matches!(
from_wire(ok),
Response::Transcript { ref text, cached: true } if text == "hello"
));
let failed = br#"{"status":"failed","message":"the audio could not be decoded"}"#;
assert!(matches!(
from_wire(failed),
Response::Err { ref message } if message == "the audio could not be decoded"
));
assert!(matches!(from_wire(b"nonsense"), Response::Err { .. }));
}
#[test]
fn a_request_names_the_services_reply_deadline_not_the_connections_default() {
assert_eq!(reply_wait(), stt::REPLY_DEADLINE);
assert!(stt::REPLY_DEADLINE > std::time::Duration::from_secs(10));
// `request` builds with that wait; the request type has no getter, so
// this only pins that it builds.
let _ = request(vec![1, 2, 3]);
}
#[test]
fn each_failure_kind_says_what_it_is() {
for (kind, said) in [
(RequestErrorKind::NoResponders, "no speech-to-text service"),
(RequestErrorKind::TimedOut, "did not answer in time"),
(RequestErrorKind::MaxPayloadExceeded, "too large"),
(RequestErrorKind::Other, "could not be reached"),
] {
assert!(request_failure(kind).contains(said), "{kind:?}");
}
}
#[tokio::test]
async fn only_a_small_regular_file_is_read() {
let dir = tempfile::tempdir().unwrap();
let small = dir.path().join("memo.ogg");
std::fs::write(&small, b"OggS").unwrap();
assert_eq!(read_audio(&small).await.unwrap(), b"OggS");
let missing = read_audio(&dir.path().join("none.ogg")).await.unwrap_err();
assert!(missing.contains("cannot read"), "{missing}");
let directory = read_audio(dir.path()).await.unwrap_err();
assert!(directory.contains("not a regular file"), "{directory}");
let big = dir.path().join("big.ogg");
let file = std::fs::File::create(&big).unwrap();
file.set_len(MAX_BYTES + 1).unwrap();
let too_big = read_audio(&big).await.unwrap_err();
assert!(too_big.contains("over the"), "{too_big}");
}
}