Artifact 880404329681334bcd1e05a5d830fec1b715a2e385331b8f96d7b8ba16afd904:
- File
test/pic/chat.sh
— part of check-in
[f41f8c8e00]
at
2026-11-19 11:17:41
on branch events
— Add an interactive line mode: plain pic in a terminal, or pic chat
A prompt that continues one conversation: each message streams its reply, Ctrl-C cancels just that reply, a line ending in a backslash continues the message, and Ctrl-D or /quit leaves. Slash commands start, resume, list, log, name, fork and compact conversations and change the model. The journal is opened only while an input is handled, so other pic commands can use it while the prompt waits. (user: dmestas, size: 3292) [annotate] [blame] [check-ins using] [more...]
#!/bin/sh # The line mode (pic chat; plain pic in a terminal): messages continue one # conversation with the reply streamed, slash commands manage it, and the # journal is free for other pic commands while the prompt waits. set -eu PIC=${PIC:?set PIC to the pic binary} SERVER=${SERVER:?set SERVER to the fake_server binary} S=$(cd "$(dirname "$0")/streams" && pwd) T=$(mktemp -d "${TMPDIR:-/tmp}/pic-chat.XXXXXX") spid= cpid= cleanup() { exec 3>&- 2>/dev/null || : [ -n "$spid" ] && kill "$spid" 2>/dev/null [ -n "$cpid" ] && kill -9 "$cpid" 2>/dev/null rm -rf -- "$T" } trap cleanup EXIT HUP INT TERM fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } export ANTHROPIC_API_KEY=pic-chat-test-key WS="$T/ws" DB="$T/j.sqlite" mkdir "$WS" "$T/log" "$SERVER" "$T/port" "$T/log" 120 "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"}]}}}' \ "$(cat "$T/port")" > "$T/models.json" export PIC_MODELS_FILE="$T/models.json" await() { # PATTERN: wait up to 10s for it in the chat's output i=0 until grep -q "$1" "$T/out" 2>/dev/null; do i=$((i + 1)) [ "$i" -lt 200 ] || fail "timed out waiting for $1: $(cat "$T/out")" sleep 0.05 done } mkfifo "$T/in" "$PIC" --db "$DB" --workspace "$WS" --model fake/m chat < "$T/in" > "$T/out" 2> "$T/err" & cpid=$! exec 3> "$T/in" await '^pic: fake/m in ' printf 'first\n' >&3 await 'Hello, "world"' # Idle at the prompt: the journal is free for another pic command. "$PIC" --db "$DB" --workspace "$WS" sessions > "$T/sessions" 2>&1 || fail "idle: journal held: $(cat "$T/sessions")" printf '/name main\nsecond \\\nline two\n' >&3 i=0 until [ "$(ls "$T/log" | wc -l | tr -d ' ')" = 2 ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "second message"; sleep 0.05; done await 'Hello, "world".*' sleep 0.3 grep -q '"messages":\[{"role":"user","content":"first"},{"role":"assistant",.*{"role":"user","content":"second \\nline two"}\]' "$T/log/2.req" || fail "follow-up: $(grep -o '"messages".*' "$T/log/2.req" | cut -c1-300)" printf '/sessions\n' >&3 await ' main ' printf '/compact focus\n/new\nthird\n' >&3 i=0 until [ "$(ls "$T/log" | wc -l | tr -d ' ')" = 3 ]; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "third message"; sleep 0.05; done sleep 0.3 grep -q '"messages":\[{"role":"user","content":"third"}\]' "$T/log/3.req" || fail "/new: not a new conversation" printf '/bogus\n/quit\n' >&3 exec 3>&- i=0 while kill -0 "$cpid" 2>/dev/null; do i=$((i + 1)); [ "$i" -lt 200 ] || fail "chat did not quit"; sleep 0.05; done wait "$cpid" || fail "chat exit status" cpid= [ "$(grep -c 'Hello, "world"' "$T/out")" = 3 ] || fail "replies not shown once each: $(cat "$T/out")" grep -q 'the history is summarized before the next message' "$T/out" || fail "/compact" grep -q '/bogus' "$T/err" || fail "unknown command not reported" [ "$("$PIC" --db "$DB" --workspace "$WS" sessions | wc -l | tr -d ' ')" = 2 ] || fail "conversations" "$PIC" --db "$DB" --workspace "$WS" check > /dev/null || fail "integrity" printf 'PASS: pic chat continues a conversation, runs its commands, frees the journal while idle\n'