|
5 | 5 |
|
6 | 6 | import os
|
7 | 7 | import platform
|
| 8 | +import re |
8 | 9 | import time
|
9 | 10 |
|
10 | 11 | import platformdirs
|
|
16 | 17 | import litellm
|
17 | 18 |
|
18 | 19 | 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: |
20 | 21 |
|
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. |
24 | 23 | 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. |
26 | 25 | 4. Respond with a VERY brief explanation followed by a markdown code block containing a shell command to address the issue.
|
27 | 26 |
|
28 | 27 | Rules:
|
|
32 | 31 | - Keep any explanatory text extremely brief and concise.
|
33 | 32 | - Place explanatory text before the code block.
|
34 | 33 | - 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. |
35 | 38 | - 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. |
37 | 41 | - Prioritize speed and conciseness in your response. Don't use markdown headings. Don't say more than a sentence or two. Be incredibly concise.
|
38 | 42 |
|
39 | 43 | User's System: {platform.system()}
|
@@ -74,7 +78,7 @@ def main():
|
74 | 78 | pyperclip.copy(clipboard)
|
75 | 79 |
|
76 | 80 | # Trim history
|
77 |
| - history = "..." + history[-3000:].strip() |
| 81 | + history = "Terminal: " + history[-10000:].strip() |
78 | 82 |
|
79 | 83 | # Remove any trailing spinner commands
|
80 | 84 | spinner_commands = [
|
@@ -104,6 +108,45 @@ def main():
|
104 | 108 | history = history[: -len(command)].strip()
|
105 | 109 | break
|
106 | 110 |
|
| 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 | + |
107 | 150 | # Prepare messages for LLM
|
108 | 151 | messages = [
|
109 | 152 | {"role": "system", "content": SYSTEM_MESSAGE},
|
|
0 commit comments