Skip to content

Commit

Permalink
don't bork if connection disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone committed Feb 25, 2025
1 parent fd17e02 commit d798d99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-loops-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/agents": patch
---

don't bork if connection disconnects
12 changes: 10 additions & 2 deletions packages/agents/src/ai-chat-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ export class AIChatAgent<Env = unknown> extends Agent<Env> {
}

private sendChatMessage(connection: Connection, message: OutgoingMessage) {
connection.send(JSON.stringify(message));
try {
connection.send(JSON.stringify(message));
} catch (e) {
// silently ignore
}
}

private broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {
this.broadcast(JSON.stringify(message), exclude);
try {
this.broadcast(JSON.stringify(message), exclude);
} catch (e) {
// silently ignore
}
}

override async onMessage(connection: Connection, message: WSMessage) {
Expand Down

0 comments on commit d798d99

Please sign in to comment.