Endpoint lifecycle
Read this page when writing a WebSocket or SSE adapter. Every long-lived connection needs the same logic: connect, reconnect with exponential backoff after a disconnect, heartbeat checks, and a clean stop. createEndpointLifecycle from zhin.js/adapter packages that as a ready-made base; napcat and other official adapters use it.
The classic bugs when hand-writing it: a late close event from an old socket kills the new connection; stop() racing a connect causes an unhandled rejection; a backoff timer is never cleared. The base avoids all of them — stop writing #started / #reconnectTimer flags by hand.
Minimal usage
import { createEndpointLifecycle } from 'zhin.js/adapter';
const lifecycle = createEndpointLifecycle({ name: config.name });
// Connect: connectFn resolves when the connection opens, rejects on failure or close before open
await lifecycle.start(async (handle) => {
const ws = new WebSocket(url, { headers });
// Register a force-close function: stop() and the heartbeat watchdog both call it
handle.onForceClose(() => { try { ws.close(); } catch { /* ignore */ } });
await new Promise<void>((resolve, reject) => {
let settled = false;
ws.on('open', () => {
settled = true;
lifecycle.startHeartbeat(() => ws.ping(), config.heartbeat_interval);
resolve();
});
ws.on('close', (code, reason) => {
// Tell the base the peer disconnected; only connections that were once open reconnect
handle.notifyClosed(new Error(`WS closed: ${code} ${reason}`));
if (!settled) { settled = true; reject(new Error(`WS closed: ${code}`)); }
});
ws.on('error', (err) => {
if (!settled) { settled = true; reject(err); }
});
});
});
// Stop: clears every timer, force-closes the connection, never triggers a reconnect
await lifecycle.stop();Two behaviors to remember:
- A failed first connection isn't reconnected by the base.
start()throws the error; only a connection that was once open reconnects after a disconnect. The framework takes over an error thrown bystart(): the account enters "start failed" and is retried in the background with backoff on a fresh Endpoint (except configuration errors and interactive logins), without failing instance startup, so the adapter needs no retry code of its own; see account logins do not hold back readiness.start()may wait until login completes; the instance does not wait for it to become ready. stop()is an intentional stop: safe to call repeatedly, never triggers a reconnect; if called while connecting,start()resolves quietly (stopping isn't a failure).
The specification’s stop hook calls private platform cleanup: stop the transport lifecycle, then release protocol resources:
// plugins/adapters/napcat/src/ws-endpoint.ts (excerpt)
async #stop(): Promise<void> {
this.#open = false;
await this.#lifecycle.stop();
rejectAllPending(this.pendingActions);
this.#inboundDeduper.clear();
if (this.transportSocket) {
try { this.transportSocket.close(); } catch { /* ignore */ }
this.transportSocket = undefined;
}
}Configure reconnection and heartbeat
const lifecycle = createEndpointLifecycle({
name: config.name, // logging only
reconnect: {
initialIntervalMs: 5_000, // first retry delay, default 5000
multiplier: 2, // backoff factor, default 2 (1 = fixed interval)
maxIntervalMs: 60_000, // delay cap, default 60000
jitterMs: 250, // max random jitter, default 250
maxAttempts: Infinity, // consecutive failures before entering closed
},
heartbeat: {
intervalMs: 30_000, // default interval for startHeartbeat; <=0 disables
watchdogMisses: 0, // missed rounds tolerated, default 0 = off
},
});reconnect: falsedisables reconnection; a disconnect goes straight toclosed.- Actual delay =
min(initial × multiplier^attempt, max) + random(0, jitter), reset after a successful reconnect. The first disconnect logs WARN, later retries DEBUG, to avoid log spam. - In tests, pass
random: () => 0for a deterministic backoff sequence.
Heartbeat watchdog
lifecycle.startHeartbeat(beat, intervalMs?); // start (calling again clears the previous timer)
lifecycle.stopHeartbeat(); // stop manually (called automatically on close, stop, watchdog)
lifecycle.notifyHeartbeatAck(); // "feed the dog": call on any reply from the peerWith watchdogMisses: N, if N consecutive heartbeats get no reply at all, the base calls your force-close function; the socket's close event then fires notifyClosed and normal backoff reconnection follows. So feed the watchdog in both message and pong:
ws.on('pong', () => this.#lifecycle.notifyHeartbeatAck());
ws.on('message', (data) => {
this.#lifecycle.notifyHeartbeatAck(); // any traffic means it's alive
handleMessage(data);
});Several accounts on one adapter
An adapter is declared once (adapters/napcat/index.ts → defineAdapter), but config can expand it into several account connections (Endpoints):
configVersion: 2
# examples/full-bot/zhin.config.yml (excerpt)
plugins:
napcat: "@zhin.js/adapter-napcat"
napcat:
connection: ws # shared config for every account
endpoints:
- id: full-bot-napcat # per-account config
url: ${ONEBOT11_WS_URL}
access_token: ${ONEBOT11_ACCESS_TOKEN}- With a non-empty
endpoints, each entry creates one account: the shared config (minus theendpointskey) is shallow-merged with the entry, andidis forced in. Withendpointsempty or missing, one account is created from the shared config. idmust be a non-empty string without~or\0; duplicate or invalid ids make the new config fail. The expanded account id is<capabilityId>~<id>.- The owning Adapter’s
createEndpoint(config, execution)receives merged single-account config.executionprovides canonicalId, instanceId and the incarnation cancellation signal. One Adapter manages several accounts; each Endpoint is one actual Client.
The expansion lives in expandEndpointConfigs in packages/im/adapter/src/adapter-index.ts.
Endpoint lifecycle hooks
A platform Endpoint extends a real SDK or substantive protocol Client, implements Endpoint, and declares these hooks through createEndpointSpecification(this, hooks). There is no public Endpoint base class; framework hooks do not replace native SDK methods of the same name.
| Hook | When |
|---|---|
start(signal) | Allocate connection resources and honor incarnation cancellation |
open() | Open local ingress; candidate-generation events still require commit admission |
close() | Close new framework admission and detach listeners; the framework drains accepted callbacks and normalized continuations |
stop() | After retirement, cancellation and lease draining, release connection resources; cleanup must be repeatable |
send(request) | Send while holding the actual incarnation lease |
Wrap the complete asynchronous SDK listener in events.run(async () => { ... }), including decoding, native events and normalized messages, rather than tracking only the final emit. close drains accepted callbacks; stop cancellation must reach actual IO. An inbound callback must not wait for its own account to close; the owning Adapter controls lifecycle.
Preserve the actual public SDK payload and separately produce canonical messages. A native event is not automatically a raw network packet: do not claim exposure of wire bytes that the SDK never provides. Advance a cursor or ACK only after the Runtime accepted the event, but do not wait for its dispatch: a message's dispatch may be a whole Agent turn, and that turn may be waiting for this platform's next inbound (a confirm button, an ask_user answer). Callers that fetch the next inbound (a poll loop, an SDK ACK, a webhook response) use events.admit(...) (the same receipt as run, settled with the callback alone) and, inside it, events.handoff(name, payload) / events.handoffMessage(input): it resolves once the Runtime accepted (recorded) the event and rejects when it did not, in which case the cursor must not advance; the later dispatch outcome is on the returned dispatched. events.run and events.emitAccepted still wait for the dispatch, so never await them before taking the next inbound.
NapCatWsEndpoint binds specification hooks to private #start/#openEndpoint/#close/#stop methods, preserving SDK APIs. The same Client uses createEndpointLifecycle for heartbeat and reconnect, without a second live-account state store.
Resolving quotes, merged forwards and media
If the platform can look up content by native id, the specification should provide a single content: EndpointContentPort — don't stuff $getMsg, quote metadata or pre-expanded forward text into messages:
interface EndpointContentPort {
resolve(
reference: ConversationReference, // message | forward | media
context: {
signal: AbortSignal;
maxDepth: number;
maxEntries: number;
maxChars: number;
},
): Promise<ConversationResolution>;
}- The result must be one of
resolved | not_found | unsupported | forbidden | expired | failed. Without platform support returnunsupported— never empty or guessed content. resolve()must honorsignaland the depth, entry and character budgets; Core holds the current version's snapshot lease for the whole async call.- Authors inside messages and forwards are written as neutral
actordata and must never be mapped to model roles. - Media may return a stable URL, validated base64 / path, or an explicit failure; never expose token-bearing download URLs to the Agent. A returned path is bound by the inbound media directories below as well.
Inbound media directories
An inbound message is remote input: a local path in it may come from a platform field, parsed text or a forwarded node. The Agent reads only one kind of local path: a file whose real path (symbolic links resolved) lies inside an inbound media directory declared by the Endpoint that produced the message, and which also passes the file policy's secret checks (.zhin/, .env*, keys, databases). Every other path is not read; the model sees a placeholder saying the message carries a file that cannot be read (inbound_path_not_owned / inbound_path_sensitive), and the turn continues.
An adapter writes the files it downloads into a dedicated directory, declares it on the specification and emits media: { kind: 'path' } explicitly:
createEndpointSpecification(this, {
// ...
inboundMedia: { roots: () => [this.#state.mediaDirectory('inbound')] },
});roots()is read when each turn is built, so it may follow configuration (an attachment download directory, or an empty list while attachments are off).- Declare only directories the Endpoint itself writes; never the project root,
data/or a home directory. - A platform
data.fileis no longer guessed to be a local path (values starting with/,./,~/are opaque platform refs,kind: 'file'); writedata.pathormedia.kind: 'path'for a real local file.
Protocol server origins
Inbound media and voice addresses connect only to the default port (https 443, and 80 when a platform voice link may use http); any other port is refused, so a URL cannot make the instance probe ports. A protocol server (Satori, OneBot, NapCat, Milky, …) often hands out resource addresses on its own port (http://1.2.3.4:5140/v1/proxy/...). An adapter declares the protocol server it is configured to talk to on the specification, and that one origin (same scheme, host and port) keeps its port:
createEndpointSpecification(this, {
// ...
inboundMedia: { origins: () => [options.config.baseUrl] },
});http(s)://andws(s)://are accepted (aws(s)://address counts ashttp(s)://on the same host and port); the path is ignored.- Declare only the protocol server from the Endpoint's configuration, never an address taken from a message; a reverse connection (the protocol server connects in) has no configured address and declares none.
origins()lifts the port rule only, and only for a media URL that points at that origin directly: a redirect into it from elsewhere, or from it to another port, is held to the default port.- Private and loopback hosts are still refused: a protocol server on the same machine or private network as the instance hands out addresses that stay unusable, whatever the port. Image downloads are HTTPS only; platform voice links may use http.
Going deeper
State machine
startedistrueexactly when the state isconnecting/open/reconnecting.- Every connection attempt bumps an internal version, and
notifyClosed/onForceCloseon stale handles are ignored — late events from an old socket can't affect the new connection.
Example: napcat's ws-endpoint
plugins/adapters/napcat/src/ws-endpoint.ts is the canonical use of the base, in four parts:
// Excerpt: private protocol methods remain in the actual platform implementation.
export class NapCatWsEndpoint extends NapcatClient implements Endpoint {
readonly [endpointSpec]: EndpointSpecification;
readonly #lifecycle: EndpointLifecycle;
constructor(options: NapCatWsEndpointOptions) {
super(options.config, undefined, options.signal);
this.#lifecycle = createEndpointLifecycle({
name: options.config.id,
reconnect: {
initialIntervalMs: options.config.reconnect_interval,
multiplier: 1,
maxIntervalMs: Number.MAX_SAFE_INTEGER,
jitterMs: 0,
},
});
this[endpointSpec] = createEndpointSpecification(this, {
start: () => this.#start(), open: () => this.#openEndpoint(),
close: () => this.#close(), stop: () => this.#stop(),
send: request => this.#send(request),
});
}
}What the base removes for you:
| Hand-written | With the base |
|---|---|
#started / #stopping / opened flags | lifecycle.state / lifecycle.started |
#reconnectTimer + a hand-rolled setTimeout chain | Built-in backoff loop with interruptible waits; stop() wakes it immediately |
#heartbeatTimer + a missed-reply counter | startHeartbeat + watchdogMisses + notifyHeartbeatAck |
| A late close from an old socket killing the new one | Stale handles expire automatically |
stop() racing a connect into an unhandled rejection | Racing waits are settled together and resolve silently |
The adapter keeps only protocol logic: the Client's API transport, rejecting pending requests (rejectAllPending) and clearing the inbound deduper (deduper.clear()). The Endpoint is the actual Client instance. Framework hooks and ingress live in its symbol namespace; the Adapter owns account lifecycle.
Next steps
- Adapter authoring: the basic
defineAdapterform. - Message flow: the full inbound and outbound path.