Fix issue 134: add_message_field no longer accepts raw JSON for tool_calls

The add_message_field function now constructs tool call JSON internally
from simple parameters (id, name, arguments) instead of accepting raw
JSON strings. This makes the test helper functions more robust and
easier to use.

Changed:
- add_message_field: tool_call field now takes id, name, args and builds JSON
- create_message_json: updated to handle tool_call's 3-parameter syntax
- create_messages_json: updated to handle tool_call's 3-parameter syntax
- is_message_field: changed tool_calls to tool_call
- Updated all test files that used the old tool_calls raw JSON syntax
This commit is contained in:
2026-06-03 07:53:39 -05:00
parent 0a734ec2ba
commit d81c75a9a3
5 changed files with 34 additions and 15 deletions
+30 -11
View File
@@ -196,8 +196,12 @@ function add_message_field() {
tool_call_id)
echo "$msg" | jq --arg v "$value" '. + {tool_call_id: $v}'
;;
tool_calls)
echo "$msg" | jq --argjson v "$value" '. + {tool_calls: $v}'
tool_call)
local call_id="$3"
local call_name="$4"
local call_args="$5"
echo "$msg" | jq --arg id "$call_id" --arg name "$call_name" --arg args "$call_args" \
'.tool_calls = ((.tool_calls // []) + [{"id": $id, "type": "function", "function": {"name": $name, "arguments": $args}}])'
;;
*)
echo "ERROR: Unknown field '$field'" >&2
@@ -209,10 +213,11 @@ export -f add_message_field
# Create a message JSON object with optional fields
# Usage: create_message_json <role> <content> [field value ...]
# Supported fields: reasoning, reasoning_content, tool_call_id, tool_calls (as JSON)
# Supported fields: reasoning, reasoning_content, tool_call_id, tool_call
# tool_call takes 3 args: tool_call <id> <name> <arguments>
# Example: create_message_json user "hello"
# Example: create_message_json user "hello" reasoning "thought" tool_call_id "call_123"
# Example: create_message_json assistant "" tool_calls '[{"id":"call_1","type":"function","function":{"name":"test","arguments":"{}"}}]'
# Example: create_message_json assistant "" tool_call "call_1" "test" "{}"
function create_message_json() {
local role="$1"
local content="$2"
@@ -223,9 +228,17 @@ function create_message_json() {
while [ $# -gt 0 ]; do
local field="$1"
local value="$2"
shift 2
msg=$(add_message_field "$msg" "$field" "$value")
if [ "$field" = "tool_call" ]; then
local call_id="$2"
local call_name="$3"
local call_args="$4"
shift 4
msg=$(add_message_field "$msg" "$field" "$call_id" "$call_name" "$call_args")
else
local value="$2"
shift 2
msg=$(add_message_field "$msg" "$field" "$value")
fi
done
echo "$msg"
}
@@ -234,7 +247,7 @@ export -f create_message_json
# Check if a string is a known message field name
function is_message_field() {
case "$1" in
reasoning|reasoning_content|tool_call_id|tool_calls) return 0 ;;
reasoning|reasoning_content|tool_call_id|tool_call) return 0 ;;
*) return 1 ;;
esac
}
@@ -243,7 +256,8 @@ export -f is_message_field
# Create a JSON array of messages
# Usage: create_messages_json <role> <content> [field value ...] [role content ...]
# Each message: role content [field value ...]
# Supported fields: reasoning, reasoning_content, tool_call_id, tool_calls (as JSON)
# Supported fields: reasoning, reasoning_content, tool_call_id, tool_call
# tool_call takes 3 args: tool_call <id> <name> <arguments>
# Example: create_messages_json user "hello" assistant "hi there"
# Example: create_messages_json user "hello" reasoning "thought" assistant "hi"
function create_messages_json() {
@@ -257,8 +271,13 @@ function create_messages_json() {
# Collect field-value pairs for this message into an array
local -a field_args=()
while [ $# -gt 0 ] && is_message_field "$1"; do
field_args+=("$1" "$2")
shift 2
if [ "$1" = "tool_call" ]; then
field_args+=("$1" "$2" "$3" "$4")
shift 4
else
field_args+=("$1" "$2")
shift 2
fi
done
local msg
@@ -2,7 +2,7 @@
# test_rebase_apply_edit_preserves_tool_calls.sh - tool_calls field preserved during edit
lines=$(create_rebase_lines edit 0 assistant)
history=$(create_messages_json assistant "calling tool" tool_calls '[{"id":"call_1","type":"function","function":{"name":"test","arguments":"{}"}}]')
history=$(create_messages_json assistant "calling tool" tool_call "call_1" "test" "{}")
OUTPUT=$("${DRHARNESS}" -A "$lines" "$history")
@@ -4,7 +4,7 @@
# Drop the tool call message but keep the result (orphaned)
lines=$(create_rebase_lines drop 0 assistant pick 1 tool)
history=$(create_messages_json \
assistant "" tool_calls '[{"id":"call_1","type":"function","function":{"name":"test","arguments":""}}]' \
assistant "" tool_call "call_1" "test" "" \
tool "result" tool_call_id "call_1")
OUTPUT=$("${DRHARNESS}" -A "$lines" "$history")
@@ -3,7 +3,7 @@
test_name="multiple_tool_calls"
json=$(create_messages_json assistant "" tool_calls '[{"id":"call_1","type":"function","function":{"name":"read_file","arguments":""}},{"id":"call_2","type":"function","function":{"name":"write_file","arguments":""}}]')
json=$(create_messages_json assistant "" tool_call "call_1" "read_file" "" tool_call "call_2" "write_file" "")
OUTPUT=$("${DRHARNESS}" -B "$json")
@@ -3,7 +3,7 @@
test_name="tool_call_display"
json=$(create_messages_json assistant "" tool_calls '[{"id":"call_1","type":"function","function":{"name":"run_shell","arguments":""}}]')
json=$(create_messages_json assistant "" tool_call "call_1" "run_shell" "")
OUTPUT=$("${DRHARNESS}" -B "$json")