Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/content/docs/agents/api-reference/agents-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -615,34 +615,34 @@ class AgentClient extends PartySocket {
import { AgentClient } from "agents/client";

// Connect to an Agent instance
const socket = new AgentClient({
const client = new AgentClient({
agent: "chat-agent", // Name of your Agent class in kebab-case
name: "support-room-123", // Specific instance name
host: window.location.host, // Using same host
});

// Optional event handlers
onOpen: () => {
console.log("Connected to agent");
// Send an initial message
socket.send(JSON.stringify({ type: "join", user: "user123" }));
},
client.onopen = () => {
console.log("Connected to agent");
// Send an initial message
client.send(JSON.stringify({ type: "join", user: "user123" }));
};

onMessage: (event) => {
// Handle incoming messages
const data = JSON.parse(event.data);
console.log("Received:", data);
client.onmessage = (event) => {
// Handle incoming messages
const data = JSON.parse(event.data);
console.log("Received:", data);

if (data.type === "state_update") {
// Update local UI with new state
updateUI(data.state);
}
},
if (data.type === "state_update") {
// Update local UI with new state
updateUI(data.state);
}
};

onClose: () => console.log("Disconnected from agent")
});
client.onclose = () => console.log("Disconnected from agent");

// Send messages to the Agent
function sendMessage(text) {
socket.send(JSON.stringify({
client.send(JSON.stringify({
type: "message",
text,
timestamp: Date.now()
Expand Down