#!/bin/sh # splinter -- A2A multi-turn: a contextId keeps the conversation. # # The fake model answers with the number of user messages it received. A fresh # context must see 1, the same context again 2, another context 1. A request # without a contextId gets one generated by the server, and continuing with # that id also reaches 2. set -u cd "$(dirname "$0")/.." || exit 1 . "$(dirname "$0")/sh/port.sh" D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-a2a-ctx-XXXXXX") || exit 1 # Two servers, so two ports out of one call: asking twice returns the same # number (see free_ports). set -- $(free_ports 2) P0=$1 P1=$2 F0= F1= cleanup() { [ -n "$F0" ] && kill "$F0" 2>/dev/null [ -n "$F1" ] && kill "$F1" 2>/dev/null rm -rf "$D" } trap cleanup EXIT mkdir -p "$D/home" fails=0 check() { if "$@"; then :; else echo "FAIL $*"; fails=$((fails + 1)); fi; } send() { # send ctx=$1 if [ -n "$ctx" ]; then params="\"contextId\":\"$ctx\"," else params="" fi curl -s -H 'Content-Type: application/json' --data \ "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"SendMessage\",\"params\":{\"message\":{\"messageId\":\"m\",\"role\":\"ROLE_USER\",$params\"parts\":[{\"text\":\"$2\"}]}}}" \ "http://127.0.0.1:$P1/a2a" } answer() { printf '%s' "$1" | grep -o 'users=[0-9]*' | head -1; } context_of() { printf '%s' "$1" | python3 -c \ 'import json,sys; print(json.load(sys.stdin)["result"]["task"]["contextId"])' } python3 -I tests/fake_openai_count.py "$P0" & F0=$! check wait_listen "$P0" "$F0" HOME="$D/home" ./splinter --no-config --endpoint \ "http://127.0.0.1:$P0/v1/chat/completions" --model m \ --a2a-server-listen 127.0.0.1 --a2a-listen-port "$P1" \ >"$D/out" 2>"$D/err" & F1=$! check wait_listen "$P1" "$F1" r=$(send "ctx-multi" "eins") check test "$(answer "$r")" = "users=1" r=$(send "ctx-multi" "zwei") check test "$(answer "$r")" = "users=2" r=$(send "ctx-other" "eins") check test "$(answer "$r")" = "users=1" # no contextId: the server makes one and returns it r=$(send "" "eins") check test "$(answer "$r")" = "users=1" ctx=$(context_of "$r") check test -n "$ctx" r=$(send "$ctx" "zwei") check test "$(answer "$r")" = "users=2" if [ "$fails" -eq 0 ]; then echo "test_a2a_context: ok"; exit 0; fi exit 1