grok-cli-to-openai-compatible 1.2.0 → 1.2.4
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-ZH.md +398 -0
- package/README.md +246 -332
- package/dist/cli/commands/migrate.d.ts.map +1 -1
- package/dist/cli/commands/migrate.js +3 -3
- package/dist/cli/commands/migrate.js.map +1 -1
- package/dist/cli/commands/seed.d.ts.map +1 -1
- package/dist/cli/commands/seed.js +6 -22
- package/dist/cli/commands/seed.js.map +1 -1
- package/dist/cli/commands/setup.d.ts.map +1 -1
- package/dist/cli/commands/setup.js +19 -34
- package/dist/cli/commands/setup.js.map +1 -1
- package/dist/cli/lib/run-prisma.d.ts +13 -0
- package/dist/cli/lib/run-prisma.d.ts.map +1 -0
- package/dist/cli/lib/run-prisma.js +99 -0
- package/dist/cli/lib/run-prisma.js.map +1 -0
- package/dist/cli/lib/seed-admin.d.ts +17 -0
- package/dist/cli/lib/seed-admin.d.ts.map +1 -0
- package/dist/cli/lib/seed-admin.js +87 -0
- package/dist/cli/lib/seed-admin.js.map +1 -0
- package/dist/services/document.service.d.ts +3 -3
- package/dist/services/update.service.d.ts.map +1 -1
- package/dist/services/update.service.js +3 -4
- package/dist/services/update.service.js.map +1 -1
- package/package.json +12 -9
- package/scripts/install.sh +53 -0
- package/scripts/postinstall.cjs +5 -0
- package/scripts/prepare.cjs +99 -0
- package/scripts/smoke.sh +32 -0
package/README-ZH.md
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# Grok CLI → OpenAI 相容 Gateway
|
|
2
|
+
|
|
3
|
+
[](https://github.com/yanshekki/Grok-Cli-to-OpenAI-compatible/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/grok-cli-to-openai-compatible)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](#預設-port)
|
|
8
|
+
|
|
9
|
+
**語言:** [English](./README.md) · 中文
|
|
10
|
+
|
|
11
|
+
將本機 **[Grok CLI](https://x.ai)**(`grok -p` headless)包裝成可上線的 **OpenAI 相容 HTTP API**。
|
|
12
|
+
|
|
13
|
+
| | |
|
|
14
|
+
|--|--|
|
|
15
|
+
| **npm** | [`grok-cli-to-openai-compatible`](https://www.npmjs.com/package/grok-cli-to-openai-compatible) |
|
|
16
|
+
| **CLI** | `gctoac` · 短名 `gcoa` |
|
|
17
|
+
| **預設 port** | **`3847`** |
|
|
18
|
+
|
|
19
|
+
**主要能力**
|
|
20
|
+
|
|
21
|
+
- OpenAI 相容 `POST /v1/chat/completions`(stream / 非 stream)
|
|
22
|
+
- Thinking / `reasoning_content`(DeepSeek 風格 + Grok `thought`)
|
|
23
|
+
- 每把 key 的 **safe** / **agent** 政策
|
|
24
|
+
- AES-256-GCM 加密 + 完整 chat 稽核
|
|
25
|
+
- Admin Panel:`/admin`(解密 prompt 輸入輸出、金鑰、一鍵更新)
|
|
26
|
+
- 控制 CLI:`gctoac setup | start | stop | status | update`
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
Client (OpenAI SDK / curl / Open WebUI)
|
|
30
|
+
│ Authorization: Bearer gk_live_...
|
|
31
|
+
▼
|
|
32
|
+
Express Gateway :3847
|
|
33
|
+
· 認證 · 限流 · safe/agent
|
|
34
|
+
· 加密稽核 · Admin /admin
|
|
35
|
+
· gctoac start | stop | status | update
|
|
36
|
+
│
|
|
37
|
+
▼
|
|
38
|
+
grok -p … (本機 Grok CLI)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## 快速開始
|
|
44
|
+
|
|
45
|
+
### 1. 前置要求
|
|
46
|
+
|
|
47
|
+
- **Node.js** ≥ 20
|
|
48
|
+
- 已安裝並登入 **Grok CLI**:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
curl -fsSL https://x.ai/cli/install.sh | bash
|
|
52
|
+
grok login
|
|
53
|
+
grok --version
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 2. 安裝並啟動
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install -g grok-cli-to-openai-compatible
|
|
60
|
+
|
|
61
|
+
gctoac doctor # 檢查 Node / Grok / 環境
|
|
62
|
+
gctoac setup # 建立 ~/.gctoac、.env、資料庫、admin API key
|
|
63
|
+
gctoac start # http://127.0.0.1:3847
|
|
64
|
+
gctoac status
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
開啟 Admin(貼上 `setup` 印出的 **admin API key**,只顯示一次):
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
http://127.0.0.1:3847/admin/
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**資料目錄:** `~/.gctoac/`
|
|
74
|
+
可用 `GCTOAC_HOME` 或 `gctoac --home /path` 覆蓋。
|
|
75
|
+
|
|
76
|
+
### 3. 呼叫 API
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
export API_KEY=gk_live_... # setup 或 Admin 取得
|
|
80
|
+
|
|
81
|
+
curl -s http://127.0.0.1:3847/v1/chat/completions \
|
|
82
|
+
-H "Authorization: Bearer $API_KEY" \
|
|
83
|
+
-H "Content-Type: application/json" \
|
|
84
|
+
-d '{
|
|
85
|
+
"model": "grok-4.5",
|
|
86
|
+
"messages": [{"role":"user","content":"用一個字打招呼"}]
|
|
87
|
+
}'
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## 安裝方式
|
|
93
|
+
|
|
94
|
+
### 全域安裝(建議)
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
npm install -g grok-cli-to-openai-compatible
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 專案依賴
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm install grok-cli-to-openai-compatible
|
|
104
|
+
npx gctoac setup
|
|
105
|
+
npx gctoac start --foreground
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 由原始碼安裝
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
git clone https://github.com/yanshekki/Grok-Cli-to-OpenAI-compatible.git
|
|
112
|
+
cd Grok-Cli-to-OpenAI-compatible
|
|
113
|
+
npm install
|
|
114
|
+
npm run build
|
|
115
|
+
npm link # 可選:把 gctoac 掛到 PATH
|
|
116
|
+
gctoac setup
|
|
117
|
+
gctoac start
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
一鍵腳本(clone 到 `~/.gctoac/src` 再 `npm link`):
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
curl -fsSL https://raw.githubusercontent.com/yanshekki/Grok-Cli-to-OpenAI-compatible/main/scripts/install.sh | bash
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
> 請優先使用 **`npm install -g grok-cli-to-openai-compatible`**。
|
|
127
|
+
> 不建議 `npm install -g github:yanshekki/...`(部分 npm 版本會失敗)。
|
|
128
|
+
|
|
129
|
+
### 更新
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
gctoac update # 更新套件並重啟
|
|
133
|
+
gctoac update --check # 只檢查是否有新版本
|
|
134
|
+
gctoac update --no-restart # 更新但不重啟
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
亦可在 Admin → **系統狀態** → 一鍵更新。
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 預設 port
|
|
142
|
+
|
|
143
|
+
| URL | 用途 |
|
|
144
|
+
|-----|------|
|
|
145
|
+
| `http://127.0.0.1:3847/v1` | OpenAI 相容 API base |
|
|
146
|
+
| `http://127.0.0.1:3847/admin/` | Admin 控制台 |
|
|
147
|
+
| `http://127.0.0.1:3847/health` | 健康檢查 |
|
|
148
|
+
|
|
149
|
+
覆蓋方式:`.env` 設 `PORT=`,或:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
gctoac --port 3847 start
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## CLI(`gctoac` / `gcoa`)
|
|
158
|
+
|
|
159
|
+
| 指令 | 說明 |
|
|
160
|
+
|------|------|
|
|
161
|
+
| `gctoac setup` | 建目錄、`.env`、migrate、seed admin key |
|
|
162
|
+
| `gctoac start` | 背景啟動 gateway |
|
|
163
|
+
| `gctoac start -f` | 前景啟動 |
|
|
164
|
+
| `gctoac stop` | 停止背景進程 |
|
|
165
|
+
| `gctoac restart` | 重啟 |
|
|
166
|
+
| `gctoac status` | 顯示 PID + health |
|
|
167
|
+
| `gctoac migrate` | 跑 Prisma migration |
|
|
168
|
+
| `gctoac seed` | 產生 admin API key |
|
|
169
|
+
| `gctoac doctor` | 檢查環境 |
|
|
170
|
+
| `gctoac update` | 自我更新後重啟 |
|
|
171
|
+
| `gctoac update --check` | 只檢查有無新版本 |
|
|
172
|
+
| `gctoac update --no-restart` | 更新但不重啟 |
|
|
173
|
+
| `gctoac open` | 印出 API / Admin URL |
|
|
174
|
+
| `gctoac version` | 顯示版本 |
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
gctoac --home ~/.gctoac-alt setup
|
|
178
|
+
gctoac --port 3847 start
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## 功能一覽
|
|
184
|
+
|
|
185
|
+
| 功能 | 說明 |
|
|
186
|
+
|------|------|
|
|
187
|
+
| OpenAI API | `POST /v1/chat/completions`(stream / 非 stream)、`GET /v1/models` |
|
|
188
|
+
| Thinking | `reasoning_content` + Grok `thought` + `grok.*` |
|
|
189
|
+
| 文件 | 加密上傳;可用 `document_ids` 注入 chat |
|
|
190
|
+
| Safe / Agent | 每 key 政策;可全域強制 safe |
|
|
191
|
+
| 加密 | AES-256-GCM 加密 prompt、response、檔案 |
|
|
192
|
+
| Admin Panel | 儀表板、完整解密 chat、keys、文件、audit、設定、一鍵更新 |
|
|
193
|
+
| CLI | 生命週期控制 + 自我更新 |
|
|
194
|
+
| 運維 | SQLite、PM2、GitHub Actions CI |
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## API
|
|
199
|
+
|
|
200
|
+
受保護路由需要:
|
|
201
|
+
|
|
202
|
+
```http
|
|
203
|
+
Authorization: Bearer gk_live_...
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Chat(stream)
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
curl -sN http://127.0.0.1:3847/v1/chat/completions \
|
|
210
|
+
-H "Authorization: Bearer $API_KEY" \
|
|
211
|
+
-H "Content-Type: application/json" \
|
|
212
|
+
-d '{
|
|
213
|
+
"model": "grok-4.5",
|
|
214
|
+
"stream": true,
|
|
215
|
+
"include_reasoning": true,
|
|
216
|
+
"messages": [{"role":"user","content":"數到 3"}]
|
|
217
|
+
}'
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### OpenAI SDK
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import OpenAI from 'openai';
|
|
224
|
+
|
|
225
|
+
const client = new OpenAI({
|
|
226
|
+
apiKey: process.env.API_KEY,
|
|
227
|
+
baseURL: 'http://127.0.0.1:3847/v1',
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const res = await client.chat.completions.create({
|
|
231
|
+
model: 'grok-4.5',
|
|
232
|
+
messages: [{ role: 'user', content: 'Hello' }],
|
|
233
|
+
});
|
|
234
|
+
console.log(res.choices[0].message.content);
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Chat body 擴充欄位
|
|
238
|
+
|
|
239
|
+
```json
|
|
240
|
+
{
|
|
241
|
+
"model": "grok-4.5",
|
|
242
|
+
"messages": [{ "role": "user", "content": "Hello" }],
|
|
243
|
+
"stream": false,
|
|
244
|
+
"include_reasoning": true,
|
|
245
|
+
"cwd": "/allowed/workspace",
|
|
246
|
+
"session_id": "optional-session",
|
|
247
|
+
"document_ids": ["uuid"]
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
| 欄位 | 說明 |
|
|
252
|
+
|------|------|
|
|
253
|
+
| `include_reasoning` | 預設 `true`;Grok `thought` → `reasoning_content` |
|
|
254
|
+
| `cwd` | 僅 **agent**(allowlist 內);**safe** 強制 sandbox |
|
|
255
|
+
| `session_id` | 傳給 `grok -s` |
|
|
256
|
+
| `document_ids` | 注入已解密文件內容 |
|
|
257
|
+
|
|
258
|
+
### Thinking 對應
|
|
259
|
+
|
|
260
|
+
| Grok 事件 | OpenAI / DeepSeek 欄位 | 額外 |
|
|
261
|
+
|-----------|------------------------|------|
|
|
262
|
+
| `thought` | `reasoning_content` / `delta.reasoning_content` | `thought` 別名 |
|
|
263
|
+
| `text` | `content` / `delta.content` | — |
|
|
264
|
+
| `end` | `finish_reason` | `grok.sessionId`、`grok.stopReason` |
|
|
265
|
+
|
|
266
|
+
### 端點
|
|
267
|
+
|
|
268
|
+
| Method | Path | 說明 |
|
|
269
|
+
|--------|------|------|
|
|
270
|
+
| GET | `/health` | 存活 |
|
|
271
|
+
| GET | `/ready` | DB + Grok 檢查 |
|
|
272
|
+
| GET | `/v1/models` | 模型列表 |
|
|
273
|
+
| POST | `/v1/chat/completions` | 對話 |
|
|
274
|
+
| POST | `/v1/documents` | 上傳(欄位 `file`) |
|
|
275
|
+
| GET/DELETE | `/v1/documents`… | 列表 / 軟刪除 |
|
|
276
|
+
| POST/GET/DELETE | `/v1/api-keys`… | Admin 管理 key |
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## Safe / Agent 政策
|
|
281
|
+
|
|
282
|
+
| Mode | 適用 | 行為 |
|
|
283
|
+
|------|------|------|
|
|
284
|
+
| **`safe`**(client 預設) | 對外應用 | Sandbox cwd、不 always-approve、限制 tools、較短 timeout |
|
|
285
|
+
| **`agent`** | 受信 / 內網 | 完整 CLI tools(可 always-approve);cwd 仍受 allowlist 限制 |
|
|
286
|
+
|
|
287
|
+
- 全域強制 safe:`GROK_SAFE_MODE=true` 或 Admin → 安全設定
|
|
288
|
+
- Client **無法** 靠 request body 提權
|
|
289
|
+
- **不要** 將 `agent` key 暴露於公網
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Admin Panel
|
|
294
|
+
|
|
295
|
+
```text
|
|
296
|
+
http://127.0.0.1:3847/admin/
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
| 頁面 | 功能 |
|
|
300
|
+
|------|------|
|
|
301
|
+
| 儀表板 | 統計、最近 chat、併發 |
|
|
302
|
+
| Chat 記錄 | **完整解密** prompt / reasoning / response |
|
|
303
|
+
| API Keys | 建立、改 mode/role/限流、撤銷 |
|
|
304
|
+
| 文件 | 列表、解密預覽、刪除 |
|
|
305
|
+
| Audit Logs | 操作紀錄 |
|
|
306
|
+
| 安全設定 | 全域 safe、tools、timeout |
|
|
307
|
+
| 系統狀態 | 健康、版本、**一鍵更新並重啟** |
|
|
308
|
+
|
|
309
|
+
Admin API 前綴:`/admin/api/*`(需要 `role=admin`)。
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## 環境變數
|
|
314
|
+
|
|
315
|
+
詳見 [`.env.example`](./.env.example)。
|
|
316
|
+
|
|
317
|
+
| 變數 | 說明 |
|
|
318
|
+
|------|------|
|
|
319
|
+
| `PORT` | 預設 **`3847`** |
|
|
320
|
+
| `DATABASE_URL` | SQLite,例如 `file:../data/gateway.db`(相對 `prisma/`) |
|
|
321
|
+
| `ENCRYPTION_KEY` | 32-byte key:`openssl rand -base64 32` |
|
|
322
|
+
| `GROK_BIN` | 預設 `grok` |
|
|
323
|
+
| `GROK_DEFAULT_MODEL` | 預設模型 |
|
|
324
|
+
| `GROK_DEFAULT_CWD` / `GROK_CWD_ALLOWLIST` | Agent cwd 政策 |
|
|
325
|
+
| `GROK_ALWAYS_APPROVE` | 只對 agent;safe 一律關閉 |
|
|
326
|
+
| `GROK_SAFE_MODE` | 強制全部 key 用 safe |
|
|
327
|
+
| `GROK_MAX_CONCURRENT` | 最多並行 Grok 進程 |
|
|
328
|
+
| `ADMIN_PANEL_ENABLED` | 開關 `/admin` |
|
|
329
|
+
| `CORS_ORIGINS` | 逗號分隔 origins |
|
|
330
|
+
| `GCTOAC_HOME` | CLI 資料目錄(預設 `~/.gctoac`) |
|
|
331
|
+
| `STORAGE_DIR` | 加密檔案 + sandbox |
|
|
332
|
+
|
|
333
|
+
**請備份 `ENCRYPTION_KEY`。** 遺失後歷史資料無法解密。
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
## 生產環境(PM2)
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
npm run build
|
|
341
|
+
pm2 start ecosystem.config.cjs
|
|
342
|
+
pm2 logs grok-openai-gateway
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
或直接:`gctoac start`(背景執行,pid 寫入資料目錄)。
|
|
346
|
+
|
|
347
|
+
---
|
|
348
|
+
|
|
349
|
+
## 專案結構
|
|
350
|
+
|
|
351
|
+
```text
|
|
352
|
+
src/ TypeScript 原始碼(app、routes、services、cli)
|
|
353
|
+
public/admin/ Admin SPA
|
|
354
|
+
prisma/ Schema、migrations、seed
|
|
355
|
+
dist/ 編譯後 JS(隨 npm 發佈)
|
|
356
|
+
scripts/ prepare、install.sh
|
|
357
|
+
tests/ Vitest
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
---
|
|
361
|
+
|
|
362
|
+
## 腳本
|
|
363
|
+
|
|
364
|
+
```bash
|
|
365
|
+
npm run dev # 開發
|
|
366
|
+
npm run build # 編譯 + prisma generate
|
|
367
|
+
npm start # node dist/server.js
|
|
368
|
+
npm test # 單元 + 整合測試
|
|
369
|
+
npm run db:setup # migrate + seed
|
|
370
|
+
gctoac setup|start|status|stop|doctor|update
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### 發佈到 npm(維護者)
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
npm login
|
|
377
|
+
npm run build
|
|
378
|
+
npm publish --access public --otp=<2FA六位碼>
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
|
|
383
|
+
## 安全注意
|
|
384
|
+
|
|
385
|
+
- API key 只存 **SHA-256 hash**
|
|
386
|
+
- Chat prompt/response 與文件以 **AES-256-GCM** 靜態加密
|
|
387
|
+
- 對外 client 請用 **`safe`** mode
|
|
388
|
+
- 不要 commit `.env`,不要外洩 admin key
|
|
389
|
+
|
|
390
|
+
---
|
|
391
|
+
|
|
392
|
+
## 授權
|
|
393
|
+
|
|
394
|
+
MIT — 見 [LICENSE](./LICENSE)。
|
|
395
|
+
|
|
396
|
+
## 免責聲明
|
|
397
|
+
|
|
398
|
+
本 gateway 會 spawn **Grok CLI**,視政策可能使用檔案系統、shell、網路等 tools。認證、暴露範圍、key 模式由你負責。作者不對濫用或資料損失負責。
|