diff --git a/packages/experimental/cua/README.i18n.yaml b/packages/experimental/cua/README.i18n.yaml index 2bc0888b63..30b56a371c 100644 --- a/packages/experimental/cua/README.i18n.yaml +++ b/packages/experimental/cua/README.i18n.yaml @@ -21,8 +21,8 @@ en: 3f638a4a76023fc5 zh: 515aeb5de6ae5b33 /deepseek-ai-dsh-cua/understand-the-implementation: - en: cf9d118b6080368b - zh: 70aa90a0b054dc9e + en: f691467605a7eccb + zh: da0e7fbcfc654fc9 /deepseek-ai-dsh-cua/further-exploration: en: c174fb5a00ea3681 zh: 039340042676881d diff --git a/packages/experimental/cua/README.md b/packages/experimental/cua/README.md index 723c95e316..8aa6f366aa 100644 --- a/packages/experimental/cua/README.md +++ b/packages/experimental/cua/README.md @@ -91,7 +91,7 @@ Permission responses identify their platform and report only the state that plat
Shared execution and platform ownership -Providers implementing `CuaBackend.heldInputState` accept `taskId` on explicit input and system pointer movement, report their connection-owned records through `getHeldInputs({ taskId })`, and acknowledge `releaseHeldInputs({ taskId })`. Native records own the actual key/button, target, process identity and input mode. Successful `run_code` and `cua_run_code` programs query those records and append a system reminder when input remains held; normal program and turn endings preserve input in both DSH and MCP. Reminders travel through logged additional context in DSH and through the tool response in MCP. macOS and Windows both implement this protocol. Capability methods remain static. +Providers implementing `CuaBackend.heldInputState` accept `taskId` on explicit input and system pointer movement, report their connection-owned records through `getHeldInputs({ taskId })`, and acknowledge `releaseHeldInputs({ taskId })`. Native records own the actual key/button, target, process identity and input mode. Successful `run_code` and `cua_run_code` programs query those records and append a system reminder when input remains held; normal program and turn endings preserve input in both DSH and MCP. DSH delivers the latest pending held-input reminder with the next model request. A later program in the same tool batch supersedes the earlier result, including when it releases all input. MCP returns the state in each program response. macOS and Windows both implement this protocol. Capability methods remain static. Held input belongs to the task and native connection, outside program processes. A DSH stop or model failure starts cleanup. The Agent can finish the turn and resolve `whenIdle()` while the native release is pending. Subsequent CUA calls wait for its outcome. Confirmed release queues a notice for the model and removes pending held-state reminders. A cleanup failure is reported through `agent/error` and disables CUA for that task. A failed or cancelled program, an active MCP request receiving cancellation, an environment reset, and connection or provider disposal each await their own cleanup. diff --git a/packages/experimental/cua/README.zh.md b/packages/experimental/cua/README.zh.md index 83f5de04dd..bd3768ec77 100644 --- a/packages/experimental/cua/README.zh.md +++ b/packages/experimental/cua/README.zh.md @@ -91,7 +91,7 @@ Computer Use 详情页的权限卡片、组件列表和来源信息使用 l3 边
共享执行与平台职责 -实现 `CuaBackend.heldInputState` 的提供方为显式输入和系统指针移动接受 `taskId`,通过 `getHeldInputs({ taskId })` 报告本连接的持有记录,并确认 `releaseHeldInputs({ taskId })`。原生记录负责保存实际按键或按钮、目标、进程身份和输入方式。成功结束的 `run_code` 和 `cua_run_code` 查询这些记录,仍有输入按住时添加 system reminder;DSH 和 MCP 的正常程序结束和回合结束均保持输入。DSH 通过已记录的额外上下文传递提醒,MCP 通过工具响应传递。macOS 和 Windows 都实现了这套协议。能力方法列表保持静态。 +实现 `CuaBackend.heldInputState` 的提供方为显式输入和系统指针移动接受 `taskId`,通过 `getHeldInputs({ taskId })` 报告本连接的持有记录,并确认 `releaseHeldInputs({ taskId })`。原生记录负责保存实际按键或按钮、目标、进程身份和输入方式。成功结束的 `run_code` 和 `cua_run_code` 查询这些记录,仍有输入按住时添加 system reminder;DSH 和 MCP 的正常程序结束和回合结束均保持输入。DSH 在下一次模型请求中只传递最新的待交付持有提醒。同一批中的后续程序松开全部输入后,这一批不会向模型报告仍有输入按住。MCP 在每次程序响应中返回当时的状态。macOS 和 Windows 都实现了这套协议。能力方法列表保持静态。 持有输入属于任务和原生连接,保存在程序进程之外。DSH 收到停止请求或模型报错时启动清理。原生释放尚未确认时,Agent 可以结束回合,`whenIdle()` 也可以返回。后续 CUA 调用等待清理结果。确认释放后,DSH 为模型记录释放提醒,并撤销仍在队列中的旧持有提醒。清理失败会通过 `agent/error` 单独报告,并禁用该任务后续的 CUA 操作。程序失败或取消、执行中的 MCP 请求收到取消、环境重置、连接关闭和提供方卸载,各自等待清理完成。 diff --git a/packages/experimental/cua/src/held-inputs.ts b/packages/experimental/cua/src/held-inputs.ts index 0f424bb19c..944c864730 100644 --- a/packages/experimental/cua/src/held-inputs.ts +++ b/packages/experimental/cua/src/held-inputs.ts @@ -1,7 +1,7 @@ /** Native-owned held input, program-end reminders, and failure/cancellation cleanup. */ import type { Context } from '@deepseek-ai/cordis' import { emitAgentEvent, type Agent } from '@deepseek-ai/dsh-agent' -import { createUserMessage } from '@deepseek-ai/dsh-llm' +import { createUserMessage, type MessageId, type UserMessage } from '@deepseek-ai/dsh-llm' import { RUN_CODE_NAME } from '@deepseek-ai/dsh-tools' import { z } from 'zod' import { NativeError, type NativeClient } from './native-client.ts' @@ -24,7 +24,15 @@ function renderInputs(inputs: z.infer, state: 'held' | 'relea ''].join('\n') } +function discardQueuedHeldNotices(agent: Agent): void { + for (const message of [...agent.inbox.nextStep, ...agent.inbox.nextTurn]) { + if (message.source.kind === 'cua-held-inputs') agent.inbox.remove(message.id) + } +} + class InputOwner { + // Batch delivery can enqueue this notice after another program has queried a newer state. + latestHeldNotice: MessageId | undefined readonly controller = new AbortController() private client: NativeClient | undefined private removeCleanup: (() => void) | undefined @@ -42,9 +50,11 @@ class InputOwner { this.client = client } - async reminder(inTurn: boolean): Promise { + async reminder(inTurn: boolean): Promise { if (this.closing || !this.client) return const inputs = heldInputs.parse(await this.client.request('getHeldInputs', { taskId: this.agent.id }, this.controller.signal)) + discardQueuedHeldNotices(this.agent) + this.latestHeldNotice = undefined if (inputs.length === 0) { this.removeCleanup?.() this.removeCleanup = undefined @@ -52,11 +62,15 @@ class InputOwner { return } this.notifySessionOnRelease ||= inTurn - return renderInputs(inputs, 'held') + const notice = createUserMessage({ content: [{ type: 'text', text: renderInputs(inputs, 'held') }], + source: { kind: 'cua-held-inputs', form: 'notice', summary: 'CUA inputs remain held' } }) + this.latestHeldNotice = notice.id + return notice } close(canRequest = true): Promise> { + this.latestHeldNotice = undefined this.controller.abort() return this.closing ??= Promise.resolve().then(async () => { try { @@ -108,9 +122,7 @@ export class CuaHeldInputs { emitAgentEvent(this.ctx, agent, 'agent/error', { turn: this.ctx.agents.turnBoundaryOf(agent.session).lastTurn, step: 0, error }) }, (inputs) => { try { - for (const message of [...agent.inbox.nextStep, ...agent.inbox.nextTurn]) { - if (message.source.kind === 'cua-held-inputs') agent.inbox.remove(message.id) - } + discardQueuedHeldNotices(agent) if (inputs.length === 0) return agent.inject(createUserMessage({ content: [{ type: 'text', text: renderInputs(inputs, 'released') }], @@ -169,16 +181,13 @@ export class CuaHeldInputs { } else if (execution.parent === undefined) { const owner = this.owners.get(agent) const inTurn = ctx.agents.turnBoundaryOf(agent.session).openTurnStartSeq !== null - const text = await owner?.reminder(inTurn).catch(async (error: unknown) => { + const notice = await owner?.reminder(inTurn).catch(async (error: unknown) => { await releaseAfter(signal.aborted ? 'Program cancelled' : `Held-input query failed: ${String(error)}`) throw error }) // oxlint-disable-next-line typescript/no-unnecessary-condition -- Cancellation can arrive while the native state query awaits. if (signal.aborted) { await releaseAfter('Program cancelled'); return result } - if (text && !owner?.closing) return { ...result, additionalContexts: [...result.additionalContexts ?? [], - createUserMessage({ content: [{ type: 'text', text }], - source: { kind: 'cua-held-inputs', form: 'notice', summary: 'CUA inputs remain held' } }), - ] } + if (notice && !owner?.closing) return { ...result, additionalContexts: [...result.additionalContexts ?? [], notice] } } return result } finally { @@ -198,7 +207,7 @@ export class CuaHeldInputs { }) yield ctx.on('agent/inbox/inserted', ({ agent, message }) => { // This live notification runs after append publication, so removal can safely write its own event. - if (message.source.kind === 'cua-held-inputs' && this.owners.get(agent)?.closing) agent.inbox.remove(message.id) + if (message.source.kind === 'cua-held-inputs' && message.id !== this.owners.get(agent)?.latestHeldNotice) agent.inbox.remove(message.id) }, { prepend: true }) yield ctx.on('session/event', (session, event) => { if (event.type !== 'turn/end') return