-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompleter.py
382 lines (341 loc) · 13.6 KB
/
completer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from collections import defaultdict
import json
from typing import Dict, List
import openai
import subprocess
import sys
import sqlite3
import os
import logging
import argparse
import asyncio
import random
import re
import tiktoken
MAX_TOKENS = 3500
log_filename = os.path.join(os.path.dirname(__file__), ".completer.log")
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S.%f",
filename=log_filename,
filemode="w",
)
REMOVE_PATTERNS = [
# sometimes GPT replies with
# 1. commandline
# 2. commandline
(re.compile(r"^\d+\.\s*(.*)$"), r"\1"),
]
def open_atuin_db(filename: str):
conn = sqlite3.connect(filename)
conn.row_factory = sqlite3.Row
return conn
def remove_extra_unicode_characters(content):
clean_content = "".join([c for c in content if ord(c) < 128])
return clean_content
def filter_paths(possibilities: List[str]) -> List[str]:
return [os.path.normpath(p) for p in possibilities if os.path.exists(p)]
def count_text_tokens(content: str, model="gpt-4") -> int:
"""
Counts the number of tokens required to send the given text.
:param content: the text
:return: the number of tokens required
"""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("gpt-3.5-turbo")
return len(encoding.encode(content))
def count_tokens(messages, model="gpt-4") -> int:
"""
Counts the number of tokens required to send the given messages.
:param messages: the messages to send
:return: the number of tokens required
"""
# Thanks for https://github.com/n3d1117/chatgpt-telegram-bot/blob/main/bot/openai_helper.py
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("gpt-3.5-turbo")
if "gpt-3.5" in model:
tokens_per_message = (
4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
)
tokens_per_name = -1 # if there's a name, the role is omitted
else:
tokens_per_message = 3
tokens_per_name = 1
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
async def main():
# Get OS user name for this user
user = os.getlogin()
default_atuin_db = "/home/" + user + "/.local/share/atuin/history.db"
parser = argparse.ArgumentParser()
parser.add_argument(
"--atuin",
"-a",
type=str,
default=default_atuin_db,
help="Path to atuin history.db",
)
parser.add_argument("--dunst",action="store_true", help="Use dunstify to show notifications")
# parser.add_argument(
# "--max-files",
# "-f",
# type=int,
# help="Maximum number of files to send in prompt (per directory)",
# )
# parser.add_argument(
# "--max-dirs",
# "-d",
# type=int,
# help="Maximum number of directories to send in prompt (per directory)",
# )
parser.add_argument(
"--cwd-history",
"-ch",
type=int,
default=10,
help="Maximum number of cwd history to send in prompt",
)
parser.add_argument(
"--process-history",
"-ph",
type=int,
default=10,
help="Maximum number of history entries for the same process to send in prompt",
)
parser.add_argument(
"--session-history",
"-sh",
type=int,
default=10,
help="Maximum number of history entries for the same session to send in prompt",
)
parser.add_argument(
"--cwd-process-history",
"-cph",
type=int,
default=5,
help="Maximum number of history entries for the same process in the same cwd to send in prompt",
)
# parser.add_argument(
# "--cwd-process-session-history",
# "-cpsh",
# type=int,
# default=10,
# help="Maximum number of history entries for the same process in the same cwd in the same session to send in prompt",
# )
parser.add_argument("--shell", "-s", type=str, default="nushell", help="Shell name. Only given to GPT")
parser.add_argument("--wezterm", "-w", action="store_true")
# parser.add_argument("--kitty", "-k", action="store_true")
parser.add_argument("--model", "-m", default="gpt-3.5-turbo", help="GPT model to use, gpt-4 or got-3.5-turbo")
parser.add_argument("commandline", nargs=argparse.REMAINDER)
args = parser.parse_args()
ATUIN_SESSION = os.environ.get("ATUIN_SESSION")
assert ATUIN_SESSION is not None, "ATUIN_SESSION is not set"
dunst_id = random.randint(2183, 1000000)
cwd = os.getcwd()
cmdline = " ".join(args.commandline)
target_process = cmdline.split(maxsplit=1)[0] if len(sys.argv) > 1 else ""
term_content = ""
if args.wezterm:
wezterm_capture_process = subprocess.run(
["wezterm", "cli", "get-text"], capture_output=True
)
term_content = wezterm_capture_process.stdout.decode("utf-8")
# elif args.kitty:
# kitty_capture_process = subprocess.run(
# ["kitty", "@", "get-text"], capture_output=True
# )
# term_content = kitty_capture_process.stdout.decode("utf-8")
if term_content:
cleaned_term_content = remove_extra_unicode_characters(term_content)
logging.info(f"cleaned_term_content from {len(term_content)} -> {cleaned_term_content}")
# Log token count as well
logging.info(f"term_content token count: {count_text_tokens(term_content)}")
logging.info(f"cleaned_term_content token count: {count_text_tokens(cleaned_term_content)}")
term_content = "Terminal content:\n" + cleaned_term_content + "\n\n"
# cwd = os.path.normpath(sys.argv[1])
# TODO: doesn't handle paths with spaces
target_paths = None
# if args.max_files or args.max_dirs:
# target_paths = filter_paths(cmdline.split())
# if os.path.abspath(cwd) not in [os.path.abspath(p) for p in target_paths]:
# logging.debug("Adding cwd to target_paths")
# target_paths.append(cwd)
# path_files: Dict[str, List[str]] = defaultdict(list)
# for path in target_paths:
# logging.debug(f"Looking for files in {path}")
# for root, dirs, files in os.walk(path):
# for file in files:
# path_files[path].append(file)
# for dir in dirs:
# path_files[path].append(dir + "/")
# logging.debug(f"Files: {path_files[path]}")
logging.info(f"target_process: {target_process}")
logging.info(f"cwd: {cwd}")
logging.info(f"cmdline: {cmdline}")
logging.info(f"target_paths: {target_paths}")
db = open_atuin_db(args.atuin)
cursor = db.cursor()
same_process_str = same_cwd_str = same_session_str = ""
same_cwd_process1_str = same_cwd_process_session_str = ""
if args.process_history > 0:
same_process = []
for entry in cursor.execute(
"SELECT DISTINCT(command) as command FROM history WHERE command LIKE ? ORDER BY timestamp DESC LIMIT ?",
(target_process + "%", args.process_history),
):
same_process.append(entry["command"])
if same_process:
same_process_str = "Latest calls for the same executable:\n"
same_process_str += "\n".join(same_process) + "\n\n"
if args.cwd_history:
same_cwd = []
for entry in cursor.execute(
"SELECT * FROM history WHERE cwd = ? ORDER BY timestamp DESC LIMIT ?",
(cwd, args.cwd_history),
):
same_cwd.append(entry["command"])
if same_cwd:
same_cwd_str = "Latest calls in the same directory:\n"
same_cwd_str += "\n".join(same_cwd) + "\n\n"
if args.session_history:
same_session = []
for entry in cursor.execute(
"SELECT * FROM history WHERE session = ? ORDER BY timestamp DESC LIMIT ?",
(ATUIN_SESSION, args.session_history),
):
same_session.append(entry["command"])
if same_session:
same_session_str = "Latest calls in the same session:\n"
same_session_str += "\n".join(same_session) + "\n\n"
if args.cwd_process_history:
same_cwd_process = []
for entry in cursor.execute(
"SELECT * FROM history WHERE cwd = ? AND command LIKE ? ORDER BY timestamp DESC LIMIT ?",
(cwd, target_process + "%", args.cwd_process_history),
):
same_cwd_process.append(entry["command"])
if same_cwd_process:
same_cwd_process_str = (
"Latest calls in the same directory for the same process:\n"
)
same_cwd_process_str += "\n".join(same_cwd_process) + "\n\n"
# if args.cwd_process_session_history:
# same_cwd_process_session = []
# for entry in cursor.execute(
# "SELECT * FROM history WHERE cwd = ? AND command LIKE ? AND session = ? ORDER BY timestamp DESC LIMIT ?",
# (
# cwd,
# target_process + "%",
# ATUIN_SESSION,
# args.cwd_process_session_history,
# ),
# ):
# same_cwd_process_session.append(entry["command"])
# if same_cwd_process_session:
# same_cwd_process_session_str = "Latest calls in the same directory for the same process in the same session:\n"
# same_cwd_process_session_str += "\n".join(same_cwd_process_session) + "\n\n"
prompt_text = f"""User terminal information:
Shell: {args.shell}
{term_content}{same_session_str}{same_cwd_str}{same_process_str}{same_cwd_process_str}{same_cwd_process_session_str}
CWD: {cwd}
Current command line: {cmdline}"""
prompt = [
{
"role": "system",
"content": """You are an AI autocompleting for user's terminal session."""},
{
"role": "user",
"content": """I will give you information from user's terminal - screen content and history of related commands ran. You can use this information to complete the current command line.
You have to reply with only the full command lines, do not output anything else. Do not prepend the lines with line numbers. Reply with 1-5 completions. The returned lines should contain the user command line input, and you can also change the user input if needed.""",
},
# {
# "role": "user",
# "name": "example_user",
# "content": ""
# }
{"role": "user", "content": prompt_text},
]
# print(prompt_text)
logging.debug(f"Prompt: {prompt_text}")
tokens = count_tokens(prompt, args.model)
logging.info(f"Request tokens: {tokens}")
if tokens > MAX_TOKENS:
logging.error(f"Request tokens: {tokens} > {MAX_TOKENS}")
if args.dunst:
subprocess.run(
["dunstify", "-r", str(dunst_id), f"Too many tokens, {tokens} > {MAX_TOKENS}", f"{cwd}\n\n{cmdline}"]
)
return
if args.dunst:
subprocess.run(
["dunstify", "-r", str(dunst_id), f"Completing, {tokens} tokens", f"{cwd}\n\n{cmdline}"]
)
response = await openai.ChatCompletion.acreate(
model=args.model, messages=prompt, stream=True
)
async def gen():
async for chunk in response:
if "choices" not in chunk or len(chunk["choices"]) == 0:
continue
yield chunk["choices"][0]["delta"].get("content", "")
yield "\n"
current_data = ""
async for chunk in gen():
# logging.info("ChunkResponse:\n",chunk)
current_data += chunk
while "\n" in current_data:
line, current_data = current_data.split("\n", 1)
if line:
logging.debug(f"Response line: {line}")
for pattern in REMOVE_PATTERNS:
line = pattern[0].sub(pattern[1], line)
line=line.strip("`")
logging.debug(f"Response line after patterns: {line}")
print(line, flush=True)
if not current_data:
current_data = ""
if current_data:
line = current_data
for pattern in REMOVE_PATTERNS:
line = pattern[0].sub(pattern[1], line)
line=line.strip("`")
print(line, flush=True)
# logging.info("Response:\n",response)
# result = json.loads(response["choices"][0]["message"]["content"])
# for x in response:
# logging.info("Response:\n","y"+x+"z")
# result = json.loads(x["choices"][0]["message"]["content"])
# if result:
# break
if args.dunst:
subprocess.run(
["dunstify", "-r", str(dunst_id), f"Completion finished", f"{cmdline}"]
)
exit()
result = json.loads(DEBUG_RESULT)
print("\n".join([x["value"] for x in result]))
# fzf_process = subprocess.run(["fzf", "--ansi", "--multi", "--preview-window", "up:50%", "--preview", "echo {}"], input="\n".join([x["value"] for x in result]).encode("utf-8"), capture_output=True)
# print(fzf_process.stdout.decode("utf-8"))
# fzf = FzfPrompt()
# selection = fzf.prompt([x.get("value") for x in result]) # , preview_window="up:50%", preview="echo {}")
# for x in result:
# print(x["value"])
# if not selection:
# print(cmdline)
# else:
# print(selection[0])
if __name__ == "__main__":
asyncio.run(main())