ThinkThen

Bash techniques
Bash · Techniques

Keep one process for a step loop

coproc holds one choose process open. --batch 1 prints each answer while the input stays open. The example replays a recording. It needs no key and no network. Use a library binding when each call must cost less time.

steps.jsonl, a file the script reads
{"id":"S-1","state":"A customer wrote to ask where their order is. Nothing has been looked up yet, and the message carries an order number.","actions":["look_up_the_order","ask_for_the_order_number","open_a_carrier_claim","close_the_ticket"]}
{"id":"S-2","state":"The order was found. It shipped nine days ago and the carrier's tracking has not moved for six days. The customer has not been told anything yet.","actions":["open_a_carrier_claim","send_the_tracking_link","ask_for_the_order_number","close_the_ticket"]}
{"id":"S-3","state":"The carrier confirmed in writing that the parcel is lost. The customer replied that they still want the item and do not want their money back.","actions":{"ship_a_replacement":"Send the same item again at no charge.","refund_the_order":"Give the money back and send nothing.","open_a_carrier_claim":"Ask the carrier to investigate the parcel.","close_the_ticket":"End the conversation with no further action."}}
Three changing action lists produce three answers from one process.
coproc PICK {
  thinkthen choose \
    'Which of these actions should be taken next?' \
    --jsonl --field /state --options /actions \
    --raw --batch 1 --replay recording
}
while IFS= read -r step; do
  printf '%s\n' "$step" >&"${PICK[1]}"
  IFS= read -r chosen <&"${PICK[0]}" || exit 1
  printf '%s\n' "$chosen"
done < steps.jsonl
exec {PICK[1]}>&-
wait "$PICK_PID"
Output
look_up_the_order
open_a_carrier_claim
ship_a_replacement
exit 0