Skip to content

Commit c1da9c7

Browse files
authored
Fixed docs on using AgentClient (#21524)
1 parent f8a09b6 commit c1da9c7

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/content/docs/agents/api-reference/agents-api.mdx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -615,34 +615,34 @@ class AgentClient extends PartySocket {
615615
import { AgentClient } from "agents/client";
616616

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

622-
// Optional event handlers
623-
onOpen: () => {
624-
console.log("Connected to agent");
625-
// Send an initial message
626-
socket.send(JSON.stringify({ type: "join", user: "user123" }));
627-
},
624+
client.onopen = () => {
625+
console.log("Connected to agent");
626+
// Send an initial message
627+
client.send(JSON.stringify({ type: "join", user: "user123" }));
628+
};
628629

629-
onMessage: (event) => {
630-
// Handle incoming messages
631-
const data = JSON.parse(event.data);
632-
console.log("Received:", data);
630+
client.onmessage = (event) => {
631+
// Handle incoming messages
632+
const data = JSON.parse(event.data);
633+
console.log("Received:", data);
633634

634-
if (data.type === "state_update") {
635-
// Update local UI with new state
636-
updateUI(data.state);
637-
}
638-
},
635+
if (data.type === "state_update") {
636+
// Update local UI with new state
637+
updateUI(data.state);
638+
}
639+
};
639640

640-
onClose: () => console.log("Disconnected from agent")
641-
});
641+
client.onclose = () => console.log("Disconnected from agent");
642642

643643
// Send messages to the Agent
644644
function sendMessage(text) {
645-
socket.send(JSON.stringify({
645+
client.send(JSON.stringify({
646646
type: "message",
647647
text,
648648
timestamp: Date.now()

0 commit comments

Comments
 (0)