@@ -13,6 +13,7 @@ import {
1313 createMemo ,
1414} from "solid-js"
1515import { createStore } from "solid-js/store"
16+ import { makePersisted } from "@solid-primitives/storage"
1617import { createFocusSignal } from "@solid-primitives/active-element"
1718import { useLocal } from "@/context/local"
1819import { ContentPart , DEFAULT_PROMPT , isPromptEqual , Prompt , useSession } from "@/context/session"
@@ -85,6 +86,69 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
8586 popoverIsOpen : false ,
8687 } )
8788
89+ const MAX_HISTORY = 100
90+ const [ history , setHistory ] = makePersisted (
91+ createStore < {
92+ entries : Prompt [ ]
93+ } > ( {
94+ entries : [ ] ,
95+ } ) ,
96+ {
97+ name : "prompt-history.v1" ,
98+ } ,
99+ )
100+ const [ historyIndex , setHistoryIndex ] = createSignal < number > ( - 1 )
101+ const [ savedPrompt , setSavedPrompt ] = createSignal < Prompt | null > ( null )
102+
103+ const clonePromptParts = ( prompt : Prompt ) : Prompt =>
104+ prompt . map ( ( part ) =>
105+ part . type === "text"
106+ ? { ...part }
107+ : {
108+ ...part ,
109+ selection : part . selection ? { ...part . selection } : undefined ,
110+ } ,
111+ )
112+
113+ const promptLength = ( prompt : Prompt ) => prompt . reduce ( ( len , part ) => len + part . content . length , 0 )
114+
115+ const applyHistoryPrompt = ( prompt : Prompt , position : "start" | "end" ) => {
116+ const length = position === "start" ? 0 : promptLength ( prompt )
117+ session . prompt . set ( prompt , length )
118+ requestAnimationFrame ( ( ) => {
119+ editorRef . focus ( )
120+ setCursorPosition ( editorRef , length )
121+ } )
122+ }
123+
124+ const getCaretLineState = ( ) => {
125+ const selection = window . getSelection ( )
126+ if ( ! selection || selection . rangeCount === 0 ) return { collapsed : false , onFirstLine : false , onLastLine : false }
127+ const range = selection . getRangeAt ( 0 )
128+ const rect = range . getBoundingClientRect ( )
129+ const editorRect = editorRef . getBoundingClientRect ( )
130+ const style = window . getComputedStyle ( editorRef )
131+ const paddingTop = parseFloat ( style . paddingTop ) || 0
132+ const paddingBottom = parseFloat ( style . paddingBottom ) || 0
133+ let lineHeight = parseFloat ( style . lineHeight )
134+ if ( ! Number . isFinite ( lineHeight ) ) lineHeight = parseFloat ( style . fontSize ) || 16
135+ const scrollTop = editorRef . scrollTop
136+ let relativeTop = rect . top - editorRect . top - paddingTop + scrollTop
137+ if ( ! Number . isFinite ( relativeTop ) ) relativeTop = scrollTop
138+ relativeTop = Math . max ( 0 , relativeTop )
139+ let caretHeight = rect . height
140+ if ( ! caretHeight || ! Number . isFinite ( caretHeight ) ) caretHeight = lineHeight
141+ const relativeBottom = relativeTop + caretHeight
142+ const contentHeight = Math . max ( caretHeight , editorRef . scrollHeight - paddingTop - paddingBottom )
143+ const threshold = Math . max ( 2 , lineHeight / 2 )
144+
145+ return {
146+ collapsed : selection . isCollapsed ,
147+ onFirstLine : relativeTop <= threshold ,
148+ onLastLine : contentHeight - relativeBottom <= threshold ,
149+ }
150+ }
151+
88152 const [ placeholder , setPlaceholder ] = createSignal ( Math . floor ( Math . random ( ) * PLACEHOLDERS . length ) )
89153
90154 onMount ( ( ) => {
@@ -221,6 +285,11 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
221285 setStore ( "popoverIsOpen" , false )
222286 }
223287
288+ if ( historyIndex ( ) >= 0 ) {
289+ setHistoryIndex ( - 1 )
290+ setSavedPrompt ( null )
291+ }
292+
224293 session . prompt . set ( rawParts , cursorPosition )
225294 }
226295
@@ -296,12 +365,100 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
296365 sessionID : session . id ! ,
297366 } )
298367
368+ const addToHistory = ( prompt : Prompt ) => {
369+ const text = prompt
370+ . map ( ( p ) => p . content )
371+ . join ( "" )
372+ . trim ( )
373+ if ( ! text ) return
374+
375+ const entry = clonePromptParts ( prompt )
376+ const lastEntry = history . entries [ 0 ]
377+ if ( lastEntry ) {
378+ const lastText = lastEntry . map ( ( p ) => p . content ) . join ( "" )
379+ if ( lastText === text ) return
380+ }
381+
382+ setHistory ( "entries" , ( entries ) => [ entry , ...entries ] . slice ( 0 , MAX_HISTORY ) )
383+ }
384+
385+ const navigateHistory = ( direction : "up" | "down" ) => {
386+ const entries = history . entries
387+ const current = historyIndex ( )
388+
389+ if ( direction === "up" ) {
390+ if ( entries . length === 0 ) return false
391+ if ( current === - 1 ) {
392+ setSavedPrompt ( clonePromptParts ( session . prompt . current ( ) ) )
393+ setHistoryIndex ( 0 )
394+ applyHistoryPrompt ( entries [ 0 ] , "start" )
395+ return true
396+ }
397+ if ( current < entries . length - 1 ) {
398+ const next = current + 1
399+ setHistoryIndex ( next )
400+ applyHistoryPrompt ( entries [ next ] , "start" )
401+ return true
402+ }
403+ return false
404+ }
405+
406+ if ( current > 0 ) {
407+ const next = current - 1
408+ setHistoryIndex ( next )
409+ applyHistoryPrompt ( entries [ next ] , "end" )
410+ return true
411+ }
412+ if ( current === 0 ) {
413+ setHistoryIndex ( - 1 )
414+ const saved = savedPrompt ( )
415+ if ( saved ) {
416+ applyHistoryPrompt ( saved , "end" )
417+ setSavedPrompt ( null )
418+ return true
419+ }
420+ applyHistoryPrompt ( DEFAULT_PROMPT , "end" )
421+ return true
422+ }
423+
424+ return false
425+ }
426+
299427 const handleKeyDown = ( event : KeyboardEvent ) => {
300428 if ( store . popoverIsOpen && ( event . key === "ArrowUp" || event . key === "ArrowDown" || event . key === "Enter" ) ) {
301429 onKeyDown ( event )
302430 event . preventDefault ( )
303431 return
304432 }
433+
434+ if ( event . key === "ArrowUp" || event . key === "ArrowDown" ) {
435+ const { collapsed, onFirstLine, onLastLine } = getCaretLineState ( )
436+ if ( ! collapsed ) return
437+ const cursorPos = getCursorPosition ( editorRef )
438+ const textLength = promptLength ( session . prompt . current ( ) )
439+ const inHistory = historyIndex ( ) >= 0
440+ const isStart = cursorPos === 0
441+ const isEnd = cursorPos === textLength
442+ const atAbsoluteStart = onFirstLine && isStart
443+ const atAbsoluteEnd = onLastLine && isEnd
444+ const allowUp = ( inHistory && isEnd ) || atAbsoluteStart
445+ const allowDown = ( inHistory && isStart ) || atAbsoluteEnd
446+
447+ if ( event . key === "ArrowUp" ) {
448+ if ( ! allowUp ) return
449+ if ( navigateHistory ( "up" ) ) {
450+ event . preventDefault ( )
451+ }
452+ return
453+ }
454+
455+ if ( ! allowDown ) return
456+ if ( navigateHistory ( "down" ) ) {
457+ event . preventDefault ( )
458+ }
459+ return
460+ }
461+
305462 if ( event . key === "Enter" && ! event . shiftKey ) {
306463 handleSubmit ( event )
307464 }
@@ -323,6 +480,10 @@ export const PromptInput: Component<PromptInputProps> = (props) => {
323480 return
324481 }
325482
483+ addToHistory ( prompt )
484+ setHistoryIndex ( - 1 )
485+ setSavedPrompt ( null )
486+
326487 let existing = session . info ( )
327488 if ( ! existing ) {
328489 const created = await sdk . client . session . create ( )
0 commit comments