blob: 3ff38d4d66b83856ac5a9c8fbe123fdbb791bbc6 (
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
62
63
64
|
#!/usr/bin/env bash
set -uo pipefail
# @describe Write the full file contents to a file at the specified path.
# This tool NEVER requests user interaction and NEVER exits with a non-zero code.
# Any error is reported in the tool output so the model can react within the same cycle.
# @option --path! The path of the file to write to
# @option --contents! The full contents to write to the file
# @env LLM_OUTPUT=/dev/stdout The output path
ROOT_DIR="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
OUT="${LLM_OUTPUT:-/dev/stdout}"
# Report an error in the tool output and finish successfully (exit 0).
err() {
echo "ERROR: $*" >> "$OUT"
exit 0
}
main() {
path="${argc_path:-}"
contents="${argc_contents:-}"
[[ -z "$path" ]] && err "missing --path"
# empty contents are allowed (used to truncate a file)
if [[ -e "$path" && ! -f "$path" ]]; then
err "target exists but is not a regular file: $path"
fi
# Show the diff (if any) so the model sees what is written.
# NOTE: no guard_operation.sh / guard_path.sh here -> no user interaction.
if [[ -f "$path" ]]; then
printf "%s" "$contents" | git diff --no-index "$path" - >> "$OUT" 2>/dev/null || true
fi
dir="$(dirname "$path")"
if ! mkdir -p "$dir" 2>/tmp/fs_write_new_mkdir.err; then
msg="$(cat /tmp/fs_write_new_mkdir.err 2>/dev/null || true)"
err "failed to create parent dir $dir: ${msg:-unknown error}"
fi
if ! printf "%s" "$contents" > "$path" 2>/tmp/fs_write_new.err; then
msg="$(cat /tmp/fs_write_new.err 2>/dev/null || true)"
err "failed to write file $path: ${msg:-unknown error}"
fi
echo "The contents written to: $path" >> "$OUT"
exit 0
}
# Parse arguments with argc and run the tool in a subshell:
# - argc calls `main`, which always ends with `exit 0`.
# - If argc itself fails (missing/invalid arguments) its generated code prints the
# error to stderr and calls `exit 1`; running it in a subshell keeps that from
# terminating this script, and the message is captured into the tool output.
# - The script itself always finishes with exit 0.
argc_err="$(mktemp)"
( eval "$(argc --argc-eval "$0" "$@" 2>/dev/null)" ) 2>"$argc_err"
if [[ -s "$argc_err" ]]; then
{ echo "ERROR: argument parsing failed:"; cat "$argc_err"; } >> "$OUT"
fi
rm -f "$argc_err"
exit 0
|