aicodeswitch 1.0.0 → 1.1.1
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.
- package/CLAUDE.md +182 -0
- package/LICENSE +671 -0
- package/README.md +13 -0
- package/bin/cli.js +35 -3
- package/bin/update.js +251 -0
- package/bin/version.js +97 -0
- package/dist/server/database.js +22 -11
- package/dist/server/proxy-server.js +134 -29
- package/dist/ui/assets/index-BJB9D8Y6.js +285 -0
- package/dist/ui/assets/index-CRLNbjRB.css +1 -0
- package/dist/ui/index.html +2 -2
- package/package.json +29 -3
- package/dist/ui/assets/index-BN77E7-U.js +0 -259
- package/dist/ui/assets/index-CaNSVfpD.css +0 -1
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
```
|
|
2
|
+
# CLAUDE.md
|
|
3
|
+
|
|
4
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
## AI Code Switch - Project Overview
|
|
8
|
+
|
|
9
|
+
AI Code Switch is a local proxy server that manages AI programming tool connections to large language models, allowing tools like Claude Code and Codex to use custom model APIs instead of official ones.
|
|
10
|
+
|
|
11
|
+
## Development Commands
|
|
12
|
+
|
|
13
|
+
### Installation
|
|
14
|
+
```bash
|
|
15
|
+
npm install
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Development
|
|
19
|
+
```bash
|
|
20
|
+
npm run dev # Run both UI and server in watch mode
|
|
21
|
+
npm run dev:ui # Run only React UI (Vite dev server)
|
|
22
|
+
npm run dev:server # Run only Node.js server (TSX watch)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Build
|
|
26
|
+
```bash
|
|
27
|
+
npm run build # Build both UI and server
|
|
28
|
+
npm run build:ui # Build React UI to dist/ui
|
|
29
|
+
npm run build:server # Build TypeScript server to dist/server
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Linting
|
|
33
|
+
```bash
|
|
34
|
+
npm run lint # Run ESLint on all .ts/.tsx files
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### CLI Commands
|
|
38
|
+
```bash
|
|
39
|
+
npm link # Link local package for CLI testing
|
|
40
|
+
aicos start # Start the proxy server
|
|
41
|
+
aicos stop # Stop the proxy server
|
|
42
|
+
aicos restart # Restart the proxy server
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Architecture
|
|
46
|
+
|
|
47
|
+
### High-Level Structure
|
|
48
|
+
```
|
|
49
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
50
|
+
│ AI Code Switch │
|
|
51
|
+
├─────────────────────────────────────────────────────────────┤
|
|
52
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
53
|
+
│ │ React UI │ │ Express API │ │ Proxy Core │ │
|
|
54
|
+
│ │ (Vite dev) │ │ (Node.js) │ │ │ │
|
|
55
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
56
|
+
│ │ │ │ │
|
|
57
|
+
│ └──────────────┼──────────────┘ │
|
|
58
|
+
│ ▼ │
|
|
59
|
+
│ ┌──────────────┐ │
|
|
60
|
+
│ │ Database │ │
|
|
61
|
+
│ │ (SQLite3) │ │
|
|
62
|
+
│ └──────────────┘ │
|
|
63
|
+
│ │ │
|
|
64
|
+
│ ▼ │
|
|
65
|
+
│ ┌──────────────┐ │
|
|
66
|
+
│ │ Transformers │ │
|
|
67
|
+
│ │ (Stream/SSE) │ │
|
|
68
|
+
│ └──────────────┘ │
|
|
69
|
+
│ │ │
|
|
70
|
+
│ ▼ │
|
|
71
|
+
│ ┌──────────────┐ │
|
|
72
|
+
│ │ Upstream │ │
|
|
73
|
+
│ │ APIs (LLMs) │ │
|
|
74
|
+
│ └──────────────┘ │
|
|
75
|
+
└─────────────────────────────────────────────────────────────┘
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Core Components
|
|
79
|
+
|
|
80
|
+
#### 1. Server (Node.js/Express) - `server/main.ts`
|
|
81
|
+
- Main entry point
|
|
82
|
+
- Configures Express with CORS, body parsing
|
|
83
|
+
- Reads configuration from `~/.aicodeswitch/aicodeswitch.conf`
|
|
84
|
+
- Sets up authentication middleware
|
|
85
|
+
- Registers all API routes
|
|
86
|
+
- Initializes database and proxy server
|
|
87
|
+
|
|
88
|
+
#### 2. Proxy Server - `server/proxy-server.ts`
|
|
89
|
+
- **Route Matching**: Finds active route based on target type (claude-code/codex)
|
|
90
|
+
- **Rule Matching**: Determines content type from request (image-understanding/thinking/long-context/background/default)
|
|
91
|
+
- **Request Transformation**: Converts between different API formats (Claude ↔ OpenAI ↔ OpenAI Responses)
|
|
92
|
+
- **Streaming**: Handles SSE (Server-Sent Events) streaming responses with real-time transformation
|
|
93
|
+
- **Logging**: Tracks requests, responses, and errors
|
|
94
|
+
|
|
95
|
+
#### 3. Transformers - `server/transformers/`
|
|
96
|
+
- **streaming.ts**: SSE parsing/serialization and event transformation
|
|
97
|
+
- **claude-openai.ts**: Claude ↔ OpenAI Chat format conversion
|
|
98
|
+
- **openai-responses.ts**: OpenAI Responses format conversion
|
|
99
|
+
- **chunk-collector.ts**: Collects streaming chunks for logging
|
|
100
|
+
|
|
101
|
+
#### 4. Database - `server/database.ts`
|
|
102
|
+
- SQLite3 database wrapper
|
|
103
|
+
- Manages: Vendors, API Services, Routes, Rules, Logs
|
|
104
|
+
- Configuration storage (API key, logging settings, etc.)
|
|
105
|
+
|
|
106
|
+
#### 5. UI (React) - `ui/`
|
|
107
|
+
- Main app: `App.tsx` - Navigation and layout
|
|
108
|
+
- Pages:
|
|
109
|
+
- `VendorsPage.tsx` - Manage AI service vendors
|
|
110
|
+
- `RoutesPage.tsx` - Configure routing rules
|
|
111
|
+
- `LogsPage.tsx` - View request/access/error logs
|
|
112
|
+
- `SettingsPage.tsx` - Application settings
|
|
113
|
+
- `WriteConfigPage.tsx` - Overwrite Claude Code/Codex config files
|
|
114
|
+
- `UsagePage.tsx` - Usage statistics
|
|
115
|
+
|
|
116
|
+
#### 6. Types - `types/`
|
|
117
|
+
- TypeScript type definitions for:
|
|
118
|
+
- Database models (Vendors, Services, Routes, Rules)
|
|
119
|
+
- API requests/responses
|
|
120
|
+
- Configuration
|
|
121
|
+
- Token usage tracking
|
|
122
|
+
|
|
123
|
+
#### 7. CLI - `bin/`
|
|
124
|
+
- `cli.js` - Main CLI entry point
|
|
125
|
+
- `start.js` - Server startup with PID management
|
|
126
|
+
- `stop.js` - Server shutdown
|
|
127
|
+
- `restart.js` - Restart server
|
|
128
|
+
|
|
129
|
+
## Key Features
|
|
130
|
+
|
|
131
|
+
### Routing System
|
|
132
|
+
- **Routes**: Define target type (Claude Code or Codex) and activation status
|
|
133
|
+
- **Rules**: Match requests by content type and route to specific API services
|
|
134
|
+
- **Content Type Detection**:
|
|
135
|
+
- `image-understanding`: Requests with image content
|
|
136
|
+
- `thinking`: Requests with reasoning/thinking signals
|
|
137
|
+
- `long-context`: Requests with large context (≥12000 chars or ≥8000 max tokens)
|
|
138
|
+
- `background`: Background/priority requests
|
|
139
|
+
- `default`: All other requests
|
|
140
|
+
|
|
141
|
+
### Request Transformation
|
|
142
|
+
- Supports multiple source types:
|
|
143
|
+
- OpenAI Chat
|
|
144
|
+
- OpenAI Code
|
|
145
|
+
- OpenAI Responses
|
|
146
|
+
- Claude Chat
|
|
147
|
+
- Claude Code
|
|
148
|
+
- DeepSeek Chat
|
|
149
|
+
|
|
150
|
+
### Configuration Management
|
|
151
|
+
- Writes/ restores Claude Code config files (`~/.claude/settings.json`, `~/.claude.json`)
|
|
152
|
+
- Writes/ restores Codex config files (`~/.codex/config.toml`, `~/.codex/auth.json`)
|
|
153
|
+
- Exports/ imports encrypted configuration data
|
|
154
|
+
|
|
155
|
+
### Logging
|
|
156
|
+
- Request logs: Detailed API call records with token usage
|
|
157
|
+
- Access logs: System access records
|
|
158
|
+
- Error logs: Error and exception records
|
|
159
|
+
|
|
160
|
+
## Development Tips
|
|
161
|
+
|
|
162
|
+
1. **Environment Variables**: Copy `.env.example` to `.env` and modify as needed
|
|
163
|
+
2. **Data Directory**: Default: `~/.aicodeswitch/data/` (SQLite3 database)
|
|
164
|
+
3. **Config File**: `~/.aicodeswitch/aicodeswitch.conf` (HOST, PORT, AUTH)
|
|
165
|
+
4. **Dev Ports**: UI (4568), Server (4567) - configured in `vite.config.ts` and `server/main.ts`
|
|
166
|
+
5. **API Endpoints**: All routes are prefixed with `/api/` except proxy routes (`/claude-code/`, `/codex/`)
|
|
167
|
+
|
|
168
|
+
## Build and Deployment
|
|
169
|
+
|
|
170
|
+
1. Run `npm run build` to create production builds
|
|
171
|
+
2. UI build outputs to `dist/ui/` (static files)
|
|
172
|
+
3. Server build outputs to `dist/server/` (JavaScript)
|
|
173
|
+
4. Configuration files are created in user's home directory on first run
|
|
174
|
+
|
|
175
|
+
## Technology Stack
|
|
176
|
+
|
|
177
|
+
- **Backend**: Node.js, Express, TypeScript, SQLite3
|
|
178
|
+
- **Frontend**: React 18, TypeScript, Vite, React Router
|
|
179
|
+
- **Streaming**: SSE (Server-Sent Events)
|
|
180
|
+
- **HTTP Client**: Axios
|
|
181
|
+
- **Encryption**: CryptoJS (AES)
|
|
182
|
+
- **CLI**: Yargs-like custom implementation
|