You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document chat processing modes and typing indicators for AIChatAgent
This update documents new features from PR #685 (cloudflare/agents):
- Chat processing modes (sequential vs batch)
- Typing indicators for smart message batching
- New AIChatAgent properties: chatProcessingMode, chatIdleTimeout, chatTypingTimeout
- New useAgentChat returns: onInputChange, inputProps, sendTypingIndicator
The documentation includes:
- API reference updates for AIChatAgent class properties
- Examples showing sequential and batch processing modes
- React examples demonstrating typing indicator integration
- Use cases and best practices for each mode
These features enable better handling of conversational chat patterns where
users send multiple rapid messages.
Related PR: cloudflare/agents#685
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
// Chat processing mode - controls how multiple incoming messages are handled
814
+
// "sequential": Each message gets its own response (default)
815
+
// "batch": Combine rapid messages into one response using debounce timing
816
+
chatProcessingMode:"sequential"|"batch";
817
+
818
+
// Idle timeout in milliseconds (batch mode only)
819
+
// How long to wait after a message is sent before processing
820
+
// Default: 5000ms (5 seconds)
821
+
chatIdleTimeout:number;
822
+
823
+
// Typing timeout in milliseconds (batch mode only)
824
+
// How long to wait after user stops typing before processing
825
+
// Requires client to send typing indicators via onInputChange
826
+
// Default: 1500ms (1.5 seconds)
827
+
chatTypingTimeout:number;
812
828
}
813
829
```
814
830
@@ -865,6 +881,75 @@ class CustomerSupportAgent extends AIChatAgent<Env> {
865
881
866
882
</TypeScriptExample>
867
883
884
+
##### Chat Processing Modes
885
+
886
+
`AIChatAgent` supports two modes for handling multiple incoming messages:
887
+
888
+
**Sequential Mode (Default)**
889
+
890
+
In sequential mode, each message gets its own response. Messages are queued and processed one at a time, waiting for each response to fully complete before processing the next. No client changes required.
// Sequential is the default - no configuration needed
899
+
chatProcessingMode ="sequential";
900
+
901
+
async onChatMessage(onFinish) {
902
+
// Each message will be processed independently
903
+
// Users will get separate responses for each message
904
+
returnawaitthis.generateResponse();
905
+
}
906
+
}
907
+
```
908
+
909
+
</TypeScriptExample>
910
+
911
+
**Batch Mode**
912
+
913
+
In batch mode, multiple rapid messages are combined into one response. This is better for conversational UX where users send multiple short messages in quick succession.
914
+
915
+
There are two ways to use batch mode:
916
+
917
+
1.**Simple batching** - Set a fixed time window (no client changes required)
918
+
2.**Smart batching** - Use typing indicators for more responsive batching (requires client changes)
When using batch mode with typing indicators on your agent, you can use the `onInputChange` handler or `inputProps` to automatically send typing signals. This allows the agent to intelligently batch messages while remaining responsive to user typing.
1091
+
1092
+
<TypeScriptExample>
1093
+
1094
+
```tsx
1095
+
// Using onInputChange for manual control
1096
+
import { useAgentChat } from"agents/ai-react";
1097
+
import { useAgent } from"agents/react";
1098
+
import { useState } from"react";
1099
+
1100
+
function SmartBatchingChat() {
1101
+
const agent =useAgent({
1102
+
agent: "smart-batch-agent",
1103
+
name: "user-123"
1104
+
});
1105
+
1106
+
const {
1107
+
messages,
1108
+
input,
1109
+
setInput,
1110
+
handleSubmit,
1111
+
onInputChange // New: typing indicator handler
1112
+
} =useAgentChat({ agent });
1113
+
1114
+
return (
1115
+
<formonSubmit={handleSubmit}>
1116
+
<input
1117
+
value={input}
1118
+
onChange={(e) => {
1119
+
setInput(e.target.value); // Update input value
1120
+
onInputChange(e); // Send typing indicator to agent
1121
+
}}
1122
+
placeholder="Type your message..."
1123
+
/>
1124
+
<buttontype="submit">Send</button>
1125
+
</form>
1126
+
);
1127
+
}
1128
+
1129
+
// Using inputProps for automatic setup
1130
+
function SimplerSmartBatchingChat() {
1131
+
const agent =useAgent({
1132
+
agent: "smart-batch-agent",
1133
+
name: "user-123"
1134
+
});
1135
+
1136
+
const {
1137
+
messages,
1138
+
input,
1139
+
setInput,
1140
+
handleSubmit,
1141
+
inputProps // New: contains onChange with typing indicator
0 commit comments