hkcc 0.1.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of hkcc might be problematic. Click here for more details.

package/README.md DELETED
@@ -1,773 +0,0 @@
1
- # Claude OAuth Proxy
2
-
3
- A proxy server providing Claude API access through two simultaneous modes:
4
-
5
- - **SDK Mode (`/sdk/*`)** - Uses locally installed Claude Code CLI via Agent SDK
6
- - **OAuth Mode (`/api/*`)** - Direct API calls with OAuth tokens
7
-
8
- Each mode supports both Anthropic and OpenAI-compatible formats:
9
-
10
- - `/sdk/anthropic/*` or `/api/anthropic/*` - Anthropic Messages API format
11
- - `/sdk/openai/*` or `/api/openai/*` - OpenAI Chat Completions API format
12
-
13
- Both modes run simultaneously. Choose based on your setup.
14
-
15
- ## Architecture
16
-
17
- ```mermaid
18
- graph LR
19
- Client[Client<br/>SDKs, Apps, CLI] --> Proxy[HKCC Proxy Server]
20
-
21
- Proxy --> SDK[SDK Mode<br/>/sdk/*]
22
- Proxy --> OAuth[OAuth Mode<br/>/api/*]
23
-
24
- SDK --> CLI[Claude Code CLI] --> API[Claude API]
25
- OAuth --> Transform[Transform Request<br/>+ OAuth Token<br/>+ Magic Headers] --> API
26
-
27
- Proxy --> DB[(Database<br/>Users & Credentials)]
28
-
29
- style SDK fill:#e3f2fd
30
- style OAuth fill:#fff3e0
31
- style DB fill:#e8f5e9
32
- ```
33
-
34
- **Two Modes:**
35
-
36
- - **SDK Mode** - Routes through locally installed Claude Code CLI (full features)
37
- - **OAuth Mode** - Direct API calls with OAuth tokens (lightweight, no CLI needed)
38
-
39
- **Multi-User Support:** Session cookies and per-user API keys with database-backed credential storage
40
-
41
- > **⚠️ Runtime Requirement**
42
- > This package **MUST be run with Bun** (>=1.0.0). It uses `bun:sqlite` under the hood for the database layer, which is not compatible with Node.js.
43
-
44
- See [TECHNICAL.md](TECHNICAL.md) for detailed architecture diagrams and implementation details.
45
-
46
- ## Quick Start
47
-
48
- ```bash
49
- bun install
50
- bun dev
51
- ```
52
-
53
- Server starts at `http://127.0.0.1:3000`
54
-
55
- ## Build
56
-
57
- Build both server and frontend dashboard:
58
-
59
- ```bash
60
- bun run build.ts
61
- ```
62
-
63
- This creates:
64
-
65
- ```
66
- dist/
67
- ├── index.js # Bundled server
68
- └── public/ # Frontend dashboard
69
- ├── index.css
70
- ├── index.html
71
- └── index.js
72
- ```
73
-
74
- Run the built server:
75
-
76
- ```bash
77
- bun run dist/index.js
78
- ```
79
-
80
- ## CLI
81
-
82
- HKCC can be run as a CLI tool via `npx` or `bunx`, perfect for quick local usage without Docker.
83
-
84
- ### Installation
85
-
86
- > **⚠️ Bun Required**: Even when using `npx`, Bun must be installed on your system because the bundled CLI uses `bun:sqlite` internally.
87
-
88
- ```bash
89
- # Via bunx (recommended - uses Bun directly)
90
- bunx hkcc --help
91
-
92
- # Via npx (Bun must be installed on system)
93
- npx hkcc --help
94
-
95
- # Or run directly from the repo
96
- bun run bin/hkcc.ts --help
97
- ```
98
-
99
- ### Usage Examples
100
-
101
- **Local SQLite (simplest setup):**
102
-
103
- ```bash
104
- # Run with local SQLite database in current directory
105
- bunx hkcc --sqlite-path ./data/hkcc.db --db-driver bun-sqlite
106
-
107
- # Or run from anywhere
108
- bunx hkcc --sqlite-path ~/hkcc-data/db.sqlite --port 8080
109
- ```
110
-
111
- **Turso Cloud Database:**
112
-
113
- ```bash
114
- bunx hkcc \
115
- --db-driver turso \
116
- --turso-url libsql://your-db.turso.io \
117
- --turso-token your-auth-token \
118
- --port 3000
119
- ```
120
-
121
- **CLI Arguments:**
122
-
123
- | Argument | Alias | Description | Default |
124
- | --------------- | ----- | -------------------------------------------- | ------------------------------ |
125
- | `--port` | `-p` | Server port | `3000` or `PORT` env var |
126
- | `--host` | `-h` | Server host | `127.0.0.1` or `HOST` env var |
127
- | `--db-driver` | | Database driver (`turso` or `bun-sqlite`) | Auto-detect |
128
- | `--turso-url` | | Turso connection URL | `TURSO_CONNECTION_URL` env var |
129
- | `--turso-token` | | Turso auth token | `TURSO_AUTH_TOKEN` env var |
130
- | `--sqlite-path` | | SQLite database path (relative to CWD) | `./data/hkcc.db` |
131
- | `--claude-path` | | Path to Claude Code CLI executable | Auto-detect |
132
- | `--production` | | Enable production mode (static file serving) | Enabled |
133
-
134
- **Key Features:**
135
-
136
- - Runs from any directory - database path is relative to CWD
137
- - Static files (dashboard CSS/JS) served correctly regardless of where you run it
138
- - Migrations run automatically from the package location
139
- - All auth modules share the same database instance
140
-
141
- **CLI vs Server Mode:**
142
-
143
- | Feature | CLI (`bunx hkcc`) | Server (`bun run src/index.ts`) |
144
- | ------------ | -------------------------- | --------------------------------- |
145
- | Entry Point | `bin/hkcc.ts` | `src/index.ts` |
146
- | Args | CLI arguments | Environment variables |
147
- | CWD | Database relative to CWD | Database relative to project root |
148
- | Use Case | Quick start, any directory | Development in project |
149
- | Static Files | Production mode | Dev mode (hot reload) |
150
-
151
- ## Dashboard
152
-
153
- The server hosts a web dashboard at the root URL (`/`) for managing OAuth credentials and viewing server status. The dashboard is built with React and Tailwind CSS.
154
-
155
- ## Docker
156
-
157
- All images require database configuration via environment variables.
158
-
159
- ```bash
160
- # Default (with Claude Code CLI, bundled JS)
161
- docker build -f Dockerfile -t hkcc:latest .
162
- docker run -p 3000:3000 \
163
- -e TURSO_CONNECTION_URL=libsql://your-db.turso.io \
164
- -e TURSO_AUTH_TOKEN=your-token \
165
- -v hkcc-claude:/home/claude/.claude \
166
- hkcc:latest
167
-
168
- # OAuth-only (smaller image, bundled JS)
169
- docker build -f Dockerfile.oauth -t hkcc:oauth .
170
- docker run -p 3000:3000 \
171
- -e TURSO_CONNECTION_URL=libsql://your-db.turso.io \
172
- -e TURSO_AUTH_TOKEN=your-token \
173
- hkcc:oauth
174
-
175
- # Compiled binary (no Bun runtime needed, with Claude Code CLI)
176
- docker build -f Dockerfile.compiled -t hkcc:compiled .
177
- docker run -p 3000:3000 \
178
- -e TURSO_CONNECTION_URL=libsql://your-db.turso.io \
179
- -e TURSO_AUTH_TOKEN=your-token \
180
- -v hkcc-claude:/home/claude/.claude \
181
- hkcc:compiled
182
-
183
- # Compiled binary (no Bun runtime, OAuth-only, smallest)
184
- docker build -f Dockerfile.compiled-oauth -t hkcc:compiled-oauth .
185
- docker run -p 3000:3000 \
186
- -e TURSO_CONNECTION_URL=libsql://your-db.turso.io \
187
- -e TURSO_AUTH_TOKEN=your-token \
188
- hkcc:compiled-oauth
189
- ```
190
-
191
- **Volume mounts:**
192
-
193
- - **SDK mode images** (`Dockerfile`, `Dockerfile.compiled`): Volume at `/home/claude/.claude`
194
- - Stores Claude Code CLI authentication for SDK mode
195
- - **OAuth-only images** (`Dockerfile.oauth`, `Dockerfile.compiled-oauth`): No volume needed
196
- - All credentials stored in database
197
-
198
- ### Multi-Platform Builds (buildx)
199
-
200
- Build images for both ARM64 (Apple Silicon) and AMD64 (Intel/AMD) architectures:
201
-
202
- ```bash
203
- # Create and use buildx builder (one-time setup)
204
- docker buildx create --use
205
-
206
- # Build for multiple platforms and push to registry
207
- docker buildx build --platform linux/amd64,linux/arm64 \
208
- -t your-registry/hkcc:latest \
209
- -f Dockerfile \
210
- --push .
211
-
212
- # Build for multiple platforms (load local - only works for single arch)
213
- docker buildx build --platform linux/amd64,linux/arm64 \
214
- -t hkcc:latest \
215
- -f Dockerfile \
216
- --output type=local,dest=./output .
217
- ```
218
-
219
- **Note**: All Dockerfiles now support Docker buildx for multi-platform builds. The compiled variants automatically detect the target platform and compile the appropriate binary.
220
-
221
- ### Docker Image Variants
222
-
223
- | Dockerfile | Claude Code CLI | Runtime | Base OS | Size | Best For |
224
- | --------------------------- | --------------- | ------------- | ------- | ----- | ----------------------------------------- |
225
- | `Dockerfile` | Yes | Bun JS | Alpine | 327MB | Development, hot reload, full features |
226
- | `Dockerfile.oauth` | No | Bun JS | Alpine | 107MB | OAuth-only deployments, smaller footprint |
227
- | `Dockerfile.compiled` | Yes | Native binary | Debian | 425MB | Production with SDK mode, faster startup |
228
- | `Dockerfile.compiled-oauth` | No | Native binary | Debian | 204MB | Production OAuth-only, smallest image |
229
-
230
- **Key Differences:**
231
-
232
- - **Bundled JS (`.js`)**: Requires Bun runtime at runtime. Larger base image but faster builds.
233
- - **Native Binary**: Pre-compiled executable. No Bun runtime needed, faster cold start, slower builds.
234
- - **Claude Code CLI**: Required for SDK mode (`/sdk/*`). Adds ~220MB to image size.
235
- - **Alpine vs Debian**: Alpine is smaller but uses musl libc. Debian uses glibc, needed for Claude Code CLI.
236
-
237
- ### Docker Compose
238
-
239
- For easier deployment and configuration management, use Docker Compose. Create a `docker-compose.yml` file:
240
-
241
- **Basic setup with Turso cloud database (recommended for production):**
242
-
243
- ```yaml
244
- version: "3.8"
245
-
246
- services:
247
- hkcc:
248
- image: huakunshen/hkcc:latest # or build locally: build: .
249
- container_name: hkcc-proxy
250
- restart: unless-stopped
251
- ports:
252
- - "3000:3000"
253
- environment:
254
- # Server configuration
255
- - HOST=0.0.0.0
256
- - PORT=3000
257
-
258
- # Database - Turso (cloud)
259
- - TURSO_CONNECTION_URL=libsql://your-db.turso.io
260
- - TURSO_AUTH_TOKEN=your-auth-token
261
-
262
- # Optional: Disable registration after creating accounts
263
- # - HKCC_REGISTRATION_ENABLED=false
264
- volumes:
265
- # Persist Claude Code CLI authentication (SDK mode only)
266
- - hkcc-claude:/home/claude/.claude
267
-
268
- volumes:
269
- hkcc-claude:
270
- ```
271
-
272
- **OAuth-only deployment (smaller image, no SDK mode):**
273
-
274
- ```yaml
275
- version: "3.8"
276
-
277
- services:
278
- hkcc:
279
- image: huakunshen/hkcc:oauth # Uses OAuth-only image (smaller)
280
- container_name: hkcc-oauth-proxy
281
- restart: unless-stopped
282
- ports:
283
- - "3000:3000"
284
- environment:
285
- - HOST=0.0.0.0
286
- - PORT=3000
287
- - TURSO_CONNECTION_URL=libsql://your-db.turso.io
288
- - TURSO_AUTH_TOKEN=your-auth-token
289
- ```
290
-
291
- **Using local SQLite database (no external database needed):**
292
-
293
- ```yaml
294
- version: "3.8"
295
-
296
- services:
297
- hkcc:
298
- image: huakunshen/hkcc:latest
299
- container_name: hkcc-proxy
300
- restart: unless-stopped
301
- ports:
302
- - "3000:3000"
303
- environment:
304
- - HOST=0.0.0.0
305
- - PORT=3000
306
- # No TURSO variables = uses local SQLite at ./data/hkcc.db
307
- # Optional: Custom database path
308
- # - SQLITE_DATABASE_PATH=/app/data/hkcc.db
309
- volumes:
310
- # Persist local SQLite database
311
- - hkcc-data:/app/data
312
- # Persist Claude Code CLI authentication (SDK mode)
313
- - hkcc-claude:/home/claude/.claude
314
-
315
- volumes:
316
- hkcc-data:
317
- hkcc-claude:
318
- ```
319
-
320
- **Compiled binary with faster startup (production-optimized):**
321
-
322
- ```yaml
323
- version: "3.8"
324
-
325
- services:
326
- hkcc:
327
- image: huakunshen/hkcc:compiled # Native binary, no Bun runtime
328
- container_name: hkcc-compiled
329
- restart: unless-stopped
330
- ports:
331
- - "3000:3000"
332
- environment:
333
- - HOST=0.0.0.0
334
- - PORT=3000
335
- - TURSO_CONNECTION_URL=libsql://your-db.turso.io
336
- - TURSO_AUTH_TOKEN=your-auth-token
337
- volumes:
338
- - hkcc-claude:/home/claude/.claude
339
-
340
- volumes:
341
- hkcc-claude:
342
- ```
343
-
344
- **Using environment file for secrets:**
345
-
346
- Create a `.env` file (git-ignored):
347
-
348
- ```bash
349
- # .env
350
- TURSO_CONNECTION_URL=libsql://your-db.turso.io
351
- TURSO_AUTH_TOKEN=your-auth-token
352
- HKCC_REGISTRATION_ENABLED=false
353
- ```
354
-
355
- Update `docker-compose.yml` to use it:
356
-
357
- ```yaml
358
- services:
359
- hkcc:
360
- image: huakunshen/hkcc:latest
361
- container_name: hkcc-proxy
362
- restart: unless-stopped
363
- ports:
364
- - "3000:3000"
365
- env_file:
366
- - .env
367
- volumes:
368
- - hkcc-claude:/home/claude/.claude
369
-
370
- volumes:
371
- hkcc-claude:
372
- ```
373
-
374
- **Deploy with Docker Compose:**
375
-
376
- ```bash
377
- # Start the service
378
- docker compose up -d
379
-
380
- # View logs
381
- docker compose logs -f
382
-
383
- # Stop the service
384
- docker compose down
385
-
386
- # Stop and remove volumes (WARNING: deletes data)
387
- docker compose down -v
388
-
389
- # Rebuild with local changes
390
- docker compose up -d --build
391
- ```
392
-
393
- **Key considerations:**
394
-
395
- | Image Variant | SDK Mode | OAuth Mode | Volume Needed | Best Use Case |
396
- | --------------------- | -------- | ---------- | ---------------------- | -------------------------- |
397
- | `hkcc:latest` | ✅ | ✅ | `/home/claude/.claude` | Full features, development |
398
- | `hkcc:oauth` | ❌ | ✅ | ❌ | OAuth-only, smaller image |
399
- | `hkcc:compiled` | ✅ | ✅ | `/home/claude/.claude` | Production, faster startup |
400
- | `hkcc:compiled-oauth` | ❌ | ✅ | ❌ | OAuth-only, production |
401
-
402
- **Database options:**
403
-
404
- - **Turso (cloud)**: Set `TURSO_CONNECTION_URL` and `TURSO_AUTH_TOKEN` - Best for production, multi-instance setups
405
- - **Local SQLite**: Don't set Turso variables - Automatically uses `/app/data/hkcc.db` - Best for single-instance, development
406
-
407
- ## API Endpoints
408
-
409
- ### Health & Status
410
-
411
- ```bash
412
- curl http://127.0.0.1:3000/health
413
- curl http://127.0.0.1:3000/oauth/status
414
- ```
415
-
416
- ### SDK Mode (`/sdk/*`) - Requires Claude Code CLI
417
-
418
- ```bash
419
- # Anthropic Messages API
420
- curl -X POST http://127.0.0.1:3000/sdk/anthropic/v1/messages \
421
- -H "Content-Type: application/json" \
422
- -d '{"model":"claude-sonnet-4-5-20250929","max_tokens":1024,"messages":[{"role":"user","content":"Hello!"}]}'
423
-
424
- # OpenAI Chat Completions API
425
- curl -X POST http://127.0.0.1:3000/sdk/openai/v1/chat/completions \
426
- -H "Content-Type: application/json" \
427
- -d '{"model":"gpt-4","messages":[{"role":"user","content":"Hello!"}]}'
428
- ```
429
-
430
- ### OAuth Mode (`/api/*`) - Requires OAuth Authentication
431
-
432
- ```bash
433
- # Anthropic Messages API
434
- curl -X POST http://127.0.0.1:3000/api/anthropic/v1/messages \
435
- -H "Content-Type: application/json" \
436
- -d '{"model":"claude-sonnet-4-5-20250929","max_tokens":1024,"messages":[{"role":"user","content":"Hello!"}]}'
437
-
438
- # OpenAI Chat Completions API
439
- curl -X POST http://127.0.0.1:3000/api/openai/v1/chat/completions \
440
- -H "Content-Type: application/json" \
441
- -d '{"model":"gpt-4","messages":[{"role":"user","content":"Hello!"}]}'
442
- ```
443
-
444
- ### Model Mapping
445
-
446
- | OpenAI Model | Claude Model |
447
- | -------------------------------- | ---------------------------- |
448
- | `gpt-4`, `gpt-4-turbo`, `gpt-4o` | `claude-sonnet-4-5-20250929` |
449
- | `gpt-3.5-turbo` | `claude-haiku-3-5-20241022` |
450
-
451
- ## Database Setup (Required)
452
-
453
- The server stores user accounts, OAuth credentials, and API keys in a database. Both cloud (Turso) and local SQLite databases are supported.
454
-
455
- ### Auto-Detection (Recommended)
456
-
457
- The server automatically detects which database to use:
458
-
459
- - If `TURSO_CONNECTION_URL` and `TURSO_AUTH_TOKEN` are set → uses Turso (cloud)
460
- - Otherwise → uses local SQLite at `./data/hkcc.db`
461
-
462
- For new users, no configuration is needed - the server will automatically use local SQLite.
463
-
464
- ### Local SQLite (Default)
465
-
466
- ```bash
467
- # No configuration needed - uses ./data/hkcc.db by default
468
- bun dev
469
- ```
470
-
471
- To customize the database file location:
472
-
473
- ```bash
474
- SQLITE_DATABASE_PATH=./custom/path/db.sqlite
475
- ```
476
-
477
- ### Turso (Cloud Database)
478
-
479
- Set the database environment variables in `.env`:
480
-
481
- ```bash
482
- TURSO_CONNECTION_URL=libsql://your-db.turso.io
483
- TURSO_AUTH_TOKEN=your-auth-token
484
- ```
485
-
486
- ### Manual Override
487
-
488
- Override auto-detection with:
489
-
490
- ```bash
491
- DB_DRIVER=turso # Force Turso
492
- # or
493
- DB_DRIVER=bun-sqlite # Force local SQLite
494
- ```
495
-
496
- ### Migrations
497
-
498
- Migrations run automatically on server startup. For manual migration management:
499
-
500
- ```bash
501
- bun drizzle-kit push # Apply schema to database
502
- bun drizzle-kit studio # Open database GUI
503
- ```
504
-
505
- ## User Authentication
506
-
507
- ```bash
508
- # Register
509
- curl -X POST http://127.0.0.1:3000/auth/register \
510
- -H "Content-Type: application/json" \
511
- -d '{"email": "user@example.com", "password": "password123", "username": "myuser"}'
512
-
513
- # Login
514
- curl -X POST http://127.0.0.1:3000/auth/login \
515
- -H "Content-Type: application/json" \
516
- -d '{"email": "user@example.com", "password": "password123"}'
517
-
518
- # Get current user
519
- curl http://127.0.0.1:3000/auth/me -b "hkcc_session=SESSION_TOKEN"
520
-
521
- # Logout
522
- curl -X POST http://127.0.0.1:3000/auth/logout -b "hkcc_session=SESSION_TOKEN"
523
- ```
524
-
525
- Registration can be disabled by setting `HKCC_REGISTRATION_ENABLED=false` after creating your account.
526
-
527
- ## OAuth Authentication
528
-
529
- After logging in, authenticate with OAuth providers via the dashboard or API:
530
-
531
- ```bash
532
- # Get Anthropic authorization URL
533
- curl http://127.0.0.1:3000/oauth/anthropic/login?mode=max
534
-
535
- # Get OpenAI/Codex authorization URL
536
- curl http://127.0.0.1:3000/oauth/openai/login
537
-
538
- # Check OAuth status
539
- curl http://127.0.0.1:3000/oauth/anthropic/status
540
- curl http://127.0.0.1:3000/oauth/openai/status
541
- ```
542
-
543
- ### PKCE Authentication Flow
544
-
545
- The server uses OAuth 2.0 with PKCE (Proof Key for Code Exchange) for secure authentication:
546
-
547
- ```mermaid
548
- sequenceDiagram
549
- participant User
550
- participant Browser
551
- participant Proxy as HKCC Proxy
552
- participant Claude as claude.ai
553
-
554
- User->>Browser: Click "Login with Claude"
555
- Browser->>Proxy: GET /oauth/anthropic/login
556
- Proxy->>Proxy: Generate PKCE<br/>code_verifier<br/>code_challenge
557
- Proxy-->>Browser: Authorization URL + state
558
-
559
- Browser->>Claude: Redirect to authorization URL
560
- Claude->>User: Show consent screen
561
- User->>Claude: Approve access
562
- Claude-->>Browser: Redirect with auth code
563
-
564
- Browser->>Proxy: GET /oauth/anthropic/callback?code=xxx
565
- Proxy->>Claude: POST /oauth/token<br/>(code + verifier)
566
- Claude-->>Proxy: access_token + refresh_token
567
- Proxy->>Proxy: Store credentials in database
568
- Proxy-->>Browser: Redirect to dashboard
569
- Browser->>User: Authentication complete
570
- ```
571
-
572
- **Security features:**
573
-
574
- - PKCE prevents authorization code interception attacks
575
- - State parameter prevents CSRF attacks
576
- - Tokens stored securely in database with user isolation
577
- - Automatic token refresh before expiration
578
-
579
- ### API Access Modes
580
-
581
- | Mode | Authentication | Credentials Used |
582
- | ------------------- | ------------------------------- | --------------------------- |
583
- | Dashboard (browser) | Session cookie (`hkcc_session`) | User's database credentials |
584
- | Per-user API key | `hkcc_sk_xxx` in header | User's database credentials |
585
-
586
- ### Per-User API Keys
587
-
588
- Each registered user can create up to 5 personal API keys for external applications like Cherry Studio, Cursor, or custom scripts.
589
-
590
- **Create an API key** (via dashboard or API):
591
-
592
- ```bash
593
- # Create key via API (requires session cookie)
594
- curl -X POST http://127.0.0.1:3000/auth/api-keys \
595
- -H "Content-Type: application/json" \
596
- -b "hkcc_session=SESSION_TOKEN" \
597
- -d '{"name": "Cherry Studio"}'
598
-
599
- # Response includes the full key (shown only once):
600
- # {"success":true,"key":"hkcc_sk_a1b2c3d4e5f6...","apiKey":{"id":1,"keyPrefix":"hkcc_sk_a1b2...","name":"Cherry Studio"}}
601
- ```
602
-
603
- **Use the API key** in external clients:
604
-
605
- ```bash
606
- curl -X POST http://127.0.0.1:3000/api/anthropic/v1/messages \
607
- -H "x-api-key: hkcc_sk_a1b2c3d4e5f6..." \
608
- -H "Content-Type: application/json" \
609
- -d '{"model":"claude-sonnet-4-5-20250929","max_tokens":100,"messages":[{"role":"user","content":"Hello"}]}'
610
-
611
- # Or use Authorization header:
612
- curl -X POST http://127.0.0.1:3000/api/anthropic/v1/messages \
613
- -H "Authorization: Bearer hkcc_sk_a1b2c3d4e5f6..." \
614
- ...
615
- ```
616
-
617
- **Manage API keys**:
618
-
619
- ```bash
620
- # List keys
621
- curl http://127.0.0.1:3000/auth/api-keys -b "hkcc_session=SESSION_TOKEN"
622
-
623
- # Delete a key
624
- curl -X DELETE http://127.0.0.1:3000/auth/api-keys/1 -b "hkcc_session=SESSION_TOKEN"
625
- ```
626
-
627
- ### Token Refresh
628
-
629
- A cron job runs every hour to automatically refresh tokens expiring within 60 minutes. Claude access tokens have an 8-hour TTL (480 minutes).
630
-
631
- ## SDK Integration
632
-
633
- ### Vercel AI SDK
634
-
635
- ```typescript
636
- import { createAnthropic } from "@ai-sdk/anthropic";
637
- import { generateText } from "ai";
638
-
639
- const anthropic = createAnthropic({
640
- baseURL: "http://127.0.0.1:3000/api/anthropic", // OAuth mode
641
- apiKey: "hkcc_sk_your_personal_api_key", // Per-user API key
642
- });
643
-
644
- const { text } = await generateText({
645
- model: anthropic("claude-sonnet-4-5-20250929"),
646
- prompt: "Hello!",
647
- });
648
- ```
649
-
650
- ### Anthropic SDK
651
-
652
- ```typescript
653
- import Anthropic from "@anthropic-ai/sdk";
654
-
655
- const client = new Anthropic({
656
- baseURL: "http://127.0.0.1:3000/api/anthropic", // OAuth mode
657
- apiKey: "hkcc_sk_your_personal_api_key", // Per-user API key
658
- });
659
-
660
- const message = await client.messages.create({
661
- model: "claude-sonnet-4-5-20250929",
662
- max_tokens: 1024,
663
- messages: [{ role: "user", content: "Hello!" }],
664
- });
665
- ```
666
-
667
- ### OpenAI SDK
668
-
669
- ```typescript
670
- import OpenAI from "openai";
671
-
672
- const client = new OpenAI({
673
- baseURL: "http://127.0.0.1:3000/api/anthropic/openai/v1", // OAuth mode with OpenAI format
674
- apiKey: "hkcc_sk_your_personal_api_key", // Per-user API key
675
- });
676
-
677
- const completion = await client.chat.completions.create({
678
- model: "claude-sonnet-4-5-20250929",
679
- messages: [{ role: "user", content: "Hello!" }],
680
- });
681
- ```
682
-
683
- ## Environment Variables
684
-
685
- ```bash
686
- # Server Configuration
687
- PORT=3000 # Server port (default: 3000)
688
- HOST=127.0.0.1 # Server host (default: 127.0.0.1)
689
-
690
- # Database (Required)
691
- TURSO_CONNECTION_URL=libsql://your-db.turso.io
692
- TURSO_AUTH_TOKEN=your-auth-token
693
-
694
- # Registration Control
695
- HKCC_REGISTRATION_ENABLED=true # Set to "false" to disable new user registration
696
- ```
697
-
698
- ## Development
699
-
700
- ```bash
701
- bun run dev # Run with hot reload
702
- bun run build.ts # Build server and frontend
703
- bun test # Run tests
704
- bun run format # Format code
705
- ```
706
-
707
- ## Project Structure
708
-
709
- ```
710
- src/
711
- ├── index.ts # Server entry point (development)
712
- ├── app.ts # Elysia app factory
713
- ├── auth/ # OAuth & user authentication, API key management
714
- ├── db/ # Drizzle ORM schema and database connection
715
- ├── cron/ # Scheduled jobs (token refresh)
716
- ├── proxy/ # SDK and OAuth proxy implementations
717
- ├── routes/ # OAuth and auth endpoints
718
- ├── adapters/ # Request/response format adapters
719
- ├── transform/ # Request/response transformations
720
- ├── middleware/ # API key validation, error handling
721
- ├── mcp-bridge/ # MCP tool bridging for client tools
722
- └── utils/ # Utilities (Claude Code detection)
723
-
724
- bin/
725
- └── hkcc.ts # CLI entry point (npx/bunx)
726
-
727
- public/ # Frontend source (React + Tailwind)
728
- ├── index.tsx # React entry point
729
- └── pages/ # Dashboard pages
730
-
731
- build.ts # Build script for server, CLI, and frontend
732
- ```
733
-
734
- See [TECHNICAL.md](./TECHNICAL.md) for architecture details.
735
-
736
- ## Tech Stack
737
-
738
- - **Runtime**: Bun (>=1.0.0) - **Required** (uses `bun:sqlite`)
739
- - **Framework**: Elysia
740
- - **Frontend**: React, TanStack Router, Tailwind CSS
741
- - **Type-Safe Client**: Eden
742
- - **Database**: Turso (libSQL) with Drizzle ORM
743
- - **Validation**: Zod + Elysia's t.Object
744
-
745
- ## Docker Build
746
-
747
- ```bash
748
- # Build and push with timestamp tags for all variants
749
- DATE_TAG=$(date +%Y-%m-%d-%H-%M-%S)
750
-
751
- # Default (latest + oauth + compiled + compiled-oauth)
752
- docker buildx build --push --platform linux/amd64,linux/arm64 \
753
- -t huakunshen/hkcc:latest \
754
- -t huakunshen/hkcc:latest-${DATE_TAG} \
755
- -f Dockerfile .
756
-
757
- docker buildx build --push --platform linux/amd64,linux/arm64 \
758
- -t huakunshen/hkcc:oauth \
759
- -t huakunshen/hkcc:oauth-${DATE_TAG} \
760
- -f Dockerfile.oauth .
761
-
762
- docker buildx build --push --platform linux/amd64,linux/arm64 \
763
- -t huakunshen/hkcc:compiled \
764
- -t huakunshen/hkcc:compiled-${DATE_TAG} \
765
- -f Dockerfile.compiled .
766
-
767
- docker buildx build --push --platform linux/amd64,linux/arm64 \
768
- -t huakunshen/hkcc:compiled-oauth \
769
- -t huakunshen/hkcc:compiled-oauth-${DATE_TAG} \
770
- -f Dockerfile.compiled-oauth .
771
- ```
772
-
773
- **Note:** Each image is tagged with both the standard tag (e.g., `latest`, `oauth`) and a timestamped tag (e.g., `oauth-2026-01-29-21-03-17`) for version tracking. All images support both `linux/amd64` and `linux/arm64` platforms.