Artifact 748bb8041d4ef89e02585f1adf33ab14a41633614381a6787369d09ab7f73e10:
- File
test/tui/tui.sh
— part of check-in
[fb82c37817]
at
2026-11-19 14:01:47
on branch models
— Model picker, slash command menu and Pi's model catalog
Models are chosen as in Pi. pic carries a snapshot of Pi's catalog for its providers (653 chat models; tools/model-catalog.sh regenerates it from pi.dev/api/models) and lays fresher per-provider lists from pi.dev over it, cached in ~/.pic/models-store.json with ETags and trusted for four hours, used only when newer than the snapshot. Resolution takes a model's API, endpoint, limits and compat settings from the catalog before the models file. pic models lists the models of providers with a key (--all every one, --json, --refresh); pic models --default saves the model used when none is given.
In the terminal interface, Ctrl+L or /model opens a picker of the models whose provider has a key, filtered by Pi's fuzzy matching as you type, current and default first; Enter uses the model, Ctrl+S also saves it as the default; Ctrl+P cycles. Typing / shows a menu of commands with their arguments (Tab completes, Enter runs) and /model completes model names. New commands show the conversation's details and token counts, copy the last reply (OSC 52 and the system clipboard), export, clone and list the hotkeys. (user: dmestas, size: 4166) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # The terminal interface, on a pseudo-terminal against a fake server: a # reply streams and lands in the transcript, tool calls show a header and # result, the footer counts tokens, Esc cancels a request, commands work # (/name, /resume with its picker, unknown ones), pic tui -c shows the # history, a resize is survived, Ctrl-D quits. 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.XXXXXX") 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; } mkdir "$T/ws" "$T/log" "$T/home" export HOME="$T/home" ANTHROPIC_API_KEY=pic-tui-test-key PIC_MODELS_FILE="$T/models.json" PIC_MODEL_CATALOG=offline # 1 text, 2 tool call, 3 text, 4 hang (cancelled), 5 hang (quit), then text "$SERVER" "$T/port" "$T/log" 120 "sse:$S/text.sse" "sse:$S/thinking_tool.sse" "sse:$S/text.sse" "hang:$S/text.sse:200" \ "hang:$S/text.sse:200" "sse:$S/text.sse" & spid=$! i=0 while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server"; sleep 0.05; done printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m","contextWindow":200000}]}}}' \ "$(cat "$T/port")" > "$T/models.json" drive() { # NAME: run pic with the script on stdin name=$1 shift "$DRIVE" "$T/$name.tx" 80 24 "$PIC" --db "$T/j" --workspace "$T/ws" --model fake/m "$@" || fail "$name: $(perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\r//g' "$T/$name.tx" | grep -v '^\s*$' | tail -8)" } drive first <<'EOF' expect pic fake/m send hello\r expect Hello, "world" expect ↑25 ↓12 send write it\r expect ✓ write t.txt expect Successfully wrote to t.txt expect > send /name main\r expect named main send /bogus\r expect unknown command /bogus size 60 18 wait 300 send \x04 expect pic: conversation EOF [ -f "$T/ws/t.txt" ] || fail "tool: file not written" printf 'PASS: a reply streams, a tool call shows its result, commands work, Ctrl-D quits\n' drive cancel <<'EOF' expect pic fake/m send wait for me\r expect Working wait 500 send \e expect request cancelled send \x04 EOF "$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "cancel: integrity" printf 'PASS: Esc cancels the running request\n' drive resume tui -c <<'EOF' expect continuing expect > wait for me send /resume\r expect Resume a conversation send \e[B wait 200 send \r expect continuing expect > hello expect > write it send \x04 EOF printf 'PASS: pic tui -c shows the history; /resume picks a conversation\n' # Quitting while a request is stuck: Ctrl-C twice leaves promptly, the # request is cancelled and the journal is whole. drive quit <<'EOF2' expect pic fake/m send stuck\r expect Working wait 300 send \x03 wait 100 send \x03 expect pic: conversation EOF2 "$PIC" --db "$T/j" --workspace "$T/ws" log | grep -q '(request cancelled)' || fail "quit: request not cancelled" "$PIC" --db "$T/j" --workspace "$T/ws" check > /dev/null || fail "quit: integrity" printf 'PASS: quitting during a stuck request cancels it and exits\n' # Slash menu and model picker: /mod completes with Tab; /model completes a # model name; Ctrl+L opens the picker, typing filters, Enter picks, Ctrl+S # also saves the default; /copy copies the last reply. drive models <<'EOF2' expect pic fake/m send /mod expect select a model send \t send haiku-5-5 expect claude-haiku-5-5 [anthropic] send \r wait 200 send \r expect model anthropic/claude-haiku-5-5 send \x0c expect Select a model send fake expect m [fake] send \r expect model fake/m send \x0c expect Select a model send fake expect ✓ m [fake] send \x13 expect saved as the default send /copy\r expect no reply to copy yet send \x04 EOF2 grep -q '"defaultModel":"fake/m"' "$HOME/.pic/settings.json" || fail "models: default not saved: $(cat "$HOME/.pic/settings.json" 2>&1)" printf 'PASS: the slash menu completes commands and models; the picker filters, picks and saves a default\n'