@sharpps/agent
v1.2.0
Published
General-purpose agent channel: an outbound WebSocket from a person's machine to a server, with plugins on both sides
Readme
@sharpps/agent
General-purpose agent channel: one outbound connection from a machine to a server, with plugins on both sides.
Commands: sharpps-agent install|list|login|run|serve (see sharpps-agent --help).
How it works
agent (client) ══ one WebSocket, opened by the agent, Clerk token ══▶ listener (server)
client side of each plugin server side of each pluginThe agent always dials out; the server never connects to it. On that one connection, work goes both ways:
| | Who starts it | Declared by | Answered with |
|---|---|---|---|
| Call | server → agent | client plugin capabilities (and the server plugin that uses it) | result / error, events while working |
| Request | agent → server | server plugin requests | response / response-error, request-events |
Calls and requests can carry streams: binary or text frames for continuous data (a browser connection). A stream belongs to one call or one request; a stream id is one two-way channel inside it.
Connecting
- WebSocket upgrade with
Authorization: Bearer <token>; the listener checks it (fails closed) and rechecks it every 5 minutes. hello(protocol 2, machine, capabilities, plugins, features). The first server plugin withagentFornames the agent (default<user>:<machine id>); every plugin'sonAgentHellomay refuse it.welcome(agent id, name, features) orreject. One live connection per agent id: a newer one replaces the older.
Who may connect: people, through the server's authenticator (Clerk device login in sharpps-agent-service), and service clients
that have no person (a web app on a serverless platform), through a server plugin's authenticate(token) (1.2.0). Plugins are asked
first and return null for tokens they do not issue; the identity a plugin returns must use a userId no person can have. The recheck
every 5 minutes applies to service tokens too, so they must outlive it; the client reconnects with a fresh token when one expires.
Features are optional parts of the protocol both sides announce. requests is the only one: an agent sends requests only when the
server announced it, and the server takes requests only from agents that announced it. Older agents and servers keep working.
Requests: what an agent may ask the server
A server plugin declares each request it accepts, and who may send it (allow, required: nothing is open by default). allow is
checked before the handler; refused agents get forbidden. The handler then gets the agent that sent it, as accepted at hello, and
decides the rest with that; never trust identity fields in the input. Capabilities an agent offers are self-declared: use them only to
sort agents, and prove anything that matters (who signed in, a key the agent holds).
// server side
const plugin: Plugin = {
name: "replayr-browser",
requests: {
"browser.connect": {
allow: (agent) => agent.capabilities.includes("runner.status"), // who may send it; others get "forbidden"
concurrency: 2, // open at once per agent (default 4)
timeoutMs: 0, // 0 = until the agent cancels or disconnects (default: the call timeout)
async handler(input, ctx) {
if (!mayUse(ctx.agent.userId, input)) throw new Error("not allowed"); // goes back as response-error "failed"
const call = host.startCall(target, "browser.open", {});
host.relay(ctx, target, call.callId); // request streams <-> call streams, same ids, both ways
ctx.signal.addEventListener("abort", () => host.cancel(call.callId));
return call.result;
},
},
},
};// client side: start() gets a context for requests
async start(values, ctx) {
const answer = await ctx.request("area.action", input, { timeoutMs: 10_000 });
const r = ctx.openRequest("browser.connect", { runId: 42 }); // with streams
r.inbound((streamId, bytes, kind) => …);
r.stream(1, bytes); r.streamEnd(1); r.cancel();
}Rules the host enforces: only declared names; only agents allow accepts; one plugin per name; open requests per agent and name are limited; a request aborts when
the agent cancels it, disconnects, it times out, or the listener stops. Error codes: unknown-request, not-enabled, busy, timeout,
duplicate, failed, forbidden; on the client also not-connected, disconnected, cancelled.
Control port
--control-port serves /plugins/<name>/… to handleHttp on 127.0.0.1, for apps on the same server, with one shared token
(SHARPPS_AGENT_CONTROL_TOKEN). It identifies nobody; agents elsewhere use requests instead.
Frames
Control messages are JSON text frames. Binary frames: byte 0 is 0/1 (call stream, text/binary) or 2/3 (request stream, text/binary), bytes 1–4 the stream id (big endian), then the payload.
