Skip to content

Commit 4ee4bc4

Browse files
authored
fix: piped input in non-interactive mode (#363)
1 parent b9100eb commit 4ee4bc4

File tree

1 file changed

+26
-4
lines changed
  • crates/chat-cli/src/cli/chat

1 file changed

+26
-4
lines changed

crates/chat-cli/src/cli/chat/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ use std::collections::{
2424
HashSet,
2525
VecDeque,
2626
};
27-
use std::io::Write;
27+
use std::io::{
28+
IsTerminal,
29+
Read,
30+
Write,
31+
};
2832
use std::process::ExitCode;
2933
use std::time::Duration;
3034

@@ -181,8 +185,26 @@ pub struct ChatArgs {
181185

182186
impl ChatArgs {
183187
pub async fn execute(self, os: &mut Os) -> Result<ExitCode> {
184-
if self.non_interactive && self.input.is_none() {
185-
bail!("Input must be supplied when running in non-interactive mode");
188+
let mut input = self.input;
189+
190+
if self.non_interactive && input.is_none() {
191+
if !std::io::stdin().is_terminal() {
192+
let mut buffer = String::new();
193+
match std::io::stdin().read_to_string(&mut buffer) {
194+
Ok(_) => {
195+
if !buffer.trim().is_empty() {
196+
input = Some(buffer.trim().to_string());
197+
}
198+
},
199+
Err(e) => {
200+
eprintln!("Error reading from stdin: {}", e);
201+
},
202+
}
203+
}
204+
205+
if input.is_none() {
206+
bail!("Input must be supplied when running in non-interactive mode");
207+
}
186208
}
187209

188210
let stdout = std::io::stdout();
@@ -289,7 +311,7 @@ impl ChatArgs {
289311
stdout,
290312
stderr,
291313
&conversation_id,
292-
self.input,
314+
input,
293315
InputSource::new(os, prompt_request_sender, prompt_response_receiver)?,
294316
self.resume,
295317
|| terminal::window_size().map(|s| s.columns.into()).ok(),

0 commit comments

Comments
 (0)