fix(cua): preserve the typed provider request variants

This commit is contained in:
creatixchu committed 2026-11-07 17:09:35 +08:00
1 parent 0356074ec5
commit 0a586fc45f
6 files changed
+16 -11

No files matched your search

@@ -10,8 +10,11 @@ import { mouseControlPrompt } from './mouse-control-locales.ts'
const pointerOptions = { useSystemPointer: z.boolean().optional().describe('Request use of the system pointer. The Host checks permission before dispatch.') }
const mouseMove = z.strictObject({ method: z.literal('mouseMove'), params: z.union(MouseMoveTargets.map(target => target.extend(pointerOptions))) })
const [first, ...rest] = Request.options
const macOperation = (op: typeof Request.options[number]) => op.shape.method.value === 'mouseMove' ? mouseMove : op
/** macOS advertises and validates its additional option for element and coordinate moves. */
export const MacRequest = z.discriminatedUnion('method', Request.options.map(op => op.shape.method.value === 'mouseMove' ? mouseMove : op))
export const MacRequest = z.discriminatedUnion('method', [macOperation(first), ...rest.map(macOperation)])
/**
* Select the native route on the provider's parsed request, before coordinates are rechecked and dispatched.
@@ -1078,12 +1078,14 @@ it('drops dismissal, aborted answers and missing human answerers without allowin
const f = await hoverFixture()
f.ask.mockRejectedValueOnce(new UserQuestionError('dismissed', 'ASK_CANCELLED'))
expect(JSON.stringify((await f.execute('mouseMove', physicalHover)).content)).toContain('MOUSE_APPROVAL_CANCELLED')
f.ask.mockRejectedValueOnce(new UserQuestionError('The answer channel disconnected', 'ASK_ABORTED'))
expect(JSON.stringify((await f.execute('mouseMove', physicalHover)).content)).toContain('The answer channel disconnected')
const controller = new AbortController()
f.ask.mockImplementationOnce(async () => { controller.abort(); return f.approved })
await Promise.allSettled([f.execute('mouseMove', physicalHover, controller.signal)])
expect(native.request).not.toHaveBeenCalled()
expect((await f.execute('mouseMove', physicalHover)).isError).toBe(false)
expect(f.ask).toHaveBeenCalledTimes(3)
expect(f.ask).toHaveBeenCalledTimes(4)
await f.provider.dispose()
await f.ctx.plugin(NativeProvider, config)
await f.select()
+4 -4
View File
@@ -6,8 +6,8 @@
en: ed2539c6d12f814a
zh: 55a6c9d7f0d21fb7
/deepseek-ai-dsh-cua:
en: e419f97a55d5c315
zh: ebe3dce308d9cd97
en: 00f8b591b74dfba6
zh: e928ab13be2eebfa
/deepseek-ai-dsh-cua/summary:
en: 2ee2024dfc558d86
zh: c3b5661be12923c4
@@ -21,8 +21,8 @@
en: d92d5948a7941e8d
zh: d43491ae6521b87e
/deepseek-ai-dsh-cua/understand-the-implementation:
en: 2ab74a1ff120459e
zh: 39b3ef198989f133
en: cb26e420c2ea222f
zh: b6f17c9b71cea7bf
/deepseek-ai-dsh-cua/further-exploration:
en: c174fb5a00ea3681
zh: 039340042676881d
+2 -2
View File
@@ -9,8 +9,6 @@ English | [中文](README.zh.md)
Providers install the app/window object API with `installCua`; callers connect through `connectCua`. The `cua` API discovers applications, and `CuaApp` objects activate the selected application and enumerate its windows. `CuaApp` and `CuaWindow` both extend `CuaSurface`; explicit window objects own screenshots and pointer input. The macOS provider supplies native execution for this interface.
`CuaSessionPermissions.request(agent, permission, prompt, signal)` asks for one named permission using the provider’s localized question and detail. A live Session caches each permission separately. Providers declare additional tool fields with their request schema and can prepare a move before dispatch.
## Summary
Inspect and operate applications through retained objects: select an application, read its accessibility tree, capture explicit windows, and dispatch authorized input. Platform providers supply native execution; this library owns the object API, request validation, application consent, and image admission. The external PTC runtime owns variable persistence.
@@ -70,6 +68,8 @@ The Host marker has no configurable fields; platform providers own native config
<a id="understand-the-implementation"></a>
## Understand the implementation
`CuaSessionPermissions.request(agent, permission, prompt, signal)` asks for one named permission using the provider’s localized question and detail. A live Session caches each permission separately. Providers declare additional tool fields with their request schema and can prepare a move before dispatch.
Permission responses identify their platform and report only the state that platform can observe. The shared page renders the controls those observations support. Before platform identification, it shows neutral observation and input rows without system actions. Native providers use the shared build lock and compiler cleanup. Standalone executables are retained by digest. The macOS provider reads its signed application receipt after compilation completes under the same lock.
<details>
+2 -2
View File
@@ -9,8 +9,6 @@ kind: "package-reference"
各提供方通过 `installCua` 安装应用/窗口对象 API;调用方通过 `connectCua` 连接。`cua` API 负责发现应用,`CuaApp` 对象负责激活选定的应用并枚举其窗口。`CuaApp` 与 `CuaWindow` 都扩展 `CuaSurface`;显式窗口对象负责截图和指针输入。macOS 提供方为此接口提供原生执行。
提供方通过 `CuaSessionPermissions.request(agent, permission, prompt, signal)` 申请指定权限,并传入本地化的问题和说明。每个会话分别记录各项权限的决定。提供方通过请求 schema 声明额外字段,可在派发前处理悬停请求。
## 概述
通过保留对象检查并操作应用:选择应用、读取辅助功能树、截取显式窗口并派发已授权输入。平台提供方负责原生执行;本库负责对象 API、请求校验、应用授权和图像接纳。变量持久性由外部 PTC 运行时负责。
@@ -70,6 +68,8 @@ Computer Use 详情页的权限卡片、组件列表和来源信息使用 l3 边
<a id="understand-the-implementation"></a>
## 理解实现
提供方通过 `CuaSessionPermissions.request(agent, permission, prompt, signal)` 申请指定权限,并传入本地化的问题和说明。每个会话分别记录各项权限的决定。提供方通过请求 schema 声明额外字段,可在派发前处理悬停请求。
权限响应标识所属平台,并且只报告该平台能够观察到的状态。共享页面显示这些观察结果支持的控件。平台尚未确认时显示通用观察和输入项,不提供系统操作。原生提供方共用编译锁和编译进程清理逻辑。独立可执行文件按内容摘要保存。macOS 提供方在锁内等待编译完成,再读取签名应用的构建回执。
<details>
+1 -1
View File
@@ -130,7 +130,7 @@ export interface CuaBackend {
/** Model request schema including this provider's operation options. Defaults to the shared schema. */
requestSchema?: z.ZodType<CuaOperation>
/** Resolve provider options on the parsed copy. An optional interactive step runs before authorized dispatch. */
prepareMouseMove?: (agent: Agent, params: Record<string, JsonValue>) => ((signal: AbortSignal) => Promise<void>) | undefined
prepareMouseMove?: (agent: Agent, params: Record<string, JsonValue | undefined>) => ((signal: AbortSignal) => Promise<void>) | undefined
/** Dispatch a directional key through the provider's execution-scoped input owner, after application authorization. */
sendKeyEvent?: (client: NativeClient, params: Record<string, JsonValue>,
execution: Readonly<ToolExecution>) => Promise<JsonValue>