#!/bin/sh
# The provider's own web search, against the fake server: the request declares the provider's
# server tool where pic's web_search function would be, the reply's search and citations are kept
# for replay and shown as text without control characters, and the conversation's permission
# mode, the webSearch setting, offline and --exclude-tools decide whether it is declared at all.
set -eu
PIC=${PIC:?set PIC to the pic binary}
SERVER=${SERVER:?set SERVER to the fake_server binary}
N=$(cd "$(dirname "$0")/native-streams" && pwd)
T=$(cd "$(mktemp -d "${TMPDIR:-/tmp}/pic-websearchnative.XXXXXX")" && 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
mkdir "$T/home"
export HOME="$T/home" PIC_PI_EXPORT=0 PIC_MODEL_CATALOG=offline
KEY="native-test-key-$$"
export ANTHROPIC_API_KEY=$KEY OPENAI_API_KEY=$KEY XAI_API_KEY=$KEY OPENROUTER_API_KEY=$KEY GEMINI_API_KEY=$KEY
"$PIC" settings loops.tool false > /dev/null
n=0
fresh() { n=$((n + 1)); DB="$T/$n.sqlite"; WS="$T/ws$n"; mkdir "$WS"; }
pic() { "$PIC" --db "$DB" --workspace "$WS" "$@"; }
serve() { # serve SPEC...: a fresh fake server; URL is its address
[ -n "$spid" ] && kill "$spid" 2>/dev/null && wait "$spid" 2>/dev/null || :
rm -rf "$T/log" "$T/port"
mkdir "$T/log"
"$SERVER" "$T/port" "$T/log" 60 "$@" &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 100 ] || fail "server"; sleep 0.05; done
URL="http://127.0.0.1:$(cat "$T/port")/v1"
}
# ask MODEL SPEC...: a conversation of one question to MODEL against a fake serving SPEC; the
# requests are in $T/log. Flags before the question go in $FLAGS, settings through the journal.
ask() {
model=$1
shift
fresh
serve "$@"
# shellcheck disable=SC2086
pic ${FLAGS:-} submit job 'what is C' > /dev/null
# shellcheck disable=SC2086
pic ${FLAGS:-} run --model "$model" --api-url "$URL${API_PATH:-/messages}" > "$T/out" 2> "$T/err" || { cat "$T/err" >&2; fail "$model: run failed"; }
}
declared() { grep -q "$1" "$T/log/1.req"; }
# Anthropic: the server tool replaces the web_search function, in the request that asks.
ask anthropic/test-model "sse:$N/anthropic.sse"
grep -q '"type":"web_search_20250305"' "$T/log/1.req" || fail "anthropic: no server tool: $(head -c 600 "$T/log/1.req")"
! grep -q '"name":"web_search","description"' "$T/log/1.req" || fail "anthropic: the function was declared too"
grep -q '"name":"read"' "$T/log/1.req" || fail "anthropic: the other tools went missing"
printf 'PASS: anthropic declares its own search\n'
# The reply: the search and the citation are in the journal verbatim (for replay) and the
# transcript shows them as text without control characters.
D=$(pic dump)
NATIVE=$("$(dirname "$SERVER")/sqlite3" "$DB" "SELECT native FROM pic_entry WHERE native IS NOT NULL")
printf '%s\n' "$NATIVE" | grep -qF '"type":"server_tool_use"' || fail "anthropic: the search was not kept"
printf '%s\n' "$NATIVE" | grep -qF '"encrypted_content":"ENC1"' || fail "anthropic: the results were not kept"
printf '%s\n' "$NATIVE" | grep -qF '"citations":[{"type":"web_search_result_location"' || fail "anthropic: the citation was not kept"
printf '%s\n' "$D" | grep -qF 'C is a language.' || fail "anthropic: no text"
printf '%s\n' "$D" | grep -qF 'Searched the web for: c language' || fail "anthropic: no search line in the transcript"
printf '%s\n' "$D" | grep -qF 'https://example.org/c' || fail "anthropic: no source in the transcript"
! printf '%s\n' "$D" | LC_ALL=C grep -q "$(printf '[\001-\010\013-\037\177]')" || fail "anthropic: a control byte reached the transcript"
! printf '%s\n' "$D" | LC_ALL=C grep -q "$(printf '\302[\200-\237]')" || fail "anthropic: a C1 control reached the transcript"
printf 'PASS: anthropic search and citations are kept and shown\n'
# The next request sends the search back verbatim, so the provider can continue from it.
ask anthropic/test-model "sse:$N/anthropic.sse" "sse:$(dirname "$N")/streams/text.sse"
pic submit job2 'and more' --session latest > /dev/null
pic run --model anthropic/test-model --api-url "$URL/messages" > /dev/null 2>&1 || fail "anthropic: the second request failed"
grep -q '"type":"server_tool_use","id":"srvtoolu_01","name":"web_search","input":{"query":"c language"}' "$T/log/2.req" || fail "anthropic: the search was not replayed: $(grep -o "server_tool_use.\{0,120\}" "$T/log/2.req" | head -2)"
grep -q '"encrypted_content":"ENC1"' "$T/log/2.req" || fail "anthropic: the results were not replayed"
grep -q '"citations":\[{"type":"web_search_result_location"' "$T/log/2.req" || fail "anthropic: the citation was not replayed"
printf 'PASS: anthropic search is replayed from the journal, not searched again\n'
# When the provider's search is not declared: the function is, so pic gates it as it does any call.
for c in "--permission-mode ask-all" "--permission-mode read-only"; do
FLAGS=$c ask anthropic/test-model "sse:$N/anthropic.sse"
! declared '"type":"web_search_20250305"' || fail "$c: the provider's search was declared"
declared '"name":"web_search","description"' || fail "$c: the function was not declared"
done
FLAGS="--permission-mode ask-edits" ask anthropic/test-model "sse:$N/anthropic.sse"
declared '"type":"web_search_20250305"' || fail "ask-edits: not declared"
printf 'PASS: modes that ask or refuse changes get the function, which pic gates\n'
"$PIC" settings webSearch brave > /dev/null
ask anthropic/test-model "sse:$N/anthropic.sse"
! declared 'web_search' || fail "webSearch brave with no Brave key: offered"
export BRAVE_API_KEY=brave-test-key
ask anthropic/test-model "sse:$N/anthropic.sse"
! declared '"type":"web_search_20250305"' || fail "webSearch brave: declared"
declared '"name":"web_search","description"' || fail "webSearch brave: no function"
unset BRAVE_API_KEY
"$PIC" settings --unset webSearch > /dev/null
"$PIC" settings offline true > /dev/null
ask anthropic/test-model "sse:$N/anthropic.sse"
! declared '"type":"web_search_20250305"' || fail "offline: declared"
"$PIC" settings --unset offline > /dev/null
FLAGS="--exclude-tools web_search" ask anthropic/test-model "sse:$N/anthropic.sse"
! declared 'web_search' || fail "--exclude-tools: still declared"
printf 'PASS: webSearch brave, offline and --exclude-tools turn the provider search off\n'