The server keeps up to 16 conversations, keyed by contextId and evicted LRU-style. A message with a known id continues it; without one the server generates an id and returns it in the task, so callers that send no context at all do not share a history. The session log still records every turn. test_a2a_context.sh uses a fake OpenAI endpoint that answers with the number of user messages it received: fresh=1, same context=2, another context=1, and continuing with the generated id reaches 2. README, PLUGINS.md, TODO, ROADMAP and the site follow.
76 lines
2.2 KiB
Bash
Executable File
76 lines
2.2 KiB
Bash
Executable File
#!/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
|
|
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-a2a-ctx-XXXXXX") || exit 1
|
|
P0=$((20000 + ($$ % 20000)))
|
|
P1=$((P0 + 1))
|
|
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; }
|
|
wait_port() {
|
|
for i in $(seq 1 50); do
|
|
curl -s -o /dev/null "http://127.0.0.1:$1/" && return 0
|
|
sleep 0.2
|
|
done
|
|
return 1
|
|
}
|
|
send() { # send <context-json-or-empty> <text>
|
|
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_port "$P0"
|
|
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_port "$P1"
|
|
|
|
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
|