-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (79 loc) · 3.07 KB
/
main.py
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
import sys
import webbrowser
from argparse import ArgumentParser
from lib.client import Client
from lib.utils import check_for_updates, get_command
from colorama import Fore, Style
from pyperclip import copy as copy_to_clipboard
# https://stackoverflow.com/a/76833666
from rich.console import Console
from rich.markdown import Markdown
check_for_updates()
COMMAND_PREFIX = '!'
parser = ArgumentParser()
console = Console()
client = Client()
parser.add_argument('-o', '--open', '--web', action = 'store_true', help = 'Open the conversation in your default web browser', required = False)
parser.add_argument('-da', '--delete-all', action = 'store_true', help = 'Delete all conversations on your account', required = False)
parser.add_argument('-d', '--delete', action = 'store_true', help = 'Delete the conversation after getting the response', required = False)
args, remainder = parser.parse_known_args()
args = vars(args)
if args['delete_all']:
if client.delete_all_conversations():
print(f'{Fore.GREEN}Successfully deleted all conversations on your account!{Style.RESET_ALL}')
sys.exit(0)
else:
print(f'{Fore.RED}Could not delete some conversations!{Style.RESET_ALL}')
sys.exit(1)
cli_prompt = remainder[0] if remainder else None
uuid: str = client.create_new_chat()['uuid']
response: str = ''
if cli_prompt:
if args['open']:
webbrowser.open(f'https://claude.ai/chat/{uuid}', new = 0, autoraise = True)
response = client.send_message(cli_prompt, uuid)
if args['open']:
sys.exit(0)
if not response:
print(f'{Fore.RED}Something went wrong!{Style.RESET_ALL}')
client.delete_conversation(uuid)
sys.exit(1)
print()
console.print(Markdown(f'{response}'))
print()
if args['delete']:
client.delete_conversation(uuid)
sys.exit(0)
print()
while True:
prompt = str(input(f'{Fore.GREEN}>{Style.RESET_ALL} ')).strip()
if not prompt:
continue
# execute commands
if prompt.startswith(COMMAND_PREFIX):
command = get_command(prompt, COMMAND_PREFIX)
if command in ['quit', 'q', 'exit']:
break
elif command in ['open', 'o', 'web']:
# https://stackoverflow.com/a/4217032
webbrowser.open(f'https://claude.ai/chat/{uuid}', new = 0, autoraise = True)
break
elif command in ['cp', 'copy']:
copy_to_clipboard(response)
continue
elif command in ['del', 'delete', 'remove', 'rm']:
if client.delete_conversation(uuid):
print(f'{Fore.GREEN}Successfully deleted the current conversation! Exiting...{Style.RESET_ALL}')
else:
print(f'{Fore.RED}Could not delete the current conversation! Aborting...{Style.RESET_ALL}')
break
else:
pass
response = client.send_message(prompt, uuid)
if not response:
print(f'{Fore.RED}Something went wrong!{Style.RESET_ALL}')
client.delete_conversation(uuid)
sys.exit(1)
print()
console.print(Markdown(f'{response}'))
print()