File size: 6,494 Bytes
1f40f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# 在 Linux 桌面提取 `ARENA_STORAGE_STATE_B64`

本指南只在 Linux 電腦完成第一次人工登入與狀態匯出,不會在 Linux 部署 API。

## 0. 適用條件

你需要:

- 有圖形桌面的 Linux,而不是只有 SSH 的純伺服器。
- x86_64/amd64 架構,或已經能正常運行 Google Chrome 的其他架構。
- 自己有權使用的 Google/Arena 帳號。
- 能在官方 Arena 頁面自行處理條款、MFA、CAPTCHA。

檢查架構:

```bash
uname -m
```

一般 Google Chrome `.deb` 適用:

```text
x86_64
```

## 1. 安裝基本工具

Ubuntu/Debian:

```bash
sudo apt update
sudo apt install -y python3 python3-pip python3-venv unzip curl wget
```

確認:

```bash
python3 --version
curl --version
unzip -v | head
```

## 2. 解壓專案

假設 ZIP 在 `~/Downloads````bash
mkdir -p ~/arena-state-export
unzip ~/Downloads/arena-playwright-api-space.zip -d ~/arena-state-export
cd ~/arena-state-export
```

確認工具:

```bash
test -f tools/export_storage_state.py && echo "export tool found"
```

若顯示 `export tool found` 才繼續。

## 3. 建立 Python venv

```bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install playwright==1.62.0
```

確認套件:

```bash
python -c "import playwright; print('Playwright Python ready')"
```

這裡只透過 CDP 連接一般 Google Chrome,不需要執行 `playwright install chromium`## 4. 安裝/確認 Google Chrome

先檢查:

```bash
command -v google-chrome-stable || command -v google-chrome
```

若已安裝:

```bash
google-chrome-stable --version 2>/dev/null || google-chrome --version
```

Ubuntu/Debian x86_64 尚未安裝時:

```bash
cd /tmp
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install -y ./google-chrome-stable_current_amd64.deb
rm -f google-chrome-stable_current_amd64.deb
```

## 5. 啟動專用 Google Chrome

不要使用日常 Chrome Profile。專用 Profile 包含暫時的 Google/Arena Session,完成後會刪除。

先建立目錄:

```bash
mkdir -p ~/.cache/arena-google-profile
```

在第一個 Terminal 執行:

```bash
google-chrome-stable \
  --remote-debugging-address=127.0.0.1 \
  --remote-debugging-port=9222 \
  --user-data-dir="$HOME/.cache/arena-google-profile" \
  --new-window \
  "https://arena.ai/text/direct"
```

若系統指令是 `google-chrome`:

```bash
google-chrome \
  --remote-debugging-address=127.0.0.1 \
  --remote-debugging-port=9222 \
  --user-data-dir="$HOME/.cache/arena-google-profile" \
  --new-window \
  "https://arena.ai/text/direct"
```

請保持第一個 Terminal 和 Chrome 都開著。

## 6. 確認 CDP

開第二個 Terminal:

```bash
curl -sS http://127.0.0.1:9222/json/version
```

正常應包含:

```text
Browser
Protocol-Version
webSocketDebuggerUrl
```

只查看必要欄位:

```bash
curl -sS http://127.0.0.1:9222/json/version | \
python3 -c 'import json,sys; x=json.load(sys.stdin); print(x.get("Browser")); print(x.get("webSocketDebuggerUrl"))'
```

如果 Connection refused:

```bash
ps -eo pid,args | grep '[c]hrome'
```

確認參數包含:

```text
--remote-debugging-port=9222
--user-data-dir=/home/.../.cache/arena-google-profile
```

## 7. 在 Chrome 完成人工操作

在專用 Chrome 中:

1. 使用自己的 Google 帳號登入 Arena。
2. 由帳號本人完成 MFA、裝置確認或 CAPTCHA。
3. 回到 `https://arena.ai/text/direct`。
4. 自行閱讀並決定是否接受 Arena 條款。
5. 選擇任意模型。
6. 發送:`請回答:Linux Storage State 測試成功`。
7. 等待模型實際回答。
8. 不要關閉 Chrome。

只登入但沒有發送訊息可能不會建立完整條款與對話狀態。

## 8. 執行匯出

在第二個 Terminal:

```bash
cd ~/arena-state-export
source .venv/bin/activate
python tools/export_storage_state.py --cdp-url http://127.0.0.1:9222
```

按照提示確認操作已完成,再按 Enter。

成功輸出:

```text
arena_storage_state.json
arena_storage_state.b64
```

## 9. 驗證輸出,不顯示 Secret 值

```bash
ls -lh arena_storage_state.json arena_storage_state.b64
wc -c arena_storage_state.b64
```

檢查只包含 Arena 網域,不列出 Cookie value:

```bash
python - <<'PY'
import base64, gzip, json
from pathlib import Path

encoded = Path("arena_storage_state.b64").read_text().strip()
state = json.loads(gzip.decompress(base64.b64decode(encoded)).decode())
print("Cookie domains:", sorted({c.get("domain") for c in state.get("cookies", [])}))
print("Origins:", [o.get("origin") for o in state.get("origins", [])])
print("Cookie count:", len(state.get("cookies", [])))
PY
```

預期只出現:

```text
arena.ai
*.arena.ai
lmarena.ai
*.lmarena.ai
```

不應出現:

```text
google.com
accounts.google.com
```

## 10. 複製 Base64

Wayland:

```bash
sudo apt install -y wl-clipboard
wl-copy < arena_storage_state.b64
```

X11:

```bash
sudo apt install -y xclip
xclip -selection clipboard < arena_storage_state.b64
```

沒有剪貼簿工具:

```bash
cat arena_storage_state.b64
```

複製完整單行內容;不要增加引號或 `ARENA_STORAGE_STATE_B64=` 前綴。

## 11. 設定 API Space

新增/更新 Secret:

```text
ARENA_STORAGE_STATE_B64
```

Google Storage State 模式保留:

```text
ADMIN_PASSWORD
BRIDGE_API_KEY
ARENA_STORAGE_STATE_B64
```

刪除:

```text
SETUP_CODE
ARENA_EMAIL
ARENA_PASSWORD
```

重新 Build API Space,檢查:

```text
/health
/v1/models
/v1/chat/completions
```

## 12. 安全清理

確認 API 正常後:

```bash
cd ~/arena-state-export
rm -f arena_storage_state.json arena_storage_state.b64
deactivate
rm -rf ~/.cache/arena-google-profile
```

如果曾把檔案提交 Git 或公開分享,請立即撤銷 Arena/Google Session 並重新建立狀態。

## 常見問題

### Google 顯示瀏覽器不安全

確認你使用的是一般 `google-chrome-stable`,而不是 Playwright 內建 Chromium;Google 登入必須由本人手動完成。

### `No Arena cookies or Local Storage were found`

確認在同一個專用 Chrome 中登入 Arena、處理條款、送出訊息並看到回答,再匯出。

### API 仍回傳 `account_consent_required`

可能在條款確認前就匯出。刪除舊輸出,回到專用 Chrome 再確認條款與聊天,重新執行匯出。

### API 回傳 503

查看 API Space Logs 與 `/health`;確認 Secret 是完整單行 Base64,沒有引號、前綴或截斷。