Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Commit utils.sh

Kilian Lieret4a210ba9871f
diff --git a/tools/defaults/lib/utils.sh b/tools/defaults/lib/utils.sh
new file mode 100644
index 00000000..d06be3b9
--- /dev/null
+++ b/tools/defaults/lib/utils.sh
@@ -0,0 +1,33 @@
+_print() {
+ local CURRENT_FILE=$(_read_env "CURRENT_FILE")
+ local CURRENT_LINE=$(_read_env "CURRENT_LINE")
+ local WINDOW=$(_read_env "WINDOW")
+
+ local total_lines=$(awk 'END {print NR}' "$CURRENT_FILE")
+ echo "[File: $(realpath "$CURRENT_FILE") ($total_lines lines total)]"
+ lines_above=$(jq -n "$CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | floor')
+ lines_below=$(jq -n "$total_lines - $CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | round')
+ if [ $lines_above -gt 0 ]; then
+ echo "($lines_above more lines above)"
+ fi
+ cat "$CURRENT_FILE" | grep -n $ | head -n $(jq -n "[$CURRENT_LINE + $WINDOW/2, $WINDOW/2] | max | floor") | tail -n $(jq -n "$WINDOW")
+ if [ $lines_below -gt 0 ]; then
+ echo "($lines_below more lines below)"
+ fi
+}
+
+_constrain_line() {
+ local CURRENT_FILE=$(_read_env "CURRENT_FILE")
+ local CURRENT_LINE=$(_read_env "CURRENT_LINE")
+ local WINDOW=$(_read_env "WINDOW")
+
+ if [ -z "$CURRENT_FILE" ]
+ then
+ echo "No file open. Use the open command first."
+ return
+ fi
+ local max_line=$(awk 'END {print NR}' "$CURRENT_FILE")
+ local half_window=$(jq -n "$WINDOW/2" | jq 'floor')
+ export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $max_line - $half_window] | min")
+ export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $half_window] | max")
+}
\ No newline at end of file