fix(cli): remove side-effect from agent:end state updater (#133)
Use a ref to track current stream text so agent:end can commit the final message without calling setMessages inside a setCurrentStreamText updater function, which violates React's rule that state updaters must be pure. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,7 @@ export function TuiApp({
|
||||
const [availableModels, setAvailableModels] = useState<ModelInfo[]>([]);
|
||||
|
||||
const socketRef = useRef<Socket | null>(null);
|
||||
const currentStreamTextRef = useRef('');
|
||||
|
||||
// Fetch available models on mount
|
||||
useEffect(() => {
|
||||
@@ -102,20 +103,22 @@ export function TuiApp({
|
||||
|
||||
socket.on('agent:start', () => {
|
||||
setIsStreaming(true);
|
||||
currentStreamTextRef.current = '';
|
||||
setCurrentStreamText('');
|
||||
});
|
||||
|
||||
socket.on('agent:text', (data: { text: string }) => {
|
||||
setCurrentStreamText((prev) => prev + data.text);
|
||||
currentStreamTextRef.current += data.text;
|
||||
setCurrentStreamText(currentStreamTextRef.current);
|
||||
});
|
||||
|
||||
socket.on('agent:end', () => {
|
||||
setCurrentStreamText((prev) => {
|
||||
if (prev) {
|
||||
setMessages((msgs) => [...msgs, { role: 'assistant', content: prev }]);
|
||||
}
|
||||
return '';
|
||||
});
|
||||
const finalText = currentStreamTextRef.current;
|
||||
currentStreamTextRef.current = '';
|
||||
setCurrentStreamText('');
|
||||
if (finalText) {
|
||||
setMessages((msgs) => [...msgs, { role: 'assistant', content: finalText }]);
|
||||
}
|
||||
setIsStreaming(false);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user