# wujun5/wb2api architecture

> Architecture diagram of the GitHub repository wujun5/wb2api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wujun5/wb2api
- Repository: https://github.com/wujun5/wb2api

## Overview

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: [`internal/pool/pool.go`](https://github.com/wujun5/wb2api/blob/master/internal/pool/pool.go) (Account pool), [`internal/upstream/client.go`](https://github.com/wujun5/wb2api/blob/master/internal/upstream/client.go) (Upstream client), [`internal/server/handler.go`](https://github.com/wujun5/wb2api/blob/master/internal/server/handler.go) (API handler), [`cmd/server/main.go`](https://github.com/wujun5/wb2api/blob/master/cmd/server/main.go) (Server bootstrap)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Gateway runtime"]
  node_server_entry["Server bootstrap<br/>[main.go]"]
  node_http_handler["API handler<br/>[handler.go]"]
  node_request_policy["Prompt and degradation<br/>[degrade.go]"]
  node_request_logging["Request logging<br/>[logging.go]"]
end

subgraph group_routing["Account routing"]
  node_pool["Account pool<br/>[pool.go]"]
  node_pool_selection["Weighted selection<br/>[pick.go]"]
  node_pool_health["Cooldown and breaker<br/>[cooldown.go]"]
  node_pool_account["Account state<br/>[entry.go]"]
  node_sticky["Sticky sessions<br/>[session.go]"]
end

subgraph group_upstream["CodeBuddy integration"]
  node_upstream_client["Upstream client<br/>[client.go]"]
  node_payload["Request transforms<br/>[payload.go]"]
  node_sse["SSE processing<br/>[sse.go]"]
  node_sanitize["Fingerprint sanitizing<br/>[sanitize.go]"]
end

subgraph group_operations["Account operations"]
  node_oauth_cli["OAuth device login<br/>[main.go]"]
  node_auth_files["Account credentials<br/>[auth.go]"]
  node_scheduler["Scheduled tasks<br/>[scheduler.go]"]
  node_travel_tasks["Buddy travel<br/>[travel.go]"]
  node_activity_cli["Activity trigger<br/>[main.go]"]
  node_signin_cli["Bulk check-in<br/>[main.go]"]
  node_credit_cli["Credit report<br/>[main.go]"]
end

subgraph group_state["State and persistence"]
  node_state_file[("Pool state file<br/>[persist.go]")]
  node_redis_store["Redis adapter<br/>[redisstore.go]"]
end

node_client(("OpenAI clients"))
node_codebuddy{{"CodeBuddy services"}}
node_redis[("Upstash Redis")]

node_client -->|"sends API requests"| node_http_handler
node_server_entry -->|"registers handler"| node_http_handler
node_server_entry -->|"constructs pool"| node_pool
node_server_entry -->|"configures client"| node_upstream_client
node_server_entry -->|"starts scheduler"| node_scheduler
node_server_entry -->|"starts router"| node_sticky
node_http_handler -->|"selects accounts"| node_pool
node_http_handler -->|"routes sessions"| node_sticky
node_http_handler -->|"rewrites requests"| node_request_policy
node_http_handler -->|"proxies chat"| node_upstream_client
node_http_handler -->|"logs requests"| node_request_logging
node_pool -->|"chooses account"| node_pool_selection
node_pool -->|"tracks failures"| node_pool_health
node_pool -->|"manages account state"| node_pool_account
node_pool -->|"persists state"| node_state_file
node_sticky -.->|"mirrors bindings"| node_redis_store
node_redis_store -->|"stores state"| node_redis
node_pool -.->|"mirrors pool state"| node_redis_store
node_upstream_client -->|"transforms payload"| node_payload
node_upstream_client -->|"sanitizes payload"| node_sanitize
node_upstream_client -->|"processes stream"| node_sse
node_upstream_client -->|"calls chat and RPC"| node_codebuddy
node_scheduler -->|"leases accounts"| node_pool
node_scheduler -->|"runs account tasks"| node_upstream_client
node_scheduler -->|"runs travel tasks"| node_travel_tasks
node_activity_cli -->|"triggers activity"| node_scheduler
node_oauth_cli -->|"requests device authorization"| node_codebuddy
node_oauth_cli -.->|"produces credentials"| node_auth_files
node_server_entry -->|"loads accounts"| node_auth_files
node_signin_cli -->|"reads credentials"| node_auth_files
node_signin_cli -->|"checks in accounts"| node_upstream_client
node_credit_cli -->|"queries billing"| node_codebuddy

click node_server_entry "https://github.com/wujun5/wb2api/blob/master/cmd/server/main.go"
click node_http_handler "https://github.com/wujun5/wb2api/blob/master/internal/server/handler.go"
click node_request_policy "https://github.com/wujun5/wb2api/blob/master/internal/server/degrade.go"
click node_request_logging "https://github.com/wujun5/wb2api/blob/master/internal/server/logging.go"
click node_pool "https://github.com/wujun5/wb2api/blob/master/internal/pool/pool.go"
click node_pool_selection "https://github.com/wujun5/wb2api/blob/master/internal/pool/pick.go"
click node_pool_health "https://github.com/wujun5/wb2api/blob/master/internal/pool/cooldown.go"
click node_pool_account "https://github.com/wujun5/wb2api/blob/master/internal/pool/entry.go"
click node_sticky "https://github.com/wujun5/wb2api/blob/master/internal/session/session.go"
click node_upstream_client "https://github.com/wujun5/wb2api/blob/master/internal/upstream/client.go"
click node_payload "https://github.com/wujun5/wb2api/blob/master/internal/upstream/payload.go"
click node_sse "https://github.com/wujun5/wb2api/blob/master/internal/upstream/sse.go"
click node_sanitize "https://github.com/wujun5/wb2api/blob/master/internal/upstream/sanitize.go"
click node_oauth_cli "https://github.com/wujun5/wb2api/blob/master/cmd/login/main.go"
click node_auth_files "https://github.com/wujun5/wb2api/blob/master/internal/auth/auth.go"
click node_scheduler "https://github.com/wujun5/wb2api/blob/master/internal/scheduler/scheduler.go"
click node_travel_tasks "https://github.com/wujun5/wb2api/blob/master/internal/scheduler/travel.go"
click node_activity_cli "https://github.com/wujun5/wb2api/blob/master/cmd/activity/main.go"
click node_signin_cli "https://github.com/wujun5/wb2api/blob/master/cmd/signin/main.go"
click node_credit_cli "https://github.com/wujun5/wb2api/blob/master/cmd/credit/main.go"
click node_state_file "https://github.com/wujun5/wb2api/blob/master/internal/pool/persist.go"
click node_redis_store "https://github.com/wujun5/wb2api/blob/master/internal/redisstore/redisstore.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server_entry,node_http_handler,node_request_policy,node_request_logging,node_client toneBlue
class node_pool,node_pool_selection,node_pool_health,node_pool_account,node_sticky,node_redis toneAmber
class node_upstream_client,node_payload,node_sse,node_sanitize toneMint
class node_oauth_cli,node_auth_files,node_scheduler,node_travel_tasks,node_activity_cli,node_signin_cli,node_credit_cli toneRose
class node_state_file,node_redis_store,node_codebuddy toneIndigo
```

## Components

### Gateway runtime

- [Server bootstrap](https://github.com/wujun5/wb2api/blob/master/cmd/server/main.go): `cmd/server/main.go`
- [API handler](https://github.com/wujun5/wb2api/blob/master/internal/server/handler.go): `internal/server/handler.go`
- [Prompt and degradation](https://github.com/wujun5/wb2api/blob/master/internal/server/degrade.go): `internal/server/degrade.go`
- [Request logging](https://github.com/wujun5/wb2api/blob/master/internal/server/logging.go): `internal/server/logging.go`

### Account routing

- [Account pool](https://github.com/wujun5/wb2api/blob/master/internal/pool/pool.go): `internal/pool/pool.go`
- [Weighted selection](https://github.com/wujun5/wb2api/blob/master/internal/pool/pick.go): `internal/pool/pick.go`
- [Cooldown and breaker](https://github.com/wujun5/wb2api/blob/master/internal/pool/cooldown.go): `internal/pool/cooldown.go`
- [Account state](https://github.com/wujun5/wb2api/blob/master/internal/pool/entry.go): `internal/pool/entry.go`
- [Sticky sessions](https://github.com/wujun5/wb2api/blob/master/internal/session/session.go): `internal/session/session.go`

### CodeBuddy integration

- [Upstream client](https://github.com/wujun5/wb2api/blob/master/internal/upstream/client.go): `internal/upstream/client.go`
- [Request transforms](https://github.com/wujun5/wb2api/blob/master/internal/upstream/payload.go): `internal/upstream/payload.go`
- [SSE processing](https://github.com/wujun5/wb2api/blob/master/internal/upstream/sse.go): `internal/upstream/sse.go`
- [Fingerprint sanitizing](https://github.com/wujun5/wb2api/blob/master/internal/upstream/sanitize.go): `internal/upstream/sanitize.go`

### Account operations

- [OAuth device login](https://github.com/wujun5/wb2api/blob/master/cmd/login/main.go): `cmd/login/main.go`
- [Account credentials](https://github.com/wujun5/wb2api/blob/master/internal/auth/auth.go): `internal/auth/auth.go`
- [Scheduled tasks](https://github.com/wujun5/wb2api/blob/master/internal/scheduler/scheduler.go): `internal/scheduler/scheduler.go`
- [Buddy travel](https://github.com/wujun5/wb2api/blob/master/internal/scheduler/travel.go): `internal/scheduler/travel.go`
- [Activity trigger](https://github.com/wujun5/wb2api/blob/master/cmd/activity/main.go): `cmd/activity/main.go`
- [Bulk check-in](https://github.com/wujun5/wb2api/blob/master/cmd/signin/main.go): `cmd/signin/main.go`
- [Credit report](https://github.com/wujun5/wb2api/blob/master/cmd/credit/main.go): `cmd/credit/main.go`

### State and persistence

- [Pool state file](https://github.com/wujun5/wb2api/blob/master/internal/pool/persist.go): `internal/pool/persist.go`
- [Redis adapter](https://github.com/wujun5/wb2api/blob/master/internal/redisstore/redisstore.go): `internal/redisstore/redisstore.go`

### Other components

- **OpenAI clients**
- **CodeBuddy services**
- **Upstash Redis**

## Connections

- OpenAI clients → API handler: sends API requests (evidence: `README.md`)
- Server bootstrap → API handler: registers handler (evidence: `cmd/server/main.go`)
- Server bootstrap → Account pool: constructs pool (evidence: `cmd/server/main.go`)
- Server bootstrap → Upstream client: configures client (evidence: `cmd/server/main.go`)
- Server bootstrap → Scheduled tasks: starts scheduler (evidence: `cmd/server/main.go`)
- Server bootstrap → Sticky sessions: starts router (evidence: `cmd/server/main.go`)
- API handler → Account pool: selects accounts (evidence: `README.md`)
- API handler → Sticky sessions: routes sessions (evidence: `README.md`)
- API handler → Prompt and degradation: rewrites requests (evidence: `README.md`)
- API handler → Upstream client: proxies chat (evidence: `README.md`)
- API handler → Request logging: logs requests (evidence: `README.md`)
- Account pool → Weighted selection: chooses account (evidence: `README.md`)
- Account pool → Cooldown and breaker: tracks failures (evidence: `README.md`)
- Account pool → Account state: manages account state (evidence: `README.md`)
- Account pool → Pool state file: persists state (evidence: `README.md`)
- Sticky sessions → Redis adapter: mirrors bindings (evidence: `README.md`)
- Redis adapter → Upstash Redis: stores state (evidence: `README.md`)
- Account pool → Redis adapter: mirrors pool state (evidence: `README.md`)
- Upstream client → Request transforms: transforms payload (evidence: `README.md`)
- Upstream client → Fingerprint sanitizing: sanitizes payload (evidence: `README.md`)
- Upstream client → SSE processing: processes stream (evidence: `README.md`)
- Upstream client → CodeBuddy services: calls chat and RPC (evidence: `README.md`)
- Scheduled tasks → Account pool: leases accounts (evidence: `README.md`)
- Scheduled tasks → Upstream client: runs account tasks
- Scheduled tasks → Buddy travel: runs travel tasks (evidence: `README.md`)
- Activity trigger → Scheduled tasks: triggers activity (evidence: `cmd/activity/main.go`)
- OAuth device login → CodeBuddy services: requests device authorization (evidence: `cmd/login/main.go`)
- OAuth device login → Account credentials: produces credentials (evidence: `README.md`)
- Server bootstrap → Account credentials: loads accounts (evidence: `cmd/server/main.go`)
- Bulk check-in → Account credentials: reads credentials (evidence: `cmd/signin/main.go`)
- Bulk check-in → Upstream client: checks in accounts (evidence: `cmd/signin/main.go`)
- Credit report → CodeBuddy services: queries billing (evidence: `cmd/credit/main.go`)
