( messages: readonly Message[], setMessages: SetMessages, isLoading: boolean, )
| 30 | * Focus state 'unknown' (terminal doesn't support DECSET 1004) is a no-op. |
| 31 | */ |
| 32 | export function useAwaySummary( |
| 33 | messages: readonly Message[], |
| 34 | setMessages: SetMessages, |
| 35 | isLoading: boolean, |
| 36 | ): void { |
| 37 | const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null) |
| 38 | const abortRef = useRef<AbortController | null>(null) |
| 39 | const messagesRef = useRef(messages) |
| 40 | const isLoadingRef = useRef(isLoading) |
| 41 | const pendingRef = useRef(false) |
| 42 | const generateRef = useRef<(() => Promise<void>) | null>(null) |
| 43 | |
| 44 | messagesRef.current = messages |
| 45 | isLoadingRef.current = isLoading |
| 46 | |
| 47 | // 3P default: false |
| 48 | const gbEnabled = getFeatureValue_CACHED_MAY_BE_STALE( |
| 49 | 'tengu_sedge_lantern', |
| 50 | false, |
| 51 | ) |
| 52 | |
| 53 | useEffect(() => { |
| 54 | if (!feature('AWAY_SUMMARY')) return |
| 55 | if (!gbEnabled) return |
| 56 | |
| 57 | function clearTimer(): void { |
| 58 | if (timerRef.current !== null) { |
| 59 | clearTimeout(timerRef.current) |
| 60 | timerRef.current = null |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | function abortInFlight(): void { |
| 65 | abortRef.current?.abort() |
| 66 | abortRef.current = null |
| 67 | } |
| 68 | |
| 69 | async function generate(): Promise<void> { |
| 70 | pendingRef.current = false |
| 71 | if (hasSummarySinceLastUserTurn(messagesRef.current)) return |
| 72 | abortInFlight() |
| 73 | const controller = new AbortController() |
| 74 | abortRef.current = controller |
| 75 | const text = await generateAwaySummary( |
| 76 | messagesRef.current, |
| 77 | controller.signal, |
| 78 | ) |
| 79 | if (controller.signal.aborted || text === null) return |
| 80 | setMessages(prev => [...prev, createAwaySummaryMessage(text)]) |
| 81 | } |
| 82 | |
| 83 | function onBlurTimerFire(): void { |
| 84 | timerRef.current = null |
| 85 | if (isLoadingRef.current) { |
| 86 | pendingRef.current = true |
| 87 | return |
| 88 | } |
| 89 | void generate() |
no test coverage detected