Artifact f8b2d7a2499a9cc2946c6501fda29658b015ab28b0b2ed9cf4ec32765d7b5c6c:
- File
test/tui/tui.sh
— part of check-in
[cb2ad5fbe9]
at
2026-11-19 13:09:42
on branch tui
— Add a terminal interface: plain pic in a terminal
A module in the pic binary (src/tui) that runs each request as a child pic --json process and knows it only through its events, so a fault in the interface cannot touch a run. Finished output is committed once to the terminal's scrollback; a small live area (the reply being streamed, a running tool call, status, queued messages, the editor and a footer with the directory, conversation, tokens, context and model) is redrawn from its first changed line, inside synchronized output. A reply that fails mid-stream only ever lived in the live area and simply goes away.
Editor: multi-line, history, bracketed paste with large pastes shown as atomic markers. Keys: Enter sends (queues while a reply runs), Ctrl+J or Alt+Enter adds a line, Esc interrupts, Ctrl+C clears and twice quits, Ctrl+D quits, Ctrl+O expands later tool output. Commands: new, resume with a picker, name, fork, compact, model, help, quit; commands typed during a reply wait for it. pic tui -c continues the latest conversation and shows its history; pic chat remains the plain prompt.
For clients, agent_start now carries the conversation, model and context window and message_end the usage; the conversation list and a conversation's entries are available as JSON. Tested with unit tests for the width, key, render and editor modules and end to end on a pseudo-terminal. (user: dmestas, size: 2690) [annotate] [blame] [check-ins using]
#!/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" # 1 text, 2 tool call, 3 text, 4 hang (cancelled), 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" \ "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'