Skip to content
pi-c Artifact [13dc298a94]
Login

Artifact [13dc298a94]

Artifact 13dc298a94a91a0a7f635d6c77f3fdc1b445c5e352ace92a00924c9df7223755:


#!/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 go in $FLAGS and the request path in $API_PATH; sh keeps a variable
# set in front of a function call, so every ask resets them.
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"; }
    FLAGS= API_PATH=
}
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'

# OpenAI and xAI (Responses): {"type":"web_search"} beside the function tools; the search call
# and its citations are kept, shown as text, and the next request leaves the call item out (it
# holds nothing the answer's own text does not).
for model in openai/test-model xai/test-model; do
    API_PATH=/responses ask "$model" "sse:$N/responses.sse" "sse:$(dirname "$N")/responses-streams/reasoning_text.sse"
    grep -q '"tools":\[{"type":"function","name":"read"' "$T/log/1.req" || fail "$model: the function tools went missing"
    grep -q '{"type":"web_search"}' "$T/log/1.req" || fail "$model: no server tool: $(grep -o "\"tools\".\{0,200\}" "$T/log/1.req" | head -c 600)"
    ! grep -q '"name":"web_search"' "$T/log/1.req" || fail "$model: the function was declared too"
    D=$(pic dump)
    printf '%s\n' "$D" | grep -qF 'Searched the web for: c language' || fail "$model: no search line in the transcript"
    printf '%s\n' "$D" | grep -qF 'C is a language.' || fail "$model: no answer text"
    printf '%s\n' "$D" | grep -qF 'https://example.net/iso' || fail "$model: no source in the transcript"
    ! printf '%s\n' "$D" | LC_ALL=C grep -q "$(printf '[\001-\010\013-\037\177]')" || fail "$model: a control byte reached the transcript"
    ! printf '%s\n' "$D" | LC_ALL=C grep -q "$(printf '\302[\200-\237]')" || fail "$model: a C1 control reached the transcript"
    NATIVE=$("$(dirname "$SERVER")/sqlite3" "$DB" "SELECT native FROM pic_entry WHERE native IS NOT NULL")
    printf '%s\n' "$NATIVE" | grep -qF '"type":"web_search_call"' || fail "$model: the search call was not kept"
    printf '%s\n' "$NATIVE" | grep -qF '"type":"url_citation"' || fail "$model: the citations were not kept"
    pic submit job2 'and more' --session latest > /dev/null
    pic run --model "$model" --api-url "$URL/responses" > /dev/null 2>&1 || fail "$model: the second request failed"
    grep -q '"text":"C is a language."' "$T/log/2.req" || fail "$model: the answer was not replayed"
    ! grep -q '"type":"web_search_call"' "$T/log/2.req" || fail "$model: the search call was sent back"
done
printf 'PASS: responses: openai and xai search natively, kept and replayed\n'


# OpenRouter (Chat Completions): its server tool in the tools array, and the citations the stream
# brings are shown as a source list. The assistant message goes back as it came (no annotations).
API_PATH=/chat/completions ask openrouter/test/model "sse:$N/openrouter.sse" "sse:$(dirname "$N")/chat-streams/text.sse"
grep -q '{"type":"openrouter:web_search"}' "$T/log/1.req" || fail "openrouter: no server tool: $(grep -o '"tools".\{0,200\}' "$T/log/1.req" | head -c 500)"
grep -q '"tools":\[{"type":"openrouter:web_search"},{"type":"function"' "$T/log/1.req" || fail "openrouter: the server tool is not first"
! grep -q '"name":"web_search"' "$T/log/1.req" || fail "openrouter: the function was declared too"
D=$(pic dump)
printf '%s\n' "$D" | grep -qF 'C is a language.' || fail "openrouter: no answer text"
printf '%s\n' "$D" | grep -qF 'Sources:' || fail "openrouter: no source list"
printf '%s\n' "$D" | grep -qF 'https://example.net/iso' || fail "openrouter: no source in the transcript"
! printf '%s\n' "$D" | LC_ALL=C grep -q "$(printf '[\001-\010\013-\037\177]')" || fail "openrouter: a control byte reached the transcript"
! printf '%s\n' "$D" | LC_ALL=C grep -q "$(printf '\302[\200-\237]')" || fail "openrouter: a C1 control reached the transcript"
pic submit job2 'and more' --session latest > /dev/null
pic run --model openrouter/test/model --api-url "$URL/chat/completions" > /dev/null 2>&1 || fail "openrouter: the second request failed"
grep -q '"role":"assistant","content":"C is a language."' "$T/log/2.req" || fail "openrouter: the answer was not replayed as it came"
printf 'PASS: openrouter searches natively, sources shown\n'