Documentation menu
API reference

AgentClient API

A route-aware subclass of Cloudflare AgentClient for non-React browser clients.

Constructor

route replaces the upstream default /agents/<class>/<name> path with Ayjnt’s file route. name is encoded as one path segment.

In a browser on the same origin, omit host and protocol. Supply them when a separate frontend connects to a deployed agent.

example
import { AgentClient } from "ayjnt/client";
import type SupportAgent from "../../agents/support/agent";

const client = new AgentClient<SupportAgent>({
  route: "/support",
  name: "customer-42",
  onStateUpdate(state, source) {
    console.log("state changed", source, state);
  },
  onIdentity(name, agentType) {
    console.log("connected", { name, agentType });
  },
});

await client.ready;

Inherited client surface

Prefer client.stub.method(...) for normal callable functions: the imported agent type supplies argument and return autocomplete. Use call() only when you need the lower-level RPC options.

MemberMeaning
readyPromise resolved after server identity arrives
stateLatest synchronized state
stub.method(...)Typed callable RPC
call(name, args, options)Lower-level RPC including streaming options
setState(next)Request a client-originated state update
close(code?, reason?)Close and reject pending calls
Call RPC and update state
// Callable methods are inferred from SupportAgent.
const reply = await client.stub.answer("Where is my order?");

// Read the latest synchronized state.
console.log(client.state);

// If the agent permits client state updates:
client.setState({ ...client.state, draft: "" });

client.close();