ccproxy-api 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl

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.
Files changed (72) hide show
  1. ccproxy/_version.py +2 -2
  2. ccproxy/adapters/codex/__init__.py +11 -0
  3. ccproxy/adapters/openai/adapter.py +1 -1
  4. ccproxy/adapters/openai/models.py +1 -1
  5. ccproxy/adapters/openai/response_adapter.py +355 -0
  6. ccproxy/adapters/openai/response_models.py +178 -0
  7. ccproxy/adapters/openai/streaming.py +1 -0
  8. ccproxy/api/app.py +150 -224
  9. ccproxy/api/dependencies.py +22 -2
  10. ccproxy/api/middleware/errors.py +27 -3
  11. ccproxy/api/middleware/logging.py +4 -0
  12. ccproxy/api/responses.py +6 -1
  13. ccproxy/api/routes/claude.py +222 -17
  14. ccproxy/api/routes/codex.py +1231 -0
  15. ccproxy/api/routes/health.py +228 -3
  16. ccproxy/api/routes/proxy.py +25 -6
  17. ccproxy/api/services/permission_service.py +2 -2
  18. ccproxy/auth/openai/__init__.py +13 -0
  19. ccproxy/auth/openai/credentials.py +166 -0
  20. ccproxy/auth/openai/oauth_client.py +334 -0
  21. ccproxy/auth/openai/storage.py +184 -0
  22. ccproxy/claude_sdk/__init__.py +4 -8
  23. ccproxy/claude_sdk/client.py +661 -131
  24. ccproxy/claude_sdk/exceptions.py +16 -0
  25. ccproxy/claude_sdk/manager.py +219 -0
  26. ccproxy/claude_sdk/message_queue.py +342 -0
  27. ccproxy/claude_sdk/options.py +6 -1
  28. ccproxy/claude_sdk/session_client.py +546 -0
  29. ccproxy/claude_sdk/session_pool.py +550 -0
  30. ccproxy/claude_sdk/stream_handle.py +538 -0
  31. ccproxy/claude_sdk/stream_worker.py +392 -0
  32. ccproxy/claude_sdk/streaming.py +53 -11
  33. ccproxy/cli/commands/auth.py +398 -1
  34. ccproxy/cli/commands/serve.py +99 -1
  35. ccproxy/cli/options/claude_options.py +47 -0
  36. ccproxy/config/__init__.py +0 -3
  37. ccproxy/config/claude.py +171 -23
  38. ccproxy/config/codex.py +100 -0
  39. ccproxy/config/discovery.py +10 -1
  40. ccproxy/config/scheduler.py +2 -2
  41. ccproxy/config/settings.py +38 -1
  42. ccproxy/core/codex_transformers.py +389 -0
  43. ccproxy/core/http_transformers.py +458 -75
  44. ccproxy/core/logging.py +108 -12
  45. ccproxy/core/transformers.py +5 -0
  46. ccproxy/models/claude_sdk.py +57 -0
  47. ccproxy/models/detection.py +208 -0
  48. ccproxy/models/requests.py +22 -0
  49. ccproxy/models/responses.py +16 -0
  50. ccproxy/observability/access_logger.py +72 -14
  51. ccproxy/observability/metrics.py +151 -0
  52. ccproxy/observability/storage/duckdb_simple.py +12 -0
  53. ccproxy/observability/storage/models.py +16 -0
  54. ccproxy/observability/streaming_response.py +107 -0
  55. ccproxy/scheduler/manager.py +31 -6
  56. ccproxy/scheduler/tasks.py +122 -0
  57. ccproxy/services/claude_detection_service.py +269 -0
  58. ccproxy/services/claude_sdk_service.py +333 -130
  59. ccproxy/services/codex_detection_service.py +263 -0
  60. ccproxy/services/proxy_service.py +618 -197
  61. ccproxy/utils/__init__.py +9 -1
  62. ccproxy/utils/disconnection_monitor.py +83 -0
  63. ccproxy/utils/id_generator.py +12 -0
  64. ccproxy/utils/model_mapping.py +7 -5
  65. ccproxy/utils/startup_helpers.py +470 -0
  66. ccproxy_api-0.1.6.dist-info/METADATA +615 -0
  67. {ccproxy_api-0.1.4.dist-info → ccproxy_api-0.1.6.dist-info}/RECORD +70 -47
  68. ccproxy/config/loader.py +0 -105
  69. ccproxy_api-0.1.4.dist-info/METADATA +0 -369
  70. {ccproxy_api-0.1.4.dist-info → ccproxy_api-0.1.6.dist-info}/WHEEL +0 -0
  71. {ccproxy_api-0.1.4.dist-info → ccproxy_api-0.1.6.dist-info}/entry_points.txt +0 -0
  72. {ccproxy_api-0.1.4.dist-info → ccproxy_api-0.1.6.dist-info}/licenses/LICENSE +0 -0
@@ -1,369 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: ccproxy-api
3
- Version: 0.1.4
4
- Summary: API server that provides an Anthropic and OpenAI compatible interface over Claude Code, allowing to use your Claude OAuth account or over the API.
5
- License-File: LICENSE
6
- Requires-Python: >=3.11
7
- Requires-Dist: aiofiles>=24.1.0
8
- Requires-Dist: aiosqlite>=0.21.0
9
- Requires-Dist: claude-code-sdk>=0.0.14
10
- Requires-Dist: duckdb-engine>=0.17.0
11
- Requires-Dist: duckdb>=1.1.0
12
- Requires-Dist: fastapi-mcp
13
- Requires-Dist: fastapi[standard]>=0.115.14
14
- Requires-Dist: httpx-sse>=0.4.1
15
- Requires-Dist: httpx>=0.28.1
16
- Requires-Dist: jsonschema>=0.33.2
17
- Requires-Dist: keyring>=25.6.0
18
- Requires-Dist: openai>=1.93.0
19
- Requires-Dist: prometheus-client>=0.22.1
20
- Requires-Dist: pydantic-settings>=2.4.0
21
- Requires-Dist: pydantic>=2.8.0
22
- Requires-Dist: rich-toolkit>=0.14.8
23
- Requires-Dist: rich>=13.0.0
24
- Requires-Dist: sqlmodel>=0.0.24
25
- Requires-Dist: structlog>=25.4.0
26
- Requires-Dist: textual>=3.7.1
27
- Requires-Dist: tomli>=2.0.0; python_version < '3.11'
28
- Requires-Dist: typer>=0.16.0
29
- Requires-Dist: typing-extensions>=4.0.0
30
- Requires-Dist: uvicorn>=0.34.0
31
- Description-Content-Type: text/markdown
32
-
33
- # CCProxy API Server
34
-
35
- `ccproxy` is a local reverse proxy server for Anthropic Claude LLM at `api.anthropic.com/v1/messages`. It allows you to use your existing Claude Max subscription to interact with the Anthropic API, bypassing the need for separate API key billing.
36
-
37
- The server provides two primary modes of operation:
38
- * **SDK Mode (`/sdk`):** Routes requests through the local `claude-code-sdk`. This enables access to tools configured in your Claude environment and includes an integrated MCP (Model Context Protocol) server for permission management.
39
- * **API Mode (`/api`):** Acts as a direct reverse proxy, injecting the necessary authentication headers. This provides full access to the underlying API features and model settings.
40
-
41
- It includes a translation layer to support both Anthropic and OpenAI-compatible API formats for requests and responses, including streaming.
42
-
43
- ## Installation
44
-
45
- ```bash
46
- # The official claude-code CLI is required for SDK mode
47
- npm install -g @anthropic-ai/claude-code
48
-
49
- # run it with uv
50
- uvx ccproxy-api
51
-
52
- # run it with pipx
53
- pipx run ccproxy-api
54
-
55
- # install with uv
56
- uv tool install ccproxy-api
57
-
58
- # Install ccproxy with pip
59
- pipx install ccproxy-api
60
-
61
- # Optional: Enable shell completion
62
- eval "$(ccproxy --show-completion zsh)" # For zsh
63
- eval "$(ccproxy --show-completion bash)" # For bash
64
- ```
65
-
66
-
67
- For dev version replace `ccproxy-api` with `git+https://github.com/caddyglow/ccproxy-api.git@dev`
68
-
69
- ## Authentication
70
-
71
- The proxy uses two different authentication mechanisms depending on the mode.
72
-
73
- 1. **Claude CLI (`sdk` mode):**
74
- This mode relies on the authentication handled by the `claude-code-sdk`.
75
- ```bash
76
- claude /login
77
- ```
78
-
79
- It's also possible now to get a long live token to avoid renewing issues
80
- using
81
- ```sh
82
- ```bash
83
- claude setup-token`
84
-
85
- 2. **ccproxy (`api` mode):**
86
- This mode uses its own OAuth2 flow to obtain credentials for direct API access.
87
- ```bash
88
- ccproxy auth login
89
- ```
90
-
91
- If you are already connected with Claude CLI the credentials should be found automatically
92
-
93
- You can check the status of these credentials with `ccproxy auth validate` and `ccproxy auth info`.
94
-
95
- Warning is show on start up if no credentials are setup.
96
-
97
- ## Usage
98
-
99
- ### Running the Server
100
-
101
- ```bash
102
- # Start the proxy server
103
- ccproxy
104
- ```
105
- The server will start on `http://127.0.0.1:8000` by default.
106
-
107
- ### Client Configuration
108
-
109
- Point your existing tools and applications to the local proxy instance by setting the appropriate environment variables. A dummy API key is required by most client libraries but is not used by the proxy itself.
110
-
111
- **For OpenAI-compatible clients:**
112
- ```bash
113
- # For SDK mode
114
- export OPENAI_BASE_URL="http://localhost:8000/sdk/v1"
115
- # For API mode
116
- export OPENAI_BASE_URL="http://localhost:8000/api/v1"
117
-
118
- export OPENAI_API_KEY="dummy-key"
119
- ```
120
-
121
- **For Anthropic-compatible clients:**
122
- ```bash
123
- # For SDK mode
124
- export ANTHROPIC_BASE_URL="http://localhost:8000/sdk"
125
- # For API mode
126
- export ANTHROPIC_BASE_URL="http://localhost:8000/api"
127
-
128
- export ANTHROPIC_API_KEY="dummy-key"
129
- ```
130
-
131
-
132
- ## MCP Server Integration & Permission System
133
-
134
- In SDK mode, CCProxy automatically configures an MCP (Model Context Protocol) server that provides permission checking tools for Claude Code. This enables interactive permission management for tool execution.
135
-
136
- ### Permission Management
137
-
138
- **Starting the Permission Handler:**
139
- ```bash
140
- # In a separate terminal, start the permission handler
141
- ccproxy permission-handler
142
-
143
- # Or with custom settings
144
- ccproxy permission-handler --host 127.0.0.1 --port 8000
145
- ```
146
-
147
- The permission handler provides:
148
- - **Real-time Permission Requests**: Streams permission requests via Server-Sent Events (SSE)
149
- - **Interactive Approval/Denial**: Command-line interface for managing tool permissions
150
- - **Automatic MCP Integration**: Works seamlessly with Claude Code SDK tools
151
-
152
- **Working Directory Control:**
153
- Control which project the Claude SDK API can access using the `--cwd` flag:
154
- ```bash
155
- # Set working directory for Claude SDK
156
- ccproxy --claude-code-options-cwd /path/to/your/project
157
-
158
- # Example with permission bypass and formatted output
159
- ccproxy --claude-code-options-cwd /tmp/tmp.AZyCo5a42N \
160
- --claude-code-options-permission-mode bypassPermissions \
161
- --claude-sdk-message-mode formatted
162
-
163
- # Alternative: Change to project directory and start ccproxy
164
- cd /path/to/your/project
165
- ccproxy
166
- ```
167
-
168
- ### Claude SDK Message Formatting
169
-
170
- CCProxy supports flexible message formatting through the `sdk_message_mode` configuration:
171
-
172
- - **`forward`** (default): Preserves original Claude SDK content blocks with full metadata
173
- - **`formatted`**: Converts content to XML tags with pretty-printed JSON data
174
- - **`ignore`**: Filters out Claude SDK-specific content entirely
175
-
176
- Configure via environment variables:
177
- ```bash
178
- # Use formatted XML output
179
- CLAUDE__SDK_MESSAGE_MODE=formatted ccproxy
180
-
181
- # Use compact formatting without pretty-printing
182
- CLAUDE__PRETTY_FORMAT=false ccproxy
183
- ```
184
-
185
- ## Using with Aider
186
-
187
- CCProxy works seamlessly with Aider and other AI coding assistants:
188
-
189
- ### Anthropic Mode
190
- ```bash
191
- export ANTHROPIC_API_KEY=dummy
192
- export ANTHROPIC_BASE_URL=http://127.0.0.1:8000/api
193
- aider --model claude-sonnet-4-20250514
194
- ```
195
-
196
- ### OpenAI Mode with Model Mapping
197
-
198
- If your tool only supports OpenAI settings, ccproxy automatically maps OpenAI models to Claude:
199
-
200
- ```bash
201
- export OPENAI_API_KEY=dummy
202
- export OPENAI_BASE_URL=http://127.0.0.1:8000/api/v1
203
- aider --model o3-mini
204
- ```
205
-
206
- ### API Mode (Direct Proxy)
207
-
208
- For minimal interference and direct API access:
209
-
210
- ```bash
211
- export OPENAI_API_KEY=dummy
212
- export OPENAI_BASE_URL=http://127.0.0.1:8000/api/v1
213
- aider --model o3-mini
214
- ```
215
-
216
- ### `curl` Example
217
-
218
- ```bash
219
- # SDK mode
220
- curl -X POST http://localhost:8000/sdk/v1/messages \
221
- -H "Content-Type: application/json" \
222
- -d '{
223
- "model": "claude-3-5-sonnet-20241022",
224
- "messages": [{"role": "user", "content": "Hello!"}],
225
- "max_tokens": 100
226
- }'
227
-
228
- # API mode
229
- curl -X POST http://localhost:8000/api/v1/messages \
230
- -H "Content-Type: application/json" \
231
- -d '{
232
- "model": "claude-3-5-sonnet-20241022",
233
- "messages": [{"role": "user", "content": "Hello!"}],
234
- "max_tokens": 100
235
- }'
236
- ```
237
- More examples are available in the `examples/` directory.
238
-
239
- ## Endpoints
240
-
241
- The proxy exposes endpoints under two prefixes, corresponding to its operating modes.
242
-
243
- | Mode | URL Prefix | Description | Use Case |
244
- |------|------------|-------------|----------|
245
- | **SDK** | `/sdk/` | Uses `claude-code-sdk` with its configured tools. | Accessing Claude with local tools. |
246
- | **API** | `/api/` | Direct proxy with header injection. | Full API control, direct access. |
247
-
248
- * **Anthropic:**
249
- * `POST /sdk/v1/messages`
250
- * `POST /api/v1/messages`
251
- * **OpenAI-Compatible:**
252
- * `POST /sdk/v1/chat/completions`
253
- * `POST /api/v1/chat/completions`
254
- * **Utility:**
255
- * `GET /health`
256
- * `GET /sdk/models`, `GET /api/models`
257
- * `GET /sdk/status`, `GET /api/status`
258
- * `GET /oauth/callback`
259
- * **MCP & Permissions:**
260
- * `POST /mcp/permission/check` - MCP permission checking endpoint
261
- * `GET /permissions/stream` - SSE stream for permission requests
262
- * `GET /permissions/{id}` - Get permission request details
263
- * `POST /permissions/{id}/respond` - Respond to permission request
264
- * **Observability (Optional):**
265
- * `GET /metrics`
266
- * `GET /logs/status`, `GET /logs/query`
267
- * `GET /dashboard`
268
-
269
- ## Supported Models
270
-
271
- CCProxy supports recent Claude models including Opus, Sonnet, and Haiku variants. The specific models available to you will depend on your Claude account and the features enabled for your subscription.
272
-
273
- * `claude-opus-4-20250514`
274
- * `claude-sonnet-4-20250514`
275
- * `claude-3-7-sonnet-20250219`
276
- * `claude-3-5-sonnet-20241022`
277
- * `claude-3-5-sonnet-20240620`
278
-
279
- ## Configuration
280
-
281
- Settings can be configured through (in order of precedence):
282
- 1. Command-line arguments
283
- 2. Environment variables
284
- 3. `.env` file
285
- 4. TOML configuration files (`.ccproxy.toml`, `ccproxy.toml`, or `~/.config/ccproxy/config.toml`)
286
- 5. Default values
287
-
288
- For complex configurations, you can use a nested syntax for environment variables with `__` as a delimiter:
289
-
290
- ```bash
291
- # Server settings
292
- SERVER__HOST=0.0.0.0
293
- SERVER__PORT=8080
294
- # etc.
295
- ```
296
-
297
- ## Securing the Proxy (Optional)
298
-
299
- You can enable token authentication for the proxy. This supports multiple header formats (`x-api-key` for Anthropic, `Authorization: Bearer` for OpenAI) for compatibility with standard client libraries.
300
-
301
- **1. Generate a Token:**
302
- ```bash
303
- ccproxy generate-token
304
- # Output: SECURITY__AUTH_TOKEN=abc123xyz789...
305
- ```
306
-
307
- **2. Configure the Token:**
308
- ```bash
309
- # Set environment variable
310
- export SECURITY__AUTH_TOKEN=abc123xyz789...
311
-
312
- # Or add to .env file
313
- echo "SECURITY__AUTH_TOKEN=abc123xyz789..." >> .env
314
- ```
315
-
316
- **3. Use in Requests:**
317
- When authentication is enabled, include the token in your API requests.
318
- ```bash
319
- # Anthropic Format (x-api-key)
320
- curl -H "x-api-key: your-token" ...
321
-
322
- # OpenAI/Bearer Format
323
- curl -H "Authorization: Bearer your-token" ...
324
- ```
325
-
326
- ## Observability
327
-
328
- `ccproxy` includes an optional but powerful observability suite for monitoring and analytics. When enabled, it provides:
329
-
330
- * **Prometheus Metrics:** A `/metrics` endpoint for real-time operational monitoring.
331
- * **Access Log Storage:** Detailed request logs, including token usage and costs, are stored in a local DuckDB database.
332
- * **Analytics API:** Endpoints to query and analyze historical usage data.
333
- * **Real-time Dashboard:** A live web interface at `/dashboard` to visualize metrics and request streams.
334
-
335
- These features are disabled by default and can be enabled via configuration. For a complete guide on setting up and using these features, see the [Observability Documentation](docs/observability.md).
336
-
337
- ## Troubleshooting
338
-
339
- ### Common Issues
340
-
341
- 1. **Authentication Error:** Ensure you're using the correct mode (`/sdk` or `/api`) for your authentication method.
342
- 2. **Claude Credentials Expired:** Run `ccproxy auth login` to refresh credentials for API mode. Run `claude /login` for SDK mode.
343
- 3. **Missing API Auth Token:** If you've enabled security, include the token in your request headers.
344
- 4. **Port Already in Use:** Start the server on a different port: `ccproxy --port 8001`.
345
- 5. **Model Not Available:** Check that your Claude subscription includes the requested model.
346
-
347
- ## Contributing
348
-
349
- Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
350
-
351
- ## License
352
-
353
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
354
-
355
- ## Documentation
356
-
357
- - **[Online Documentation](https://caddyglow.github.io/ccproxy-api)**
358
- - **[API Reference](https://caddyglow.github.io/ccproxy-api/api-reference/overview/)**
359
- - **[Developer Guide](https://caddyglow.github.io/ccproxy-api/developer-guide/architecture/)**
360
-
361
- ## Support
362
-
363
- - Issues: [GitHub Issues](https://github.com/CaddyGlow/ccproxy-api/issues)
364
- - Documentation: [Project Documentation](https://caddyglow.github.io/ccproxy-api)
365
-
366
- ## Acknowledgments
367
-
368
- - [Anthropic](https://anthropic.com) for Claude and the Claude Code SDK
369
- - The open-source community