Open a peer CLI inside Codey's terminal, auto-type the prompt, capture output. Args: cli_name: Short display name (e.g. "copilot") cmd: Shell command (e.g. "copilot") prompt_text: Task to type automatically once the CLI is ready. Returns:
(cli_name: str, cmd: str, prompt_text: str = "")
| 84 | |
| 85 | |
| 86 | def run_peer(cli_name: str, cmd: str, prompt_text: str = "") -> str: |
| 87 | """ |
| 88 | Open a peer CLI inside Codey's terminal, auto-type the prompt, capture output. |
| 89 | |
| 90 | Args: |
| 91 | cli_name: Short display name (e.g. "copilot") |
| 92 | cmd: Shell command (e.g. "copilot") |
| 93 | prompt_text: Task to type automatically once the CLI is ready. |
| 94 | |
| 95 | Returns: |
| 96 | Full captured output string (everything that appeared in the peer session). |
| 97 | """ |
| 98 | if not _check_pexpect(): |
| 99 | warning("pexpect not installed — run: pip install pexpect") |
| 100 | warning("Falling back to basic shell (no auto-typing).") |
| 101 | return _run_basic_fallback(cli_name, cmd, prompt_text) |
| 102 | |
| 103 | import pexpect |
| 104 | |
| 105 | width = _terminal_width() |
| 106 | border = "─" * width |
| 107 | capture = _LiveCapture() |
| 108 | |
| 109 | # ── Draw opening border ──────────────────────────────────────────────── |
| 110 | print(f"\n{_CYAN}{_header(cli_name.upper() + ' CLI', width)}{_RESET}") |
| 111 | info(f"Opening {cli_name} — task will be typed automatically.") |
| 112 | if prompt_text: |
| 113 | print(f"{_DIM}Task: {prompt_text[:120]}{'…' if len(prompt_text) > 120 else ''}{_RESET}") |
| 114 | print(f"{_DIM}Ctrl+B → take over input | close {cli_name} normally when done{_RESET}") |
| 115 | print(f"{_DIM}{border}{_RESET}\n") |
| 116 | |
| 117 | try: |
| 118 | child = pexpect.spawn( |
| 119 | cmd, |
| 120 | encoding="utf-8", |
| 121 | timeout=300, |
| 122 | dimensions=(40, min(width, 220)), |
| 123 | ) |
| 124 | child.logfile_read = capture # everything child prints → stdout + buffer |
| 125 | |
| 126 | # ── Wait for CLI to initialise ──────────────────────────────────── |
| 127 | _wait_for_ready(child, cli_name, pexpect) |
| 128 | |
| 129 | # ── Auto-type the prompt ────────────────────────────────────────── |
| 130 | if prompt_text: |
| 131 | time.sleep(0.2) |
| 132 | child.sendline(prompt_text) |
| 133 | |
| 134 | # ── Hand control to user (Ctrl+B = escape back to Codey) ───────── |
| 135 | # interact() returns when: |
| 136 | # a) user presses the escape character (Ctrl+B → \x02), OR |
| 137 | # b) the child process exits (EOF) |
| 138 | child.interact(escape_character="\x02") |
| 139 | |
| 140 | # ── Drain any last output, then close ──────────────────────────── |
| 141 | try: |
| 142 | child.expect(pexpect.EOF, timeout=8) |
| 143 | except (pexpect.EOF, pexpect.TIMEOUT): |
no test coverage detected