CUA scoped surfaces and separated input capabilities
CUA 作用域表面与分离的输入能力
Application and window objects share accessibility operations, but only an explicit window can capture or deliver pointer input. Duplicating shared methods on both factories creates two implementations to keep aligned. Moving application-only capability checks into the shared base makes window behavior depend on an application-level decision. Keyboard input has the same problem at a smaller scale: a chord string comb
English
Problem
Application and window objects share accessibility operations, but only an explicit window can capture or deliver pointer input. Duplicating shared methods on both factories creates two implementations to keep aligned. Moving application-only capability checks into the shared base makes window behavior depend on an application-level decision. Keyboard input has the same problem at a smaller scale: a chord string combines a key and its modifiers, and a complete-keystroke method cannot express a modifier held across calls or a standalone key-up.
Decision
CuaSurface declares operations shared by application and window objects; CuaApp and CuaWindow extend it with their own operations. One base factory builds the surface, and the derived factories inject scope parameters plus requireAccessibility and requireKeyboard checks. The shared base never branches on a capability that exists only for applications.
Provider startup capabilities are static. Platform, supported methods, application-level accessibility, keyboard scopes, browser support, and coordinate space are fixed when the provider is installed. The native getCapabilities RPC reports runtime permissions and session lock only; installCua merges the two layers for the model-visible result. The neutral RPC is a common operation representation, not a capability inventory; provider guidance names the actions its platform supports.
Keep the types, guidance, and documentation in packages/experimental/cua limited to behavior callers can rely on across providers. Each platform package owns its supported operations, limits, and output formats. For example, showDiff requests changes through the shared API. Each provider documents what its output means, so changing one provider’s format requires no change to the shared API or its prompt.
Keyboard input keeps keys and modifiers separate: pressKey(key, modifiers?) presses and releases one key while holding the listed modifiers, and its keyboard modifier list is a plain string array defined by the provider. sendKeyEvent(key, direction) sends exactly one key-down or key-up without an implicit counterpart. A caller can therefore hold a modifier or split a gesture; neither method packs a chord string, and the shared schema does not enumerate platform key names.
Within one API instance, the object facade keeps one current accessibility snapshot per application handle, shared by every application and window object for that handle, including repeated selections of the same application. getApp and getWindow select objects without reading accessibility or replacing that snapshot; native window operations reject closed or foreign windows. An explicit getAccessibilityTree read invalidates the previous snapshot before dispatch. The facade automatically attaches the latest snapshot ID to element operations; callers use element IDs from the latest tree and do not assume an older ID still identifies the same control. Element IDs are opaque provider strings. Only an explicit CuaWindow captures screenshots or sends pointer input. The native helper retains its own application and optional-window snapshots behind that facade; the native layer and the facade snapshot remain separate. The external runtime continues to own variable persistence and completion serialization; the facade adds no evaluator. The native object decision owns evaluator independence, and the shared helper decision owns connection lifetime.
Alternatives considered
Duplicate application and window methods would give each factory its own implementation of the same accessibility operations. Every shared change would then need matching edits, and the two surfaces could accept different arguments or results. One CuaSurface implementation keeps that behavior single.
An application-specific branch in the shared base would let window objects inherit decisions and capability checks that apply only to applications. Scope factories that inject their own requirements keep the shared operations independent of which scope uses them.
A packed chord string requires parsing key names, separators, and plus escaping together, and it pushes platform alias normalization into the shared layer. Keystroke and direction are separate semantics: pressKey describes one complete keystroke, while sendKeyEvent describes one explicit direction. Separate key and modifier values keep those semantics without a parser.
Platform keyboard key enumerations in the shared schema would couple the portable operation schema to one provider's key names and require a schema change for each platform. Provider-defined strings let the provider interpret its own names while the shared schema stays neutral.
Static capabilities reported by native would duplicate provider configuration in the native helper and make the helper responsible for knowledge that is fixed before it starts. The helper reports runtime state; the provider owns its startup capabilities.
Consequences
Element IDs belong to the application's current accessibility snapshot. Reading one window replaces the facade’s current snapshot for that application, so callers observe again and confirm the IDs they use. The new read does not guarantee that an old string is rejected or that an ID changes; callers must not assume an older ID still identifies the same control. Pointer input and screenshots require an explicit window object; callers enumerate windows first.
Keyboard behavior follows the provider's supported scopes. A caller that uses sendKeyEvent controls its down/up pairing. Each Agent owns an input connection, and the helper tracks holds by that connection. Successful DSH and MCP programs report remaining held input without releasing it. A client pause between completed MCP calls does not notify this server and can leave input held; native ownership still permits explicit release and connection cleanup. Explicit up, program failure, user cancellation and resource disposal release owned input. macOS and Windows both implement this lifecycle. Custom helpers must support the matching connection-scoped query and release operations. Keyboard modifier names are provider-defined, so the same string may have different support across providers.
Provider guidance owns the list of supported actions. Adding a method to the shared schema does not claim that every provider implements it, and the merged getCapabilities result remains the runtime source for permissions and session lock.
中文
问题
应用对象和窗口对象共享辅助功能操作,但只有显式窗口能截图或发送指针输入。若在两个工厂上分别复制共享方法,就会出现两套需要同步的实现。把只适用于应用的能力检查放进共享基类,会让窗口行为依赖应用级判断。键盘输入也有同样的问题:组合键字符串把按键与修饰键混在一起,完整击键方法无法表达跨调用保持修饰键或单独抬起按键。
决策
CuaSurface 声明应用对象与窗口对象共享的操作;CuaApp 与 CuaWindow 通过扩展它增加各自的操作。一个基类工厂负责构建表面,派生工厂注入作用域参数以及 requireAccessibility、requireKeyboard 检查。共享基类从不为只属于应用的能力分支。
Provider 的启动能力是静态的。平台、支持的方法、应用级辅助功能、键盘作用域、浏览器支持和坐标空间在安装 provider 时固定。原生 getCapabilities RPC 只报告运行时权限和会话锁定状态;installCua 为模型可见结果合并两层。中立的 RPC 是通用操作表示,不是能力清单;provider 指导文本说明其平台支持的动作。
共享包 packages/experimental/cua 的类型、模型提示词和文档只描述调用方在各平台都能依赖的行为。每个平台支持哪些操作、有哪些限制、返回什么格式,由对应的平台包说明。例如,共享 API 的 showDiff 表示请求变化,输出如何解读由提供方说明。一个平台调整输出格式时,共享 API 和提示词无需跟着改。
键盘输入把按键与修饰键分开:pressKey(key, modifiers?) 在按住所列修饰键时按下并释放一个键,其键盘修饰键列表是 provider 定义的普通字符串数组。sendKeyEvent(key, direction) 只发送一次按下或抬起,不隐式发送相反事件。因此调用方可以保持修饰键或拆分手势;两个方法都不接受组合键字符串,共享 schema 也不枚举平台按键名。
在单个 API 实例内,对象门面按应用句柄保存一份当前辅助功能快照,该句柄的所有应用对象和窗口对象共享这份快照,包括对同一应用的重复选择。getApp 和 getWindow 只选择对象,不读取辅助功能或替换该快照;原生窗口操作会拒绝已关闭或属于其他应用的窗口。显式 getAccessibilityTree 读取会在分派前使上一份快照失效。门面会为元素操作自动附加最新快照 ID;调用方应使用最新树中的元素 ID,不应假定旧 ID 仍指向同一控件。元素 ID 是 provider 定义的不透明字符串。只有显式 CuaWindow 可以截图或发送指针输入。原生 helper 在该门面之后保留自己的应用快照和可选窗口快照;原生层与门面快照相互独立。外部运行时继续负责变量持久化和最终表达式序列化;门面不加入 evaluator。原生对象决定负责 evaluator 独立性,共享 helper 决定负责连接生命期。
考虑过的替代方案
在应用和窗口上重复方法会让每个工厂各自实现同一套辅助功能操作。每次共享改动都需要成对修改,两个表面还可能接受不同的参数或结果。单一 CuaSurface 实现让这份行为只有一处。
在共享基类中加入应用专属分支会让窗口对象继承只适用于应用的决定和能力检查。由各作用域工厂注入自己的要求,可以让共享操作不依赖使用它的作用域。
打包的组合键字符串需要一起解析按键名、分隔符和加号转义,并把平台别名归一化推入共享层。击键与方向是两种语义:pressKey 描述一次完整击键,sendKeyEvent 描述一个显式方向。分开的按键和修饰键值让这些语义无需解析器。
在共享 schema 中枚举平台键盘按键会把可移植的操作 schema 绑定到某个 provider 的按键名,并让每个平台都修改 schema。由 provider 定义字符串,平台可以解释自己的名称,共享 schema 保持中立。
由原生报告静态能力会在原生 helper 中重复 provider 配置,并让 helper 负责它在启动前就固定的知识。helper 报告运行时状态;启动能力由 provider 负责。
影响
元素 ID 属于应用当前的辅助功能快照。读取一个窗口会替换门面对该应用的当前快照,因此调用方需要重新观察并确认要使用的 ID。新的读取不保证拒绝旧字符串,也不保证 ID 会改变;调用方不得假定旧 ID 仍指向同一控件。指针输入和截图都要求显式的窗口对象;调用方先枚举窗口。
键盘行为遵循 provider 支持的作用域。使用 sendKeyEvent 的调用方控制按下与抬起的配对。每个 Agent 使用独立的输入连接,helper 按连接记录持有输入。DSH 和 MCP 程序成功结束时报告剩余持有输入,不自动释放。客户端在两次 MCP 调用之间暂停时不会通知服务端,输入可能继续按住;原生所有权仍支持显式释放和连接清理。显式 up、程序失败、用户取消和资源销毁释放所属输入。macOS 和 Windows 都实现了这套生命周期。自定义 helper 必须支持按连接查询和释放持有输入。键盘修饰键名称由 provider 定义,同一字符串在不同 provider 上可能支持不同。
Provider 指导文本负责支持动作的清单。把方法加入共享 schema 并不表示每个 provider 都已实现它;合并后的 getCapabilities 结果仍是权限和会话锁定状态的运行时来源。