#!/bin/sh
# Live format check of web search, one real search per source: the tests use a fake server, so this
# is what tells a decoder that a provider changed the shape of its search. Not part of make
# test-pic, and it spends money: each provider gets one short question on its smallest model that
# can search, plus one tiny follow-up that checks the provider takes the search back as pic replays
# it. Run it by hand with keys in the environment:
#
# ANTHROPIC_API_KEY=... OPENAI_API_KEY=... sh test/live/websearch-live.sh [--chatgpt] [SOURCE...]
#
# Sources: anthropic openai xai openrouter gemini (each needs its *_API_KEY; GEMINI_API_KEY for
# gemini), brave (BRAVE_API_KEY), stackexchange (no key; one of the 300 a day of this address), and
# chatgpt (only with --chatgpt: it uses the ChatGPT sign-in stored in your real ~/.pic, so it runs
# with your HOME; everything else runs with a private HOME). With no SOURCE, every source whose key
# is set. A model can be changed with LIVE_MODEL_<SOURCE>, such as LIVE_MODEL_OPENAI=gpt-5-mini.
#
# PIC (default build/pic/release/pic) and SQLITE3 (default build/pic/sqlite3) name the binaries.
# Exit 0 when every source checked passed, 1 when one failed.
set -u
here=$(cd "$(dirname "$0")/../.." && pwd)
PIC=${PIC:-$here/build/pic/release/pic}
SQLITE3=${SQLITE3:-$here/build/pic/sqlite3}
[ -x "$PIC" ] || { echo "websearch-live: no pic at $PIC (make build/pic/release/pic, or set PIC)" >&2; exit 2; }
[ -x "$SQLITE3" ] || { echo "websearch-live: no sqlite3 at $SQLITE3 (make build/pic/sqlite3, or set SQLITE3)" >&2; exit 2; }
CHATGPT=no
[ "${1:-}" = --chatgpt ] && { CHATGPT=yes; shift; }
REALHOME=$HOME
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-websearch-live.XXXXXX") || exit 2
T=$(cd "$T" && pwd -P)
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
mkdir "$T/home"
export PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline PIC_NO_AUTO_UPDATE=1
QUESTION='Search the web once for the latest SQLite release number, then answer in one short sentence.'
FOLLOW='Reply with the single word ok.'
failed=0
n=0
say() { printf '%-14s %s\n' "$1" "$2"; }
bad() { say "$1" "FAIL: $2"; failed=1; }
# sql DB QUERY: one value from the journal.
sql() { "$SQLITE3" "$1" "$2" 2>/dev/null; }
native() { sql "$1" "SELECT native FROM pic_entry WHERE kind = 'answer' AND native IS NOT NULL ORDER BY seq LIMIT 1"; }
neutral() { sql "$1" "SELECT group_concat(body, char(10)) FROM pic_entry WHERE kind = 'answer'"; }
# has DB SQL: the first answer's native form satisfies SQL, a WHERE over json_each(n) and n.value.
has() { [ "$(sql "$1" "SELECT count(*) FROM (SELECT native AS j FROM pic_entry WHERE kind = 'answer' AND native IS NOT NULL ORDER BY seq LIMIT 1), json_each(j) n WHERE $2")" != 0 ]; }
# ask SOURCE MODEL HOME: the question, then the follow-up, to MODEL; the journal in $DB.
ask() {
n=$((n + 1))
DB="$T/$n.sqlite" WS="$T/ws$n"
mkdir "$WS"
# allow: the provider's search is declared only where pic need not ask first (a real ~/.pic may
# say otherwise)
HOME=$3 "$PIC" --db "$DB" --workspace "$WS" --permission-mode allow submit job "$QUESTION" > /dev/null || return 1
HOME=$3 "$PIC" --db "$DB" --workspace "$WS" run --model "$2" > "$T/$n.out" 2> "$T/$n.err" || return 1
HOME=$3 "$PIC" --db "$DB" --workspace "$WS" submit job2 "$FOLLOW" --session latest > /dev/null || return 1
HOME=$3 "$PIC" --db "$DB" --workspace "$WS" run --model "$2" > "$T/$n.out2" 2> "$T/$n.err2" || return 2
}
err() { tail -3 "$T/$n.err" "$T/$n.err2" 2>/dev/null | grep -v '^==>' | tr '\n' ' ' | cut -c1-300; }
# searched SOURCE: the transcript shows the search as text, as every decoder writes it.
searched() { neutral "$DB" | grep -q 'Searched the web' || { bad "$1" "no 'Searched the web' line in the transcript"; return 1; }; }
check() { # check SOURCE MODEL [HOME]
src=$1 model=$2 home=${3:-$T/home}
ask "$src" "$model" "$home"
rc=$?
if [ "$rc" = 1 ]; then bad "$src" "the search request failed ($model; is webSearch auto?): $(err)"; return; fi
if [ "$rc" = 2 ]; then bad "$src" "the follow-up, which sends the search back, was refused ($model): $(err)"; return; fi
case $src in
anthropic)
has "$DB" "json_extract(n.value,'\$.type') = 'server_tool_use' AND json_extract(n.value,'\$.name') = 'web_search'
AND json_type(n.value,'\$.input.query') = 'text'" || { bad "$src" "no server_tool_use web_search with input.query"; return; }
has "$DB" "json_extract(n.value,'\$.type') = 'web_search_tool_result' AND json_type(n.value,'\$.content') = 'array'
AND json_type(n.value,'\$.content[0].url') = 'text' AND json_type(n.value,'\$.content[0].encrypted_content') = 'text'" ||
{ bad "$src" "no web_search_tool_result with content[].url and encrypted_content"; return; }
has "$DB" "json_extract(n.value,'\$.type') = 'text' AND json_type(n.value,'\$.citations') = 'array'" ||
say "$src" "note: no text block carried citations"
;;
openai | xai | chatgpt)
has "$DB" "json_extract(n.value,'\$.type') = 'web_search_call' AND (json_type(n.value,'\$.action.query') = 'text'
OR json_type(n.value,'\$.action.queries') = 'array')" || { bad "$src" "no web_search_call item with action.query or action.queries"; return; }
has "$DB" "json_extract(n.value,'\$.type') = 'message' AND EXISTS (SELECT 1 FROM json_each(n.value,'\$.content') c,
json_each(c.value,'\$.annotations') a WHERE json_extract(a.value,'\$.type') = 'url_citation')" ||
say "$src" "note: no url_citation annotation"
;;
openrouter) # its decoder shows the sources only; there is no search line to look for
neutral "$DB" | grep -q 'Sources:' || { bad "$src" "no Sources list (delta.annotations url_citation)"; return; }
say "$src" "ok ($model)"
return
;;
gemini)
has "$DB" "json_type(n.value,'\$.toolCall') = 'object' AND json_extract(n.value,'\$.toolCall.toolType') LIKE 'GOOGLE_SEARCH%'
AND json_type(n.value,'\$.toolCall.id') = 'text'" || { bad "$src" "no toolCall part with a GOOGLE_SEARCH toolType and an id"; return; }
has "$DB" "json_type(n.value,'\$.toolResponse') = 'object' AND json_type(n.value,'\$.toolResponse.id') = 'text'" ||
{ bad "$src" "no toolResponse part with an id"; return; }
has "$DB" "json_type(n.value,'\$.thoughtSignature') = 'text' AND (json_type(n.value,'\$.toolCall') = 'object')" ||
say "$src" "note: the toolCall part had no thoughtSignature"
neutral "$DB" | grep -q 'Sources:' || say "$src" "note: no groundingMetadata sources"
;;
esac
searched "$src" || return
say "$src" "ok ($model)"
}
tool() { # tool SOURCE ENV...: pic's own web_search, called by a scripted reply (no model, no cost)
src=$1
shift
n=$((n + 1))
DB="$T/$n.sqlite" WS="$T/ws$n"
mkdir "$WS"
printf 'tool raw web_search {"query":"sqlite database is locked error","count":3}\ntext done\n' > "$T/script"
HOME=$T/home "$PIC" --db "$DB" --workspace "$WS" --tools +web_search submit job search > /dev/null
env -u BRAVE_API_KEY HOME="$T/home" "$@" "$PIC" --db "$DB" --workspace "$WS" --tools +web_search run --script "$T/script" > /dev/null 2>&1
out=$(sql "$DB" "SELECT group_concat(body, char(10)) FROM pic_entry WHERE kind = 'tool_result'")
case $out in
"web_search:"*) bad "$src" "$(printf '%s' "$out" | head -2 | tr '\n' ' ' | cut -c1-300)"; return ;;
esac
printf '%s\n' "$out" | grep -q '^1\. ' || { bad "$src" "no numbered result: $(printf '%s' "$out" | head -c 200)"; return; }
printf '%s\n' "$out" | grep -q '^ https\?://' || { bad "$src" "no result address"; return; }
if [ "$src" = stackexchange ]; then
printf '%s\n' "$out" | grep -q '^Stack Overflow results' || { bad "$src" "not labelled as Stack Overflow"; return; }
printf '%s\n' "$out" | grep -q 'https://stackoverflow.com/[aq]/[0-9]' || { bad "$src" "no question or answer link"; return; }
fi
say "$src" "ok"
}
want() { # want SOURCE: whether SOURCE was asked for (or, with none named, whether its key is set)
if [ "$#" -gt 0 ] && [ -n "$ONLY" ]; then
case " $ONLY " in *" $1 "*) return 0 ;; *) return 1 ;; esac
fi
return 0
}
ONLY="$*"
key() { eval "v=\${$1:-}"; [ -n "$v" ]; }
for src in anthropic openai xai openrouter gemini brave stackexchange chatgpt; do
want "$src" || continue
case $src in
anthropic) var=ANTHROPIC_API_KEY model=anthropic/${LIVE_MODEL_ANTHROPIC:-claude-haiku-5-5} ;;
openai) var=OPENAI_API_KEY model=openai/${LIVE_MODEL_OPENAI:-gpt-5-mini} ;;
xai) var=XAI_API_KEY model=xai/${LIVE_MODEL_XAI:-grok-4.3} ;;
openrouter) var=OPENROUTER_API_KEY model=openrouter/${LIVE_MODEL_OPENROUTER:-openai/gpt-5-mini} ;;
gemini) var=GEMINI_API_KEY model=google/${LIVE_MODEL_GEMINI:-gemini-3.1-flash-lite} ;;
brave) var=BRAVE_API_KEY model='' ;;
stackexchange) var='' model='' ;;
chatgpt) var='' model=openai/${LIVE_MODEL_CHATGPT:-gpt-5-mini} ;;
esac
if [ "$src" = chatgpt ]; then
if [ "$CHATGPT" = yes ]; then
# The stored sign-in wins over OPENAI_API_KEY (pic's order); unset it so a key cannot stand in.
(unset OPENAI_API_KEY; check chatgpt "$model" "$REALHOME"; exit "$failed") || failed=1
elif [ -n "$ONLY" ]; then
say chatgpt "skipped: needs --chatgpt (it uses your real ~/.pic sign-in)"
fi
continue
fi
if [ -n "$var" ] && ! key "$var"; then
[ -n "$ONLY" ] && say "$src" "skipped: $var is not set"
continue
fi
case $src in
brave) tool brave BRAVE_API_KEY="$BRAVE_API_KEY" ;;
stackexchange) tool stackexchange ;;
*) check "$src" "$model" ;;
esac
done
exit "$failed"