Refresh current thread when run settles
This commit is contained in:
@@ -168,6 +168,7 @@ function scheduleSessionListRefresh() {
|
|||||||
|
|
||||||
export const Thread: FC = () => {
|
export const Thread: FC = () => {
|
||||||
useRefreshReplayedRun();
|
useRefreshReplayedRun();
|
||||||
|
useRefreshCurrentRun();
|
||||||
return (
|
return (
|
||||||
<ThreadPrimitive.Root
|
<ThreadPrimitive.Root
|
||||||
className="aui-root aui-thread-root @container flex h-full flex-col bg-background"
|
className="aui-root aui-thread-root @container flex h-full flex-col bg-background"
|
||||||
@@ -252,6 +253,59 @@ function useRefreshReplayedRun() {
|
|||||||
}, [replayedRun.active, replayedRun.sessionId, replaySession]);
|
}, [replayedRun.active, replayedRun.sessionId, replaySession]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function useRefreshCurrentRun() {
|
||||||
|
const { replaySession } = useClawSessionReplay();
|
||||||
|
const runtimeRunning = useAuiState((s) => s.thread.isRunning);
|
||||||
|
const sessionId = useCurrentThreadSessionId({
|
||||||
|
includePending: true,
|
||||||
|
includeActive: true,
|
||||||
|
});
|
||||||
|
const activeRunRef = useRef<string | null>(null);
|
||||||
|
const appliedTerminalRef = useRef<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!sessionId) return;
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
async function refreshIfSettled() {
|
||||||
|
if (!sessionId) return;
|
||||||
|
const runStatus = await fetchLatestRunStatus(sessionId);
|
||||||
|
if (cancelled) return;
|
||||||
|
if (runStatus?.status === "queued" || runStatus?.status === "running") {
|
||||||
|
activeRunRef.current = runStatus.run_id ?? "active";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!runtimeRunning && !activeRunRef.current) return;
|
||||||
|
const terminalKey = [
|
||||||
|
runStatus?.run_id ?? activeRunRef.current ?? "unknown",
|
||||||
|
runStatus?.status ?? "idle",
|
||||||
|
runStatus?.finished_at ?? "",
|
||||||
|
runStatus?.elapsed_ms ?? "",
|
||||||
|
].join(":");
|
||||||
|
if (appliedTerminalRef.current === terminalKey) return;
|
||||||
|
const response = await fetch(`/api/claw/sessions/${sessionId}`, {
|
||||||
|
cache: "no-store",
|
||||||
|
});
|
||||||
|
if (!response.ok || cancelled) return;
|
||||||
|
const payload = await response.json();
|
||||||
|
replaySession(
|
||||||
|
sessionId,
|
||||||
|
toReplayRepository(payload, sessionId, runStatus),
|
||||||
|
);
|
||||||
|
activeRunRef.current = null;
|
||||||
|
appliedTerminalRef.current = terminalKey;
|
||||||
|
scheduleSessionListRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshIfSettled();
|
||||||
|
const interval = window.setInterval(refreshIfSettled, 1500);
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
window.clearInterval(interval);
|
||||||
|
};
|
||||||
|
}, [runtimeRunning, sessionId, replaySession]);
|
||||||
|
}
|
||||||
|
|
||||||
function useReplayedRunState() {
|
function useReplayedRunState() {
|
||||||
const active = useAuiState((s) =>
|
const active = useAuiState((s) =>
|
||||||
s.thread.messages.some((message) => isReplayRunMessageId(message.id)),
|
s.thread.messages.some((message) => isReplayRunMessageId(message.id)),
|
||||||
|
|||||||
Reference in New Issue
Block a user