Skip to content

Commit cb8c91f

Browse files
committed
Fix
1 parent bc82358 commit cb8c91f

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,36 @@ interface ChatState {
291291
};
292292
}
293293

294+
interface Env {
295+
// Your bindings and environment variables
296+
}
297+
294298
// Inside your Agent class
295-
async addMessage(sender: string, text: string) {
296-
// Update state with new message
297-
this.setState({
298-
...this.state,
299-
messages: [
300-
...this.state.messages,
301-
{ sender, text, timestamp: Date.now() }
302-
].slice(-this.state.settings.maxHistoryLength) // Maintain max history
303-
});
299+
export class YourAgent extends Agent<Env, ChatState> {
300+
async addMessage(sender: string, text: string) {
301+
// Update state with new message
302+
this.setState({
303+
...this.state,
304+
messages: [
305+
...this.state.messages,
306+
{ sender, text, timestamp: Date.now() }
307+
].slice(-this.state.settings.maxHistoryLength) // Maintain max history
308+
});
304309

305-
// The onStateUpdate method will automatically be called
306-
// and all connected clients will receive the update
307-
}
310+
// The onStateUpdate method will automatically be called
311+
// and all connected clients will receive the update
312+
}
308313

309-
// Override onStateUpdate to add custom behavior when state changes
310-
onStateUpdate(state: ChatState, source: "server" | Connection) {
311-
console.log(`State updated by ${source === "server" ? "server" : "client"}`);
314+
// Override onStateUpdate to add custom behavior when state changes
315+
onStateUpdate(state: ChatState, source: "server" | Connection) {
316+
console.log(`State updated by ${source === "server" ? "server" : "client"}`);
312317

313-
// You could trigger additional actions based on state changes
314-
if (state.messages.length > 0) {
315-
const lastMessage = state.messages[state.messages.length - 1];
316-
if (lastMessage.text.includes('@everyone')) {
317-
this.notifyAllParticipants(lastMessage);
318+
// You could trigger additional actions based on state changes
319+
if (state.messages.length > 0) {
320+
const lastMessage = state.messages[state.messages.length - 1];
321+
if (lastMessage.text.includes('@everyone')) {
322+
this.notifyAllParticipants(lastMessage);
323+
}
318324
}
319325
}
320326
}

0 commit comments

Comments
 (0)