Skip to content

Commit 4595a1c

Browse files
yyyyaaaclaude
andcommitted
fix(react): close three useChat gaps surfaced by PR #5 review
- send(): sync messagesRef before runStream so rapid synchronous sends both reach the outgoing request body - useState init: hydrate pendingDecisions from initialMessages so rehydrated paused tool calls render decision UI immediately - unmount: abort the in-flight fetch on cleanup to prevent leaked streams when the consumer unmounts mid-request Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cd00eaf commit 4595a1c

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

packages/react/src/use-chat.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function useChat(options: UseChatOptions): UseChatResult {
9494
const [isStreaming, setIsStreaming] = useState(false);
9595
const [pendingDecisions, setPendingDecisions] = useState<
9696
ReadonlyMap<string, ToolDecisionPendingEvent>
97-
>(() => new Map());
97+
>(() => rederivePendingDecisions(options.initialMessages ?? []));
9898
const [executingToolCallIds, setExecutingToolCallIds] = useState<ReadonlySet<string>>(
9999
() => new Set()
100100
);
@@ -293,6 +293,7 @@ export function useChat(options: UseChatOptions): UseChatResult {
293293
const userMessage: Message =
294294
typeof input === 'string' ? createUserMessage(input) : input;
295295
const requestMessages = [...messagesRef.current, userMessage];
296+
messagesRef.current = requestMessages;
296297
await runStream(requestMessages, userMessage);
297298
},
298299
[runStream]
@@ -358,6 +359,13 @@ export function useChat(options: UseChatOptions): UseChatResult {
358359
[]
359360
);
360361

362+
useEffect(() => {
363+
return () => {
364+
abortControllerRef.current?.abort();
365+
abortControllerRef.current = null;
366+
};
367+
}, []);
368+
361369
const abort = useCallback(() => {
362370
abortControllerRef.current?.abort();
363371
abortControllerRef.current = null;

0 commit comments

Comments
 (0)