The best way to control Claude Code, Codex and more from any computer or phone
| 1 | export interface ComposerNativeEventSnapshot { |
| 2 | readonly eventCount: number; |
| 3 | readonly value: string; |
| 4 | readonly selection: ComposerEditorSelection | null; |
| 5 | } |
| 6 | |
| 7 | interface ComposerEditorSelection { |
| 8 | readonly start: number; |
| 9 | readonly end: number; |
| 10 | } |
| 11 | |
| 12 | export function acknowledgeComposerNativeEvent( |
| 13 | mostRecentEventCount: number, |
| 14 | incomingEventCount: number, |
| 15 | resetGeneration = 0, |
| 16 | incomingResetGeneration = 0, |
| 17 | ): number | null { |
| 18 | if ( |
| 19 | !Number.isSafeInteger(incomingEventCount) || |
| 20 | incomingEventCount <= mostRecentEventCount || |
| 21 | // Events from before the editor applied a parent reset describe text the parent replaced. |
| 22 | incomingResetGeneration < resetGeneration |
| 23 | ) { |
| 24 | return null; |
| 25 | } |
| 26 | return incomingEventCount; |
| 27 | } |
| 28 | |
| 29 | export function resolveComposerControlledEventCount( |
| 30 | value: string, |
| 31 | selection: ComposerEditorSelection | null, |
| 32 | mostRecentEventCount: number, |
| 33 | snapshots: ReadonlyArray<ComposerNativeEventSnapshot>, |
| 34 | ): number { |
| 35 | let newestValueEventCount: number | null = null; |
| 36 | for (let index = snapshots.length - 1; index >= 0; index -= 1) { |
| 37 | const snapshot = snapshots[index]; |
| 38 | if (snapshot?.value !== value) continue; |
| 39 | |
| 40 | newestValueEventCount ??= snapshot.eventCount; |
| 41 | if (selection === null || snapshotSelectionMatches(snapshot, selection)) { |
| 42 | return snapshot.eventCount; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Mismatched native text/selection pairs are intermediate renders; keep their revision behind to avoid moving the caret. |
| 47 | if (newestValueEventCount !== null && mostRecentEventCount > 0) { |
| 48 | return Math.min(newestValueEventCount, mostRecentEventCount - 1); |
| 49 | } |
| 50 | |
| 51 | return mostRecentEventCount; |
| 52 | } |
| 53 | |
| 54 | // Null-selection snapshots allow assumed bounded carets; echoes must not use that wildcard or drop caret moves. |
| 55 | function snapshotSelectionMatches( |
| 56 | snapshot: ComposerNativeEventSnapshot, |
| 57 | selection: ComposerEditorSelection, |
| 58 | ): boolean { |
| 59 | if (snapshot.selection === null) return true; |
| 60 | return snapshot.selection.start === selection.start && snapshot.selection.end === selection.end; |
| 61 | } |
| 62 | |
| 63 | export function isComposerNativeEcho( |
| 64 | value: string, |
| 65 | selection: ComposerEditorSelection | null, |
| 66 | eventCount: number, |
| 67 | snapshots: ReadonlyArray<ComposerNativeEventSnapshot>, |
| 68 | ): boolean { |
| 69 | for (let index = snapshots.length - 1; index >= 0; index -= 1) { |
| 70 | const snapshot = snapshots[index]; |
| 71 | if ( |
| 72 | snapshot !== undefined && |
| 73 | snapshot.eventCount === eventCount && |
| 74 | snapshot.value === value && |
| 75 | (selection === null || |
| 76 | (snapshot.selection !== null && |
| 77 | snapshot.selection.start === selection.start && |
| 78 | snapshot.selection.end === selection.end)) |
| 79 | ) { |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | /** Replace history with assumed controlled state so returning to old native values counts as fresh parent edits. */ |
| 87 | export function assumeComposerControlledState( |
| 88 | snapshots: ReadonlyArray<ComposerNativeEventSnapshot>, |
| 89 | eventCount: number, |
| 90 | value: string, |
| 91 | ): ComposerNativeEventSnapshot[] { |
| 92 | return [ |
| 93 | { eventCount, value, selection: null }, |
| 94 | ...snapshots.filter((snapshot) => snapshot.eventCount > eventCount), |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | export function pruneAcknowledgedComposerNativeEvents( |
| 99 | snapshots: ReadonlyArray<ComposerNativeEventSnapshot>, |
| 100 | acknowledgedEventCount: number, |
| 101 | ): ComposerNativeEventSnapshot[] { |
| 102 | // Retain the latest acknowledgement so unrelated renders stay echoes and do not reset caret or iOS autocorrect. |
| 103 | let latestAcknowledgedIndex = -1; |
| 104 | for (let index = snapshots.length - 1; index >= 0; index -= 1) { |
| 105 | const snapshot = snapshots[index]; |
| 106 | if (snapshot !== undefined && snapshot.eventCount <= acknowledgedEventCount) { |
| 107 | latestAcknowledgedIndex = index; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | return snapshots.filter( |
| 112 | (snapshot, index) => |
| 113 | index === latestAcknowledgedIndex || snapshot.eventCount > acknowledgedEventCount, |
| 114 | ); |
| 115 | } |