Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Add prompt history key navigation

yxlyx63a055325075
diff --git a/gui/src/components/PromptComposer.tsx b/gui/src/components/PromptComposer.tsx
index 93605e6f..142aa8c9 100644
--- a/gui/src/components/PromptComposer.tsx
+++ b/gui/src/components/PromptComposer.tsx
@@ -62,6 +62,9 @@ export function PromptComposer({ binding, onCommandResult }: PromptComposerProps
} = useCommandRouter(binding, { onCommandResult });
const requiresProviderSetup =
promptSettings != null && promptSettings.availableModels.length === 0;
+ const promptHistory = messages
+ .filter((message) => message.kind === "user")
+ .map((message) => message.text);
const handleSubmit = useCallback(async () => {
if (trySubmitAsCommand(promptDraft)) {
@@ -167,6 +170,7 @@ export function PromptComposer({ binding, onCommandResult }: PromptComposerProps
isPlanningMode={isPlanningMode}
promptSettings={promptSettings}
promptDraft={promptDraft}
+ promptHistory={promptHistory}
isInputDisabled={requiresProviderSetup}
binding={binding}
workspacePath={workspacePath}
diff --git a/gui/src/components/PromptInputCard.tsx b/gui/src/components/PromptInputCard.tsx
index aa54e1c5..b3d43c03 100644
--- a/gui/src/components/PromptInputCard.tsx
+++ b/gui/src/components/PromptInputCard.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useState } from "react";
+import { useEffect, useRef, useState, type KeyboardEvent } from "react";
import {
ArrowUpIcon,
ChevronDownIcon,
@@ -39,12 +39,26 @@ import { useDropZone } from "@/hooks/useFileDrop";
import { usePromptModelPicker } from "@/hooks/usePromptModelPicker";
import { savePastedImage, setFast } from "@/services/desktop/client";
import { cn } from "@/utils/cn";
+import {
+ getPromptHistoryNavigationResult,
+ type PromptHistoryCursor,
+} from "./promptHistoryNavigation";
import {
formatPlanningThinkingLabel,
formatReasoningEffortLabel,
} from "@/utils/reasoning";
import type { PromptInputCardProps } from "./types/prompt";
+const EMPTY_PROMPT_HISTORY: string[] = [];
+
+function isCaretOnFirstLine(textarea: HTMLTextAreaElement) {
+ return !textarea.value.slice(0, textarea.selectionStart).includes("\n");
+}
+
+function isCaretOnLastLine(textarea: HTMLTextAreaElement) {
+ return !textarea.value.slice(textarea.selectionEnd).includes("\n");
+}
+
export function PromptInputCard({
canCompose,
isRequestActive,
@@ -53,6 +67,7 @@ export function PromptInputCard({
placeholder = "Ask about this workspace…",
promptSettings,
promptDraft,
+ promptHistory = EMPTY_PROMPT_HISTORY,
focusSignal,
isInputDisabled = false,
binding,
@@ -114,9 +129,20 @@ export function PromptInputCard({
const [fastEnabled, setFastEnabled] = useState(
promptSettings?.fastEnabled ?? false,
);
+ const [historyCursor, setHistoryCursor] =
+ useState<PromptHistoryCursor>(null);
+ const [draftBeforeHistory, setDraftBeforeHistory] = useState("");
+ const promptHistoryLengthRef = useRef(promptHistory.length);
+
+ function resetHistoryNavigation() {
+ setHistoryCursor(null);
+ setDraftBeforeHistory("");
+ }
+
useEffect(() => {
setFastEnabled(promptSettings?.fastEnabled ?? false);
}, [promptSettings?.fastEnabled]);
+
const isCodexModel = selectedModel?.providerId === "codex";
function handleFastToggle() {
@@ -167,9 +193,85 @@ export function PromptInputCard({
return;
}
+ resetHistoryNavigation();
void submitPrompt();
}
+ function handleDraftChange(value: string) {
+ resetHistoryNavigation();
+ setPromptDraft(value);
+ }
+
+ function moveCaretToEndOnNextFrame() {
+ requestAnimationFrame(() => {
+ const textarea = textareaRef.current;
+ if (textarea == null) {
+ return;
+ }
+
+ const end = textarea.value.length;
+ textarea.setSelectionRange(end, end);
+ });
+ }
+
+ function handleHistoryNavigation(
+ event: KeyboardEvent<HTMLTextAreaElement>,
+ ) {
+ if (
+ event.nativeEvent.isComposing ||
+ event.altKey ||
+ event.ctrlKey ||
+ event.metaKey ||
+ event.shiftKey
+ ) {
+ return false;
+ }
+
+ const direction =
+ event.key === "ArrowUp"
+ ? "previous"
+ : event.key === "ArrowDown"
+ ? "next"
+ : null;
+ if (direction == null) {
+ return false;
+ }
+
+ const textarea = event.currentTarget;
+ if (direction === "previous" && !isCaretOnFirstLine(textarea)) {
+ return false;
+ }
+ if (direction === "next" && !isCaretOnLastLine(textarea)) {
+ return false;
+ }
+
+ const didHistoryLengthChange =
+ promptHistoryLengthRef.current !== promptHistory.length;
+ const effectiveCursor = didHistoryLengthChange ? null : historyCursor;
+ const effectiveDraftBeforeHistory = didHistoryLengthChange
+ ? ""
+ : draftBeforeHistory;
+ promptHistoryLengthRef.current = promptHistory.length;
+
+ const result = getPromptHistoryNavigationResult({
+ direction,
+ promptHistory,
+ cursor: effectiveCursor,
+ currentDraft: promptDraft,
+ draftBeforeHistory: effectiveDraftBeforeHistory,
+ });
+ if (result == null) {
+ return false;
+ }
+
+ event.preventDefault();
+ setHistoryCursor(result.cursor);
+ setDraftBeforeHistory(result.draftBeforeHistory);
+ setPromptDraft(result.draft);
+ moveCaretToEndOnNextFrame();
+ return true;
+ }
+
function handlePrimaryAction() {
if (!isSubmitDisabled) {
handleSubmit();
@@ -269,7 +371,7 @@ export function PromptInputCard({
)}
placeholder={isWorking ? "Queue a follow-up…" : placeholder}
value={promptDraft}
- onChange={(event) => setPromptDraft(event.target.value)}
+ onChange={(event) => handleDraftChange(event.target.value)}
onPaste={handlePaste}
onScroll={(event) => {
if (commandOverlayRef.current != null) {
@@ -283,6 +385,9 @@ export function PromptInputCard({
if (commandAutocomplete.handleKeyDown(event)) {
return;
}
+ if (handleHistoryNavigation(event)) {
+ return;
+ }
if (event.key !== "Enter") {
return;
}
diff --git a/gui/src/components/new-chat-screen/NewChatPromptSection.tsx b/gui/src/components/new-chat-screen/NewChatPromptSection.tsx
index e510bf8a..45bf2085 100644
--- a/gui/src/components/new-chat-screen/NewChatPromptSection.tsx
+++ b/gui/src/components/new-chat-screen/NewChatPromptSection.tsx
@@ -68,6 +68,9 @@ export function NewChatPromptSection({
followupRequest == null &&
interrupt == null &&
lastAssistant != null;
+ const promptHistory = messages
+ .filter((message) => message.kind === "user")
+ .map((message) => message.text);
const handleSubmit = useCallback(async () => {
if (trySubmitAsCommand(promptDraft)) {
return;
@@ -131,6 +134,7 @@ export function NewChatPromptSection({
}
isPlanningMode={isPlanningMode}
promptDraft={promptDraft}
+ promptHistory={promptHistory}
promptSettings={promptSettings}
isInputDisabled={!hasCurrentWorkspace}
binding={binding}
diff --git a/gui/src/components/promptHistoryNavigation.test.ts b/gui/src/components/promptHistoryNavigation.test.ts
new file mode 100644
index 00000000..8c17f89c
--- /dev/null
+++ b/gui/src/components/promptHistoryNavigation.test.ts
@@ -0,0 +1,165 @@
+import { expect, test } from "bun:test";
+
+import {
+ getPromptHistoryNavigationResult,
+ type PromptHistoryCursor,
+} from "./promptHistoryNavigation";
+
+test("returns null when navigating previous with empty history", () => {
+ expect(
+ getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory: [],
+ cursor: null,
+ currentDraft: "draft",
+ draftBeforeHistory: "",
+ }),
+ ).toBeNull();
+});
+
+test("returns newest prompt when navigating previous from a draft", () => {
+ expect(
+ getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory: ["first", "second", "third"],
+ cursor: null,
+ currentDraft: "draft in progress",
+ draftBeforeHistory: "",
+ }),
+ ).toEqual({
+ cursor: 2,
+ draft: "third",
+ draftBeforeHistory: "draft in progress",
+ });
+});
+
+test("walks backward and forward through history before restoring draft", () => {
+ const promptHistory = ["first", "second", "third"];
+
+ let cursor: PromptHistoryCursor = null;
+ let draft = "draft in progress";
+ let draftBeforeHistory = "";
+
+ let result = getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory,
+ cursor,
+ currentDraft: draft,
+ draftBeforeHistory,
+ });
+ expect(result).toEqual({
+ cursor: 2,
+ draft: "third",
+ draftBeforeHistory: "draft in progress",
+ });
+
+ cursor = result!.cursor;
+ draft = result!.draft;
+ draftBeforeHistory = result!.draftBeforeHistory;
+
+ result = getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory,
+ cursor,
+ currentDraft: draft,
+ draftBeforeHistory,
+ });
+ expect(result).toEqual({
+ cursor: 1,
+ draft: "second",
+ draftBeforeHistory: "draft in progress",
+ });
+
+ cursor = result!.cursor;
+ draft = result!.draft;
+ draftBeforeHistory = result!.draftBeforeHistory;
+
+ result = getPromptHistoryNavigationResult({
+ direction: "next",
+ promptHistory,
+ cursor,
+ currentDraft: draft,
+ draftBeforeHistory,
+ });
+ expect(result).toEqual({
+ cursor: 2,
+ draft: "third",
+ draftBeforeHistory: "draft in progress",
+ });
+
+ cursor = result!.cursor;
+ draft = result!.draft;
+ draftBeforeHistory = result!.draftBeforeHistory;
+
+ result = getPromptHistoryNavigationResult({
+ direction: "next",
+ promptHistory,
+ cursor,
+ currentDraft: draft,
+ draftBeforeHistory,
+ });
+ expect(result).toEqual({
+ cursor: null,
+ draft: "draft in progress",
+ draftBeforeHistory: "",
+ });
+});
+
+test("keeps previous navigation pinned at oldest prompt", () => {
+ expect(
+ getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory: ["first", "second", "third"],
+ cursor: 0,
+ currentDraft: "first",
+ draftBeforeHistory: "draft in progress",
+ }),
+ ).toEqual({
+ cursor: 0,
+ draft: "first",
+ draftBeforeHistory: "draft in progress",
+ });
+});
+
+test("preserves duplicate prompts as separate history entries", () => {
+ const promptHistory = ["same", "different", "same"];
+
+ let result = getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory,
+ cursor: null,
+ currentDraft: "draft",
+ draftBeforeHistory: "",
+ });
+ expect(result).toEqual({
+ cursor: 2,
+ draft: "same",
+ draftBeforeHistory: "draft",
+ });
+
+ result = getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory,
+ cursor: result!.cursor,
+ currentDraft: result!.draft,
+ draftBeforeHistory: result!.draftBeforeHistory,
+ });
+ expect(result).toEqual({
+ cursor: 1,
+ draft: "different",
+ draftBeforeHistory: "draft",
+ });
+
+ result = getPromptHistoryNavigationResult({
+ direction: "previous",
+ promptHistory,
+ cursor: result!.cursor,
+ currentDraft: result!.draft,
+ draftBeforeHistory: result!.draftBeforeHistory,
+ });
+ expect(result).toEqual({
+ cursor: 0,
+ draft: "same",
+ draftBeforeHistory: "draft",
+ });
+});
diff --git a/gui/src/components/promptHistoryNavigation.ts b/gui/src/components/promptHistoryNavigation.ts
new file mode 100644
index 00000000..2b179a43
--- /dev/null
+++ b/gui/src/components/promptHistoryNavigation.ts
@@ -0,0 +1,59 @@
+export type PromptHistoryCursor = number | null;
+
+export type PromptHistoryNavigationDirection = "previous" | "next";
+
+export interface PromptHistoryNavigationInput {
+ direction: PromptHistoryNavigationDirection;
+ promptHistory: string[];
+ cursor: PromptHistoryCursor;
+ currentDraft: string;
+ draftBeforeHistory: string;
+}
+
+export interface PromptHistoryNavigationResult {
+ cursor: PromptHistoryCursor;
+ draft: string;
+ draftBeforeHistory: string;
+}
+
+export function getPromptHistoryNavigationResult({
+ direction,
+ promptHistory,
+ cursor,
+ currentDraft,
+ draftBeforeHistory,
+}: PromptHistoryNavigationInput): PromptHistoryNavigationResult | null {
+ if (promptHistory.length === 0) {
+ return null;
+ }
+
+ if (direction === "previous") {
+ const nextCursor =
+ cursor == null ? promptHistory.length - 1 : Math.max(0, cursor - 1);
+
+ return {
+ cursor: nextCursor,
+ draft: promptHistory[nextCursor] ?? currentDraft,
+ draftBeforeHistory: cursor == null ? currentDraft : draftBeforeHistory,
+ };
+ }
+
+ if (cursor == null) {
+ return null;
+ }
+
+ const nextCursor = cursor + 1;
+ if (nextCursor >= promptHistory.length) {
+ return {
+ cursor: null,
+ draft: draftBeforeHistory,
+ draftBeforeHistory: "",
+ };
+ }
+
+ return {
+ cursor: nextCursor,
+ draft: promptHistory[nextCursor] ?? currentDraft,
+ draftBeforeHistory,
+ };
+}
diff --git a/gui/src/components/types/prompt.ts b/gui/src/components/types/prompt.ts
index ba640fe9..bfbfe825 100644
--- a/gui/src/components/types/prompt.ts
+++ b/gui/src/components/types/prompt.ts
@@ -33,6 +33,7 @@ export interface PromptInputCardProps {
placeholder?: string;
promptSettings: PromptSettings | null;
promptDraft: string;
+ promptHistory?: string[];
focusSignal?: number;
isInputDisabled?: boolean;
binding?: ChatBinding | null;