Skip to content

Commit adaacfd

Browse files
Add documentation for chat processing modes and typing indicators
Documents new features from cloudflare/agents#685: - Chat processing modes (sequential and batch) for AIChatAgent - Typing indicator support via onInputChange and inputProps - Configuration options: chatProcessingMode, chatIdleTimeout, chatTypingTimeout - Updated useAgentChat API reference with new return values - Added comprehensive examples for both processing modes Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 5344722 commit adaacfd

File tree

1 file changed

+130
-2
lines changed

1 file changed

+130
-2
lines changed

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

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,21 @@ class AIChatAgent<Env = unknown, State = unknown> extends Agent<Env, State> {
809809

810810
// Persist messages within the Agent's local storage.
811811
async saveMessages(messages: Message[]): Promise<void>;
812+
813+
// Chat processing mode - how to handle multiple incoming messages
814+
// - "sequential": Process each message one-by-one (default)
815+
// - "batch": Combine multiple rapid messages into one response
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+
// Default: 1500ms (1.5 seconds)
826+
chatTypingTimeout: number;
812827
}
813828
```
814829

@@ -865,6 +880,106 @@ class CustomerSupportAgent extends AIChatAgent<Env> {
865880

866881
</TypeScriptExample>
867882

883+
#### Chat processing modes
884+
885+
The `AIChatAgent` supports two modes for handling multiple incoming chat messages:
886+
887+
- **Sequential mode** (default): Processes each message one-by-one, with each message getting its own response. Messages are queued and processed in order, waiting for each response to complete before starting the next.
888+
889+
- **Batch mode**: Combines multiple rapid messages into a single response. Uses debounce timing and optional typing indicators to intelligently batch messages, providing a better conversational experience when users send multiple short messages in quick succession.
890+
891+
**Sequential mode example:**
892+
893+
<TypeScriptExample>
894+
895+
```ts
896+
import { AIChatAgent } from "agents/ai-chat-agent";
897+
898+
class MyAgent extends AIChatAgent<Env> {
899+
// Sequential mode is the default - no configuration needed
900+
// Each message gets its own response
901+
async onChatMessage(onFinish) {
902+
// Process message and return response
903+
}
904+
}
905+
```
906+
907+
</TypeScriptExample>
908+
909+
**Batch mode example:**
910+
911+
<TypeScriptExample>
912+
913+
```ts
914+
import { AIChatAgent } from "agents/ai-chat-agent";
915+
916+
class MyAgent extends AIChatAgent<Env> {
917+
// Enable batch mode
918+
chatProcessingMode = "batch";
919+
920+
// Configure batching timeouts
921+
chatIdleTimeout = 3000; // 3 seconds - wait for user to start typing
922+
chatTypingTimeout = 1500; // 1.5 seconds - wait after typing stops
923+
924+
async onChatMessage(onFinish) {
925+
// Messages sent within the batching window are combined
926+
// Only the latest message (with full conversation) is processed
927+
}
928+
}
929+
```
930+
931+
</TypeScriptExample>
932+
933+
**Batch mode with typing indicators:**
934+
935+
For the best user experience in batch mode, use typing indicators to intelligently delay processing until the user finishes typing. This requires client-side integration:
936+
937+
<TypeScriptExample>
938+
939+
```tsx
940+
import { useAgentChat } from "agents/ai-react";
941+
import { useAgent } from "agents/react";
942+
943+
function ChatInterface() {
944+
const agent = useAgent({ agent: "my-agent", name: "user-123" });
945+
946+
const { input, handleInputChange, handleSubmit, onInputChange } = useAgentChat({
947+
agent
948+
});
949+
950+
return (
951+
<form onSubmit={handleSubmit}>
952+
<textarea
953+
value={input}
954+
onChange={(e) => {
955+
handleInputChange(e);
956+
// Send typing indicator to enable smart batching
957+
onInputChange(e);
958+
}}
959+
/>
960+
<button type="submit">Send</button>
961+
</form>
962+
);
963+
}
964+
```
965+
966+
</TypeScriptExample>
967+
968+
**How batch mode timing works:**
969+
970+
1. **Without typing indicators** (simple batching): All messages within `chatIdleTimeout` are batched together.
971+
972+
2. **With typing indicators** (smart batching):
973+
- After a message is sent, the agent waits up to `chatIdleTimeout` for the user to start typing
974+
- Once typing is detected, it switches to the shorter `chatTypingTimeout`
975+
- Each typing indicator resets the timer
976+
- Processing begins after typing stops for `chatTypingTimeout` milliseconds
977+
978+
This approach ensures that:
979+
- Multiple quick messages from the user are batched into one response
980+
- The agent waits for the user to finish their thought
981+
- Response generation happens promptly after the user stops typing
982+
868983
### Chat Agent React API
869984

870985
#### useAgentChat
@@ -923,6 +1038,14 @@ function useAgentChat(options: UseAgentChatOptions): {
9231038
addToolResult: ({ toolCallId, result }: { toolCallId: string; result: any }) => void;
9241039
// Current error if any
9251040
error: Error | undefined;
1041+
// Send a typing indicator to the agent (throttled to 500ms)
1042+
sendTypingIndicator: () => void;
1043+
// Input change handler that automatically sends typing indicators
1044+
onInputChange: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
1045+
// Props to spread onto your input element for automatic typing detection
1046+
inputProps: {
1047+
onChange: (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
1048+
};
9261049
};
9271050
```
9281051

@@ -949,7 +1072,8 @@ function ChatInterface() {
9491072
handleSubmit,
9501073
isLoading,
9511074
error,
952-
clearHistory
1075+
clearHistory,
1076+
onInputChange // New: for typing indicators
9531077
} = useAgentChat({
9541078
agent: agentConnection,
9551079
initialMessages: [
@@ -974,7 +1098,11 @@ function ChatInterface() {
9741098
<form onSubmit={handleSubmit} className="message-input">
9751099
<input
9761100
value={input}
977-
onChange={handleInputChange}
1101+
onChange={(e) => {
1102+
handleInputChange(e);
1103+
// Send typing indicators for batch mode agents
1104+
onInputChange(e);
1105+
}}
9781106
placeholder="Type your message..."
9791107
disabled={isLoading}
9801108
/>

0 commit comments

Comments
 (0)