Skip to content

Commit 84a2384

Browse files
authored
fix(agent): address message send races
Generated-By: PostHog Code Task-Id: a0ce6802-e1c9-4c53-bcf0-85bacfdf2536
1 parent b27454c commit 84a2384

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

packages/ui/src/features/sessions/components/SessionView.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Pause, Spinner, Warning } from "@phosphor-icons/react";
2+
import { contentToXml } from "@posthog/core/message-editor/content";
23
import {
34
createLatestPlanTracker,
45
SESSION_SERVICE,
@@ -299,6 +300,9 @@ export function SessionView({
299300
]);
300301

301302
const isCloudRun = useIsWorkspaceCloudRun(taskId);
303+
const editorRef = useRef<PromptInputHandle>(null);
304+
const sendInFlightRef = useRef(false);
305+
const [isSendingPrompt, setIsSendingPrompt] = useState(false);
302306

303307
const latestPlanTrackerRef = useRef<ReturnType<
304308
typeof createLatestPlanTracker
@@ -309,22 +313,25 @@ export function SessionView({
309313
(): Plan | null => latestPlanTracker.update(events) as Plan | null,
310314
[events, latestPlanTracker],
311315
);
312-
const [isSendingPrompt, setIsSendingPrompt] = useState(false);
313-
314316
const handleSubmit = useCallback(
315317
async (text: string): Promise<void> => {
316-
if (!text.trim() || isSendingPrompt) return;
318+
if (!text.trim() || sendInFlightRef.current) return;
317319

320+
sendInFlightRef.current = true;
318321
setIsSendingPrompt(true);
319322
try {
320323
if (await onSendPrompt(text)) {
321-
editorRef.current?.clear();
324+
const editor = editorRef.current;
325+
if (editor && contentToXml(editor.getContent()) === text) {
326+
editor.clear();
327+
}
322328
}
323329
} finally {
330+
sendInFlightRef.current = false;
324331
setIsSendingPrompt(false);
325332
}
326333
},
327-
[isSendingPrompt, onSendPrompt],
334+
[onSendPrompt],
328335
);
329336

330337
const handleBeforeSubmit = useCallback(
@@ -345,7 +352,6 @@ export function SessionView({
345352
const cancelQueuedEdit = useCancelQueuedMessageEdit(taskId);
346353

347354
const [isDraggingFile, setIsDraggingFile] = useState(false);
348-
const editorRef = useRef<PromptInputHandle>(null);
349355
const promptRecallRef = useRef<PromptRecallHandler | null>(null);
350356
const handlePromptRecall = useCallback<PromptRecallHandler>(
351357
(direction) => promptRecallRef.current?.(direction) ?? null,

0 commit comments

Comments
 (0)