|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import yaml |
| 6 | +import random |
| 7 | +import time |
| 8 | +import asyncio |
| 9 | +import argparse |
| 10 | +import termios |
| 11 | +import tty |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +# Ensure we can import from src/codebotler |
| 15 | +# Assuming the script is run from the workspace root or src/codebotler/scripts |
| 16 | +# We want to add 'src/codebotler' to path so we can import cli |
| 17 | +# Or better, add 'src' so we can do 'from codebotler import cli' |
| 18 | + |
| 19 | +CURRENT_DIR = Path(__file__).resolve().parent |
| 20 | +# Add src/codebotler to sys.path |
| 21 | +CODEBOTLER_DIR = CURRENT_DIR.parent |
| 22 | +SRC_DIR = CODEBOTLER_DIR.parent |
| 23 | +sys.path.insert(0, str(CODEBOTLER_DIR)) |
| 24 | + |
| 25 | +print(f"DEBUG: CURRENT_DIR={CURRENT_DIR}") |
| 26 | +print(f"DEBUG: CODEBOTLER_DIR={CODEBOTLER_DIR}") |
| 27 | +print(f"DEBUG: sys.path[0]={sys.path[0]}") |
| 28 | +print(f"DEBUG: Files in CODEBOTLER_DIR: {list(CODEBOTLER_DIR.iterdir())}") |
| 29 | + |
| 30 | +try: |
| 31 | + import cli |
| 32 | +except ImportError as e: |
| 33 | + print(f"Error: Could not import 'cli': {e}") |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | +COMMAND_TEMPLATES = { |
| 37 | + "move": "Go to {{location}}.", |
| 38 | + "observe": "If there is an {{object}} in the room, say yes", |
| 39 | + #"say": "Say {{phrase}}" |
| 40 | + #"pick": "Grab the {{object}}.", |
| 41 | + "pick": "If there is an {{object}}, grab it. If not say that the {{object}} does not exist" |
| 42 | + #"place": "Place the object in the bin" |
| 43 | +} |
| 44 | + |
| 45 | +OBJECTS = ["can", "cup", "chair", "bottle", "book", "laptop", "phone", "notebook", "pen", "backpack", "keyboard", "mouse", "table", "lamp"] |
| 46 | +PICK_OBJECTS = ["soda can", "cup", "plastic bottle"] |
| 47 | +PHRASES = [ |
| 48 | + "Hello", |
| 49 | + "How are you?", |
| 50 | + "I am a robot.", |
| 51 | + "I like to help humans.", |
| 52 | + "I am testing Codebotler.", |
| 53 | + "I will be moving around.", |
| 54 | + "I will be looking for objects.", |
| 55 | +] |
| 56 | + |
| 57 | +def load_locations(): |
| 58 | + data_path = SRC_DIR / "codebotler_amrl_impl" / "data.yaml" |
| 59 | + if not data_path.exists(): |
| 60 | + print(f"Error: Could not find data.yaml at {data_path}") |
| 61 | + return [] |
| 62 | + |
| 63 | + with open(data_path, "r") as f: |
| 64 | + data = yaml.safe_load(f) |
| 65 | + |
| 66 | + map_name = data.get("MAP", "ahg2apt") |
| 67 | + locations_dict = data.get("LOCATIONS", {}).get(map_name, {}) |
| 68 | + return list(locations_dict.keys()) |
| 69 | + |
| 70 | +def get_key(): |
| 71 | + fd = sys.stdin.fileno() |
| 72 | + old_settings = termios.tcgetattr(fd) |
| 73 | + try: |
| 74 | + tty.setraw(sys.stdin.fileno()) |
| 75 | + ch = sys.stdin.read(1) |
| 76 | + finally: |
| 77 | + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
| 78 | + return ch |
| 79 | + |
| 80 | +def wait_for_user_input(): |
| 81 | + print("Press Enter or Space to continue, Esc to quit...") |
| 82 | + while True: |
| 83 | + key = get_key() |
| 84 | + if key == '\x1b': # Escape key |
| 85 | + return False |
| 86 | + if key == ' ' or key == '\r' or key == '\n': |
| 87 | + return True |
| 88 | + # Ignore other keys |
| 89 | + |
| 90 | +def main(): |
| 91 | + parser = argparse.ArgumentParser(description="Integration test for Codebotler") |
| 92 | + parser.add_argument("--single-step", action="store_true", help="Enable single-step mode (wait for user input before each command)") |
| 93 | + args = parser.parse_args() |
| 94 | + |
| 95 | + locations = load_locations() |
| 96 | + if not locations: |
| 97 | + print("No locations found. Exiting.") |
| 98 | + return |
| 99 | + |
| 100 | + print(f"Loaded {len(locations)} locations: {locations}") |
| 101 | + |
| 102 | + last_action = None |
| 103 | + |
| 104 | + # Default server config |
| 105 | + host = "10.1.0.13" |
| 106 | + port = 8190 |
| 107 | + uri = f"ws://{host}:{port}" |
| 108 | + |
| 109 | + try: |
| 110 | + while True: |
| 111 | + if args.single_step: |
| 112 | + if not wait_for_user_input(): |
| 113 | + print("\nQuitting...") |
| 114 | + break |
| 115 | + |
| 116 | + command_type = "" |
| 117 | + command_text = "" |
| 118 | + |
| 119 | + if last_action == "pick" and False: |
| 120 | + command_type = "place" |
| 121 | + command_text = COMMAND_TEMPLATES["place"] |
| 122 | + else: |
| 123 | + # Sample action type (move or pick) |
| 124 | + command_type = random.choice(list(COMMAND_TEMPLATES.keys())) |
| 125 | + |
| 126 | + if command_type == "move": |
| 127 | + loc = random.choice(locations) |
| 128 | + command_text = COMMAND_TEMPLATES["move"].replace("{{location}}", loc) |
| 129 | + elif command_type == "say": |
| 130 | + phrase = random.choice(PHRASES) |
| 131 | + command_text = COMMAND_TEMPLATES[command_type].replace("{{phrase}}", phrase) |
| 132 | + elif command_type == "observe": |
| 133 | + obj = random.choice(OBJECTS) |
| 134 | + command_text = COMMAND_TEMPLATES[command_type].replace("{{object}}", obj) |
| 135 | + elif command_type == "pick": |
| 136 | + obj = random.choice(PICK_OBJECTS) |
| 137 | + command_text = COMMAND_TEMPLATES[command_type].replace("{{object}}", obj) |
| 138 | + |
| 139 | + print(f"\n--- Spending command: {command_text} ---") |
| 140 | + |
| 141 | + # Send to codebotler via cli |
| 142 | + # We use the programmatic interface we added to cli.py |
| 143 | + response = asyncio.run(cli.send_request(uri, command_text, execute=True, verbose=True)) |
| 144 | + |
| 145 | + if "error" in response: |
| 146 | + print("Failed to send command. Is the server running?") |
| 147 | + # Wait a bit before retrying to strictly avoid spamming failed connect requests |
| 148 | + time.sleep(5) |
| 149 | + else: |
| 150 | + # Update state |
| 151 | + last_action = command_type |
| 152 | + |
| 153 | + # Wait a bit before next command |
| 154 | + time.sleep(5) |
| 155 | + |
| 156 | + except KeyboardInterrupt: |
| 157 | + print("\nStopping integration test.") |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments