use crate::errors::AppError;
use std::io::{IsTerminal, Read};
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
pub fn read_stdin_with_timeout(secs: u64) -> Result<String, AppError> {
if std::io::stdin().is_terminal() {
return Err(AppError::Internal(anyhow::anyhow!(
"stdin is attached to a terminal; pipe data via stdin \
(e.g. `echo ... | sqlite-graphrag ...` or `... < file`) \
or use --body instead of --body-stdin"
)));
}
let (tx, rx) = mpsc::channel::<std::io::Result<String>>();
thread::spawn(move || {
let mut buf = String::new();
let result = std::io::stdin().read_to_string(&mut buf).map(|_| buf);
let _ = tx.send(result);
});
match rx.recv_timeout(Duration::from_secs(secs)) {
Ok(Ok(buf)) => Ok(buf),
Ok(Err(e)) => Err(AppError::Io(e)),
Err(mpsc::RecvTimeoutError::Timeout) => Err(AppError::Internal(anyhow::anyhow!(
"stdin read timed out after {secs}s; pipe must close within timeout window"
))),
Err(mpsc::RecvTimeoutError::Disconnected) => Err(AppError::Internal(anyhow::anyhow!(
"stdin reader thread disconnected unexpectedly"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Instant;
#[test]
fn read_stdin_with_timeout_returns_internal_error_on_timeout() {
let start = Instant::now();
let result = read_stdin_with_timeout(1);
let elapsed = start.elapsed();
match result {
Err(AppError::Internal(e)) => {
let msg = e.to_string();
assert!(
msg.contains("timed out") || msg.contains("terminal"),
"unexpected internal error: {msg}"
);
assert!(elapsed.as_secs_f64() < 2.5);
}
Ok(_) | Err(AppError::Io(_)) => {
}
Err(other) => panic!("unexpected error variant: {other:?}"),
}
}
}