#!/bin/sh
# The model last chosen in the terminal interface is the one the next conversation starts on:
# a picker pick and /model NAME save it, and the first screen says "your last choice". Ctrl+P only
# cycles and saves nothing, as --model and the setting rememberModel=false save nothing; ask and -p
# start on it too.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
DRIVE=${DRIVE:?set DRIVE to the pty_drive binary}
S=$(cd "$(dirname "$0")/../pic/streams" && pwd)
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-lastmodel.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
unset PIC_TEST_PASSPHRASE
mkdir "$T/ws" "$T/log" "$T/home"
export HOME="$T/home" PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 T
export PIC_NO_NATIVE_CLIPBOARD=1
"$SERVER" "$T/port" "$T/log" 120 "at:/v1/models=status:404:-" "sse:$S/text.sse" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
port=$(cat "$T/port")
cat > "$T/models.json" <<EOM
{"providers":{
"good":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$GOOD_API_KEY","models":[
{"id":"m","contextWindow":200000},{"id":"n","contextWindow":200000}]},
"fake":{"baseUrl":"http://127.0.0.1:$port","api":"anthropic-messages","apiKey":"\$FAKE_API_KEY","models":[
{"id":"m","contextWindow":200000}]}}}
EOM
export GOOD_API_KEY=pic-lm-good FAKE_API_KEY=pic-lm-fake
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -12; }
drive() { # NAME ARGS...: pic on a pseudo-terminal, the script on stdin
name=$1
shift
"$DRIVE" "$T/$name.tx" 120 30 "$PIC" --tui-mode regular --db "$T/j" --workspace "$T/ws" "$@" || fail "$name: $(screen "$T/$name.tx")"
}
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }
saved() { pic settings defaultModel || :; }
# A pick in the picker is the next conversation's model, and the first screen says so.
drive pick --model good/m <<'EOF2'
expect pic good/m
send \ep
expect Select a model
send fake
send \r
expect model fake/m; new conversations start on it
send \x04\x04
EOF2
[ "$(saved)" = fake/m ] || fail "pick: not saved: $(saved)"
drive next <<'EOF2'
expect pic fake/m
expect model fake/m (your last choice;
send \x04\x04
EOF2
printf 'PASS: a model picked in the picker is where the next conversation starts\n'
# -p and ask pick it up too.
pic -p x < /dev/null > /dev/null 2> "$T/p.err" || :
grep -qF 'pic: model fake/m (your last choice' "$T/p.err" || fail "-p: $(cat "$T/p.err")"
printf 'PASS: -p starts on the last choice\n'
# --model is for one run: it saves nothing, and a run with it is not a choice.
drive flag --model good/n <<'EOF2'
expect pic good/n
send \x04\x04
EOF2
[ "$(saved)" = fake/m ] || fail "--model saved: $(saved)"
printf 'PASS: --model saves nothing\n'
# /model NAME saves, Ctrl+P does not; the confirmation is said once, not on every step.
drive slash <<'EOF2'
expect pic fake/m
send /model good/n\r
expect model good/n; new conversations start on it
send \x10
expect model
send \x10
expect model
send \x04\x04
EOF2
n=$(grep -c 'new conversations start on it' "$T/slash.tx" || :)
[ "$n" -le 2 ] || fail "confirmation repeated ($n)"
[ "$(saved)" = good/n ] || fail "/model NAME: not saved: $(saved)"
grep -qF 'this conversation only; Shift+Ctrl+P goes back to good/n' "$T/slash.tx" || fail "Ctrl+P: no notice: $(screen "$T/slash.tx")"
[ "$(saved)" = good/n ] || fail "Ctrl+P saved: $(saved)"
drive cycled <<'EOF2'
expect pic good/n
expect (your last choice;
send \x04\x04
EOF2
printf 'PASS: /model NAME saves the model; Ctrl+P does not\n'
# Ctrl+S in the picker is the same as Enter, and says it is already saved.
drive ctrls --model good/m <<'EOF2'
expect pic good/m
send \ep
expect Select a model
send fake
send \x13
expect already saved
send \x04\x04
EOF2
[ "$(saved)" = fake/m ] || fail "ctrl+s: $(saved)"
printf 'PASS: Ctrl+S in the picker says it is already saved\n'
# rememberModel=false is the old behaviour: a pick lasts for the conversation, Ctrl+S saves.
pic settings rememberModel false > /dev/null
drive off --model good/m <<'EOF2'
expect pic good/m
send \ep
expect Select a model
send good/n
send \r
expect model good/n
send \x04\x04
EOF2
[ "$(saved)" = fake/m ] || fail "off: saved $(saved)"
drive offsave --model good/m <<'EOF2'
expect pic good/m
send \ep
expect Select a model
send good/n
send \x13
expect saved as the default
send \x04\x04
EOF2
[ "$(saved)" = good/n ] || fail "off+ctrl+s: $(saved)"
drive offnext <<'EOF2'
expect model good/n (your saved default;
send \x04\x04
EOF2
printf 'PASS: rememberModel false keeps a fixed default; Ctrl+S saves it\n'
# A conversation taken up goes on with its own model, whatever the saved default, and says so; one
# whose key is gone says which model it ran on, and what the next message goes to.
pic models --default fake/m > /dev/null
pic --model good/n --session-id conv-t -p 'kept' < /dev/null > /dev/null 2> "$T/kept.err" || fail "kept: $(cat "$T/kept.err")"
drive own tui --session conv-t <<'EOF2'
expect continuing conv-t
expect model good/n (this conversation's own; /model to change)
send \x04\x04
EOF2
! grep -q '(your saved default;' "$T/own.tx" || fail "own: the saved default was given as the reason: $(screen "$T/own.tx")"
drive owncont tui -c <<'EOF2'
expect model good/n (this conversation's own;
send \x04\x04
EOF2
(
unset GOOD_API_KEY
drive gone tui --session conv-t <<'EOF2'
expect this conversation ran on good/n, and good has no key now; it goes on with fake/m
expect /model picks another model
send \x04\x04
EOF2
)
drive flagged --model fake/m tui --session conv-t <<'EOF2'
expect continuing conv-t
send \x04\x04
EOF2
! grep -q "this conversation's own" "$T/flagged.tx" || fail "--model: the conversation's model replaced it: $(screen "$T/flagged.tx")"
printf 'PASS: a conversation taken up in the terminal goes on with its own model, and says so\n'