-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathConversationView.tsx
More file actions
251 lines (234 loc) · 7.61 KB
/
ConversationView.tsx
File metadata and controls
251 lines (234 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {
sessionStoreSetters,
useOptimisticItemsForTask,
usePendingPermissionsForTask,
useQueuedMessagesForTask,
} from "@features/sessions/stores/sessionStore";
import { useSettingsStore } from "@features/settings/stores/settingsStore";
import { useFeatureFlag } from "@hooks/useFeatureFlag";
import { ArrowDown, XCircle } from "@phosphor-icons/react";
import { Box, Button, Flex, Text } from "@radix-ui/themes";
import type { AcpMessage } from "@shared/types/session-events";
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
buildConversationItems,
type ConversationItem,
type TurnContext,
} from "./buildConversationItems";
import { GitActionMessage } from "./GitActionMessage";
import { GitActionResult } from "./GitActionResult";
import { SessionFooter } from "./SessionFooter";
import { QueuedMessageView } from "./session-update/QueuedMessageView";
import {
type RenderItem,
SessionUpdateView,
} from "./session-update/SessionUpdateView";
import { UserMessage } from "./session-update/UserMessage";
import { UserShellExecuteView } from "./session-update/UserShellExecuteView";
import { VirtualizedList, type VirtualizedListHandle } from "./VirtualizedList";
interface ConversationViewProps {
events: AcpMessage[];
isPromptPending: boolean | null;
promptStartedAt?: number | null;
repoPath?: string | null;
taskId?: string;
slackThreadUrl?: string;
}
export function ConversationView({
events,
isPromptPending,
promptStartedAt,
repoPath,
taskId,
slackThreadUrl,
}: ConversationViewProps) {
const listRef = useRef<VirtualizedListHandle>(null);
const [showScrollButton, setShowScrollButton] = useState(false);
const agentLogsEnabled = useFeatureFlag("posthog-code-background-agent-logs");
const debugLogsCloudRuns = useSettingsStore((s) => s.debugLogsCloudRuns);
const showDebugLogs = agentLogsEnabled && debugLogsCloudRuns;
const { items: conversationItems, lastTurnInfo } = useMemo(
() =>
buildConversationItems(events, isPromptPending, {
showDebugLogs,
}),
[events, isPromptPending, showDebugLogs],
);
const firstUserMessageIdRef = useRef<string | undefined>(undefined);
if (firstUserMessageIdRef.current === undefined) {
firstUserMessageIdRef.current = conversationItems.find(
(i) => i.type === "user_message",
)?.id;
}
const firstUserMessageId = firstUserMessageIdRef.current;
const pendingPermissions = usePendingPermissionsForTask(taskId ?? "");
const pendingPermissionsCount = pendingPermissions.size;
const queuedMessages = useQueuedMessagesForTask(taskId);
const optimisticItems = useOptimisticItemsForTask(taskId);
const queuedItems = useMemo<Extract<ConversationItem, { type: "queued" }>[]>(
() =>
queuedMessages.map((msg) => ({
type: "queued" as const,
id: msg.id,
message: msg,
})),
[queuedMessages],
);
const items = useMemo<ConversationItem[]>(() => {
const result: ConversationItem[] = [
...conversationItems,
...optimisticItems,
];
return queuedItems.length > 0 ? [...result, ...queuedItems] : result;
}, [conversationItems, optimisticItems, queuedItems]);
const handleScrollStateChange = useCallback((isAtBottom: boolean) => {
setShowScrollButton(!isAtBottom);
}, []);
const scrollToBottom = useCallback(() => {
listRef.current?.scrollToBottom();
setShowScrollButton(false);
}, []);
useEffect(() => {
const handleVisibilityChange = () => {
if (!document.hidden) {
listRef.current?.scrollToBottom();
setShowScrollButton(false);
}
};
document.addEventListener("visibilitychange", handleVisibilityChange);
return () =>
document.removeEventListener("visibilitychange", handleVisibilityChange);
}, []);
const renderItem = useCallback(
(item: ConversationItem) => {
switch (item.type) {
case "user_message":
return (
<UserMessage
content={item.content}
timestamp={item.timestamp}
sourceUrl={
slackThreadUrl && item.id === firstUserMessageId
? slackThreadUrl
: undefined
}
/>
);
case "git_action":
return <GitActionMessage actionType={item.actionType} />;
case "session_update":
return (
<SessionUpdateRow
update={item.update}
turnContext={item.turnContext}
thoughtComplete={item.thoughtComplete}
/>
);
case "git_action_result":
return repoPath ? (
<GitActionResult
actionType={item.actionType}
repoPath={repoPath}
turnId={item.turnId}
/>
) : null;
case "turn_cancelled":
return <TurnCancelledView interruptReason={item.interruptReason} />;
case "user_shell_execute":
return <UserShellExecuteView item={item} />;
case "queued":
return (
<QueuedMessageView
message={item.message}
onRemove={
taskId
? () =>
sessionStoreSetters.removeQueuedMessage(
taskId,
item.message.id,
)
: undefined
}
/>
);
}
},
[repoPath, taskId, slackThreadUrl, firstUserMessageId],
);
const getItemKey = useCallback((item: ConversationItem) => item.id, []);
return (
<div className="relative flex-1">
<VirtualizedList
ref={listRef}
items={items}
getItemKey={getItemKey}
renderItem={renderItem}
onScrollStateChange={handleScrollStateChange}
className="absolute inset-0 bg-gray-1"
itemClassName="mx-auto max-w-[750px] px-2 py-1.5"
footer={
<div className="pb-16">
<SessionFooter
isPromptPending={isPromptPending}
promptStartedAt={promptStartedAt}
lastGenerationDuration={
lastTurnInfo?.isComplete ? lastTurnInfo.durationMs : null
}
lastStopReason={lastTurnInfo?.stopReason}
queuedCount={queuedMessages.length}
hasPendingPermission={pendingPermissionsCount > 0}
/>
</div>
}
/>
{showScrollButton && (
<Box className="absolute right-4 bottom-4 z-10">
<Button size="1" variant="solid" onClick={scrollToBottom}>
<ArrowDown size={14} weight="bold" />
Scroll to bottom
</Button>
</Box>
)}
</div>
);
}
const SessionUpdateRow = memo(function SessionUpdateRow({
update,
turnContext,
thoughtComplete,
}: {
update: RenderItem;
turnContext: TurnContext;
thoughtComplete?: boolean;
}) {
return (
<SessionUpdateView
item={update}
toolCalls={turnContext.toolCalls}
childItems={turnContext.childItems}
turnCancelled={turnContext.turnCancelled}
turnComplete={turnContext.turnComplete}
thoughtComplete={thoughtComplete}
/>
);
});
const TurnCancelledView = memo(function TurnCancelledView({
interruptReason,
}: {
interruptReason?: string;
}) {
const message =
interruptReason === "moving_to_worktree"
? "Paused while worktree is focused"
: "Interrupted by user";
return (
<Box className="border-gray-4 border-l-2 py-0.5 pl-3">
<Flex align="center" gap="2" className="text-gray-9">
<XCircle size={14} />
<Text size="1" color="gray">
{message}
</Text>
</Flex>
</Box>
);
});