Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
deriveWorkLogEntries,
hasActionableProposedPlan,
hasToolActivityForTurn,
hasSettledLatestTurnAdvanced,
isLatestTurnSettled,
formatElapsed,
} from "../session-logic";
Expand Down Expand Up @@ -388,6 +389,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});
const attachmentPreviewHandoffTimeoutByMessageIdRef = useRef<Record<string, number>>({});
const sendInFlightRef = useRef(false);
const sendBaselineTurnIdRef = useRef<TurnId | null>(null);
const dragDepthRef = useRef(0);
const terminalOpenByThreadRef = useRef<Record<string, boolean>>({});
const setMessagesScrollContainerRef = useCallback((element: HTMLDivElement | null) => {
Expand Down Expand Up @@ -2061,12 +2063,17 @@ export default function ChatView({ threadId }: ChatViewProps) {
};
}, [phase]);

const beginSendPhase = useCallback((nextPhase: Exclude<SendPhase, "idle">) => {
setSendStartedAt((current) => current ?? new Date().toISOString());
setSendPhase(nextPhase);
}, []);
const beginSendPhase = useCallback(
(nextPhase: Exclude<SendPhase, "idle">) => {
sendBaselineTurnIdRef.current = activeLatestTurn?.turnId ?? null;
setSendStartedAt((current) => current ?? new Date().toISOString());
setSendPhase(nextPhase);
},
[activeLatestTurn?.turnId],
);

const resetSendPhase = useCallback(() => {
sendBaselineTurnIdRef.current = null;
setSendPhase("idle");
setSendStartedAt(null);
}, []);
Expand All @@ -2092,6 +2099,23 @@ export default function ChatView({ threadId }: ChatViewProps) {
sendPhase,
]);

useEffect(() => {
if (sendPhase === "idle") {
return;
}
if (
!hasSettledLatestTurnAdvanced(
activeLatestTurn,
activeThread?.session ?? null,
sendBaselineTurnIdRef.current,
)
) {
return;
}

resetSendPhase();
}, [activeLatestTurn, activeThread?.session, resetSendPhase, sendPhase]);

useEffect(() => {
if (!activeThreadId) return;
const previous = terminalOpenByThreadRef.current[activeThreadId] ?? false;
Expand Down
11 changes: 11 additions & 0 deletions apps/web/src/session-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ export function isLatestTurnSettled(
return true;
}

export function hasSettledLatestTurnAdvanced(
latestTurn: LatestTurnTiming | null,
session: SessionActivityState | null,
baselineTurnId: TurnId | null,
): boolean {
if (!isLatestTurnSettled(latestTurn, session)) {
return false;
}
return (latestTurn?.turnId ?? null) !== baselineTurnId;
}

export function deriveActiveWorkStartedAt(
latestTurn: LatestTurnTiming | null,
session: SessionActivityState | null,
Expand Down