Skip to content

Commit 4c7ead2

Browse files
committed
refactor(cli): refactor message-with-agents and add chat types
Refactor message-with-agents component to use zustand store. Add chat types and update error-boundary component.
1 parent 7b65f1b commit 4c7ead2

File tree

5 files changed

+159
-238
lines changed

5 files changed

+159
-238
lines changed

cli/src/chat.tsx

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { getProjectRoot } from './project-files'
5757
import { useChatStore } from './state/chat-store'
5858
import { useChatHistoryStore } from './state/chat-history-store'
5959
import { useFeedbackStore } from './state/feedback-store'
60+
import { useMessageBlockStore } from './state/message-block-store'
6061
import { usePublishStore } from './state/publish-store'
6162
import {
6263
addClipboardPlaceholder,
@@ -1363,6 +1364,52 @@ export const Chat = ({
13631364
[messages],
13641365
)
13651366

1367+
// Sync message block context to zustand store for child components
1368+
const setMessageBlockContext = useMessageBlockStore(
1369+
(state) => state.setContext,
1370+
)
1371+
const setMessageBlockCallbacks = useMessageBlockStore(
1372+
(state) => state.setCallbacks,
1373+
)
1374+
1375+
// Update context when values change
1376+
useEffect(() => {
1377+
setMessageBlockContext({
1378+
theme,
1379+
markdownPalette,
1380+
messageTree,
1381+
isWaitingForResponse,
1382+
timerStartTime,
1383+
availableWidth: messageAvailableWidth,
1384+
})
1385+
}, [
1386+
theme,
1387+
markdownPalette,
1388+
messageTree,
1389+
isWaitingForResponse,
1390+
timerStartTime,
1391+
messageAvailableWidth,
1392+
setMessageBlockContext,
1393+
])
1394+
1395+
// Update callbacks once (they're stable)
1396+
useEffect(() => {
1397+
setMessageBlockCallbacks({
1398+
onToggleCollapsed: handleCollapseToggle,
1399+
onBuildFast: handleBuildFast,
1400+
onBuildMax: handleBuildMax,
1401+
onFeedback: handleMessageFeedback,
1402+
onCloseFeedback: handleCloseFeedback,
1403+
})
1404+
}, [
1405+
handleCollapseToggle,
1406+
handleBuildFast,
1407+
handleBuildMax,
1408+
handleMessageFeedback,
1409+
handleCloseFeedback,
1410+
setMessageBlockCallbacks,
1411+
])
1412+
13661413
// Compute visible messages slice (from the end)
13671414
const visibleTopLevelMessages = useMemo(() => {
13681415
if (topLevelMessages.length <= visibleMessageCount) {
@@ -1530,20 +1577,7 @@ export const Chat = ({
15301577
message={message}
15311578
depth={0}
15321579
isLastMessage={isLast}
1533-
theme={theme}
1534-
markdownPalette={markdownPalette}
1535-
streamingAgents={streamingAgents}
1536-
messageTree={messageTree}
1537-
messages={messages}
15381580
availableWidth={messageAvailableWidth}
1539-
setFocusedAgentId={setFocusedAgentId}
1540-
isWaitingForResponse={isWaitingForResponse}
1541-
timerStartTime={timerStartTime}
1542-
onToggleCollapsed={handleCollapseToggle}
1543-
onBuildFast={handleBuildFast}
1544-
onBuildMax={handleBuildMax}
1545-
onFeedback={handleMessageFeedback}
1546-
onCloseFeedback={handleCloseFeedback}
15471581
/>
15481582
)
15491583
})}

cli/src/components/error-boundary.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
import { memo, type ReactNode } from 'react'
22

3-
interface ErrorBoundaryProps {
3+
interface ErrorBoundaryPlaceholderProps {
44
children: ReactNode
55
fallback: ReactNode
66
componentName?: string
77
}
88

99
/**
10-
* A wrapper component that provides error boundary-like behavior.
11-
* Since OpenTUI's JSX types don't support React class components,
12-
* this uses a memo wrapper. Errors that occur during render will
13-
* be caught by React's error boundary mechanism if one exists higher
14-
* in the tree, or will propagate normally.
10+
* **WARNING: This component does NOT catch render errors.**
1511
*
16-
* For true error boundary behavior in OpenTUI, wrap at the application
17-
* root level using React's native error boundary support.
12+
* This is a placeholder/passthrough component that exists for structural purposes.
13+
* OpenTUI's JSX types don't support React class components, which are required
14+
* for true error boundary functionality.
15+
*
16+
* For actual error catching in render functions, use `withErrorFallback()` instead.
17+
*
18+
* @example
19+
* // Use withErrorFallback for catching render errors:
20+
* const safeContent = withErrorFallback(
21+
* () => riskyRenderFunction(),
22+
* <FallbackComponent />,
23+
* 'MyComponent'
24+
* )
1825
*/
19-
export const ErrorBoundary = memo(
20-
({ children, fallback, componentName }: ErrorBoundaryProps) => {
21-
// Note: This is a structural wrapper. True error catching requires
22-
// a class component, but OpenTUI's JSX types don't support them.
23-
// The fallback is available for parent components to use when they
24-
// detect errors through other means.
26+
export const ErrorBoundaryPlaceholder = memo(
27+
({ children }: ErrorBoundaryPlaceholderProps) => {
28+
// This component does NOT catch errors - it's a passthrough.
29+
// Use withErrorFallback() for actual error catching.
2530
return <>{children}</>
2631
},
2732
)
2833

34+
/**
35+
* @deprecated Use `ErrorBoundaryPlaceholder` instead. This alias exists for backward
36+
* compatibility but the name is misleading since it doesn't actually catch errors.
37+
*/
38+
export const ErrorBoundary = ErrorBoundaryPlaceholder
39+
2940
/**
3041
* Helper to safely render content with error handling.
3142
* Use this when you need to catch render errors in a functional context.

0 commit comments

Comments
 (0)