char-slop/ai-dots
ai dotfiles
git clone https://git.t4t.associates/char-slop/ai-dots
28ac03c
main
1import type { AssistantMessage } from "@earendil-works/pi-ai" ; 2import { 3AssistantMessageComponent , 4type ExtensionAPI , 5} from "@earendil-works/pi-coding-agent" ; 6import { Markdown , truncateToWidth , type Component } from "@earendil-works/pi-tui" ; 7 8const LINES = 8 ; 9type AssistantInternals = { 10contentContainer :{ children :Component [] }; 11lastMessage ?:AssistantMessage ; 12}; 13type ThinkingMarkdown = { 14defaultTextStyle ?:{ italic ?:boolean }; 15paddingX :number ; 16}; 17type Prototype = typeof AssistantMessageComponent . prototype & { 18setExpanded ?:( expanded :boolean ) => void ; 19}; 20 21export default function ( pi :ExtensionAPI ) { 22const prototype = AssistantMessageComponent . prototype as Prototype ; 23const updateContent = prototype . updateContent ; 24const setExpanded = prototype . setExpanded ; 25const expanded = new WeakSet < AssistantMessageComponent >(); 26 27prototype . updateContent = function ( message ) { 28updateContent . call ( this , message ); 29if ( expanded . has ( this )) return ; 30 31const { children} = ( this as unknown as AssistantInternals ). contentContainer ; 32for ( const child of children ) { 33if ( ! ( child instanceof Markdown )) continue ; 34const markdown = child as unknown as ThinkingMarkdown ; 35if ( ! markdown . defaultTextStyle ?. italic ) continue ; 36const render = child . render . bind ( child ); 37child . render = ( width ) => { 38const lines = render ( width ); 39if ( lines . length <= LINES ) return lines ; 40const indicator = `( ${ lines . length - LINES } earlier lines hidden - ctrl+o to expand)` ; 41const margin = " " . repeat ( markdown . paddingX ); 42return [ 43 ...lines . slice ( - LINES ), 44truncateToWidth ( ` ${ margin } ${ indicator } ${ margin } ` , width , "" , true ), 45]; 46}; 47} 48}; 49 50prototype . setExpanded = function ( value ) { 51value ?expanded . add ( this ) :expanded . delete ( this ); 52setExpanded ?. call ( this , value ); 53const message = ( this as unknown as AssistantInternals ). lastMessage ; 54if ( message ) this . updateContent ( message ); 55}; 56 57pi . on ( "session_shutdown" , () => { 58prototype . updateContent = updateContent ; 59if ( setExpanded ) prototype . setExpanded = setExpanded ; 60else delete prototype . setExpanded ; 61}); 62}