MCPcopy Create free account
hub / github.com/Ishabdullah/Codey-v2 / run_prompted

Function run_prompted

core/peer_shell.py:225–283  ·  view source on GitHub ↗

Run a peer CLI in non-interactive mode by passing the prompt as a flag. e.g. claude -p "write a function that reverses a string" gemini --model gemini-2.0-flash -p "explain this" Streams output live to the terminal and captures it for Codey. No TUI, no trust dialogs, no

(cli_name: str, cmd: str, flag: str, prompt_text: str)

Source from the content-addressed store, hash-verified

223
224
225def run_prompted(cli_name: str, cmd: str, flag: str, prompt_text: str) -> str:
226 """
227 Run a peer CLI in non-interactive mode by passing the prompt as a flag.
228 e.g. claude -p "write a function that reverses a string"
229 gemini --model gemini-2.0-flash -p "explain this"
230
231 Streams output live to the terminal and captures it for Codey.
232 No TUI, no trust dialogs, no pexpect needed.
233 Returns "[PEER_ERROR: ...]" if the CLI fails so callers can handle it.
234 """
235 import shlex
236 import subprocess
237 width = _terminal_width()
238 border = "─" * width
239
240 print(f"\n{_CYAN}{_header(cli_name.upper() + ' CLI (direct)', width)}{_RESET}")
241 info(f"Asking {cli_name}: {prompt_text[:100]}{'…' if len(prompt_text) > 100 else ''}")
242 print(f"{_DIM}{border}{_RESET}\n")
243
244 captured = []
245 returncode = 0
246 try:
247 # shlex.split handles "gemini --model x" → ["gemini", "--model", "x"]
248 argv = shlex.split(cmd) + [flag, prompt_text]
249 proc = subprocess.Popen(
250 argv,
251 stdout=subprocess.PIPE,
252 stderr=subprocess.STDOUT,
253 text=True,
254 bufsize=1,
255 )
256 for line in proc.stdout:
257 sys.stdout.write(line)
258 sys.stdout.flush()
259 captured.append(line)
260 proc.wait()
261 returncode = proc.returncode
262 except FileNotFoundError:
263 print(f"{_RED}[peer_shell] Command not found: {cmd}{_RESET}")
264 returncode = 127
265 except Exception as e:
266 print(f"{_RED}[peer_shell] Error: {e}{_RESET}")
267 returncode = 1
268
269 print(f"\n{_DIM}{border}{_RESET}")
270 print(f"{_CYAN}{_header(cli_name.upper() + ' DONE', width)}{_RESET}\n")
271 info(f"Back in Codey — reading {cli_name} output…")
272
273 output = "".join(captured)
274 # Strip startup noise before error detection so noise lines don't
275 # interfere, then return the clean output to the agent.
276 if cli_name == "gemini":
277 output = _strip_gemini_noise(output)
278 reason = _detect_peer_error(output, returncode)
279 if reason:
280 msg = f"[PEER_ERROR: {cli_name} failed — {reason}]"
281 warning(f"Peer {cli_name} failed: {reason}")
282 return msg

Callers 1

callMethod · 0.90

Calls 9

infoFunction · 0.90
warningFunction · 0.90
_terminal_widthFunction · 0.85
_headerFunction · 0.85
_strip_gemini_noiseFunction · 0.85
_detect_peer_errorFunction · 0.85
flushMethod · 0.80
appendMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected