claude2api-client 0.1.0__tar.gz
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.
- claude2api_client-0.1.0/LICENSE +21 -0
- claude2api_client-0.1.0/PKG-INFO +147 -0
- claude2api_client-0.1.0/README.md +121 -0
- claude2api_client-0.1.0/claude2api_client/__init__.py +6 -0
- claude2api_client-0.1.0/claude2api_client/client.py +121 -0
- claude2api_client-0.1.0/claude2api_client/py.typed +0 -0
- claude2api_client-0.1.0/claude2api_client.egg-info/PKG-INFO +147 -0
- claude2api_client-0.1.0/claude2api_client.egg-info/SOURCES.txt +11 -0
- claude2api_client-0.1.0/claude2api_client.egg-info/dependency_links.txt +1 -0
- claude2api_client-0.1.0/claude2api_client.egg-info/requires.txt +2 -0
- claude2api_client-0.1.0/claude2api_client.egg-info/top_level.txt +1 -0
- claude2api_client-0.1.0/pyproject.toml +41 -0
- claude2api_client-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 claude2api contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: claude2api-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for claude2Api bridge service
|
|
5
|
+
Author: claude2api contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/anthropics/claude2api
|
|
8
|
+
Project-URL: Repository, https://github.com/anthropics/claude2api
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.8
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: requests>=2.28.0
|
|
24
|
+
Requires-Dist: sseclient-py>=1.8.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# claude2api-client
|
|
28
|
+
|
|
29
|
+
Python client for the [claude2Api](https://github.com/anthropics/claude2api) bridge service — call Claude Code's full capabilities (file I/O, Bash, multi-turn conversations) over HTTP from any Python project.
|
|
30
|
+
|
|
31
|
+
## Prerequisites
|
|
32
|
+
|
|
33
|
+
A running **claude2Api** Node.js bridge service. See the [main repo](https://github.com/anthropics/claude2api) for setup instructions.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install claude2api-client
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from claude2api_client import Claude2Api
|
|
45
|
+
|
|
46
|
+
api = Claude2Api("http://localhost:3456")
|
|
47
|
+
|
|
48
|
+
# Single turn
|
|
49
|
+
r = api.chat("Explain Python GIL in one sentence")
|
|
50
|
+
print(r.result)
|
|
51
|
+
|
|
52
|
+
# Multi-turn conversation
|
|
53
|
+
r2 = api.chat("How about JavaScript?", session_id=r.session_id)
|
|
54
|
+
print(r2.result)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Streaming
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
def on_stream(data):
|
|
61
|
+
if isinstance(data.get("content"), list):
|
|
62
|
+
for block in data["content"]:
|
|
63
|
+
if isinstance(block, dict) and block.get("type") == "text":
|
|
64
|
+
print(block["text"], end="", flush=True)
|
|
65
|
+
|
|
66
|
+
r = api.chat("Write a haiku", on_stream=on_stream)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API Reference
|
|
70
|
+
|
|
71
|
+
### `Claude2Api(base_url, timeout)`
|
|
72
|
+
|
|
73
|
+
| Param | Default | Description |
|
|
74
|
+
|-------|---------|-------------|
|
|
75
|
+
| `base_url` | `http://localhost:3456` | Bridge service URL |
|
|
76
|
+
| `timeout` | `300` | Request timeout in seconds |
|
|
77
|
+
|
|
78
|
+
### `chat(prompt, **kwargs) -> ChatResult`
|
|
79
|
+
|
|
80
|
+
| Param | Type | Description |
|
|
81
|
+
|-------|------|-------------|
|
|
82
|
+
| `prompt` | `str` | Message to send (required) |
|
|
83
|
+
| `session_id` | `str` | Resume an existing session |
|
|
84
|
+
| `cwd` | `str` | Working directory for file operations |
|
|
85
|
+
| `model` | `str` | Model ID, e.g. `"claude-sonnet-4-6"` |
|
|
86
|
+
| `allowed_tools` | `list` | e.g. `["Read", "Edit", "Bash"]` |
|
|
87
|
+
| `max_turns` | `int` | Max agent turns |
|
|
88
|
+
| `system_prompt` | `str` | Custom system prompt |
|
|
89
|
+
| `on_stream` | `callable` | Callback for intermediate SSE events |
|
|
90
|
+
|
|
91
|
+
### `ChatResult`
|
|
92
|
+
|
|
93
|
+
| Field | Type | Description |
|
|
94
|
+
|-------|------|-------------|
|
|
95
|
+
| `session_id` | `str \| None` | Session identifier |
|
|
96
|
+
| `result` | `str \| None` | Final response text |
|
|
97
|
+
| `cost` | `float \| None` | Token cost |
|
|
98
|
+
| `duration` | `float \| None` | Execution time |
|
|
99
|
+
| `is_error` | `bool` | Whether an error occurred |
|
|
100
|
+
| `error` | `str \| None` | Error message |
|
|
101
|
+
|
|
102
|
+
### Utility methods
|
|
103
|
+
|
|
104
|
+
- `health() -> dict` — Health check
|
|
105
|
+
- `sessions() -> list` — List active sessions
|
|
106
|
+
- `abort(session_id) -> dict` — Abort a running session
|
|
107
|
+
|
|
108
|
+
## Publishing to PyPI
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# Build
|
|
112
|
+
cd python
|
|
113
|
+
python -m build
|
|
114
|
+
|
|
115
|
+
# Upload to PyPI
|
|
116
|
+
twine upload dist/*
|
|
117
|
+
# username: __token__
|
|
118
|
+
# password: your PyPI API token (https://pypi.org/manage/account/token/)
|
|
119
|
+
|
|
120
|
+
# Or test on TestPyPI first
|
|
121
|
+
twine upload --repository testpypi dist/*
|
|
122
|
+
pip install --index-url https://test.pypi.org/simple/ claude2api
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
To skip entering credentials every time, create `~/.pypirc`:
|
|
126
|
+
|
|
127
|
+
```ini
|
|
128
|
+
[distutils]
|
|
129
|
+
index-servers =
|
|
130
|
+
pypi
|
|
131
|
+
testpypi
|
|
132
|
+
|
|
133
|
+
[pypi]
|
|
134
|
+
username = __token__
|
|
135
|
+
password = pypi-xxxxxxxxxxxxxxxx
|
|
136
|
+
|
|
137
|
+
[testpypi]
|
|
138
|
+
repository = https://test.pypi.org/legacy/
|
|
139
|
+
username = __token__
|
|
140
|
+
password = pypi-xxxxxxxxxxxxxxxx
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
To release a new version, update `version` in both `pyproject.toml` and `claude2api_client/__init__.py`, then rebuild and upload.
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# claude2api-client
|
|
2
|
+
|
|
3
|
+
Python client for the [claude2Api](https://github.com/anthropics/claude2api) bridge service — call Claude Code's full capabilities (file I/O, Bash, multi-turn conversations) over HTTP from any Python project.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
A running **claude2Api** Node.js bridge service. See the [main repo](https://github.com/anthropics/claude2api) for setup instructions.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install claude2api-client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from claude2api_client import Claude2Api
|
|
19
|
+
|
|
20
|
+
api = Claude2Api("http://localhost:3456")
|
|
21
|
+
|
|
22
|
+
# Single turn
|
|
23
|
+
r = api.chat("Explain Python GIL in one sentence")
|
|
24
|
+
print(r.result)
|
|
25
|
+
|
|
26
|
+
# Multi-turn conversation
|
|
27
|
+
r2 = api.chat("How about JavaScript?", session_id=r.session_id)
|
|
28
|
+
print(r2.result)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Streaming
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
def on_stream(data):
|
|
35
|
+
if isinstance(data.get("content"), list):
|
|
36
|
+
for block in data["content"]:
|
|
37
|
+
if isinstance(block, dict) and block.get("type") == "text":
|
|
38
|
+
print(block["text"], end="", flush=True)
|
|
39
|
+
|
|
40
|
+
r = api.chat("Write a haiku", on_stream=on_stream)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API Reference
|
|
44
|
+
|
|
45
|
+
### `Claude2Api(base_url, timeout)`
|
|
46
|
+
|
|
47
|
+
| Param | Default | Description |
|
|
48
|
+
|-------|---------|-------------|
|
|
49
|
+
| `base_url` | `http://localhost:3456` | Bridge service URL |
|
|
50
|
+
| `timeout` | `300` | Request timeout in seconds |
|
|
51
|
+
|
|
52
|
+
### `chat(prompt, **kwargs) -> ChatResult`
|
|
53
|
+
|
|
54
|
+
| Param | Type | Description |
|
|
55
|
+
|-------|------|-------------|
|
|
56
|
+
| `prompt` | `str` | Message to send (required) |
|
|
57
|
+
| `session_id` | `str` | Resume an existing session |
|
|
58
|
+
| `cwd` | `str` | Working directory for file operations |
|
|
59
|
+
| `model` | `str` | Model ID, e.g. `"claude-sonnet-4-6"` |
|
|
60
|
+
| `allowed_tools` | `list` | e.g. `["Read", "Edit", "Bash"]` |
|
|
61
|
+
| `max_turns` | `int` | Max agent turns |
|
|
62
|
+
| `system_prompt` | `str` | Custom system prompt |
|
|
63
|
+
| `on_stream` | `callable` | Callback for intermediate SSE events |
|
|
64
|
+
|
|
65
|
+
### `ChatResult`
|
|
66
|
+
|
|
67
|
+
| Field | Type | Description |
|
|
68
|
+
|-------|------|-------------|
|
|
69
|
+
| `session_id` | `str \| None` | Session identifier |
|
|
70
|
+
| `result` | `str \| None` | Final response text |
|
|
71
|
+
| `cost` | `float \| None` | Token cost |
|
|
72
|
+
| `duration` | `float \| None` | Execution time |
|
|
73
|
+
| `is_error` | `bool` | Whether an error occurred |
|
|
74
|
+
| `error` | `str \| None` | Error message |
|
|
75
|
+
|
|
76
|
+
### Utility methods
|
|
77
|
+
|
|
78
|
+
- `health() -> dict` — Health check
|
|
79
|
+
- `sessions() -> list` — List active sessions
|
|
80
|
+
- `abort(session_id) -> dict` — Abort a running session
|
|
81
|
+
|
|
82
|
+
## Publishing to PyPI
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Build
|
|
86
|
+
cd python
|
|
87
|
+
python -m build
|
|
88
|
+
|
|
89
|
+
# Upload to PyPI
|
|
90
|
+
twine upload dist/*
|
|
91
|
+
# username: __token__
|
|
92
|
+
# password: your PyPI API token (https://pypi.org/manage/account/token/)
|
|
93
|
+
|
|
94
|
+
# Or test on TestPyPI first
|
|
95
|
+
twine upload --repository testpypi dist/*
|
|
96
|
+
pip install --index-url https://test.pypi.org/simple/ claude2api
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
To skip entering credentials every time, create `~/.pypirc`:
|
|
100
|
+
|
|
101
|
+
```ini
|
|
102
|
+
[distutils]
|
|
103
|
+
index-servers =
|
|
104
|
+
pypi
|
|
105
|
+
testpypi
|
|
106
|
+
|
|
107
|
+
[pypi]
|
|
108
|
+
username = __token__
|
|
109
|
+
password = pypi-xxxxxxxxxxxxxxxx
|
|
110
|
+
|
|
111
|
+
[testpypi]
|
|
112
|
+
repository = https://test.pypi.org/legacy/
|
|
113
|
+
username = __token__
|
|
114
|
+
password = pypi-xxxxxxxxxxxxxxxx
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
To release a new version, update `version` in both `pyproject.toml` and `claude2api_client/__init__.py`, then rebuild and upload.
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""Claude2Api Python Client"""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Optional, Callable
|
|
6
|
+
|
|
7
|
+
import requests
|
|
8
|
+
import sseclient
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class ChatResult:
|
|
13
|
+
session_id: Optional[str] = None
|
|
14
|
+
result: Optional[str] = None
|
|
15
|
+
cost: Optional[float] = None
|
|
16
|
+
duration: Optional[float] = None
|
|
17
|
+
is_error: bool = False
|
|
18
|
+
error: Optional[str] = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Claude2Api:
|
|
22
|
+
"""Python client for claude2Api bridge service.
|
|
23
|
+
|
|
24
|
+
Usage:
|
|
25
|
+
api = Claude2Api("http://localhost:3456")
|
|
26
|
+
r = api.chat("你好")
|
|
27
|
+
print(r.result)
|
|
28
|
+
|
|
29
|
+
# 多轮对话
|
|
30
|
+
r2 = api.chat("继续", session_id=r.session_id)
|
|
31
|
+
|
|
32
|
+
# 交互式(Claude 提问后回复)
|
|
33
|
+
r3 = api.chat("用 JWT", session_id=r2.session_id)
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, base_url: str = "http://localhost:3456", timeout: int = 300):
|
|
37
|
+
self.base_url = base_url.rstrip("/")
|
|
38
|
+
self.timeout = timeout
|
|
39
|
+
|
|
40
|
+
def health(self) -> dict:
|
|
41
|
+
return requests.get(f"{self.base_url}/health", timeout=5).json()
|
|
42
|
+
|
|
43
|
+
def sessions(self) -> list:
|
|
44
|
+
return requests.get(f"{self.base_url}/api/sessions", timeout=5).json()
|
|
45
|
+
|
|
46
|
+
def abort(self, session_id: str) -> dict:
|
|
47
|
+
return requests.post(
|
|
48
|
+
f"{self.base_url}/api/abort/{session_id}", timeout=5
|
|
49
|
+
).json()
|
|
50
|
+
|
|
51
|
+
def chat(
|
|
52
|
+
self,
|
|
53
|
+
prompt: str,
|
|
54
|
+
session_id: Optional[str] = None,
|
|
55
|
+
cwd: Optional[str] = None,
|
|
56
|
+
model: Optional[str] = None,
|
|
57
|
+
allowed_tools: Optional[list] = None,
|
|
58
|
+
max_turns: Optional[int] = None,
|
|
59
|
+
system_prompt: Optional[str] = None,
|
|
60
|
+
on_stream: Optional[Callable[[dict], None]] = None,
|
|
61
|
+
) -> ChatResult:
|
|
62
|
+
"""Send a message and stream the response.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
prompt: The message to send.
|
|
66
|
+
session_id: Resume an existing session (multi-turn).
|
|
67
|
+
cwd: Working directory for file operations.
|
|
68
|
+
model: Model ID (e.g. "claude-sonnet-4-6").
|
|
69
|
+
allowed_tools: List of tools (e.g. ["Read", "Edit", "Bash"]).
|
|
70
|
+
max_turns: Max agent turns.
|
|
71
|
+
system_prompt: Custom system prompt.
|
|
72
|
+
on_stream: Callback for intermediate stream events.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
ChatResult with session_id, result, cost, etc.
|
|
76
|
+
"""
|
|
77
|
+
url = (
|
|
78
|
+
f"{self.base_url}/api/chat/{session_id}"
|
|
79
|
+
if session_id
|
|
80
|
+
else f"{self.base_url}/api/chat"
|
|
81
|
+
)
|
|
82
|
+
payload = {"prompt": prompt}
|
|
83
|
+
if cwd:
|
|
84
|
+
payload["cwd"] = cwd
|
|
85
|
+
if model:
|
|
86
|
+
payload["model"] = model
|
|
87
|
+
if allowed_tools:
|
|
88
|
+
payload["allowedTools"] = allowed_tools
|
|
89
|
+
if max_turns:
|
|
90
|
+
payload["maxTurns"] = max_turns
|
|
91
|
+
if system_prompt:
|
|
92
|
+
payload["systemPrompt"] = system_prompt
|
|
93
|
+
|
|
94
|
+
resp = requests.post(url, json=payload, stream=True, timeout=self.timeout)
|
|
95
|
+
if resp.status_code != 200:
|
|
96
|
+
return ChatResult(
|
|
97
|
+
session_id=session_id,
|
|
98
|
+
is_error=True,
|
|
99
|
+
error=resp.text,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
client = sseclient.SSEClient(resp)
|
|
103
|
+
cr = ChatResult(session_id=session_id)
|
|
104
|
+
|
|
105
|
+
for event in client.events():
|
|
106
|
+
data = json.loads(event.data)
|
|
107
|
+
if event.event == "session":
|
|
108
|
+
cr.session_id = data["sessionId"]
|
|
109
|
+
elif event.event == "stream":
|
|
110
|
+
if on_stream:
|
|
111
|
+
on_stream(data)
|
|
112
|
+
elif event.event == "result":
|
|
113
|
+
cr.result = data.get("result")
|
|
114
|
+
cr.cost = data.get("cost")
|
|
115
|
+
cr.duration = data.get("duration")
|
|
116
|
+
cr.is_error = data.get("isError", False)
|
|
117
|
+
elif event.event == "error":
|
|
118
|
+
cr.is_error = True
|
|
119
|
+
cr.error = data.get("message")
|
|
120
|
+
|
|
121
|
+
return cr
|
|
File without changes
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: claude2api-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for claude2Api bridge service
|
|
5
|
+
Author: claude2api contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/anthropics/claude2api
|
|
8
|
+
Project-URL: Repository, https://github.com/anthropics/claude2api
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.8
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: requests>=2.28.0
|
|
24
|
+
Requires-Dist: sseclient-py>=1.8.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# claude2api-client
|
|
28
|
+
|
|
29
|
+
Python client for the [claude2Api](https://github.com/anthropics/claude2api) bridge service — call Claude Code's full capabilities (file I/O, Bash, multi-turn conversations) over HTTP from any Python project.
|
|
30
|
+
|
|
31
|
+
## Prerequisites
|
|
32
|
+
|
|
33
|
+
A running **claude2Api** Node.js bridge service. See the [main repo](https://github.com/anthropics/claude2api) for setup instructions.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install claude2api-client
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from claude2api_client import Claude2Api
|
|
45
|
+
|
|
46
|
+
api = Claude2Api("http://localhost:3456")
|
|
47
|
+
|
|
48
|
+
# Single turn
|
|
49
|
+
r = api.chat("Explain Python GIL in one sentence")
|
|
50
|
+
print(r.result)
|
|
51
|
+
|
|
52
|
+
# Multi-turn conversation
|
|
53
|
+
r2 = api.chat("How about JavaScript?", session_id=r.session_id)
|
|
54
|
+
print(r2.result)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Streaming
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
def on_stream(data):
|
|
61
|
+
if isinstance(data.get("content"), list):
|
|
62
|
+
for block in data["content"]:
|
|
63
|
+
if isinstance(block, dict) and block.get("type") == "text":
|
|
64
|
+
print(block["text"], end="", flush=True)
|
|
65
|
+
|
|
66
|
+
r = api.chat("Write a haiku", on_stream=on_stream)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API Reference
|
|
70
|
+
|
|
71
|
+
### `Claude2Api(base_url, timeout)`
|
|
72
|
+
|
|
73
|
+
| Param | Default | Description |
|
|
74
|
+
|-------|---------|-------------|
|
|
75
|
+
| `base_url` | `http://localhost:3456` | Bridge service URL |
|
|
76
|
+
| `timeout` | `300` | Request timeout in seconds |
|
|
77
|
+
|
|
78
|
+
### `chat(prompt, **kwargs) -> ChatResult`
|
|
79
|
+
|
|
80
|
+
| Param | Type | Description |
|
|
81
|
+
|-------|------|-------------|
|
|
82
|
+
| `prompt` | `str` | Message to send (required) |
|
|
83
|
+
| `session_id` | `str` | Resume an existing session |
|
|
84
|
+
| `cwd` | `str` | Working directory for file operations |
|
|
85
|
+
| `model` | `str` | Model ID, e.g. `"claude-sonnet-4-6"` |
|
|
86
|
+
| `allowed_tools` | `list` | e.g. `["Read", "Edit", "Bash"]` |
|
|
87
|
+
| `max_turns` | `int` | Max agent turns |
|
|
88
|
+
| `system_prompt` | `str` | Custom system prompt |
|
|
89
|
+
| `on_stream` | `callable` | Callback for intermediate SSE events |
|
|
90
|
+
|
|
91
|
+
### `ChatResult`
|
|
92
|
+
|
|
93
|
+
| Field | Type | Description |
|
|
94
|
+
|-------|------|-------------|
|
|
95
|
+
| `session_id` | `str \| None` | Session identifier |
|
|
96
|
+
| `result` | `str \| None` | Final response text |
|
|
97
|
+
| `cost` | `float \| None` | Token cost |
|
|
98
|
+
| `duration` | `float \| None` | Execution time |
|
|
99
|
+
| `is_error` | `bool` | Whether an error occurred |
|
|
100
|
+
| `error` | `str \| None` | Error message |
|
|
101
|
+
|
|
102
|
+
### Utility methods
|
|
103
|
+
|
|
104
|
+
- `health() -> dict` — Health check
|
|
105
|
+
- `sessions() -> list` — List active sessions
|
|
106
|
+
- `abort(session_id) -> dict` — Abort a running session
|
|
107
|
+
|
|
108
|
+
## Publishing to PyPI
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# Build
|
|
112
|
+
cd python
|
|
113
|
+
python -m build
|
|
114
|
+
|
|
115
|
+
# Upload to PyPI
|
|
116
|
+
twine upload dist/*
|
|
117
|
+
# username: __token__
|
|
118
|
+
# password: your PyPI API token (https://pypi.org/manage/account/token/)
|
|
119
|
+
|
|
120
|
+
# Or test on TestPyPI first
|
|
121
|
+
twine upload --repository testpypi dist/*
|
|
122
|
+
pip install --index-url https://test.pypi.org/simple/ claude2api
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
To skip entering credentials every time, create `~/.pypirc`:
|
|
126
|
+
|
|
127
|
+
```ini
|
|
128
|
+
[distutils]
|
|
129
|
+
index-servers =
|
|
130
|
+
pypi
|
|
131
|
+
testpypi
|
|
132
|
+
|
|
133
|
+
[pypi]
|
|
134
|
+
username = __token__
|
|
135
|
+
password = pypi-xxxxxxxxxxxxxxxx
|
|
136
|
+
|
|
137
|
+
[testpypi]
|
|
138
|
+
repository = https://test.pypi.org/legacy/
|
|
139
|
+
username = __token__
|
|
140
|
+
password = pypi-xxxxxxxxxxxxxxxx
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
To release a new version, update `version` in both `pyproject.toml` and `claude2api_client/__init__.py`, then rebuild and upload.
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
claude2api_client/__init__.py
|
|
5
|
+
claude2api_client/client.py
|
|
6
|
+
claude2api_client/py.typed
|
|
7
|
+
claude2api_client.egg-info/PKG-INFO
|
|
8
|
+
claude2api_client.egg-info/SOURCES.txt
|
|
9
|
+
claude2api_client.egg-info/dependency_links.txt
|
|
10
|
+
claude2api_client.egg-info/requires.txt
|
|
11
|
+
claude2api_client.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
claude2api_client
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=64", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "claude2api-client"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python client for claude2Api bridge service"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.8"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "claude2api contributors" },
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.8",
|
|
20
|
+
"Programming Language :: Python :: 3.9",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"requests>=2.28.0",
|
|
30
|
+
"sseclient-py>=1.8.0",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/anthropics/claude2api"
|
|
35
|
+
Repository = "https://github.com/anthropics/claude2api"
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.packages.find]
|
|
38
|
+
include = ["claude2api_client*"]
|
|
39
|
+
|
|
40
|
+
[tool.setuptools.package-data]
|
|
41
|
+
claude2api_client = ["py.typed"]
|