How wujun5/wb2api works
WorkBuddy2API is a self-hosted OpenAI-compatible gateway for Tencent CodeBuddy accounts. The primary workflow accepts chat requests, applies gateway authentication and prompt/request transformations, selects an account with pool and session-sticky policies, then proxies upstream SSE and returns streaming or locally aggregated output. Separate product branches handle OAuth account acquisition, scheduled account activity, health/status, and persistent pool/session state. Edges are limited to caller relationships visible in the supplied excerpts or README; some scheduler and handler internals are unsampled.
Main entry points: pool.go Account pool · client.go Upstream client · handler.go API handler · main.go Server bootstrap
Components 25
Gateway runtime
- Server bootstrap
cmd/server/main.go - API handler
internal/server/handler.go - Prompt and degradation
internal/server/degrade.go - Request logging
internal/server/logging.go
Account routing
- Account pool
internal/pool/pool.go - Weighted selection
internal/pool/pick.go - Cooldown and breaker
internal/pool/cooldown.go - Account state
internal/pool/entry.go - Sticky sessions
internal/session/session.go
CodeBuddy integration
- Upstream client
internal/upstream/client.go - Request transforms
internal/upstream/payload.go - SSE processing
internal/upstream/sse.go - Fingerprint sanitizing
internal/upstream/sanitize.go
Account operations
- OAuth device login
cmd/login/main.go - Account credentials
internal/auth/auth.go - Scheduled tasks
internal/scheduler/scheduler.go - Buddy travel
internal/scheduler/travel.go - Activity trigger
cmd/activity/main.go - Bulk check-in
cmd/signin/main.go - Credit report
cmd/credit/main.go
State and persistence
- Pool state file
internal/pool/persist.go - Redis adapter
internal/redisstore/redisstore.go
Other components
- OpenAI clients
- CodeBuddy services
- Upstash Redis
Connections 32
- OpenAI clients → API handler: sends API requests
README.md - Server bootstrap → API handler: registers handler
cmd/server/main.go - Server bootstrap → Account pool: constructs pool
cmd/server/main.go - Server bootstrap → Upstream client: configures client
cmd/server/main.go - Server bootstrap → Scheduled tasks: starts scheduler
cmd/server/main.go - Server bootstrap → Sticky sessions: starts router
cmd/server/main.go - API handler → Account pool: selects accounts
README.md - API handler → Sticky sessions: routes sessions
README.md - API handler → Prompt and degradation: rewrites requests
README.md - API handler → Upstream client: proxies chat
README.md - API handler → Request logging: logs requests
README.md - Account pool → Weighted selection: chooses account
README.md - Account pool → Cooldown and breaker: tracks failures
README.md - Account pool → Account state: manages account state
README.md - Account pool → Pool state file: persists state
README.md - Sticky sessions → Redis adapter: mirrors bindings
README.md - Redis adapter → Upstash Redis: stores state
README.md - Account pool → Redis adapter: mirrors pool state
README.md - Upstream client → Request transforms: transforms payload
README.md - Upstream client → Fingerprint sanitizing: sanitizes payload
README.md - Upstream client → SSE processing: processes stream
README.md - Upstream client → CodeBuddy services: calls chat and RPC
README.md - Scheduled tasks → Account pool: leases accounts
README.md - Scheduled tasks → Upstream client: runs account tasks
- Scheduled tasks → Buddy travel: runs travel tasks
README.md - Activity trigger → Scheduled tasks: triggers activity
cmd/activity/main.go - OAuth device login → CodeBuddy services: requests device authorization
cmd/login/main.go - OAuth device login → Account credentials: produces credentials
README.md - Server bootstrap → Account credentials: loads accounts
cmd/server/main.go - Bulk check-in → Account credentials: reads credentials
cmd/signin/main.go - Bulk check-in → Upstream client: checks in accounts
cmd/signin/main.go - Credit report → CodeBuddy services: queries billing
cmd/credit/main.go