The scripts p2-run-stream.sh, p2-run-stream.py and p2-run-stream.ts are in full under Complete script.
Before you start
Set CONTEXT_BASE_URL, CONTEXT_API_KEY and AGENT_ID as in the Quickstart.
Client setup used by every step
#!/usr/bin/env bashset -euo pipefail: "${CONTEXT_BASE_URL:?Set CONTEXT_BASE_URL, for example https://api.context.ai}": "${CONTEXT_API_KEY:?Set CONTEXT_API_KEY to a personal Task API key}": "${AGENT_ID:?Set AGENT_ID to an id from GET /v1/agents}"api() { local path=$1; shift curl -sS --fail-with-body "$CONTEXT_BASE_URL/v1$path" -H "x-api-key: $CONTEXT_API_KEY" "$@"}fail() { echo "error: $*" >&2; exit 1; }# Opens the session's event stream; exec makes the background job's pid the curl process itself.stream() { exec curl -sS --fail-with-body -N "$CONTEXT_BASE_URL/v1/sessions/$1/events/stream" \ -H "x-api-key: $CONTEXT_API_KEY" "${@:2}"}# The JSON of every event received so far on a stream saved to a file. Ping frames carry no id and are skipped.events_in() { sed -n 's/^data: //p' "$1" | jq -c 'select(.id)'; }# Waits until the stream file holds a session.status_idle that ends the turn (not requires_action).wait_for_end_of_turn() { for _ in $(seq 1 300); do grep -q '^event: error' "$1" && fail "the stream ended with an error: $(grep -A2 '^event: error' "$1" | sed -n 's/^data: //p')" events_in "$1" | jq -se 'any(.[]; .type == "session.status_idle" and .stop_reason.type != "requires_action")' >/dev/null && return 0 sleep 1 done fail "the turn did not end within 300 seconds"}WORK=$(mktemp -d)
Create a session
A new session is idle until you send it a message.
stream "$SESSION_ID" > "$WORK/stream.txt" &STREAM_PID=$!sleep 1kill -0 "$STREAM_PID" 2>/dev/null || fail "the stream did not open: $(cat "$WORK/stream.txt")"echo "step 2: stream open"
Sample output:
Code
step 2: stream open
Send a message
Sending returns one receipt for each event you send, with a sevt_receipt_ id. The stored event, with its own id, arrives on the stream. Don’t use a receipt id as Last-Event-ID. To resume, use the id of a stored event from the stream.
step 4: 6 events, last sevt_4_1, agent said: Tokyo is the most populous metropolitan area in the world.
Resume the stream from an event id
If the connection drops, reopen the stream with a Last-Event-ID header set to the last event id you received. The server replays every event after it.
# After a dropped connection, send the id of the last event you received. Here we resume from the first one.stream "$SESSION_ID" -H "Last-Event-ID: $FIRST_ID" > "$WORK/resumed.txt" &RESUME_PID=$!wait_for_end_of_turn "$WORK/resumed.txt"kill "$RESUME_PID"[[ $(events_in "$WORK/resumed.txt" | jq -rs '.[0].id') != "$FIRST_ID" ]] || fail "the resumed stream repeated $FIRST_ID"echo "step 5: resumed after $FIRST_ID, replayed $(events_in "$WORK/resumed.txt" | wc -l) events up to $(events_in "$WORK/resumed.txt" | jq -rs '.[-1].id')"
Sample output:
Code
step 5: resumed after sevt_1_0, replayed 5 events up to sevt_4_1
List the session’s events
The events endpoint returns the stored history with the same ids the stream carried, oldest first. The script prints how many events it listed and which event types they include.