aboutsummaryrefslogtreecommitdiffstats
path: root/agents/coder/tools.sh
blob: 15a320866076485f4962642b8f929815745df2e4 (plain) (blame)
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
#!/usr/bin/env bash
set -e

# @cmd Create a new file at the specified path with contents.
# @option --path! The path where the file should be created
# @option --contents! The contents of the file
fs_create() {
    _guard_path "$argc_path" Create
    printf "%s" "$argc_contents" > "$argc_path"
    echo "File created: $argc_path" >> "$LLM_OUTPUT"
}

# @cmd Apply changes to a file. Use this when you need to edit an existing file.
# YOU ALWAYS PROVIDE THE FULL FILE CONTENTS WHEN EDITING. NO PARTIAL CONTENTS OR COMMENTS.
# YOU MUST PROVIDE THE FULL FILE CONTENTS.

# @option --path! The path of the file to edit
# @option --contents! The new contents to apply to the file
# @meta require-tools git
fs_edit() {
    if [[ -f "$argc_path" ]]; then
        _guard_path "$argc_path" Edit
        changed=0
        printf "%s" "$argc_contents" | git diff --no-index "$argc_path" - || {
            changed=1
        }
        if [[ "$changed" -eq 0 ]]; then
            echo "No changes detected." >> "$LLM_OUTPUT"
        else
            if [ -t 1 ]; then
                echo
                read -r -p "Apply changes? [Y/n] " ans
                if [[ "$ans" == "N" || "$ans" == "n" ]]; then
                    echo "Aborted!"
                    exit 1
                fi
            fi
            printf "%s" "$argc_contents" > "$argc_path"
            echo "Applied changes" >> "$LLM_OUTPUT"
        fi
    else
        echo "Not found file: $argc_path" >> "$LLM_OUTPUT"
    fi
}

_guard_path() {
    path="$(realpath "$1")"
    action="$2"
    if [[ ! "$path" == "$(pwd)"* ]]; then
        if [ -t 1 ]; then
            read -r -p "$action $path? [Y/n] " ans
            if [[ "$ans" == "N" || "$ans" == "n" ]]; then
                echo "Aborted!"
                exit 1
            fi
        fi
    fi
}

# See more details at https://github.com/sigoden/argc
eval "$(argc --argc-eval "$0" "$@")"