Live tail
client.events.tail(input, handlers) reads matching events in sequence order over Server-Sent Events. Use it for a view that stays current; use events.query or iterate for stored history. It starts with new events, or catches up from a sequence when you pass after.
New events
Create a server client with your project’s secret key. The tail takes the same filter as events.query, without its time window, sort, or paging options:
import { createClient } from '@agentpost-tools/eventwolf-sdk';
const client = createClient({ token: process.env.AGENTPOST_SECRET_KEY });
const subscription = client.events.tail(
{ filter: { types: ['request.*'] } },
{
onEvent(record) {
console.log(record.sequence, record.event);
},
onHeartbeat(through) {
console.log('caught up through', through);
},
onError(error, { stopped }) {
console.error(error);
if (stopped) console.error('the tail needs a new credential or request');
},
},
);
Only onEvent is required. onHeartbeat receives the sequence through which every matching event has been sent, even when the filter matches nothing. Call subscription.close() when the view ends; no callback runs after it.
Resume after a sequence
Pass after to receive matching events after a known sequence, then keep reading new ones:
const resumed = client.events.tail(
{ filter: { types: ['request.*'] }, after: 42 },
{ onEvent: (record) => console.log(record.sequence, record.event) },
);
subscription.lastSequence is the last event or heartbeat’s sequence, or the initial after before a message arrives. Keep it to resume later with the same project and filter. A heartbeat is safe to resume from because every matching event through it has already been sent. Without after, the value is undefined until the first event or heartbeat.
The tail reconnects by itself after a dropped connection, an HTTP 429, or a server fault. Once it has a sequence, it sends it as Last-Event-ID and catches up on retained events in order. A tail without a sequence starts at the current head on reconnect. Waits grow with consecutive failures. Set input.maxRetryMs to cap the wait in milliseconds. The default is 30_000. Imported history is left out, as Events explains.
Workspace keys
For a tool that reads several projects, create one client per project with a workspace key:
const workspaceClient = createClient({
token: process.env.AGENTPOST_WORKSPACE_KEY,
project: 'prj_01JE2T7WQ8M4X6B1C9D3F5G7H0',
});
const workspaceTail = workspaceClient.events.tail(
{ filter: { types: ['request.*'] } },
{ onEvent: (record) => console.log(record.event) },
);
The client sends x-agentpost-project on every connection, including reconnects. Sequences belong to one project; keep a separate resume sequence for each.
Errors
onError(error, { stopped: false }) reports a fault the tail will retry. A refusal, before the stream opens or in its final error message, ends it with stopped: true. Create a new tail after correcting the credential or request. An expired or revoked workspace key is a refusal.
When onEvent throws, the tail reports the error and reconnects from the previous sequence. Set after when you need to resume even if the first callback fails before the tail has a sequence.
timeoutMs applies to JSON requests, so it never cuts off a live tail. The stream reference describes the wire format.