|
| 1 | +""" |
| 2 | +OperatorValidatorCallback |
| 3 | +
|
| 4 | +Ensures agent output actions conform to expected schemas by fixing common issues: |
| 5 | +- click: add default button='left' if missing |
| 6 | +- keypress: wrap keys string into a list |
| 7 | +- etc. |
| 8 | +
|
| 9 | +This runs in on_llm_end, which receives the output array (AgentMessage[] as dicts). |
| 10 | +The purpose is to avoid spending another LLM call to fix broken computer call syntax when possible. |
| 11 | +""" |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from typing import Any, Dict, List |
| 15 | + |
| 16 | +from .base import AsyncCallbackHandler |
| 17 | + |
| 18 | + |
| 19 | +class OperatorNormalizerCallback(AsyncCallbackHandler): |
| 20 | + """Normalizes common computer call hallucinations / errors in computer call syntax.""" |
| 21 | + |
| 22 | + async def on_llm_end(self, output: List[Dict[str, Any]]) -> List[Dict[str, Any]]: |
| 23 | + # Mutate in-place as requested, but still return the list for chaining |
| 24 | + for item in output or []: |
| 25 | + if item.get("type") != "computer_call": |
| 26 | + continue |
| 27 | + action = item.get("action") |
| 28 | + if not isinstance(action, dict): |
| 29 | + continue |
| 30 | + |
| 31 | + # rename mouse click actions to "click" |
| 32 | + for mouse_btn in ["left", "right", "wheel", "back", "forward"]: |
| 33 | + if action.get("type", "") == f"{mouse_btn}_click": |
| 34 | + action["type"] = "click" |
| 35 | + action["button"] = mouse_btn |
| 36 | + # rename hotkey actions to "keypress" |
| 37 | + for alias in ["hotkey", "key", "press", "key_press"]: |
| 38 | + if action.get("type", "") == alias: |
| 39 | + action["type"] = "keypress" |
| 40 | + # assume click actions |
| 41 | + if "button" in action and "type" not in action: |
| 42 | + action["type"] = "click" |
| 43 | + if "click" in action and "type" not in action: |
| 44 | + action["type"] = "click" |
| 45 | + if ("scroll_x" in action or "scroll_y" in action) and "type" not in action: |
| 46 | + action["type"] = "scroll" |
| 47 | + if "text" in action and "type" not in action: |
| 48 | + action["type"] = "type" |
| 49 | + |
| 50 | + action_type = action.get("type") |
| 51 | + def _keep_keys(action: Dict[str, Any], keys_to_keep: List[str]): |
| 52 | + """Keep only the provided keys on action; delete everything else. |
| 53 | + Always ensures required 'type' is present if listed in keys_to_keep. |
| 54 | + """ |
| 55 | + for key in list(action.keys()): |
| 56 | + if key not in keys_to_keep: |
| 57 | + del action[key] |
| 58 | + # rename "coordinate" to "x", "y" |
| 59 | + if "coordinate" in action: |
| 60 | + action["x"] = action["coordinate"][0] |
| 61 | + action["y"] = action["coordinate"][1] |
| 62 | + del action["coordinate"] |
| 63 | + if action_type == "click": |
| 64 | + # convert "click" to "button" |
| 65 | + if "button" not in action and "click" in action: |
| 66 | + action["button"] = action["click"] |
| 67 | + del action["click"] |
| 68 | + # default button to "left" |
| 69 | + action["button"] = action.get("button", "left") |
| 70 | + # add default scroll x, y if missing |
| 71 | + if action_type == "scroll": |
| 72 | + action["scroll_x"] = action.get("scroll_x", 0) |
| 73 | + action["scroll_y"] = action.get("scroll_y", 0) |
| 74 | + # ensure keys arg is a list (normalize aliases first) |
| 75 | + if action_type == "keypress": |
| 76 | + keys = action.get("keys") |
| 77 | + for keys_alias in ["keypress", "key", "press", "key_press", "text"]: |
| 78 | + if keys_alias in action: |
| 79 | + action["keys"] = action[keys_alias] |
| 80 | + del action[keys_alias] |
| 81 | + keys = action.get("keys") |
| 82 | + if isinstance(keys, str): |
| 83 | + action["keys"] = keys.replace("-", "+").split("+") if len(keys) > 1 else [keys] |
| 84 | + required_keys_by_type = { |
| 85 | + # OpenAI actions |
| 86 | + "click": ["type", "button", "x", "y"], |
| 87 | + "double_click": ["type", "x", "y"], |
| 88 | + "drag": ["type", "path"], |
| 89 | + "keypress": ["type", "keys"], |
| 90 | + "move": ["type", "x", "y"], |
| 91 | + "screenshot": ["type"], |
| 92 | + "scroll": ["type", "scroll_x", "scroll_y", "x", "y"], |
| 93 | + "type": ["type", "text"], |
| 94 | + "wait": ["type"], |
| 95 | + # Anthropic actions |
| 96 | + "left_mouse_down": ["type", "x", "y"], |
| 97 | + "left_mouse_up": ["type", "x", "y"], |
| 98 | + "triple_click": ["type", "button", "x", "y"], |
| 99 | + } |
| 100 | + keep = required_keys_by_type.get(action_type or "") |
| 101 | + if keep: |
| 102 | + _keep_keys(action, keep) |
| 103 | + |
| 104 | + |
| 105 | + # Second pass: if an assistant message is immediately followed by a computer_call, |
| 106 | + # replace the assistant message itself with a reasoning message with summary text. |
| 107 | + if isinstance(output, list): |
| 108 | + for i, item in enumerate(output): |
| 109 | + # AssistantMessage shape: { type: 'message', role: 'assistant', content: OutputContent[] } |
| 110 | + if item.get("type") == "message" and item.get("role") == "assistant": |
| 111 | + next_idx = i + 1 |
| 112 | + if next_idx >= len(output): |
| 113 | + continue |
| 114 | + next_item = output[next_idx] |
| 115 | + if not isinstance(next_item, dict): |
| 116 | + continue |
| 117 | + if next_item.get("type") != "computer_call": |
| 118 | + continue |
| 119 | + contents = item.get("content") or [] |
| 120 | + # Extract text from OutputContent[] |
| 121 | + text_parts: List[str] = [] |
| 122 | + if isinstance(contents, list): |
| 123 | + for c in contents: |
| 124 | + if isinstance(c, dict) and c.get("type") == "output_text" and isinstance(c.get("text"), str): |
| 125 | + text_parts.append(c["text"]) |
| 126 | + text_content = "\n".join(text_parts).strip() |
| 127 | + # Replace assistant message with reasoning message |
| 128 | + output[i] = { |
| 129 | + "type": "reasoning", |
| 130 | + "summary": [ |
| 131 | + { |
| 132 | + "type": "summary_text", |
| 133 | + "text": text_content, |
| 134 | + } |
| 135 | + ], |
| 136 | + } |
| 137 | + |
| 138 | + return output |
0 commit comments