@@ -1758,6 +1758,137 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
17581758 } ) ;
17591759 } , [ ] ) ;
17601760
1761+ // ------------------------------------------------------------------
1762+ // Prompt history
1763+ // ------------------------------------------------------------------
1764+ const promptHistoryEntries = useMemo ( ( ) => {
1765+ const entries : string [ ] = [ ] ;
1766+ for ( const message of activeThread ?. messages ?? [ ] ) {
1767+ if ( message . role !== "user" ) continue ;
1768+ if ( message . text . trim ( ) . length === 0 ) continue ;
1769+ if ( entries [ entries . length - 1 ] === message . text ) continue ;
1770+ entries . push ( message . text ) ;
1771+ }
1772+ return entries ;
1773+ } , [ activeThread ?. messages ] ) ;
1774+ const [ promptHistoryIndex , setPromptHistoryIndex ] = useState < number | null > ( null ) ;
1775+ const promptHistoryRecallRef = useRef < string | null > ( null ) ;
1776+ const promptHistoryDraftRef = useRef ( "" ) ;
1777+
1778+ useEffect ( ( ) => {
1779+ return ( ) => {
1780+ // Leaving this thread (or unmounting) while browsing history: put the
1781+ // saved draft back so the recalled entry doesn't overwrite it.
1782+ if ( promptHistoryRecallRef . current !== null ) {
1783+ setComposerDraftPrompt ( composerDraftTarget , promptHistoryDraftRef . current ) ;
1784+ }
1785+ setPromptHistoryIndex ( null ) ;
1786+ promptHistoryRecallRef . current = null ;
1787+ promptHistoryDraftRef . current = "" ;
1788+ } ;
1789+ } , [ composerDraftTarget , setComposerDraftPrompt ] ) ;
1790+
1791+ // A thread-snapshot reload can shrink the entry list while browsing it.
1792+ useEffect ( ( ) => {
1793+ if ( promptHistoryIndex !== null && promptHistoryIndex >= promptHistoryEntries . length ) {
1794+ promptHistoryRecallRef . current = null ;
1795+ setPromptHistoryIndex ( null ) ;
1796+ }
1797+ } , [ promptHistoryEntries , promptHistoryIndex ] ) ;
1798+
1799+ // Editing a recalled entry (or any external prompt change, e.g. clearing on
1800+ // send) leaves history mode; the recalled text stays behind as a plain draft.
1801+ useEffect ( ( ) => {
1802+ if ( promptHistoryRecallRef . current === null ) return ;
1803+ if ( prompt === promptHistoryRecallRef . current ) return ;
1804+ promptHistoryRecallRef . current = null ;
1805+ setPromptHistoryIndex ( null ) ;
1806+ } , [ prompt ] ) ;
1807+
1808+ const recallPromptHistoryEntry = ( index : number | null ) => {
1809+ const nextPrompt =
1810+ index === null ? promptHistoryDraftRef . current : ( promptHistoryEntries [ index ] ?? "" ) ;
1811+ promptHistoryRecallRef . current = index === null ? null : nextPrompt ;
1812+ setPromptHistoryIndex ( index ) ;
1813+ promptRef . current = nextPrompt ;
1814+ setPrompt ( nextPrompt ) ;
1815+ setComposerCursor ( collapseExpandedComposerCursor ( nextPrompt , nextPrompt . length ) ) ;
1816+ setComposerTrigger ( null ) ;
1817+ } ;
1818+
1819+ // History only activates when the caret cannot move further in the pressed
1820+ // direction, so arrow keys keep their normal movement inside multiline text.
1821+ const caretOnComposerEdgeLine = ( event : KeyboardEvent , edge : "top" | "bottom" ) : boolean => {
1822+ if ( promptRef . current . length === 0 ) return true ;
1823+ // Logical gate first: the caret must be on the first/last hard line. The
1824+ // visual check below can't be trusted alone — empty lines yield zero-size
1825+ // caret rects whose fallback measures the whole block.
1826+ const snapshot = composerEditorRef . current ?. readSnapshot ( ) ;
1827+ if ( ! snapshot ) return false ;
1828+ // snapshot.value is source text, so index it with the expanded cursor —
1829+ // the collapsed cursor counts inline tokens (mentions, skills) as 1 char.
1830+ const beforeCaret = snapshot . value . slice ( 0 , snapshot . expandedCursor ) ;
1831+ const afterCaret = snapshot . value . slice ( snapshot . expandedCursor ) ;
1832+ if ( edge === "top" ? beforeCaret . includes ( "\n" ) : afterCaret . includes ( "\n" ) ) {
1833+ return false ;
1834+ }
1835+ const target = event . target ;
1836+ if ( ! ( target instanceof HTMLElement ) ) return false ;
1837+ const root = target . closest < HTMLElement > ( '[contenteditable="true"]' ) ?? target ;
1838+ const selection = window . getSelection ( ) ;
1839+ if ( ! selection || selection . rangeCount === 0 || ! selection . isCollapsed ) return false ;
1840+ const range = selection . getRangeAt ( 0 ) ;
1841+ let caretRect = range . getBoundingClientRect ( ) ;
1842+ if ( caretRect . height === 0 ) {
1843+ // Collapsed selections on empty lines report a zero rect.
1844+ const container = range . startContainer ;
1845+ const element = container instanceof HTMLElement ? container : container . parentElement ;
1846+ if ( ! element ) return false ;
1847+ caretRect = element . getBoundingClientRect ( ) ;
1848+ }
1849+ // Compare against the first/last block of text rather than the editor box:
1850+ // the box can be taller than its content (min-height, padding).
1851+ const edgeElement = edge === "top" ? root . firstElementChild : root . lastElementChild ;
1852+ const edgeRect = ( edgeElement ?? root ) . getBoundingClientRect ( ) ;
1853+ const threshold = ( caretRect . height || 20 ) / 2 ;
1854+ if ( edge === "top" ) {
1855+ return caretRect . top - edgeRect . top < threshold ;
1856+ }
1857+ return edgeRect . bottom - caretRect . bottom < threshold ;
1858+ } ;
1859+
1860+ const navigateComposerPromptHistory = (
1861+ key : "ArrowUp" | "ArrowDown" ,
1862+ event : KeyboardEvent ,
1863+ ) : boolean => {
1864+ if ( isComposerApprovalState || activePendingProgress || pendingUserInputs . length > 0 ) {
1865+ return false ;
1866+ }
1867+ if ( event . shiftKey || event . altKey || event . metaKey || event . ctrlKey || event . isComposing ) {
1868+ return false ;
1869+ }
1870+ // Terminal-context chips only render as inline placeholders in the draft
1871+ // text; a recalled entry has none, so the chips would vanish from view yet
1872+ // still be appended on send. Keep history off while any are attached.
1873+ if ( composerTerminalContexts . length > 0 ) return false ;
1874+ if ( promptHistoryEntries . length === 0 ) return false ;
1875+ if ( key === "ArrowUp" ) {
1876+ if ( ! caretOnComposerEdgeLine ( event , "top" ) ) return false ;
1877+ const currentIndex = promptHistoryIndex ?? promptHistoryEntries . length ;
1878+ if ( currentIndex === 0 ) return false ;
1879+ if ( promptHistoryIndex === null ) {
1880+ promptHistoryDraftRef . current = promptRef . current ;
1881+ }
1882+ recallPromptHistoryEntry ( currentIndex - 1 ) ;
1883+ return true ;
1884+ }
1885+ if ( promptHistoryIndex === null ) return false ;
1886+ if ( ! caretOnComposerEdgeLine ( event , "bottom" ) ) return false ;
1887+ const nextIndex = promptHistoryIndex + 1 ;
1888+ recallPromptHistoryEntry ( nextIndex >= promptHistoryEntries . length ? null : nextIndex ) ;
1889+ return true ;
1890+ } ;
1891+
17611892 // ------------------------------------------------------------------
17621893 // Callbacks: command key
17631894 // ------------------------------------------------------------------
@@ -1787,6 +1918,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
17871918 return true ;
17881919 }
17891920 }
1921+ // Gate history on the visible popup, not the live-resolved trigger: a
1922+ // recalled entry ending in "@name"/"$20" resolves a trigger at the caret
1923+ // even though no menu is open, which would strand history navigation.
1924+ if ( ( key === "ArrowUp" || key === "ArrowDown" ) && ! composerMenuOpenRef . current ) {
1925+ if ( navigateComposerPromptHistory ( key , event ) ) {
1926+ return true ;
1927+ }
1928+ }
17901929 if (
17911930 key === "Enter" &&
17921931 shouldSubmitComposerOnEnter ( { isMobileViewport, shiftKey : event . shiftKey } )
@@ -2479,6 +2618,16 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24792618 ) }
24802619
24812620 < div className = "relative" >
2621+ { promptHistoryIndex !== null &&
2622+ ! isComposerApprovalState &&
2623+ activePendingProgress === null ? (
2624+ < div
2625+ data-testid = "composer-history-indicator"
2626+ className = "pb-1.5 text-xs text-muted-foreground/70"
2627+ >
2628+ History { promptHistoryIndex + 1 } /{ promptHistoryEntries . length }
2629+ </ div >
2630+ ) : null }
24822631 < ComposerPromptEditor
24832632 editorRef = { composerEditorRef }
24842633 value = {
0 commit comments