AWP

API

The TypeScript SDK's Peer, its options and its events.

import { Peer, type Event } from "@agentwireprotocol/sdk";

Options

new Peer({ dir, name, about, trust, pingInterval, blobLimit, caps, tailcat, log, trace });
OptionMeaning
dirWhere everything durable lives. Omit it for an ephemeral Peer: a fresh identity, state in memory, removed on close.
name, aboutSent in hello. name defaults to awp-ts@<hostname>.
trustKeys whose grants are honored as this Peer’s own.
pingIntervalMilliseconds idle before a ping; 30 000 by default, 0 disables pings.
blobLimitThe largest blob accepted or sent, in bytes; 50 MiB.
capsThe hello caps; chat, blob, grant, introduce by default.
tailcatThe tailcat CLI to run for tailcat addresses.
log, traceThe log, and every line sent ("out") and received ("in").

Peer

MethodDoes
listen(addr)Accepts connections on tcp:HOST:PORT, unix:/path or "tailcat"; resolves with the address to share. addresses lists them.
connect(addr, timeoutMs = 30000)Dials and resolves with the peer’s key after the handshake; keeps dialing with backoff afterwards. null waits forever.
send(to, text, { thread, subject, replyTo, parts, files })Queues a msg; returns { id, thread, to, newThread }. to may be null for the default peer.
setState(to, thread, state, note)This side’s state on a thread.
waitAck(id, timeoutMs)Resolves once the peer acks the message.
grant(to, caps, ttl = 3600)Mints, records and sends a grant; returns it. caps(to) is the set the peer holds on this Peer; grants() lists issued and held grants with a hash each; revoke(hash) stops honoring one.
introduce(to, peer, caps = [], ttl = 3600, thread?)Hands to the key and address of peer, with a grant bound to peer.
bye(to, reason = "done")Closes gracefully and parks the peer.
connected(to), listPeers(), threads(to?)What the Peer knows.
events(), nextEvent(timeoutMs)Every event from now on, as an async iterator; or the next one.
close()Stops everything without a bye.
key, name, dir, addressesThe identity, the hello name, the directory, the listen addresses.

rawHook receives every received line as an object, for drivers that want the raw protocol.

Events

A discriminated union on type; every event has peer, the key of the peer it is about.

typeFieldsWhen
connectedname, about, caps, outboundA handshake completed.
disconnectedreasonThe connection ended.
messageid, thread, parts, subject, replyTo, requests, textA msg, stored and acked before it is reported. requests lists capability requests in the data parts as { part, mime, cap, allowed }.
stateid, thread, state, noteThe peer set its state.
ackedidThe peer acked a msg or state of ours.
blobref, path, size, sha256, thread, name, mimeA file arrived in full.
grantissuer, caps, expires, grantA grant to this Peer, now held.
introducedkey, name, address, thread, grantAnother peer’s key and address to connect to.
errorcode, detail, replyTo, refAn err the peer sent.
byereasonThe peer closed gracefully.
local-errordetailSomething went wrong locally; peer is empty.

Lower level

Identity, mintGrant, verifyGrant, canonical, formatKey, parseKey and parseAddr are exported for anything below the Peer. The wire namespace holds the message types generated from the schema: wire.Hello, wire.Msg, wire.Part, wire.Grant and the rest.