1
1
import os
2
+ import sys
2
3
import webbrowser
3
4
5
+ import pyperclip
6
+ import rich
4
7
import typer
5
8
from rich .console import Console
6
9
from yt_dlp .cookies import SUPPORTED_BROWSERS
@@ -18,6 +21,15 @@ def _open_chatgpt_browser():
18
21
webbrowser .open (CHATGPT_LOGIN_URL )
19
22
20
23
24
+ def _ask_user_copy_session_token_to_clipboard (session_token : str ) -> None :
25
+ copy_session_token = typer .confirm ('Do you want to copy the session token to your clipboard?' )
26
+ if copy_session_token :
27
+ pyperclip .copy (session_token )
28
+ rich .print (
29
+ 'Session token copied to clipboard. [bold]`export CHATGPT_SESSION_TOKEN=<session_token>`[/bold] to set it.'
30
+ )
31
+
32
+
21
33
@cli_app .command ()
22
34
def ask (question : str , language_model : LanguageModel = LanguageModel .REVERSE_ENGINEERED_CHATGPT ):
23
35
query_client : QueryClient
@@ -34,7 +46,13 @@ def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENG
34
46
BROWSER_NAME = typer .prompt (f'Which browser did you use to log in? [{ SUPPORTED_BROWSERS } ]' )
35
47
adapter = OpenAICookieAdapter (BROWSER_NAME )
36
48
session_token = adapter .get_openai_session_token ()
37
- query_client = ReverseEngineeredChatGPTClient (session_token = session_token )
49
+ if session_token is not None :
50
+ os .environ ['CHATGPT_SESSION_TOKEN' ] = session_token
51
+ _ask_user_copy_session_token_to_clipboard (session_token )
52
+ ask (question , language_model )
53
+ else :
54
+ print ('Failed to log in.' )
55
+ sys .exit ()
38
56
39
57
query_client .query (question )
40
58
@@ -43,9 +61,9 @@ def ask(question: str, language_model: LanguageModel = LanguageModel.REVERSE_ENG
43
61
f'''
44
62
[green] AiShell is thinking of `{ question } ` ...[/green]
45
63
46
- [italic ]AiShell is not responsible for any damage caused by the command executed by the user.[/italic ]''' .strip (), ):
64
+ [dim ]AiShell is not responsible for any damage caused by the command executed by the user.[/dim ]''' .strip (), ):
47
65
response = query_client .query (question )
48
- console .print (f'[italic]ai$hell : { response } \n ' )
66
+ console .print (f'AiShell : { response } \n ' )
49
67
50
68
will_execute = typer .confirm ('Execute this command?' )
51
69
0 commit comments