|
| 1 | +import { areUiSessionKeysEquivalent } from "../../lib/sessions/session-key.ts"; |
| 2 | +import type { SessionChatRouteData } from "./route-loader.ts"; |
| 3 | + |
| 4 | +const HANDOFF_TTL_MS = 30_000; |
| 5 | +const MAINTENANCE_MS = 15_000; |
| 6 | +const RETRY_MS = 250; |
| 7 | +const COMPOSER_SELECTOR = ".agent-chat__composer-combobox textarea"; |
| 8 | + |
| 9 | +type PendingHandoff = { |
| 10 | + expiresAt: number; |
| 11 | + sessionKey: string; |
| 12 | + sourceData: SessionChatRouteData; |
| 13 | +}; |
| 14 | + |
| 15 | +export type ChatPaneElement = HTMLElement & { |
| 16 | + active?: boolean; |
| 17 | + paneId?: string; |
| 18 | + sessionKey?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +let pendingHandoff: PendingHandoff | undefined; |
| 22 | + |
| 23 | +function pendingMatches(sessionKey: string, data: SessionChatRouteData): boolean { |
| 24 | + if (!pendingHandoff) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + if (pendingHandoff.expiresAt <= Date.now()) { |
| 28 | + pendingHandoff = undefined; |
| 29 | + return false; |
| 30 | + } |
| 31 | + return ( |
| 32 | + pendingHandoff.sourceData !== data && |
| 33 | + areUiSessionKeysEquivalent(pendingHandoff.sessionKey, sessionKey) |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +export class RouteDraftComposerFocus { |
| 38 | + private timer: number | undefined; |
| 39 | + private readonly host: HTMLElement; |
| 40 | + |
| 41 | + constructor(host: HTMLElement) { |
| 42 | + this.host = host; |
| 43 | + } |
| 44 | + |
| 45 | + rendered( |
| 46 | + data: SessionChatRouteData | undefined, |
| 47 | + activeSessionKey: string | null | undefined, |
| 48 | + consumedData: SessionChatRouteData | null, |
| 49 | + ): boolean { |
| 50 | + const matchesActivePane = Boolean( |
| 51 | + data && |
| 52 | + (activeSessionKey === undefined || |
| 53 | + (activeSessionKey !== null && |
| 54 | + areUiSessionKeysEquivalent(activeSessionKey, data.sessionKey))), |
| 55 | + ); |
| 56 | + if (data && !data.draft && matchesActivePane && pendingMatches(data.sessionKey, data)) { |
| 57 | + pendingHandoff = undefined; |
| 58 | + this.maintain(data.sessionKey); |
| 59 | + } |
| 60 | + return Boolean(data?.draft && consumedData !== data && matchesActivePane); |
| 61 | + } |
| 62 | + |
| 63 | + shouldFocusPane( |
| 64 | + active: boolean, |
| 65 | + hasDraft: unknown, |
| 66 | + sessionKey: string, |
| 67 | + data?: SessionChatRouteData, |
| 68 | + ): boolean { |
| 69 | + return Boolean( |
| 70 | + active && |
| 71 | + data && |
| 72 | + ((hasDraft && data.focusComposer) || (!hasDraft && pendingMatches(sessionKey, data))), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + beforeDraftCleanup(data: SessionChatRouteData): void { |
| 77 | + if (!data.focusComposer) { |
| 78 | + return; |
| 79 | + } |
| 80 | + // Draft cleanup remounts the route page; carry the one-shot focus fact |
| 81 | + // to that final owner so focus does not die with this page instance. |
| 82 | + pendingHandoff = { |
| 83 | + expiresAt: Date.now() + HANDOFF_TTL_MS, |
| 84 | + sessionKey: data.sessionKey, |
| 85 | + sourceData: data, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + private maintain(sessionKey: string): void { |
| 90 | + if (this.timer !== undefined) { |
| 91 | + window.clearTimeout(this.timer); |
| 92 | + this.timer = undefined; |
| 93 | + } |
| 94 | + const deadline = Date.now() + MAINTENANCE_MS; |
| 95 | + let ownedComposer: HTMLTextAreaElement | undefined; |
| 96 | + |
| 97 | + const focusCurrentComposer = () => { |
| 98 | + if (!this.host.isConnected) { |
| 99 | + this.timer = undefined; |
| 100 | + return; |
| 101 | + } |
| 102 | + const pane = [...this.host.querySelectorAll<ChatPaneElement>("openclaw-chat-pane")].find( |
| 103 | + (candidate) => |
| 104 | + candidate.active && |
| 105 | + candidate.sessionKey !== undefined && |
| 106 | + areUiSessionKeysEquivalent(candidate.sessionKey, sessionKey), |
| 107 | + ); |
| 108 | + const composer = pane?.querySelector<HTMLTextAreaElement>(COMPOSER_SELECTOR); |
| 109 | + const activeElement = document.activeElement; |
| 110 | + const focusStillOwned = |
| 111 | + activeElement === ownedComposer || |
| 112 | + activeElement === document.body || |
| 113 | + (activeElement instanceof HTMLElement && !activeElement.isConnected); |
| 114 | + |
| 115 | + if (composer && (!ownedComposer || focusStillOwned)) { |
| 116 | + composer.focus({ preventScroll: true }); |
| 117 | + ownedComposer = composer; |
| 118 | + } else if (ownedComposer && !focusStillOwned) { |
| 119 | + this.timer = undefined; |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if (Date.now() < deadline) { |
| 124 | + this.timer = window.setTimeout(focusCurrentComposer, RETRY_MS); |
| 125 | + } else { |
| 126 | + this.timer = undefined; |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + focusCurrentComposer(); |
| 131 | + } |
| 132 | +} |
0 commit comments