Skip to content

improve cua key mapping logic #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

tkattkat
Copy link
Collaborator

@tkattkat tkattkat commented Jul 23, 2025

why

Currently we have a handful of keys that fail to be used, and redundant logic around key handling. It is now consolidated to one location, and properly handles the failing keys
part of STG-587

what changed

consolidated key handling for CUA agents to one util

test plan

tested locally

Copy link

changeset-bot bot commented Jul 23, 2025

🦋 Changeset detected

Latest commit: 3319506

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR consolidates key mapping logic for Computer Use Agent (CUA) interactions by extracting duplicate key handling code into a centralized utility function. The changes include:

  1. New centralized utility: Creates lib/agent/utils/cuaKeyMapping.ts with a mapKeyToPlaywright() function that provides comprehensive key mapping from various formats (Anthropic CUA, OpenAI CUA) to Playwright-compatible key names

  2. Code deduplication: Removes ~40-45 lines of inline key mapping logic from both agentHandler.ts and AnthropicCUAClient.ts, replacing hardcoded if-else chains with calls to the centralized utility

  3. File reorganization: Moves imageCompressionUtils.ts to lib/agent/utils/imageCompression.ts to group utilities together in a dedicated utils directory

  4. Enhanced key support: The new utility provides more comprehensive key mapping including cross-platform variations (e.g., macOS 'COMMAND' vs Windows 'CTRL', 'OPTION' -> 'Alt') and multiple naming conventions for the same keys

The consolidation improves maintainability by centralizing key mapping logic that was previously scattered across multiple files with slightly different implementations. This ensures consistent key handling across all CUA providers and makes it easier to add support for new key mappings in the future. The change fits into the broader Stagehand architecture by creating reusable utilities that can be shared between different agent client implementations (Anthropic, OpenAI, etc.).

Confidence score: 5/5

• This PR is very safe to merge - it's a straightforward refactoring that consolidates duplicate code without changing functionality
• The changes are well-structured, maintain backward compatibility, and improve code organization through proper utility extraction
• All files look correct with proper imports and the key mapping logic appears comprehensive and well-tested

4 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@tkattkat tkattkat added the parity To note required feature parity in client SDKs label Jul 23, 2025
@stagehand-parity-bot
Copy link

🔄 Feature Parity Issue Created

An issue has been automatically created in the Python SDK repository to track parity implementation:
browserbase/stagehand-python#164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parity To note required feature parity in client SDKs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant