|
| 1 | +import { InteractiveMode } from "@earendil-works/pi-coding-agent"; |
| 2 | + |
| 3 | +import { getUiTweaksPatchState } from "./patch-state.ts"; |
| 4 | + |
| 5 | +const PRESERVE_COMPACTION_HISTORY_PATCH_KEY = Symbol.for( |
| 6 | + "zigai.pi-ui-tweaks.preserve-compaction-history-patched", |
| 7 | +); |
| 8 | + |
| 9 | +type CompactionEvent = { |
| 10 | + readonly type?: unknown; |
| 11 | + readonly aborted?: unknown; |
| 12 | + readonly result?: unknown; |
| 13 | +}; |
| 14 | + |
| 15 | +type ChatContainer = { |
| 16 | + clear: () => void; |
| 17 | +}; |
| 18 | + |
| 19 | +type InteractiveModeCompactionTarget = { |
| 20 | + [PRESERVE_COMPACTION_HISTORY_PATCH_KEY]?: true; |
| 21 | + chatContainer: ChatContainer; |
| 22 | + handleEvent: (event: unknown) => Promise<void>; |
| 23 | + rebuildChatFromMessages: () => void; |
| 24 | +}; |
| 25 | + |
| 26 | +function isObject(value: unknown): value is object { |
| 27 | + return (typeof value === "object" && value !== null) || typeof value === "function"; |
| 28 | +} |
| 29 | + |
| 30 | +function isSuccessfulCompaction(event: unknown): event is CompactionEvent { |
| 31 | + if (!isObject(event)) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + return ( |
| 36 | + Reflect.get(event, "type") === "compaction_end" && |
| 37 | + Reflect.get(event, "aborted") === false && |
| 38 | + Reflect.get(event, "result") !== undefined |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function isInteractiveModeCompactionTarget( |
| 43 | + value: unknown, |
| 44 | +): value is InteractiveModeCompactionTarget { |
| 45 | + if (!isObject(value)) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + const chatContainer: unknown = Reflect.get(value, "chatContainer") as unknown; |
| 50 | + return ( |
| 51 | + isObject(chatContainer) && |
| 52 | + typeof Reflect.get(chatContainer, "clear") === "function" && |
| 53 | + typeof Reflect.get(value, "rebuildChatFromMessages") === "function" |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +function warnPreserveCompactionHistoryPatchUnavailable(reason?: string): void { |
| 58 | + let suffix = ""; |
| 59 | + if (reason !== undefined && reason.length > 0) { |
| 60 | + suffix = `: ${reason}`; |
| 61 | + } |
| 62 | + |
| 63 | + console.warn( |
| 64 | + `[pi-ui-tweaks] preserve compaction history patch unavailable; Pi internals may have changed${suffix}`, |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Sets whether successful live compactions retain the existing rendered transcript. |
| 70 | + */ |
| 71 | +export function setPreserveCompactionHistory(enabled: boolean): void { |
| 72 | + getUiTweaksPatchState().preserveCompactionHistory = enabled; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Installs an idempotent patch that preserves rendered history during live compaction. |
| 77 | + */ |
| 78 | +export function installPreserveCompactionHistoryPatch(prototype?: unknown): void { |
| 79 | + const prototypeValue: unknown = prototype ?? InteractiveMode.prototype; |
| 80 | + if (!isObject(prototypeValue)) { |
| 81 | + warnPreserveCompactionHistoryPatchUnavailable(); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const originalHandleEventValue: unknown = Reflect.get(prototypeValue, "handleEvent") as unknown; |
| 86 | + if (typeof originalHandleEventValue !== "function") { |
| 87 | + warnPreserveCompactionHistoryPatchUnavailable("missing handleEvent"); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // SAFETY: The runtime guard verifies the prototype method before the private |
| 92 | + // InteractiveMode adapter is narrowed to the patch target. |
| 93 | + const targetPrototype = prototypeValue as InteractiveModeCompactionTarget; |
| 94 | + if (targetPrototype[PRESERVE_COMPACTION_HISTORY_PATCH_KEY] === true) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // SAFETY: InteractiveMode.handleEvent is an async runtime method. The wrapper |
| 99 | + // forwards its event unchanged and restores temporary method replacements. |
| 100 | + const originalHandleEvent = |
| 101 | + originalHandleEventValue as InteractiveModeCompactionTarget["handleEvent"]; |
| 102 | + |
| 103 | + targetPrototype.handleEvent = async function patchedHandleEvent( |
| 104 | + this: InteractiveModeCompactionTarget, |
| 105 | + event: unknown, |
| 106 | + ): Promise<void> { |
| 107 | + if ( |
| 108 | + !getUiTweaksPatchState().preserveCompactionHistory || |
| 109 | + !isSuccessfulCompaction(event) || |
| 110 | + !isInteractiveModeCompactionTarget(this) |
| 111 | + ) { |
| 112 | + return originalHandleEvent.call(this, event); |
| 113 | + } |
| 114 | + |
| 115 | + const originalClear = this.chatContainer.clear; |
| 116 | + const originalRebuildChatFromMessages = this.rebuildChatFromMessages; |
| 117 | + this.chatContainer.clear = () => {}; |
| 118 | + this.rebuildChatFromMessages = () => {}; |
| 119 | + |
| 120 | + try { |
| 121 | + await originalHandleEvent.call(this, event); |
| 122 | + } finally { |
| 123 | + this.chatContainer.clear = originalClear; |
| 124 | + this.rebuildChatFromMessages = originalRebuildChatFromMessages; |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + targetPrototype[PRESERVE_COMPACTION_HISTORY_PATCH_KEY] = true; |
| 129 | +} |
0 commit comments