refactor(codex): 將 app-server 取帳號邏輯抽到 app/codex_account.py
把原本內嵌在 action.yml heredoc 的 python 抽成獨立檔 app/codex_account.py, action 改以 $GITHUB_ACTION_PATH/app/codex_account.py 呼叫,便於閱讀與維護。
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
"""透過 codex app-server 的 JSON-RPC account/read 取得目前登入帳號的 email。
|
||||
|
||||
這是 TUI `/status` Account 欄位的程式化來源,不解析本地 OAuth token,
|
||||
而是由 codex 自身回報登入帳號。將 email 印到 stdout(取不到時印空字串)。
|
||||
"""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
|
||||
def read_account_email(timeout_seconds: float = 25.0) -> str:
|
||||
process = subprocess.Popen(
|
||||
["codex", "app-server"],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
)
|
||||
|
||||
def send(obj):
|
||||
process.stdin.write(json.dumps(obj) + "\n")
|
||||
process.stdin.flush()
|
||||
|
||||
send({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "initialize",
|
||||
"params": {"clientInfo": {"name": "ci", "version": "1.0"}},
|
||||
})
|
||||
send({"jsonrpc": "2.0", "method": "initialized", "params": {}})
|
||||
send({"jsonrpc": "2.0", "id": 2, "method": "account/read", "params": {}})
|
||||
|
||||
email = ""
|
||||
deadline = time.time() + timeout_seconds
|
||||
try:
|
||||
while time.time() < deadline:
|
||||
line = process.stdout.readline()
|
||||
if not line:
|
||||
break
|
||||
try:
|
||||
message = json.loads(line)
|
||||
except ValueError:
|
||||
continue
|
||||
if message.get("id") == 2:
|
||||
account = (message.get("result") or {}).get("account") or {}
|
||||
email = account.get("email") or ""
|
||||
break
|
||||
finally:
|
||||
process.terminate()
|
||||
|
||||
return email
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(read_account_email())
|
||||
Reference in New Issue
Block a user