# linguo2625469/workbuddy2api-panel architecture

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

- Interactive diagram: https://gitdiagram.com/linguo2625469/workbuddy2api-panel
- Repository: https://github.com/linguo2625469/workbuddy2api-panel

## Overview

WorkBuddy2API is a self-hosted gateway that turns CodeBuddy accounts into an OpenAI-compatible chat API, with an operational web panel. The main request path authenticates a client, derives session affinity, selects an eligible account, forwards the request upstream, and returns streamed or aggregated output; pool health and usage are tracked alongside it. Separate capabilities include browser OAuth, scheduled account maintenance, growth-task automation, and live panel configuration. The graph follows documented behavior and shown wiring; some internal calls in unsampled modules are omitted rather than inferred.

Main entry points: [`internal/server/handler.go`](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/server/handler.go) (OpenAI API), [`internal/panel/panel.go`](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/panel.go) (Panel API), [`internal/upstream/client.go`](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/client.go) (CodeBuddy Client), [`internal/pool/pick.go`](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/pool/pick.go) (Account Pool)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["API Gateway"]
  node_handler["OpenAI API<br/>[handler.go]"]
  node_session["Session Affinity<br/>[session.go]"]
  node_prompt["Prompt Rewriting<br/>[prompt.go]"]
  node_requestlog[("Request Metrics<br/>[reqlog.go]")]
end

subgraph group_accounts["Account Operations"]
  node_pool["Account Pool<br/>[pick.go]"]
  node_poolstate["Pool Health<br/>[state.go]"]
  node_auth["Account Credentials<br/>[auth.go]"]
  node_scheduler["Scheduled Jobs<br/>[scheduler.go]"]
end

subgraph group_panel["Web Management"]
  node_panelui["Management Panel<br/>[index.go]"]
  node_panelapi["Panel API<br/>[panel.go]"]
  node_autotask["Task Automation<br/>[autotask.go]"]
  node_paneltasks["Task Controls<br/>[tasks.go]"]
  node_oauth["Browser Login<br/>[login.go]"]
end

subgraph group_integrations["State and Upstream"]
  node_upstream["CodeBuddy Client<br/>[client.go]"]
  node_sse["Stream Handling<br/>[sse.go]"]
  node_taskapi["Growth Task API<br/>[tasks.go]"]
  node_desktop["Task Event Chains<br/>[desktop.go]"]
  node_redis[("Redis Mirror<br/>[redisstore.go]")]
end

node_client(("API Client"))
node_admin(("Operator"))
node_codebuddy{{"CodeBuddy Services"}}

node_client -->|"sends requests"| node_handler
node_handler -->|"resolves affinity"| node_session
node_handler -->|"selects account"| node_pool
node_handler -->|"forwards chat"| node_upstream
node_handler -->|"rewrites prompts"| node_prompt
node_upstream -->|"calls API"| node_codebuddy
node_upstream -->|"handles streams"| node_sse
node_handler -->|"records metrics"| node_requestlog
node_session -->|"mirrors bindings"| node_redis
node_pool -->|"uses credentials"| node_auth
node_handler -->|"updates health"| node_poolstate
node_admin -->|"operates"| node_panelui
node_panelui -->|"calls panel"| node_panelapi
node_panelapi -->|"starts login"| node_oauth
node_oauth -->|"authorizes account"| node_codebuddy
node_panelapi -->|"manages accounts"| node_pool
node_panelapi -->|"runs tasks"| node_autotask
node_paneltasks -->|"lists and claims"| node_taskapi
node_autotask -->|"automates tasks"| node_taskapi
node_taskapi -->|"calls task endpoints"| node_codebuddy
node_autotask -->|"uses event chains"| node_desktop
node_desktop -->|"reports events"| node_codebuddy
node_scheduler -->|"checks accounts"| node_pool
node_scheduler -->|"runs maintenance"| node_upstream
node_scheduler -->|"runs task flows"| node_taskapi
node_panelapi -->|"shows request records"| node_requestlog

click node_handler "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/server/handler.go"
click node_session "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/session/session.go"
click node_pool "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/pool/pick.go"
click node_poolstate "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/pool/state.go"
click node_auth "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/auth/auth.go"
click node_upstream "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/client.go"
click node_sse "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/sse.go"
click node_prompt "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/prompt/prompt.go"
click node_scheduler "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/scheduler/scheduler.go"
click node_taskapi "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/tasks.go"
click node_desktop "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/desktop.go"
click node_panelui "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/index.go"
click node_panelapi "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/panel.go"
click node_autotask "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/autotask.go"
click node_paneltasks "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/tasks.go"
click node_oauth "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/login.go"
click node_redis "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/redisstore/redisstore.go"
click node_requestlog "https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/reqlog/reqlog.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_handler,node_session,node_prompt,node_requestlog,node_client toneBlue
class node_pool,node_poolstate,node_auth,node_scheduler toneAmber
class node_panelui,node_panelapi,node_autotask,node_paneltasks,node_oauth toneMint
class node_upstream,node_sse,node_taskapi,node_desktop,node_redis toneRose
class node_admin,node_codebuddy toneIndigo
```

## Components

### API Gateway

- [OpenAI API](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/server/handler.go): `internal/server/handler.go`
- [Session Affinity](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/session/session.go): `internal/session/session.go`
- [Prompt Rewriting](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/prompt/prompt.go): `internal/prompt/prompt.go`
- [Request Metrics](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/reqlog/reqlog.go): `internal/reqlog/reqlog.go`

### Account Operations

- [Account Pool](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/pool/pick.go): `internal/pool/pick.go`
- [Pool Health](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/pool/state.go): `internal/pool/state.go`
- [Account Credentials](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/auth/auth.go): `internal/auth/auth.go`
- [Scheduled Jobs](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/scheduler/scheduler.go): `internal/scheduler/scheduler.go`

### Web Management

- [Management Panel](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/index.go): `internal/panel/index.go`
- [Panel API](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/panel.go): `internal/panel/panel.go`
- [Task Automation](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/autotask.go): `internal/panel/autotask.go`
- [Task Controls](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/tasks.go): `internal/panel/tasks.go`
- [Browser Login](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/panel/login.go): `internal/panel/login.go`

### State and Upstream

- [CodeBuddy Client](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/client.go): `internal/upstream/client.go`
- [Stream Handling](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/sse.go): `internal/upstream/sse.go`
- [Growth Task API](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/tasks.go): `internal/upstream/tasks.go`
- [Task Event Chains](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/upstream/desktop.go): `internal/upstream/desktop.go`
- [Redis Mirror](https://github.com/linguo2625469/workbuddy2api-panel/blob/main/internal/redisstore/redisstore.go): `internal/redisstore/redisstore.go`

### Other components

- **API Client**
- **Operator**
- **CodeBuddy Services**

## Connections

- API Client → OpenAI API: sends requests
- OpenAI API → Session Affinity: resolves affinity (evidence: `internal/server/handler.go`)
- OpenAI API → Account Pool: selects account
- OpenAI API → CodeBuddy Client: forwards chat (evidence: `internal/server/handler.go`)
- OpenAI API → Prompt Rewriting: rewrites prompts (evidence: `internal/server/handler.go`)
- CodeBuddy Client → CodeBuddy Services: calls API
- CodeBuddy Client → Stream Handling: handles streams
- OpenAI API → Request Metrics: records metrics
- Session Affinity → Redis Mirror: mirrors bindings (evidence: `internal/session/session.go`)
- Account Pool → Account Credentials: uses credentials
- OpenAI API → Pool Health: updates health
- Operator → Management Panel: operates
- Management Panel → Panel API: calls panel
- Panel API → Browser Login: starts login
- Browser Login → CodeBuddy Services: authorizes account
- Panel API → Account Pool: manages accounts
- Panel API → Task Automation: runs tasks
- Task Controls → Growth Task API: lists and claims (evidence: `internal/panel/tasks.go`)
- Task Automation → Growth Task API: automates tasks (evidence: `internal/panel/autotask.go`)
- Growth Task API → CodeBuddy Services: calls task endpoints (evidence: `internal/upstream/tasks.go`)
- Task Automation → Task Event Chains: uses event chains
- Task Event Chains → CodeBuddy Services: reports events
- Scheduled Jobs → Account Pool: checks accounts (evidence: `internal/scheduler/scheduler.go`)
- Scheduled Jobs → CodeBuddy Client: runs maintenance (evidence: `internal/scheduler/scheduler.go`)
- Scheduled Jobs → Growth Task API: runs task flows
- Panel API → Request Metrics: shows request records
