logo
0
0
WeChat Login
feat(channelws): add bounded caches and event hooks

workbuddy

Go 版 WorkBuddy 消息网关,已接入真实 copilot.tencent.com 登录链、registerWorkspace 凭证获取、ACP HTTP transport 切换,以及 WeCom / Wechat KF 绑定与消息收发入口,并内置一个完整测试页面。

目标

  • 用 Go 重建 WorkBuddy 的核心消息网关
  • 抽离 HTTPAuthSessionConnectorMarketplaceACP-RPC 能力
  • 为后续实现 IPC bridge、会话编排、浏览器登录、模型路由留出稳定接口

当前包含

  • Config 默认配置与标准化
  • 通用上游 HTTP Client
  • 原生登录流骨架:
    • fetch auth state
    • open browser
    • poll token
    • fetch account
    • fetch accounts
    • store session
  • ConversationConnectorPluginSupport API client
  • ProductScenes 本地加载器骨架
  • WeCom / Wechat KF 通道桥接骨架
  • QQ Bot / WeCom AiBot 真长连 websocket 客户端
  • 可外部复用的 pkg/channelws 公共包
  • ACP-RPC 协议结构体
  • 内存 SessionStore
  • Auth 状态机
  • 服务层:AuthServiceConversationServiceWeComService

目录

workbuddy/ ├── cmd/workbuddy ├── docs ├── examples │ └── channelws ├── pkg │ └── channelws ├── internal │ ├── auth │ ├── browser │ ├── config │ ├── flow │ ├── gateway │ ├── protocol │ ├── session │ ├── bridge │ └── upstream ├── DESIGN.md ├── README.md ├── go.mod └── workbuddy.go

快速开始

cd /mnt/workbuddy go test ./... go run ./cmd/workbuddy go run ./cmd/workbuddy -listen :8080

可用 HTTP 入口

  • POST /api/v1/auth/login
  • POST /api/v1/auth/state
  • POST /api/v1/auth/exchange
  • GET /api/v1/auth/session
  • POST /api/v1/auth/refresh
  • POST /api/v1/auth/logout
  • GET /api/v1/scenes
  • GET|POST /api/v1/conversations
  • GET|PATCH|DELETE /api/v1/conversations/{id}
  • GET /api/v1/conversations/{id}/session
  • POST /api/v1/conversations/{id}/archive
  • GET /api/v1/connectors/user
  • PATCH|DELETE /api/v1/connectors/user/{name}/*
  • POST /api/v1/connectors/tasks
  • GET|PATCH /api/v1/connectors/tasks/{taskId}/*
  • GET|POST /api/v1/connectors/oauth/{provider}/*
  • GET /api/v1/plugins/installed
  • POST /api/v1/plugins/install
  • POST /api/v1/plugins/installed/{installId}/*
  • GET|POST /api/v1/marketplaces
  • POST /api/v1/marketplaces/{sourceId}/*
  • GET /api/v1/marketplaces/{sourceId}/plugins*
  • POST /api/v1/support/presigned-url
  • GET /api/v1/support/scenes
  • POST /api/v1/support/report
  • POST /api/v1/pending-input
  • GET /api/v1/pending-input/{id}
  • POST /api/v1/usage/personal
  • POST /api/v1/usage/enterprise
  • POST /api/v1/checkin/status
  • POST /api/v1/checkin/claim
  • POST /api/v1/backend/call
  • POST /api/v1/wecom/register-channel
  • GET /api/v1/wecom/runtime
  • POST /api/v1/wecom/runtime/host
  • POST /api/v1/wecom/runtime/workspace
  • POST /api/v1/wecom/runtime/bootstrap
  • POST /api/v1/wecom/runtime/connect
  • GET /api/v1/wecom/runtime/messages
  • GET /api/v1/wecom/session-id
  • POST /api/v1/wecom/channels/register
  • GET /api/v1/wecom/channels/status
  • GET /api/v1/wecom/channels/configs
  • GET /api/v1/wecom/channels/{type}/response
  • DELETE /api/v1/wecom/channels/{type}
  • POST /api/v1/wecom/ack
  • POST /api/v1/wecom/response
  • POST /api/v1/wecom/push
  • GET|POST /api/v1/wecom/auto-reply
  • POST /api/v1/wecom/wechatkf/link
  • GET /api/v1/wecom/wechatkf/bind-status
  • DELETE /api/v1/wecom/wechatkf/unbind
  • POST /api/v1/acp/config
  • POST /api/v1/acp/config/from-conversation
  • GET /api/v1/acp/state
  • POST /api/v1/acp/new-session
  • POST /api/v1/acp/set-session-model
  • POST /api/v1/acp/prompt
  • POST /api/v1/acp/execute-command

公共 websocket SDK

如果魔尊要在其他 Go 项目里直接复用 QQ BotWeCom AiBot 长连接客户端,不必再 import internal/*,可以直接使用:

  • cnb.cool/zishuo/workbuddy/pkg/channelws

模块文档与调用手册:

可运行外部示例:

  • go run ./examples/channelws -mode qq -qq-app-id <appId> -qq-app-secret <secret>
  • go run ./examples/channelws -mode aibot -aibot-id <botId> -aibot-secret <secret>

测试页面

启动后可直接访问:

http://127.0.0.1:8080/gateway-test

页面包含:

  • 登录 / 换取 session / 退出
  • ACP endpoint / mode 配置
  • hostId 设置与 workspace bootstrap
  • Wechat KF 绑定状态查询、二维码链接、解绑
  • QQ / WeCom AiBot 等渠道配置、绑定、解绑
  • 最近消息轮询
  • 对选中消息发送 ACK / Response
  • 主动推送消息
  • 自动 ACP 回复配置

默认持久化路径

  • sessionPath = ~/.workbuddy/gateway/session.json
  • runtimePath = ~/.workbuddy/gateway/wecom-runtime.json
  • autoReplyPath = ~/.workbuddy/gateway/auto-reply.json

CLI 可覆盖这些路径:

go run ./cmd/workbuddy \ -listen :8080 \ -session-path /data/workbuddy/session.json \ -runtime-path /data/workbuddy/wecom-runtime.json \ -auto-reply-path /data/workbuddy/auto-reply.json \ -acp-endpoint http://127.0.0.1:8090

真人登录与微信收消息最短链

  1. 启动服务:
cd /mnt/workbuddy go run ./cmd/workbuddy -listen :8080
  1. 取登录地址:
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/state

返回会包含 stateauthUrl。手动打开 authUrl 完成浏览器登录。

  1. 登录完成后换取 IDE session:
curl -sS -X POST http://127.0.0.1:8080/api/v1/auth/exchange \ -H 'Content-Type: application/json' \ -d '{"state":"<上一步返回的 state>"}'
  1. 设置本机 hostId
curl -sS -X POST http://127.0.0.1:8080/api/v1/wecom/runtime/host \ -H 'Content-Type: application/json' \ -d '{"hostId":"demo-host"}'
  1. 注册并连接 workspace。成功后返回的 credentials.url 就是类似 wss://www.codebuddy.cn/v2/agentos/localagent/workspaces/websocket 的真实长链接地址:
curl -sS -X POST http://127.0.0.1:8080/api/v1/wecom/runtime/bootstrap \ -H 'Content-Type: application/json' \ -d '{"workspaceId":"demo-workspace","workspaceName":"Demo Workspace"}'
  1. 获取客服微信绑定链接:
curl -sS -X POST http://127.0.0.1:8080/api/v1/wecom/wechatkf/link \ -H 'Content-Type: application/json' \ -d '{"hostId":"demo-host","workspace":"demo-workspace"}'
  1. 微信绑定完成后,从客服微信发送消息,再查看最近收到的消息:
curl -sS 'http://127.0.0.1:8080/api/v1/wecom/runtime/messages?limit=20'
  1. 如需查看或确认当前 sessionId:
curl -sS 'http://127.0.0.1:8080/api/v1/wecom/session-id?hostId=demo-host&workspace=demo-workspace'
  1. 如需主动推送:
curl -sS -X POST http://127.0.0.1:8080/api/v1/wecom/push \ -H 'Content-Type: application/json' \ -d '{"hostId":"demo-host","workspace":"demo-workspace","title":"WorkBuddy Gateway","message":"这是一条主动推送测试消息。","success":true,"statusCode":200}'
  1. 如需开启自动回复:
curl -sS -X POST http://127.0.0.1:8080/api/v1/wecom/auto-reply \ -H 'Content-Type: application/json' \ -d '{"enabled":true,"autoAck":true,"ackMessage":"已收到,正在处理。","title":"WorkBuddy Auto Reply","modelId":"deepseek-r1","systemPrompt":"你是客服回复助手"}'

参考资料

  • docs/upstream-http-openapi.yaml
  • docs/upstream-migration-report.md
  • docs/upstream-lifecycle-analysis.md
  • docs/gateway-integration-guide.md
  • docs/missing-interface-audit.md

已复核的运行时链路

  • 登录授权:main.log 已确认 fetch auth state -> open browser -> poll token -> get account -> get accounts -> sessionChanged
  • Product scenes:main.log 已确认 codebuddy:getProductScenes.workbuddy/plugins/marketplaces/cb_teams_marketplace/scenes.json 加载
  • WeCom / Wechat KF:main.log 已确认存在 getBindStatus -> sendAck -> sendResponse 的收发链,以及失败分支 400/403

当前边界

  • 这里只实现消息网关与协议骨架,不直接复刻 Electron/VS Code 运行时
  • IPC 与窗口编排尚未实现,只保留桥接接口与消息模型
  • 认证浏览器打开器使用接口抽象,默认提供 stdlib 实现
  • WeCom/Wechat KF 的真实 HTTP 和 workspace websocket 已接入,但要收到真实微信消息,仍需要真人完成浏览器登录与微信绑定
  • session 现已默认持久化到本地文件,重启后可恢复登录态
  • hostId 也会落盘,避免重启后微信绑定漂移
  • 主动推送已支持,但默认仍建议优先基于已绑定 chat 发送

已可直接集成的能力

  • 基于真实日志与逆向结果的原生登录流程编排
  • 会话列表、会话详情、会话归档/删除、session 获取
  • connector user/task/oauth 管理接口
  • plugin / marketplace / support / billing / pending input HTTP 能力
  • __backend__ HTTP 兼容入口
  • ProductScenes 本地加载能力
  • WeCom / Wechat KF 消息与回执结构建模
  • 主动推送消息
  • 自动 inbound -> ACP -> response 流水线

仍需继续实现的能力

  • Electron IPC bridge
  • AuthProvider.createSession() 之外的 GUI 回调承接
  • connector / plugin / support 等上游能力的对外 HTTP server 暴露
  • __backend__ RPC 命名空间与 IPC 仿真层