|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Interactive runner for AE Agent - runs inside container after main task. |
| 3 | +
|
| 4 | +Used when interactive=True: docker exec -it <container_id> python3 /agent/interactive_runner.py <model> |
| 5 | +Artifact at /repo; API keys from container env. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | +sys.path.insert(0, '/agent') |
| 13 | + |
| 14 | +try: |
| 15 | + from utils import DEFAULT_TIMEOUT_MS |
| 16 | +except ImportError: |
| 17 | + DEFAULT_TIMEOUT_MS = 172_800_000 |
| 18 | + |
| 19 | +try: |
| 20 | + from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient |
| 21 | +except ImportError as e: |
| 22 | + print(f"ERROR: claude_agent_sdk not available: {e}", file=sys.stderr) |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + |
| 26 | +def _build_system_prompt() -> str: |
| 27 | + try: |
| 28 | + timeout_ms_env = os.environ.get("BASH_MAX_TIMEOUT_MS") |
| 29 | + timeout_ms = int(timeout_ms_env) if timeout_ms_env else DEFAULT_TIMEOUT_MS |
| 30 | + except ValueError: |
| 31 | + timeout_ms = DEFAULT_TIMEOUT_MS |
| 32 | + |
| 33 | + return """You are an experienced software engineer in an interactive session. |
| 34 | +
|
| 35 | +ENVIRONMENT: |
| 36 | +- You are inside a Docker container with root permissions. |
| 37 | +- The artifact repository is at /repo. Change to it: cd /repo |
| 38 | +- You have access to Read, Write, and Bash tools. |
| 39 | +
|
| 40 | +TIMEOUT: Long-running commands can take hours; do not set short timeouts. |
| 41 | +
|
| 42 | +You will receive follow-up instructions from the user. Complete each one and respond. |
| 43 | +If the user asks to stop or says 'quit'/'exit', acknowledge and they will end the session.""" |
| 44 | + |
| 45 | + |
| 46 | +def _display_message(msg) -> None: |
| 47 | + if hasattr(msg, 'content'): |
| 48 | + for block in msg.content: |
| 49 | + if hasattr(block, 'text'): |
| 50 | + print(block.text, end='', flush=True) |
| 51 | + print(flush=True) |
| 52 | + |
| 53 | + |
| 54 | +async def _interactive_loop(model_name: str) -> int: |
| 55 | + options = ClaudeAgentOptions( |
| 56 | + system_prompt=_build_system_prompt(), |
| 57 | + allowed_tools=["Read", "Write", "Bash"], |
| 58 | + setting_sources=["user"], |
| 59 | + ) |
| 60 | + |
| 61 | + print("\n" + "=" * 60, flush=True) |
| 62 | + print("Interactive mode - Agent ready. Type your instructions (or 'quit'/'exit' to end).", flush=True) |
| 63 | + print("=" * 60 + "\n", flush=True) |
| 64 | + |
| 65 | + async with ClaudeSDKClient(options=options) as client: |
| 66 | + await client.query( |
| 67 | + "Please confirm you are in /repo and ready for the user's follow-up instructions. Reply briefly that you are ready." |
| 68 | + ) |
| 69 | + async for msg in client.receive_response(): |
| 70 | + _display_message(msg) |
| 71 | + |
| 72 | + while True: |
| 73 | + try: |
| 74 | + user_input = input("\n>>> ").strip() |
| 75 | + except (EOFError, KeyboardInterrupt): |
| 76 | + print("\nExiting interactive mode.", flush=True) |
| 77 | + return 0 |
| 78 | + |
| 79 | + if not user_input: |
| 80 | + continue |
| 81 | + if user_input.lower() in ('quit', 'exit', 'q'): |
| 82 | + print("Exiting interactive mode.", flush=True) |
| 83 | + return 0 |
| 84 | + |
| 85 | + await client.query(user_input) |
| 86 | + async for msg in client.receive_response(): |
| 87 | + _display_message(msg) |
| 88 | + |
| 89 | + return 0 |
| 90 | + |
| 91 | + |
| 92 | +def main() -> int: |
| 93 | + model_name = os.environ.get("AE_AGENT_MODEL", "claude-sonnet-4-5-20250929") |
| 94 | + if len(sys.argv) >= 2: |
| 95 | + model_name = sys.argv[1] |
| 96 | + |
| 97 | + if not os.environ.get('ANTHROPIC_API_KEY') and not os.environ.get('ANTHROPIC_FOUNDRY_API_KEY'): |
| 98 | + print("ERROR: ANTHROPIC_API_KEY or ANTHROPIC_FOUNDRY_API_KEY must be set.", file=sys.stderr) |
| 99 | + return 1 |
| 100 | + |
| 101 | + return asyncio.run(_interactive_loop(model_name)) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + sys.exit(main()) |
0 commit comments