pi-mono/packages/client
2026-08-14 01:32:32 +03:00
..
src feat(client): discover local Unix services 2026-08-13 23:53:52 +03:00
test fix: restore client-server protocol test coverage 2026-08-14 01:32:32 +03:00
CHANGELOG.md Add [Unreleased] section for next cycle 2026-08-07 07:48:13 +02:00
package.json Release v0.84.1 2026-08-07 07:48:13 +02:00
README.md feat(client): discover local Unix services 2026-08-13 23:53:52 +03:00
tsconfig.build.json feat(client): add runtime-neutral session client 2026-07-31 12:19:19 +03:00
tsconfig.test.json feat(protocol): add service-addressed session RPC 2026-08-13 18:24:27 +03:00
vitest.config.ts feat(client): add runtime-neutral session client 2026-07-31 12:19:19 +03:00

@earendil-works/pi-client

Transport-neutral client for the initial Pi list and attach protocol slice.

import { PiClient, type ByteTransportFactory } from "@earendil-works/pi-client";

const transportFactory: ByteTransportFactory = async (handlers) => {
  // Connect using WebSocket, Unix socket, or another ordered byte transport.
  return {
    async send(chunk) {
      // Deliver bytes in invocation order and honor backpressure.
    },
    close() {},
  };
};

const client = await PiClient.connect({
  serviceId: "0123456789abcdef0123456789abcdef",
  transportFactory,
});
const sessions = await client.listSessions();
const attachment = await client.attachSession(sessions[0].id);

The client verifies that the physical endpoint reports the expected logical serviceId. Every list and attach request carries that ID again so the final server can reject misdelivery.

attachSession() currently returns only { sessionId }. Remote Session and Harness methods will be added directly from the new shared interfaces in a later slice. The client does not reconnect or replay requests automatically. After disconnection, call reconnect() and explicitly repeat safe control-plane actions.

Call transport handlers as follows:

  • handlers.onData(chunk) for inbound bytes;
  • handlers.onClose() for an orderly terminal close;
  • handlers.onError(error) for transport failures.

A transport factory creates a fresh authenticated connection for each attempt. Requests are correlated by ID, and server failures are exposed as PiServerError.

Unix-domain sockets

Node.js and Bun consumers can use the separate Unix transport:

import { PiClient } from "@earendil-works/pi-client";
import { createUnixTransportFactory } from "@earendil-works/pi-client/unix";

const client = new PiClient({
  serviceId: "0123456789abcdef0123456789abcdef",
  transportFactory: createUnixTransportFactory({ path: "/tmp/pi.sock" }),
});
await client.connect();

Unix discovery scans ~/.pi/server/*.sock, derives each expected service ID from its filename, and verifies it through the existing handshake:

import { discoverUnixServices } from "@earendil-works/pi-client/unix";

const routes = await discoverUnixServices();
// [{ serviceId: "...", path: "/home/me/.pi/server/<serviceId>.sock" }]

Malformed entries, non-sockets, stale or unresponsive endpoints, and service-ID mismatches are ignored. Discovery is read-only and probes at most 16 sockets concurrently. Unexpected filesystem and socket errors reject discovery. Pass directory or timeoutMs to override the defaults.

PiClientOptions.maxFrameLength bounds protocol payloads. maxPendingBytes bounds queued Unix transport output. Configure matching limits on both peers.