llm-cookie-bridge 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.
- llm_cookie_bridge-0.1.0/.github/workflows/publish.yml +46 -0
- llm_cookie_bridge-0.1.0/.gitignore +7 -0
- llm_cookie_bridge-0.1.0/LICENSE +21 -0
- llm_cookie_bridge-0.1.0/PKG-INFO +180 -0
- llm_cookie_bridge-0.1.0/README.md +151 -0
- llm_cookie_bridge-0.1.0/pyproject.toml +47 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/__init__.py +21 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/client.py +71 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/exceptions.py +26 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/providers/__init__.py +13 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/providers/base.py +216 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/providers/chatgpt.py +105 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/providers/claude.py +100 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/providers/gemini.py +142 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/providers/perplexity.py +108 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/sse.py +41 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/types.py +33 -0
- llm_cookie_bridge-0.1.0/src/llm_cookie_bridge/utils.py +142 -0
- llm_cookie_bridge-0.1.0/tests/test_chatgpt.py +101 -0
- llm_cookie_bridge-0.1.0/tests/test_claude.py +30 -0
- llm_cookie_bridge-0.1.0/tests/test_gemini.py +40 -0
- llm_cookie_bridge-0.1.0/tests/test_perplexity.py +43 -0
- llm_cookie_bridge-0.1.0/tests/test_refresh.py +42 -0
- llm_cookie_bridge-0.1.0/tests/test_security_defaults.py +20 -0
- llm_cookie_bridge-0.1.0/tests/test_utils.py +17 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- name: Check out repository
|
|
13
|
+
uses: actions/checkout@v4
|
|
14
|
+
|
|
15
|
+
- name: Set up Python
|
|
16
|
+
uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
|
|
20
|
+
- name: Install build tooling
|
|
21
|
+
run: python -m pip install --upgrade build
|
|
22
|
+
|
|
23
|
+
- name: Build distributions
|
|
24
|
+
run: python -m build
|
|
25
|
+
|
|
26
|
+
- name: Upload dist artifacts
|
|
27
|
+
uses: actions/upload-artifact@v4
|
|
28
|
+
with:
|
|
29
|
+
name: python-package-distributions
|
|
30
|
+
path: dist/*
|
|
31
|
+
|
|
32
|
+
publish:
|
|
33
|
+
needs: build
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
environment: pypi
|
|
36
|
+
permissions:
|
|
37
|
+
id-token: write
|
|
38
|
+
steps:
|
|
39
|
+
- name: Download dist artifacts
|
|
40
|
+
uses: actions/download-artifact@v4
|
|
41
|
+
with:
|
|
42
|
+
name: python-package-distributions
|
|
43
|
+
path: dist/
|
|
44
|
+
|
|
45
|
+
- name: Publish package distributions to PyPI
|
|
46
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tkgo11
|
|
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,180 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llm-cookie-bridge
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Unified async cookie/session bridge for major AI web apps.
|
|
5
|
+
Project-URL: Homepage, https://github.com/tkgo11/LLMCookieBridge
|
|
6
|
+
Project-URL: Repository, https://github.com/tkgo11/LLMCookieBridge
|
|
7
|
+
Project-URL: Issues, https://github.com/tkgo11/LLMCookieBridge/issues
|
|
8
|
+
Author: tkgo11
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: async,chatgpt,claude,cookies,gemini,perplexity
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: httpx<1,>=0.27
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: twine>=5.0; extra == 'dev'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# LLMCookieBridge
|
|
31
|
+
|
|
32
|
+
`llm-cookie-bridge` is a unified async Python library for talking to major AI web apps using browser cookies or session-derived web tokens instead of official API keys.
|
|
33
|
+
|
|
34
|
+
> Warning
|
|
35
|
+
>
|
|
36
|
+
> This package targets reverse-engineered web endpoints that may change without notice. Treat it as an unstable bridge, not a production SLA surface.
|
|
37
|
+
|
|
38
|
+
## Implemented providers
|
|
39
|
+
|
|
40
|
+
- Google Gemini web
|
|
41
|
+
- ChatGPT / OpenAI web
|
|
42
|
+
- Claude web
|
|
43
|
+
- Perplexity web
|
|
44
|
+
|
|
45
|
+
## Design notes
|
|
46
|
+
|
|
47
|
+
- `httpx.AsyncClient` transport
|
|
48
|
+
- per-provider auth bootstrap + best-effort refresh
|
|
49
|
+
- unified async chat and streaming interface
|
|
50
|
+
- minimal dependencies
|
|
51
|
+
- unit-tested request builders and parsers with mocked transports
|
|
52
|
+
- secure defaults: provider hosts are pinned and redirects are disabled unless explicitly opted in
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install llm-cookie-bridge
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Quick start
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import asyncio
|
|
64
|
+
import os
|
|
65
|
+
from llm_cookie_bridge import LLMCookieBridge
|
|
66
|
+
|
|
67
|
+
async def main() -> None:
|
|
68
|
+
bridge = LLMCookieBridge.create(
|
|
69
|
+
"chatgpt",
|
|
70
|
+
cookies={
|
|
71
|
+
"__Secure-next-auth.session-token": os.environ["CHATGPT_SESSION_TOKEN"],
|
|
72
|
+
},
|
|
73
|
+
)
|
|
74
|
+
async with bridge:
|
|
75
|
+
response = await bridge.chat("Say hello in one sentence.")
|
|
76
|
+
print(response.text)
|
|
77
|
+
|
|
78
|
+
async for chunk in bridge.stream("Stream a short poem."):
|
|
79
|
+
print(chunk.delta, end="", flush=True)
|
|
80
|
+
|
|
81
|
+
asyncio.run(main())
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Provider examples
|
|
85
|
+
|
|
86
|
+
### Gemini
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import os
|
|
90
|
+
|
|
91
|
+
bridge = LLMCookieBridge.create(
|
|
92
|
+
"gemini",
|
|
93
|
+
cookies={
|
|
94
|
+
"__Secure-1PSID": os.environ["GEMINI_1PSID"],
|
|
95
|
+
"__Secure-1PSIDTS": os.environ["GEMINI_1PSIDTS"],
|
|
96
|
+
},
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### ChatGPT / OpenAI web
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import os
|
|
104
|
+
|
|
105
|
+
bridge = LLMCookieBridge.create(
|
|
106
|
+
"chatgpt",
|
|
107
|
+
cookies={"__Secure-next-auth.session-token": os.environ["CHATGPT_SESSION_TOKEN"]},
|
|
108
|
+
)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Claude
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
import os
|
|
115
|
+
|
|
116
|
+
bridge = LLMCookieBridge.create(
|
|
117
|
+
"claude",
|
|
118
|
+
cookie_header=os.environ["CLAUDE_COOKIE_HEADER"],
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Perplexity
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
import os
|
|
126
|
+
|
|
127
|
+
bridge = LLMCookieBridge.create(
|
|
128
|
+
"perplexity",
|
|
129
|
+
cookies={"__Secure-next-auth.session-token": os.environ["PERPLEXITY_SESSION_TOKEN"]},
|
|
130
|
+
)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Auto refresh
|
|
134
|
+
|
|
135
|
+
Each provider exposes a best-effort `refresh()` implementation:
|
|
136
|
+
|
|
137
|
+
- Gemini reboots app state and extracts `SNlM0e`, `bl`, and `f.sid`
|
|
138
|
+
- ChatGPT re-fetches a bearer token from the web session endpoint when a next-auth cookie is present
|
|
139
|
+
- Claude re-discovers the organization UUID and can use a custom refresh callback for cookie renewal
|
|
140
|
+
- Perplexity re-primes the next-auth session endpoint and can use a custom refresh callback
|
|
141
|
+
|
|
142
|
+
You can also inject a custom refresh callback:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
async def refresh_cookies(provider_name: str):
|
|
146
|
+
return {"sessionKey": "new-cookie-value"}
|
|
147
|
+
|
|
148
|
+
bridge = LLMCookieBridge.create(
|
|
149
|
+
"claude",
|
|
150
|
+
cookie_header=os.environ["CLAUDE_COOKIE_HEADER"],
|
|
151
|
+
refresh_callback=refresh_cookies,
|
|
152
|
+
)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Security notes
|
|
156
|
+
|
|
157
|
+
- Do not pass untrusted values into `cookies`, `cookie_header`, `headers`, or `base_url`.
|
|
158
|
+
- `base_url` overrides are pinned to the provider host by default; cross-host overrides require `allow_custom_base_url=True`.
|
|
159
|
+
- Custom `authorization`, `cookie`, `host`, `origin`, and `referer` headers are intentionally rejected.
|
|
160
|
+
- Treat each bridge/provider instance as single-session and single-tenant; do not reuse one instance across multiple users.
|
|
161
|
+
|
|
162
|
+
## API surface
|
|
163
|
+
|
|
164
|
+
- `await bridge.chat(message, **kwargs)`
|
|
165
|
+
- `async for chunk in bridge.stream(message, **kwargs)`
|
|
166
|
+
- `await bridge.refresh(force=True)`
|
|
167
|
+
|
|
168
|
+
## Research references
|
|
169
|
+
|
|
170
|
+
These informed the request shapes and auth bootstraps, but are **not dependencies**:
|
|
171
|
+
|
|
172
|
+
- Gemini: `HanaokaYuzu/Gemini-API`
|
|
173
|
+
- ChatGPT: `acheong08/ChatGPT`, `lanqian528/chat2api`
|
|
174
|
+
- Claude: `Xerxes-2/clewdr`, `st1vms/unofficial-claude-api`, `KoushikNavuluri/Claude-API`
|
|
175
|
+
- Perplexity: `helallao/perplexity-ai`, `henrique-coder/perplexity-webui-scraper`, `nathanrchn/perplexityai`
|
|
176
|
+
|
|
177
|
+
## Publishing
|
|
178
|
+
|
|
179
|
+
This repository is configured for PyPI Trusted Publishing from GitHub Actions using `.github/workflows/publish.yml`.
|
|
180
|
+
Create a GitHub Release after configuring the Trusted Publisher on PyPI to publish a new version automatically.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# LLMCookieBridge
|
|
2
|
+
|
|
3
|
+
`llm-cookie-bridge` is a unified async Python library for talking to major AI web apps using browser cookies or session-derived web tokens instead of official API keys.
|
|
4
|
+
|
|
5
|
+
> Warning
|
|
6
|
+
>
|
|
7
|
+
> This package targets reverse-engineered web endpoints that may change without notice. Treat it as an unstable bridge, not a production SLA surface.
|
|
8
|
+
|
|
9
|
+
## Implemented providers
|
|
10
|
+
|
|
11
|
+
- Google Gemini web
|
|
12
|
+
- ChatGPT / OpenAI web
|
|
13
|
+
- Claude web
|
|
14
|
+
- Perplexity web
|
|
15
|
+
|
|
16
|
+
## Design notes
|
|
17
|
+
|
|
18
|
+
- `httpx.AsyncClient` transport
|
|
19
|
+
- per-provider auth bootstrap + best-effort refresh
|
|
20
|
+
- unified async chat and streaming interface
|
|
21
|
+
- minimal dependencies
|
|
22
|
+
- unit-tested request builders and parsers with mocked transports
|
|
23
|
+
- secure defaults: provider hosts are pinned and redirects are disabled unless explicitly opted in
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install llm-cookie-bridge
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import asyncio
|
|
35
|
+
import os
|
|
36
|
+
from llm_cookie_bridge import LLMCookieBridge
|
|
37
|
+
|
|
38
|
+
async def main() -> None:
|
|
39
|
+
bridge = LLMCookieBridge.create(
|
|
40
|
+
"chatgpt",
|
|
41
|
+
cookies={
|
|
42
|
+
"__Secure-next-auth.session-token": os.environ["CHATGPT_SESSION_TOKEN"],
|
|
43
|
+
},
|
|
44
|
+
)
|
|
45
|
+
async with bridge:
|
|
46
|
+
response = await bridge.chat("Say hello in one sentence.")
|
|
47
|
+
print(response.text)
|
|
48
|
+
|
|
49
|
+
async for chunk in bridge.stream("Stream a short poem."):
|
|
50
|
+
print(chunk.delta, end="", flush=True)
|
|
51
|
+
|
|
52
|
+
asyncio.run(main())
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Provider examples
|
|
56
|
+
|
|
57
|
+
### Gemini
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import os
|
|
61
|
+
|
|
62
|
+
bridge = LLMCookieBridge.create(
|
|
63
|
+
"gemini",
|
|
64
|
+
cookies={
|
|
65
|
+
"__Secure-1PSID": os.environ["GEMINI_1PSID"],
|
|
66
|
+
"__Secure-1PSIDTS": os.environ["GEMINI_1PSIDTS"],
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### ChatGPT / OpenAI web
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import os
|
|
75
|
+
|
|
76
|
+
bridge = LLMCookieBridge.create(
|
|
77
|
+
"chatgpt",
|
|
78
|
+
cookies={"__Secure-next-auth.session-token": os.environ["CHATGPT_SESSION_TOKEN"]},
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Claude
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import os
|
|
86
|
+
|
|
87
|
+
bridge = LLMCookieBridge.create(
|
|
88
|
+
"claude",
|
|
89
|
+
cookie_header=os.environ["CLAUDE_COOKIE_HEADER"],
|
|
90
|
+
)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Perplexity
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
import os
|
|
97
|
+
|
|
98
|
+
bridge = LLMCookieBridge.create(
|
|
99
|
+
"perplexity",
|
|
100
|
+
cookies={"__Secure-next-auth.session-token": os.environ["PERPLEXITY_SESSION_TOKEN"]},
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Auto refresh
|
|
105
|
+
|
|
106
|
+
Each provider exposes a best-effort `refresh()` implementation:
|
|
107
|
+
|
|
108
|
+
- Gemini reboots app state and extracts `SNlM0e`, `bl`, and `f.sid`
|
|
109
|
+
- ChatGPT re-fetches a bearer token from the web session endpoint when a next-auth cookie is present
|
|
110
|
+
- Claude re-discovers the organization UUID and can use a custom refresh callback for cookie renewal
|
|
111
|
+
- Perplexity re-primes the next-auth session endpoint and can use a custom refresh callback
|
|
112
|
+
|
|
113
|
+
You can also inject a custom refresh callback:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
async def refresh_cookies(provider_name: str):
|
|
117
|
+
return {"sessionKey": "new-cookie-value"}
|
|
118
|
+
|
|
119
|
+
bridge = LLMCookieBridge.create(
|
|
120
|
+
"claude",
|
|
121
|
+
cookie_header=os.environ["CLAUDE_COOKIE_HEADER"],
|
|
122
|
+
refresh_callback=refresh_cookies,
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Security notes
|
|
127
|
+
|
|
128
|
+
- Do not pass untrusted values into `cookies`, `cookie_header`, `headers`, or `base_url`.
|
|
129
|
+
- `base_url` overrides are pinned to the provider host by default; cross-host overrides require `allow_custom_base_url=True`.
|
|
130
|
+
- Custom `authorization`, `cookie`, `host`, `origin`, and `referer` headers are intentionally rejected.
|
|
131
|
+
- Treat each bridge/provider instance as single-session and single-tenant; do not reuse one instance across multiple users.
|
|
132
|
+
|
|
133
|
+
## API surface
|
|
134
|
+
|
|
135
|
+
- `await bridge.chat(message, **kwargs)`
|
|
136
|
+
- `async for chunk in bridge.stream(message, **kwargs)`
|
|
137
|
+
- `await bridge.refresh(force=True)`
|
|
138
|
+
|
|
139
|
+
## Research references
|
|
140
|
+
|
|
141
|
+
These informed the request shapes and auth bootstraps, but are **not dependencies**:
|
|
142
|
+
|
|
143
|
+
- Gemini: `HanaokaYuzu/Gemini-API`
|
|
144
|
+
- ChatGPT: `acheong08/ChatGPT`, `lanqian528/chat2api`
|
|
145
|
+
- Claude: `Xerxes-2/clewdr`, `st1vms/unofficial-claude-api`, `KoushikNavuluri/Claude-API`
|
|
146
|
+
- Perplexity: `helallao/perplexity-ai`, `henrique-coder/perplexity-webui-scraper`, `nathanrchn/perplexityai`
|
|
147
|
+
|
|
148
|
+
## Publishing
|
|
149
|
+
|
|
150
|
+
This repository is configured for PyPI Trusted Publishing from GitHub Actions using `.github/workflows/publish.yml`.
|
|
151
|
+
Create a GitHub Release after configuring the Trusted Publisher on PyPI to publish a new version automatically.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "llm-cookie-bridge"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Unified async cookie/session bridge for major AI web apps."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "tkgo11" }]
|
|
13
|
+
keywords = ["async", "cookies", "chatgpt", "claude", "gemini", "perplexity"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Framework :: AsyncIO",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"httpx>=0.27,<1",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = [
|
|
31
|
+
"build>=1.2",
|
|
32
|
+
"pytest>=8.0",
|
|
33
|
+
"pytest-asyncio>=0.23",
|
|
34
|
+
"twine>=5.0",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/tkgo11/LLMCookieBridge"
|
|
39
|
+
Repository = "https://github.com/tkgo11/LLMCookieBridge"
|
|
40
|
+
Issues = "https://github.com/tkgo11/LLMCookieBridge/issues"
|
|
41
|
+
|
|
42
|
+
[tool.hatch.build.targets.wheel]
|
|
43
|
+
packages = ["src/llm_cookie_bridge"]
|
|
44
|
+
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
asyncio_mode = "auto"
|
|
47
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .client import LLMCookieBridge
|
|
2
|
+
from .exceptions import (
|
|
3
|
+
AuthenticationError,
|
|
4
|
+
BridgeError,
|
|
5
|
+
ParseError,
|
|
6
|
+
ProviderResponseError,
|
|
7
|
+
RateLimitError,
|
|
8
|
+
)
|
|
9
|
+
from .types import ChatChunk, ChatResponse, CookieRefreshResult
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"AuthenticationError",
|
|
13
|
+
"BridgeError",
|
|
14
|
+
"ChatChunk",
|
|
15
|
+
"ChatResponse",
|
|
16
|
+
"CookieRefreshResult",
|
|
17
|
+
"LLMCookieBridge",
|
|
18
|
+
"ParseError",
|
|
19
|
+
"ProviderResponseError",
|
|
20
|
+
"RateLimitError",
|
|
21
|
+
]
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, AsyncIterator, Literal
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from .providers import ChatGPTProvider, ClaudeProvider, GeminiProvider, PerplexityProvider
|
|
8
|
+
from .providers.base import BaseProvider
|
|
9
|
+
from .types import ChatChunk, ChatResponse
|
|
10
|
+
|
|
11
|
+
ProviderName = Literal["gemini", "chatgpt", "claude", "perplexity"]
|
|
12
|
+
|
|
13
|
+
_PROVIDERS: dict[str, type[BaseProvider]] = {
|
|
14
|
+
"gemini": GeminiProvider,
|
|
15
|
+
"chatgpt": ChatGPTProvider,
|
|
16
|
+
"claude": ClaudeProvider,
|
|
17
|
+
"perplexity": PerplexityProvider,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class LLMCookieBridge:
|
|
22
|
+
def __init__(self, provider: BaseProvider) -> None:
|
|
23
|
+
self.provider = provider
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def create(
|
|
27
|
+
cls,
|
|
28
|
+
provider: ProviderName,
|
|
29
|
+
*,
|
|
30
|
+
cookies: dict[str, str] | None = None,
|
|
31
|
+
cookie_header: str | None = None,
|
|
32
|
+
headers: dict[str, str] | None = None,
|
|
33
|
+
timeout: float = 30.0,
|
|
34
|
+
transport: httpx.AsyncBaseTransport | None = None,
|
|
35
|
+
refresh_callback: Any = None,
|
|
36
|
+
allow_custom_base_url: bool = False,
|
|
37
|
+
follow_redirects: bool = False,
|
|
38
|
+
**provider_kwargs: Any,
|
|
39
|
+
) -> "LLMCookieBridge":
|
|
40
|
+
provider_cls = _PROVIDERS[provider]
|
|
41
|
+
instance = provider_cls(
|
|
42
|
+
cookies=cookies,
|
|
43
|
+
cookie_header=cookie_header,
|
|
44
|
+
headers=headers,
|
|
45
|
+
timeout=timeout,
|
|
46
|
+
transport=transport,
|
|
47
|
+
refresh_callback=refresh_callback,
|
|
48
|
+
allow_custom_base_url=allow_custom_base_url,
|
|
49
|
+
follow_redirects=follow_redirects,
|
|
50
|
+
**provider_kwargs,
|
|
51
|
+
)
|
|
52
|
+
return cls(instance)
|
|
53
|
+
|
|
54
|
+
async def refresh(self, force: bool = False) -> None:
|
|
55
|
+
await self.provider.refresh(force=force)
|
|
56
|
+
|
|
57
|
+
async def chat(self, message: str, **kwargs: Any) -> ChatResponse:
|
|
58
|
+
return await self.provider.chat(message, **kwargs)
|
|
59
|
+
|
|
60
|
+
async def stream(self, message: str, **kwargs: Any) -> AsyncIterator[ChatChunk]:
|
|
61
|
+
async for chunk in self.provider.stream_chat(message, **kwargs):
|
|
62
|
+
yield chunk
|
|
63
|
+
|
|
64
|
+
async def aclose(self) -> None:
|
|
65
|
+
await self.provider.close()
|
|
66
|
+
|
|
67
|
+
async def __aenter__(self) -> "LLMCookieBridge":
|
|
68
|
+
return self
|
|
69
|
+
|
|
70
|
+
async def __aexit__(self, *_: object) -> None:
|
|
71
|
+
await self.aclose()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
class BridgeError(Exception):
|
|
4
|
+
"""Base exception for llm-cookie-bridge."""
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AuthenticationError(BridgeError):
|
|
8
|
+
"""Raised when a provider cannot authenticate with the supplied cookies/session."""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ProviderResponseError(BridgeError):
|
|
12
|
+
"""Raised when a provider returns a non-successful response."""
|
|
13
|
+
|
|
14
|
+
def __init__(self, provider: str, status_code: int, message: str) -> None:
|
|
15
|
+
super().__init__(f"{provider} returned HTTP {status_code}: {message}")
|
|
16
|
+
self.provider = provider
|
|
17
|
+
self.status_code = status_code
|
|
18
|
+
self.message = message
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ParseError(BridgeError):
|
|
22
|
+
"""Raised when a provider response cannot be parsed."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class RateLimitError(BridgeError):
|
|
26
|
+
"""Raised when a provider indicates a rate or usage limit."""
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .base import BaseProvider
|
|
2
|
+
from .chatgpt import ChatGPTProvider
|
|
3
|
+
from .claude import ClaudeProvider
|
|
4
|
+
from .gemini import GeminiProvider
|
|
5
|
+
from .perplexity import PerplexityProvider
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"BaseProvider",
|
|
9
|
+
"ChatGPTProvider",
|
|
10
|
+
"ClaudeProvider",
|
|
11
|
+
"GeminiProvider",
|
|
12
|
+
"PerplexityProvider",
|
|
13
|
+
]
|