Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(app): recall the first chat message with ArrowUp (#4400) (#4684)

Louis Beaumonta3ef1bc80865
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-composer-shell.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-composer-shell.ts
index 018574887..e533ddf28 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-composer-shell.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-composer-shell.ts
@@ -143,6 +143,31 @@ export function useChatComposerShellActions({
// Index from the newest message. `null` means the user is editing normally.
const historyIndexRef = useRef<number | null>(null);
+ // Recall source for the arrow-key history. `messageHistory` is derived from
+ // the `messages` state, which lags a send: on the first message of a session
+ // `sendMessage` awaits session/preset init before committing the user
+ // message, so the just-sent text isn't in `messageHistory` yet and recall
+ // silently does nothing until a second message exists (#4400). We append to
+ // this ref synchronously at send time so the message is recallable
+ // immediately, and resync it from `messageHistory` whenever a different
+ // conversation loads.
+ const recallHistoryRef = useRef<string[]>(messageHistory);
+
+ useEffect(() => {
+ const recall = recallHistoryRef.current;
+ // Keep the locally-tracked list only while it is `messageHistory` plus
+ // freshly-sent messages not yet reflected in `messages` (the send lag).
+ // That requires `messageHistory` to be a non-empty prefix of `recall`.
+ const recallExtendsHistory =
+ messageHistory.length > 0 &&
+ messageHistory.length <= recall.length &&
+ messageHistory.every((message, index) => message === recall[index]);
+ if (!recallExtendsHistory) {
+ // A different (or new/empty) conversation loaded — adopt its history.
+ recallHistoryRef.current = messageHistory;
+ }
+ }, [messageHistory]);
+
const sendComposerMessage = useCallback(() => {
if (pendingDocsRef.current.length > 0) return;
if (!input.trim() && pastedImages.length === 0 && attachedDocsRef.current.length === 0) return;
@@ -150,8 +175,14 @@ export function useChatComposerShellActions({
historyIndexRef.current = null;
const chip = connectionChip;
setConnectionChip(null);
+ const outgoing = chip ? buildChipModelContent(chip, input.trim()) : input.trim();
+ if (outgoing) {
+ // Mirror what `messageHistory` stores (message.content) so the resync
+ // prefix check above matches once the message commits.
+ recallHistoryRef.current = [...recallHistoryRef.current, outgoing];
+ }
void sendMessage(
- chip ? buildChipModelContent(chip, input.trim()) : input.trim(),
+ outgoing,
chip ? buildChipDisplayContent(chip, input.trim()) : undefined,
);
}, [
@@ -263,17 +294,18 @@ export function useChatComposerShellActions({
}
const isPlainArrow = !event.shiftKey && !event.altKey && !event.ctrlKey && !event.metaKey;
+ const recallHistory = recallHistoryRef.current;
if (
!mentions.isOpen &&
isPlainArrow &&
event.key === "ArrowUp" &&
(input === "" || historyIndexRef.current !== null)
) {
- const nextIndex = Math.min((historyIndexRef.current ?? -1) + 1, messageHistory.length - 1);
+ const nextIndex = Math.min((historyIndexRef.current ?? -1) + 1, recallHistory.length - 1);
if (nextIndex >= 0) {
event.preventDefault();
historyIndexRef.current = nextIndex;
- setInput(messageHistory[messageHistory.length - 1 - nextIndex]);
+ setInput(recallHistory[recallHistory.length - 1 - nextIndex]);
}
return;
}
@@ -287,7 +319,7 @@ export function useChatComposerShellActions({
event.preventDefault();
const nextIndex = historyIndexRef.current - 1;
historyIndexRef.current = nextIndex >= 0 ? nextIndex : null;
- setInput(nextIndex >= 0 ? messageHistory[messageHistory.length - 1 - nextIndex] : "");
+ setInput(nextIndex >= 0 ? recallHistory[recallHistory.length - 1 - nextIndex] : "");
return;
}
@@ -324,7 +356,6 @@ export function useChatComposerShellActions({
isComposing,
isMac,
input,
- messageHistory,
mentionActions,
mentions,
sendComposerMessage,