Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat: allow retrieving last typed message by pressing up arrow key
bittobydd70464bd8ed
diff --git a/web-app/src/containers/ChatInput.tsx b/web-app/src/containers/ChatInput.tsxindex 82427a6d4..49eda3212 100644--- a/web-app/src/containers/ChatInput.tsx+++ b/web-app/src/containers/ChatInput.tsx@@ -124,6 +124,8 @@ const ChatInput = memo(function ChatInput({const setActiveModels = useAppState((state) => state.setActiveModels)const prompt = usePrompt((state) => state.prompt)const setPrompt = usePrompt((state) => state.setPrompt)+ const addToHistory = usePrompt((state) => state.addToHistory)+ const navigateHistory = usePrompt((state) => state.navigateHistory)const currentThreadId = useThreads((state) => state.currentThreadId)const currentThread = useThreads((state) => state.getCurrentThread())const updateCurrentThreadAssistant = useThreads(@@ -372,6 +374,7 @@ const ChatInput = memo(function ChatInput({}setMessage('')+ addToHistory(prompt)// Use onSubmit prop if available (AI SDK), otherwise create thread and navigateif (onSubmit) {@@ -1650,6 +1653,27 @@ const ChatInput = memo(function ChatInput({}// When Shift+Enter is pressed, a new line is added (default behavior)}+ // Navigate prompt history with Up/Down arrow keys+ if (e.key === 'ArrowUp' && !isComposing) {+ const textarea = e.currentTarget+ const cursorAtStart =+ textarea.selectionStart === 0 &&+ textarea.selectionEnd === 0+ if (cursorAtStart || !prompt) {+ e.preventDefault()+ navigateHistory('up')+ }+ }+ if (e.key === 'ArrowDown' && !isComposing) {+ const textarea = e.currentTarget+ const cursorAtEnd =+ textarea.selectionStart === prompt.length &&+ textarea.selectionEnd === prompt.length+ if (cursorAtEnd) {+ e.preventDefault()+ navigateHistory('down')+ }+ }}}onPaste={handlePaste}placeholder={t('common:placeholder.chatInput')}diff --git a/web-app/src/hooks/__tests__/usePrompt.test.ts b/web-app/src/hooks/__tests__/usePrompt.test.tsindex 4a56cd38b..ccdb8ec67 100644--- a/web-app/src/hooks/__tests__/usePrompt.test.ts+++ b/web-app/src/hooks/__tests__/usePrompt.test.ts@@ -1,101 +1,93 @@-import { describe, it, expect, beforeEach, vi } from 'vitest'+import { describe, it, expect, beforeEach } from 'vitest'import { act, renderHook } from '@testing-library/react'import { usePrompt } from '../usePrompt'describe('usePrompt', () => {beforeEach(() => {- vi.clearAllMocks()- })-- it('should initialize with empty prompt', () => {- const { result } = renderHook(() => usePrompt())-- expect(result.current.prompt).toBe('')- expect(typeof result.current.setPrompt).toBe('function')- })-- it('should update prompt', () => {- const { result } = renderHook(() => usePrompt())-act(() => {- result.current.setPrompt('Hello, world!')+ usePrompt.setState({ prompt: '', promptHistory: [], historyIndex: -1, draftPrompt: '' })})-- expect(result.current.prompt).toBe('Hello, world!')})- it('should clear prompt', () => {+ it('should set and clear prompt', () => {const { result } = renderHook(() => usePrompt())-- act(() => {- result.current.setPrompt('Some text')- })-- expect(result.current.prompt).toBe('Some text')-- act(() => {- result.current.setPrompt('')- })-++ act(() => result.current.setPrompt('Hello'))+ expect(result.current.prompt).toBe('Hello')++ act(() => result.current.resetPrompt())expect(result.current.prompt).toBe('')})- it('should handle multiple prompt updates', () => {- const { result } = renderHook(() => usePrompt())-- act(() => {- result.current.setPrompt('First')- })-- expect(result.current.prompt).toBe('First')-- act(() => {- result.current.setPrompt('Second')- })-- expect(result.current.prompt).toBe('Second')-- act(() => {- result.current.setPrompt('Third')+ describe('prompt history', () => {+ it('should add entries, skip empty/duplicates, and cap at 100', () => {+ const { result } = renderHook(() => usePrompt())++ act(() => {+ result.current.addToHistory('')+ result.current.addToHistory(' ')+ result.current.addToHistory('first')+ result.current.addToHistory('first') // duplicate+ result.current.addToHistory('second')+ })++ expect(result.current.promptHistory).toEqual(['second', 'first'])++ act(() => {+ for (let i = 0; i < 110; i++) result.current.addToHistory(`msg ${i}`)+ })+ expect(result.current.promptHistory.length).toBe(100)})-- expect(result.current.prompt).toBe('Third')- })- it('should handle special characters in prompt', () => {- const { result } = renderHook(() => usePrompt())-- const specialText = 'Hello! @#$%^&*()_+{}|:"<>?[]\\;\',./'-- act(() => {- result.current.setPrompt(specialText)+ it('should navigate up/down and restore draft', () => {+ const { result } = renderHook(() => usePrompt())++ act(() => {+ result.current.addToHistory('first')+ result.current.addToHistory('second')+ result.current.setPrompt('my draft')+ })++ // Up through history+ act(() => result.current.navigateHistory('up'))+ expect(result.current.prompt).toBe('second')++ act(() => result.current.navigateHistory('up'))+ expect(result.current.prompt).toBe('first')++ // Can't go past oldest+ act(() => result.current.navigateHistory('up'))+ expect(result.current.prompt).toBe('first')++ // Down restores+ act(() => result.current.navigateHistory('down'))+ expect(result.current.prompt).toBe('second')++ act(() => result.current.navigateHistory('down'))+ expect(result.current.prompt).toBe('my draft')+ expect(result.current.historyIndex).toBe(-1)})-- expect(result.current.prompt).toBe(specialText)- })- it('should handle multiline prompts', () => {- const { result } = renderHook(() => usePrompt())-- const multilineText = 'Line 1\nLine 2\nLine 3'-- act(() => {- result.current.setPrompt(multilineText)+ it('should do nothing on empty history or no active browsing', () => {+ const { result } = renderHook(() => usePrompt())++ act(() => result.current.setPrompt('text'))+ act(() => result.current.navigateHistory('up'))+ expect(result.current.prompt).toBe('text')++ act(() => result.current.navigateHistory('down'))+ expect(result.current.prompt).toBe('text')})-- expect(result.current.prompt).toBe(multilineText)- })- it('should handle very long prompts', () => {- const { result } = renderHook(() => usePrompt())-- const longText = 'A'.repeat(10000)-- act(() => {- result.current.setPrompt(longText)+ it('should reset history index when user types', () => {+ const { result } = renderHook(() => usePrompt())++ act(() => result.current.addToHistory('old'))+ act(() => result.current.navigateHistory('up'))+ expect(result.current.historyIndex).toBe(0)++ act(() => result.current.setPrompt('typing'))+ expect(result.current.historyIndex).toBe(-1)})-- expect(result.current.prompt).toBe(longText)- expect(result.current.prompt.length).toBe(10000)})})diff --git a/web-app/src/hooks/usePrompt.ts b/web-app/src/hooks/usePrompt.tsindex cd78ace23..1c97ef20f 100644--- a/web-app/src/hooks/usePrompt.ts+++ b/web-app/src/hooks/usePrompt.ts@@ -1,13 +1,81 @@import { create } from 'zustand'+const MAX_HISTORY_SIZE = 100+type PromptStoreState = {prompt: stringsetPrompt: (value: string) => voidresetPrompt: () => void++ // Prompt history for up/down arrow navigation+ promptHistory: string[]+ historyIndex: number+ draftPrompt: string+ addToHistory: (value: string) => void+ navigateHistory: (direction: 'up' | 'down') => void+ resetHistoryNavigation: () => void}-export const usePrompt = create<PromptStoreState>((set) => ({+export const usePrompt = create<PromptStoreState>((set, get) => ({prompt: '',- setPrompt: (value) => set({ prompt: value }),+ setPrompt: (value) => {+ set({ prompt: value })+ // Reset history navigation when user types manually+ if (get().historyIndex !== -1) {+ set({ historyIndex: -1 })+ }+ },resetPrompt: () => set({ prompt: '' }),++ // History state+ promptHistory: [],+ historyIndex: -1,+ draftPrompt: '',++ addToHistory: (value) => {+ const trimmed = value.trim()+ if (!trimmed) return+ const { promptHistory } = get()+ // Avoid consecutive duplicates+ if (promptHistory.length > 0 && promptHistory[0] === trimmed) return+ set({+ promptHistory: [trimmed, ...promptHistory].slice(0, MAX_HISTORY_SIZE),+ historyIndex: -1,+ })+ },++ navigateHistory: (direction) => {+ const { promptHistory, historyIndex, prompt, draftPrompt } = get()+ if (promptHistory.length === 0) return++ if (direction === 'up') {+ const nextIndex = historyIndex + 1+ if (nextIndex >= promptHistory.length) return+ // Save current input as draft when first entering history+ const newDraft = historyIndex === -1 ? prompt : draftPrompt+ set({+ historyIndex: nextIndex,+ draftPrompt: newDraft,+ prompt: promptHistory[nextIndex],+ })+ } else {+ // direction === 'down'+ if (historyIndex <= -1) return+ const nextIndex = historyIndex - 1+ if (nextIndex === -1) {+ // Restore draft+ set({+ historyIndex: -1,+ prompt: draftPrompt,+ })+ } else {+ set({+ historyIndex: nextIndex,+ prompt: promptHistory[nextIndex],+ })+ }+ }+ },++ resetHistoryNavigation: () => set({ historyIndex: -1, draftPrompt: '' }),}))