Skip to content

Commit 55f5e3a

Browse files
feat: add piped input support to CLI
Implement piped command functionality similar to fabric project, allowing: - echo "content" | praisonai "command" - cat file.txt | praisonai --auto "task" - echo "question" | praisonai Added read_stdin_if_available() method to detect and read piped input, and modified main() to handle piped content in three scenarios: 1. Command + piped input 2. Direct prompt + piped input 3. Only piped input Fixes #17 Co-authored-by: MervinPraison <MervinPraison@users.noreply.github.com>
1 parent ee869fe commit 55f5e3a

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

src/praisonai/praisonai/cli.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ def run(self):
147147
"""
148148
return self.main()
149149

150+
def read_stdin_if_available(self):
151+
"""
152+
Read from stdin if it's available (when data is piped in).
153+
Returns the stdin content or None if no piped input is available.
154+
"""
155+
try:
156+
# Check if stdin is not a terminal (i.e., has piped input)
157+
if not sys.stdin.isatty():
158+
stdin_content = sys.stdin.read().strip()
159+
return stdin_content if stdin_content else None
160+
except Exception:
161+
# If there's any error reading stdin, ignore it
162+
pass
163+
return None
164+
150165
def main(self):
151166
"""
152167
The main function of the PraisonAI object. It parses the command-line arguments,
@@ -164,21 +179,41 @@ def main(self):
164179

165180
self.framework = args.framework or self.framework
166181

182+
# Check for piped input from stdin
183+
stdin_input = self.read_stdin_if_available()
184+
167185
if args.command:
168186
if args.command.startswith("tests.test") or args.command.startswith("tests/test"): # Argument used for testing purposes
169187
print("test")
170188
return "test"
171189
else:
172-
self.agent_file = args.command
190+
# If stdin input is available, append it to the command
191+
if stdin_input:
192+
combined_prompt = f"{args.command} {stdin_input}"
193+
result = self.handle_direct_prompt(combined_prompt)
194+
print(result)
195+
return result
196+
else:
197+
self.agent_file = args.command
173198
elif hasattr(args, 'direct_prompt') and args.direct_prompt:
174199
# Only handle direct prompt if agent_file wasn't explicitly set in constructor
175200
if original_agent_file == "agents.yaml": # Default value, so safe to use direct prompt
176-
result = self.handle_direct_prompt(args.direct_prompt)
201+
# If stdin input is available, append it to the direct prompt
202+
prompt = args.direct_prompt
203+
if stdin_input:
204+
prompt = f"{args.direct_prompt} {stdin_input}"
205+
result = self.handle_direct_prompt(prompt)
177206
print(result)
178207
return result
179208
else:
180209
# Agent file was explicitly set, ignore direct prompt and use the file
181210
pass
211+
elif stdin_input:
212+
# If only stdin input is provided (no command), use it as direct prompt
213+
if original_agent_file == "agents.yaml": # Default value, so safe to use stdin as prompt
214+
result = self.handle_direct_prompt(stdin_input)
215+
print(result)
216+
return result
182217
# If no command or direct_prompt, preserve agent_file from constructor (don't overwrite)
183218

184219
if args.deploy:

0 commit comments

Comments
 (0)