Skip to content

Commit c633891

Browse files
feat: 省略旧消息的代码 diff 展示,仅保留最新消息的完整 diff
1 parent bcbb8a6 commit c633891

5 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/components/Message.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export type Props = {
7777
lastThinkingBlockId?: string | null
7878
/** UUID of the latest user bash output message (for auto-expanding) */
7979
latestBashOutputUUID?: string | null
80+
/** Whether to collapse diff display for this message */
81+
shouldCollapseDiffs?: boolean
8082
}
8183

8284
function MessageImpl({
@@ -99,6 +101,7 @@ function MessageImpl({
99101
isUserContinuation = false,
100102
lastThinkingBlockId,
101103
latestBashOutputUUID,
104+
shouldCollapseDiffs,
102105
}: Props): React.ReactNode {
103106
switch (message.type) {
104107
case 'attachment':
@@ -181,6 +184,7 @@ function MessageImpl({
181184
isUserContinuation={isUserContinuation}
182185
lookups={lookups}
183186
isTranscriptMode={isTranscriptMode}
187+
shouldCollapseDiffs={shouldCollapseDiffs}
184188
/>
185189
))}
186190
</Box>
@@ -293,6 +297,7 @@ function UserMessage({
293297
isUserContinuation,
294298
lookups,
295299
isTranscriptMode,
300+
shouldCollapseDiffs,
296301
}: {
297302
message: NormalizedUserMessage
298303
addMargin: boolean
@@ -309,6 +314,7 @@ function UserMessage({
309314
isUserContinuation: boolean
310315
lookups: ReturnType<typeof buildMessageLookups>
311316
isTranscriptMode: boolean
317+
shouldCollapseDiffs?: boolean
312318
}): React.ReactNode {
313319
const { columns } = useTerminalSize()
314320
switch (param.type) {
@@ -344,6 +350,7 @@ function UserMessage({
344350
verbose={verbose}
345351
width={columns - 5}
346352
isTranscriptMode={isTranscriptMode}
353+
shouldCollapseDiffs={shouldCollapseDiffs}
347354
/>
348355
)
349356
default:

src/components/MessageRow.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type Props = {
5555
columns: number
5656
isLoading: boolean
5757
lookups: ReturnType<typeof buildMessageLookups>
58+
shouldCollapseDiffs?: boolean
5859
}
5960

6061
/**
@@ -141,6 +142,7 @@ function MessageRowImpl({
141142
columns,
142143
isLoading,
143144
lookups,
145+
shouldCollapseDiffs,
144146
}: Props): React.ReactNode {
145147
const isTranscriptMode = screen === 'transcript'
146148
const isGrouped = msg.type === 'grouped_tool_use'
@@ -221,6 +223,7 @@ function MessageRowImpl({
221223
isUserContinuation={isUserContinuation}
222224
lastThinkingBlockId={lastThinkingBlockId}
223225
latestBashOutputUUID={latestBashOutputUUID}
226+
shouldCollapseDiffs={shouldCollapseDiffs}
224227
/>
225228
)
226229
// OffscreenFreeze: the outer React.memo already bails for static messages,

src/components/Messages.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,12 @@ const MessagesImpl = ({
814814
streamingToolUseIDs,
815815
))
816816

817+
// Collapse diffs for messages beyond the latest N messages.
818+
// verbose (ctrl+o) overrides and always shows full diffs.
819+
const DIFF_COLLAPSE_DISTANCE = 0
820+
const shouldCollapseDiffs =
821+
renderableMessages.length - 1 - index > DIFF_COLLAPSE_DISTANCE
822+
817823
const k = messageKey(msg)
818824
const row = (
819825
<MessageRow
@@ -838,6 +844,7 @@ const MessagesImpl = ({
838844
columns={columns}
839845
isLoading={isLoading}
840846
lookups={lookups}
847+
shouldCollapseDiffs={shouldCollapseDiffs}
841848
/>
842849
)
843850

src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Props = {
2727
verbose: boolean
2828
width: number | string
2929
isTranscriptMode?: boolean
30+
shouldCollapseDiffs?: boolean
3031
}
3132

3233
export function UserToolResultMessage({
@@ -39,6 +40,7 @@ export function UserToolResultMessage({
3940
verbose,
4041
width,
4142
isTranscriptMode,
43+
shouldCollapseDiffs,
4244
}: Props): React.ReactNode {
4345
const toolUse = useGetToolFromMessages(param.tool_use_id, tools, lookups)
4446
if (!toolUse) {
@@ -96,6 +98,7 @@ export function UserToolResultMessage({
9698
verbose={verbose}
9799
width={width}
98100
isTranscriptMode={isTranscriptMode}
101+
shouldCollapseDiffs={shouldCollapseDiffs}
99102
/>
100103
)
101104
}

src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Props = {
3333
verbose: boolean
3434
width: number | string
3535
isTranscriptMode?: boolean
36+
shouldCollapseDiffs?: boolean
3637
}
3738

3839
export function UserToolSuccessMessage({
@@ -46,6 +47,7 @@ export function UserToolSuccessMessage({
4647
verbose,
4748
width,
4849
isTranscriptMode,
50+
shouldCollapseDiffs,
4951
}: Props): React.ReactNode {
5052
const [theme] = useTheme()
5153
// Hook stays inside feature() ternary so external builds don't pay a
@@ -83,12 +85,16 @@ export function UserToolSuccessMessage({
8385
}
8486
const toolResult = parsedOutput?.data ?? message.toolUseResult
8587

88+
// Collapse diff display for old messages (verbose/ctrl+o overrides)
89+
const effectiveStyle =
90+
shouldCollapseDiffs && !verbose ? 'condensed' : style
91+
8692
const renderedMessage =
8793
tool.renderToolResultMessage?.(
8894
toolResult as never,
8995
filterToolProgressMessages(progressMessagesForMessage),
9096
{
91-
style,
97+
style: effectiveStyle,
9298
theme,
9399
tools,
94100
verbose,

0 commit comments

Comments
 (0)