-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.sh
256 lines (218 loc) Β· 5.19 KB
/
script.sh
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
#!/bin/bash
#
# Run:
# chmod +x script.sh
# export OPENAI_API_KEY="" && ./script.sh
#
# This script demonstrates how to use an AI to interact with your codebase.
# It uses the 'aider' CLI to prompt the AI with a message and a list of files.
#
# Usage:
# addToIndex ./src/features/tagManagement/api/tagManager.ts
# prompt "for each function in the file, create a new separate file with that function."
# Proxy: Uncomment to use local LLM from Ollama
# export OLLAMA_API_BASE=http://127.0.0.1:11434
# Arrays
declare -a filesToIndex
# Local variables
model="ollama/qwen2:latest"
#
# Resets the 'filesToIndex' array.
#
# Parameters:
# None
# Example:
# reset
# Returns:
# None
#
function reset() {
unset filesToIndex
declare -a filesToIndex
}
#
# Adds the given files to the 'filesToIndex' array.
#
# Parameters:
# $@: The files to add to the 'filesToIndex' array.
# Example:
# addToIndex file1 file2 file3
# Returns:
# None
#
function addToIndex() {
for file in "$@"; do
if [ ! -f "$file" ]; then
echo "Error: File '$file' does not exist."
exit 1
fi
done
filesToIndex+=("$@")
}
#
# Searches for files containing the given text and adds them to the 'filesToIndex' array.
#
# Parameters:
# $1: The text to search for in the files.
# $2: The file extension to search for. Default is '*.*'.
# Example:
# indexFilesContainingText "function myFunction"
# Returns:
# None
#
function indexFilesContainingText() {
local text=$1
local extension=${2:-*.*}
local cmd="find . -type f -name \"$extension\" -exec grep -l \"$text\" {} +"
local files=$(eval $cmd)
local uniqueFiles=$(echo "$files" | sort | uniq)
addToIndex ${uniqueFiles[@]}
}
#
# Indexes all files in the given directory.
#
# Parameters:
# $1: The directory to index.
# $2: The file extension to search for. Default is '*.*'.
# Example:
# indexContentInDirectory "./src/features/tagManagement/api" "*.ts"
# Returns:
# None
#
function indexContentInDirectory() {
local directory=$1
local extension=${2:-*.*}
local files=`find "$directory" -type f -name "$extension"`
addToIndex ${files[@]}
}
#
# Lists all file paths in the given directory.
#
# Parameters:
# $1: The directory to list file paths from.
# $2: The file extension to search for. Default is '*.*'.
# Example:
# listFilePathsInDirectory "./src/features/tagManagement/api" "*.ts"
# Returns:
# A list of file paths, like: "file1 file2 file3"
#
function listFilePathsInDirectory() {
local directory=$1
local extension=${2:-*.*}
local files=`find "$directory" -type f -name "$extension"`
echo "$files"
}
#
# Prompt the AI with the given message.
#
# Parameters:
# $1: The message to prompt the AI with.
# Example:
# prompt "Add a new function to the file."
# Returns:
# None
#
function prompt() {
# check if the files to index are empty
if [ ${#filesToIndex[@]} -eq 0 ]; then
echo "π
No files to index."
exit 1
fi
local message=$1
echo -e "π Prompting AI with message:\n---\n$message\n---"
if [ -n "$OLLAMA_API_BASE" ]; then
aider --yes \
--no-auto-test \
--no-dirty-commits \
--no-auto-commits \
--model "$model" \
--message="$message" \
"$(getFilesToIndex)"
else
aider --yes \
--no-auto-test \
--no-dirty-commits \
--no-auto-commits \
--message="$message" \
"$(getFilesToIndex)"
fi
}
#
# Get the files to index.
#
# Parameters:
# None
# Example:
# getFilesToIndex
# Returns:
# A list of indexed files, like: "file1 file2 file3"
#
function getFilesToIndex() {
echo "${filesToIndex[@]}"
}
#
# Formats the array to string.
#
# Parameters:
# $1: The array to format.
# Example:
# arrayToString "file1 file2 file3"
# Returns:
# A list of formatted paths, like: "file1\nfile2\nfile3"
#
function arrayToString() {
local paths="$1"
echo $paths | tr ' ' '\n' # | sed 's/^/- /'
}
#
# Pause the interaction with the AI and ask the user if he wants to continue.
#
# Parameters:
# None
# Example:
# shouldContinue
# Returns:
# None
#
function shouldContinue() {
echo ">>> π Review AI changes before next prompt. π"
read -p "Do you want to continue? [y/N] " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Continuing..."
else
echo "Aborting..."
exit 1
fi
}
#
# Main
#
#
# 1) Split "tagManager" into multiple files.
#
reset
addToIndex "./src/features/tagManagement/api/tagManager.ts"
prompt "for each function, create a new separate file for those functions."
# npm run typecheck
shouldContinue
#
# 2) Change imports and references in all files.
#
reset
# Get paths of newly created files.
filePaths=$(listFilePathsInDirectory "./src/features/tagManagement/api" "*.ts")
param1=$(arrayToString "$filePaths")
# List all files containing "tagManager".
indexFilesContainingText "tagManager" "*.ts*"
param2=$(arrayToString "$(getFilesToIndex)")
# Prompting
message=$(cat <<EOF
The "tagManager" file does not exist anymore and you need to update your imports and references.
Functions inside are now split into multiple files:
$param1
For each of these files, change the imports and references replacing "tagManager" file with the new files:
$param2
EOF
)
prompt "$message"