Skip to content

Commit ced52d6

Browse files
committed
Fix cli sluggishness by removing unnecessary re-renders
1 parent c008b88 commit ced52d6

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

cli/src/chat.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export const Chat = ({
164164
setSlashSelectedIndex,
165165
agentSelectedIndex,
166166
setAgentSelectedIndex,
167-
streamingAgents,
167+
streamingAgents: rawStreamingAgents,
168168
focusedAgentId,
169169
setFocusedAgentId,
170170
messages,
@@ -200,6 +200,16 @@ export const Chat = ({
200200
isRetrying: store.isRetrying,
201201
})),
202202
)
203+
204+
// Stabilize streamingAgents reference - only create new Set when content changes
205+
const streamingAgentsKey = useMemo(
206+
() => Array.from(rawStreamingAgents).sort().join(','),
207+
[rawStreamingAgents],
208+
)
209+
const streamingAgents = useMemo(
210+
() => rawStreamingAgents,
211+
[streamingAgentsKey],
212+
)
203213
const pendingBashMessages = useChatStore((state) => state.pendingBashMessages)
204214

205215
// Refs for tracking state across renders
@@ -889,7 +899,7 @@ export const Chat = ({
889899
useEffect(() => {
890900
inputValueRef.current = inputValue
891901
}, [inputValue])
892-
902+
893903
// Report activity on input changes for ad rotation (debounced via separate effect)
894904
const lastReportedActivityRef = useRef<number>(0)
895905
useEffect(() => {
@@ -976,7 +986,13 @@ export const Chat = ({
976986
reportActivity()
977987
const result = await onSubmitPrompt(inputValue, agentMode)
978988
handleCommandResult(result)
979-
}, [onSubmitPrompt, inputValue, agentMode, handleCommandResult, reportActivity])
989+
}, [
990+
onSubmitPrompt,
991+
inputValue,
992+
agentMode,
993+
handleCommandResult,
994+
reportActivity,
995+
])
980996

981997
const totalMentionMatches = agentMatches.length + fileMatches.length
982998
const historyNavUpEnabled =

cli/src/components/message-with-agents.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TextAttributes } from '@opentui/core'
2-
import { memo, useMemo, type ReactNode } from 'react'
2+
import { memo, useCallback, useMemo, type ReactNode } from 'react'
33
import React from 'react'
44

55
import { Button } from './button'
@@ -64,6 +64,18 @@ export const MessageWithAgents = memo(
6464
const SIDE_GUTTER = 1
6565
const isAgent = message.variant === 'agent'
6666

67+
// Memoize onOpenFeedback to prevent unnecessary re-renders
68+
const onOpenFeedback = useCallback(
69+
(options?: {
70+
category?: string
71+
footerMessage?: string
72+
errors?: Array<{ id: string; message: string }>
73+
}) => {
74+
onFeedback(message.id, options)
75+
},
76+
[onFeedback, message.id],
77+
)
78+
6779
const contentBoxStyle = useMemo(
6880
() => ({
6981
backgroundColor: theme.background,
@@ -209,11 +221,7 @@ export const MessageWithAgents = memo(
209221
onFeedback={onFeedback}
210222
onCloseFeedback={onCloseFeedback}
211223
validationErrors={message.validationErrors}
212-
onOpenFeedback={
213-
onFeedback
214-
? (options) => onFeedback(message.id, options)
215-
: undefined
216-
}
224+
onOpenFeedback={onOpenFeedback}
217225
attachments={message.attachments}
218226
metadata={message.metadata}
219227
isLastMessage={isLastMessage}

0 commit comments

Comments
 (0)