2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { useSessionStore } from '@/stores/useSessionStore';
|
|
|
|
|
|
2026-02-05 01:59:49 +02:00
|
|
|
// Mirrors OpenCode SessionStatus: busy|retry|idle.
|
|
|
|
|
export type SessionActivityPhase = 'idle' | 'busy' | 'retry';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
export interface SessionActivityResult {
|
|
|
|
|
|
|
|
|
|
phase: SessionActivityPhase;
|
|
|
|
|
|
|
|
|
|
isWorking: boolean;
|
|
|
|
|
|
|
|
|
|
isBusy: boolean;
|
|
|
|
|
|
2026-02-05 01:59:49 +02:00
|
|
|
// Kept for backward compatibility; always false with server session.status.
|
2025-12-07 19:32:53 +02:00
|
|
|
isCooldown: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IDLE_RESULT: SessionActivityResult = {
|
|
|
|
|
phase: 'idle',
|
|
|
|
|
isWorking: false,
|
|
|
|
|
isBusy: false,
|
|
|
|
|
isCooldown: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function useSessionActivity(sessionId: string | null | undefined): SessionActivityResult {
|
|
|
|
|
|
|
|
|
|
const phase = useSessionStore((state) => {
|
2026-02-05 01:59:49 +02:00
|
|
|
if (!sessionId || !state.sessionStatus) {
|
2025-12-07 19:32:53 +02:00
|
|
|
return 'idle' as SessionActivityPhase;
|
|
|
|
|
}
|
2026-02-05 01:59:49 +02:00
|
|
|
const status = state.sessionStatus.get(sessionId);
|
|
|
|
|
return (status?.type ?? 'idle') as SessionActivityPhase;
|
2025-12-07 19:32:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return React.useMemo<SessionActivityResult>(() => {
|
|
|
|
|
if (phase === 'idle') {
|
|
|
|
|
return IDLE_RESULT;
|
|
|
|
|
}
|
|
|
|
|
const isBusy = phase === 'busy';
|
2026-02-05 01:59:49 +02:00
|
|
|
// No cooldown in server session.status; treat retry as working.
|
|
|
|
|
const isCooldown = false;
|
2025-12-07 19:32:53 +02:00
|
|
|
return {
|
|
|
|
|
phase,
|
2026-02-05 01:59:49 +02:00
|
|
|
isWorking: phase === 'busy' || phase === 'retry',
|
2025-12-07 19:32:53 +02:00
|
|
|
isBusy,
|
|
|
|
|
isCooldown,
|
|
|
|
|
};
|
|
|
|
|
}, [phase]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useCurrentSessionActivity(): SessionActivityResult {
|
|
|
|
|
const currentSessionId = useSessionStore((state) => state.currentSessionId);
|
|
|
|
|
return useSessionActivity(currentSessionId);
|
|
|
|
|
}
|