mirror of
https://github.com/hanzoai/bot.git
synced 2026-08-06 23:13:37 +00:00
3.9 KiB
3.9 KiB
Hanzo Bot — Architecture & Context
Overview
Multi-channel AI messaging gateway (TypeScript ESM). Routes messages between 50+ messaging platforms and AI models/agents. Composable plugin architecture with WebSocket + HTTP server core.
Key Architecture Layers
- CLI (
src/cli/) — Command registry, arg parsing,bot gateway run,bot agent,bot channels - Gateway (
src/gateway/) — WebSocket + HTTP server, auth, billing, marketplace, channels - Channels (
src/channels/,src/discord/,src/slack/,src/telegram/, etc.) — Platform adapters - Agents (
src/agents/) — ACP-based agent spawning, model selection, auth profiles - Extensions (
extensions/) — 50+ channel/feature plugins as workspace packages
Gateway Server (src/gateway/)
server.impl.ts— Main initialization and lifecycleserver-http.ts— HTTP handler chain: /health → /auth → hooks → tools → Slack → plugins → /v1/responses → /v1/marketplace → /v1/chat/completions → canvas → control-ui → 404server-ws.ts— WebSocket connection mgmt for nodes/clientsserver-methods.ts— RPC method implementationsbilling/billing-gate.ts— Pre-request billing check (fail-closed in production)billing/usage-reporter.ts— Async usage reporting (batch 50, flush 5s, retry 3x)marketplace-http.ts— P2P inference marketplacebots-http.ts— org-scopedGET /v1/bots(list the caller org's runs) +POST /v1/bots/:runId/stop(own-key stop guard), authenticated at the pod boundarycoding-tasks-http.ts—POST /v1/coding-tasks: native coding-task runner (clone → agent → commit → push); sandbox isolation is fail-closed and OFF by default
Model System (src/agents/)
model-selection.ts— Provider/model parsing, alias resolution, allowlist matchingdefaults.ts—DEFAULT_PROVIDER = "hanzo",DEFAULT_MODEL = "claude-sonnet-4-6"- Tier-aware routing: free → claude-sonnet-4-6, paid → zen4-pro
- Auth profiles: multi-key round-robin with cooldown recovery
Configuration
- Zod schema in
src/config/zod-schema.ts - Primary:
~/.hanzo/bot/node.json(fallback:~/.bot/) - JSON5 parsing, hot-reload on file change
- Gateway config under
config.gateway.*, agents underconfig.agents.*
Plugin Model
type ChannelPlugin = {
id: string;
messaging: ChannelMessagingAdapter;
auth?: ChannelAuthAdapter;
// ... 10+ adapters
};
- Plugins in
extensions/*/with ownpackage.json - Runtime deps in
dependencies(noworkspace:*) - Loaded via
jitialias resolver
Testing
- Framework: Vitest (multiple configs: unit, gateway, e2e, live, extensions)
- Colocated
*.test.tsfiles - Coverage: 70% thresholds
- Workers: max 16, 2048MB heap each
pnpm test(vitest),pnpm test:coverage,pnpm test:live(real APIs)
Build
- pnpm + tsdown (bundle) + tsc (types) + oxfmt (format) + oxlint (lint)
- Node 22+, Bun supported for scripts/dev/tests
- Output:
dist/index.js, Docker:ghcr.io/hanzoai/bot:latest
Key Patterns
- Dual routing: inbound channels → agents, outbound agents → channels
- Billing gate: open/warn/fail-closed modes via
BILLING_GATE_MODEenv - Exec approval: Code execution gated behind interactive UI
- Config injection:
createDefaultDeps()pattern for CLI, plugin services for runtime - Channel adapters: Loose coupling via interface-based plugins
Billing Flow
- Pre-request:
checkBillingAllowance()→ Commerce API balance check (cached 60s) - Request: Route to LLM provider
- Post-request:
reportUsage()→ async queue → Commerce/api/v1/billing/usage - Commerce API:
COMMERCE_API_URL(default:commerce.hanzo.svc.cluster.local:8001)
Git Remotes
origin=ssh://github.com/hanzoai/bot(canonical, PRs here)upstream=git@github.com:openclaw/openclaw(the MIT project this forks; see NOTICE)