AnyBot/docs/restructure/notes/b04b-chatgpt.md

29 lines
2.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# b04b:chatgpt.rs(1,285 行)— ChatGPT 方案登入(OAuth)
## 常數(實讀)
TOOL="chatgpt_login"、ISSUER="https://auth.openai.com"、API_BASE="https://api.openai.com/v1"、USAGE_URL="https://chatgpt.com/settings/usage"、PLAN_SCOPE="chatgpt.tokens.use.direct"、SCOPES="openid profile email offline_access resource.invoke chatgpt.tokens.use.direct"、FIRST_CLIENT="dynamic_agent_client"、PREFERRED_PORT=1455、LOGIN_WAIT=10 分、HTTP_SECS=20、RENEW_EARLY_MS=5 分(到期前 5 分即視同過期)。
文案常數:NEED_LOGIN/NO_PLAN/EXPIRED(三者都是給使用者的整句指引)。
`SEALED_FIELDS = [access, refresh, id_token]`(line 141)=落盤前加密的三欄。
## 帳號與落盤
- `Account` struct(52);存 `<data>/providers/chatgpt.json`;token 欄位落盤用 vault_store 的 SEALED 前綴封裝(186:未封裝=舊檔要遷)。
- `api_base()`(118)、`account()`(191)、`note()/set_note()`(219/223)、`stored_token()`(258)、`why_not_ready()`(264)。
- `limited()/set_limited()`(802/806):用量上限旗標(到上限即停)。
- `usable()`(844)、`status_text()`(755)、`for_screen(v)`(771)/`view()`(792)=UI 只看到不含 token 的畫面。
- `brain_view()/set_brain_view()/brain_view_stale(secs)`(821–839):腦那端的方案狀態快取。
## 登入流(順序實讀)
1. `start()`(389):dynamic client registration(FIRST_CLIENT)→ PKCE(S256)→ login_url()(229)給使用者按。
2. callback 落在 `127.0.0.1:<port>/auth/callback`(PREFERRED_PORT=1455,佔不了就隨機)。
3. `finish_from_url(callback)`(447):code→token exchange;ID token **RS256** 驗證 `verify_id_token(token, jwks, client_id, nonce, now)`(553)→ `Who`(533)。
4. 同意 PLAN_SCOPE=「讓 AnyBot 用你的方案」;未同意 → NO_PLAN。
5. access token 約 1 小時;refresh **輪換(rotate)**,同時只續發一次(643:兩個並發會花掉同一 refresh 兩次)。`fresh_token()`(624)→ `renew_after(stale)`(645);`SPENT = [invalid_grant, invalid_refresh_token, token_expired, refresh_token_expired, refresh_token_invalidated, refresh_token_reused]`(692)=refresh 用盡的 6 種答案 → token 全清、重登入沿用原 registration。`NoToken` struct(607)=要重新登入的訊號。
6. `logout(forget)`(711);`take_finished()`(355)取回背景登入結果;`welcome_once()`(745)首次成功登入的歡迎字串。
7. `is_sign_in_page(url)`(811):瀏覽器分頁是否在登入頁(輔助使用者手動走完)。
## 工具面
`spec()`(850)=chatgpt_login 工具定义;`run_tool(args)`(859)回 (text, ok);`ask_label(args)`(885)/`describe(args)`(895)=審批卡文案。
## 迁移
chatgpt.rs → **net::oauth::chatgpt**(ureq + rustls;callback 走 httpd 的 localhost listener)。紅線:tungstenite 與 rustls(ring) 不動;ISSUER/SCOPES/PKCE S256/RS256 逐字保留。