Skip to content

Commit da91a72

Browse files
committed
🇺🇸 Context
1 parent 9663aa1 commit da91a72

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

‎scripts/wtf.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
import platform
8+
import re
89
import time
910

1011
import platformdirs
@@ -16,13 +17,11 @@
1617
import litellm
1718

1819
SYSTEM_MESSAGE = f"""
19-
# Terminal History Analysis Prompt
20+
You are a fast, efficient AI assistant specialized in analyzing terminal history and providing solutions. You are summoned via the wtf command. Your task is to:
2021
21-
You are a fast, efficient AI assistant specialized in analyzing terminal history and providing quick solutions. Your task is to:
22-
23-
1. Quickly scan the provided terminal history.
22+
1. Scan the provided terminal history.
2423
2. Identify the most recent error or issue.
25-
3. Determine the most likely solution or debugging step.
24+
3. Take a deep breath, and thoughtfully, carefully determine the most likely solution or debugging step.
2625
4. Respond with a VERY brief explanation followed by a markdown code block containing a shell command to address the issue.
2726
2827
Rules:
@@ -32,8 +31,13 @@
3231
- Keep any explanatory text extremely brief and concise.
3332
- Place explanatory text before the code block.
3433
- NEVER USE COMMENTS IN YOUR CODE.
34+
- Construct the command with proper escaping: e.g. use sed with correctly escaped quotes to ensure the shell interprets the command correctly. This involves:
35+
• Using double quotes around the sed expression to handle single quotes within the command.
36+
• Combining single and double quotes to properly escape characters within the shell command.
37+
- If previous commands attempted to fix the issue and failed, learn from them by proposing a DIFFERENT command.
3538
- Focus on the most recent error, ignoring earlier unrelated commands.
36-
- The error may be as simple as a spelling error, or as complex as requiring tests to be run. You may even need to suggest a command that edits text in a file to fix some code.
39+
- If you need more information to confidently fix the problem, ask the user to run wtf again in a moment, then write a command like grep to learn more about the problem.
40+
- The error may be as simple as a spelling error, or as complex as requiring tests to be run, or code to be find-and-replaced.
3741
- Prioritize speed and conciseness in your response. Don't use markdown headings. Don't say more than a sentence or two. Be incredibly concise.
3842
3943
User's System: {platform.system()}
@@ -74,7 +78,7 @@ def main():
7478
pyperclip.copy(clipboard)
7579

7680
# Trim history
77-
history = "..." + history[-3000:].strip()
81+
history = "Terminal: " + history[-10000:].strip()
7882

7983
# Remove any trailing spinner commands
8084
spinner_commands = [
@@ -104,6 +108,45 @@ def main():
104108
history = history[: -len(command)].strip()
105109
break
106110

111+
# Get error context
112+
113+
# Regex pattern to extract filename and line number
114+
pattern = r'File "([^"]+)", line (\d+)'
115+
matches = re.findall(pattern, history)
116+
117+
# Only keep the last X matches
118+
matches = matches[-1:] # Just the last match, change -1 to get more
119+
120+
# Function to get specified lines from a file
121+
def get_lines_from_file(filename, line_number):
122+
lines = []
123+
try:
124+
with open(filename, "r") as file:
125+
all_lines = file.readlines()
126+
start_line = max(0, line_number - 3) # Preceding lines
127+
end_line = min(len(all_lines), line_number + 2) # Following lines
128+
for i in range(start_line, end_line + 1):
129+
lines.append(f"Line {i+1}: " + all_lines[i].rstrip())
130+
except Exception as e:
131+
lines.append(f"Error reading file: {e}")
132+
return lines
133+
134+
# Create the dictionary with filename, line number, and text
135+
result = []
136+
for match in matches:
137+
filename, line_number = match
138+
line_number = int(line_number)
139+
lines = get_lines_from_file(filename, line_number)
140+
result.append({"filename": filename, "text": "\n".join(lines)})
141+
142+
# Add context
143+
for entry in result:
144+
history = f"""File: {entry["filename"]}\n{entry["text"]}\n\n""" + history
145+
146+
# print(history)
147+
# print("---")
148+
# time.sleep(10)
149+
107150
# Prepare messages for LLM
108151
messages = [
109152
{"role": "system", "content": SYSTEM_MESSAGE},

0 commit comments

Comments
 (0)