feat: WebSocket reverse-connection gateway Phase 1 (#210) #212

Closed
xingyue wants to merge 1 commits from feat/210-ws-gateway-phase1 into main
Owner

What

Phase 1 of #210: WebSocket 反向连接替代 Cloudflare Tunnel。

Why

Cloudflared tunnel 依赖外部工具、KV heartbeat TTL 脆弱、URL 漂移导致 530。WS 反向连接零外部依赖,连接即注册,断开即离线。

Changes

Gateway (packages/workflow-gateway/)

  • src/agent-socket.ts新增 AgentSocket Durable Object,per-agent 持有一个 WS 连接,支持 status 查询和新连接替换旧连接
  • src/index.ts — 新增 /ws/connect 路由(GATEWAY_SECRET 认证 + WS upgrade 转发到 DO);endpoints 列表合并 KV + DO 状态
  • wrangler.toml — 新增 AGENT_SOCKET DO binding + migration

Agent (packages/cli-workflow/src/commands/serve/)

  • ws-client.ts新增 WS 客户端,指数退避自动重连(1s→30s),Phase 2 请求处理占位
  • serve.ts — 默认走 WS 模式(不再自动启 cloudflared),保留 --tunnel-url--no-tunnel fallback
  • types.ts — 新增 tunnelUrl 字段

Testing

#211 验证步骤:

  • Step 1: Gateway healthz 正常
  • Step 2: serve 启动后 WS 连接建立
  • Step 3: endpoints 返回 status: "online"
  • Step 4: 停止 serve 后从 endpoints 列表消失
  • Step 5: 代码含指数退避重连逻辑

Ref

## What Phase 1 of #210: WebSocket 反向连接替代 Cloudflare Tunnel。 ## Why Cloudflared tunnel 依赖外部工具、KV heartbeat TTL 脆弱、URL 漂移导致 530。WS 反向连接零外部依赖,连接即注册,断开即离线。 ## Changes ### Gateway (`packages/workflow-gateway/`) - `src/agent-socket.ts` — **新增** AgentSocket Durable Object,per-agent 持有一个 WS 连接,支持 status 查询和新连接替换旧连接 - `src/index.ts` — 新增 `/ws/connect` 路由(GATEWAY_SECRET 认证 + WS upgrade 转发到 DO);endpoints 列表合并 KV + DO 状态 - `wrangler.toml` — 新增 AGENT_SOCKET DO binding + migration ### Agent (`packages/cli-workflow/src/commands/serve/`) - `ws-client.ts` — **新增** WS 客户端,指数退避自动重连(1s→30s),Phase 2 请求处理占位 - `serve.ts` — 默认走 WS 模式(不再自动启 cloudflared),保留 `--tunnel-url` 和 `--no-tunnel` fallback - `types.ts` — 新增 `tunnelUrl` 字段 ## Testing #211 验证步骤: - ✅ Step 1: Gateway healthz 正常 - ✅ Step 2: serve 启动后 WS 连接建立 - ✅ Step 3: endpoints 返回 `status: "online"` - ✅ Step 4: 停止 serve 后从 endpoints 列表消失 - ✅ Step 5: 代码含指数退避重连逻辑 ## Ref - RFC: #210 - Testing: #211
xingyue added 1 commit 2026-05-12 06:22:11 +00:00
- Add AgentSocket Durable Object (holds one WS per agent name)
- Add /ws/connect route with GATEWAY_SECRET auth
- Add ws-client.ts with auto-reconnect (exponential backoff 1s-30s)
- serve defaults to WS mode (no cloudflared needed)
- Keep --tunnel-url and --no-tunnel as fallback options
- Endpoints list merges KV heartbeat + DO WebSocket status

Testing: #211
xingyue force-pushed feat/210-ws-gateway-phase1 from 321b5431f4 to f8835e0016 2026-05-13 03:05:13 +00:00 Compare
xiaomo approved these changes 2026-05-13 03:06:37 +00:00
xiaomo left a comment
Owner

LGTM. Clean phase 1: proper null checks replacing non-null assertions, functional ws-client with exponential backoff, DurableObject class acceptable as CF platform requirement. Conventions followed.

LGTM. Clean phase 1: proper null checks replacing non-null assertions, functional ws-client with exponential backoff, DurableObject class acceptable as CF platform requirement. Conventions followed.
xingyue closed this pull request 2026-05-13 03:10:12 +00:00

Pull request closed

Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/workflow#212