jh-web-gateway 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.
package/README.md ADDED
@@ -0,0 +1,182 @@
1
+ # jh-web-gateway
2
+
3
+ Local HTTP server that exposes an **OpenAI-compatible API** backed by the JH web platform ([chat.ai.jh.edu](https://chat.ai.jh.edu)). It connects to Chrome via CDP, captures your JH session, and proxies requests through the browser — bypassing Cloudflare and using your existing JH credentials.
4
+
5
+ Use it to plug JH-hosted models (Claude, GPT, Llama) into any tool that speaks the OpenAI protocol: Cursor, Continue, aider, Open Interpreter, custom scripts, etc.
6
+
7
+ ## Requirements
8
+
9
+ - Node.js 22+
10
+ - Google Chrome
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install -g jh-web-gateway
16
+ ```
17
+
18
+ ## Getting Started
19
+
20
+ ### First run — log in once
21
+
22
+ ```bash
23
+ jh-gateway start
24
+ ```
25
+
26
+ This launches Chrome, opens [chat.ai.jh.edu](https://chat.ai.jh.edu), and waits for you to sign in. Once you do, it captures your credentials, minimizes Chrome, and starts the gateway server. Your credentials are saved to `~/.jh-gateway/config.json` for future runs.
27
+
28
+ ### Subsequent runs — headless
29
+
30
+ ```bash
31
+ jh-gateway start --headless
32
+ ```
33
+
34
+ No browser window. Credentials are loaded from the config, and a background token refresher keeps them alive automatically. If the session ever expires, run `jh-gateway start` (without `--headless`) to re-login.
35
+
36
+ ### Connect your tools
37
+
38
+ Point any OpenAI-compatible tool at:
39
+ - Base URL: `http://127.0.0.1:8741/v1`
40
+ - API Key: printed at startup as `API Key: jh-local-...`
41
+
42
+ ## Usage
43
+
44
+ ### curl
45
+
46
+ ```bash
47
+ # Health check
48
+ curl http://127.0.0.1:8741/health
49
+
50
+ # List available models
51
+ curl http://127.0.0.1:8741/v1/models \
52
+ -H "Authorization: Bearer <your-api-key>"
53
+
54
+ # Chat completion
55
+ curl http://127.0.0.1:8741/v1/chat/completions \
56
+ -H "Content-Type: application/json" \
57
+ -H "Authorization: Bearer <your-api-key>" \
58
+ -d '{
59
+ "model": "claude-sonnet-4.5",
60
+ "messages": [{"role": "user", "content": "Hello"}]
61
+ }'
62
+
63
+ # Streaming
64
+ curl http://127.0.0.1:8741/v1/chat/completions \
65
+ -H "Content-Type: application/json" \
66
+ -H "Authorization: Bearer <your-api-key>" \
67
+ -d '{
68
+ "model": "claude-sonnet-4.5",
69
+ "stream": true,
70
+ "messages": [{"role": "user", "content": "Hello"}]
71
+ }'
72
+ ```
73
+
74
+ ### Python (OpenAI SDK)
75
+
76
+ ```python
77
+ from openai import OpenAI
78
+
79
+ client = OpenAI(
80
+ base_url="http://127.0.0.1:8741/v1",
81
+ api_key="jh-local-..." # printed at startup
82
+ )
83
+
84
+ response = client.chat.completions.create(
85
+ model="claude-opus-4.5",
86
+ messages=[{"role": "user", "content": "Explain quicksort"}]
87
+ )
88
+ print(response.choices[0].message.content)
89
+ ```
90
+
91
+ ### Node.js (OpenAI SDK)
92
+
93
+ ```typescript
94
+ import OpenAI from "openai";
95
+
96
+ const client = new OpenAI({
97
+ baseURL: "http://127.0.0.1:8741/v1",
98
+ apiKey: "jh-local-...",
99
+ });
100
+
101
+ const completion = await client.chat.completions.create({
102
+ model: "claude-sonnet-4.5",
103
+ messages: [{ role: "user", content: "Hello" }],
104
+ });
105
+ console.log(completion.choices[0].message.content);
106
+ ```
107
+
108
+ ### Cursor / Continue / Other Tools
109
+
110
+ Point any OpenAI-compatible tool at:
111
+ - Base URL: `http://127.0.0.1:8741/v1`
112
+ - API Key: your `jh-local-...` key from startup
113
+
114
+ ## CLI Reference
115
+
116
+ | Command | Description |
117
+ |---------|-------------|
118
+ | `jh-gateway start` | Launch Chrome, authenticate, and start the gateway |
119
+ | `jh-gateway setup` | Interactive setup wizard (legacy) |
120
+ | `jh-gateway serve` | Start the HTTP server only (legacy) |
121
+ | `jh-gateway auth` | Re-capture JH credentials manually |
122
+ | `jh-gateway config` | Print current config (credentials redacted) |
123
+ | `jh-gateway status` | Show Chrome connection, token expiry, gateway state |
124
+ | `jh-gateway logs` | Display recent request logs |
125
+
126
+ ### `start` options
127
+
128
+ | Flag | Description |
129
+ |------|-------------|
130
+ | `--headless` | Launch Chrome without a visible window (requires prior login) |
131
+ | `--port <n>` | Override the configured port |
132
+ | `--pages <n>` | Max concurrent browser pages (default: 3) |
133
+
134
+ ## Token Refresh
135
+
136
+ While the gateway is running, a background process checks your token every 60 seconds. If it's within 5 minutes of expiry, it silently reloads the JH page in Chrome and captures a fresh token — no interruption to in-flight requests. If refresh fails after 3 retries, a warning is printed to the terminal.
137
+
138
+ ## Available Models
139
+
140
+ | Model | Provider |
141
+ |-------|----------|
142
+ | `claude-opus-4.5` | Anthropic |
143
+ | `claude-sonnet-4.5` | Anthropic |
144
+ | `claude-haiku-4.5` | Anthropic |
145
+ | `gpt-4.1` | OpenAI |
146
+ | `gpt-5` | OpenAI |
147
+ | `gpt-5.1` | OpenAI |
148
+ | `gpt-5.2` | OpenAI |
149
+ | `o3` | OpenAI |
150
+ | `o3-mini` | OpenAI |
151
+ | `llama3-3-70b-instruct` | Meta |
152
+
153
+ ## Configuration
154
+
155
+ Config is stored at `~/.jh-gateway/config.json`. Edit directly or use the CLI:
156
+
157
+ ```json
158
+ {
159
+ "cdpUrl": "http://127.0.0.1:9222",
160
+ "port": 8741,
161
+ "defaultModel": "claude-opus-4.5",
162
+ "auth": { "mode": "bearer", "token": "jh-local-..." },
163
+ "maxQueueWaitMs": 120000
164
+ }
165
+ ```
166
+
167
+ ## How It Works
168
+
169
+ 1. Connects to Chrome via CDP (Chrome DevTools Protocol)
170
+ 2. Captures your JH session credentials from the browser
171
+ 3. Runs an HTTP server that accepts OpenAI-format requests
172
+ 4. Proxies requests through the browser's authenticated session
173
+ 5. Translates JH SSE responses back to OpenAI format
174
+ 6. Proactively refreshes tokens before they expire
175
+
176
+ ## Development
177
+
178
+ ```bash
179
+ npm test # run tests
180
+ npm run build # build with tsup
181
+ npm run dev # build in watch mode
182
+ ```