Windows source Helper 已具备 UI Automation 元素树、指针输入和原生文本剪贴板,但视觉链路仍是占位实现:
ax_native.node 的 captureWindowImage 固定返回 null;SystemSurface 声明 supportsScreenCapture=false;screenshot 无法捕获所选显示器;get_app_state(include_screenshot=true) 只能返回 UIA 状态,不能附带窗口图像;space:"state_image" 和 space:"screenshot" 已有坐标语义,却没有 Windows 图像来源。macOS 已经形成“窗口身份校验 + 窗口图像 + 状态缓存”的完整闭环。Windows 采用相同的产品边界,但底层像素来源使用 Windows Graphics Capture(WGC),以支持被其他普通窗口遮挡的目标窗口。
本设计只覆盖本地 Windows source Helper。macOS、Linux、SSH、WSL、Docker、remote workspace、手机 /remote 与产品打包链路保持不变。
本阶段一次完成 Windows 本地视觉闭环:
screenshot 捕获用户所选显示器,保留负坐标显示器原点;get_app_state(include_screenshot=true) 将同一次已验证观察的窗口图像附加到状态;state_id、space:"state_image" 和最新 screenshot visual-frame 语义,不新增截图修订协议;本阶段明确不包含:
/remote;desktopCapturer 或私有二进制作为 fallback;screenshotId 截图句柄,或修改 MCP tool schema;采用现有 Windows C++ addon ax_native.node,增加 C++/WinRT + D3D11 + WIC 实现,并通过 N-API 异步 Promise 暴露给 TypeScript。
未采用的方案:
desktopCapturer:把独立 Node Helper 反向耦合到 desktop renderer/main,破坏当前 Helper 可独立启动的边界。技术选型依据:
IGraphicsCaptureItemInterop 提供 CreateForWindow 和 CreateForMonitor,允许从 HWND/HMONITOR 创建 capture item;Direct3D11CaptureFramePool.CreateFreeThreaded 使用内部 worker thread 触发 frame event,不要求调用线程拥有 DispatcherQueue;GraphicsCaptureSession.IsSupported,并说明 WGC 的 frame-pool/session 生命周期;screenshot / get_app_state(include_screenshot=true)
|
v
TypeScript capability + target identity gate
|
v
native Promise
|
v
Napi::AsyncWorker(后台 MTA)
|
+--> IGraphicsCaptureItemInterop
| +--> CreateForMonitor(HMONITOR)
| `--> CreateForWindow(HWND)
|
v
Direct3D11CaptureFramePool::CreateFreeThreaded
|
v
等待第一张有效帧 / target closed / deadline
|
v
只复制 ContentSize 到 D3D11 staging texture
|
v
WIC 内存 PNG 编码
|
v
再次校验 monitor/HWND/PID/exe/bounds
|
+--> screenshot:写入最新 visual-frame
|
`--> get_app_state:图像与 UIA 状态一起进入 state cache
|
`--> state_id + space:"state_image"
所有状态关联仍由现有 broker 负责:
显示器 screenshot N
`--> visual-frame N(原点 = 该显示器物理 bounds)
`--> space:"screenshot" 只允许引用当前 visual-frame
窗口 capture_app N
`--> UIA tree N + PNG N + capture bounds N
`--> state_id N
`--> space:"state_image" 只允许引用 state_id N 的图像
后一次 screenshot/zoom 会替换 session 当前 visual-frame;后一次 get_app_state 会生成新 state_id。因此现有协议已经具备截图版本边界,无需再引入 screenshotId。
在 Windows addon 上增加 additive、可选导出:
type NativeScreenCaptureSuccess = {
ok: true;
format: "png";
data: Uint8Array;
width: number;
height: number;
bounds: [number, number, number, number];
};
type NativeScreenCaptureFailure = {
ok: false;
error:
| "unsupported"
| "unavailable"
| "busy"
| "timeout"
| "invalid_target"
| "target_changed"
| "too_large"
| "device_lost"
| "capture_failed"
| "encode_failed";
};
type NativeScreenCaptureOutcome = NativeScreenCaptureSuccess | NativeScreenCaptureFailure;
interface AxWinNativeAddon {
isScreenCaptureSupported?(): boolean;
captureMonitorPngAsync?(
bounds: [number, number, number, number],
): Promise<NativeScreenCaptureOutcome>;
captureWindowPngVerifiedAsync?(
windowId: number,
pid: number,
expectedCanonicalBundleId: string,
expectedBounds?: [number, number, number, number],
): Promise<NativeScreenCaptureOutcome>;
}
data 在实现中可由 Node Buffer 承载,但 ABI 按 Uint8Array 校验,避免上层依赖 Buffer 专有方法。失败 outcome 不暴露 HRESULT、系统错误文本、窗口标题、可执行路径、图像内容、pipe 或 token。
三个导出必须成组存在。旧 addon、半组导出、异常返回值或 rejected Promise 全部 fail closed,不允许回退到同步占位函数。
broker 已选择显示器并持有其物理 bounds。native 不信任暴露给 JS 的 HMONITOR 数值长期有效,而是:
[x, y, width, height];HMONITOR;target_changed。成功 outcome 的 bounds 是最终验证过的显示器物理 bounds。负 x/y 保留,不做归零;visual-frame 用该 bounds 作为全局到图像坐标的原点。
窗口捕获接收 HWND、期望 PID、规范化可执行路径以及 UIA 观察时的可选 bounds。捕获前后执行相同校验:
IsWindow(hwnd) 为真;GetWindowThreadProcessId 等于期望 PID;IsIconic(hwnd) 为假,窗口宽高大于零;GetWindowRect 为有效物理坐标;若窗口、进程或边界在观察期间变化,返回 target_changed。对于 get_app_state,此错误使整个观察失败并要求重新观察,不能把旧 UIA tree 和另一时刻的图像组合起来。
WGC 的帧像素尺寸与 GetWindowRect 可能受系统边框或平台实现细节影响。成功 outcome 的 bounds 表示捕获前后已复核的桌面物理坐标范围,width/height 表示 PNG 像素尺寸;上层以这两组数据建立 state_image 坐标映射,不盲目信任先前 UIA outer bounds,也不在二者不一致时假设 1:1。
每个 capture worker 完成一张图后立即销毁捕获会话:
IDirect3DDevice;Direct3D11CaptureFramePool::CreateFreeThreaded 创建 2-buffer frame pool,避免依赖 DispatcherQueue;ContentSize 范围,拒绝复用纹理中的历史边缘像素;RowPitch 读取;输出是非交错标准 PNG。第一阶段使用 SDR BGRA8;HDR 显示器的颜色精度是已知限制,但不得因此伪报 capture success 后返回空白图。
WGC 与编码不能运行在 JS 主线程:
Napi::AsyncWorker;NAPI_DISABLE_CPP_EXCEPTIONS 继续约束 node-addon-api;WinRT 异常不得越过 worker/native ABI;std::timed_mutex g_screenCaptureMutex,一个 Helper 同时最多执行一项 WGC 捕获;busy;timeout;too_large。Helper 关闭时先停止新 admission。worker 在创建 session 前和取得帧后都检查 shutdown 状态;已接收的 worker 通过 RAII 关闭 session、frame pool、D3D/WinRT 对象后才完成 Promise。不得留下捕获线程、窗口、进程或磁盘临时图像。
Windows supportsScreenCapture 为真必须同时满足:
platform == win32
AND interactive desktop preflight passed
AND isScreenCaptureSupported() == true
AND typeof captureMonitorPngAsync == "function"
AND typeof captureWindowPngVerifiedAsync == "function"
isScreenCaptureSupported() 内部使用 GraphicsCaptureSession::IsSupported(),并确认运行系统支持 IGraphicsCaptureItemInterop::CreateForWindow/CreateForMonitor。
状态语义:
screenCaptureStatus="granted";"denied";"unknown",同时 capability 为 false。capability 表示当前 source Helper 可执行视觉捕获,不代表产品安装包、签名或无边框 capability 已完成。
createNodeAutomationAdapter 沿用原生剪贴板的成组 capability 模式:
captureScreenPng(area) 调用 monitor capture,并将成功结果转换为现有 screen capture 结构。它不缓存第二份图片;session visual-frame 仍由现有 backend 建立。
createAxWinReadOnlySource 增加一个可注入的 verified window capture callback:
includeScreenshot=false 时不启动 WGC;includeScreenshot=true 时,先取得 UIA snapshot 和窗口身份,再调用 verified capture;invalid_target / target_changed 使整个 observation 失败;busy、timeout、device_lost、capture_failed 或 encode_failed 时,允许保留 UIA tree,同时 screenshot=null,并附加 additive、净化后的 screenshot_error warning;state_image visual target,后续 space:"state_image" 必须明确拒绝并要求重新观察。这样既不因为一次只读视觉失败丢掉可用 UIA 信息,也不会让模型误以为该 state 含有图像。
| Native error | Broker 语义 | 重试规则 |
|---|---|---|
busy / timeout |
timeout |
只读观察可由调用方显式重试 |
unsupported |
capability false / not_authorized |
不在本次 session 重试 |
unavailable |
internal 或启动 capability false |
环境恢复后显式重试 |
invalid_target / target_changed |
element_unavailable,要求 reobserve |
不复用旧 state |
too_large |
invalid_request |
缩小目标后重试 |
device_lost |
retryable internal |
不自动重放动作,只重试只读捕获 |
capture_failed / encode_failed |
sanitized internal |
显式重试 |
视觉捕获是只读操作,可以明确重试;任何后续点击、输入等动作仍遵循原有 delivery-state,视觉失败不能触发动作自动重放。
Windows 进程已使用 PER_MONITOR_AWARE_V2,因此 UIA、GetWindowRect、monitor bounds 与 WGC capture bounds 都按物理像素解释。
space:"screenshot"全局物理坐标 = screenshot 图片坐标 + selected monitor bounds 原点
非主显示器的负坐标必须保留。后一次 screenshot 或 zoom 替换当前 visual-frame,旧 screenshot 坐标不得继续使用。
space:"state_image"全局物理坐标 = state_image 图片坐标 + state capture bounds 原点
state cache 保存 native 成功 outcome 的实际 bounds 和像素尺寸。若实际像素尺寸与 bounds 宽高不一致,上层必须按现有 image transform 显式缩放;不得默认为 1:1 后静默点击错误位置。窗口移动、缩放、最小化、销毁或 HWND/PID 复用都使旧 state 失效。
debug 级生命周期统计,且只记录结果类别、耗时、尺寸,不记录内容;source 开发阶段接受 Windows 自带捕获边框,这是系统向用户展示捕获行为的一部分;不通过隐藏窗口、manifest 欺骗或私有 API 绕过。
实现阶段严格按以下顺序先写失败测试,再写最小实现:
includeScreenshot=false 不调用捕获;state_image target。broker_info.capabilities.screen_capture;screenshot 建立正确 monitor visual-frame;get_app_state 建立带图像的 state;state_image fail closed;pnpm typecheck、pnpm lint;实机 smoke 默认关闭,必须显式 opt-in,并且只在普通已解锁桌面运行。测试使用受控测试窗口或测试期间启动的普通应用,不依赖用户已有窗口内容。
验证顺序:
get_app_state(include_screenshot=true),确认返回图像、新 state_id 和正确 capture bounds;state_image 坐标转换命中测试窗口内预设的安全目标;任何恢复失败都使 smoke 失败。锁屏、LockApp、无 foreground、非交互 desktop 环境只记录为“环境不满足”,不得报告 live acceptance 通过。
screen_capture capability 在完整 WGC ABI 下为 true;screenshot 返回所选显示器的有效 PNG,并正确处理负坐标原点;get_app_state(include_screenshot=true) 返回同一已验证窗口观察的 UIA tree、PNG、bounds 与新 state_id;space:"state_image" 只使用对应 state 的图像和 capture bounds;GetWindowRect 与可见内容边界可能存在系统边框差异,必须以返回的 capture bounds/尺寸做映射;这些风险不改变本阶段选择:先让源码开发环境形成可重复、可验证的 Windows WGC 视觉闭环,再进入发布与产品化。