Skip to content

Commit d43292c

Browse files
committed
Improved template handling for document commands
Generalized cmd_document to support custom templates [feat] Made cmd_document use full path to prompt templates [refactor] Removed hardcoded document types from cmd_document function signature [fix] Added validation check for template file existence [docs] Updated test_commands.bats to use TEMPLATES_DIR for template paths
1 parent a903a62 commit d43292c

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

src/giv.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,20 +711,20 @@ if [ "${_is_sourced}" -eq 0 ]; then
711711
case "${subcmd}" in
712712
message) cmd_message "${REVISION}" ;;
713713
summary) cmd_document \
714-
summary \
714+
"${TEMPLATE_DIR}/final_summary_prompt.md" \
715715
"${REVISION}" \
716716
"${output_file:-}" \
717717
"${model_mode}" \
718718
"0.7" "" ;;
719719
release-notes) cmd_document \
720-
release_notes \
720+
"${TEMPLATE_DIR}/release_notes_prompt.md" \
721721
"${REVISION}" \
722722
"${output_file:-$release_notes_file}" \
723723
"${model_mode}" \
724724
"0.6" \
725725
"65536" ;;
726726
announcement) cmd_document \
727-
announcement \
727+
"${TEMPLATE_DIR}/announcement_prompt.md" \
728728
"${REVISION}" \
729729
"${output_file:-$announce_file}" \
730730
"${model_mode}" \

src/helpers.sh

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -855,54 +855,64 @@ summarize_commit() {
855855

856856
printf '%s\n' "${res}"
857857
}
858-
859858
# -------------------------------------------------------------------
860-
# cmd_document: generic driver for summary/release_notes/announcement
859+
# cmd_document: generic driver for any prompt template
861860
#
862861
# Arguments:
863-
# $1 = document type (e.g. "summary", "release_notes", "announcement")
864-
# $2 = revision specifier (e.g. "--current" or "$REVISION")
862+
# $1 = full path to prompt template file
863+
# $2 = revision specifier (e.g. "--current" or "$REVISION")
865864
# $3 = output file path
866-
# $4 = model mode (e.g. "auto", "your-model")
867-
# $5 = temperature (e.g. "0.7", "0.6")
868-
# $6 = context window size (optional; e.g. "65536" or "")
869-
# $7 = any extra flags to pass to build_prompt
865+
# $4 = model mode (e.g. "auto", "your-model")
866+
# $5 = temperature (e.g. "0.7", "0.6")
867+
# $6 = context window size (optional; e.g. "65536")
868+
# $7 = extra flags for build_prompt (e.g. --example, --rules)
870869
#
871870
# Side-effects:
872871
# - Summaries are written to a temp file
873872
# - A prompt is built and written to another temp file
874873
# - generate_from_prompt is invoked to create the final output
875874
#
876875
cmd_document() {
877-
doc_type="$1"
876+
prompt_tpl="$1"
878877
revision="${2:---current}"
879878
out="${3:-}"
880879
mode="${4:-auto}"
881880
temp="${5:-0.9}"
882881
ctx="${6:-32768}"
883882
shift 6
884883

884+
# validate template exists
885+
if [ ! -f "${prompt_tpl}" ]; then
886+
printf 'template file not found: %s\n' "${prompt_tpl}" >&2
887+
exit 1
888+
fi
889+
890+
# derive base name for temp file prefixes
891+
doc_base=$(basename "${prompt_tpl%.*}")
892+
885893
# 1) Summarize
886-
summaries=$(portable_mktemp "${doc_type}_summaries_XXXXXX.md")
887-
print_debug "Generating ${doc_type} summaries to: ${summaries}"
894+
summaries=$(portable_mktemp "${doc_base}_summaries_XXXXXX.md")
895+
print_debug "Generating summaries to: ${summaries}"
888896
summarize_target "${revision}" "${summaries}" "${mode}"
889897

890-
# Bail if nothing came back
898+
# bail if no summaries
891899
if [ ! -f "${summaries}" ]; then
892-
printf 'Error: No summaries generated for %s.\n' "${doc_type}" >&2
900+
printf 'Error: No summaries generated for %s.\n' "${revision}" >&2
893901
exit 1
894902
fi
895903

896904
# 2) Build prompt
897-
prompt_tpl="${TEMPLATE_DIR}/${doc_type}_prompt.md"
898-
prompt_tmp=$(portable_mktemp "${doc_type}_prompt_XXXXXX.md")
899-
905+
prompt_tmp=$(portable_mktemp "${doc_base}_prompt_XXXXXX.md")
900906
title=$(parse_project_title "${summaries}")
901-
current_version=$(get_version_info "${revision}" "$(find_version_file)")
907+
current_version="$(get_version_info --current "$(find_version_file)" )"
902908

903-
print_debug "Project title: ${title}"
904-
build_prompt --project-title "${title}" --version "${current_version}" \
905-
--template "${prompt_tpl}" --summary "${summaries}" >"${prompt_tmp}"
909+
print_debug "Building prompt from ${prompt_tpl} using ${summaries}"
910+
build_prompt \
911+
--project-title "${title}" \
912+
--version "${current_version}" \
913+
--template "${prompt_tpl}" \
914+
--summary "${summaries}" \
915+
"$@" >"${prompt_tmp}"
906916

907917
print_debug "Built prompt file: ${prompt_tmp}"
908918

@@ -913,3 +923,4 @@ cmd_document() {
913923
generate_from_prompt "${prompt_tmp}" "${out}" "${mode}" "${temp}"
914924
fi
915925
}
926+

tests/test_commands.bats

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ BATS_TEST_START_TIME="$(date +%s)"
1111
SCRIPT="$BATS_TEST_DIRNAME/../src/giv.sh"
1212
# shellcheck source=../src/giv.sh
1313
HELPERS="$BATS_TEST_DIRNAME/../src/helpers.sh"
14+
# shellcheck source=../src/giv.sh
15+
TEMPLATES_DIR="$BATS_TEST_DIRNAME/../templates"
1416

1517
setup() {
1618
# create a temp git repo
@@ -185,7 +187,7 @@ EOF
185187
summarize_target() { echo "SUM"; }
186188

187189

188-
run cmd_document "summary" "--current" "" "auto" "0.7" ""
190+
run cmd_document "$TEMPLATES_DIR/final_summary_prompt.md" "--current" "" "auto" "0.7" ""
189191
printf "Output: %s\n" "$output"
190192
ollama() {
191193
# shellcheck disable=SC2317
@@ -199,15 +201,15 @@ EOF
199201
ollama() {
200202
echo "SUM"
201203
}
202-
run cmd_document "summary" HEAD~1 "" "auto" "0.7"
204+
run cmd_document "$TEMPLATES_DIR/final_summary_prompt.md" HEAD~1 "" "auto" "0.7"
203205
assert_success
204206
assert_output --partial "SUM"
205207
}
206208

207209
@test "cmd_summary writes to file when output_file set" {
208210
output_file="out.sum"
209211
summarize_target() { echo "SUM"; }
210-
run cmd_document "summary" "--current" "${output_file}" "auto"
212+
run cmd_document "$TEMPLATES_DIR/final_summary_prompt.md" "--current" "${output_file}" "auto"
211213
assert_success
212214
[ -f out.sum ]
213215
assert_output --partial "Response written to out.sum"
@@ -219,13 +221,13 @@ EOF
219221
#----------------------------------------
220222

221223
@test "cmd_release_notes writes to its default file" {
222-
run cmd_document "release_notes" "" "RELEASE_NOTES.md"
224+
run cmd_document "$TEMPLATES_DIR/release_notes_prompt.md" "" "RELEASE_NOTES.md"
223225
assert_success
224226
assert_output --partial "Response written to RELEASE_NOTES.md"
225227
}
226228

227229
@test "cmd_announcement writes to its default file" {
228-
run cmd_document "announcement" "" "ANNOUNCEMENT.md"
230+
run cmd_document "$TEMPLATES_DIR/announcement_prompt.md" "" "ANNOUNCEMENT.md"
229231
assert_success
230232
assert_output --partial "Response written to ANNOUNCEMENT.md"
231233
}

0 commit comments

Comments
 (0)