The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | effectiveSnoozed, |
| 3 | type ThreadSnoozeShell, |
| 4 | } from "@t3tools/client-runtime/state/thread-settled"; |
| 5 | |
| 6 | export type ThreadNotificationKind = "input" | "completion"; |
| 7 | |
| 8 | export interface ThreadNotificationObservation { |
| 9 | /** Status and request key, or status and run without a request, while the thread needs attention. */ |
| 10 | readonly attention: string | null; |
| 11 | /** The pending request's id, when it has one; null whenever attention is. */ |
| 12 | readonly requestId: string | null; |
| 13 | /** The thread is idle and its latest run completed. */ |
| 14 | readonly settled: boolean; |
| 15 | /** When the latest run completed, if it did. */ |
| 16 | readonly completedAt: number | null; |
| 17 | /** The latest run answers a background-work progress wake, like a Monitor line. */ |
| 18 | readonly backgroundWake: boolean; |
| 19 | } |
| 20 | |
| 21 | /** Matches the push relay's completion confirmation: a final wake can start right after a progress wake. */ |
| 22 | export const RELEASED_COMPLETION_GRACE_MS = 5_000; |
| 23 | |
| 24 | /** |
| 25 | * Whether completion notifications are suppressed by snooze. A completion that landed |
| 26 | * during the snooze stays suppressed even when its update is processed after the wake. |
| 27 | */ |
| 28 | export function isSnoozedForNotification( |
| 29 | thread: ThreadSnoozeShell, |
| 30 | completedAt: number | null, |
| 31 | now: number, |
| 32 | ): boolean { |
| 33 | if (effectiveSnoozed(thread, { now: new Date(now).toISOString() })) return true; |
| 34 | if (completedAt === null) return false; |
| 35 | if (thread.snoozedAt != null && completedAt < Date.parse(thread.snoozedAt)) return false; |
| 36 | return effectiveSnoozed(thread, { now: new Date(completedAt).toISOString() }); |
| 37 | } |
| 38 | |
| 39 | export function createThreadNotificationGate<Key>( |
| 40 | notify: (key: Key, kind: ThreadNotificationKind) => void, |
| 41 | ) { |
| 42 | // The newest completion already notified, or deliberately skipped. |
| 43 | const handled = new Map<Key, number>(); |
| 44 | // Wake completions waiting out the grace, by completion time; no timer while paused. |
| 45 | const releases = new Map< |
| 46 | Key, |
| 47 | { readonly at: number; readonly timer: ReturnType<typeof setTimeout> | undefined } |
| 48 | >(); |
| 49 | const release = (key: Key, at: number) => { |
| 50 | const timer = setTimeout(() => { |
| 51 | releases.delete(key); |
| 52 | handled.set(key, at); |
| 53 | notify(key, "completion"); |
| 54 | }, RELEASED_COMPLETION_GRACE_MS); |
| 55 | releases.set(key, { at, timer }); |
| 56 | }; |
| 57 | const skip = (key: Key, at: number | null) => { |
| 58 | if (at !== null && at > (handled.get(key) ?? -Infinity)) handled.set(key, at); |
| 59 | }; |
| 60 | const cancel = (key: Key) => { |
| 61 | clearTimeout(releases.get(key)?.timer); |
| 62 | releases.delete(key); |
| 63 | }; |
| 64 | // Attention keys with a request id seen since the thread last stopped needing attention. |
| 65 | const attended = new Map<Key, Set<string>>(); |
| 66 | const attend = ( |
| 67 | key: Key, |
| 68 | prior: ThreadNotificationObservation | undefined, |
| 69 | next: ThreadNotificationObservation, |
| 70 | ): boolean => { |
| 71 | if (next.attention === null) { |
| 72 | attended.delete(key); |
| 73 | return false; |
| 74 | } |
| 75 | if (next.requestId === null) return next.attention !== prior?.attention; |
| 76 | const seen = attended.get(key); |
| 77 | if (seen?.has(next.attention)) return false; |
| 78 | if (seen) seen.add(next.attention); |
| 79 | else attended.set(key, new Set([next.attention])); |
| 80 | return true; |
| 81 | }; |
| 82 | return { |
| 83 | observe( |
| 84 | key: Key, |
| 85 | prior: ThreadNotificationObservation | undefined, |
| 86 | next: ThreadNotificationObservation, |
| 87 | options: { readonly archived: boolean; readonly quiet: boolean; readonly snoozed: boolean }, |
| 88 | ): void { |
| 89 | // The grace survives updates that keep the wake's completion; anything else cancels it. |
| 90 | const pending = releases.get(key); |
| 91 | const silenced = options.archived || options.quiet || options.snoozed; |
| 92 | if (pending?.at === next.completedAt && !silenced) { |
| 93 | if (pending.timer === undefined) release(key, pending.at); |
| 94 | return; |
| 95 | } |
| 96 | cancel(key); |
| 97 | // The first sighting, archived or snoozed threads, and stopped or silent runs never notify completion. |
| 98 | if (!prior || silenced) skip(key, next.completedAt); |
| 99 | const fresh = attend(key, prior, next); |
| 100 | if (!prior || options.archived) return; |
| 101 | if (fresh) { |
| 102 | notify(key, "input"); |
| 103 | return; |
| 104 | } |
| 105 | const at = next.completedAt; |
| 106 | if (!next.settled || at === null || at <= (handled.get(key) ?? -Infinity)) return; |
| 107 | if (next.backgroundWake) { |
| 108 | release(key, at); |
| 109 | return; |
| 110 | } |
| 111 | handled.set(key, at); |
| 112 | notify(key, "completion"); |
| 113 | }, |
| 114 | /** Stops grace timers while the snapshot is stale; the next observation restarts or drops them. */ |
| 115 | pause(): void { |
| 116 | for (const [key, { at, timer }] of releases) { |
| 117 | clearTimeout(timer); |
| 118 | releases.set(key, { at, timer: undefined }); |
| 119 | } |
| 120 | }, |
| 121 | /** Forgets threads that left the snapshot. */ |
| 122 | retain(keys: ReadonlySet<Key>): void { |
| 123 | for (const key of handled.keys()) if (!keys.has(key)) handled.delete(key); |
| 124 | for (const key of releases.keys()) if (!keys.has(key)) cancel(key); |
| 125 | for (const key of attended.keys()) if (!keys.has(key)) attended.delete(key); |
| 126 | }, |
| 127 | clear(): void { |
| 128 | for (const key of releases.keys()) cancel(key); |
| 129 | handled.clear(); |
| 130 | attended.clear(); |
| 131 | }, |
| 132 | }; |
| 133 | } |