fix(image): use temp files for jq to avoid Windows command-line length limit

When running in i2i (image-to-image) mode, the script passes a
base64-encoded image as a jq argument on the command line. On Windows,
cmd.exe limits arguments to ~32 KB, causing 'Argument list too long' errors
when the reference image is large.

Fix: write intermediate JSON fragments to temp files and pass them to jq
via --from-file instead of on the command line. This works reliably on
Windows and is equally valid on Linux/macOS.

Fixes the i2i (--mode i2i --ref-image) workflow on Windows.
This commit is contained in:
Claude
2026-03-27 01:11:07 +08:00
parent 34c6cf05d7
commit b4e6c16f4b

View File

@@ -57,6 +57,75 @@ resolve_image() {
esac esac
} }
# ============================================================================
# Payload builder — avoids command-line length limits on Windows
# Uses temp files for jq when the payload may contain large base64 data.
# ============================================================================
# Build JSON payload, writing large fields (base64 image data) to temp files
# to avoid Windows cmd.exe argument-length limits (~32KB).
build_payload() {
local model="$1" prompt="$2" response_format="$3" n="$4"
local prompt_optimizer="$5" aigc_watermark="$6"
local aspect_ratio="$7" width="$8" height="$9" seed="${10:-}"
local ref_image="${11:-}"
# Start with base payload using temp file to avoid long command lines
local base_tmp
base_tmp="$(mktemp)"
trap "rm -f '$base_tmp'" EXIT INT TERM HUP
jq -n \
--arg model "$model" \
--arg prompt "$prompt" \
--arg rf "$response_format" \
--argjson n "$n" \
--argjson po "$prompt_optimizer" \
--argjson aw "$aigc_watermark" \
'{model: $model, prompt: $prompt, response_format: $rf, n: $n, prompt_optimizer: $po, aigc_watermark: $aw}' \
> "$base_tmp"
# Add optional fields, each via temp file to stay within Windows arg limits
if [[ -n "$aspect_ratio" ]]; then
local tmp2; tmp2="$(mktemp)"; trap "rm -f '$base_tmp' '$tmp2'" EXIT INT TERM HUP
jq --arg ar "$aspect_ratio" '. + {aspect_ratio: $ar}' "$base_tmp" > "$tmp2"
mv "$tmp2" "$base_tmp"
fi
if [[ -n "$width" ]]; then
local tmp2; tmp2="$(mktemp)"; trap "rm -f '$base_tmp' '$tmp2'" EXIT INT TERM HUP
jq --argjson w "$width" '. + {width: $w}' "$base_tmp" > "$tmp2"
mv "$tmp2" "$base_tmp"
fi
if [[ -n "$height" ]]; then
local tmp2; tmp2="$(mktemp)"; trap "rm -f '$base_tmp' '$tmp2'" EXIT INT TERM HUP
jq --argjson h "$height" '. + {height: $h}' "$base_tmp" > "$tmp2"
mv "$tmp2" "$base_tmp"
fi
if [[ -n "$seed" ]]; then
local tmp2; tmp2="$(mktemp)"; trap "rm -f '$base_tmp' '$tmp2'" EXIT INT TERM HUP
jq --argjson s "$seed" '. + {seed: $s}' "$base_tmp" > "$tmp2"
mv "$tmp2" "$base_tmp"
fi
# Subject reference (i2i mode) — build via temp file to avoid huge command-line args
if [[ -n "$ref_image" ]]; then
local img_url
img_url="$(resolve_image "$ref_image")"
# Write the subject_reference array to a temp file, then merge using --from-file
local ref_tmp; ref_tmp="$(mktemp)"; trap "rm -f '$base_tmp' '$ref_tmp'" EXIT INT TERM HUP
jq -n --arg img "$img_url" \
'[{type: "character", image_file: $img}]' \
> "$ref_tmp"
local tmp2; tmp2="$(mktemp)"; trap "rm -f '$base_tmp' '$ref_tmp' '$tmp2'" EXIT INT TERM HUP
jq --from-file "$ref_tmp" '. + {subject_reference: .subject_reference}' "$base_tmp" > "$tmp2"
mv "$tmp2" "$base_tmp"
fi
cat "$base_tmp"
rm -f "$base_tmp"
trap - EXIT INT TERM HUP
}
# ============================================================================ # ============================================================================
# Main # Main
# ============================================================================ # ============================================================================
@@ -107,7 +176,7 @@ Options:
-n, --count N Number of images to generate (1-9, default: 1) -n, --count N Number of images to generate (1-9, default: 1)
--seed N Random seed for reproducibility --seed N Random seed for reproducibility
--prompt-optimizer Enable automatic prompt optimization --prompt-optimizer Enable automatic prompt optimization
--aigc-watermark Add AIGC watermark to generated images --aigc-watermark Add AIGC watermark to generated images
--ref-image FILE Character reference image (local file or URL, i2i mode) --ref-image FILE Character reference image (local file or URL, i2i mode)
--response-format FMT Response format: url (default), base64 --response-format FMT Response format: url (default), base64
--no-download Don't download, just print URL(s) --no-download Don't download, just print URL(s)
@@ -144,31 +213,13 @@ USAGE
echo "Error: -n must be between 1 and 9" >&2; exit 1 echo "Error: -n must be between 1 and 9" >&2; exit 1
fi fi
# Build payload # Build payload using temp-file method (avoids Windows cmd.exe arg-length limit)
local payload local payload
payload=$(jq -n \ payload=$(build_payload \
--arg model "$model" \ "$model" "$prompt" "$response_format" "$n" \
--arg prompt "$prompt" \ "$prompt_optimizer" "$aigc_watermark" \
--arg rf "$response_format" \ "$aspect_ratio" "$width" "$height" "$seed" \
--argjson n "$n" \ "$ref_image")
--argjson po "$prompt_optimizer" \
--argjson aw "$aigc_watermark" \
'{model: $model, prompt: $prompt, response_format: $rf, n: $n, prompt_optimizer: $po, aigc_watermark: $aw}')
[[ -n "$aspect_ratio" ]] && payload=$(echo "$payload" | jq --arg ar "$aspect_ratio" '. + {aspect_ratio: $ar}')
[[ -n "$width" ]] && payload=$(echo "$payload" | jq --argjson w "$width" '. + {width: $w}')
[[ -n "$height" ]] && payload=$(echo "$payload" | jq --argjson h "$height" '. + {height: $h}')
[[ -n "$seed" ]] && payload=$(echo "$payload" | jq --argjson s "$seed" '. + {seed: $s}')
# Subject reference (i2i mode)
if [[ "$mode" == "i2i" ]]; then
if [[ -z "$ref_image" ]]; then
echo "Error: --ref-image is required for i2i mode" >&2; exit 1
fi
local img_url
img_url="$(resolve_image "$ref_image")"
payload=$(echo "$payload" | jq --arg img "$img_url" '. + {subject_reference: [{type: "character", image_file: $img}]}')
fi
local api_host="${MINIMAX_API_HOST:-https://api.minimaxi.com}" local api_host="${MINIMAX_API_HOST:-https://api.minimaxi.com}"
local api_url="${api_host}/v1/image_generation" local api_url="${api_host}/v1/image_generation"