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)
| 223 | |
| 224 | |
| 225 | def 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 |
no test coverage detected