aboutsummaryrefslogtreecommitdiffstats
path: root/agents
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-08-02 23:26:49 +0800
committerGitHub <noreply@github.com>2024-08-02 23:26:49 +0800
commitd66d8938f1f845e92966835f70bc2f97ebb8246b (patch)
treee81b6da5ff07f1aeaa1799a314f7850c89e97d1c /agents
parentf5c67b3eff5925e35379760d57f886f705120158 (diff)
downloadllm-functions-docker-d66d8938f1f845e92966835f70bc2f97ebb8246b.tar.gz
feat: prompt confirmation for fs write operations outside cwd (#90)
Diffstat (limited to 'agents')
-rwxr-xr-xagents/coder/tools.sh32
1 files changed, 22 insertions, 10 deletions
diff --git a/agents/coder/tools.sh b/agents/coder/tools.sh
index b0c0cd4..685746a 100755
--- a/agents/coder/tools.sh
+++ b/agents/coder/tools.sh
@@ -1,15 +1,13 @@
#!/usr/bin/env bash
set -e
-# @env FS_BASE_DIR=. The base dir
-
# @cmd Create a new file at the specified path with content.
# @option --path! The path where the file should be created
# @option --content! The content of the file
fs_create() {
- path="$FS_BASE_DIR/$argc_path"
- printf "%s" "$argc_content" > "$path"
- echo "File created: $path" >> "$LLM_OUTPUT"
+ _guard_path "$argc_path" Create
+ printf "%s" "$argc_content" > "$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.
@@ -20,10 +18,10 @@ fs_create() {
# @option --content! The new content to apply to the file
# @meta require-tools git
fs_edit() {
- path="$FS_BASE_DIR/$argc_path"
- if [[ -f "$path" ]]; then
+ if [[ -f "$argc_path" ]]; then
+ _guard_path "$argc_path" Edit
changed=0
- printf "%s" "$argc_content" | git diff --no-index "$path" - || {
+ printf "%s" "$argc_content" | git diff --no-index "$argc_path" - || {
changed=1
}
if [[ "$changed" -eq 0 ]]; then
@@ -37,11 +35,25 @@ fs_edit() {
exit 1
fi
fi
- printf "%s" "$argc_content" > "$path"
+ printf "%s" "$argc_content" > "$argc_path"
echo "Applied changes" >> "$LLM_OUTPUT"
fi
else
- echo "Not found file: $path" >> "$LLM_OUTPUT"
+ 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
}