-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path06_openai_codex_oauth.py
More file actions
75 lines (62 loc) · 1.96 KB
/
06_openai_codex_oauth.py
File metadata and controls
75 lines (62 loc) · 1.96 KB
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
from __future__ import annotations
import argparse
import os
from republic import (
LLM,
load_openai_codex_oauth_tokens,
login_openai_codex_oauth,
openai_codex_oauth_resolver,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Authenticate with OpenAI Codex OAuth and run a simple Republic chat.",
)
parser.add_argument(
"--login-only",
action="store_true",
help="Run OAuth login and persist tokens without sending a chat request.",
)
parser.add_argument(
"--force-login",
action="store_true",
help="Always run the OAuth login flow even if local Codex tokens already exist.",
)
parser.add_argument(
"--model",
default=os.getenv("REPUBLIC_CODEX_MODEL", "openai:gpt-5.3-codex"),
help="Model to use after login.",
)
parser.add_argument(
"--prompt",
default="Explain tape-first workflows in one sentence.",
help="Prompt to send after login.",
)
return parser.parse_args()
def prompt_for_redirect(authorize_url: str) -> str:
print("Open this URL in your browser and complete the sign-in flow:\n")
print(authorize_url)
print("\nPaste the full callback URL (or the authorization code) here.")
return input("> ").strip()
def main() -> None:
args = parse_args()
resolver = openai_codex_oauth_resolver()
tokens = load_openai_codex_oauth_tokens()
if args.force_login or resolver("openai") is None:
tokens = login_openai_codex_oauth(
prompt_for_redirect=None,
)
print("login: ok")
else:
tokens = load_openai_codex_oauth_tokens()
print("login: reused")
print("account_id:", tokens.account_id or "-")
if args.login_only:
return
llm = LLM(
model=args.model,
api_key_resolver=resolver,
)
out = llm.chat(args.prompt)
print("text:", out)
if __name__ == "__main__":
main()