-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.llm-cmd2
More file actions
100 lines (83 loc) · 2.88 KB
/
.llm-cmd2
File metadata and controls
100 lines (83 loc) · 2.88 KB
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
# Global variables to store last failed command info
LAST_FAILED_COMMAND=""
LAST_FAILED_OUTPUT=""
# Function to capture command failures
capture_command_failure() {
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
LAST_FAILED_COMMAND="$1"
LAST_FAILED_OUTPUT="$2"
fi
return $exit_code
}
# Wrapper for command execution that captures failures
run_with_capture() {
local cmd="$1"
local output
output=$(eval "$cmd" 2>&1)
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
LAST_FAILED_COMMAND="$cmd"
LAST_FAILED_OUTPUT="$output"
fi
echo "$output"
return $exit_code
}
llmcmd() {
# Detect OS information
local os_info=""
if [[ "$OSTYPE" == "darwin"* ]]; then
os_info="macOS"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
os_info="Linux"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
os_info="Windows"
else
os_info="$OSTYPE"
fi
local SYSTEM_PROMPT="You are running on $os_info. Return only the command to be executed as a raw string, no string delimiters wrapping it, no yapping, no markdown, no fenced code blocks, what you return will be passed to subprocess.check_output() directly.
For example, if the user asks: undo last git commit
You return only: git reset --soft HEAD~1
Use commands appropriate for $os_info when there are OS-specific differences."
if [[ $# -eq 0 ]]; then
echo "Usage: llmcmd <prompt>"
echo " llmcmd --fix|-f (to fix the last failed command)"
return 1
fi
# Check if user wants to fix the last failed command
if [[ "$1" == "--fix" || "$1" == "-f" ]]; then
if [[ -z "$LAST_FAILED_COMMAND" ]]; then
echo "No failed command to fix"
return 1
fi
local fix_prompt="The command '$LAST_FAILED_COMMAND' failed with this output:
$LAST_FAILED_OUTPUT
Please provide a corrected version of the command that will work."
local generated_command=$(llm prompt -s "$SYSTEM_PROMPT" "$fix_prompt")
else
local prompt="$*"
local generated_command=$(llm prompt -s "$SYSTEM_PROMPT" "$prompt")
fi
if [[ $? -ne 0 ]]; then
echo "Error: Failed to generate command"
return 1
fi
print -z "$generated_command"
}
# Hook into command execution to capture failures
preexec() {
# Store the command that's about to be executed
CURRENT_COMMAND="$1"
}
precmd() {
# Check if the last command failed and capture it
local exit_code=$?
if [[ $exit_code -ne 0 && -n "$CURRENT_COMMAND" ]]; then
# Don't capture llmcmd failures themselves
if [[ "$CURRENT_COMMAND" != llmcmd* ]]; then
LAST_FAILED_COMMAND="$CURRENT_COMMAND"
# Get the last few lines of history for context
LAST_FAILED_OUTPUT="Command exited with code $exit_code"
fi
fi
}