已废弃: 本设计描述的是已移除的仓库内 relay /
remoteControlToken/bridgeToken方案。当前实现使用外部 relaywss://zcode.z.ai/ws与/remoteQR 参数,业务消息统一封装在data.payload。
当前 Web 远程控制的 token/sessionId 绑定到某个 workspace 的一次访问。手机扫码后进入的是当前 workspace 的旁路访问;跨 workspace 打开 task 时,会在同一条 relay control tunnel 下创建新的 relay session,并跳转到新的 connectUrl 重新 bootstrap。
这个模型适合“分享当前 workspace”,但不适合“手机端在一个有效期内控制整个 desktop 窗口”。目标模型应把“手机对窗口的短期 pairing 授权”和“当前 workspace 的数据桥”拆开。
第一版改造目标:
deviceToken 有效期内控制一个 desktop 窗口,而不是某个 workspace。task-b 后,桌面端仍可留在 task-a;桌面端之后手动切到 task-b 时能看到手机端产生的结果。第一版不做:
新模型拆成窗口级控制会话和 workspace 数据桥。
Desktop Window
└── WindowControlSession
├── windowControlSessionId
├── deviceToken
├── tunnelId
├── mobileConnected
├── mobileViewState
│ ├── activeWorkspaceKey
│ ├── activeTaskId
│ └── updatedAt
└── currentWorkspaceBridge
├── bridgeSessionId
├── workspaceKey
├── workspacePath
├── kind: local | remote
├── remote.workspaceIdentity
├── remote.remoteSessionId
└── initialTaskId?
语义变化:
旧模型:
token/sessionId = 某个 workspace 的一次访问
新模型:
deviceToken/windowControlSessionId = 控制整个 desktop 窗口
bridgeSessionId = 手机当前打开的 workspace 数据桥
视图状态和业务状态分离:
desktop activeTaskId = 桌面端当前选择
mobile activeTaskId = 手机端当前选择
task data/runtime = 双端共享
packages/shared/src/web-remote-control.ts 增加窗口级类型。
export type WebRemoteControlTokenKind = "window-pairing";
export type WebRemoteControlWindowFailureReason =
| WebRemoteControlFailureReason
| "invalid-mobile-connection"
| "desktop-bootstrap-timeout";
export interface WebRemoteControlWindowSession {
windowControlSessionId: string;
deviceToken: string;
tokenKind: WebRemoteControlTokenKind;
connectUrl: string;
qrUrl: string;
expiresAt: number;
status: "idle" | "starting" | "running" | "active" | "error";
}
export interface WebRemoteControlMobileViewState {
activeWorkspaceKey?: string;
activeTaskId?: string;
updatedAt: number;
}
export interface WebRemoteControlLocalWorkspaceBridge {
bridgeSessionId: string;
bridgeToken: string;
kind: "local";
workspaceKey: string;
workspacePath: string;
initialTaskId?: string;
wsUrl: string;
}
export interface WebRemoteControlRemoteWorkspaceBridge {
bridgeSessionId: string;
bridgeToken: string;
kind: "remote";
workspaceKey: string;
workspacePath: string;
workspaceIdentity: string;
remoteSessionId: string;
initialTaskId?: string;
wsUrl: string;
}
export type WebRemoteControlWorkspaceBridge =
| WebRemoteControlLocalWorkspaceBridge
| WebRemoteControlRemoteWorkspaceBridge;
export interface WebRemoteControlWindowBootstrapResult {
windowControlSessionId: string;
workspaces: WebRemoteControlWorkspaceTarget[];
tasks: WebRemoteControlTaskTarget[];
mobileViewState?: WebRemoteControlMobileViewState;
}
export interface WebRemoteControlWindowControlReadyEvent {
type: "window-control-ready";
windowControlSessionId: string;
mobileConnectionId: string;
expiresAt: number;
}
resolveWebRemoteControlWorkspaceKey() 继续使用现有规则:
workspaceKey = workspaceIdentity?.trim() || workspacePath;
约束:
deviceToken 是短期窗口 pairing token,不是永久设备凭据。deviceToken 默认 8 小时过期;手机端有有效操作时滑动刷新 expiresAt。deviceToken 刷新。workspaceIdentity,并且 workspaceKey 必须按 workspaceIdentity?.trim() || workspacePath 计算。buildRemoteWorkspaceIdentity;禁止在 relay、desktop main 或 web 里手写拼接规则。deviceToken 本身就是本次远控的一次性授权,mobile web 携带 remoteControlToken 访问时不要求先登录。invalid-mobile-connection 和 desktop-bootstrap-timeout 要进入共享错误类型,避免 relay/web/测试使用裸字符串。Relay 需要拆分两类 session manager。
WindowControlSessionManager
deviceToken -> window control session
WorkspaceBridgeSessionManager
bridgeToken / bridgeSessionId -> workspace bridge session
窗口级 session 只保存授权、连接和当前 mobile lease;workspace/task 快照不缓存在 relay。
deviceToken
├── expiresAt
├── activeMobileConnectionId?
└── activeWindowControlSocket?
窗口级 API:
POST /api/remote-control/windows
Desktop main 创建窗口级远控 session,返回 8 小时滑动过期的 deviceToken。
GET /api/remote-control/windows/bootstrap/:deviceToken
Mobile web bootstrap。Relay 校验 deviceToken 未过期后,通过 desktop control socket 实时请求窗口内 workspace/task 快照和 mobileViewState。
WS /ws/remote-control/window/:deviceToken
Mobile window control socket。第一版只允许一个 mobile socket。连接成功后 relay 下发 mobileConnectionId。
POST /api/remote-control/windows/:deviceToken/workspace-bridge
Mobile 请求打开或切换 workspace bridge。必须携带当前 active mobileConnectionId。
POST /api/remote-control/windows/:deviceToken/mobile-view-state
Mobile 上报当前 activeWorkspaceKey / activeTaskId。必须携带当前 active mobileConnectionId。
变更类 HTTP API 的连接校验:
X-ZCode-Mobile-Connection-Id: <mobileConnectionId>
Relay 只接受 activeMobileConnectionId 匹配且对应 window control socket 仍打开的请求;否则返回 401 invalid-mobile-connection 或 409 session-conflict。这样第二个浏览器标签页即使知道 deviceToken,在 window control socket 被拒后也不能切 bridge 或覆盖 mobileViewState。
window control socket 连接成功后,relay 向 mobile 发送:
{
"type": "window-control-ready",
"windowControlSessionId": "<windowControlSessionId>",
"mobileConnectionId": "<mobileConnectionId>",
"expiresAt": 0
}
Mobile 只有收到该事件后,才允许请求 workspace-bridge 或 mobile-view-state。
deviceToken 滑动刷新规则:
workspace-bridge / mobile-view-state 校验 active connection 成功后刷新一次。activeMobileConnectionId,后续裸 token 请求不能刷新过期时间。数据桥 API:
WS /ws/remote-control/bridge/:bridgeToken
Mobile 当前 workspace 的 RPC 数据 socket。
WS /ws/desktop/bridge/:bridgeSessionId
Desktop main 专用 host bridge 的数据 socket。
/r/:deviceToken 可以继续保留,但语义改成窗口入口。Relay 重定向到 web 时仍可使用:
?remoteControlToken=<deviceToken>&relayOrigin=<relay-origin>
这样前端 URL 参数名字可以暂时不变,减少入口改动。
第一版把桌面端二维码视为一次性短期授权入口,不把 Web 登录作为远控使用前置条件。
访问规则:
remoteControlToken 时,mobile web 跳过当前 Web OAuth 登录门禁,直接进入 relay bootstrap。remoteControlToken 的安全边界由 8 小时滑动过期、active mobileConnectionId 和单手机连接策略承担。remoteControlToken、直接访问 /web-remote 时,继续走现有登录页 / 登录后 waiting page,用于后续设备列表和长期设备管理。packages/web/src/main.tsx 的入口判断需要拆分:
hasRemoteControlToken = params.has("remoteControlToken");
if (hasRemoteControlToken) {
// 二维码一次性 token 模式:不要求 Web OAuth 登录。
return bootstrapWebRemoteControlByToken();
}
if (isWebRemoteRoute()) {
// 登录后的设备列表 / waiting page 模式。
return bootstrapWebRemoteAuthShell();
}
Desktop main 与 relay 的 control socket 继续承担控制面,不承载 RPC 二进制帧。
新增消息:
type RelayDesktopControlEvent =
| { type: "window-control-registered"; tunnelId: string }
| {
type: "window-bootstrap-request";
requestId: string;
windowControlSessionId: string;
}
| {
type: "window-bootstrap-response";
requestId: string;
windowControlSessionId: string;
success: true;
result: WebRemoteControlWindowBootstrapResult;
}
| {
type: "window-bootstrap-response";
requestId: string;
windowControlSessionId: string;
success: false;
error: string;
}
| { type: "mobile-connected"; windowControlSessionId: string }
| { type: "mobile-disconnected"; windowControlSessionId: string }
| {
type: "workspace-bridge-request";
requestId: string;
windowControlSessionId: string;
workspaceKey: string;
taskId?: string;
}
| {
type: "workspace-bridge-response";
requestId: string;
windowControlSessionId: string;
success: true;
bridge: WebRemoteControlWorkspaceBridge;
}
| {
type: "workspace-bridge-response";
requestId: string;
windowControlSessionId: string;
success: false;
error: string;
}
| {
type: "mobile-view-state-update";
windowControlSessionId: string;
viewState: WebRemoteControlMobileViewState;
};
现有 workspace list、platform request 仍走 control socket。第一版可以保留现有 request/response 形态,但参数语义要从 workspace session token 改成 window control session。
GET /api/remote-control/windows/bootstrap/:deviceToken 的返回值以 desktop main 为准,relay 不保存 workspace/task 快照。
Bootstrap 时序:
deviceToken 存在且 expiresAt > Date.now()。window-bootstrap-request { requestId, windowControlSessionId }。WebRemoteControlWorkspaceTarget[]WebRemoteControlTaskTarget[]mobileViewStatewindow-bootstrap-response。错误语义:
404/410 session-not-found | session-expired。410 desktop-disconnected。504 desktop-bootstrap-timeout。windowControlSessionId;mobile 进入空态并提供刷新。workspaceIdentity 或 remoteSessionId:desktop main 在快照中过滤该 workspace,避免 mobile 进入不可桥接状态。WebRemoteControlManager 从 workspace runtime 改为 window runtime。
interface WindowControlRuntime {
windowId: number;
windowControlSessionId: string;
deviceToken: string;
expiresAt: number;
activeMobileConnectionId?: string;
tunnelId: string;
connectUrl: string;
qrUrl: string;
controlSocket: WebSocket;
mobileConnected: boolean;
mobileViewState?: WebRemoteControlMobileViewState;
currentBridge?: WorkspaceBridgeRuntime;
error?: string;
failure?: WebRemoteControlFailure;
}
interface LocalWorkspaceBridgeRuntime {
bridgeSessionId: string;
bridgeToken: string;
kind: "local";
workspaceKey: string;
workspacePath: string;
initialTaskId?: string;
host?: WebRemoteControlHostHandle;
dataSocket?: WebSocket;
disposeBridge?: () => void;
}
interface RemoteWorkspaceBridgeRuntime {
bridgeSessionId: string;
bridgeToken: string;
kind: "remote";
workspaceKey: string;
workspacePath: string;
workspaceIdentity: string;
remoteSessionId: string;
initialTaskId?: string;
host?: WebRemoteControlHostHandle;
dataSocket?: WebSocket;
disposeBridge?: () => void;
}
type WorkspaceBridgeRuntime =
| LocalWorkspaceBridgeRuntime
| RemoteWorkspaceBridgeRuntime;
start(windowId) 行为:
/ws/desktop/control。tunnelId。/api/remote-control/windows 创建窗口级 session。WindowControlRuntime。deviceToken / connectUrl / qrUrl / windowControlSessionId 给桌面弹层。createWorkspaceBridge(windowId, workspaceKey, taskId?) 行为:
remoteSessionId 和 workspaceIdentity。currentBridge 的 host、data socket 和协议桥。InitLocal 创建 dedicated host。remoteSessionId 找原 remote target,通过 InitRemote 创建 dedicated host。/ws/desktop/bridge/:bridgeSessionId。MessagePort。mobileViewState 行为:
mobile-view-state-update 后只写入 window runtime。packages/web/src/main.tsx 保持 remoteControlToken 参数名,但把它解释为 deviceToken。
Bootstrap 流程:
remoteControlToken,跳过 Web OAuth 登录检查。GET /api/remote-control/windows/bootstrap/:deviceToken。mobileViewState。WS /ws/remote-control/window/:deviceToken。mobileConnectionId。mobileViewState.activeWorkspaceKey,优先打开该 workspace;否则打开第一个可用 workspace。X-ZCode-Mobile-Connection-Id 请求 workspace-bridge,拿到 bridge wsUrl。wsUrl 建立当前 workspace 的 service accessor。Root,传入 initialWorkspaceAbsPath / initialWorkspaceIdentity / initialTaskId。普通 /web-remote 流程:
remoteControlToken 时继续走现有 Web OAuth 登录逻辑。Task 点击行为:
onSelectTask()。X-ZCode-Mobile-Connection-Id POST mobile-view-state。X-ZCode-Mobile-Connection-Id POST workspace-bridge。activeWorkspaceKey / activeTaskId。X-ZCode-Mobile-Connection-Id POST mobile-view-state。deviceToken,不跳新的窗口级入口。第一版可以通过重新挂载 Root 来替换 service accessor,避免在 UI 层引入复杂的多 accessor 热切换。重新挂载只发生在手机切 workspace 时,不影响桌面端。
Desktop main 保存单份 mobileViewState:
type MobileViewState = {
activeWorkspaceKey?: string;
activeTaskId?: string;
updatedAt: number;
};
桌面端如果需要展示,可以新增平台事件:
PlatformChannels.WebRemoteControlMobileViewStateChanged
新增该事件时必须同时修改:
packages/shared/src/channels.ts 的 PlatformChannels。PlatformChannelMap 的 request/response 类型。packages/shared/src/platform.ts 的 IPlatformService 订阅接口。renderer 订阅后只展示状态,不改变当前选中项。禁止在 renderer 里直接监听硬编码字符串频道。
为了保证手机操作结果能被桌面端后续看到,采用低风险刷新策略。
如果后续要做实时 invalidation,必须先补正式平台频道和事件来源:
PlatformChannels.WebRemoteControlTaskInvalidated
{ workspaceKey, taskId }
事件来源必须是 service/runtime 层显式发出的 task updated 领域事件,再由 desktop main 转发;禁止使用散落硬编码字符串,也禁止通过解析 RPC 二进制帧推断 task 更新。
renderer 收到后只标记缓存过期,不改变当前 active task。这样手机端持续操作不会打断桌面端,但桌面端进入该 task 时能看到最新结果。
第一版不做跨端 Zustand 强同步,也不把 mobile host 注册到 desktop BroadcastHub。
第一版同一 deviceToken 只允许一个 mobile window control socket。
如果第二个手机或第二个浏览器标签页连接:
session-conflict。mobileConnectionId。workspace-bridge / mobile-view-state HTTP 请求都会因为缺少 active mobileConnectionId 被拒绝。选择“不踢旧端”可以避免用户扫码误操作导致当前手机会话被中断。
共享协议层
packages/shared/src/web-remote-control.ts。workspaceIdentity 和 remoteSessionId。Relay
deviceToken 实现 8 小时过期和 active connection 滑动刷新。workspace-bridge / mobile-view-state 校验 active mobileConnectionId。/r/:token 入口,改语义为 deviceToken。Desktop Main
webRemoteControlManager 状态模型。start() 改为创建 window runtime。createWorkspaceBridge()。workspaceIdentity 和 remoteSessionId。window-bootstrap-request,返回当前窗口已同步的 workspace/task 快照。mobileViewState。createWebRemoteControlHost() 的 InitRemote 路径。Web
remoteControlToken 模式绕过 Web OAuth 登录门禁,直接走一次性 token bootstrap。/web-remote 无 token 模式保留登录 / waiting page。mobileConnectionId,再请求 bridge 或上报 view state。connectUrl。UI 与刷新
PlatformChannels + IPlatformService,禁止硬编码频道。清理旧模型
docs/web-remote-control/web-remote-control-architecture.md。Relay 单测:
deviceToken 和 expiresAt。deviceToken 第二个 mobile socket 返回 session-conflict。mobileConnectionId 时,workspace-bridge / mobile-view-state 返回 401 invalid-mobile-connection 或 409 session-conflict。expiresAt,bootstrap 不刷新。Desktop main 单测:
start(windowId) 不需要 workspace context。createWorkspaceBridge() 能为 local workspace 创建 host。createWorkspaceBridge() 能为 remote workspace 复用 remoteSessionId 找 target,并要求 workspaceIdentity 存在。mobileViewState 不影响桌面 active tab/task。window-bootstrap-request 返回当前窗口同步的 workspace/task 快照。Web/UI 单测:
remoteControlToken 且未登录时,不渲染登录页,直接使用 deviceToken 获取窗口数据。remoteControlToken 访问 /web-remote 时,仍按现有 Web OAuth 登录 / waiting page 逻辑处理。mobileConnectionId。connectUrl,并携带 active connection header。workspaceIdentity 作为隔离 key。connectRemote() 在远控模式继续明确返回不支持。集成验证:
task-b,桌面停留在 task-a。task-b 发送消息并产生结果。task-b,能看到手机端结果。机械校验:
pnpm lintpnpm typecheckRoot 切 workspace 比热切换 accessor 简单稳定,但跨 workspace 切换会有短暂 loading。deviceToken 是 8 小时滑动过期的短期 pairing token,二维码入口不要求登录;正式发布前仍需要补可选登录态增强校验、主动撤销、权限提示和 feature flag。优先实现窗口级 session 和单手机控制,再处理体验优化:
这样每一步都可以独立验证,并且可以保留旧 workspace 级入口一段时间用于回归对比。