diff --git a/README.md b/README.md index 55507ed..e945099 100644 --- a/README.md +++ b/README.md @@ -723,4 +723,14 @@ In your task file you can import these functions from the taskfile library. # Import commands [ -f ~/taskfile.build/bin/update-submodule ] && source ~/taskfile.build/bin/update-submodule +``` + +### update-with-llm + +```bash + printf "$COLUMN" "update-with-llm" "[glob][prompt]" "Feed files matching glob with prompt to LLM and apply file changes." + +# Import commands + +[ -f ~/taskfile.build/bin/update-with-llm ] && source ~/taskfile.build/bin/update-with-llm ``` \ No newline at end of file diff --git a/bin/commit-with-llm b/bin/commit-with-llm index 5cdc130..a1b4243 100755 --- a/bin/commit-with-llm +++ b/bin/commit-with-llm @@ -1,4 +1,5 @@ #!/bin/bash + function commit-with-llm() { mkdir -p tmp diff --git a/bin/copy-env b/bin/copy-env index d44bae0..1ef05df 100755 --- a/bin/copy-env +++ b/bin/copy-env @@ -1,4 +1,5 @@ #!/bin/bash + function copy-env() { if test -z "$1"; then echo "\$1 is empty."; exit; fi if test -z "$2"; then echo "\$2 is empty."; exit; fi diff --git a/bin/edit-env b/bin/edit-env index 581a051..ee17c7f 100755 --- a/bin/edit-env +++ b/bin/edit-env @@ -1,4 +1,5 @@ #!/bin/bash + function edit-env() { if test -z "$1"; then echo "\$1 is empty."; exit; fi TASK_CONFIG_DIR=${TASK_CONFIG_DIR:="$HOME/.config/taskfile.build"} diff --git a/bin/help b/bin/help index 0ba52b0..8850bf8 100755 --- a/bin/help +++ b/bin/help @@ -1,3 +1,5 @@ +#!/bin/bash + function help() { echo if [ -n "$1" ]; then diff --git a/bin/list-env b/bin/list-env index cafe288..ad6bdc7 100755 --- a/bin/list-env +++ b/bin/list-env @@ -1,4 +1,5 @@ #!/bin/bash + function list-env() { TASK_CONFIG_DIR=${TASK_CONFIG_DIR:="$HOME/.config/taskfile.build"} ls -a1 "$TASK_CONFIG_DIR" diff --git a/bin/load-env b/bin/load-env index b5ea3cb..e909eab 100755 --- a/bin/load-env +++ b/bin/load-env @@ -1,4 +1,5 @@ #!/bin/bash + function load-env() { TASK_CONFIG_DIR=${TASK_CONFIG_DIR:="$HOME/.config/taskfile.build"} ENV_FILEPATH="$TASK_CONFIG_DIR/.env.$1" diff --git a/bin/show-env b/bin/show-env index b6a1b67..a4d4dec 100755 --- a/bin/show-env +++ b/bin/show-env @@ -1,4 +1,5 @@ #!/bin/bash + function show-env() { if test -z "$1"; then echo "\$1 is empty."; exit; fi TASK_CONFIG_DIR=${TASK_CONFIG_DIR:="$HOME/.config/taskfile.build"} diff --git a/bin/update-submodule b/bin/update-submodule index 5f1c7d2..d4e1ba8 100755 --- a/bin/update-submodule +++ b/bin/update-submodule @@ -1,4 +1,5 @@ #!/bin/bash + function update-submodule() { echo "Update git submodules." git submodule update --init --recursive --checkout --depth=1 diff --git a/bin/update-with-llm b/bin/update-with-llm new file mode 100644 index 0000000..b0d4471 --- /dev/null +++ b/bin/update-with-llm @@ -0,0 +1,79 @@ +#!/bin/bash + +function update-with-llm() { + if [ $# -lt 2 ]; then + echo "Usage: update-with-llm [glob] [prompt]" + exit 1 + fi + + # Get the last argument as the task description + TASK_DESCRIPTION="${@: -1}" + + # All preceding arguments are file paths + FILES=("${@:1:$#-1}") + + echo -e "Load these files into prompt:\n\n${FILES[*]}\n" + echo -e "Task: $TASK_DESCRIPTION\n" + + FILE_CONTENTS="" + for FILE in "${FILES[@]}"; do + FILE_CONTENTS+="<<<$FILE>>> +$(cat "$FILE") + +" + done + + # Define prompt content + PROMPT_FILE="tmp/llm_update" + echo -e "\nWrite prompt to $PROMPT_FILE." + cat << EOF > "$PROMPT_FILE" +Look at the code files below and do the following: + +$TASK_DESCRIPTION + +Output all files that you need to change in full again, including your changes. +In the same format as I provide the files below. Under no circumstances output any other text, +no additional infos, no code formatting chars. Only the code in the given format. + +Here are the files: + +$FILE_CONTENTS +EOF + + # Run the llm command + echo -e "Send prompt and wait for the response of the $LLM_MODEL LLM." + RESULT=$(cat "$PROMPT_FILE" | llm -m "$LLM_MODEL") + + # Check if result is empty + if [ -z "$RESULT" ]; then + echo "No response from the model. Exiting." + exit 1 + fi + + # Save the result to a file + RESULT_FILE="tmp/llm_update_result" + echo "$RESULT" > "$RESULT_FILE" + echo -e "Saved response to $RESULT_FILE.\n" + + # Show a preview of result file + less "$RESULT_FILE" + + # Ask for confirmation before updating files + read -p "Do you want to apply these updates to the files? (y/n): " CONFIRM + if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then + exit 0 + fi + + # Parse the response from the file and update the files directly + echo -e "Parsing the response and updating files...\n" + CURRENT_FILE="" + while IFS= read -r LINE; do + if [[ $LINE =~ ^"<<<"(.*)">>>" ]]; then + CURRENT_FILE="${BASH_REMATCH[1]}" + echo "Update file $CURRENT_FILE." + > "$CURRENT_FILE" + elif [[ -n $CURRENT_FILE ]]; then + echo "$LINE" >> "$CURRENT_FILE" + fi + done < "$RESULT_FILE" +} \ No newline at end of file