|
| 1 | +from computers import Computer |
| 2 | +from utils import ( |
| 3 | + create_response, |
| 4 | + show_image, |
| 5 | + pp, |
| 6 | + sanitize_message, |
| 7 | + check_blocklisted_url, |
| 8 | +) |
| 9 | +import json |
| 10 | +from typing import Callable |
| 11 | + |
| 12 | + |
| 13 | +class Agent: |
| 14 | + """ |
| 15 | + A sample agent class that can be used to interact with a computer. |
| 16 | +
|
| 17 | + (See simple_cua_loop.py for a simple example without an agent.) |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + model="computer-use-preview", |
| 23 | + computer: Computer = None, |
| 24 | + tools: list[dict] = [], |
| 25 | + acknowledge_safety_check_callback: Callable = lambda message: False, |
| 26 | + ): |
| 27 | + self.model = model |
| 28 | + self.computer = computer |
| 29 | + self.tools = tools |
| 30 | + self.print_steps = True |
| 31 | + self.debug = False |
| 32 | + self.show_images = False |
| 33 | + self.acknowledge_safety_check_callback = acknowledge_safety_check_callback |
| 34 | + |
| 35 | + if computer: |
| 36 | + dimensions = computer.get_dimensions() |
| 37 | + self.tools += [ |
| 38 | + { |
| 39 | + "type": "computer-preview", |
| 40 | + "display_width": dimensions[0], |
| 41 | + "display_height": dimensions[1], |
| 42 | + "environment": computer.get_environment(), |
| 43 | + }, |
| 44 | + { |
| 45 | + "type": "function", |
| 46 | + "name": "back", |
| 47 | + "description": "Go back to the previous page.", |
| 48 | + "parameters": {}, |
| 49 | + }, |
| 50 | + { |
| 51 | + "type": "function", |
| 52 | + "name": "goto", |
| 53 | + "description": "Go to a specific URL.", |
| 54 | + "parameters": { |
| 55 | + "type": "object", |
| 56 | + "properties": { |
| 57 | + "url": { |
| 58 | + "type": "string", |
| 59 | + "description": "Fully qualified URL to navigate to.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + "additionalProperties": False, |
| 63 | + "required": ["url"], |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + "type": "function", |
| 68 | + "name": "forward", |
| 69 | + "description": "Go forward to the next page.", |
| 70 | + "parameters": {}, |
| 71 | + }, |
| 72 | + ] |
| 73 | + |
| 74 | + def debug_print(self, *args): |
| 75 | + if self.debug: |
| 76 | + pp(*args) |
| 77 | + |
| 78 | + def handle_item(self, item): |
| 79 | + """Handle each item; may cause a computer action + screenshot.""" |
| 80 | + if item["type"] == "message": |
| 81 | + if self.print_steps: |
| 82 | + print(item["content"][0]["text"]) |
| 83 | + |
| 84 | + if item["type"] == "function_call": |
| 85 | + name, args = item["name"], json.loads(item["arguments"]) |
| 86 | + if self.print_steps: |
| 87 | + print(f"{name}({args})") |
| 88 | + |
| 89 | + if hasattr(self.computer, name): # if function exists on computer, call it |
| 90 | + method = getattr(self.computer, name) |
| 91 | + method(**args) |
| 92 | + return [ |
| 93 | + { |
| 94 | + "type": "function_call_output", |
| 95 | + "call_id": item["call_id"], |
| 96 | + "output": "success", # hard-coded output for demo |
| 97 | + } |
| 98 | + ] |
| 99 | + |
| 100 | + if item["type"] == "computer_call": |
| 101 | + action = item["action"] |
| 102 | + action_type = action["type"] |
| 103 | + action_args = {k: v for k, v in action.items() if k != "type"} |
| 104 | + if self.print_steps: |
| 105 | + print(f"{action_type}({action_args})") |
| 106 | + |
| 107 | + method = getattr(self.computer, action_type) |
| 108 | + method(**action_args) |
| 109 | + |
| 110 | + screenshot_base64 = self.computer.screenshot() |
| 111 | + if self.show_images: |
| 112 | + show_image(screenshot_base64) |
| 113 | + |
| 114 | + # if user doesn't ack all safety checks exit with error |
| 115 | + pending_checks = item.get("pending_safety_checks", []) |
| 116 | + for check in pending_checks: |
| 117 | + message = check["message"] |
| 118 | + if not self.acknowledge_safety_check_callback(message): |
| 119 | + raise ValueError( |
| 120 | + f"Safety check failed: {message}. Cannot continue with unacknowledged safety checks." |
| 121 | + ) |
| 122 | + |
| 123 | + call_output = { |
| 124 | + "type": "computer_call_output", |
| 125 | + "call_id": item["call_id"], |
| 126 | + "acknowledged_safety_checks": pending_checks, |
| 127 | + "output": { |
| 128 | + "type": "input_image", |
| 129 | + "image_url": f"data:image/png;base64,{screenshot_base64}", |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + # additional URL safety checks for browser environments |
| 134 | + if self.computer.get_environment() == "browser": |
| 135 | + current_url = self.computer.get_current_url() |
| 136 | + check_blocklisted_url(current_url) |
| 137 | + call_output["output"]["current_url"] = current_url |
| 138 | + |
| 139 | + return [call_output] |
| 140 | + return [] |
| 141 | + |
| 142 | + def run_full_turn( |
| 143 | + self, input_items, print_steps=True, debug=False, show_images=False |
| 144 | + ): |
| 145 | + self.print_steps = print_steps |
| 146 | + self.debug = debug |
| 147 | + self.show_images = show_images |
| 148 | + new_items = [] |
| 149 | + |
| 150 | + # keep looping until we get a final response |
| 151 | + while new_items[-1].get("role") != "assistant" if new_items else True: |
| 152 | + self.debug_print([sanitize_message(msg) for msg in input_items + new_items]) |
| 153 | + |
| 154 | + response = create_response( |
| 155 | + model=self.model, |
| 156 | + input=input_items + new_items, |
| 157 | + tools=self.tools, |
| 158 | + truncation="auto", |
| 159 | + ) |
| 160 | + self.debug_print(response) |
| 161 | + |
| 162 | + if "output" not in response and self.debug: |
| 163 | + print(response) |
| 164 | + raise ValueError("No output from model") |
| 165 | + else: |
| 166 | + new_items += response["output"] |
| 167 | + for item in response["output"]: |
| 168 | + new_items += self.handle_item(item) |
| 169 | + |
| 170 | + return new_items |
0 commit comments